@sema-agent/core 7.49.5 → 7.50.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.
Files changed (193) hide show
  1. package/CHANGELOG.md +138 -0
  2. package/dist/agents/observer.js +3 -10
  3. package/dist/agents/subagent-steps.d.ts +6 -14
  4. package/dist/agents/subagent-steps.js +3 -17
  5. package/dist/brain/stream-shared.d.ts +1 -1
  6. package/dist/core/allow-entry.d.ts +1 -1
  7. package/dist/core/allow-entry.js +1 -1
  8. package/dist/core/ask-origin.d.ts +15 -11
  9. package/dist/core/ask-origin.js +6 -14
  10. package/dist/core/auto-mode-prompt.js +6 -2
  11. package/dist/core/checkpoint-store.d.ts +6 -4
  12. package/dist/core/checkpoint-store.js +1 -1
  13. package/dist/core/context-edit.d.ts +2 -2
  14. package/dist/core/decision-receipt.d.ts +2 -2
  15. package/dist/core/decision-receipt.js +30 -11
  16. package/dist/core/delegation-frames.d.ts +66 -18
  17. package/dist/core/delegation-frames.js +22 -0
  18. package/dist/core/engine-notice.d.ts +7 -5
  19. package/dist/core/frame-settlement.d.ts +6 -1
  20. package/dist/core/gate-fold.js +19 -13
  21. package/dist/core/gate-lanes.js +12 -11
  22. package/dist/core/gate-outcome.d.ts +2 -2
  23. package/dist/core/governance-codes.js +2 -0
  24. package/dist/core/hooks.d.ts +28 -20
  25. package/dist/core/human-input-projection.js +2 -0
  26. package/dist/core/lsp-diagnostics.d.ts +3 -0
  27. package/dist/core/lsp-diagnostics.js +6 -0
  28. package/dist/core/lsp-session.d.ts +39 -17
  29. package/dist/core/lsp-session.js +118 -58
  30. package/dist/core/lsp.d.ts +83 -3
  31. package/dist/core/lsp.js +146 -16
  32. package/dist/core/mcp.js +2 -1
  33. package/dist/core/media-byte-cap.d.ts +14 -0
  34. package/dist/core/media-byte-cap.js +15 -3
  35. package/dist/core/memory-engine/consolidation-driver.d.ts +23 -1
  36. package/dist/core/memory-engine/consolidation-driver.js +54 -6
  37. package/dist/core/memory-engine/consolidation-write-gate.d.ts +46 -0
  38. package/dist/core/memory-engine/consolidation-write-gate.js +82 -0
  39. package/dist/core/memory-engine/consolidation.d.ts +17 -0
  40. package/dist/core/memory-engine/consolidation.js +1 -0
  41. package/dist/core/memory-engine/distiller.d.ts +8 -2
  42. package/dist/core/memory-engine/distiller.js +8 -6
  43. package/dist/core/memory-engine/engine.d.ts +37 -4
  44. package/dist/core/memory-engine/engine.js +127 -38
  45. package/dist/core/memory-engine/file-backend.d.ts +8 -3
  46. package/dist/core/memory-engine/file-backend.js +44 -16
  47. package/dist/core/memory-engine/index.d.ts +2 -2
  48. package/dist/core/memory-engine/index.js +1 -1
  49. package/dist/core/memory-engine/landing-admission.d.ts +65 -0
  50. package/dist/core/memory-engine/landing-admission.js +68 -0
  51. package/dist/core/memory-engine/types.d.ts +23 -0
  52. package/dist/core/mode-policy.d.ts +3 -2
  53. package/dist/core/mode-policy.js +2 -0
  54. package/dist/core/permission-rule-model.d.ts +19 -13
  55. package/dist/core/permission-rule-model.js +7 -4
  56. package/dist/core/permission-rule-provider.d.ts +11 -0
  57. package/dist/core/permission-rules.d.ts +1 -1
  58. package/dist/core/permission-rules.js +5 -2
  59. package/dist/core/persisted-rule-arms.d.ts +15 -7
  60. package/dist/core/persisted-rule-arms.js +13 -6
  61. package/dist/core/reminder-mint.d.ts +5 -0
  62. package/dist/core/reminder-mint.js +1 -0
  63. package/dist/core/retired-keys.d.ts +8 -0
  64. package/dist/core/retired-keys.js +4 -0
  65. package/dist/core/rule-lane-reads.d.ts +4 -4
  66. package/dist/core/rule-lane-reads.js +10 -1
  67. package/dist/core/runner/construction-doors.d.ts +10 -4
  68. package/dist/core/runner/construction-doors.js +25 -2
  69. package/dist/core/runner/contracts.d.ts +20 -3
  70. package/dist/core/runner/env-capability-census.js +1 -1
  71. package/dist/core/runner/gate-exit.js +2 -1
  72. package/dist/core/runner/memory-consolidation-driver.d.ts +6 -4
  73. package/dist/core/runner/memory-consolidation-driver.js +5 -2
  74. package/dist/core/runner/memory-consolidation-write-judge.d.ts +17 -0
  75. package/dist/core/runner/memory-consolidation-write-judge.js +14 -0
  76. package/dist/core/runner/permission-rule-lanes.d.ts +16 -22
  77. package/dist/core/runner/permission-rule-lanes.js +13 -43
  78. package/dist/core/runner/person-path-rows.d.ts +244 -0
  79. package/dist/core/runner/person-path-rows.js +387 -0
  80. package/dist/core/runner/prepare-acquire-reconcile.js +8 -1
  81. package/dist/core/runner/prepare-artifact.d.ts +5 -3
  82. package/dist/core/runner/prepare-artifact.js +2 -8
  83. package/dist/core/runner/prepare-caps-and-workflow.d.ts +4 -3
  84. package/dist/core/runner/prepare-caps-and-workflow.js +1 -1
  85. package/dist/core/runner/prepare-context-lane.js +2 -2
  86. package/dist/core/runner/prepare-gate-stations.d.ts +3 -3
  87. package/dist/core/runner/prepare-gate-stations.js +1 -1
  88. package/dist/core/runner/prepare-hands-readface.d.ts +19 -10
  89. package/dist/core/runner/prepare-hands-readface.js +57 -43
  90. package/dist/core/runner/prepare-lsp.d.ts +15 -13
  91. package/dist/core/runner/prepare-lsp.js +11 -11
  92. package/dist/core/runner/prepare-policy-chain.d.ts +8 -4
  93. package/dist/core/runner/prepare-policy-chain.js +6 -7
  94. package/dist/core/runner/prepare-run-refs.js +1 -1
  95. package/dist/core/runner/prepare-task.js +24 -16
  96. package/dist/core/runner/prepare-turn-wiring.d.ts +13 -6
  97. package/dist/core/runner/prepare-turn-wiring.js +19 -8
  98. package/dist/core/runner/read-root-grant.d.ts +1 -25
  99. package/dist/core/runner/read-root-grant.js +1 -17
  100. package/dist/core/runner/retired-runner-deps.d.ts +2 -2
  101. package/dist/core/runner/run-brain-sinks.d.ts +1 -5
  102. package/dist/core/runner/run-brain-sinks.js +2 -4
  103. package/dist/core/runner/run-harness-handlers.js +5 -4
  104. package/dist/core/runner/run-identity-wiring.d.ts +4 -4
  105. package/dist/core/runner/run-identity-wiring.js +10 -1
  106. package/dist/core/runner/run-leg.d.ts +1 -1
  107. package/dist/core/runner/run-leg.js +22 -9
  108. package/dist/core/runner/run-settle-and-teardown.d.ts +0 -4
  109. package/dist/core/runner/run-settle-and-teardown.js +7 -4
  110. package/dist/core/runner/run-terminal-adoption.d.ts +1 -1
  111. package/dist/core/runner/run-terminal-adoption.js +1 -1
  112. package/dist/core/runner/run-turn-boundary.js +2 -2
  113. package/dist/core/runner/runtask.js +8 -8
  114. package/dist/core/runner/session-file-state-replay.d.ts +14 -4
  115. package/dist/core/runner/session-file-state-replay.js +8 -0
  116. package/dist/core/runner/stream-steer-verb.js +3 -0
  117. package/dist/core/runner/tool-end-body.d.ts +5 -4
  118. package/dist/core/runner/tool-end-body.js +4 -4
  119. package/dist/core/runner/tool-output-projection.d.ts +6 -4
  120. package/dist/core/runner/tool-output-projection.js +66 -47
  121. package/dist/core/runner-deps.d.ts +6 -3
  122. package/dist/core/shared-memory/types.d.ts +2 -2
  123. package/dist/core/shell-read-silence.d.ts +23 -0
  124. package/dist/core/shell-read-silence.js +18 -0
  125. package/dist/core/synthetic-read-verdict.d.ts +80 -21
  126. package/dist/core/synthetic-read-verdict.js +89 -18
  127. package/dist/core/task-event.d.ts +113 -25
  128. package/dist/core/task-spec.d.ts +44 -0
  129. package/dist/core/task-tool-shape.d.ts +1 -1
  130. package/dist/core/tool-catalog-entries.js +16 -2
  131. package/dist/core/tool-policy.d.ts +25 -15
  132. package/dist/core/tool-policy.js +9 -13
  133. package/dist/core/trace.d.ts +3 -2
  134. package/dist/core/types.d.ts +3 -3
  135. package/dist/core/types.js +1 -1
  136. package/dist/core/untrusted-text.d.ts +2 -2
  137. package/dist/core/user-attachment.d.ts +164 -0
  138. package/dist/core/user-attachment.js +437 -0
  139. package/dist/engine/compaction/compaction.js +10 -3
  140. package/dist/engine/compaction/utils.js +6 -0
  141. package/dist/engine/execution-env/node-execution-env.d.ts +19 -1
  142. package/dist/engine/execution-env/node-execution-env.js +125 -0
  143. package/dist/engine/harness/agent-harness.d.ts +12 -2
  144. package/dist/engine/harness/agent-harness.js +15 -3
  145. package/dist/engine/harness/types.d.ts +21 -0
  146. package/dist/engine/llm/content-block-render.d.ts +15 -0
  147. package/dist/engine/llm/content-block-render.js +10 -0
  148. package/dist/engine/llm/index.d.ts +1 -0
  149. package/dist/engine/llm/index.js +1 -0
  150. package/dist/engine/llm/types.d.ts +80 -4
  151. package/dist/engine/llm/types.js +43 -1
  152. package/dist/engine/session/import-validate.js +2 -0
  153. package/dist/index.d.ts +6 -4
  154. package/dist/index.js +2 -1
  155. package/dist/internal/harness-types.d.ts +1 -1
  156. package/dist/internal/llm.d.ts +2 -2
  157. package/dist/internal/llm.js +1 -1
  158. package/dist/orchestration/workflow.d.ts +4 -5
  159. package/dist/orchestration/workflow.js +2 -2
  160. package/dist/prompts/default.d.ts +1 -1
  161. package/dist/tools/artifact/artifact-tool.js +1 -1
  162. package/dist/tools/fs/bash-readonly-classifier.d.ts +1 -1
  163. package/dist/tools/fs/dir-entries.d.ts +4 -10
  164. package/dist/tools/fs/dir-entries.js +4 -11
  165. package/dist/tools/fs/file-read-check.js +3 -3
  166. package/dist/tools/fs/fs-pdf.js +2 -4
  167. package/dist/tools/fs/fs-read.d.ts +47 -4
  168. package/dist/tools/fs/fs-read.js +63 -19
  169. package/dist/tools/fs/fs-search-tools.d.ts +3 -3
  170. package/dist/tools/fs/fs-search-tools.js +7 -2
  171. package/dist/tools/fs/fs-write.d.ts +1 -1
  172. package/dist/tools/fs/fs-write.js +7 -6
  173. package/dist/tools/fs/index.d.ts +7 -1
  174. package/dist/tools/fs/index.js +56 -34
  175. package/dist/tools/fs/read-deny-home.d.ts +19 -1
  176. package/dist/tools/fs/read-deny-home.js +32 -22
  177. package/dist/tools/fs/read-deny.d.ts +14 -0
  178. package/dist/tools/fs/read-deny.js +8 -0
  179. package/dist/tools/fs/repo-map.d.ts +3 -2
  180. package/dist/tools/fs/repo-map.js +11 -3
  181. package/dist/tools/fs/safety.d.ts +41 -2
  182. package/dist/tools/fs/safety.js +12 -3
  183. package/dist/tools/fs/shell-read-check.d.ts +12 -3
  184. package/dist/tools/fs/shell-read-check.js +28 -15
  185. package/dist/tools/fs/shell-read-device.d.ts +1 -1
  186. package/dist/tools/fs/shell-read-judge.d.ts +9 -4
  187. package/dist/tools/fs/shell-read-judge.js +1 -1
  188. package/dist/tools/loop-tick.d.ts +2 -2
  189. package/package.json +3 -1
  190. package/test/export-surface.snapshot.json +39 -1
  191. package/wire-changes/core@7.50.0.json +1699 -0
  192. package/dist/core/runner/persisted-path-rows.d.ts +0 -88
  193. package/dist/core/runner/persisted-path-rows.js +0 -108
package/dist/core/lsp.js CHANGED
@@ -1,8 +1,13 @@
1
1
  import { uriToPath } from "./lsp-protocol.js";
2
- import { anchorSpelling, foldRedundantPathSegments, pathFamilyOf } from "../tools/fs/safety.js";
2
+ import { anchorSpelling, foldRedundantPathSegments, pathFamilyOf, resolveKey, violationDetails, violationText } from "../tools/fs/safety.js";
3
+ import { seatForCall, sensitivePathDenyList } from "../tools/fs/read-deny.js";
4
+ import { pathTargetBaseOf } from "./effective-path-target.js";
5
+ import { DEFAULT_GATE_AWAIT_TIMEOUT_MS, withGateTimeout } from "./gate-bounded-await.js";
6
+ import { pathTargetOf } from "./tool-registry.js";
3
7
  import { Type } from "typebox";
4
8
  import { defineTool, errorResult } from "./tools.js";
5
9
  import { toolFace } from "./tool-catalog-entries.js";
10
+ import { thrownText } from "./thrown-value.js";
6
11
  export const LSP_OPERATIONS = [
7
12
  "goToDefinition", "findReferences", "hover", "documentSymbol", "workspaceSymbol",
8
13
  "goToImplementation", "prepareCallHierarchy", "incomingCalls", "outgoingCalls",
@@ -73,6 +78,63 @@ export function resolveLspPath(filePath, cwd) {
73
78
  return foldRedundantPathSegments(filePath);
74
79
  return foldRedundantPathSegments(anchorSpelling(pathFamilyOf({ root: cwd }), cwd, filePath));
75
80
  }
81
+ const LSP_PATH_TARGET = pathTargetOf("LSP");
82
+ export function lspBaseOf(readFace, env) {
83
+ return pathTargetBaseOf(LSP_PATH_TARGET, readFace !== undefined ? { root: readFace.rootCanonical, cwd: readFace.cwdRef?.current } : { cwd: env?.cwd });
84
+ }
85
+ function judgeLspRead(env, face, spelling, signal, admitted) {
86
+ return resolveKey(env, face.rootCanonical, spelling, signal, lspBaseOf(face), face.roots, admitted, face.readDeny, face.face);
87
+ }
88
+ const LSP_UNBINDABLE = "this environment cannot bind a read to the file that was checked";
89
+ export function lspReadGateOf(env, face, authorizedKey) {
90
+ const exempt = authorizedKey === undefined ? undefined : (key) => key === authorizedKey;
91
+ const keyOf = async (filePath, signal) => {
92
+ const r = await judgeLspRead(env, face, filePath, signal, exempt).catch(() => undefined);
93
+ return r?.ok === true ? r.key : undefined;
94
+ };
95
+ return async (filePath, signal) => {
96
+ await face.ready?.(undefined, signal);
97
+ const key = await keyOf(filePath, signal);
98
+ if (key === undefined)
99
+ return { ok: false, reason: "the read boundary refuses this file" };
100
+ if (env.openBound === undefined)
101
+ return { ok: false, reason: LSP_UNBINDABLE };
102
+ let held;
103
+ try {
104
+ held = await env.openBound(key, signal);
105
+ }
106
+ catch (e) {
107
+ return { ok: false, reason: thrownText(e) };
108
+ }
109
+ if (!held.ok)
110
+ return { ok: false, reason: held.error.message };
111
+ if ((await keyOf(filePath, signal)) !== key) {
112
+ await held.value.close();
113
+ return { ok: false, reason: "it resolved to a different file while it was being checked" };
114
+ }
115
+ return { ok: true, key, object: held.value };
116
+ };
117
+ }
118
+ export async function lspListedWithheld(env, face, uris, signal) {
119
+ const out = new Map();
120
+ if (face.readDeny === undefined || uris.length === 0)
121
+ return out;
122
+ await face.ready?.(undefined, signal);
123
+ const distinct = [...new Set(uris)];
124
+ const unreadable = { unreadable: true };
125
+ const judgeOne = async (uri) => {
126
+ const r = await resolveKey(env, face.rootCanonical, uriToPath(uri), signal, undefined, face.roots, undefined, face.readDeny, "open");
127
+ if (r.ok)
128
+ return undefined;
129
+ return r.violation.code === "read_path_denied" && r.violation.pattern !== undefined ? { pattern: r.violation.pattern } : unreadable;
130
+ };
131
+ const batch = Promise.all(distinct.map(async (uri) => [uri, await judgeOne(uri).catch(() => unreadable)]));
132
+ const judged = await withGateTimeout(batch, DEFAULT_GATE_AWAIT_TIMEOUT_MS, signal, "lspListedPaths").catch(() => undefined);
133
+ for (const [uri, withholding] of judged ?? distinct.map((uri) => [uri, unreadable]))
134
+ if (withholding !== undefined)
135
+ out.set(uri, withholding);
136
+ return out;
137
+ }
76
138
  export const MAX_LSP_FILE_BYTES = 10 * 1000 * 1000;
77
139
  function fmt(result, ctx) {
78
140
  switch (result.kind) {
@@ -139,10 +201,67 @@ async function filterIgnored(result, isIgnored) {
139
201
  }
140
202
  return result;
141
203
  }
204
+ async function withholdDenied(result, withheldOf) {
205
+ if (result.kind !== "locations" && result.kind !== "symbols")
206
+ return { result, note: "" };
207
+ const entries = result.kind === "locations" ? result.locations : result.symbols;
208
+ const withheld = await withheldOf(entries.map((e) => e.uri));
209
+ const patterns = [];
210
+ let denied = 0;
211
+ let unreadable = 0;
212
+ const keeps = (uri) => {
213
+ const w = withheld.get(uri);
214
+ if (w === undefined)
215
+ return true;
216
+ if ("pattern" in w) {
217
+ denied += 1;
218
+ if (!patterns.includes(w.pattern))
219
+ patterns.push(w.pattern);
220
+ }
221
+ else
222
+ unreadable += 1;
223
+ return false;
224
+ };
225
+ const kept = result.kind === "locations" ? { kind: "locations", locations: result.locations.filter((l) => keeps(l.uri)) } : { kind: "symbols", symbols: result.symbols.filter((s) => keeps(s.uri)) };
226
+ const n = (k) => `${k} result${k === 1 ? "" : "s"}`;
227
+ const note = (denied > 0 ? `\n[note: ${n(denied)} in files matching ${sensitivePathDenyList(patterns)} ${denied === 1 ? "was" : "were"} withheld]` : "") +
228
+ (unreadable > 0 ? `\n[note: ${n(unreadable)} withheld: the file${unreadable === 1 ? "" : "s"} could not be resolved to be judged against the read deny list]` : "");
229
+ return { result: kept, note };
230
+ }
231
+ const factsOfFileError = (code) => (code === "not_found" ? "missing" : code === "invalid" ? "not_a_file" : undefined);
232
+ async function heldTargetFacts(env, key, signal) {
233
+ if (env.openBound === undefined)
234
+ return undefined;
235
+ const held = await env.openBound(key, signal).catch(() => undefined);
236
+ if (held === undefined)
237
+ return undefined;
238
+ if (!held.ok)
239
+ return factsOfFileError(held.error.code);
240
+ try {
241
+ if (held.value.kind === "directory")
242
+ return "not_a_file";
243
+ const size = await held.value.size();
244
+ return size.ok ? { size: size.value } : undefined;
245
+ }
246
+ finally {
247
+ await held.value.close();
248
+ }
249
+ }
250
+ async function namedTargetFacts(env, target, signal) {
251
+ const info = await env.fileInfo(target, signal);
252
+ if (!info.ok)
253
+ return factsOfFileError(info.error.code);
254
+ if (info.value.kind === "directory")
255
+ return "not_a_file";
256
+ return info.value.kind === "file" ? { size: info.value.size } : undefined;
257
+ }
142
258
  const POSITION_OPS = new Set([
143
259
  "goToDefinition", "findReferences", "hover", "goToImplementation", "prepareCallHierarchy", "incomingCalls", "outgoingCalls",
144
260
  ]);
145
261
  export function createLspTool(manager, opts = {}) {
262
+ const faceSeat = opts.readFace;
263
+ if (faceSeat !== undefined && opts.env === undefined)
264
+ throw new TypeError("createLspTool: LspToolOptions.readFace needs the env it reads through (LspToolOptions.env)");
146
265
  return defineTool({
147
266
  name: "LSP",
148
267
  ...toolFace("lsp"),
@@ -173,6 +292,8 @@ export function createLspTool(manager, opts = {}) {
173
292
  query: Type.Optional(Type.String({ description: "Symbol query for workspaceSymbol (may be empty to browse broadly)." })),
174
293
  }),
175
294
  execute: async (args, ctx) => {
295
+ await faceSeat?.ready?.(ctx.toolCallId, ctx.signal);
296
+ const readFace = faceSeat === undefined ? undefined : { ...faceSeat, readDeny: seatForCall(faceSeat.readDeny, ctx.toolCallId), roots: seatForCall(faceSeat.roots, ctx.toolCallId), ready: undefined };
176
297
  const { operation, filePath, line, character, query } = args;
177
298
  if (POSITION_OPS.has(operation) && (line === undefined || character === undefined)) {
178
299
  return errorResult(`Error (LSP): the "${operation}" operation needs both line and character.`);
@@ -184,18 +305,24 @@ export function createLspTool(manager, opts = {}) {
184
305
  if (operation === "workspaceSymbol" && wsQuery === undefined) {
185
306
  return errorResult(`Error (LSP): the "workspaceSymbol" operation needs a query.`);
186
307
  }
187
- const target = resolveLspPath(filePath, opts.env?.cwd);
188
- if (opts.env) {
189
- const info = await opts.env.fileInfo(target, ctx.signal);
190
- if (!info.ok && info.error.code === "not_found")
191
- return errorResult(`Error (LSP): File does not exist: ${filePath}`);
192
- if (!info.ok && info.error.code === "invalid")
193
- return errorResult(`Error (LSP): Path is not a file: ${filePath}`);
194
- if (info.ok && info.value.kind === "directory")
195
- return errorResult(`Error (LSP): Path is not a file: ${filePath}`);
196
- if (info.ok && info.value.kind === "file" && info.value.size > MAX_LSP_FILE_BYTES) {
197
- return `File too large for LSP analysis (${Math.ceil(info.value.size / 1e6)}MB exceeds 10MB limit)`;
198
- }
308
+ const target = resolveLspPath(filePath, lspBaseOf(readFace, opts.env));
309
+ let gate;
310
+ let judgedKey;
311
+ if (readFace !== undefined && opts.env !== undefined) {
312
+ const admitted = readFace.admitted;
313
+ const r = await judgeLspRead(opts.env, readFace, filePath, ctx.signal, admitted && ((key) => admitted(ctx.toolCallId, key)));
314
+ if (!r.ok)
315
+ return errorResult(violationText("LSP", r.violation), violationDetails(r.violation));
316
+ judgedKey = r.key;
317
+ gate = lspReadGateOf(opts.env, readFace, r.key);
318
+ }
319
+ const facts = opts.env === undefined ? undefined : judgedKey !== undefined ? await heldTargetFacts(opts.env, judgedKey, ctx.signal) : await namedTargetFacts(opts.env, target, ctx.signal);
320
+ if (facts === "missing")
321
+ return errorResult(`Error (LSP): File does not exist: ${filePath}`);
322
+ if (facts === "not_a_file")
323
+ return errorResult(`Error (LSP): Path is not a file: ${filePath}`);
324
+ if (facts !== undefined && facts.size > MAX_LSP_FILE_BYTES) {
325
+ return `File too large for LSP analysis (${Math.ceil(facts.size / 1e6)}MB exceeds 10MB limit)`;
199
326
  }
200
327
  const session = await manager.sessionFor(target, ctx.signal, opts.env);
201
328
  if (!session) {
@@ -203,9 +330,12 @@ export function createLspTool(manager, opts = {}) {
203
330
  return `LSP is unavailable for "${filePath}" (${why}). Fall back to Grep/Read for this lookup.`;
204
331
  }
205
332
  const seamCharacter = character !== undefined ? character - 1 : undefined;
206
- const result = await session.request(operation, { filePath: target, line, character: seamCharacter, symbol: wsQuery }, ctx.signal);
207
- const final = toModelBase(await filterIgnored(result, opts.isPathIgnored));
208
- const text = fmt(final, { operation, filePath });
333
+ const result = await session.request(operation, { filePath: target, line, character: seamCharacter, symbol: wsQuery }, ctx.signal, gate);
334
+ const ignored = await filterIgnored(result, opts.isPathIgnored);
335
+ const env = opts.env;
336
+ const kept = readFace !== undefined && env !== undefined ? await withholdDenied(ignored, (uris) => lspListedWithheld(env, readFace, uris, ctx.signal)) : { result: ignored, note: "" };
337
+ const final = toModelBase(kept.result);
338
+ const text = fmt(final, { operation, filePath }) + kept.note;
209
339
  return final.kind === "none" && final.reason !== undefined && LSP_ERROR_REASONS.has(final.reason) ? errorResult(text) : text;
210
340
  },
211
341
  });
package/dist/core/mcp.js CHANGED
@@ -19,6 +19,7 @@ import { sliceHeadSafe, sliceTailSafe } from "./surrogate-safe-slice.js";
19
19
  import { truncateError } from "./tool-errors.js";
20
20
  import { delimitUntrusted, inlineUntrusted, sanitizeUntrustedText } from "./untrusted-text.js";
21
21
  import { discloseReminderShaped, observeReminderMarkEcho } from "./reminder-disclosure.js";
22
+ import { MARKED_BLOCK_AFTER_DATA } from "./reminder-mint.js";
22
23
  import { resolveMcpDeclaredResultSize, resolveMcpRequiresUserInteraction } from "./mcp-tool-meta.js";
23
24
  export { resolveMcpDeclaredResultSize } from "./mcp-tool-meta.js";
24
25
  import { withContentOrigin } from "./memory-engine/content-origin.js";
@@ -1797,7 +1798,7 @@ function intakeListedTools(listed, spec, client, health, imageResizer, reminderD
1797
1798
  if (d.defused)
1798
1799
  textIdx.forEach((ci, si) => (content[ci] = { type: "text", text: d.segments[si] }));
1799
1800
  if (d.trailer !== undefined)
1800
- content.push({ type: "text", text: d.trailer });
1801
+ content.push({ type: "text", text: `${MARKED_BLOCK_AFTER_DATA}${d.trailer}` });
1801
1802
  }
1802
1803
  if (sc !== undefined)
1803
1804
  return { content, details: { type: "mcp", structuredContent: sc }, terminate: false };
@@ -1,4 +1,5 @@
1
1
  import type { AgentMessage } from "../internal/harness-types.js";
2
+ import type { DocumentContent, ImageContent } from "../internal/llm.js";
2
3
  /**
3
4
  * Aggregate MEDIA (image) byte budget — the multimodal sibling of {@link AGGREGATE_TOOL_RESULT_BUDGET_CHARS}
4
5
  * (which only counts TEXT, design/64 §17.2). batch-C added image reads (`Read` → `ImageContent`); each is bounded
@@ -14,6 +15,19 @@ import type { AgentMessage } from "../internal/harness-types.js";
14
15
  /** Default aggregate inline-media budget (bytes). Generous — only the pathological "N parallel huge image reads"
15
16
  * case strips. Bounds the request while leaving normal multimodal use untouched. Overridable per-deployment. */
16
17
  export declare const AGGREGATE_MEDIA_BUDGET_BYTES: number;
18
+ /** The one reading of a deployment's media budget (`RunnerDeps.mediaByteCapBytes`, screened by {@link checkedMediaByteCap}
19
+ * at `new Runner`), for both of its readers — the request strip below and the `tool_end.output` projection: absent ⇒
20
+ * {@link AGGREGATE_MEDIA_BUDGET_BYTES}; a finite positive number ⇒ itself; `0` or `Infinity` ⇒ no budget, `Infinity`
21
+ * (a direct caller of {@link capAggregateMediaBytes} that passes a negative or `NaN` meets the same no-op). */
22
+ export declare function mediaBudgetBytes(cap: number | undefined): number;
23
+ /** The deployment's media budget, screened once at `new Runner`: absent, a non-negative number of bytes, or `Infinity`
24
+ * (`0` and `Infinity` both mean "no budget"). A non-number, `NaN` or a negative is refused (`config.invalid_argument`),
25
+ * never read as "no budget". */
26
+ export declare function checkedMediaByteCap(value: unknown, seat: string): void;
27
+ /** Decoded byte size of a base64 media payload (4 base64 chars ≈ 3 bytes). Approximate — fine for a budget.
28
+ * PDF read: `document` blocks (base64 PDFs) count against the SAME aggregate budget — same class of inline
29
+ * media bytes, same unbounded-accumulation failure mode. */
30
+ export declare function imageBytes(b: ImageContent | DocumentContent): number;
17
31
  /** Telemetry for one strip pass (mirrors CC's `tengu_media_byte_cap_stripped` fields). */
18
32
  export interface MediaStripInfo {
19
33
  totalBytes: number;
@@ -1,6 +1,18 @@
1
+ import { describeRejectedValue } from "./tool-search-setting.js";
1
2
  export const AGGREGATE_MEDIA_BUDGET_BYTES = 30 * 1024 * 1024;
3
+ export function mediaBudgetBytes(cap) {
4
+ const limit = cap ?? AGGREGATE_MEDIA_BUDGET_BYTES;
5
+ return Number.isFinite(limit) && limit > 0 ? limit : Infinity;
6
+ }
7
+ export function checkedMediaByteCap(value, seat) {
8
+ if (value === undefined || (typeof value === "number" && value >= 0))
9
+ return;
10
+ const e = new Error(`${seat} ${describeRejectedValue(value)} is not a media byte budget — expected a non-negative number of bytes (0 or Infinity: no budget), or omit it for the default ${AGGREGATE_MEDIA_BUDGET_BYTES}`);
11
+ e.code = "config.invalid_argument";
12
+ throw e;
13
+ }
2
14
  const MEDIA_STRIPPED_MARKER = "[media removed to fit the request media budget — re-read the file if you still need to see it]";
3
- function imageBytes(b) {
15
+ export function imageBytes(b) {
4
16
  return typeof b.data === "string" ? Math.floor((b.data.length * 3) / 4) : 0;
5
17
  }
6
18
  function blocksOf(m) {
@@ -8,8 +20,8 @@ function blocksOf(m) {
8
20
  return Array.isArray(content) ? content : undefined;
9
21
  }
10
22
  export function capAggregateMediaBytes(messages, opts) {
11
- const { limitBytes } = opts;
12
- if (!Number.isFinite(limitBytes) || limitBytes <= 0)
23
+ const limitBytes = mediaBudgetBytes(opts.limitBytes);
24
+ if (limitBytes === Infinity)
13
25
  return messages;
14
26
  let total = 0;
15
27
  for (const m of messages) {
@@ -21,8 +21,9 @@
21
21
  * auto lane's task-boundary tick), never on a background schedule of core's own.
22
22
  */
23
23
  import type { EngineNotice } from "../types.js";
24
- import { type ConsolidationGateRow, type ConsolidationRunStopReason, type MemoryConsolidationOptions } from "./consolidation.js";
24
+ import { type ConsolidationGateRow, CONSOLIDATION_WRITE_REFUSED_CODE, type ConsolidationRunStopReason, type MemoryConsolidationOptions } from "./consolidation.js";
25
25
  import { type ConsolidationPlanFoldEvidence, type ConsolidationPlanSummary } from "./engine.js";
26
+ import type { SyntheticWriteVerdict } from "../synthetic-read-verdict.js";
26
27
  import { mintLlmConsolidationPlan, type ConsolidationDriveCycleRow, type ConsolidationDriveEngine, type ConsolidationFoldState, type DistillerChatFn, type LlmConsolidationPlan, type LlmConsolidationPlanProduct, type LlmDistillerContract } from "./distiller.js";
27
28
  export declare const CONSOLIDATION_DRIVER_RUNS_FILE = "distiller-runs.json";
28
29
  /** §1.6-4 — the mint archive: what was shown to the model and what it answered, verbatim, per run
@@ -142,14 +143,35 @@ export interface ConsolidationDriverEngine extends ConsolidationDriveEngine {
142
143
  listConsolidationPlans(): ConsolidationPlanSummary[];
143
144
  readConsolidationPlanFoldEvidence(planId: string): ConsolidationPlanFoldEvidence;
144
145
  readConsolidationFoldState(scope: string): Promise<ConsolidationFoldState>;
146
+ /** The canonical directory a commit lands the scope's products in (the write judge's early question). */
147
+ consolidationWriteHome(scope: string): string;
145
148
  readonly controlPlaneRoot: string;
146
149
  }
150
+ /** One write the judge is asked about: `path` is a file (or the library directory) of `scope`'s library, absolute and canonical. */
151
+ export interface ConsolidationWriteTarget {
152
+ scope: string;
153
+ path: string;
154
+ }
155
+ /** The write judge's seat: one target in, the synthetic-write judge's verdict out (only `no_objection` permits the write). */
156
+ export type ConsolidationWriteJudge = (target: ConsolidationWriteTarget) => Promise<SyntheticWriteVerdict> | SyntheticWriteVerdict;
157
+ export { CONSOLIDATION_WRITE_REFUSED_CODE };
147
158
  export interface RunMemoryConsolidationOptions {
148
159
  /** The explicit model seat (design/339 §1.1: the ENGINE holds none). Build one with
149
160
  * `openAiCompatChatSeat` or hand any JSON-only completion function. */
150
161
  chat: DistillerChatFn;
151
162
  /** The explicit model id, recorded into the run and the archive (alias spellings refuse). */
152
163
  model: string;
164
+ /** The write judge — REQUIRED. A run writes the library with nobody watching, so it asks the judgment a foreground write
165
+ * into the same library meets: the person's standing path rules and the deployment's policy, read through the ONE judge
166
+ * (`createSyntheticWriteJudge`; the Runner's auto lane passes the leg's own). It is asked in two places over the run, with the same
167
+ * seat and the same refusal: once for the library directory before any claim, seat or model call (an early refusal that
168
+ * spends nothing), and — the authority — where a plan is applied (a commit, a replay of a suspended plan, the drive's
169
+ * recovery), at the backend's final spelling of every product before any is written: a refusal there writes nothing and
170
+ * discards the plan.
171
+ * Anything but `no_objection` — a deny, an ask (nobody is present to answer it), an unreadable rule source, a
172
+ * throw — refuses the run with `memory.consolidation_write_refused`, and nothing is written. A host that holds no rules
173
+ * says so by passing a judge that answers `no_objection`; an absent seat refuses, it never reads as "no rules". */
174
+ writeJudge: ConsolidationWriteJudge;
153
175
  /** Audit locator recorded into the archive when the seat is endpoint-shaped. */
154
176
  baseUrl?: string;
155
177
  /** The frozen prompt contract; default = the product v1 (dl-3 byte-identical). A different
@@ -3,7 +3,7 @@ import { uuidv7 } from "../../internal/harness.js";
3
3
  import { atomicWriteFileSync, ensureDirExists, lockedStrictUpdate, readStrictSidecar, enqueueMemoryAnnouncement, ControlPlaneCorruptError } from "./layout.js";
4
4
  import { inlineUntrusted } from "../untrusted-text.js";
5
5
  import { deliverEngineNotice } from "../engine-notice.js";
6
- import { CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, screenConsolidationOptions, supersessionFuseCeiling, updateConsolidationGate, recommendationUnconsumed, } from "./consolidation.js";
6
+ import { CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS, screenConsolidationOptions, supersessionFuseCeiling, updateConsolidationGate, recommendationUnconsumed, CONSOLIDATION_WRITE_REFUSED_CODE, ConsolidationRefusedError, } from "./consolidation.js";
7
7
  import { memoryConsolidationIncompleteNotice, memoryConsolidationWithheldNotice } from "./engine.js";
8
8
  import { asThrownError, thrownText } from "../thrown-value.js";
9
9
  import { CONSOLIDATION_DRIVE_ABORT, CONSOLIDATION_SNAPSHOT_PARKED, MEMORY_DISTILLER_CONTRACT_V1, detectCleanArmVerbatimLeak, driveConsolidationToFixpoint, isAliasModelId, llmPlanDistiller, mintExposurePartitionedPlan, mintLlmConsolidationPlan, normalizedCandidateRev, } from "./distiller.js";
@@ -61,6 +61,7 @@ export function archiveDistillerPlan(controlDir, runId, plan, attempt = 1) {
61
61
  atomicWriteFileSync(join(controlDir, CONSOLIDATION_DRIVER_PLANS_DIR, name), `${JSON.stringify(plan, null, 2)}\n`);
62
62
  return name;
63
63
  }
64
+ export { CONSOLIDATION_WRITE_REFUSED_CODE };
64
65
  function driverConfigRefusal(message) {
65
66
  const e = new Error(message);
66
67
  e.code = "config.memory_consolidation_driver";
@@ -93,6 +94,33 @@ const codeOf = (err) => {
93
94
  const c = err?.code;
94
95
  return typeof c === "string" ? c : undefined;
95
96
  };
97
+ async function refuseUnlessWriteAllowed(judge, target, consequence) {
98
+ let verdict;
99
+ let threw;
100
+ try {
101
+ verdict = await judge(target);
102
+ }
103
+ catch (err) {
104
+ threw = thrownText(err);
105
+ }
106
+ const answer = typeof verdict === "object" && verdict !== null ? verdict : undefined;
107
+ if (threw === undefined && answer?.verdict === "no_objection")
108
+ return;
109
+ const text = (v) => (typeof v === "string" && v !== "" ? inlineUntrusted(v, 200) : undefined);
110
+ const where = text(answer?.layer);
111
+ let why;
112
+ if (threw !== undefined)
113
+ why = `the write judge threw (${inlineUntrusted(threw, 200)}), so whether a standing rule forbids this write is unknown`;
114
+ else if (answer?.verdict === "deny")
115
+ why = `a standing rule layer${where !== undefined ? ` (${where})` : ""} refuses writes into this library${text(answer.message) !== undefined ? `: ${text(answer.message)}` : ""}`;
116
+ else if (answer?.verdict === "ask")
117
+ why = `a standing rule layer${where !== undefined ? ` (${where})` : ""} would ask a person before a write into this library, and a background run has nobody to answer${text(answer.message) !== undefined ? `: ${text(answer.message)}` : ""}`;
118
+ else if (answer?.verdict === "unanswerable")
119
+ why = `the write judge could not answer${where !== undefined ? ` at ${where}` : ""}${text(answer.cause) !== undefined ? ` (${text(answer.cause)})` : ""}${text(answer.reason) !== undefined ? `: ${text(answer.reason)}` : ""}, so whether a standing rule forbids this write is unknown`;
120
+ else
121
+ why = "the write judge answered something outside its closed set of verdicts";
122
+ throw new ConsolidationRefusedError(CONSOLIDATION_WRITE_REFUSED_CODE, `memory consolidation refused for scope ${inlineUntrusted(target.scope, 80)} (${inlineUntrusted(target.path, 200)}): ${why}${consequence !== undefined ? ` — ${consequence}` : ""}`);
123
+ }
96
124
  const MAX_THROTTLE_WAITS_PER_SNAPSHOT = 4;
97
125
  const MAX_BUSY_WAITS = 2;
98
126
  export async function runMemoryConsolidationDriver(engine, scope, opts) {
@@ -127,7 +155,24 @@ export async function runMemoryConsolidationDriver(engine, scope, opts) {
127
155
  };
128
156
  const throttleWindowMs = Math.min(screened.minRunIntervalMs, CONSOLIDATION_FORCE_THROTTLE_FLOOR_MS);
129
157
  const controlDir = engine.controlPlaneRoot;
130
- const parkedSeat = await recoverSeat(engine, scope, { sleep, windowMs: throttleWindowMs });
158
+ if (typeof opts.writeJudge !== "function")
159
+ throw driverConfigRefusal(`runMemoryConsolidationDriver needs an explicit write judge (opts.writeJudge), got ${typeof opts.writeJudge} — a run writes the library with nobody watching, so it asks the judgment a foreground write meets before it starts; an absent seat is refused, never read as "no rules apply" (a host that holds no rules passes a judge that answers no_objection).`);
160
+ let libraryHome;
161
+ try {
162
+ libraryHome = engine.consolidationWriteHome(scope);
163
+ }
164
+ catch (err) {
165
+ throw new ConsolidationRefusedError(CONSOLIDATION_WRITE_REFUSED_CODE, `memory consolidation refused for scope ${inlineUntrusted(scope, 80)}: where this run would write could not be derived (${inlineUntrusted(thrownText(err), 200)}), so whether a standing rule forbids it is unknown — no run was started and nothing was written.`);
166
+ }
167
+ await refuseUnlessWriteAllowed(opts.writeJudge, { scope, path: libraryHome }, "no run was started and nothing was written.");
168
+ const writeGate = (target) => refuseUnlessWriteAllowed(opts.writeJudge, target);
169
+ let refusedSeat = undefined;
170
+ const parkedSeat = await recoverSeat(engine, scope, { sleep, windowMs: throttleWindowMs, writeGate }).catch((err) => {
171
+ if (!(err instanceof ConsolidationRefusedError) || err.code !== CONSOLIDATION_WRITE_REFUSED_CODE)
172
+ throw err;
173
+ refusedSeat = err;
174
+ return undefined;
175
+ });
131
176
  const openFold = await engine.readConsolidationFoldState(scope);
132
177
  const { row: run, resumed } = claimDriverRun(controlDir, scope, () => ({
133
178
  runId: uuidv7(),
@@ -258,6 +303,8 @@ export async function runMemoryConsolidationDriver(engine, scope, opts) {
258
303
  if (parkedSeat !== undefined) {
259
304
  return settle("parked", undefined, parkedSeat);
260
305
  }
306
+ if (refusedSeat !== undefined)
307
+ return settle("refused", undefined, `reconcileConsolidation refused the plan an earlier run left seated: ${refusedSeat.code}: ${refusedSeat.message}`);
261
308
  let staleResidue = [];
262
309
  let staleResidueMemberIds = new Set();
263
310
  const distill = async (candidates, snapshot, s) => {
@@ -425,7 +472,7 @@ export async function runMemoryConsolidationDriver(engine, scope, opts) {
425
472
  if (busyWaits >= MAX_BUSY_WAITS)
426
473
  throw err;
427
474
  busyWaits += 1;
428
- const parked = await recoverSeat(engine, scope, { sleep, windowMs: throttleWindowMs });
475
+ const parked = await recoverSeat(engine, scope, { sleep, windowMs: throttleWindowMs, writeGate });
429
476
  if (parked !== undefined) {
430
477
  const e = new Error(parked);
431
478
  e[CONSOLIDATION_SNAPSHOT_PARKED] = true;
@@ -448,6 +495,7 @@ export async function runMemoryConsolidationDriver(engine, scope, opts) {
448
495
  requestIdPrefix: run.requestId,
449
496
  maxCycles: opts.maxCycles ?? 64,
450
497
  takeSnapshot,
498
+ writeGate,
451
499
  onCycle: (row) => {
452
500
  cyclesSeen.push(row);
453
501
  if (row.committed.products > 0 || row.committed.superseded > 0)
@@ -564,8 +612,8 @@ function collectRunFoldEvidence(engine, scope, requestId) {
564
612
  }
565
613
  return { targets, skipped };
566
614
  }
567
- async function recoverSeat(engine, scope, { sleep, windowMs }) {
568
- await engine.reconcileConsolidation(scope);
615
+ async function recoverSeat(engine, scope, { sleep, windowMs, writeGate }) {
616
+ await engine.reconcileConsolidation(scope, { writeGate });
569
617
  let seat = engine.readConsolidationGate(scope)?.openPlanId;
570
618
  if (seat === undefined)
571
619
  return undefined;
@@ -574,7 +622,7 @@ async function recoverSeat(engine, scope, { sleep, windowMs }) {
574
622
  return `plan ${seat} holds the ${JSON.stringify(scope)} seat in a ${summary.corrupt === true ? "corrupt" : "conflict"} state — resolveConsolidationPlan(planId, "retry" | "discard") is the host valve; the run parks rather than stacking a second plan on the seat`;
575
623
  }
576
624
  await sleep(windowMs);
577
- await engine.reconcileConsolidation(scope);
625
+ await engine.reconcileConsolidation(scope, { writeGate });
578
626
  seat = engine.readConsolidationGate(scope)?.openPlanId;
579
627
  if (seat === undefined)
580
628
  return undefined;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * The write gate, asked at the apply for every entry file a plan writes or removes: the backend's FINAL spelling of each (as its
3
+ * `admit`), or the one the apply knows for a backend that cannot admit. Kept beside the engine rather than in it; everything about
4
+ * how a refusal is shaped lives here — including what it says was written, which only the apply knows ({@link refusalNamingWritten}).
5
+ */
6
+ import { ConsolidationRefusedError, type ConsolidationDirectedPatch, type ConsolidationWriteGate } from "./consolidation.js";
7
+ import type { AdmitLandings, PatchLanding } from "./landing-admission.js";
8
+ import type { MemoryBackend, MemoryEntry } from "./types.js";
9
+ /** The refusal of a verb that may apply a plan and was handed no write gate — thrown before the verb claims or changes anything. */
10
+ export declare const CONSOLIDATION_WRITE_GATE_REQUIRED_CODE = "config.memory_consolidation_write_gate";
11
+ /** A verb that may apply a plan carries the write gate; absent, it refuses before it claims or changes anything (a host that holds
12
+ * no rules passes a gate that resolves, and so says it). */
13
+ export declare function requireWriteGate(gate: unknown, verb: string): asserts gate is ConsolidationWriteGate;
14
+ /**
15
+ * Where a plan's pending directed patches write or remove files, as the store holds the entries now: an update's target and, when
16
+ * it moves the entry, the file it leaves; the file a delete removes. An id the store no longer holds is no write of a delete.
17
+ */
18
+ export declare function directedLandings(directed: readonly ConsolidationDirectedPatch[], getByIds: (ids: readonly string[]) => Promise<MemoryEntry[]>): Promise<PatchLanding[]>;
19
+ /** What of a plan the store already holds: products it carries (the ids are the plan's own, minted at the freeze, so the store
20
+ * holding one means the plan wrote it) and directed patches whose planned state stands. */
21
+ export interface PlanWritten {
22
+ readonly products: readonly PatchLanding[];
23
+ readonly directed: readonly string[];
24
+ }
25
+ /**
26
+ * {@link PlanWritten} for `plan`, read through `committed` once `backend` has replayed every commit it holds (a locked entry that
27
+ * writes nothing runs its recovery first) — what an earlier pass, which a crash or a failure cut short before its book was written,
28
+ * or this pass before a refusal, put in the store. Undefined when the store could not be read (a refusal then says so).
29
+ */
30
+ export declare function planWritten(plan: {
31
+ readonly products: readonly {
32
+ readonly id: string;
33
+ }[];
34
+ readonly directed: readonly ConsolidationDirectedPatch[];
35
+ }, backend: Pick<MemoryBackend, "applyPatches">, committed: Pick<MemoryBackend, "getByIds">): Promise<PlanWritten | undefined>;
36
+ /**
37
+ * The write gate's refusal at the apply, completed with what the plan has written: the gate knows why it refused; the apply knows
38
+ * that nothing further is written and what the store already holds of the plan (`written`; undefined = could not be read).
39
+ */
40
+ export declare function refusalNamingWritten(err: ConsolidationRefusedError, written: PlanWritten | undefined): ConsolidationRefusedError;
41
+ /**
42
+ * The write gate as one apply's `admit`: every landing (`<slug>.md` in its scope's library directory) is put to the gate, all of
43
+ * them before any is written; the first refusal rejects with the gate's own error. A landing admitted once in the apply is not
44
+ * asked again (the apply asks its directed targets up front, and the backend names them again when it plans their batch).
45
+ */
46
+ export declare function admitLandingsThrough(gate: ConsolidationWriteGate, homeOf: (scope: string) => string): AdmitLandings;
@@ -0,0 +1,82 @@
1
+ import { join } from "node:path";
2
+ import { inlineUntrusted } from "../untrusted-text.js";
3
+ import { thrownText } from "../thrown-value.js";
4
+ import { CONSOLIDATION_WRITE_REFUSED_CODE, ConsolidationRefusedError } from "./consolidation.js";
5
+ export const CONSOLIDATION_WRITE_GATE_REQUIRED_CODE = "config.memory_consolidation_write_gate";
6
+ export function requireWriteGate(gate, verb) {
7
+ if (typeof gate === "function")
8
+ return;
9
+ throw new ConsolidationRefusedError(CONSOLIDATION_WRITE_GATE_REQUIRED_CODE, `${verb} needs a write gate (opts.writeGate), got ${typeof gate}: it applies a plan's writes, each asked of the gate where it lands — an absent gate is refused, never read as "no rules apply"`);
10
+ }
11
+ async function askConsolidationWriteGate(gate, scope, file, homeOf) {
12
+ let path;
13
+ try {
14
+ path = join(homeOf(), file);
15
+ }
16
+ catch (err) {
17
+ throw new ConsolidationRefusedError(CONSOLIDATION_WRITE_REFUSED_CODE, `memory consolidation refused for scope ${inlineUntrusted(scope, 80)}: where this plan would write could not be derived (${inlineUntrusted(thrownText(err), 200)}), so whether a standing rule forbids it is unknown`);
18
+ }
19
+ try {
20
+ await gate({ scope, path });
21
+ }
22
+ catch (err) {
23
+ if (err instanceof ConsolidationRefusedError)
24
+ throw err;
25
+ throw new ConsolidationRefusedError(CONSOLIDATION_WRITE_REFUSED_CODE, `memory consolidation refused for scope ${inlineUntrusted(scope, 80)}: the write gate failed (${inlineUntrusted(thrownText(err), 200)}), so whether a standing rule forbids writing ${inlineUntrusted(path, 200)} is unknown`);
26
+ }
27
+ }
28
+ export async function directedLandings(directed, getByIds) {
29
+ if (directed.length === 0)
30
+ return [];
31
+ const held = new Map((await getByIds(directed.map((d) => d.id))).map((e) => [e.id, e]));
32
+ return directed.flatMap((d) => {
33
+ const at = held.get(d.id);
34
+ const from = at !== undefined ? [{ id: d.id, scope: at.scope, slug: at.slug }] : [];
35
+ if (d.op === "delete" || d.entry === undefined)
36
+ return from;
37
+ const to = { id: d.id, scope: d.entry.scope, slug: d.entry.slug };
38
+ return [...from.filter((f) => f.scope !== to.scope || f.slug !== to.slug), to];
39
+ });
40
+ }
41
+ export async function planWritten(plan, backend, committed) {
42
+ try {
43
+ await backend.applyPatches([]);
44
+ const held = new Map((await committed.getByIds([...plan.products.map((p) => p.id), ...plan.directed.map((d) => d.id)])).map((e) => [e.id, e]));
45
+ const products = plan.products.flatMap((p) => {
46
+ const e = held.get(p.id);
47
+ return e === undefined ? [] : [{ id: p.id, scope: e.scope, slug: e.slug }];
48
+ });
49
+ const standing = (id) => (held.get(id)?.frontmatter.deleted === true ? undefined : held.get(id));
50
+ const directed = plan.directed.filter((d) => (d.op === "delete" ? standing(d.id) === undefined : standing(d.id)?.rev === d.plannedPostRev)).map((d) => d.id);
51
+ return { products, directed };
52
+ }
53
+ catch {
54
+ return undefined;
55
+ }
56
+ }
57
+ export function refusalNamingWritten(err, written) {
58
+ const reasonLines = err.reasons.length > 0 ? `\n${err.reasons.map((r) => ` - ${r}`).join("\n")}` : "";
59
+ const why = err.message.slice(0, err.message.length - reasonLines.length);
60
+ const stays = [
61
+ ...(written !== undefined && written.products.length > 0 ? [`${written.products.length} of its products — ${written.products.map((l) => inlineUntrusted(`${l.id} (${l.scope}/${l.slug}.md)`, 200)).join(", ")}`] : []),
62
+ ...(written !== undefined && written.directed.length > 0 ? [`${written.directed.length} of its directed changes, already in their planned state — ${written.directed.map((id) => inlineUntrusted(id, 80)).join(", ")}`] : []),
63
+ ];
64
+ const outcome = written === undefined
65
+ ? "nothing further of it was written, and whether any of it had been written already could not be read"
66
+ : stays.length === 0
67
+ ? "nothing of it was written"
68
+ : `nothing further of it was written; what had already landed stays: ${stays.join("; ")}`;
69
+ return new ConsolidationRefusedError(err.code, `${why} — this plan is discarded and ${outcome}.`, err.reasons);
70
+ }
71
+ export function admitLandingsThrough(gate, homeOf) {
72
+ const admitted = new Set();
73
+ return async (landings) => {
74
+ for (const l of landings) {
75
+ const key = `${l.scope}\u0000${l.slug}`;
76
+ if (admitted.has(key))
77
+ continue;
78
+ await askConsolidationWriteGate(gate, l.scope, `${l.slug}.md`, () => homeOf(l.scope));
79
+ admitted.add(key);
80
+ }
81
+ };
82
+ }
@@ -445,6 +445,9 @@ export interface ConsolidationPlanFile {
445
445
  state: "pending" | "settled" | "abandoned";
446
446
  }>;
447
447
  state: ConsolidationPlanState;
448
+ /** The token of the applier that holds this plan's application (the LAST claimer holds it; an earlier one is stale and
449
+ * stops). Written by the engine at the start of each apply; absent on a plan nobody has applied yet. */
450
+ applier?: string;
448
451
  audit: Array<{
449
452
  at: number;
450
453
  event: string;
@@ -497,6 +500,20 @@ export declare function readIntentCredentials(controlDir: string): Record<string
497
500
  /** Derive a product's slug from its proposed name (memory filename charset; collisions are the
498
501
  * backend's deterministic -n suffix). */
499
502
  export declare function deriveProductSlug(name: string | undefined, fallback: string): string;
503
+ /** The refusal code of a run (or one cycle's commit) a write judge stopped. */
504
+ export declare const CONSOLIDATION_WRITE_REFUSED_CODE = "memory.consolidation_write_refused";
505
+ /**
506
+ * The write gate a plan's application may carry, asked per product with the file it lands in. It resolves iff that write is
507
+ * permitted and rejects with a {@link ConsolidationRefusedError} (code {@link CONSOLIDATION_WRITE_REFUSED_CODE}) otherwise.
508
+ * It is asked where the plan is APPLIED, whichever entry applies it (commit, `reconcileConsolidation`'s replay, the drive's
509
+ * recovery), at the backend's FINAL landing spelling of every product before any is written; a refusal there writes nothing and
510
+ * DISCARDS the plan, so a suspended plan is no grant that outlives the rule that now forbids it. Products an earlier pass of the
511
+ * plan landed (a crash cut it short between two products) stay; the refusal the apply throws names them.
512
+ */
513
+ export type ConsolidationWriteGate = (target: {
514
+ scope: string;
515
+ path: string;
516
+ }) => Promise<void>;
500
517
  /** The refusal error every consolidation verb throws (structured: stable `code` + the per-item
501
518
  * disclosure list — G5/G7/G14's "结构化拒+披露" shape). */
502
519
  export declare class ConsolidationRefusedError extends Error {
@@ -359,6 +359,7 @@ export function deriveProductSlug(name, fallback) {
359
359
  const base = (name ?? "").toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^[-.]+|[-.]+$/g, "");
360
360
  return base.length > 0 ? base : fallback;
361
361
  }
362
+ export const CONSOLIDATION_WRITE_REFUSED_CODE = "memory.consolidation_write_refused";
362
363
  export class ConsolidationRefusedError extends Error {
363
364
  code;
364
365
  reasons;