@cad0p/pi-napkin 0.7.1 → 0.7.2-20260818.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -466,6 +466,13 @@ export default function (pi: ExtensionAPI) {
466
466
  // session_shutdown handlers complete BEFORE invalidation (pi lifecycle), so
467
467
  // flipping this flag there makes any queued tick a clean no-op (issue #84).
468
468
  let sessionActive = false;
469
+ // Session generation (issue #93): incremented at every session_start. Each
470
+ // timer captures the generation at ARM time; a queued tick from an earlier
471
+ // session that fires after the next session_start re-armed `sessionActive`
472
+ // sees a mismatch and returns BEFORE touching its (invalidated) ctx — a
473
+ // clean no-op instead of the stale-ctx throw + `tick failed` log that #84's
474
+ // try/catch could only downgrade, not prevent.
475
+ let sessionGeneration = 0;
469
476
 
470
477
  pi.on("before_provider_request", (event) =>
471
478
  applyDistillPromptCacheKey(event.payload),
@@ -508,6 +515,11 @@ export default function (pi: ExtensionAPI) {
508
515
  // a tick queued from a previous session can never observe a dead session
509
516
  // while this one is starting (issue #84).
510
517
  sessionActive = true;
518
+ // Increment the session generation BEFORE any early return (issue #93): a
519
+ // previous session's tick already queued in the macrotask queue must see
520
+ // a generation mismatch even when this session_start bails on vault/
521
+ // config below.
522
+ sessionGeneration += 1;
511
523
 
512
524
  // Reset to default before any early-return paths below; the persisted state
513
525
  // (if any) is re-read further down when distill is enabled for the session.
@@ -692,11 +704,17 @@ export default function (pi: ExtensionAPI) {
692
704
  );
693
705
  }
694
706
 
707
+ // Generation seen by the timers armed by THIS session_start. A queued
708
+ // tick from an earlier session that fires after the next session_start
709
+ // re-armed the flag sees a mismatch and no-ops (issue #93).
710
+ const armedGeneration = sessionGeneration;
711
+
695
712
  if (ctx.hasUI && showStatus) {
696
713
  countdownHandle = setInterval(() => {
697
714
  // Queued tick after session replacement/reload must not touch
698
715
  // `uiRef.ui` (stale ctx) — skip the countdown render entirely.
699
716
  if (!sessionActive) return;
717
+ if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
700
718
  try {
701
719
  // `renderIdleStatus` self-guards against in-flight distills; when
702
720
  // off it paints `distill: off (session)` (pi dedupes identical
@@ -713,6 +731,7 @@ export default function (pi: ExtensionAPI) {
713
731
 
714
732
  intervalHandle = setInterval(() => {
715
733
  if (!sessionActive) return; // queued tick after replacement/reload → clean no-op
734
+ if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
716
735
  if (autoDistillSuppressed) return;
717
736
  try {
718
737
  runAutoDistill(ctx);
@@ -1289,9 +1308,14 @@ export default function (pi: ExtensionAPI) {
1289
1308
  }
1290
1309
  };
1291
1310
 
1311
+ // Capture the generation at ARM time so a queued poll tick from an
1312
+ // earlier session that fires after the next session_start re-armed the
1313
+ // flag no-ops before touching its stale ctx (issue #93).
1314
+ const armedGeneration = sessionGeneration;
1292
1315
  pollHandle = setInterval(() => {
1293
1316
  // Queued stale tick after session replacement/reload → clean no-op.
1294
1317
  if (!sessionActive) return;
1318
+ if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
1295
1319
  try {
1296
1320
  pollTick();
1297
1321
  } catch (err) {
@@ -188,9 +188,17 @@ function getOverview(n: Napkin): { text: string; root: string } | null {
188
188
  const overview = n.overview();
189
189
  if (!overview) return null;
190
190
 
191
- let text = overview.context || "";
191
+ // The tags delimit the injected blocks so the provider's merge of
192
+ // consecutive user-role turns keeps vault context distinguishable from
193
+ // the user's actual message. Constant tag text keeps the prompt-cache
194
+ // prefix stable.
195
+ const blocks: string[] = [];
196
+ const context = overview.context?.trim();
197
+ if (context) {
198
+ blocks.push(`<napkin-context>\n${context}\n</napkin-context>`);
199
+ }
192
200
  if (overview.overview && overview.overview.length > 0) {
193
- text += "\n\n";
201
+ let listing = "";
194
202
  for (const folder of overview.overview) {
195
203
  const collapsed = folder.collapsedFolders
196
204
  ? ` (+${folder.collapsedFolders} similar subfolders)`
@@ -198,19 +206,22 @@ function getOverview(n: Napkin): { text: string; root: string } | null {
198
206
  // The SDK reports the vault root as "/"; the CLI renders it as
199
207
  // "./". Mirror that here — "//" looked like a broken path.
200
208
  const path = folder.path === "/" ? "." : folder.path;
201
- text += `${path}/${collapsed}\n`;
209
+ listing += `${path}/${collapsed}\n`;
202
210
  if (folder.keywords && folder.keywords.length > 0) {
203
- text += ` keywords: ${folder.keywords.join(", ")}\n`;
211
+ listing += ` keywords: ${folder.keywords.join(", ")}\n`;
204
212
  }
205
- text += ` notes: ${folder.notes}\n`;
213
+ listing += ` notes: ${folder.notes}\n`;
206
214
  }
207
215
  if (overview.truncated) {
208
216
  // maxRows cap in the SDK dropped the tail of the sorted listing;
209
217
  // tell the agent the vault is bigger than what's shown.
210
- text += `\n… ${overview.truncated.rows} more folders (${overview.truncated.notes} notes) — use kb_search to find specific content\n`;
218
+ listing += `\n… ${overview.truncated.rows} more folders (${overview.truncated.notes} notes) — use kb_search to find specific content\n`;
211
219
  }
220
+ blocks.push(
221
+ `<napkin-overview>\n${listing.trimEnd()}\n</napkin-overview>`,
222
+ );
212
223
  }
213
- const body = text.trim();
224
+ const body = blocks.join("\n\n").trim();
214
225
  if (!body) return null;
215
226
  return { text: body, root: overview.root };
216
227
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cad0p/pi-napkin",
3
- "version": "0.7.1",
3
+ "version": "0.7.2-20260818.0",
4
4
  "description": "📜 Napkin integration for pi — vault context, knowledge tools, and automatic distillation with git-worktree concurrency safety",
5
5
  "type": "module",
6
6
  "exports": {