@agent-deck/backend 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/agent-run/adapter.d.ts +134 -0
- package/dist/agent-run/adapter.d.ts.map +1 -0
- package/dist/agent-run/adapter.js +70 -0
- package/dist/agent-run/adapter.js.map +1 -0
- package/dist/agent-run/claude-probe.d.ts +30 -0
- package/dist/agent-run/claude-probe.d.ts.map +1 -0
- package/dist/agent-run/claude-probe.js +85 -0
- package/dist/agent-run/claude-probe.js.map +1 -0
- package/dist/agent-run/claude.d.ts +49 -0
- package/dist/agent-run/claude.d.ts.map +1 -0
- package/dist/agent-run/claude.js +104 -0
- package/dist/agent-run/claude.js.map +1 -0
- package/dist/agent-run/codex.d.ts +86 -0
- package/dist/agent-run/codex.d.ts.map +1 -0
- package/dist/agent-run/codex.js +377 -0
- package/dist/agent-run/codex.js.map +1 -0
- package/dist/agent-run/cursor.d.ts +80 -0
- package/dist/agent-run/cursor.d.ts.map +1 -0
- package/dist/agent-run/cursor.js +462 -0
- package/dist/agent-run/cursor.js.map +1 -0
- package/dist/agent-run/index.d.ts +15 -0
- package/dist/agent-run/index.d.ts.map +1 -0
- package/dist/agent-run/index.js +31 -0
- package/dist/agent-run/index.js.map +1 -0
- package/dist/agent-run/muse.d.ts +134 -0
- package/dist/agent-run/muse.d.ts.map +1 -0
- package/dist/agent-run/muse.js +512 -0
- package/dist/agent-run/muse.js.map +1 -0
- package/dist/agent-run/provenance.d.ts +28 -0
- package/dist/agent-run/provenance.d.ts.map +1 -0
- package/dist/agent-run/provenance.js +118 -0
- package/dist/agent-run/provenance.js.map +1 -0
- package/dist/agent-run/records.d.ts +182 -0
- package/dist/agent-run/records.d.ts.map +1 -0
- package/dist/agent-run/records.js +359 -0
- package/dist/agent-run/records.js.map +1 -0
- package/dist/agent-run/service.d.ts +206 -0
- package/dist/agent-run/service.d.ts.map +1 -0
- package/dist/agent-run/service.js +503 -0
- package/dist/agent-run/service.js.map +1 -0
- package/dist/agent-run-api/auth.d.ts +22 -0
- package/dist/agent-run-api/auth.d.ts.map +1 -0
- package/dist/agent-run-api/auth.js +54 -0
- package/dist/agent-run-api/auth.js.map +1 -0
- package/dist/agent-run-api/finalization-store.d.ts +62 -0
- package/dist/agent-run-api/finalization-store.d.ts.map +1 -0
- package/dist/agent-run-api/finalization-store.js +126 -0
- package/dist/agent-run-api/finalization-store.js.map +1 -0
- package/dist/agent-run-api/git-finalize.d.ts +98 -0
- package/dist/agent-run-api/git-finalize.d.ts.map +1 -0
- package/dist/agent-run-api/git-finalize.js +639 -0
- package/dist/agent-run-api/git-finalize.js.map +1 -0
- package/dist/agent-run-api/host.d.ts +10 -0
- package/dist/agent-run-api/host.d.ts.map +1 -0
- package/dist/agent-run-api/host.js +27 -0
- package/dist/agent-run-api/host.js.map +1 -0
- package/dist/agent-run-api/run-index.d.ts +40 -0
- package/dist/agent-run-api/run-index.d.ts.map +1 -0
- package/dist/agent-run-api/run-index.js +69 -0
- package/dist/agent-run-api/run-index.js.map +1 -0
- package/dist/audit/store.d.ts +22 -3
- package/dist/audit/store.d.ts.map +1 -1
- package/dist/audit/store.js +67 -6
- package/dist/audit/store.js.map +1 -1
- package/dist/auth/client-grants.d.ts +28 -3
- package/dist/auth/client-grants.d.ts.map +1 -1
- package/dist/auth/client-grants.js +40 -10
- package/dist/auth/client-grants.js.map +1 -1
- package/dist/auth/hosted-mode.d.ts +2 -0
- package/dist/auth/hosted-mode.d.ts.map +1 -1
- package/dist/auth/hosted-mode.js +27 -1
- package/dist/auth/hosted-mode.js.map +1 -1
- package/dist/routes/agent-grants.d.ts.map +1 -1
- package/dist/routes/agent-grants.js +63 -1
- package/dist/routes/agent-grants.js.map +1 -1
- package/dist/routes/agent-runs.d.ts +40 -0
- package/dist/routes/agent-runs.d.ts.map +1 -0
- package/dist/routes/agent-runs.js +755 -0
- package/dist/routes/agent-runs.js.map +1 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +6 -0
- package/dist/server/index.js.map +1 -1
- package/dist/trusted-session/auth.d.ts +12 -1
- package/dist/trusted-session/auth.d.ts.map +1 -1
- package/dist/trusted-session/auth.js +1 -0
- package/dist/trusted-session/auth.js.map +1 -1
- package/dist/trusted-session/policy-hook.d.ts.map +1 -1
- package/dist/trusted-session/policy-hook.js +25 -9
- package/dist/trusted-session/policy-hook.js.map +1 -1
- package/dist/trusted-session/route-policy-registry.d.ts.map +1 -1
- package/dist/trusted-session/route-policy-registry.js +11 -0
- package/dist/trusted-session/route-policy-registry.js.map +1 -1
- package/package.json +13 -2
- package/static-ui/assets/{index-ynIby2JK.js → index-eHjUHhT-.js} +58 -58
- package/static-ui/index.html +1 -1
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerAgentRunRoutes = registerAgentRunRoutes;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const shared_1 = require("@agent-deck/shared");
|
|
6
|
+
const auth_1 = require("../agent-run-api/auth");
|
|
7
|
+
const finalization_store_1 = require("../agent-run-api/finalization-store");
|
|
8
|
+
const git_finalize_1 = require("../agent-run-api/git-finalize");
|
|
9
|
+
const run_index_1 = require("../agent-run-api/run-index");
|
|
10
|
+
const store_1 = require("../audit/store");
|
|
11
|
+
function rejectionReply(code, request) {
|
|
12
|
+
switch (code) {
|
|
13
|
+
case 'prompt_empty':
|
|
14
|
+
return { status: 400, code: 'INVALID_REQUEST', message: 'prompt must not be empty', reason: 'invalid_request' };
|
|
15
|
+
case 'cwd_not_found':
|
|
16
|
+
case 'cwd_not_directory':
|
|
17
|
+
return {
|
|
18
|
+
status: 422,
|
|
19
|
+
code: 'WORKSPACE_INVALID',
|
|
20
|
+
message: 'cwd is not an existing directory on the Agent Deck host',
|
|
21
|
+
reason: 'workspace_invalid',
|
|
22
|
+
};
|
|
23
|
+
case 'deck_not_found':
|
|
24
|
+
return { status: 404, code: 'DECK_NOT_FOUND', message: 'Deck not found', reason: 'deck_not_found' };
|
|
25
|
+
case 'deck_unassigned':
|
|
26
|
+
case 'deck_mismatch':
|
|
27
|
+
return {
|
|
28
|
+
status: 422,
|
|
29
|
+
code: 'WORKSPACE_MISMATCH',
|
|
30
|
+
message: 'cwd is not assigned to deckId on the Agent Deck host',
|
|
31
|
+
reason: 'workspace_mismatch',
|
|
32
|
+
};
|
|
33
|
+
case 'client_unavailable':
|
|
34
|
+
return {
|
|
35
|
+
status: 503,
|
|
36
|
+
code: 'CLIENT_UNAVAILABLE',
|
|
37
|
+
message: `${request.client} is not installed or not signed in on the Agent Deck host`,
|
|
38
|
+
reason: 'client_unavailable',
|
|
39
|
+
};
|
|
40
|
+
case 'profile_unsupported':
|
|
41
|
+
return {
|
|
42
|
+
status: 422,
|
|
43
|
+
code: 'PROFILE_UNSUPPORTED',
|
|
44
|
+
message: `${request.client} cannot enforce tools "${request.tools}"${request.resumeSessionId ? ' on a resumed session' : ''}`,
|
|
45
|
+
reason: 'profile_unsupported',
|
|
46
|
+
};
|
|
47
|
+
case 'resume_unverified':
|
|
48
|
+
return {
|
|
49
|
+
status: 422,
|
|
50
|
+
code: 'RESUME_UNVERIFIED',
|
|
51
|
+
message: `resumeSessionId was not started by a matching ${request.client} run on this deck, cwd, and tools profile`,
|
|
52
|
+
reason: 'resume_unverified',
|
|
53
|
+
};
|
|
54
|
+
case 'finalization_invalid':
|
|
55
|
+
return {
|
|
56
|
+
status: 422,
|
|
57
|
+
code: 'FINALIZATION_INVALID',
|
|
58
|
+
message: 'prepareFinalization requires a Git worktree cwd with a commit on a local branch or detached HEAD',
|
|
59
|
+
reason: 'finalization_invalid',
|
|
60
|
+
};
|
|
61
|
+
case 'index_dirty':
|
|
62
|
+
return {
|
|
63
|
+
status: 409,
|
|
64
|
+
code: 'INDEX_DIRTY',
|
|
65
|
+
message: 'prepareFinalization requires a clean Git index (nothing staged or unmerged)',
|
|
66
|
+
reason: 'index_dirty',
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Stable HTTP mapping for every finalization failure (NOT-486). */
|
|
71
|
+
const FINALIZATION_FAILURES = {
|
|
72
|
+
finalization_invalid: { status: 422, code: 'FINALIZATION_INVALID' },
|
|
73
|
+
index_dirty: { status: 409, code: 'INDEX_DIRTY' },
|
|
74
|
+
base_mismatch: { status: 409, code: 'BASE_MISMATCH' },
|
|
75
|
+
baseline_mismatch: { status: 409, code: 'BASELINE_MISMATCH' },
|
|
76
|
+
path_invalid: { status: 422, code: 'PATH_INVALID' },
|
|
77
|
+
path_unexpected: { status: 409, code: 'PATH_UNEXPECTED' },
|
|
78
|
+
digest_mismatch: { status: 409, code: 'DIGEST_MISMATCH' },
|
|
79
|
+
git_lock_conflict: { status: 409, code: 'GIT_LOCK_CONFLICT' },
|
|
80
|
+
ref_race: { status: 409, code: 'REF_RACE' },
|
|
81
|
+
finalization_canceled: { status: 409, code: 'FINALIZATION_CANCELED' },
|
|
82
|
+
finalization_failed: { status: 500, code: 'FINALIZATION_FAILED' },
|
|
83
|
+
};
|
|
84
|
+
function finalizationReply(error) {
|
|
85
|
+
const mapped = FINALIZATION_FAILURES[error.code];
|
|
86
|
+
return { status: mapped.status, code: mapped.code, message: error.message, reason: error.code };
|
|
87
|
+
}
|
|
88
|
+
/** Same normalized finalize request (manifest order-independent), bound to one run. */
|
|
89
|
+
function finalizeRequestHash(runId, request) {
|
|
90
|
+
const normalized = {
|
|
91
|
+
runId,
|
|
92
|
+
baselineDigest: request.baselineDigest,
|
|
93
|
+
expectedBaseCommit: request.expectedBaseCommit,
|
|
94
|
+
reviewId: request.reviewId,
|
|
95
|
+
commitMessage: request.commitMessage,
|
|
96
|
+
manifestDigest: (0, git_finalize_1.manifestDigestOf)(request.files),
|
|
97
|
+
};
|
|
98
|
+
return (0, node_crypto_1.createHash)('sha256').update(JSON.stringify(normalized), 'utf8').digest('hex');
|
|
99
|
+
}
|
|
100
|
+
/** Same normalized request (after defaults), independent of key order. */
|
|
101
|
+
function requestHash(request) {
|
|
102
|
+
const normalized = {
|
|
103
|
+
client: request.client,
|
|
104
|
+
cwd: request.cwd,
|
|
105
|
+
deckId: request.deckId,
|
|
106
|
+
prompt: request.prompt,
|
|
107
|
+
resumeSessionId: request.resumeSessionId ?? null,
|
|
108
|
+
tools: request.tools,
|
|
109
|
+
prepareFinalization: request.prepareFinalization,
|
|
110
|
+
};
|
|
111
|
+
return (0, node_crypto_1.createHash)('sha256').update(JSON.stringify(normalized), 'utf8').digest('hex');
|
|
112
|
+
}
|
|
113
|
+
function callerOf(request) {
|
|
114
|
+
const principal = request.requestPrincipal;
|
|
115
|
+
if (principal?.kind === 'dashboard') {
|
|
116
|
+
return { kind: 'owner' };
|
|
117
|
+
}
|
|
118
|
+
return { kind: 'grant', grant: grantOf(request) };
|
|
119
|
+
}
|
|
120
|
+
function actorOf(caller) {
|
|
121
|
+
return caller.kind === 'owner' ? 'owner' : caller.grant.grantId;
|
|
122
|
+
}
|
|
123
|
+
function grantOf(request) {
|
|
124
|
+
const principal = request.requestPrincipal;
|
|
125
|
+
if (principal?.kind !== 'grant') {
|
|
126
|
+
throw new Error('Internal: agent-run route reached without a run grant');
|
|
127
|
+
}
|
|
128
|
+
return principal.grant;
|
|
129
|
+
}
|
|
130
|
+
async function registerAgentRunRoutes(fastify, options) {
|
|
131
|
+
const service = options.service;
|
|
132
|
+
const index = options.index ?? new run_index_1.AgentRunIndex(fastify.db.getSqliteDatabase());
|
|
133
|
+
const finalizer = options.finalizer ?? (0, git_finalize_1.createGitFinalizer)();
|
|
134
|
+
const finalizations = options.finalizations ?? new finalization_store_1.FinalizationStore(fastify.db.getSqliteDatabase());
|
|
135
|
+
/** In-process launches keyed by grant + idempotency key; concurrent repeats await the first. */
|
|
136
|
+
const inflight = new Map();
|
|
137
|
+
/** In-process finalizations by run id; identical repeats await, cancel aborts (NOT-486). */
|
|
138
|
+
const finalizing = new Map();
|
|
139
|
+
const audit = (actor, event, targetId, outcome, reasonCode, detail) => {
|
|
140
|
+
fastify.auditStore?.append({ actor, event, targetId, outcome, reasonCode, ...(detail ? { detail } : {}) });
|
|
141
|
+
};
|
|
142
|
+
const sendError = (reply, error) => reply.status(error.status).send((0, shared_1.agentRunError)(error.code, error.message));
|
|
143
|
+
/** Truthful public state: `starting` until the first event; `died` once the process is gone. */
|
|
144
|
+
const publicState = (runId) => {
|
|
145
|
+
const status = service.status(runId);
|
|
146
|
+
if (!status.found) {
|
|
147
|
+
// Indexed but never recorded: the launch did not survive (no process to wait on).
|
|
148
|
+
return { state: 'died', sessionId: null, startedAt: null, lastEventAt: null };
|
|
149
|
+
}
|
|
150
|
+
const { info } = status;
|
|
151
|
+
const state = info.state === 'running' ? (info.lastLogTs === null ? 'starting' : 'running') : info.state;
|
|
152
|
+
return { state, sessionId: info.sessionId, startedAt: info.startedAt, lastEventAt: info.lastLogTs };
|
|
153
|
+
};
|
|
154
|
+
const readableRun = (request, reply) => {
|
|
155
|
+
const caller = callerOf(request);
|
|
156
|
+
const runId = request.params.runId;
|
|
157
|
+
const entry = shared_1.AgentRunIdSchema.safeParse(runId).success ? index.get(runId) : null;
|
|
158
|
+
if (!entry) {
|
|
159
|
+
void reply.status(404).send((0, shared_1.agentRunError)('RUN_NOT_FOUND', 'Run not found'));
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
if (caller.kind === 'owner') {
|
|
163
|
+
return entry;
|
|
164
|
+
}
|
|
165
|
+
const { grant } = caller;
|
|
166
|
+
const outOfScope = (message) => {
|
|
167
|
+
audit(grant.grantId, 'agent_run.denied', entry.runId, 'denied', 'resource_out_of_scope');
|
|
168
|
+
void reply
|
|
169
|
+
.status((0, shared_1.httpStatusForTrustedError)('RESOURCE_OUT_OF_SCOPE'))
|
|
170
|
+
.send((0, shared_1.trustedSessionError)('RESOURCE_OUT_OF_SCOPE', message));
|
|
171
|
+
return null;
|
|
172
|
+
};
|
|
173
|
+
if (!grant.allowedDecks.includes(entry.deckId)) {
|
|
174
|
+
return outOfScope("Run deck is outside this grant's allowed decks");
|
|
175
|
+
}
|
|
176
|
+
if (entry.grantId !== grant.grantId) {
|
|
177
|
+
return outOfScope('Run was started by another grant');
|
|
178
|
+
}
|
|
179
|
+
return entry;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* NOT-486 gate shared by `/changes` and `/finalize`: the creating grant on
|
|
183
|
+
* an allowed deck (via {@link readableRun}), a run prepared before launch,
|
|
184
|
+
* and a terminal state. Denials are audited under `event`.
|
|
185
|
+
*/
|
|
186
|
+
const finalizableRun = (request, reply, event) => {
|
|
187
|
+
const entry = readableRun(request, reply);
|
|
188
|
+
if (!entry) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const actor = actorOf(callerOf(request));
|
|
192
|
+
const stored = finalizations.getBaseline(entry.runId);
|
|
193
|
+
if (!stored) {
|
|
194
|
+
audit(actor, event, entry.runId, 'denied', 'finalization_not_prepared');
|
|
195
|
+
void sendError(reply, {
|
|
196
|
+
status: 409,
|
|
197
|
+
code: 'FINALIZATION_NOT_PREPARED',
|
|
198
|
+
message: 'Run was not started with prepareFinalization',
|
|
199
|
+
reason: 'finalization_not_prepared',
|
|
200
|
+
});
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
const { state } = publicState(entry.runId);
|
|
204
|
+
if (!shared_1.AGENT_RUN_TERMINAL_STATES.includes(state)) {
|
|
205
|
+
audit(actor, event, entry.runId, 'denied', 'run_not_terminal', { repositoryId: (0, git_finalize_1.repositoryIdOf)(stored.baseline) });
|
|
206
|
+
void sendError(reply, {
|
|
207
|
+
status: 409,
|
|
208
|
+
code: 'RUN_NOT_TERMINAL',
|
|
209
|
+
message: 'Run has not reached a terminal state',
|
|
210
|
+
reason: 'run_not_terminal',
|
|
211
|
+
});
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
return { entry, stored, actor };
|
|
215
|
+
};
|
|
216
|
+
const finalizationStatus = (runId) => {
|
|
217
|
+
const stored = finalizations.getBaseline(runId);
|
|
218
|
+
if (!stored) {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
const record = finalizations.get(runId);
|
|
222
|
+
return {
|
|
223
|
+
baselineRef: stored.baselineRef,
|
|
224
|
+
baselineDigest: stored.baseline.digest,
|
|
225
|
+
state: record?.state ?? 'prepared',
|
|
226
|
+
commit: record?.state === 'finalized' ? record.result?.commit ?? null : null,
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
const statusBody = (entry) => {
|
|
230
|
+
const current = publicState(entry.runId);
|
|
231
|
+
return shared_1.AgentRunStatusSchema.parse({
|
|
232
|
+
runId: entry.runId,
|
|
233
|
+
deckId: entry.deckId,
|
|
234
|
+
client: entry.client,
|
|
235
|
+
tools: entry.tools,
|
|
236
|
+
state: current.state,
|
|
237
|
+
sessionId: current.sessionId,
|
|
238
|
+
startedAt: current.startedAt ?? entry.createdAt,
|
|
239
|
+
lastEventAt: current.lastEventAt,
|
|
240
|
+
finalization: finalizationStatus(entry.runId),
|
|
241
|
+
});
|
|
242
|
+
};
|
|
243
|
+
const watchCompletion = (grantId, launched) => {
|
|
244
|
+
void launched.completion.then((finished) => {
|
|
245
|
+
// Cancellation audits itself; an abandoned run reconciles at the next start.
|
|
246
|
+
if (finished.state === 'canceled' || finished.state === 'died') {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
audit(grantId, 'agent_run.finished', finished.runId, finished.state === 'ended' ? 'succeeded' : 'failed', finished.state === 'ended' ? null : 'run_error');
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
const create = async (grant, idempotencyKey, body) => {
|
|
253
|
+
const hash = requestHash(body);
|
|
254
|
+
const inflightKey = `${grant.grantId}\u0000${idempotencyKey}`;
|
|
255
|
+
const { runId } = service.allocate();
|
|
256
|
+
const claim = index.reserve({
|
|
257
|
+
runId,
|
|
258
|
+
grantId: grant.grantId,
|
|
259
|
+
idempotencyKey,
|
|
260
|
+
requestHash: hash,
|
|
261
|
+
deckId: body.deckId,
|
|
262
|
+
client: body.client,
|
|
263
|
+
tools: body.tools,
|
|
264
|
+
});
|
|
265
|
+
if (!claim.reserved) {
|
|
266
|
+
const existing = claim.existing;
|
|
267
|
+
if (existing.requestHash !== hash) {
|
|
268
|
+
return {
|
|
269
|
+
ok: false,
|
|
270
|
+
error: {
|
|
271
|
+
status: 409,
|
|
272
|
+
code: 'IDEMPOTENCY_CONFLICT',
|
|
273
|
+
message: 'Idempotency-Key was already used with a different request',
|
|
274
|
+
reason: 'idempotency_conflict',
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
const pending = inflight.get(inflightKey);
|
|
279
|
+
if (pending) {
|
|
280
|
+
return pending;
|
|
281
|
+
}
|
|
282
|
+
return { ok: true, status: 202, body: { runId: existing.runId, state: publicState(existing.runId).state } };
|
|
283
|
+
}
|
|
284
|
+
// NOT-486: the baseline is captured after every launch gate and before the adapter starts.
|
|
285
|
+
let prepared = null;
|
|
286
|
+
const beforeStart = body.prepareFinalization
|
|
287
|
+
? async () => {
|
|
288
|
+
try {
|
|
289
|
+
prepared = finalizations.saveBaseline(runId, await finalizer.captureBaseline(body.cwd));
|
|
290
|
+
return { ok: true };
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (!(error instanceof git_finalize_1.FinalizationError)) {
|
|
294
|
+
throw error;
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
ok: false,
|
|
298
|
+
code: error.code === 'index_dirty' ? 'index_dirty' : 'finalization_invalid',
|
|
299
|
+
message: error.message,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
: undefined;
|
|
304
|
+
const launch = (async () => {
|
|
305
|
+
const outcome = await service.launch({
|
|
306
|
+
deckRef: body.deckId,
|
|
307
|
+
client: body.client,
|
|
308
|
+
cwd: body.cwd,
|
|
309
|
+
prompt: body.prompt,
|
|
310
|
+
tools: body.tools,
|
|
311
|
+
...(body.resumeSessionId ? { resumeId: body.resumeSessionId } : {}),
|
|
312
|
+
}, { runId, ownProcessGroup: true, ...(beforeStart ? { beforeStart } : {}) });
|
|
313
|
+
if (outcome.kind === 'rejected') {
|
|
314
|
+
// Failed closed before model work: free the key so a corrected retry may reuse it.
|
|
315
|
+
finalizations.deleteBaseline(runId);
|
|
316
|
+
index.release(runId);
|
|
317
|
+
return { ok: false, error: rejectionReply(outcome.code, body) };
|
|
318
|
+
}
|
|
319
|
+
audit(grant.grantId, 'agent_run.created', runId, 'succeeded', null);
|
|
320
|
+
const baseline = prepared?.baseline;
|
|
321
|
+
if (baseline) {
|
|
322
|
+
audit(grant.grantId, 'agent_run.finalization_prepared', runId, 'succeeded', null, {
|
|
323
|
+
repositoryId: (0, git_finalize_1.repositoryIdOf)(baseline),
|
|
324
|
+
baselineDigest: baseline.digest,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
watchCompletion(grant.grantId, outcome);
|
|
328
|
+
return { ok: true, status: 202, body: { runId, state: publicState(runId).state } };
|
|
329
|
+
})().catch((error) => {
|
|
330
|
+
finalizations.deleteBaseline(runId);
|
|
331
|
+
index.release(runId);
|
|
332
|
+
throw error;
|
|
333
|
+
});
|
|
334
|
+
inflight.set(inflightKey, launch);
|
|
335
|
+
try {
|
|
336
|
+
return await launch;
|
|
337
|
+
}
|
|
338
|
+
finally {
|
|
339
|
+
inflight.delete(inflightKey);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
// Restart recovery: re-derive every `running` record this process does not
|
|
343
|
+
// own from its owner process and canonical log before serving reads.
|
|
344
|
+
const reconcileAudit = (row) => {
|
|
345
|
+
const entry = index.get(row.runId);
|
|
346
|
+
if (!entry) {
|
|
347
|
+
return; // A CLI run: reconciled on disk, not an API audit subject.
|
|
348
|
+
}
|
|
349
|
+
const reason = {
|
|
350
|
+
ended: null,
|
|
351
|
+
error: 'run_error',
|
|
352
|
+
died: 'run_died',
|
|
353
|
+
canceled: 'run_canceled',
|
|
354
|
+
};
|
|
355
|
+
audit(entry.grantId, 'agent_run.reconciled', row.runId, row.state === 'ended' ? 'succeeded' : 'failed', reason[row.state]);
|
|
356
|
+
};
|
|
357
|
+
for (const row of service.reconcile()) {
|
|
358
|
+
reconcileAudit(row);
|
|
359
|
+
}
|
|
360
|
+
// NOT-486: a finalization interrupted by a crash either moved the ref to
|
|
361
|
+
// its recorded commit (finalized) or did not (released, staging removed).
|
|
362
|
+
for (const record of finalizations.listInProgress()) {
|
|
363
|
+
const stored = finalizations.getBaseline(record.runId);
|
|
364
|
+
const recovered = stored
|
|
365
|
+
? await finalizer.recover(stored.baseline, record.runId, {
|
|
366
|
+
commit: record.pendingCommit,
|
|
367
|
+
lockIno: record.lockIno,
|
|
368
|
+
})
|
|
369
|
+
: false;
|
|
370
|
+
if (recovered && record.result) {
|
|
371
|
+
finalizations.complete(record.runId, record.result);
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
finalizations.release(record.runId);
|
|
375
|
+
}
|
|
376
|
+
audit(record.grantId, 'agent_run.finalized', record.runId, recovered ? 'succeeded' : 'failed', recovered ? null : 'finalization_failed', stored
|
|
377
|
+
? {
|
|
378
|
+
repositoryId: (0, git_finalize_1.repositoryIdOf)(stored.baseline),
|
|
379
|
+
...(recovered && record.result
|
|
380
|
+
? {
|
|
381
|
+
commit: record.result.commit,
|
|
382
|
+
tree: record.result.tree,
|
|
383
|
+
manifestDigest: record.result.manifestDigest,
|
|
384
|
+
fileCount: record.result.fileCount,
|
|
385
|
+
}
|
|
386
|
+
: {}),
|
|
387
|
+
}
|
|
388
|
+
: undefined);
|
|
389
|
+
}
|
|
390
|
+
// A run cannot outlive the process streaming it: stop owned groups on close.
|
|
391
|
+
fastify.addHook('onClose', async () => {
|
|
392
|
+
service.shutdown();
|
|
393
|
+
});
|
|
394
|
+
await fastify.register(async (scope) => {
|
|
395
|
+
// Body-limit and parse failures happen after auth (onRequest) but before
|
|
396
|
+
// the handler; map them to the run error contract and audit the denial.
|
|
397
|
+
scope.setErrorHandler((error, request, reply) => {
|
|
398
|
+
const tooLarge = error.statusCode === 413;
|
|
399
|
+
const parseError = error.statusCode === 400 || error.statusCode === 415;
|
|
400
|
+
if (!tooLarge && !parseError) {
|
|
401
|
+
return reply.send(error);
|
|
402
|
+
}
|
|
403
|
+
const principal = request.requestPrincipal;
|
|
404
|
+
if (principal?.kind === 'grant') {
|
|
405
|
+
audit(principal.grant.grantId, 'agent_run.denied', (0, auth_1.auditRunTarget)(request.url.split('?')[0]), 'denied', tooLarge ? 'request_too_large' : 'invalid_request');
|
|
406
|
+
}
|
|
407
|
+
return tooLarge
|
|
408
|
+
? reply.status(413).send((0, shared_1.agentRunError)('REQUEST_TOO_LARGE', 'Request body is too large'))
|
|
409
|
+
: reply.status(400).send((0, shared_1.agentRunError)('INVALID_REQUEST', 'Request body must be JSON'));
|
|
410
|
+
});
|
|
411
|
+
scope.post('/agent-runs', { bodyLimit: shared_1.AGENT_RUN_REQUEST_BODY_LIMIT_BYTES }, async (request, reply) => {
|
|
412
|
+
const grant = grantOf(request);
|
|
413
|
+
const actor = grant.grantId;
|
|
414
|
+
const rawDeck = request.body?.deckId;
|
|
415
|
+
const deckTarget = (0, store_1.auditDeckTarget)(typeof rawDeck === 'string' ? rawDeck : '');
|
|
416
|
+
const deny = (error) => {
|
|
417
|
+
audit(actor, 'agent_run.denied', deckTarget, 'denied', error.reason);
|
|
418
|
+
return sendError(reply, error);
|
|
419
|
+
};
|
|
420
|
+
const rawKey = request.headers[shared_1.AGENT_RUN_IDEMPOTENCY_HEADER];
|
|
421
|
+
const key = shared_1.AgentRunIdempotencyKeySchema.safeParse(typeof rawKey === 'string' ? rawKey : undefined);
|
|
422
|
+
if (!key.success) {
|
|
423
|
+
return deny({
|
|
424
|
+
status: 400,
|
|
425
|
+
code: 'IDEMPOTENCY_KEY_REQUIRED',
|
|
426
|
+
message: 'A printable Idempotency-Key header (1-200 characters) is required',
|
|
427
|
+
reason: 'idempotency_key_required',
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
const parsed = shared_1.CreateAgentRunRequestSchema.safeParse(request.body);
|
|
431
|
+
if (!parsed.success) {
|
|
432
|
+
// Field paths only: issue messages could echo the prompt.
|
|
433
|
+
const fields = [...new Set(parsed.error.issues.map((issue) => issue.path.join('.') || 'body'))];
|
|
434
|
+
return deny({
|
|
435
|
+
status: 400,
|
|
436
|
+
code: 'INVALID_REQUEST',
|
|
437
|
+
message: `Invalid agent run request: ${fields.join(', ')}`,
|
|
438
|
+
reason: 'invalid_request',
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
if (!grant.allowedDecks.includes(parsed.data.deckId)) {
|
|
442
|
+
audit(actor, 'agent_run.denied', deckTarget, 'denied', 'resource_out_of_scope');
|
|
443
|
+
return reply
|
|
444
|
+
.status((0, shared_1.httpStatusForTrustedError)('RESOURCE_OUT_OF_SCOPE'))
|
|
445
|
+
.send((0, shared_1.trustedSessionError)('RESOURCE_OUT_OF_SCOPE', "Deck is outside this grant's allowed decks"));
|
|
446
|
+
}
|
|
447
|
+
if (parsed.data.prepareFinalization && parsed.data.tools !== 'edit') {
|
|
448
|
+
return deny({
|
|
449
|
+
status: 422,
|
|
450
|
+
code: 'FINALIZATION_INVALID',
|
|
451
|
+
message: 'prepareFinalization requires tools "edit"',
|
|
452
|
+
reason: 'finalization_invalid',
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
const result = await create(grant, key.data, parsed.data);
|
|
456
|
+
if (!result.ok) {
|
|
457
|
+
return deny(result.error);
|
|
458
|
+
}
|
|
459
|
+
return reply
|
|
460
|
+
.header('Cache-Control', 'no-store')
|
|
461
|
+
.status(result.status)
|
|
462
|
+
.send({ success: true, data: shared_1.CreateAgentRunResponseSchema.parse(result.body) });
|
|
463
|
+
});
|
|
464
|
+
// No implicit HEAD routes: the policy hook skips HEAD, and run reads must
|
|
465
|
+
// never be reachable without a run grant.
|
|
466
|
+
scope.get('/agent-runs/capabilities', { exposeHeadRoute: false }, async (_request, reply) => {
|
|
467
|
+
const data = shared_1.AgentRunCapabilitiesResponseSchema.parse({
|
|
468
|
+
host: 'same-host',
|
|
469
|
+
clients: await service.capabilities(),
|
|
470
|
+
});
|
|
471
|
+
return reply.header('Cache-Control', 'no-store').send({ success: true, data });
|
|
472
|
+
});
|
|
473
|
+
scope.get('/agent-runs/:runId', { exposeHeadRoute: false }, async (request, reply) => {
|
|
474
|
+
const entry = readableRun(request, reply);
|
|
475
|
+
if (!entry) {
|
|
476
|
+
return reply;
|
|
477
|
+
}
|
|
478
|
+
return reply.header('Cache-Control', 'no-store').send({ success: true, data: statusBody(entry) });
|
|
479
|
+
});
|
|
480
|
+
scope.post('/agent-runs/:runId/cancel', async (request, reply) => {
|
|
481
|
+
const entry = readableRun(request, reply);
|
|
482
|
+
if (!entry) {
|
|
483
|
+
return reply;
|
|
484
|
+
}
|
|
485
|
+
const actor = actorOf(callerOf(request));
|
|
486
|
+
// NOT-486: cancel also stops an in-flight finalization (honored only before its commit).
|
|
487
|
+
finalizing.get(entry.runId)?.controller.abort();
|
|
488
|
+
const outcome = service.cancel(entry.runId);
|
|
489
|
+
if (outcome.kind === 'not_cancelable') {
|
|
490
|
+
audit(actor, 'agent_run.denied', entry.runId, 'denied', 'run_not_cancelable');
|
|
491
|
+
return reply
|
|
492
|
+
.status(409)
|
|
493
|
+
.send((0, shared_1.agentRunError)('RUN_NOT_CANCELABLE', 'Run is owned by another host process that cannot be signaled'));
|
|
494
|
+
}
|
|
495
|
+
// `not_found`: indexed but never recorded, which reads as `died`.
|
|
496
|
+
audit(actor, 'agent_run.canceled', entry.runId, 'succeeded', outcome.kind === 'canceled' ? null : 'already_terminal');
|
|
497
|
+
return reply.header('Cache-Control', 'no-store').send({ success: true, data: statusBody(entry) });
|
|
498
|
+
});
|
|
499
|
+
scope.get('/agent-runs/:runId/events', { exposeHeadRoute: false }, async (request, reply) => {
|
|
500
|
+
const entry = readableRun(request, reply);
|
|
501
|
+
if (!entry) {
|
|
502
|
+
return reply;
|
|
503
|
+
}
|
|
504
|
+
const query = shared_1.AgentRunEventsQuerySchema.safeParse(request.query ?? {});
|
|
505
|
+
if (!query.success) {
|
|
506
|
+
return reply
|
|
507
|
+
.status(400)
|
|
508
|
+
.send((0, shared_1.agentRunError)('INVALID_REQUEST', 'after must be a cursor and limit 1-1000'));
|
|
509
|
+
}
|
|
510
|
+
// State first: a terminal state read before the log guarantees the
|
|
511
|
+
// page that follows already holds the terminal event.
|
|
512
|
+
const { state } = publicState(entry.runId);
|
|
513
|
+
const after = query.data.after ? Number(query.data.after) : 0;
|
|
514
|
+
const limit = query.data.limit ?? shared_1.AGENT_RUN_EVENTS_DEFAULT_LIMIT;
|
|
515
|
+
const log = service.events(entry.runId);
|
|
516
|
+
const events = [];
|
|
517
|
+
for (const [position, { event }] of (log?.events ?? []).entries()) {
|
|
518
|
+
const seq = position + 1;
|
|
519
|
+
if (seq <= after) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (events.length >= limit) {
|
|
523
|
+
break;
|
|
524
|
+
}
|
|
525
|
+
const mapped = toPublicEvent(seq, event);
|
|
526
|
+
if (mapped) {
|
|
527
|
+
events.push(mapped);
|
|
528
|
+
if (mapped.type === 'canceled') {
|
|
529
|
+
break; // Cancellation is final: nothing after it is delivered.
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
const nextCursor = String(events.length > 0 ? events[events.length - 1].seq : after);
|
|
534
|
+
const data = shared_1.AgentRunEventsResponseSchema.parse({ runId: entry.runId, state, events, nextCursor });
|
|
535
|
+
return reply.header('Cache-Control', 'no-store').send({ success: true, data });
|
|
536
|
+
});
|
|
537
|
+
// NOT-486: candidate manifest of a terminal prepared run (creating grant only).
|
|
538
|
+
scope.get('/agent-runs/:runId/changes', { exposeHeadRoute: false }, async (request, reply) => {
|
|
539
|
+
const gate = finalizableRun(request, reply, 'agent_run.changes_read');
|
|
540
|
+
if (!gate) {
|
|
541
|
+
return reply;
|
|
542
|
+
}
|
|
543
|
+
const { entry, stored, actor } = gate;
|
|
544
|
+
const repositoryId = (0, git_finalize_1.repositoryIdOf)(stored.baseline);
|
|
545
|
+
let snapshot;
|
|
546
|
+
try {
|
|
547
|
+
snapshot = await finalizer.candidate(stored.baseline);
|
|
548
|
+
}
|
|
549
|
+
catch (error) {
|
|
550
|
+
if (!(error instanceof git_finalize_1.FinalizationError)) {
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
const failure = finalizationReply(error);
|
|
554
|
+
audit(actor, 'agent_run.changes_read', entry.runId, 'denied', failure.reason, { repositoryId });
|
|
555
|
+
return sendError(reply, failure);
|
|
556
|
+
}
|
|
557
|
+
audit(actor, 'agent_run.changes_read', entry.runId, 'succeeded', null, {
|
|
558
|
+
repositoryId,
|
|
559
|
+
baselineDigest: stored.baseline.digest,
|
|
560
|
+
manifestDigest: snapshot.manifestDigest,
|
|
561
|
+
fileCount: snapshot.files.length,
|
|
562
|
+
});
|
|
563
|
+
const data = shared_1.AgentRunChangesResponseSchema.parse({
|
|
564
|
+
runId: entry.runId,
|
|
565
|
+
baselineRef: stored.baselineRef,
|
|
566
|
+
baselineDigest: stored.baseline.digest,
|
|
567
|
+
baseCommit: snapshot.baseCommit,
|
|
568
|
+
files: snapshot.files,
|
|
569
|
+
manifestDigest: snapshot.manifestDigest,
|
|
570
|
+
});
|
|
571
|
+
return reply.header('Cache-Control', 'no-store').send({ success: true, data });
|
|
572
|
+
});
|
|
573
|
+
// NOT-486: one reviewed manifest → one local commit (creating grant with finalizationEnabled).
|
|
574
|
+
scope.post('/agent-runs/:runId/finalize', { bodyLimit: shared_1.AGENT_RUN_REQUEST_BODY_LIMIT_BYTES }, async (request, reply) => {
|
|
575
|
+
const grant = grantOf(request);
|
|
576
|
+
const actor = grant.grantId;
|
|
577
|
+
const target = (0, auth_1.auditRunTarget)(request.url.split('?')[0]);
|
|
578
|
+
if (!grant.finalizationEnabled) {
|
|
579
|
+
audit(actor, 'agent_run.finalized', target, 'denied', 'finalization_capability_required');
|
|
580
|
+
return reply
|
|
581
|
+
.status((0, shared_1.httpStatusForTrustedError)('FINALIZATION_CAPABILITY_REQUIRED'))
|
|
582
|
+
.send((0, shared_1.trustedSessionError)('FINALIZATION_CAPABILITY_REQUIRED', 'This grant is not enabled for run finalization'));
|
|
583
|
+
}
|
|
584
|
+
const gate = finalizableRun(request, reply, 'agent_run.finalized');
|
|
585
|
+
if (!gate) {
|
|
586
|
+
return reply;
|
|
587
|
+
}
|
|
588
|
+
const { entry, stored } = gate;
|
|
589
|
+
const runId = entry.runId;
|
|
590
|
+
const repositoryId = (0, git_finalize_1.repositoryIdOf)(stored.baseline);
|
|
591
|
+
const deny = (error, detail = { repositoryId }) => {
|
|
592
|
+
audit(actor, 'agent_run.finalized', runId, error.reason === 'finalization_failed' ? 'failed' : 'denied', error.reason, detail);
|
|
593
|
+
return sendError(reply, error);
|
|
594
|
+
};
|
|
595
|
+
const rawKey = request.headers[shared_1.AGENT_RUN_IDEMPOTENCY_HEADER];
|
|
596
|
+
const key = shared_1.AgentRunIdempotencyKeySchema.safeParse(typeof rawKey === 'string' ? rawKey : undefined);
|
|
597
|
+
if (!key.success) {
|
|
598
|
+
return deny({
|
|
599
|
+
status: 400,
|
|
600
|
+
code: 'IDEMPOTENCY_KEY_REQUIRED',
|
|
601
|
+
message: 'A printable Idempotency-Key header (1-200 characters) is required',
|
|
602
|
+
reason: 'idempotency_key_required',
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
const parsed = shared_1.FinalizeAgentRunRequestSchema.safeParse(request.body);
|
|
606
|
+
if (!parsed.success) {
|
|
607
|
+
// Field paths only: issue messages could echo the commit message or a path.
|
|
608
|
+
const fields = [
|
|
609
|
+
...new Set(parsed.error.issues.map((issue) => issue.path.filter((part) => typeof part === 'string').join('.') || 'body')),
|
|
610
|
+
];
|
|
611
|
+
return deny({
|
|
612
|
+
status: 400,
|
|
613
|
+
code: 'INVALID_REQUEST',
|
|
614
|
+
message: `Invalid finalize request: ${fields.join(', ')}`,
|
|
615
|
+
reason: 'invalid_request',
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
const body = parsed.data;
|
|
619
|
+
const manifestDigest = (0, git_finalize_1.manifestDigestOf)(body.files);
|
|
620
|
+
const detail = {
|
|
621
|
+
repositoryId,
|
|
622
|
+
baselineDigest: stored.baseline.digest,
|
|
623
|
+
manifestDigest,
|
|
624
|
+
fileCount: body.files.length,
|
|
625
|
+
};
|
|
626
|
+
const send = (outcome) => outcome.ok
|
|
627
|
+
? reply.header('Cache-Control', 'no-store').send({ success: true, data: outcome.data })
|
|
628
|
+
: sendError(reply, outcome.error);
|
|
629
|
+
const hash = finalizeRequestHash(runId, body);
|
|
630
|
+
const claim = finalizations.claim({ runId, grantId: grant.grantId, idempotencyKey: key.data, requestHash: hash });
|
|
631
|
+
if (!claim.claimed) {
|
|
632
|
+
const existing = claim.existing;
|
|
633
|
+
const same = existing.runId === runId && existing.idempotencyKey === key.data && existing.requestHash === hash;
|
|
634
|
+
if (!same) {
|
|
635
|
+
return deny({
|
|
636
|
+
status: 409,
|
|
637
|
+
code: 'IDEMPOTENCY_CONFLICT',
|
|
638
|
+
message: 'This run or Idempotency-Key already has a different finalization request',
|
|
639
|
+
reason: 'idempotency_conflict',
|
|
640
|
+
}, detail);
|
|
641
|
+
}
|
|
642
|
+
if (existing.state === 'finalized' && existing.result) {
|
|
643
|
+
// Durable replay: the original commit, with no Git invocation.
|
|
644
|
+
const result = existing.result;
|
|
645
|
+
audit(actor, 'agent_run.finalized', runId, 'succeeded', 'replayed', {
|
|
646
|
+
...detail,
|
|
647
|
+
commit: result.commit,
|
|
648
|
+
tree: result.tree,
|
|
649
|
+
});
|
|
650
|
+
return send({ ok: true, data: result });
|
|
651
|
+
}
|
|
652
|
+
const pending = finalizing.get(runId);
|
|
653
|
+
if (pending) {
|
|
654
|
+
return send(await pending.outcome);
|
|
655
|
+
}
|
|
656
|
+
return deny({
|
|
657
|
+
status: 409,
|
|
658
|
+
code: 'GIT_LOCK_CONFLICT',
|
|
659
|
+
message: 'This finalization is still in progress',
|
|
660
|
+
reason: 'git_lock_conflict',
|
|
661
|
+
}, detail);
|
|
662
|
+
}
|
|
663
|
+
const controller = new AbortController();
|
|
664
|
+
// A caller that disconnects before the commit cancels the operation.
|
|
665
|
+
const onClose = () => {
|
|
666
|
+
if (!reply.raw.writableEnded) {
|
|
667
|
+
controller.abort();
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
reply.raw.once('close', onClose);
|
|
671
|
+
const outcome = (async () => {
|
|
672
|
+
try {
|
|
673
|
+
const result = await finalizer.finalize(stored.baseline, {
|
|
674
|
+
runId,
|
|
675
|
+
baselineDigest: body.baselineDigest,
|
|
676
|
+
expectedBaseCommit: body.expectedBaseCommit,
|
|
677
|
+
commitMessage: body.commitMessage,
|
|
678
|
+
files: body.files,
|
|
679
|
+
}, {
|
|
680
|
+
signal: controller.signal,
|
|
681
|
+
onLocked: (lockIno) => finalizations.markLocked(runId, lockIno),
|
|
682
|
+
onCommitObject: (commit, tree) => finalizations.markPending(runId, shared_1.FinalizeAgentRunResponseSchema.parse({
|
|
683
|
+
runId,
|
|
684
|
+
finalizationState: 'finalized',
|
|
685
|
+
commit,
|
|
686
|
+
tree,
|
|
687
|
+
parent: body.expectedBaseCommit,
|
|
688
|
+
manifestDigest,
|
|
689
|
+
fileCount: body.files.length,
|
|
690
|
+
reviewId: body.reviewId,
|
|
691
|
+
})),
|
|
692
|
+
});
|
|
693
|
+
const data = shared_1.FinalizeAgentRunResponseSchema.parse({
|
|
694
|
+
runId,
|
|
695
|
+
finalizationState: 'finalized',
|
|
696
|
+
commit: result.commit,
|
|
697
|
+
tree: result.tree,
|
|
698
|
+
parent: result.parent,
|
|
699
|
+
manifestDigest: result.manifestDigest,
|
|
700
|
+
fileCount: result.fileCount,
|
|
701
|
+
reviewId: body.reviewId,
|
|
702
|
+
});
|
|
703
|
+
finalizations.complete(runId, data);
|
|
704
|
+
audit(actor, 'agent_run.finalized', runId, 'succeeded', null, {
|
|
705
|
+
...detail,
|
|
706
|
+
commit: data.commit,
|
|
707
|
+
tree: data.tree,
|
|
708
|
+
});
|
|
709
|
+
return { ok: true, data };
|
|
710
|
+
}
|
|
711
|
+
catch (error) {
|
|
712
|
+
finalizations.release(runId);
|
|
713
|
+
const failure = finalizationReply(error instanceof git_finalize_1.FinalizationError
|
|
714
|
+
? error
|
|
715
|
+
: new git_finalize_1.FinalizationError('finalization_failed', 'Finalization failed before the commit'));
|
|
716
|
+
audit(actor, 'agent_run.finalized', runId, failure.reason === 'finalization_failed' ? 'failed' : 'denied', failure.reason, detail);
|
|
717
|
+
return { ok: false, error: failure };
|
|
718
|
+
}
|
|
719
|
+
})();
|
|
720
|
+
finalizing.set(runId, { outcome, controller });
|
|
721
|
+
try {
|
|
722
|
+
return send(await outcome);
|
|
723
|
+
}
|
|
724
|
+
finally {
|
|
725
|
+
finalizing.delete(runId);
|
|
726
|
+
reply.raw.off('close', onClose);
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
/** Canonical log line → public event. Unknown line types are skipped (their `seq` stays reserved). */
|
|
732
|
+
function toPublicEvent(seq, event) {
|
|
733
|
+
const ts = typeof event.ts === 'string' ? event.ts : '';
|
|
734
|
+
const sessionId = typeof event.sessionId === 'string' && event.sessionId ? event.sessionId : null;
|
|
735
|
+
const str = (value) => (typeof value === 'string' ? value : '');
|
|
736
|
+
switch (event.t) {
|
|
737
|
+
case 'text':
|
|
738
|
+
return { seq, ts, type: 'text', text: str(event.text) };
|
|
739
|
+
case 'tool':
|
|
740
|
+
return { seq, ts, type: 'tool', name: str(event.name) };
|
|
741
|
+
case 'notice':
|
|
742
|
+
return { seq, ts, type: 'notice', message: str(event.message), sessionId };
|
|
743
|
+
case 'result':
|
|
744
|
+
return { seq, ts, type: 'result', subtype: str(event.subtype), result: event.result, sessionId };
|
|
745
|
+
case 'end':
|
|
746
|
+
return { seq, ts, type: 'end', sessionId };
|
|
747
|
+
case 'error':
|
|
748
|
+
return { seq, ts, type: 'error', message: str(event.message), sessionId };
|
|
749
|
+
case 'canceled':
|
|
750
|
+
return { seq, ts, type: 'canceled', sessionId };
|
|
751
|
+
default:
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
//# sourceMappingURL=agent-runs.js.map
|