fito-tui 0.2.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 +88 -0
- package/agents/fito-case-source-planner.md +265 -0
- package/agents/fito-performance-reviewer.md +231 -0
- package/agents/fito-practice-concept-content-generator.md +318 -0
- package/agents/fito-practice-concept-identifier.md +482 -0
- package/agents/fito-practice-execution-fact-content-generator.md +299 -0
- package/agents/fito-practice-execution-fact-identifier.md +367 -0
- package/agents/fito-practice-usage-scenario-content-generator.md +331 -0
- package/agents/fito-practice-usage-scenario-identifier.md +397 -0
- package/agents/fito-subagent-selftest.md +27 -0
- package/agents/fito-technology-generator.md +207 -0
- package/agents/fito-tool-concept-content-generator.md +282 -0
- package/agents/fito-tool-concept-identifier.md +384 -0
- package/agents/fito-tool-execution-fact-content-generator.md +382 -0
- package/agents/fito-tool-execution-fact-identifier.md +360 -0
- package/agents/fito-tool-usage-scenario-content-generator.md +381 -0
- package/agents/fito-tool-usage-scenario-identifier.md +432 -0
- package/agents/fito-tutor.md +185 -0
- package/extensions/fito-delegate.js +539 -0
- package/extensions/fito-delegate.ts +691 -0
- package/extensions/fito-fetch-tools.js +380 -0
- package/extensions/fito-fetch-tools.ts +419 -0
- package/extensions/fito-startup.js +166 -0
- package/extensions/fito-startup.ts +188 -0
- package/package.json +56 -0
- package/prompts/fito-auth-refresh.md +15 -0
- package/prompts/fito-case-design.md +506 -0
- package/prompts/fito-case-edit.md +91 -0
- package/prompts/fito-case-generate.md +222 -0
- package/prompts/fito-case-open.md +124 -0
- package/prompts/fito-case-plan-source.md +114 -0
- package/prompts/fito-create-or-update-competence.md +261 -0
- package/prompts/fito-create-or-update-technology.md +199 -0
- package/prompts/fito-delete-competence.md +147 -0
- package/prompts/fito-evaluate.md +148 -0
- package/prompts/fito-fetch-urls-content.md +35 -0
- package/prompts/fito-generate-domain-concepts.md +85 -0
- package/prompts/fito-generate-execution-facts.md +85 -0
- package/prompts/fito-generate-usage-scenarios.md +85 -0
- package/prompts/fito-identify-domain-concepts.md +49 -0
- package/prompts/fito-identify-execution-facts.md +49 -0
- package/prompts/fito-identify-usage-scenarios.md +49 -0
- package/prompts/fito-login.md +21 -0
- package/prompts/fito-logout.md +14 -0
- package/prompts/fito-publish-case.md +266 -0
- package/prompts/fito-push-knowledge.md +84 -0
- package/prompts/fito-submit.md +208 -0
- package/prompts/fito-tutor.md +120 -0
- package/shared/case-briefing/briefing.js +221 -0
- package/shared/case-briefing/briefing.ts +261 -0
- package/shared/case-quiz/scoring.js +16 -0
- package/shared/case-quiz/scoring.ts +61 -0
- package/shared/case-validation/case-loader.js +88 -0
- package/shared/case-validation/case-loader.ts +106 -0
- package/shared/case-validation/case-schema.js +716 -0
- package/shared/case-validation/case-schema.ts +895 -0
- package/shared/competences/competence-list.js +29 -0
- package/shared/competences/competence-list.ts +32 -0
- package/shared/fito-api/README.md +192 -0
- package/shared/fito-api/client.js +731 -0
- package/shared/fito-api/client.ts +1426 -0
- package/shared/fito-session/credentials.js +79 -0
- package/shared/fito-session/credentials.ts +88 -0
- package/shared/fito-session/session.js +16 -0
- package/shared/fito-session/session.ts +35 -0
- package/shared/github-url/parse.js +26 -0
- package/shared/github-url/parse.ts +35 -0
- package/shared/knowledge-components/component-names.js +35 -0
- package/shared/knowledge-components/component-names.ts +39 -0
- package/shared/knowledge-components/knowledge-fetch.js +46 -0
- package/shared/knowledge-components/knowledge-fetch.ts +57 -0
- package/shared/knowledge-components/knowledge-search.js +101 -0
- package/shared/knowledge-components/knowledge-search.ts +144 -0
- package/shared/knowledge-components/push-payload.js +104 -0
- package/shared/knowledge-components/push-payload.ts +129 -0
- package/shared/knowledge-components/reference.js +24 -0
- package/shared/knowledge-components/reference.ts +43 -0
- package/shared/knowledge-components/technology-fetch.js +30 -0
- package/shared/knowledge-components/technology-fetch.ts +38 -0
- package/shared/shell-exec/exec.js +22 -0
- package/shared/shell-exec/exec.ts +46 -0
- package/skills/fito-auth/SKILL.md +68 -0
- package/skills/fito-auth/auth-refresh.js +23 -0
- package/skills/fito-auth/auth-refresh.ts +25 -0
- package/skills/fito-auth/browser.js +35 -0
- package/skills/fito-auth/browser.ts +57 -0
- package/skills/fito-auth/callback-server.js +152 -0
- package/skills/fito-auth/callback-server.ts +189 -0
- package/skills/fito-auth/login.js +37 -0
- package/skills/fito-auth/login.ts +42 -0
- package/skills/fito-auth/logout.js +24 -0
- package/skills/fito-auth/logout.ts +25 -0
- package/skills/fito-case-author/SKILL.md +179 -0
- package/skills/fito-case-author/checkout.js +222 -0
- package/skills/fito-case-author/checkout.ts +281 -0
- package/skills/fito-case-author/github-publish.js +218 -0
- package/skills/fito-case-author/github-publish.ts +251 -0
- package/skills/fito-case-author/publish-payload.js +108 -0
- package/skills/fito-case-author/publish-payload.ts +186 -0
- package/skills/fito-case-author/publish-state.js +72 -0
- package/skills/fito-case-author/publish-state.ts +87 -0
- package/skills/fito-case-author/publish-version.js +364 -0
- package/skills/fito-case-author/publish-version.ts +495 -0
- package/skills/fito-case-author/publish.js +284 -0
- package/skills/fito-case-author/publish.ts +394 -0
- package/skills/fito-case-author/semver.js +55 -0
- package/skills/fito-case-author/semver.ts +63 -0
- package/skills/fito-case-author/status-gate.js +34 -0
- package/skills/fito-case-author/status-gate.ts +54 -0
- package/skills/fito-case-author/validate.js +62 -0
- package/skills/fito-case-author/validate.ts +64 -0
- package/skills/fito-case-runner/SKILL.md +119 -0
- package/skills/fito-case-runner/case-component-names.js +45 -0
- package/skills/fito-case-runner/case-component-names.ts +65 -0
- package/skills/fito-case-runner/case-detail.js +20 -0
- package/skills/fito-case-runner/case-detail.ts +20 -0
- package/skills/fito-case-runner/github.js +234 -0
- package/skills/fito-case-runner/github.ts +287 -0
- package/skills/fito-case-runner/manifest.js +56 -0
- package/skills/fito-case-runner/manifest.ts +73 -0
- package/skills/fito-case-runner/open.js +175 -0
- package/skills/fito-case-runner/open.ts +241 -0
- package/skills/fito-case-runner/report.js +22 -0
- package/skills/fito-case-runner/report.ts +24 -0
- package/skills/fito-case-runner/runner.js +158 -0
- package/skills/fito-case-runner/runner.ts +304 -0
- package/skills/fito-case-runner/score-quiz-mc.js +85 -0
- package/skills/fito-case-runner/score-quiz-mc.ts +99 -0
- package/skills/fito-case-runner/submit-evaluation.js +73 -0
- package/skills/fito-case-runner/submit-evaluation.ts +98 -0
- package/skills/fito-case-runner/submit-quiz-result.js +75 -0
- package/skills/fito-case-runner/submit-quiz-result.ts +100 -0
- package/skills/fito-case-runner/submit.js +85 -0
- package/skills/fito-case-runner/submit.ts +111 -0
- package/skills/fito-case-runner/workdir.js +10 -0
- package/skills/fito-case-runner/workdir.ts +11 -0
- package/skills/fito-competence-write/SKILL.md +155 -0
- package/skills/fito-competence-write/create.js +64 -0
- package/skills/fito-competence-write/create.ts +80 -0
- package/skills/fito-competence-write/delete.js +136 -0
- package/skills/fito-competence-write/delete.ts +166 -0
- package/skills/fito-competence-write/display.js +20 -0
- package/skills/fito-competence-write/display.ts +21 -0
- package/skills/fito-competence-write/payload.js +96 -0
- package/skills/fito-competence-write/payload.ts +115 -0
- package/skills/fito-competence-write/update.js +155 -0
- package/skills/fito-competence-write/update.ts +206 -0
- package/skills/fito-evaluator/SKILL.md +73 -0
- package/skills/fito-evaluator/criterion-exec.js +64 -0
- package/skills/fito-evaluator/criterion-exec.ts +73 -0
- package/skills/fito-evaluator/evaluate-automated.js +57 -0
- package/skills/fito-evaluator/evaluate-automated.ts +84 -0
- package/skills/fito-evaluator/evaluate.js +28 -0
- package/skills/fito-evaluator/evaluate.ts +86 -0
- package/skills/fito-evaluator/report.js +112 -0
- package/skills/fito-evaluator/report.ts +128 -0
- package/skills/fito-evaluator/rubric.js +90 -0
- package/skills/fito-evaluator/rubric.ts +170 -0
- package/skills/fito-knowledge-push/SKILL.md +114 -0
- package/skills/fito-knowledge-push/push.js +282 -0
- package/skills/fito-knowledge-push/push.ts +360 -0
- package/skills/fito-source-ingestion/SKILL.md +77 -0
- package/skills/fito-source-ingestion/authoring-check.js +128 -0
- package/skills/fito-source-ingestion/authoring-check.ts +155 -0
- package/skills/fito-source-ingestion/bundle.js +198 -0
- package/skills/fito-source-ingestion/bundle.ts +246 -0
- package/skills/fito-technology-write/SKILL.md +108 -0
- package/skills/fito-technology-write/create.js +134 -0
- package/skills/fito-technology-write/create.ts +161 -0
- package/skills/fito-technology-write/payload.js +64 -0
- package/skills/fito-technology-write/payload.ts +70 -0
- package/skills/fito-technology-write/update.js +122 -0
- package/skills/fito-technology-write/update.ts +171 -0
- package/skills/fito-url-content-fetcher/SKILL.md +213 -0
- package/templates/concept-practice-content-template.md +281 -0
- package/templates/concept-tool-content-template.md +207 -0
- package/templates/execution-fact-practice-content-template.md +372 -0
- package/templates/execution-fact-tool-content-template.md +307 -0
- package/templates/usage-scenario-content-template.md +248 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
/**
|
|
3
|
+
* `fito_delegate` — Fito's own delegation tool (Task 11.8).
|
|
4
|
+
*
|
|
5
|
+
* **Why it exists.** Eleven Fito prompts used to instruct the model to call `pi-subagents`'
|
|
6
|
+
* `subagent` tool and named that tool's parameters in prose (`agent`, `task`, `async: false`).
|
|
7
|
+
* That is a vendor schema living in markdown, with no type-checker behind it, and it has already
|
|
8
|
+
* been wrong at **twelve call sites simultaneously** — `pi-subagents` 0.49.0 inverted the `async`
|
|
9
|
+
* default and every blocking delegation backgrounded itself at once (Task 11.4a1, lesson 82).
|
|
10
|
+
* Inference cannot cover the gap either: the `subagent` schema describes `async` as
|
|
11
|
+
* `"default: false, or per config"` while `resolveAsyncByDefault` returns
|
|
12
|
+
* `config.asyncByDefault !== false`, so an unset config means *background* — the schema reads as
|
|
13
|
+
* the opposite of what it does. The parameter therefore has to be pinned by a mechanism, and this
|
|
14
|
+
* module is that mechanism: prompts name a worker and a task, and the launch mode lives here, in
|
|
15
|
+
* TypeScript, with tests.
|
|
16
|
+
*
|
|
17
|
+
* **The transport.** `pi-subagents` ships an extension-to-extension delegation API, whose own
|
|
18
|
+
* header calls it *"the established extension-to-extension transport"*: emit
|
|
19
|
+
* {@link DELEGATION_REQUEST_EVENT}, receive {@link DELEGATION_RESPONSE_EVENT}. Its adapter
|
|
20
|
+
* hardcodes `output: false, acceptance: false, async: false, foregroundOnly: true` and routes
|
|
21
|
+
* through `executeDelegated`. **Nothing is imported from `pi-subagents`** — the learner installs
|
|
22
|
+
* it separately and it is absent from `tui/node_modules` (ADR-002 Decision 2, lesson 59), so the
|
|
23
|
+
* coupling is `pi.events` plus the payload shape restated below. That restatement is the point of
|
|
24
|
+
* the criterion this module answers: a vendor change lands in *this file*, next to its tests.
|
|
25
|
+
*
|
|
26
|
+
* **The contract, read from `pi-subagents` 0.50.0** (`src/api/delegation.ts` for the shapes,
|
|
27
|
+
* `src/slash/delegation-request.ts` for what the parser accepts):
|
|
28
|
+
*
|
|
29
|
+
* - the request carries `requestId` + `ownerRunId` + `nodeId` as its identity, `agent`, `task`,
|
|
30
|
+
* `context`, `cwd`, and a **required** `result` mode — omitting `result` comes back
|
|
31
|
+
* `invalid_request` (measured, Task 11.4b);
|
|
32
|
+
* - the parser **rejects any key it does not know** (`Unsupported delegation field: …`), which is
|
|
33
|
+
* why {@link buildDelegationRequest} sends exactly the supported set and its guard asserts that
|
|
34
|
+
* set exactly;
|
|
35
|
+
* - `ownerRunId` + `nodeId` is the logical identity and a second live attempt on the same pair
|
|
36
|
+
* comes back `duplicate_node`, so every call here mints a fresh `ownerRunId`;
|
|
37
|
+
* - the response carries one of twelve terminal statuses, and **`failed` is the only one carrying
|
|
38
|
+
* the child's `exitCode` and its own error text** — including, in the case this work exists for,
|
|
39
|
+
* its `Failed to load extension …` line.
|
|
40
|
+
*
|
|
41
|
+
* **What the throwaway probe measured** (2026-08-14, same agent, same task text, same 0.49.0,
|
|
42
|
+
* only the entry point differing), carried here from the probe directory Task 11.8 deleted:
|
|
43
|
+
* the `subagent` tool appended an output-path instruction that countermanded the caller's own
|
|
44
|
+
* path, and the delegation API did not, while both landed their files at the caller's path.
|
|
45
|
+
* Upstream fixed the append in 0.50.0, so that is history rather than the reason — but it is why
|
|
46
|
+
* this transport was chosen over patching eleven prompts again.
|
|
47
|
+
*
|
|
48
|
+
* **Text results only, deliberately.** The API also offers `result: { kind: "structured", schema }`,
|
|
49
|
+
* which could one day carry a reviewer's JSON array directly instead of prose the caller re-parses.
|
|
50
|
+
* It is real and it is not built: nothing needs it yet, and a schema chosen before a consumer
|
|
51
|
+
* exists is a guess (Task 11.8, "deliberately not built").
|
|
52
|
+
*
|
|
53
|
+
* **Registration is main-session-only**, under `fito-startup.ts`'s `PI_SUBAGENT_CHILD` guard: a
|
|
54
|
+
* child never delegates.
|
|
55
|
+
*/
|
|
56
|
+
/** The event `pi-subagents` listens on for a structured delegation request. */
|
|
57
|
+
export const DELEGATION_REQUEST_EVENT = "prompt-template:subagent:request";
|
|
58
|
+
/** The event carrying the one terminal response for an attempt. */
|
|
59
|
+
export const DELEGATION_RESPONSE_EVENT = "prompt-template:subagent:response";
|
|
60
|
+
/** The model-facing tool name. Fito's prompts name this and no vendor parameter. */
|
|
61
|
+
export const DELEGATE_TOOL = "fito_delegate";
|
|
62
|
+
/**
|
|
63
|
+
* How long a delegated worker may run before `pi-subagents` stops it and answers `timed_out`.
|
|
64
|
+
*
|
|
65
|
+
* Thirty minutes matches `pi-subagents`' own `subagent_wait` default, so a Fito delegation waits
|
|
66
|
+
* exactly as long as the vendor's blocking wait would. The bound is sent to the vendor rather
|
|
67
|
+
* than enforced here on purpose: the vendor can end the child, while a local timer would only
|
|
68
|
+
* stop listening and leave it running.
|
|
69
|
+
*/
|
|
70
|
+
export const DELEGATION_TIMEOUT_MS = 30 * 60 * 1000;
|
|
71
|
+
/**
|
|
72
|
+
* Extra wait before this module gives up on the response event itself.
|
|
73
|
+
*
|
|
74
|
+
* It covers the one failure `pi-subagents` cannot report: with the package absent, nothing is
|
|
75
|
+
* subscribed to {@link DELEGATION_REQUEST_EVENT} and the request falls into silence.
|
|
76
|
+
*/
|
|
77
|
+
export const RESPONSE_GRACE_MS = 30_000;
|
|
78
|
+
/**
|
|
79
|
+
* Build one delegation request.
|
|
80
|
+
*
|
|
81
|
+
* `ownerRunId` is derived from the attempt's own `requestId` so no two calls ever share a logical
|
|
82
|
+
* identity, which is what keeps `duplicate_node` structurally out of reach. `nodeId` is the agent
|
|
83
|
+
* name, so the vendor's own progress UI reads as the worker rather than as an opaque id.
|
|
84
|
+
*/
|
|
85
|
+
export function buildDelegationRequest(params) {
|
|
86
|
+
return {
|
|
87
|
+
requestId: params.requestId,
|
|
88
|
+
ownerRunId: `fito-delegate-${params.requestId}`,
|
|
89
|
+
nodeId: params.agent,
|
|
90
|
+
agent: params.agent,
|
|
91
|
+
task: params.task,
|
|
92
|
+
context: "fresh",
|
|
93
|
+
cwd: params.cwd,
|
|
94
|
+
timeoutMs: params.timeoutMs ?? DELEGATION_TIMEOUT_MS,
|
|
95
|
+
result: { kind: "text" },
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Is this payload the response to `request`?
|
|
100
|
+
*
|
|
101
|
+
* The event bus is shared, so every subscriber sees every delegation's response. `requestId` is a
|
|
102
|
+
* UUID and settles it alone; `ownerRunId` and `nodeId` are checked **when present** rather than
|
|
103
|
+
* required, because `pi-subagents` types them optional on its invalid-request response — a
|
|
104
|
+
* malformed request that cannot be correlated by the full tuple must still reach its caller
|
|
105
|
+
* rather than time out.
|
|
106
|
+
*/
|
|
107
|
+
export function isMatchingResponse(request, payload) {
|
|
108
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload))
|
|
109
|
+
return false;
|
|
110
|
+
const { requestId, ownerRunId, nodeId } = payload;
|
|
111
|
+
if (requestId !== request.requestId)
|
|
112
|
+
return false;
|
|
113
|
+
if (ownerRunId !== undefined && ownerRunId !== request.ownerRunId)
|
|
114
|
+
return false;
|
|
115
|
+
if (nodeId !== undefined && nodeId !== request.nodeId)
|
|
116
|
+
return false;
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The caller-facing message for each way a delegation ends without a result.
|
|
121
|
+
*
|
|
122
|
+
* Every entry names {@link DELEGATE_TOOL} and says what happened, because **a delegation that
|
|
123
|
+
* failed is not a delegation that returned nothing** — the distinction all six identifier
|
|
124
|
+
* personas already draw for their own probes. The split that matters most is between the
|
|
125
|
+
* statuses where a child *ran* (`failed`, `timed_out`, the budgets) and those where the request
|
|
126
|
+
* never became a child at all (`invalid_request`, `unavailable_context`, `duplicate_node`), since
|
|
127
|
+
* only the first group leaves anything on disk to inspect.
|
|
128
|
+
*/
|
|
129
|
+
const TERMINAL_MESSAGES = {
|
|
130
|
+
failed: (request, response) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent failed` +
|
|
131
|
+
`${response.exitCode === undefined ? "" : ` (exit code ${response.exitCode})`}. ` +
|
|
132
|
+
`${response.error ?? "It reported no error text."}`,
|
|
133
|
+
timed_out: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent timed out after ` +
|
|
134
|
+
`${Math.round(request.timeoutMs / 60_000)} minutes and was stopped. Anything it wrote is incomplete.`,
|
|
135
|
+
cancelled: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` delegation was cancelled before it finished.`,
|
|
136
|
+
interrupted: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent was interrupted before it finished.`,
|
|
137
|
+
turn_budget_exhausted: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent used its whole turn budget before finishing.`,
|
|
138
|
+
tool_budget_exhausted: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent used its whole tool-call budget before finishing.`,
|
|
139
|
+
structured_output_failed: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent could not produce the requested structured result.`,
|
|
140
|
+
acceptance_failed: (request) => `${DELEGATE_TOOL}: the \`${request.agent}\` subagent ran, and its acceptance check rejected the result.`,
|
|
141
|
+
invalid_request: (request, response) => `${DELEGATE_TOOL} sent a request pi-subagents rejected as malformed, so the \`${request.agent}\` ` +
|
|
142
|
+
`subagent never started. ${response.error ?? "No reason was given."} This is a defect in ` +
|
|
143
|
+
`extensions/fito-delegate.ts, not in the task.`,
|
|
144
|
+
unavailable_context: (request) => `${DELEGATE_TOOL}: pi-subagents had no active extension context to run in, so the ` +
|
|
145
|
+
`\`${request.agent}\` subagent never started.`,
|
|
146
|
+
duplicate_node: (request) => `${DELEGATE_TOOL}: a delegation with this identity is already running, so a second ` +
|
|
147
|
+
`\`${request.agent}\` subagent never started.`,
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Read a terminal response into either the child's text or a message saying what went wrong.
|
|
151
|
+
*
|
|
152
|
+
* A missing `response` is the local watchdog firing — see {@link RESPONSE_GRACE_MS}.
|
|
153
|
+
*/
|
|
154
|
+
export function readDelegationOutcome(request, response) {
|
|
155
|
+
if (response === undefined) {
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
message: `${DELEGATE_TOOL}: pi-subagents never answered the delegation, so the \`${request.agent}\` ` +
|
|
159
|
+
"subagent may never have started. Check that pi-subagents is installed " +
|
|
160
|
+
"(`pi install npm:pi-subagents`).",
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (response.status === "completed") {
|
|
164
|
+
const result = response.result;
|
|
165
|
+
if (result?.kind === "text" && typeof result.text === "string")
|
|
166
|
+
return { ok: true, text: result.text };
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
message: `${DELEGATE_TOOL}: the \`${request.agent}\` subagent finished but captured no text result.`,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const message = TERMINAL_MESSAGES[response.status ?? ""];
|
|
173
|
+
if (message !== undefined)
|
|
174
|
+
return { ok: false, message: message(request, response) };
|
|
175
|
+
// A status this release of Fito has not seen. Reporting it verbatim beats collapsing it into
|
|
176
|
+
// a neighbour's wording, which would describe a failure that did not happen.
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
message: `${DELEGATE_TOOL}: the \`${request.agent}\` delegation ended with the unrecognised status ` +
|
|
180
|
+
`"${response.status ?? "(none)"}"${response.error === undefined ? "" : `: ${response.error}`}.`,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Emit the request and resolve with the response carrying the same identity.
|
|
185
|
+
*
|
|
186
|
+
* Resolves `undefined` when nothing answers within the run's own timeout plus `graceMs`; the
|
|
187
|
+
* subscription is dropped either way, so a long session accumulates no listeners.
|
|
188
|
+
*/
|
|
189
|
+
export function awaitDelegation(pi, request, graceMs = RESPONSE_GRACE_MS) {
|
|
190
|
+
return new Promise((resolve) => {
|
|
191
|
+
let settled = false;
|
|
192
|
+
let unsubscribe;
|
|
193
|
+
const timer = setTimeout(() => finish(undefined), request.timeoutMs + graceMs);
|
|
194
|
+
function finish(response) {
|
|
195
|
+
if (settled)
|
|
196
|
+
return;
|
|
197
|
+
settled = true;
|
|
198
|
+
clearTimeout(timer);
|
|
199
|
+
if (typeof unsubscribe === "function")
|
|
200
|
+
unsubscribe();
|
|
201
|
+
resolve(response);
|
|
202
|
+
}
|
|
203
|
+
unsubscribe = pi.events.on(DELEGATION_RESPONSE_EVENT, (payload) => {
|
|
204
|
+
if (!isMatchingResponse(request, payload))
|
|
205
|
+
return;
|
|
206
|
+
finish(payload);
|
|
207
|
+
});
|
|
208
|
+
pi.events.emit(DELEGATION_REQUEST_EVENT, request);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/* ------------------------------------------------------------------------------------------- *
|
|
212
|
+
* The thread path (Task 11.8b) — `pi-subagents`' in-process RPC.
|
|
213
|
+
*
|
|
214
|
+
* The delegation API above cannot hold a conversation: its adapter hardcodes
|
|
215
|
+
* `foregroundOnly: true` and the surface has no resume verb, so a child is gone the moment it
|
|
216
|
+
* answers. `/fito-tutor` needs the opposite — one worker the learner returns to across turns —
|
|
217
|
+
* and the RPC surface (`src/extension/rpc.ts`) has exactly that: `spawn` and `resume`, keyed by
|
|
218
|
+
* run id. This module routes between the two doors so no prompt has to name either.
|
|
219
|
+
*
|
|
220
|
+
* **What was measured, live on 0.51.0 (2026-08-18), and why the routing is not symmetric:**
|
|
221
|
+
*
|
|
222
|
+
* - RPC `spawn` answers with a **workflow** run id, and resuming that id is refused —
|
|
223
|
+
* *"Async child '…' is missing its required run fan-out recovery identity"*. The resumable
|
|
224
|
+
* identity is the **child's**, reported one level down in `results[0].runId`. Three turns on
|
|
225
|
+
* that handle appended to one child session file, and the handle never changed.
|
|
226
|
+
* - The vendor tool has the same defect and cannot route around it: it reports only the workflow
|
|
227
|
+
* id to the model, so `/fito-tutor`'s documented resume fails and the flow starts a fresh
|
|
228
|
+
* thread instead. Reading the child id out of the completion is something only an extension can
|
|
229
|
+
* do, which is why moving the tutor here **repairs** continuity rather than merely relocating it.
|
|
230
|
+
* - RPC does **not** pin `acceptance: false` the way the delegation API does, so the child's task
|
|
231
|
+
* carries the vendor's Acceptance Contract. Measured harmless to the caller: `results[0].output`
|
|
232
|
+
* is the child's prose with the acceptance-report block already stripped. It is still a reason
|
|
233
|
+
* to leave the ten blocking delegations on the first door rather than routing everything here.
|
|
234
|
+
* - The output-path append this whole line of work began with is **absent** on this path, on the
|
|
235
|
+
* spawn and on both revivals, read from the child's own `session.jsonl`.
|
|
236
|
+
* ------------------------------------------------------------------------------------------- */
|
|
237
|
+
/** The event `pi-subagents`' RPC bridge listens on. */
|
|
238
|
+
export const RPC_REQUEST_EVENT = "subagents:rpc:v1:request";
|
|
239
|
+
/** Replies are delivered per request, on this prefix plus the request's own id. */
|
|
240
|
+
export const RPC_REPLY_EVENT_PREFIX = "subagents:rpc:v1:reply:";
|
|
241
|
+
/** The event carrying a finished async run, which is where a turn's answer arrives. */
|
|
242
|
+
export const ASYNC_COMPLETE_EVENT = "subagent:async-complete";
|
|
243
|
+
/** The RPC protocol version this module speaks; `pi-subagents` rejects anything else. */
|
|
244
|
+
export const RPC_PROTOCOL_VERSION = 1;
|
|
245
|
+
/**
|
|
246
|
+
* The line carrying the thread handle back to the prompt, in **Fito's own wording**.
|
|
247
|
+
*
|
|
248
|
+
* `prompts/fito-tutor.md` matches on this and `prompts/fito-tutor.test.ts` binds that prompt text
|
|
249
|
+
* to this constant, so renaming one without the other fails. It replaces Task 11.4a's
|
|
250
|
+
* `subagent-run-id.ts` marker, which lifted an id out of the vendor tool's `details`: with the
|
|
251
|
+
* tutor on this path there is no vendor tool result left to annotate, and the handle is something
|
|
252
|
+
* this module already holds.
|
|
253
|
+
*/
|
|
254
|
+
export const THREAD_MARKER = "Fito thread id:";
|
|
255
|
+
/**
|
|
256
|
+
* Read the `thread` parameter into a route.
|
|
257
|
+
*
|
|
258
|
+
* A tool schema is read by a model rather than by a compiler, so the spellings a model reaches
|
|
259
|
+
* for when it has no id yet — `true`, `"true"`, `"new"`, an empty string — all mean *start one*.
|
|
260
|
+
* Only a non-empty id that is none of those continues a conversation, and an absent parameter is
|
|
261
|
+
* the blocking delegation the ten pipeline prompts use.
|
|
262
|
+
*/
|
|
263
|
+
export function readThreadParam(value) {
|
|
264
|
+
if (value === undefined || value === null || value === false)
|
|
265
|
+
return { kind: "none" };
|
|
266
|
+
if (value === true)
|
|
267
|
+
return { kind: "start" };
|
|
268
|
+
if (typeof value !== "string")
|
|
269
|
+
return { kind: "none" };
|
|
270
|
+
const trimmed = value.trim();
|
|
271
|
+
if (trimmed === "" || trimmed === "true" || trimmed === "new")
|
|
272
|
+
return { kind: "start" };
|
|
273
|
+
return { kind: "continue", thread: trimmed };
|
|
274
|
+
}
|
|
275
|
+
/** Wrap one RPC request. `source` is advisory and names this extension in the vendor's logs. */
|
|
276
|
+
export function buildRpcEnvelope(params) {
|
|
277
|
+
return {
|
|
278
|
+
version: RPC_PROTOCOL_VERSION,
|
|
279
|
+
requestId: params.requestId,
|
|
280
|
+
method: params.method,
|
|
281
|
+
params: params.params,
|
|
282
|
+
source: { extension: "fito-delegate" },
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The parameters that start a thread — the agent and its task, and nothing else.
|
|
287
|
+
*
|
|
288
|
+
* **No `context`, deliberately.** An explicit context overrides every agent's own default, and
|
|
289
|
+
* `context: "fork"` would branch the caller's session into the child — handing `fito-tutor` the
|
|
290
|
+
* case solution its allowlist exists to withhold. `/fito-tutor` used to carry that prohibition in
|
|
291
|
+
* prose; with the parameter unreachable from a prompt it belongs here, next to a test.
|
|
292
|
+
*
|
|
293
|
+
* **`timeoutMs` is sent, and it is not decoration.** The vendor's own schema says a *single* async
|
|
294
|
+
* run falls back to 30 minutes but **"async composites have no default parent deadline"** — and an
|
|
295
|
+
* RPC spawn of `{agent, task}` is normalized into a workflow, which is a composite. Omit it and a
|
|
296
|
+
* stuck child has no deadline at all, while {@link awaitThreadTurn}'s local timer would only stop
|
|
297
|
+
* listening and leave it running. The value reaches the child through the workflow defaults
|
|
298
|
+
* (`subagent-executor.ts:3606`). A revival takes its bounds from the run it revives, so
|
|
299
|
+
* {@link buildThreadResumeParams} sends none.
|
|
300
|
+
*/
|
|
301
|
+
export function buildThreadSpawnParams(params) {
|
|
302
|
+
return { agent: params.agent, task: params.task, timeoutMs: params.timeoutMs ?? DELEGATION_TIMEOUT_MS };
|
|
303
|
+
}
|
|
304
|
+
/** The parameters that continue a thread. `runId` is the **child's** id — see the header. */
|
|
305
|
+
export function buildThreadResumeParams(params) {
|
|
306
|
+
return { runId: params.thread, message: params.message };
|
|
307
|
+
}
|
|
308
|
+
/** Read the reply envelope into the run id for this turn, or the reason there is none. */
|
|
309
|
+
export function readRpcAck(reply) {
|
|
310
|
+
if (typeof reply !== "object" || reply === null) {
|
|
311
|
+
return {
|
|
312
|
+
ok: false,
|
|
313
|
+
message: `${DELEGATE_TOOL}: pi-subagents never answered the request, so the conversation did not ` +
|
|
314
|
+
"start. Check that pi-subagents is installed (`pi install npm:pi-subagents`).",
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
const envelope = reply;
|
|
318
|
+
if (envelope.success !== true) {
|
|
319
|
+
const code = envelope.error?.code ?? "(no code)";
|
|
320
|
+
const detail = envelope.error?.message ?? "No reason was given.";
|
|
321
|
+
return { ok: false, message: `${DELEGATE_TOOL}: pi-subagents refused the turn [${code}]. ${detail}` };
|
|
322
|
+
}
|
|
323
|
+
const details = envelope.data?.details;
|
|
324
|
+
const runId = firstIdString(details?.runId, details?.asyncId);
|
|
325
|
+
if (runId === undefined) {
|
|
326
|
+
return { ok: false, message: `${DELEGATE_TOOL}: pi-subagents accepted the turn but reported no run to wait for.` };
|
|
327
|
+
}
|
|
328
|
+
return { ok: true, runId };
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Is this completion the one this turn is waiting for?
|
|
332
|
+
*
|
|
333
|
+
* Every subscriber sees every run's completion, and a run reports the same value under both `id`
|
|
334
|
+
* and `runId`, so either settles it.
|
|
335
|
+
*/
|
|
336
|
+
export function isMatchingCompletion(runId, payload) {
|
|
337
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload))
|
|
338
|
+
return false;
|
|
339
|
+
const { id, runId: completedRunId } = payload;
|
|
340
|
+
return completedRunId === runId || id === runId;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Read a completion into the child's words and the handle for the next turn.
|
|
344
|
+
*
|
|
345
|
+
* The answer comes from `results[0].output` rather than from `summary`: the summary is display
|
|
346
|
+
* text — agent-prefixed on a revival, truncated on a workflow — while `output` is the child's
|
|
347
|
+
* reply as written, with the vendor's acceptance-report block already stripped. A completed run
|
|
348
|
+
* carrying no output is reported as a failure rather than relayed as an empty answer, the same
|
|
349
|
+
* distinction every Fito worker path draws (a failed delegation is not an empty one).
|
|
350
|
+
*
|
|
351
|
+
* **The handle is whichever run the vendor just made resumable, and the order matters across
|
|
352
|
+
* releases.** A workflow completion carries the child one level down (`results[0].runId`) — that
|
|
353
|
+
* is 0.51.0's shape, where the top-level id belongs to the workflow and `resume` refuses it. A
|
|
354
|
+
* **`mode: "single"`** completion carries no child row id at all and its *own* `runId` is the
|
|
355
|
+
* resumable run — that is the shape of every revival, and of a first launch on the 0.50.0 the repo
|
|
356
|
+
* pins. Reading the child first and the completion second is therefore not a preference but the
|
|
357
|
+
* only order that yields a usable handle on both. `currentThread` remains as a last resort so a
|
|
358
|
+
* turn that answers without naming any run still leaves the conversation continuable.
|
|
359
|
+
*/
|
|
360
|
+
export function readThreadOutcome(payload, currentThread) {
|
|
361
|
+
if (typeof payload !== "object" || payload === null) {
|
|
362
|
+
return { ok: false, message: `${DELEGATE_TOOL}: the subagent never reported back, so this turn has no answer.` };
|
|
363
|
+
}
|
|
364
|
+
const completion = payload;
|
|
365
|
+
const first = Array.isArray(completion.results) ? completion.results[0] : undefined;
|
|
366
|
+
if (completion.success !== true) {
|
|
367
|
+
const reported = firstNonEmptyString(completion.error, completion.summary) ?? "It reported no reason.";
|
|
368
|
+
return {
|
|
369
|
+
ok: false,
|
|
370
|
+
message: `${DELEGATE_TOOL}: the subagent turn ended without an answer` +
|
|
371
|
+
`${typeof completion.exitCode === "number" ? ` (exit code ${completion.exitCode})` : ""}. ${reported}`,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
const text = firstNonEmptyString(first?.output);
|
|
375
|
+
if (text === undefined) {
|
|
376
|
+
return { ok: false, message: `${DELEGATE_TOOL}: the subagent finished but sent no reply back.` };
|
|
377
|
+
}
|
|
378
|
+
const thread = firstIdString(first?.runId, completion.runId, completion.id) ?? currentThread;
|
|
379
|
+
if (thread === undefined) {
|
|
380
|
+
return {
|
|
381
|
+
ok: false,
|
|
382
|
+
message: `${DELEGATE_TOOL}: the subagent answered but reported no thread handle, so the conversation cannot be continued.`,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
return { ok: true, text, thread };
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Emit one RPC turn and resolve with its reply and the completion that follows it.
|
|
389
|
+
*
|
|
390
|
+
* Completions are buffered from **before** the request is emitted: the run id only arrives with
|
|
391
|
+
* the reply, and a fast child can finish first, so subscribing after the reply would race. Both
|
|
392
|
+
* subscriptions are dropped either way.
|
|
393
|
+
*/
|
|
394
|
+
export function awaitThreadTurn(pi, envelope, timeoutMs = DELEGATION_TIMEOUT_MS, graceMs = RESPONSE_GRACE_MS) {
|
|
395
|
+
return new Promise((resolve) => {
|
|
396
|
+
const seen = [];
|
|
397
|
+
let settled = false;
|
|
398
|
+
let waitingFor;
|
|
399
|
+
let unsubscribeCompletion;
|
|
400
|
+
let unsubscribeReply;
|
|
401
|
+
const timer = setTimeout(() => finish({ ok: false, message: `${DELEGATE_TOOL}: pi-subagents did not answer this turn in time.` }), timeoutMs + graceMs);
|
|
402
|
+
function finish(ack, completion) {
|
|
403
|
+
if (settled)
|
|
404
|
+
return;
|
|
405
|
+
settled = true;
|
|
406
|
+
clearTimeout(timer);
|
|
407
|
+
if (typeof unsubscribeCompletion === "function")
|
|
408
|
+
unsubscribeCompletion();
|
|
409
|
+
if (typeof unsubscribeReply === "function")
|
|
410
|
+
unsubscribeReply();
|
|
411
|
+
resolve({ ack, completion });
|
|
412
|
+
}
|
|
413
|
+
unsubscribeCompletion = pi.events.on(ASYNC_COMPLETE_EVENT, (payload) => {
|
|
414
|
+
if (waitingFor === undefined) {
|
|
415
|
+
seen.push(payload);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (isMatchingCompletion(waitingFor, payload))
|
|
419
|
+
finish({ ok: true, runId: waitingFor }, payload);
|
|
420
|
+
});
|
|
421
|
+
unsubscribeReply = pi.events.on(`${RPC_REPLY_EVENT_PREFIX}${envelope.requestId}`, (reply) => {
|
|
422
|
+
const ack = readRpcAck(reply);
|
|
423
|
+
if (!ack.ok) {
|
|
424
|
+
finish(ack);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
waitingFor = ack.runId;
|
|
428
|
+
const already = seen.find((payload) => isMatchingCompletion(ack.runId, payload));
|
|
429
|
+
if (already !== undefined)
|
|
430
|
+
finish(ack, already);
|
|
431
|
+
});
|
|
432
|
+
pi.events.emit(RPC_REQUEST_EVENT, envelope);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
function firstIdString(...candidates) {
|
|
436
|
+
return candidates.find((candidate) => typeof candidate === "string" && candidate.length > 0);
|
|
437
|
+
}
|
|
438
|
+
function firstNonEmptyString(...candidates) {
|
|
439
|
+
return candidates.find((candidate) => typeof candidate === "string" && candidate.trim().length > 0);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Run one conversational turn and shape it as a tool result.
|
|
443
|
+
*
|
|
444
|
+
* The reply is followed by {@link THREAD_MARKER} and the handle, because the handle has to reach
|
|
445
|
+
* the **model** to be usable on the next turn and `details` never does. It is the same split
|
|
446
|
+
* Task 11.4a's run-id marker was built for, minus the middleware: this module already holds the
|
|
447
|
+
* id, so it writes it into its own result rather than annotating someone else's.
|
|
448
|
+
*/
|
|
449
|
+
async function runThreadTurn(pi, mode, agent, task) {
|
|
450
|
+
const envelope = mode.kind === "start"
|
|
451
|
+
? buildRpcEnvelope({ method: "spawn", params: buildThreadSpawnParams({ agent, task }), requestId: crypto.randomUUID() })
|
|
452
|
+
: buildRpcEnvelope({
|
|
453
|
+
method: "resume",
|
|
454
|
+
params: buildThreadResumeParams({ thread: mode.thread, message: task }),
|
|
455
|
+
requestId: crypto.randomUUID(),
|
|
456
|
+
});
|
|
457
|
+
const turn = await awaitThreadTurn(pi, envelope);
|
|
458
|
+
if (!turn.ack.ok)
|
|
459
|
+
throw new Error(turn.ack.message);
|
|
460
|
+
const outcome = readThreadOutcome(turn.completion, mode.kind === "continue" ? mode.thread : undefined);
|
|
461
|
+
if (!outcome.ok)
|
|
462
|
+
throw new Error(outcome.message);
|
|
463
|
+
return {
|
|
464
|
+
content: [{ type: "text", text: `${outcome.text}\n\n${THREAD_MARKER} ${outcome.thread}` }],
|
|
465
|
+
details: { agent, thread: outcome.thread, runId: turn.ack.runId, method: envelope.method },
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Register `fito_delegate` for the main session.
|
|
470
|
+
*
|
|
471
|
+
* A non-`completed` delegation **throws**, so pi marks the tool result `isError` and the model
|
|
472
|
+
* reads a failure as a failure — the same contract the `fito_*` fetch tools use.
|
|
473
|
+
*/
|
|
474
|
+
export function registerFitoDelegateTool(pi) {
|
|
475
|
+
pi.registerTool({
|
|
476
|
+
name: DELEGATE_TOOL,
|
|
477
|
+
label: "Delegate to a Fito subagent",
|
|
478
|
+
description: "Delegate one task to a Fito worker subagent and wait for its result. Use whenever a Fito " +
|
|
479
|
+
"flow says to delegate to a named `fito-*` agent: pass the agent name and the full task " +
|
|
480
|
+
"text, and the child's reply comes back as this tool's result. The child runs to " +
|
|
481
|
+
"completion before this tool returns, so its files are on disk and its summary is in hand " +
|
|
482
|
+
"when you read the result. A delegation that fails comes back as an error naming what " +
|
|
483
|
+
"went wrong — a failed delegation is not an empty one. It also runs a **conversation** " +
|
|
484
|
+
"with a worker: see `thread`, for a flow that asks, reads the answer, and comes back " +
|
|
485
|
+
"with a follow-up.",
|
|
486
|
+
promptSnippet: "Delegate one task to a Fito worker subagent and wait for its result",
|
|
487
|
+
promptGuidelines: [
|
|
488
|
+
`Use ${DELEGATE_TOOL} to run a Fito \`fito-*\` worker persona, handing it the agent name and the full task text.`,
|
|
489
|
+
],
|
|
490
|
+
parameters: Type.Object({
|
|
491
|
+
agent: Type.String({
|
|
492
|
+
description: "The worker persona's name, e.g. `fito-case-source-planner` — the `name:` field of a " +
|
|
493
|
+
"Fito agent, not a file path.",
|
|
494
|
+
}),
|
|
495
|
+
task: Type.String({
|
|
496
|
+
description: "The complete task text for the child. It is the child's only briefing — it starts " +
|
|
497
|
+
"fresh and sees nothing of this conversation — so state every path, id, and " +
|
|
498
|
+
"instruction it needs to finish the job. On a conversation this is the next thing " +
|
|
499
|
+
"to say to the worker, and it already remembers the turns before it.",
|
|
500
|
+
}),
|
|
501
|
+
thread: Type.Optional(Type.Union([Type.Boolean(), Type.String()], {
|
|
502
|
+
description: "Leave this out for a one-off delegation: the worker answers once and is gone. " +
|
|
503
|
+
"Pass `true` to open a conversation you will come back to — the result then " +
|
|
504
|
+
`ends with a "${THREAD_MARKER} <id>" line. Pass that id to put the next ` +
|
|
505
|
+
"question to the same worker, which still remembers everything said so far.",
|
|
506
|
+
})),
|
|
507
|
+
}),
|
|
508
|
+
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
509
|
+
const mode = readThreadParam(params.thread);
|
|
510
|
+
if (mode.kind !== "none")
|
|
511
|
+
return await runThreadTurn(pi, mode, params.agent, params.task);
|
|
512
|
+
const request = buildDelegationRequest({
|
|
513
|
+
agent: params.agent,
|
|
514
|
+
task: params.task,
|
|
515
|
+
cwd: ctx.cwd,
|
|
516
|
+
requestId: crypto.randomUUID(),
|
|
517
|
+
});
|
|
518
|
+
const response = await awaitDelegation(pi, request);
|
|
519
|
+
const outcome = readDelegationOutcome(request, response);
|
|
520
|
+
if (!outcome.ok)
|
|
521
|
+
throw new Error(outcome.message);
|
|
522
|
+
return {
|
|
523
|
+
content: [{ type: "text", text: outcome.text }],
|
|
524
|
+
// `details` is what pi renders and stores, and never reaches the model — which is
|
|
525
|
+
// why the thread path writes its handle into the result text instead. `status` is
|
|
526
|
+
// always `completed` here,
|
|
527
|
+
// since every other status throws above — it is carried so a reader of a stored
|
|
528
|
+
// result need not infer which branch produced it — while `usage` is the part that
|
|
529
|
+
// actually varies: cost, turns, and `durationMs` for the run just paid for.
|
|
530
|
+
details: {
|
|
531
|
+
agent: request.agent,
|
|
532
|
+
runId: response?.runId,
|
|
533
|
+
status: response?.status,
|
|
534
|
+
usage: response?.usage,
|
|
535
|
+
},
|
|
536
|
+
};
|
|
537
|
+
},
|
|
538
|
+
});
|
|
539
|
+
}
|