blun-king-cli 9.1.643 → 9.1.645
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/CHANGELOG.md +19 -123
- package/LIESMICH.txt +17 -45
- package/README.md +15 -76
- package/RELEASE-9.1.645.md +29 -0
- package/bin/blun.js +3 -3
- package/bin/king.js +3 -3
- package/bin/launcher-mode.js +125 -126
- package/bin/launcher-runtime.js +603 -616
- package/bin/plugin-bootstrap.js +339 -405
- package/bin/rename-private-staging.cjs +2 -2
- package/bin/retired-bundled-plugins.cjs +84 -0
- package/bin/startup-security-host.cjs +105 -0
- package/bin/startup-security.cjs +9 -105
- package/bin/startup-warning-policy.cjs +22 -0
- package/bin/update-lease-installer.cjs +125 -0
- package/bin/update-lease-sockets.cjs +228 -0
- package/bin/update-lease.js +22 -327
- package/bin/verify-bundled-agent-sources.cjs +2 -4
- package/blun.mjs +77 -180
- package/bundled-agent-sources.json +1 -552
- package/package.json +3 -6
- package/tool-liveness642/receipt.json +1 -1
- package/worker-host.mjs +62 -120
- package/agent-spine-plugin/.claude-plugin/marketplace.json +0 -20
- package/agent-spine-plugin/.claude-plugin/plugin.json +0 -14
- package/agent-spine-plugin/.codex-plugin/plugin.json +0 -47
- package/agent-spine-plugin/.mcp.json +0 -8
- package/agent-spine-plugin/CHANGELOG.md +0 -1614
- package/agent-spine-plugin/LICENSE +0 -186
- package/agent-spine-plugin/README.md +0 -432
- package/agent-spine-plugin/assets/agentspine-banner.svg +0 -32
- package/agent-spine-plugin/bin/agentspine-mcp.js +0 -4
- package/agent-spine-plugin/bin/agentspine.js +0 -7
- package/agent-spine-plugin/blun.plugin.json +0 -68
- package/agent-spine-plugin/docs/acceptance.md +0 -61
- package/agent-spine-plugin/docs/artifact-evaluation.md +0 -93
- package/agent-spine-plugin/docs/host-integration.md +0 -193
- package/agent-spine-plugin/docs/preflight-recall.md +0 -71
- package/agent-spine-plugin/docs/preservation-contract.md +0 -53
- package/agent-spine-plugin/docs/quality-gates.md +0 -50
- package/agent-spine-plugin/docs/session-timeline.md +0 -112
- package/agent-spine-plugin/docs/source-roots.md +0 -113
- package/agent-spine-plugin/docs/world-model.md +0 -119
- package/agent-spine-plugin/hooks/codex.json +0 -47
- package/agent-spine-plugin/hooks/hooks.json +0 -106
- package/agent-spine-plugin/hooks/version.json +0 -5
- package/agent-spine-plugin/package.json +0 -77
- package/agent-spine-plugin/scripts/check-codex-install.js +0 -226
- package/agent-spine-plugin/scripts/check-hosts.js +0 -208
- package/agent-spine-plugin/scripts/check-install-hook.js +0 -227
- package/agent-spine-plugin/scripts/check-install-selfstarter.js +0 -154
- package/agent-spine-plugin/scripts/check-install.js +0 -478
- package/agent-spine-plugin/scripts/check-line-budget.js +0 -58
- package/agent-spine-plugin/scripts/check-syntax.js +0 -29
- package/agent-spine-plugin/scripts/github-actions.js +0 -11
- package/agent-spine-plugin/scripts/hermetic-process.js +0 -191
- package/agent-spine-plugin/scripts/release-check.js +0 -150
- package/agent-spine-plugin/scripts/run-acceptance.js +0 -19
- package/agent-spine-plugin/scripts/run-checks.js +0 -50
- package/agent-spine-plugin/scripts/run-tests-hermetic.js +0 -112
- package/agent-spine-plugin/skills/agent-spine/SKILL.md +0 -99
- package/agent-spine-plugin/spine-example/1-identity.md +0 -12
- package/agent-spine-plugin/spine-example/2-voice.md +0 -6
- package/agent-spine-plugin/spine-example/3-conduct.md +0 -8
- package/agent-spine-plugin/spine-example/4-history.md +0 -4
- package/agent-spine-plugin/src/cli-agent.js +0 -315
- package/agent-spine-plugin/src/cli-attention.js +0 -95
- package/agent-spine-plugin/src/cli-autonomy.js +0 -36
- package/agent-spine-plugin/src/cli-common.js +0 -71
- package/agent-spine-plugin/src/cli-continuity.js +0 -116
- package/agent-spine-plugin/src/cli-core.js +0 -128
- package/agent-spine-plugin/src/cli-diagnostics.js +0 -291
- package/agent-spine-plugin/src/cli-host.js +0 -21
- package/agent-spine-plugin/src/cli-learning.js +0 -320
- package/agent-spine-plugin/src/cli-premortem.js +0 -16
- package/agent-spine-plugin/src/cli-sharing.js +0 -230
- package/agent-spine-plugin/src/cli.js +0 -181
- package/agent-spine-plugin/src/codex-reader-launcher.js +0 -182
- package/agent-spine-plugin/src/hook.js +0 -500
- package/agent-spine-plugin/src/index.js +0 -108
- package/agent-spine-plugin/src/lib/acceptance.js +0 -435
- package/agent-spine-plugin/src/lib/action-lesson-recall.js +0 -118
- package/agent-spine-plugin/src/lib/attention-context.js +0 -167
- package/agent-spine-plugin/src/lib/attention-events.js +0 -113
- package/agent-spine-plugin/src/lib/attention-privacy.js +0 -72
- package/agent-spine-plugin/src/lib/attention-schema.js +0 -164
- package/agent-spine-plugin/src/lib/attention-storage.js +0 -93
- package/agent-spine-plugin/src/lib/attention.js +0 -164
- package/agent-spine-plugin/src/lib/audit-premortem.js +0 -223
- package/agent-spine-plugin/src/lib/audit.js +0 -401
- package/agent-spine-plugin/src/lib/authentication.js +0 -515
- package/agent-spine-plugin/src/lib/autonomy-policy.js +0 -110
- package/agent-spine-plugin/src/lib/autonomy-store.js +0 -202
- package/agent-spine-plugin/src/lib/autonomy.js +0 -8
- package/agent-spine-plugin/src/lib/briefing.js +0 -493
- package/agent-spine-plugin/src/lib/catalog-document-read.js +0 -51
- package/agent-spine-plugin/src/lib/catalog.js +0 -167
- package/agent-spine-plugin/src/lib/channel-continuity.js +0 -19
- package/agent-spine-plugin/src/lib/channel-runtime.js +0 -665
- package/agent-spine-plugin/src/lib/codex-installation.js +0 -231
- package/agent-spine-plugin/src/lib/codex-skill-installation.js +0 -211
- package/agent-spine-plugin/src/lib/context.js +0 -156
- package/agent-spine-plugin/src/lib/continuity.js +0 -338
- package/agent-spine-plugin/src/lib/coordination.js +0 -577
- package/agent-spine-plugin/src/lib/delivery-agent-usage.js +0 -231
- package/agent-spine-plugin/src/lib/delivery-assignment.js +0 -220
- package/agent-spine-plugin/src/lib/delivery-command-actions.js +0 -462
- package/agent-spine-plugin/src/lib/delivery-knowledge.js +0 -78
- package/agent-spine-plugin/src/lib/delivery-premortem-binding.js +0 -107
- package/agent-spine-plugin/src/lib/delivery-premortem-closure.js +0 -171
- package/agent-spine-plugin/src/lib/delivery-premortem-codec.js +0 -45
- package/agent-spine-plugin/src/lib/delivery-premortem-correction.js +0 -101
- package/agent-spine-plugin/src/lib/delivery-premortem-file.js +0 -21
- package/agent-spine-plugin/src/lib/delivery-premortem-index.js +0 -493
- package/agent-spine-plugin/src/lib/delivery-premortem-inspection.js +0 -37
- package/agent-spine-plugin/src/lib/delivery-premortem-recovery.js +0 -120
- package/agent-spine-plugin/src/lib/delivery-premortem-rejection.js +0 -65
- package/agent-spine-plugin/src/lib/delivery-premortem-results.js +0 -28
- package/agent-spine-plugin/src/lib/delivery-premortem-session-guard.js +0 -46
- package/agent-spine-plugin/src/lib/delivery-premortem-write-ledger.js +0 -285
- package/agent-spine-plugin/src/lib/delivery-premortem.js +0 -499
- package/agent-spine-plugin/src/lib/delivery-shell-heredoc.js +0 -115
- package/agent-spine-plugin/src/lib/delivery-shell-substitutions.js +0 -111
- package/agent-spine-plugin/src/lib/delivery-shell-wrapper.js +0 -64
- package/agent-spine-plugin/src/lib/delivery-target.js +0 -73
- package/agent-spine-plugin/src/lib/delivery-verification.js +0 -445
- package/agent-spine-plugin/src/lib/documents.js +0 -239
- package/agent-spine-plugin/src/lib/feed-transport.js +0 -499
- package/agent-spine-plugin/src/lib/filesystem-retry.js +0 -34
- package/agent-spine-plugin/src/lib/gateway-common.js +0 -68
- package/agent-spine-plugin/src/lib/gateway-control.js +0 -370
- package/agent-spine-plugin/src/lib/gateway-delivery.js +0 -103
- package/agent-spine-plugin/src/lib/gateway-execution.js +0 -350
- package/agent-spine-plugin/src/lib/gateway-group-response.js +0 -128
- package/agent-spine-plugin/src/lib/gateway-host-fencing.js +0 -92
- package/agent-spine-plugin/src/lib/gateway-host-lifecycle.js +0 -193
- package/agent-spine-plugin/src/lib/gateway-inspection.js +0 -85
- package/agent-spine-plugin/src/lib/gateway-knowledge.js +0 -129
- package/agent-spine-plugin/src/lib/gateway-policy-provenance.js +0 -197
- package/agent-spine-plugin/src/lib/gateway-premortem-disposition.js +0 -82
- package/agent-spine-plugin/src/lib/gateway-premortem.js +0 -363
- package/agent-spine-plugin/src/lib/gateway-prepared-host.js +0 -28
- package/agent-spine-plugin/src/lib/gateway-runs.js +0 -345
- package/agent-spine-plugin/src/lib/gateway-runtime-identity.js +0 -31
- package/agent-spine-plugin/src/lib/gateway-runtime-records.js +0 -21
- package/agent-spine-plugin/src/lib/gateway-runtime.js +0 -18
- package/agent-spine-plugin/src/lib/gateway-state-transaction.js +0 -356
- package/agent-spine-plugin/src/lib/gateway-state.js +0 -344
- package/agent-spine-plugin/src/lib/graph.js +0 -337
- package/agent-spine-plugin/src/lib/hook-artifact-guards.js +0 -356
- package/agent-spine-plugin/src/lib/hook-audit.js +0 -43
- package/agent-spine-plugin/src/lib/hook-briefing-use.js +0 -78
- package/agent-spine-plugin/src/lib/hook-context.js +0 -441
- package/agent-spine-plugin/src/lib/hook-final-message.js +0 -26
- package/agent-spine-plugin/src/lib/hook-input.js +0 -27
- package/agent-spine-plugin/src/lib/hook-output.js +0 -284
- package/agent-spine-plugin/src/lib/hook-premortem.js +0 -257
- package/agent-spine-plugin/src/lib/hook-process-advisory.js +0 -36
- package/agent-spine-plugin/src/lib/hook-protection.js +0 -95
- package/agent-spine-plugin/src/lib/hook-stop-verification.js +0 -84
- package/agent-spine-plugin/src/lib/hook-timeline.js +0 -102
- package/agent-spine-plugin/src/lib/host-instruction-budget.js +0 -11
- package/agent-spine-plugin/src/lib/https-transport.js +0 -392
- package/agent-spine-plugin/src/lib/identifier-analysis.js +0 -446
- package/agent-spine-plugin/src/lib/indexed-memory-offline.js +0 -40
- package/agent-spine-plugin/src/lib/indexed-memory.js +0 -298
- package/agent-spine-plugin/src/lib/knowledge-evidence.js +0 -431
- package/agent-spine-plugin/src/lib/learning-applications.js +0 -441
- package/agent-spine-plugin/src/lib/learning-artifact-evaluator.js +0 -114
- package/agent-spine-plugin/src/lib/learning-candidates.js +0 -274
- package/agent-spine-plugin/src/lib/learning-context.js +0 -137
- package/agent-spine-plugin/src/lib/learning-delivery-contracts.js +0 -310
- package/agent-spine-plugin/src/lib/learning-evaluation-contracts.js +0 -350
- package/agent-spine-plugin/src/lib/learning-evaluation-registration.js +0 -372
- package/agent-spine-plugin/src/lib/learning-evaluation-revocation.js +0 -292
- package/agent-spine-plugin/src/lib/learning-evidence-contracts.js +0 -321
- package/agent-spine-plugin/src/lib/learning-findings.js +0 -458
- package/agent-spine-plugin/src/lib/learning-measurement-contracts.js +0 -285
- package/agent-spine-plugin/src/lib/learning-measurements.js +0 -265
- package/agent-spine-plugin/src/lib/learning-outcome-contracts.js +0 -165
- package/agent-spine-plugin/src/lib/learning-outcomes.js +0 -343
- package/agent-spine-plugin/src/lib/learning-reconciliation.js +0 -290
- package/agent-spine-plugin/src/lib/learning-retry-contracts.js +0 -188
- package/agent-spine-plugin/src/lib/learning-schema.js +0 -229
- package/agent-spine-plugin/src/lib/learning-scope-targets.js +0 -424
- package/agent-spine-plugin/src/lib/learning-state-upgrade.js +0 -477
- package/agent-spine-plugin/src/lib/learning-status-configuration.js +0 -476
- package/agent-spine-plugin/src/lib/learning-storage.js +0 -203
- package/agent-spine-plugin/src/lib/learning-trial-recovery.js +0 -218
- package/agent-spine-plugin/src/lib/learning-validation-contracts.js +0 -375
- package/agent-spine-plugin/src/lib/learning-validation-renewal.js +0 -298
- package/agent-spine-plugin/src/lib/learning-validation-runtime.js +0 -234
- package/agent-spine-plugin/src/lib/learning.js +0 -36
- package/agent-spine-plugin/src/lib/lesson-recall-session.js +0 -172
- package/agent-spine-plugin/src/lib/mcp-autonomy-tools.js +0 -37
- package/agent-spine-plugin/src/lib/mcp-delivery-completion.js +0 -124
- package/agent-spine-plugin/src/lib/mcp-delivery-tools.js +0 -45
- package/agent-spine-plugin/src/lib/mcp-premortem.js +0 -68
- package/agent-spine-plugin/src/lib/mcp-runtime.js +0 -355
- package/agent-spine-plugin/src/lib/mcp-source-context.js +0 -61
- package/agent-spine-plugin/src/lib/mcp-timeline-tools.js +0 -240
- package/agent-spine-plugin/src/lib/mcp-world-tools.js +0 -52
- package/agent-spine-plugin/src/lib/object-transport.js +0 -206
- package/agent-spine-plugin/src/lib/owned-file-lock.js +0 -182
- package/agent-spine-plugin/src/lib/paths.js +0 -109
- package/agent-spine-plugin/src/lib/peer-transport.js +0 -283
- package/agent-spine-plugin/src/lib/persona-runtime.js +0 -581
- package/agent-spine-plugin/src/lib/pre-answer-timeline-recall.js +0 -104
- package/agent-spine-plugin/src/lib/preflight-delivery-id.js +0 -27
- package/agent-spine-plugin/src/lib/preflight.js +0 -688
- package/agent-spine-plugin/src/lib/project-portfolio.js +0 -176
- package/agent-spine-plugin/src/lib/runtime.js +0 -13
- package/agent-spine-plugin/src/lib/selfstarter-core.js +0 -382
- package/agent-spine-plugin/src/lib/selfstarter-jobs.js +0 -149
- package/agent-spine-plugin/src/lib/selfstarter-lease.js +0 -204
- package/agent-spine-plugin/src/lib/selfstarter-policy.js +0 -90
- package/agent-spine-plugin/src/lib/selfstarter-workspace.js +0 -111
- package/agent-spine-plugin/src/lib/selfstarter.js +0 -11
- package/agent-spine-plugin/src/lib/session-timeline-auth.js +0 -316
- package/agent-spine-plugin/src/lib/session-timeline-codex.js +0 -69
- package/agent-spine-plugin/src/lib/session-timeline-contract.js +0 -56
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-source.js +0 -38
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-storage.js +0 -128
- package/agent-spine-plugin/src/lib/session-timeline-enrollment-transport.js +0 -15
- package/agent-spine-plugin/src/lib/session-timeline-enrollment.js +0 -478
- package/agent-spine-plugin/src/lib/session-timeline-event-extract.js +0 -186
- package/agent-spine-plugin/src/lib/session-timeline-host-origin.js +0 -73
- package/agent-spine-plugin/src/lib/session-timeline-host-receipt.js +0 -117
- package/agent-spine-plugin/src/lib/session-timeline-invocation.js +0 -201
- package/agent-spine-plugin/src/lib/session-timeline-king.js +0 -93
- package/agent-spine-plugin/src/lib/session-timeline-prior.js +0 -57
- package/agent-spine-plugin/src/lib/session-timeline-provider.js +0 -36
- package/agent-spine-plugin/src/lib/session-timeline-query.js +0 -56
- package/agent-spine-plugin/src/lib/session-timeline-results.js +0 -56
- package/agent-spine-plugin/src/lib/session-timeline-root.js +0 -11
- package/agent-spine-plugin/src/lib/session-timeline-search.js +0 -82
- package/agent-spine-plugin/src/lib/session-timeline-sid-acl.js +0 -213
- package/agent-spine-plugin/src/lib/session-timeline-source-open.js +0 -30
- package/agent-spine-plugin/src/lib/session-timeline-source.js +0 -80
- package/agent-spine-plugin/src/lib/session-timeline-state.js +0 -45
- package/agent-spine-plugin/src/lib/session-timeline-transport.js +0 -42
- package/agent-spine-plugin/src/lib/session-timeline-windows-acl.js +0 -145
- package/agent-spine-plugin/src/lib/session-timeline.js +0 -462
- package/agent-spine-plugin/src/lib/sharing.js +0 -969
- package/agent-spine-plugin/src/lib/source-roots.js +0 -475
- package/agent-spine-plugin/src/lib/source-tree-scan.js +0 -178
- package/agent-spine-plugin/src/lib/sqlite-transport.js +0 -501
- package/agent-spine-plugin/src/lib/task-knowledge-context.js +0 -99
- package/agent-spine-plugin/src/lib/telegram-adapter.js +0 -119
- package/agent-spine-plugin/src/lib/timeline-continuation-update.js +0 -100
- package/agent-spine-plugin/src/lib/timeline-tool-guard.js +0 -225
- package/agent-spine-plugin/src/lib/timeline-user-feedback.js +0 -216
- package/agent-spine-plugin/src/lib/timeline-world-capture.js +0 -233
- package/agent-spine-plugin/src/lib/voice-runtime.js +0 -39
- package/agent-spine-plugin/src/lib/world-knowledge.js +0 -306
- package/agent-spine-plugin/src/lib/world-model.js +0 -333
- package/agent-spine-plugin/src/mcp.js +0 -456
- package/agent-spine-plugin/src/version.js +0 -1
- package/agent-spine-plugin/src/worker.js +0 -257
- package/bin/agentspine-king-goal-inbox.mjs +0 -127
- package/bin/agentspine-king-goal-intake.mjs +0 -106
- package/bin/agentspine-king-host-runner.mjs +0 -109
- package/bin/agentspine-king-snapshot-policy.cjs +0 -80
- package/bin/agentspine-king-status-policy.mjs +0 -226
- package/bin/agentspine-king-worker-host.mjs +0 -160
- package/bin/managed-plugin-selection.cjs +0 -141
- package/bin/verify-agent-behavior.cjs +0 -122
- package/bin/verify-agent-components.cjs +0 -104
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { lstat, mkdir, open, readFile, realpath, rm } from "node:fs/promises";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { withOwnedFileLock } from "./owned-file-lock.js";
|
|
7
|
-
import { replaceFileWithRetry } from "./filesystem-retry.js";
|
|
8
|
-
import { defaultCodexSkillsRoot, installCodexSkill } from "./codex-skill-installation.js";
|
|
9
|
-
|
|
10
|
-
const REGISTRATION_SCHEMA = "agentspine.codex-reader-registration/v1";
|
|
11
|
-
const AUTHORITY = "host-registration-only";
|
|
12
|
-
const START = "# BEGIN AGENTSPINE MANAGED MCP";
|
|
13
|
-
const END = "# END AGENTSPINE MANAGED MCP";
|
|
14
|
-
const MAX_CONFIG_BYTES = 1024 * 1024;
|
|
15
|
-
const EXPECTED_TOOLS = [
|
|
16
|
-
"session_briefing", "delivery_knowledge_query",
|
|
17
|
-
"record_delivery_premortem", "read_document"
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
function sha256(value) {
|
|
21
|
-
return createHash("sha256").update(value).digest("hex");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function canonical(value) {
|
|
25
|
-
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
26
|
-
if (value && typeof value === "object") {
|
|
27
|
-
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}`;
|
|
28
|
-
}
|
|
29
|
-
return JSON.stringify(value);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function seal(value) {
|
|
33
|
-
return { ...value, digest: sha256(canonical(value)) };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async function regularFile(path, label) {
|
|
37
|
-
const metadata = await lstat(path);
|
|
38
|
-
if (!metadata.isFile() || metadata.isSymbolicLink()) throw new Error(`${label} must be a regular non-symlink file`);
|
|
39
|
-
return path;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async function exactDirectory(path, label, { create = false } = {}) {
|
|
43
|
-
const target = resolve(path);
|
|
44
|
-
if (!isAbsolute(path)) throw new Error(`${label} must be absolute`);
|
|
45
|
-
if (create) await mkdir(target, { recursive: true, mode: 0o700 });
|
|
46
|
-
const metadata = await lstat(target);
|
|
47
|
-
if (!metadata.isDirectory() || metadata.isSymbolicLink()) throw new Error(`${label} must be a regular non-symlink directory`);
|
|
48
|
-
return realpath(target);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async function optionalText(path) {
|
|
52
|
-
try {
|
|
53
|
-
await regularFile(path, "Codex config.toml");
|
|
54
|
-
const value = await readFile(path, "utf8");
|
|
55
|
-
if (Buffer.byteLength(value) > MAX_CONFIG_BYTES) throw new Error("Codex config.toml exceeds 1 MiB");
|
|
56
|
-
return value;
|
|
57
|
-
} catch (error) {
|
|
58
|
-
if (error.code === "ENOENT") return "";
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async function regularOrMissing(path, label) {
|
|
64
|
-
try {
|
|
65
|
-
await regularFile(path, label);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
if (error.code !== "ENOENT") throw error;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function managedRange(text) {
|
|
72
|
-
const starts = [...text.matchAll(new RegExp(`^${START}$`, "gm"))];
|
|
73
|
-
const ends = [...text.matchAll(new RegExp(`^${END}$`, "gm"))];
|
|
74
|
-
if (!starts.length && !ends.length) return null;
|
|
75
|
-
if (starts.length !== 1 || ends.length !== 1 || ends[0].index < starts[0].index) {
|
|
76
|
-
throw new Error("Codex config contains a malformed AgentSpine managed block");
|
|
77
|
-
}
|
|
78
|
-
let end = ends[0].index + END.length;
|
|
79
|
-
if (text.slice(end, end + 2) === "\r\n") end += 2;
|
|
80
|
-
else if (text[end] === "\n") end += 1;
|
|
81
|
-
const start = starts[0].index > 0 ? starts[0].index - 1 : starts[0].index;
|
|
82
|
-
return { start, end };
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function hasRegistration(text) {
|
|
86
|
-
return /^\s*\[\s*mcp_servers\s*\.\s*(?:agent-spine|["']agent-spine["'])\s*\]\s*(?:#.*)?$/m.test(text);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function tomlString(value) {
|
|
90
|
-
return JSON.stringify(value);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function managedBlock(nodePath, launcherPath) {
|
|
94
|
-
return [
|
|
95
|
-
START,
|
|
96
|
-
"[mcp_servers.agent-spine]",
|
|
97
|
-
`command = ${tomlString(nodePath)}`,
|
|
98
|
-
`args = [${tomlString(launcherPath)}]`,
|
|
99
|
-
"enabled = true",
|
|
100
|
-
END,
|
|
101
|
-
""
|
|
102
|
-
].join("\n");
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function updateConfig(text, block) {
|
|
106
|
-
const range = managedRange(text);
|
|
107
|
-
if (range) {
|
|
108
|
-
const prefix = text.slice(0, range.start);
|
|
109
|
-
const suffix = text.slice(range.end);
|
|
110
|
-
const outside = prefix + suffix;
|
|
111
|
-
if (hasRegistration(outside)) throw new Error("Codex config has an unmanaged AgentSpine MCP registration");
|
|
112
|
-
return { text: prefix + (prefix.length ? "\n" : "") + block + suffix,
|
|
113
|
-
outsideBefore: outside, outsideAfter: outside, status: "updated" };
|
|
114
|
-
}
|
|
115
|
-
if (hasRegistration(text)) throw new Error("Codex config has an unmanaged AgentSpine MCP registration");
|
|
116
|
-
const separator = text.length ? "\n" : "";
|
|
117
|
-
return { text: text + separator + block,
|
|
118
|
-
outsideBefore: text, outsideAfter: text, status: "installed" };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async function atomicWrite(path, content, assertOwned) {
|
|
122
|
-
const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
123
|
-
let handle;
|
|
124
|
-
try {
|
|
125
|
-
handle = await open(temporary, "wx", 0o600);
|
|
126
|
-
await handle.writeFile(content);
|
|
127
|
-
await handle.sync();
|
|
128
|
-
await handle.close();
|
|
129
|
-
handle = null;
|
|
130
|
-
await replaceFileWithRetry(temporary, path, { beforeAttempt: assertOwned });
|
|
131
|
-
} finally {
|
|
132
|
-
await handle?.close();
|
|
133
|
-
await rm(temporary, { force: true }).catch(() => {});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async function packageRegistration(packageRoot, nodePath) {
|
|
138
|
-
const root = await exactDirectory(packageRoot, "AgentSpine package root");
|
|
139
|
-
const entrypoint = await regularFile(join(root, "src", "mcp.js"), "AgentSpine MCP entrypoint");
|
|
140
|
-
const versionSource = await regularFile(join(root, "src", "version.js"), "AgentSpine version source");
|
|
141
|
-
const packageFile = await regularFile(join(root, "package.json"), "AgentSpine package manifest");
|
|
142
|
-
const packageJson = await readFile(packageFile, "utf8");
|
|
143
|
-
const pkg = JSON.parse(packageJson);
|
|
144
|
-
const source = await readFile(versionSource, "utf8");
|
|
145
|
-
const sourceVersion = source.match(/^export const VERSION = "([^"]+)";\s*$/)?.[1];
|
|
146
|
-
if (!pkg.version || sourceVersion !== pkg.version) throw new Error("AgentSpine package and runtime versions differ");
|
|
147
|
-
const canonicalNode = await realpath(await regularFile(nodePath, "Node.js runtime"));
|
|
148
|
-
return seal({
|
|
149
|
-
schema: REGISTRATION_SCHEMA,
|
|
150
|
-
expectedName: "agent-spine",
|
|
151
|
-
expectedVersion: pkg.version,
|
|
152
|
-
expectedTools: EXPECTED_TOOLS,
|
|
153
|
-
packageRoot: root,
|
|
154
|
-
nodePath: canonicalNode,
|
|
155
|
-
entrypoint,
|
|
156
|
-
entrypointDigest: sha256(await readFile(entrypoint)),
|
|
157
|
-
packageDigest: sha256(packageJson),
|
|
158
|
-
versionSourceDigest: sha256(source),
|
|
159
|
-
authority: AUTHORITY
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function defaultCodexHome(env = process.env) {
|
|
164
|
-
return resolve(env.CODEX_HOME || join(homedir(), ".codex"));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export async function installCodexMcp({
|
|
168
|
-
codexHome = defaultCodexHome(),
|
|
169
|
-
skillsRoot = defaultCodexSkillsRoot(),
|
|
170
|
-
packageRoot = fileURLToPath(new URL("../..", import.meta.url)),
|
|
171
|
-
nodePath = process.execPath,
|
|
172
|
-
faultAfter = null
|
|
173
|
-
} = {}) {
|
|
174
|
-
const home = await exactDirectory(codexHome, "Codex home", { create: true });
|
|
175
|
-
const managed = join(home, "agentspine");
|
|
176
|
-
await exactDirectory(managed, "Codex AgentSpine directory", { create: true });
|
|
177
|
-
const configPath = join(home, "config.toml");
|
|
178
|
-
const launcherPath = join(managed, "reader-launcher.mjs");
|
|
179
|
-
const registrationPath = join(managed, "registration.json");
|
|
180
|
-
const lockPath = join(managed, "install.lock");
|
|
181
|
-
try {
|
|
182
|
-
const lock = await lstat(lockPath);
|
|
183
|
-
if (lock.isSymbolicLink()) throw new Error("Codex AgentSpine install lock must not be a symlink");
|
|
184
|
-
} catch (error) {
|
|
185
|
-
if (error.code !== "ENOENT") throw error;
|
|
186
|
-
}
|
|
187
|
-
return withOwnedFileLock(lockPath, async ({ assertOwned }) => {
|
|
188
|
-
await Promise.all([
|
|
189
|
-
regularOrMissing(launcherPath, "Codex AgentSpine launcher"),
|
|
190
|
-
regularOrMissing(registrationPath, "Codex AgentSpine registration")
|
|
191
|
-
]);
|
|
192
|
-
const [before, registration, launcher] = await Promise.all([
|
|
193
|
-
optionalText(configPath), packageRegistration(packageRoot, nodePath),
|
|
194
|
-
readFile(fileURLToPath(new URL("../codex-reader-launcher.js", import.meta.url)))
|
|
195
|
-
]);
|
|
196
|
-
const update = updateConfig(before, managedBlock(registration.nodePath, launcherPath));
|
|
197
|
-
if (update.outsideBefore !== update.outsideAfter) throw new Error("unmanaged Codex configuration bytes changed");
|
|
198
|
-
const skill = await installCodexSkill({
|
|
199
|
-
skillsRoot, packageRoot,
|
|
200
|
-
faultAfter: faultAfter === "skill-marker" ? "marker" : faultAfter === "skill" ? "skill" : null
|
|
201
|
-
});
|
|
202
|
-
if (skill.version !== registration.expectedVersion || skill.packageRoot !== registration.packageRoot) {
|
|
203
|
-
throw new Error("Codex skill and MCP reader package identity differ");
|
|
204
|
-
}
|
|
205
|
-
await atomicWrite(launcherPath, launcher, assertOwned);
|
|
206
|
-
if (faultAfter === "launcher") throw new Error("synthetic crash after launcher publication");
|
|
207
|
-
await atomicWrite(registrationPath, `${JSON.stringify(registration, null, 2)}\n`, assertOwned);
|
|
208
|
-
if (faultAfter === "registration") throw new Error("synthetic crash after registration publication");
|
|
209
|
-
await atomicWrite(configPath, update.text, assertOwned);
|
|
210
|
-
return {
|
|
211
|
-
schema: "agentspine.codex-installation/v1",
|
|
212
|
-
status: update.status,
|
|
213
|
-
version: registration.expectedVersion,
|
|
214
|
-
configPath,
|
|
215
|
-
launcherPath,
|
|
216
|
-
registrationPath,
|
|
217
|
-
registrationDigest: registration.digest,
|
|
218
|
-
unmanagedBeforeDigest: sha256(update.outsideBefore),
|
|
219
|
-
unmanagedAfterDigest: sha256(update.outsideAfter),
|
|
220
|
-
configDigest: sha256(update.text),
|
|
221
|
-
skill,
|
|
222
|
-
restartRequired: true,
|
|
223
|
-
authority: AUTHORITY
|
|
224
|
-
};
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export function stripManagedCodexBlock(text) {
|
|
229
|
-
const range = managedRange(text);
|
|
230
|
-
return range ? text.slice(0, range.start) + text.slice(range.end) : text;
|
|
231
|
-
}
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { lstat, mkdir, open, readFile, realpath, rm } from "node:fs/promises";
|
|
3
|
-
import { homedir } from "node:os";
|
|
4
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
-
import { withOwnedFileLock } from "./owned-file-lock.js";
|
|
6
|
-
import { replaceFileWithRetry } from "./filesystem-retry.js";
|
|
7
|
-
|
|
8
|
-
const MARKER_SCHEMA = "agentspine.codex-skill-installation/v1";
|
|
9
|
-
const AUTHORITY = "host-registration-only";
|
|
10
|
-
const SKILL_NAME = "agent-spine";
|
|
11
|
-
const MAX_SKILL_BYTES = 256 * 1024;
|
|
12
|
-
|
|
13
|
-
function sha256(value) {
|
|
14
|
-
return createHash("sha256").update(value).digest("hex");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function canonical(value) {
|
|
18
|
-
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
19
|
-
if (value && typeof value === "object") {
|
|
20
|
-
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}`;
|
|
21
|
-
}
|
|
22
|
-
return JSON.stringify(value);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function seal(value) {
|
|
26
|
-
return { ...value, digest: sha256(canonical(value)) };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function verifySeal(value) {
|
|
30
|
-
if (!value || typeof value !== "object" || Array.isArray(value) || typeof value.digest !== "string") return false;
|
|
31
|
-
const { digest, ...payload } = value;
|
|
32
|
-
return digest === sha256(canonical(payload));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function exactDirectory(path, label, { create = false } = {}) {
|
|
36
|
-
if (!isAbsolute(path)) throw new Error(`${label} must be absolute`);
|
|
37
|
-
const target = resolve(path);
|
|
38
|
-
if (create) await mkdir(target, { recursive: true, mode: 0o700 });
|
|
39
|
-
const metadata = await lstat(target);
|
|
40
|
-
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
41
|
-
throw new Error(`${label} must be a regular non-symlink directory`);
|
|
42
|
-
}
|
|
43
|
-
return realpath(target);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function regularOrMissing(path, label) {
|
|
47
|
-
try {
|
|
48
|
-
const metadata = await lstat(path);
|
|
49
|
-
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
50
|
-
throw new Error(`${label} must be a regular non-symlink file`);
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
} catch (error) {
|
|
54
|
-
if (error.code === "ENOENT") return false;
|
|
55
|
-
throw error;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function atomicWrite(path, content, assertOwned) {
|
|
60
|
-
const temporary = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
61
|
-
let handle;
|
|
62
|
-
try {
|
|
63
|
-
handle = await open(temporary, "wx", 0o600);
|
|
64
|
-
await handle.writeFile(content);
|
|
65
|
-
await handle.sync();
|
|
66
|
-
await handle.close();
|
|
67
|
-
handle = null;
|
|
68
|
-
await replaceFileWithRetry(temporary, path, { beforeAttempt: assertOwned });
|
|
69
|
-
} finally {
|
|
70
|
-
await handle?.close();
|
|
71
|
-
await rm(temporary, { force: true }).catch(() => {});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async function sourceSkill(packageRoot) {
|
|
76
|
-
const root = await exactDirectory(packageRoot, "AgentSpine package root");
|
|
77
|
-
const packagePath = join(root, "package.json");
|
|
78
|
-
const skillPath = join(root, "skills", SKILL_NAME, "SKILL.md");
|
|
79
|
-
if (!await regularOrMissing(packagePath, "AgentSpine package manifest")) {
|
|
80
|
-
throw new Error("AgentSpine package manifest is missing");
|
|
81
|
-
}
|
|
82
|
-
if (!await regularOrMissing(skillPath, "AgentSpine skill source")) {
|
|
83
|
-
throw new Error("AgentSpine skill source is missing");
|
|
84
|
-
}
|
|
85
|
-
const [packageText, content] = await Promise.all([
|
|
86
|
-
readFile(packagePath, "utf8"), readFile(skillPath)
|
|
87
|
-
]);
|
|
88
|
-
if (content.length === 0 || content.length > MAX_SKILL_BYTES) {
|
|
89
|
-
throw new Error("AgentSpine skill source size is outside the install limit");
|
|
90
|
-
}
|
|
91
|
-
const version = JSON.parse(packageText).version;
|
|
92
|
-
if (typeof version !== "string" || !version) throw new Error("AgentSpine package version is missing");
|
|
93
|
-
if (!content.toString("utf8").startsWith("---\nname: agent-spine\n")) {
|
|
94
|
-
throw new Error("AgentSpine skill source has invalid frontmatter");
|
|
95
|
-
}
|
|
96
|
-
return { packageRoot: root, sourcePath: skillPath, version, content, digest: sha256(content) };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function readMarker(path) {
|
|
100
|
-
if (!await regularOrMissing(path, "Codex AgentSpine skill marker")) return null;
|
|
101
|
-
let value;
|
|
102
|
-
try { value = JSON.parse(await readFile(path, "utf8")); }
|
|
103
|
-
catch (error) { throw new Error("Codex AgentSpine skill marker is malformed", { cause: error }); }
|
|
104
|
-
if (!verifySeal(value) || value.schema !== MARKER_SCHEMA || !["updating", "installed"].includes(value.state)) {
|
|
105
|
-
throw new Error("Codex AgentSpine skill marker is invalid");
|
|
106
|
-
}
|
|
107
|
-
return value;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function digestOrNull(path) {
|
|
111
|
-
return await regularOrMissing(path, "Codex AgentSpine SKILL.md") ? sha256(await readFile(path)) : null;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async function recoverMarker(marker, markerPath, skillPath, assertOwned) {
|
|
115
|
-
if (!marker) return null;
|
|
116
|
-
const actual = await digestOrNull(skillPath);
|
|
117
|
-
if (marker.state === "installed") {
|
|
118
|
-
if (actual !== marker.skillDigest) throw new Error("installed Codex AgentSpine skill was changed outside its managed update");
|
|
119
|
-
return marker;
|
|
120
|
-
}
|
|
121
|
-
if (actual === marker.targetDigest) {
|
|
122
|
-
const recovered = seal({
|
|
123
|
-
schema: MARKER_SCHEMA, state: "installed", skillName: SKILL_NAME,
|
|
124
|
-
skillDigest: marker.targetDigest, version: marker.targetVersion,
|
|
125
|
-
packageRoot: marker.targetPackageRoot, authority: AUTHORITY
|
|
126
|
-
});
|
|
127
|
-
await atomicWrite(markerPath, `${JSON.stringify(recovered, null, 2)}\n`, assertOwned);
|
|
128
|
-
return recovered;
|
|
129
|
-
}
|
|
130
|
-
if (actual === marker.previousDigest) {
|
|
131
|
-
if (actual === null) {
|
|
132
|
-
await assertOwned();
|
|
133
|
-
await rm(markerPath, { force: true });
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
const recovered = seal({
|
|
137
|
-
schema: MARKER_SCHEMA, state: "installed", skillName: SKILL_NAME,
|
|
138
|
-
skillDigest: marker.previousDigest, version: marker.previousVersion,
|
|
139
|
-
packageRoot: marker.previousPackageRoot, authority: AUTHORITY
|
|
140
|
-
});
|
|
141
|
-
await atomicWrite(markerPath, `${JSON.stringify(recovered, null, 2)}\n`, assertOwned);
|
|
142
|
-
return recovered;
|
|
143
|
-
}
|
|
144
|
-
throw new Error("Codex AgentSpine skill update has an unrecognized partial state");
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function defaultCodexSkillsRoot(env = process.env) {
|
|
148
|
-
const home = env.HOME || env.USERPROFILE || homedir();
|
|
149
|
-
return resolve(env.AGENTSPINE_CODEX_SKILLS_ROOT || join(home, ".agents", "skills"));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export async function installCodexSkill({
|
|
153
|
-
skillsRoot = defaultCodexSkillsRoot(), packageRoot,
|
|
154
|
-
faultAfter = null
|
|
155
|
-
} = {}) {
|
|
156
|
-
const source = await sourceSkill(packageRoot);
|
|
157
|
-
const root = await exactDirectory(skillsRoot, "Codex user skills root", { create: true });
|
|
158
|
-
const skillDirectoryPath = join(root, SKILL_NAME);
|
|
159
|
-
const lockPath = join(root, ".agent-spine-install.lock");
|
|
160
|
-
if (await regularOrMissing(lockPath, "Codex AgentSpine skill install lock")) {
|
|
161
|
-
// withOwnedFileLock owns regular stale/current lock handling.
|
|
162
|
-
}
|
|
163
|
-
return withOwnedFileLock(lockPath, async ({ assertOwned, recovered: lockRecovered }) => {
|
|
164
|
-
let created = false;
|
|
165
|
-
try { await exactDirectory(skillDirectoryPath, "Codex AgentSpine skill directory"); }
|
|
166
|
-
catch (error) {
|
|
167
|
-
if (error.code !== "ENOENT") throw error;
|
|
168
|
-
await mkdir(skillDirectoryPath, { mode: 0o700 });
|
|
169
|
-
created = true;
|
|
170
|
-
}
|
|
171
|
-
const skillDirectory = await realpath(skillDirectoryPath);
|
|
172
|
-
const markerPath = join(skillDirectory, ".agentspine-install.json");
|
|
173
|
-
const skillPath = join(skillDirectory, "SKILL.md");
|
|
174
|
-
let marker = await readMarker(markerPath);
|
|
175
|
-
if (!marker && !created) {
|
|
176
|
-
throw new Error("Codex already has an unmanaged agent-spine skill directory");
|
|
177
|
-
}
|
|
178
|
-
marker = await recoverMarker(marker, markerPath, skillPath, assertOwned);
|
|
179
|
-
if (marker?.skillDigest === source.digest && marker.version === source.version
|
|
180
|
-
&& marker.packageRoot === source.packageRoot) {
|
|
181
|
-
return {
|
|
182
|
-
schema: "agentspine.codex-skill-install-result/v1", status: "unchanged",
|
|
183
|
-
version: source.version, packageRoot: source.packageRoot, skillPath, skillDigest: source.digest,
|
|
184
|
-
markerPath, markerDigest: marker.digest, recovered: lockRecovered, authority: AUTHORITY
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
const updating = seal({
|
|
188
|
-
schema: MARKER_SCHEMA, state: "updating", skillName: SKILL_NAME,
|
|
189
|
-
previousDigest: marker?.skillDigest ?? null, previousVersion: marker?.version ?? null,
|
|
190
|
-
previousPackageRoot: marker?.packageRoot ?? null,
|
|
191
|
-
targetDigest: source.digest, targetVersion: source.version,
|
|
192
|
-
targetPackageRoot: source.packageRoot, authority: AUTHORITY
|
|
193
|
-
});
|
|
194
|
-
await atomicWrite(markerPath, `${JSON.stringify(updating, null, 2)}\n`, assertOwned);
|
|
195
|
-
if (faultAfter === "marker") throw new Error("synthetic crash after Codex skill update marker");
|
|
196
|
-
await atomicWrite(skillPath, source.content, assertOwned);
|
|
197
|
-
if (faultAfter === "skill") throw new Error("synthetic crash after Codex skill publication");
|
|
198
|
-
const installed = seal({
|
|
199
|
-
schema: MARKER_SCHEMA, state: "installed", skillName: SKILL_NAME,
|
|
200
|
-
skillDigest: source.digest, version: source.version,
|
|
201
|
-
packageRoot: source.packageRoot, authority: AUTHORITY
|
|
202
|
-
});
|
|
203
|
-
await atomicWrite(markerPath, `${JSON.stringify(installed, null, 2)}\n`, assertOwned);
|
|
204
|
-
return {
|
|
205
|
-
schema: "agentspine.codex-skill-install-result/v1",
|
|
206
|
-
status: marker ? "updated" : "installed", version: source.version, packageRoot: source.packageRoot,
|
|
207
|
-
skillPath, skillDigest: source.digest, markerPath, markerDigest: installed.digest,
|
|
208
|
-
recovered: lockRecovered, authority: AUTHORITY
|
|
209
|
-
};
|
|
210
|
-
});
|
|
211
|
-
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
-
import { ancestorsBetween, canonicalPath, isInside } from "./paths.js";
|
|
4
|
-
import { buildCatalog } from "./catalog.js";
|
|
5
|
-
import { attachDocumentSnapshot, documentSnapshotContent } from "./documents.js";
|
|
6
|
-
import { loadGraph } from "./graph.js";
|
|
7
|
-
import { readCatalogDocument } from "./catalog-document-read.js";
|
|
8
|
-
|
|
9
|
-
function depth(root, path) {
|
|
10
|
-
const value = relative(root, dirname(path));
|
|
11
|
-
return value ? value.split(/[\\/]/).length : 0;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function nativeDocuments(catalog, cwd, host, graph) {
|
|
15
|
-
if (catalog.sourceRegistry) {
|
|
16
|
-
return [...catalog.documents]
|
|
17
|
-
.filter((document) => document.hosts.includes(host))
|
|
18
|
-
.sort((left, right) => left.precedence - right.precedence || left.relativePath.localeCompare(right.relativePath));
|
|
19
|
-
}
|
|
20
|
-
const ancestors = new Set(ancestorsBetween(catalog.root, cwd));
|
|
21
|
-
const inScope = catalog.documents.filter((document) => ancestors.has(dirname(document.path)));
|
|
22
|
-
const selected = [];
|
|
23
|
-
|
|
24
|
-
if (host === "codex") {
|
|
25
|
-
for (const directory of ancestorsBetween(catalog.root, cwd)) {
|
|
26
|
-
const local = inScope.filter((document) => dirname(document.path) === directory);
|
|
27
|
-
const override = local.find((document) => document.name === "AGENTS.override.md");
|
|
28
|
-
const regular = local.find((document) => document.name === "AGENTS.md");
|
|
29
|
-
if (override || regular) selected.push(override || regular);
|
|
30
|
-
}
|
|
31
|
-
} else if (host === "claude") {
|
|
32
|
-
selected.push(...inScope.filter((document) =>
|
|
33
|
-
document.name === "CLAUDE.md" || document.name === "CLAUDE.local.md"
|
|
34
|
-
));
|
|
35
|
-
} else {
|
|
36
|
-
selected.push(...inScope.filter((document) => document.layer === "constitution"));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
selected.push(...inScope.filter((document) => document.layer === "soul"));
|
|
40
|
-
selected.push(...inScope.filter((document) => document.layer === "memory-index"));
|
|
41
|
-
const inferredLayers = new Set(["constitution", "soul", "memory-index"]);
|
|
42
|
-
const inferredPaths = new Set(graph.annotations
|
|
43
|
-
.filter((annotation) => inferredLayers.has(annotation.layer))
|
|
44
|
-
.map((annotation) => annotation.path));
|
|
45
|
-
selected.push(...inScope.filter((document) => inferredPaths.has(document.relativePath)));
|
|
46
|
-
return [...new Map(selected.map((document) => [document.relativePath, document])).values()];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function addLinkedDocuments(catalog, seed, graph) {
|
|
50
|
-
const byPath = new Map(catalog.documents.map((document) => [document.relativePath, document]));
|
|
51
|
-
const selected = new Map(seed.map((document) => [document.relativePath, document]));
|
|
52
|
-
const queue = [...seed];
|
|
53
|
-
while (queue.length) {
|
|
54
|
-
const current = queue.shift();
|
|
55
|
-
const learnedLinks = graph.edges
|
|
56
|
-
.filter((edge) => edge.from === current.relativePath && edge.confidence >= 0.6)
|
|
57
|
-
.map((edge) => edge.to);
|
|
58
|
-
for (const linkedPath of [...current.links, ...learnedLinks]) {
|
|
59
|
-
const linked = byPath.get(linkedPath);
|
|
60
|
-
if (linked && !selected.has(linked.relativePath)) {
|
|
61
|
-
selected.set(linked.relativePath, linked);
|
|
62
|
-
queue.push(linked);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return [...selected.values()];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function precedence(layer) {
|
|
70
|
-
if (layer === "constitution") return 0;
|
|
71
|
-
if (layer === "soul") return 1;
|
|
72
|
-
if (layer === "memory-index") return 2;
|
|
73
|
-
if (layer === "memory-fact") return 3;
|
|
74
|
-
return 4;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export async function resolveContext({ root = process.cwd(), cwd = root, host = "generic", maxBytes = 65536, includeContent = true, catalog: providedCatalog = null } = {}) {
|
|
78
|
-
const canonicalRoot = await canonicalPath(root);
|
|
79
|
-
const canonicalCwd = await canonicalPath(cwd);
|
|
80
|
-
if (!isInside(canonicalRoot, canonicalCwd) && !providedCatalog?.sourceRegistry) {
|
|
81
|
-
throw new Error(`cwd must be inside root: ${canonicalCwd}`);
|
|
82
|
-
}
|
|
83
|
-
const catalog = providedCatalog || await buildCatalog(canonicalRoot);
|
|
84
|
-
if (catalog.root !== canonicalRoot) throw new Error("provided catalog belongs to a different project root");
|
|
85
|
-
const { graph } = await loadGraph(canonicalRoot, catalog);
|
|
86
|
-
const annotations = new Map(graph.annotations.map((annotation) => [annotation.path, annotation]));
|
|
87
|
-
const seed = nativeDocuments(catalog, canonicalCwd, host, graph);
|
|
88
|
-
const documents = addLinkedDocuments(catalog, seed, graph)
|
|
89
|
-
.map((document) => {
|
|
90
|
-
const annotation = annotations.get(document.relativePath);
|
|
91
|
-
const resolved = {
|
|
92
|
-
...document,
|
|
93
|
-
effectiveLayer: annotation?.layer || document.layer,
|
|
94
|
-
authority: annotation ? "context-only" : document.authority,
|
|
95
|
-
classificationSource: annotation ? "agent-overlay" : document.classificationSource
|
|
96
|
-
};
|
|
97
|
-
return attachDocumentSnapshot(resolved, documentSnapshotContent(document));
|
|
98
|
-
})
|
|
99
|
-
.sort((a, b) => catalog.sourceRegistry
|
|
100
|
-
? a.precedence - b.precedence || a.relativePath.localeCompare(b.relativePath)
|
|
101
|
-
: precedence(a.effectiveLayer) - precedence(b.effectiveLayer) || depth(catalog.root, a.path) - depth(catalog.root, b.path) || a.relativePath.localeCompare(b.relativePath));
|
|
102
|
-
|
|
103
|
-
let remaining = Math.max(0, Number(maxBytes) || 0);
|
|
104
|
-
const resolved = [];
|
|
105
|
-
for (const document of documents) {
|
|
106
|
-
let content = null;
|
|
107
|
-
let loaded = false;
|
|
108
|
-
if (includeContent && document.bytes <= remaining) {
|
|
109
|
-
const snapshot = documentSnapshotContent(document);
|
|
110
|
-
content = snapshot ? snapshot.toString("utf8") : await readFile(document.path, "utf8");
|
|
111
|
-
remaining -= Buffer.byteLength(content);
|
|
112
|
-
loaded = true;
|
|
113
|
-
}
|
|
114
|
-
resolved.push({ ...document, content, loaded });
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
schema: "agentspine.context/v1",
|
|
119
|
-
root: canonicalRoot,
|
|
120
|
-
cwd: canonicalCwd,
|
|
121
|
-
host,
|
|
122
|
-
graph: {
|
|
123
|
-
edges: graph.edges,
|
|
124
|
-
annotations: graph.annotations,
|
|
125
|
-
authority: "context-only"
|
|
126
|
-
},
|
|
127
|
-
conflicts: catalog.conflicts,
|
|
128
|
-
budget: { maxBytes, remainingBytes: remaining },
|
|
129
|
-
documents: resolved,
|
|
130
|
-
omitted: resolved.filter((document) => !document.loaded).map((document) => document.relativePath),
|
|
131
|
-
note: "Omitted content remains available byte-for-byte through agentspine read or the MCP read_document tool."
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export async function readDocument({ root = process.cwd(), path, offset = 0, length = 65536, catalog: providedCatalog = null }) {
|
|
136
|
-
if (!path) throw new Error("path is required");
|
|
137
|
-
if (providedCatalog) return readCatalogDocument({ root, path, offset, length, catalog: providedCatalog });
|
|
138
|
-
const catalog = await buildCatalog(root);
|
|
139
|
-
const absolute = resolve(catalog.root, path);
|
|
140
|
-
const document = catalog.documents.find((item) => item.path === absolute);
|
|
141
|
-
if (!document) throw new Error("path is not an indexed Markdown document");
|
|
142
|
-
const buffer = await readFile(absolute);
|
|
143
|
-
const start = Math.max(0, Number(offset) || 0);
|
|
144
|
-
const end = Math.min(buffer.length, start + Math.max(0, Number(length) || 0));
|
|
145
|
-
return {
|
|
146
|
-
path: document.relativePath,
|
|
147
|
-
sha256: document.sha256,
|
|
148
|
-
offset: start,
|
|
149
|
-
bytes: end - start,
|
|
150
|
-
totalBytes: buffer.length,
|
|
151
|
-
eof: end >= buffer.length,
|
|
152
|
-
encoding: "utf8",
|
|
153
|
-
content: buffer.subarray(start, end).toString("utf8"),
|
|
154
|
-
contentBase64: buffer.subarray(start, end).toString("base64")
|
|
155
|
-
};
|
|
156
|
-
}
|