@narumitw/pi-subagents 2.1.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -1250
- package/dist/child-communication-bridge.ts +136 -0
- package/dist/child-communication-bridge.ts.map +7 -0
- package/dist/chunks/chunk-UKTWUQRM.js +667 -0
- package/dist/chunks/chunk-UKTWUQRM.js.map +7 -0
- package/dist/index.ts +1448 -1598
- package/dist/index.ts.map +4 -4
- package/docs/design-principles.md +31 -0
- package/docs/ipc-transport-idea.md +103 -0
- package/docs/tools.md +109 -0
- package/package.json +11 -19
- package/src/broker-credentials.ts +71 -0
- package/src/child-communication-bridge.ts +153 -0
- package/src/child-communication-tools.ts +147 -0
- package/src/completion-renderer.ts +60 -0
- package/src/index.ts +1 -1
- package/src/message-broker.ts +628 -0
- package/src/model-output.ts +42 -0
- package/src/process.ts +688 -0
- package/src/runtime.ts +538 -0
- package/src/subagents.ts +33 -34
- package/src/tools.ts +416 -0
- package/src/types.ts +85 -0
- package/src/widget.ts +130 -0
- package/dist/chunks/auto-transport-FUUKFDIG.ts +0 -109
- package/dist/chunks/auto-transport-FUUKFDIG.ts.map +0 -7
- package/dist/chunks/capability-grant-PR72SWWS.ts +0 -19
- package/dist/chunks/capability-grant-PR72SWWS.ts.map +0 -7
- package/dist/chunks/child-peer-bridge.ts +0 -108
- package/dist/chunks/child-peer-bridge.ts.map +0 -7
- package/dist/chunks/chunk-2LMJU25E.ts +0 -1641
- package/dist/chunks/chunk-2LMJU25E.ts.map +0 -7
- package/dist/chunks/chunk-47SK2QYC.ts +0 -56
- package/dist/chunks/chunk-47SK2QYC.ts.map +0 -7
- package/dist/chunks/chunk-4AQSF7AS.ts +0 -29
- package/dist/chunks/chunk-4AQSF7AS.ts.map +0 -7
- package/dist/chunks/chunk-5DGRMKNV.ts +0 -126
- package/dist/chunks/chunk-5DGRMKNV.ts.map +0 -7
- package/dist/chunks/chunk-6H6TBBED.ts +0 -108
- package/dist/chunks/chunk-6H6TBBED.ts.map +0 -7
- package/dist/chunks/chunk-6KJ34M6S.ts +0 -159
- package/dist/chunks/chunk-6KJ34M6S.ts.map +0 -7
- package/dist/chunks/chunk-6NSJVPXX.ts +0 -480
- package/dist/chunks/chunk-6NSJVPXX.ts.map +0 -7
- package/dist/chunks/chunk-7AAJEUSL.ts +0 -238
- package/dist/chunks/chunk-7AAJEUSL.ts.map +0 -7
- package/dist/chunks/chunk-BFK2Z4ZF.ts +0 -189
- package/dist/chunks/chunk-BFK2Z4ZF.ts.map +0 -7
- package/dist/chunks/chunk-BOAXY55Y.ts +0 -153
- package/dist/chunks/chunk-BOAXY55Y.ts.map +0 -7
- package/dist/chunks/chunk-D4CR7T73.ts +0 -353
- package/dist/chunks/chunk-D4CR7T73.ts.map +0 -7
- package/dist/chunks/chunk-FEVPWRMU.ts +0 -300
- package/dist/chunks/chunk-FEVPWRMU.ts.map +0 -7
- package/dist/chunks/chunk-G6VZTCFH.ts +0 -70
- package/dist/chunks/chunk-G6VZTCFH.ts.map +0 -7
- package/dist/chunks/chunk-H3FP6DLR.ts +0 -160
- package/dist/chunks/chunk-H3FP6DLR.ts.map +0 -7
- package/dist/chunks/chunk-HA36LPCO.ts +0 -130
- package/dist/chunks/chunk-HA36LPCO.ts.map +0 -7
- package/dist/chunks/chunk-HOP5FXDT.ts +0 -27
- package/dist/chunks/chunk-HOP5FXDT.ts.map +0 -7
- package/dist/chunks/chunk-IGAMVWFN.ts +0 -139
- package/dist/chunks/chunk-IGAMVWFN.ts.map +0 -7
- package/dist/chunks/chunk-ILEQ27AL.ts +0 -45
- package/dist/chunks/chunk-ILEQ27AL.ts.map +0 -7
- package/dist/chunks/chunk-ITVWPNU4.ts +0 -819
- package/dist/chunks/chunk-ITVWPNU4.ts.map +0 -7
- package/dist/chunks/chunk-IWC32VPY.ts +0 -172
- package/dist/chunks/chunk-IWC32VPY.ts.map +0 -7
- package/dist/chunks/chunk-JSZIP73U.ts +0 -362
- package/dist/chunks/chunk-JSZIP73U.ts.map +0 -7
- package/dist/chunks/chunk-JU6LUNLP.ts +0 -380
- package/dist/chunks/chunk-JU6LUNLP.ts.map +0 -7
- package/dist/chunks/chunk-KMGKCEO4.ts +0 -38
- package/dist/chunks/chunk-KMGKCEO4.ts.map +0 -7
- package/dist/chunks/chunk-LASD73CM.ts +0 -339
- package/dist/chunks/chunk-LASD73CM.ts.map +0 -7
- package/dist/chunks/chunk-LEOYDZI3.ts +0 -22
- package/dist/chunks/chunk-LEOYDZI3.ts.map +0 -7
- package/dist/chunks/chunk-LL4LP2T7.ts +0 -737
- package/dist/chunks/chunk-LL4LP2T7.ts.map +0 -7
- package/dist/chunks/chunk-N2T5IN4X.ts +0 -18
- package/dist/chunks/chunk-N2T5IN4X.ts.map +0 -7
- package/dist/chunks/chunk-N7BLVXKK.ts +0 -73
- package/dist/chunks/chunk-N7BLVXKK.ts.map +0 -7
- package/dist/chunks/chunk-NLT67IZS.ts +0 -322
- package/dist/chunks/chunk-NLT67IZS.ts.map +0 -7
- package/dist/chunks/chunk-NTRPLF46.ts +0 -63
- package/dist/chunks/chunk-NTRPLF46.ts.map +0 -7
- package/dist/chunks/chunk-ONDTY4EL.ts +0 -91
- package/dist/chunks/chunk-ONDTY4EL.ts.map +0 -7
- package/dist/chunks/chunk-OVPHFGKO.ts +0 -1769
- package/dist/chunks/chunk-OVPHFGKO.ts.map +0 -7
- package/dist/chunks/chunk-P7OH4XMF.ts +0 -60
- package/dist/chunks/chunk-P7OH4XMF.ts.map +0 -7
- package/dist/chunks/chunk-PBZMBTNJ.ts +0 -422
- package/dist/chunks/chunk-PBZMBTNJ.ts.map +0 -7
- package/dist/chunks/chunk-PGLSFLYW.ts +0 -52
- package/dist/chunks/chunk-PGLSFLYW.ts.map +0 -7
- package/dist/chunks/chunk-RRR66UWR.ts +0 -28
- package/dist/chunks/chunk-RRR66UWR.ts.map +0 -7
- package/dist/chunks/chunk-RSUXZD6S.ts +0 -275
- package/dist/chunks/chunk-RSUXZD6S.ts.map +0 -7
- package/dist/chunks/chunk-RTCVYIZA.ts +0 -221
- package/dist/chunks/chunk-RTCVYIZA.ts.map +0 -7
- package/dist/chunks/chunk-RY2AEGMZ.ts +0 -34
- package/dist/chunks/chunk-RY2AEGMZ.ts.map +0 -7
- package/dist/chunks/chunk-SWGQLFSD.ts +0 -60
- package/dist/chunks/chunk-SWGQLFSD.ts.map +0 -7
- package/dist/chunks/chunk-TM2R67J3.ts +0 -52
- package/dist/chunks/chunk-TM2R67J3.ts.map +0 -7
- package/dist/chunks/chunk-TMZRHIIK.ts +0 -497
- package/dist/chunks/chunk-TMZRHIIK.ts.map +0 -7
- package/dist/chunks/chunk-TZ34IQ3M.ts +0 -59
- package/dist/chunks/chunk-TZ34IQ3M.ts.map +0 -7
- package/dist/chunks/chunk-UMBPMVJW.ts +0 -22
- package/dist/chunks/chunk-UMBPMVJW.ts.map +0 -7
- package/dist/chunks/chunk-VDG7LTYE.ts +0 -80
- package/dist/chunks/chunk-VDG7LTYE.ts.map +0 -7
- package/dist/chunks/chunk-X4NMONPE.ts +0 -73
- package/dist/chunks/chunk-X4NMONPE.ts.map +0 -7
- package/dist/chunks/chunk-YPJEN6NU.ts +0 -65
- package/dist/chunks/chunk-YPJEN6NU.ts.map +0 -7
- package/dist/chunks/chunk-YU53SHA7.ts +0 -328
- package/dist/chunks/chunk-YU53SHA7.ts.map +0 -7
- package/dist/chunks/completion-delivery-RSJU6BXL.ts +0 -17
- package/dist/chunks/completion-delivery-RSJU6BXL.ts.map +0 -7
- package/dist/chunks/config-status-FKGDECZ3.ts +0 -35
- package/dist/chunks/config-status-FKGDECZ3.ts.map +0 -7
- package/dist/chunks/config-ui-ABHYNGQ7.ts +0 -1212
- package/dist/chunks/config-ui-ABHYNGQ7.ts.map +0 -7
- package/dist/chunks/consult-LJU3IQY5.ts +0 -519
- package/dist/chunks/consult-LJU3IQY5.ts.map +0 -7
- package/dist/chunks/context-QDKLVQXE.ts +0 -11
- package/dist/chunks/context-QDKLVQXE.ts.map +0 -7
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts +0 -130
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts.map +0 -7
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts +0 -17
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts.map +0 -7
- package/dist/chunks/delegation-contract-LA56I5DT.ts +0 -24
- package/dist/chunks/delegation-contract-LA56I5DT.ts.map +0 -7
- package/dist/chunks/discovery-MIFB2U4Y.ts +0 -12
- package/dist/chunks/discovery-MIFB2U4Y.ts.map +0 -7
- package/dist/chunks/execution-Q2JZLKJA.ts +0 -4037
- package/dist/chunks/execution-Q2JZLKJA.ts.map +0 -7
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts +0 -39
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts.map +0 -7
- package/dist/chunks/inspect-UH2TKH6E.ts +0 -634
- package/dist/chunks/inspect-UH2TKH6E.ts.map +0 -7
- package/dist/chunks/peer-communication-KL36Z6CO.ts +0 -294
- package/dist/chunks/peer-communication-KL36Z6CO.ts.map +0 -7
- package/dist/chunks/persistence-UY3PY6E5.ts +0 -348
- package/dist/chunks/persistence-UY3PY6E5.ts.map +0 -7
- package/dist/chunks/registry-BT54L6CY.ts +0 -1445
- package/dist/chunks/registry-BT54L6CY.ts.map +0 -7
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts +0 -81
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts.map +0 -7
- package/dist/chunks/rpc-transport-7R7DVCEB.ts +0 -1222
- package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +0 -7
- package/dist/chunks/runtime-policy-5YELCOVC.ts +0 -15
- package/dist/chunks/runtime-policy-5YELCOVC.ts.map +0 -7
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts +0 -19
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts.map +0 -7
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts +0 -13
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts.map +0 -7
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts +0 -15
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts.map +0 -7
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts +0 -160
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +0 -7
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +0 -164
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +0 -7
- package/dist/chunks/workspace-WDKLN72J.ts +0 -11
- package/dist/chunks/workspace-WDKLN72J.ts.map +0 -7
- package/docs/async-runtime-protocol.md +0 -84
- package/docs/implementation-notes/pi-subagents-capability-matrix.md +0 -62
- package/docs/implementation-notes/pi-subagents-current-direction.md +0 -99
- package/docs/implementation-notes/pi-subagents-rpc-v1.md +0 -153
- package/docs/pi-subagents-diagrams.md +0 -183
- package/src/adaptive-scheduler.ts +0 -224
- package/src/admission-benchmark.ts +0 -95
- package/src/admission-policy.ts +0 -78
- package/src/agent-projection.ts +0 -53
- package/src/agents/built-ins.ts +0 -71
- package/src/agents/catalog.ts +0 -241
- package/src/agents/discovery.ts +0 -265
- package/src/agents/types.ts +0 -100
- package/src/agents.ts +0 -51
- package/src/async-subagent-benchmark.ts +0 -532
- package/src/auto-transport.ts +0 -121
- package/src/blocking-status.ts +0 -63
- package/src/cached-module-loader.ts +0 -18
- package/src/capabilities.ts +0 -145
- package/src/capability-grant.ts +0 -115
- package/src/capability-router.ts +0 -107
- package/src/child-peer-bridge.ts +0 -124
- package/src/child-peer-tools.ts +0 -132
- package/src/completion-delivery.ts +0 -409
- package/src/completion-render.ts +0 -190
- package/src/completion-requirement.ts +0 -479
- package/src/completion-routing.ts +0 -24
- package/src/config-registration.ts +0 -114
- package/src/config-status.ts +0 -217
- package/src/config-ui.ts +0 -983
- package/src/consult-policy.ts +0 -15
- package/src/consult-registration.ts +0 -125
- package/src/consult-render.ts +0 -194
- package/src/consult-resources.ts +0 -51
- package/src/consult-tool.ts +0 -95
- package/src/consult.ts +0 -566
- package/src/context.ts +0 -126
- package/src/create-stateful-transport.ts +0 -157
- package/src/cwd-policy.ts +0 -183
- package/src/delegation-contract.ts +0 -463
- package/src/execution/budget.ts +0 -56
- package/src/execution/runtime-policy.ts +0 -19
- package/src/execution-plan.ts +0 -322
- package/src/execution-ui.ts +0 -259
- package/src/execution.ts +0 -1679
- package/src/in-process-transport.ts +0 -941
- package/src/inspect-registration.ts +0 -64
- package/src/inspect-render.ts +0 -334
- package/src/inspect-tool.ts +0 -45
- package/src/inspect.ts +0 -769
- package/src/integration-controller.ts +0 -98
- package/src/limits.ts +0 -75
- package/src/orchestration-metrics.ts +0 -116
- package/src/outcome.ts +0 -61
- package/src/panel-child-group.ts +0 -35
- package/src/panel-contract.ts +0 -343
- package/src/panel-evidence.ts +0 -59
- package/src/panel-execution.ts +0 -769
- package/src/panel-failure.ts +0 -56
- package/src/panel-planning.ts +0 -175
- package/src/panel-presets.ts +0 -3
- package/src/panel-prompts.ts +0 -132
- package/src/panel-reconciliation.ts +0 -57
- package/src/panel-render.ts +0 -103
- package/src/parallel-limit-ui.ts +0 -113
- package/src/params.ts +0 -278
- package/src/peer-communication.ts +0 -352
- package/src/peer-transport.ts +0 -49
- package/src/persistence.ts +0 -522
- package/src/pi-args.ts +0 -43
- package/src/pi-invocation.ts +0 -168
- package/src/process-control.ts +0 -43
- package/src/prompt-resources.ts +0 -38
- package/src/prompt-source-safety.ts +0 -48
- package/src/protocol.ts +0 -76
- package/src/registry-types.ts +0 -204
- package/src/registry.ts +0 -1698
- package/src/render-common.ts +0 -252
- package/src/render.ts +0 -704
- package/src/result-contract.ts +0 -431
- package/src/retained-semantic-state.ts +0 -100
- package/src/rpc-timeout-finalization.ts +0 -207
- package/src/rpc-transport-metadata.ts +0 -65
- package/src/rpc-transport.ts +0 -1035
- package/src/rpc-turn-capture.ts +0 -217
- package/src/runner-outcome.ts +0 -31
- package/src/runner-result.ts +0 -55
- package/src/runner-types.ts +0 -102
- package/src/runner-usage.ts +0 -48
- package/src/runner.ts +0 -866
- package/src/safe-text.ts +0 -67
- package/src/semantic-snapshot.ts +0 -214
- package/src/session-guidance-contract.ts +0 -399
- package/src/settings/inspection.ts +0 -302
- package/src/settings/schema.ts +0 -195
- package/src/settings-reader.ts +0 -215
- package/src/settings.ts +0 -452
- package/src/spawn-idempotency.ts +0 -68
- package/src/stateful-agent-view.ts +0 -85
- package/src/stateful-config.ts +0 -13
- package/src/stateful-guidance.ts +0 -29
- package/src/stateful-lifecycle.ts +0 -74
- package/src/stateful-limit-ui.ts +0 -249
- package/src/stateful-limits.ts +0 -96
- package/src/stateful-prompt.ts +0 -49
- package/src/stateful-registration.ts +0 -1320
- package/src/stateful-render.ts +0 -318
- package/src/stateful-safety.ts +0 -47
- package/src/stateful-tool-params.ts +0 -215
- package/src/stateful.ts +0 -12
- package/src/subagent-details.ts +0 -43
- package/src/subagents-extension.ts +0 -355
- package/src/subprocess-transport.ts +0 -145
- package/src/supervision.ts +0 -104
- package/src/task-path.ts +0 -65
- package/src/timeout-checkpoint.ts +0 -305
- package/src/timeout-finalization.ts +0 -75
- package/src/tool-schema-compatibility.ts +0 -73
- package/src/transport-types.ts +0 -74
- package/src/transport-ui.ts +0 -135
- package/src/transport.ts +0 -43
- package/src/turn-budget.ts +0 -109
- package/src/usage-format.ts +0 -42
- package/src/usage-recording-config.ts +0 -13
- package/src/usage-recording-store.ts +0 -183
- package/src/usage-recording.ts +0 -478
- package/src/verification-harness.ts +0 -516
- package/src/verification-policy.ts +0 -67
- package/src/verification-receipt.ts +0 -275
- package/src/verified-execution-benchmark.ts +0 -86
- package/src/verified-execution-contract.ts +0 -191
- package/src/verified-execution-schema.ts +0 -32
- package/src/work-item-ledger.ts +0 -1404
- package/src/work-item-persistence.ts +0 -254
- package/src/workflow-completion-controller.ts +0 -397
- package/src/workflow-planning.ts +0 -172
- package/src/workflow-tree-identity.ts +0 -289
- package/src/workflow-ui.ts +0 -69
- package/src/workflow-verification.ts +0 -296
- package/src/workspace.ts +0 -174
package/src/transport.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import type { AgentMailboxMessage, ManagedAgent, TurnOutcome } from "./registry.js";
|
|
2
|
-
import type { TransportProgressCallback } from "./transport-types.js";
|
|
3
|
-
|
|
4
|
-
export interface SubagentTransport {
|
|
5
|
-
readonly kind: "subprocess" | "in-process" | "rpc" | "auto" | "fake";
|
|
6
|
-
runTurn(
|
|
7
|
-
agent: ManagedAgent,
|
|
8
|
-
task: string,
|
|
9
|
-
signal: AbortSignal,
|
|
10
|
-
onProgress?: TransportProgressCallback,
|
|
11
|
-
): Promise<TurnOutcome>;
|
|
12
|
-
deliverMessage?(agent: ManagedAgent, message: AgentMailboxMessage): Promise<boolean>;
|
|
13
|
-
release?(agent: ManagedAgent): Promise<void>;
|
|
14
|
-
shutdown?(): Promise<void>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type AgentTurnRunner = (
|
|
18
|
-
agent: ManagedAgent,
|
|
19
|
-
task: string,
|
|
20
|
-
signal: AbortSignal,
|
|
21
|
-
onProgress?: TransportProgressCallback,
|
|
22
|
-
) => Promise<TurnOutcome>;
|
|
23
|
-
|
|
24
|
-
export class FunctionTransport implements SubagentTransport {
|
|
25
|
-
readonly kind = "fake" as const;
|
|
26
|
-
|
|
27
|
-
constructor(private readonly runner: AgentTurnRunner) {}
|
|
28
|
-
|
|
29
|
-
runTurn(
|
|
30
|
-
agent: ManagedAgent,
|
|
31
|
-
task: string,
|
|
32
|
-
signal: AbortSignal,
|
|
33
|
-
onProgress?: TransportProgressCallback,
|
|
34
|
-
): Promise<TurnOutcome> {
|
|
35
|
-
return this.runner(agent, task, signal, onProgress);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function normalizeTransport(
|
|
40
|
-
transport: SubagentTransport | AgentTurnRunner,
|
|
41
|
-
): SubagentTransport {
|
|
42
|
-
return typeof transport === "function" ? new FunctionTransport(transport) : transport;
|
|
43
|
-
}
|
package/src/turn-budget.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { MAX_SUBAGENT_TIMEOUT_MS } from "./limits.js";
|
|
2
|
-
import type { TurnTerminationReason } from "./timeout-checkpoint.js";
|
|
3
|
-
|
|
4
|
-
export const MAX_SUBAGENT_TURNS = 1_000_000;
|
|
5
|
-
export const MAX_SUBAGENT_TOOL_CALLS = 1_000_000;
|
|
6
|
-
|
|
7
|
-
export interface TurnLimits {
|
|
8
|
-
idleTimeoutMs?: number;
|
|
9
|
-
maxTurns?: number;
|
|
10
|
-
maxToolCalls?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface TurnBudgetStop {
|
|
14
|
-
reason: Extract<TurnTerminationReason, "idle_timeout" | "turn_limit" | "tool_call_limit">;
|
|
15
|
-
limit: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface TurnBudgetMonitorOptions extends TurnLimits {
|
|
19
|
-
onExceeded(stop: TurnBudgetStop): void;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const TERMINAL_STOP_REASONS = new Set(["stop", "length", "error", "aborted"]);
|
|
23
|
-
|
|
24
|
-
export function validateTurnLimits(limits: TurnLimits): TurnLimits {
|
|
25
|
-
validateOptionalInteger("idleTimeoutMs", limits.idleTimeoutMs, MAX_SUBAGENT_TIMEOUT_MS);
|
|
26
|
-
validateOptionalInteger("maxTurns", limits.maxTurns, MAX_SUBAGENT_TURNS);
|
|
27
|
-
validateOptionalInteger("maxToolCalls", limits.maxToolCalls, MAX_SUBAGENT_TOOL_CALLS);
|
|
28
|
-
return { ...limits };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class TurnBudgetMonitor {
|
|
32
|
-
private readonly limits: TurnLimits;
|
|
33
|
-
private readonly onExceeded: (stop: TurnBudgetStop) => void;
|
|
34
|
-
private idleTimer?: NodeJS.Timeout;
|
|
35
|
-
private assistantTurns = 0;
|
|
36
|
-
private toolCalls = 0;
|
|
37
|
-
private stopped = false;
|
|
38
|
-
|
|
39
|
-
constructor(options: TurnBudgetMonitorOptions) {
|
|
40
|
-
this.limits = validateTurnLimits({
|
|
41
|
-
idleTimeoutMs: options.idleTimeoutMs,
|
|
42
|
-
maxTurns: options.maxTurns,
|
|
43
|
-
maxToolCalls: options.maxToolCalls,
|
|
44
|
-
});
|
|
45
|
-
this.onExceeded = options.onExceeded;
|
|
46
|
-
this.resetIdleTimer();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
recordActivity(): void {
|
|
50
|
-
if (this.stopped) return;
|
|
51
|
-
this.resetIdleTimer();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
recordAssistantTurn(stopReason?: string): void {
|
|
55
|
-
if (this.stopped) return;
|
|
56
|
-
this.assistantTurns++;
|
|
57
|
-
this.recordActivity();
|
|
58
|
-
if (
|
|
59
|
-
this.limits.maxTurns !== undefined &&
|
|
60
|
-
this.assistantTurns >= this.limits.maxTurns &&
|
|
61
|
-
!TERMINAL_STOP_REASONS.has(stopReason ?? "")
|
|
62
|
-
) {
|
|
63
|
-
this.exceed({ reason: "turn_limit", limit: this.limits.maxTurns });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
recordToolCalls(count = 1): void {
|
|
68
|
-
if (this.stopped || count < 1) return;
|
|
69
|
-
this.toolCalls += count;
|
|
70
|
-
if (this.limits.maxToolCalls !== undefined && this.toolCalls > this.limits.maxToolCalls) {
|
|
71
|
-
this.exceed({ reason: "tool_call_limit", limit: this.limits.maxToolCalls });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
dispose(): void {
|
|
76
|
-
this.stopped = true;
|
|
77
|
-
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
78
|
-
this.idleTimer = undefined;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private resetIdleTimer(): void {
|
|
82
|
-
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
83
|
-
const idleTimeoutMs = this.limits.idleTimeoutMs;
|
|
84
|
-
if (idleTimeoutMs === undefined || this.stopped) {
|
|
85
|
-
this.idleTimer = undefined;
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
this.idleTimer = setTimeout(
|
|
89
|
-
() => this.exceed({ reason: "idle_timeout", limit: idleTimeoutMs }),
|
|
90
|
-
idleTimeoutMs,
|
|
91
|
-
);
|
|
92
|
-
this.idleTimer.unref?.();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private exceed(stop: TurnBudgetStop): void {
|
|
96
|
-
if (this.stopped) return;
|
|
97
|
-
this.stopped = true;
|
|
98
|
-
if (this.idleTimer) clearTimeout(this.idleTimer);
|
|
99
|
-
this.idleTimer = undefined;
|
|
100
|
-
this.onExceeded(stop);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function validateOptionalInteger(name: string, value: number | undefined, maximum: number): void {
|
|
105
|
-
if (value === undefined) return;
|
|
106
|
-
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
|
|
107
|
-
throw new Error(`${name} must be an integer between 1 and ${maximum}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
package/src/usage-format.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { SubagentThinkingLevel } from "./agents/types.js";
|
|
2
|
-
import { safeLine } from "./render-common.js";
|
|
3
|
-
|
|
4
|
-
export function formatTokens(count: number): string {
|
|
5
|
-
if (count < 1000) return count.toString();
|
|
6
|
-
if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
|
|
7
|
-
if (count < 1000000) return `${Math.round(count / 1000)}k`;
|
|
8
|
-
return `${(count / 1000000).toFixed(1)}M`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function formatUsageStats(
|
|
12
|
-
usage: {
|
|
13
|
-
input: number;
|
|
14
|
-
output: number;
|
|
15
|
-
cacheRead: number;
|
|
16
|
-
cacheWrite: number;
|
|
17
|
-
cost: number;
|
|
18
|
-
contextTokens?: number;
|
|
19
|
-
turns?: number;
|
|
20
|
-
},
|
|
21
|
-
model?: string,
|
|
22
|
-
thinkingLevel?: SubagentThinkingLevel,
|
|
23
|
-
actualProvider?: string,
|
|
24
|
-
actualModel?: string,
|
|
25
|
-
): string {
|
|
26
|
-
const parts: string[] = [];
|
|
27
|
-
if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
|
|
28
|
-
if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
|
|
29
|
-
if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
|
|
30
|
-
if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
|
|
31
|
-
if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
|
|
32
|
-
if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
|
|
33
|
-
if (usage.contextTokens && usage.contextTokens > 0)
|
|
34
|
-
parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
|
|
35
|
-
const safeProvider = actualProvider ? safeLine(actualProvider, "", 256) : undefined;
|
|
36
|
-
const safeModel = actualModel ? safeLine(actualModel, "", 256) : undefined;
|
|
37
|
-
const actual =
|
|
38
|
-
safeProvider && safeModel ? `${safeProvider}/${safeModel}` : (safeModel ?? safeProvider);
|
|
39
|
-
if (actual ?? model) parts.push(actual ?? safeLine(model, "", 256));
|
|
40
|
-
if (thinkingLevel) parts.push(`requested-thinking:${safeLine(thinkingLevel, "", 128)}`);
|
|
41
|
-
return parts.join(" ");
|
|
42
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export const USAGE_RECORDING_DIRECTORY = "pi-subagents-usage";
|
|
2
|
-
export const USAGE_RECORDING_RETENTION_DAYS = 30;
|
|
3
|
-
export const USAGE_RECORDING_STUDY_ID = "pi-subagents-surface-v1";
|
|
4
|
-
|
|
5
|
-
export interface SubagentUsageRecordingSettings {
|
|
6
|
-
enabled?: boolean;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function resolveUsageRecordingEnabled(
|
|
10
|
-
settings: SubagentUsageRecordingSettings | undefined,
|
|
11
|
-
): boolean {
|
|
12
|
-
return settings?.enabled === true;
|
|
13
|
-
}
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { chmod, lstat, mkdir, readdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
const WRITER_PATTERN =
|
|
6
|
-
/^runtime-[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\.jsonl$/u;
|
|
7
|
-
const MAX_EVENT_BYTES = 8 * 1024;
|
|
8
|
-
const WRITE_TIMEOUT_MS = 2_000;
|
|
9
|
-
|
|
10
|
-
export interface UsageEventStorePort {
|
|
11
|
-
readonly path: string;
|
|
12
|
-
append(event: unknown): Promise<void>;
|
|
13
|
-
prune(retentionDays: number): Promise<void>;
|
|
14
|
-
close(): Promise<void>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class UsageEventStore implements UsageEventStorePort {
|
|
18
|
-
readonly path: string;
|
|
19
|
-
private readonly writerPath: string;
|
|
20
|
-
private mutationTail: Promise<void> = Promise.resolve();
|
|
21
|
-
private readonly lifecycle = new AbortController();
|
|
22
|
-
private closed = false;
|
|
23
|
-
|
|
24
|
-
constructor(rootPath: string, options: { createId?: () => string; now?: () => number } = {}) {
|
|
25
|
-
this.path = rootPath;
|
|
26
|
-
this.createId = options.createId ?? randomUUID;
|
|
27
|
-
this.now = options.now ?? Date.now;
|
|
28
|
-
this.writerPath = path.join(rootPath, `runtime-${validId(this.createId())}.jsonl`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
private readonly createId: () => string;
|
|
32
|
-
private readonly now: () => number;
|
|
33
|
-
|
|
34
|
-
append(event: unknown): Promise<void> {
|
|
35
|
-
if (this.closed) return Promise.reject(new Error("Usage recording storage is closed."));
|
|
36
|
-
const frame = encodeEvent(event);
|
|
37
|
-
return this.enqueue(() =>
|
|
38
|
-
withTimeout(
|
|
39
|
-
(signal) => this.appendFrame(frame, signal),
|
|
40
|
-
this.lifecycle.signal,
|
|
41
|
-
WRITE_TIMEOUT_MS,
|
|
42
|
-
"Usage recording write timed out",
|
|
43
|
-
),
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
prune(retentionDays: number): Promise<void> {
|
|
48
|
-
if (this.closed) return Promise.reject(new Error("Usage recording storage is closed."));
|
|
49
|
-
return this.enqueue(() =>
|
|
50
|
-
withTimeout(
|
|
51
|
-
async (signal) => {
|
|
52
|
-
await ensurePrivateDirectory(this.path);
|
|
53
|
-
signal.throwIfAborted();
|
|
54
|
-
const cutoff = this.now() - retentionDays * 24 * 60 * 60 * 1000;
|
|
55
|
-
for (const entry of await readdir(this.path, { withFileTypes: true })) {
|
|
56
|
-
signal.throwIfAborted();
|
|
57
|
-
if (!entry.isFile() || !WRITER_PATTERN.test(entry.name)) continue;
|
|
58
|
-
const candidate = path.join(this.path, entry.name);
|
|
59
|
-
if (candidate === this.writerPath) continue;
|
|
60
|
-
let metadata: Awaited<ReturnType<typeof lstat>>;
|
|
61
|
-
try {
|
|
62
|
-
metadata = await lstat(candidate);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
if (isNodeError(error) && error.code === "ENOENT") continue;
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
67
|
-
if (!metadata.isFile() || metadata.isSymbolicLink() || metadata.mtimeMs >= cutoff) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
await rm(candidate, { force: true });
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
this.lifecycle.signal,
|
|
74
|
-
WRITE_TIMEOUT_MS,
|
|
75
|
-
"Usage recording retention cleanup timed out",
|
|
76
|
-
),
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async close(): Promise<void> {
|
|
81
|
-
if (this.closed) return;
|
|
82
|
-
this.closed = true;
|
|
83
|
-
this.lifecycle.abort(new DOMException("Usage recording storage closed", "AbortError"));
|
|
84
|
-
await this.mutationTail;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private enqueue(operation: () => Promise<void>): Promise<void> {
|
|
88
|
-
const result = this.mutationTail.then(operation);
|
|
89
|
-
this.mutationTail = result.catch(() => undefined);
|
|
90
|
-
return result;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private async appendFrame(frame: string, signal: AbortSignal): Promise<void> {
|
|
94
|
-
await ensurePrivateDirectory(this.path);
|
|
95
|
-
signal.throwIfAborted();
|
|
96
|
-
await assertOptionalPrivateRegularFile(this.writerPath);
|
|
97
|
-
signal.throwIfAborted();
|
|
98
|
-
await writeFile(this.writerPath, frame, {
|
|
99
|
-
encoding: "utf8",
|
|
100
|
-
flag: "a",
|
|
101
|
-
mode: 0o600,
|
|
102
|
-
signal,
|
|
103
|
-
});
|
|
104
|
-
if (process.platform !== "win32") await chmod(this.writerPath, 0o600);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function encodeEvent(event: unknown): string {
|
|
109
|
-
const frame = `${JSON.stringify(event)}\n`;
|
|
110
|
-
if (Buffer.byteLength(frame) > MAX_EVENT_BYTES) {
|
|
111
|
-
throw new Error("Usage recording event exceeds the storage bound.");
|
|
112
|
-
}
|
|
113
|
-
return frame;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async function ensurePrivateDirectory(directoryPath: string): Promise<void> {
|
|
117
|
-
try {
|
|
118
|
-
const metadata = await lstat(directoryPath);
|
|
119
|
-
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
120
|
-
throw new Error("Usage recording storage must be a regular directory, not a link.");
|
|
121
|
-
}
|
|
122
|
-
} catch (error) {
|
|
123
|
-
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
124
|
-
await mkdir(directoryPath, { recursive: true, mode: 0o700 });
|
|
125
|
-
const metadata = await lstat(directoryPath);
|
|
126
|
-
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
127
|
-
throw new Error("Usage recording storage must be a regular directory, not a link.");
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (process.platform !== "win32") await chmod(directoryPath, 0o700);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async function assertOptionalPrivateRegularFile(filePath: string): Promise<void> {
|
|
134
|
-
try {
|
|
135
|
-
const metadata = await lstat(filePath);
|
|
136
|
-
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
137
|
-
throw new Error("Usage recording writers must be regular files, not links.");
|
|
138
|
-
}
|
|
139
|
-
if (process.platform !== "win32") await chmod(filePath, 0o600);
|
|
140
|
-
} catch (error) {
|
|
141
|
-
if (isNodeError(error) && error.code === "ENOENT") return;
|
|
142
|
-
throw error;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async function withTimeout<T>(
|
|
147
|
-
operation: (signal: AbortSignal) => Promise<T>,
|
|
148
|
-
lifecycleSignal: AbortSignal,
|
|
149
|
-
timeoutMs: number,
|
|
150
|
-
message: string,
|
|
151
|
-
): Promise<T> {
|
|
152
|
-
lifecycleSignal.throwIfAborted();
|
|
153
|
-
const controller = new AbortController();
|
|
154
|
-
const abortLifecycle = () =>
|
|
155
|
-
controller.abort(
|
|
156
|
-
lifecycleSignal.reason ?? new DOMException("Usage recording storage closed", "AbortError"),
|
|
157
|
-
);
|
|
158
|
-
lifecycleSignal.addEventListener("abort", abortLifecycle, { once: true });
|
|
159
|
-
const timer = setTimeout(
|
|
160
|
-
() => controller.abort(new DOMException(message, "TimeoutError")),
|
|
161
|
-
timeoutMs,
|
|
162
|
-
);
|
|
163
|
-
try {
|
|
164
|
-
return await operation(controller.signal);
|
|
165
|
-
} catch (error) {
|
|
166
|
-
if (controller.signal.aborted) throw controller.signal.reason;
|
|
167
|
-
throw error;
|
|
168
|
-
} finally {
|
|
169
|
-
clearTimeout(timer);
|
|
170
|
-
lifecycleSignal.removeEventListener("abort", abortLifecycle);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function validId(value: string): string {
|
|
175
|
-
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u.test(value)) {
|
|
176
|
-
throw new Error("Usage recording storage received an invalid writer identity.");
|
|
177
|
-
}
|
|
178
|
-
return value;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
182
|
-
return error instanceof Error && "code" in error;
|
|
183
|
-
}
|