@herbertgao/pi-extensions 2026.10.7 → 2026.10.9
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 +4 -5
- package/THIRD_PARTY_NOTICES.md +2 -28
- package/node_modules/@herbertgao/pi-cc-extensions/README.en.md +28 -27
- package/node_modules/@herbertgao/pi-cc-extensions/README.md +6 -6
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/config.ts +16 -27
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +49 -48
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/compact-thinking.ts +2 -3
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/context.ts +0 -60
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/reference/index.ts +2 -3
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/footer-layout.ts +105 -23
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/footer.ts +59 -43
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +96 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/index.ts +6 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/compact-mode.ts +36 -34
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/default-mode.ts +7 -4
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/mouse/interaction.ts +3 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/mouse/scroll.ts +23 -8
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/mouse/sticky-prompt.ts +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/ccstyle-tool.ts +24 -12
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/codemode.ts +121 -6
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/diff/diff-limits.ts +3 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/diff/diff-palette.ts +39 -8
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/diff/diff-presentation.ts +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/grouping.ts +13 -13
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/message-display.ts +3 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/names.ts +49 -49
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/result.ts +24 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/tool-state.ts +51 -2
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +4 -4
- package/node_modules/@herbertgao/pi-cc-extensions/themes/cc-dark.json +71 -70
- package/node_modules/@herbertgao/pi-cc-extensions/themes/cc-light.json +71 -70
- package/node_modules/@tifan/pi-inline-skills/package.json +5 -1
- package/node_modules/@tifan/pi-inline-skills/src/index.ts +5 -2
- package/node_modules/pi-provider-qoder/dist/index.js +136 -31
- package/node_modules/pi-provider-qoder/package.json +1 -1
- package/node_modules/pi-typesafe/README.md +3 -3
- package/node_modules/pi-typesafe/dist/backends.d.ts +7 -2
- package/node_modules/pi-typesafe/dist/backends.js +42 -0
- package/node_modules/pi-typesafe/dist/batch.d.ts +10 -6
- package/node_modules/pi-typesafe/dist/batch.js +20 -9
- package/node_modules/pi-typesafe/dist/client.d.ts +4 -1
- package/node_modules/pi-typesafe/dist/client.js +5 -4
- package/node_modules/pi-typesafe/dist/index.d.ts +2 -2
- package/node_modules/pi-typesafe/dist/index.js +1 -1
- package/node_modules/pi-typesafe/dist/schema.d.ts +18 -3
- package/node_modules/pi-typesafe/dist/schema.js +29 -3
- package/node_modules/pi-typesafe/package.json +1 -1
- package/node_modules/pi-web-access/CHANGELOG.md +16 -0
- package/node_modules/pi-web-access/README.md +5 -2
- package/node_modules/pi-web-access/dist/index.js +13 -2
- package/node_modules/pi-web-access/dist/mcp-cli.js +14 -3
- package/node_modules/pi-web-access/extract.ts +4 -2
- package/node_modules/pi-web-access/package.json +1 -1
- package/node_modules/pi-web-access/searxng.ts +2 -1
- package/node_modules/pi-web-access/ssrf-protection.ts +11 -0
- package/package.json +6 -11
- package/node_modules/pi-multi-account/CHANGELOG.md +0 -1264
- package/node_modules/pi-multi-account/CONTRIBUTING.md +0 -61
- package/node_modules/pi-multi-account/LICENSE +0 -21
- package/node_modules/pi-multi-account/README.md +0 -277
- package/node_modules/pi-multi-account/SECURITY.md +0 -27
- package/node_modules/pi-multi-account/auth-file-transaction.ts +0 -56
- package/node_modules/pi-multi-account/child-usability.ts +0 -233
- package/node_modules/pi-multi-account/compaction-summary.ts +0 -32
- package/node_modules/pi-multi-account/completion-route-planner.ts +0 -224
- package/node_modules/pi-multi-account/config-file-transaction.ts +0 -42
- package/node_modules/pi-multi-account/context-guard.ts +0 -420
- package/node_modules/pi-multi-account/cursor/LICENSE +0 -21
- package/node_modules/pi-multi-account/cursor/NOTICE +0 -2
- package/node_modules/pi-multi-account/cursor/auth.ts +0 -165
- package/node_modules/pi-multi-account/cursor/bridge-handle.ts +0 -155
- package/node_modules/pi-multi-account/cursor/conversation-registry.ts +0 -104
- package/node_modules/pi-multi-account/cursor/cursor-models-raw.json +0 -611
- package/node_modules/pi-multi-account/cursor/cursor-shared.ts +0 -192
- package/node_modules/pi-multi-account/cursor/h2-bridge.mjs +0 -175
- package/node_modules/pi-multi-account/cursor/index.ts +0 -572
- package/node_modules/pi-multi-account/cursor/message-parsing.ts +0 -323
- package/node_modules/pi-multi-account/cursor/prompt-usage.ts +0 -53
- package/node_modules/pi-multi-account/cursor/proto/agent_pb.ts +0 -15294
- package/node_modules/pi-multi-account/cursor/proxy.ts +0 -2510
- package/node_modules/pi-multi-account/cursor/session-lifecycle.ts +0 -40
- package/node_modules/pi-multi-account/cursor/sse-keepalive.ts +0 -24
- package/node_modules/pi-multi-account/cursor/stream-lifecycle.ts +0 -193
- package/node_modules/pi-multi-account/cursor/upstream-watchdog.ts +0 -88
- package/node_modules/pi-multi-account/cursor-bridge.ts +0 -240
- package/node_modules/pi-multi-account/cursor-model-name.ts +0 -12
- package/node_modules/pi-multi-account/index.ts +0 -12340
- package/node_modules/pi-multi-account/model-catalog.ts +0 -354
- package/node_modules/pi-multi-account/model-pins.ts +0 -36
- package/node_modules/pi-multi-account/package.json +0 -104
- package/node_modules/pi-multi-account/pi-contract.ts +0 -281
- package/node_modules/pi-multi-account/provider-payload-stream.ts +0 -52
- package/node_modules/pi-multi-account/provider-priority.ts +0 -189
- package/node_modules/pi-multi-account/slot-proxy-auth.ts +0 -177
- package/node_modules/pi-multi-account/slot-proxy.ts +0 -366
- package/node_modules/pi-multi-account/state-file-transaction.ts +0 -67
- package/node_modules/pi-multi-account/usage.ts +0 -1128
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Can an extension-free Pi child actually use this rotation slot?
|
|
3
|
-
*
|
|
4
|
-
* ## Why this exists
|
|
5
|
-
*
|
|
6
|
-
* Pi keeps an optional saved global default in `settings.json`. On Pi <=0.84.2 every model
|
|
7
|
-
* switch rewrites it; since Pi 0.84.3 ordinary model selection is session-scoped and only an
|
|
8
|
-
* explicit persistent choice rewrites it. Anything that later spawns a bare
|
|
9
|
-
* `pi -p --no-extensions` child without `--model` inherits that saved default — which may
|
|
10
|
-
* intentionally differ from whichever rotation slot is active in this session. Brokered and
|
|
11
|
-
* subagent children pass an explicit provider/model and do not use this fallback.
|
|
12
|
-
*
|
|
13
|
-
* A child launched that way does not load this extension, so a slot named `openai-codex-account-4`
|
|
14
|
-
* exists for it only if we published it into Pi's own `models.json`. Publishing the *name*,
|
|
15
|
-
* however, is not the same as publishing a usable route, and the difference is invisible until
|
|
16
|
-
* something fails far away from here.
|
|
17
|
-
*
|
|
18
|
-
* ## What was measured (2026-08-24), not assumed
|
|
19
|
-
*
|
|
20
|
-
* In an isolated agent directory containing one `models.json` provider entry mirroring the
|
|
21
|
-
* built-in Codex definition plus the matching **OAuth** credential under the same key:
|
|
22
|
-
*
|
|
23
|
-
* pi -p --no-extensions --no-session --model openai-codex-account-4/gpt-5.6-sol …
|
|
24
|
-
* → exit 1: "No API key found for openai-codex-account-4."
|
|
25
|
-
*
|
|
26
|
-
* The same child, pointed at the **built-in** `openai-codex` provider with its OAuth credential,
|
|
27
|
-
* got past authentication (it reached the network instead of refusing).
|
|
28
|
-
*
|
|
29
|
-
* The reason is in Pi: `checkProviderAuth` honours an OAuth credential only when the *provider
|
|
30
|
-
* definition* declares an OAuth flow. A `models.json` entry declares none, so an OAuth token
|
|
31
|
-
* sitting in `auth.json` under exactly that key is never consulted. An API key is different —
|
|
32
|
-
* it resolves through the credential store and works.
|
|
33
|
-
*
|
|
34
|
-
* ## What was measured again (2026-08-30), and what it corrected
|
|
35
|
-
*
|
|
36
|
-
* The row below claiming a built-in provider is usable whatever its credential turned out to be
|
|
37
|
-
* true of authentication and false of the request. On the account the parent session was itself
|
|
38
|
-
* using successfully at that moment:
|
|
39
|
-
*
|
|
40
|
-
* pi -p --no-extensions --no-session --model anthropic/claude-opus-5 …
|
|
41
|
-
* → 400 invalid_request_error: "Third-party apps now draw from your extra usage, not your
|
|
42
|
-
* plan limits."
|
|
43
|
-
*
|
|
44
|
-
* Pi resolved the built-in provider and its OAuth credential correctly; Anthropic then refused,
|
|
45
|
-
* because a subscription token is only honoured for a request carrying the client identity the
|
|
46
|
-
* parent adds and a bare child does not. So for that family being built-in is not sufficient,
|
|
47
|
-
* and the same parent-owned loopback route the alias slots use is what makes it usable. Codex
|
|
48
|
-
* is unaffected — the 2026-08-24 measurement of a bare child on built-in `openai-codex` reached
|
|
49
|
-
* the network and was served.
|
|
50
|
-
*
|
|
51
|
-
* | slot shape | child outcome |
|
|
52
|
-
* |---|---|
|
|
53
|
-
* | built-in provider, credential the vendor serves any client (API key, Codex OAuth) | usable — Pi owns the auth flow |
|
|
54
|
-
* | **built-in Anthropic on a subscription OAuth token** | **authenticates, then refused as a third-party app** |
|
|
55
|
-
* | alias slot published with a real or placeholder `apiKey` | usable — Pi sees a credential |
|
|
56
|
-
* | **alias slot with an OAuth credential and no `apiKey`** | **resolves by name, then fails at auth** |
|
|
57
|
-
*
|
|
58
|
-
* The third row is what we do today for Kimi slots, under a comment promising that
|
|
59
|
-
* "extension-free children resolve it". True of the name; false of the credential. The Cursor
|
|
60
|
-
* slots avoid it by publishing a non-secret placeholder that points at a parent-owned local
|
|
61
|
-
* proxy, so the child authenticates to `127.0.0.1` while the real token stays in the parent.
|
|
62
|
-
*
|
|
63
|
-
* This module is deliberately pure: it decides and explains, and it touches no file, no socket
|
|
64
|
-
* and no credential. Wiring and the proxy itself are separate, reviewable pieces — this one can
|
|
65
|
-
* be tested exhaustively without any of them.
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
/** How a slot's credential is stored, as far as `auth.json` is concerned. */
|
|
69
|
-
export type SlotCredentialKind = "oauth" | "api_key" | "none";
|
|
70
|
-
|
|
71
|
-
export interface SlotChildFacts {
|
|
72
|
-
/** Rotation slot id, e.g. `openai-codex-account-4` or a base provider name. */
|
|
73
|
-
slotId: string;
|
|
74
|
-
/** Credential kind held for this exact slot id. */
|
|
75
|
-
credential: SlotCredentialKind;
|
|
76
|
-
/** True when Pi itself defines this provider (it then owns the OAuth flow). */
|
|
77
|
-
builtin: boolean;
|
|
78
|
-
/** `apiKey` published for this slot in `models.json`, when we published one. */
|
|
79
|
-
publishedApiKey?: string;
|
|
80
|
-
/** `baseUrl` published for this slot in `models.json`, when we published one. */
|
|
81
|
-
publishedBaseUrl?: string;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export type ChildUsability =
|
|
85
|
-
/** A bare child can authenticate and run on this slot. */
|
|
86
|
-
| { usable: true; slotId: string; via: "builtin" | "api-key" | "parent-proxy"; note: string }
|
|
87
|
-
/** A bare child cannot use it; `remedy` says what would change that. */
|
|
88
|
-
| { usable: false; slotId: string; reason: string; remedy: string };
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Families whose subscription refuses a request that does not carry the parent's client
|
|
92
|
-
* identity, even when Pi resolved the credential. Being built-in does not help here: the
|
|
93
|
-
* failure is the vendor's answer, not Pi's auth lookup.
|
|
94
|
-
*/
|
|
95
|
-
function vendorRefusesBareChild(
|
|
96
|
-
slotId: string,
|
|
97
|
-
credential: SlotCredentialKind,
|
|
98
|
-
builtin: boolean,
|
|
99
|
-
): boolean {
|
|
100
|
-
// Numbered alias slots never get this far: Pi does not consult their OAuth blob at all.
|
|
101
|
-
// The 400 is specific to a built-in Anthropic provider, which does resolve the credential.
|
|
102
|
-
return builtin && credential === "oauth" && /^anthropic(?:-account-\d+)?$/.test(slotId);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Loopback-only, so a published route can never send a child off this machine. */
|
|
106
|
-
function isLoopback(baseUrl?: string): boolean {
|
|
107
|
-
if (!baseUrl) return false;
|
|
108
|
-
try {
|
|
109
|
-
// `URL` reports an IPv6 host bracketed (`[::1]`), so compare against the unwrapped form.
|
|
110
|
-
const hostname = new URL(baseUrl).hostname.replace(/^\[|\]$/g, "");
|
|
111
|
-
return hostname === "127.0.0.1" || hostname === "localhost" || hostname === "::1";
|
|
112
|
-
} catch {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Decide, for one slot, whether an extension-free child can run on it.
|
|
119
|
-
*
|
|
120
|
-
* Order matters: Pi's resolver keys off the stored credential *type* before it ever looks at a
|
|
121
|
-
* models.json apiKey. An OAuth blob under the same key as a published placeholder is therefore
|
|
122
|
-
* not a usable child route — measured against Pi's own `resolveProviderAuth`: stored OAuth plus a
|
|
123
|
-
* provider that has no OAuth method returns undefined (\"No API key found\"), and stored OAuth
|
|
124
|
-
* plus a built-in OAuth method ignores the placeholder entirely.
|
|
125
|
-
*/
|
|
126
|
-
export function classifyChildUsability(facts: SlotChildFacts): ChildUsability {
|
|
127
|
-
const { slotId, credential, builtin, publishedApiKey, publishedBaseUrl } = facts;
|
|
128
|
-
|
|
129
|
-
const vendorRefuses = vendorRefusesBareChild(slotId, credential, builtin);
|
|
130
|
-
|
|
131
|
-
// A built-in provider is resolved by Pi without us — but resolution is not service. When the
|
|
132
|
-
// vendor refuses a bare client, a loopback baseUrl is what actually redirects the request;
|
|
133
|
-
// the published apiKey is not what Pi sends, because the stored credential is still OAuth.
|
|
134
|
-
if (builtin && !vendorRefuses) {
|
|
135
|
-
return {
|
|
136
|
-
usable: true,
|
|
137
|
-
slotId,
|
|
138
|
-
via: "builtin",
|
|
139
|
-
note: "Pi defines this provider itself and owns its auth flow, so a bare child resolves it without this extension.",
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (vendorRefuses && isLoopback(publishedBaseUrl)) {
|
|
144
|
-
return {
|
|
145
|
-
usable: true,
|
|
146
|
-
slotId,
|
|
147
|
-
via: "parent-proxy",
|
|
148
|
-
note: "The child still presents the subscription token; the published loopback is what makes the parent shape that request instead of the vendor refusing it as a third-party app.",
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (vendorRefuses) {
|
|
153
|
-
return {
|
|
154
|
-
usable: false,
|
|
155
|
-
slotId,
|
|
156
|
-
reason:
|
|
157
|
-
"The credential is a subscription OAuth token. Pi resolves it, but the vendor then refuses the call as a third-party app (\"Third-party apps now draw from your extra usage, not your plan limits\"), because a bare child does not send the client identity the parent adds.",
|
|
158
|
-
remedy:
|
|
159
|
-
"Publish this account against a parent-owned loopback proxy so the child's request is shaped by the parent that holds the subscription.",
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (credential === "api_key") {
|
|
164
|
-
if (publishedApiKey && isLoopback(publishedBaseUrl)) {
|
|
165
|
-
return {
|
|
166
|
-
usable: true,
|
|
167
|
-
slotId,
|
|
168
|
-
via: "parent-proxy",
|
|
169
|
-
note: "The child authenticates to a parent-owned loopback route with a non-secret placeholder; the real credential never leaves the parent.",
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
return {
|
|
173
|
-
usable: true,
|
|
174
|
-
slotId,
|
|
175
|
-
via: "api-key",
|
|
176
|
-
note: "The credential is an API key, which Pi resolves through its own credential store for a published provider.",
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (credential === "oauth") {
|
|
181
|
-
return {
|
|
182
|
-
usable: false,
|
|
183
|
-
slotId,
|
|
184
|
-
reason:
|
|
185
|
-
"The credential is OAuth, and Pi honours OAuth only for a provider definition that declares the flow. A models.json entry declares none, so the token under this key is never consulted — even a published placeholder is ignored, and the slot then fails with \"No API key found\".",
|
|
186
|
-
remedy:
|
|
187
|
-
"While the parent proxy is listening, present this slot to a child as a non-secret api_key placeholder and keep the OAuth blob where only the parent reads it.",
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
usable: false,
|
|
193
|
-
slotId,
|
|
194
|
-
reason: "No credential is held for this slot.",
|
|
195
|
-
remedy: "Log in to this account, or drop the slot from the published registry.",
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* The slot a bare unpinned child would actually be sent to, given Pi's saved global default.
|
|
201
|
-
* Returns `undefined` when that saved route is usable.
|
|
202
|
-
*
|
|
203
|
-
* This is the check that turns a silent, far-away failure into something sayable here: when the
|
|
204
|
-
* active rotation slot is not child-usable, every child spawned without an explicit `--model`
|
|
205
|
-
* falls through Pi's own "first available provider" list instead — which is a different account,
|
|
206
|
-
* often a different vendor, and never the one the rotation chose.
|
|
207
|
-
*/
|
|
208
|
-
export function defaultRouteWarning(
|
|
209
|
-
savedDefaultSlotId: string | undefined,
|
|
210
|
-
classify: (slotId: string) => ChildUsability | undefined,
|
|
211
|
-
): string | undefined {
|
|
212
|
-
if (!savedDefaultSlotId) return undefined;
|
|
213
|
-
const verdict = classify(savedDefaultSlotId);
|
|
214
|
-
if (!verdict || verdict.usable) return undefined;
|
|
215
|
-
return (
|
|
216
|
-
`Pi's saved global default is ${savedDefaultSlotId}, but a bare extension-free child launched ` +
|
|
217
|
-
`without --model cannot use it: ${verdict.reason} Such an unpinned child falls back to Pi's ` +
|
|
218
|
-
`own first-available provider. The live multi-account session and explicitly pinned ` +
|
|
219
|
-
`broker/subagent children are unaffected. ${verdict.remedy}`
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** One line per slot, for `/multi-account status`. Stable order: unusable first, then by id. */
|
|
224
|
-
export function describeChildUsability(verdicts: readonly ChildUsability[]): string[] {
|
|
225
|
-
const ordered = [...verdicts].sort(
|
|
226
|
-
(a, b) => Number(a.usable) - Number(b.usable) || a.slotId.localeCompare(b.slotId),
|
|
227
|
-
);
|
|
228
|
-
return ordered.map((verdict) =>
|
|
229
|
-
verdict.usable
|
|
230
|
-
? ` ${verdict.slotId} — usable by a bare child (${verdict.via})`
|
|
231
|
-
: ` ${verdict.slotId} — NOT usable by a bare child: ${verdict.reason}`,
|
|
232
|
-
);
|
|
233
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Guard against Pi throwing away an accumulated compaction summary.
|
|
3
|
-
*
|
|
4
|
-
* Pi's `compact()` has a branch for "the cut fell inside a turn": it summarizes the history
|
|
5
|
-
* range, summarizes the turn prefix, and glues them together. When the history range is empty —
|
|
6
|
-
* which is exactly what happens when a second compaction fires minutes after the first — that
|
|
7
|
-
* branch substitutes the literal string "No prior history." and never passes `previousSummary`
|
|
8
|
-
* to the model at all. The new compaction entry then replaces the accumulated summary with a
|
|
9
|
-
* note about one truncated turn, and everything the session had learned is gone.
|
|
10
|
-
*
|
|
11
|
-
* Seen in a real session: an 11 130-char summary became 2 032 chars beginning "No prior history."
|
|
12
|
-
* and describing work from a different day.
|
|
13
|
-
*
|
|
14
|
-
* We intercept compaction anyway (to route it to a healthy account), so the repair goes here:
|
|
15
|
-
* put the previous summary back where the placeholder is, keeping the turn-prefix context Pi
|
|
16
|
-
* did manage to produce.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/** The exact placeholder Pi writes when it has no history left to summarize. */
|
|
20
|
-
const NO_HISTORY_PLACEHOLDER = "No prior history.";
|
|
21
|
-
|
|
22
|
-
export function restorePreviousSummary(summary: string, previousSummary?: string): string {
|
|
23
|
-
if (!summary.startsWith(NO_HISTORY_PLACEHOLDER)) return summary;
|
|
24
|
-
const previous = previousSummary?.trim();
|
|
25
|
-
if (!previous) return summary;
|
|
26
|
-
return `${previous}${summary.slice(NO_HISTORY_PLACEHOLDER.length)}`;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** True when Pi produced a summary that dropped an existing accumulated summary. */
|
|
30
|
-
export function droppedPreviousSummary(summary: string, previousSummary?: string): boolean {
|
|
31
|
-
return summary.startsWith(NO_HISTORY_PLACEHOLDER) && !!previousSummary?.trim();
|
|
32
|
-
}
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { comparePriority } from "./provider-priority.ts";
|
|
2
|
-
|
|
3
|
-
export interface RouteModelRef {
|
|
4
|
-
provider: string;
|
|
5
|
-
id: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface RouteAttemptFact {
|
|
9
|
-
operationId: string;
|
|
10
|
-
purpose: string;
|
|
11
|
-
attempt: number;
|
|
12
|
-
model: RouteModelRef;
|
|
13
|
-
dispatched: boolean;
|
|
14
|
-
stopReason: string;
|
|
15
|
-
errorMessage?: string;
|
|
16
|
-
response?: {
|
|
17
|
-
status: number;
|
|
18
|
-
retryAfterMs?: number;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface RouteRequestFact {
|
|
23
|
-
operationId: string;
|
|
24
|
-
purpose: string;
|
|
25
|
-
attempt: number;
|
|
26
|
-
maxAttempts: number;
|
|
27
|
-
deadlineAt: number;
|
|
28
|
-
sessionModel?: RouteModelRef;
|
|
29
|
-
preferredModels: readonly RouteModelRef[];
|
|
30
|
-
attempts: readonly RouteAttemptFact[];
|
|
31
|
-
previous?: RouteAttemptFact;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export type RouteDecision =
|
|
35
|
-
| { action: "route"; model: RouteModelRef; delayMs?: number }
|
|
36
|
-
| { action: "stop"; reason?: string };
|
|
37
|
-
|
|
38
|
-
export interface RouteFailurePatterns {
|
|
39
|
-
ignore: readonly string[];
|
|
40
|
-
auth: readonly string[];
|
|
41
|
-
limit: readonly string[];
|
|
42
|
-
transient: readonly string[];
|
|
43
|
-
model: readonly string[];
|
|
44
|
-
contextOverflow: readonly string[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export type RouteFailureKind =
|
|
48
|
-
| "success"
|
|
49
|
-
| "aborted"
|
|
50
|
-
| "route_unavailable"
|
|
51
|
-
| "context_overflow"
|
|
52
|
-
| "auth"
|
|
53
|
-
| "limit"
|
|
54
|
-
| "model"
|
|
55
|
-
| "cursor_stall"
|
|
56
|
-
| "transient"
|
|
57
|
-
| "unhandled";
|
|
58
|
-
|
|
59
|
-
export interface ScoredRouteCandidate<T> {
|
|
60
|
-
model: T;
|
|
61
|
-
remaining: number;
|
|
62
|
-
rotIndex: number;
|
|
63
|
-
rank: number;
|
|
64
|
-
lastRefusalAt: number;
|
|
65
|
-
predictedBusy: boolean;
|
|
66
|
-
confirmed: boolean;
|
|
67
|
-
group: string;
|
|
68
|
-
sameFamily: boolean;
|
|
69
|
-
sameModel: boolean;
|
|
70
|
-
provider: string;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface RankAutomaticRouteOptions {
|
|
74
|
-
preferLatestModel: boolean;
|
|
75
|
-
preferSameIdentity: boolean;
|
|
76
|
-
providerPriority: readonly string[];
|
|
77
|
-
availableNowOnly: boolean;
|
|
78
|
-
lastLeftProvider?: string;
|
|
79
|
-
lastLeftAt?: number;
|
|
80
|
-
antiPingPongMs: number;
|
|
81
|
-
now: number;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function matches(text: string, patterns: readonly string[]): boolean {
|
|
85
|
-
const lower = text.toLowerCase();
|
|
86
|
-
return patterns.some((pattern) => pattern.length > 0 && lower.includes(pattern.toLowerCase()));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function sameRoute(left: RouteModelRef, right: RouteModelRef): boolean {
|
|
90
|
-
return left.provider === right.provider && left.id === right.id;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function routeKey(route: RouteModelRef): string {
|
|
94
|
-
return `${route.provider}/${route.id}`;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function isCursorStall(provider: string, text: string): boolean {
|
|
98
|
-
if (!provider.startsWith("cursor")) return false;
|
|
99
|
-
const lower = text.toLowerCase();
|
|
100
|
-
return (
|
|
101
|
-
(lower.includes("cursor run stalled") && lower.includes("timed out")) ||
|
|
102
|
-
(lower.includes("produced no output") && lower.includes("timed out"))
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function classifyRouteAttempt(
|
|
107
|
-
attempt: RouteAttemptFact,
|
|
108
|
-
patterns: RouteFailurePatterns,
|
|
109
|
-
): RouteFailureKind {
|
|
110
|
-
if (attempt.stopReason === "route_unavailable") return "route_unavailable";
|
|
111
|
-
if (attempt.stopReason === "aborted") return "aborted";
|
|
112
|
-
if (attempt.stopReason !== "error") return "success";
|
|
113
|
-
const text = attempt.errorMessage?.trim() ?? "";
|
|
114
|
-
if (matches(text, patterns.contextOverflow)) return "context_overflow";
|
|
115
|
-
if (text && matches(text, patterns.ignore)) return "unhandled";
|
|
116
|
-
if (attempt.response?.status === 401 || matches(text, patterns.auth)) return "auth";
|
|
117
|
-
if (
|
|
118
|
-
attempt.response?.status === 402 ||
|
|
119
|
-
attempt.response?.status === 403 ||
|
|
120
|
-
attempt.response?.status === 429 ||
|
|
121
|
-
matches(text, patterns.limit)
|
|
122
|
-
) {
|
|
123
|
-
return "limit";
|
|
124
|
-
}
|
|
125
|
-
if (matches(text, patterns.model)) return "model";
|
|
126
|
-
if (isCursorStall(attempt.model.provider, text)) return "cursor_stall";
|
|
127
|
-
if (attempt.response && attempt.response.status >= 500) return "transient";
|
|
128
|
-
if (matches(text, patterns.transient)) return "transient";
|
|
129
|
-
return "unhandled";
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function selectOperationRoute(options: {
|
|
133
|
-
request: RouteRequestFact;
|
|
134
|
-
orderedCandidates: readonly RouteModelRef[];
|
|
135
|
-
patterns: RouteFailurePatterns;
|
|
136
|
-
maxSameRouteTransientRetries: number;
|
|
137
|
-
transientRetryDelayMs: number;
|
|
138
|
-
}): RouteDecision {
|
|
139
|
-
const { request } = options;
|
|
140
|
-
const ordered = [...request.preferredModels, ...(request.sessionModel ? [request.sessionModel] : []), ...options.orderedCandidates];
|
|
141
|
-
const candidates = ordered.filter(
|
|
142
|
-
(candidate, index) => ordered.findIndex((other) => sameRoute(candidate, other)) === index,
|
|
143
|
-
);
|
|
144
|
-
const unavailable = new Set(
|
|
145
|
-
request.attempts
|
|
146
|
-
.filter((attempt) => attempt.stopReason === "route_unavailable")
|
|
147
|
-
.map((attempt) => routeKey(attempt.model)),
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
if (!request.previous) {
|
|
151
|
-
const initial = candidates.find((candidate) => !unavailable.has(routeKey(candidate)));
|
|
152
|
-
return initial
|
|
153
|
-
? { action: "route", model: initial }
|
|
154
|
-
: { action: "stop", reason: "No completion route is available" };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const failureKind = classifyRouteAttempt(request.previous, options.patterns);
|
|
158
|
-
if (failureKind === "success") {
|
|
159
|
-
return { action: "stop", reason: "Completion already succeeded" };
|
|
160
|
-
}
|
|
161
|
-
if (failureKind === "aborted") {
|
|
162
|
-
return { action: "stop", reason: "Completion was cancelled" };
|
|
163
|
-
}
|
|
164
|
-
if (failureKind === "context_overflow" || failureKind === "unhandled") {
|
|
165
|
-
return {
|
|
166
|
-
action: "stop",
|
|
167
|
-
reason: `Completion failure is not provider-route evidence (${failureKind})`,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (failureKind === "transient" || failureKind === "cursor_stall") {
|
|
172
|
-
const sameRouteTransientFailures = request.attempts.filter(
|
|
173
|
-
(attempt) =>
|
|
174
|
-
sameRoute(attempt.model, request.previous!.model) &&
|
|
175
|
-
["transient", "cursor_stall"].includes(classifyRouteAttempt(attempt, options.patterns)),
|
|
176
|
-
).length;
|
|
177
|
-
if (sameRouteTransientFailures <= options.maxSameRouteTransientRetries) {
|
|
178
|
-
return {
|
|
179
|
-
action: "route",
|
|
180
|
-
model: request.previous.model,
|
|
181
|
-
delayMs: request.previous.response?.retryAfterMs ?? options.transientRetryDelayMs,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const attempted = new Set(request.attempts.map((attempt) => routeKey(attempt.model)));
|
|
187
|
-
const fallback = candidates.find((candidate) => !attempted.has(routeKey(candidate)));
|
|
188
|
-
return fallback
|
|
189
|
-
? { action: "route", model: fallback }
|
|
190
|
-
: { action: "stop", reason: `No healthy fallback after ${failureKind}` };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export function rankAutomaticRouteCandidates<T>(
|
|
194
|
-
scored: readonly ScoredRouteCandidate<T>[],
|
|
195
|
-
options: RankAutomaticRouteOptions,
|
|
196
|
-
): T[] {
|
|
197
|
-
const byRankThenRotation = (left: ScoredRouteCandidate<T>, right: ScoredRouteCandidate<T>) =>
|
|
198
|
-
(options.preferLatestModel ? left.rank - right.rank : 0) || left.rotIndex - right.rotIndex;
|
|
199
|
-
const byPolicy = (left: ScoredRouteCandidate<T>, right: ScoredRouteCandidate<T>) =>
|
|
200
|
-
(options.preferSameIdentity
|
|
201
|
-
? Number(right.sameModel) - Number(left.sameModel) || Number(right.sameFamily) - Number(left.sameFamily)
|
|
202
|
-
: 0) ||
|
|
203
|
-
Number(right.confirmed) - Number(left.confirmed) ||
|
|
204
|
-
Number(left.predictedBusy) - Number(right.predictedBusy) ||
|
|
205
|
-
comparePriority(left.group, right.group, options.providerPriority) ||
|
|
206
|
-
left.lastRefusalAt - right.lastRefusalAt ||
|
|
207
|
-
byRankThenRotation(left, right);
|
|
208
|
-
|
|
209
|
-
let available = scored.filter((candidate) => candidate.remaining === 0).sort(byPolicy);
|
|
210
|
-
if (
|
|
211
|
-
options.lastLeftProvider &&
|
|
212
|
-
options.lastLeftAt !== undefined &&
|
|
213
|
-
options.now - options.lastLeftAt < options.antiPingPongMs &&
|
|
214
|
-
available.length > 1
|
|
215
|
-
) {
|
|
216
|
-
const alternatives = available.filter((candidate) => candidate.provider !== options.lastLeftProvider);
|
|
217
|
-
if (alternatives.length > 0) available = alternatives;
|
|
218
|
-
}
|
|
219
|
-
if (available.length > 0) return available.map((candidate) => candidate.model);
|
|
220
|
-
if (options.availableNowOnly) return [];
|
|
221
|
-
return [...scored]
|
|
222
|
-
.sort((left, right) => left.remaining - right.remaining || byPolicy(left, right))
|
|
223
|
-
.map((candidate) => candidate.model);
|
|
224
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/** Shared read-latest/field-scoped transaction for every extension config writer. */
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { createRequire } from "node:module";
|
|
4
|
-
import { readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
-
|
|
6
|
-
const require = createRequire(import.meta.url);
|
|
7
|
-
const pause = new Int32Array(new SharedArrayBuffer(4));
|
|
8
|
-
|
|
9
|
-
export function mutateConfigFile(
|
|
10
|
-
path: string,
|
|
11
|
-
patch: (disk: Record<string, any>) => Record<string, unknown>,
|
|
12
|
-
canCommit: () => boolean = () => true,
|
|
13
|
-
): Record<string, any> {
|
|
14
|
-
// Preserve config symlinks and make aliases coordinate on the same target lock.
|
|
15
|
-
const target = realpathSync(path);
|
|
16
|
-
const lockfile = require("proper-lockfile") as { lockSync(path: string, options: object): () => void };
|
|
17
|
-
let release: (() => void) | undefined;
|
|
18
|
-
const deadline = Date.now() + 250;
|
|
19
|
-
while (!release) {
|
|
20
|
-
if (!canCommit()) throw new Error("session changed before config persistence");
|
|
21
|
-
try { release = lockfile.lockSync(target, { realpath: false }); }
|
|
22
|
-
catch (error: any) {
|
|
23
|
-
if (error?.code !== "ELOCKED" || Date.now() >= deadline) throw error;
|
|
24
|
-
Atomics.wait(pause, 0, 0, 5);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
const temp = `${target}.${process.pid}.${randomUUID()}.tmp`;
|
|
28
|
-
try {
|
|
29
|
-
const disk = JSON.parse(readFileSync(target, "utf8"));
|
|
30
|
-
if (!disk || typeof disk !== "object" || Array.isArray(disk)) throw new Error("config must be an object");
|
|
31
|
-
const delta = patch(disk);
|
|
32
|
-
if (!delta || typeof delta !== "object" || Array.isArray(delta)) throw new Error("config patch must be an object");
|
|
33
|
-
const next = { ...disk, ...delta };
|
|
34
|
-
if (!canCommit()) throw new Error("session changed before config persistence");
|
|
35
|
-
writeFileSync(temp, `${JSON.stringify(next, null, "\t")}\n`, { mode: 0o600, flag: "wx" });
|
|
36
|
-
if (!canCommit()) throw new Error("session changed before config persistence");
|
|
37
|
-
renameSync(temp, target);
|
|
38
|
-
return next;
|
|
39
|
-
} finally {
|
|
40
|
-
try { rmSync(temp, { force: true }); } finally { release(); }
|
|
41
|
-
}
|
|
42
|
-
}
|