@akagilnc/pi-workflow-roles 0.1.2020 → 0.1.2033

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.
@@ -1,6 +1,7 @@
1
1
  import { Type } from "typebox";
2
2
  import { executeAuditorChild, } from "./evidence-child-executor.js";
3
3
  import { createAuditorDossierTool } from "./auditor-dossier-tool.js";
4
+ import { withPackageOwnedToolIdleSuspended } from "./package-owned-tool-idle.js";
4
5
  /** Zero-projection kickoff — soul already carries dossier-fetch duty; no hand-delivered materials. */
5
6
  export const AUDITOR_DOSSIER_PROMPT = "Audit the current run dossier.";
6
7
  const nonblank = Type.String({ minLength: 1, pattern: "\\S" });
@@ -61,24 +62,28 @@ export function readComplianceCandidate(arguments_, usage) {
61
62
  return { status: "audit-incomplete", observation: { kind: "object-status-unreadable", status: status === undefined ? "missing" : "unknown" }, candidate: arguments_, ...(usage === undefined ? {} : { usage }) };
62
63
  }
63
64
  export async function runComplianceAudit(options) {
64
- const prompt = options.serializedInput ?? AUDITOR_DOSSIER_PROMPT;
65
- const receipt = await executeAuditorChild({
66
- tool: options.tool,
67
- dossierTool: createAuditorDossierTool(options.runDirectory),
68
- systemPrompt: options.systemPrompt,
69
- prompt,
70
- roleLabel: options.roleLabel,
71
- context: options.context,
72
- retainResponse: (response) => retainComplianceResponse(options.context, response),
73
- ...(options.runCompletion === undefined ? {} : { runCompletion: options.runCompletion }),
74
- ...(options.signal === undefined ? {} : { signal: options.signal }),
65
+ // #339: only the real compliance-audit await leaves the outer package-owned
66
+ // idle owner. Pre/post-audit work stays under the single outer backstop.
67
+ return withPackageOwnedToolIdleSuspended(async () => {
68
+ const prompt = options.serializedInput ?? AUDITOR_DOSSIER_PROMPT;
69
+ const receipt = await executeAuditorChild({
70
+ tool: options.tool,
71
+ dossierTool: createAuditorDossierTool(options.runDirectory),
72
+ systemPrompt: options.systemPrompt,
73
+ prompt,
74
+ roleLabel: options.roleLabel,
75
+ context: options.context,
76
+ retainResponse: (response) => retainComplianceResponse(options.context, response),
77
+ ...(options.runCompletion === undefined ? {} : { runCompletion: options.runCompletion }),
78
+ ...(options.signal === undefined ? {} : { signal: options.signal }),
79
+ });
80
+ if (receipt.noReceiptLifecycle !== undefined) {
81
+ return {
82
+ status: "no-receipt",
83
+ ...receipt.noReceiptLifecycle,
84
+ ...(receipt.response.usage === undefined ? {} : { usage: receipt.response.usage }),
85
+ };
86
+ }
87
+ return readComplianceCandidate(receipt.decision, receipt.response.usage);
75
88
  });
76
- if (receipt.noReceiptLifecycle !== undefined) {
77
- return {
78
- status: "no-receipt",
79
- ...receipt.noReceiptLifecycle,
80
- ...(receipt.response.usage === undefined ? {} : { usage: receipt.response.usage }),
81
- };
82
- }
83
- return readComplianceCandidate(receipt.decision, receipt.response.usage);
84
89
  }
@@ -296,6 +296,10 @@ export async function createInheritedRuntime(options) {
296
296
  },
297
297
  };
298
298
  runtime.registerNativeProvider(provider);
299
+ // registerNativeProvider fires a background refresh; await it so child
300
+ // AgentSession.prompt sees configured auth without racing void refresh
301
+ // (mock timers / slow CI otherwise stall before the compliance stream).
302
+ await runtime.refresh({ allowNetwork: false });
299
303
  return state;
300
304
  }
301
305
  function numericHttpStatus(value) {
@@ -1,8 +1,26 @@
1
+ /**
2
+ * #102 package-owned tool idle backstop.
3
+ *
4
+ * Fixed 183000ms silence clock on package-owned tool execute only.
5
+ * Real producing onUpdate resets; final resolve/reject clears; timeout throws so
6
+ * Pi settles the current call as an LLM-visible isError tool result. No retry,
7
+ * role failure, process termination, signal abort, config, or Pi built-in coverage.
8
+ *
9
+ * #339: do not name-exempt whole terminating tools. Outer idle stays armed for
10
+ * pre/post-audit work. Only the real compliance-audit await suspends this single
11
+ * layer (ADR 0059 owns that interval); resume re-arms the same outer backstop.
12
+ */
13
+ import { AsyncLocalStorage } from "node:async_hooks";
1
14
  import { DEFAULT_STREAM_IDLE_TIMEOUT_MS, createStreamIdleGuard, } from "./stream-idle-guard.js";
2
15
  import { isProducingToolUpdate } from "./tool-execution-observation.js";
3
16
  export const PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_MS = DEFAULT_STREAM_IDLE_TIMEOUT_MS;
4
17
  export const PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_CODE = "AK_PACKAGE_OWNED_TOOL_IDLE_TIMEOUT";
5
18
  const WRAPPED = Symbol.for("ak.packageOwnedToolIdleWrapped");
19
+ /**
20
+ * Active outer package-owned execute idle, if any. Nested tool executes install
21
+ * their own store; compliance audit only suspends the store visible at await time.
22
+ */
23
+ const packageOwnedToolIdleScope = new AsyncLocalStorage();
6
24
  export class PackageOwnedToolIdleTimeoutError extends Error {
7
25
  code = PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_CODE;
8
26
  idleTimeoutMs = PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_MS;
@@ -33,6 +51,23 @@ function isPackageOwnedToolActivityUpdate(partialResult) {
33
51
  return Reflect.ownKeys(details).length > 0;
34
52
  return true;
35
53
  }
54
+ /**
55
+ * #339: suspend the active package-owned execute idle for one real compliance
56
+ * audit await. Single-layer only — production has one runComplianceAudit seam.
57
+ * No-op outside a wrapped package-owned execute. No second timeout or retry.
58
+ */
59
+ export async function withPackageOwnedToolIdleSuspended(run) {
60
+ const scope = packageOwnedToolIdleScope.getStore();
61
+ if (scope === undefined)
62
+ return run();
63
+ scope.suspend();
64
+ try {
65
+ return await run();
66
+ }
67
+ finally {
68
+ scope.resume();
69
+ }
70
+ }
36
71
  /**
37
72
  * Single shared execute wrapper for package-owned tool definitions.
38
73
  * Idempotent: wrapping twice returns the same protected definition.
@@ -48,7 +83,8 @@ export function wrapPackageOwnedToolDefinition(tool) {
48
83
  const onUpdate = args[3];
49
84
  return new Promise((resolve, reject) => {
50
85
  let settled = false;
51
- const idle = createStreamIdleGuard({
86
+ let suspended = false;
87
+ let idle = createStreamIdleGuard({
52
88
  idleTimeoutMs: PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_MS,
53
89
  });
54
90
  const settle = (deliver) => {
@@ -63,6 +99,26 @@ export function wrapPackageOwnedToolDefinition(tool) {
63
99
  settle(() => reject(new PackageOwnedToolIdleTimeoutError()));
64
100
  };
65
101
  idle.signal.addEventListener("abort", onIdle, { once: true });
102
+ const suspension = {
103
+ suspend() {
104
+ if (settled || suspended)
105
+ return;
106
+ suspended = true;
107
+ // ADR 0059 owns the audit interval — drop this layer until audit returns.
108
+ idle.signal.removeEventListener("abort", onIdle);
109
+ idle.dispose();
110
+ },
111
+ resume() {
112
+ if (settled || !suspended)
113
+ return;
114
+ suspended = false;
115
+ // Fresh single-layer silence window for post-audit work (e.g. cleanup).
116
+ idle = createStreamIdleGuard({
117
+ idleTimeoutMs: PACKAGE_OWNED_TOOL_IDLE_TIMEOUT_MS,
118
+ });
119
+ idle.signal.addEventListener("abort", onIdle, { once: true });
120
+ },
121
+ };
66
122
  const guardedOnUpdate = onUpdate === undefined
67
123
  ? undefined
68
124
  : (partialResult) => {
@@ -76,9 +132,15 @@ export function wrapPackageOwnedToolDefinition(tool) {
76
132
  // Preserve the original signal at args[2]; timeout must not abort it.
77
133
  callArgs[2] = signal;
78
134
  callArgs[3] = guardedOnUpdate;
79
- void Promise.resolve()
80
- .then(() => originalExecute(...callArgs))
81
- .then((result) => settle(() => resolve(result)), (error) => settle(() => reject(error)));
135
+ void packageOwnedToolIdleScope.run(suspension, async () => {
136
+ try {
137
+ const result = await originalExecute(...callArgs);
138
+ settle(() => resolve(result));
139
+ }
140
+ catch (error) {
141
+ settle(() => reject(error));
142
+ }
143
+ });
82
144
  });
83
145
  };
84
146
  wrappedExecute[WRAPPED] = true;