@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,274 @@
1
+ /**
2
+ * ak-role-owned one-invocation explicit Internal activation (ADR 0052 / #105).
3
+ * Ordinary Pi package auto-registration does not load the role runtime; only
4
+ * this adapter (or an intentional developer `pi -e`) crosses that boundary.
5
+ */
6
+ import { execFile, spawn } from "node:child_process";
7
+ import { constants } from "node:fs";
8
+ import { access, realpath } from "node:fs/promises";
9
+ import { delimiter, isAbsolute, join, resolve } from "node:path";
10
+ import { platform } from "node:process";
11
+ import { promisify } from "node:util";
12
+
13
+ import { recordLaunchedPiIdentity } from "./invocation.ts";
14
+ import { INTERNAL_ROLE_ENTRYPOINT_RELATIVE } from "./registry.ts";
15
+ import type { ControlledFailureCause } from "./terminal.ts";
16
+
17
+ export function resolveInternalRoleEntrypoint(packageRoot: string): string {
18
+ return join(packageRoot, INTERNAL_ROLE_ENTRYPOINT_RELATIVE);
19
+ }
20
+
21
+ /**
22
+ * Explicit one-invocation Internal activation args for the installed package copy.
23
+ * Ordinary Pi package auto-registration does not include this entrypoint (ADR 0052).
24
+ */
25
+ export function buildExplicitInternalActivationArgs(
26
+ packageRoot: string,
27
+ extraArgs: readonly string[] = [],
28
+ ): string[] {
29
+ return [
30
+ "--no-extensions",
31
+ "-e",
32
+ resolveInternalRoleEntrypoint(packageRoot),
33
+ ...extraArgs,
34
+ ];
35
+ }
36
+
37
+ /** Production-owned typed failure carried on a resolved runner result. */
38
+ export type ExplicitInternalKnownFailure = {
39
+ readonly cause: ControlledFailureCause;
40
+ readonly identity?: {
41
+ readonly name?: string;
42
+ readonly code?: string | number;
43
+ };
44
+ /**
45
+ * Optional diagnostic already owned by a typed production field (e.g. session
46
+ * assistant errorMessage). Settlement prefers this over child stderr selection.
47
+ */
48
+ readonly diagnostic?: string;
49
+ /** Secondary evidence attached to the same typed failure record. */
50
+ readonly details?: Readonly<Record<string, unknown>>;
51
+ };
52
+
53
+ /**
54
+ * Produce a typed provider knownFailure from a native session assistant stop.
55
+ * Source fields are session-typed (stopReason / errorMessage / provider) — never
56
+ * child stderr prose. Used by the public classifier after a real Pi child exits.
57
+ */
58
+ export function knownFailureFromProviderStop(input: {
59
+ readonly stopReason?: string;
60
+ readonly errorMessage?: string | null;
61
+ readonly provider?: string;
62
+ readonly model?: string;
63
+ }): ExplicitInternalKnownFailure | undefined {
64
+ if (input.stopReason !== "error") return undefined;
65
+ const diagnostic =
66
+ typeof input.errorMessage === "string" && input.errorMessage.trim() !== ""
67
+ ? input.errorMessage.trim()
68
+ : "provider failure";
69
+ const identity: { name: string; code?: string } = {
70
+ name: "ProviderStopError",
71
+ };
72
+ if (typeof input.provider === "string" && input.provider.trim() !== "") {
73
+ identity.code = input.provider;
74
+ } else if (typeof input.model === "string" && input.model.trim() !== "") {
75
+ identity.code = input.model;
76
+ }
77
+ return {
78
+ cause: "provider",
79
+ identity,
80
+ diagnostic,
81
+ };
82
+ }
83
+
84
+ export type ExplicitInternalPiResult = {
85
+ code: number | null;
86
+ stderr: string;
87
+ timedOut: boolean;
88
+ /** Full argv passed to the Pi process (includes explicit -e load). */
89
+ args: string[];
90
+ /** Canonical identity of the executable selected and launched by this runner. */
91
+ piIdentity?: { executable: string; version: string };
92
+ /**
93
+ * Production-owned typed failure channel. Set only when the runner already
94
+ * knows the cause without stderr-prose inference. Settlement trusts this over
95
+ * the nonzero→activation default.
96
+ */
97
+ knownFailure?: ExplicitInternalKnownFailure;
98
+ };
99
+
100
+ /**
101
+ * Thrown activation failure with a production-owned typed cause.
102
+ * Prefer this over ad-hoc Error property tags so settlement retains typed identity.
103
+ */
104
+ export class ExplicitInternalActivationError extends Error {
105
+ readonly knownCause: ControlledFailureCause;
106
+ readonly failureCode?: string | number;
107
+
108
+ constructor(
109
+ message: string,
110
+ options: {
111
+ knownCause: ControlledFailureCause;
112
+ code?: string | number;
113
+ name?: string;
114
+ cause?: unknown;
115
+ },
116
+ ) {
117
+ super(
118
+ message,
119
+ options.cause === undefined ? undefined : { cause: options.cause },
120
+ );
121
+ this.name = options.name ?? "ExplicitInternalActivationError";
122
+ this.knownCause = options.knownCause;
123
+ if (options.code !== undefined) {
124
+ this.failureCode = options.code;
125
+ }
126
+ }
127
+ }
128
+
129
+ export type ExplicitInternalPiRunner = (
130
+ args: readonly string[],
131
+ options: {
132
+ cwd: string;
133
+ env: NodeJS.ProcessEnv;
134
+ timeoutMs?: number;
135
+ },
136
+ ) => Promise<ExplicitInternalPiResult>;
137
+
138
+ const execFileAsync = promisify(execFile);
139
+
140
+ async function resolveSelectedPi(command: string, cwd: string, env: NodeJS.ProcessEnv): Promise<string> {
141
+ // Match child_process.spawn lookup: path-like commands and every relative or
142
+ // empty PATH entry are interpreted from the child's cwd. When PATH is absent,
143
+ // Node uses the platform search default rather than an empty search list.
144
+ const searchPath = env.PATH ?? (platform === "win32" ? (process.env.PATH ?? "") : "/usr/bin:/bin");
145
+ const candidates = isAbsolute(command) || command.includes("/")
146
+ ? [resolve(cwd, command)]
147
+ : searchPath.split(delimiter).map((dir) => resolve(cwd, dir, command));
148
+ for (const candidate of candidates) {
149
+ try {
150
+ await access(candidate, constants.X_OK);
151
+ } catch (error) {
152
+ const code = (error as NodeJS.ErrnoException).code;
153
+ if (code === "ENOENT" || code === "ENOTDIR" || code === "EACCES") continue;
154
+ throw error;
155
+ }
156
+ // Once a candidate qualifies, canonicalization failures are real filesystem
157
+ // failures, not evidence that PATH contained no executable.
158
+ return await realpath(candidate);
159
+ }
160
+ throw new Error(`Pi executable not found: ${command}`);
161
+ }
162
+
163
+ async function selectedPiIdentity(command: string, cwd: string, env: NodeJS.ProcessEnv): Promise<{ executable: string; version: string }> {
164
+ const executable = await resolveSelectedPi(command, cwd, env);
165
+ const { stdout } = await execFileAsync(executable, ["--version"], {
166
+ cwd,
167
+ env,
168
+ encoding: "utf8",
169
+ });
170
+ const version = stdout.trim();
171
+ if (version === "") throw new Error(`Pi executable returned an empty version: ${executable}`);
172
+ return { executable, version };
173
+ }
174
+
175
+ /** Default runner: canonically select `pi` on PATH (or PI_BINARY) and launch that exact file. */
176
+ export const defaultExplicitInternalPiRunner: ExplicitInternalPiRunner = async (
177
+ args,
178
+ options,
179
+ ) => {
180
+ const command = options.env.PI_BINARY ?? "pi";
181
+ const piIdentity = await selectedPiIdentity(command, options.cwd, options.env);
182
+ return await new Promise((resolveResult, reject) => {
183
+ // Child stdout is discarded at the stdio seam (CLAUDE.md Role invocation
184
+ // evidence). Do not pipe or accumulate it. stderr stays piped for diagnostics.
185
+ const child = spawn(piIdentity.executable, [...args], {
186
+ cwd: options.cwd,
187
+ env: options.env,
188
+ stdio: ["ignore", "ignore", "pipe"],
189
+ });
190
+ let stderr = "";
191
+ let timedOut = false;
192
+ // No default wall clock. Only an explicit caller budget arms a timer (ADR 0010).
193
+ // SIGKILL is unconditionally forbidden (host constitution art. 9) — graceful SIGTERM only.
194
+ let timer: ReturnType<typeof setTimeout> | undefined;
195
+ const armTimeoutAfterChildReady = (): void => {
196
+ if (options.timeoutMs === undefined) return;
197
+ timer = setTimeout(() => {
198
+ timedOut = true;
199
+ child.kill("SIGTERM");
200
+ }, options.timeoutMs);
201
+ };
202
+ // The spawn event is the child-process readiness seam. Start the caller's
203
+ // budget only after the child is actually created, not while spawn is pending.
204
+ let identityRecorded: Promise<void> = Promise.resolve();
205
+ child.once("spawn", () => {
206
+ armTimeoutAfterChildReady();
207
+ const runDirectory = options.env.AK_ROLE_RUN_DIR;
208
+ if (typeof runDirectory === "string" && runDirectory !== "") {
209
+ identityRecorded = recordLaunchedPiIdentity(runDirectory, piIdentity);
210
+ }
211
+ });
212
+ child.stderr.setEncoding("utf8").on("data", (chunk) => {
213
+ stderr += chunk;
214
+ });
215
+ child.on("error", (error) => {
216
+ if (timer !== undefined) clearTimeout(timer);
217
+ reject(error);
218
+ });
219
+ child.on("close", (code) => {
220
+ if (timer !== undefined) clearTimeout(timer);
221
+ void identityRecorded.then(
222
+ () => resolveResult({
223
+ code,
224
+ stderr,
225
+ timedOut,
226
+ args: [...args],
227
+ piIdentity,
228
+ }),
229
+ reject,
230
+ );
231
+ });
232
+ });
233
+ };
234
+
235
+ /**
236
+ * Spawn Pi once with `--no-extensions -e <packageRoot>/extensions/role-runtime.ts`
237
+ * plus caller args. Used by ak-role so the public CLI owns the load boundary.
238
+ */
239
+ export async function runExplicitInternalActivation(options: {
240
+ packageRoot: string;
241
+ extraArgs?: readonly string[];
242
+ cwd: string;
243
+ home: string;
244
+ agentDir: string;
245
+ env?: NodeJS.ProcessEnv;
246
+ timeoutMs?: number | undefined;
247
+ runner?: ExplicitInternalPiRunner;
248
+ }): Promise<ExplicitInternalPiResult> {
249
+ const args = buildExplicitInternalActivationArgs(
250
+ options.packageRoot,
251
+ options.extraArgs ?? [],
252
+ );
253
+ const runner = options.runner ?? defaultExplicitInternalPiRunner;
254
+ return await runner(args, {
255
+ cwd: options.cwd,
256
+ ...(options.timeoutMs === undefined ? {} : { timeoutMs: options.timeoutMs }),
257
+ env: {
258
+ ...process.env,
259
+ ...options.env,
260
+ HOME: options.home,
261
+ PI_CODING_AGENT_DIR: options.agentDir,
262
+ },
263
+ });
264
+ }
265
+
266
+ /** Non-dispatch args: load Internal once and exit via Pi help (no model turn). */
267
+ export const EXPLICIT_INTERNAL_LOAD_PROBE_ARGS = [
268
+ "--no-skills",
269
+ "--no-prompt-templates",
270
+ "--no-themes",
271
+ "--no-context-files",
272
+ "--no-session",
273
+ "--help",
274
+ ] as const;