blun-king-cli 9.1.642 → 9.1.644

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 (273) hide show
  1. package/LIESMICH.txt +18 -0
  2. package/README.md +20 -6
  3. package/bin/blun.js +3 -3
  4. package/bin/king.js +3 -3
  5. package/bin/launcher-mode.js +125 -126
  6. package/bin/launcher-runtime.js +603 -616
  7. package/bin/plugin-bootstrap.js +339 -405
  8. package/bin/rename-private-staging.cjs +2 -2
  9. package/bin/retired-bundled-plugins.cjs +84 -0
  10. package/bin/startup-security-host.cjs +105 -0
  11. package/bin/startup-security.cjs +9 -105
  12. package/bin/startup-warning-policy.cjs +22 -0
  13. package/bin/update-lease-installer.cjs +125 -0
  14. package/bin/update-lease-sockets.cjs +228 -0
  15. package/bin/update-lease.js +22 -327
  16. package/bin/verify-bundled-agent-sources.cjs +2 -4
  17. package/blun.mjs +78 -193
  18. package/bundled-agent-sources.json +1 -552
  19. package/package.json +4 -6
  20. package/tool-liveness642/index.cjs +7 -0
  21. package/tool-liveness642/queue-delivery.cjs +109 -0
  22. package/tool-liveness642/receipt.json +40 -0
  23. package/tool-liveness642/shell-yield.cjs +43 -0
  24. package/tool-liveness642/task-wait.cjs +45 -0
  25. package/versionierung/BELEG.json +43 -0
  26. package/versionierung/LIESMICH.md +5 -0
  27. package/versionierung/urspruenglicher-overlay-beleg.json +39 -0
  28. package/worker-host.mjs +48 -128
  29. package/agent-spine-plugin/.claude-plugin/marketplace.json +0 -20
  30. package/agent-spine-plugin/.claude-plugin/plugin.json +0 -14
  31. package/agent-spine-plugin/.codex-plugin/plugin.json +0 -47
  32. package/agent-spine-plugin/.mcp.json +0 -8
  33. package/agent-spine-plugin/CHANGELOG.md +0 -1614
  34. package/agent-spine-plugin/LICENSE +0 -186
  35. package/agent-spine-plugin/README.md +0 -432
  36. package/agent-spine-plugin/assets/agentspine-banner.svg +0 -32
  37. package/agent-spine-plugin/bin/agentspine-mcp.js +0 -4
  38. package/agent-spine-plugin/bin/agentspine.js +0 -7
  39. package/agent-spine-plugin/blun.plugin.json +0 -68
  40. package/agent-spine-plugin/docs/acceptance.md +0 -61
  41. package/agent-spine-plugin/docs/artifact-evaluation.md +0 -93
  42. package/agent-spine-plugin/docs/host-integration.md +0 -193
  43. package/agent-spine-plugin/docs/preflight-recall.md +0 -71
  44. package/agent-spine-plugin/docs/preservation-contract.md +0 -53
  45. package/agent-spine-plugin/docs/quality-gates.md +0 -50
  46. package/agent-spine-plugin/docs/session-timeline.md +0 -112
  47. package/agent-spine-plugin/docs/source-roots.md +0 -113
  48. package/agent-spine-plugin/docs/world-model.md +0 -119
  49. package/agent-spine-plugin/hooks/codex.json +0 -47
  50. package/agent-spine-plugin/hooks/hooks.json +0 -106
  51. package/agent-spine-plugin/hooks/version.json +0 -5
  52. package/agent-spine-plugin/package.json +0 -77
  53. package/agent-spine-plugin/scripts/check-codex-install.js +0 -226
  54. package/agent-spine-plugin/scripts/check-hosts.js +0 -208
  55. package/agent-spine-plugin/scripts/check-install-hook.js +0 -227
  56. package/agent-spine-plugin/scripts/check-install-selfstarter.js +0 -154
  57. package/agent-spine-plugin/scripts/check-install.js +0 -478
  58. package/agent-spine-plugin/scripts/check-line-budget.js +0 -58
  59. package/agent-spine-plugin/scripts/check-syntax.js +0 -29
  60. package/agent-spine-plugin/scripts/github-actions.js +0 -11
  61. package/agent-spine-plugin/scripts/hermetic-process.js +0 -191
  62. package/agent-spine-plugin/scripts/release-check.js +0 -150
  63. package/agent-spine-plugin/scripts/run-acceptance.js +0 -19
  64. package/agent-spine-plugin/scripts/run-checks.js +0 -50
  65. package/agent-spine-plugin/scripts/run-tests-hermetic.js +0 -112
  66. package/agent-spine-plugin/skills/agent-spine/SKILL.md +0 -99
  67. package/agent-spine-plugin/spine-example/1-identity.md +0 -12
  68. package/agent-spine-plugin/spine-example/2-voice.md +0 -6
  69. package/agent-spine-plugin/spine-example/3-conduct.md +0 -8
  70. package/agent-spine-plugin/spine-example/4-history.md +0 -4
  71. package/agent-spine-plugin/src/cli-agent.js +0 -315
  72. package/agent-spine-plugin/src/cli-attention.js +0 -95
  73. package/agent-spine-plugin/src/cli-autonomy.js +0 -36
  74. package/agent-spine-plugin/src/cli-common.js +0 -71
  75. package/agent-spine-plugin/src/cli-continuity.js +0 -116
  76. package/agent-spine-plugin/src/cli-core.js +0 -128
  77. package/agent-spine-plugin/src/cli-diagnostics.js +0 -291
  78. package/agent-spine-plugin/src/cli-host.js +0 -21
  79. package/agent-spine-plugin/src/cli-learning.js +0 -320
  80. package/agent-spine-plugin/src/cli-premortem.js +0 -16
  81. package/agent-spine-plugin/src/cli-sharing.js +0 -230
  82. package/agent-spine-plugin/src/cli.js +0 -181
  83. package/agent-spine-plugin/src/codex-reader-launcher.js +0 -182
  84. package/agent-spine-plugin/src/hook.js +0 -500
  85. package/agent-spine-plugin/src/index.js +0 -108
  86. package/agent-spine-plugin/src/lib/acceptance.js +0 -435
  87. package/agent-spine-plugin/src/lib/action-lesson-recall.js +0 -118
  88. package/agent-spine-plugin/src/lib/attention-context.js +0 -167
  89. package/agent-spine-plugin/src/lib/attention-events.js +0 -113
  90. package/agent-spine-plugin/src/lib/attention-privacy.js +0 -72
  91. package/agent-spine-plugin/src/lib/attention-schema.js +0 -164
  92. package/agent-spine-plugin/src/lib/attention-storage.js +0 -93
  93. package/agent-spine-plugin/src/lib/attention.js +0 -164
  94. package/agent-spine-plugin/src/lib/audit-premortem.js +0 -223
  95. package/agent-spine-plugin/src/lib/audit.js +0 -401
  96. package/agent-spine-plugin/src/lib/authentication.js +0 -515
  97. package/agent-spine-plugin/src/lib/autonomy-policy.js +0 -110
  98. package/agent-spine-plugin/src/lib/autonomy-store.js +0 -202
  99. package/agent-spine-plugin/src/lib/autonomy.js +0 -8
  100. package/agent-spine-plugin/src/lib/briefing.js +0 -493
  101. package/agent-spine-plugin/src/lib/catalog-document-read.js +0 -51
  102. package/agent-spine-plugin/src/lib/catalog.js +0 -167
  103. package/agent-spine-plugin/src/lib/channel-continuity.js +0 -19
  104. package/agent-spine-plugin/src/lib/channel-runtime.js +0 -665
  105. package/agent-spine-plugin/src/lib/codex-installation.js +0 -231
  106. package/agent-spine-plugin/src/lib/codex-skill-installation.js +0 -211
  107. package/agent-spine-plugin/src/lib/context.js +0 -156
  108. package/agent-spine-plugin/src/lib/continuity.js +0 -338
  109. package/agent-spine-plugin/src/lib/coordination.js +0 -577
  110. package/agent-spine-plugin/src/lib/delivery-agent-usage.js +0 -231
  111. package/agent-spine-plugin/src/lib/delivery-assignment.js +0 -220
  112. package/agent-spine-plugin/src/lib/delivery-command-actions.js +0 -462
  113. package/agent-spine-plugin/src/lib/delivery-knowledge.js +0 -78
  114. package/agent-spine-plugin/src/lib/delivery-premortem-binding.js +0 -107
  115. package/agent-spine-plugin/src/lib/delivery-premortem-closure.js +0 -171
  116. package/agent-spine-plugin/src/lib/delivery-premortem-codec.js +0 -45
  117. package/agent-spine-plugin/src/lib/delivery-premortem-correction.js +0 -101
  118. package/agent-spine-plugin/src/lib/delivery-premortem-file.js +0 -21
  119. package/agent-spine-plugin/src/lib/delivery-premortem-index.js +0 -493
  120. package/agent-spine-plugin/src/lib/delivery-premortem-inspection.js +0 -37
  121. package/agent-spine-plugin/src/lib/delivery-premortem-recovery.js +0 -120
  122. package/agent-spine-plugin/src/lib/delivery-premortem-rejection.js +0 -65
  123. package/agent-spine-plugin/src/lib/delivery-premortem-results.js +0 -28
  124. package/agent-spine-plugin/src/lib/delivery-premortem-session-guard.js +0 -46
  125. package/agent-spine-plugin/src/lib/delivery-premortem-write-ledger.js +0 -285
  126. package/agent-spine-plugin/src/lib/delivery-premortem.js +0 -499
  127. package/agent-spine-plugin/src/lib/delivery-shell-heredoc.js +0 -115
  128. package/agent-spine-plugin/src/lib/delivery-shell-substitutions.js +0 -111
  129. package/agent-spine-plugin/src/lib/delivery-shell-wrapper.js +0 -64
  130. package/agent-spine-plugin/src/lib/delivery-target.js +0 -73
  131. package/agent-spine-plugin/src/lib/delivery-verification.js +0 -445
  132. package/agent-spine-plugin/src/lib/documents.js +0 -239
  133. package/agent-spine-plugin/src/lib/feed-transport.js +0 -499
  134. package/agent-spine-plugin/src/lib/filesystem-retry.js +0 -34
  135. package/agent-spine-plugin/src/lib/gateway-common.js +0 -68
  136. package/agent-spine-plugin/src/lib/gateway-control.js +0 -370
  137. package/agent-spine-plugin/src/lib/gateway-delivery.js +0 -103
  138. package/agent-spine-plugin/src/lib/gateway-execution.js +0 -350
  139. package/agent-spine-plugin/src/lib/gateway-group-response.js +0 -128
  140. package/agent-spine-plugin/src/lib/gateway-host-fencing.js +0 -92
  141. package/agent-spine-plugin/src/lib/gateway-host-lifecycle.js +0 -193
  142. package/agent-spine-plugin/src/lib/gateway-inspection.js +0 -85
  143. package/agent-spine-plugin/src/lib/gateway-knowledge.js +0 -129
  144. package/agent-spine-plugin/src/lib/gateway-policy-provenance.js +0 -197
  145. package/agent-spine-plugin/src/lib/gateway-premortem-disposition.js +0 -82
  146. package/agent-spine-plugin/src/lib/gateway-premortem.js +0 -363
  147. package/agent-spine-plugin/src/lib/gateway-prepared-host.js +0 -28
  148. package/agent-spine-plugin/src/lib/gateway-runs.js +0 -345
  149. package/agent-spine-plugin/src/lib/gateway-runtime-identity.js +0 -31
  150. package/agent-spine-plugin/src/lib/gateway-runtime-records.js +0 -21
  151. package/agent-spine-plugin/src/lib/gateway-runtime.js +0 -18
  152. package/agent-spine-plugin/src/lib/gateway-state-transaction.js +0 -356
  153. package/agent-spine-plugin/src/lib/gateway-state.js +0 -344
  154. package/agent-spine-plugin/src/lib/graph.js +0 -337
  155. package/agent-spine-plugin/src/lib/hook-artifact-guards.js +0 -356
  156. package/agent-spine-plugin/src/lib/hook-audit.js +0 -43
  157. package/agent-spine-plugin/src/lib/hook-briefing-use.js +0 -78
  158. package/agent-spine-plugin/src/lib/hook-context.js +0 -441
  159. package/agent-spine-plugin/src/lib/hook-final-message.js +0 -26
  160. package/agent-spine-plugin/src/lib/hook-input.js +0 -27
  161. package/agent-spine-plugin/src/lib/hook-output.js +0 -284
  162. package/agent-spine-plugin/src/lib/hook-premortem.js +0 -257
  163. package/agent-spine-plugin/src/lib/hook-process-advisory.js +0 -36
  164. package/agent-spine-plugin/src/lib/hook-protection.js +0 -95
  165. package/agent-spine-plugin/src/lib/hook-stop-verification.js +0 -84
  166. package/agent-spine-plugin/src/lib/hook-timeline.js +0 -102
  167. package/agent-spine-plugin/src/lib/host-instruction-budget.js +0 -11
  168. package/agent-spine-plugin/src/lib/https-transport.js +0 -392
  169. package/agent-spine-plugin/src/lib/identifier-analysis.js +0 -446
  170. package/agent-spine-plugin/src/lib/indexed-memory-offline.js +0 -40
  171. package/agent-spine-plugin/src/lib/indexed-memory.js +0 -298
  172. package/agent-spine-plugin/src/lib/knowledge-evidence.js +0 -431
  173. package/agent-spine-plugin/src/lib/learning-applications.js +0 -441
  174. package/agent-spine-plugin/src/lib/learning-artifact-evaluator.js +0 -114
  175. package/agent-spine-plugin/src/lib/learning-candidates.js +0 -274
  176. package/agent-spine-plugin/src/lib/learning-context.js +0 -137
  177. package/agent-spine-plugin/src/lib/learning-delivery-contracts.js +0 -310
  178. package/agent-spine-plugin/src/lib/learning-evaluation-contracts.js +0 -350
  179. package/agent-spine-plugin/src/lib/learning-evaluation-registration.js +0 -372
  180. package/agent-spine-plugin/src/lib/learning-evaluation-revocation.js +0 -292
  181. package/agent-spine-plugin/src/lib/learning-evidence-contracts.js +0 -321
  182. package/agent-spine-plugin/src/lib/learning-findings.js +0 -458
  183. package/agent-spine-plugin/src/lib/learning-measurement-contracts.js +0 -285
  184. package/agent-spine-plugin/src/lib/learning-measurements.js +0 -265
  185. package/agent-spine-plugin/src/lib/learning-outcome-contracts.js +0 -165
  186. package/agent-spine-plugin/src/lib/learning-outcomes.js +0 -343
  187. package/agent-spine-plugin/src/lib/learning-reconciliation.js +0 -290
  188. package/agent-spine-plugin/src/lib/learning-retry-contracts.js +0 -188
  189. package/agent-spine-plugin/src/lib/learning-schema.js +0 -229
  190. package/agent-spine-plugin/src/lib/learning-scope-targets.js +0 -424
  191. package/agent-spine-plugin/src/lib/learning-state-upgrade.js +0 -477
  192. package/agent-spine-plugin/src/lib/learning-status-configuration.js +0 -476
  193. package/agent-spine-plugin/src/lib/learning-storage.js +0 -203
  194. package/agent-spine-plugin/src/lib/learning-trial-recovery.js +0 -218
  195. package/agent-spine-plugin/src/lib/learning-validation-contracts.js +0 -375
  196. package/agent-spine-plugin/src/lib/learning-validation-renewal.js +0 -298
  197. package/agent-spine-plugin/src/lib/learning-validation-runtime.js +0 -234
  198. package/agent-spine-plugin/src/lib/learning.js +0 -36
  199. package/agent-spine-plugin/src/lib/lesson-recall-session.js +0 -172
  200. package/agent-spine-plugin/src/lib/mcp-autonomy-tools.js +0 -37
  201. package/agent-spine-plugin/src/lib/mcp-delivery-completion.js +0 -124
  202. package/agent-spine-plugin/src/lib/mcp-delivery-tools.js +0 -45
  203. package/agent-spine-plugin/src/lib/mcp-premortem.js +0 -68
  204. package/agent-spine-plugin/src/lib/mcp-runtime.js +0 -355
  205. package/agent-spine-plugin/src/lib/mcp-source-context.js +0 -61
  206. package/agent-spine-plugin/src/lib/mcp-timeline-tools.js +0 -240
  207. package/agent-spine-plugin/src/lib/mcp-world-tools.js +0 -52
  208. package/agent-spine-plugin/src/lib/object-transport.js +0 -206
  209. package/agent-spine-plugin/src/lib/owned-file-lock.js +0 -182
  210. package/agent-spine-plugin/src/lib/paths.js +0 -109
  211. package/agent-spine-plugin/src/lib/peer-transport.js +0 -283
  212. package/agent-spine-plugin/src/lib/persona-runtime.js +0 -581
  213. package/agent-spine-plugin/src/lib/pre-answer-timeline-recall.js +0 -104
  214. package/agent-spine-plugin/src/lib/preflight-delivery-id.js +0 -27
  215. package/agent-spine-plugin/src/lib/preflight.js +0 -688
  216. package/agent-spine-plugin/src/lib/project-portfolio.js +0 -176
  217. package/agent-spine-plugin/src/lib/runtime.js +0 -13
  218. package/agent-spine-plugin/src/lib/selfstarter-core.js +0 -382
  219. package/agent-spine-plugin/src/lib/selfstarter-jobs.js +0 -149
  220. package/agent-spine-plugin/src/lib/selfstarter-lease.js +0 -204
  221. package/agent-spine-plugin/src/lib/selfstarter-policy.js +0 -90
  222. package/agent-spine-plugin/src/lib/selfstarter-workspace.js +0 -111
  223. package/agent-spine-plugin/src/lib/selfstarter.js +0 -11
  224. package/agent-spine-plugin/src/lib/session-timeline-auth.js +0 -316
  225. package/agent-spine-plugin/src/lib/session-timeline-codex.js +0 -69
  226. package/agent-spine-plugin/src/lib/session-timeline-contract.js +0 -56
  227. package/agent-spine-plugin/src/lib/session-timeline-enrollment-source.js +0 -38
  228. package/agent-spine-plugin/src/lib/session-timeline-enrollment-storage.js +0 -128
  229. package/agent-spine-plugin/src/lib/session-timeline-enrollment-transport.js +0 -15
  230. package/agent-spine-plugin/src/lib/session-timeline-enrollment.js +0 -478
  231. package/agent-spine-plugin/src/lib/session-timeline-event-extract.js +0 -186
  232. package/agent-spine-plugin/src/lib/session-timeline-host-origin.js +0 -73
  233. package/agent-spine-plugin/src/lib/session-timeline-host-receipt.js +0 -117
  234. package/agent-spine-plugin/src/lib/session-timeline-invocation.js +0 -201
  235. package/agent-spine-plugin/src/lib/session-timeline-king.js +0 -93
  236. package/agent-spine-plugin/src/lib/session-timeline-prior.js +0 -57
  237. package/agent-spine-plugin/src/lib/session-timeline-provider.js +0 -36
  238. package/agent-spine-plugin/src/lib/session-timeline-query.js +0 -56
  239. package/agent-spine-plugin/src/lib/session-timeline-results.js +0 -56
  240. package/agent-spine-plugin/src/lib/session-timeline-root.js +0 -11
  241. package/agent-spine-plugin/src/lib/session-timeline-search.js +0 -82
  242. package/agent-spine-plugin/src/lib/session-timeline-sid-acl.js +0 -213
  243. package/agent-spine-plugin/src/lib/session-timeline-source-open.js +0 -30
  244. package/agent-spine-plugin/src/lib/session-timeline-source.js +0 -80
  245. package/agent-spine-plugin/src/lib/session-timeline-state.js +0 -45
  246. package/agent-spine-plugin/src/lib/session-timeline-transport.js +0 -42
  247. package/agent-spine-plugin/src/lib/session-timeline-windows-acl.js +0 -145
  248. package/agent-spine-plugin/src/lib/session-timeline.js +0 -462
  249. package/agent-spine-plugin/src/lib/sharing.js +0 -969
  250. package/agent-spine-plugin/src/lib/source-roots.js +0 -475
  251. package/agent-spine-plugin/src/lib/source-tree-scan.js +0 -178
  252. package/agent-spine-plugin/src/lib/sqlite-transport.js +0 -501
  253. package/agent-spine-plugin/src/lib/task-knowledge-context.js +0 -99
  254. package/agent-spine-plugin/src/lib/telegram-adapter.js +0 -119
  255. package/agent-spine-plugin/src/lib/timeline-continuation-update.js +0 -100
  256. package/agent-spine-plugin/src/lib/timeline-tool-guard.js +0 -225
  257. package/agent-spine-plugin/src/lib/timeline-user-feedback.js +0 -216
  258. package/agent-spine-plugin/src/lib/timeline-world-capture.js +0 -233
  259. package/agent-spine-plugin/src/lib/voice-runtime.js +0 -39
  260. package/agent-spine-plugin/src/lib/world-knowledge.js +0 -306
  261. package/agent-spine-plugin/src/lib/world-model.js +0 -333
  262. package/agent-spine-plugin/src/mcp.js +0 -456
  263. package/agent-spine-plugin/src/version.js +0 -1
  264. package/agent-spine-plugin/src/worker.js +0 -257
  265. package/bin/agentspine-king-goal-inbox.mjs +0 -127
  266. package/bin/agentspine-king-goal-intake.mjs +0 -106
  267. package/bin/agentspine-king-host-runner.mjs +0 -109
  268. package/bin/agentspine-king-snapshot-policy.cjs +0 -80
  269. package/bin/agentspine-king-status-policy.mjs +0 -226
  270. package/bin/agentspine-king-worker-host.mjs +0 -160
  271. package/bin/managed-plugin-selection.cjs +0 -141
  272. package/bin/verify-agent-behavior.cjs +0 -122
  273. package/bin/verify-agent-components.cjs +0 -104
@@ -1,298 +0,0 @@
1
- import { createHash, randomUUID } from "node:crypto";
2
- import { constants as fsConstants } from "node:fs";
3
- import { lstat, mkdir, open, readFile, realpath, rename, stat, unlink, writeFile } from "node:fs/promises";
4
- import { basename, dirname, join, relative, resolve, sep } from "node:path";
5
- import { setTimeout as delay } from "node:timers/promises";
6
- import { isFileLockContention } from "./filesystem-retry.js";
7
- import { isInside, stateRoot } from "./paths.js";
8
- import { markdownOutsideCode } from "./documents.js";
9
-
10
- export const INDEXED_MEMORY_CACHE_SCHEMA = "agentspine.indexed-memory-cache/v1";
11
- const CACHE_LIMIT = 16 * 1024 * 1024;
12
- const FILE_LIMIT = 4 * 1024 * 1024;
13
- const MAX_ATTEMPTS = 3;
14
- const MAX_INDEXED_LINKS = 4096;
15
- const MAX_LIVE_SELECTIONS = 8;
16
-
17
- function sha256(value) { return createHash("sha256").update(value).digest("hex"); }
18
- function cachePath(env) { return join(stateRoot(env), "indexed-memory-cache.json"); }
19
- function emptyCache() { return { schema: INDEXED_MEMORY_CACHE_SCHEMA, revision: 0, roots: {} }; }
20
- function rootKey(root) { return sha256(root).slice(0, 32); }
21
-
22
- function validateCache(value) {
23
- if (!value || value.schema !== INDEXED_MEMORY_CACHE_SCHEMA || !Number.isInteger(value.revision)
24
- || !value.roots || typeof value.roots !== "object" || Array.isArray(value.roots)) {
25
- throw new Error("indexed memory cache is corrupt; memory recall is disabled until repaired");
26
- }
27
- for (const records of Object.values(value.roots)) {
28
- if (!records || typeof records !== "object" || Array.isArray(records)) throw new Error("indexed memory cache contains an invalid root");
29
- for (const [path, record] of Object.entries(records)) {
30
- if (!path || path.startsWith("../") || path === ".." || !record || typeof record !== "object"
31
- || typeof record.contentBase64 !== "string" || typeof record.sha256 !== "string"
32
- || typeof record.identity !== "string" || !Number.isInteger(record.bytes)) {
33
- throw new Error("indexed memory cache contains an invalid record");
34
- }
35
- const buffer = Buffer.from(record.contentBase64, "base64");
36
- if (buffer.byteLength !== record.bytes || sha256(buffer) !== record.sha256) {
37
- throw new Error("indexed memory cache integrity check failed");
38
- }
39
- }
40
- }
41
- return value;
42
- }
43
-
44
- async function readCache(env) {
45
- const path = cachePath(env);
46
- try {
47
- const metadata = await stat(path);
48
- if (metadata.size > CACHE_LIMIT) throw new Error("indexed memory cache exceeds 16 MiB");
49
- return { cache: validateCache(JSON.parse(await readFile(path, "utf8"))), path };
50
- } catch (error) {
51
- if (error.code !== "ENOENT") throw error;
52
- return { cache: emptyCache(), path };
53
- }
54
- }
55
-
56
- async function mutateCache(env, task) {
57
- const target = cachePath(env);
58
- await mkdir(dirname(target), { recursive: true, mode: 0o700 });
59
- const lock = `${target}.lock`;
60
- let handle;
61
- for (let attempt = 0; attempt < 80; attempt += 1) {
62
- try { handle = await open(lock, "wx", 0o600); break; } catch (error) {
63
- if (!isFileLockContention(error)) throw error;
64
- try {
65
- const metadata = await stat(lock);
66
- if (Date.now() - metadata.mtimeMs > 15000) await unlink(lock);
67
- } catch (lockError) {
68
- if (lockError.code !== "ENOENT") throw lockError;
69
- }
70
- await delay(25);
71
- }
72
- }
73
- if (!handle) throw new Error("indexed memory cache is busy; retry shortly");
74
- const temporary = `${target}.${process.pid}.${randomUUID()}.tmp`;
75
- try {
76
- const { cache } = await readCache(env);
77
- const result = await task(cache);
78
- cache.revision += 1;
79
- const content = `${JSON.stringify(cache)}\n`;
80
- if (Buffer.byteLength(content) > CACHE_LIMIT) throw new Error("indexed memory cache exceeds 16 MiB");
81
- await writeFile(temporary, content, { mode: 0o600 });
82
- for (let attempt = 0; ; attempt += 1) {
83
- try { await rename(temporary, target); break; } catch (error) {
84
- const transientWindowsReplace = process.platform === "win32"
85
- && ["EACCES", "EBUSY", "EPERM"].includes(error.code);
86
- if (!transientWindowsReplace || attempt >= 7) throw error;
87
- await delay(10 * (attempt + 1));
88
- }
89
- }
90
- return { ...result, cachePath: target, cacheRevision: cache.revision };
91
- } finally {
92
- await handle.close();
93
- await unlink(temporary).catch((error) => { if (error.code !== "ENOENT") throw error; });
94
- await unlink(lock).catch((error) => { if (error.code !== "ENOENT") throw error; });
95
- }
96
- }
97
-
98
- function identity(metadata) {
99
- return [metadata.dev, metadata.ino, metadata.size, metadata.mtimeNs, metadata.ctimeNs].join(":");
100
- }
101
-
102
- function sameFile(left, right) {
103
- return left.isFile() && right.isFile() && !left.isSymbolicLink?.() && !right.isSymbolicLink?.()
104
- && left.dev === right.dev && left.ino === right.ino && left.size === right.size
105
- && left.mtimeNs === right.mtimeNs && left.ctimeNs === right.ctimeNs;
106
- }
107
-
108
- async function pathClassification(root, relativePath) {
109
- const candidate = resolve(root, relativePath);
110
- if (!isInside(root, candidate) || candidate === root) return { rejected: "path", candidate: null };
111
- const parts = relative(root, candidate).split(sep);
112
- let cursor = root;
113
- for (let index = 0; index < parts.length; index += 1) {
114
- cursor = join(cursor, parts[index]);
115
- let metadata;
116
- try { metadata = await lstat(cursor, { bigint: true }); } catch (error) {
117
- if (["ENOENT", "ENOTDIR"].includes(error.code)) return { rejected: "missing", candidate };
118
- throw error;
119
- }
120
- if (metadata.isSymbolicLink()) return { rejected: "symlink", candidate };
121
- if (index < parts.length - 1 && !metadata.isDirectory()) return { rejected: "path", candidate };
122
- if (index === parts.length - 1 && !metadata.isFile()) return { rejected: "path", candidate };
123
- }
124
- return { rejected: null, candidate };
125
- }
126
-
127
- async function safeSnapshotOnce(root, relativePath, cached, hooks, attempt) {
128
- const classified = await pathClassification(root, relativePath);
129
- if (classified.rejected) return { rejected: classified.rejected };
130
- const flags = fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW || 0);
131
- let handle;
132
- try { handle = await open(classified.candidate, flags); } catch (error) {
133
- if (["ENOENT", "ENOTDIR"].includes(error.code)) return { rejected: "missing" };
134
- if (["ELOOP", "EMLINK"].includes(error.code)) return { rejected: "symlink" };
135
- throw error;
136
- }
137
- try {
138
- await hooks.onOpen?.({ relativePath, attempt });
139
- const before = await handle.stat({ bigint: true });
140
- if (!before.isFile() || before.size > BigInt(FILE_LIMIT)) return { rejected: before.size > BigInt(FILE_LIMIT) ? "size" : "path" };
141
- const canonical = await realpath(classified.candidate);
142
- if (!isInside(root, canonical)) return { rejected: "scope" };
143
- const expectedIdentity = identity(before);
144
- let buffer;
145
- let cacheHit = false;
146
- if (cached?.identity === expectedIdentity) {
147
- buffer = Buffer.from(cached.contentBase64, "base64");
148
- if (buffer.byteLength !== cached.bytes || sha256(buffer) !== cached.sha256) throw new Error("indexed memory cache integrity check failed");
149
- cacheHit = true;
150
- } else {
151
- buffer = await handle.readFile();
152
- }
153
- await hooks.afterRead?.({ relativePath, attempt, path: classified.candidate });
154
- const after = await handle.stat({ bigint: true });
155
- let pathname;
156
- try { pathname = await lstat(classified.candidate, { bigint: true }); } catch (error) {
157
- if (["ENOENT", "ENOTDIR"].includes(error.code)) return { rejected: "race" };
158
- throw error;
159
- }
160
- if (!sameFile(before, after) || !sameFile(after, pathname)) return { rejected: "race" };
161
- if (buffer.byteLength !== Number(after.size)) return { rejected: "race" };
162
- const digest = cacheHit ? cached.sha256 : sha256(buffer);
163
- return {
164
- rejected: null, cacheHit, path: canonical, relativePath, buffer,
165
- record: { identity: expectedIdentity, bytes: buffer.byteLength, sha256: digest, contentBase64: buffer.toString("base64") },
166
- metadata: { bytes: buffer.byteLength, sha256: digest, modifiedAt: new Date(Number(after.mtimeNs / 1000000n)).toISOString() }
167
- };
168
- } finally {
169
- await handle.close();
170
- }
171
- }
172
-
173
- export async function safeIndexedSnapshot(root, relativePath, cached = null, hooks = {}) {
174
- for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt += 1) {
175
- const result = await safeSnapshotOnce(root, relativePath, cached, hooks, attempt);
176
- if (result.rejected !== "race") return result;
177
- await hooks.onRace?.({ relativePath, attempt });
178
- }
179
- return { rejected: "race" };
180
- }
181
-
182
- function tokens(value) {
183
- return new Set(String(value || "").toLowerCase().normalize("NFKC").match(/[\p{L}\p{N}]{3,}/gu) || []);
184
- }
185
-
186
- export function parseMemoryIndex(text, indexPath, root) {
187
- const entries = [];
188
- const seen = new Set();
189
- const pattern = /\[([^\]]+)\]\(([^)]+\.md)(?:#[^)]+)?\)([^\n]*)/gi;
190
- for (const match of markdownOutsideCode(text).matchAll(pattern)) {
191
- const label = match[1].trim();
192
- let raw = match[2].trim().replace(/^<|>$/g, "");
193
- if (/^[a-z]+:/i.test(raw)) continue;
194
- try { raw = decodeURIComponent(raw); } catch { /* Retain malformed local paths for safe rejection. */ }
195
- const target = resolve(dirname(indexPath), raw);
196
- const relativePath = relative(root, target).replaceAll("\\", "/");
197
- if (seen.has(relativePath) || relativePath === "MEMORY.md") continue;
198
- seen.add(relativePath);
199
- const suffix = match[3] || "";
200
- const marker = suffix.match(/<!--\s*agentspine:([^>]+)-->/i)?.[1] || "";
201
- const directives = Object.fromEntries([...marker.matchAll(/([a-z]+)\s*=\s*([^,;\s]+)/gi)].map((item) => [item[1].toLowerCase(), item[2]]));
202
- const always = /(?:^|[,;\s])always(?:$|[,;\s])/i.test(marker) || /(?:—|-)\s*always\s*$/i.test(suffix);
203
- entries.push({ relativePath, label, always, directives, promptTokens: [...tokens(`${label} ${basename(relativePath)} ${directives.keywords || ""}`)] });
204
- }
205
- return entries;
206
- }
207
-
208
- export function relevantMemoryEntry(entry, scope = {}) {
209
- const pinned = new Set(Array.isArray(scope.pinnedPaths) ? scope.pinnedPaths : []);
210
- if (pinned.has(entry.relativePath)) return { relevant: true, reason: "pinned", score: 1000 };
211
- const exact = [
212
- ["entity", scope.entityId], ["person", scope.entityId], ["project", scope.projectId],
213
- ["group", scope.groupId], ["task", scope.currentTaskId]
214
- ];
215
- for (const [key, value] of exact) {
216
- if (entry.directives[key] && value && entry.directives[key] === value) {
217
- return { relevant: true, reason: key, score: 500 };
218
- }
219
- }
220
- const prompt = tokens(scope.prompt);
221
- const matches = entry.promptTokens.filter((value) => prompt.has(value)).length;
222
- if (matches) return { relevant: true, reason: "prompt", score: 700 + Math.min(matches, 99) };
223
- if (entry.always) return { relevant: true, reason: "always", score: 100 };
224
- return { relevant: false, reason: "scope", score: 0 };
225
- }
226
-
227
- export async function resolveIndexedMemory({ root, scope = {}, env = process.env, hooks = {}, deadline = Infinity }) {
228
- function checkDeadline() {
229
- if (Date.now() > deadline) throw new Error("indexed memory resolution exceeded its time limit");
230
- }
231
- checkDeadline();
232
- const canonicalRoot = await realpath(root);
233
- const { cache } = await readCache(env);
234
- const key = rootKey(canonicalRoot);
235
- const cached = cache.roots[key] || {};
236
- const diagnostics = {
237
- indexed: 0, relevant: 0, selected: 0, omittedRelevant: 0, loaded: 0, cacheHits: 0, cacheMisses: 0, missing: 0,
238
- rejected: { scope: 0, path: 0, symlink: 0, race: 0, size: 0 }, directoryEnumeration: 0
239
- };
240
- const index = await safeIndexedSnapshot(canonicalRoot, "MEMORY.md", cached["MEMORY.md"], hooks);
241
- if (index.rejected) {
242
- if (index.rejected === "missing") diagnostics.missing += 1;
243
- else diagnostics.rejected[index.rejected] = (diagnostics.rejected[index.rejected] || 0) + 1;
244
- await mutateCache(env, (value) => { delete value.roots[key]; return {}; });
245
- return { sources: [], diagnostics };
246
- }
247
- diagnostics.cacheHits += Number(index.cacheHit);
248
- diagnostics.cacheMisses += Number(!index.cacheHit);
249
- const entries = parseMemoryIndex(index.buffer.toString("utf8"), index.path, canonicalRoot);
250
- if (entries.length > MAX_INDEXED_LINKS) throw new Error(`MEMORY.md indexes more than ${MAX_INDEXED_LINKS} direct files`);
251
- diagnostics.indexed = entries.length;
252
- const sources = [{ path: index.path, relativePath: "MEMORY.md", snapshot: index, relevance: "index" }];
253
- const selected = [];
254
- for (const entry of entries) {
255
- checkDeadline();
256
- const relevance = relevantMemoryEntry(entry, scope);
257
- if (!relevance.relevant) { diagnostics.rejected.scope += 1; continue; }
258
- diagnostics.relevant += 1;
259
- selected.push({ entry, relevance });
260
- }
261
- const chosen = new Set([...selected].sort((left, right) => right.relevance.score - left.relevance.score
262
- || left.entry.relativePath.localeCompare(right.entry.relativePath)).slice(0, MAX_LIVE_SELECTIONS)
263
- .map(({ entry }) => entry.relativePath));
264
- diagnostics.selected = chosen.size;
265
- diagnostics.omittedRelevant = Math.max(0, selected.length - MAX_LIVE_SELECTIONS);
266
- for (const { entry, relevance } of selected) {
267
- if (!chosen.has(entry.relativePath)) continue;
268
- checkDeadline();
269
- const snapshot = await safeIndexedSnapshot(canonicalRoot, entry.relativePath, cached[entry.relativePath], hooks);
270
- if (snapshot.rejected) {
271
- if (snapshot.rejected === "missing") diagnostics.missing += 1;
272
- else diagnostics.rejected[snapshot.rejected] = (diagnostics.rejected[snapshot.rejected] || 0) + 1;
273
- continue;
274
- }
275
- diagnostics.loaded += 1;
276
- diagnostics.cacheHits += Number(snapshot.cacheHit);
277
- diagnostics.cacheMisses += Number(!snapshot.cacheHit);
278
- sources.push({ path: snapshot.path, relativePath: entry.relativePath, snapshot, relevance: relevance.reason });
279
- }
280
- checkDeadline();
281
- const finalIndex = await safeIndexedSnapshot(canonicalRoot, "MEMORY.md", index.record, hooks);
282
- if (finalIndex.rejected || finalIndex.metadata.sha256 !== index.metadata.sha256) throw new Error("MEMORY.md changed during indexed resolution; retry the hook");
283
- await mutateCache(env, (value) => {
284
- const records = { "MEMORY.md": index.record };
285
- for (const source of sources.slice(1)) records[source.relativePath] = source.snapshot.record;
286
- value.roots[key] = records;
287
- return {};
288
- });
289
- return { sources, diagnostics };
290
- }
291
-
292
- export async function purgeIndexedMemoryCache(root, env = process.env) {
293
- let canonical;
294
- try { canonical = await realpath(root); } catch (error) {
295
- if (error.code === "ENOENT") canonical = resolve(root); else throw error;
296
- }
297
- return mutateCache(env, (cache) => { delete cache.roots[rootKey(canonical)]; return { purged: true }; });
298
- }