agent-dealer 1.4.1 → 1.4.3
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/bundle/server/dist/adapters/github.js +66 -3
- package/bundle/server/dist/adapters/github.test.js +90 -0
- package/bundle/server/dist/coordinator/auto-merge.js +17 -6
- package/bundle/server/dist/coordinator/commands.js +18 -2
- package/bundle/server/dist/coordinator/developer-effect.js +49 -7
- package/bundle/server/dist/coordinator/developer-effect.test.js +118 -4
- package/bundle/server/dist/coordinator/muse-developer.integration.test.js +2 -1
- package/bundle/server/dist/coordinator/operator-criteria.js +250 -17
- package/bundle/server/dist/coordinator/operator-criteria.test.js +177 -4
- package/bundle/server/dist/coordinator/operator-verification.integration.test.js +208 -0
- package/bundle/server/dist/coordinator/projection.js +5 -1
- package/bundle/server/dist/coordinator/prompts.js +77 -4
- package/bundle/server/dist/coordinator/prompts.test.js +22 -4
- package/bundle/server/dist/coordinator/reviewer-effect.js +64 -45
- package/bundle/server/dist/coordinator/reviewer-effect.test.js +63 -10
- package/bundle/server/dist/coordinator/reviewer-pr-evidence.test.js +245 -0
- package/bundle/server/dist/coordinator/reviewer-result.js +4 -3
- package/bundle/server/dist/coordinator/reviewer-result.test.js +6 -0
- package/bundle/server/dist/coordinator/routing.js +3 -0
- package/bundle/server/dist/coordinator/routing.test.js +19 -0
- package/bundle/server/dist/coordinator/usage.js +35 -2
- package/bundle/server/dist/coordinator/usage.test.js +49 -1
- package/bundle/server/dist/docs-api-reference.test.js +89 -0
- package/bundle/server/package.json +2 -2
- package/bundle/shared/package.json +1 -1
- package/package.json +1 -1
|
@@ -26,6 +26,7 @@ const { createAgent } = await import("../repository/agents.js");
|
|
|
26
26
|
const { createIssue, getIssue } = await import("../repository/issues.js");
|
|
27
27
|
const { listWorkerSessionsForIssue } = await import("../repository/worker-sessions.js");
|
|
28
28
|
const { listWorkItemsForIssue, cancelWorkItem } = await import("../repository/work-items.js");
|
|
29
|
+
const { createIssueArtifact } = await import("../repository/artifacts.js");
|
|
29
30
|
const { listArtifactsForIssue } = await import("../repository/artifacts-for-issue.js");
|
|
30
31
|
const { listHumanActionsForIssue } = await import("../repository/human-actions.js");
|
|
31
32
|
const { listFindingsForIssue } = await import("../repository/findings.js");
|
|
@@ -105,7 +106,7 @@ async function makeIssue(opts = {}) {
|
|
|
105
106
|
maxReviewRounds: 3,
|
|
106
107
|
maxInfraAttempts: opts.maxInfraAttempts ?? 3,
|
|
107
108
|
// NOT-489: omitted now means auto-merge; this suite exercises final human review.
|
|
108
|
-
autoMerge: false,
|
|
109
|
+
autoMerge: opts.autoMerge ?? false,
|
|
109
110
|
source: "manual"
|
|
110
111
|
}).id;
|
|
111
112
|
}
|
|
@@ -283,6 +284,53 @@ test("approved: the coordinator verifies the pinned SHA, publishes the review, a
|
|
|
283
284
|
assert.equal(usage.length, 1);
|
|
284
285
|
assert.equal(usage[0].workerSessionId, revSession.id);
|
|
285
286
|
});
|
|
287
|
+
test("NOT-575: the reviewer prompt carries the PR's live body as evidence", async () => {
|
|
288
|
+
const issueId = await makeIssue();
|
|
289
|
+
const github = fakeGithub();
|
|
290
|
+
github.prBody = async () => "Implementation conclusion.\n\nProbe output:\n```\nSSO login OK\n```";
|
|
291
|
+
await advanceToReviewing(issueId, github);
|
|
292
|
+
let captured = "";
|
|
293
|
+
const capturing = async (input) => {
|
|
294
|
+
captured = input.prompt;
|
|
295
|
+
return verdictSpawn({ verdict: "approved" })(input);
|
|
296
|
+
};
|
|
297
|
+
registerEffectHandler("reviewer", (ctx) => runReviewerEffect(ctx, { deckCallTool: okDeckCallTool, spawn: capturing, github }));
|
|
298
|
+
await pump(1);
|
|
299
|
+
assert.match(captured, /## PR body \(live — evidence pasted there counts\)/);
|
|
300
|
+
assert.match(captured, /SSO login OK/);
|
|
301
|
+
assert.match(captured, /never escalate only because it lives here instead of in the diff/);
|
|
302
|
+
assert.equal(getIssue(issueId).status, "final_review");
|
|
303
|
+
});
|
|
304
|
+
test("NOT-575: the reviewer prompt carries recorded operator attestations as evidence", async () => {
|
|
305
|
+
const issueId = await makeIssue();
|
|
306
|
+
const github = fakeGithub();
|
|
307
|
+
await advanceToReviewing(issueId, github);
|
|
308
|
+
const headSha = getIssue(issueId).headSha;
|
|
309
|
+
createIssueArtifact({
|
|
310
|
+
issueId,
|
|
311
|
+
kind: "operator_verification",
|
|
312
|
+
author: "human",
|
|
313
|
+
content: {
|
|
314
|
+
headSha,
|
|
315
|
+
note: "SSO login OK — dashboard shown.",
|
|
316
|
+
verifiedBy: "op",
|
|
317
|
+
verifiedAt: new Date().toISOString(),
|
|
318
|
+
criteria: ["Operator signs in with SSO [operator]"],
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
let captured = "";
|
|
322
|
+
const capturing = async (input) => {
|
|
323
|
+
captured = input.prompt;
|
|
324
|
+
return verdictSpawn({ verdict: "approved" })(input);
|
|
325
|
+
};
|
|
326
|
+
registerEffectHandler("reviewer", (ctx) => runReviewerEffect(ctx, { deckCallTool: okDeckCallTool, spawn: capturing, github }));
|
|
327
|
+
await pump(1);
|
|
328
|
+
assert.match(captured, /## Operator attestations \(recorded results count as evidence\)/);
|
|
329
|
+
assert.match(captured, /SSO login OK — dashboard shown/);
|
|
330
|
+
assert.match(captured, /never escalate for evidence an attestation below already provides/);
|
|
331
|
+
assert.match(captured, /this head/);
|
|
332
|
+
assert.equal(getIssue(issueId).status, "final_review");
|
|
333
|
+
});
|
|
286
334
|
test("same-identity fallback: GitHub rejecting APPROVE as a self-review still records the internal verdict and moves to final_review", async () => {
|
|
287
335
|
const issueId = await makeIssue();
|
|
288
336
|
const github = fakeGithub({ rejectSelfReview: true });
|
|
@@ -452,7 +500,7 @@ test("publish claim: a prior claimant that recorded failure is safely reclaimed
|
|
|
452
500
|
test("diff truncated: AC-met approved (no blocking) stays on the approve path — never bare escalate", async () => {
|
|
453
501
|
const issueId = await makeIssue();
|
|
454
502
|
const github = fakeGithub();
|
|
455
|
-
// A single file whose diff alone exceeds prompts.ts's TOTAL_DIFF_LIMIT (
|
|
503
|
+
// A single file whose diff alone exceeds prompts.ts's TOTAL_DIFF_LIMIT (600,000 chars).
|
|
456
504
|
const bigFileSpawn = async (input) => {
|
|
457
505
|
const lines = Array.from({ length: 20_000 }, (_, i) => `line ${i} of a very large generated file`);
|
|
458
506
|
fs.writeFileSync(path.join(input.cwd, "big.txt"), `${lines.join("\n")}\n`);
|
|
@@ -475,7 +523,7 @@ test("diff truncated: AC-met approved (no blocking) stays on the approve path
|
|
|
475
523
|
const published = listArtifactsForIssue(issueId).find((a) => a.kind === "review_published");
|
|
476
524
|
assert.equal(JSON.parse(published.contentJson).event, "APPROVE");
|
|
477
525
|
});
|
|
478
|
-
test("diff truncated: blocking
|
|
526
|
+
test("diff truncated: the reviewer's own blocking finding becomes changes_requested without an incomplete-review finding", async () => {
|
|
479
527
|
const issueId = await makeIssue();
|
|
480
528
|
const github = fakeGithub();
|
|
481
529
|
const bigFileSpawn = async (input) => {
|
|
@@ -507,10 +555,13 @@ test("diff truncated: blocking incomplete-review finding remaps to changes_reque
|
|
|
507
555
|
const truncateEvidence = listArtifactsForIssue(issueId).find((a) => a.kind === "diff_truncated_evidence");
|
|
508
556
|
assert.ok(truncateEvidence);
|
|
509
557
|
assert.equal(JSON.parse(truncateEvidence.contentJson).overriddenTo, "changes_requested");
|
|
558
|
+
const findings = listFindingsForIssue(issueId);
|
|
559
|
+
assert.deepEqual(findings.map(({ fingerprint, severity, title, rationale }) => ({ fingerprint, severity, title, rationale })), [finding]);
|
|
560
|
+
assert.ok(!findings.some((f) => f.fingerprint === "diff-truncated-incomplete-review"));
|
|
510
561
|
assert.ok(!listHumanActionsForIssue(issueId).find((a) => a.actionType === "policy_escalation"));
|
|
511
562
|
});
|
|
512
|
-
test("diff truncated: bare escalated
|
|
513
|
-
const issueId = await makeIssue();
|
|
563
|
+
test("diff truncated: bare escalated opens final_review with omitted paths even when auto-merge is on", async () => {
|
|
564
|
+
const issueId = await makeIssue({ autoMerge: true });
|
|
514
565
|
const github = fakeGithub();
|
|
515
566
|
const bigFileSpawn = async (input) => {
|
|
516
567
|
const lines = Array.from({ length: 20_000 }, (_, i) => `line ${i} of a very large generated file`);
|
|
@@ -525,12 +576,14 @@ test("diff truncated: bare escalated remaps to changes_requested with incomplete
|
|
|
525
576
|
registerEffectHandler("reviewer", (ctx) => runReviewerEffect(ctx, { deckCallTool: okDeckCallTool, spawn: verdictSpawn({ verdict: "escalated" }), github }));
|
|
526
577
|
await pump(1);
|
|
527
578
|
const issue = getIssue(issueId);
|
|
528
|
-
assert.equal(issue.status, "
|
|
579
|
+
assert.equal(issue.status, "final_review");
|
|
529
580
|
const truncateEvidence = listArtifactsForIssue(issueId).find((a) => a.kind === "diff_truncated_evidence");
|
|
530
|
-
assert.equal(JSON.parse(truncateEvidence.contentJson).overriddenTo, "
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
assert.
|
|
581
|
+
assert.equal(JSON.parse(truncateEvidence.contentJson).overriddenTo, "escalated");
|
|
582
|
+
const action = listHumanActionsForIssue(issueId).find((a) => a.actionType === "final_review");
|
|
583
|
+
assert.ok(action);
|
|
584
|
+
assert.equal(action.reason, "Diff exceeds the reviewer limit (600,000 characters); not reviewed: big.txt");
|
|
585
|
+
assert.equal(listWorkItemsForIssue(issueId).filter((item) => item.kind === "developer" && item.round === 2).length, 0, "operator escalation must not queue a repair round");
|
|
586
|
+
assert.deepEqual(listFindingsForIssue(issueId), []);
|
|
534
587
|
});
|
|
535
588
|
test("changes_requested: findings thread onto the issue and a fresh developer repair round is queued", async () => {
|
|
536
589
|
const issueId = await makeIssue();
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// NOT-575: the reviewer can see PR-body evidence and recorded operator
|
|
2
|
+
// attestations — the sandbox has no network/`gh`, so both arrive via the
|
|
3
|
+
// prompt. Covers the PR-body path and the attestation path.
|
|
4
|
+
import { test, before, beforeEach } from "node:test";
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
process.env.AGENT_DEALER_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "dealer-not575-rev-"));
|
|
10
|
+
const { migrate, getDb } = await import("../db/index.js");
|
|
11
|
+
const { BUILTIN_AGENT_CLAUDE_ID, BUILTIN_AGENT_CURSOR_ID } = await import("@agent-dealer/shared");
|
|
12
|
+
const { createIssue } = await import("../repository/issues.js");
|
|
13
|
+
const { createIssueArtifact } = await import("../repository/artifacts.js");
|
|
14
|
+
const { appendWorkflowEvent } = await import("../repository/workflow-events.js");
|
|
15
|
+
const { listOperatorAttestations } = await import("./operator-criteria.js");
|
|
16
|
+
const { buildReviewerPrompt, operatorAttestationsSection, prBodyEvidenceSection, PR_BODY_EVIDENCE_MAX_CHARS, OPERATOR_ATTESTATION_NOTE_MAX_CHARS, OPERATOR_ATTESTATIONS_MAX_COUNT, } = await import("./prompts.js");
|
|
17
|
+
const { readPrBodyForReview } = await import("./reviewer-effect.js");
|
|
18
|
+
const { createGithubAdapter, parsePrBody } = await import("../adapters/github.js");
|
|
19
|
+
before(() => migrate());
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
getDb().exec(`
|
|
22
|
+
DELETE FROM work_items;
|
|
23
|
+
DELETE FROM human_actions;
|
|
24
|
+
DELETE FROM workflow_events;
|
|
25
|
+
DELETE FROM worker_sessions;
|
|
26
|
+
DELETE FROM artifacts;
|
|
27
|
+
DELETE FROM workflow_instances;
|
|
28
|
+
DELETE FROM issues;
|
|
29
|
+
`);
|
|
30
|
+
});
|
|
31
|
+
const taskSnapshot = {
|
|
32
|
+
title: "Add widget",
|
|
33
|
+
description: "Build the widget.",
|
|
34
|
+
acceptanceCriteria: "Widget renders.",
|
|
35
|
+
repo: "acme/app",
|
|
36
|
+
baseBranch: "main",
|
|
37
|
+
};
|
|
38
|
+
const reviewerBase = {
|
|
39
|
+
taskSnapshot,
|
|
40
|
+
round: 1,
|
|
41
|
+
baseSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
42
|
+
headSha: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
43
|
+
diff: "diff --git a/x b/x\n+added line\n",
|
|
44
|
+
};
|
|
45
|
+
test("PR-body path: the prompt embeds the live body and never escalates for living there", () => {
|
|
46
|
+
const withBody = buildReviewerPrompt({
|
|
47
|
+
...reviewerBase,
|
|
48
|
+
prBody: "Implementation conclusion.\n\nProbe output:\n```\nSSO login OK — dashboard shown\n```",
|
|
49
|
+
});
|
|
50
|
+
assert.match(withBody, /## PR body \(live — evidence pasted there counts\)/);
|
|
51
|
+
assert.match(withBody, /SSO login OK — dashboard shown/);
|
|
52
|
+
assert.match(withBody, /never escalate only because it lives here instead of in the diff/);
|
|
53
|
+
});
|
|
54
|
+
test("PR-body path: absent or blank bodies render nothing (byte-for-byte)", () => {
|
|
55
|
+
const without = buildReviewerPrompt(reviewerBase);
|
|
56
|
+
assert.doesNotMatch(without, /## PR body/);
|
|
57
|
+
assert.equal(buildReviewerPrompt({ ...reviewerBase, prBody: undefined }), without);
|
|
58
|
+
assert.equal(buildReviewerPrompt({ ...reviewerBase, prBody: null }), without);
|
|
59
|
+
assert.equal(buildReviewerPrompt({ ...reviewerBase, prBody: " \n " }), without);
|
|
60
|
+
assert.deepEqual(prBodyEvidenceSection(undefined), []);
|
|
61
|
+
assert.deepEqual(prBodyEvidenceSection(" "), []);
|
|
62
|
+
});
|
|
63
|
+
test("PR-body path: long bodies cap with an explicit truncation marker", () => {
|
|
64
|
+
const body = `start-${"x".repeat(PR_BODY_EVIDENCE_MAX_CHARS + 500)}`;
|
|
65
|
+
const section = prBodyEvidenceSection(body).join("\n");
|
|
66
|
+
assert.match(section, /start-xxx/);
|
|
67
|
+
assert.match(section, new RegExp(`PR body truncated to ${PR_BODY_EVIDENCE_MAX_CHARS} characters`));
|
|
68
|
+
assert.ok(section.length < body.length + 1000);
|
|
69
|
+
});
|
|
70
|
+
test("attestation path: verified and waived results render with heads, criteria, and notes", () => {
|
|
71
|
+
const section = operatorAttestationsSection([
|
|
72
|
+
{
|
|
73
|
+
kind: "verified",
|
|
74
|
+
headSha: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
75
|
+
note: "SSO login OK — dashboard shown.",
|
|
76
|
+
by: "op",
|
|
77
|
+
at: "2026-11-01T00:00:00.000Z",
|
|
78
|
+
criteria: ["Operator signs in with SSO [operator]"],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
kind: "waived",
|
|
82
|
+
headSha: "cccccccccccccccccccccccccccccccccccccccc",
|
|
83
|
+
note: "Staging down; shipping behind the flag.",
|
|
84
|
+
by: "op2",
|
|
85
|
+
at: null,
|
|
86
|
+
criteria: [],
|
|
87
|
+
},
|
|
88
|
+
], "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").join("\n");
|
|
89
|
+
assert.match(section, /## Operator attestations \(recorded results count as evidence\)/);
|
|
90
|
+
assert.match(section, /never escalate for evidence an attestation below already provides/);
|
|
91
|
+
assert.match(section, /### Verified \(this head by op at 2026-11-01T00:00:00\.000Z\)/);
|
|
92
|
+
assert.match(section, /- Criterion: Operator signs in with SSO \[operator\]/);
|
|
93
|
+
assert.match(section, /SSO login OK — dashboard shown/);
|
|
94
|
+
assert.match(section, /### Waived \(head `cccccccc` by op2\)/);
|
|
95
|
+
assert.match(section, /Staging down; shipping behind the flag/);
|
|
96
|
+
});
|
|
97
|
+
test("attestation path: empty renders nothing; notes and count cap with markers", () => {
|
|
98
|
+
assert.deepEqual(operatorAttestationsSection(undefined, reviewerBase.headSha), []);
|
|
99
|
+
assert.deepEqual(operatorAttestationsSection([], reviewerBase.headSha), []);
|
|
100
|
+
const without = buildReviewerPrompt(reviewerBase);
|
|
101
|
+
assert.doesNotMatch(without, /## Operator attestations/);
|
|
102
|
+
assert.equal(buildReviewerPrompt({ ...reviewerBase, operatorAttestations: [] }), without);
|
|
103
|
+
const long = operatorAttestationsSection([
|
|
104
|
+
{
|
|
105
|
+
kind: "verified",
|
|
106
|
+
headSha: reviewerBase.headSha,
|
|
107
|
+
note: `n-${"y".repeat(OPERATOR_ATTESTATION_NOTE_MAX_CHARS + 100)}`,
|
|
108
|
+
by: null,
|
|
109
|
+
at: null,
|
|
110
|
+
criteria: [],
|
|
111
|
+
},
|
|
112
|
+
], reviewerBase.headSha).join("\n");
|
|
113
|
+
assert.match(long, new RegExp(`attestation note truncated to ${OPERATOR_ATTESTATION_NOTE_MAX_CHARS} characters`));
|
|
114
|
+
const many = Array.from({ length: OPERATOR_ATTESTATIONS_MAX_COUNT + 2 }, (_, i) => ({
|
|
115
|
+
kind: "verified",
|
|
116
|
+
headSha: reviewerBase.headSha,
|
|
117
|
+
note: `note-${i}`,
|
|
118
|
+
by: null,
|
|
119
|
+
at: null,
|
|
120
|
+
criteria: [],
|
|
121
|
+
}));
|
|
122
|
+
const capped = operatorAttestationsSection(many, reviewerBase.headSha).join("\n");
|
|
123
|
+
assert.match(capped, /2 older attestation\(s\) omitted/);
|
|
124
|
+
assert.match(capped, /note-6/);
|
|
125
|
+
assert.doesNotMatch(capped, /note-0/);
|
|
126
|
+
});
|
|
127
|
+
test("attestation path: the prompt carries both evidence sections together", () => {
|
|
128
|
+
const both = buildReviewerPrompt({
|
|
129
|
+
...reviewerBase,
|
|
130
|
+
prBody: "PR body with the probe command.",
|
|
131
|
+
operatorAttestations: [
|
|
132
|
+
{
|
|
133
|
+
kind: "verified",
|
|
134
|
+
headSha: reviewerBase.headSha,
|
|
135
|
+
note: "SSO login OK.",
|
|
136
|
+
by: "op",
|
|
137
|
+
at: null,
|
|
138
|
+
criteria: ["Operator signs in with SSO [operator]"],
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
assert.match(both, /## PR body \(live/);
|
|
143
|
+
assert.match(both, /PR body with the probe command/);
|
|
144
|
+
assert.match(both, /## Operator attestations/);
|
|
145
|
+
assert.match(both, /SSO login OK/);
|
|
146
|
+
});
|
|
147
|
+
function newIssueId() {
|
|
148
|
+
return createIssue({
|
|
149
|
+
title: "Widget",
|
|
150
|
+
description: "d",
|
|
151
|
+
acceptanceCriteria: "Widget renders.",
|
|
152
|
+
repo: "acme/app",
|
|
153
|
+
developerAgentId: BUILTIN_AGENT_CLAUDE_ID,
|
|
154
|
+
reviewerAgentId: BUILTIN_AGENT_CURSOR_ID,
|
|
155
|
+
baseBranch: "main",
|
|
156
|
+
maxReviewRounds: 3,
|
|
157
|
+
maxInfraAttempts: 3,
|
|
158
|
+
source: "manual",
|
|
159
|
+
autoMerge: true,
|
|
160
|
+
}).id;
|
|
161
|
+
}
|
|
162
|
+
test("listOperatorAttestations reads verified artifacts and waiver events, oldest first", () => {
|
|
163
|
+
const issueId = newIssueId();
|
|
164
|
+
assert.deepEqual(listOperatorAttestations(issueId), []);
|
|
165
|
+
createIssueArtifact({
|
|
166
|
+
issueId,
|
|
167
|
+
kind: "operator_verification",
|
|
168
|
+
author: "human",
|
|
169
|
+
content: {
|
|
170
|
+
headSha: "aaa",
|
|
171
|
+
note: "SSO login OK.",
|
|
172
|
+
verifiedBy: "op",
|
|
173
|
+
verifiedAt: "2026-11-01T00:00:00.000Z",
|
|
174
|
+
criteria: ["Operator signs in with SSO [operator]"],
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
appendWorkflowEvent({
|
|
178
|
+
issueId,
|
|
179
|
+
type: "operator_verification.waived",
|
|
180
|
+
actorType: "human",
|
|
181
|
+
actorRef: "op2",
|
|
182
|
+
stage: "needs_human",
|
|
183
|
+
round: 1,
|
|
184
|
+
payload: {
|
|
185
|
+
actionType: "operator_verification",
|
|
186
|
+
choice: "waive",
|
|
187
|
+
headSha: "bbb",
|
|
188
|
+
waivedBy: "op2",
|
|
189
|
+
note: "Staging down.",
|
|
190
|
+
criteria: ["Operator completes a paid checkout [operator]"],
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
const attestations = listOperatorAttestations(issueId);
|
|
194
|
+
assert.equal(attestations.length, 2);
|
|
195
|
+
assert.deepEqual(attestations[0], {
|
|
196
|
+
kind: "verified",
|
|
197
|
+
headSha: "aaa",
|
|
198
|
+
note: "SSO login OK.",
|
|
199
|
+
by: "op",
|
|
200
|
+
at: "2026-11-01T00:00:00.000Z",
|
|
201
|
+
criteria: ["Operator signs in with SSO [operator]"],
|
|
202
|
+
});
|
|
203
|
+
assert.equal(attestations[1].kind, "waived");
|
|
204
|
+
assert.equal(attestations[1].headSha, "bbb");
|
|
205
|
+
assert.equal(attestations[1].note, "Staging down.");
|
|
206
|
+
assert.equal(attestations[1].by, "op2");
|
|
207
|
+
assert.deepEqual(attestations[1].criteria, ["Operator completes a paid checkout [operator]"]);
|
|
208
|
+
});
|
|
209
|
+
test("listOperatorAttestations skips unreadable rows instead of throwing", () => {
|
|
210
|
+
const issueId = newIssueId();
|
|
211
|
+
getDb()
|
|
212
|
+
.prepare("INSERT INTO artifacts (id, issue_id, kind, content_json, author, created_at) VALUES (?, ?, 'operator_verification', 'not json', 'human', ?)")
|
|
213
|
+
.run(crypto.randomUUID(), issueId, new Date().toISOString());
|
|
214
|
+
assert.deepEqual(listOperatorAttestations(issueId), []);
|
|
215
|
+
});
|
|
216
|
+
test("readPrBodyForReview returns the body, null without a PR, a read, or on failure", async () => {
|
|
217
|
+
const withBody = { prBody: async () => "live body" };
|
|
218
|
+
assert.equal(await readPrBodyForReview(withBody, "/wt", 42), "live body");
|
|
219
|
+
assert.equal(await readPrBodyForReview(withBody, "/wt", null), null);
|
|
220
|
+
assert.equal(await readPrBodyForReview({}, "/wt", 42), null);
|
|
221
|
+
const failing = {
|
|
222
|
+
prBody: async () => {
|
|
223
|
+
throw new Error("gh down");
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
assert.equal(await readPrBodyForReview(failing, "/wt", 42), null);
|
|
227
|
+
});
|
|
228
|
+
test("parsePrBody keeps the body text, null when absent or blank", () => {
|
|
229
|
+
assert.equal(parsePrBody("hello"), "hello");
|
|
230
|
+
assert.equal(parsePrBody(" padded "), " padded ");
|
|
231
|
+
assert.equal(parsePrBody(""), null);
|
|
232
|
+
assert.equal(parsePrBody(" "), null);
|
|
233
|
+
assert.equal(parsePrBody(null), null);
|
|
234
|
+
assert.equal(parsePrBody(undefined), null);
|
|
235
|
+
assert.equal(parsePrBody(42), null);
|
|
236
|
+
});
|
|
237
|
+
test("the real adapter reads the PR body with an explicit-number gh pr view", async () => {
|
|
238
|
+
const seen = [];
|
|
239
|
+
const adapter = createGithubAdapter(async (args, opts) => {
|
|
240
|
+
seen.push({ args, cwd: opts.cwd });
|
|
241
|
+
return { stdout: JSON.stringify({ body: "the live body" }) };
|
|
242
|
+
});
|
|
243
|
+
assert.equal(await adapter.prBody({ cwd: "/repo", number: 42 }), "the live body");
|
|
244
|
+
assert.deepEqual(seen, [{ args: ["pr", "view", "42", "--json", "body"], cwd: "/repo" }]);
|
|
245
|
+
});
|
|
@@ -74,15 +74,16 @@ export function normalizeReviewerResult(raw) {
|
|
|
74
74
|
return withoutQuestion;
|
|
75
75
|
}
|
|
76
76
|
/** Mirrors the plan-triage/reflect JSON-fence parsing pattern already used elsewhere. */
|
|
77
|
-
export function parseReviewerResult(text) {
|
|
77
|
+
export function parseReviewerResult(text, options = {}) {
|
|
78
78
|
const trimmed = text.trim();
|
|
79
79
|
const fenceMatch = trimmed.match(/```(?:json)?\s*\n([\s\S]*?)\n```/);
|
|
80
80
|
const candidates = [fenceMatch?.[1] ?? trimmed, trimmed];
|
|
81
81
|
for (const candidate of candidates) {
|
|
82
82
|
try {
|
|
83
83
|
const parsed = ReviewerResult.safeParse(JSON.parse(candidate));
|
|
84
|
-
if (parsed.success)
|
|
85
|
-
return normalizeReviewerResult(parsed.data);
|
|
84
|
+
if (parsed.success) {
|
|
85
|
+
return options.normalize === false ? parsed.data : normalizeReviewerResult(parsed.data);
|
|
86
|
+
}
|
|
86
87
|
}
|
|
87
88
|
catch {
|
|
88
89
|
// try next candidate
|
|
@@ -43,6 +43,12 @@ test("NOT-150: escalated without productScopeQuestion remaps to changes_requeste
|
|
|
43
43
|
assert.ok(result?.findings.some((f) => f.severity === "blocking"));
|
|
44
44
|
assert.ok(result?.findings.some((f) => f.fingerprint === "escalated-without-product-scope-question"));
|
|
45
45
|
});
|
|
46
|
+
test("NOT-630: parsing can preserve a bare escalated verdict for truncated-diff policy", () => {
|
|
47
|
+
const bare = { ...valid, verdict: "escalated" };
|
|
48
|
+
const result = parseReviewerResult(`\`\`\`json\n${JSON.stringify(bare)}\n\`\`\``, { normalize: false });
|
|
49
|
+
assert.equal(result?.verdict, "escalated");
|
|
50
|
+
assert.deepEqual(result?.findings, []);
|
|
51
|
+
});
|
|
46
52
|
test("NOT-150: bare escalate with existing blocking findings keeps those findings", () => {
|
|
47
53
|
const bare = {
|
|
48
54
|
...valid,
|
|
@@ -366,6 +366,9 @@ export function routeReviewerOutcome(outcome, limits, pinnedHeadSha) {
|
|
|
366
366
|
// See routeDeveloperOutcome — the reviewer never spawned either (NOT-136).
|
|
367
367
|
return { next: "defer_work", reason: outcome.reason };
|
|
368
368
|
case "verdict":
|
|
369
|
+
if (outcome.finalReviewReason) {
|
|
370
|
+
return { next: "final_review", reason: outcome.finalReviewReason };
|
|
371
|
+
}
|
|
369
372
|
return routeVerdict(outcome.result, limits);
|
|
370
373
|
}
|
|
371
374
|
}
|
|
@@ -377,6 +377,25 @@ test("approved verdict with autoMerge routes to auto_merge instead of final_revi
|
|
|
377
377
|
};
|
|
378
378
|
assert.deepStrictEqual(routeReviewerOutcome(outcome, { ...REVIEW_ROUNDS_LEFT, autoMerge: true }, PINNED_HEAD), { next: "auto_merge" });
|
|
379
379
|
});
|
|
380
|
+
test("NOT-630: an incomplete truncated review forces final_review even with autoMerge", () => {
|
|
381
|
+
const outcome = {
|
|
382
|
+
kind: "verdict",
|
|
383
|
+
result: {
|
|
384
|
+
verdict: "escalated",
|
|
385
|
+
baseSha: "b",
|
|
386
|
+
headSha: "h",
|
|
387
|
+
acceptanceCriteriaAssessment: "incomplete",
|
|
388
|
+
evidenceAssessment: "visible diff reviewed",
|
|
389
|
+
findings: [],
|
|
390
|
+
risks: [],
|
|
391
|
+
},
|
|
392
|
+
finalReviewReason: "Diff exceeds the reviewer limit (600,000 characters); not reviewed: big.txt",
|
|
393
|
+
};
|
|
394
|
+
assert.deepStrictEqual(routeReviewerOutcome(outcome, { ...REVIEW_ROUNDS_LEFT, autoMerge: true }, PINNED_HEAD), {
|
|
395
|
+
next: "final_review",
|
|
396
|
+
reason: "Diff exceeds the reviewer limit (600,000 characters); not reviewed: big.txt",
|
|
397
|
+
});
|
|
398
|
+
});
|
|
380
399
|
test("changes_requested with rounds remaining retries the developer with findings", () => {
|
|
381
400
|
const outcome = {
|
|
382
401
|
kind: "verdict",
|
|
@@ -2,6 +2,39 @@ import { extractResultText, parseNdjsonFile } from "../runners/stream-json.js";
|
|
|
2
2
|
import { normalizeCodexEvents, parseCodexJsonl } from "../runners/codex-jsonl.js";
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
const EMPTY = { tokensIn: null, tokensOut: null, costUsd: null };
|
|
5
|
+
function asFiniteNumber(v) {
|
|
6
|
+
return typeof v === "number" && Number.isFinite(v) ? v : null;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Total input tokens for `usage_events.tokens_in` / execution reports.
|
|
10
|
+
*
|
|
11
|
+
* Providers disagree on whether cache is inside `input_tokens`:
|
|
12
|
+
* - Codex: `input_tokens` already includes `cached_input_tokens` (subset) — use input alone.
|
|
13
|
+
* - Claude / Cursor / Muse: uncached `input_tokens` plus cache read/write are disjoint — sum them.
|
|
14
|
+
*
|
|
15
|
+
* Without the sum, Claude sessions store ~tens of input tokens while cost/`tokens_out`
|
|
16
|
+
* look normal, so the report's TOKENS IN column reads as nonsense (e.g. 16.9K vs 7.7M out).
|
|
17
|
+
*/
|
|
18
|
+
export function totalTokensInFromUsage(usage, runtime) {
|
|
19
|
+
const input = asFiniteNumber(usage.input_tokens ?? usage.inputTokens);
|
|
20
|
+
if (input === null)
|
|
21
|
+
return null;
|
|
22
|
+
if (runtime === "codex_local")
|
|
23
|
+
return input;
|
|
24
|
+
const cacheRead = asFiniteNumber(usage.cache_read_input_tokens ?? usage.cacheReadTokens) ?? 0;
|
|
25
|
+
const cacheWrite = asFiniteNumber(usage.cache_creation_input_tokens ??
|
|
26
|
+
usage.cache_write_input_tokens ??
|
|
27
|
+
usage.cacheWriteTokens ??
|
|
28
|
+
usage.cacheCreationInputTokens) ?? 0;
|
|
29
|
+
return input + cacheRead + cacheWrite;
|
|
30
|
+
}
|
|
31
|
+
/** Muse's parsed usage: same additive cache rule as Claude (input excludes cache). */
|
|
32
|
+
export function totalTokensInFromMuseUsage(usage) {
|
|
33
|
+
const parts = [usage.inputTokens, usage.cacheReadTokens, usage.cacheWriteTokens];
|
|
34
|
+
if (parts.every((p) => p === null))
|
|
35
|
+
return null;
|
|
36
|
+
return parts.reduce((sum, p) => sum + (p ?? 0), 0);
|
|
37
|
+
}
|
|
5
38
|
/** Best-effort: a missing/unparseable log never blocks recording the session's duration. */
|
|
6
39
|
export function extractSpawnUsage(logPath, runtime) {
|
|
7
40
|
if (!fs.existsSync(logPath))
|
|
@@ -12,8 +45,8 @@ export function extractSpawnUsage(logPath, runtime) {
|
|
|
12
45
|
const result = events.find((e) => e.type === "result");
|
|
13
46
|
const usage = result?.usage ?? {};
|
|
14
47
|
return {
|
|
15
|
-
tokensIn: usage
|
|
16
|
-
tokensOut: usage.output_tokens ?? usage.outputTokens
|
|
48
|
+
tokensIn: totalTokensInFromUsage(usage, runtime),
|
|
49
|
+
tokensOut: asFiniteNumber(usage.output_tokens ?? usage.outputTokens),
|
|
17
50
|
costUsd: typeof result?.total_cost_usd === "number" ? result.total_cost_usd : null,
|
|
18
51
|
};
|
|
19
52
|
}
|
|
@@ -4,7 +4,7 @@ import assert from "node:assert/strict";
|
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { extractResultTranscript, extractSpawnUsage } from "./usage.js";
|
|
7
|
+
import { extractResultTranscript, extractSpawnUsage, totalTokensInFromMuseUsage, totalTokensInFromUsage, } from "./usage.js";
|
|
8
8
|
function writeLog(lines) {
|
|
9
9
|
const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), "dealer-usage-")), "session.ndjson");
|
|
10
10
|
fs.writeFileSync(file, lines.map((l) => JSON.stringify(l)).join("\n"));
|
|
@@ -21,15 +21,63 @@ test("extractSpawnUsage reads claude-shaped stream-json usage/cost", () => {
|
|
|
21
21
|
assert.equal(usage.tokensOut, 200);
|
|
22
22
|
assert.equal(usage.costUsd, 0.0421);
|
|
23
23
|
});
|
|
24
|
+
test("extractSpawnUsage sums Claude cache read/creation into tokensIn", () => {
|
|
25
|
+
// Real Claude Code result shape: input_tokens is uncached-only; almost all volume is cache.
|
|
26
|
+
const logPath = writeLog([
|
|
27
|
+
{
|
|
28
|
+
type: "result",
|
|
29
|
+
total_cost_usd: 0.700155,
|
|
30
|
+
usage: {
|
|
31
|
+
input_tokens: 22,
|
|
32
|
+
cache_creation_input_tokens: 55468,
|
|
33
|
+
cache_read_input_tokens: 629415,
|
|
34
|
+
output_tokens: 6522,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
]);
|
|
38
|
+
const usage = extractSpawnUsage(logPath, "claude_code");
|
|
39
|
+
assert.equal(usage.tokensIn, 22 + 55468 + 629415);
|
|
40
|
+
assert.equal(usage.tokensOut, 6522);
|
|
41
|
+
assert.equal(usage.costUsd, 0.700155);
|
|
42
|
+
});
|
|
43
|
+
test("extractSpawnUsage sums Cursor camelCase cache fields into tokensIn", () => {
|
|
44
|
+
const logPath = writeLog([
|
|
45
|
+
{
|
|
46
|
+
type: "result",
|
|
47
|
+
usage: {
|
|
48
|
+
inputTokens: 86839,
|
|
49
|
+
cacheReadTokens: 1314304,
|
|
50
|
+
cacheWriteTokens: 0,
|
|
51
|
+
outputTokens: 14718,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
const usage = extractSpawnUsage(logPath, "cursor_local");
|
|
56
|
+
assert.equal(usage.tokensIn, 86839 + 1314304);
|
|
57
|
+
assert.equal(usage.tokensOut, 14718);
|
|
58
|
+
});
|
|
24
59
|
test("extractSpawnUsage reads codex-shaped jsonl usage (no cost field)", () => {
|
|
25
60
|
const logPath = writeLog([
|
|
26
61
|
{ type: "turn.completed", usage: { input_tokens: 500, cached_input_tokens: 100, output_tokens: 50 } },
|
|
27
62
|
]);
|
|
28
63
|
const usage = extractSpawnUsage(logPath, "codex_local");
|
|
64
|
+
// Codex input_tokens already includes cached_input_tokens — do not double-count.
|
|
29
65
|
assert.equal(usage.tokensIn, 500);
|
|
30
66
|
assert.equal(usage.tokensOut, 50);
|
|
31
67
|
assert.equal(usage.costUsd, null);
|
|
32
68
|
});
|
|
69
|
+
test("totalTokensInFromUsage does not double-count Codex cache after normalize", () => {
|
|
70
|
+
// After normalizeCodexEvents, cached_input_tokens becomes cache_read_input_tokens on the result.
|
|
71
|
+
assert.equal(totalTokensInFromUsage({ input_tokens: 24763, cache_read_input_tokens: 24448, output_tokens: 122 }, "codex_local"), 24763);
|
|
72
|
+
});
|
|
73
|
+
test("totalTokensInFromMuseUsage sums disjoint cache fields", () => {
|
|
74
|
+
assert.equal(totalTokensInFromMuseUsage({
|
|
75
|
+
inputTokens: 17770,
|
|
76
|
+
cacheReadTokens: 5105,
|
|
77
|
+
cacheWriteTokens: 100,
|
|
78
|
+
}), 17770 + 5105 + 100);
|
|
79
|
+
assert.equal(totalTokensInFromMuseUsage({ inputTokens: null, cacheReadTokens: null, cacheWriteTokens: null }), null);
|
|
80
|
+
});
|
|
33
81
|
test("extractSpawnUsage returns nulls for a missing log file", () => {
|
|
34
82
|
const usage = extractSpawnUsage("/does/not/exist.ndjson", "claude_code");
|
|
35
83
|
assert.deepEqual(usage, { tokensIn: null, tokensOut: null, costUsd: null });
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// NOT-575: docs/API.md is the canonical guidance/resolve reference. Guards
|
|
2
|
+
// that it documents both payloads and that every stored choice per action
|
|
3
|
+
// type appears in it — the doc is generated from the same constants the
|
|
4
|
+
// server validates against, so drift fails here instead of surprising an
|
|
5
|
+
// operator at resolve time.
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import os from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
process.env.AGENT_DEALER_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "dealer-docs-api-"));
|
|
13
|
+
const { responseOptionsFor } = await import("./coordinator/commands.js");
|
|
14
|
+
const { PRODUCT_SCOPE_NOTE_MAX_CHARS } = await import("./coordinator/human-resolution.js");
|
|
15
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
16
|
+
const API_REF = "docs/API.md";
|
|
17
|
+
const read = () => fs.readFileSync(path.join(repoRoot, API_REF), "utf8");
|
|
18
|
+
test("the API reference exists and names the discovery fields", () => {
|
|
19
|
+
assert.ok(fs.existsSync(path.join(repoRoot, API_REF)));
|
|
20
|
+
const doc = read();
|
|
21
|
+
// The acceptance evidence: rg -n "responseOptions|resolvedBy" docs/API.md.
|
|
22
|
+
assert.match(doc, /responseOptions/);
|
|
23
|
+
assert.match(doc, /resolvedBy/);
|
|
24
|
+
});
|
|
25
|
+
test("the guidance payload is documented with its markdown field", () => {
|
|
26
|
+
const doc = read();
|
|
27
|
+
assert.match(doc, /POST \/api\/issues\/:id\/guidance/);
|
|
28
|
+
assert.match(doc, /"markdown"/);
|
|
29
|
+
});
|
|
30
|
+
test("the resolve payload is documented with resolvedBy, choice, and note", () => {
|
|
31
|
+
const doc = read();
|
|
32
|
+
assert.match(doc, /POST \/api\/human-actions\/:id\/resolve/);
|
|
33
|
+
assert.match(doc, /"resolvedBy"/);
|
|
34
|
+
assert.match(doc, /"choice"/);
|
|
35
|
+
assert.match(doc, /"note"/);
|
|
36
|
+
assert.ok(doc.includes(String(PRODUCT_SCOPE_NOTE_MAX_CHARS)), "the note cap must match code");
|
|
37
|
+
});
|
|
38
|
+
test("every stored choice per action type appears in the reference", () => {
|
|
39
|
+
const doc = read();
|
|
40
|
+
const stored = [
|
|
41
|
+
...responseOptionsFor("final_review"),
|
|
42
|
+
...responseOptionsFor("attempts_exhausted"),
|
|
43
|
+
...responseOptionsFor("policy_escalation"),
|
|
44
|
+
...responseOptionsFor("policy_escalation", false, { mergeFailure: true }),
|
|
45
|
+
...responseOptionsFor("policy_escalation", false, {
|
|
46
|
+
pushDivergence: {
|
|
47
|
+
branch: "b",
|
|
48
|
+
localSha: "l",
|
|
49
|
+
remoteSha: "r",
|
|
50
|
+
ahead: 1,
|
|
51
|
+
behind: 1,
|
|
52
|
+
relationship: "diverged",
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
55
|
+
...responseOptionsFor("product_scope_decision"),
|
|
56
|
+
...responseOptionsFor("operator_verification"),
|
|
57
|
+
];
|
|
58
|
+
for (const { choice } of stored) {
|
|
59
|
+
assert.ok(doc.includes(`\`${choice}\``), `missing stored choice: ${choice}`);
|
|
60
|
+
}
|
|
61
|
+
// Accepted but not stored: the final_review legacy synonym (VALID_CHOICES in
|
|
62
|
+
// human-resolution.ts) and the special-resolver sets (reflect-trigger.ts,
|
|
63
|
+
// approve-deliver.ts, commands.ts's muse branch, linear-merge-verify.ts).
|
|
64
|
+
for (const choice of [
|
|
65
|
+
"complete",
|
|
66
|
+
"retry",
|
|
67
|
+
"dismiss",
|
|
68
|
+
"retry_send",
|
|
69
|
+
"reject",
|
|
70
|
+
"acknowledge",
|
|
71
|
+
"recheck",
|
|
72
|
+
]) {
|
|
73
|
+
assert.ok(doc.includes(`\`${choice}\``), `missing accepted choice: ${choice}`);
|
|
74
|
+
}
|
|
75
|
+
// Every action type has a row.
|
|
76
|
+
for (const actionType of [
|
|
77
|
+
"final_review",
|
|
78
|
+
"attempts_exhausted",
|
|
79
|
+
"policy_escalation",
|
|
80
|
+
"product_scope_decision",
|
|
81
|
+
"deck_interaction_required",
|
|
82
|
+
"reflection_interaction_required",
|
|
83
|
+
"outbound_delivery_interaction_required",
|
|
84
|
+
"muse_capability",
|
|
85
|
+
"operator_verification",
|
|
86
|
+
]) {
|
|
87
|
+
assert.ok(doc.includes(`\`${actionType}\``), `missing action type: ${actionType}`);
|
|
88
|
+
}
|
|
89
|
+
});
|