@fitlab-ai/agent-infra 0.11.11 → 0.11.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/internal-cli.js +2 -2
- package/dist/lib/agent-clients/adapter.js +7 -1
- package/dist/lib/agent-clients/adapters/claude-code.js +5 -1
- package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +78 -63
- package/dist/lib/agent-clients/adapters/codex-lifecycle/binding.js +114 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/cli.js +447 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/controller-client.js +130 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/controller-operation.js +131 -0
- package/dist/lib/{sandbox/control → agent-clients/adapters/codex-lifecycle}/controller-registration.js +2 -2
- package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +48 -1
- package/dist/lib/agent-clients/adapters/codex-lifecycle/recovery.js +124 -62
- package/dist/lib/{internal/codex-sandbox-controller.js → agent-clients/adapters/codex-lifecycle/sandbox-controller-cli.js} +3 -3
- package/dist/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.js +3 -5
- package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +211 -213
- package/dist/lib/agent-clients/adapters/codex-orchestration.js +468 -0
- package/dist/lib/agent-clients/adapters/codex.js +40 -1
- package/dist/lib/agent-clients/adapters/index.js +2 -10
- package/dist/lib/agent-clients/tokens.js +3 -2
- package/dist/lib/internal/agent-client.js +132 -72
- package/dist/lib/internal/cli-route-inventory.js +2 -2
- package/dist/lib/internal/sandbox-control.js +1 -81
- package/dist/lib/internal/task-finalization.js +26 -19
- package/dist/lib/internal/task-lifecycle.js +1 -1
- package/dist/lib/internal/task-operation-registry.js +1 -1
- package/dist/lib/internal/task-validate.js +1 -1
- package/dist/lib/platform/github-client.js +33 -3
- package/dist/lib/platform/issues.js +18 -6
- package/dist/lib/runtime/agent-runtime.js +6 -29
- package/dist/lib/sandbox/commands/control-status.js +1 -0
- package/dist/lib/sandbox/commands/create.js +7 -2
- package/dist/lib/sandbox/commands/ls.js +4 -5
- package/dist/lib/sandbox/commands/rm.js +1 -1
- package/dist/lib/sandbox/control/agent-operation.js +34 -0
- package/dist/lib/sandbox/control/client.js +14 -201
- package/dist/lib/sandbox/control/executor.js +12 -125
- package/dist/lib/sandbox/control/lifecycle.js +66 -4
- package/dist/lib/sandbox/control/protocol.js +15 -54
- package/dist/lib/sandbox/control/server.js +216 -281
- package/dist/lib/sandbox/control/task-view.js +0 -13
- package/dist/lib/sandbox/mounts.js +9 -1
- package/dist/lib/sandbox/recovery.js +31 -10
- package/dist/lib/sandbox/removal.js +28 -5
- package/dist/lib/sandbox/workspace-identity.js +18 -6
- package/dist/lib/sandbox/workspace-view.js +56 -19
- package/dist/lib/task/commands/ls.js +3 -3
- package/dist/lib/task/control-authority.js +56 -264
- package/dist/lib/task/control-recovery.js +2 -3
- package/dist/lib/task/create.js +2 -2
- package/dist/lib/task/delegation-receipts.js +74 -161
- package/dist/lib/task/events.js +88 -44
- package/dist/lib/task/finalization.js +35 -6
- package/dist/lib/task/lifecycle.js +274 -115
- package/dist/lib/task/orchestration.js +201 -194
- package/dist/lib/task/runtime-paths.js +10 -0
- package/dist/lib/task/short-id.js +37 -27
- package/dist/lib/task/verification-engine.js +32 -26
- package/dist/lib/task/workspace-snapshot.js +1 -1
- package/dist/lifecycle-build-manifest.json +3 -3
- package/lib/agent-clients/adapter.ts +39 -0
- package/lib/agent-clients/adapters/claude-code.ts +5 -1
- package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +105 -80
- package/lib/agent-clients/adapters/codex-lifecycle/binding.ts +147 -0
- package/lib/agent-clients/adapters/codex-lifecycle/cli.ts +510 -0
- package/lib/agent-clients/adapters/codex-lifecycle/controller-client.ts +158 -0
- package/lib/agent-clients/adapters/codex-lifecycle/controller-context.ts +1 -1
- package/lib/agent-clients/adapters/codex-lifecycle/controller-operation.ts +151 -0
- package/lib/{sandbox/control → agent-clients/adapters/codex-lifecycle}/controller-registration.ts +4 -4
- package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +43 -0
- package/lib/agent-clients/adapters/codex-lifecycle/manifest-files.json +8 -5
- package/lib/agent-clients/adapters/codex-lifecycle/recovery.ts +190 -65
- package/lib/{internal/codex-sandbox-controller.ts → agent-clients/adapters/codex-lifecycle/sandbox-controller-cli.ts} +2 -2
- package/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.ts +3 -5
- package/lib/agent-clients/adapters/codex-lifecycle/store.ts +270 -224
- package/lib/agent-clients/adapters/codex-orchestration.ts +632 -0
- package/lib/agent-clients/adapters/codex.ts +44 -1
- package/lib/agent-clients/adapters/index.ts +7 -20
- package/lib/agent-clients/tokens.ts +4 -3
- package/lib/internal/agent-client.ts +142 -73
- package/lib/internal/cli-route-inventory.ts +2 -2
- package/lib/internal/sandbox-control.ts +0 -97
- package/lib/internal/task-finalization.ts +30 -18
- package/lib/internal/task-lifecycle.ts +1 -1
- package/lib/internal/task-operation-registry.ts +1 -1
- package/lib/internal/task-validate.ts +1 -1
- package/lib/platform/github-client.ts +34 -3
- package/lib/platform/issues.ts +18 -6
- package/lib/runtime/agent-runtime.ts +6 -41
- package/lib/sandbox/commands/control-status.ts +1 -0
- package/lib/sandbox/commands/create.ts +7 -1
- package/lib/sandbox/commands/ls.ts +4 -5
- package/lib/sandbox/commands/rm.ts +1 -1
- package/lib/sandbox/control/agent-operation.ts +49 -0
- package/lib/sandbox/control/client.ts +20 -254
- package/lib/sandbox/control/executor.ts +13 -136
- package/lib/sandbox/control/lifecycle.ts +67 -4
- package/lib/sandbox/control/protocol.ts +22 -66
- package/lib/sandbox/control/server.ts +21 -95
- package/lib/sandbox/control/task-view.ts +0 -19
- package/lib/sandbox/mounts.ts +10 -1
- package/lib/sandbox/recovery.ts +38 -17
- package/lib/sandbox/removal.ts +33 -7
- package/lib/sandbox/workspace-identity.ts +16 -6
- package/lib/sandbox/workspace-view.ts +50 -18
- package/lib/task/commands/ls.ts +3 -3
- package/lib/task/control-authority.ts +66 -378
- package/lib/task/control-recovery.ts +3 -4
- package/lib/task/create.ts +3 -3
- package/lib/task/delegation-receipts.ts +87 -231
- package/lib/task/events.ts +100 -42
- package/lib/task/finalization.ts +37 -7
- package/lib/task/lifecycle.ts +257 -109
- package/lib/task/orchestration.ts +260 -237
- package/lib/task/runtime-paths.ts +12 -0
- package/lib/task/short-id.ts +33 -40
- package/lib/task/verification-engine.ts +30 -25
- package/lib/task/workspace-snapshot.ts +1 -1
- package/package.json +1 -1
- package/templates/.agents/hooks/lifecycle-delegation.js +1 -25
- package/templates/.agents/rules/README.en.md +2 -1
- package/templates/.agents/rules/README.zh-CN.md +2 -1
- package/templates/.agents/rules/next-step-output.en.md +6 -19
- package/templates/.agents/rules/next-step-output.zh-CN.md +6 -19
- package/templates/.agents/rules/strategy-pattern.en.md +25 -0
- package/templates/.agents/rules/strategy-pattern.zh-CN.md +25 -0
- package/templates/.agents/skills/analyze-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/analyze-task/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/check-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/check-task/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/code-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/code-task/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/code-task/reference/code-rules.en.md +4 -0
- package/templates/.agents/skills/code-task/reference/code-rules.zh-CN.md +4 -0
- package/templates/.agents/skills/complete-manual-validation/SKILL.en.md +0 -1
- package/templates/.agents/skills/complete-manual-validation/SKILL.zh-CN.md +0 -1
- package/templates/.agents/skills/complete-task/SKILL.en.md +5 -29
- package/templates/.agents/skills/complete-task/SKILL.zh-CN.md +5 -26
- package/templates/.agents/skills/plan-task/SKILL.en.md +7 -4
- package/templates/.agents/skills/plan-task/SKILL.zh-CN.md +7 -4
- package/templates/.agents/skills/review-analysis/SKILL.en.md +1 -1
- package/templates/.agents/skills/review-analysis/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/review-code/SKILL.en.md +1 -1
- package/templates/.agents/skills/review-code/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/review-plan/SKILL.en.md +1 -1
- package/templates/.agents/skills/review-plan/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/run-manual-validation/SKILL.en.md +1 -1
- package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/run-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/run-task/SKILL.zh-CN.md +1 -1
- package/dist/lib/agent-clients/adapters/codex-lifecycle/capability-store.js +0 -546
- package/dist/lib/internal/codex-lifecycle.js +0 -385
- package/dist/lib/task/codex-orchestration.js +0 -206
- package/lib/agent-clients/adapters/codex-lifecycle/capability-store.ts +0 -776
- package/lib/internal/codex-lifecycle.ts +0 -390
- package/lib/task/codex-orchestration.ts +0 -276
|
@@ -1,546 +0,0 @@
|
|
|
1
|
-
import crypto from 'node:crypto';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { isCanonicalLifecyclePackageVersion, isLifecycleProtocolVersion, LIFECYCLE_PROTOCOL_VERSION, verifyLifecycleBuildIdentity } from './build-identity.js';
|
|
5
|
-
import { resolveAgentRuntimeStoreRoot } from '../../../runtime/agent-runtime.js';
|
|
6
|
-
const DEFAULT_TTL_MS = 30_000;
|
|
7
|
-
const DEFAULT_TOMBSTONE_MS = 86_400_000;
|
|
8
|
-
const DEFAULT_MAX_RECORDS = 128;
|
|
9
|
-
const LOCK_RETRY_MS = 10;
|
|
10
|
-
const LOCK_TIMEOUT_MS = 1_000;
|
|
11
|
-
const LOCK_STALE_MS = 30_000;
|
|
12
|
-
const DETAIL_DOMAIN = 'codex-capability-provenance/v1';
|
|
13
|
-
const TASK_ID_PATTERN = /^TASK-[0-9]{8}-[0-9]{6}$/u;
|
|
14
|
-
const SHA256_PATTERN = /^[0-9a-f]{64}$/u;
|
|
15
|
-
const DIGEST_PREFIX_PATTERN = /^sha256:[0-9a-f]{16}$/u;
|
|
16
|
-
function digest(value) {
|
|
17
|
-
return crypto.createHash('sha256').update(value).digest('hex');
|
|
18
|
-
}
|
|
19
|
-
function capabilityError(code, message, detail) {
|
|
20
|
-
const error = new Error(`${code}: ${message}`);
|
|
21
|
-
error.name = code;
|
|
22
|
-
if (detail)
|
|
23
|
-
error.detail = detail;
|
|
24
|
-
return error;
|
|
25
|
-
}
|
|
26
|
-
function objectValue(value, key) {
|
|
27
|
-
if (!value || typeof value !== 'object')
|
|
28
|
-
return undefined;
|
|
29
|
-
return value[key];
|
|
30
|
-
}
|
|
31
|
-
function digestPrefix(fieldName, value) {
|
|
32
|
-
return {
|
|
33
|
-
kind: 'digest-prefix',
|
|
34
|
-
value: `sha256:${crypto.createHash('sha256')
|
|
35
|
-
.update(`${DETAIL_DOMAIN}\0${fieldName}\0${value}`)
|
|
36
|
-
.digest('hex')
|
|
37
|
-
.slice(0, 16)}`
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function projectDigest(fieldName, value, pattern) {
|
|
41
|
-
return typeof value === 'string' && pattern.test(value) ? digestPrefix(fieldName, value) : { kind: 'absent' };
|
|
42
|
-
}
|
|
43
|
-
function projectTaskId(value) {
|
|
44
|
-
return projectDigest('taskId', value, TASK_ID_PATTERN);
|
|
45
|
-
}
|
|
46
|
-
function projectHash(fieldName, value) {
|
|
47
|
-
return projectDigest(fieldName, value, SHA256_PATTERN);
|
|
48
|
-
}
|
|
49
|
-
function projectProtocol(value) {
|
|
50
|
-
return isLifecycleProtocolVersion(value)
|
|
51
|
-
? { kind: 'protocol-version', value: LIFECYCLE_PROTOCOL_VERSION }
|
|
52
|
-
: { kind: 'absent' };
|
|
53
|
-
}
|
|
54
|
-
function projectPackageVersion(value) {
|
|
55
|
-
return isCanonicalLifecyclePackageVersion(value)
|
|
56
|
-
? { kind: 'semver', value }
|
|
57
|
-
: { kind: 'absent' };
|
|
58
|
-
}
|
|
59
|
-
function projectPresence(value) {
|
|
60
|
-
return { kind: 'presence', present: Boolean(value && typeof value === 'object') };
|
|
61
|
-
}
|
|
62
|
-
function field(matches, expected, actual) {
|
|
63
|
-
return { matches, expected, actual };
|
|
64
|
-
}
|
|
65
|
-
function provenanceDetail(record, expected) {
|
|
66
|
-
const actualBuild = record.buildIdentity;
|
|
67
|
-
const expectedBuild = expected.buildIdentity;
|
|
68
|
-
const actualController = record.controller;
|
|
69
|
-
const expectedController = expected.controller ?? null;
|
|
70
|
-
return {
|
|
71
|
-
kind: 'codex-capability-provenance-mismatch',
|
|
72
|
-
version: 1,
|
|
73
|
-
fields: {
|
|
74
|
-
taskId: field(record.taskId === expected.taskId, projectTaskId(expected.taskId), projectTaskId(record.taskId)),
|
|
75
|
-
hookDefinitionHash: field(record.hookDefinitionHash === expected.hookDefinitionHash, projectHash('hookDefinitionHash', expected.hookDefinitionHash), projectHash('hookDefinitionHash', record.hookDefinitionHash)),
|
|
76
|
-
buildIdentity: {
|
|
77
|
-
protocolVersion: field(objectValue(actualBuild, 'protocolVersion') === objectValue(expectedBuild, 'protocolVersion'), projectProtocol(objectValue(expectedBuild, 'protocolVersion')), projectProtocol(objectValue(actualBuild, 'protocolVersion'))),
|
|
78
|
-
packageVersion: field(objectValue(actualBuild, 'packageVersion') === objectValue(expectedBuild, 'packageVersion'), projectPackageVersion(objectValue(expectedBuild, 'packageVersion')), projectPackageVersion(objectValue(actualBuild, 'packageVersion'))),
|
|
79
|
-
internalExecutableBuildHash: field(objectValue(actualBuild, 'internalExecutableBuildHash') === objectValue(expectedBuild, 'internalExecutableBuildHash'), projectHash('buildIdentity.internalExecutableBuildHash', objectValue(expectedBuild, 'internalExecutableBuildHash')), projectHash('buildIdentity.internalExecutableBuildHash', objectValue(actualBuild, 'internalExecutableBuildHash'))),
|
|
80
|
-
lifecycleContractHash: field(objectValue(actualBuild, 'lifecycleContractHash') === objectValue(expectedBuild, 'lifecycleContractHash'), projectHash('buildIdentity.lifecycleContractHash', objectValue(expectedBuild, 'lifecycleContractHash')), projectHash('buildIdentity.lifecycleContractHash', objectValue(actualBuild, 'lifecycleContractHash')))
|
|
81
|
-
},
|
|
82
|
-
controller: {
|
|
83
|
-
instanceDigest: field(sameControllerField(actualController, expectedController, 'instanceDigest'), projectPresence(objectValue(expectedController, 'instanceDigest') === undefined ? null : expectedController), projectPresence(objectValue(actualController, 'instanceDigest') === undefined ? null : actualController)),
|
|
84
|
-
controlGeneration: field(sameControllerField(actualController, expectedController, 'controlGeneration'), projectPresence(objectValue(expectedController, 'controlGeneration') === undefined ? null : expectedController), projectPresence(objectValue(actualController, 'controlGeneration') === undefined ? null : actualController))
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function exactKeys(value, keys) {
|
|
90
|
-
return Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
91
|
-
}
|
|
92
|
-
function isRecoveryPhase(value) {
|
|
93
|
-
return value === 'orchestration.prepare'
|
|
94
|
-
|| value === 'artifact.finalize-local'
|
|
95
|
-
|| value === 'task-event.completed';
|
|
96
|
-
}
|
|
97
|
-
function isRecoveryPhaseRecord(value) {
|
|
98
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
99
|
-
return false;
|
|
100
|
-
const phase = value;
|
|
101
|
-
return exactKeys(phase, ['phase', 'requestId', 'state'])
|
|
102
|
-
&& isRecoveryPhase(phase.phase)
|
|
103
|
-
&& typeof phase.requestId === 'string'
|
|
104
|
-
&& /^[a-f0-9-]{16,64}$/u.test(phase.requestId)
|
|
105
|
-
&& (phase.state === 'issued' || phase.state === 'consumed');
|
|
106
|
-
}
|
|
107
|
-
function isCodexControllerBinding(value) {
|
|
108
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
109
|
-
return false;
|
|
110
|
-
const record = value;
|
|
111
|
-
return exactKeys(record, ['instanceDigest', 'controlGeneration'])
|
|
112
|
-
&& typeof record.instanceDigest === 'string'
|
|
113
|
-
&& typeof record.controlGeneration === 'string';
|
|
114
|
-
}
|
|
115
|
-
function isProjection(value) {
|
|
116
|
-
if (!value || typeof value !== 'object')
|
|
117
|
-
return false;
|
|
118
|
-
const record = value;
|
|
119
|
-
if (record.kind === 'absent')
|
|
120
|
-
return exactKeys(record, ['kind']);
|
|
121
|
-
if (record.kind === 'protocol-version') {
|
|
122
|
-
return exactKeys(record, ['kind', 'value']) && isLifecycleProtocolVersion(record.value);
|
|
123
|
-
}
|
|
124
|
-
if (record.kind === 'semver') {
|
|
125
|
-
return exactKeys(record, ['kind', 'value']) && isCanonicalLifecyclePackageVersion(record.value);
|
|
126
|
-
}
|
|
127
|
-
if (record.kind === 'digest-prefix') {
|
|
128
|
-
return exactKeys(record, ['kind', 'value'])
|
|
129
|
-
&& typeof record.value === 'string'
|
|
130
|
-
&& DIGEST_PREFIX_PATTERN.test(record.value);
|
|
131
|
-
}
|
|
132
|
-
if (record.kind === 'presence') {
|
|
133
|
-
return exactKeys(record, ['kind', 'present']) && typeof record.present === 'boolean';
|
|
134
|
-
}
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
function isProvenanceField(value) {
|
|
138
|
-
if (!value || typeof value !== 'object')
|
|
139
|
-
return false;
|
|
140
|
-
const record = value;
|
|
141
|
-
return exactKeys(record, ['matches', 'expected', 'actual'])
|
|
142
|
-
&& typeof record.matches === 'boolean'
|
|
143
|
-
&& isProjection(record.expected)
|
|
144
|
-
&& isProjection(record.actual);
|
|
145
|
-
}
|
|
146
|
-
function isCodexCapabilityProvenanceDetail(value) {
|
|
147
|
-
if (!value || typeof value !== 'object')
|
|
148
|
-
return false;
|
|
149
|
-
const record = value;
|
|
150
|
-
if (!exactKeys(record, ['kind', 'version', 'fields'])
|
|
151
|
-
|| record.kind !== 'codex-capability-provenance-mismatch'
|
|
152
|
-
|| record.version !== 1
|
|
153
|
-
|| !record.fields || typeof record.fields !== 'object')
|
|
154
|
-
return false;
|
|
155
|
-
const fields = record.fields;
|
|
156
|
-
if (!exactKeys(fields, ['taskId', 'hookDefinitionHash', 'buildIdentity', 'controller'])
|
|
157
|
-
|| !isProvenanceField(fields.taskId)
|
|
158
|
-
|| !isProvenanceField(fields.hookDefinitionHash))
|
|
159
|
-
return false;
|
|
160
|
-
const buildIdentity = fields.buildIdentity;
|
|
161
|
-
const controller = fields.controller;
|
|
162
|
-
if (!buildIdentity || typeof buildIdentity !== 'object' || !controller || typeof controller !== 'object')
|
|
163
|
-
return false;
|
|
164
|
-
const build = buildIdentity;
|
|
165
|
-
const binding = controller;
|
|
166
|
-
return exactKeys(build, ['protocolVersion', 'packageVersion', 'internalExecutableBuildHash', 'lifecycleContractHash'])
|
|
167
|
-
&& exactKeys(binding, ['instanceDigest', 'controlGeneration'])
|
|
168
|
-
&& isProvenanceField(build.protocolVersion)
|
|
169
|
-
&& isProvenanceField(build.packageVersion)
|
|
170
|
-
&& isProvenanceField(build.internalExecutableBuildHash)
|
|
171
|
-
&& isProvenanceField(build.lifecycleContractHash)
|
|
172
|
-
&& isProvenanceField(binding.instanceDigest)
|
|
173
|
-
&& isProvenanceField(binding.controlGeneration);
|
|
174
|
-
}
|
|
175
|
-
function sameControllerField(left, right, key) {
|
|
176
|
-
if (left === null || right === null)
|
|
177
|
-
return left === null && right === null;
|
|
178
|
-
return isCodexControllerBinding(left)
|
|
179
|
-
&& isCodexControllerBinding(right)
|
|
180
|
-
&& left[key] === right[key];
|
|
181
|
-
}
|
|
182
|
-
function sameController(left, right) {
|
|
183
|
-
if (left === null || right === null)
|
|
184
|
-
return left === null && right === null;
|
|
185
|
-
return isCodexControllerBinding(left)
|
|
186
|
-
&& isCodexControllerBinding(right)
|
|
187
|
-
&& left.instanceDigest === right.instanceDigest
|
|
188
|
-
&& left.controlGeneration === right.controlGeneration;
|
|
189
|
-
}
|
|
190
|
-
function createCodexCapabilityStore(options = {}) {
|
|
191
|
-
const root = options.root
|
|
192
|
-
?? resolveAgentRuntimeStoreRoot({ store: 'capabilities' });
|
|
193
|
-
const now = options.now ?? Date.now;
|
|
194
|
-
const createReference = options.reference ?? (() => crypto.randomBytes(32).toString('base64url'));
|
|
195
|
-
const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
196
|
-
const tombstoneMs = options.tombstoneMs ?? DEFAULT_TOMBSTONE_MS;
|
|
197
|
-
const maxRecords = options.maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
198
|
-
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
199
|
-
fs.chmodSync(root, 0o700);
|
|
200
|
-
function fileFor(capabilityRef) {
|
|
201
|
-
return path.join(root, `${digest(capabilityRef)}.json`);
|
|
202
|
-
}
|
|
203
|
-
function read(file) {
|
|
204
|
-
const value = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
205
|
-
const keys = Object.keys(value).sort().join(',');
|
|
206
|
-
if (keys !== 'armedAt,attestedAt,buildIdentity,capabilityRefDigest,consumedAt,controller,expiresAt,hookDefinitionHash,recoveryOperationId,recoveryPhases,recoveryState,reservedAt,revision,schemaVersion,sessionId,status,taskId,toolUseId,turnId'
|
|
207
|
-
|| value.schemaVersion !== 2 || !Number.isSafeInteger(value.revision) || value.revision < 1
|
|
208
|
-
|| typeof value.capabilityRefDigest !== 'string' || !/^[a-f0-9]{64}$/u.test(value.capabilityRefDigest)
|
|
209
|
-
|| !['unreserved', 'reserved', 'consumed'].includes(value.recoveryState)
|
|
210
|
-
|| (value.recoveryOperationId !== null && typeof value.recoveryOperationId !== 'string')
|
|
211
|
-
|| (value.reservedAt !== null && !Number.isSafeInteger(value.reservedAt))
|
|
212
|
-
|| !Array.isArray(value.recoveryPhases)
|
|
213
|
-
|| !value.recoveryPhases.every((phase) => isRecoveryPhaseRecord(phase))) {
|
|
214
|
-
throw capabilityError('CODEX_CAPABILITY_STATE_INVALID', 'capability record schema is invalid');
|
|
215
|
-
}
|
|
216
|
-
return value;
|
|
217
|
-
}
|
|
218
|
-
function withWriteLock(operation) {
|
|
219
|
-
const lock = path.join(root, '.write.lock');
|
|
220
|
-
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
221
|
-
let descriptor = null;
|
|
222
|
-
while (descriptor === null) {
|
|
223
|
-
try {
|
|
224
|
-
descriptor = fs.openSync(lock, 'wx', 0o600);
|
|
225
|
-
}
|
|
226
|
-
catch (error) {
|
|
227
|
-
if (error.code !== 'EEXIST')
|
|
228
|
-
throw error;
|
|
229
|
-
try {
|
|
230
|
-
if (Date.now() - fs.statSync(lock).mtimeMs > LOCK_STALE_MS) {
|
|
231
|
-
fs.unlinkSync(lock);
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
catch (lockError) {
|
|
236
|
-
if (lockError.code === 'ENOENT')
|
|
237
|
-
continue;
|
|
238
|
-
throw lockError;
|
|
239
|
-
}
|
|
240
|
-
if (Date.now() >= deadline) {
|
|
241
|
-
throw capabilityError('CODEX_CAPABILITY_STORE_BUSY', 'capability store is busy');
|
|
242
|
-
}
|
|
243
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, LOCK_RETRY_MS);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
try {
|
|
247
|
-
return operation();
|
|
248
|
-
}
|
|
249
|
-
finally {
|
|
250
|
-
fs.closeSync(descriptor);
|
|
251
|
-
fs.unlinkSync(lock);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
function write(file, value, expectedRevision) {
|
|
255
|
-
const temporary = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
256
|
-
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, {
|
|
257
|
-
encoding: 'utf8',
|
|
258
|
-
flag: 'wx',
|
|
259
|
-
mode: 0o600
|
|
260
|
-
});
|
|
261
|
-
try {
|
|
262
|
-
withWriteLock(() => {
|
|
263
|
-
options.beforeCompareAndSwap?.({ path: file, expectedRevision });
|
|
264
|
-
const actualRevision = fs.existsSync(file) ? read(file).revision : 0;
|
|
265
|
-
if (actualRevision !== expectedRevision) {
|
|
266
|
-
throw capabilityError('CODEX_CAPABILITY_REVISION_CONFLICT', `capability revision changed from ${expectedRevision} to ${actualRevision}`);
|
|
267
|
-
}
|
|
268
|
-
fs.renameSync(temporary, file);
|
|
269
|
-
fs.chmodSync(file, 0o600);
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
finally {
|
|
273
|
-
if (fs.existsSync(temporary))
|
|
274
|
-
fs.unlinkSync(temporary);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
function files() {
|
|
278
|
-
return fs.readdirSync(root)
|
|
279
|
-
.filter((name) => /^[a-f0-9]{64}\.json$/u.test(name))
|
|
280
|
-
.map((name) => path.join(root, name));
|
|
281
|
-
}
|
|
282
|
-
function expire(record, file, timestamp) {
|
|
283
|
-
if ((record.status === 'armed' || record.status === 'attested') && timestamp > record.expiresAt) {
|
|
284
|
-
const expired = Object.freeze({
|
|
285
|
-
...record,
|
|
286
|
-
revision: record.revision + 1,
|
|
287
|
-
status: 'expired'
|
|
288
|
-
});
|
|
289
|
-
write(file, expired, record.revision);
|
|
290
|
-
return expired;
|
|
291
|
-
}
|
|
292
|
-
return record;
|
|
293
|
-
}
|
|
294
|
-
function sweep() {
|
|
295
|
-
const timestamp = now();
|
|
296
|
-
const records = files().map((file) => ({ file, record: expire(read(file), file, timestamp) }));
|
|
297
|
-
for (const entry of records) {
|
|
298
|
-
const terminalAt = entry.record.consumedAt ?? entry.record.expiresAt;
|
|
299
|
-
if (['consumed', 'expired'].includes(entry.record.status)
|
|
300
|
-
&& timestamp - terminalAt > tombstoneMs) {
|
|
301
|
-
fs.unlinkSync(entry.file);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
const remaining = files().map((file) => ({ file, record: read(file) }));
|
|
305
|
-
if (remaining.length <= maxRecords)
|
|
306
|
-
return;
|
|
307
|
-
const removable = remaining
|
|
308
|
-
.filter(({ record }) => ['consumed', 'expired'].includes(record.status))
|
|
309
|
-
.sort((left, right) => (left.record.consumedAt ?? left.record.expiresAt)
|
|
310
|
-
- (right.record.consumedAt ?? right.record.expiresAt));
|
|
311
|
-
while (files().length > maxRecords && removable.length > 0) {
|
|
312
|
-
fs.unlinkSync(removable.shift().file);
|
|
313
|
-
}
|
|
314
|
-
if (files().length > maxRecords) {
|
|
315
|
-
throw capabilityError('CODEX_CAPABILITY_CAPACITY_EXCEEDED', 'capability store has too many active records');
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
function arm(input) {
|
|
319
|
-
sweep();
|
|
320
|
-
const capabilityRef = createReference();
|
|
321
|
-
const file = fileFor(capabilityRef);
|
|
322
|
-
if (fs.existsSync(file))
|
|
323
|
-
throw capabilityError('CODEX_CAPABILITY_TOKEN_COLLISION', 'capability token already exists');
|
|
324
|
-
const timestamp = now();
|
|
325
|
-
const record = Object.freeze({
|
|
326
|
-
schemaVersion: 2,
|
|
327
|
-
revision: 1,
|
|
328
|
-
capabilityRefDigest: digest(capabilityRef),
|
|
329
|
-
taskId: input.taskId,
|
|
330
|
-
status: 'armed',
|
|
331
|
-
recoveryOperationId: null,
|
|
332
|
-
recoveryState: 'unreserved',
|
|
333
|
-
reservedAt: null,
|
|
334
|
-
recoveryPhases: Object.freeze([]),
|
|
335
|
-
buildIdentity: input.buildIdentity,
|
|
336
|
-
controller: input.controller ?? null,
|
|
337
|
-
sessionId: null,
|
|
338
|
-
turnId: null,
|
|
339
|
-
toolUseId: null,
|
|
340
|
-
hookDefinitionHash: null,
|
|
341
|
-
armedAt: timestamp,
|
|
342
|
-
expiresAt: timestamp + ttlMs,
|
|
343
|
-
attestedAt: null,
|
|
344
|
-
consumedAt: null
|
|
345
|
-
});
|
|
346
|
-
write(file, record, 0);
|
|
347
|
-
const result = {
|
|
348
|
-
...record,
|
|
349
|
-
capabilityRef,
|
|
350
|
-
path: file,
|
|
351
|
-
marker: `agent-infra-codex-capability:${capabilityRef}`
|
|
352
|
-
};
|
|
353
|
-
return Object.freeze(result);
|
|
354
|
-
}
|
|
355
|
-
function loadActive(capabilityRef, options = {}) {
|
|
356
|
-
const file = fileFor(capabilityRef);
|
|
357
|
-
if (!fs.existsSync(file)) {
|
|
358
|
-
throw capabilityError('CODEX_CAPABILITY_MISSING', 'capability token was not found');
|
|
359
|
-
}
|
|
360
|
-
const record = expire(read(file), file, now());
|
|
361
|
-
if (record.status === 'expired' && !options.allowExpiredReserved) {
|
|
362
|
-
throw capabilityError('CODEX_CAPABILITY_EXPIRED', 'capability token expired');
|
|
363
|
-
}
|
|
364
|
-
return { file, record };
|
|
365
|
-
}
|
|
366
|
-
function attestByReference(input) {
|
|
367
|
-
const { file, record } = loadActive(input.capabilityRef);
|
|
368
|
-
if (record.status !== 'armed') {
|
|
369
|
-
throw capabilityError(record.status === 'consumed' ? 'CODEX_CAPABILITY_REPLAY' : 'CODEX_CAPABILITY_STATE_INVALID', `capability is ${record.status}, expected armed`);
|
|
370
|
-
}
|
|
371
|
-
const identity = verifyLifecycleBuildIdentity(record.buildIdentity, input.buildIdentity);
|
|
372
|
-
if (!identity.ok || !sameController(record.controller, input.controller ?? null)) {
|
|
373
|
-
throw capabilityError('CODEX_CAPABILITY_PROVENANCE_MISMATCH', 'capability build or controller identity does not match');
|
|
374
|
-
}
|
|
375
|
-
if (![input.sessionId, input.turnId, input.toolUseId, input.hookDefinitionHash]
|
|
376
|
-
.every((value) => value.trim().length > 0)) {
|
|
377
|
-
throw capabilityError('CODEX_CAPABILITY_IDENTITY_INVALID', 'hook identity is incomplete');
|
|
378
|
-
}
|
|
379
|
-
const timestamp = now();
|
|
380
|
-
const next = Object.freeze({
|
|
381
|
-
...record,
|
|
382
|
-
revision: record.revision + 1,
|
|
383
|
-
status: 'attested',
|
|
384
|
-
sessionId: input.sessionId,
|
|
385
|
-
turnId: input.turnId,
|
|
386
|
-
toolUseId: input.toolUseId,
|
|
387
|
-
hookDefinitionHash: input.hookDefinitionHash,
|
|
388
|
-
attestedAt: timestamp
|
|
389
|
-
});
|
|
390
|
-
write(file, next, record.revision);
|
|
391
|
-
return next;
|
|
392
|
-
}
|
|
393
|
-
function validateRecord(record, expected, options = {}) {
|
|
394
|
-
if (record.status === 'consumed') {
|
|
395
|
-
throw capabilityError('CODEX_CAPABILITY_REPLAY', 'capability token was already consumed');
|
|
396
|
-
}
|
|
397
|
-
const expiredReserved = options.allowExpiredReserved
|
|
398
|
-
&& record.status === 'expired'
|
|
399
|
-
&& record.recoveryState === 'reserved';
|
|
400
|
-
if (record.status !== 'attested' && !expiredReserved) {
|
|
401
|
-
throw capabilityError('CODEX_CAPABILITY_NOT_ATTESTED', 'capability token has no current-session hook attestation');
|
|
402
|
-
}
|
|
403
|
-
const identity = record.buildIdentity && typeof record.buildIdentity === 'object'
|
|
404
|
-
? verifyLifecycleBuildIdentity(record.buildIdentity, expected.buildIdentity)
|
|
405
|
-
: { ok: false };
|
|
406
|
-
if (!identity.ok
|
|
407
|
-
|| record.taskId !== expected.taskId
|
|
408
|
-
|| record.hookDefinitionHash !== expected.hookDefinitionHash
|
|
409
|
-
|| !sameController(record.controller, expected.controller ?? null)) {
|
|
410
|
-
throw capabilityError('CODEX_CAPABILITY_PROVENANCE_MISMATCH', 'capability consumption identity does not match', provenanceDetail(record, expected));
|
|
411
|
-
}
|
|
412
|
-
return record;
|
|
413
|
-
}
|
|
414
|
-
function validateReference(capabilityRef, expected) {
|
|
415
|
-
sweep();
|
|
416
|
-
const { record } = loadActive(capabilityRef);
|
|
417
|
-
return validateRecord(record, expected);
|
|
418
|
-
}
|
|
419
|
-
function consumeReference(capabilityRef, operationId, expected, options = {}) {
|
|
420
|
-
sweep();
|
|
421
|
-
if (!operationId || /[\r\n]/u.test(operationId)) {
|
|
422
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'recovery operation id is invalid');
|
|
423
|
-
}
|
|
424
|
-
const { file, record } = loadActive(capabilityRef, options);
|
|
425
|
-
if (record.status === 'consumed' && record.recoveryState === 'consumed' && record.recoveryOperationId === operationId) {
|
|
426
|
-
return record;
|
|
427
|
-
}
|
|
428
|
-
validateRecord(record, expected, options);
|
|
429
|
-
if (record.recoveryState === 'consumed') {
|
|
430
|
-
if (record.recoveryOperationId === operationId)
|
|
431
|
-
return record;
|
|
432
|
-
throw capabilityError('CODEX_CAPABILITY_REPLAY', 'capability recovery operation was already consumed');
|
|
433
|
-
}
|
|
434
|
-
if (record.recoveryState !== 'reserved' || record.recoveryOperationId !== operationId) {
|
|
435
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'capability must be reserved by this operation before consumption');
|
|
436
|
-
}
|
|
437
|
-
const next = Object.freeze({
|
|
438
|
-
...record,
|
|
439
|
-
revision: record.revision + 1,
|
|
440
|
-
status: 'consumed',
|
|
441
|
-
recoveryState: 'consumed',
|
|
442
|
-
consumedAt: now()
|
|
443
|
-
});
|
|
444
|
-
write(file, next, record.revision);
|
|
445
|
-
return next;
|
|
446
|
-
}
|
|
447
|
-
function reserveRecoveryPhase(capabilityRef, operationId, phase, requestId, expected) {
|
|
448
|
-
if (!operationId || /[\r\n]/u.test(operationId) || !isRecoveryPhase(phase)
|
|
449
|
-
|| !/^[a-f0-9-]{16,64}$/u.test(requestId)) {
|
|
450
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'recovery phase identity is invalid');
|
|
451
|
-
}
|
|
452
|
-
sweep();
|
|
453
|
-
const { file, record } = loadActive(capabilityRef);
|
|
454
|
-
validateRecord(record, expected);
|
|
455
|
-
if (record.recoveryState !== 'reserved' || record.recoveryOperationId !== operationId) {
|
|
456
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'capability must be reserved by this operation before phase reservation');
|
|
457
|
-
}
|
|
458
|
-
if (record.recoveryPhases.some((entry) => entry.phase === phase)) {
|
|
459
|
-
throw capabilityError('CODEX_CAPABILITY_PHASE_REPLAY', 'recovery phase was already reserved');
|
|
460
|
-
}
|
|
461
|
-
const next = Object.freeze({
|
|
462
|
-
...record,
|
|
463
|
-
revision: record.revision + 1,
|
|
464
|
-
recoveryPhases: Object.freeze([
|
|
465
|
-
...record.recoveryPhases,
|
|
466
|
-
Object.freeze({ phase, requestId, state: 'issued' })
|
|
467
|
-
])
|
|
468
|
-
});
|
|
469
|
-
write(file, next, record.revision);
|
|
470
|
-
return next;
|
|
471
|
-
}
|
|
472
|
-
function consumeRecoveryPhase(capabilityRef, operationId, phase, requestId, expected, options = {}) {
|
|
473
|
-
if (!operationId || /[\r\n]/u.test(operationId) || !isRecoveryPhase(phase)
|
|
474
|
-
|| !/^[a-f0-9-]{16,64}$/u.test(requestId)) {
|
|
475
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'recovery phase identity is invalid');
|
|
476
|
-
}
|
|
477
|
-
sweep();
|
|
478
|
-
const { file, record } = loadActive(capabilityRef, options);
|
|
479
|
-
validateRecord(record, expected, options);
|
|
480
|
-
if (record.recoveryState !== 'reserved' && record.recoveryState !== 'consumed') {
|
|
481
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'capability has no reserved recovery operation');
|
|
482
|
-
}
|
|
483
|
-
if (record.recoveryOperationId !== operationId) {
|
|
484
|
-
throw capabilityError('CODEX_CAPABILITY_REPLAY', 'capability recovery operation does not match');
|
|
485
|
-
}
|
|
486
|
-
const current = record.recoveryPhases.find((entry) => entry.phase === phase);
|
|
487
|
-
if (!current || current.requestId !== requestId) {
|
|
488
|
-
throw capabilityError('CODEX_CAPABILITY_PHASE_REPLAY', 'recovery phase identity does not match');
|
|
489
|
-
}
|
|
490
|
-
if (current.state === 'consumed')
|
|
491
|
-
return record;
|
|
492
|
-
const next = Object.freeze({
|
|
493
|
-
...record,
|
|
494
|
-
revision: record.revision + 1,
|
|
495
|
-
recoveryPhases: Object.freeze(record.recoveryPhases.map((entry) => entry.phase === phase ? Object.freeze({ ...entry, state: 'consumed' }) : entry))
|
|
496
|
-
});
|
|
497
|
-
write(file, next, record.revision);
|
|
498
|
-
return next;
|
|
499
|
-
}
|
|
500
|
-
function reserveReference(capabilityRef, operationId, expected) {
|
|
501
|
-
if (!operationId || /[\r\n]/u.test(operationId)) {
|
|
502
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'recovery operation id is invalid');
|
|
503
|
-
}
|
|
504
|
-
sweep();
|
|
505
|
-
const { file, record } = loadActive(capabilityRef);
|
|
506
|
-
validateRecord(record, expected);
|
|
507
|
-
if (record.recoveryState === 'reserved') {
|
|
508
|
-
if (record.recoveryOperationId === operationId)
|
|
509
|
-
return record;
|
|
510
|
-
throw capabilityError('CODEX_CAPABILITY_BUSY', 'capability is reserved by another recovery operation');
|
|
511
|
-
}
|
|
512
|
-
if (record.recoveryState === 'consumed') {
|
|
513
|
-
if (record.recoveryOperationId === operationId)
|
|
514
|
-
return record;
|
|
515
|
-
throw capabilityError('CODEX_CAPABILITY_REPLAY', 'capability recovery operation was already consumed');
|
|
516
|
-
}
|
|
517
|
-
const next = Object.freeze({
|
|
518
|
-
...record,
|
|
519
|
-
revision: record.revision + 1,
|
|
520
|
-
recoveryOperationId: operationId,
|
|
521
|
-
recoveryState: 'reserved',
|
|
522
|
-
reservedAt: now()
|
|
523
|
-
});
|
|
524
|
-
write(file, next, record.revision);
|
|
525
|
-
return next;
|
|
526
|
-
}
|
|
527
|
-
function inspectReference(capabilityRef) {
|
|
528
|
-
return loadActive(capabilityRef).record;
|
|
529
|
-
}
|
|
530
|
-
function findByRecoveryOperation(operationId) {
|
|
531
|
-
if (!operationId || /[\r\n]/u.test(operationId)) {
|
|
532
|
-
throw capabilityError('CODEX_CAPABILITY_RECOVERY_INVALID', 'recovery operation id is invalid');
|
|
533
|
-
}
|
|
534
|
-
return Object.freeze(files()
|
|
535
|
-
.map((file) => read(file))
|
|
536
|
-
.filter((record) => record.recoveryOperationId === operationId));
|
|
537
|
-
}
|
|
538
|
-
return Object.freeze({
|
|
539
|
-
arm, attestByReference, reserveReference, reserveRecoveryPhase, consumeRecoveryPhase,
|
|
540
|
-
consumeReference, inspectReference,
|
|
541
|
-
findByRecoveryOperation, validateReference, sweep
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
export { createCodexCapabilityStore };
|
|
545
|
-
export { isCodexCapabilityProvenanceDetail };
|
|
546
|
-
//# sourceMappingURL=capability-store.js.map
|