repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

feat(notes): carry notes into new sessions

6fc661560eccf4bdcad9db817c874df65634664f

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (3)

extensions/notes/README.md

Modified
33 33 Pending notes are persisted as `notes-state` custom session entries.
34 34 Custom entries are invisible to the model context.
35 -Notes are branch-local because state is reconstructed from the current session branch.
36 -The widget is compact on reload or resume.
35 +Notes are branch-local because state is reconstructed from the current session branch, including after `/tree` navigation.
36 +After `/new`, Pi asks whether to copy pending notes into the new session.
37 +The handoff is in memory, so it works before either session file exists.
38 +The widget is compact on reload, resume, or tree navigation.
38 -No project-global storage exists.
40 +No persistent project-global storage exists.
39 41 No cross-session note list exists.
40 42 No system-prompt or model-context injection exists.
41 43 No steer mode exists.

extensions/notes/__tests__/harness.test.ts

Modified
63 63 afterEach(() => {
64 + __test.takeNewSessionNotes();
64 65 t?.dispose();
65 66 t = undefined;
137 + it("restores notes after tree navigation", async () => {
138 + t = await createTestSession({ extensions: [extensionPath()] });
139 + await noteCommand("add abandoned note");
141 + const manager = t.session.sessionManager as any;
142 + manager.resetLeaf();
143 + manager.appendCustomEntry("notes-state", {
144 + op: "add",
145 + id: 7,
146 + text: "target note",
147 + createdAt: 1,
148 + });
149 + await t.session.extensionRunner.emit({
150 + type: "session_tree",
151 + oldLeafId: "old",
152 + newLeafId: manager.getLeafId(),
153 + });
155 + expect(renderLatestWidget()).toEqual([
156 + "󰎚 1 notes · alt+n expand · last: target note",
157 + ]);
158 + await noteCommand("add next note");
159 + expect(customEntries("notes-state").at(-1)).toEqual({
160 + op: "add",
161 + id: 8,
162 + text: "next note",
163 + createdAt: expect.any(Number),
164 + });
165 + });
167 + it("offers to carry pending notes into a new session", async () => {
168 + t = await createTestSession({
169 + extensions: [extensionPath()],
170 + mockUI: { confirm: true },
171 + });
172 + await noteCommand("add carried note");
173 + await t.session.extensionRunner.emit({
174 + type: "session_shutdown",
175 + reason: "new",
176 + });
177 + (t.session.sessionManager as any).newSession();
178 + await t.session.extensionRunner.emit({
179 + type: "session_start",
180 + reason: "new",
181 + });
183 + expect(uiArgs("confirm").at(-1)).toEqual([
184 + "Carry notes?",
185 + "Copy 1 pending notes into the new session?",
186 + ]);
187 + expect(customEntries("notes-state")).toEqual([
189 + op: "add",
190 + id: 1,
191 + text: "carried note",
192 + createdAt: expect.any(Number),
193 + },
194 + ]);
195 + });
197 + it("discards pending notes when new-session carry is rejected", async () => {
198 + t = await createTestSession({
199 + extensions: [extensionPath()],
200 + mockUI: { confirm: false },
201 + });
202 + await noteCommand("add old note");
203 + await t.session.extensionRunner.emit({
204 + type: "session_shutdown",
205 + reason: "new",
206 + });
207 + (t.session.sessionManager as any).newSession();
208 + await t.session.extensionRunner.emit({
209 + type: "session_start",
210 + reason: "new",
211 + });
213 + expect(customEntries("notes-state")).toEqual([]);
214 + expect(latestWidget()).toBeUndefined();
215 + });
136 217 it("leaves widget hidden and notifies when toggling with no notes", async () => {
137 218 t = await createTestSession({ extensions: [extensionPath()] });
138 219 await noteCommand("");

extensions/notes/index.ts

Modified
9 9 const WIDGET_KEY = "notes";
10 10 const NOTES_ICON = "󰎚";
11 11 const WIDGET_DEFAULT_WIDTH = 80;
12 +const NEW_SESSION_NOTES = Symbol.for("@bugabinga/pi-ext-notes/new-session");
13 14 type Note = { id: number; text: string };
14 15 type NotesEvent =
123 124 target.nextId = source.nextId;
127 +function newSessionHandoffs(): Map<string | undefined, Note[]> {
128 + const existing = Reflect.get(globalThis, NEW_SESSION_NOTES);
129 + if (existing instanceof Map) return existing;
130 + const handoffs = new Map<string | undefined, Note[]>();
131 + Reflect.set(globalThis, NEW_SESSION_NOTES, handoffs);
132 + return handoffs;
135 +function stashNewSessionNotes(
136 + targetSessionFile: string | undefined,
137 + notes: Note[],
138 +): void {
139 + newSessionHandoffs().set(targetSessionFile, structuredClone(notes));
142 +function takeNewSessionNotes(targetSessionFile?: string): Note[] {
143 + const handoffs = newSessionHandoffs();
144 + const notes = handoffs.get(targetSessionFile) ?? [];
145 + handoffs.delete(targetSessionFile);
146 + if (handoffs.size === 0)
147 + Reflect.deleteProperty(globalThis, NEW_SESSION_NOTES);
148 + return notes;
126 151 function appendEvent(
127 152 pi: ExtensionAPI,
128 153 state: NotesState,
429 454 handler: async (ctx) => toggleWidget(ctx),
432 - pi.on("session_start", async (_event, ctx) => {
457 + function restoreBranch(ctx: ExtensionContext): void {
433 458 replaceState(
434 459 state,
435 460 replayNotes(ctx.sessionManager.getBranch() as SessionEntryLike[]),
437 462 expanded = false;
438 463 refreshWidget(ctx);
466 + pi.on("session_shutdown", (event) => {
467 + if (event.reason === "new") {
468 + stashNewSessionNotes(event.targetSessionFile, pendingNotes(state));
472 + pi.on("session_start", async (event, ctx) => {
473 + restoreBranch(ctx);
474 + if (event.reason !== "new") return;
476 + const notes = takeNewSessionNotes(ctx.sessionManager.getSessionFile());
477 + if (
478 + notes.length === 0 ||
479 + !ctx.hasUI ||
480 + !(await ctx.ui.confirm(
481 + "Carry notes?",
482 + `Copy ${notes.length} pending notes into the new session?`,
483 + ))
484 + ) {
485 + return;
487 + for (const note of notes) {
488 + appendEvent(pi, state, {
489 + op: "add",
490 + id: note.id,
491 + text: note.text,
492 + createdAt: Date.now(),
493 + });
495 + refreshWidget(ctx);
496 + });
498 + pi.on("session_tree", (_event, ctx) => restoreBranch(ctx));
442 501 export const __test = {
448 507 pendingNotes,
449 508 preview,
450 509 replayNotes,
510 + takeNewSessionNotes,
451 511 widgetLines,