pi-ext
bugabingas pi extensions
owned by bugabinga
Add ui active branch session seed
fec4ac48eb6b9efe41e6a8ca1548cd3e8f9ef488
Oliver Krylow <o.krylow@isp-insoft.de> ·
unsigned
Browse filesCompare with HEAD
Changed files (3)
extensions/ui/__tests__/session-seed.test.ts
Addednew file mode 100644 1 +import { describe, expect, it }
from "vitest";
2 +import { seedUiSession }
from "../session-seed";
4 +describe(
"ui session seed", ()
=> {
5 + it(
"creates an exact active-branch session from the current leaf", ()
=> {
6 + const calls
: string[]
= [];
7 + const result
= seedUiSession({
10 + getLeafId: ()
=> "leaf-1",
11 + getSessionFile: ()
=> "C:/sessions/source.jsonl",
12 + createBranchedSession: (leafId
: string)
=> {
14 + return "C:/sessions/ui-branch.jsonl";
19 + expect(calls).
toEqual([
"leaf-1"]);
20 + expect(result).
toEqual({
21 + path:
"C:/sessions/ui-branch.jsonl",
22 + method:
"active-branch",
27 + it(
"fails actionably when no active leaf exists", ()
=> {
32 + getLeafId: ()
=> null,
33 + getSessionFile: ()
=> "C:/sessions/source.jsonl",
34 + createBranchedSession: ()
=> "unused",
37 + ).
toThrow(
/active leaf/);
40 + it(
"uses whole-tree fallback only when explicitly requested", ()
=> {
41 + const result
= seedUiSession({
43 + allowWholeTreeFallback:
true,
45 + getLeafId: ()
=> "leaf-1",
46 + getSessionFile: ()
=> "C:/sessions/source.jsonl",
47 + createBranchedSession: ()
=> undefined,
49 + forkWholeTree: (source, cwd)
=> `fork:${source}:${cwd}`,
52 + expect(result).
toEqual({
53 + path:
"fork:C:/sessions/source.jsonl:C:/repo",
54 + method:
"whole-tree",
56 + "Whole-tree session fork includes abandoned branches; active LLM context remains branch-clean.",
extensions/ui/errors.ts
Modified 4 4 export type UiErrorCode = 5 5 | "server_bind_failed" 8 8 | "runtime_not_active" 9 9 | "runtime_already_active" 10 + | "session_seed_failed" extensions/ui/session-seed.ts
Addednew file mode 100644 1 +import { uiError }
from "./errors";
3 +export type UiSeedMethod = "active-branch" | "whole-tree";
5 +export type UiSeedResult = {
7 + method
: UiSeedMethod;
11 +export type UiSeedSessionManager = {
12 + getLeafId()
: string | null;
13 + getSessionFile()
: string | undefined;
14 + createBranchedSession(leafId
: string)
: string | undefined;
17 +export type UiSeedOptions = {
19 + sessionManager
: UiSeedSessionManager;
20 + allowWholeTreeFallback
?: boolean;
21 + forkWholeTree?: (sourcePath
: string, cwd
: string)
=> string | undefined;
24 +export const WHOLE_TREE_WARNING
= 25 + "Whole-tree session fork includes abandoned branches; active LLM context remains branch-clean.";
27 +export function seedUiSession(options
: UiSeedOptions)
: UiSeedResult {
28 + const leafId
= options.sessionManager.
getLeafId();
30 + throw uiError(
"session_seed_failed",
"Cannot seed UI session: no active leaf", {
31 + action:
"Send at least one message in the terminal session, then run /ui again.",
36 + const branched
= options.sessionManager.
createBranchedSession(leafId);
37 + if (branched)
return { path: branched, method:
"active-branch" };
39 + if (options.allowWholeTreeFallback) {
40 + const source
= options.sessionManager.
getSessionFile();
41 + const forked
= source
? options.
forkWholeTree?.(source, options.cwd)
: undefined;
45 + method:
"whole-tree",
46 + warning: WHOLE_TREE_WARNING,
51 + throw uiError(
"session_seed_failed",
"Cannot seed UI session", {
53 + "Use a persistent terminal session with an active branch, then run /ui again.",