@timmyjl12/pi-subagents 0.0.0-stage → 0.21.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/CHANGELOG.md +862 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1125 -3
- package/SECURITY.md +97 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/active-agents-prompt.d.ts +40 -0
- package/dist/active-agents-prompt.js +98 -0
- package/dist/advisor-status.d.ts +16 -0
- package/dist/advisor-status.js +74 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +131 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +264 -0
- package/dist/agent-manager.d.ts +872 -0
- package/dist/agent-manager.js +2910 -0
- package/dist/agent-runner.d.ts +358 -0
- package/dist/agent-runner.js +1219 -0
- package/dist/agent-types.d.ts +142 -0
- package/dist/agent-types.js +315 -0
- package/dist/awaited-result.d.ts +37 -0
- package/dist/awaited-result.js +150 -0
- package/dist/child-context.d.ts +15 -0
- package/dist/child-context.js +181 -0
- package/dist/child-session-shutdown.d.ts +21 -0
- package/dist/child-session-shutdown.js +97 -0
- package/dist/cleanup-plans.d.ts +10 -0
- package/dist/cleanup-plans.js +112 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +73 -0
- package/dist/cross-extension-rpc.js +192 -0
- package/dist/custom-agents.d.ts +79 -0
- package/dist/custom-agents.js +583 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +125 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/error-log.d.ts +64 -0
- package/dist/error-log.js +176 -0
- package/dist/force-main-model.d.ts +21 -0
- package/dist/force-main-model.js +28 -0
- package/dist/goal-bridge.d.ts +155 -0
- package/dist/goal-bridge.js +305 -0
- package/dist/group-join.d.ts +36 -0
- package/dist/group-join.js +161 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +6390 -0
- package/dist/invocation-config.d.ts +164 -0
- package/dist/invocation-config.js +158 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +178 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +76 -0
- package/dist/nested-tools.js +393 -0
- package/dist/output-diagnostics.d.ts +11 -0
- package/dist/output-diagnostics.js +64 -0
- package/dist/output-file.d.ts +64 -0
- package/dist/output-file.js +351 -0
- package/dist/prompts.d.ts +61 -0
- package/dist/prompts.js +94 -0
- package/dist/recovery.d.ts +56 -0
- package/dist/recovery.js +341 -0
- package/dist/report-cleanup.d.ts +204 -0
- package/dist/report-cleanup.js +1632 -0
- package/dist/report-store.d.ts +738 -0
- package/dist/report-store.js +3612 -0
- package/dist/schedule-store.d.ts +52 -0
- package/dist/schedule-store.js +240 -0
- package/dist/schedule.d.ts +121 -0
- package/dist/schedule.js +420 -0
- package/dist/settings.d.ts +388 -0
- package/dist/settings.js +268 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/standalone-fallback.d.ts +30 -0
- package/dist/standalone-fallback.js +239 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/terminal-progress.d.ts +115 -0
- package/dist/terminal-progress.js +206 -0
- package/dist/types.d.ts +411 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +304 -0
- package/dist/ui/agent-widget.js +770 -0
- package/dist/ui/cleanup-preview.d.ts +59 -0
- package/dist/ui/cleanup-preview.js +466 -0
- package/dist/ui/conversation-viewer.d.ts +105 -0
- package/dist/ui/conversation-viewer.js +544 -0
- package/dist/ui/fleet-list.d.ts +183 -0
- package/dist/ui/fleet-list.js +494 -0
- package/dist/ui/fleet-overlay.d.ts +203 -0
- package/dist/ui/fleet-overlay.js +887 -0
- package/dist/ui/hub-feed.d.ts +52 -0
- package/dist/ui/hub-feed.js +148 -0
- package/dist/ui/native-transcript.d.ts +33 -0
- package/dist/ui/native-transcript.js +234 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/tool-progress.d.ts +22 -0
- package/dist/ui/tool-progress.js +56 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +848 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +147 -0
- package/dist/usage.js +147 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +65 -0
- package/dist/workflow/host.js +477 -0
- package/dist/workflow/journal.d.ts +113 -0
- package/dist/workflow/journal.js +177 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +373 -0
- package/dist/workflow/runtime.js +919 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +145 -0
- package/dist/workflow/task.js +213 -0
- package/dist/workflow/tool-description.d.ts +43 -0
- package/dist/workflow/tool-description.js +204 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/DEVELOPMENT.md +40 -0
- package/docs/artifacts.md +93 -0
- package/docs/recovery.md +62 -0
- package/docs/rpc.md +252 -0
- package/docs/runtime-performance.md +53 -0
- package/docs/workflows.md +467 -0
- package/examples/agent-tool-description.md +43 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +66 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +75 -6
- package/src/abortable.ts +43 -0
- package/src/active-agents-prompt.ts +115 -0
- package/src/advisor-status.ts +74 -0
- package/src/agent-color.ts +169 -0
- package/src/agent-file-toggle.ts +273 -0
- package/src/agent-manager.ts +3312 -0
- package/src/agent-runner.ts +1500 -0
- package/src/agent-types.ts +390 -0
- package/src/awaited-result.ts +187 -0
- package/src/child-context.ts +173 -0
- package/src/child-session-shutdown.ts +114 -0
- package/src/cleanup-plans.ts +112 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +257 -0
- package/src/custom-agents.ts +689 -0
- package/src/default-agents.ts +129 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/error-log.ts +202 -0
- package/src/force-main-model.ts +29 -0
- package/src/goal-bridge.ts +468 -0
- package/src/group-join.ts +188 -0
- package/src/index.ts +6856 -0
- package/src/invocation-config.ts +276 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +221 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +553 -0
- package/src/output-diagnostics.ts +68 -0
- package/src/output-file.ts +380 -0
- package/src/prompts.ts +151 -0
- package/src/recovery.ts +320 -0
- package/src/report-cleanup.ts +2044 -0
- package/src/report-store.ts +4130 -0
- package/src/schedule-store.ts +244 -0
- package/src/schedule.ts +450 -0
- package/src/settings.ts +632 -0
- package/src/skill-loader.ts +102 -0
- package/src/standalone-fallback.ts +195 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/terminal-progress.ts +215 -0
- package/src/types.ts +424 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +866 -0
- package/src/ui/cleanup-preview.ts +518 -0
- package/src/ui/conversation-viewer.ts +527 -0
- package/src/ui/fleet-list.ts +563 -0
- package/src/ui/fleet-overlay.ts +1048 -0
- package/src/ui/hub-feed.ts +179 -0
- package/src/ui/native-transcript.ts +224 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/tool-progress.ts +67 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1119 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +217 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +524 -0
- package/src/workflow/journal.ts +221 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1342 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +313 -0
- package/src/workflow/tool-description.ts +205 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* structured-output.ts — the synthetic tool behind `agent(prompt, { schema })`.
|
|
3
|
+
*
|
|
4
|
+
* A workflow script that passes a `schema` wants an *object* back, not prose it
|
|
5
|
+
* has to parse. Claude Code does this by giving the child a `StructuredOutput`
|
|
6
|
+
* tool whose input schema is the caller's schema, so the provider fills the
|
|
7
|
+
* fields, and returning the validated payload as the agent's result.
|
|
8
|
+
*
|
|
9
|
+
* We do the same, with one gap named up front: Claude Code *forces* the call,
|
|
10
|
+
* and we cannot. `toolChoice` exists in pi-ai's provider layer but is not
|
|
11
|
+
* plumbed through `AgentSession`, so an extension has no way to require a
|
|
12
|
+
* particular tool. What we have instead is three softer pressures —
|
|
13
|
+
*
|
|
14
|
+
* 1. `constrainedSampling`, so providers that support it hold the payload to
|
|
15
|
+
* the schema at sampling time;
|
|
16
|
+
* 2. the tool's description, snippet and guideline, which say the answer must
|
|
17
|
+
* come through this call;
|
|
18
|
+
* 3. validation here, answering a bad payload with `isError` so the model
|
|
19
|
+
* sees what was wrong and calls again inside the same run.
|
|
20
|
+
*
|
|
21
|
+
* — and, when all three fail, one more prompt from `runAgent`. See
|
|
22
|
+
* {@link structuredRetryPrompt}.
|
|
23
|
+
*
|
|
24
|
+
* The name matches Claude Code's exactly, so a ported prompt that mentions
|
|
25
|
+
* `StructuredOutput` is still telling the truth.
|
|
26
|
+
*/
|
|
27
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
28
|
+
/**
|
|
29
|
+
* Deliberately NOT added to `SUBAGENT_TOOL_NAMES`: that list becomes
|
|
30
|
+
* `EXCLUDED_TOOL_NAMES`, which is exactly the denial this tool has to avoid.
|
|
31
|
+
* Nor to `BUILTIN_TOOL_NAMES` — it is ours to inject, never a name a user may
|
|
32
|
+
* ask for in an agent's `tools:` frontmatter.
|
|
33
|
+
*/
|
|
34
|
+
export const STRUCTURED_OUTPUT_TOOL_NAME = "StructuredOutput";
|
|
35
|
+
export function createStructuredCapture() {
|
|
36
|
+
return { called: false };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Build the tool for one child.
|
|
40
|
+
*
|
|
41
|
+
* `capture` is the box the caller reads afterwards. It is passed in rather than
|
|
42
|
+
* returned so `runAgent` owns its lifetime and can consult it on every exit
|
|
43
|
+
* path, including the ones where the tool was never reached.
|
|
44
|
+
*/
|
|
45
|
+
export function createStructuredOutputTool(compiled, capture) {
|
|
46
|
+
return defineTool({
|
|
47
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
48
|
+
label: "Structured Output",
|
|
49
|
+
description: "Report your final answer. Call this exactly once, with the complete result, and put everything the "
|
|
50
|
+
+ "caller needs inside the arguments — text written outside this call is discarded. If a call is "
|
|
51
|
+
+ "rejected for not matching the schema, fix the reported fields and call it again.",
|
|
52
|
+
promptSnippet: "Report your final answer as structured data",
|
|
53
|
+
promptGuidelines: [
|
|
54
|
+
"Your final answer MUST be reported by calling StructuredOutput. Prose outside that call is discarded.",
|
|
55
|
+
],
|
|
56
|
+
// The caller's schema *is* the tool's input schema, verbatim — that is what
|
|
57
|
+
// makes the provider fill the fields. pi types this as TypeBox's `TSchema`,
|
|
58
|
+
// which v1 defines as an open interface, so a plain JSON Schema satisfies
|
|
59
|
+
// it without a cast at runtime or a conversion at author time.
|
|
60
|
+
parameters: compiled.schema,
|
|
61
|
+
// "prefer", not "require": a provider that cannot constrain sampling should
|
|
62
|
+
// fall through to validation-and-retry rather than fail the call outright.
|
|
63
|
+
constrainedSampling: { type: "json_schema", strict: "prefer" },
|
|
64
|
+
// Models occasionally send the whole payload as one JSON string instead of
|
|
65
|
+
// an object. Recovering that costs nothing and saves a whole retry.
|
|
66
|
+
prepareArguments: (args) => {
|
|
67
|
+
if (typeof args !== "string")
|
|
68
|
+
return args;
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(args);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return args;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
execute: async (_toolCallId, params) => {
|
|
77
|
+
capture.called = true;
|
|
78
|
+
const verdict = compiled.check(params);
|
|
79
|
+
if (verdict !== true) {
|
|
80
|
+
capture.lastError = verdict;
|
|
81
|
+
// `isError` puts the reason in front of the model as a tool result, so
|
|
82
|
+
// it can correct itself inside this same run. This is where most
|
|
83
|
+
// mismatches are resolved; the prompt-level retry is the backstop.
|
|
84
|
+
return {
|
|
85
|
+
content: [{
|
|
86
|
+
type: "text",
|
|
87
|
+
text: `StructuredOutput did not match the required schema:\n${verdict}\nCall it again with a corrected value.`,
|
|
88
|
+
}],
|
|
89
|
+
isError: true,
|
|
90
|
+
details: {},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// Last valid call wins: a model that calls twice meant the second one.
|
|
94
|
+
capture.json = JSON.stringify(params);
|
|
95
|
+
capture.lastError = undefined;
|
|
96
|
+
return { content: [{ type: "text", text: "Recorded." }], details: {} };
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The one extra prompt sent when a run ended with nothing captured.
|
|
102
|
+
*
|
|
103
|
+
* Distinguishes "never called it" from "called it wrongly" — the two need
|
|
104
|
+
* different corrections, and telling a model it got the shape wrong when it
|
|
105
|
+
* never answered at all sends it looking for a mistake it did not make.
|
|
106
|
+
*/
|
|
107
|
+
export function structuredRetryPrompt(capture) {
|
|
108
|
+
const reason = capture.called && capture.lastError !== undefined
|
|
109
|
+
? `Your last ${STRUCTURED_OUTPUT_TOOL_NAME} call did not match the required schema: ${capture.lastError}`
|
|
110
|
+
: `You did not call ${STRUCTURED_OUTPUT_TOOL_NAME}, so your answer was not recorded.`;
|
|
111
|
+
return `${reason}\n\nCall ${STRUCTURED_OUTPUT_TOOL_NAME} now with your complete final answer. Do not reply with prose.`;
|
|
112
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* terminal-progress.ts — Keep the terminal's progress spinner alive while subagents run.
|
|
3
|
+
*
|
|
4
|
+
* pi drives the tab/taskbar progress spinner (OSC 9;4, "indeterminate") from
|
|
5
|
+
* the MAIN session only: `turn_start` sets it, `agent_end` clears it. A turn
|
|
6
|
+
* that ends with background agents still running therefore blanks the spinner
|
|
7
|
+
* even though work continues — the session looks finished when it isn't.
|
|
8
|
+
*
|
|
9
|
+
* This module holds the indicator up across exactly that gap, writing the same
|
|
10
|
+
* escape sequences pi itself writes (pi-tui's `ProcessTerminal.setProgress`):
|
|
11
|
+
*
|
|
12
|
+
* active: ESC ] 9 ; 4 ; 3 BEL — indeterminate progress
|
|
13
|
+
* clear: ESC ] 9 ; 4 ; 0 BEL — progress off
|
|
14
|
+
*
|
|
15
|
+
* Two writers share one indicator, so ownership rules keep them from fighting:
|
|
16
|
+
*
|
|
17
|
+
* - While the MAIN session is busy (`ctx.isIdle()` false — an agent run, a
|
|
18
|
+
* queued continuation, a retry, a compaction), pi owns the indicator. We
|
|
19
|
+
* still assert alongside it — the bytes are identical, so the duplicate
|
|
20
|
+
* write is invisible — but we never CLEAR in that window; that would blank
|
|
21
|
+
* pi's own spinner. pi clears at its `agent_end`, and the `agent_end` hook
|
|
22
|
+
* re-asserts right behind it (see onMainAgentEnd).
|
|
23
|
+
* - While the main session is idle and subagent work exists, we own it: assert
|
|
24
|
+
* on every refresh, re-send on a 1s keepalive (same cadence as pi — some
|
|
25
|
+
* terminals, Windows Terminal included, drop the progress state when the
|
|
26
|
+
* title is rewritten, so it must be re-sent), and CLEAR once when the work
|
|
27
|
+
* drains — only ever undoing our own assert.
|
|
28
|
+
* - With no subagent work we never assert: sessions that never spawn agents
|
|
29
|
+
* see pi's indicator behavior untouched.
|
|
30
|
+
*
|
|
31
|
+
* Writes are gated to `ctx.mode === "tui"`: RPC mode's stdout is a JSON
|
|
32
|
+
* channel the sequences would corrupt, and headless child sessions — every
|
|
33
|
+
* subagent re-activates this extension in-process — have no terminal to spin.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Read pi's `terminal.showTerminalProgress` setting (project
|
|
37
|
+
* `<cwd>/.pi/settings.json` over global `<agentDir>/settings.json`, mirroring
|
|
38
|
+
* pi's SettingsManager merge — the same precedence src/enabled-models.ts uses
|
|
39
|
+
* for `enabledModels`).
|
|
40
|
+
*
|
|
41
|
+
* The gate matters because pi's own indicator is opt-in (pi defaults the
|
|
42
|
+
* setting to false): a user who disabled terminal progress should not get a
|
|
43
|
+
* subagent-driven spinner pi would never have shown. Defaults to false on
|
|
44
|
+
* absent field and on unreadable file — the same effective state pi itself
|
|
45
|
+
* degrades to — with a stderr warning on a corrupt file so the miss is
|
|
46
|
+
* diagnosable rather than looking like a dead feature.
|
|
47
|
+
*/
|
|
48
|
+
export declare function readShowTerminalProgress(cwd: string): boolean;
|
|
49
|
+
/** Live inputs the tracker reads on every decision. All are getters on purpose: state is re-probed, never cached. */
|
|
50
|
+
export interface TerminalProgressInputs {
|
|
51
|
+
/** Whether any subagent work is running or queued (AgentManager.hasRunning). */
|
|
52
|
+
hasWork(): boolean;
|
|
53
|
+
/** Whether the MAIN session owns the indicator: an agent run or compaction in flight (pi shows its own spinner). */
|
|
54
|
+
isMainBusy(): boolean;
|
|
55
|
+
/** Whether writing to the terminal is allowed at all (interactive TUI session). */
|
|
56
|
+
canWrite(): boolean;
|
|
57
|
+
/** Whether the user enabled terminal progress in pi's settings. */
|
|
58
|
+
enabled(): boolean;
|
|
59
|
+
}
|
|
60
|
+
export declare class TerminalProgressTracker {
|
|
61
|
+
private inputs;
|
|
62
|
+
/** Sink for the escape sequences. Injectable so tests can capture writes. */
|
|
63
|
+
private write;
|
|
64
|
+
private interval;
|
|
65
|
+
/** Last sequence this tracker wrote. ACTIVE re-sends are the keepalive's whole job; CLEAR only ever undoes an assert (see clear()). */
|
|
66
|
+
private lastWrite;
|
|
67
|
+
private disposed;
|
|
68
|
+
constructor(inputs: TerminalProgressInputs,
|
|
69
|
+
/** Sink for the escape sequences. Injectable so tests can capture writes. */
|
|
70
|
+
write?: (data: string) => void);
|
|
71
|
+
/**
|
|
72
|
+
* Re-evaluate and sync the indicator. Called when subagent work starts or
|
|
73
|
+
* settles, and from the keepalive tick — a re-probe, so a missed event
|
|
74
|
+
* self-heals within one keepalive period.
|
|
75
|
+
*/
|
|
76
|
+
refresh(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Hook for the main session's `agent_end`.
|
|
79
|
+
*
|
|
80
|
+
* pi clears the indicator there AFTER extension handlers run (agent-session
|
|
81
|
+
* emits to extensions before its own listeners), so re-asserting
|
|
82
|
+
* synchronously would be overwritten a moment later. Deferring with
|
|
83
|
+
* setImmediate lands after pi's write in the common case — but that ordering
|
|
84
|
+
* is not guaranteed: another extension's agent_end handler that awaits real
|
|
85
|
+
* I/O lets this callback run first, and the ACTIVE is wiped by pi's clear.
|
|
86
|
+
* That is fine by design: the 1s keepalive re-sends within a second, and the
|
|
87
|
+
* `agent_settled` refresh (which the wiring registers) re-probes the final
|
|
88
|
+
* state. This hook is the fast path, not the only one.
|
|
89
|
+
*
|
|
90
|
+
* Deliberately not gated on `isMainBusy`: agent_end also fires between an
|
|
91
|
+
* intermediate turn and its queued continuation, a window where pi has
|
|
92
|
+
* already cleared but work is still ongoing — the spinner belongs up there
|
|
93
|
+
* too. pi's next turn_start re-asserts over ours harmlessly.
|
|
94
|
+
*/
|
|
95
|
+
onMainAgentEnd(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Stop the keepalive and clear the indicator. Call on session shutdown, before pi restores the terminal.
|
|
98
|
+
*
|
|
99
|
+
* The clear goes through clear()'s "only undo our own assert" guard, which
|
|
100
|
+
* is the only safe gate here: dispose runs in every activation
|
|
101
|
+
* (session_shutdown fires inside child sessions too, #242), and both a child
|
|
102
|
+
* activation (mode "print") and an RPC-mode root must not put these bytes
|
|
103
|
+
* on stdout — RPC's stdout is a JSON channel, and a child teardown clearing
|
|
104
|
+
* mid-run would blank the root session's spinner. Those activations never
|
|
105
|
+
* pass the write gate, so they never assert, and dispose writes nothing.
|
|
106
|
+
* Reading `canWrite()` here instead would be wrong in the one case dispose
|
|
107
|
+
* exists for: the root TUI session, whose handler has already dropped
|
|
108
|
+
* `currentCtx` by the time dispose runs.
|
|
109
|
+
*/
|
|
110
|
+
dispose(): void;
|
|
111
|
+
private assert;
|
|
112
|
+
private clear;
|
|
113
|
+
private ensureKeepalive;
|
|
114
|
+
private stopKeepalive;
|
|
115
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* terminal-progress.ts — Keep the terminal's progress spinner alive while subagents run.
|
|
3
|
+
*
|
|
4
|
+
* pi drives the tab/taskbar progress spinner (OSC 9;4, "indeterminate") from
|
|
5
|
+
* the MAIN session only: `turn_start` sets it, `agent_end` clears it. A turn
|
|
6
|
+
* that ends with background agents still running therefore blanks the spinner
|
|
7
|
+
* even though work continues — the session looks finished when it isn't.
|
|
8
|
+
*
|
|
9
|
+
* This module holds the indicator up across exactly that gap, writing the same
|
|
10
|
+
* escape sequences pi itself writes (pi-tui's `ProcessTerminal.setProgress`):
|
|
11
|
+
*
|
|
12
|
+
* active: ESC ] 9 ; 4 ; 3 BEL — indeterminate progress
|
|
13
|
+
* clear: ESC ] 9 ; 4 ; 0 BEL — progress off
|
|
14
|
+
*
|
|
15
|
+
* Two writers share one indicator, so ownership rules keep them from fighting:
|
|
16
|
+
*
|
|
17
|
+
* - While the MAIN session is busy (`ctx.isIdle()` false — an agent run, a
|
|
18
|
+
* queued continuation, a retry, a compaction), pi owns the indicator. We
|
|
19
|
+
* still assert alongside it — the bytes are identical, so the duplicate
|
|
20
|
+
* write is invisible — but we never CLEAR in that window; that would blank
|
|
21
|
+
* pi's own spinner. pi clears at its `agent_end`, and the `agent_end` hook
|
|
22
|
+
* re-asserts right behind it (see onMainAgentEnd).
|
|
23
|
+
* - While the main session is idle and subagent work exists, we own it: assert
|
|
24
|
+
* on every refresh, re-send on a 1s keepalive (same cadence as pi — some
|
|
25
|
+
* terminals, Windows Terminal included, drop the progress state when the
|
|
26
|
+
* title is rewritten, so it must be re-sent), and CLEAR once when the work
|
|
27
|
+
* drains — only ever undoing our own assert.
|
|
28
|
+
* - With no subagent work we never assert: sessions that never spawn agents
|
|
29
|
+
* see pi's indicator behavior untouched.
|
|
30
|
+
*
|
|
31
|
+
* Writes are gated to `ctx.mode === "tui"`: RPC mode's stdout is a JSON
|
|
32
|
+
* channel the sequences would corrupt, and headless child sessions — every
|
|
33
|
+
* subagent re-activates this extension in-process — have no terminal to spin.
|
|
34
|
+
*/
|
|
35
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
36
|
+
import { join } from "node:path";
|
|
37
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
38
|
+
/** OSC 9;4;3 — indeterminate progress. BEL-terminated, like pi-tui's. */
|
|
39
|
+
const PROGRESS_ACTIVE = "\x1b]9;4;3\x07";
|
|
40
|
+
/** OSC 9;4;0 — progress off. */
|
|
41
|
+
const PROGRESS_CLEAR = "\x1b]9;4;0\x07";
|
|
42
|
+
/**
|
|
43
|
+
* Keepalive cadence. Mirrors pi-tui's TERMINAL_PROGRESS_KEEPALIVE_MS so the
|
|
44
|
+
* two writers re-assert at the same rate; a faster one would win nothing and
|
|
45
|
+
* a slower one would let the state drop for longer.
|
|
46
|
+
*/
|
|
47
|
+
const KEEPALIVE_MS = 1_000;
|
|
48
|
+
/**
|
|
49
|
+
* Read pi's `terminal.showTerminalProgress` setting (project
|
|
50
|
+
* `<cwd>/.pi/settings.json` over global `<agentDir>/settings.json`, mirroring
|
|
51
|
+
* pi's SettingsManager merge — the same precedence src/enabled-models.ts uses
|
|
52
|
+
* for `enabledModels`).
|
|
53
|
+
*
|
|
54
|
+
* The gate matters because pi's own indicator is opt-in (pi defaults the
|
|
55
|
+
* setting to false): a user who disabled terminal progress should not get a
|
|
56
|
+
* subagent-driven spinner pi would never have shown. Defaults to false on
|
|
57
|
+
* absent field and on unreadable file — the same effective state pi itself
|
|
58
|
+
* degrades to — with a stderr warning on a corrupt file so the miss is
|
|
59
|
+
* diagnosable rather than looking like a dead feature.
|
|
60
|
+
*/
|
|
61
|
+
export function readShowTerminalProgress(cwd) {
|
|
62
|
+
const read = (path) => {
|
|
63
|
+
if (!existsSync(path))
|
|
64
|
+
return undefined;
|
|
65
|
+
try {
|
|
66
|
+
const raw = JSON.parse(readFileSync(path, "utf-8"));
|
|
67
|
+
const value = raw?.terminal
|
|
68
|
+
?.showTerminalProgress;
|
|
69
|
+
return typeof value === "boolean" ? value : undefined;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
73
|
+
console.warn(`[pi-subagents] Ignoring malformed settings at ${path}: ${reason}`);
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const project = read(join(cwd, ".pi", "settings.json"));
|
|
78
|
+
if (project !== undefined)
|
|
79
|
+
return project;
|
|
80
|
+
return read(join(getAgentDir(), "settings.json")) ?? false;
|
|
81
|
+
}
|
|
82
|
+
export class TerminalProgressTracker {
|
|
83
|
+
inputs;
|
|
84
|
+
write;
|
|
85
|
+
interval;
|
|
86
|
+
/** Last sequence this tracker wrote. ACTIVE re-sends are the keepalive's whole job; CLEAR only ever undoes an assert (see clear()). */
|
|
87
|
+
lastWrite;
|
|
88
|
+
disposed = false;
|
|
89
|
+
constructor(inputs,
|
|
90
|
+
/** Sink for the escape sequences. Injectable so tests can capture writes. */
|
|
91
|
+
write = (data) => { process.stdout.write(data); }) {
|
|
92
|
+
this.inputs = inputs;
|
|
93
|
+
this.write = write;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Re-evaluate and sync the indicator. Called when subagent work starts or
|
|
97
|
+
* settles, and from the keepalive tick — a re-probe, so a missed event
|
|
98
|
+
* self-heals within one keepalive period.
|
|
99
|
+
*/
|
|
100
|
+
refresh() {
|
|
101
|
+
if (this.disposed)
|
|
102
|
+
return;
|
|
103
|
+
if (!this.inputs.enabled() || !this.inputs.canWrite()) {
|
|
104
|
+
// Opted out, or nowhere to write. Undo an assert this tracker made —
|
|
105
|
+
// clear() keys off `lastWrite`, which is proof this tracker passed the
|
|
106
|
+
// write gate when it asserted (child/RPC activations can never assert,
|
|
107
|
+
// and the sink never changes), so the clear lands on the same stdout the
|
|
108
|
+
// ACTIVE did; that is also the policy dispose() states. The mid-session
|
|
109
|
+
// opt-out this guards against is unreachable today (the setting is read
|
|
110
|
+
// once per session bind); the branch is the correct undo if that ever
|
|
111
|
+
// becomes live-read, and without it a stranded ACTIVE would have no
|
|
112
|
+
// re-probe left — the keepalive is being stopped here.
|
|
113
|
+
this.clear();
|
|
114
|
+
this.stopKeepalive();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (this.inputs.hasWork()) {
|
|
118
|
+
this.assert();
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.stopKeepalive();
|
|
122
|
+
// No subagent work. Clear only when pi isn't mid-run: pi clears its own
|
|
123
|
+
// indicator at its agent_end, and a clear from us in that window would
|
|
124
|
+
// blank the main session's spinner until pi's 1s keepalive re-sent it.
|
|
125
|
+
if (!this.inputs.isMainBusy())
|
|
126
|
+
this.clear();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Hook for the main session's `agent_end`.
|
|
131
|
+
*
|
|
132
|
+
* pi clears the indicator there AFTER extension handlers run (agent-session
|
|
133
|
+
* emits to extensions before its own listeners), so re-asserting
|
|
134
|
+
* synchronously would be overwritten a moment later. Deferring with
|
|
135
|
+
* setImmediate lands after pi's write in the common case — but that ordering
|
|
136
|
+
* is not guaranteed: another extension's agent_end handler that awaits real
|
|
137
|
+
* I/O lets this callback run first, and the ACTIVE is wiped by pi's clear.
|
|
138
|
+
* That is fine by design: the 1s keepalive re-sends within a second, and the
|
|
139
|
+
* `agent_settled` refresh (which the wiring registers) re-probes the final
|
|
140
|
+
* state. This hook is the fast path, not the only one.
|
|
141
|
+
*
|
|
142
|
+
* Deliberately not gated on `isMainBusy`: agent_end also fires between an
|
|
143
|
+
* intermediate turn and its queued continuation, a window where pi has
|
|
144
|
+
* already cleared but work is still ongoing — the spinner belongs up there
|
|
145
|
+
* too. pi's next turn_start re-asserts over ours harmlessly.
|
|
146
|
+
*/
|
|
147
|
+
onMainAgentEnd() {
|
|
148
|
+
if (this.disposed)
|
|
149
|
+
return;
|
|
150
|
+
if (!this.inputs.enabled() || !this.inputs.canWrite())
|
|
151
|
+
return;
|
|
152
|
+
setImmediate(() => {
|
|
153
|
+
if (!this.disposed && this.inputs.hasWork())
|
|
154
|
+
this.assert();
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Stop the keepalive and clear the indicator. Call on session shutdown, before pi restores the terminal.
|
|
159
|
+
*
|
|
160
|
+
* The clear goes through clear()'s "only undo our own assert" guard, which
|
|
161
|
+
* is the only safe gate here: dispose runs in every activation
|
|
162
|
+
* (session_shutdown fires inside child sessions too, #242), and both a child
|
|
163
|
+
* activation (mode "print") and an RPC-mode root must not put these bytes
|
|
164
|
+
* on stdout — RPC's stdout is a JSON channel, and a child teardown clearing
|
|
165
|
+
* mid-run would blank the root session's spinner. Those activations never
|
|
166
|
+
* pass the write gate, so they never assert, and dispose writes nothing.
|
|
167
|
+
* Reading `canWrite()` here instead would be wrong in the one case dispose
|
|
168
|
+
* exists for: the root TUI session, whose handler has already dropped
|
|
169
|
+
* `currentCtx` by the time dispose runs.
|
|
170
|
+
*/
|
|
171
|
+
dispose() {
|
|
172
|
+
if (this.disposed)
|
|
173
|
+
return;
|
|
174
|
+
this.disposed = true;
|
|
175
|
+
this.stopKeepalive();
|
|
176
|
+
this.clear();
|
|
177
|
+
}
|
|
178
|
+
assert() {
|
|
179
|
+
this.ensureKeepalive();
|
|
180
|
+
this.write(PROGRESS_ACTIVE);
|
|
181
|
+
this.lastWrite = "active";
|
|
182
|
+
}
|
|
183
|
+
clear() {
|
|
184
|
+
// Only undo an assert this tracker made. A speculative clear would be
|
|
185
|
+
// noise on every no-work refresh (session bind), and worse: it cannot
|
|
186
|
+
// know the terminal's true state — pi drives the indicator too. If our
|
|
187
|
+
// ACTIVE was clobbered by pi's own clear, the keepalive re-asserts; the
|
|
188
|
+
// indicator is only ever cleared by whoever left it up.
|
|
189
|
+
if (this.lastWrite !== "active")
|
|
190
|
+
return;
|
|
191
|
+
this.write(PROGRESS_CLEAR);
|
|
192
|
+
this.lastWrite = "clear";
|
|
193
|
+
}
|
|
194
|
+
ensureKeepalive() {
|
|
195
|
+
if (this.interval)
|
|
196
|
+
return;
|
|
197
|
+
this.interval = setInterval(() => this.refresh(), KEEPALIVE_MS);
|
|
198
|
+
this.interval.unref?.();
|
|
199
|
+
}
|
|
200
|
+
stopKeepalive() {
|
|
201
|
+
if (!this.interval)
|
|
202
|
+
return;
|
|
203
|
+
clearInterval(this.interval);
|
|
204
|
+
this.interval = undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|