@remnic/core 10.0.137 → 10.0.138
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.
- package/dist/access-admin-ops-surface.js +38 -38
- package/dist/access-cli.js +38 -38
- package/dist/access-extraction-force-flush-preempt.js +38 -38
- package/dist/access-extraction-force-flush.js +38 -38
- package/dist/access-http-briefing.js +38 -38
- package/dist/access-http-query.js +38 -38
- package/dist/access-http.js +38 -38
- package/dist/access-identity-continuity-surface.js +38 -38
- package/dist/access-lcm-surface.js +38 -38
- package/dist/access-mcp.js +38 -38
- package/dist/access-observe-write-surface.js +38 -38
- package/dist/access-operations-batch.js +38 -38
- package/dist/access-operations.js +38 -38
- package/dist/access-recall-concurrency.js +38 -38
- package/dist/access-recall-diagnostics.js +2 -2
- package/dist/access-recall-response.js +38 -38
- package/dist/access-recall-surface.js +38 -38
- package/dist/access-service.js +38 -38
- package/dist/active-recall.js +3 -3
- package/dist/briefing.js +3 -3
- package/dist/buffer-turn.d.ts +11 -0
- package/dist/buffer-turn.d.ts.map +1 -1
- package/dist/causal-consolidation.js +4 -4
- package/dist/{chunk-SDRF2WLZ.js → chunk-2NJ74RQU.js} +2 -2
- package/dist/{chunk-K4EBUTPN.js → chunk-2VS3DAXE.js} +2 -2
- package/dist/{chunk-W4VZ52B3.js → chunk-5IGWEYNV.js} +3 -3
- package/dist/{chunk-2SZWPNG4.js → chunk-A33VT7GC.js} +2 -2
- package/dist/{chunk-YDAAGCM5.js → chunk-AV6R66KO.js} +2 -2
- package/dist/{chunk-4HXJFMPS.js → chunk-CFSCKCT5.js} +2 -1
- package/dist/chunk-CFSCKCT5.js.map +1 -0
- package/dist/{chunk-IM4XEI2C.js → chunk-CKG7T4AD.js} +2 -2
- package/dist/{chunk-SMMRDLLU.js → chunk-DI2QWY6J.js} +8 -8
- package/dist/{chunk-7WIZNDR5.js → chunk-HOSS72UP.js} +2 -2
- package/dist/{chunk-5K2UIHW4.js → chunk-I3UBDUHO.js} +2 -2
- package/dist/{chunk-2LZHXBJM.js → chunk-JEAMQL63.js} +3 -3
- package/dist/{chunk-H2N65TDE.js → chunk-JG6AHBJW.js} +2 -2
- package/dist/{chunk-3F6COEHZ.js → chunk-JVNGFQJN.js} +1 -1
- package/dist/chunk-JVNGFQJN.js.map +1 -0
- package/dist/{chunk-V4HY4PEF.js → chunk-JY327EH4.js} +2 -2
- package/dist/{chunk-D3CBHMMY.js → chunk-K4DBE5MH.js} +2 -2
- package/dist/{chunk-E3DXY24J.js → chunk-KCLHHANX.js} +2 -2
- package/dist/{chunk-LKJVCDVS.js → chunk-KCVPKFDG.js} +2 -2
- package/dist/{chunk-EFMAYZOB.js → chunk-L6CMR5OI.js} +2 -2
- package/dist/{chunk-B7ZHJGPX.js → chunk-LEH4FVBJ.js} +2 -2
- package/dist/chunk-LEO4MQ3S.js +140 -0
- package/dist/chunk-LEO4MQ3S.js.map +1 -0
- package/dist/{chunk-4VL2H5Q3.js → chunk-LL7WIK2Z.js} +2 -2
- package/dist/{chunk-FIP6HDPC.js → chunk-LT35JWKZ.js} +2 -2
- package/dist/{chunk-O6NONCRR.js → chunk-MP6V4CLP.js} +2 -2
- package/dist/{chunk-N7HF7C4N.js → chunk-MXE42X3I.js} +3 -3
- package/dist/{chunk-ET3RHBTM.js → chunk-N6JGLQV7.js} +2 -2
- package/dist/{chunk-FJUCMMNT.js → chunk-NB2BKYUS.js} +2 -2
- package/dist/{chunk-3KOHTVQO.js → chunk-NOZDET3Q.js} +3 -3
- package/dist/{chunk-H3IOEMTW.js → chunk-OI3UMEW3.js} +2 -2
- package/dist/{chunk-TDPBSVFW.js → chunk-OJJ73HLA.js} +2 -2
- package/dist/{chunk-QYUYM7XE.js → chunk-PKPW7MXT.js} +2 -2
- package/dist/{chunk-X4V5BBI2.js → chunk-PKV3NNM2.js} +2 -2
- package/dist/{chunk-KYY7NODE.js → chunk-PNT3SZRY.js} +7 -7
- package/dist/{chunk-4XPWHXEF.js → chunk-POB7N5UM.js} +2 -2
- package/dist/{chunk-WS2X4WHX.js → chunk-PQ5FYNY2.js} +2 -2
- package/dist/{chunk-P3TQ62XC.js → chunk-QZ5JZNXN.js} +165 -56
- package/dist/chunk-QZ5JZNXN.js.map +1 -0
- package/dist/{chunk-S57YMPLX.js → chunk-SRW4IKHM.js} +2 -2
- package/dist/{chunk-UBSN42MM.js → chunk-VF3EVRUN.js} +2 -2
- package/dist/{chunk-DGKSCOB4.js → chunk-VSZEV7HG.js} +2 -2
- package/dist/{chunk-DONY5FXC.js → chunk-YDOLJQ55.js} +3 -3
- package/dist/cli.js +38 -38
- package/dist/coding/export-okf-codegraph.js +2 -2
- package/dist/compounding/engine.js +4 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +3 -3
- package/dist/connectors/codex-materialize-runner.js +3 -3
- package/dist/connectors/index.js +3 -3
- package/dist/convergence-refresh.js +5 -5
- package/dist/corpus-watermark.js +5 -5
- package/dist/entity-retrieval.js +3 -3
- package/dist/explicit-capture.js +38 -38
- package/dist/extraction-window-drain.d.ts +21 -0
- package/dist/extraction-window-drain.d.ts.map +1 -1
- package/dist/extraction-window-drain.js +5 -3
- package/dist/extraction.js +3 -3
- package/dist/harmonic-retrieval.js +3 -3
- package/dist/index.js +38 -38
- package/dist/location/index.js +4 -4
- package/dist/maintenance/memory-governance.js +3 -3
- package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +3 -3
- package/dist/maintenance/rebuild-memory-projection-runner.js +5 -5
- package/dist/maintenance/rebuild-memory-projection.js +4 -4
- package/dist/namespaces/migrate.js +4 -4
- package/dist/namespaces/storage.js +3 -3
- package/dist/operator-doctor-corpus.js +6 -6
- package/dist/operator-toolkit.js +10 -10
- package/dist/orchestration/backlog-scheduler.js +39 -39
- package/dist/orchestration/buffer-drain-runner.js +39 -39
- package/dist/orchestration/extraction-drain.d.ts +13 -0
- package/dist/orchestration/extraction-drain.d.ts.map +1 -1
- package/dist/orchestration/maintenance.js +11 -11
- package/dist/orchestrator.d.ts.map +1 -1
- package/dist/orchestrator.js +38 -38
- package/dist/recall-explain-renderer.js +3 -3
- package/dist/recall-navigate-access.js +2 -2
- package/dist/recall-navigate-disclosure.js +2 -2
- package/dist/recall-navigate.js +2 -2
- package/dist/recall-navigation.js +38 -38
- package/dist/recall-xray-cli.js +4 -4
- package/dist/recall-xray-renderer.js +3 -3
- package/dist/recall-xray.js +2 -2
- package/dist/resume-bundles.js +4 -4
- package/dist/semantic-consolidation.js +4 -4
- package/dist/semantic-rule-promotion.js +3 -3
- package/dist/semantic-rule-verifier.js +3 -3
- package/dist/source-agent-qualifier.js +3 -3
- package/dist/standup/build.js +3 -3
- package/dist/storage.js +2 -2
- package/dist/transfer/export-okf.js +3 -3
- package/dist/types.d.ts +5 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/verified-recall.js +3 -3
- package/dist/who-knows-cli.js +4 -4
- package/dist/who-knows.js +3 -3
- package/package.json +2 -2
- package/src/buffer-turn.ts +8 -0
- package/src/config.ts +2 -0
- package/src/extraction-window-drain.test.ts +66 -0
- package/src/extraction-window-drain.ts +113 -2
- package/src/orchestration/extraction-drain-parts.test.ts +260 -0
- package/src/orchestration/extraction-drain.ts +186 -23
- package/src/orchestrator-flush.test.ts +3 -0
- package/src/orchestrator.ts +1 -0
- package/src/part-failure-threshold.test.ts +91 -0
- package/src/types.ts +5 -6
- package/dist/chunk-3F6COEHZ.js.map +0 -1
- package/dist/chunk-4HXJFMPS.js.map +0 -1
- package/dist/chunk-LZIOY7ZU.js +0 -57
- package/dist/chunk-LZIOY7ZU.js.map +0 -1
- package/dist/chunk-P3TQ62XC.js.map +0 -1
- /package/dist/{chunk-SDRF2WLZ.js.map → chunk-2NJ74RQU.js.map} +0 -0
- /package/dist/{chunk-K4EBUTPN.js.map → chunk-2VS3DAXE.js.map} +0 -0
- /package/dist/{chunk-W4VZ52B3.js.map → chunk-5IGWEYNV.js.map} +0 -0
- /package/dist/{chunk-2SZWPNG4.js.map → chunk-A33VT7GC.js.map} +0 -0
- /package/dist/{chunk-YDAAGCM5.js.map → chunk-AV6R66KO.js.map} +0 -0
- /package/dist/{chunk-IM4XEI2C.js.map → chunk-CKG7T4AD.js.map} +0 -0
- /package/dist/{chunk-SMMRDLLU.js.map → chunk-DI2QWY6J.js.map} +0 -0
- /package/dist/{chunk-7WIZNDR5.js.map → chunk-HOSS72UP.js.map} +0 -0
- /package/dist/{chunk-5K2UIHW4.js.map → chunk-I3UBDUHO.js.map} +0 -0
- /package/dist/{chunk-2LZHXBJM.js.map → chunk-JEAMQL63.js.map} +0 -0
- /package/dist/{chunk-H2N65TDE.js.map → chunk-JG6AHBJW.js.map} +0 -0
- /package/dist/{chunk-V4HY4PEF.js.map → chunk-JY327EH4.js.map} +0 -0
- /package/dist/{chunk-D3CBHMMY.js.map → chunk-K4DBE5MH.js.map} +0 -0
- /package/dist/{chunk-E3DXY24J.js.map → chunk-KCLHHANX.js.map} +0 -0
- /package/dist/{chunk-LKJVCDVS.js.map → chunk-KCVPKFDG.js.map} +0 -0
- /package/dist/{chunk-EFMAYZOB.js.map → chunk-L6CMR5OI.js.map} +0 -0
- /package/dist/{chunk-B7ZHJGPX.js.map → chunk-LEH4FVBJ.js.map} +0 -0
- /package/dist/{chunk-4VL2H5Q3.js.map → chunk-LL7WIK2Z.js.map} +0 -0
- /package/dist/{chunk-FIP6HDPC.js.map → chunk-LT35JWKZ.js.map} +0 -0
- /package/dist/{chunk-O6NONCRR.js.map → chunk-MP6V4CLP.js.map} +0 -0
- /package/dist/{chunk-N7HF7C4N.js.map → chunk-MXE42X3I.js.map} +0 -0
- /package/dist/{chunk-ET3RHBTM.js.map → chunk-N6JGLQV7.js.map} +0 -0
- /package/dist/{chunk-FJUCMMNT.js.map → chunk-NB2BKYUS.js.map} +0 -0
- /package/dist/{chunk-3KOHTVQO.js.map → chunk-NOZDET3Q.js.map} +0 -0
- /package/dist/{chunk-H3IOEMTW.js.map → chunk-OI3UMEW3.js.map} +0 -0
- /package/dist/{chunk-TDPBSVFW.js.map → chunk-OJJ73HLA.js.map} +0 -0
- /package/dist/{chunk-QYUYM7XE.js.map → chunk-PKPW7MXT.js.map} +0 -0
- /package/dist/{chunk-X4V5BBI2.js.map → chunk-PKV3NNM2.js.map} +0 -0
- /package/dist/{chunk-KYY7NODE.js.map → chunk-PNT3SZRY.js.map} +0 -0
- /package/dist/{chunk-4XPWHXEF.js.map → chunk-POB7N5UM.js.map} +0 -0
- /package/dist/{chunk-WS2X4WHX.js.map → chunk-PQ5FYNY2.js.map} +0 -0
- /package/dist/{chunk-S57YMPLX.js.map → chunk-SRW4IKHM.js.map} +0 -0
- /package/dist/{chunk-UBSN42MM.js.map → chunk-VF3EVRUN.js.map} +0 -0
- /package/dist/{chunk-DGKSCOB4.js.map → chunk-VSZEV7HG.js.map} +0 -0
- /package/dist/{chunk-DONY5FXC.js.map → chunk-YDOLJQ55.js.map} +0 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import { SmartBuffer } from "../buffer.js";
|
|
7
|
+
import { parseConfig } from "../config.js";
|
|
8
|
+
import { StorageManager } from "../storage.js";
|
|
9
|
+
import type { BufferTurn } from "../types.js";
|
|
10
|
+
import { ExtractionDrain, type DrainRunOptions } from "./extraction-drain.js";
|
|
11
|
+
import type { ExtractionRunResult } from "./extraction-run.js";
|
|
12
|
+
|
|
13
|
+
function makeTurn(content: string, overrides: Partial<BufferTurn> = {}): BufferTurn {
|
|
14
|
+
return { role: "user", content, timestamp: "2026-11-17T12:00:00.000Z", sessionKey: "s1", ...overrides };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DONE: ExtractionRunResult = { status: "completed", persistedCount: 1, durableOutputCount: 1 };
|
|
18
|
+
const DEFERRED: ExtractionRunResult = {
|
|
19
|
+
status: "skipped",
|
|
20
|
+
reason: "breaker_open",
|
|
21
|
+
retainedBuffer: true,
|
|
22
|
+
failureClass: "provider_retryable",
|
|
23
|
+
persistedCount: 0,
|
|
24
|
+
durableOutputCount: 0,
|
|
25
|
+
};
|
|
26
|
+
const FAILED: ExtractionRunResult = {
|
|
27
|
+
status: "skipped",
|
|
28
|
+
reason: "empty_extraction_result",
|
|
29
|
+
retainedBuffer: true,
|
|
30
|
+
failureClass: "parse_empty",
|
|
31
|
+
persistedCount: 0,
|
|
32
|
+
durableOutputCount: 0,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface DrainOptions {
|
|
36
|
+
turns: BufferTurn[];
|
|
37
|
+
results: ExtractionRunResult[];
|
|
38
|
+
runs?: number;
|
|
39
|
+
budgetChars?: number;
|
|
40
|
+
maxTurnChars?: number;
|
|
41
|
+
dropAfter?: number;
|
|
42
|
+
saveCopies?: { reason: string; contents: string }[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A real buffer on real storage, drained with a stub runExtraction that
|
|
46
|
+
* clears like the coordinator does for a `clearMatchingTurns` drain. */
|
|
47
|
+
async function withDrain(opts: DrainOptions): Promise<{
|
|
48
|
+
calls: BufferTurn[][];
|
|
49
|
+
buffer: SmartBuffer;
|
|
50
|
+
}> {
|
|
51
|
+
const root = await mkdtemp(path.join(os.tmpdir(), "remnic-parts-drain-"));
|
|
52
|
+
const calls: BufferTurn[][] = [];
|
|
53
|
+
const buffer = new SmartBuffer(
|
|
54
|
+
parseConfig({ memoryDir: root, bufferMaxTurnsPerSession: 2000 }),
|
|
55
|
+
new StorageManager(root),
|
|
56
|
+
);
|
|
57
|
+
try {
|
|
58
|
+
for (const entry of opts.turns) await buffer.addTurn("s1", entry);
|
|
59
|
+
await buffer.flushPendingSave();
|
|
60
|
+
const drain = new ExtractionDrain({
|
|
61
|
+
config: {
|
|
62
|
+
extractionMinUserTurns: 1,
|
|
63
|
+
extractionMinChars: 0,
|
|
64
|
+
extractionMaxTurnChars: opts.maxTurnChars ?? 60,
|
|
65
|
+
extractionChunkDropAfterFailures: opts.dropAfter ?? 3,
|
|
66
|
+
},
|
|
67
|
+
extractionWindowBudgetChars: () => opts.budgetChars ?? 100,
|
|
68
|
+
measureRenderedTurns: (turns) => turns.reduce((total, seen) => total + seen.content.length, 0),
|
|
69
|
+
runExtraction: async (turns, options) => {
|
|
70
|
+
calls.push([...turns]);
|
|
71
|
+
const result = opts.results.shift() ?? DONE;
|
|
72
|
+
if (result.status === "completed") {
|
|
73
|
+
await buffer.clearAfterExtraction(
|
|
74
|
+
"s1",
|
|
75
|
+
turns,
|
|
76
|
+
options?.clearMatchingTurns === true ? { allowNonPrefix: true } : undefined,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
},
|
|
81
|
+
getStorage: async () => ({
|
|
82
|
+
saveDroppedExtractionChunk: async (_bufferKey: string, reason: string, turns: readonly BufferTurn[]) => {
|
|
83
|
+
opts.saveCopies?.push({ reason, contents: turns.map((seen) => seen.content).join("") });
|
|
84
|
+
return `/recovery/${turns.length}.json`;
|
|
85
|
+
},
|
|
86
|
+
}),
|
|
87
|
+
buffer: {
|
|
88
|
+
dropExtractionChunk: (bufferKey, turns, dropOptions) =>
|
|
89
|
+
buffer.dropExtractionChunk(bufferKey, turns, dropOptions),
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
const options: DrainRunOptions = {
|
|
93
|
+
bufferKey: "s1",
|
|
94
|
+
clearBufferAfterExtraction: true,
|
|
95
|
+
clearMatchingTurns: true,
|
|
96
|
+
skipUserTurnThreshold: false,
|
|
97
|
+
failOnExtractionFailure: false,
|
|
98
|
+
forceExtractionAttempt: false,
|
|
99
|
+
};
|
|
100
|
+
for (let runIndex = 0; runIndex < (opts.runs ?? 1); runIndex += 1) {
|
|
101
|
+
await drain.run(opts.turns, options);
|
|
102
|
+
}
|
|
103
|
+
return { calls, buffer };
|
|
104
|
+
} finally {
|
|
105
|
+
await rm(root, { recursive: true, force: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function allExtractedText(calls: readonly BufferTurn[][]): string {
|
|
110
|
+
return calls.flat().map((seen) => seen.content).join("");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
test("an oversized turn drains as ordered parts and the parent clears once", async () => {
|
|
114
|
+
// 150 chars at a 60-char part budget -> 3 parts; the 100-char call budget
|
|
115
|
+
// packs one part per chunk, so every part is a separate extraction call.
|
|
116
|
+
const parent = makeTurn(`${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`);
|
|
117
|
+
const { calls, buffer } = await withDrain({ turns: [parent], results: [DONE, DONE, DONE] });
|
|
118
|
+
assert.equal(calls.length, 2, "the parts pack greedily into budget-fitting chunks");
|
|
119
|
+
assert.deepEqual(
|
|
120
|
+
calls.flat().map((seen) => seen.extractionPartOf),
|
|
121
|
+
[
|
|
122
|
+
{ part: 1, of: 3 },
|
|
123
|
+
{ part: 2, of: 3 },
|
|
124
|
+
{ part: 3, of: 3 },
|
|
125
|
+
],
|
|
126
|
+
"each part copy carries its in-flight marker",
|
|
127
|
+
);
|
|
128
|
+
assert.equal(allExtractedText(calls), parent.content, "every part extracted — verbatim coverage, nothing dropped");
|
|
129
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "the parent clears when its last part drains");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("parts keep sentence boundaries and carry no marker text in content", async () => {
|
|
133
|
+
const parent = makeTurn(`${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`);
|
|
134
|
+
const { calls } = await withDrain({ turns: [parent], results: [DONE, DONE, DONE] });
|
|
135
|
+
const parts = calls.flat();
|
|
136
|
+
for (const part of parts) {
|
|
137
|
+
assert.ok(part.content.trimEnd().endsWith(".") || part === parts.at(-1), `part ends on a sentence edge: ${part.content}`);
|
|
138
|
+
assert.ok(!part.content.includes("extractionPartOf"), "no marker text is spliced into content");
|
|
139
|
+
assert.ok(!part.content.includes("part"), "no ordinal text is spliced into content");
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("the parent stays buffered until every part drains", async () => {
|
|
144
|
+
const parent = makeTurn(`${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`);
|
|
145
|
+
const root = await mkdtemp(path.join(os.tmpdir(), "remnic-parts-retry-"));
|
|
146
|
+
const buffer = new SmartBuffer(
|
|
147
|
+
parseConfig({ memoryDir: root, bufferMaxTurnsPerSession: 2000 }),
|
|
148
|
+
new StorageManager(root),
|
|
149
|
+
);
|
|
150
|
+
try {
|
|
151
|
+
await buffer.addTurn("s1", parent);
|
|
152
|
+
await buffer.flushPendingSave();
|
|
153
|
+
const results = [DEFERRED, DONE, DONE, DONE];
|
|
154
|
+
const drain = new ExtractionDrain({
|
|
155
|
+
config: {
|
|
156
|
+
extractionMinUserTurns: 1,
|
|
157
|
+
extractionMinChars: 0,
|
|
158
|
+
extractionMaxTurnChars: 60,
|
|
159
|
+
extractionChunkDropAfterFailures: 3,
|
|
160
|
+
},
|
|
161
|
+
extractionWindowBudgetChars: () => 100,
|
|
162
|
+
measureRenderedTurns: (turns) => turns.reduce((total, seen) => total + seen.content.length, 0),
|
|
163
|
+
runExtraction: async (turns, options) => {
|
|
164
|
+
const result = results.shift() ?? DONE;
|
|
165
|
+
if (result.status === "completed") {
|
|
166
|
+
await buffer.clearAfterExtraction(
|
|
167
|
+
"s1",
|
|
168
|
+
turns,
|
|
169
|
+
options?.clearMatchingTurns === true ? { allowNonPrefix: true } : undefined,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
},
|
|
174
|
+
getStorage: async () => ({ saveDroppedExtractionChunk: async () => "/recovery/x.json" }),
|
|
175
|
+
buffer: {
|
|
176
|
+
dropExtractionChunk: (bufferKey, turns, dropOptions) =>
|
|
177
|
+
buffer.dropExtractionChunk(bufferKey, turns, dropOptions),
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
const options: DrainRunOptions = {
|
|
181
|
+
bufferKey: "s1",
|
|
182
|
+
clearBufferAfterExtraction: true,
|
|
183
|
+
clearMatchingTurns: true,
|
|
184
|
+
skipUserTurnThreshold: false,
|
|
185
|
+
failOnExtractionFailure: false,
|
|
186
|
+
forceExtractionAttempt: false,
|
|
187
|
+
};
|
|
188
|
+
await drain.run([parent], options);
|
|
189
|
+
assert.deepEqual(
|
|
190
|
+
buffer.getTurns("s1").map((seen) => seen.content),
|
|
191
|
+
[parent.content],
|
|
192
|
+
"the deferred first part leaves the whole parent buffered",
|
|
193
|
+
);
|
|
194
|
+
await drain.run([parent], options);
|
|
195
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "the retry drains all parts and clears the parent");
|
|
196
|
+
} finally {
|
|
197
|
+
await rm(root, { recursive: true, force: true });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("a turn past the part limit clamps, and the parent leaves through a recovery copy", async () => {
|
|
202
|
+
// 600 chars at a 60-char part budget needs 10 parts; the cap emits 8.
|
|
203
|
+
const parent = makeTurn(Array.from({ length: 10 }, (_v, i) => `${String(i)}${"x".repeat(58)}.`).join(" "));
|
|
204
|
+
const saveCopies: { reason: string; contents: string }[] = [];
|
|
205
|
+
const { calls, buffer } = await withDrain({
|
|
206
|
+
turns: [parent],
|
|
207
|
+
results: Array.from({ length: 8 }, () => DONE),
|
|
208
|
+
budgetChars: 80,
|
|
209
|
+
saveCopies,
|
|
210
|
+
});
|
|
211
|
+
const partMarkers = calls.flat().map((seen) => seen.extractionPartOf?.part);
|
|
212
|
+
assert.deepEqual(partMarkers, [1, 2, 3, 4, 5, 6, 7, 8], "exactly the capped eight parts extract");
|
|
213
|
+
assert.equal(saveCopies.length, 1, "the clamped parent leaves via the recovery-copy path");
|
|
214
|
+
assert.equal(saveCopies[0]?.contents, parent.content, "the recovery copy holds the FULL parent, not slices");
|
|
215
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "the buffer releases the clamped turn");
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("a part chunk that keeps failing drops the parent with its full content", async () => {
|
|
219
|
+
const parent = makeTurn(`${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`);
|
|
220
|
+
const saveCopies: { reason: string; contents: string }[] = [];
|
|
221
|
+
const { buffer } = await withDrain({
|
|
222
|
+
turns: [parent],
|
|
223
|
+
results: [FAILED, FAILED],
|
|
224
|
+
dropAfter: 2,
|
|
225
|
+
runs: 2,
|
|
226
|
+
saveCopies,
|
|
227
|
+
});
|
|
228
|
+
assert.equal(saveCopies.length, 1, "one recovery copy for the dropped parent");
|
|
229
|
+
assert.equal(saveCopies[0]?.contents, parent.content, "the copy preserves the full parent content");
|
|
230
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "the parent is dropped, not the unmatched slice copies");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("an oversized turn inside a fitting window still splits", async () => {
|
|
234
|
+
// A 150-char turn under a 12k-style call budget would previously lose its
|
|
235
|
+
// tail to the per-turn render clamp; the split keeps every byte.
|
|
236
|
+
const parent = makeTurn(`${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`);
|
|
237
|
+
const small = makeTurn("short note", { timestamp: "2026-11-17T12:00:01.000Z" });
|
|
238
|
+
const { calls, buffer } = await withDrain({
|
|
239
|
+
turns: [parent, small],
|
|
240
|
+
results: [DONE, DONE, DONE, DONE],
|
|
241
|
+
budgetChars: 12000,
|
|
242
|
+
});
|
|
243
|
+
const text = allExtractedText(calls);
|
|
244
|
+
assert.ok(text.includes(parent.content), "the split turn is fully extracted");
|
|
245
|
+
assert.ok(text.includes(small.content), "the sibling turn is extracted");
|
|
246
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "the whole window drains");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test("a window of exact duplicates that is also oversized extracts every copy's content", async () => {
|
|
250
|
+
// Dedupe cannot fan representatives out into parts, so the oversized window
|
|
251
|
+
// takes the split path and each buffered identity still clears.
|
|
252
|
+
const content = `${"a".repeat(50)}. ${"b".repeat(50)}. ${"c".repeat(45)}`;
|
|
253
|
+
const first = makeTurn(content);
|
|
254
|
+
const duplicate = makeTurn(content, { timestamp: "2026-11-17T12:00:02.000Z" });
|
|
255
|
+
const { buffer } = await withDrain({
|
|
256
|
+
turns: [first, duplicate],
|
|
257
|
+
results: Array.from({ length: 8 }, () => DONE),
|
|
258
|
+
});
|
|
259
|
+
assert.deepEqual(buffer.getTurns("s1"), [], "both buffered identities drain");
|
|
260
|
+
});
|
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { isAbortError, preemptedExtractionError } from "../abort-error.js";
|
|
5
5
|
import { describeError } from "../buffer-turn-helpers.js";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
chunkExtractionWindow,
|
|
8
|
+
classifyDrainResult,
|
|
9
|
+
mergeDrainResults,
|
|
10
|
+
splitOversizedTurns,
|
|
11
|
+
} from "../extraction-window-drain.js";
|
|
7
12
|
import { log } from "../logger.js";
|
|
8
13
|
import type { BufferTurn } from "../types.js";
|
|
9
14
|
import { type CollapsedDuplicates, collapseExactDuplicateTurns, markDuplicateTurns } from "./extraction-dedupe.js";
|
|
@@ -45,10 +50,16 @@ export interface ExtractionDrainDeps {
|
|
|
45
50
|
};
|
|
46
51
|
}
|
|
47
52
|
|
|
48
|
-
/** Identity of a chunk: its failure count must not follow a buffer key to
|
|
53
|
+
/** Identity of a chunk: its failure count must not follow a buffer key to
|
|
54
|
+
* different content. Part copies carry the part ordinal: two slices of one
|
|
55
|
+
* oversized turn can be byte-identical, and their failure counts must not
|
|
56
|
+
* share a slot (a success on part 1 would erase part 2's count). */
|
|
49
57
|
function chunkFingerprint(chunk: readonly BufferTurn[]): string {
|
|
50
58
|
const hash = createHash("sha256");
|
|
51
|
-
for (const turn of chunk)
|
|
59
|
+
for (const turn of chunk) {
|
|
60
|
+
const part = turn.extractionPartOf ? `#${turn.extractionPartOf.part}/${turn.extractionPartOf.of}` : "";
|
|
61
|
+
hash.update(`${turn.role}\u0000${turn.timestamp}\u0000${turn.content}${part}\u0001`);
|
|
62
|
+
}
|
|
52
63
|
return hash.digest("hex");
|
|
53
64
|
}
|
|
54
65
|
|
|
@@ -57,6 +68,12 @@ interface ChunkPlan {
|
|
|
57
68
|
singleRun: boolean;
|
|
58
69
|
chunks: BufferTurn[][];
|
|
59
70
|
resumed: boolean;
|
|
71
|
+
/** In-flight part copy → its source turn, for `extractionPartOf` splits. */
|
|
72
|
+
partParents?: Map<BufferTurn, BufferTurn>;
|
|
73
|
+
/** How many oversized turns hit the max-parts clamp. */
|
|
74
|
+
clampedParts: number;
|
|
75
|
+
/** Parents whose unemitted tail needs a recovery copy before clearing. */
|
|
76
|
+
clampedParents?: Set<BufferTurn>;
|
|
60
77
|
}
|
|
61
78
|
|
|
62
79
|
/** The drain-loop action for one chunk result. */
|
|
@@ -74,7 +91,7 @@ interface ChunkExecutionOptions {
|
|
|
74
91
|
|
|
75
92
|
export class ExtractionDrain {
|
|
76
93
|
/** Consecutive failed attempts on the head chunk, per buffer key. In memory on */
|
|
77
|
-
private readonly failures = new Map<string,
|
|
94
|
+
private readonly failures = new Map<string, number>();
|
|
78
95
|
|
|
79
96
|
/**
|
|
80
97
|
* Buffer key to the first turn of the chunk a qualified window stopped on.
|
|
@@ -105,14 +122,27 @@ export class ExtractionDrain {
|
|
|
105
122
|
return total;
|
|
106
123
|
}
|
|
107
124
|
|
|
125
|
+
/** Failure counts are keyed by buffer key AND chunk fingerprint: with one
|
|
126
|
+
* slot per key, a sibling chunk's success (count reset to 0) would delete
|
|
127
|
+
* a failing part's count and it would never reach the drop threshold. */
|
|
108
128
|
private failureCount(bufferKey: string, chunk: readonly BufferTurn[]): number {
|
|
109
|
-
|
|
110
|
-
return entry && entry.fingerprint === chunkFingerprint(chunk) ? entry.count : 0;
|
|
129
|
+
return this.failures.get(this.failureSlot(bufferKey, chunk)) ?? 0;
|
|
111
130
|
}
|
|
112
131
|
|
|
113
132
|
private setFailureCount(bufferKey: string, chunk: readonly BufferTurn[], count: number): void {
|
|
114
|
-
|
|
115
|
-
|
|
133
|
+
const slot = this.failureSlot(bufferKey, chunk);
|
|
134
|
+
if (count <= 0) this.failures.delete(slot);
|
|
135
|
+
else this.failures.set(slot, count);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private failureSlot(bufferKey: string, chunk: readonly BufferTurn[]): string {
|
|
139
|
+
return `${bufferKey}\u0000${chunkFingerprint(chunk)}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private clearBufferFailures(bufferKey: string): void {
|
|
143
|
+
for (const slot of this.failures.keys()) {
|
|
144
|
+
if (slot.startsWith(`${bufferKey}\u0000`)) this.failures.delete(slot);
|
|
145
|
+
}
|
|
116
146
|
}
|
|
117
147
|
|
|
118
148
|
/**
|
|
@@ -148,9 +178,27 @@ export class ExtractionDrain {
|
|
|
148
178
|
// sees each text once. Every original stays a distinct identity the chunk must
|
|
149
179
|
// account for, so chunks carry the duplicates as marked copies — clearing and
|
|
150
180
|
// drops remove representative and duplicates together.
|
|
151
|
-
const
|
|
181
|
+
const partBudget = Math.min(budgetChars, this.deps.config.extractionMaxTurnChars);
|
|
182
|
+
const hasOversized =
|
|
183
|
+
Number.isFinite(partBudget) &&
|
|
184
|
+
partBudget > 0 &&
|
|
185
|
+
this.chunkable(runOptions, turns, budgetChars) &&
|
|
186
|
+
turns.some((turn) => turn.content.length > partBudget);
|
|
187
|
+
const collapsed = hasOversized
|
|
188
|
+
? {
|
|
189
|
+
representatives: turns,
|
|
190
|
+
representativePositions: turns.map((_, index) => index),
|
|
191
|
+
duplicateFlags: turns.map(() => false),
|
|
192
|
+
duplicateCount: 0,
|
|
193
|
+
}
|
|
194
|
+
: collapseExactDuplicateTurns(turns);
|
|
152
195
|
const window = collapsed.duplicateCount > 0 ? markDuplicateTurns(turns, collapsed) : turns;
|
|
153
|
-
const plan = this.chunkPlan(turns, runOptions, budgetChars, collapsed);
|
|
196
|
+
const plan = this.chunkPlan(turns, runOptions, budgetChars, collapsed, partBudget);
|
|
197
|
+
if (plan.clampedParts > 0) {
|
|
198
|
+
log.warn(
|
|
199
|
+
`buffer[${runOptions.bufferKey}]: clamped ${plan.clampedParts} oversized turns at the part limit; the unemitted tail content stays buffered for the next drain`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
154
202
|
// The tail of a window that already qualified goes through the chunk loop even when it now fits in one
|
|
155
203
|
// chunk, so its failures still count toward the drop threshold.
|
|
156
204
|
if (plan.singleRun) {
|
|
@@ -181,7 +229,7 @@ export class ExtractionDrain {
|
|
|
181
229
|
collapsed.duplicateCount > 0
|
|
182
230
|
? { ...runOptions, skipCharThreshold: true, skipUserTurnThreshold: true }
|
|
183
231
|
: runOptions;
|
|
184
|
-
return await this.runChunked(plan.chunks, options, preemptable, preemptHandle);
|
|
232
|
+
return await this.runChunked(plan.chunks, options, preemptable, preemptHandle, plan.partParents, plan.clampedParents);
|
|
185
233
|
}
|
|
186
234
|
|
|
187
235
|
/** Chunking plan for one trigger-drain window. Accepted-capture groups
|
|
@@ -192,10 +240,24 @@ export class ExtractionDrain {
|
|
|
192
240
|
turns: readonly BufferTurn[],
|
|
193
241
|
runOptions: DrainRunOptions,
|
|
194
242
|
budgetChars: number,
|
|
195
|
-
collapsed?: CollapsedDuplicates
|
|
243
|
+
collapsed?: CollapsedDuplicates,
|
|
244
|
+
partBudget = Number.POSITIVE_INFINITY
|
|
196
245
|
): ChunkPlan {
|
|
197
246
|
const deduped = collapsed !== undefined && collapsed.duplicateCount > 0;
|
|
198
|
-
|
|
247
|
+
// Long-turn split (payload-cap drain): a turn longer than the render clamp
|
|
248
|
+
// would lose its tail to the per-turn slice, so it becomes ordered verbatim
|
|
249
|
+
// part copies marked `extractionPartOf`. The parts pack like any turn; the
|
|
250
|
+
// parent clears only when its last part drains.
|
|
251
|
+
const split =
|
|
252
|
+
deduped || !this.chunkable(runOptions, turns, budgetChars)
|
|
253
|
+
? {
|
|
254
|
+
turns: [...turns],
|
|
255
|
+
partParents: new Map<BufferTurn, BufferTurn>(),
|
|
256
|
+
clampedParents: new Set<BufferTurn>(),
|
|
257
|
+
clamped: false,
|
|
258
|
+
}
|
|
259
|
+
: splitOversizedTurns(turns, partBudget);
|
|
260
|
+
const planning = deduped ? collapsed.representatives : split.turns;
|
|
199
261
|
const repChunks = this.chunkable(runOptions, turns, budgetChars)
|
|
200
262
|
? this.deps.measureRenderedTurns !== undefined
|
|
201
263
|
? this.chunkByRenderedSize(planning, budgetChars)
|
|
@@ -204,7 +266,15 @@ export class ExtractionDrain {
|
|
|
204
266
|
const resumed =
|
|
205
267
|
repChunks.length > 0 && this.qualified.get(runOptions.bufferKey) === chunkFingerprint(repChunks[0].slice(0, 1));
|
|
206
268
|
const chunks = deduped ? this.expandedChunks(turns, collapsed, repChunks) : repChunks;
|
|
207
|
-
|
|
269
|
+
const partParents = split.partParents.size > 0 ? split.partParents : undefined;
|
|
270
|
+
return {
|
|
271
|
+
singleRun: chunks.length <= 1 && !resumed && partParents === undefined,
|
|
272
|
+
chunks,
|
|
273
|
+
resumed,
|
|
274
|
+
partParents,
|
|
275
|
+
clampedParts: split.clampedParents.size,
|
|
276
|
+
clampedParents: split.clampedParents.size > 0 ? split.clampedParents : undefined,
|
|
277
|
+
};
|
|
208
278
|
}
|
|
209
279
|
|
|
210
280
|
/** Each representative chunk expands to a PARTITION slice of the original window:
|
|
@@ -267,7 +337,9 @@ export class ExtractionDrain {
|
|
|
267
337
|
chunks: readonly BufferTurn[][],
|
|
268
338
|
runOptions: DrainRunOptions,
|
|
269
339
|
preemptable: boolean,
|
|
270
|
-
preemptHandle?: ExtractionPreemptHandle
|
|
340
|
+
preemptHandle?: ExtractionPreemptHandle,
|
|
341
|
+
partParents?: Map<BufferTurn, BufferTurn>,
|
|
342
|
+
clampedParents?: Set<BufferTurn>
|
|
271
343
|
): Promise<ExtractionRunResult> {
|
|
272
344
|
const baseOptions = { ...runOptions, skipCharThreshold: true, skipUserTurnThreshold: true };
|
|
273
345
|
const bufferKey = runOptions.bufferKey;
|
|
@@ -293,11 +365,24 @@ export class ExtractionDrain {
|
|
|
293
365
|
this.rethrowChunkAbort(err, preempt, runOptions.abortSignal?.aborted === true);
|
|
294
366
|
// A successfully dropped thrown chunk does not end the drain: later
|
|
295
367
|
// chunks still run this pass (live drop policy).
|
|
296
|
-
aggregate = await this.settleThrownChunk(
|
|
368
|
+
aggregate = await this.settleThrownChunk(
|
|
369
|
+
bufferKey,
|
|
370
|
+
chunk,
|
|
371
|
+
failures,
|
|
372
|
+
dropAfter,
|
|
373
|
+
err,
|
|
374
|
+
byIdentity,
|
|
375
|
+
aggregate,
|
|
376
|
+
partParents
|
|
377
|
+
);
|
|
297
378
|
continue;
|
|
298
379
|
}
|
|
299
380
|
const settled = this.settleVerdict(bufferKey, chunk, result, aggregate);
|
|
300
381
|
if (settled.action === "continue") {
|
|
382
|
+
// Every part extracted: the slice copies match no buffered turn, so the
|
|
383
|
+
// run cleared only the chunk's real turns; the parent is dropped now
|
|
384
|
+
// that its last part drained.
|
|
385
|
+
await this.clearFinishedPartParents(bufferKey, chunk, partParents, clampedParents);
|
|
301
386
|
aggregate = settled.result;
|
|
302
387
|
continue;
|
|
303
388
|
}
|
|
@@ -310,6 +395,7 @@ export class ExtractionDrain {
|
|
|
310
395
|
result: settled.result,
|
|
311
396
|
byIdentity,
|
|
312
397
|
aggregate: settled.aggregate,
|
|
398
|
+
partParents,
|
|
313
399
|
});
|
|
314
400
|
if (failed.action === "continue") {
|
|
315
401
|
aggregate = failed.result;
|
|
@@ -318,6 +404,7 @@ export class ExtractionDrain {
|
|
|
318
404
|
return failed.result;
|
|
319
405
|
}
|
|
320
406
|
this.qualified.delete(bufferKey);
|
|
407
|
+
this.clearBufferFailures(bufferKey);
|
|
321
408
|
return aggregate ?? { status: "skipped", persistedCount: 0, durableOutputCount: 0 };
|
|
322
409
|
}
|
|
323
410
|
|
|
@@ -371,7 +458,8 @@ export class ExtractionDrain {
|
|
|
371
458
|
dropAfter: number,
|
|
372
459
|
cause: unknown,
|
|
373
460
|
byIdentity: boolean,
|
|
374
|
-
aggregate: ExtractionRunResult | null
|
|
461
|
+
aggregate: ExtractionRunResult | null,
|
|
462
|
+
partParents?: Map<BufferTurn, BufferTurn>
|
|
375
463
|
): Promise<ExtractionRunResult> {
|
|
376
464
|
failures += 1;
|
|
377
465
|
if (failures < dropAfter || !this.dropFailedChunks) {
|
|
@@ -384,7 +472,7 @@ export class ExtractionDrain {
|
|
|
384
472
|
persistedCount: 0,
|
|
385
473
|
durableOutputCount: 0,
|
|
386
474
|
};
|
|
387
|
-
if (!(await this.dropFailedChunk(bufferKey, chunk, dropAfter, thrown, byIdentity))) {
|
|
475
|
+
if (!(await this.dropFailedChunk(bufferKey, chunk, dropAfter, thrown, byIdentity, partParents))) {
|
|
388
476
|
this.setFailureCount(bufferKey, chunk, failures);
|
|
389
477
|
throw cause;
|
|
390
478
|
}
|
|
@@ -424,10 +512,20 @@ export class ExtractionDrain {
|
|
|
424
512
|
result: ExtractionRunResult;
|
|
425
513
|
byIdentity: boolean;
|
|
426
514
|
aggregate: ExtractionRunResult | null;
|
|
515
|
+
partParents?: Map<BufferTurn, BufferTurn>;
|
|
427
516
|
}): Promise<{ action: "continue" | "return"; result: ExtractionRunResult }> {
|
|
428
517
|
const failures = args.failures + 1;
|
|
429
518
|
if (failures >= args.dropAfter && this.dropFailedChunks) {
|
|
430
|
-
if (
|
|
519
|
+
if (
|
|
520
|
+
!(await this.dropFailedChunk(
|
|
521
|
+
args.bufferKey,
|
|
522
|
+
args.chunk,
|
|
523
|
+
args.dropAfter,
|
|
524
|
+
args.result,
|
|
525
|
+
args.byIdentity,
|
|
526
|
+
args.partParents
|
|
527
|
+
))
|
|
528
|
+
) {
|
|
431
529
|
// No recovery copy: keep the only buffered copy and retry later.
|
|
432
530
|
this.setFailureCount(args.bufferKey, args.chunk, failures);
|
|
433
531
|
return { action: "return", result: mergeDrainResults(args.aggregate, args.result) };
|
|
@@ -462,16 +560,22 @@ export class ExtractionDrain {
|
|
|
462
560
|
chunk: readonly BufferTurn[],
|
|
463
561
|
dropAfter: number,
|
|
464
562
|
result: ExtractionRunResult,
|
|
465
|
-
byIdentity: boolean
|
|
563
|
+
byIdentity: boolean,
|
|
564
|
+
partParents?: Map<BufferTurn, BufferTurn>
|
|
466
565
|
): Promise<boolean> {
|
|
467
566
|
const reason =
|
|
468
567
|
`dropped after ${dropAfter} repeated extraction failures ` +
|
|
469
568
|
`(last result: status=${result.status} reason=${result.reason ?? "n/a"})`;
|
|
470
|
-
|
|
569
|
+
// Part copies match nothing buffered: their drop set is the parents (full
|
|
570
|
+
// content lands in the recovery copy, not slices).
|
|
571
|
+
const dropTurns = partParents === undefined ? chunk : this.dropSetForParts(chunk, partParents);
|
|
572
|
+
const copyPath = await this.saveRecoveryCopy(bufferKey, reason, dropTurns);
|
|
471
573
|
if (copyPath === null) return false;
|
|
472
|
-
log.warn(`buffer[${bufferKey}]: dropping ${
|
|
574
|
+
log.warn(`buffer[${bufferKey}]: dropping ${dropTurns.length} turns — ${reason}; copy saved to ${copyPath}`);
|
|
473
575
|
try {
|
|
474
|
-
const dropped = await this.deps.buffer.dropExtractionChunk(bufferKey,
|
|
576
|
+
const dropped = await this.deps.buffer.dropExtractionChunk(bufferKey, dropTurns, {
|
|
577
|
+
allowNonPrefix: byIdentity || partParents !== undefined,
|
|
578
|
+
});
|
|
475
579
|
if (dropped > 0) {
|
|
476
580
|
log.warn(`buffer[${bufferKey}]: removed ${dropped} buffered turns — ${reason}`);
|
|
477
581
|
return true;
|
|
@@ -504,4 +608,63 @@ export class ExtractionDrain {
|
|
|
504
608
|
return null;
|
|
505
609
|
}
|
|
506
610
|
}
|
|
611
|
+
|
|
612
|
+
/** The buffered identities a part-carrying chunk actually removes on drop:
|
|
613
|
+
* real turns themselves plus each part copy's parent, once per parent. */
|
|
614
|
+
private dropSetForParts(
|
|
615
|
+
chunk: readonly BufferTurn[],
|
|
616
|
+
partParents: Map<BufferTurn, BufferTurn>
|
|
617
|
+
): BufferTurn[] {
|
|
618
|
+
const seen = new Set<BufferTurn>();
|
|
619
|
+
const dropTurns: BufferTurn[] = [];
|
|
620
|
+
for (const turn of chunk) {
|
|
621
|
+
const target = partParents.get(turn) ?? turn;
|
|
622
|
+
if (seen.has(target)) continue;
|
|
623
|
+
seen.add(target);
|
|
624
|
+
dropTurns.push(target);
|
|
625
|
+
}
|
|
626
|
+
return dropTurns;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** A part's slice never equals its parent's buffered content, so no drain
|
|
630
|
+
* clears a parent implicitly. When the LAST part of a parent drains, the
|
|
631
|
+
* whole turn has been extracted and the parent is removed by identity —
|
|
632
|
+
* non-prefix, since earlier chunks may leave it mid-buffer. */
|
|
633
|
+
private async clearFinishedPartParents(
|
|
634
|
+
bufferKey: string,
|
|
635
|
+
chunk: readonly BufferTurn[],
|
|
636
|
+
partParents?: Map<BufferTurn, BufferTurn>,
|
|
637
|
+
clampedParents?: Set<BufferTurn>
|
|
638
|
+
): Promise<void> {
|
|
639
|
+
if (partParents === undefined) return;
|
|
640
|
+
for (const turn of chunk) {
|
|
641
|
+
const marker = turn.extractionPartOf;
|
|
642
|
+
if (marker === undefined || marker.part !== marker.of) continue;
|
|
643
|
+
const parent = partParents.get(turn);
|
|
644
|
+
if (parent === undefined) continue;
|
|
645
|
+
if (clampedParents?.has(parent) === true) {
|
|
646
|
+
// The emitted parts covered only the head: the tail stays out of the
|
|
647
|
+
// model, so the parent leaves the buffer through the recovery-copy
|
|
648
|
+
// path — removed, never lost. A failed copy keeps it for retry.
|
|
649
|
+
const copyPath = await this.saveRecoveryCopy(
|
|
650
|
+
bufferKey,
|
|
651
|
+
"clamped at the per-turn part limit; unemitted tail preserved",
|
|
652
|
+
[parent]
|
|
653
|
+
);
|
|
654
|
+
if (copyPath === null) continue;
|
|
655
|
+
log.warn(`buffer[${bufferKey}]: clamped turn removed after part extraction; copy saved to ${copyPath}`);
|
|
656
|
+
}
|
|
657
|
+
try {
|
|
658
|
+
const dropped = await this.deps.buffer.dropExtractionChunk(bufferKey, [parent], { allowNonPrefix: true });
|
|
659
|
+
if (dropped === 0) {
|
|
660
|
+
log.warn(`buffer[${bufferKey}]: extracted turn's parts drained but its parent no longer matches the buffer`);
|
|
661
|
+
}
|
|
662
|
+
} catch (err) {
|
|
663
|
+
log.warn(
|
|
664
|
+
`buffer[${bufferKey}]: failed to clear an extracted turn's parent (it stays buffered for retry): ${describeError(err)}`
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
507
669
|
}
|
|
670
|
+
|
|
@@ -369,6 +369,7 @@ test("flushSession skips scoped ownership enforcement when namespaces are disabl
|
|
|
369
369
|
|
|
370
370
|
test("flushSession waits for queued extraction task completion", async () => {
|
|
371
371
|
const orchestrator = Object.create(Orchestrator.prototype) as any;
|
|
372
|
+
orchestrator.config = parseConfig({});
|
|
372
373
|
let releaseExtraction!: () => void;
|
|
373
374
|
let extractionStarted = false;
|
|
374
375
|
let flushSettled = false;
|
|
@@ -477,7 +478,9 @@ test("queued extraction settles immediately when its caller aborts while waiting
|
|
|
477
478
|
const queue = new ExtractionQueueCoordinator();
|
|
478
479
|
let extractionStarted = false;
|
|
479
480
|
let releaseExtraction!: () => void;
|
|
481
|
+
// SAFETY: fixture deps stub — only the queued-extraction path fields are exercised.
|
|
480
482
|
const coordinator = new TurnIngestionCoordinator({
|
|
483
|
+
config: parseConfig({}),
|
|
481
484
|
extractionQueueCoordinator: queue,
|
|
482
485
|
shouldQueueExtraction: () => true,
|
|
483
486
|
runExtraction: async () => {
|
package/src/orchestrator.ts
CHANGED
|
@@ -3102,6 +3102,7 @@ export class Orchestrator {
|
|
|
3102
3102
|
this.config.modelSource !== "gateway" && resolveLocalLlmCapabilities(this.config).localLlm,
|
|
3103
3103
|
model: this.config.localLlmModel,
|
|
3104
3104
|
maxContext: this.config.localLlmMaxContext,
|
|
3105
|
+
callMaxChars: this.config.extractionCallMaxChars,
|
|
3105
3106
|
});
|
|
3106
3107
|
}
|
|
3107
3108
|
|