pi-mega-compact 0.20.87 → 0.20.88

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.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * context-handler/injectionConfirm.fixture.ts — shared fixtures for the 3WF-4
3
+ * InjectionConfirm tests.
4
+ *
5
+ * Split out so each test file stays under the extensions/300-soft-cap the way
6
+ * src/recall/recall3wf.fixture.ts does for 3WF-3. These are REAL fixtures, not
7
+ * mocks/stubs: a REAL VectorStore over a temp stateDir with REAL checkpoints
8
+ * persisted via compactSession; the MegaRuntime is a minimal typed stub exposing
9
+ * only the fields confirmInjection touches (store, pendingRecallBlock,
10
+ * pendingMemoryRecallBlock, appendEvent), matching the triggerGuard/thrashGuard
11
+ * test conventions.
12
+ */
13
+ import { mkdtempSync } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+ import { VectorStore } from "../../../src/vectorStore.js";
17
+ import { compactSession } from "../../../src/engine.js";
18
+ /** Real EngineMessage fixture. */
19
+ export function msg(role, text) {
20
+ return { role, text };
21
+ }
22
+ /** A user-role AgentMessage carrying `text` (the tail-block shape). */
23
+ export function userMsg(text) {
24
+ return { role: "user", content: text, timestamp: 1 };
25
+ }
26
+ /** Fresh isolated state dir per VectorStore. */
27
+ export function freshStore() {
28
+ const dir = mkdtempSync(join(tmpdir(), "mc-inject-"));
29
+ return { store: new VectorStore({ dedupSim: 0.9, stateDir: dir }), dir };
30
+ }
31
+ /** Persist N distinct checkpoints with ascending timestamps. */
32
+ export function seed(store, topics, sid = "sess_inject") {
33
+ topics.forEach((t, i) => {
34
+ compactSession({
35
+ sessionId: sid,
36
+ messages: [msg("user", t), msg("assistant", "ok")],
37
+ keepFrom: 2,
38
+ timestamp: i + 1,
39
+ }, store);
40
+ });
41
+ }
42
+ /** Minimal MegaRuntime stub exposing only the confirmInjection touch-points. */
43
+ export function runtimeStub(store, over = {}) {
44
+ const events = [];
45
+ const runtime = {
46
+ store,
47
+ pendingRecallBlock: over.pendingRecallBlock,
48
+ pendingMemoryRecallBlock: over.pendingMemoryRecallBlock,
49
+ perfTurnStart: undefined,
50
+ rt: { recallInjectedThisTurn: false },
51
+ appendEvent: (name, payload) => {
52
+ events.push({ name, payload });
53
+ },
54
+ };
55
+ return { runtime, events };
56
+ }
57
+ /** Config stub: only the flags confirmInjection reads. */
58
+ export function configStub(over = {}) {
59
+ return {
60
+ threeWayFailback: over.threeWayFailback ?? true,
61
+ recallTailInject: over.recallTailInject ?? true,
62
+ };
63
+ }
@@ -0,0 +1,107 @@
1
+ import { vectorList } from "../../../src/vectorStore.js";
2
+ import { normalizeSessionId } from "../../../src/store.js";
3
+ import { buildFloorBlock } from "../../../src/failback/floor.js";
4
+ import { withRecallTail } from "../recall-tail.js";
5
+ import { messageContentText } from "./messageText.js";
6
+ /**
7
+ * The marker substring used to locate a staged block inside a message. The
8
+ * block's first non-empty line, capped, so prompt reshapes (cache striping /
9
+ * message separation) that regroup messages cannot defeat the match, while a
10
+ * genuinely dropped block still fails it.
11
+ */
12
+ export function blockMarker(block) {
13
+ const line = block
14
+ .split("\n")
15
+ .map((l) => l.trim())
16
+ .find((l) => l.length > 0);
17
+ return (line ?? "").slice(0, 80);
18
+ }
19
+ /**
20
+ * PURE decision function: did the staged block land in this view, and if not,
21
+ * which rung should repair it? Takes the already-extracted message texts so it
22
+ * stays free of pi types and is directly unit-testable.
23
+ */
24
+ export function decideInjection(input, messageTexts, hasPendingBlocks) {
25
+ const marker = input.staged ? blockMarker(input.staged) : "";
26
+ // Nothing staged => nothing to assert; NOT a miss. Injecting a floor here
27
+ // would push provenance text into sessions that never had recall to lose.
28
+ if (!marker)
29
+ return { landed: true, recovered: "none" };
30
+ const landed = messageTexts.some((t) => t.includes(marker));
31
+ if (landed)
32
+ return { landed: true, recovered: "none" };
33
+ // Absent: recompose when the runtime still holds pending blocks, else floor.
34
+ return { landed: false, recovered: hasPendingBlocks ? "recomposed" : "floor" };
35
+ }
36
+ /** Append `text` as a user-role tail message (same shape as recall-tail.ts). */
37
+ function withFloorTail(view, text) {
38
+ const tailMsg = {
39
+ role: "user",
40
+ content: text,
41
+ timestamp: Date.now(),
42
+ };
43
+ return { messages: [...view.messages, tailMsg] };
44
+ }
45
+ /**
46
+ * Thin caller: verify (and if needed repair) one composed view. Returns the view
47
+ * to actually return from the handler. `sessionId` sources the floor checkpoints.
48
+ *
49
+ * The recompose rung deliberately re-appends via `withRecallTail` onto the
50
+ * ALREADY-COMPOSED view rather than re-running `buildTailResult`: the reshape
51
+ * stages (cache striping / message separation) are the realistic way a tail
52
+ * message gets regrouped away, and re-running the same composition would
53
+ * reproduce the same loss. Appending after the reshape is the actual repair, and
54
+ * it keeps the PREVENT-PI-001/002 tail-append invariant (a single user-role
55
+ * message after a complete prefix can never split a toolCall/toolResult pair).
56
+ */
57
+ export function confirmInjection(runtime, config, view, sessionId) {
58
+ try {
59
+ if (!config.threeWayFailback)
60
+ return view;
61
+ // What the tail composition was supposed to inject. BOTH staged blocks
62
+ // count: withRecallTail joins recall + memory blocks into one tail
63
+ // message, so either one going missing is a real injection failure.
64
+ const staged = runtime.pendingRecallBlock ?? runtime.pendingMemoryRecallBlock ?? null;
65
+ // Can the recompose rung actually re-append? Only when a block is still
66
+ // staged on the runtime. When it is not (blocks consumed between
67
+ // composition and this check), or when withRecallTail declines to append,
68
+ // the ladder falls through to the floor rung below.
69
+ const hasPending = runtime.pendingRecallBlock != null ||
70
+ runtime.pendingMemoryRecallBlock != null;
71
+ // Legacy prepend mode: the block is not expected in the message list —
72
+ // verify our composed return value contains it instead (A3 degrade path).
73
+ if (!config.recallTailInject) {
74
+ const composed = view.messages.map(messageContentText).join("\n");
75
+ const marker = staged ? blockMarker(staged) : "";
76
+ runtime.appendEvent("injection_confirmed", {
77
+ mode: "prepend",
78
+ landed: marker ? composed.includes(marker) : true,
79
+ });
80
+ return view;
81
+ }
82
+ const verdict = decideInjection({ staged, tailMode: true }, view.messages.map(messageContentText), hasPending);
83
+ if (verdict.landed) {
84
+ runtime.appendEvent("injection_confirmed", { mode: "tail", landed: true });
85
+ return view;
86
+ }
87
+ if (verdict.recovered === "recomposed") {
88
+ const rebuilt = withRecallTail(view.messages, runtime, config);
89
+ // withRecallTail returns the input array unchanged on failure; only
90
+ // treat a genuine append as a recovery.
91
+ if (rebuilt.length > view.messages.length) {
92
+ runtime.appendEvent("injection_recovered", { via: "recomposed" });
93
+ return { messages: rebuilt };
94
+ }
95
+ }
96
+ const floor = buildFloorBlock(vectorList(runtime.store, normalizeSessionId(sessionId)));
97
+ runtime.appendEvent("injection_recovered", {
98
+ via: "floor",
99
+ basis: floor.basis,
100
+ });
101
+ return withFloorTail(view, floor.text);
102
+ }
103
+ catch {
104
+ // Non-fatal: return the unverified view (pre-sprint behavior).
105
+ return view;
106
+ }
107
+ }
@@ -2,6 +2,7 @@ import { normalizeSessionId } from "../../../src/store.js";
2
2
  import { recall } from "../../../src/engine.js";
3
3
  import { formatRecallBlock } from "../../../src/recall.js";
4
4
  import { vectorStats, vectorList } from "../../../src/vectorStore.js";
5
+ import { buildFloorBlock as sharedFloorBlock, FLOOR_UNAVAILABLE_TEXT, } from "../../../src/failback/floor.js";
5
6
  import { recentUserQuery } from "../../mega-runtime.js";
6
7
  /** One-shot completion marker per MegaRuntime (dies with the runtime). */
7
8
  const guardDone = new WeakMap();
@@ -61,26 +62,19 @@ export function runTriggerGuard(runtime, config, ctx) {
61
62
  /* never throws; best-effort guard */
62
63
  }
63
64
  }
64
- /** Build the provenance floor string from the session's newest checkpoint. */
65
+ /**
66
+ * Build the provenance floor string from the session's newest checkpoint.
67
+ *
68
+ * 3WF-4: the text construction moved to the SHARED pure builder
69
+ * (src/failback/floor.ts) — this keeps the store read (`vectorList`, unfiltered,
70
+ * exactly as 3WF-1 shipped) and the string return type, so output is
71
+ * byte-identical to the pre-refactor version.
72
+ */
65
73
  function buildFloorBlock(runtime, sid) {
66
74
  try {
67
- const cps = vectorList(runtime.store, sid);
68
- let newest = cps[0];
69
- for (const cp of cps) {
70
- if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0))
71
- newest = cp;
72
- }
73
- const summary = newest?.summary?.trim();
74
- if (summary) {
75
- return ("The following compacted context is the most recent checkpoint from " +
76
- "this session (recall found no query-relevant match):\n\n" + summary);
77
- }
78
- return ("This session has compacted context but recall could not surface a " +
79
- "checkpoint relevant to the current request; the most recent checkpoint " +
80
- "summary is unavailable.");
75
+ return sharedFloorBlock(vectorList(runtime.store, sid)).text;
81
76
  }
82
77
  catch {
83
- return ("This session has compacted context but recall could not surface a " +
84
- "checkpoint relevant to the current request.");
78
+ return FLOOR_UNAVAILABLE_TEXT;
85
79
  }
86
80
  }
@@ -2,6 +2,7 @@ import { estimateSessionTokens } from "../../src/tokens.js";
2
2
  import { piCompactWouldNoop } from "../mega-pipeline.js";
3
3
  import { buildTailResult } from "./context-handler/tailResult.js";
4
4
  import { runTriggerGuard } from "./context-handler/triggerGuard.js";
5
+ import { confirmInjection } from "./context-handler/injectionConfirm.js";
5
6
  import { persistEpochAndMaintain } from "./context-handler/afterCompact.js";
6
7
  import { appendMirrorAndLedger } from "./context-handler/dbMirrorAppend.js";
7
8
  import { evaluateGate, thrashGuardBlocks } from "./context-handler/gateCheck.js";
@@ -43,7 +44,22 @@ export function registerContextHandler(pi, runtime, config) {
43
44
  // tail message at any view-return point. Returns undefined when nothing
44
45
  // is staged (or the flag is OFF) so the caller falls through to its
45
46
  // normal return.
46
- const tailResult = buildTailResult(runtime, config, messages);
47
+ const composeTail = buildTailResult(runtime, config, messages);
48
+ // 3WF-4 InjectionConfirm: wrap the tail factory so EVERY return point of
49
+ // this handler (gate / replay / debounce / thrash-guard / pipeline /
50
+ // live-trim) is verified — the staged block's marker must be present in
51
+ // the message list pi will send (tail mode), else we re-compose from the
52
+ // runtime's pending blocks and finally fall back to the shared floor.
53
+ // A composition that yields nothing staged (undefined) is passed through
54
+ // untouched, so flag-OFF and no-recall paths are byte-identical.
55
+ const tailResult = config.threeWayFailback
56
+ ? (msgs) => {
57
+ const view = composeTail(msgs);
58
+ if (!view)
59
+ return view;
60
+ return confirmInjection(runtime, config, view, ctx.sessionManager.getSessionId());
61
+ }
62
+ : composeTail;
47
63
  // Always track context for the dashboard/widget, even when auto is off.
48
64
  // (v0.8 regression: !config.auto gate sat above this, leaving ctx stats
49
65
  // null -> widget '?% / ?/?' when auto disabled. Track first, THEN gate.)
@@ -0,0 +1,35 @@
1
+ /** Floor text when the newest checkpoint summary is available (prefix). */
2
+ const WITH_SUMMARY_PREFIX = "The following compacted context is the most recent checkpoint from " +
3
+ "this session (recall found no query-relevant match):\n\n";
4
+ /** Floor text when checkpoints exist but no usable summary does. */
5
+ const NO_SUMMARY_TEXT = "This session has compacted context but recall could not surface a " +
6
+ "checkpoint relevant to the current request; the most recent checkpoint " +
7
+ "summary is unavailable.";
8
+ /** Floor text when the checkpoint read itself failed (hard last resort). */
9
+ export const FLOOR_UNAVAILABLE_TEXT = "This session has compacted context but recall could not surface a " +
10
+ "checkpoint relevant to the current request.";
11
+ /** The newest checkpoint by timestamp (first element wins ties, as before). */
12
+ export function newestCheckpoint(cps) {
13
+ let newest = cps[0];
14
+ for (const cp of cps) {
15
+ if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0))
16
+ newest = cp;
17
+ }
18
+ return newest;
19
+ }
20
+ /**
21
+ * Build the provenance floor from an already-read checkpoint list. Pure: the
22
+ * caller owns the read (and any dedup-status filtering), so both legacy call
23
+ * sites keep byte-identical output.
24
+ */
25
+ export function buildFloorBlock(cps) {
26
+ const summary = newestCheckpoint(cps)?.summary?.trim();
27
+ if (summary) {
28
+ return { text: WITH_SUMMARY_PREFIX + summary, basis: "lastCheckpoint" };
29
+ }
30
+ return { text: NO_SUMMARY_TEXT, basis: "lastCheckpoint" };
31
+ }
32
+ /** The hard last-resort floor (checkpoint read unavailable or threw). */
33
+ export function unavailableFloorBlock() {
34
+ return { text: FLOOR_UNAVAILABLE_TEXT, basis: "none" };
35
+ }
@@ -26,36 +26,22 @@ import { defaultEmbedder, cosineSimilarity } from "../embedder.js";
26
26
  // [] for live sessions). Mirrors vector-search.ts / tieredRouter.ts.
27
27
  import { listCheckpoints } from "../store/sqlite.js";
28
28
  import { RECALL_MIN_COSINE } from "../config.js";
29
- /** Build the provenance floor block from the session's newest checkpoint. */
29
+ import { buildFloorBlock as sharedFloorBlock, unavailableFloorBlock, } from "../failback/floor.js";
30
+ /**
31
+ * Build the provenance floor block from the session's newest checkpoint.
32
+ *
33
+ * 3WF-4: the text construction moved to the SHARED pure builder
34
+ * (src/failback/floor.ts). This wrapper keeps THIS call site's read semantics —
35
+ * `listCheckpoints` filtered to `dedupStatus !== "removed"` — so the output is
36
+ * byte-identical to the pre-refactor 3WF-3 version.
37
+ */
30
38
  function buildFloorBlock(sessionId, store) {
31
39
  try {
32
40
  const cps = listCheckpoints(sessionId, store.stateDir).filter((c) => c.dedupStatus !== "removed");
33
- let newest = cps[0];
34
- for (const cp of cps) {
35
- if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0))
36
- newest = cp;
37
- }
38
- const summary = newest?.summary?.trim();
39
- if (summary) {
40
- return {
41
- text: "The following compacted context is the most recent checkpoint from " +
42
- "this session (recall found no query-relevant match):\n\n" + summary,
43
- basis: "lastCheckpoint",
44
- };
45
- }
46
- return {
47
- text: "This session has compacted context but recall could not surface a " +
48
- "checkpoint relevant to the current request; the most recent checkpoint " +
49
- "summary is unavailable.",
50
- basis: "lastCheckpoint",
51
- };
41
+ return sharedFloorBlock(cps);
52
42
  }
53
43
  catch {
54
- return {
55
- text: "This session has compacted context but recall could not surface a " +
56
- "checkpoint relevant to the current request.",
57
- basis: "none",
58
- };
44
+ return unavailableFloorBlock();
59
45
  }
60
46
  }
61
47
  /**
@@ -0,0 +1,90 @@
1
+ /**
2
+ * context-handler/injectionConfirm.fixture.ts — shared fixtures for the 3WF-4
3
+ * InjectionConfirm tests.
4
+ *
5
+ * Split out so each test file stays under the extensions/300-soft-cap the way
6
+ * src/recall/recall3wf.fixture.ts does for 3WF-3. These are REAL fixtures, not
7
+ * mocks/stubs: a REAL VectorStore over a temp stateDir with REAL checkpoints
8
+ * persisted via compactSession; the MegaRuntime is a minimal typed stub exposing
9
+ * only the fields confirmInjection touches (store, pendingRecallBlock,
10
+ * pendingMemoryRecallBlock, appendEvent), matching the triggerGuard/thrashGuard
11
+ * test conventions.
12
+ */
13
+ import { mkdtempSync } from "node:fs";
14
+ import { tmpdir } from "node:os";
15
+ import { join } from "node:path";
16
+
17
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
18
+ import { VectorStore } from "../../../src/vectorStore.js";
19
+ import { compactSession } from "../../../src/engine.js";
20
+ import type { MegaRuntime } from "../../mega-runtime.js";
21
+ import type { MegaConfig } from "../../mega-config.js";
22
+
23
+ /** Real EngineMessage fixture. */
24
+ export function msg(role: "user" | "assistant", text: string): any {
25
+ return { role, text };
26
+ }
27
+
28
+ /** A user-role AgentMessage carrying `text` (the tail-block shape). */
29
+ export function userMsg(text: string): AgentMessage {
30
+ return { role: "user", content: text, timestamp: 1 } as unknown as AgentMessage;
31
+ }
32
+
33
+ /** Recorded appendEvent calls, for telemetry assertions. */
34
+ export interface RecordedEvent {
35
+ name: string;
36
+ payload: Record<string, unknown>;
37
+ }
38
+
39
+ /** Fresh isolated state dir per VectorStore. */
40
+ export function freshStore(): { store: VectorStore; dir: string } {
41
+ const dir = mkdtempSync(join(tmpdir(), "mc-inject-"));
42
+ return { store: new VectorStore({ dedupSim: 0.9, stateDir: dir }), dir };
43
+ }
44
+
45
+ /** Persist N distinct checkpoints with ascending timestamps. */
46
+ export function seed(store: VectorStore, topics: string[], sid = "sess_inject"): void {
47
+ topics.forEach((t, i) => {
48
+ compactSession(
49
+ {
50
+ sessionId: sid,
51
+ messages: [msg("user", t), msg("assistant", "ok")],
52
+ keepFrom: 2,
53
+ timestamp: i + 1,
54
+ },
55
+ store,
56
+ );
57
+ });
58
+ }
59
+
60
+ /** Minimal MegaRuntime stub exposing only the confirmInjection touch-points. */
61
+ export function runtimeStub(
62
+ store: VectorStore,
63
+ over: Partial<{
64
+ pendingRecallBlock: string | undefined;
65
+ pendingMemoryRecallBlock: string | undefined;
66
+ }> = {},
67
+ ): { runtime: MegaRuntime; events: RecordedEvent[] } {
68
+ const events: RecordedEvent[] = [];
69
+ const runtime = {
70
+ store,
71
+ pendingRecallBlock: over.pendingRecallBlock,
72
+ pendingMemoryRecallBlock: over.pendingMemoryRecallBlock,
73
+ perfTurnStart: undefined,
74
+ rt: { recallInjectedThisTurn: false },
75
+ appendEvent: (name: string, payload: Record<string, unknown>) => {
76
+ events.push({ name, payload });
77
+ },
78
+ } as unknown as MegaRuntime;
79
+ return { runtime, events };
80
+ }
81
+
82
+ /** Config stub: only the flags confirmInjection reads. */
83
+ export function configStub(
84
+ over: Partial<{ threeWayFailback: boolean; recallTailInject: boolean }> = {},
85
+ ): MegaConfig {
86
+ return {
87
+ threeWayFailback: over.threeWayFailback ?? true,
88
+ recallTailInject: over.recallTailInject ?? true,
89
+ } as unknown as MegaConfig;
90
+ }
@@ -0,0 +1,168 @@
1
+ /**
2
+ * context-handler/injectionConfirm.ts — 3WF-4 InjectionConfirm.
3
+ *
4
+ * QA amendment A3 (binding): pi exposes NO prompt readback API, so the only
5
+ * verifiable proxy for what the provider will actually receive is the pre-LLM
6
+ * message list. In DEFAULT tail mode (`recallTailInject` ON) the staged recall
7
+ * block rides in as a user-role tail message, so we assert the block's marker
8
+ * text is present in the message list we are about to return. In LEGACY prepend
9
+ * mode (`recallTailInject` OFF) the block never enters the message list at all,
10
+ * so the guard degrades to a string-contains check over our own composed return
11
+ * value and never reports a false miss.
12
+ *
13
+ * Recovery ladder when the marker is absent (tail mode only):
14
+ * 1. recomposed — rebuild the view from the runtime's pending blocks via the
15
+ * SAME `buildTailResult` composition the handler uses (self-repair on this
16
+ * event; the user sees nothing).
17
+ * 2. floor — nothing pending either: append the shared provenance floor text
18
+ * (src/failback/floor.ts) as a user-role tail message so the model is never
19
+ * silently left with no compacted-context provenance at all.
20
+ *
21
+ * Stack position: wraps the `tailResult` closure returned by buildTailResult, so
22
+ * EVERY return point of the context handler (gate / replay / debounce /
23
+ * thrash-guard / pipeline / live-trim) is verified with one wiring point.
24
+ *
25
+ * Non-fatal everywhere: any throw degrades to the unverified view (pre-sprint
26
+ * behavior). Flag OFF => the wrapper is never installed (byte-identical).
27
+ */
28
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
29
+ import type { MegaRuntime } from "../../mega-runtime.js";
30
+ import type { MegaConfig } from "../../mega-config.js";
31
+ import type { InjectionVerdict } from "../../../src/failback/types.js";
32
+ import { vectorList } from "../../../src/vectorStore.js";
33
+ import { normalizeSessionId } from "../../../src/store.js";
34
+ import { buildFloorBlock } from "../../../src/failback/floor.js";
35
+ import { withRecallTail } from "../recall-tail.js";
36
+ import { messageContentText } from "./messageText.js";
37
+
38
+ /** A composed context view (the shape every handler return point produces). */
39
+ export interface TailView {
40
+ messages: AgentMessage[];
41
+ }
42
+
43
+ /** The staged blocks + mode this pass verifies (pure inputs, no pi runtime). */
44
+ export interface ConfirmInput {
45
+ /** The staged block text expected to have landed (null => nothing to verify). */
46
+ staged: string | null;
47
+ /** False in legacy prepend mode: verify the return string, not the list. */
48
+ tailMode: boolean;
49
+ }
50
+
51
+ /**
52
+ * The marker substring used to locate a staged block inside a message. The
53
+ * block's first non-empty line, capped, so prompt reshapes (cache striping /
54
+ * message separation) that regroup messages cannot defeat the match, while a
55
+ * genuinely dropped block still fails it.
56
+ */
57
+ export function blockMarker(block: string): string {
58
+ const line = block
59
+ .split("\n")
60
+ .map((l) => l.trim())
61
+ .find((l) => l.length > 0);
62
+ return (line ?? "").slice(0, 80);
63
+ }
64
+
65
+ /**
66
+ * PURE decision function: did the staged block land in this view, and if not,
67
+ * which rung should repair it? Takes the already-extracted message texts so it
68
+ * stays free of pi types and is directly unit-testable.
69
+ */
70
+ export function decideInjection(
71
+ input: ConfirmInput,
72
+ messageTexts: readonly string[],
73
+ hasPendingBlocks: boolean,
74
+ ): InjectionVerdict {
75
+ const marker = input.staged ? blockMarker(input.staged) : "";
76
+ // Nothing staged => nothing to assert; NOT a miss. Injecting a floor here
77
+ // would push provenance text into sessions that never had recall to lose.
78
+ if (!marker) return { landed: true, recovered: "none" };
79
+ const landed = messageTexts.some((t) => t.includes(marker));
80
+ if (landed) return { landed: true, recovered: "none" };
81
+ // Absent: recompose when the runtime still holds pending blocks, else floor.
82
+ return { landed: false, recovered: hasPendingBlocks ? "recomposed" : "floor" };
83
+ }
84
+
85
+ /** Append `text` as a user-role tail message (same shape as recall-tail.ts). */
86
+ function withFloorTail(view: TailView, text: string): TailView {
87
+ const tailMsg = {
88
+ role: "user" as const,
89
+ content: text,
90
+ timestamp: Date.now(),
91
+ } as unknown as AgentMessage;
92
+ return { messages: [...view.messages, tailMsg] };
93
+ }
94
+
95
+ /**
96
+ * Thin caller: verify (and if needed repair) one composed view. Returns the view
97
+ * to actually return from the handler. `sessionId` sources the floor checkpoints.
98
+ *
99
+ * The recompose rung deliberately re-appends via `withRecallTail` onto the
100
+ * ALREADY-COMPOSED view rather than re-running `buildTailResult`: the reshape
101
+ * stages (cache striping / message separation) are the realistic way a tail
102
+ * message gets regrouped away, and re-running the same composition would
103
+ * reproduce the same loss. Appending after the reshape is the actual repair, and
104
+ * it keeps the PREVENT-PI-001/002 tail-append invariant (a single user-role
105
+ * message after a complete prefix can never split a toolCall/toolResult pair).
106
+ */
107
+ export function confirmInjection(
108
+ runtime: MegaRuntime,
109
+ config: MegaConfig,
110
+ view: TailView,
111
+ sessionId: string,
112
+ ): TailView {
113
+ try {
114
+ if (!config.threeWayFailback) return view;
115
+ // What the tail composition was supposed to inject. BOTH staged blocks
116
+ // count: withRecallTail joins recall + memory blocks into one tail
117
+ // message, so either one going missing is a real injection failure.
118
+ const staged =
119
+ runtime.pendingRecallBlock ?? runtime.pendingMemoryRecallBlock ?? null;
120
+ // Can the recompose rung actually re-append? Only when a block is still
121
+ // staged on the runtime. When it is not (blocks consumed between
122
+ // composition and this check), or when withRecallTail declines to append,
123
+ // the ladder falls through to the floor rung below.
124
+ const hasPending =
125
+ runtime.pendingRecallBlock != null ||
126
+ runtime.pendingMemoryRecallBlock != null;
127
+ // Legacy prepend mode: the block is not expected in the message list —
128
+ // verify our composed return value contains it instead (A3 degrade path).
129
+ if (!config.recallTailInject) {
130
+ const composed = view.messages.map(messageContentText).join("\n");
131
+ const marker = staged ? blockMarker(staged) : "";
132
+ runtime.appendEvent("injection_confirmed", {
133
+ mode: "prepend",
134
+ landed: marker ? composed.includes(marker) : true,
135
+ });
136
+ return view;
137
+ }
138
+ const verdict = decideInjection(
139
+ { staged, tailMode: true },
140
+ view.messages.map(messageContentText),
141
+ hasPending,
142
+ );
143
+ if (verdict.landed) {
144
+ runtime.appendEvent("injection_confirmed", { mode: "tail", landed: true });
145
+ return view;
146
+ }
147
+ if (verdict.recovered === "recomposed") {
148
+ const rebuilt = withRecallTail(view.messages, runtime, config);
149
+ // withRecallTail returns the input array unchanged on failure; only
150
+ // treat a genuine append as a recovery.
151
+ if (rebuilt.length > view.messages.length) {
152
+ runtime.appendEvent("injection_recovered", { via: "recomposed" });
153
+ return { messages: rebuilt };
154
+ }
155
+ }
156
+ const floor = buildFloorBlock(
157
+ vectorList(runtime.store, normalizeSessionId(sessionId)),
158
+ );
159
+ runtime.appendEvent("injection_recovered", {
160
+ via: "floor",
161
+ basis: floor.basis,
162
+ });
163
+ return withFloorTail(view, floor.text);
164
+ } catch {
165
+ // Non-fatal: return the unverified view (pre-sprint behavior).
166
+ return view;
167
+ }
168
+ }
@@ -30,6 +30,10 @@ import { normalizeSessionId } from "../../../src/store.js";
30
30
  import { recall } from "../../../src/engine.js";
31
31
  import { formatRecallBlock } from "../../../src/recall.js";
32
32
  import { vectorStats, vectorList } from "../../../src/vectorStore.js";
33
+ import {
34
+ buildFloorBlock as sharedFloorBlock,
35
+ FLOOR_UNAVAILABLE_TEXT,
36
+ } from "../../../src/failback/floor.js";
33
37
  import { recentUserQuery } from "../../mega-runtime.js";
34
38
 
35
39
  /** One-shot completion marker per MegaRuntime (dies with the runtime). */
@@ -108,30 +112,18 @@ export function runTriggerGuard(
108
112
  }
109
113
  }
110
114
 
111
- /** Build the provenance floor string from the session's newest checkpoint. */
115
+ /**
116
+ * Build the provenance floor string from the session's newest checkpoint.
117
+ *
118
+ * 3WF-4: the text construction moved to the SHARED pure builder
119
+ * (src/failback/floor.ts) — this keeps the store read (`vectorList`, unfiltered,
120
+ * exactly as 3WF-1 shipped) and the string return type, so output is
121
+ * byte-identical to the pre-refactor version.
122
+ */
112
123
  function buildFloorBlock(runtime: MegaRuntime, sid: string): string {
113
124
  try {
114
- const cps = vectorList(runtime.store, sid);
115
- let newest = cps[0];
116
- for (const cp of cps) {
117
- if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0)) newest = cp;
118
- }
119
- const summary = newest?.summary?.trim();
120
- if (summary) {
121
- return (
122
- "The following compacted context is the most recent checkpoint from " +
123
- "this session (recall found no query-relevant match):\n\n" + summary
124
- );
125
- }
126
- return (
127
- "This session has compacted context but recall could not surface a " +
128
- "checkpoint relevant to the current request; the most recent checkpoint " +
129
- "summary is unavailable."
130
- );
125
+ return sharedFloorBlock(vectorList(runtime.store, sid)).text;
131
126
  } catch {
132
- return (
133
- "This session has compacted context but recall could not surface a " +
134
- "checkpoint relevant to the current request."
135
- );
127
+ return FLOOR_UNAVAILABLE_TEXT;
136
128
  }
137
129
  }
@@ -24,6 +24,7 @@ import { piCompactWouldNoop } from "../mega-pipeline.js";
24
24
  import type { MegaConfig } from "../mega-config.js";
25
25
  import { buildTailResult } from "./context-handler/tailResult.js";
26
26
  import { runTriggerGuard } from "./context-handler/triggerGuard.js";
27
+ import { confirmInjection } from "./context-handler/injectionConfirm.js";
27
28
  import { persistEpochAndMaintain } from "./context-handler/afterCompact.js";
28
29
  import { appendMirrorAndLedger } from "./context-handler/dbMirrorAppend.js";
29
30
  import { evaluateGate, thrashGuardBlocks } from "./context-handler/gateCheck.js";
@@ -72,7 +73,21 @@ export function registerContextHandler(
72
73
  // tail message at any view-return point. Returns undefined when nothing
73
74
  // is staged (or the flag is OFF) so the caller falls through to its
74
75
  // normal return.
75
- const tailResult = buildTailResult(runtime, config, messages);
76
+ const composeTail = buildTailResult(runtime, config, messages);
77
+ // 3WF-4 InjectionConfirm: wrap the tail factory so EVERY return point of
78
+ // this handler (gate / replay / debounce / thrash-guard / pipeline /
79
+ // live-trim) is verified — the staged block's marker must be present in
80
+ // the message list pi will send (tail mode), else we re-compose from the
81
+ // runtime's pending blocks and finally fall back to the shared floor.
82
+ // A composition that yields nothing staged (undefined) is passed through
83
+ // untouched, so flag-OFF and no-recall paths are byte-identical.
84
+ const tailResult: typeof composeTail = config.threeWayFailback
85
+ ? (msgs) => {
86
+ const view = composeTail(msgs);
87
+ if (!view) return view;
88
+ return confirmInjection(runtime, config, view, ctx.sessionManager.getSessionId());
89
+ }
90
+ : composeTail;
76
91
  // Always track context for the dashboard/widget, even when auto is off.
77
92
  // (v0.8 regression: !config.auto gate sat above this, leaving ctx stats
78
93
  // null -> widget '?% / ?/?' when auto disabled. Track first, THEN gate.)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.20.87",
3
+ "version": "0.20.88",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-3-Clause",
@@ -0,0 +1,71 @@
1
+ /**
2
+ * src/failback/floor.ts — the SHARED pure provenance-floor builder (3WF-4).
3
+ *
4
+ * Consolidation refactor (ZERO behavior change). Before this module the same
5
+ * floor text was built twice:
6
+ * (a) `extensions/mega-events/context-handler/triggerGuard.ts` (3WF-1) —
7
+ * read checkpoints via `vectorList` (unfiltered), returned a bare string;
8
+ * (b) `src/recall/validator.ts` (3WF-3) — read checkpoints via
9
+ * `listCheckpoints` filtered to `dedupStatus !== "removed"`, returned a
10
+ * `FloorBlock`.
11
+ * The three text variants were byte-identical between the two; only the
12
+ * checkpoint READ differed. So this module takes the already-read checkpoint
13
+ * list from the caller (staying pure — no store, no pi types, no I/O) and each
14
+ * call site keeps its own read semantics. Output is byte-identical to both.
15
+ *
16
+ * 3WF-4's InjectionConfirm is the third consumer: when neither the message list
17
+ * nor the runtime's pending blocks yield a block, it needs the SAME last-resort
18
+ * floor text rather than a fourth copy.
19
+ *
20
+ * Non-fatal by construction: every branch returns a `FloorBlock`; the `none`
21
+ * basis carries the shortest text (used when the checkpoint read itself threw).
22
+ */
23
+ import type { StoredCheckpoint } from "../store.js";
24
+ import type { FloorBlock } from "./types.js";
25
+
26
+ /** Floor text when the newest checkpoint summary is available (prefix). */
27
+ const WITH_SUMMARY_PREFIX =
28
+ "The following compacted context is the most recent checkpoint from " +
29
+ "this session (recall found no query-relevant match):\n\n";
30
+
31
+ /** Floor text when checkpoints exist but no usable summary does. */
32
+ const NO_SUMMARY_TEXT =
33
+ "This session has compacted context but recall could not surface a " +
34
+ "checkpoint relevant to the current request; the most recent checkpoint " +
35
+ "summary is unavailable.";
36
+
37
+ /** Floor text when the checkpoint read itself failed (hard last resort). */
38
+ export const FLOOR_UNAVAILABLE_TEXT =
39
+ "This session has compacted context but recall could not surface a " +
40
+ "checkpoint relevant to the current request.";
41
+
42
+ /** The newest checkpoint by timestamp (first element wins ties, as before). */
43
+ export function newestCheckpoint(
44
+ cps: readonly StoredCheckpoint[],
45
+ ): StoredCheckpoint | undefined {
46
+ let newest = cps[0];
47
+ for (const cp of cps) {
48
+ if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0)) newest = cp;
49
+ }
50
+ return newest;
51
+ }
52
+
53
+ /**
54
+ * Build the provenance floor from an already-read checkpoint list. Pure: the
55
+ * caller owns the read (and any dedup-status filtering), so both legacy call
56
+ * sites keep byte-identical output.
57
+ */
58
+ export function buildFloorBlock(
59
+ cps: readonly StoredCheckpoint[],
60
+ ): FloorBlock {
61
+ const summary = newestCheckpoint(cps)?.summary?.trim();
62
+ if (summary) {
63
+ return { text: WITH_SUMMARY_PREFIX + summary, basis: "lastCheckpoint" };
64
+ }
65
+ return { text: NO_SUMMARY_TEXT, basis: "lastCheckpoint" };
66
+ }
67
+
68
+ /** The hard last-resort floor (checkpoint read unavailable or threw). */
69
+ export function unavailableFloorBlock(): FloorBlock {
70
+ return { text: FLOOR_UNAVAILABLE_TEXT, basis: "none" };
71
+ }
@@ -103,6 +103,22 @@ export interface RecallCandidate {
103
103
  source: "vector" | "fts5" | "recency";
104
104
  }
105
105
 
106
+ /**
107
+ * Verdict of an InjectionConfirm pass (3WF-4). There is NO prompt readback API
108
+ * in pi, so the only verifiable proxy for what the provider will receive is the
109
+ * pre-LLM message list (`ContextEvent.messages`, transformed). `landed` is true
110
+ * when the staged block's text was found there (tail mode) or in the composed
111
+ * return string (legacy prepend mode). `recovered` records which repair rung
112
+ * this event used: none (already landed), recomposed (rebuilt from the runtime's
113
+ * pending blocks), or floor (last-resort provenance text).
114
+ */
115
+ export interface InjectionVerdict {
116
+ /** True when the staged block text is present in the verified view. */
117
+ landed: boolean;
118
+ /** Which repair rung ran to make the block present. */
119
+ recovered: "none" | "recomposed" | "floor";
120
+ }
121
+
106
122
  /**
107
123
  * Outcome of the three-source recall vote. `winners` are the agreed candidates
108
124
  * (ranked), `votes` counts how many distinct sources named each checkpointId,
@@ -28,6 +28,10 @@ import { listCheckpoints } from "../store/sqlite.js";
28
28
  import { RECALL_MIN_COSINE } from "../config.js";
29
29
  import type { VectorStore } from "../vectorStore.js";
30
30
  import type { RecallCandidate, FloorBlock } from "../failback/types.js";
31
+ import {
32
+ buildFloorBlock as sharedFloorBlock,
33
+ unavailableFloorBlock,
34
+ } from "../failback/floor.js";
31
35
 
32
36
  /** Options for the recall validator. */
33
37
  export interface ValidateOptions {
@@ -54,39 +58,22 @@ export type ValidationOutcome =
54
58
  | { kind: "candidate"; candidate: RecallCandidate }
55
59
  | { kind: "floor"; floor: FloorBlock };
56
60
 
57
- /** Build the provenance floor block from the session's newest checkpoint. */
61
+ /**
62
+ * Build the provenance floor block from the session's newest checkpoint.
63
+ *
64
+ * 3WF-4: the text construction moved to the SHARED pure builder
65
+ * (src/failback/floor.ts). This wrapper keeps THIS call site's read semantics —
66
+ * `listCheckpoints` filtered to `dedupStatus !== "removed"` — so the output is
67
+ * byte-identical to the pre-refactor 3WF-3 version.
68
+ */
58
69
  function buildFloorBlock(sessionId: string, store: VectorStore): FloorBlock {
59
70
  try {
60
71
  const cps = listCheckpoints(sessionId, store.stateDir).filter(
61
72
  (c) => c.dedupStatus !== "removed",
62
73
  );
63
- let newest = cps[0];
64
- for (const cp of cps) {
65
- if (!newest || (cp.timestamp ?? 0) > (newest.timestamp ?? 0)) newest = cp;
66
- }
67
- const summary = newest?.summary?.trim();
68
- if (summary) {
69
- return {
70
- text:
71
- "The following compacted context is the most recent checkpoint from " +
72
- "this session (recall found no query-relevant match):\n\n" + summary,
73
- basis: "lastCheckpoint",
74
- };
75
- }
76
- return {
77
- text:
78
- "This session has compacted context but recall could not surface a " +
79
- "checkpoint relevant to the current request; the most recent checkpoint " +
80
- "summary is unavailable.",
81
- basis: "lastCheckpoint",
82
- };
74
+ return sharedFloorBlock(cps);
83
75
  } catch {
84
- return {
85
- text:
86
- "This session has compacted context but recall could not surface a " +
87
- "checkpoint relevant to the current request.",
88
- basis: "none",
89
- };
76
+ return unavailableFloorBlock();
90
77
  }
91
78
  }
92
79