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
@@ -1,16 +1,12 @@
1
1
  import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'
2
2
  import { dirname } from 'node:path'
3
3
 
4
- import {
5
- type AuthStorage,
6
- type AuthStorageBackend,
7
- AuthStorage as AuthStorageImpl,
8
- } from '@mariozechner/pi-coding-agent'
4
+ import type { AuthOperationOptions, Credential, CredentialInfo, CredentialStore } from '@earendil-works/pi-ai'
5
+ import { operationSignal, raceWithAbortSignal } from '@earendil-works/pi-ai/utils/abort'
9
6
  import lockfile from 'proper-lockfile'
10
7
 
11
8
  import { providerKeyDefaultEnv } from './defaults'
12
- import { registerXaiOAuthProvider } from './oauth-xai'
13
- import { resolveSecret, type Secret } from './resolve'
9
+ import { resolveSecret } from './resolve'
14
10
  import {
15
11
  type Channels,
16
12
  type GithubCliSecrets,
@@ -30,112 +26,115 @@ const DIR_MODE = 0o700
30
26
  const SYNC_LOCK_RETRIES = 10
31
27
  const SYNC_LOCK_DELAY_MS = 20
32
28
 
33
- const ASYNC_LOCK_OPTIONS = {
29
+ // `realpath: false` matches the sync lock, so both paths always contend on the
30
+ // same `<secrets.json>.lock` even when secrets.json is a symlink; otherwise the
31
+ // shared `stale` threshold below would not make them exclude each other.
32
+ export const ASYNC_LOCK_OPTIONS = {
34
33
  retries: { retries: 10, factor: 2, minTimeout: 100, maxTimeout: 10000, randomize: true },
35
34
  stale: 30000,
35
+ realpath: false,
36
36
  } as const
37
37
 
38
- // SecretsBackend bridges TypeClaw's on-disk envelope (v2: providers with
39
- // Secret-typed keys, channels with per-adapter field shapes) to upstream
40
- // `AuthStorage`'s flat `Record<provider, AuthCredential>` contract.
41
- //
42
- // READ (withLock's `current` parameter):
43
- // - Parse the envelope, walk `providers`, resolve each api-key `Secret` to
44
- // a flat string via env-wins (process.env wins over file value).
45
- // - OAuth credentials pass through untouched.
46
- // - Capture the resolved string for each provider into `readSnapshot` so
47
- // the write path can detect "unchanged" without re-resolving (the env
48
- // can change between read and write, and re-resolving would misclassify
49
- // env mutations as credential mutations).
50
- // - Hand AuthStorage a flat-shape JSON string. Upstream is none the wiser.
51
- //
52
- // WRITE (the `next` field):
53
- // - AuthStorage hands back the full flat slice as JSON. We do NOT
54
- // wholesale-replace the on-disk `providers` slice with this.
55
- // - Instead, we DIFF at credential level against the prior envelope using
56
- // the read-time `readSnapshot`:
57
- // * Provider unchanged (flatKey === readSnapshot[providerId]) → preserve
58
- // on-disk Secret bytes verbatim (no flatten, no rewrap). This is the
59
- // idempotency rule that prevents OAuth-refresh from accidentally
60
- // persisting env-resolved api-key values into the file.
61
- // * Provider changed → rewrap as Secret, preserving any prior `env`
62
- // field the user authored.
63
- // * Provider added → write as string shorthand (no env binding).
64
- // * Provider removed → actually remove (do NOT resurrect).
65
- // - OAuth credentials stay flat strings in the envelope (no Secret
66
- // wrapping) — they're not env-injectable.
67
- // - Unknown credential `type` values pass through verbatim, in case
68
- // upstream adds a third type in a future release.
69
- // - Empty/missing `key` from AuthStorage on api-key is treated as no-op
70
- // (preserve prior on-disk Secret if any). The schema requires non-empty
71
- // `value`, so writing an empty key would corrupt the file at next read.
72
- //
73
- // Locking and durability mirror upstream's FileAuthStorageBackend: sync
74
- // busy-loop retry on ELOCKED to keep callers synchronous, 0o600 file, 0o700
75
- // parent, atomic temp+rename.
76
- export class SecretsBackend implements AuthStorageBackend {
77
- constructor(private readonly secretsPath: string) {}
78
-
79
- withLock<T>(fn: (current: string | undefined) => { result: T; next?: string }): T {
80
- this.ensureParentDir()
81
- this.ensureFileExists()
82
- let release: (() => void) | undefined
83
- try {
84
- release = this.acquireSyncLockWithRetry()
85
- const envelope = this.readEnvelope()
86
- const { flatJson, readSnapshot } = flattenProvidersForAuthStorage(envelope.providers, process.env)
38
+ type AsyncLockOptions = Omit<typeof ASYNC_LOCK_OPTIONS, 'stale'> & { stale: number }
39
+
40
+ // The pi CredentialStore contract requires the lock only around serialized
41
+ // read-modify-write operations. Its reads are lock-free snapshots: every writer
42
+ // publishes a complete envelope through writeEnvelopeAtomic's temp-file rename,
43
+ // so they see either the old or new file. This also keeps request-time reads
44
+ // available while an OAuth refresh holds the writer lock across network I/O.
45
+ export class SecretsBackend implements CredentialStore {
46
+ constructor(
47
+ private readonly secretsPath: string,
48
+ private readonly lockOptions: AsyncLockOptions = ASYNC_LOCK_OPTIONS,
49
+ ) {}
50
+
51
+ async read(providerId: string, options?: AuthOperationOptions): Promise<Credential | undefined> {
52
+ options?.signal?.throwIfAborted()
53
+ if (!existsSync(this.secretsPath)) return undefined
54
+ return toPiCredential(this.readEnvelope().providers[providerId], process.env)
55
+ }
87
56
 
88
- const { result, next } = fn(flatJson)
89
- if (next !== undefined) {
90
- const merged = mergeProvidersIntoEnvelope(envelope, next, readSnapshot)
91
- this.writeEnvelopeAtomic(merged)
92
- }
93
- return result
94
- } finally {
95
- release?.()
96
- }
57
+ async list(options?: AuthOperationOptions): Promise<readonly CredentialInfo[]> {
58
+ options?.signal?.throwIfAborted()
59
+ if (!existsSync(this.secretsPath)) return []
60
+ return Object.entries(this.readEnvelope().providers)
61
+ .filter(([, credential]) => credential.type === 'api_key' || credential.type === 'oauth')
62
+ .map(([providerId, credential]) => ({ providerId, type: credential.type }))
97
63
  }
98
64
 
99
- async withLockAsync<T>(fn: (current: string | undefined) => Promise<{ result: T; next?: string }>): Promise<T> {
100
- this.ensureParentDir()
101
- this.ensureFileExists()
65
+ modify(
66
+ providerId: string,
67
+ fn: (current: Credential | undefined) => Promise<Credential | undefined>,
68
+ options?: AuthOperationOptions,
69
+ ): Promise<Credential | undefined> {
70
+ const signal = operationSignal(options?.signal)
71
+ const operation = Promise.resolve().then(() => {
72
+ signal.throwIfAborted()
73
+ this.ensureParentDir()
74
+ this.ensureFileExists()
75
+ return this.withProviderLock(async (assertHeld) => {
76
+ signal.throwIfAborted()
77
+ const envelope = this.readEnvelope()
78
+ const prior = envelope.providers[providerId]
79
+ const next = await fn(toPiCredential(prior, process.env))
80
+ assertHeld()
81
+ signal.throwIfAborted()
82
+ if (next === undefined) return toPiCredential(prior, process.env)
83
+ const credential = fromPiCredential(next, prior, providerId, process.env)
84
+ this.writeEnvelopeAtomic({
85
+ ...envelope,
86
+ $schema: envelope.$schema ?? SCHEMA_REL,
87
+ version: SECRETS_FILE_VERSION,
88
+ providers: { ...envelope.providers, [providerId]: credential },
89
+ })
90
+ return toPiCredential(credential, process.env)
91
+ })
92
+ })
93
+ return raceWithAbortSignal(operation, signal)
94
+ }
95
+
96
+ delete(providerId: string, options?: AuthOperationOptions): Promise<void> {
97
+ const signal = operationSignal(options?.signal)
98
+ const operation = Promise.resolve().then(() => {
99
+ signal.throwIfAborted()
100
+ if (!existsSync(this.secretsPath)) return
101
+ return this.withProviderLock(() => {
102
+ signal.throwIfAborted()
103
+ const envelope = this.readEnvelope()
104
+ if (!(providerId in envelope.providers)) return
105
+ const { [providerId]: _removed, ...providers } = envelope.providers
106
+ this.writeEnvelopeAtomic({ ...envelope, providers })
107
+ })
108
+ })
109
+ return raceWithAbortSignal(operation, signal)
110
+ }
111
+
112
+ private async withProviderLock<T>(fn: (assertHeld: () => void) => T | Promise<T>): Promise<T> {
102
113
  let release: (() => Promise<void>) | undefined
103
114
  let lockCompromised = false
104
115
  let lockCompromisedError: Error | undefined
105
- const throwIfCompromised = (): void => {
116
+ const assertHeld = (): void => {
106
117
  if (lockCompromised) {
107
118
  throw lockCompromisedError ?? new Error('Secrets store lock was compromised')
108
119
  }
109
120
  }
110
121
  try {
111
122
  release = await lockfile.lock(this.secretsPath, {
112
- ...ASYNC_LOCK_OPTIONS,
123
+ ...this.lockOptions,
113
124
  onCompromised: (err: Error) => {
114
125
  lockCompromised = true
115
126
  lockCompromisedError = err
116
127
  },
117
128
  })
118
- throwIfCompromised()
119
- const envelope = this.readEnvelope()
120
- const { flatJson, readSnapshot } = flattenProvidersForAuthStorage(envelope.providers, process.env)
121
-
122
- const { result, next } = await fn(flatJson)
123
- throwIfCompromised()
124
- if (next !== undefined) {
125
- const merged = mergeProvidersIntoEnvelope(envelope, next, readSnapshot)
126
- this.writeEnvelopeAtomic(merged)
127
- }
128
- throwIfCompromised()
129
+ assertHeld()
130
+ const result = await fn(assertHeld)
131
+ assertHeld()
129
132
  return result
130
133
  } finally {
131
134
  if (release) {
132
135
  try {
133
136
  await release()
134
- } catch {
135
- // Ignore unlock errors when the lock is compromised — there is
136
- // nothing useful we can do, and surfacing the secondary error would
137
- // mask the primary failure (mirrors upstream behaviour).
138
- }
137
+ } catch {}
139
138
  }
140
139
  }
141
140
  }
@@ -177,12 +176,9 @@ export class SecretsBackend implements AuthStorageBackend {
177
176
  release?.()
178
177
  }
179
178
  }
180
-
181
- // Returns a shallow snapshot of the `providers` slice. Used by post-init CLI
182
- // commands (`typeclaw provider list/remove`, `typeclaw model list`) that need
183
- // to inspect what's on disk without forcing AuthStorage's env-wins flatten —
184
- // we want to show users which providers are file-backed vs env-overridden,
185
- // and the flatten path collapses that distinction.
179
+ // Returns a shallow snapshot of providers for host-stage CLI inspection
180
+ // without resolving env overrides, so file-backed versus env-backed state
181
+ // remains visible to the operator.
186
182
  tryReadProvidersSync(): Providers {
187
183
  if (!existsSync(this.secretsPath)) return {}
188
184
  let release: (() => void) | undefined
@@ -277,7 +273,7 @@ export class SecretsBackend implements AuthStorageBackend {
277
273
  }
278
274
  try {
279
275
  release = await lockfile.lock(this.secretsPath, {
280
- ...ASYNC_LOCK_OPTIONS,
276
+ ...this.lockOptions,
281
277
  onCompromised: (err: Error) => {
282
278
  lockCompromised = true
283
279
  lockCompromisedError = err
@@ -328,13 +324,8 @@ export class SecretsBackend implements AuthStorageBackend {
328
324
  }
329
325
  }
330
326
 
331
- // Atomic provider credential write. Idempotent at the type level: callers
332
- // pass the full `ProviderCredential` (api_key or oauth), and the entry is
333
- // merged into `providers.<id>` verbatim — same shape the schema accepts on
334
- // read. Used by `typeclaw provider add/set` to write api-key credentials
335
- // without going through AuthStorage's flatten/unflatten round-trip.
336
- // OAuth flows MUST continue to go through `AuthStorage.login()` so refresh
337
- // tokens land in the correct shape; this method is api-key oriented.
327
+ // Host-stage CLI API-key write. OAuth login and refresh use CredentialStore
328
+ // through ModelRuntime so rotating token state remains serialized.
338
329
  writeProviderCredentialSync(providerId: string, credential: ProviderCredential): void {
339
330
  this.ensureParentDir()
340
331
  this.ensureFileExists()
@@ -441,7 +432,7 @@ export class SecretsBackend implements AuthStorageBackend {
441
432
  }
442
433
  try {
443
434
  release = await lockfile.lock(this.secretsPath, {
444
- ...ASYNC_LOCK_OPTIONS,
435
+ ...this.lockOptions,
445
436
  onCompromised: (err: Error) => {
446
437
  lockCompromised = true
447
438
  lockCompromisedError = err
@@ -477,18 +468,19 @@ export class SecretsBackend implements AuthStorageBackend {
477
468
  }
478
469
  }
479
470
 
471
+ // Seeding runs only when the file is absent, so steady-state calls do no
472
+ // I/O. Two processes seeding a first-ever store at once can still race; that
473
+ // window is accepted rather than publishing through link(), which some
474
+ // bind-mounted filesystems reject.
480
475
  private ensureFileExists(): void {
481
- if (existsSync(this.secretsPath)) return
482
- const seed = newEmptyEnvelope()
483
- writeFileSync(this.secretsPath, stringifyEnvelope(seed), 'utf8')
484
- chmodSync(this.secretsPath, FILE_MODE)
476
+ if (!existsSync(this.secretsPath)) this.writeEnvelopeAtomic(newEmptyEnvelope())
485
477
  }
486
478
 
487
479
  private acquireSyncLockWithRetry(): () => void {
488
480
  let lastError: unknown
489
481
  for (let attempt = 1; attempt <= SYNC_LOCK_RETRIES; attempt++) {
490
482
  try {
491
- return lockfile.lockSync(this.secretsPath, { realpath: false })
483
+ return lockfile.lockSync(this.secretsPath, { realpath: false, stale: this.lockOptions.stale })
492
484
  } catch (error) {
493
485
  const code =
494
486
  typeof error === 'object' && error !== null && 'code' in error
@@ -522,7 +514,7 @@ export class SecretsBackend implements AuthStorageBackend {
522
514
  }
523
515
 
524
516
  private writeEnvelopeAtomic(envelope: SecretsFile): void {
525
- const tmp = `${this.secretsPath}.${process.pid}.${Date.now()}.tmp`
517
+ const tmp = `${this.secretsPath}.${process.pid}.${Date.now()}.${tempSequence++}.tmp`
526
518
  writeFileSync(tmp, stringifyEnvelope(envelope), { encoding: 'utf8', mode: FILE_MODE })
527
519
  try {
528
520
  renameSync(tmp, this.secretsPath)
@@ -538,11 +530,12 @@ export class SecretsBackend implements AuthStorageBackend {
538
530
  }
539
531
  }
540
532
 
541
- export function createSecretsStoreForAgent(secretsPath: string): AuthStorage {
542
- registerXaiOAuthProvider()
543
- return AuthStorageImpl.fromStorage(new SecretsBackend(secretsPath))
533
+ export function createSecretsStoreForAgent(secretsPath: string): SecretsBackend {
534
+ return new SecretsBackend(secretsPath)
544
535
  }
545
536
 
537
+ let tempSequence = 0
538
+
546
539
  function newEmptyEnvelope(): SecretsFile {
547
540
  return { $schema: SCHEMA_REL, version: SECRETS_FILE_VERSION, providers: {}, channels: {}, mcp: {} }
548
541
  }
@@ -551,134 +544,40 @@ function stringifyEnvelope(envelope: SecretsFile): string {
551
544
  return `${JSON.stringify(envelope, null, 2)}\n`
552
545
  }
553
546
 
554
- type ReadSnapshot = Map<string, string>
555
-
556
- // Build the flat shape AuthStorage expects, resolving Secret-typed api-key
557
- // keys to plain strings on the way out. Also capture each resolved api-key
558
- // value into a snapshot keyed by providerId; the write path uses this
559
- // snapshot (NOT a re-resolution against current process.env) to detect
560
- // untouched providers. OAuth and unknown types are passed through verbatim
561
- // and never enter the snapshot — they don't participate in env-wins.
562
- function flattenProvidersForAuthStorage(
563
- providers: Providers,
547
+ // Preserve an untouched API-key Secret object byte-for-byte. Only an explicit
548
+ // non-empty credential mutation rewrites value while retaining an authored env
549
+ // binding; empty keys remain a no-op because v2 forbids empty Secret values.
550
+ function fromPiCredential(
551
+ next: Credential,
552
+ prior: ProviderCredential | undefined,
553
+ providerId: string,
564
554
  env: NodeJS.ProcessEnv,
565
- ): { flatJson: string; readSnapshot: ReadSnapshot } {
566
- const flat: Record<string, unknown> = {}
567
- const readSnapshot: ReadSnapshot = new Map()
568
- for (const [providerId, cred] of Object.entries(providers)) {
569
- if (cred.type === 'api_key') {
570
- const defaultEnv = providerKeyDefaultEnv(providerId)
571
- const resolved = resolveSecret(cred.key, defaultEnv, env) ?? cred.key.value ?? ''
572
- flat[providerId] = { type: 'api_key', key: resolved }
573
- readSnapshot.set(providerId, resolved)
574
- } else {
575
- flat[providerId] = cred
576
- }
555
+ ): ProviderCredential {
556
+ if (next.type === 'oauth') return next as ProviderCredential
557
+ if (!next.key) {
558
+ if (prior) return prior
559
+ throw new Error('Cannot persist an empty API key')
577
560
  }
578
- return { flatJson: JSON.stringify(flat, null, 2), readSnapshot }
579
- }
580
-
581
- // Diff-and-preserve merge per the bridge idempotency rule. AuthStorage hands
582
- // back the full flat provider slice; we walk it credential-by-credential and
583
- // decide for each provider whether to:
584
- // - preserve the prior on-disk Secret bytes verbatim (untouched provider,
585
- // detected by comparing AuthStorage's flat value to the read-time
586
- // snapshot, NOT a re-resolution against current env),
587
- // - reconstruct as Secret with prior `env` preserved (api-key value changed),
588
- // - write as new shape (provider added),
589
- // and we drop providers that disappeared from the flat slice (real removal).
590
- function mergeProvidersIntoEnvelope(
591
- envelope: SecretsFile,
592
- nextProvidersJson: string,
593
- readSnapshot: ReadSnapshot,
594
- ): SecretsFile {
595
- let parsed: unknown
596
- try {
597
- parsed = JSON.parse(nextProvidersJson)
598
- } catch (err) {
599
- throw new Error(
600
- `AuthStorage produced invalid JSON for the providers slice: ${err instanceof Error ? err.message : String(err)}`,
601
- )
561
+ if (
562
+ prior?.type === 'api_key' &&
563
+ (resolveSecret(prior.key, providerKeyDefaultEnv(providerId), env) ?? prior.key.value) === next.key
564
+ ) {
565
+ return prior
602
566
  }
603
- if (!isPlainObject(parsed)) {
604
- throw new Error('AuthStorage produced a non-object providers slice')
605
- }
606
-
607
- const nextProviders: Providers = {}
608
- for (const [providerId, raw] of Object.entries(parsed)) {
609
- const reconstructed = reconstructProviderCredential(
610
- raw,
611
- envelope.providers[providerId],
612
- readSnapshot.get(providerId),
613
- )
614
- if (reconstructed !== undefined) {
615
- nextProviders[providerId] = reconstructed
616
- }
617
- }
618
-
619
567
  return {
620
- ...envelope,
621
- $schema: envelope.$schema ?? SCHEMA_REL,
622
- version: SECRETS_FILE_VERSION,
623
- providers: nextProviders,
568
+ type: 'api_key',
569
+ key: prior?.type === 'api_key' && prior.key.env ? { value: next.key, env: prior.key.env } : { value: next.key },
624
570
  }
625
571
  }
626
572
 
627
- function reconstructProviderCredential(
628
- raw: unknown,
629
- priorOnDisk: ProviderCredential | undefined,
630
- resolvedAtRead: string | undefined,
631
- ): ProviderCredential | undefined {
632
- if (!isPlainObject(raw)) return undefined
633
- const type = raw['type']
634
-
635
- if (type === 'api_key') {
636
- const flatKey = typeof raw['key'] === 'string' ? raw['key'] : ''
637
-
638
- // Empty/missing key from AuthStorage on an api-key credential cannot
639
- // round-trip: the schema requires `value` to be non-empty, so writing
640
- // `{ value: '' }` would make the file unparseable on next read. Treat
641
- // it as "no-op" and preserve the prior on-disk Secret if any. A real
642
- // deletion comes through as the provider being absent from `next`,
643
- // which is handled by the caller dropping it.
644
- if (flatKey === '') {
645
- if (priorOnDisk !== undefined) return priorOnDisk
646
- return undefined
647
- }
648
-
649
- // Idempotency: if AuthStorage's flat key matches the resolved value
650
- // captured at read time, the credential is untouched. Preserve the
651
- // on-disk Secret verbatim — including any `env` binding and any string
652
- // shorthand the user authored. Comparing against the read-time snapshot
653
- // (not a re-resolution against current process.env) is what makes this
654
- // safe against env mutations between read and write.
655
- if (priorOnDisk && priorOnDisk.type === 'api_key' && resolvedAtRead === flatKey) {
656
- return priorOnDisk
657
- }
658
-
659
- // Mutation path: rewrap as Secret, preserving the user's `env` binding
660
- // when prior was also an api-key so the next boot's env-wins still
661
- // consults the right variable.
662
- if (priorOnDisk && priorOnDisk.type === 'api_key' && priorOnDisk.key.env !== undefined) {
663
- return { type: 'api_key', key: { value: flatKey, env: priorOnDisk.key.env } satisfies Secret }
664
- }
665
-
666
- return { type: 'api_key', key: { value: flatKey } }
667
- }
668
-
669
- if (type === 'oauth') {
670
- // OAuth credentials are not env-injectable. Pass through verbatim,
671
- // preserving every upstream-controlled field (access, refresh, expires,
672
- // and any future additions covered by the catchall).
673
- return raw as ProviderCredential
674
- }
675
-
676
- // Unknown credential type. Pass through verbatim as a defensive measure
677
- // against upstream adding a third type in a future release. Better to
678
- // round-trip user data than drop it.
679
- return raw as ProviderCredential
680
- }
681
-
682
- function isPlainObject(value: unknown): value is Record<string, unknown> {
683
- return typeof value === 'object' && value !== null && !Array.isArray(value)
573
+ // The store returns what is persisted: read() after modify() yields the value
574
+ // just written. Env-over-disk and OAuth-over-env precedence is applied once, by
575
+ // the runtime overlay in src/agent/auth.ts (setRuntimeApiKey). Resolving the
576
+ // provider's default env var here too would duplicate that policy and make a
577
+ // store read return an ambient key instead of the stored one.
578
+ function toPiCredential(credential: ProviderCredential | undefined, env: NodeJS.ProcessEnv): Credential | undefined {
579
+ if (!credential) return undefined
580
+ if (credential.type === 'oauth') return credential as Credential
581
+ const key = resolveSecret(credential.key, undefined, env) ?? credential.key.value
582
+ return key ? { type: 'api_key', key } : undefined
684
583
  }
@@ -10,6 +10,8 @@ import {
10
10
  import { applyTurnThinkingLevel } from '@/agent/attention-escalation'
11
11
  import { promptWithSameRefRetryOnly } from '@/agent/retry-same-ref'
12
12
  import type { ChannelRouter } from '@/channels/router'
13
+ import { readOomEvents, toolOomNote } from '@/container/oom-events'
14
+ import { withToolOomPriorityArgv } from '@/container/tool-oom-priority'
13
15
  import type { McpManager } from '@/mcp'
14
16
  import type { PermissionService } from '@/permissions'
15
17
  import type {
@@ -554,8 +556,9 @@ export async function runExecForCommand(
554
556
  // server started by sh -c "node server.js") would keep stdout pipes open
555
557
  // for minutes, masking the abort. See src/agent/tools/ddg.ts for the
556
558
  // same pattern applied to curl-impersonate wrappers.
559
+ const oomBefore = readOomEvents()
557
560
  const proc = Bun.spawn({
558
- cmd: ['sh', '-c', cmd],
561
+ cmd: withToolOomPriorityArgv(['sh', '-c', cmd]),
559
562
  cwd: opts.cwd,
560
563
  env: buildExecEnv(opts.cwd),
561
564
  stdout: 'pipe',
@@ -582,7 +585,8 @@ export async function runExecForCommand(
582
585
  new Response(proc.stdout as unknown as ReadableStream<Uint8Array>).text(),
583
586
  new Response(proc.stderr as unknown as ReadableStream<Uint8Array>).text(),
584
587
  ])
585
- return { stdout: stdoutText, stderr: stderrText, exitCode }
588
+ const note = toolOomNote(oomBefore, exitCode, 'plugin exec', { log: (line) => console.warn(line) })
589
+ return { stdout: stdoutText, stderr: note === null ? stderrText : `${stderrText}${note}\n`, exitCode }
586
590
  } finally {
587
591
  opts.signal.removeEventListener('abort', onAbort)
588
592
  if (escalationTimer !== null) clearTimeout(escalationTimer)