@akagilnc/pi-workflow-roles 0.1.1751

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +104 -0
  3. package/README.zh-CN.md +133 -0
  4. package/THIRD_PARTY_NOTICES.md +60 -0
  5. package/dist/activation-ledger-git.js +68 -0
  6. package/dist/activation-ledger-session.js +120 -0
  7. package/dist/activation-ledger-topology.js +239 -0
  8. package/dist/activation-reconciliation.js +61 -0
  9. package/dist/audit-escalation.js +108 -0
  10. package/dist/auditor-dossier-tool.js +35 -0
  11. package/dist/canonical-json.js +78 -0
  12. package/dist/compliance-transport.js +77 -0
  13. package/dist/doctor-contracts.js +172 -0
  14. package/dist/dossier-resolution.js +103 -0
  15. package/dist/evidence-child-executor.js +661 -0
  16. package/dist/exact-utf8.js +12 -0
  17. package/dist/git-object-id.js +7 -0
  18. package/dist/in-process-session.js +50 -0
  19. package/dist/merger-contracts.js +76 -0
  20. package/dist/navigator-attendance.js +995 -0
  21. package/dist/navigator-invocation-identity.js +220 -0
  22. package/dist/open-tool-schema.js +39 -0
  23. package/dist/package-contracts/collector-output.js +50 -0
  24. package/dist/package-contracts/fixer-output.js +72 -0
  25. package/dist/package-contracts/fixer-packet.js +77 -0
  26. package/dist/package-contracts/judge-output.js +17 -0
  27. package/dist/package-contracts/reviewer-output.js +82 -0
  28. package/dist/package-contracts/terminating-tools.js +173 -0
  29. package/dist/package-contracts/worker-output.js +13 -0
  30. package/dist/package-owned-tool-idle.js +104 -0
  31. package/dist/packaged-role-registry.js +34 -0
  32. package/dist/public-cli/main.js +23867 -0
  33. package/dist/public-command-renderer.js +20 -0
  34. package/dist/reviewer-agent.js +93 -0
  35. package/dist/reviewer-child-executor.js +23 -0
  36. package/dist/reviewer-construction.js +95 -0
  37. package/dist/reviewer-dispatch.js +77 -0
  38. package/dist/reviewer-execution-ledger.js +160 -0
  39. package/dist/reviewer-failure-diagnostic.js +17 -0
  40. package/dist/reviewer-git-snapshot.js +38 -0
  41. package/dist/reviewer-pinned-git.js +146 -0
  42. package/dist/reviewer-preflight-error.js +15 -0
  43. package/dist/reviewer-prompt-identity.js +10 -0
  44. package/dist/reviewer-scope-prompt.js +21 -0
  45. package/dist/reviewer-workspace.js +151 -0
  46. package/dist/sha256.js +5 -0
  47. package/dist/sitian-record-entry.js +33 -0
  48. package/dist/stderr-jsonl.js +26 -0
  49. package/dist/stream-idle-guard.js +75 -0
  50. package/dist/tool-execution-observation.js +141 -0
  51. package/dist/uuidv7.js +21 -0
  52. package/dist/work-subject-identity.js +53 -0
  53. package/extensions/role-runtime.ts +303 -0
  54. package/package.json +69 -0
  55. package/packets/fixer-prerequisites.json +6 -0
  56. package/packets/fixer-repair.md +5 -0
  57. package/packets/judge-apply.md +77 -0
  58. package/packets/judge-authority.md +64 -0
  59. package/packets/judge-plan.md +55 -0
  60. package/packets/judge-review.md +49 -0
  61. package/packets/judge-submission.md +34 -0
  62. package/resources/methods/code-review/SKILL.md +92 -0
  63. package/resources/methods/code-review/agents/openai.yaml +3 -0
  64. package/resources/methods/code-review/provenance.json +26 -0
  65. package/resources/methods/diagnosing-bugs/SKILL.md +134 -0
  66. package/resources/methods/diagnosing-bugs/agents/openai.yaml +3 -0
  67. package/resources/methods/diagnosing-bugs/provenance.json +31 -0
  68. package/resources/methods/diagnosing-bugs/scripts/hitl-loop.template.sh +41 -0
  69. package/resources/methods/resolving-merge-conflicts/SKILL.md +14 -0
  70. package/resources/methods/resolving-merge-conflicts/agents/openai.yaml +3 -0
  71. package/resources/methods/resolving-merge-conflicts/provenance.json +26 -0
  72. package/resources/methods/tdd/SKILL.md +38 -0
  73. package/resources/methods/tdd/agents/openai.yaml +3 -0
  74. package/resources/methods/tdd/mocking.md +59 -0
  75. package/resources/methods/tdd/provenance.json +36 -0
  76. package/resources/methods/tdd/tests.md +77 -0
  77. package/resources/navigator-route-playbook.md +32 -0
  78. package/schemas/tool-execution-observation.schema.json +107 -0
  79. package/scripts/build-package.mjs +65 -0
  80. package/scripts/generate-tool-execution-observation-schema.ts +7 -0
  81. package/souls/coder.md +10 -0
  82. package/souls/collector.md +11 -0
  83. package/souls/doctor-auditor.md +23 -0
  84. package/souls/doctor.md +8 -0
  85. package/souls/fixer-auditor.md +33 -0
  86. package/souls/fixer.md +13 -0
  87. package/souls/judge-auditor.md +33 -0
  88. package/souls/judge.md +74 -0
  89. package/souls/merger.md +5 -0
  90. package/souls/navigator.md +5 -0
  91. package/souls/reviewer-auditor.md +25 -0
  92. package/souls/reviewer.md +11 -0
  93. package/src/activation-ledger-git.ts +96 -0
  94. package/src/activation-ledger-session.ts +188 -0
  95. package/src/activation-ledger-topology.ts +301 -0
  96. package/src/activation-ledger.ts +240 -0
  97. package/src/activation-reconciliation.ts +163 -0
  98. package/src/activation-trace.ts +38 -0
  99. package/src/audit-escalation.ts +177 -0
  100. package/src/auditor-dossier-tool.ts +48 -0
  101. package/src/auditor-soul.ts +28 -0
  102. package/src/canonical-json.ts +74 -0
  103. package/src/canonical-skill-binding.ts +107 -0
  104. package/src/collector-config.ts +89 -0
  105. package/src/collector-evidence.ts +461 -0
  106. package/src/collector-github.ts +656 -0
  107. package/src/collector-identity.ts +161 -0
  108. package/src/collector-ledger.ts +827 -0
  109. package/src/collector-receipt.ts +87 -0
  110. package/src/collector-role.ts +592 -0
  111. package/src/collector-tool-schemas.ts +19 -0
  112. package/src/compliance-transport.ts +130 -0
  113. package/src/doctor-auditor.ts +53 -0
  114. package/src/doctor-contracts.ts +166 -0
  115. package/src/doctor-evidence.ts +47 -0
  116. package/src/doctor-role.ts +18 -0
  117. package/src/dossier-resolution.ts +137 -0
  118. package/src/evidence-child-executor.ts +775 -0
  119. package/src/exact-utf8.ts +9 -0
  120. package/src/factory-board.ts +1822 -0
  121. package/src/git-object-id.ts +11 -0
  122. package/src/human-format.ts +65 -0
  123. package/src/in-process-session.ts +78 -0
  124. package/src/judge-auditor.ts +55 -0
  125. package/src/judge-recording-anti-forge.ts +53 -0
  126. package/src/judge-role.ts +160 -0
  127. package/src/merger-contracts.ts +71 -0
  128. package/src/merger-git-state.ts +76 -0
  129. package/src/merger-role.ts +60 -0
  130. package/src/navigator-attendance.ts +1254 -0
  131. package/src/navigator-invocation-identity.ts +446 -0
  132. package/src/open-tool-schema.ts +46 -0
  133. package/src/package-contracts/collector-output.ts +109 -0
  134. package/src/package-contracts/fixer-output.ts +81 -0
  135. package/src/package-contracts/fixer-packet.ts +93 -0
  136. package/src/package-contracts/judge-output.ts +39 -0
  137. package/src/package-contracts/reviewer-output.ts +115 -0
  138. package/src/package-contracts/terminating-tools.ts +259 -0
  139. package/src/package-contracts/worker-output.ts +36 -0
  140. package/src/package-owned-tool-idle.ts +134 -0
  141. package/src/package-resources/method-skill-binding.ts +87 -0
  142. package/src/package-resources/method-skill.ts +358 -0
  143. package/src/packaged-role-registry.ts +36 -0
  144. package/src/public-cli/cli-errors.ts +11 -0
  145. package/src/public-cli/cli-io.ts +4 -0
  146. package/src/public-cli/cli.ts +912 -0
  147. package/src/public-cli/coder-run.ts +575 -0
  148. package/src/public-cli/collector-run.ts +375 -0
  149. package/src/public-cli/command-renderer.ts +8 -0
  150. package/src/public-cli/config.ts +346 -0
  151. package/src/public-cli/doctor-run.ts +355 -0
  152. package/src/public-cli/explicit-internal.ts +274 -0
  153. package/src/public-cli/fixer-run.ts +587 -0
  154. package/src/public-cli/host-pi-runtime.ts +112 -0
  155. package/src/public-cli/invocation.ts +1958 -0
  156. package/src/public-cli/judge-run.ts +507 -0
  157. package/src/public-cli/main.ts +15 -0
  158. package/src/public-cli/merger-run.ts +681 -0
  159. package/src/public-cli/public-run-credentials.ts +71 -0
  160. package/src/public-cli/registry.ts +153 -0
  161. package/src/public-cli/reviewer-run.ts +561 -0
  162. package/src/public-cli/run-lifecycle.ts +884 -0
  163. package/src/public-cli/settlement.ts +3765 -0
  164. package/src/public-cli/terminal.ts +325 -0
  165. package/src/public-command-renderer.ts +43 -0
  166. package/src/reviewer-agent.ts +94 -0
  167. package/src/reviewer-auditor.ts +53 -0
  168. package/src/reviewer-child-executor.ts +31 -0
  169. package/src/reviewer-construction.ts +137 -0
  170. package/src/reviewer-dispatch.ts +94 -0
  171. package/src/reviewer-execution-ledger.ts +206 -0
  172. package/src/reviewer-failure-diagnostic.ts +18 -0
  173. package/src/reviewer-git-snapshot.ts +53 -0
  174. package/src/reviewer-pinned-git.ts +144 -0
  175. package/src/reviewer-preflight-error.ts +14 -0
  176. package/src/reviewer-prompt-identity.ts +17 -0
  177. package/src/reviewer-role.ts +193 -0
  178. package/src/reviewer-scope-prompt.ts +24 -0
  179. package/src/reviewer-settlement.ts +63 -0
  180. package/src/reviewer-workspace.ts +111 -0
  181. package/src/role-runtime.ts +884 -0
  182. package/src/sha256.ts +6 -0
  183. package/src/sitian-record-entry.ts +57 -0
  184. package/src/stderr-jsonl.ts +28 -0
  185. package/src/stream-idle-guard.ts +98 -0
  186. package/src/ticket-snapshot.ts +662 -0
  187. package/src/ticket-trajectory.ts +1000 -0
  188. package/src/tool-execution-observation.ts +168 -0
  189. package/src/uuidv7.ts +1 -0
  190. package/src/work-subject-identity.ts +94 -0
  191. package/src/worker-role.ts +434 -0
  192. package/src/worker-submission-gates.ts +225 -0
@@ -0,0 +1,561 @@
1
+ /**
2
+ * Public Reviewer Role run: admit → explicit Internal
3
+ * activate → settle Terminal result (#111).
4
+ * Package-owned adapted code-review method is forced; users never submit
5
+ * extra packets. Controlled-failure settlement reuses #107.
6
+ */
7
+ import { writeFile } from "node:fs/promises";
8
+ import { join } from "node:path";
9
+
10
+ import {
11
+ loadPackagedMethodSkillMaterial,
12
+ resolvePackagedMethodSkillPath,
13
+ type PackagedMethodSkillMaterial,
14
+ type PackagedMethodSkillProvenance,
15
+ } from "../package-resources/method-skill.ts";
16
+ import {
17
+ runExplicitInternalActivation,
18
+ type ExplicitInternalKnownFailure,
19
+ type ExplicitInternalPiRunner,
20
+ type ExplicitInternalPiResult,
21
+ } from "./explicit-internal.ts";
22
+ import { CliUsageError } from "./cli-errors.ts";
23
+ import {
24
+ admitReviewerInvocation,
25
+ buildReviewerTransportPrompt,
26
+ type AdmittedReviewerInvocation,
27
+ } from "./invocation.ts";
28
+ import {
29
+ type CredentialProviders,
30
+ type SeatModelConfig,
31
+ } from "./config.ts";
32
+ import {
33
+ missingCredentialPreDispatchFailure,
34
+ postRunMissingCredentialFailure,
35
+ } from "./public-run-credentials.ts";
36
+ import {
37
+ acquireRunWriterLease,
38
+ clearTypedProviderHttpObservation,
39
+ isSessionPrincipalAvailable,
40
+ isV1ResumableFailure,
41
+ loadResumableReviewerRun,
42
+ markRunAdmitted,
43
+ markRunResumable,
44
+ markRunRunning,
45
+ markRunTerminal,
46
+ readTypedHttp429Observation,
47
+ renderResumeCommand,
48
+ RESUME_TRANSPORT_ENVELOPE,
49
+ RunWriterLeaseHeldError,
50
+ type RunWriterLease,
51
+ } from "./run-lifecycle.ts";
52
+ import {
53
+ classifyPostAdmissionFailure,
54
+ exitCodeForTerminalOutcome,
55
+ formatCliDiagnostic,
56
+ formatTerminalResult,
57
+ hasLawfulReviewerTerminalResult,
58
+ inspectJudgeSession,
59
+ isLawfulTypedTerminalOutcome,
60
+ presentFailureTerminal,
61
+ presentStructuralRejection,
62
+ resolveAuditedRunnerKnownFailure,
63
+ explicitInternalKnownFailureClassificationInput,
64
+ settleFailureTerminalResult,
65
+ trySettleReviewerTerminalResult,
66
+ trySettleComplianceAuditIncompleteTerminalResult,
67
+ } from "./settlement.ts";
68
+ import type { CliIo } from "./cli-io.ts";
69
+ import type {
70
+ ControlledFailureCause,
71
+ TerminalResult,
72
+ } from "./terminal.ts";
73
+
74
+ export type ReviewerRunEnv = {
75
+ home: string;
76
+ agentDir: string;
77
+ packageRoot: string;
78
+ cwd: string;
79
+ correlationId?: string;
80
+ piRunner?: ExplicitInternalPiRunner;
81
+ model?: SeatModelConfig;
82
+ credentials?: CredentialProviders;
83
+ createRunId?: () => string;
84
+ extraPiArgs?: readonly string[];
85
+ timeoutMs?: number;
86
+ };
87
+
88
+ function buildModelArgs(model: SeatModelConfig | undefined): string[] {
89
+ if (model === undefined) return [];
90
+ return [
91
+ "--provider",
92
+ model.provider,
93
+ "--model",
94
+ model.model,
95
+ "--thinking",
96
+ model.thinking,
97
+ ];
98
+ }
99
+
100
+ /**
101
+ * Build Internal activation extra-args for an admitted Reviewer run.
102
+ * Package code-review Skill is forced via --skill; ambient home skills stay off.
103
+ * Capabilities path is adapter-derived at admission — never a caller packet.
104
+ */
105
+ export function buildReviewerActivationExtraArgs(
106
+ admitted: AdmittedReviewerInvocation,
107
+ options: {
108
+ packageRoot: string;
109
+ model?: SeatModelConfig;
110
+ extraPiArgs?: readonly string[];
111
+ },
112
+ ): string[] {
113
+ const prompt = buildReviewerTransportPrompt(admitted);
114
+ const skillPath = resolvePackagedMethodSkillPath(
115
+ options.packageRoot,
116
+ "code-review",
117
+ );
118
+ return [
119
+ "--no-skills",
120
+ "--skill",
121
+ skillPath,
122
+ "--no-prompt-templates",
123
+ "--no-themes",
124
+ "--no-context-files",
125
+ "--session",
126
+ admitted.sessionFile,
127
+ "--session-dir",
128
+ admitted.sessionDirectory,
129
+ ...(options.extraPiArgs ?? []),
130
+ "--ak-role",
131
+ "reviewer",
132
+ "--ak-review-base",
133
+ admitted.baseRevision,
134
+ "--mode",
135
+ "json",
136
+ ...buildModelArgs(options.model),
137
+ prompt,
138
+ ];
139
+ }
140
+
141
+ /**
142
+ * Reopen the exact Reviewer Pi session for resume. Preserves fixed Reviewer base
143
+ * and package code-review binding; never resubmits caller instruction as control.
144
+ */
145
+ export function buildReviewerResumeActivationExtraArgs(
146
+ admitted: AdmittedReviewerInvocation,
147
+ options: {
148
+ packageRoot: string;
149
+ model?: SeatModelConfig;
150
+ extraPiArgs?: readonly string[];
151
+ },
152
+ ): string[] {
153
+ const skillPath = resolvePackagedMethodSkillPath(
154
+ options.packageRoot,
155
+ "code-review",
156
+ );
157
+ return [
158
+ "--no-skills",
159
+ "--skill",
160
+ skillPath,
161
+ "--no-prompt-templates",
162
+ "--no-themes",
163
+ "--no-context-files",
164
+ "--session",
165
+ admitted.sessionFile,
166
+ "--session-dir",
167
+ admitted.sessionDirectory,
168
+ ...(options.extraPiArgs ?? []),
169
+ "--ak-role",
170
+ "reviewer",
171
+ "--ak-review-base",
172
+ admitted.baseRevision,
173
+ "--mode",
174
+ "json",
175
+ ...buildModelArgs(options.model),
176
+ RESUME_TRANSPORT_ENVELOPE,
177
+ ];
178
+ }
179
+
180
+ async function presentControlledFailure(
181
+ admitted: AdmittedReviewerInvocation,
182
+ failureInput: {
183
+ timedOut: boolean;
184
+ code: number | null;
185
+ stderr: string;
186
+ thrown?: unknown;
187
+ knownFailure?: ExplicitInternalKnownFailure;
188
+ },
189
+ io: CliIo,
190
+ ): Promise<{
191
+ exitCode: number;
192
+ admitted: AdmittedReviewerInvocation;
193
+ terminal: TerminalResult;
194
+ }> {
195
+ const hasThrown = Object.hasOwn(failureInput, "thrown");
196
+ const session =
197
+ !hasThrown &&
198
+ !failureInput.timedOut &&
199
+ failureInput.knownFailure === undefined
200
+ ? await inspectJudgeSession(admitted.sessionFile)
201
+ : undefined;
202
+ const failure = classifyPostAdmissionFailure({
203
+ timedOut: failureInput.timedOut,
204
+ code: failureInput.code,
205
+ stderr: failureInput.stderr,
206
+ ...(hasThrown ? { thrown: failureInput.thrown } : {}),
207
+ ...explicitInternalKnownFailureClassificationInput(failureInput.knownFailure),
208
+ ...(session === undefined ? {} : { session }),
209
+ });
210
+
211
+ const hasLawfulTerminalResult = await hasLawfulReviewerTerminalResult(admitted);
212
+ const typedHttp429 = await readTypedHttp429Observation(admitted.runDirectory);
213
+ const sessionPrincipalAvailable = await isSessionPrincipalAvailable(
214
+ admitted.sessionFile,
215
+ );
216
+ const resumable =
217
+ sessionPrincipalAvailable &&
218
+ isV1ResumableFailure({
219
+ hasLawfulTerminalResult,
220
+ ...(typedHttp429 === undefined ? {} : { typedHttp429 }),
221
+ });
222
+ if (resumable && typedHttp429 !== undefined) {
223
+ await markRunResumable(admitted.runDirectory, typedHttp429);
224
+ } else {
225
+ await markRunTerminal(admitted.runDirectory).catch(() => undefined);
226
+ }
227
+
228
+ const terminal = await settleFailureTerminalResult(
229
+ admitted,
230
+ failure,
231
+ resumable
232
+ ? { resume: { command: renderResumeCommand(admitted.runId) } }
233
+ : {},
234
+ );
235
+ presentFailureTerminal(terminal, io);
236
+ return {
237
+ exitCode: exitCodeForTerminalOutcome(terminal.roleOutcome),
238
+ admitted,
239
+ terminal,
240
+ };
241
+ }
242
+
243
+ async function dispatchAdmittedReviewer(input: {
244
+ admitted: AdmittedReviewerInvocation;
245
+ env: ReviewerRunEnv;
246
+ io: CliIo;
247
+ extraArgs: string[];
248
+ lease: RunWriterLease;
249
+ methodMaterial: PackagedMethodSkillMaterial;
250
+ }): Promise<{
251
+ exitCode: number;
252
+ admitted: AdmittedReviewerInvocation;
253
+ terminal?: TerminalResult;
254
+ }> {
255
+ const { admitted, env, io, extraArgs, lease, methodMaterial } = input;
256
+ try {
257
+ const missingCredential = missingCredentialPreDispatchFailure(
258
+ env.model,
259
+ env.credentials,
260
+ );
261
+ if (missingCredential !== undefined) {
262
+ return await presentControlledFailure(
263
+ admitted,
264
+ missingCredential,
265
+ io,
266
+ );
267
+ }
268
+ await markRunRunning(admitted.runDirectory);
269
+ await clearTypedProviderHttpObservation(admitted.runDirectory);
270
+
271
+ const childEnv: NodeJS.ProcessEnv = {
272
+ ...process.env,
273
+ HOME: env.home,
274
+ PI_CODING_AGENT_DIR: env.agentDir,
275
+ AK_ROLE_RUN_DIR: admitted.runDirectory,
276
+ };
277
+ if (env.correlationId !== undefined && env.correlationId.trim() !== "") {
278
+ childEnv.AK_CORRELATION_ID = env.correlationId;
279
+ }
280
+
281
+ let result: ExplicitInternalPiResult;
282
+ try {
283
+ result = await runExplicitInternalActivation({
284
+ packageRoot: env.packageRoot,
285
+ extraArgs,
286
+ cwd: admitted.projectRoot,
287
+ home: env.home,
288
+ agentDir: env.agentDir,
289
+ env: childEnv,
290
+ timeoutMs: env.timeoutMs,
291
+ ...(env.piRunner === undefined ? {} : { runner: env.piRunner }),
292
+ });
293
+ } catch (error) {
294
+ return await presentControlledFailure(
295
+ admitted,
296
+ {
297
+ timedOut: false,
298
+ code: null,
299
+ stderr: "",
300
+ thrown: error,
301
+ },
302
+ io,
303
+ );
304
+ }
305
+
306
+ try {
307
+ await writeFile(
308
+ join(admitted.runDirectory, "stderr.log"),
309
+ result.stderr,
310
+ "utf8",
311
+ );
312
+ } catch {
313
+ // continue to lawful / controlled-failure settlement
314
+ }
315
+
316
+ let lawful: TerminalResult | undefined;
317
+ try {
318
+ lawful = await trySettleReviewerTerminalResult(admitted, {
319
+ methodProvenance: methodMaterial.provenance,
320
+ methodSkillPath: methodMaterial.skillPath,
321
+ methodSkillConfiguredPath: resolvePackagedMethodSkillPath(
322
+ env.packageRoot,
323
+ "code-review",
324
+ ),
325
+ });
326
+ } catch (error) {
327
+ return await presentControlledFailure(
328
+ admitted,
329
+ {
330
+ timedOut: false,
331
+ code: result.code,
332
+ stderr: result.stderr,
333
+ thrown: error,
334
+ },
335
+ io,
336
+ );
337
+ }
338
+ if (lawful !== undefined && isLawfulTypedTerminalOutcome(lawful.roleOutcome)) {
339
+ await markRunTerminal(admitted.runDirectory).catch(() => undefined);
340
+ io.stdout(formatTerminalResult(lawful));
341
+ return {
342
+ exitCode: exitCodeForTerminalOutcome(lawful.roleOutcome),
343
+ admitted,
344
+ terminal: lawful,
345
+ };
346
+ }
347
+
348
+ const auditIncomplete = await trySettleComplianceAuditIncompleteTerminalResult(admitted);
349
+ if (auditIncomplete !== undefined) {
350
+ await markRunTerminal(admitted.runDirectory).catch(() => undefined);
351
+ if (auditIncomplete.roleOutcome.kind === "failure") {
352
+ presentFailureTerminal(auditIncomplete, io);
353
+ } else {
354
+ io.stdout(formatTerminalResult(auditIncomplete));
355
+ }
356
+ return {
357
+ exitCode: exitCodeForTerminalOutcome(auditIncomplete.roleOutcome),
358
+ admitted,
359
+ terminal: auditIncomplete,
360
+ };
361
+ }
362
+
363
+ const credentialFailure = postRunMissingCredentialFailure(
364
+ result,
365
+ env.model,
366
+ env.credentials,
367
+ );
368
+ const knownFailure = await resolveAuditedRunnerKnownFailure({
369
+ runner: result.knownFailure,
370
+ sessionFile: admitted.sessionFile,
371
+ credential: credentialFailure,
372
+ });
373
+ return await presentControlledFailure(
374
+ admitted,
375
+ {
376
+ timedOut: result.timedOut,
377
+ code: result.code,
378
+ stderr: result.stderr,
379
+ ...(knownFailure === undefined ? {} : { knownFailure }),
380
+ },
381
+ io,
382
+ );
383
+ } finally {
384
+ await lease.release();
385
+ }
386
+ }
387
+
388
+ async function loadReviewerMethodMaterial(
389
+ packageRoot: string,
390
+ ): Promise<PackagedMethodSkillMaterial> {
391
+ return await loadPackagedMethodSkillMaterial(packageRoot, "code-review");
392
+ }
393
+
394
+ export async function runPublicReviewer(
395
+ argv: readonly string[],
396
+ env: ReviewerRunEnv,
397
+ io: CliIo,
398
+ parseReviewerArgv: (args: readonly string[]) => {
399
+ instruction: string;
400
+ attachmentPaths: string[];
401
+ baseRevision: string;
402
+ project?: string;
403
+ },
404
+ ): Promise<{
405
+ exitCode: number;
406
+ admitted?: AdmittedReviewerInvocation;
407
+ terminal?: TerminalResult;
408
+ }> {
409
+ let admitted: AdmittedReviewerInvocation;
410
+ try {
411
+ const parsed = parseReviewerArgv(argv);
412
+ admitted = await admitReviewerInvocation({
413
+ home: env.home,
414
+ cwd: env.cwd,
415
+ instruction: parsed.instruction,
416
+ attachmentPaths: parsed.attachmentPaths,
417
+ baseRevision: parsed.baseRevision,
418
+ ...(parsed.project === undefined ? {} : { project: parsed.project }),
419
+ ...(env.createRunId === undefined ? {} : { createRunId: env.createRunId }),
420
+ });
421
+ } catch (error) {
422
+ if (error instanceof CliUsageError) {
423
+ presentStructuralRejection(error, io);
424
+ return { exitCode: 2 };
425
+ }
426
+ throw error;
427
+ }
428
+
429
+ await markRunAdmitted(admitted);
430
+
431
+ let lease: RunWriterLease;
432
+ try {
433
+ lease = await acquireRunWriterLease(admitted.runDirectory);
434
+ } catch (error) {
435
+ if (error instanceof RunWriterLeaseHeldError) {
436
+ presentStructuralRejection(error, io);
437
+ return { exitCode: 2 };
438
+ }
439
+ throw error;
440
+ }
441
+
442
+ let methodMaterial: PackagedMethodSkillMaterial;
443
+ try {
444
+ methodMaterial = await loadReviewerMethodMaterial(env.packageRoot);
445
+ } catch (error) {
446
+ await lease.release();
447
+ return await presentControlledFailure(
448
+ admitted,
449
+ {
450
+ timedOut: false,
451
+ code: null,
452
+ stderr: "",
453
+ thrown: error,
454
+ },
455
+ io,
456
+ );
457
+ }
458
+
459
+ const extraArgs = buildReviewerActivationExtraArgs(admitted, {
460
+ packageRoot: env.packageRoot,
461
+ ...(env.model === undefined ? {} : { model: env.model }),
462
+ ...(env.extraPiArgs === undefined ? {} : { extraPiArgs: env.extraPiArgs }),
463
+ });
464
+
465
+ return await dispatchAdmittedReviewer({
466
+ admitted,
467
+ env,
468
+ io,
469
+ extraArgs,
470
+ lease,
471
+ methodMaterial,
472
+ });
473
+ }
474
+
475
+ /**
476
+ * Resume a previously admitted Reviewer Role run after a typed HTTP 429.
477
+ * Restores task/base/session identity; model override is temporary.
478
+ */
479
+ export async function runPublicReviewerResume(
480
+ argv: readonly string[],
481
+ env: ReviewerRunEnv,
482
+ io: CliIo,
483
+ ): Promise<{
484
+ exitCode: number;
485
+ admitted?: AdmittedReviewerInvocation;
486
+ terminal?: TerminalResult;
487
+ }> {
488
+ const runId = argv[0];
489
+ if (runId === undefined || runId.trim() === "" || runId.startsWith("-")) {
490
+ presentStructuralRejection(
491
+ new CliUsageError("usage: ak-role resume <runId>"),
492
+ io,
493
+ );
494
+ return { exitCode: 2 };
495
+ }
496
+ if (argv.length > 1) {
497
+ presentStructuralRejection(
498
+ new CliUsageError("resume takes exactly one run id"),
499
+ io,
500
+ );
501
+ return { exitCode: 2 };
502
+ }
503
+
504
+ let loaded;
505
+ try {
506
+ loaded = await loadResumableReviewerRun(env.home, runId);
507
+ } catch (error) {
508
+ if (error instanceof CliUsageError) {
509
+ presentStructuralRejection(error, io);
510
+ return { exitCode: 2 };
511
+ }
512
+ throw error;
513
+ }
514
+
515
+ const { admitted } = loaded;
516
+
517
+ let lease: RunWriterLease;
518
+ try {
519
+ lease = await acquireRunWriterLease(admitted.runDirectory);
520
+ } catch (error) {
521
+ if (error instanceof RunWriterLeaseHeldError) {
522
+ io.stderr(formatCliDiagnostic(error.message));
523
+ return { exitCode: 1 };
524
+ }
525
+ throw error;
526
+ }
527
+
528
+ let methodMaterial: PackagedMethodSkillMaterial;
529
+ try {
530
+ methodMaterial = await loadReviewerMethodMaterial(env.packageRoot);
531
+ } catch (error) {
532
+ await lease.release();
533
+ return await presentControlledFailure(
534
+ admitted,
535
+ {
536
+ timedOut: false,
537
+ code: null,
538
+ stderr: "",
539
+ thrown: error,
540
+ },
541
+ io,
542
+ );
543
+ }
544
+
545
+ const extraArgs = buildReviewerResumeActivationExtraArgs(admitted, {
546
+ packageRoot: env.packageRoot,
547
+ ...(env.model === undefined ? {} : { model: env.model }),
548
+ ...(env.extraPiArgs === undefined ? {} : { extraPiArgs: env.extraPiArgs }),
549
+ });
550
+
551
+ return await dispatchAdmittedReviewer({
552
+ admitted,
553
+ env,
554
+ io,
555
+ extraArgs,
556
+ lease,
557
+ methodMaterial,
558
+ });
559
+ }
560
+
561
+ export type { ExplicitInternalKnownFailure, PackagedMethodSkillProvenance };