@cad0p/pi-napkin 0.7.1 → 0.7.2-20260819.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,21 @@ 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;
|
|
476
|
+
// Stale-ctx lockdown (issue #95): if a tick ever catches the stale-ctx
|
|
477
|
+
// error (the session_start's ctx was invalidated without the matching
|
|
478
|
+
// session_shutdown clearing this closure's timers — a pi-core edge that
|
|
479
|
+
// the #84/#93 guards cannot see because they key off events this closure
|
|
480
|
+
// never received), the interval would otherwise throw + log on EVERY
|
|
481
|
+
// tick forever. On first detection we disarm the auto interval and log
|
|
482
|
+
// once; a later session_start re-arms everything cleanly.
|
|
483
|
+
let staleCtxDisarmed = false;
|
|
469
484
|
|
|
470
485
|
pi.on("before_provider_request", (event) =>
|
|
471
486
|
applyDistillPromptCacheKey(event.payload),
|
|
@@ -503,15 +518,59 @@ export default function (pi: ExtensionAPI) {
|
|
|
503
518
|
: `${secs}s`;
|
|
504
519
|
}
|
|
505
520
|
|
|
521
|
+
/**
|
|
522
|
+
* True when `err` is pi's stale-ctx error (the ctx captured at
|
|
523
|
+
* session_start was invalidated by a session replacement/reload).
|
|
524
|
+
* Matched by the message pi's ExtensionRunner throws — deliberately
|
|
525
|
+
* substring-based so it survives minor wording drift and stays
|
|
526
|
+
* independent of the internal stack frames.
|
|
527
|
+
*/
|
|
528
|
+
function isStaleCtxError(err: unknown): boolean {
|
|
529
|
+
return (
|
|
530
|
+
err instanceof Error &&
|
|
531
|
+
err.message.includes("stale after session replacement")
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* One-time stale-ctx lockdown (issue #95). If a tick catches the
|
|
537
|
+
* stale-ctx error, the closure's session_start ctx is dead and the
|
|
538
|
+
* #84/#93 event-keyed guards can't see it (the matching
|
|
539
|
+
* session_shutdown never reached this closure). The interval would
|
|
540
|
+
* otherwise throw + log on EVERY tick until the next session_start.
|
|
541
|
+
* Disarm the auto interval on first detection and log once; the next
|
|
542
|
+
* session_start clears the flag and re-arms cleanly.
|
|
543
|
+
*/
|
|
544
|
+
function handleStaleCtxInTick(kind: "auto" | "poll" | "countdown"): void {
|
|
545
|
+
if (staleCtxDisarmed) return;
|
|
546
|
+
staleCtxDisarmed = true;
|
|
547
|
+
if (intervalHandle) {
|
|
548
|
+
clearInterval(intervalHandle);
|
|
549
|
+
intervalHandle = null;
|
|
550
|
+
}
|
|
551
|
+
console.error(
|
|
552
|
+
`[napkin-distill] ${kind} tick hit a stale session ctx; auto-distill disarmed until the next session_start (pi session replaced/reloaded mid-session)`,
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
|
|
506
556
|
pi.on("session_start", async (event, ctx) => {
|
|
507
557
|
// Arm the session liveness guard FIRST — before any early-return path — so
|
|
508
558
|
// a tick queued from a previous session can never observe a dead session
|
|
509
559
|
// while this one is starting (issue #84).
|
|
510
560
|
sessionActive = true;
|
|
561
|
+
// Increment the session generation BEFORE any early return (issue #93): a
|
|
562
|
+
// previous session's tick already queued in the macrotask queue must see
|
|
563
|
+
// a generation mismatch even when this session_start bails on vault/
|
|
564
|
+
// config below.
|
|
565
|
+
sessionGeneration += 1;
|
|
511
566
|
|
|
512
567
|
// Reset to default before any early-return paths below; the persisted state
|
|
513
568
|
// (if any) is re-read further down when distill is enabled for the session.
|
|
514
569
|
autoDistillSuppressed = false;
|
|
570
|
+
// A fresh session_start means this closure has a live session again —
|
|
571
|
+
// clear the stale-ctx lockdown so the new session's timers can run
|
|
572
|
+
// (issue #95).
|
|
573
|
+
staleCtxDisarmed = false;
|
|
515
574
|
|
|
516
575
|
const napkinVault = resolveDistillVault(ctx.cwd);
|
|
517
576
|
if (!napkinVault) return;
|
|
@@ -692,11 +751,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
692
751
|
);
|
|
693
752
|
}
|
|
694
753
|
|
|
754
|
+
// Generation seen by the timers armed by THIS session_start. A queued
|
|
755
|
+
// tick from an earlier session that fires after the next session_start
|
|
756
|
+
// re-armed the flag sees a mismatch and no-ops (issue #93).
|
|
757
|
+
const armedGeneration = sessionGeneration;
|
|
758
|
+
|
|
695
759
|
if (ctx.hasUI && showStatus) {
|
|
696
760
|
countdownHandle = setInterval(() => {
|
|
697
761
|
// Queued tick after session replacement/reload must not touch
|
|
698
762
|
// `uiRef.ui` (stale ctx) — skip the countdown render entirely.
|
|
699
763
|
if (!sessionActive) return;
|
|
764
|
+
if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
|
|
700
765
|
try {
|
|
701
766
|
// `renderIdleStatus` self-guards against in-flight distills; when
|
|
702
767
|
// off it paints `distill: off (session)` (pi dedupes identical
|
|
@@ -705,21 +770,35 @@ export default function (pi: ExtensionAPI) {
|
|
|
705
770
|
} catch (err) {
|
|
706
771
|
// A tick must never take down pi (same invariant as the
|
|
707
772
|
// auto-distill interval tick): any unexpected render error logs
|
|
708
|
-
// and is retried on the next countdown repaint.
|
|
709
|
-
|
|
773
|
+
// and is retried on the next countdown repaint. A stale-ctx
|
|
774
|
+
// error means the session was replaced under us — disarm once
|
|
775
|
+
// instead of spamming (issue #95).
|
|
776
|
+
if (isStaleCtxError(err)) {
|
|
777
|
+
handleStaleCtxInTick("countdown");
|
|
778
|
+
} else {
|
|
779
|
+
console.error("[napkin-distill] countdown tick failed:", err);
|
|
780
|
+
}
|
|
710
781
|
}
|
|
711
782
|
}, IDLE_STATUS_REPAINT_INTERVAL_MS);
|
|
712
783
|
}
|
|
713
784
|
|
|
714
785
|
intervalHandle = setInterval(() => {
|
|
715
786
|
if (!sessionActive) return; // queued tick after replacement/reload → clean no-op
|
|
787
|
+
if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
|
|
716
788
|
if (autoDistillSuppressed) return;
|
|
717
789
|
try {
|
|
718
790
|
runAutoDistill(ctx);
|
|
719
791
|
} catch (err) {
|
|
720
792
|
// A tick must never take down pi: any unexpected error (stale ctx,
|
|
721
|
-
// vault config, spawn) logs and is retried on the next tick.
|
|
722
|
-
|
|
793
|
+
// vault config, spawn) logs and is retried on the next tick. A
|
|
794
|
+
// stale-ctx error means this closure's session was replaced without
|
|
795
|
+
// the matching shutdown reaching us — disarm once instead of
|
|
796
|
+
// throwing+logging on every tick (issue #95).
|
|
797
|
+
if (isStaleCtxError(err)) {
|
|
798
|
+
handleStaleCtxInTick("auto");
|
|
799
|
+
} else {
|
|
800
|
+
console.error("[napkin-distill] auto-distill tick failed:", err);
|
|
801
|
+
}
|
|
723
802
|
}
|
|
724
803
|
}, intervalMs);
|
|
725
804
|
});
|
|
@@ -1289,17 +1368,27 @@ export default function (pi: ExtensionAPI) {
|
|
|
1289
1368
|
}
|
|
1290
1369
|
};
|
|
1291
1370
|
|
|
1371
|
+
// Capture the generation at ARM time so a queued poll tick from an
|
|
1372
|
+
// earlier session that fires after the next session_start re-armed the
|
|
1373
|
+
// flag no-ops before touching its stale ctx (issue #93).
|
|
1374
|
+
const armedGeneration = sessionGeneration;
|
|
1292
1375
|
pollHandle = setInterval(() => {
|
|
1293
1376
|
// Queued stale tick after session replacement/reload → clean no-op.
|
|
1294
1377
|
if (!sessionActive) return;
|
|
1378
|
+
if (armedGeneration !== sessionGeneration) return; // old-session tick → clean no-op (#93)
|
|
1295
1379
|
try {
|
|
1296
1380
|
pollTick();
|
|
1297
1381
|
} catch (err) {
|
|
1298
1382
|
// A tick must never take down pi (same invariant as the auto-distill
|
|
1299
1383
|
// interval tick): in the cached-module reload window an old-session
|
|
1300
1384
|
// tick can pass the re-armed guard and hit the stale ctx — log and
|
|
1301
|
-
// drop (the next session owns all poll state).
|
|
1302
|
-
|
|
1385
|
+
// drop (the next session owns all poll state). A stale-ctx error
|
|
1386
|
+
// disarms the auto interval once instead of spamming (issue #95).
|
|
1387
|
+
if (isStaleCtxError(err)) {
|
|
1388
|
+
handleStaleCtxInTick("poll");
|
|
1389
|
+
} else {
|
|
1390
|
+
console.error("[napkin-distill] distill poll tick failed:", err);
|
|
1391
|
+
}
|
|
1303
1392
|
}
|
|
1304
1393
|
}, DISTILL_POLL_TICK_MS);
|
|
1305
1394
|
}
|
|
@@ -83,9 +83,10 @@
|
|
|
83
83
|
# main, pushes if origin, cleans
|
|
84
84
|
# up worktree+branch — see
|
|
85
85
|
# extensions/distill/distill-prompt.md)
|
|
86
|
-
# 4. validate agent output (
|
|
86
|
+
# 4. validate agent output (unresolved conflicts,
|
|
87
|
+
# HEAD on default,
|
|
87
88
|
# commit count; see
|
|
88
|
-
#
|
|
89
|
+
# validate_no_unresolved_conflicts /
|
|
89
90
|
# validate_head_on_default /
|
|
90
91
|
# validate_commit_count below)
|
|
91
92
|
# 5. salvage if validation fails (force-cleanup worktree+
|
|
@@ -294,12 +295,13 @@ ERROR_LOG="$ERROR_DIR/${TIMESTAMP}-$$-${BRANCH_SHORT}.log"
|
|
|
294
295
|
# failed:<reason> → error.
|
|
295
296
|
OUTCOME_PATH="$ERROR_DIR/${TIMESTAMP}-$$-${BRANCH_SHORT}.outcome"
|
|
296
297
|
|
|
297
|
-
# Pre-distill
|
|
298
|
+
# Pre-distill unresolved-conflict snapshot (CORR-1). Captured right before
|
|
298
299
|
# the agent runs (after START_SHA is recovered from meta.json) so the
|
|
299
|
-
# post-agent
|
|
300
|
-
# from pre-existing ones.
|
|
301
|
-
#
|
|
302
|
-
|
|
300
|
+
# post-agent validate_no_unresolved_conflicts can distinguish
|
|
301
|
+
# agent-induced conflicts from pre-existing ones. A plain shell variable
|
|
302
|
+
# (no tmp file) — `git ls-files -u` output is small and the EXIT trap has
|
|
303
|
+
# nothing to clean up.
|
|
304
|
+
PRE_DISTILL_CONFLICT_FILES=""
|
|
303
305
|
|
|
304
306
|
# Lazy-create error log on first write. Empty file is the "no error" signal.
|
|
305
307
|
ERROR_LOG_TOUCHED=0
|
|
@@ -546,13 +548,13 @@ salvage() {
|
|
|
546
548
|
local hint
|
|
547
549
|
case "$reason" in
|
|
548
550
|
markers-after-agent-exit)
|
|
549
|
-
hint="
|
|
551
|
+
hint="Unresolved merge conflicts landed in the vault. Inspect '$vault' and resolve them with 'git -C $vault mergetool' (or edit the conflicted files + 'git add' them), then commit the resolution. Distill content is recoverable from 'git -C $vault reflog' for ~90 days."
|
|
550
552
|
;;
|
|
551
553
|
pre-existing-markers)
|
|
552
|
-
hint="
|
|
554
|
+
hint="Unresolved merge conflicts were already present in the vault before this distill ran (likely from a prior failed merge or an in-progress manual merge). The agent did NOT introduce them, so this distill was rejected to avoid compounding the corruption. Inspect '$vault' (the error log lists the affected files) and resolve the merge manually — edit the conflicted files, 'git add' them, and commit. Once clean, re-run distill."
|
|
553
555
|
;;
|
|
554
556
|
internal-validator-error)
|
|
555
|
-
hint="The post-distill conflict
|
|
557
|
+
hint="The post-distill conflict validator could NOT run (git ls-files failed — likely a corrupt or unreadable vault git dir). The vault was NOT scanned for unresolved conflicts after the agent exited, so the wrapper cannot say whether the squash commit introduced a conflict state. Inspect '$vault' manually with 'git -C $vault status' and 'git -C $vault ls-files -u'; if clean, the squash is keepable, otherwise resolve the conflicts. Distill content is recoverable from 'git -C $vault reflog' for ~90 days."
|
|
556
558
|
;;
|
|
557
559
|
head-not-on-default)
|
|
558
560
|
hint="Vault HEAD is not on '$DEFAULT_BRANCH'. Run 'git -C $vault checkout $DEFAULT_BRANCH' before the next distill. Distill content is recoverable from 'git -C $vault reflog'."
|
|
@@ -622,184 +624,96 @@ salvage() {
|
|
|
622
624
|
# beyond logging — the salvage path (A4) is the only thing that mutates
|
|
623
625
|
# the worktree/branch on validation failure.
|
|
624
626
|
|
|
625
|
-
#
|
|
626
|
-
#
|
|
627
|
-
#
|
|
628
|
-
#
|
|
629
|
-
#
|
|
630
|
-
#
|
|
631
|
-
#
|
|
632
|
-
#
|
|
633
|
-
#
|
|
634
|
-
#
|
|
635
|
-
#
|
|
636
|
-
#
|
|
637
|
-
#
|
|
638
|
-
#
|
|
639
|
-
#
|
|
640
|
-
#
|
|
641
|
-
#
|
|
642
|
-
# touches; scanning the entire vault would false-positive on user
|
|
643
|
-
# scripts that legitimately discuss markers.
|
|
644
|
-
#
|
|
645
|
-
# Why a `git ls-files` enumeration: gitignored content (e.g. the
|
|
646
|
-
# per-distill `.napkin/distill/` shim dir) is skipped automatically.
|
|
647
|
-
# Per-file `grep -q` invocations are O(N) in tracked .md files; the
|
|
648
|
-
# prior single-pass `xargs grep` was O(1) but couldn't express the
|
|
649
|
-
# per-file all-three-co-present predicate. For vaults with thousands
|
|
650
|
-
# of .md files this is slower but still well under wall-clock
|
|
651
|
-
# budget. CLEAN-A-15 tracks the optimisation opportunity.
|
|
652
|
-
#
|
|
653
|
-
# bash 3.2 portability (macOS default): `while read -d ''` with array
|
|
654
|
-
# `+=` is supported. Process substitution `< <(...)` is also bash 3.2.
|
|
655
|
-
#
|
|
656
|
-
# Used by:
|
|
657
|
-
# - The pre-distill snapshot capture (right before the agent runs)
|
|
658
|
-
# to record which files already had markers, so post-distill
|
|
659
|
-
# validation can distinguish agent-induced markers from
|
|
660
|
-
# pre-existing ones (CORR-1).
|
|
661
|
-
# - validate_no_markers (post-distill), which calls this helper
|
|
662
|
-
# against the same vault and diffs the two snapshots.
|
|
663
|
-
list_marker_files() {
|
|
664
|
-
local vault="$1"
|
|
665
|
-
local output_file="$2"
|
|
666
|
-
local file
|
|
667
|
-
while IFS= read -r -d '' file; do
|
|
668
|
-
# All three markers must appear in the same file. `grep -q`
|
|
669
|
-
# short-circuits on first match, and the `&&` chain stops as
|
|
670
|
-
# soon as one marker type is absent — minimising work in the
|
|
671
|
-
# common no-conflict case.
|
|
672
|
-
if grep -qE '^<{7} ' -- "$vault/$file" 2>/dev/null \
|
|
673
|
-
&& grep -qE '^={7}$' -- "$vault/$file" 2>/dev/null \
|
|
674
|
-
&& grep -qE '^>{7} ' -- "$vault/$file" 2>/dev/null; then
|
|
675
|
-
printf '%s\n' "$file" >> "$output_file"
|
|
676
|
-
fi
|
|
677
|
-
done < <(git -C "$vault" ls-files -z -- '*.md' 2>/dev/null)
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
# validate_no_markers <vault> <pre_distill_marker_files>
|
|
681
|
-
#
|
|
682
|
-
# Searches the vault's tracked `*.md` files for unresolved git conflict
|
|
683
|
-
# markers and classifies any matches against the pre-distill snapshot
|
|
684
|
-
# captured just before the agent ran:
|
|
685
|
-
#
|
|
686
|
-
# pass (rc 0) — no marker-bearing files post-distill
|
|
687
|
-
# fail rc 1 — at least one NEW marker-bearing file (agent-induced)
|
|
688
|
-
# fail rc 2 — only pre-existing marker-bearing files (NOT
|
|
689
|
-
# agent-induced — the user's vault was already in this
|
|
690
|
-
# state; the agent didn't make it worse)
|
|
691
|
-
#
|
|
692
|
-
# rc 1 takes priority when both new and pre-existing markers are
|
|
627
|
+
# validate_no_unresolved_conflicts <vault> <pre_conflict_files>
|
|
628
|
+
#
|
|
629
|
+
# Checks the vault for UNRESOLVED MERGE CONFLICTS via the authoritative
|
|
630
|
+
# git index state (`git ls-files -u`: entries with stages 1/2/3 exist
|
|
631
|
+
# only during an unresolved merge) and classifies any conflicts against
|
|
632
|
+
# the pre-distill snapshot captured just before the agent ran:
|
|
633
|
+
#
|
|
634
|
+
# pass (rc 0) — no unresolved conflicts post-distill
|
|
635
|
+
# fail rc 1 — at least one NEW conflict path (agent-induced)
|
|
636
|
+
# fail rc 2 — only pre-existing conflict paths (NOT agent-induced —
|
|
637
|
+
# the user's vault was already mid-merge; the agent
|
|
638
|
+
# didn't make it worse)
|
|
639
|
+
# fail rc 3 — validator could not run (git ls-files failed);
|
|
640
|
+
# internal-validator-error (graceful degradation; do
|
|
641
|
+
# NOT blame the agent for state nobody observed)
|
|
642
|
+
#
|
|
643
|
+
# rc 1 takes priority when both new and pre-existing conflicts are
|
|
693
644
|
# present: the agent's run made things worse, which is the dominant
|
|
694
|
-
# signal even if it didn't touch the pre-existing files.
|
|
695
|
-
#
|
|
696
|
-
#
|
|
697
|
-
#
|
|
698
|
-
#
|
|
699
|
-
# code
|
|
700
|
-
#
|
|
701
|
-
#
|
|
702
|
-
#
|
|
703
|
-
#
|
|
704
|
-
#
|
|
705
|
-
#
|
|
706
|
-
#
|
|
707
|
-
#
|
|
708
|
-
#
|
|
709
|
-
#
|
|
710
|
-
#
|
|
711
|
-
|
|
712
|
-
#
|
|
713
|
-
# CORR-1: pre-existing markers (from a prior failed run, user error,
|
|
714
|
-
# leftover state from a botched manual merge) used to classify as
|
|
715
|
-
# `markers-after-agent-exit` because the wrapper had no
|
|
716
|
-
# pre-distill marker snapshot. That misled the user into blaming the
|
|
717
|
-
# agent for state it didn't create. Capturing the snapshot at
|
|
718
|
-
# wrapper start lets us differentiate.
|
|
719
|
-
#
|
|
720
|
-
# Marker triple definition (CORR-A-1, SEC-A-7): a real merge conflict
|
|
721
|
-
# always emits ALL THREE marker types in the same file. See
|
|
722
|
-
# list_marker_files for the per-file predicate.
|
|
723
|
-
#
|
|
724
|
-
# Acknowledged tradeoff: a vault that genuinely documents a complete
|
|
725
|
-
# `<<<<<<<` / `=======` / `>>>>>>>` example INSIDE A SINGLE FILE will
|
|
726
|
-
# trip the validator post-distill — but if it ALSO tripped
|
|
727
|
-
# pre-distill, this code path classifies it as `pre-existing-markers`
|
|
728
|
-
# (rc 2) instead of as agent-induced. Users who hit this can escape
|
|
729
|
-
# the markers in their docs (leading space, HTML comments) once and
|
|
730
|
-
# subsequent distills see no markers either pre or post.
|
|
731
|
-
#
|
|
732
|
-
# Why scan post-squash on the vault: the agent merges into its branch
|
|
733
|
-
# inside the worktree, then squashes onto the default branch in the
|
|
734
|
-
# vault. Markers in the agent's branch get squashed into the vault's
|
|
735
|
-
# default branch — they become committed corruption in the vault. The
|
|
736
|
-
# vault working tree is the canonical post-condition.
|
|
737
|
-
validate_no_markers() {
|
|
645
|
+
# signal even if it didn't touch the pre-existing files.
|
|
646
|
+
#
|
|
647
|
+
# Why the index state instead of scanning file contents (CORR-A-1,
|
|
648
|
+
# SEC-A-7, issue #95): a REAL merge conflict is recorded in the index
|
|
649
|
+
# by git itself — no regex can beat that signal. A documented conflict
|
|
650
|
+
# EXAMPLE inside a fenced code block is ordinary committed content, not
|
|
651
|
+
# an unmerged index entry, so it can never false-positive a distill.
|
|
652
|
+
# The old content scan needed fence-aware parsing precisely because it
|
|
653
|
+
# grepped marker TEXT anywhere in tracked *.md files, which is
|
|
654
|
+
# inherently ambiguous with documentation.
|
|
655
|
+
#
|
|
656
|
+
# `git ls-files -u` emits three entries per conflicted path (stages
|
|
657
|
+
# 1/2/3); `cut -f2` isolates the path column and `sort -u` dedupes.
|
|
658
|
+
# The caller passes the pre-distill snapshot as a newline-separated
|
|
659
|
+
# path list (empty string when none was captured), and /dev/null when
|
|
660
|
+
# the pre-capture failed, which collapses to "every post conflict is
|
|
661
|
+
# NEW" and routes to failed:markers-after-agent-exit.
|
|
662
|
+
validate_no_unresolved_conflicts() {
|
|
738
663
|
local vault="$1"
|
|
739
|
-
local
|
|
740
|
-
if [ -z "$vault" ]
|
|
741
|
-
log_error "
|
|
664
|
+
local pre_files="$2"
|
|
665
|
+
if [ -z "$vault" ]; then
|
|
666
|
+
log_error "validate_no_unresolved_conflicts: <vault> is required"
|
|
742
667
|
return 3
|
|
743
668
|
fi
|
|
744
669
|
|
|
745
|
-
local
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
# rc 3 so the caller routes to `failed:internal-validator-error`
|
|
755
|
-
# whose recovery hint truthfully tells the user the validator
|
|
756
|
-
# couldn't run — inspect the vault before relying on the squash.
|
|
757
|
-
log_error "validate_no_markers: mktemp failed for post-distill snapshot — cannot scan vault for conflict markers; routing to internal-validator-error (NOT markers-after-agent-exit) to avoid misattributing unobserved state to the agent"
|
|
670
|
+
local post_files post_rc
|
|
671
|
+
# `ls-files -u` exit status is load-bearing: a git failure must route
|
|
672
|
+
# to rc 3 (internal-validator-error), NOT be mistaken for "no
|
|
673
|
+
# conflicts" (empty output). The pipe to cut/sort would mask git's
|
|
674
|
+
# exit code (the last command's status wins), so capture it first.
|
|
675
|
+
post_files="$(git -C "$vault" ls-files -u 2>/dev/null)"
|
|
676
|
+
post_rc=$?
|
|
677
|
+
if [ "$post_rc" -ne 0 ]; then
|
|
678
|
+
log_error "validate_no_unresolved_conflicts: git ls-files failed (rc $post_rc) — cannot scan vault for unresolved conflicts; routing to internal-validator-error (NOT markers-after-agent-exit) to avoid misattributing unobserved state to the agent"
|
|
758
679
|
return 3
|
|
759
680
|
fi
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
if [ ! -s "$post_file" ]; then
|
|
764
|
-
rm -f "$post_file" 2>/dev/null || true
|
|
681
|
+
post_files="$(printf '%s\n' "$post_files" | cut -f2 | sort -u)"
|
|
682
|
+
if [ -z "$post_files" ]; then
|
|
683
|
+
# No unresolved conflicts post-distill = pass.
|
|
765
684
|
return 0
|
|
766
685
|
fi
|
|
767
686
|
|
|
768
|
-
#
|
|
769
|
-
#
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
local new_marker_files pre_existing_marker_files
|
|
774
|
-
new_marker_files="$(comm -23 <(sort -u "$post_file") <(sort -u "$pre_file" 2>/dev/null) 2>/dev/null || cat "$post_file")"
|
|
775
|
-
pre_existing_marker_files="$(comm -12 <(sort -u "$post_file") <(sort -u "$pre_file" 2>/dev/null) 2>/dev/null || true)"
|
|
776
|
-
|
|
777
|
-
rm -f "$post_file" 2>/dev/null || true
|
|
687
|
+
# Diff against the pre-distill snapshot. `comm` requires sorted input;
|
|
688
|
+
# /dev/null as pre collapses to "every post conflict is NEW".
|
|
689
|
+
local new_conflicts pre_existing_conflicts
|
|
690
|
+
new_conflicts="$(comm -23 <(printf '%s\n' "$post_files") <(printf '%s\n' "$pre_files" 2>/dev/null | sort -u) 2>/dev/null || printf '%s\n' "$post_files")"
|
|
691
|
+
pre_existing_conflicts="$(comm -12 <(printf '%s\n' "$post_files") <(printf '%s\n' "$pre_files" 2>/dev/null | sort -u) 2>/dev/null || true)"
|
|
778
692
|
|
|
779
|
-
if [ -n "$
|
|
780
|
-
# Agent-induced
|
|
693
|
+
if [ -n "$new_conflicts" ]; then
|
|
694
|
+
# Agent-induced conflicts present (alone or alongside pre-existing).
|
|
781
695
|
# Dominant signal: the run made things worse.
|
|
782
|
-
log_error "
|
|
696
|
+
log_error "validate_no_unresolved_conflicts: NEW unresolved merge conflicts (agent-induced) in vault \`$vault\`:"
|
|
783
697
|
while IFS= read -r file; do
|
|
784
698
|
[ -n "$file" ] && log_error " $vault/$file"
|
|
785
|
-
done <<< "$
|
|
786
|
-
if [ -n "$
|
|
787
|
-
log_error "
|
|
699
|
+
done <<< "$new_conflicts"
|
|
700
|
+
if [ -n "$pre_existing_conflicts" ]; then
|
|
701
|
+
log_error "validate_no_unresolved_conflicts: also pre-existing conflicts (present before agent ran) in:"
|
|
788
702
|
while IFS= read -r file; do
|
|
789
703
|
[ -n "$file" ] && log_error " $vault/$file"
|
|
790
|
-
done <<< "$
|
|
704
|
+
done <<< "$pre_existing_conflicts"
|
|
791
705
|
fi
|
|
792
706
|
return 1
|
|
793
707
|
fi
|
|
794
708
|
|
|
795
|
-
# Only pre-existing
|
|
709
|
+
# Only pre-existing conflicts post-distill — the agent didn't touch
|
|
796
710
|
# those files (or touched them but didn't make things worse). Not
|
|
797
711
|
# agent-induced; route to a different reason code so the user can
|
|
798
|
-
#
|
|
799
|
-
log_error "
|
|
712
|
+
# resolve the merge manually.
|
|
713
|
+
log_error "validate_no_unresolved_conflicts: pre-existing unresolved merge conflicts (NOT agent-induced) in vault \`$vault\`:"
|
|
800
714
|
while IFS= read -r file; do
|
|
801
715
|
[ -n "$file" ] && log_error " $vault/$file"
|
|
802
|
-
done <<< "$
|
|
716
|
+
done <<< "$pre_existing_conflicts"
|
|
803
717
|
return 2
|
|
804
718
|
}
|
|
805
719
|
|
|
@@ -987,13 +901,9 @@ cleanup() {
|
|
|
987
901
|
# was the last distill for the vault. ENOTEMPTY (other concurrent
|
|
988
902
|
# distills) and ENOENT (race) are both expected and benign.
|
|
989
903
|
rmdir "$(dirname "$WORKTREE")" 2>/dev/null || true
|
|
990
|
-
#
|
|
991
|
-
#
|
|
992
|
-
#
|
|
993
|
-
# mktemp failed). `rm -f` is idempotent on missing paths.
|
|
994
|
-
if [ -n "${PRE_DISTILL_MARKER_FILES_FILE:-}" ]; then
|
|
995
|
-
rm -f "$PRE_DISTILL_MARKER_FILES_FILE" 2>/dev/null || true
|
|
996
|
-
fi
|
|
904
|
+
# The pre-distill conflict snapshot is a plain variable — nothing to
|
|
905
|
+
# clean up on exit (the old mktemp-backed marker snapshot file is gone;
|
|
906
|
+
# issue #95 simplification).
|
|
997
907
|
# SEC-2 R3: pi_stderr is the agent's stderr capture file (allocated
|
|
998
908
|
# via mktemp before invoking pi). Normal-path cleanup happens inline
|
|
999
909
|
# at the post-agent block, but a SIGTERM-on-grace, OOM kill, or any
|
|
@@ -1193,35 +1103,39 @@ if [ "${NAPKIN_DISTILL_FORCE_CLEANUP:-}" = "1" ]; then
|
|
|
1193
1103
|
exit 1
|
|
1194
1104
|
fi
|
|
1195
1105
|
|
|
1196
|
-
# --- Pre-distill
|
|
1106
|
+
# --- Pre-distill conflict snapshot (CORR-1) -------------------------------
|
|
1197
1107
|
#
|
|
1198
|
-
# Capture
|
|
1199
|
-
#
|
|
1200
|
-
#
|
|
1108
|
+
# Capture the vault's UNRESOLVED MERGE CONFLICTS (index stage entries)
|
|
1109
|
+
# BEFORE the agent runs. Used by validate_no_unresolved_conflicts
|
|
1110
|
+
# post-agent to differentiate:
|
|
1201
1111
|
#
|
|
1202
|
-
# -
|
|
1112
|
+
# - Conflict paths post-distill but NOT pre-distill
|
|
1203
1113
|
# → NEW (agent-induced) → failed:markers-after-agent-exit
|
|
1204
|
-
# -
|
|
1114
|
+
# - Conflict paths pre-distill AND post-distill
|
|
1205
1115
|
# → pre-existing (NOT agent-induced) → failed:pre-existing-markers
|
|
1206
1116
|
#
|
|
1207
|
-
#
|
|
1208
|
-
#
|
|
1209
|
-
#
|
|
1210
|
-
#
|
|
1211
|
-
#
|
|
1212
|
-
#
|
|
1213
|
-
#
|
|
1214
|
-
#
|
|
1215
|
-
#
|
|
1216
|
-
#
|
|
1217
|
-
#
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1117
|
+
# `git ls-files -u` is the authoritative "is this file in a conflicted
|
|
1118
|
+
# merge state" signal: it lists index entries with stages 1/2/3, which
|
|
1119
|
+
# git only writes during an unresolved merge. Documented conflict-marker
|
|
1120
|
+
# EXAMPLES inside fenced code blocks are ordinary committed content, not
|
|
1121
|
+
# unmerged index entries — they never appear here, so they can never
|
|
1122
|
+
# false-positive a distill (issue #95). This replaces the old
|
|
1123
|
+
# marker-text scan (which grepped file contents and needed fence-aware
|
|
1124
|
+
# parsing to avoid flagging documentation).
|
|
1125
|
+
#
|
|
1126
|
+
# Best-effort: if the command fails, the variable is empty and any
|
|
1127
|
+
# post-agent conflict routes to markers-after-agent-exit (treating the
|
|
1128
|
+
# state as unobserved rather than pre-existing). Capture git's exit
|
|
1129
|
+
# status before the pipe masks it (same load-bearing pattern as the
|
|
1130
|
+
# post-distill validator).
|
|
1131
|
+
PRE_DISTILL_CONFLICT_FILES_RAW="$(git -C "$VAULT" ls-files -u 2>/dev/null)"
|
|
1132
|
+
if [ $? -ne 0 ]; then
|
|
1133
|
+
log_error "pre-distill conflict snapshot: git ls-files failed; treating vault as conflict-free (any post-agent conflict will route to markers-after-agent-exit)"
|
|
1134
|
+
PRE_DISTILL_CONFLICT_FILES=""
|
|
1221
1135
|
else
|
|
1222
|
-
|
|
1223
|
-
PRE_DISTILL_MARKER_FILES_FILE="/dev/null"
|
|
1136
|
+
PRE_DISTILL_CONFLICT_FILES="$(printf '%s\n' "$PRE_DISTILL_CONFLICT_FILES_RAW" | cut -f2 | sort -u)"
|
|
1224
1137
|
fi
|
|
1138
|
+
unset PRE_DISTILL_CONFLICT_FILES_RAW
|
|
1225
1139
|
|
|
1226
1140
|
# --- Step: run the agent under a hard timeout (PR #12 architecture) --------
|
|
1227
1141
|
#
|
|
@@ -1295,11 +1209,13 @@ fi
|
|
|
1295
1209
|
#
|
|
1296
1210
|
# Validates the agent's output and writes the appropriate outcome class.
|
|
1297
1211
|
# A3 wires:
|
|
1298
|
-
# -
|
|
1299
|
-
#
|
|
1300
|
-
#
|
|
1301
|
-
#
|
|
1302
|
-
#
|
|
1212
|
+
# - validate_no_unresolved_conflicts
|
|
1213
|
+
# (markers-after-agent-exit when NEW
|
|
1214
|
+
# conflict paths post-agent;
|
|
1215
|
+
# pre-existing-markers when only
|
|
1216
|
+
# pre-existing conflicts — CORR-1;
|
|
1217
|
+
# internal-validator-error when the
|
|
1218
|
+
# validator cannot run — CORR-1 R3 /
|
|
1303
1219
|
# SEC-1 R3)
|
|
1304
1220
|
# - validate_head_on_default (head-not-on-default on fail)
|
|
1305
1221
|
# - validate_commit_count (no-content when 0 commits since startSha)
|
|
@@ -1312,14 +1228,16 @@ fi
|
|
|
1312
1228
|
# outcome sidecar.
|
|
1313
1229
|
#
|
|
1314
1230
|
# Reason codes (from the V3 verification report):
|
|
1315
|
-
# markers-after-agent-exit — V1 fail, NEW
|
|
1316
|
-
#
|
|
1317
|
-
#
|
|
1318
|
-
#
|
|
1319
|
-
#
|
|
1320
|
-
#
|
|
1321
|
-
#
|
|
1322
|
-
#
|
|
1231
|
+
# markers-after-agent-exit — V1 fail, NEW unresolved merge conflicts
|
|
1232
|
+
# in the vault introduced by the agent's
|
|
1233
|
+
# run (git ls-files -u)
|
|
1234
|
+
# pre-existing-markers — V1 fail, ONLY pre-existing unresolved
|
|
1235
|
+
# conflicts in the vault (present before
|
|
1236
|
+
# agent ran; not the agent's fault —
|
|
1237
|
+
# CORR-1)
|
|
1238
|
+
# internal-validator-error — V1 fail, the conflict validator could
|
|
1239
|
+
# NOT scan the vault (git ls-files
|
|
1240
|
+
# failed). Distinct from
|
|
1323
1241
|
# markers-after-agent-exit so the user
|
|
1324
1242
|
# isn't blamed-via-agent for state
|
|
1325
1243
|
# nobody observed (CORR-1 R3 / SEC-1 R3)
|
|
@@ -1371,23 +1289,22 @@ if ! validate_head_on_default "$VAULT" "$DEFAULT_BRANCH"; then
|
|
|
1371
1289
|
exit 1
|
|
1372
1290
|
fi
|
|
1373
1291
|
|
|
1374
|
-
#
|
|
1375
|
-
#
|
|
1376
|
-
# 0 → no
|
|
1377
|
-
# 1 → NEW
|
|
1378
|
-
# 2 → only pre-existing
|
|
1379
|
-
# agent-induced; user must
|
|
1380
|
-
#
|
|
1292
|
+
# Conflict validation (CORR-A-1, SEC-A-7, CORR-1, CORR-1 R3 / SEC-1 R3).
|
|
1293
|
+
# Three rc paths (plus rc 3 for validator failure):
|
|
1294
|
+
# 0 → no unresolved conflicts post-distill, pass
|
|
1295
|
+
# 1 → NEW conflict paths (agent-induced) → markers-after-agent-exit
|
|
1296
|
+
# 2 → only pre-existing conflict paths → pre-existing-markers (NOT
|
|
1297
|
+
# agent-induced; user must resolve the merge manually before next
|
|
1298
|
+
# distill)
|
|
1299
|
+
# 3 → git ls-files failed; validator could not scan →
|
|
1381
1300
|
# internal-validator-error (graceful degradation; do NOT
|
|
1382
1301
|
# blame the agent for state nobody observed — CORR-1 R3 /
|
|
1383
1302
|
# SEC-1 R3)
|
|
1384
1303
|
#
|
|
1385
|
-
# Pass the pre-distill snapshot
|
|
1386
|
-
#
|
|
1387
|
-
#
|
|
1388
|
-
|
|
1389
|
-
# failed:markers-after-agent-exit.
|
|
1390
|
-
validate_no_markers "$VAULT" "$PRE_DISTILL_MARKER_FILES_FILE"
|
|
1304
|
+
# Pass the pre-distill snapshot (newline-separated conflict paths, empty
|
|
1305
|
+
# when none were captured) so validate_no_unresolved_conflicts can do
|
|
1306
|
+
# the diff.
|
|
1307
|
+
validate_no_unresolved_conflicts "$VAULT" "$PRE_DISTILL_CONFLICT_FILES"
|
|
1391
1308
|
MARKER_RC=$?
|
|
1392
1309
|
if [ "$MARKER_RC" -eq 1 ]; then
|
|
1393
1310
|
record_dangling_sha
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
209
|
+
listing += `${path}/${collapsed}\n`;
|
|
202
210
|
if (folder.keywords && folder.keywords.length > 0) {
|
|
203
|
-
|
|
211
|
+
listing += ` keywords: ${folder.keywords.join(", ")}\n`;
|
|
204
212
|
}
|
|
205
|
-
|
|
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
|
-
|
|
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 =
|
|
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