typeclaw 0.48.12 → 0.49.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.
Files changed (213) hide show
  1. package/package.json +6 -3
  2. package/scripts/dump-system-prompt.ts +52 -110
  3. package/src/agent/attention-escalation.ts +8 -5
  4. package/src/agent/auth-reloadable.ts +2 -3
  5. package/src/agent/auth.ts +59 -79
  6. package/src/agent/compaction.ts +2 -2
  7. package/src/agent/index.ts +173 -85
  8. package/src/agent/live-sessions.ts +2 -2
  9. package/src/agent/live-subagents.ts +1 -0
  10. package/src/agent/llm-replay-sanitizer.ts +1 -1
  11. package/src/agent/model-overrides.ts +1 -1
  12. package/src/agent/multimodal/look-at.ts +3 -3
  13. package/src/agent/multimodal/read-redirect.ts +1 -1
  14. package/src/agent/plugin-tools.ts +76 -39
  15. package/src/agent/prompt-cache-key.ts +42 -0
  16. package/src/agent/provider-error.ts +179 -10
  17. package/src/agent/reload-tool.ts +1 -1
  18. package/src/agent/restart-handoff/index.ts +7 -14
  19. package/src/agent/reviewer-activity.ts +68 -0
  20. package/src/agent/session-origin.ts +33 -0
  21. package/src/agent/subagents.ts +14 -2
  22. package/src/agent/system-prompt.ts +68 -202
  23. package/src/agent/todo/continuation-policy.ts +12 -4
  24. package/src/agent/tool-error-remediation.ts +1 -1
  25. package/src/agent/tool-file-safety.ts +113 -14
  26. package/src/agent/tool-not-found-nudge.ts +1 -1
  27. package/src/agent/tool-result-budget.ts +2 -2
  28. package/src/agent/tools/channel-disengage.ts +2 -2
  29. package/src/agent/tools/channel-edit.ts +2 -2
  30. package/src/agent/tools/channel-fetch-attachment.ts +2 -2
  31. package/src/agent/tools/channel-history-render.ts +2 -1
  32. package/src/agent/tools/channel-history.ts +2 -2
  33. package/src/agent/tools/channel-react.ts +2 -2
  34. package/src/agent/tools/channel-read.ts +2 -2
  35. package/src/agent/tools/channel-reply.ts +25 -7
  36. package/src/agent/tools/channel-send.ts +51 -9
  37. package/src/agent/tools/curl-impersonate.ts +11 -2
  38. package/src/agent/tools/grant-role.ts +2 -2
  39. package/src/agent/tools/post-github-review.ts +79 -17
  40. package/src/agent/tools/restart.ts +2 -2
  41. package/src/agent/tools/skip-response.ts +4 -4
  42. package/src/agent/tools/spawn-subagent.ts +165 -14
  43. package/src/agent/tools/stream-snapshot.ts +2 -2
  44. package/src/agent/tools/subagent-cancel.ts +2 -2
  45. package/src/agent/tools/subagent-output.ts +10 -2
  46. package/src/agent/tools/todo/index.ts +2 -2
  47. package/src/agent/tools/webfetch/tool.ts +2 -2
  48. package/src/agent/tools/websearch.ts +2 -2
  49. package/src/bundled-plugins/backup/README.md +10 -9
  50. package/src/bundled-plugins/backup/runner.ts +140 -58
  51. package/src/bundled-plugins/backup/subagents.ts +58 -26
  52. package/src/bundled-plugins/github-cli-auth/gh-command.ts +34 -2
  53. package/src/bundled-plugins/github-cli-auth/granted-push.ts +135 -0
  54. package/src/bundled-plugins/github-cli-auth/index.ts +83 -11
  55. package/src/bundled-plugins/github-cli-auth/review-account.ts +23 -0
  56. package/src/bundled-plugins/github-cli-auth/review-checkout.ts +9 -2
  57. package/src/bundled-plugins/github-cli-auth/review-recorder.ts +45 -7
  58. package/src/bundled-plugins/operator/operator.ts +15 -4
  59. package/src/bundled-plugins/reviewer/skills/code-review.ts +1 -1
  60. package/src/bundled-plugins/security/index.ts +50 -2
  61. package/src/bundled-plugins/security/policies/git-exfil.ts +24 -12
  62. package/src/channels/adapters/discord-bot-reactions.ts +18 -0
  63. package/src/channels/adapters/discord-bot.ts +122 -67
  64. package/src/channels/adapters/discord-reactions.ts +18 -0
  65. package/src/channels/adapters/discord.ts +114 -8
  66. package/src/channels/adapters/github/inbound.ts +103 -8
  67. package/src/channels/adapters/github/index.ts +251 -19
  68. package/src/channels/adapters/github/last-alive.ts +27 -0
  69. package/src/channels/adapters/github/outbound.ts +54 -18
  70. package/src/channels/adapters/github/pending-review-store.ts +141 -0
  71. package/src/channels/adapters/github/permission-guidance.ts +1 -0
  72. package/src/channels/adapters/github/reactions.ts +100 -0
  73. package/src/channels/adapters/github/reconcile-cooldown-store.ts +197 -67
  74. package/src/channels/adapters/github/reconcile-open-prs.ts +418 -33
  75. package/src/channels/adapters/github/recovery.ts +106 -0
  76. package/src/channels/adapters/github/review-check.ts +510 -0
  77. package/src/channels/adapters/github/review-submitter.ts +34 -2
  78. package/src/channels/adapters/instagram.ts +32 -14
  79. package/src/channels/adapters/kakaotalk.ts +22 -7
  80. package/src/channels/adapters/line.ts +18 -3
  81. package/src/channels/adapters/outbound-account.ts +20 -0
  82. package/src/channels/adapters/own-reactions.ts +66 -0
  83. package/src/channels/adapters/recovery-correlation.ts +214 -0
  84. package/src/channels/adapters/slack-bot-classify.ts +18 -11
  85. package/src/channels/adapters/slack-bot-dedupe.ts +111 -28
  86. package/src/channels/adapters/slack-bot-reactions.ts +18 -0
  87. package/src/channels/adapters/slack-bot.ts +125 -57
  88. package/src/channels/adapters/slack-classify.ts +25 -4
  89. package/src/channels/adapters/slack-inbound-revision.ts +96 -0
  90. package/src/channels/adapters/slack-reactions.ts +18 -0
  91. package/src/channels/adapters/slack-thread-ts.ts +6 -0
  92. package/src/channels/adapters/slack.ts +48 -3
  93. package/src/channels/adapters/teams.ts +14 -4
  94. package/src/channels/adapters/telegram-bot.ts +56 -10
  95. package/src/channels/adapters/webex-bot.ts +12 -2
  96. package/src/channels/adapters/webex.ts +12 -2
  97. package/src/channels/background-handoff.ts +434 -0
  98. package/src/channels/background-obligations.ts +893 -0
  99. package/src/channels/continuity-types.ts +271 -0
  100. package/src/channels/github-review-check.ts +598 -0
  101. package/src/channels/github-review-turn-ledger.ts +102 -28
  102. package/src/channels/github-review-verdict-coordinator.ts +96 -13
  103. package/src/channels/github-token-bridge.ts +11 -5
  104. package/src/channels/inbound-journal.ts +1738 -0
  105. package/src/channels/index.ts +6 -1
  106. package/src/channels/manager.ts +17 -7
  107. package/src/channels/recovery-dispatcher.ts +264 -0
  108. package/src/channels/recovery-notice.ts +94 -0
  109. package/src/channels/recovery-outbox.ts +459 -0
  110. package/src/channels/router.ts +2582 -633
  111. package/src/channels/schema.ts +8 -1
  112. package/src/channels/subagent-completion-bridge.ts +17 -14
  113. package/src/channels/types.ts +121 -16
  114. package/src/channels/waiting-reactions.ts +664 -0
  115. package/src/cli/compose.ts +7 -4
  116. package/src/cli/hostd.ts +49 -3
  117. package/src/cli/inspect.ts +11 -14
  118. package/src/cli/model.ts +7 -5
  119. package/src/cli/oauth-callbacks.ts +38 -23
  120. package/src/coding-agent/broker-body.ts +38 -0
  121. package/src/coding-agent/broker.ts +290 -0
  122. package/src/coding-agent/claude.ts +95 -0
  123. package/src/coding-agent/codex.ts +104 -0
  124. package/src/coding-agent/index.ts +506 -0
  125. package/src/coding-agent/omo-state.ts +69 -0
  126. package/src/coding-agent/omo-stream.ts +98 -0
  127. package/src/coding-agent/omo.ts +192 -0
  128. package/src/coding-agent/runner.ts +263 -0
  129. package/src/coding-agent/tool.ts +62 -0
  130. package/src/coding-agent/worktree.ts +355 -0
  131. package/src/compose/restart.ts +10 -3
  132. package/src/compose/start.ts +20 -3
  133. package/src/config/config.ts +70 -26
  134. package/src/config/providers.ts +278 -50
  135. package/src/container/exit-reason.ts +44 -0
  136. package/src/container/index.ts +5 -0
  137. package/src/container/log-archive.ts +51 -13
  138. package/src/container/memory-limit.ts +1 -1
  139. package/src/container/memory-oversubscription.ts +36 -6
  140. package/src/container/oom-events.ts +86 -0
  141. package/src/container/restart.ts +2 -0
  142. package/src/container/shared.ts +8 -1
  143. package/src/container/start.ts +25 -13
  144. package/src/container/stop.ts +6 -2
  145. package/src/container/tool-oom-priority.ts +32 -0
  146. package/src/cron/consumer.ts +10 -4
  147. package/src/cron/reloadable.ts +23 -2
  148. package/src/doctor/checks.ts +14 -2
  149. package/src/hostd/daemon.ts +20 -10
  150. package/src/hostd/spawn.ts +106 -5
  151. package/src/hostd/version.ts +20 -0
  152. package/src/hostd/watchdog.ts +241 -0
  153. package/src/init/dockerfile.ts +57 -592
  154. package/src/init/index.ts +6 -7
  155. package/src/init/oauth-login.ts +57 -17
  156. package/src/inspect/transcript-view.ts +6 -3
  157. package/src/mcp/client.ts +3 -1
  158. package/src/operations/classify-tool-outcome.ts +45 -1
  159. package/src/operations/incidents.ts +10 -5
  160. package/src/plugin/core-tool-names.ts +1 -1
  161. package/src/plugin/define.ts +1 -0
  162. package/src/plugin/index.ts +1 -0
  163. package/src/plugin/types.ts +6 -1
  164. package/src/run/background-handoff-boot.ts +86 -0
  165. package/src/run/channel-session-factory.ts +5 -4
  166. package/src/run/index.ts +93 -93
  167. package/src/run/resource-sample.ts +19 -0
  168. package/src/sandbox/build.ts +4 -0
  169. package/src/sandbox/coding-agent.ts +214 -0
  170. package/src/sandbox/policy.ts +1 -0
  171. package/src/sandbox/runtime-private.ts +4 -1
  172. package/src/secrets/coding-agent-auth.ts +97 -0
  173. package/src/secrets/index.ts +0 -16
  174. package/src/secrets/refresh-provider-oauth.ts +30 -76
  175. package/src/secrets/schema.ts +3 -4
  176. package/src/secrets/storage.ts +133 -234
  177. package/src/server/command-runner.ts +6 -2
  178. package/src/server/index.ts +102 -41
  179. package/src/sessions/index.ts +1 -1
  180. package/src/skills/typeclaw-channel-github/SKILL.md +7 -5
  181. package/src/skills/typeclaw-claude-code/SKILL.md +25 -260
  182. package/src/skills/typeclaw-claude-code/references/auth-flow.md +7 -23
  183. package/src/skills/typeclaw-claude-code/references/headless-sessions.md +11 -0
  184. package/src/skills/typeclaw-claude-code/references/results-and-worktrees.md +13 -0
  185. package/src/skills/typeclaw-codex-cli/SKILL.md +35 -270
  186. package/src/skills/typeclaw-codex-cli/references/auth-flow.md +13 -20
  187. package/src/skills/typeclaw-codex-cli/references/headless-sessions.md +26 -0
  188. package/src/skills/typeclaw-codex-cli/references/results-and-worktrees.md +28 -0
  189. package/src/skills/typeclaw-config/SKILL.md +24 -24
  190. package/src/skills/typeclaw-config/references/dockerfile.md +14 -13
  191. package/src/skills/typeclaw-cron/SKILL.md +1 -1
  192. package/src/skills/typeclaw-github-contributing/SKILL.md +2 -2
  193. package/src/skills/typeclaw-memory/SKILL.md +3 -3
  194. package/src/skills/typeclaw-omo-cli/SKILL.md +52 -0
  195. package/src/skills/typeclaw-omo-cli/references/auth-flow.md +11 -0
  196. package/src/skills/typeclaw-omo-cli/references/headless-sessions.md +11 -0
  197. package/src/skills/typeclaw-omo-cli/references/results-and-worktrees.md +23 -0
  198. package/src/skills/typeclaw-plugins/SKILL.md +20 -20
  199. package/src/skills/typeclaw-skills/SKILL.md +19 -2
  200. package/src/skills/typeclaw-troubleshooting/SKILL.md +15 -2
  201. package/src/tui/index.ts +5 -3
  202. package/src/tui/theme.ts +1 -1
  203. package/typeclaw.schema.json +44 -2
  204. package/src/agent/adaptive-thinking-compat.ts +0 -79
  205. package/src/secrets/claude-credentials-json.ts +0 -129
  206. package/src/secrets/codex-auth-json.ts +0 -102
  207. package/src/secrets/export-claude-credentials-file.ts +0 -270
  208. package/src/secrets/export-codex-auth-file.ts +0 -250
  209. package/src/secrets/oauth-xai.ts +0 -348
  210. package/src/skills/typeclaw-claude-code/references/stop-hook.md +0 -101
  211. package/src/skills/typeclaw-claude-code/references/tmux-driving.md +0 -243
  212. package/src/skills/typeclaw-codex-cli/references/stop-hook.md +0 -92
  213. package/src/skills/typeclaw-codex-cli/references/tmux-driving.md +0 -239
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typeclaw",
3
- "version": "0.48.12",
3
+ "version": "0.49.0",
4
4
  "homepage": "https://github.com/typeclaw/typeclaw#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/typeclaw/typeclaw/issues"
@@ -45,9 +45,11 @@
45
45
  "dependencies": {
46
46
  "@clack/core": "^1.2.0",
47
47
  "@clack/prompts": "^1.2.0",
48
+ "@earendil-works/pi-agent-core": "^0.99.2",
49
+ "@earendil-works/pi-ai": "^0.99.2",
50
+ "@earendil-works/pi-coding-agent": "^0.99.2",
51
+ "@earendil-works/pi-tui": "^0.99.2",
48
52
  "@huggingface/transformers": "4.2.0",
49
- "@mariozechner/pi-coding-agent": "^0.73.1",
50
- "@mariozechner/pi-tui": "^0.73.1",
51
53
  "@modelcontextprotocol/sdk": "^1.29.0",
52
54
  "@mozilla/readability": "^0.6.0",
53
55
  "agent-messenger": "^2.37.2",
@@ -60,6 +62,7 @@
60
62
  "qrcode": "^1.5.4",
61
63
  "tough-cookie": "^6.0.2",
62
64
  "turndown": "^7.2.4",
65
+ "typebox": "1.3.27",
63
66
  "zod": "^4.3.6"
64
67
  },
65
68
  "devDependencies": {
@@ -1,9 +1,20 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
+ // Debug dump of the TypeClaw-composed system-prompt preamble for each standard
4
+ // session origin, with placeholder inputs. Every origin — including the
5
+ // default subagent — goes through the real `composeSystemPrompt`, so the
6
+ // sections and their order are exactly what `createResourceLoader` produces.
7
+ //
8
+ // This is the preamble only. pi appends project context (AGENTS.md), the skill
9
+ // catalog, and the cwd when it assembles the final system message; capture a
10
+ // real session's `session.systemPrompt` for that. Sessions created with an
11
+ // explicit `systemPromptOverride` do not use this composer and are not dumped.
12
+
3
13
  import { parseArgs } from 'node:util'
4
14
 
5
- import { composeSystemPrompt, deriveSystemPromptMode, renderTurnTimeAnchor, type SystemPromptMode } from '@/agent'
6
- import type { SessionOrigin, SessionRoleContext } from '@/agent/session-origin'
15
+ import { composeSystemPrompt, deriveSystemPromptMode, renderTurnTimeAnchor } from '@/agent'
16
+ import { renderInteractiveSessionContext, type SessionOrigin, type SessionRoleContext } from '@/agent/session-origin'
17
+ import { buildSystemPolicy, DEFAULT_SUBAGENT_ROSTER, renderRuntimeBlock } from '@/agent/system-prompt'
7
18
  import { renderRetrievedMemorySection, type RetrievedMemoryItem } from '@/bundled-plugins/memory/load-memory'
8
19
  import { composeTurnPrompt } from '@/channels/router'
9
20
 
@@ -178,10 +189,10 @@ export type DumpResult = {
178
189
  totalTokens: number
179
190
  }
180
191
 
181
- // Heuristic: ~4 chars per token. Industry rule-of-thumb (e.g. OpenAI tokenizer
182
- // docs); accurate to ~15% for English prose / markdown, model-agnostic across
183
- // Claude / GPT / Gemini families. Exposed so tests can assert the methodology.
184
- export const TOKENS_PER_CHAR = 0.25
192
+ // Rough size estimate: ~4 chars per token. A model-agnostic rule of thumb for
193
+ // English prose/markdown, NOT a tokenizer count — use a real tokenizer for any
194
+ // measurement you report.
195
+ const TOKENS_PER_CHAR = 0.25
185
196
 
186
197
  export function estimateTokens(text: string): number {
187
198
  return Math.round(text.length * TOKENS_PER_CHAR)
@@ -198,12 +209,6 @@ export function byteLength(text: string): number {
198
209
  return encoder.encode(text).length
199
210
  }
200
211
 
201
- const PLACEHOLDER_SUBAGENT_OVERRIDE = [
202
- 'You are typeclaw <PLACEHOLDER: subagent name>, a narrow worker subagent.',
203
- '',
204
- '<PLACEHOLDER: contents of the subagent-specific system prompt — owned by the plugin/bundled subagent that declared this worker. Real examples: memory-logger (~1000 tok), dreaming (~2200 tok). The prompt is opaque to the runtime; it teaches the subagent its job, its tools, and its termination contract.>',
205
- ].join('\n')
206
-
207
212
  const mkSection = (name: string, body: string): SectionBreakdown => ({
208
213
  name,
209
214
  bytes: byteLength(body),
@@ -211,87 +216,32 @@ const mkSection = (name: string, body: string): SectionBreakdown => ({
211
216
  tokens: estimateTokens(body),
212
217
  })
213
218
 
219
+ // Each section row runs from its marker to the next one, so it includes the
220
+ // `\n\n` separator that follows it and the rows sum exactly to the totals.
214
221
  export function dumpSystemPromptWithBreakdown(
215
222
  kind: OriginKind,
216
223
  options: { gitNudge: boolean } = { gitNudge: true },
217
224
  ): DumpResult {
218
- if (kind === 'subagent') return dumpSubagentOverridePrompt()
219
- return dumpDefaultLoaderPrompt(kind, options)
220
- }
221
-
222
- // Subagent sessions in production go through `defaultCreateSessionForSubagent`
223
- // (and the plugin-subagent path in run/index.ts), both of which set
224
- // `systemPromptOverride: subagent.systemPrompt`. That routes through
225
- // `createOverrideResourceLoader`, which emits only:
226
- // <override string> + runtime block + origin (with role)
227
- // No DEFAULT/SLIM base, no IDENTITY/SOUL, no git-nudge.
228
- //
229
- // Without this branch, the dumper would report a misleadingly large slim
230
- // breakdown for the subagent case and contradict AGENTS.md's "the section
231
- // order it prints is the section order an agent actually sees" contract.
232
- function dumpSubagentOverridePrompt(): DumpResult {
233
- const fixture = buildFixture('subagent')
234
- const runtimeBlock = `## Runtime\n\nTypeClaw runtime version: ${PLACEHOLDER_RUNTIME_VERSION}.`
235
- const originBlock = `## Session origin\n\nYou are a \`${(fixture.origin as { subagent: string }).subagent}\` subagent spawned by parent session\n\`${(fixture.origin as { parentSessionId: string }).parentSessionId}\`. Stay narrowly within the task you were given.\nReturn cleanly when done; do not sprawl into unrelated work.\n\n## Your role in this session\n\nRole: \`${fixture.roleContext.role}\`. Permissions: ${fixture.roleContext.permissions.map((p) => `\`${p}\``).join(', ')}.\n\nThis is the role the runtime resolved at session creation. Tool calls\nand channel admission are gated by these permissions; a \`blocked:\` or\n"denied by permissions" message means the current actor lacks the\npermission the guard was looking for. See the \`typeclaw-permissions\`\nskill for what each role can do and how to grant access.`
236
-
237
- const prompt = `${PLACEHOLDER_SUBAGENT_OVERRIDE}\n\n${runtimeBlock}\n\n${originBlock}`
238
- const sections: SectionBreakdown[] = [
239
- mkSection('Subagent override prompt', PLACEHOLDER_SUBAGENT_OVERRIDE),
240
- mkSection('Runtime block', runtimeBlock),
241
- mkSection('Session origin + role', originBlock),
242
- ]
243
- return {
244
- prompt,
245
- sections,
246
- totalBytes: byteLength(prompt),
247
- totalChars: prompt.length,
248
- totalTokens: estimateTokens(prompt),
249
- }
250
- }
251
-
252
- function dumpDefaultLoaderPrompt(kind: Exclude<OriginKind, 'subagent'>, options: { gitNudge: boolean }): DumpResult {
253
225
  const fixture = buildFixture(kind)
254
- const mode: SystemPromptMode = deriveSystemPromptMode(fixture.origin)
255
- const wantGitNudge = options.gitNudge && mode === 'full'
256
- const parts = {
226
+ const mode = deriveSystemPromptMode(fixture.origin)
227
+ const gitNudge = options.gitNudge && mode === 'full' ? PLACEHOLDER_GIT_NUDGE : ''
228
+ const prompt = composeSystemPrompt({
257
229
  mode,
258
230
  self: PLACEHOLDER_SELF,
259
231
  runtimeVersion: PLACEHOLDER_RUNTIME_VERSION,
260
232
  origin: fixture.origin,
261
233
  roleContext: fixture.roleContext,
262
- gitNudge: wantGitNudge ? PLACEHOLDER_GIT_NUDGE : '',
263
- } as const
264
-
265
- const prompt = composeSystemPrompt(parts)
266
-
267
- const baseEnd = prompt.indexOf(`\n\n${parts.self}`)
268
- const base = baseEnd > 0 ? prompt.slice(0, baseEnd) : ''
269
- const baseLabel = mode === 'slim' ? 'SLIM_SYSTEM_PROMPT (base)' : 'DEFAULT_SYSTEM_PROMPT (base)'
270
- const sections: SectionBreakdown[] = [
271
- mkSection(baseLabel, base),
272
- mkSection('Identity (IDENTITY.md + SOUL.md)', parts.self),
273
- mkSection('Runtime block', `## Runtime\n\nTypeClaw runtime version: ${parts.runtimeVersion}.`),
274
- mkSection('Session origin', extractSection(prompt, '## Session origin', '## Your role in this session')),
275
- mkSection(
276
- 'Role context',
277
- extractSection(
278
- prompt,
279
- '## Your role in this session',
280
- parts.gitNudge !== '' ? '## Uncommitted changes at session start' : undefined,
281
- ),
282
- ),
234
+ gitNudge,
235
+ })
236
+ const markers: Array<readonly [string, string]> = [
237
+ ['Shared policy', buildSystemPolicy()],
238
+ ['Identity (IDENTITY.md + SOUL.md)', PLACEHOLDER_SELF],
239
+ ['Runtime block', renderRuntimeBlock(PLACEHOLDER_RUNTIME_VERSION)],
283
240
  ]
284
- if (parts.gitNudge !== '') {
285
- sections.push(mkSection('Git nudge', parts.gitNudge))
286
- }
287
-
288
- return {
289
- prompt,
290
- sections,
291
- totalBytes: byteLength(prompt),
292
- totalChars: prompt.length,
293
- totalTokens: estimateTokens(prompt),
294
- }
241
+ if (mode === 'full') markers.push(['Interactive context', renderInteractiveSessionContext(DEFAULT_SUBAGENT_ROSTER)])
242
+ markers.push(['Session origin', '## Session origin\n'], ['Role context', '## Your role in this session\n'])
243
+ if (gitNudge !== '') markers.push(['Git nudge', gitNudge])
244
+ return buildDumpResultFromMarkers(prompt, markers)
295
245
  }
296
246
 
297
247
  export function dumpSystemPrompt(kind: OriginKind, options: { gitNudge: boolean } = { gitNudge: true }): string {
@@ -334,6 +284,7 @@ export function dumpTurnPromptWithBreakdown(kind: OriginKind): DumpResult {
334
284
  ]
335
285
  const batch = [
336
286
  {
287
+ inputId: 'system-prompt-preview',
337
288
  text: PLACEHOLDER_TURN_TEXT.channel,
338
289
  authorId: 'U<PLACEHOLDER-AUTHOR>',
339
290
  authorName: '<PLACEHOLDER: current speaker name>',
@@ -372,7 +323,7 @@ export function dumpTurnPrompt(kind: OriginKind): string {
372
323
 
373
324
  function buildDumpResult(prompt: string, parts: ReadonlyArray<readonly [string, string]>): DumpResult {
374
325
  if (parts.map(([, body]) => body).join('') !== prompt) {
375
- throw new Error('turn-prompt breakdown does not cover the rendered prompt exactly')
326
+ throw new Error('prompt breakdown does not cover the rendered prompt exactly')
376
327
  }
377
328
  return {
378
329
  prompt,
@@ -383,37 +334,26 @@ function buildDumpResult(prompt: string, parts: ReadonlyArray<readonly [string,
383
334
  }
384
335
  }
385
336
 
337
+ // Locates each marker after the previous one, so a marker string that also
338
+ // occurs earlier (inside policy prose, say) cannot be mistaken for its section.
386
339
  function buildDumpResultFromMarkers(prompt: string, markers: ReadonlyArray<readonly [string, string]>): DumpResult {
387
- const starts = markers.map(([name, marker], index) => {
388
- const start = prompt.indexOf(marker, index === 0 ? 0 : 1)
389
- if (start < 0) throw new Error(`turn-prompt section marker not found: ${name}`)
390
- return { name, start }
391
- })
392
- if (starts[0]?.start !== 0 || starts.some((part, index) => index > 0 && part.start <= starts[index - 1]!.start)) {
393
- throw new Error('turn-prompt section markers are not in rendered order')
340
+ const starts: Array<{ name: string; start: number }> = []
341
+ for (const [name, marker] of markers) {
342
+ const previous = starts.at(-1)
343
+ const start = prompt.indexOf(marker, previous === undefined ? 0 : previous.start + 1)
344
+ if (start < 0) throw new Error(`prompt section marker not found in rendered order: ${name}`)
345
+ starts.push({ name, start })
394
346
  }
347
+ if (starts[0]?.start !== 0) throw new Error('the first prompt section marker does not start the prompt')
395
348
  return buildDumpResult(
396
349
  prompt,
397
350
  starts.map((part, index) => [part.name, prompt.slice(part.start, starts[index + 1]?.start)] as const),
398
351
  )
399
352
  }
400
353
 
401
- // Slice between two unique headers in the rendered prompt. Both anchors are
402
- // guaranteed unique by `composeSystemPrompt`'s contract (each section's
403
- // header appears exactly once). Used by the breakdown so we attribute each
404
- // section's chars precisely instead of guessing from input fixtures.
405
- function extractSection(prompt: string, startHeader: string, endHeader: string | undefined): string {
406
- const start = prompt.lastIndexOf(`\n\n${startHeader}`)
407
- if (start < 0) return ''
408
- const afterStart = start + 2
409
- if (endHeader === undefined) return prompt.slice(afterStart)
410
- const end = prompt.indexOf(`\n\n${endHeader}`, afterStart)
411
- return end < 0 ? prompt.slice(afterStart) : prompt.slice(afterStart, end)
412
- }
413
-
414
354
  function header(kind: OriginKind, result: DumpResult, label: 'SYSTEM PROMPT' | 'USER TURN'): string {
415
355
  const bar = '═'.repeat(78)
416
- const summary = `~${result.totalTokens} tok / ${result.totalChars} chars / ${result.totalBytes} bytes (tok est. chars/4)`
356
+ const summary = `${result.totalChars} chars / ${result.totalBytes} bytes / ~${result.totalTokens} tok (chars/4 estimate, not a tokenizer count)`
417
357
  return `\n${bar}\n ${label} — origin: ${kind} — ${summary}\n${bar}\n`
418
358
  }
419
359
 
@@ -429,7 +369,7 @@ function renderBreakdownTable(result: DumpResult): string {
429
369
  const sep = ` ${'─'.repeat(nameW)} ${'─'.repeat(tokW)} ${'─'.repeat(charW)} ${'─'.repeat(byteW)}`
430
370
 
431
371
  const lines = [
432
- row('Section', 'Tokens', 'Chars', 'Bytes'),
372
+ row('Section', '~Tok', 'Chars', 'Bytes'),
433
373
  sep,
434
374
  ...result.sections.map((s) => row(s.name, `~${s.tokens}`, String(s.chars), String(s.bytes))),
435
375
  sep,
@@ -455,11 +395,13 @@ function main(): void {
455
395
  [
456
396
  'Usage: bun run debug:prompt [--turn] [--origin <kind>] [--no-git-nudge]',
457
397
  '',
458
- 'Dump the rendered system prompt, or the per-turn user message with --turn,',
459
- 'for one or all session-origin kinds,',
460
- 'using placeholder values for every dynamic field. Each dump is prefixed',
461
- 'with a per-section breakdown showing approximate tokens (chars/4),',
462
- 'character count, and UTF-8 byte length.',
398
+ 'Dump the TypeClaw-composed system-prompt preamble, or the per-turn user',
399
+ 'message with --turn, for one or all session-origin kinds, using',
400
+ 'placeholder values for every dynamic field. pi appends project context,',
401
+ 'the skill catalog, and the cwd to the preamble at session creation.',
402
+ 'Each dump is prefixed with a per-section breakdown of character count,',
403
+ 'UTF-8 byte length, and a rough chars/4 token estimate (not a tokenizer',
404
+ 'count); rows include their trailing separator and sum to the totals.',
463
405
  '',
464
406
  'Options:',
465
407
  ' -o, --origin <kind> tui | cron | channel | subagent | all (default: all)',
@@ -1,4 +1,4 @@
1
- import type { ThinkingLevel } from '@mariozechner/pi-agent-core'
1
+ import type { ThinkingLevel } from '@earendil-works/pi-agent-core'
2
2
 
3
3
  function normalize(text: string): string {
4
4
  return text
@@ -710,9 +710,11 @@ export function detectAttentionEscalation(text: string, prior?: QuestionSignal |
710
710
  return MORPHEME_PATTERNS.some((pattern) => pattern.test(normalized))
711
711
  }
712
712
 
713
- // `xhigh` is the strongest level. It's OpenAI-family-only, but `setThinkingLevel`
714
- // clamps it per-model (down to `high` on non-OpenAI), so it's safe to pass
715
- // unconditionally and gives OpenAI models maximum effort on escalation turns.
713
+ // Escalation targets `xhigh`. `setThinkingLevel` clamps it per model (down to
714
+ // `high` where unsupported), so it's safe to pass unconditionally. `max` ranks
715
+ // above `xhigh` (pi-ai models.js level order) on the models that offer it
716
+ // (GPT-6, Claude Opus 5.5), so a session already at `max` keeps it: an
717
+ // escalation turn must never get less effort than an ordinary one.
716
718
  const ESCALATED_LEVEL: ThinkingLevel = 'xhigh'
717
719
 
718
720
  // `allowEscalation: false` pins the turn to `sessionDefault`. Subagent turns set
@@ -729,7 +731,8 @@ export function resolveTurnThinkingLevel(
729
731
  options?: TurnThinkingOptions,
730
732
  ): ThinkingLevel | undefined {
731
733
  if (options?.allowEscalation === false) return sessionDefault
732
- return detectAttentionEscalation(text, prior) ? ESCALATED_LEVEL : sessionDefault
734
+ if (sessionDefault === 'max' || !detectAttentionEscalation(text, prior)) return sessionDefault
735
+ return ESCALATED_LEVEL
733
736
  }
734
737
 
735
738
  type ThinkingLevelSettable = {
@@ -3,9 +3,8 @@ import type { Reloadable, ReloadResult } from '@/reload'
3
3
  import { invalidateProviderAuthCache } from './auth'
4
4
 
5
5
  export type CreateProviderAuthReloadableOptions = {
6
- // Fired after the auth cache is cleared. The run stage wires this to channel
7
- // session teardown so live sessions — which captured an AuthStorage at
8
- // creation — are recreated with freshly-resolved credentials.
6
+ // Fired after the runtime cache is cleared. The run stage tears down live
7
+ // sessions so replacements capture freshly resolved credentials.
9
8
  onProviderAuthChanged?: () => void | Promise<void>
10
9
  }
11
10
 
package/src/agent/auth.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { join } from 'node:path'
2
2
 
3
- import { AuthStorage, ModelRegistry } from '@mariozechner/pi-coding-agent'
3
+ import { ModelRuntime } from '@earendil-works/pi-coding-agent'
4
4
 
5
- import { getConfig } from '@/config'
5
+ import { getConfig, resolveModel } from '@/config'
6
6
  import {
7
7
  KNOWN_PROVIDERS,
8
8
  providerForModelRef,
@@ -12,10 +12,8 @@ import {
12
12
  } from '@/config/providers'
13
13
  import { createSecretsStoreForAgent } from '@/secrets'
14
14
 
15
- type Auth = {
16
- authStorage: AuthStorage
17
- modelRegistry: ModelRegistry
18
- }
15
+ export type ProviderAuth = { modelRuntime: ModelRuntime }
16
+ type RuntimeProviderConfig = Parameters<ModelRuntime['registerProvider']>[1]
19
17
 
20
18
  const TEST_DUMMY_API_KEY = 'test_dummy_key'
21
19
 
@@ -23,89 +21,81 @@ function secretsJsonPath(): string {
23
21
  return join(process.cwd(), 'secrets.json')
24
22
  }
25
23
 
26
- // Per-provider cache. Sessions that use a profile mapped to provider X share
27
- // a single AuthStorage + ModelRegistry for that provider; first use of a new
28
- // provider lazily resolves its credentials. This replaces the singleton
29
- // `getAuth()` from before multi-model — the singleton couldn't represent
30
- // "auth for `default` is OpenAI, auth for `vision` is Fireworks" without
31
- // constructing both at boot.
32
- const cached = new Map<KnownProviderId, Auth>()
24
+ // Cache the in-flight Promise: concurrent sessions for a provider share one
25
+ // runtime and one credential store instead of racing OAuth refresh/setup.
26
+ const cached = new Map<KnownProviderId, Promise<ProviderAuth>>()
33
27
 
34
- export function getAuthFor(providerId: KnownProviderId): Auth {
28
+ export function getAuthFor(providerId: KnownProviderId): Promise<ProviderAuth> {
35
29
  const existing = cached.get(providerId)
36
30
  if (existing) return existing
31
+ const created: Promise<ProviderAuth> = createProviderAuth(providerId).catch((error) => {
32
+ // Evict only this attempt: after a reload, a newer creation may own the key.
33
+ if (cached.get(providerId) === created) cached.delete(providerId)
34
+ throw error
35
+ })
36
+ cached.set(providerId, created)
37
+ return created
38
+ }
37
39
 
40
+ async function createProviderAuth(providerId: KnownProviderId): Promise<ProviderAuth> {
38
41
  const provider = KNOWN_PROVIDERS[providerId]
39
-
40
- if (process.env.NODE_ENV === 'test' && !hasAnyCredentialInEnv(provider.apiKeyEnv)) {
41
- const authStorage = AuthStorage.inMemory()
42
- if (supportsApiKey(provider)) {
43
- authStorage.setRuntimeApiKey(provider.id, TEST_DUMMY_API_KEY)
44
- }
45
- const modelRegistry = ModelRegistry.create(authStorage)
46
- const auth = { authStorage, modelRegistry }
47
- cached.set(providerId, auth)
48
- return auth
42
+ const credentials = createSecretsStoreForAgent(secretsJsonPath())
43
+ const modelRuntime = await ModelRuntime.create({
44
+ credentials,
45
+ modelsPath: null,
46
+ refreshOnCreate: false,
47
+ })
48
+
49
+ // A session can switch to any configured fallback model. Register every
50
+ // curated provider, including built-ins: extension models replace the
51
+ // provider catalog, and composeModelProvider dispatches each passed model
52
+ // by its own api/baseUrl when its transport differs from the built-in.
53
+ for (const knownProvider of Object.values(KNOWN_PROVIDERS)) {
54
+ modelRuntime.registerProvider(knownProvider.id, toRuntimeProviderConfig(knownProvider))
49
55
  }
50
56
 
51
- const authStorage = createSecretsStoreForAgent(secretsJsonPath())
52
-
53
- // Env-wins for api-key providers: when the canonical env var is set, layer
54
- // that value in via setRuntimeApiKey so AuthStorage's hasAuth resolves
55
- // true without persisting anything to secrets.json. This is the explicit
56
- // reversal of the pre-v2 auto-migrate-to-file behaviour.
57
- //
58
- // setRuntimeApiKey is in-memory only (it writes to runtimeOverrides, never
59
- // through withLock), so the file remains untouched even when the env var
60
- // is set. OAuth credentials in the file still take precedence on read
61
- // because AuthStorage's hasAuth checks runtimeOverrides first only for
62
- // api-key-shaped credentials — OAuth on disk wins on its own.
63
- if (supportsApiKey(provider) && provider.apiKeyEnv) {
64
- const envKey = process.env[provider.apiKeyEnv]
65
- if (envKey !== undefined && envKey !== '') {
66
- const existingCred = authStorage.get(provider.id)
67
- if (existingCred === undefined || existingCred.type === 'api_key') {
68
- authStorage.setRuntimeApiKey(provider.id, envKey)
69
- }
57
+ const oauthProviders = new Set(
58
+ (await credentials.list())
59
+ .filter((credential) => credential.type === 'oauth')
60
+ .map((credential) => credential.providerId),
61
+ )
62
+ for (const knownProvider of Object.values(KNOWN_PROVIDERS)) {
63
+ if (!supportsApiKey(knownProvider) || !knownProvider.apiKeyEnv) continue
64
+ const envKey = process.env[knownProvider.apiKeyEnv]
65
+ // Runtime keys are memory-only. A persisted OAuth credential wins for
66
+ // dual-auth providers and env values never leak into the v2 envelope.
67
+ if (envKey && !oauthProviders.has(knownProvider.id)) {
68
+ await modelRuntime.setRuntimeApiKey(knownProvider.id, envKey)
70
69
  }
71
70
  }
72
71
 
73
- if (!authStorage.hasAuth(provider.id)) {
72
+ if (process.env.NODE_ENV === 'test' && !hasAnyCredentialInEnv(provider.apiKeyEnv)) {
73
+ if (supportsApiKey(provider)) await modelRuntime.setRuntimeApiKey(provider.id, TEST_DUMMY_API_KEY)
74
+ return { modelRuntime }
75
+ }
76
+ // checkAuth deliberately does not refresh OAuth. Boot must only reject a
77
+ // missing credential; a transient refresh failure belongs to the request
78
+ // path where pi can retry it on the next turn.
79
+ if (!(await modelRuntime.checkAuth(provider.id))) {
74
80
  console.error(missingCredentialMessage(providerId))
75
81
  process.exit(1)
76
82
  }
77
-
78
- const modelRegistry = ModelRegistry.create(authStorage)
79
- const auth = { authStorage, modelRegistry }
80
- cached.set(providerId, auth)
81
- return auth
83
+ return { modelRuntime }
82
84
  }
83
85
 
84
- // Back-compat shim for callers that still want the `default` profile's auth
85
- // (the main session path). Equivalent to `getAuthFor(provider-of-default)`.
86
- // Uses the head of the fallback chain; auth for the rest of the chain is
87
- // resolved lazily when fallback actually fires.
88
- export function getAuth(): Auth {
89
- const defaultRef = getConfig().models.default.refs[0]!
90
- return getAuthFor(providerForModelRef(defaultRef))
86
+ function toRuntimeProviderConfig(provider: (typeof KNOWN_PROVIDERS)[KnownProviderId]): RuntimeProviderConfig {
87
+ return {
88
+ name: provider.name,
89
+ baseUrl: provider.baseUrl,
90
+ api: Object.values(provider.models)[0]?.api,
91
+ models: Object.values(provider.models) as RuntimeProviderConfig['models'],
92
+ }
91
93
  }
92
94
 
93
- // Clears the per-provider cache so the next getAuthFor() re-resolves from
94
- // secrets.json (bind-mounted live) and re-layers process.env. Wired into
95
- // `typeclaw reload` via the providers reloadable so a rotated secrets.json
96
- // key takes effect without a container restart. Resolution stays lazy, so a
97
- // half-written secrets.json can't turn reload into getAuthFor's process-kill;
98
- // the missing-credential exit only fires on next actual use. Does NOT mutate
99
- // live AgentSessions that already captured an AuthStorage — the run stage
100
- // tears those down so the next session is built with fresh auth.
101
95
  export function invalidateProviderAuthCache(): void {
102
96
  cached.clear()
103
97
  }
104
98
 
105
- export function resetAuthForTesting(): void {
106
- cached.clear()
107
- }
108
-
109
99
  function hasAnyCredentialInEnv(apiKeyEnv: string | null): boolean {
110
100
  return apiKeyEnv !== null && process.env[apiKeyEnv] !== undefined && process.env[apiKeyEnv] !== ''
111
101
  }
@@ -114,18 +104,8 @@ function missingCredentialMessage(providerId: KnownProviderId): string {
114
104
  const provider = KNOWN_PROVIDERS[providerId]
115
105
  const defaultRef = getConfig().models.default.refs[0]!
116
106
  const defaultProviderId = providerForModelRef(defaultRef)
117
- // For the `default` profile, name the model in the error message (matches
118
- // pre-multi-model behavior). For any other profile, the user is mixing
119
- // providers across profiles and the error must name the failing provider
120
- // without claiming it's tied to the `default` model.
121
107
  const isDefault = defaultProviderId === providerId
122
- const ref = isDefault ? defaultRef : null
123
- const modelName =
124
- ref !== null
125
- ? ((provider.models as Record<string, { name: string }>)[ref.slice(ref.indexOf('/') + 1)]?.name ??
126
- ref.slice(ref.indexOf('/') + 1))
127
- : null
128
-
108
+ const modelName = isDefault ? resolveModel(defaultRef).name : null
129
109
  const oauthOnly = supportsOAuth(provider) && !supportsApiKey(provider)
130
110
  const apiKeyOnly = supportsApiKey(provider) && !supportsOAuth(provider)
131
111
 
@@ -1,5 +1,5 @@
1
- import type { KnownApi, Model } from '@mariozechner/pi-ai'
2
- import { SettingsManager } from '@mariozechner/pi-coding-agent'
1
+ import type { KnownApi, Model } from '@earendil-works/pi-ai'
2
+ import { SettingsManager } from '@earendil-works/pi-coding-agent'
3
3
 
4
4
  // Compaction trigger expressed as a fraction of the model's context window.
5
5
  // pi-coding-agent auto-compaction fires when `contextTokens > contextWindow -