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.
- package/package.json +6 -3
- package/scripts/dump-system-prompt.ts +52 -110
- package/src/agent/attention-escalation.ts +8 -5
- package/src/agent/auth-reloadable.ts +2 -3
- package/src/agent/auth.ts +59 -79
- package/src/agent/compaction.ts +2 -2
- package/src/agent/index.ts +173 -85
- package/src/agent/live-sessions.ts +2 -2
- package/src/agent/live-subagents.ts +1 -0
- package/src/agent/llm-replay-sanitizer.ts +1 -1
- package/src/agent/model-overrides.ts +1 -1
- package/src/agent/multimodal/look-at.ts +3 -3
- package/src/agent/multimodal/read-redirect.ts +1 -1
- package/src/agent/plugin-tools.ts +76 -39
- package/src/agent/prompt-cache-key.ts +42 -0
- package/src/agent/provider-error.ts +179 -10
- package/src/agent/reload-tool.ts +1 -1
- package/src/agent/restart-handoff/index.ts +7 -14
- package/src/agent/reviewer-activity.ts +68 -0
- package/src/agent/session-origin.ts +33 -0
- package/src/agent/subagents.ts +14 -2
- package/src/agent/system-prompt.ts +68 -202
- package/src/agent/todo/continuation-policy.ts +12 -4
- package/src/agent/tool-error-remediation.ts +1 -1
- package/src/agent/tool-file-safety.ts +113 -14
- package/src/agent/tool-not-found-nudge.ts +1 -1
- package/src/agent/tool-result-budget.ts +2 -2
- package/src/agent/tools/channel-disengage.ts +2 -2
- package/src/agent/tools/channel-edit.ts +2 -2
- package/src/agent/tools/channel-fetch-attachment.ts +2 -2
- package/src/agent/tools/channel-history-render.ts +2 -1
- package/src/agent/tools/channel-history.ts +2 -2
- package/src/agent/tools/channel-react.ts +2 -2
- package/src/agent/tools/channel-read.ts +2 -2
- package/src/agent/tools/channel-reply.ts +25 -7
- package/src/agent/tools/channel-send.ts +51 -9
- package/src/agent/tools/curl-impersonate.ts +11 -2
- package/src/agent/tools/grant-role.ts +2 -2
- package/src/agent/tools/post-github-review.ts +79 -17
- package/src/agent/tools/restart.ts +2 -2
- package/src/agent/tools/skip-response.ts +4 -4
- package/src/agent/tools/spawn-subagent.ts +165 -14
- package/src/agent/tools/stream-snapshot.ts +2 -2
- package/src/agent/tools/subagent-cancel.ts +2 -2
- package/src/agent/tools/subagent-output.ts +10 -2
- package/src/agent/tools/todo/index.ts +2 -2
- package/src/agent/tools/webfetch/tool.ts +2 -2
- package/src/agent/tools/websearch.ts +2 -2
- package/src/bundled-plugins/backup/README.md +10 -9
- package/src/bundled-plugins/backup/runner.ts +140 -58
- package/src/bundled-plugins/backup/subagents.ts +58 -26
- package/src/bundled-plugins/github-cli-auth/gh-command.ts +34 -2
- package/src/bundled-plugins/github-cli-auth/granted-push.ts +135 -0
- package/src/bundled-plugins/github-cli-auth/index.ts +83 -11
- package/src/bundled-plugins/github-cli-auth/review-account.ts +23 -0
- package/src/bundled-plugins/github-cli-auth/review-checkout.ts +9 -2
- package/src/bundled-plugins/github-cli-auth/review-recorder.ts +45 -7
- package/src/bundled-plugins/operator/operator.ts +15 -4
- package/src/bundled-plugins/reviewer/skills/code-review.ts +1 -1
- package/src/bundled-plugins/security/index.ts +50 -2
- package/src/bundled-plugins/security/policies/git-exfil.ts +24 -12
- package/src/channels/adapters/discord-bot-reactions.ts +18 -0
- package/src/channels/adapters/discord-bot.ts +122 -67
- package/src/channels/adapters/discord-reactions.ts +18 -0
- package/src/channels/adapters/discord.ts +114 -8
- package/src/channels/adapters/github/inbound.ts +103 -8
- package/src/channels/adapters/github/index.ts +251 -19
- package/src/channels/adapters/github/last-alive.ts +27 -0
- package/src/channels/adapters/github/outbound.ts +54 -18
- package/src/channels/adapters/github/pending-review-store.ts +141 -0
- package/src/channels/adapters/github/permission-guidance.ts +1 -0
- package/src/channels/adapters/github/reactions.ts +100 -0
- package/src/channels/adapters/github/reconcile-cooldown-store.ts +197 -67
- package/src/channels/adapters/github/reconcile-open-prs.ts +418 -33
- package/src/channels/adapters/github/recovery.ts +106 -0
- package/src/channels/adapters/github/review-check.ts +510 -0
- package/src/channels/adapters/github/review-submitter.ts +34 -2
- package/src/channels/adapters/instagram.ts +32 -14
- package/src/channels/adapters/kakaotalk.ts +22 -7
- package/src/channels/adapters/line.ts +18 -3
- package/src/channels/adapters/outbound-account.ts +20 -0
- package/src/channels/adapters/own-reactions.ts +66 -0
- package/src/channels/adapters/recovery-correlation.ts +214 -0
- package/src/channels/adapters/slack-bot-classify.ts +18 -11
- package/src/channels/adapters/slack-bot-dedupe.ts +111 -28
- package/src/channels/adapters/slack-bot-reactions.ts +18 -0
- package/src/channels/adapters/slack-bot.ts +125 -57
- package/src/channels/adapters/slack-classify.ts +25 -4
- package/src/channels/adapters/slack-inbound-revision.ts +96 -0
- package/src/channels/adapters/slack-reactions.ts +18 -0
- package/src/channels/adapters/slack-thread-ts.ts +6 -0
- package/src/channels/adapters/slack.ts +48 -3
- package/src/channels/adapters/teams.ts +14 -4
- package/src/channels/adapters/telegram-bot.ts +56 -10
- package/src/channels/adapters/webex-bot.ts +12 -2
- package/src/channels/adapters/webex.ts +12 -2
- package/src/channels/background-handoff.ts +434 -0
- package/src/channels/background-obligations.ts +893 -0
- package/src/channels/continuity-types.ts +271 -0
- package/src/channels/github-review-check.ts +598 -0
- package/src/channels/github-review-turn-ledger.ts +102 -28
- package/src/channels/github-review-verdict-coordinator.ts +96 -13
- package/src/channels/github-token-bridge.ts +11 -5
- package/src/channels/inbound-journal.ts +1738 -0
- package/src/channels/index.ts +6 -1
- package/src/channels/manager.ts +17 -7
- package/src/channels/recovery-dispatcher.ts +264 -0
- package/src/channels/recovery-notice.ts +94 -0
- package/src/channels/recovery-outbox.ts +459 -0
- package/src/channels/router.ts +2582 -633
- package/src/channels/schema.ts +8 -1
- package/src/channels/subagent-completion-bridge.ts +17 -14
- package/src/channels/types.ts +121 -16
- package/src/channels/waiting-reactions.ts +664 -0
- package/src/cli/compose.ts +7 -4
- package/src/cli/hostd.ts +49 -3
- package/src/cli/inspect.ts +11 -14
- package/src/cli/model.ts +7 -5
- package/src/cli/oauth-callbacks.ts +38 -23
- package/src/coding-agent/broker-body.ts +38 -0
- package/src/coding-agent/broker.ts +290 -0
- package/src/coding-agent/claude.ts +95 -0
- package/src/coding-agent/codex.ts +104 -0
- package/src/coding-agent/index.ts +506 -0
- package/src/coding-agent/omo-state.ts +69 -0
- package/src/coding-agent/omo-stream.ts +98 -0
- package/src/coding-agent/omo.ts +192 -0
- package/src/coding-agent/runner.ts +263 -0
- package/src/coding-agent/tool.ts +62 -0
- package/src/coding-agent/worktree.ts +355 -0
- package/src/compose/restart.ts +10 -3
- package/src/compose/start.ts +20 -3
- package/src/config/config.ts +70 -26
- package/src/config/providers.ts +278 -50
- package/src/container/exit-reason.ts +44 -0
- package/src/container/index.ts +5 -0
- package/src/container/log-archive.ts +51 -13
- package/src/container/memory-limit.ts +1 -1
- package/src/container/memory-oversubscription.ts +36 -6
- package/src/container/oom-events.ts +86 -0
- package/src/container/restart.ts +2 -0
- package/src/container/shared.ts +8 -1
- package/src/container/start.ts +25 -13
- package/src/container/stop.ts +6 -2
- package/src/container/tool-oom-priority.ts +32 -0
- package/src/cron/consumer.ts +10 -4
- package/src/cron/reloadable.ts +23 -2
- package/src/doctor/checks.ts +14 -2
- package/src/hostd/daemon.ts +20 -10
- package/src/hostd/spawn.ts +106 -5
- package/src/hostd/version.ts +20 -0
- package/src/hostd/watchdog.ts +241 -0
- package/src/init/dockerfile.ts +57 -592
- package/src/init/index.ts +6 -7
- package/src/init/oauth-login.ts +57 -17
- package/src/inspect/transcript-view.ts +6 -3
- package/src/mcp/client.ts +3 -1
- package/src/operations/classify-tool-outcome.ts +45 -1
- package/src/operations/incidents.ts +10 -5
- package/src/plugin/core-tool-names.ts +1 -1
- package/src/plugin/define.ts +1 -0
- package/src/plugin/index.ts +1 -0
- package/src/plugin/types.ts +6 -1
- package/src/run/background-handoff-boot.ts +86 -0
- package/src/run/channel-session-factory.ts +5 -4
- package/src/run/index.ts +93 -93
- package/src/run/resource-sample.ts +19 -0
- package/src/sandbox/build.ts +4 -0
- package/src/sandbox/coding-agent.ts +214 -0
- package/src/sandbox/policy.ts +1 -0
- package/src/sandbox/runtime-private.ts +4 -1
- package/src/secrets/coding-agent-auth.ts +97 -0
- package/src/secrets/index.ts +0 -16
- package/src/secrets/refresh-provider-oauth.ts +30 -76
- package/src/secrets/schema.ts +3 -4
- package/src/secrets/storage.ts +133 -234
- package/src/server/command-runner.ts +6 -2
- package/src/server/index.ts +102 -41
- package/src/sessions/index.ts +1 -1
- package/src/skills/typeclaw-channel-github/SKILL.md +7 -5
- package/src/skills/typeclaw-claude-code/SKILL.md +25 -260
- package/src/skills/typeclaw-claude-code/references/auth-flow.md +7 -23
- package/src/skills/typeclaw-claude-code/references/headless-sessions.md +11 -0
- package/src/skills/typeclaw-claude-code/references/results-and-worktrees.md +13 -0
- package/src/skills/typeclaw-codex-cli/SKILL.md +35 -270
- package/src/skills/typeclaw-codex-cli/references/auth-flow.md +13 -20
- package/src/skills/typeclaw-codex-cli/references/headless-sessions.md +26 -0
- package/src/skills/typeclaw-codex-cli/references/results-and-worktrees.md +28 -0
- package/src/skills/typeclaw-config/SKILL.md +24 -24
- package/src/skills/typeclaw-config/references/dockerfile.md +14 -13
- package/src/skills/typeclaw-cron/SKILL.md +1 -1
- package/src/skills/typeclaw-github-contributing/SKILL.md +2 -2
- package/src/skills/typeclaw-memory/SKILL.md +3 -3
- package/src/skills/typeclaw-omo-cli/SKILL.md +52 -0
- package/src/skills/typeclaw-omo-cli/references/auth-flow.md +11 -0
- package/src/skills/typeclaw-omo-cli/references/headless-sessions.md +11 -0
- package/src/skills/typeclaw-omo-cli/references/results-and-worktrees.md +23 -0
- package/src/skills/typeclaw-plugins/SKILL.md +20 -20
- package/src/skills/typeclaw-skills/SKILL.md +19 -2
- package/src/skills/typeclaw-troubleshooting/SKILL.md +15 -2
- package/src/tui/index.ts +5 -3
- package/src/tui/theme.ts +1 -1
- package/typeclaw.schema.json +44 -2
- package/src/agent/adaptive-thinking-compat.ts +0 -79
- package/src/secrets/claude-credentials-json.ts +0 -129
- package/src/secrets/codex-auth-json.ts +0 -102
- package/src/secrets/export-claude-credentials-file.ts +0 -270
- package/src/secrets/export-codex-auth-file.ts +0 -250
- package/src/secrets/oauth-xai.ts +0 -348
- package/src/skills/typeclaw-claude-code/references/stop-hook.md +0 -101
- package/src/skills/typeclaw-claude-code/references/tmux-driving.md +0 -243
- package/src/skills/typeclaw-codex-cli/references/stop-hook.md +0 -92
- package/src/skills/typeclaw-codex-cli/references/tmux-driving.md +0 -239
package/src/hostd/spawn.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
|
-
import { lstat, open, readFile, unlink, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { appendFile, lstat, open, readFile, stat, unlink, writeFile } from 'node:fs/promises'
|
|
3
3
|
|
|
4
4
|
import lockfile from 'proper-lockfile'
|
|
5
5
|
|
|
@@ -34,6 +34,12 @@ const EXIT_SETTLE_MS = 500
|
|
|
34
34
|
// seen as stale by a contender — only a crashed holder, whose refresh timer
|
|
35
35
|
// died with it, is reclaimed. Mirrors the models/secrets locks' 30s ceiling.
|
|
36
36
|
const LOCK_STALE_MS = 30_000
|
|
37
|
+
// A pidfile child that is still unreachable this long after it was spawned is
|
|
38
|
+
// wedged (e.g. stuck in Bun's crash handler after a segfault), not slow-booting.
|
|
39
|
+
// The pidfile is written at spawn time, so its mtime is the child's birth time.
|
|
40
|
+
const WEDGED_DAEMON_AGE_MS = 30_000
|
|
41
|
+
const SHUTDOWN_EXIT_GRACE_MS = 2_000
|
|
42
|
+
const KILL_GRACE_MS = 2_000
|
|
37
43
|
const LOCK_RETRY_BACKOFF = {
|
|
38
44
|
factor: 1,
|
|
39
45
|
minTimeout: POLL_INTERVAL_MS,
|
|
@@ -90,6 +96,7 @@ async function readHttpPort(): Promise<number | null> {
|
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
async function requestShutdownAndWait(): Promise<boolean> {
|
|
99
|
+
const pid = await readPidQuiet()
|
|
93
100
|
const reply = await send({ kind: 'shutdown' }, { timeoutMs: 1_000 })
|
|
94
101
|
if (!reply.ok) return false
|
|
95
102
|
const deadline = Date.now() + SHUTDOWN_TIMEOUT_MS
|
|
@@ -99,12 +106,31 @@ async function requestShutdownAndWait(): Promise<boolean> {
|
|
|
99
106
|
await sleep(POLL_INTERVAL_MS)
|
|
100
107
|
continue
|
|
101
108
|
}
|
|
102
|
-
if (!existsSync(socketPath()))
|
|
109
|
+
if (!existsSync(socketPath())) {
|
|
110
|
+
await reapLingeringDaemon(pid)
|
|
111
|
+
return true
|
|
112
|
+
}
|
|
103
113
|
await sleep(POLL_INTERVAL_MS)
|
|
104
114
|
}
|
|
105
115
|
return false
|
|
106
116
|
}
|
|
107
117
|
|
|
118
|
+
// The daemon unlinks its socket at the end of stop(), before exiting. A daemon
|
|
119
|
+
// that never makes it to exit (observed in the field: "stopping" logged, process
|
|
120
|
+
// alive for days) keeps its HTTP control port and renewal timers, so the fresh
|
|
121
|
+
// daemon falls back to another port and two daemons run against one home.
|
|
122
|
+
async function reapLingeringDaemon(pid: number): Promise<void> {
|
|
123
|
+
if (pid <= 0) return
|
|
124
|
+
if (await waitForExit(pid, SHUTDOWN_EXIT_GRACE_MS)) return
|
|
125
|
+
if (!(await isHostdProcess(pid))) return
|
|
126
|
+
const reaped = await terminateProcess(pid)
|
|
127
|
+
await appendSpawnLog(
|
|
128
|
+
reaped
|
|
129
|
+
? `reaped host daemon pid ${pid} that acknowledged shutdown but did not exit`
|
|
130
|
+
: `host daemon pid ${pid} acknowledged shutdown but did not exit and could not be terminated`,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
108
134
|
type SpawnAttemptResult = { ok: true; pid: number; spawned: boolean; httpPort: number } | { ok: false; reason: string }
|
|
109
135
|
|
|
110
136
|
async function spawnUnderLock(opts: EnsureDaemonOptions): Promise<SpawnAttemptResult> {
|
|
@@ -135,9 +161,18 @@ async function spawnUnderLock(opts: EnsureDaemonOptions): Promise<SpawnAttemptRe
|
|
|
135
161
|
// adopt the existing child and poll IT for readiness instead of re-spawning.
|
|
136
162
|
const existingPid = await livePidfileChild()
|
|
137
163
|
if (existingPid !== null) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
164
|
+
if (!(await isWedgedDaemon(existingPid))) {
|
|
165
|
+
const ready = await pollForReadiness(opts.spawnTimeoutMs ?? DEFAULT_SPAWN_TIMEOUT_MS)
|
|
166
|
+
if (ready === null) return { ok: false, reason: 'daemon spawned but did not become reachable yet' }
|
|
167
|
+
return { ok: true, pid: existingPid, spawned: false, httpPort: ready }
|
|
168
|
+
}
|
|
169
|
+
// Adopting a wedged daemon would block every future start forever, so
|
|
170
|
+
// replace it. Only reached after isDaemonReachable() failed above.
|
|
171
|
+
if (!(await terminateProcess(existingPid))) {
|
|
172
|
+
await appendSpawnLog(`host daemon pid ${existingPid} is unresponsive and could not be terminated`)
|
|
173
|
+
return { ok: false, reason: `host daemon pid ${existingPid} is unresponsive and could not be terminated` }
|
|
174
|
+
}
|
|
175
|
+
await appendSpawnLog(`replaced unresponsive host daemon pid ${existingPid}`)
|
|
141
176
|
}
|
|
142
177
|
await opts.onSpawnEnter?.()
|
|
143
178
|
return await spawnDaemonDetached(opts)
|
|
@@ -160,6 +195,72 @@ async function livePidfileChild(): Promise<number | null> {
|
|
|
160
195
|
}
|
|
161
196
|
}
|
|
162
197
|
|
|
198
|
+
async function isWedgedDaemon(pid: number): Promise<boolean> {
|
|
199
|
+
if (isWindows()) return false
|
|
200
|
+
let spawnedAtMs: number
|
|
201
|
+
try {
|
|
202
|
+
spawnedAtMs = (await stat(pidfilePath())).mtimeMs
|
|
203
|
+
} catch {
|
|
204
|
+
return false
|
|
205
|
+
}
|
|
206
|
+
if (Date.now() - spawnedAtMs < WEDGED_DAEMON_AGE_MS) return false
|
|
207
|
+
return isHostdProcess(pid)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// PID-reuse guard: a pidfile pid is only signalled when its full command line
|
|
211
|
+
// is exactly the three-token launch signature from spawnDaemonDetached — the
|
|
212
|
+
// bun executable, the typeclaw CLI entry, `_hostd` — so a recycled pid that
|
|
213
|
+
// mentions `_hostd`, or passes the whole signature as arguments to another
|
|
214
|
+
// program (`watch … pgrep -f bun …/cli/index.ts _hostd`), is never killed.
|
|
215
|
+
// Tokens are whitespace-free: `ps` joins argv with spaces, so a path containing
|
|
216
|
+
// spaces is ambiguous and fails closed (adopted, never signalled).
|
|
217
|
+
const HOSTD_COMMAND_SIGNATURE = /^(?:\S*\/)?bun \S*\/(?:cli\/index\.ts|typeclaw) _hostd$/
|
|
218
|
+
|
|
219
|
+
async function isHostdProcess(pid: number): Promise<boolean> {
|
|
220
|
+
if (isWindows()) return false
|
|
221
|
+
try {
|
|
222
|
+
const proc = Bun.spawn({
|
|
223
|
+
cmd: ['ps', '-o', 'command=', '-p', String(pid)],
|
|
224
|
+
stdin: 'ignore',
|
|
225
|
+
stdout: 'pipe',
|
|
226
|
+
stderr: 'ignore',
|
|
227
|
+
})
|
|
228
|
+
const out = await new Response(proc.stdout).text()
|
|
229
|
+
await proc.exited
|
|
230
|
+
return HOSTD_COMMAND_SIGNATURE.test(out.trim())
|
|
231
|
+
} catch {
|
|
232
|
+
return false
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// SIGTERM first so a merely-hung event loop can still run its shutdown handler;
|
|
237
|
+
// SIGKILL for a process that can't (e.g. spinning inside a crash handler).
|
|
238
|
+
async function terminateProcess(pid: number): Promise<boolean> {
|
|
239
|
+
for (const signal of ['SIGTERM', 'SIGKILL'] as const) {
|
|
240
|
+
try {
|
|
241
|
+
process.kill(pid, signal)
|
|
242
|
+
} catch (error) {
|
|
243
|
+
if (errorCode(error) === 'ESRCH') return true
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
if (await waitForExit(pid, KILL_GRACE_MS)) return true
|
|
247
|
+
}
|
|
248
|
+
return false
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function waitForExit(pid: number, timeoutMs: number): Promise<boolean> {
|
|
252
|
+
const deadline = Date.now() + timeoutMs
|
|
253
|
+
while (processExists(pid)) {
|
|
254
|
+
if (Date.now() >= deadline) return false
|
|
255
|
+
await sleep(POLL_INTERVAL_MS)
|
|
256
|
+
}
|
|
257
|
+
return true
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function appendSpawnLog(message: string): Promise<void> {
|
|
261
|
+
await appendFile(logfilePath(), `${new Date().toISOString()} [hostd-spawn] ${message}\n`).catch(() => {})
|
|
262
|
+
}
|
|
263
|
+
|
|
163
264
|
async function pollForReadiness(timeoutMs: number): Promise<number | null> {
|
|
164
265
|
const deadline = Date.now() + timeoutMs
|
|
165
266
|
while (Date.now() < deadline) {
|
package/src/hostd/version.ts
CHANGED
|
@@ -28,6 +28,11 @@ import { dirname, join, resolve, sep } from 'node:path'
|
|
|
28
28
|
// callback in `src/cli/hostd.ts` reaches into `src/container/` and
|
|
29
29
|
// `src/config/`). Over-respawning on `src/agent/` changes is acceptable
|
|
30
30
|
// cost: a daemon spawn is < 100ms.
|
|
31
|
+
//
|
|
32
|
+
// The Bun runtime version is folded in too. The daemon keeps running on the
|
|
33
|
+
// binary it was spawned with, so after `bun upgrade` it would otherwise stay on
|
|
34
|
+
// the old runtime until killed by hand — and an old runtime crashing is exactly
|
|
35
|
+
// what took hostd down in the field.
|
|
31
36
|
|
|
32
37
|
export type SourceVersion = string
|
|
33
38
|
|
|
@@ -38,6 +43,8 @@ export type ComputeOptions = {
|
|
|
38
43
|
// Test seam. Tests inject an in-memory file map to keep the unit tests
|
|
39
44
|
// hermetic; production reads the real filesystem.
|
|
40
45
|
fs?: VersionFs
|
|
46
|
+
// Test seam: defaults to the Bun running this process.
|
|
47
|
+
runtimeVersion?: string
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
export type VersionFs = {
|
|
@@ -60,6 +67,7 @@ export async function computeSourceVersion(opts: ComputeOptions): Promise<Source
|
|
|
60
67
|
files.sort()
|
|
61
68
|
|
|
62
69
|
const hash = createHash('sha256')
|
|
70
|
+
hash.update(`bun:${opts.runtimeVersion ?? process.versions.bun ?? 'unknown'}\u0000`)
|
|
63
71
|
for (const rel of files) {
|
|
64
72
|
const abs = join(root, rel)
|
|
65
73
|
const bytes = await fs.readFile(abs)
|
|
@@ -113,3 +121,15 @@ export function resolveSrcRoot(cliEntry: string): string | null {
|
|
|
113
121
|
// preserves correctness for non-dev installs at the cost of disabling drift
|
|
114
122
|
// detection there.
|
|
115
123
|
export const UNVERSIONED_SENTINEL: SourceVersion = 'unversioned'
|
|
124
|
+
|
|
125
|
+
// The Bun that CI and the container base image (`BUN_BASE_IMAGE`) run. Bump it
|
|
126
|
+
// together with the `bun-version:` pins in .github/workflows/*.yml.
|
|
127
|
+
export const TESTED_BUN_VERSION = '1.4.0'
|
|
128
|
+
|
|
129
|
+
// The host daemon runs on the host's Bun, not the container's pinned one, so an
|
|
130
|
+
// operator can silently stay on a runtime CI never tested (Bun 1.3.14's
|
|
131
|
+
// segfault-then-wedge is what took a host daemon down for days).
|
|
132
|
+
export function hostBunVersionWarning(hostBunVersion: string | undefined): string | null {
|
|
133
|
+
if (!hostBunVersion || Bun.semver.order(hostBunVersion, TESTED_BUN_VERSION) >= 0) return null
|
|
134
|
+
return `host Bun ${hostBunVersion} is older than Bun ${TESTED_BUN_VERSION}, which typeclaw is tested on; older runtimes have known crash bugs that take the host daemon down. Run \`bun upgrade\`.`
|
|
135
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// The host daemon is a single long-lived Bun process, and a runtime fault in it
|
|
2
|
+
// takes every host-side service down with it: credential renewal, the
|
|
3
|
+
// portbroker, container self-restart. Worse, Bun's crash handler can wedge
|
|
4
|
+
// instead of exiting (observed in the field: a segfault left the process
|
|
5
|
+
// spinning at 100% CPU for days, holding the pidfile). Nothing re-spawned it
|
|
6
|
+
// until an operator happened to run a host CLI command.
|
|
7
|
+
//
|
|
8
|
+
// So `_hostd` is a thin parent that runs the real daemon as a child and keeps
|
|
9
|
+
// it alive: a child that exits non-zero is respawned, and a child that stops
|
|
10
|
+
// answering its control socket is SIGKILLed and respawned. The parent does
|
|
11
|
+
// nothing but spawn, probe, and kill, which keeps it off the fetch/WebSocket/
|
|
12
|
+
// timer-heavy paths where the runtime faults have been observed.
|
|
13
|
+
//
|
|
14
|
+
// A clean (code 0) child exit is an intentional shutdown — the version-drift
|
|
15
|
+
// `shutdown` RPC or SIGTERM — so the parent exits with it and the next CLI call
|
|
16
|
+
// respawns the pair, exactly as before the watchdog existed.
|
|
17
|
+
|
|
18
|
+
export type WatchdogWorkerExit = { code: number | null; signal: string | null }
|
|
19
|
+
|
|
20
|
+
export type WatchdogWorker = {
|
|
21
|
+
pid: number
|
|
22
|
+
exited: Promise<WatchdogWorkerExit>
|
|
23
|
+
kill: (signal: NodeJS.Signals) => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type WatchdogOptions = {
|
|
27
|
+
spawnWorker: () => WatchdogWorker
|
|
28
|
+
probe: () => Promise<boolean>
|
|
29
|
+
onLog: (message: string) => void
|
|
30
|
+
probeIntervalMs?: number
|
|
31
|
+
// A freshly spawned child needs time to bind its socket; probe misses inside
|
|
32
|
+
// this window don't count unless the child has already answered once.
|
|
33
|
+
startupGraceMs?: number
|
|
34
|
+
maxMisses?: number
|
|
35
|
+
initialBackoffMs?: number
|
|
36
|
+
maxBackoffMs?: number
|
|
37
|
+
// A child that stayed up this long was healthy, so the next respawn starts
|
|
38
|
+
// from the initial backoff again instead of the crash-loop ceiling.
|
|
39
|
+
healthyResetMs?: number
|
|
40
|
+
// `spawn.ts` escalates SIGTERM to SIGKILL on this parent after 2s, so the
|
|
41
|
+
// child must be forced down inside that window or it outlives the parent.
|
|
42
|
+
stopGraceMs?: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type Watchdog = {
|
|
46
|
+
done: Promise<number>
|
|
47
|
+
stop: () => Promise<void>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const DEFAULT_PROBE_INTERVAL_MS = 10_000
|
|
51
|
+
const DEFAULT_STARTUP_GRACE_MS = 30_000
|
|
52
|
+
const DEFAULT_MAX_MISSES = 3
|
|
53
|
+
const DEFAULT_INITIAL_BACKOFF_MS = 1_000
|
|
54
|
+
const DEFAULT_MAX_BACKOFF_MS = 60_000
|
|
55
|
+
const DEFAULT_HEALTHY_RESET_MS = 5 * 60_000
|
|
56
|
+
const DEFAULT_STOP_GRACE_MS = 1_500
|
|
57
|
+
const KILL_WAIT_MS = 5_000
|
|
58
|
+
|
|
59
|
+
type SuperviseOutcome =
|
|
60
|
+
| { kind: 'stopped' }
|
|
61
|
+
| { kind: 'exited'; exit: WatchdogWorkerExit }
|
|
62
|
+
| { kind: 'unresponsive'; misses: number }
|
|
63
|
+
|
|
64
|
+
export function runWatchdog(opts: WatchdogOptions): Watchdog {
|
|
65
|
+
const probeIntervalMs = opts.probeIntervalMs ?? DEFAULT_PROBE_INTERVAL_MS
|
|
66
|
+
const startupGraceMs = opts.startupGraceMs ?? DEFAULT_STARTUP_GRACE_MS
|
|
67
|
+
const maxMisses = opts.maxMisses ?? DEFAULT_MAX_MISSES
|
|
68
|
+
const initialBackoffMs = opts.initialBackoffMs ?? DEFAULT_INITIAL_BACKOFF_MS
|
|
69
|
+
const maxBackoffMs = opts.maxBackoffMs ?? DEFAULT_MAX_BACKOFF_MS
|
|
70
|
+
const healthyResetMs = opts.healthyResetMs ?? DEFAULT_HEALTHY_RESET_MS
|
|
71
|
+
const stopGraceMs = opts.stopGraceMs ?? DEFAULT_STOP_GRACE_MS
|
|
72
|
+
const log = opts.onLog
|
|
73
|
+
|
|
74
|
+
let stopping = false
|
|
75
|
+
let resolveStop: () => void = () => {}
|
|
76
|
+
const stopRequested = new Promise<'stop'>((resolve) => {
|
|
77
|
+
resolveStop = () => resolve('stop')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const done = (async (): Promise<number> => {
|
|
81
|
+
let backoffMs = initialBackoffMs
|
|
82
|
+
while (!stopping) {
|
|
83
|
+
const startedAt = Date.now()
|
|
84
|
+
let worker: WatchdogWorker
|
|
85
|
+
try {
|
|
86
|
+
worker = opts.spawnWorker()
|
|
87
|
+
} catch (error) {
|
|
88
|
+
log(`failed to spawn host daemon worker: ${error instanceof Error ? error.message : String(error)}`)
|
|
89
|
+
if (await sleepOrStop(backoffMs)) break
|
|
90
|
+
backoffMs = Math.min(backoffMs * 2, maxBackoffMs)
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
log(`started host daemon worker pid ${worker.pid}`)
|
|
94
|
+
|
|
95
|
+
const outcome = await supervise(worker)
|
|
96
|
+
if (outcome.kind === 'stopped') break
|
|
97
|
+
if (outcome.kind === 'exited' && outcome.exit.code === 0) {
|
|
98
|
+
log(`host daemon worker pid ${worker.pid} exited cleanly; watchdog exiting`)
|
|
99
|
+
return 0
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (Date.now() - startedAt >= healthyResetMs) backoffMs = initialBackoffMs
|
|
103
|
+
const reason =
|
|
104
|
+
outcome.kind === 'exited'
|
|
105
|
+
? `exited unexpectedly (${describeExit(outcome.exit)})`
|
|
106
|
+
: `stopped answering its control socket (${outcome.misses} missed probes) and was killed`
|
|
107
|
+
log(`host daemon worker pid ${worker.pid} ${reason}; respawning in ${backoffMs}ms`)
|
|
108
|
+
if (await sleepOrStop(backoffMs)) break
|
|
109
|
+
backoffMs = Math.min(backoffMs * 2, maxBackoffMs)
|
|
110
|
+
}
|
|
111
|
+
return 0
|
|
112
|
+
})()
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
done,
|
|
116
|
+
stop: async () => {
|
|
117
|
+
stopping = true
|
|
118
|
+
resolveStop()
|
|
119
|
+
await done
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function supervise(worker: WatchdogWorker): Promise<SuperviseOutcome> {
|
|
124
|
+
const spawnedAt = Date.now()
|
|
125
|
+
const exit = worker.exited.then((result) => ({ kind: 'exit' as const, result }))
|
|
126
|
+
let answeredOnce = false
|
|
127
|
+
let misses = 0
|
|
128
|
+
|
|
129
|
+
while (true) {
|
|
130
|
+
const wake = await Promise.race([exit, stopRequested, sleep(probeIntervalMs).then(() => 'tick' as const)])
|
|
131
|
+
if (wake === 'stop') {
|
|
132
|
+
await terminate(worker, exit)
|
|
133
|
+
return { kind: 'stopped' }
|
|
134
|
+
}
|
|
135
|
+
if (wake !== 'tick') return { kind: 'exited', exit: wake.result }
|
|
136
|
+
|
|
137
|
+
// A stalled probe can take its full timeout; stop must not wait it out,
|
|
138
|
+
// or spawn.ts SIGKILLs this parent before the worker is down.
|
|
139
|
+
const answered = await Promise.race([opts.probe().catch(() => false), exit.then(() => null), stopRequested])
|
|
140
|
+
if (answered === 'stop') {
|
|
141
|
+
await terminate(worker, exit)
|
|
142
|
+
return { kind: 'stopped' }
|
|
143
|
+
}
|
|
144
|
+
if (answered === null) continue
|
|
145
|
+
if (answered) {
|
|
146
|
+
answeredOnce = true
|
|
147
|
+
misses = 0
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
if (!answeredOnce && Date.now() - spawnedAt < startupGraceMs) continue
|
|
151
|
+
misses += 1
|
|
152
|
+
if (misses < maxMisses) continue
|
|
153
|
+
|
|
154
|
+
// A wedged Bun crash handler does not run signal handlers, so SIGTERM
|
|
155
|
+
// would just burn the wait; go straight to SIGKILL.
|
|
156
|
+
worker.kill('SIGKILL')
|
|
157
|
+
if ((await Promise.race([exit, sleep(KILL_WAIT_MS).then(() => 'timeout' as const)])) === 'timeout') {
|
|
158
|
+
log(`host daemon worker pid ${worker.pid} did not exit after SIGKILL; respawning anyway`)
|
|
159
|
+
}
|
|
160
|
+
return { kind: 'unresponsive', misses }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function terminate(worker: WatchdogWorker, exit: Promise<unknown>): Promise<void> {
|
|
165
|
+
worker.kill('SIGTERM')
|
|
166
|
+
if ((await Promise.race([exit, sleep(stopGraceMs).then(() => 'timeout' as const)])) !== 'timeout') return
|
|
167
|
+
worker.kill('SIGKILL')
|
|
168
|
+
await Promise.race([exit, sleep(KILL_WAIT_MS)])
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function sleepOrStop(ms: number): Promise<boolean> {
|
|
172
|
+
return (await Promise.race([stopRequested, sleep(ms).then(() => 'slept' as const)])) === 'stop'
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export type OrphanWatchOptions = {
|
|
177
|
+
// Passed from the parent rather than read from `process.ppid` at startup: a
|
|
178
|
+
// parent that dies before the worker finishes loading would otherwise be
|
|
179
|
+
// captured as the already-reparented pid and never noticed.
|
|
180
|
+
watchdogPid: number
|
|
181
|
+
onOrphaned: () => void
|
|
182
|
+
getParentPid?: () => number
|
|
183
|
+
exit?: (code: number) => void
|
|
184
|
+
intervalMs?: number
|
|
185
|
+
// Graceful shutdown awaits restore and renewal drains with no deadline of
|
|
186
|
+
// their own, and no supervisor is left to force it, so cap it here.
|
|
187
|
+
exitDeadlineMs?: number
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const DEFAULT_ORPHAN_CHECK_INTERVAL_MS = 2_000
|
|
191
|
+
const DEFAULT_ORPHAN_EXIT_DEADLINE_MS = 3_000
|
|
192
|
+
|
|
193
|
+
// A worker whose watchdog parent was SIGKILLed would keep the socket and
|
|
194
|
+
// renewal timers alive with no pidfile pointing at it, so the next CLI call
|
|
195
|
+
// could neither reach a fresh daemon nor reap this one. Exits within
|
|
196
|
+
// intervalMs + exitDeadlineMs of the parent dying.
|
|
197
|
+
export function watchForOrphaning(opts: OrphanWatchOptions): () => void {
|
|
198
|
+
const getParentPid = opts.getParentPid ?? (() => process.ppid)
|
|
199
|
+
const exit = opts.exit ?? ((code: number) => process.exit(code))
|
|
200
|
+
const exitDeadlineMs = opts.exitDeadlineMs ?? DEFAULT_ORPHAN_EXIT_DEADLINE_MS
|
|
201
|
+
let interval: ReturnType<typeof setInterval> | undefined
|
|
202
|
+
|
|
203
|
+
const check = (): boolean => {
|
|
204
|
+
if (getParentPid() === opts.watchdogPid) return false
|
|
205
|
+
if (interval) clearInterval(interval)
|
|
206
|
+
setTimeout(() => exit(1), exitDeadlineMs)
|
|
207
|
+
opts.onOrphaned()
|
|
208
|
+
return true
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (check()) return () => {}
|
|
212
|
+
interval = setInterval(check, opts.intervalMs ?? DEFAULT_ORPHAN_CHECK_INTERVAL_MS)
|
|
213
|
+
interval.unref()
|
|
214
|
+
return () => clearInterval(interval)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function spawnProcessWorker(cmd: string[]): WatchdogWorker {
|
|
218
|
+
const proc = Bun.spawn({
|
|
219
|
+
cmd,
|
|
220
|
+
stdin: 'ignore',
|
|
221
|
+
stdout: 'inherit',
|
|
222
|
+
stderr: 'inherit',
|
|
223
|
+
env: { ...process.env },
|
|
224
|
+
})
|
|
225
|
+
return {
|
|
226
|
+
pid: proc.pid,
|
|
227
|
+
exited: proc.exited.then(() => ({ code: proc.exitCode, signal: proc.signalCode })),
|
|
228
|
+
kill: (signal) => {
|
|
229
|
+
if (proc.exitCode === null && proc.signalCode === null) proc.kill(signal)
|
|
230
|
+
},
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function describeExit(exit: WatchdogWorkerExit): string {
|
|
235
|
+
if (exit.signal) return `signal ${exit.signal}`
|
|
236
|
+
return `code ${exit.code}`
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function sleep(ms: number): Promise<void> {
|
|
240
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
241
|
+
}
|