repositories / pi-ext

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

Added
new 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({
8 + cwd: "C:/repo",
9 + sessionManager: {
10 + getLeafId: () => "leaf-1",
11 + getSessionFile: () => "C:/sessions/source.jsonl",
12 + createBranchedSession: (leafId: string) => {
13 + calls.push(leafId);
14 + return "C:/sessions/ui-branch.jsonl";
15 + },
16 + },
17 + });
19 + expect(calls).toEqual(["leaf-1"]);
20 + expect(result).toEqual({
21 + path: "C:/sessions/ui-branch.jsonl",
22 + method: "active-branch",
23 + warning: undefined,
24 + });
25 + });
27 + it("fails actionably when no active leaf exists", () => {
28 + expect(() =>
29 + seedUiSession({
30 + cwd: "C:/repo",
31 + sessionManager: {
32 + getLeafId: () => null,
33 + getSessionFile: () => "C:/sessions/source.jsonl",
34 + createBranchedSession: () => "unused",
35 + },
36 + }),
37 + ).toThrow(/active leaf/);
38 + });
40 + it("uses whole-tree fallback only when explicitly requested", () => {
41 + const result = seedUiSession({
42 + cwd: "C:/repo",
43 + allowWholeTreeFallback: true,
44 + sessionManager: {
45 + getLeafId: () => "leaf-1",
46 + getSessionFile: () => "C:/sessions/source.jsonl",
47 + createBranchedSession: () => undefined,
48 + },
49 + forkWholeTree: (source, cwd) => `fork:${source}:${cwd}`,
50 + });
52 + expect(result).toEqual({
53 + path: "fork:C:/sessions/source.jsonl:C:/repo",
54 + method: "whole-tree",
55 + warning:
56 + "Whole-tree session fork includes abandoned branches; active LLM context remains branch-clean.",
57 + });
58 + });
59 +});

extensions/ui/errors.ts

Modified
4 4 export type UiErrorCode =
5 5 | "server_bind_failed"
6 6 | "no_lan_address"
7 7 | "auth_failed"
8 8 | "runtime_not_active"
9 9 | "runtime_already_active"
10 + | "session_seed_failed"
10 11 | "open_failed"
11 12 | "qr_failed";

extensions/ui/session-seed.ts

Added
new file mode 100644
1 +import { uiError } from "./errors";
3 +export type UiSeedMethod = "active-branch" | "whole-tree";
5 +export type UiSeedResult = {
6 + path: string;
7 + method: UiSeedMethod;
8 + warning?: string;
9 +};
11 +export type UiSeedSessionManager = {
12 + getLeafId(): string | null;
13 + getSessionFile(): string | undefined;
14 + createBranchedSession(leafId: string): string | undefined;
15 +};
17 +export type UiSeedOptions = {
18 + cwd: string;
19 + sessionManager: UiSeedSessionManager;
20 + allowWholeTreeFallback?: boolean;
21 + forkWholeTree?: (sourcePath: string, cwd: string) => string | undefined;
22 +};
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();
29 + if (!leafId) {
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.",
32 + retryable: true,
33 + });
34 + }
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;
42 + if (forked) {
43 + return {
44 + path: forked,
45 + method: "whole-tree",
46 + warning: WHOLE_TREE_WARNING,
47 + };
48 + }
49 + }
51 + throw uiError("session_seed_failed", "Cannot seed UI session", {
52 + action:
53 + "Use a persistent terminal session with an active branch, then run /ui again.",
54 + retryable: true,
55 + });