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
Modified33 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 64 + __test.
takeNewSessionNotes();
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", {
146 + text:
"target note",
149 + await t.session.extensionRunner.
emit({
150 + type:
"session_tree",
152 + newLeafId: manager.
getLeafId(),
155 + expect(
renderLatestWidget()).
toEqual([
156 + " 1 notes · alt+n expand · last: target note",
158 + await noteCommand(
"add next note");
159 + expect(
customEntries(
"notes-state").
at(
-1)).
toEqual({
163 + createdAt: expect.
any(
Number),
167 + it(
"offers to carry pending notes into a new session",
async ()
=> {
168 + t
= await createTestSession({
169 + extensions: [
extensionPath()],
170 + mockUI: { confirm:
true },
172 + await noteCommand(
"add carried note");
173 + await t.session.extensionRunner.
emit({
174 + type:
"session_shutdown",
177 + (t.session.sessionManager
as any).
newSession();
178 + await t.session.extensionRunner.
emit({
179 + type:
"session_start",
183 + expect(
uiArgs(
"confirm").
at(
-1)).
toEqual([
185 + "Copy 1 pending notes into the new session?",
187 + expect(
customEntries(
"notes-state")).
toEqual([
191 + text:
"carried note",
192 + createdAt: expect.
any(
Number),
197 + it(
"discards pending notes when new-session carry is rejected",
async ()
=> {
198 + t
= await createTestSession({
199 + extensions: [
extensionPath()],
200 + mockUI: { confirm:
false },
202 + await noteCommand(
"add old note");
203 + await t.session.extensionRunner.
emit({
204 + type:
"session_shutdown",
207 + (t.session.sessionManager
as any).
newSession();
208 + await t.session.extensionRunner.
emit({
209 + type:
"session_start",
213 + expect(
customEntries(
"notes-state")).
toEqual([]);
214 + expect(
latestWidget()).
toBeUndefined();
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 };
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);
135 +function stashNewSessionNotes(
136 + targetSessionFile
: string | undefined,
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);
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 {
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());
478 + notes.
length === 0 || 480 + !(
await ctx.ui.
confirm(
482 + `Copy ${notes.length} pending notes into the new session?`,
487 + for (
const note
of notes) {
488 + appendEvent(pi, state, {
492 + createdAt:
Date.
now(),
495 + refreshWidget(ctx);
498 + pi.
on(
"session_tree", (_event, ctx)
=> restoreBranch(ctx));
442 501 export const __test
= {
510 + takeNewSessionNotes,