@narumitw/pi-subagents 2.0.6 → 2.1.1
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 +171 -60
- 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-CBP76ARQ.ts → chunk-2LMJU25E.ts} +172 -74
- package/dist/chunks/chunk-2LMJU25E.ts.map +7 -0
- 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-YBUWBRF7.ts → chunk-6NSJVPXX.ts} +6 -16
- package/dist/chunks/{chunk-YBUWBRF7.ts.map → chunk-6NSJVPXX.ts.map} +2 -2
- 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-NIF42QMF.ts → chunk-FEVPWRMU.ts} +7 -4
- package/dist/chunks/chunk-FEVPWRMU.ts.map +7 -0
- 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-NLT67IZS.ts +322 -0
- package/dist/chunks/chunk-NLT67IZS.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-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-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/{chunk-434NII74.ts → chunk-YU53SHA7.ts} +54 -8
- package/dist/chunks/chunk-YU53SHA7.ts.map +7 -0
- package/dist/chunks/{completion-delivery-YPOWSSV3.ts → completion-delivery-RSJU6BXL.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-ABHYNGQ7.ts} +297 -231
- package/dist/chunks/config-ui-ABHYNGQ7.ts.map +7 -0
- package/dist/chunks/{consult-IV7UBNQQ.ts → consult-LJU3IQY5.ts} +14 -13
- package/dist/chunks/consult-LJU3IQY5.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-UY3PY6E5.ts} +11 -7
- package/dist/chunks/persistence-UY3PY6E5.ts.map +7 -0
- package/dist/chunks/{registry-E6XPJB7L.ts → registry-BT54L6CY.ts} +136 -15
- package/dist/chunks/registry-BT54L6CY.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 +682 -82
- package/dist/index.ts.map +3 -3
- package/docs/async-runtime-protocol.md +83 -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 +479 -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-registration.ts +4 -12
- 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/session-guidance-contract.ts +309 -0
- 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 +11 -18
- package/src/stateful-limit-ui.ts +23 -20
- package/src/stateful-limits.ts +10 -10
- package/src/stateful-registration.ts +126 -36
- package/src/stateful-render.ts +27 -2
- package/src/subagent-details.ts +43 -0
- package/src/subagents-extension.ts +116 -68
- 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-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-RSJU6BXL.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
package/src/completion-render.ts
CHANGED
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ContextEvent } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { DEFAULT_MAX_CONTEXT_BYTES, truncateUtf8 } from "./limits.js";
|
|
4
|
+
import type { AgentLifecycleState, ManagedAgent } from "./registry-types.js";
|
|
5
|
+
|
|
6
|
+
export const COMPLETION_REQUIREMENT_MODES = ["background", "required"] as const;
|
|
7
|
+
export type CompletionRequirementMode = (typeof COMPLETION_REQUIREMENT_MODES)[number];
|
|
8
|
+
|
|
9
|
+
export const CompletionRequirementModeSchema = StringEnum(COMPLETION_REQUIREMENT_MODES, {
|
|
10
|
+
description:
|
|
11
|
+
"Mark this exact turn as background (default) or required for the parent final answer. Required results are tracked until their exact completion becomes visible, but current Pi versions do not provide a hard pre-display final-answer barrier.",
|
|
12
|
+
default: "background",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const COMPLETION_REQUIREMENT_VERSION = "pi-subagents:completion-requirement:v1" as const;
|
|
16
|
+
export const COMPLETION_REQUIREMENT_CONTEXT_TYPE = "pi-subagent-required-completions";
|
|
17
|
+
export const COMPLETION_REQUIREMENT_TRANSITION_TYPE = "pi-subagent-required-completion-transition";
|
|
18
|
+
export const COMPLETION_REQUIREMENT_TRANSITION_VERSION =
|
|
19
|
+
"pi-subagents:completion-requirement-transition:v1" as const;
|
|
20
|
+
const MAX_REQUIREMENTS_PER_AGENT = 20;
|
|
21
|
+
export const MAX_UNRESOLVED_REQUIRED_COMPLETIONS = 64;
|
|
22
|
+
|
|
23
|
+
export type CompletionRequirementState = "pending" | "available" | "visible" | "cancelled";
|
|
24
|
+
|
|
25
|
+
export interface CompletionRequirementRecord {
|
|
26
|
+
version: typeof COMPLETION_REQUIREMENT_VERSION;
|
|
27
|
+
runId: string;
|
|
28
|
+
generation: number;
|
|
29
|
+
state: CompletionRequirementState;
|
|
30
|
+
createdAt: number;
|
|
31
|
+
updatedAt: number;
|
|
32
|
+
completionId?: string;
|
|
33
|
+
terminalState?: AgentLifecycleState;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function beginCompletionRequirement(
|
|
37
|
+
records: readonly CompletionRequirementRecord[] | undefined,
|
|
38
|
+
input: { runId: string; generation: number; createdAt: number },
|
|
39
|
+
): CompletionRequirementRecord[] {
|
|
40
|
+
const retained = (records ?? []).filter(
|
|
41
|
+
(record) => record.runId !== input.runId || record.generation !== input.generation,
|
|
42
|
+
);
|
|
43
|
+
retained.push({
|
|
44
|
+
version: COMPLETION_REQUIREMENT_VERSION,
|
|
45
|
+
runId: input.runId,
|
|
46
|
+
generation: input.generation,
|
|
47
|
+
state: "pending",
|
|
48
|
+
createdAt: input.createdAt,
|
|
49
|
+
updatedAt: input.createdAt,
|
|
50
|
+
});
|
|
51
|
+
return retained.slice(-MAX_REQUIREMENTS_PER_AGENT);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function makeCompletionAvailable(
|
|
55
|
+
records: readonly CompletionRequirementRecord[] | undefined,
|
|
56
|
+
input: {
|
|
57
|
+
runId: string;
|
|
58
|
+
generation: number;
|
|
59
|
+
completionId: string;
|
|
60
|
+
terminalState: AgentLifecycleState;
|
|
61
|
+
updatedAt: number;
|
|
62
|
+
},
|
|
63
|
+
): CompletionRequirementRecord[] | undefined {
|
|
64
|
+
return updateExact(records, input.runId, input.generation, (record) => ({
|
|
65
|
+
...record,
|
|
66
|
+
state: "available",
|
|
67
|
+
completionId: input.completionId,
|
|
68
|
+
terminalState: input.terminalState,
|
|
69
|
+
updatedAt: input.updatedAt,
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function makeCompletionVisible(
|
|
74
|
+
records: readonly CompletionRequirementRecord[] | undefined,
|
|
75
|
+
completionId: string,
|
|
76
|
+
updatedAt: number,
|
|
77
|
+
): CompletionRequirementRecord[] | undefined {
|
|
78
|
+
if (!records?.some((record) => record.completionId === completionId)) {
|
|
79
|
+
return records?.map((record) => ({ ...record }));
|
|
80
|
+
}
|
|
81
|
+
return records.map((record) =>
|
|
82
|
+
record.completionId === completionId
|
|
83
|
+
? { ...record, state: "visible" as const, updatedAt }
|
|
84
|
+
: { ...record },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function cancelPendingCompletionRequirements(
|
|
89
|
+
records: readonly CompletionRequirementRecord[] | undefined,
|
|
90
|
+
terminalState: AgentLifecycleState,
|
|
91
|
+
updatedAt: number,
|
|
92
|
+
): CompletionRequirementRecord[] | undefined {
|
|
93
|
+
if (!records?.some((record) => record.state === "pending")) {
|
|
94
|
+
return records?.map((record) => ({ ...record }));
|
|
95
|
+
}
|
|
96
|
+
return records.map((record) =>
|
|
97
|
+
record.state === "pending"
|
|
98
|
+
? {
|
|
99
|
+
...record,
|
|
100
|
+
state: "cancelled" as const,
|
|
101
|
+
terminalState,
|
|
102
|
+
updatedAt,
|
|
103
|
+
}
|
|
104
|
+
: { ...record },
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function requirementForCompletion(
|
|
109
|
+
agent: Pick<ManagedAgent, "completionRequirements">,
|
|
110
|
+
completionId: string,
|
|
111
|
+
): CompletionRequirementRecord | undefined {
|
|
112
|
+
return agent.completionRequirements?.find((record) => record.completionId === completionId);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export interface BranchCompletionRequirementState {
|
|
116
|
+
observedState: boolean;
|
|
117
|
+
records: Map<string, CompletionRequirementRecord>;
|
|
118
|
+
keys: Set<string>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function completionRequirementKey(
|
|
122
|
+
record: Pick<CompletionRequirementRecord, "runId" | "generation">,
|
|
123
|
+
): string {
|
|
124
|
+
return `${record.runId}\u0000${record.generation}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function completionRequirementsFromBranch(
|
|
128
|
+
entries: readonly unknown[],
|
|
129
|
+
): BranchCompletionRequirementState {
|
|
130
|
+
const records = new Map<string, CompletionRequirementRecord>();
|
|
131
|
+
let observedState = false;
|
|
132
|
+
for (const entry of entries) {
|
|
133
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
|
|
134
|
+
const candidate = entry as Record<string, unknown>;
|
|
135
|
+
const message =
|
|
136
|
+
candidate.type === "message" && candidate.message && typeof candidate.message === "object"
|
|
137
|
+
? (candidate.message as Record<string, unknown>)
|
|
138
|
+
: candidate;
|
|
139
|
+
if (
|
|
140
|
+
message.role === "toolResult" &&
|
|
141
|
+
["subagent_spawn", "subagent_send", "subagent_await", "subagent_manage"].includes(
|
|
142
|
+
String(message.toolName),
|
|
143
|
+
)
|
|
144
|
+
) {
|
|
145
|
+
observedState = true;
|
|
146
|
+
const details = message.details;
|
|
147
|
+
if (!details || typeof details !== "object" || Array.isArray(details)) continue;
|
|
148
|
+
const detailsRecord = details as Record<string, unknown>;
|
|
149
|
+
const agents = [
|
|
150
|
+
detailsRecord.agent,
|
|
151
|
+
...(Array.isArray(detailsRecord.agents) ? detailsRecord.agents : []),
|
|
152
|
+
];
|
|
153
|
+
for (const agent of agents) {
|
|
154
|
+
if (!agent || typeof agent !== "object" || Array.isArray(agent)) continue;
|
|
155
|
+
const requirements = (agent as Record<string, unknown>).completionRequirements;
|
|
156
|
+
if (!Array.isArray(requirements)) continue;
|
|
157
|
+
for (const requirement of requirements) {
|
|
158
|
+
if (isCompletionRequirementRecord(requirement)) {
|
|
159
|
+
records.set(completionRequirementKey(requirement), { ...requirement });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (
|
|
166
|
+
message.role === "custom" &&
|
|
167
|
+
message.customType === COMPLETION_REQUIREMENT_TRANSITION_TYPE
|
|
168
|
+
) {
|
|
169
|
+
const details = message.details;
|
|
170
|
+
if (!details || typeof details !== "object" || Array.isArray(details)) continue;
|
|
171
|
+
const detailRecord = details as Record<string, unknown>;
|
|
172
|
+
if (detailRecord.version !== COMPLETION_REQUIREMENT_TRANSITION_VERSION) continue;
|
|
173
|
+
const requirements = Array.isArray(detailRecord.records) ? detailRecord.records : [];
|
|
174
|
+
for (const requirement of requirements) {
|
|
175
|
+
if (!isCompletionRequirementRecord(requirement) || requirement.state !== "cancelled") {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
observedState = true;
|
|
179
|
+
records.set(completionRequirementKey(requirement), { ...requirement });
|
|
180
|
+
}
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (message.role !== "custom" || message.customType !== "pi-subagent-completion") continue;
|
|
184
|
+
const details = message.details;
|
|
185
|
+
if (!details || typeof details !== "object" || Array.isArray(details)) continue;
|
|
186
|
+
const detailRecord = details as Record<string, unknown>;
|
|
187
|
+
const completionRecords = Array.isArray(detailRecord.completions)
|
|
188
|
+
? detailRecord.completions
|
|
189
|
+
: [detailRecord];
|
|
190
|
+
for (const completion of completionRecords) {
|
|
191
|
+
if (!completion || typeof completion !== "object" || Array.isArray(completion)) continue;
|
|
192
|
+
const requirement = (completion as Record<string, unknown>).completionRequirement;
|
|
193
|
+
if (!isCompletionRequirementRecord(requirement)) continue;
|
|
194
|
+
records.set(completionRequirementKey(requirement), {
|
|
195
|
+
...requirement,
|
|
196
|
+
state: "visible",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { observedState, records, keys: new Set(records.keys()) };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function pendingRequiredCompletionCount(
|
|
204
|
+
agent: Pick<ManagedAgent, "completionRequirements">,
|
|
205
|
+
): number {
|
|
206
|
+
return (agent.completionRequirements ?? []).filter(
|
|
207
|
+
(record) => record.state === "pending" || record.state === "available",
|
|
208
|
+
).length;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function createRequiredCompletionTransition(
|
|
212
|
+
messages: ContextEvent["messages"],
|
|
213
|
+
agents: readonly ManagedAgent[],
|
|
214
|
+
) {
|
|
215
|
+
const summarized = leadingSummaryBoundary(messages) > 0;
|
|
216
|
+
const state = modelRequirementState(agents);
|
|
217
|
+
const visible = completionRequirementsFromBranch(messages).records;
|
|
218
|
+
const records = state.records.filter((record) => {
|
|
219
|
+
if (record.state !== "cancelled") return false;
|
|
220
|
+
const retained = visible.get(completionRequirementKey(record));
|
|
221
|
+
if (modelRequirementStateMatches(retained, record)) return false;
|
|
222
|
+
return !summarized || retained !== undefined;
|
|
223
|
+
});
|
|
224
|
+
if (records.length === 0) return undefined;
|
|
225
|
+
return {
|
|
226
|
+
role: "custom" as const,
|
|
227
|
+
customType: COMPLETION_REQUIREMENT_TRANSITION_TYPE,
|
|
228
|
+
content: requiredCompletionTransitionContent(records, state.omittedCancelled),
|
|
229
|
+
display: false,
|
|
230
|
+
details: {
|
|
231
|
+
version: COMPLETION_REQUIREMENT_TRANSITION_VERSION,
|
|
232
|
+
records: records.map((record) => ({ ...record })),
|
|
233
|
+
},
|
|
234
|
+
timestamp: 0,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function reconcileRequiredCompletionContext(
|
|
239
|
+
messages: ContextEvent["messages"],
|
|
240
|
+
agents: readonly ManagedAgent[],
|
|
241
|
+
restorationPredecessors: readonly string[] = [],
|
|
242
|
+
restoredBoundaryContent?: string,
|
|
243
|
+
): ContextEvent["messages"] {
|
|
244
|
+
const existing = messages.filter(isRequiredCompletionContextMessage);
|
|
245
|
+
const withoutPrior = messages.filter((message) => !isRequiredCompletionContextMessage(message));
|
|
246
|
+
const state = modelRequirementState(agents);
|
|
247
|
+
const summaryBoundary = leadingSummaryBoundary(withoutPrior);
|
|
248
|
+
const currentContent =
|
|
249
|
+
state.records.length > 0 && !hasModelVisibleRequirementState(withoutPrior, state.records)
|
|
250
|
+
? requiredCompletionContextContent(state.records, state.omittedCancelled)
|
|
251
|
+
: undefined;
|
|
252
|
+
const content = summaryBoundary > 0 ? (restoredBoundaryContent ?? currentContent) : undefined;
|
|
253
|
+
let insertionBoundary = summaryBoundary;
|
|
254
|
+
while (insertionBoundary < withoutPrior.length) {
|
|
255
|
+
const predecessor = withoutPrior[insertionBoundary];
|
|
256
|
+
if (
|
|
257
|
+
predecessor?.role !== "custom" ||
|
|
258
|
+
!restorationPredecessors.includes(predecessor.customType)
|
|
259
|
+
) {
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
insertionBoundary += 1;
|
|
263
|
+
}
|
|
264
|
+
if (
|
|
265
|
+
content !== undefined &&
|
|
266
|
+
existing.length === 1 &&
|
|
267
|
+
messages[insertionBoundary] === existing[0] &&
|
|
268
|
+
existing[0]?.content === content &&
|
|
269
|
+
hasCompletionRequirementContextVersion(existing[0])
|
|
270
|
+
) {
|
|
271
|
+
return messages;
|
|
272
|
+
}
|
|
273
|
+
if (existing.length === 0 && content === undefined) return messages;
|
|
274
|
+
if (content === undefined) return withoutPrior;
|
|
275
|
+
return [
|
|
276
|
+
...withoutPrior.slice(0, insertionBoundary),
|
|
277
|
+
{
|
|
278
|
+
role: "custom",
|
|
279
|
+
customType: COMPLETION_REQUIREMENT_CONTEXT_TYPE,
|
|
280
|
+
content,
|
|
281
|
+
display: false,
|
|
282
|
+
details: { version: COMPLETION_REQUIREMENT_VERSION },
|
|
283
|
+
timestamp: 0,
|
|
284
|
+
},
|
|
285
|
+
...withoutPrior.slice(insertionBoundary),
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
interface ModelRequirementRecord {
|
|
290
|
+
state: CompletionRequirementState;
|
|
291
|
+
runId: string;
|
|
292
|
+
generation: number;
|
|
293
|
+
terminalState: AgentLifecycleState | undefined;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function modelRequirementState(agents: readonly ManagedAgent[]): {
|
|
297
|
+
records: CompletionRequirementRecord[];
|
|
298
|
+
omittedCancelled: number;
|
|
299
|
+
} {
|
|
300
|
+
const allRecords = agents
|
|
301
|
+
.flatMap((agent) =>
|
|
302
|
+
(agent.completionRequirements ?? []).map((requirement) => ({ ...requirement })),
|
|
303
|
+
)
|
|
304
|
+
.filter((record) => record.state !== "visible");
|
|
305
|
+
const unresolved = allRecords.filter(
|
|
306
|
+
(record) => record.state === "pending" || record.state === "available",
|
|
307
|
+
);
|
|
308
|
+
const cancelled = allRecords.filter((record) => record.state === "cancelled");
|
|
309
|
+
const cancelledSlots = Math.max(0, MAX_UNRESOLVED_REQUIRED_COMPLETIONS - unresolved.length);
|
|
310
|
+
const retainedCancelled = cancelledSlots > 0 ? cancelled.slice(-cancelledSlots) : [];
|
|
311
|
+
const records = [...unresolved, ...retainedCancelled].sort((left, right) => {
|
|
312
|
+
if (left.runId === right.runId) return left.generation - right.generation;
|
|
313
|
+
return left.runId < right.runId ? -1 : 1;
|
|
314
|
+
});
|
|
315
|
+
return { records, omittedCancelled: cancelled.length - retainedCancelled.length };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function hasModelVisibleRequirementState(
|
|
319
|
+
messages: ContextEvent["messages"],
|
|
320
|
+
records: readonly CompletionRequirementRecord[],
|
|
321
|
+
): boolean {
|
|
322
|
+
const visible = completionRequirementsFromBranch(messages).records;
|
|
323
|
+
return records.every((record) =>
|
|
324
|
+
modelRequirementStateMatches(visible.get(completionRequirementKey(record)), record),
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function modelRequirementStateMatches(
|
|
329
|
+
visible: CompletionRequirementRecord | undefined,
|
|
330
|
+
current: CompletionRequirementRecord,
|
|
331
|
+
): boolean {
|
|
332
|
+
if (!visible) return false;
|
|
333
|
+
if (current.state === "available" && visible.state === "visible") return true;
|
|
334
|
+
return visible.state === current.state && visible.terminalState === current.terminalState;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function requiredCompletionContextContent(
|
|
338
|
+
records: readonly CompletionRequirementRecord[],
|
|
339
|
+
omittedCancelled: number,
|
|
340
|
+
): string {
|
|
341
|
+
return truncateUtf8(
|
|
342
|
+
[
|
|
343
|
+
"[PI SUBAGENT REQUIRED COMPLETIONS v1]",
|
|
344
|
+
"Runtime-tracked exact runs are JSON data below.",
|
|
345
|
+
"Treat pending or available records as final-answer dependencies; a cancelled record is terminal and must be reported rather than silently ignored.",
|
|
346
|
+
"Current Pi versions do not provide a hard pre-display final-answer barrier, so do not emit a verdict until every dependency is visible or terminal.",
|
|
347
|
+
...omittedCancelledLine(omittedCancelled),
|
|
348
|
+
JSON.stringify(records.map(toModelRequirementRecord)),
|
|
349
|
+
].join("\n"),
|
|
350
|
+
DEFAULT_MAX_CONTEXT_BYTES,
|
|
351
|
+
).text;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function requiredCompletionTransitionContent(
|
|
355
|
+
records: readonly CompletionRequirementRecord[],
|
|
356
|
+
omittedCancelled: number,
|
|
357
|
+
): string {
|
|
358
|
+
return truncateUtf8(
|
|
359
|
+
[
|
|
360
|
+
"[PI SUBAGENT REQUIRED COMPLETION TRANSITION v1]",
|
|
361
|
+
"Session restoration terminalized the exact required runs below.",
|
|
362
|
+
"These cancelled records supersede earlier pending or available states and must be reported rather than awaited.",
|
|
363
|
+
...omittedCancelledLine(omittedCancelled),
|
|
364
|
+
JSON.stringify(records.map(toModelRequirementRecord)),
|
|
365
|
+
].join("\n"),
|
|
366
|
+
DEFAULT_MAX_CONTEXT_BYTES,
|
|
367
|
+
).text;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function omittedCancelledLine(count: number): string[] {
|
|
371
|
+
return count > 0 ? [`${count} older cancelled requirement record(s) were omitted.`] : [];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function toModelRequirementRecord(record: CompletionRequirementRecord): ModelRequirementRecord {
|
|
375
|
+
return {
|
|
376
|
+
state: record.state,
|
|
377
|
+
runId: record.runId,
|
|
378
|
+
generation: record.generation,
|
|
379
|
+
terminalState: record.terminalState,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
type RequiredCompletionContextMessage = Extract<
|
|
384
|
+
ContextEvent["messages"][number],
|
|
385
|
+
{ role: "custom" }
|
|
386
|
+
> & { content: string };
|
|
387
|
+
|
|
388
|
+
function isRequiredCompletionContextMessage(
|
|
389
|
+
message: ContextEvent["messages"][number],
|
|
390
|
+
): message is RequiredCompletionContextMessage {
|
|
391
|
+
return message.role === "custom" && message.customType === COMPLETION_REQUIREMENT_CONTEXT_TYPE;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function hasCompletionRequirementContextVersion(
|
|
395
|
+
message: RequiredCompletionContextMessage,
|
|
396
|
+
): boolean {
|
|
397
|
+
return (
|
|
398
|
+
typeof message.details === "object" &&
|
|
399
|
+
message.details !== null &&
|
|
400
|
+
!Array.isArray(message.details) &&
|
|
401
|
+
(message.details as Record<string, unknown>).version === COMPLETION_REQUIREMENT_VERSION
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function leadingSummaryBoundary(messages: ContextEvent["messages"]): number {
|
|
406
|
+
let index = 0;
|
|
407
|
+
while (index < messages.length) {
|
|
408
|
+
const role = messages[index]?.role;
|
|
409
|
+
if (role !== "compactionSummary" && role !== "branchSummary") break;
|
|
410
|
+
index += 1;
|
|
411
|
+
}
|
|
412
|
+
return index;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export function isCompletionRequirementRecord(
|
|
416
|
+
value: unknown,
|
|
417
|
+
): value is CompletionRequirementRecord {
|
|
418
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
419
|
+
const record = value as Record<string, unknown>;
|
|
420
|
+
const state = String(record.state);
|
|
421
|
+
const hasCompletion = typeof record.completionId === "string" && record.completionId.length > 0;
|
|
422
|
+
const hasTerminalState = isAgentLifecycleState(record.terminalState);
|
|
423
|
+
const transitionShapeValid =
|
|
424
|
+
state === "pending"
|
|
425
|
+
? record.completionId === undefined && record.terminalState === undefined
|
|
426
|
+
: state === "available" || state === "visible"
|
|
427
|
+
? hasCompletion && hasTerminalState
|
|
428
|
+
: state === "cancelled" && record.completionId === undefined && hasTerminalState;
|
|
429
|
+
return (
|
|
430
|
+
transitionShapeValid &&
|
|
431
|
+
record.version === COMPLETION_REQUIREMENT_VERSION &&
|
|
432
|
+
typeof record.runId === "string" &&
|
|
433
|
+
record.runId.length > 0 &&
|
|
434
|
+
record.runId.length <= 256 &&
|
|
435
|
+
typeof record.generation === "number" &&
|
|
436
|
+
Number.isSafeInteger(record.generation) &&
|
|
437
|
+
record.generation >= 1 &&
|
|
438
|
+
["pending", "available", "visible", "cancelled"].includes(state) &&
|
|
439
|
+
typeof record.createdAt === "number" &&
|
|
440
|
+
Number.isFinite(record.createdAt) &&
|
|
441
|
+
typeof record.updatedAt === "number" &&
|
|
442
|
+
Number.isFinite(record.updatedAt) &&
|
|
443
|
+
record.updatedAt >= record.createdAt &&
|
|
444
|
+
(record.completionId === undefined ||
|
|
445
|
+
(typeof record.completionId === "string" && record.completionId.length <= 256)) &&
|
|
446
|
+
(record.terminalState === undefined || isAgentLifecycleState(record.terminalState))
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function updateExact(
|
|
451
|
+
records: readonly CompletionRequirementRecord[] | undefined,
|
|
452
|
+
runId: string,
|
|
453
|
+
generation: number,
|
|
454
|
+
update: (record: CompletionRequirementRecord) => CompletionRequirementRecord,
|
|
455
|
+
): CompletionRequirementRecord[] | undefined {
|
|
456
|
+
if (!records?.some((record) => record.runId === runId && record.generation === generation)) {
|
|
457
|
+
return records?.map((record) => ({ ...record }));
|
|
458
|
+
}
|
|
459
|
+
return records.map((record) =>
|
|
460
|
+
record.runId === runId && record.generation === generation ? update(record) : { ...record },
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function isAgentLifecycleState(value: unknown): value is AgentLifecycleState {
|
|
465
|
+
return [
|
|
466
|
+
"starting",
|
|
467
|
+
"running",
|
|
468
|
+
"idle",
|
|
469
|
+
"completed",
|
|
470
|
+
"partial",
|
|
471
|
+
"blocked",
|
|
472
|
+
"needs-input",
|
|
473
|
+
"abstained",
|
|
474
|
+
"stale",
|
|
475
|
+
"interrupted",
|
|
476
|
+
"failed",
|
|
477
|
+
"closed",
|
|
478
|
+
].includes(String(value));
|
|
479
|
+
}
|
|
@@ -3,9 +3,9 @@ import { cachedModuleLoader } from "./cached-module-loader.js";
|
|
|
3
3
|
import type { SubagentMenuOwner, SubagentSettingsRuntime } from "./config-ui.js";
|
|
4
4
|
|
|
5
5
|
const SUBCOMMANDS = [
|
|
6
|
-
{ value: "settings", label: "settings", description: "
|
|
7
|
-
{ value: "status", label: "status", description: "Show
|
|
8
|
-
{ value: "help", label: "help", description: "Show subagent
|
|
6
|
+
{ value: "settings", label: "settings", description: "Open grouped subagent settings" },
|
|
7
|
+
{ value: "status", label: "status", description: "Show detailed subagent diagnostics" },
|
|
8
|
+
{ value: "help", label: "help", description: "Show subagent first steps and safety help" },
|
|
9
9
|
];
|
|
10
10
|
|
|
11
11
|
type ConfigUiModule = Pick<
|
|
@@ -50,7 +50,7 @@ export function registerSubagentConfigCommand(
|
|
|
50
50
|
dependencies.loadConfigStatus ?? (() => import("./config-status.js")),
|
|
51
51
|
);
|
|
52
52
|
pi.registerCommand("subagents", {
|
|
53
|
-
description: "Manage
|
|
53
|
+
description: "Manage subagents, settings, diagnostics, and help",
|
|
54
54
|
getArgumentCompletions(prefix: string) {
|
|
55
55
|
const normalized = prefix.trim().toLowerCase();
|
|
56
56
|
const matches = SUBCOMMANDS.filter((item) => item.value.startsWith(normalized));
|
|
@@ -103,7 +103,7 @@ export function registerSubagentConfigCommand(
|
|
|
103
103
|
}
|
|
104
104
|
if (!isCurrent()) return;
|
|
105
105
|
if (!subcommand) await configUi.showSubagentManager(pi, ctx, runtime, owner);
|
|
106
|
-
else await configUi.showSubagentSettings(ctx, runtime, owner);
|
|
106
|
+
else await configUi.showSubagentSettings(pi, ctx, runtime, owner);
|
|
107
107
|
return;
|
|
108
108
|
}
|
|
109
109
|
if (ctx.mode === "tui" || ctx.hasUI) {
|