@herbertgao/pi-extensions 2026.11.0 → 2026.11.2
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 +768 -0
- package/README.md +7 -7
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/extensions/clef.mjs +1 -1
- package/node_modules/@herbertgao/pi-agency/CHANGELOG.md +19 -0
- package/node_modules/@herbertgao/pi-agency/package.json +4 -2
- package/node_modules/@herbertgao/pi-agency/src/index.ts +1 -1
- package/node_modules/@herbertgao/pi-bark/CHANGELOG.md +52 -0
- package/node_modules/@herbertgao/pi-bark/package.json +2 -1
- package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +207 -0
- package/node_modules/@herbertgao/pi-cc-extensions/README.en.md +7 -5
- package/node_modules/@herbertgao/pi-cc-extensions/README.md +10 -7
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/config.ts +20 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +61 -6
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/agent-summary/core.ts +11 -16
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/agent-summary/index.ts +60 -11
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/compact-thinking.ts +29 -11
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/footer-layout.ts +54 -6
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/footer.ts +23 -5
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/session-tokens.ts +52 -0
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +5 -4
- package/node_modules/@herbertgao/pi-impeccable/CHANGELOG.md +13 -0
- package/node_modules/@herbertgao/pi-impeccable/package.json +2 -1
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-subagents/README.md +43 -33
- package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +1 -1
- package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +2 -0
- package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
- package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +9 -8
- package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +70 -9
- package/node_modules/@herbertgao/pi-subagents/src/agent-record-entry.ts +167 -0
- package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +156 -13
- package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +52 -8
- package/node_modules/@herbertgao/pi-subagents/src/default-agents.ts +3 -4
- package/node_modules/@herbertgao/pi-subagents/src/index.ts +153 -56
- package/node_modules/@herbertgao/pi-subagents/src/settings.ts +3 -2
- package/node_modules/@herbertgao/pi-subagents/src/status-note.ts +3 -1
- package/node_modules/@herbertgao/pi-subagents/src/types.ts +5 -0
- package/node_modules/@herbertgao/pi-subagents/src/workflow/host.ts +88 -6
- package/node_modules/@herbertgao/pi-subagents/src/workflow/progress.ts +3 -2
- package/node_modules/@herbertgao/sol-pi/CHANGELOG.md +84 -0
- package/node_modules/@herbertgao/sol-pi/README.md +3 -3
- package/node_modules/@herbertgao/sol-pi/THIRD_PARTY_NOTICES.md +4 -4
- package/node_modules/@herbertgao/sol-pi/agents-install.md +3 -3
- package/node_modules/@herbertgao/sol-pi/docs/compatibility.md +1 -1
- package/node_modules/@herbertgao/sol-pi/package.json +2 -1
- package/node_modules/pi-provider-qoder/README.md +40 -0
- package/node_modules/pi-provider-qoder/dist/index.js +69 -1
- package/node_modules/pi-provider-qoder/package.json +4 -3
- package/node_modules/pi-typesafe/README.md +3 -3
- package/node_modules/pi-typesafe/dist/auth.d.ts +21 -4
- package/node_modules/pi-typesafe/dist/auth.js +32 -7
- package/node_modules/pi-typesafe/dist/backends.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/backends.js +9 -0
- package/node_modules/pi-typesafe/dist/client.js +7 -12
- package/node_modules/pi-typesafe/dist/errors.d.ts +3 -2
- package/node_modules/pi-typesafe/dist/errors.js +12 -10
- package/node_modules/pi-typesafe/dist/extension.js +14 -10
- package/node_modules/pi-typesafe/package.json +1 -1
- package/package.json +10 -9
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
ExtensionAPI,
|
|
24
24
|
ExtensionContext,
|
|
25
25
|
} from "@earendil-works/pi-coding-agent"
|
|
26
|
+
import { worktreeLost } from "./agent-record-entry.js"
|
|
26
27
|
import { resumeAgent, runAgent, type ToolActivity } from "./agent-runner.js"
|
|
27
28
|
import { getAgentConfig } from "./agent-types.js"
|
|
28
29
|
import { assignHandle, handleBase } from "./mention.js"
|
|
@@ -232,8 +233,13 @@ interface SpawnOptions {
|
|
|
232
233
|
* Internal capability, like `resumeSessionFile`: a forged handle would
|
|
233
234
|
* duplicate a live agent's name and make `resolveMention` ambiguous, so
|
|
234
235
|
* `spawnTopLevel` strips it from anything a caller sends.
|
|
236
|
+
*
|
|
237
|
+
* `id` keeps the evicted agent's id too, so the id the model already holds
|
|
238
|
+
* names the reopened conversation (`Agent({ resume })`, a workflow's
|
|
239
|
+
* `resume`). A name is taken back only while nothing else holds it — see
|
|
240
|
+
* `reclaimable`.
|
|
235
241
|
*/
|
|
236
|
-
reclaim?: { handle
|
|
242
|
+
reclaim?: { id?: string; handle?: string; alias?: string }
|
|
237
243
|
model?: Model<any>
|
|
238
244
|
maxTurns?: number
|
|
239
245
|
isolated?: boolean
|
|
@@ -575,7 +581,18 @@ export class AgentManager {
|
|
|
575
581
|
// can fix and retry; the RPC layer converts throws into error envelopes.
|
|
576
582
|
assertValidSpawnCwd(options.cwd)
|
|
577
583
|
|
|
578
|
-
|
|
584
|
+
// A reclaimed id must not be live: the record would replace the one in the
|
|
585
|
+
// map, orphaning its run beyond the reach of stop and `abortAll()`.
|
|
586
|
+
// Callers reopen only what they found evicted; this keeps that true.
|
|
587
|
+
if (
|
|
588
|
+
options.reclaim?.id !== undefined &&
|
|
589
|
+
this.agents.has(options.reclaim.id)
|
|
590
|
+
) {
|
|
591
|
+
throw new Error(
|
|
592
|
+
`Agent ${options.reclaim.id} is still in memory, so it cannot be reopened from its saved session.`,
|
|
593
|
+
)
|
|
594
|
+
}
|
|
595
|
+
const id = options.reclaim?.id ?? randomUUID().slice(0, 17)
|
|
579
596
|
const abortController = new AbortController()
|
|
580
597
|
const record: AgentRecord = {
|
|
581
598
|
id,
|
|
@@ -585,15 +602,18 @@ export class AgentManager {
|
|
|
585
602
|
// not consume a name a top-level sibling could otherwise take.
|
|
586
603
|
handle: !isTopLevelAgent(options)
|
|
587
604
|
? undefined
|
|
588
|
-
: // A reclaimed handle is
|
|
589
|
-
// spawn is reopening, and re-deriving it would
|
|
590
|
-
|
|
605
|
+
: // A reclaimed handle is taken back as-is while it is free: it belongs to
|
|
606
|
+
// the conversation this spawn is reopening, and re-deriving it would
|
|
607
|
+
// lose the numbering.
|
|
608
|
+
(this.reclaimable(options.reclaim?.handle, options.reclaim?.id) ??
|
|
591
609
|
assignHandle(handleBase(type), this.takenHandles())),
|
|
592
610
|
description: options.description,
|
|
593
611
|
// Reclaimed here, or filled in below from `name` — in which case it must
|
|
594
612
|
// see the handle this record just took, since both come out of the same
|
|
595
613
|
// namespace.
|
|
596
|
-
alias: isTopLevelAgent(options)
|
|
614
|
+
alias: isTopLevelAgent(options)
|
|
615
|
+
? this.reclaimable(options.reclaim?.alias, options.reclaim?.id)
|
|
616
|
+
: undefined,
|
|
597
617
|
// Overwritten below when the spawn is actually queued; a foreground spawn
|
|
598
618
|
// that queues flips to "queued" there rather than being guessed at here,
|
|
599
619
|
// since the pool decision needs the finished record.
|
|
@@ -619,6 +639,12 @@ export class AgentManager {
|
|
|
619
639
|
workflowId: options.workflowId,
|
|
620
640
|
maxSubagentDepth: options.maxSubagentDepth,
|
|
621
641
|
rootSessionId: options.rootSessionId,
|
|
642
|
+
// A reopen continues this file until its session says otherwise (a fork
|
|
643
|
+
// gets its own, set at session creation). Seeded so a run that fails
|
|
644
|
+
// before it has a session still names the conversation in its saved
|
|
645
|
+
// record and tombstone: without it, the next resume would be refused as
|
|
646
|
+
// never saved.
|
|
647
|
+
sessionFile: options.resumeSessionFile,
|
|
622
648
|
}
|
|
623
649
|
this.agents.set(id, record)
|
|
624
650
|
// After the insert, so `takenHandles()` already counts this record's own
|
|
@@ -830,10 +856,15 @@ export class AgentManager {
|
|
|
830
856
|
// mentions pass none, and RPC forwards only its caller's value. Workflow
|
|
831
857
|
// children are the exception, as in Claude Code, whose workflow agent()
|
|
832
858
|
// reads only the script's option and ignores the definition's.
|
|
859
|
+
// A reopen never gets a worktree, as in Claude Code: a fresh copy of HEAD
|
|
860
|
+
// would not hold the earlier run's edits, and a reopen whose old worktree
|
|
861
|
+
// held any is refused before it gets here (`reopenTarget`).
|
|
833
862
|
const isolation =
|
|
834
|
-
options.
|
|
835
|
-
?
|
|
836
|
-
:
|
|
863
|
+
options.resumeSessionFile !== undefined
|
|
864
|
+
? undefined
|
|
865
|
+
: options.workflowId !== undefined
|
|
866
|
+
? options.isolation
|
|
867
|
+
: (getAgentConfig(type)?.isolation ?? options.isolation)
|
|
837
868
|
let worktreeCwd: string | undefined
|
|
838
869
|
if (isolation === "worktree" && isWorktreeIsolationEnabled()) {
|
|
839
870
|
const wt = await createWorktree(pi, baseCwd, id)
|
|
@@ -1626,6 +1657,35 @@ export class AgentManager {
|
|
|
1626
1657
|
return taken
|
|
1627
1658
|
}
|
|
1628
1659
|
|
|
1660
|
+
/**
|
|
1661
|
+
* `name`, if a reopened agent can take it back. Within one activation that is
|
|
1662
|
+
* always so — tombstoned names are never allocated (`takenHandles`) — but a
|
|
1663
|
+
* resume after a reload reopens from the session's saved record, with no
|
|
1664
|
+
* tombstone holding the name, so a fresh agent may have taken it since. A
|
|
1665
|
+
* name held by a live record, or by another agent's tombstone, stays with
|
|
1666
|
+
* its holder and the reopened agent gets a fresh one.
|
|
1667
|
+
*
|
|
1668
|
+
* `id` is the agent being reopened, when it keeps its own: its tombstone is
|
|
1669
|
+
* the one holding the name, legitimately. The `@handle` path keeps no id and
|
|
1670
|
+
* reopens exactly the tombstone that holds the name, so it skips that check.
|
|
1671
|
+
*/
|
|
1672
|
+
private reclaimable(
|
|
1673
|
+
name: string | undefined,
|
|
1674
|
+
id: string | undefined,
|
|
1675
|
+
): string | undefined {
|
|
1676
|
+
if (name === undefined) return undefined
|
|
1677
|
+
for (const record of this.agents.values()) {
|
|
1678
|
+
if (record.handle === name || record.alias === name) return undefined
|
|
1679
|
+
}
|
|
1680
|
+
if (id !== undefined) {
|
|
1681
|
+
for (const entry of this.tombstones.values()) {
|
|
1682
|
+
if (entry.id !== id && (entry.handle === name || entry.alias === name))
|
|
1683
|
+
return undefined
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
return name
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1629
1689
|
/**
|
|
1630
1690
|
* Resolve an `@name` from the prompt. Matches a top-level agent's handle
|
|
1631
1691
|
* case-insensitively, preferring one that can still be steered and otherwise
|
|
@@ -1744,6 +1804,7 @@ export class AgentManager {
|
|
|
1744
1804
|
description: record.description,
|
|
1745
1805
|
sessionFile: record.sessionFile,
|
|
1746
1806
|
completedAt: record.completedAt ?? Date.now(),
|
|
1807
|
+
worktreeLost: worktreeLost(record) || undefined,
|
|
1747
1808
|
})
|
|
1748
1809
|
// Bound the memory a long session can accumulate. Oldest first, since the
|
|
1749
1810
|
// agent someone still wants to reach is the one they used most recently.
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-record-entry.ts — what a finished top-level agent leaves behind in the session.
|
|
3
|
+
*
|
|
4
|
+
* The live record is evicted about ten minutes after the agent finishes, read or
|
|
5
|
+
* not, and is gone after a reload or a restart. Its completion notification
|
|
6
|
+
* carries only a preview of the result and points at `get_subagent_result` for
|
|
7
|
+
* the rest, so that pointer has to outlive the record: the final record is
|
|
8
|
+
* appended to the session as this entry, and `get_subagent_result` falls back
|
|
9
|
+
* to it.
|
|
10
|
+
*
|
|
11
|
+
* It is also what lets `Agent({ resume })` reopen an evicted agent: the entry
|
|
12
|
+
* names the session file the conversation lives in, so the id the model holds
|
|
13
|
+
* still leads somewhere after eviction, a reload or a restart.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { existsSync } from "node:fs"
|
|
17
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent"
|
|
18
|
+
import { resolveSpawnType } from "./agent-types.js"
|
|
19
|
+
import type { AgentRecord, SubagentType } from "./types.js"
|
|
20
|
+
|
|
21
|
+
/** `customType` of the session entry a finished top-level agent appends. */
|
|
22
|
+
export const AGENT_RECORD_ENTRY_TYPE = "subagents:record"
|
|
23
|
+
|
|
24
|
+
/** The persisted snapshot of a finished agent. */
|
|
25
|
+
export interface AgentRecordEntryData {
|
|
26
|
+
id: string
|
|
27
|
+
type: AgentRecord["type"]
|
|
28
|
+
description: string
|
|
29
|
+
status: AgentRecord["status"]
|
|
30
|
+
result?: string
|
|
31
|
+
error?: string
|
|
32
|
+
startedAt: number
|
|
33
|
+
completedAt?: number
|
|
34
|
+
/**
|
|
35
|
+
* The agent's persisted pi session, when it has one — what a resume reopens.
|
|
36
|
+
* Absent for an in-memory session (`rememberAgents` off, or
|
|
37
|
+
* `persist_session: false`), which leaves nothing to come back to.
|
|
38
|
+
*/
|
|
39
|
+
sessionFile?: string
|
|
40
|
+
/** Names to give back to the reopened agent, as the `@handle` path does. */
|
|
41
|
+
handle?: string
|
|
42
|
+
alias?: string
|
|
43
|
+
/** Set when the agent's work lived in a worktree that is now gone — see {@link worktreeLost}. */
|
|
44
|
+
worktreeLost?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether the agent ran in a worktree that held its work and has since been
|
|
49
|
+
* removed. Settling always removes the worktree, committing any changes to a
|
|
50
|
+
* branch first, so only a clean one (`hasChanges: false`) leaves nothing
|
|
51
|
+
* behind; a run that failed before cleanup reported nothing and counts as lost.
|
|
52
|
+
* Claude Code draws the same line: it resumes an isolated agent outside its
|
|
53
|
+
* worktree only when the worktree was removed clean, and never builds a fresh
|
|
54
|
+
* one on resume.
|
|
55
|
+
*/
|
|
56
|
+
export function worktreeLost(
|
|
57
|
+
record: Pick<AgentRecord, "worktree" | "worktreeResult">,
|
|
58
|
+
): boolean {
|
|
59
|
+
return (
|
|
60
|
+
record.worktree !== undefined && record.worktreeResult?.hasChanges !== false
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Snapshot a finished record for {@link AGENT_RECORD_ENTRY_TYPE}. */
|
|
65
|
+
export function agentRecordEntryData(
|
|
66
|
+
record: AgentRecord,
|
|
67
|
+
): AgentRecordEntryData {
|
|
68
|
+
return {
|
|
69
|
+
id: record.id,
|
|
70
|
+
type: record.type,
|
|
71
|
+
description: record.description,
|
|
72
|
+
status: record.status,
|
|
73
|
+
result: record.result,
|
|
74
|
+
error: record.error,
|
|
75
|
+
startedAt: record.startedAt,
|
|
76
|
+
completedAt: record.completedAt,
|
|
77
|
+
sessionFile: record.sessionFile,
|
|
78
|
+
handle: record.handle,
|
|
79
|
+
alias: record.alias,
|
|
80
|
+
worktreeLost: worktreeLost(record) || undefined,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isAgentRecordEntryData(data: unknown): data is AgentRecordEntryData {
|
|
85
|
+
if (typeof data !== "object" || data === null) return false
|
|
86
|
+
const { id, type, status, startedAt } = data as Record<string, unknown>
|
|
87
|
+
return (
|
|
88
|
+
typeof id === "string" &&
|
|
89
|
+
typeof type === "string" &&
|
|
90
|
+
typeof status === "string" &&
|
|
91
|
+
typeof startedAt === "number"
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The latest persisted record of agent `id` on the current branch. A resumed
|
|
97
|
+
* agent appends a new entry on each finish, so the last one wins; an entry on
|
|
98
|
+
* an abandoned branch is not this conversation's.
|
|
99
|
+
*/
|
|
100
|
+
export function findAgentRecordEntry(
|
|
101
|
+
ctx: ExtensionContext,
|
|
102
|
+
id: string,
|
|
103
|
+
): AgentRecordEntryData | undefined {
|
|
104
|
+
const entries = ctx.sessionManager.getBranch()
|
|
105
|
+
for (let i = entries.length - 1; i >= 0; i--) {
|
|
106
|
+
const entry = entries[i]
|
|
107
|
+
if (entry.type !== "custom" || entry.customType !== AGENT_RECORD_ENTRY_TYPE)
|
|
108
|
+
continue
|
|
109
|
+
if (isAgentRecordEntryData(entry.data) && entry.data.id === id)
|
|
110
|
+
return entry.data
|
|
111
|
+
}
|
|
112
|
+
return undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Whether an evicted agent's saved conversation can be reopened, and the
|
|
117
|
+
* agent type to reopen it under. Shared by `@handle`, `Agent({ resume })` and
|
|
118
|
+
* a workflow's `resume`, so all three refuse the same cases in the same words;
|
|
119
|
+
* `reason` completes "Could not resume X —" and "cannot be resumed —".
|
|
120
|
+
*
|
|
121
|
+
* The file is checked here rather than left to SessionManager.open: that runs
|
|
122
|
+
* inside runAgent, whose rejection lands on the record as an agent error, not
|
|
123
|
+
* as a refusal. A `/new` in another pi window or a manual delete makes the
|
|
124
|
+
* conversation unrecoverable (Claude Code's `not_reachable`); `gone` marks
|
|
125
|
+
* that, and the no-file case, as refusals that can never clear.
|
|
126
|
+
*
|
|
127
|
+
* The Agent tool deliberately falls back to general-purpose for a type it
|
|
128
|
+
* cannot resolve (#183), which covers a deleted file AND a merely disabled
|
|
129
|
+
* one. A resume must not inherit that: reopening this conversation under a
|
|
130
|
+
* different agent's prompt and tools is not continuing it, and the new record
|
|
131
|
+
* would re-tombstone under the substitute, so the handle would never find its
|
|
132
|
+
* way back. `@handle` and the Agent tool reload the agent files first.
|
|
133
|
+
*
|
|
134
|
+
* `worktreeLost` refuses for good, as Claude Code does ("its worktree no
|
|
135
|
+
* longer exists"): the conversation's edits were made in a directory that is
|
|
136
|
+
* gone, and reopening it in the parent's checkout or a fresh worktree would
|
|
137
|
+
* continue that work somewhere it never happened.
|
|
138
|
+
*/
|
|
139
|
+
export function reopenTarget(
|
|
140
|
+
type: string,
|
|
141
|
+
sessionFile: string | undefined,
|
|
142
|
+
worktreeLost?: boolean,
|
|
143
|
+
):
|
|
144
|
+
| { ok: true; type: SubagentType; sessionFile: string }
|
|
145
|
+
| { ok: false; reason: string; gone: boolean } {
|
|
146
|
+
if (sessionFile === undefined) {
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
reason:
|
|
150
|
+
"its conversation was not saved (`rememberAgents` or `persist_session` was off)",
|
|
151
|
+
gone: true,
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (!existsSync(sessionFile))
|
|
155
|
+
return { ok: false, reason: "its session is gone", gone: true }
|
|
156
|
+
if (worktreeLost)
|
|
157
|
+
return { ok: false, reason: "its worktree no longer exists", gone: true }
|
|
158
|
+
const dispatch = resolveSpawnType(type)
|
|
159
|
+
if (!dispatch.ok || dispatch.fellBackFrom !== undefined) {
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
reason: `the ${type} agent is no longer available`,
|
|
163
|
+
gone: false,
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { ok: true, type: dispatch.type, sessionFile }
|
|
167
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { readFileSync } from "node:fs"
|
|
6
6
|
import { homedir } from "node:os"
|
|
7
|
-
import { basename, dirname, isAbsolute, join, resolve } from "node:path"
|
|
7
|
+
import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path"
|
|
8
8
|
import type { Model } from "@earendil-works/pi-ai"
|
|
9
9
|
import type {
|
|
10
10
|
ExtensionContext,
|
|
@@ -102,11 +102,12 @@ export function extensionCanonicalName(extPath: string): string {
|
|
|
102
102
|
* - a `node_modules` directory: a package never spans one (it's where OTHER
|
|
103
103
|
* packages live), so reaching it means we've climbed out of the package —
|
|
104
104
|
* stop before reading a consumer's or parent package's manifest.
|
|
105
|
-
* The name is then taken only when that root's `pi.extensions` manifest
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
105
|
+
* The name is then taken only when that root's `pi.extensions` manifest declares
|
|
106
|
+
* this entry, either as the file itself or as a directory containing it (pi
|
|
107
|
+
* expands `"./dist"` to `dist/index.js`). That "declares this entry" check is
|
|
108
|
+
* deliberate: our own test fixtures live under this repo, whose root manifest
|
|
109
|
+
* declares `./src/index.ts` as `@tintinweb/pi-subagents`, so a looser rule would
|
|
110
|
+
* misattribute every co-located file to `pi-subagents`.
|
|
110
111
|
*/
|
|
111
112
|
function extensionPackageName(extPath: string): string | undefined {
|
|
112
113
|
// `builtin:<name>` names no file; climbing from it would read the process cwd's manifest.
|
|
@@ -130,7 +131,15 @@ function extensionPackageName(extPath: string): string | undefined {
|
|
|
130
131
|
if (
|
|
131
132
|
typeof pkg.name === "string" &&
|
|
132
133
|
Array.isArray(entries) &&
|
|
133
|
-
entries.some((e) =>
|
|
134
|
+
entries.some((e) => {
|
|
135
|
+
if (typeof e !== "string") return false
|
|
136
|
+
// A declaration may name a directory (`"./dist"`) that pi resolves to
|
|
137
|
+
// `dist/index.js`, so an entry belongs to this package when it equals the
|
|
138
|
+
// declaration or lives underneath it. The `${sep}` boundary keeps that
|
|
139
|
+
// prefix test platform-correct (`dist-v2` never matches `"./dist"`).
|
|
140
|
+
const declared = resolve(dir, e)
|
|
141
|
+
return declared === entry || entry.startsWith(`${declared}${sep}`)
|
|
142
|
+
})
|
|
134
143
|
) {
|
|
135
144
|
const short = pkg.name.startsWith("@")
|
|
136
145
|
? pkg.name.slice(pkg.name.indexOf("/") + 1)
|
|
@@ -237,6 +246,36 @@ export function parseExtSelectors(entries: string[]): {
|
|
|
237
246
|
return { extNames, narrowing }
|
|
238
247
|
}
|
|
239
248
|
|
|
249
|
+
/** pi's MCP resource tools, which it counts as MCP tools alongside `mcp__<server>__<tool>`. */
|
|
250
|
+
const MCP_RESOURCE_TOOL_NAMES = new Set([
|
|
251
|
+
"list_mcp_resources",
|
|
252
|
+
"list_mcp_resource_templates",
|
|
253
|
+
"read_mcp_resource",
|
|
254
|
+
])
|
|
255
|
+
|
|
256
|
+
/** The tools that reach `codemode`/`deferred` MCP tools: neither is declared to the model. */
|
|
257
|
+
const MCP_BRIDGE_TOOL_NAMES = new Set(["codemode", "tool_search"])
|
|
258
|
+
|
|
259
|
+
const isMcpToolName = (name: string) =>
|
|
260
|
+
name.startsWith("mcp__") || MCP_RESOURCE_TOOL_NAMES.has(name)
|
|
261
|
+
|
|
262
|
+
/** Whether `name` matches one of the `tools:` `mcp__` entries: an exact name or a `*` pattern. */
|
|
263
|
+
function matchesToolPattern(
|
|
264
|
+
name: string,
|
|
265
|
+
patterns: readonly string[],
|
|
266
|
+
): boolean {
|
|
267
|
+
return patterns.some((p) =>
|
|
268
|
+
p.includes("*")
|
|
269
|
+
? new RegExp(
|
|
270
|
+
`^${p
|
|
271
|
+
.split("*")
|
|
272
|
+
.map((s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&"))
|
|
273
|
+
.join(".*")}$`,
|
|
274
|
+
).test(name)
|
|
275
|
+
: p === name,
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
240
279
|
/**
|
|
241
280
|
* Keep a subagent's tool scope correct as extensions register tools over time.
|
|
242
281
|
*
|
|
@@ -270,6 +309,8 @@ export function installExtensionToolScope(
|
|
|
270
309
|
disallowedSet: Set<string> | undefined
|
|
271
310
|
extNames: Set<string>
|
|
272
311
|
narrowing: Map<string, Set<string>>
|
|
312
|
+
/** Bare `mcp__` entries from `tools:`; undefined leaves MCP tools to the rules above. */
|
|
313
|
+
mcpToolPatterns: string[] | undefined
|
|
273
314
|
/**
|
|
274
315
|
* Injected `customTools` to keep active regardless of the built-in list.
|
|
275
316
|
*
|
|
@@ -289,6 +330,7 @@ export function installExtensionToolScope(
|
|
|
289
330
|
disallowedSet,
|
|
290
331
|
extNames,
|
|
291
332
|
narrowing,
|
|
333
|
+
mcpToolPatterns,
|
|
292
334
|
readmitToolNames,
|
|
293
335
|
nestedCallGate,
|
|
294
336
|
} = ctx
|
|
@@ -312,6 +354,25 @@ export function installExtensionToolScope(
|
|
|
312
354
|
keep.add(name)
|
|
313
355
|
}
|
|
314
356
|
}
|
|
357
|
+
// Bare `mcp__` entries, as in pi's `--tools`: only matching MCP tools stay, from
|
|
358
|
+
// whichever extension serves them (pi's MCP or an adapter), even one the `ext:`
|
|
359
|
+
// opt-in left out — and so do the bridges that reach them.
|
|
360
|
+
if (mcpToolPatterns) {
|
|
361
|
+
for (const name of keep) {
|
|
362
|
+
if (isMcpToolName(name) && !matchesToolPattern(name, mcpToolPatterns))
|
|
363
|
+
keep.delete(name)
|
|
364
|
+
}
|
|
365
|
+
for (const extension of loader.getExtensions().extensions) {
|
|
366
|
+
for (const name of extension.tools.keys()) {
|
|
367
|
+
if (disallowedSet?.has(name)) continue
|
|
368
|
+
if (
|
|
369
|
+
MCP_BRIDGE_TOOL_NAMES.has(name) ||
|
|
370
|
+
matchesToolPattern(name, mcpToolPatterns)
|
|
371
|
+
)
|
|
372
|
+
keep.add(name)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
315
376
|
for (const name of EXCLUDED_TOOL_NAMES) keep.delete(name)
|
|
316
377
|
// Injected tools are legitimately active for this agent — re-admit them so
|
|
317
378
|
// the renarrow keeps them in the active set and beforeToolCall doesn't
|
|
@@ -321,13 +382,14 @@ export function installExtensionToolScope(
|
|
|
321
382
|
return keep
|
|
322
383
|
}
|
|
323
384
|
|
|
324
|
-
// Names this agent selects explicitly: its built-ins, injected tools,
|
|
325
|
-
// `
|
|
385
|
+
// Names this agent selects explicitly: its built-ins, injected tools, `ext:foo/bar`
|
|
386
|
+
// narrowings and exact `mcp__` entries. pi activates a named tool even when it is
|
|
326
387
|
// `defaultActive: false`, as `--tools` does.
|
|
327
388
|
const named = new Set([
|
|
328
389
|
...toolNames,
|
|
329
390
|
...readmitToolNames,
|
|
330
391
|
...[...narrowing.values()].flatMap((s) => [...s]),
|
|
392
|
+
...(mcpToolPatterns ?? []).filter((p) => !p.includes("*")),
|
|
331
393
|
])
|
|
332
394
|
|
|
333
395
|
// Activate what pi itself would: only tools declared to the model (`direct`/
|
|
@@ -836,6 +898,32 @@ function resolveConfiguredSessionDir(
|
|
|
836
898
|
return resolve(cwd, sessionDir)
|
|
837
899
|
}
|
|
838
900
|
|
|
901
|
+
/**
|
|
902
|
+
* Open an agent's saved session to continue it — or a copy of it, when the
|
|
903
|
+
* session belongs to another parent.
|
|
904
|
+
*
|
|
905
|
+
* pi's `/fork` and `/clone` copy the parent's entries, saved agent records
|
|
906
|
+
* included, so two parent sessions can each resume the same agent. Appending
|
|
907
|
+
* both continuations to one file would interleave two histories in it. The
|
|
908
|
+
* child's header names the parent that spawned it; a resume from any other
|
|
909
|
+
* parent forks the child alongside, exactly as pi forked the parent, and the
|
|
910
|
+
* copy is that parent's from then on. A header with no parent (a child
|
|
911
|
+
* spawned from a context without a session file) gives nothing to compare,
|
|
912
|
+
* so it opens in place as before.
|
|
913
|
+
*/
|
|
914
|
+
function reopenSession(
|
|
915
|
+
file: string,
|
|
916
|
+
parentSessionFile: string | undefined,
|
|
917
|
+
sessionDir: string | undefined,
|
|
918
|
+
): SessionManager {
|
|
919
|
+
const opened = SessionManager.open(file, sessionDir)
|
|
920
|
+
const owner = opened.getHeader()?.parentSession
|
|
921
|
+
if (owner === undefined || owner === parentSessionFile) return opened
|
|
922
|
+
return SessionManager.forkFrom(file, opened.getCwd(), sessionDir, {
|
|
923
|
+
parentSession: parentSessionFile,
|
|
924
|
+
})
|
|
925
|
+
}
|
|
926
|
+
|
|
839
927
|
export async function runAgent(
|
|
840
928
|
ctx: ExtensionContext,
|
|
841
929
|
type: SubagentType,
|
|
@@ -1104,6 +1192,13 @@ export async function runAgent(
|
|
|
1104
1192
|
}
|
|
1105
1193
|
}
|
|
1106
1194
|
}
|
|
1195
|
+
// MCP tools come from extensions, so with none loaded these entries cannot match.
|
|
1196
|
+
if (agentConfig?.mcpToolPatterns && noExtensions) {
|
|
1197
|
+
options.onToolActivity?.({
|
|
1198
|
+
type: "end",
|
|
1199
|
+
toolName: `tools-error:${agentConfig.mcpToolPatterns.join(", ")} requested by agent "${type}" need extensions, but it loads none`,
|
|
1200
|
+
})
|
|
1201
|
+
}
|
|
1107
1202
|
|
|
1108
1203
|
// A subagent spawns mid-task, so a bad `extensions:`/`ext:` entry warns rather
|
|
1109
1204
|
// than aborts. Two distinct misconfigurations to catch:
|
|
@@ -1234,6 +1329,49 @@ export async function runAgent(
|
|
|
1234
1329
|
...structuredToolNames,
|
|
1235
1330
|
])
|
|
1236
1331
|
|
|
1332
|
+
// As in Claude Code, refuse an agent whose `tools:` list resolved to nothing: with
|
|
1333
|
+
// no tool it can only narrate calls it cannot make. Extension tools register after
|
|
1334
|
+
// spawn (MCP once its servers connect), so they count as possible whenever an
|
|
1335
|
+
// extension they could come from loaded, or any `mcp__` entry can reach a bridge.
|
|
1336
|
+
const loadedNames = new Set(
|
|
1337
|
+
loader
|
|
1338
|
+
.getExtensions()
|
|
1339
|
+
.extensions.filter((e) => e.path !== `<inline:${SCOPE_GATE_NAME}>`)
|
|
1340
|
+
.flatMap((e) => extensionCanonicalNames(e.path)),
|
|
1341
|
+
)
|
|
1342
|
+
const unrecognized =
|
|
1343
|
+
agentConfig?.builtinToolNames?.filter(
|
|
1344
|
+
(n) => !BUILTIN_TOOL_NAMES.includes(n),
|
|
1345
|
+
) ?? []
|
|
1346
|
+
const unmatched = [...extNames].filter((n) => !loadedNames.has(n))
|
|
1347
|
+
// `extensions: false` and `isolated` load nothing, so `loadedNames` is empty then.
|
|
1348
|
+
const extensionToolsPossible =
|
|
1349
|
+
loadedNames.size > 0 &&
|
|
1350
|
+
(extNames.size === 0 ||
|
|
1351
|
+
unmatched.length < extNames.size ||
|
|
1352
|
+
!!agentConfig?.mcpToolPatterns)
|
|
1353
|
+
if (
|
|
1354
|
+
(unrecognized.length > 0 || unmatched.length > 0) &&
|
|
1355
|
+
toolNames.every(
|
|
1356
|
+
(n) => !BUILTIN_TOOL_NAMES.includes(n) || disallowedSet?.has(n),
|
|
1357
|
+
) &&
|
|
1358
|
+
readmitToolNames.size === 0 &&
|
|
1359
|
+
!extensionToolsPossible
|
|
1360
|
+
) {
|
|
1361
|
+
const reasons = [
|
|
1362
|
+
unrecognized.length > 0
|
|
1363
|
+
? `unrecognized [${unrecognized.join(", ")}]`
|
|
1364
|
+
: "",
|
|
1365
|
+
unmatched.length > 0
|
|
1366
|
+
? `recognized but matched no tools in this session [${unmatched.map((n) => `ext:${n}`).join(", ")}]`
|
|
1367
|
+
: "",
|
|
1368
|
+
].filter(Boolean)
|
|
1369
|
+
throw new Error(
|
|
1370
|
+
`Agent '${type}' would be spawned with zero tools — refusing. Its tools list resolved to nothing: ` +
|
|
1371
|
+
`${reasons.join("; ")}. Fix the agent's tools frontmatter or pass a different subagent_type.`,
|
|
1372
|
+
)
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1237
1375
|
// ─── Tool scoping ───────────────────────────────────────────────────────
|
|
1238
1376
|
//
|
|
1239
1377
|
// Some extensions register their tools ASYNCHRONOUSLY, long after the
|
|
@@ -1310,12 +1448,14 @@ export async function runAgent(
|
|
|
1310
1448
|
// as `outputTranscript`.
|
|
1311
1449
|
const persistSession =
|
|
1312
1450
|
agentConfig?.persistSession ?? (options.nested ? false : rememberAgents)
|
|
1451
|
+
const parentSessionFile = ctx.sessionManager?.getSessionFile?.()
|
|
1313
1452
|
const sessionManager = options.resumeSessionFile
|
|
1314
1453
|
? // Reopening an existing conversation: the file already carries its own
|
|
1315
1454
|
// header (cwd, parent) and history, so none of the create-time options
|
|
1316
1455
|
// apply. `sessionDir` still matters for a later /new or /branch off it.
|
|
1317
|
-
|
|
1456
|
+
reopenSession(
|
|
1318
1457
|
options.resumeSessionFile,
|
|
1458
|
+
parentSessionFile,
|
|
1319
1459
|
configuredSessionDir ?? defaultSessionDir,
|
|
1320
1460
|
)
|
|
1321
1461
|
: persistSession
|
|
@@ -1328,7 +1468,7 @@ export async function runAgent(
|
|
|
1328
1468
|
// `persist_session: true` agent; now it runs for every spawn, so a
|
|
1329
1469
|
// context without a session manager (a bare programmatic ctx) must
|
|
1330
1470
|
// still persist rather than take the whole spawn down.
|
|
1331
|
-
parentSession:
|
|
1471
|
+
parentSession: parentSessionFile,
|
|
1332
1472
|
},
|
|
1333
1473
|
)
|
|
1334
1474
|
: SessionManager.inMemory(effectiveCwd)
|
|
@@ -1406,6 +1546,7 @@ export async function runAgent(
|
|
|
1406
1546
|
disallowedSet,
|
|
1407
1547
|
extNames,
|
|
1408
1548
|
narrowing,
|
|
1549
|
+
mcpToolPatterns: isolated ? undefined : agentConfig?.mcpToolPatterns,
|
|
1409
1550
|
readmitToolNames,
|
|
1410
1551
|
nestedCallGate,
|
|
1411
1552
|
})
|
|
@@ -1421,9 +1562,11 @@ export async function runAgent(
|
|
|
1421
1562
|
const collector = collectResponseText(session)
|
|
1422
1563
|
const cleanupAbort = forwardAbortSignal(session, options.signal)
|
|
1423
1564
|
|
|
1424
|
-
// Build the effective prompt: optionally prepend parent context
|
|
1565
|
+
// Build the effective prompt: optionally prepend parent context. Only for a
|
|
1566
|
+
// fresh conversation — a reopened one already holds its history, and a live
|
|
1567
|
+
// resume never re-sends the parent's either.
|
|
1425
1568
|
let effectivePrompt = prompt
|
|
1426
|
-
if (options.inheritContext) {
|
|
1569
|
+
if (options.inheritContext && !options.resumeSessionFile) {
|
|
1427
1570
|
const parentContext = buildParentContext(ctx)
|
|
1428
1571
|
if (parentContext) {
|
|
1429
1572
|
effectivePrompt = parentContext + prompt
|