@narumitw/pi-subagents 2.0.6 → 2.1.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 +152 -50
- package/dist/chunks/{auto-transport-SY2VHUFH.ts → auto-transport-FUUKFDIG.ts} +6 -6
- package/dist/chunks/{capability-grant-CGEWOEKE.ts → capability-grant-PR72SWWS.ts} +4 -4
- package/dist/chunks/{chunk-DPPVEQAM.ts → chunk-4AQSF7AS.ts} +1 -1
- package/dist/chunks/chunk-4AQSF7AS.ts.map +7 -0
- package/dist/chunks/chunk-6H6TBBED.ts +108 -0
- package/dist/chunks/chunk-6H6TBBED.ts.map +7 -0
- package/dist/chunks/{chunk-DSOOH73Y.ts → chunk-7AAJEUSL.ts} +3 -3
- package/dist/chunks/{chunk-DSOOH73Y.ts.map → chunk-7AAJEUSL.ts.map} +2 -2
- package/dist/chunks/{chunk-7QPPBBXZ.ts → chunk-D4CR7T73.ts} +21 -3
- package/dist/chunks/chunk-D4CR7T73.ts.map +7 -0
- package/dist/chunks/{chunk-434NII74.ts → chunk-DIHBUR2E.ts} +54 -8
- package/dist/chunks/chunk-DIHBUR2E.ts.map +7 -0
- package/dist/chunks/{chunk-NIF42QMF.ts → chunk-FEVPWRMU.ts} +7 -4
- package/dist/chunks/chunk-FEVPWRMU.ts.map +7 -0
- package/dist/chunks/{chunk-YBUWBRF7.ts → chunk-FXI45N3J.ts} +5 -5
- package/dist/chunks/{chunk-S2IWVK3J.ts → chunk-ILEQ27AL.ts} +3 -2
- package/dist/chunks/chunk-ILEQ27AL.ts.map +7 -0
- package/dist/chunks/{chunk-DQMN4OYM.ts → chunk-ITVWPNU4.ts} +12 -106
- package/dist/chunks/chunk-ITVWPNU4.ts.map +7 -0
- package/dist/chunks/{chunk-QDVGH2X7.ts → chunk-IWC32VPY.ts} +2 -1
- package/dist/chunks/{chunk-QDVGH2X7.ts.map → chunk-IWC32VPY.ts.map} +2 -2
- package/dist/chunks/{chunk-I2FAK44T.ts → chunk-JSZIP73U.ts} +48 -10
- package/dist/chunks/chunk-JSZIP73U.ts.map +7 -0
- package/dist/chunks/{chunk-ZHTNCZIA.ts → chunk-JU6LUNLP.ts} +2 -2
- package/dist/chunks/{chunk-PSK43Y6A.ts → chunk-LASD73CM.ts} +2 -2
- package/dist/chunks/{chunk-C4L6P266.ts → chunk-LEOYDZI3.ts} +1 -1
- package/dist/chunks/{chunk-C4L6P266.ts.map → chunk-LEOYDZI3.ts.map} +2 -2
- package/dist/chunks/{chunk-H3BFJ7HJ.ts → chunk-LL4LP2T7.ts} +5 -5
- package/dist/chunks/chunk-N2T5IN4X.ts +18 -0
- package/dist/chunks/chunk-N2T5IN4X.ts.map +7 -0
- package/dist/chunks/{chunk-G3RSMSXJ.ts → chunk-NTRPLF46.ts} +1 -1
- package/dist/chunks/chunk-NTRPLF46.ts.map +7 -0
- package/dist/chunks/{chunk-CBP76ARQ.ts → chunk-PABJJYP6.ts} +174 -47
- package/dist/chunks/chunk-PABJJYP6.ts.map +7 -0
- package/dist/chunks/{chunk-EFBPISNT.ts → chunk-PBZMBTNJ.ts} +104 -103
- package/dist/chunks/chunk-PBZMBTNJ.ts.map +7 -0
- package/dist/chunks/{chunk-SCZ33MYW.ts → chunk-PGLSFLYW.ts} +2 -2
- package/dist/chunks/{chunk-O4VO6JUJ.ts → chunk-TM2R67J3.ts} +3 -3
- package/dist/chunks/{chunk-QBORTI4W.ts → chunk-TMZRHIIK.ts} +32 -3
- package/dist/chunks/chunk-TMZRHIIK.ts.map +7 -0
- package/dist/chunks/chunk-TZ34IQ3M.ts +59 -0
- package/dist/chunks/chunk-TZ34IQ3M.ts.map +7 -0
- package/dist/chunks/chunk-VBDGNNLM.ts +214 -0
- package/dist/chunks/chunk-VBDGNNLM.ts.map +7 -0
- package/dist/chunks/{chunk-7OBINKFM.ts → chunk-VDG7LTYE.ts} +11 -11
- package/dist/chunks/chunk-VDG7LTYE.ts.map +7 -0
- package/dist/chunks/{chunk-AEUYS7JC.ts → chunk-X4NMONPE.ts} +1 -1
- package/dist/chunks/chunk-X4NMONPE.ts.map +7 -0
- package/dist/chunks/{chunk-HKC4ES4B.ts → chunk-YPJEN6NU.ts} +2 -2
- package/dist/chunks/{completion-delivery-YPOWSSV3.ts → completion-delivery-JVLNQRWX.ts} +5 -4
- package/dist/chunks/{config-status-J4GPWP46.ts → config-status-FKGDECZ3.ts} +7 -6
- package/dist/chunks/{config-ui-2YUCUEBM.ts → config-ui-DDKERQHI.ts} +297 -231
- package/dist/chunks/config-ui-DDKERQHI.ts.map +7 -0
- package/dist/chunks/{consult-IV7UBNQQ.ts → consult-PQ6PRAKC.ts} +14 -13
- package/dist/chunks/consult-PQ6PRAKC.ts.map +7 -0
- package/dist/chunks/{create-stateful-transport-ZP3IXNV3.ts → create-stateful-transport-JWC2EFYL.ts} +6 -6
- package/dist/chunks/{delegation-contract-WJPT4FVS.ts → delegation-contract-LA56I5DT.ts} +2 -2
- package/dist/chunks/{discovery-K25T3SH4.ts → discovery-MIFB2U4Y.ts} +3 -3
- package/dist/chunks/{execution-LA7HN5YF.ts → execution-Q2JZLKJA.ts} +19 -16
- package/dist/chunks/execution-Q2JZLKJA.ts.map +7 -0
- package/dist/chunks/{in-process-transport-4RF3J6XK.ts → in-process-transport-HJ6TZXC3.ts} +9 -9
- package/dist/chunks/{inspect-XV2QALMR.ts → inspect-UH2TKH6E.ts} +25 -10
- package/dist/chunks/inspect-UH2TKH6E.ts.map +7 -0
- package/dist/chunks/{persistence-VSGXAW4P.ts → persistence-XHPJBZL7.ts} +11 -7
- package/dist/chunks/persistence-XHPJBZL7.ts.map +7 -0
- package/dist/chunks/{registry-E6XPJB7L.ts → registry-XDXPECWF.ts} +136 -15
- package/dist/chunks/registry-XDXPECWF.ts.map +7 -0
- package/dist/chunks/{retained-semantic-state-7WXSGRJU.ts → retained-semantic-state-GM75FZGE.ts} +3 -3
- package/dist/chunks/{rpc-transport-NEHBHWX4.ts → rpc-transport-7R7DVCEB.ts} +12 -16
- package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +7 -0
- package/dist/chunks/{spawn-idempotency-QGHO2WKC.ts → spawn-idempotency-BNHOSMZW.ts} +2 -2
- package/dist/chunks/{subprocess-transport-NSHOGDC4.ts → subprocess-transport-VHZJRBTW.ts} +15 -14
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +7 -0
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +164 -0
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +7 -0
- package/dist/index.ts +427 -30
- package/dist/index.ts.map +3 -3
- package/docs/async-runtime-protocol.md +69 -0
- package/docs/implementation-notes/pi-subagents-capability-matrix.md +62 -0
- package/docs/implementation-notes/pi-subagents-current-direction.md +99 -0
- package/docs/implementation-notes/pi-subagents-rpc-v1.md +153 -0
- package/docs/pi-subagents-diagrams.md +183 -0
- package/package.json +9 -8
- package/src/agents/types.ts +2 -0
- package/src/async-subagent-benchmark.ts +532 -0
- package/src/completion-delivery.ts +71 -4
- package/src/completion-render.ts +1 -0
- package/src/completion-requirement.ts +308 -0
- package/src/config-registration.ts +5 -5
- package/src/config-status.ts +66 -70
- package/src/config-ui.ts +244 -150
- package/src/consult-resources.ts +1 -1
- package/src/consult.ts +3 -8
- package/src/delegation-contract.ts +55 -9
- package/src/execution-plan.ts +1 -1
- package/src/execution-ui.ts +40 -29
- package/src/execution.ts +2 -3
- package/src/inspect.ts +24 -1
- package/src/orchestration-metrics.ts +1 -1
- package/src/panel-execution.ts +2 -3
- package/src/panel-failure.ts +1 -1
- package/src/panel-render.ts +1 -1
- package/src/parallel-limit-ui.ts +6 -5
- package/src/params.ts +2 -1
- package/src/persistence.ts +9 -0
- package/src/process-control.ts +43 -0
- package/src/registry-types.ts +5 -0
- package/src/registry.ts +162 -18
- package/src/render.ts +2 -1
- package/src/rpc-transport.ts +1 -1
- package/src/runner-outcome.ts +1 -1
- package/src/runner-result.ts +1 -1
- package/src/runner-types.ts +102 -0
- package/src/runner.ts +11 -192
- package/src/settings/inspection.ts +27 -0
- package/src/settings/schema.ts +9 -0
- package/src/settings-reader.ts +7 -0
- package/src/settings.ts +20 -0
- package/src/spawn-idempotency.ts +5 -0
- package/src/stateful-agent-view.ts +15 -19
- package/src/stateful-guidance.ts +14 -4
- package/src/stateful-limit-ui.ts +23 -20
- package/src/stateful-limits.ts +10 -10
- package/src/stateful-registration.ts +126 -9
- package/src/stateful-render.ts +27 -2
- package/src/subagent-details.ts +43 -0
- package/src/subagents-extension.ts +66 -8
- package/src/subagents.ts +2 -0
- package/src/subprocess-transport.ts +2 -1
- package/src/supervision.ts +2 -1
- package/src/timeout-finalization.ts +1 -1
- package/src/tool-schema-compatibility.ts +73 -0
- package/src/transport-types.ts +6 -0
- package/src/transport-ui.ts +18 -46
- package/src/usage-recording-config.ts +13 -0
- package/src/usage-recording-store.ts +183 -0
- package/src/usage-recording.ts +478 -0
- package/src/verification-harness.ts +1 -1
- package/src/workflow-ui.ts +17 -9
- package/dist/chunks/chunk-434NII74.ts.map +0 -7
- package/dist/chunks/chunk-7OBINKFM.ts.map +0 -7
- package/dist/chunks/chunk-7QPPBBXZ.ts.map +0 -7
- package/dist/chunks/chunk-AEUYS7JC.ts.map +0 -7
- package/dist/chunks/chunk-CBP76ARQ.ts.map +0 -7
- package/dist/chunks/chunk-DPPVEQAM.ts.map +0 -7
- package/dist/chunks/chunk-DQMN4OYM.ts.map +0 -7
- package/dist/chunks/chunk-EFBPISNT.ts.map +0 -7
- package/dist/chunks/chunk-G3RSMSXJ.ts.map +0 -7
- package/dist/chunks/chunk-I2FAK44T.ts.map +0 -7
- package/dist/chunks/chunk-NIF42QMF.ts.map +0 -7
- package/dist/chunks/chunk-QBORTI4W.ts.map +0 -7
- package/dist/chunks/chunk-S2IWVK3J.ts.map +0 -7
- package/dist/chunks/config-ui-2YUCUEBM.ts.map +0 -7
- package/dist/chunks/consult-IV7UBNQQ.ts.map +0 -7
- package/dist/chunks/execution-LA7HN5YF.ts.map +0 -7
- package/dist/chunks/inspect-XV2QALMR.ts.map +0 -7
- package/dist/chunks/persistence-VSGXAW4P.ts.map +0 -7
- package/dist/chunks/registry-E6XPJB7L.ts.map +0 -7
- package/dist/chunks/rpc-transport-NEHBHWX4.ts.map +0 -7
- package/dist/chunks/subprocess-transport-NSHOGDC4.ts.map +0 -7
- /package/dist/chunks/{auto-transport-SY2VHUFH.ts.map → auto-transport-FUUKFDIG.ts.map} +0 -0
- /package/dist/chunks/{capability-grant-CGEWOEKE.ts.map → capability-grant-PR72SWWS.ts.map} +0 -0
- /package/dist/chunks/{chunk-YBUWBRF7.ts.map → chunk-FXI45N3J.ts.map} +0 -0
- /package/dist/chunks/{chunk-ZHTNCZIA.ts.map → chunk-JU6LUNLP.ts.map} +0 -0
- /package/dist/chunks/{chunk-PSK43Y6A.ts.map → chunk-LASD73CM.ts.map} +0 -0
- /package/dist/chunks/{chunk-H3BFJ7HJ.ts.map → chunk-LL4LP2T7.ts.map} +0 -0
- /package/dist/chunks/{chunk-SCZ33MYW.ts.map → chunk-PGLSFLYW.ts.map} +0 -0
- /package/dist/chunks/{chunk-O4VO6JUJ.ts.map → chunk-TM2R67J3.ts.map} +0 -0
- /package/dist/chunks/{chunk-HKC4ES4B.ts.map → chunk-YPJEN6NU.ts.map} +0 -0
- /package/dist/chunks/{completion-delivery-YPOWSSV3.ts.map → completion-delivery-JVLNQRWX.ts.map} +0 -0
- /package/dist/chunks/{config-status-J4GPWP46.ts.map → config-status-FKGDECZ3.ts.map} +0 -0
- /package/dist/chunks/{create-stateful-transport-ZP3IXNV3.ts.map → create-stateful-transport-JWC2EFYL.ts.map} +0 -0
- /package/dist/chunks/{delegation-contract-WJPT4FVS.ts.map → delegation-contract-LA56I5DT.ts.map} +0 -0
- /package/dist/chunks/{discovery-K25T3SH4.ts.map → discovery-MIFB2U4Y.ts.map} +0 -0
- /package/dist/chunks/{in-process-transport-4RF3J6XK.ts.map → in-process-transport-HJ6TZXC3.ts.map} +0 -0
- /package/dist/chunks/{retained-semantic-state-7WXSGRJU.ts.map → retained-semantic-state-GM75FZGE.ts.map} +0 -0
- /package/dist/chunks/{spawn-idempotency-QGHO2WKC.ts.map → spawn-idempotency-BNHOSMZW.ts.map} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { redactPrivateText } from "./context.js";
|
|
2
2
|
import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
|
|
3
3
|
import { appendResultInstruction, type SubagentResultFormat } from "./result-contract.js";
|
|
4
|
-
import type { RecentActivityItem } from "./runner.js";
|
|
4
|
+
import type { RecentActivityItem } from "./runner-types.js";
|
|
5
5
|
import {
|
|
6
6
|
formatTimeoutCheckpoint,
|
|
7
7
|
type TimeoutCheckpoint,
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type TObject, type TObjectOptions, type TProperties, type TSchema, Type } from "typebox";
|
|
2
|
+
|
|
3
|
+
const LLAMA_CPP_MAX_REPETITION_THRESHOLD = 2_000;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Remove large nested string repetitions that llama.cpp cannot compile into GBNF.
|
|
7
|
+
*
|
|
8
|
+
* Runtime normalization and validation continue to bound these inputs.
|
|
9
|
+
* See https://github.com/ggml-org/llama.cpp/issues/25746.
|
|
10
|
+
*/
|
|
11
|
+
export function grammarSafeToolObject<Properties extends TProperties>(
|
|
12
|
+
properties: Properties,
|
|
13
|
+
options?: TObjectOptions,
|
|
14
|
+
): TObject<Properties> {
|
|
15
|
+
return makeGrammarSafeToolSchema(Type.Object(properties, options));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function makeGrammarSafeToolSchema<Schema extends TSchema>(schema: Schema): Schema {
|
|
19
|
+
return cloneSchema(schema, 0) as Schema;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function cloneSchema(schema: unknown, depth: number): unknown {
|
|
23
|
+
if (!isRecord(schema)) return schema;
|
|
24
|
+
const clone = cloneWithDescriptors(schema);
|
|
25
|
+
if (
|
|
26
|
+
depth > 1 &&
|
|
27
|
+
clone.type === "string" &&
|
|
28
|
+
typeof clone.maxLength === "number" &&
|
|
29
|
+
clone.maxLength >= LLAMA_CPP_MAX_REPETITION_THRESHOLD
|
|
30
|
+
) {
|
|
31
|
+
delete clone.maxLength;
|
|
32
|
+
}
|
|
33
|
+
if (isRecord(clone.properties)) {
|
|
34
|
+
clone.properties = mapSchemas(clone.properties, depth + 1);
|
|
35
|
+
}
|
|
36
|
+
if (clone.items !== undefined) {
|
|
37
|
+
clone.items = Array.isArray(clone.items)
|
|
38
|
+
? clone.items.map((item) => cloneSchema(item, depth + 1))
|
|
39
|
+
: cloneSchema(clone.items, depth + 1);
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(clone.prefixItems)) {
|
|
42
|
+
clone.prefixItems = clone.prefixItems.map((item) => cloneSchema(item, depth + 1));
|
|
43
|
+
}
|
|
44
|
+
for (const keyword of ["allOf", "anyOf", "oneOf"] as const) {
|
|
45
|
+
if (Array.isArray(clone[keyword])) {
|
|
46
|
+
clone[keyword] = clone[keyword].map((item) => cloneSchema(item, depth));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (isRecord(clone.patternProperties)) {
|
|
50
|
+
clone.patternProperties = mapSchemas(clone.patternProperties, depth + 1);
|
|
51
|
+
}
|
|
52
|
+
if (isRecord(clone.additionalProperties)) {
|
|
53
|
+
clone.additionalProperties = cloneSchema(clone.additionalProperties, depth + 1);
|
|
54
|
+
}
|
|
55
|
+
return clone;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function mapSchemas(schemas: Record<string, unknown>, depth: number): Record<string, unknown> {
|
|
59
|
+
return Object.fromEntries(
|
|
60
|
+
Object.entries(schemas).map(([name, schema]) => [name, cloneSchema(schema, depth)]),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function cloneWithDescriptors(value: Record<string, unknown>): Record<string, unknown> {
|
|
65
|
+
return Object.defineProperties(
|
|
66
|
+
Object.create(Object.getPrototypeOf(value)),
|
|
67
|
+
Object.getOwnPropertyDescriptors(value),
|
|
68
|
+
) as Record<string, unknown>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
72
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
73
|
+
}
|
package/src/transport-types.ts
CHANGED
|
@@ -50,6 +50,12 @@ export interface TransportTelemetry {
|
|
|
50
50
|
model?: string;
|
|
51
51
|
thinkingLevel?: SubagentThinkingLevel;
|
|
52
52
|
usage?: TransportUsage;
|
|
53
|
+
budgetSource?: {
|
|
54
|
+
timeout: "runtime" | "explicit";
|
|
55
|
+
idleTimeout: "runtime" | "explicit";
|
|
56
|
+
turnLimit: "runtime" | "explicit";
|
|
57
|
+
toolCallLimit: "runtime" | "explicit";
|
|
58
|
+
};
|
|
53
59
|
failurePhase?: TransportProgressPhase;
|
|
54
60
|
}
|
|
55
61
|
|
package/src/transport-ui.ts
CHANGED
|
@@ -11,25 +11,26 @@ const TRANSPORT_OPTIONS: Array<{
|
|
|
11
11
|
}> = [
|
|
12
12
|
{
|
|
13
13
|
value: "subprocess",
|
|
14
|
-
label: "Fresh
|
|
15
|
-
description:
|
|
14
|
+
label: "Fresh process",
|
|
15
|
+
description:
|
|
16
|
+
"Start a separate Pi process for every turn. Most compatible, but slower for follow-ups.",
|
|
16
17
|
},
|
|
17
18
|
{
|
|
18
19
|
value: "in-process",
|
|
19
|
-
label: "
|
|
20
|
+
label: "Inside Pi",
|
|
20
21
|
description:
|
|
21
|
-
"
|
|
22
|
+
"Run built-in tools inside Pi for faster follow-ups. Shares Pi's memory and crash boundary.",
|
|
22
23
|
},
|
|
23
24
|
{
|
|
24
25
|
value: "rpc",
|
|
25
|
-
label: "Persistent RPC
|
|
26
|
-
description:
|
|
26
|
+
label: "Persistent process (RPC)",
|
|
27
|
+
description:
|
|
28
|
+
"Keep one separate Pi process per background subagent so follow-ups preserve history.",
|
|
27
29
|
},
|
|
28
30
|
{
|
|
29
31
|
value: "auto",
|
|
30
|
-
label: "Automatic",
|
|
31
|
-
description:
|
|
32
|
-
"Read-only built-ins use in-process, write-capable built-ins use RPC, and custom tools use subprocess.",
|
|
32
|
+
label: "Automatic · Recommended",
|
|
33
|
+
description: "Let Pi choose a compatible method for each subagent and its tools.",
|
|
33
34
|
},
|
|
34
35
|
];
|
|
35
36
|
|
|
@@ -42,12 +43,15 @@ export function transportSettingsScreen(runtime: TransportUiRuntime) {
|
|
|
42
43
|
const current = runtime.getRuntimeStatus();
|
|
43
44
|
return {
|
|
44
45
|
kind: "actions" as const,
|
|
45
|
-
title: configured.error
|
|
46
|
+
title: configured.error
|
|
47
|
+
? "Background Agent Transport · Read only"
|
|
48
|
+
: "Background Agent Transport",
|
|
46
49
|
lines: [
|
|
50
|
+
"Advanced: choose how Pi hosts background subagents.",
|
|
47
51
|
`Current session: ${transportLabel(current.transport)}`,
|
|
48
52
|
`Configured after reload: ${transportLabel(configured.value)} (${configured.source})`,
|
|
49
|
-
"
|
|
50
|
-
"
|
|
53
|
+
"Separate processes do not restrict filesystem or network access.",
|
|
54
|
+
"Persistent process (RPC) supports built-in Pi tools and does not load child extensions.",
|
|
51
55
|
...(configured.error
|
|
52
56
|
? [
|
|
53
57
|
`Settings cannot be edited: ${safeTerminalText(configured.error)}`,
|
|
@@ -84,7 +88,7 @@ export async function applyTransportSetting(
|
|
|
84
88
|
const status = runtime.getRuntimeStatus();
|
|
85
89
|
if (status.retainedAgents > 0) {
|
|
86
90
|
ctx.ui.notify(
|
|
87
|
-
`Cannot change transport while ${status.retainedAgents}
|
|
91
|
+
`Cannot change transport while ${status.retainedAgents} subagent${status.retainedAgents === 1 ? " is" : "s are"} saved for follow-up. Clear Current subagents first.`,
|
|
88
92
|
"warning",
|
|
89
93
|
);
|
|
90
94
|
return { kind: "rejected" as const };
|
|
@@ -104,7 +108,7 @@ export async function applyTransportSetting(
|
|
|
104
108
|
}
|
|
105
109
|
if (runtime.getRuntimeStatus().retainedAgents > 0) {
|
|
106
110
|
ctx.ui.notify(
|
|
107
|
-
"
|
|
111
|
+
"New subagents were saved for follow-up while confirming. Clear them before changing transport.",
|
|
108
112
|
"warning",
|
|
109
113
|
);
|
|
110
114
|
return { kind: "rejected" as const };
|
|
@@ -122,38 +126,6 @@ export async function applyTransportSetting(
|
|
|
122
126
|
}
|
|
123
127
|
}
|
|
124
128
|
|
|
125
|
-
export function responsivenessSetupScreen(runtime: TransportUiRuntime) {
|
|
126
|
-
const current = runtime.getRuntimeStatus();
|
|
127
|
-
const configured = inspectStatefulTransportSettings();
|
|
128
|
-
return {
|
|
129
|
-
kind: "actions" as const,
|
|
130
|
-
title: "Responsiveness Setup",
|
|
131
|
-
lines: [
|
|
132
|
-
`Current transport: ${transportLabel(current.transport)}`,
|
|
133
|
-
`Configured transport: ${transportLabel(configured.value)}`,
|
|
134
|
-
"Transport, completion delivery, and thinking defaults are separate explicit choices.",
|
|
135
|
-
"Use Automatic to reduce retained follow-up startup while keeping custom-tool compatibility.",
|
|
136
|
-
],
|
|
137
|
-
items: [
|
|
138
|
-
{
|
|
139
|
-
id: "auto",
|
|
140
|
-
label: "Preview Automatic transport",
|
|
141
|
-
description: "Choose a deterministic transport before each retained agent starts",
|
|
142
|
-
action: "set-transport" as const,
|
|
143
|
-
},
|
|
144
|
-
{ id: "transport", label: "Compare all transports", to: "transport" as const },
|
|
145
|
-
{
|
|
146
|
-
id: "completion",
|
|
147
|
-
label: "Completion delivery",
|
|
148
|
-
description: "Choose separately whether an idle root resumes for synthesis",
|
|
149
|
-
to: "settings" as const,
|
|
150
|
-
},
|
|
151
|
-
{ id: "back", label: "Back", action: "back" as const },
|
|
152
|
-
],
|
|
153
|
-
hint: "back" as const,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
129
|
export function transportLabel(value: SubagentTransportKind): string {
|
|
158
130
|
return TRANSPORT_OPTIONS.find((option) => option.value === value)?.label ?? value;
|
|
159
131
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
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
|
+
}
|