typeclaw 0.48.13 → 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 +5 -5
- package/scripts/dump-system-prompt.ts +52 -110
- package/src/agent/index.ts +144 -72
- package/src/agent/live-sessions.ts +2 -2
- package/src/agent/live-subagents.ts +1 -0
- package/src/agent/plugin-tools.ts +53 -11
- package/src/agent/prompt-cache-key.ts +42 -0
- package/src/agent/provider-error.ts +179 -10
- 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 +13 -1
- package/src/agent/system-prompt.ts +68 -202
- package/src/agent/todo/continuation-policy.ts +12 -4
- package/src/agent/tool-file-safety.ts +113 -14
- package/src/agent/tool-not-found-nudge.ts +1 -1
- package/src/agent/tools/channel-history-render.ts +2 -1
- package/src/agent/tools/channel-reply.ts +23 -5
- package/src/agent/tools/channel-send.ts +49 -7
- package/src/agent/tools/curl-impersonate.ts +11 -2
- package/src/agent/tools/post-github-review.ts +77 -15
- package/src/agent/tools/skip-response.ts +2 -2
- package/src/agent/tools/spawn-subagent.ts +163 -12
- package/src/agent/tools/subagent-output.ts +8 -0
- 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 +72 -12
- 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 +2539 -615
- 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/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 +10 -3
- package/src/config/providers.ts +12 -9
- 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 +2 -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/index.ts +83 -83
- 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/server/command-runner.ts +6 -2
- package/src/server/index.ts +101 -40
- 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 +21 -21
- 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 +15 -15
- package/src/skills/typeclaw-skills/SKILL.md +19 -2
- package/src/skills/typeclaw-troubleshooting/SKILL.md +15 -2
- package/typeclaw.schema.json +12 -1
- 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/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
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { lstat, realpath } from 'node:fs/promises'
|
|
2
|
+
import { join, posix, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { createAndOpenAnchoredDirectory } from '@/agent/tool-file-safety'
|
|
5
|
+
import { config } from '@/config/config'
|
|
6
|
+
|
|
7
|
+
import { canMountRealProc, ensureBwrapAvailable, getProcBindSafetyVerdict } from './availability'
|
|
8
|
+
import { buildSandboxedCommand, type SandboxedCommand } from './build'
|
|
9
|
+
import {
|
|
10
|
+
CANONICAL_AGENT_SECRET_DIRS,
|
|
11
|
+
CANONICAL_AGENT_SECRET_FILES,
|
|
12
|
+
CANONICAL_HOME_SECRET_DIRS,
|
|
13
|
+
CANONICAL_HOME_SECRET_FILES,
|
|
14
|
+
} from './canonical-secrets'
|
|
15
|
+
import { verifyHiddenMaskTargets } from './hidden-paths'
|
|
16
|
+
import { DEFAULT_SANDBOX_ENV, type SandboxMount, type SandboxPolicy, type SandboxProcStrategy } from './policy'
|
|
17
|
+
import { formatCommand } from './quote'
|
|
18
|
+
import { CANONICAL_AGENT_RUNTIME_PRIVATE_FILES } from './runtime-private'
|
|
19
|
+
|
|
20
|
+
export const CODING_SOURCE = '/typeclaw-coding-source'
|
|
21
|
+
export const CODING_MASK_DIRS = [
|
|
22
|
+
...CANONICAL_AGENT_SECRET_DIRS,
|
|
23
|
+
// Repository-scoped Claude/Codex settings are executable project context,
|
|
24
|
+
// not the runtime HOME profiles (HOME is never mounted). Mask their auth files.
|
|
25
|
+
...CANONICAL_HOME_SECRET_DIRS.filter((dir) => dir !== '.claude' && dir !== '.codex'),
|
|
26
|
+
'.typeclaw',
|
|
27
|
+
] as const
|
|
28
|
+
export const CODING_MASK_FILES = [
|
|
29
|
+
...CANONICAL_AGENT_SECRET_FILES,
|
|
30
|
+
...CANONICAL_HOME_SECRET_FILES,
|
|
31
|
+
...CANONICAL_AGENT_RUNTIME_PRIVATE_FILES,
|
|
32
|
+
'.claude/.credentials.json',
|
|
33
|
+
'.codex/auth.json',
|
|
34
|
+
] as const
|
|
35
|
+
|
|
36
|
+
export function codingSourceMaskDirs(source: string, taskRoot: string): readonly string[] {
|
|
37
|
+
return source === resolve(taskRoot, '../../..') ? [...CODING_MASK_DIRS, 'workspace'] : CODING_MASK_DIRS
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type CodingSandboxOptions = {
|
|
41
|
+
readonly source: string
|
|
42
|
+
readonly taskRoot: string
|
|
43
|
+
readonly mode: 'code' | 'plan' | 'snapshot'
|
|
44
|
+
readonly cwd: string
|
|
45
|
+
readonly env?: Record<string, string>
|
|
46
|
+
readonly engine?: 'claude' | 'codex' | 'omo'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function assertCodingDirectory(path: string): Promise<void> {
|
|
50
|
+
const absolute = resolve(path)
|
|
51
|
+
if ((await realpath(absolute)) !== absolute || !(await lstat(absolute)).isDirectory()) {
|
|
52
|
+
throw new Error('Coding task directory is a symlink or changed identity.')
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function codingSandboxPolicy(
|
|
57
|
+
options: CodingSandboxOptions,
|
|
58
|
+
proc: SandboxProcStrategy,
|
|
59
|
+
): Promise<SandboxPolicy> {
|
|
60
|
+
await assertCodingDirectory(options.source)
|
|
61
|
+
await assertCodingDirectory(options.taskRoot)
|
|
62
|
+
// Keep the canonical alias checks: unmounting the write directory must not
|
|
63
|
+
// admit a pre-existing credential hardlink into otherwise visible Git/task data.
|
|
64
|
+
const dirs: string[] = []
|
|
65
|
+
const files: string[] = []
|
|
66
|
+
for (const name of new Set([...codingSourceMaskDirs(options.source, options.taskRoot), ...CODING_MASK_FILES])) {
|
|
67
|
+
const path = join(options.source, name)
|
|
68
|
+
const stat = await lstat(path).catch((error: unknown) => {
|
|
69
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return undefined
|
|
70
|
+
throw error
|
|
71
|
+
})
|
|
72
|
+
if (!stat) continue
|
|
73
|
+
if (stat.isSymbolicLink() || (!stat.isDirectory() && (!stat.isFile() || stat.nlink !== 1))) {
|
|
74
|
+
throw new Error('Source has an unsafe canonical mask target.')
|
|
75
|
+
}
|
|
76
|
+
if (stat.isDirectory()) dirs.push(path)
|
|
77
|
+
else files.push(path)
|
|
78
|
+
}
|
|
79
|
+
await verifyHiddenMaskTargets({ dirs, files })
|
|
80
|
+
const sourceGit = join(options.source, '.git')
|
|
81
|
+
if (options.mode === 'snapshot') {
|
|
82
|
+
await assertCodingDirectory(sourceGit)
|
|
83
|
+
}
|
|
84
|
+
const configMasks: string[] = []
|
|
85
|
+
if (options.engine === 'omo') {
|
|
86
|
+
// Only these ancestors are visible through the task bind. Other host
|
|
87
|
+
// ancestors (including the real HOME) are absent in the empty sandbox root.
|
|
88
|
+
for (const directory of [options.taskRoot, join(options.taskRoot, 'worktree')]) {
|
|
89
|
+
const omo = join(directory, '.omo')
|
|
90
|
+
const stat = await lstat(omo).catch((error: unknown) => {
|
|
91
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return undefined
|
|
92
|
+
throw error
|
|
93
|
+
})
|
|
94
|
+
if (!stat) continue
|
|
95
|
+
await assertCodingDirectory(omo)
|
|
96
|
+
for (const name of ['omo.json', 'omo.jsonc']) {
|
|
97
|
+
const file = join(omo, name)
|
|
98
|
+
const target = await lstat(file).catch((error: unknown) => {
|
|
99
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return undefined
|
|
100
|
+
throw error
|
|
101
|
+
})
|
|
102
|
+
if (!target) continue
|
|
103
|
+
if (!target.isFile() || target.nlink !== 1) throw new Error('Unsafe OmO project configuration operand.')
|
|
104
|
+
configMasks.push(file)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
cwd: options.cwd,
|
|
110
|
+
proc,
|
|
111
|
+
network: 'inherit',
|
|
112
|
+
env: { set: options.env ?? {} },
|
|
113
|
+
...(configMasks.length ? { masks: { files: configMasks } } : {}),
|
|
114
|
+
mounts: [
|
|
115
|
+
// Never bind the live source worktree: credential rotation can create
|
|
116
|
+
// arbitrary siblings and replace masked inodes there during an invocation.
|
|
117
|
+
// Only snapshot bootstrap needs source Git metadata, not its parent.
|
|
118
|
+
...(options.mode === 'snapshot'
|
|
119
|
+
? [{ type: 'ro-bind' as const, source: sourceGit, dest: posix.join(CODING_SOURCE, '.git') }]
|
|
120
|
+
: []),
|
|
121
|
+
{ type: 'bind', source: options.taskRoot, dest: options.taskRoot },
|
|
122
|
+
...(options.mode === 'plan'
|
|
123
|
+
? [
|
|
124
|
+
{ type: 'ro-bind' as const, source: join(options.taskRoot, 'git'), dest: join(options.taskRoot, 'git') },
|
|
125
|
+
{
|
|
126
|
+
type: 'ro-bind' as const,
|
|
127
|
+
source: join(options.taskRoot, 'worktree'),
|
|
128
|
+
dest: join(options.taskRoot, 'worktree'),
|
|
129
|
+
},
|
|
130
|
+
]
|
|
131
|
+
: []),
|
|
132
|
+
],
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function prepareCodingCommand(
|
|
137
|
+
argv: readonly string[],
|
|
138
|
+
options: CodingSandboxOptions,
|
|
139
|
+
): Promise<SandboxedCommand & { extraFiles: Array<number | 'ignore'>; cleanup(): Promise<void> }> {
|
|
140
|
+
await ensureBwrapAvailable()
|
|
141
|
+
fdBindSupport ??= supportsFdBinds()
|
|
142
|
+
if (!(await fdBindSupport)) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
'Coding delegation requires bwrap --bind-fd support. Rebuild the TypeClaw container image; ordinary bash is unaffected.',
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
const proc =
|
|
148
|
+
config.sandbox.realProc && (await canMountRealProc())
|
|
149
|
+
? 'real-proc'
|
|
150
|
+
: (await getProcBindSafetyVerdict()) === 'safe'
|
|
151
|
+
? 'proc-bind'
|
|
152
|
+
: undefined
|
|
153
|
+
if (!proc)
|
|
154
|
+
throw new Error(
|
|
155
|
+
'Coding delegation requires a verified safe /proc sandbox. Fix the container user-namespace policy and retry.',
|
|
156
|
+
)
|
|
157
|
+
const policy = await codingSandboxPolicy(options, proc)
|
|
158
|
+
const capabilities: Record<string, string> = {}
|
|
159
|
+
const settings = { ...options.env }
|
|
160
|
+
for (const name of ['ANTHROPIC_AUTH_TOKEN', 'TYPECLAW_CODING_AGENT_CAPABILITY']) {
|
|
161
|
+
const value = settings[name]
|
|
162
|
+
if (value) {
|
|
163
|
+
capabilities[name] = value
|
|
164
|
+
delete settings[name]
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Capabilities travel in the explicit environment, never visible process argv.
|
|
168
|
+
policy.env = { set: settings, inherit: Object.keys(capabilities) }
|
|
169
|
+
const opened: Awaited<ReturnType<typeof createAndOpenAnchoredDirectory>>[] = []
|
|
170
|
+
// buildSandboxedCommand reserves descriptors starting at 3 for file masks.
|
|
171
|
+
// Keep anchored bind descriptors above them: shell mask redirects must never
|
|
172
|
+
// replace the task/source directory handles before bwrap consumes them.
|
|
173
|
+
const extraFiles: Array<number | 'ignore'> = (policy.masks?.files ?? []).map(() => 'ignore')
|
|
174
|
+
try {
|
|
175
|
+
const mounts: SandboxMount[] = []
|
|
176
|
+
// Native FD binds verify inode identity after mounting AND consume the FD.
|
|
177
|
+
// A plain bind of /proc/self/fd/N does neither: it resolves the path and
|
|
178
|
+
// leaves a directory handle capable of bypassing masks in the final child.
|
|
179
|
+
for (const mount of policy.mounts ?? []) {
|
|
180
|
+
if (mount.type !== 'bind' && mount.type !== 'ro-bind') {
|
|
181
|
+
mounts.push(mount)
|
|
182
|
+
continue
|
|
183
|
+
}
|
|
184
|
+
const file = await createAndOpenAnchoredDirectory(mount.source, false)
|
|
185
|
+
opened.push(file)
|
|
186
|
+
mounts.push({
|
|
187
|
+
type: mount.type === 'bind' ? 'bind-fd' : 'ro-bind-fd',
|
|
188
|
+
fd: 3 + extraFiles.length,
|
|
189
|
+
dest: mount.dest,
|
|
190
|
+
})
|
|
191
|
+
extraFiles.push(file.fd)
|
|
192
|
+
}
|
|
193
|
+
policy.mounts = mounts
|
|
194
|
+
const built = buildSandboxedCommand(`exec ${formatCommand([...argv])}`, policy)
|
|
195
|
+
return {
|
|
196
|
+
...built,
|
|
197
|
+
spawnEnv: { ...built.spawnEnv, ...capabilities },
|
|
198
|
+
extraFiles,
|
|
199
|
+
cleanup: async () => {
|
|
200
|
+
await Promise.all(opened.map((file) => file.close()))
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
await Promise.all(opened.map((file) => file.close()))
|
|
205
|
+
throw error
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
let fdBindSupport: Promise<boolean> | undefined
|
|
210
|
+
async function supportsFdBinds(): Promise<boolean> {
|
|
211
|
+
const child = Bun.spawn(['bwrap', '--help'], { env: DEFAULT_SANDBOX_ENV, stdout: 'pipe', stderr: 'ignore' })
|
|
212
|
+
const help = await new Response(child.stdout).text()
|
|
213
|
+
return (await child.exited) === 0 && help.includes('--bind-fd') && help.includes('--ro-bind-fd')
|
|
214
|
+
}
|
package/src/sandbox/policy.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type SandboxMount =
|
|
2
2
|
| { type: 'ro-bind'; source: string; dest: string }
|
|
3
3
|
| { type: 'bind'; source: string; dest: string }
|
|
4
|
+
| { type: 'ro-bind-fd' | 'bind-fd'; fd: number; dest: string }
|
|
4
5
|
| { type: 'tmpfs'; dest: string }
|
|
5
6
|
| { type: 'dev'; dest: string }
|
|
6
7
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// Runtime-owned operational state is private to TypeClaw, but is not a
|
|
2
2
|
// credential. Keep this category separate from canonical-secrets so security
|
|
3
3
|
// diagnostics never mislabel incident metadata as secret material.
|
|
4
|
-
export const CANONICAL_AGENT_RUNTIME_PRIVATE_FILES = [
|
|
4
|
+
export const CANONICAL_AGENT_RUNTIME_PRIVATE_FILES = [
|
|
5
|
+
'.typeclaw/incidents.json',
|
|
6
|
+
'.typeclaw/coding-agent-tasks.json',
|
|
7
|
+
] as const
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import type { Credential, CredentialStore } from '@earendil-works/pi-ai'
|
|
4
|
+
import { ModelRuntime } from '@earendil-works/pi-coding-agent'
|
|
5
|
+
|
|
6
|
+
import { createSecretsStoreForAgent } from './storage'
|
|
7
|
+
|
|
8
|
+
/** Runtime-only value. Never serialize this into a tool receipt or child configuration. */
|
|
9
|
+
export type CodingAgentProvider = 'anthropic' | 'openai-codex'
|
|
10
|
+
export type CodingAgentAuth = {
|
|
11
|
+
readonly token: string
|
|
12
|
+
readonly secrets: readonly string[]
|
|
13
|
+
} & (
|
|
14
|
+
| { readonly provider?: 'anthropic'; readonly type: 'api_key' | 'oauth' }
|
|
15
|
+
| { readonly provider: 'openai-codex'; readonly type: 'oauth'; readonly accountId: string }
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
export class CodingAgentAuthError extends Error {
|
|
19
|
+
constructor(provider: CodingAgentProvider) {
|
|
20
|
+
super(
|
|
21
|
+
`${provider} authentication is unavailable. On the host, run \`typeclaw provider add ${provider}\`, then retry this task.`,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type CodingAgentAuthResolver = {
|
|
27
|
+
resolve(signal?: AbortSignal, rejected?: CodingAgentAuth, provider?: CodingAgentProvider): Promise<CodingAgentAuth>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function createCodingAgentAuthResolver(
|
|
31
|
+
agentDir: string,
|
|
32
|
+
dependencies?: { credentials: CredentialStore; runtime: ModelRuntime },
|
|
33
|
+
): Promise<CodingAgentAuthResolver> {
|
|
34
|
+
const credentials = dependencies?.credentials ?? createSecretsStoreForAgent(join(agentDir, 'secrets.json'))
|
|
35
|
+
const runtime =
|
|
36
|
+
dependencies?.runtime ?? (await ModelRuntime.create({ credentials, modelsPath: null, refreshOnCreate: false }))
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
async resolve(signal, rejected, provider = 'anthropic') {
|
|
40
|
+
try {
|
|
41
|
+
// Invalidate only the rejected generation, under the SAME lock used by pi.
|
|
42
|
+
// A concurrent refresh or host re-login must never be overwritten.
|
|
43
|
+
if (rejected?.provider === provider && rejected.type === 'oauth') {
|
|
44
|
+
await credentials.modify(
|
|
45
|
+
provider,
|
|
46
|
+
async (current) => {
|
|
47
|
+
if (current?.type === 'oauth' && current.access === rejected.token) {
|
|
48
|
+
return { ...current, expires: 0 }
|
|
49
|
+
}
|
|
50
|
+
return undefined
|
|
51
|
+
},
|
|
52
|
+
{ signal },
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
const stored = await credentials.read(provider, { signal })
|
|
56
|
+
// Subscription delegation must never fall back to separately billed API auth.
|
|
57
|
+
if (provider === 'openai-codex' && stored?.type !== 'oauth') throw new CodingAgentAuthError(provider)
|
|
58
|
+
const resolved = await runtime.getAuth(provider, {
|
|
59
|
+
signal,
|
|
60
|
+
// Persisted OAuth wins over ambient API keys, as on the main-agent path.
|
|
61
|
+
...(stored?.type === 'oauth' || dependencies ? { env: {} } : {}),
|
|
62
|
+
})
|
|
63
|
+
if (!resolved?.auth.apiKey) throw new CodingAgentAuthError(provider)
|
|
64
|
+
const token = resolved.auth.apiKey
|
|
65
|
+
const current = await credentials.read(provider, { signal })
|
|
66
|
+
const secrets = [token, ...credentialSecrets(stored), ...credentialSecrets(current)].filter(Boolean)
|
|
67
|
+
if (provider === 'openai-codex') {
|
|
68
|
+
// Bind the account to the exact resolved token generation, not a racing re-login.
|
|
69
|
+
const credential = [current, stored].find((value) => value?.type === 'oauth' && value.access === token)
|
|
70
|
+
if (
|
|
71
|
+
credential?.type !== 'oauth' ||
|
|
72
|
+
typeof credential.accountId !== 'string' ||
|
|
73
|
+
!/^[\x21-\x7e]+$/.test(credential.accountId)
|
|
74
|
+
)
|
|
75
|
+
throw new CodingAgentAuthError(provider)
|
|
76
|
+
return { provider, type: 'oauth', token, accountId: credential.accountId, secrets }
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
provider,
|
|
80
|
+
type: current?.type === 'oauth' ? 'oauth' : 'api_key',
|
|
81
|
+
token,
|
|
82
|
+
secrets,
|
|
83
|
+
}
|
|
84
|
+
} catch {
|
|
85
|
+
// OAuth transports can include token endpoint bodies in their errors.
|
|
86
|
+
throw new CodingAgentAuthError(provider)
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function credentialSecrets(credential: Credential | undefined): string[] {
|
|
93
|
+
if (credential?.type !== 'oauth') return []
|
|
94
|
+
return [credential.access, credential.refresh, credential.idToken, credential.id_token].filter(
|
|
95
|
+
(value): value is string => typeof value === 'string' && value.length > 0,
|
|
96
|
+
)
|
|
97
|
+
}
|
package/src/secrets/index.ts
CHANGED
|
@@ -24,22 +24,6 @@ export {
|
|
|
24
24
|
exportGithubCliStoreIfApplicable,
|
|
25
25
|
} from './export-github-cli-store'
|
|
26
26
|
|
|
27
|
-
export {
|
|
28
|
-
type ExportCodexAuthFileResult,
|
|
29
|
-
exportCodexAuthFileForAgent,
|
|
30
|
-
exportCodexAuthFileIfApplicable,
|
|
31
|
-
} from './export-codex-auth-file'
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
CLAUDE_CREDENTIALS_FILE_NAME,
|
|
35
|
-
CLAUDE_CREDENTIALS_RELATIVE_PATH,
|
|
36
|
-
CLAUDE_DEFAULT_CONFIG_DIR_NAME,
|
|
37
|
-
type ExportClaudeCredentialsFileResult,
|
|
38
|
-
exportClaudeCredentialsFileForAgent,
|
|
39
|
-
exportClaudeCredentialsFileIfApplicable,
|
|
40
|
-
resolveClaudeCredentialsPath,
|
|
41
|
-
} from './export-claude-credentials-file'
|
|
42
|
-
|
|
43
27
|
export {
|
|
44
28
|
type ProviderRefreshEntry,
|
|
45
29
|
type ProviderRefreshOutcome,
|
|
@@ -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
|
-
|
|
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)
|
package/src/server/index.ts
CHANGED
|
@@ -144,6 +144,12 @@ export type ServerOptions = {
|
|
|
144
144
|
// registry is fine — `/inspect` will report "session not live, replay
|
|
145
145
|
// from JSONL only" when it can't resolve the id.
|
|
146
146
|
liveSessionRegistry?: LiveSessionRegistry
|
|
147
|
+
// Test seam for holding an outcome write open and asserting drain ordering.
|
|
148
|
+
// Production always uses the real persistence function.
|
|
149
|
+
recordTurnOutcome?: typeof recordTurnOutcome
|
|
150
|
+
// Test seam: the idle/todo continuation driver invoked by the drain loop.
|
|
151
|
+
// Production always uses the real runIdleContinuation.
|
|
152
|
+
runIdleContinuation?: typeof runIdleContinuation
|
|
147
153
|
}
|
|
148
154
|
|
|
149
155
|
const consoleLogger: ServerLogger = {
|
|
@@ -215,6 +221,12 @@ type SessionState = {
|
|
|
215
221
|
// not re-target this session's lifecycle hooks.
|
|
216
222
|
runtimeSnapshot: PluginRuntimeState | null
|
|
217
223
|
unsubTurnOutcome: Unsubscribe | null
|
|
224
|
+
// Serializes outcome persistence and the idle continuation decision. The
|
|
225
|
+
// tail always fulfills so one disk failure cannot poison every later turn;
|
|
226
|
+
// its value separately reports whether the latest write succeeded before
|
|
227
|
+
// continuation reads state.
|
|
228
|
+
todoOutcomeWrite: Promise<boolean>
|
|
229
|
+
todoContinuation: { recordTurnOutcome: typeof recordTurnOutcome; runIdleContinuation: typeof runIdleContinuation }
|
|
218
230
|
// Latest turn's usage, captured from `message_end` by forwardSessionEvents and
|
|
219
231
|
// read at the `done` send site (which lives outside that subscriber). Reset at
|
|
220
232
|
// each turn start so a turn with no usage event sends a plain `done`.
|
|
@@ -297,6 +309,8 @@ export function createServer({
|
|
|
297
309
|
subagentCoalescer,
|
|
298
310
|
createSessionForSubagent,
|
|
299
311
|
liveSessionRegistry,
|
|
312
|
+
recordTurnOutcome: todoOutcomeWriter = recordTurnOutcome,
|
|
313
|
+
runIdleContinuation: todoIdleContinuation = runIdleContinuation,
|
|
300
314
|
}: ServerOptions) {
|
|
301
315
|
const sessionStates = new WeakMap<Ws, SessionState>()
|
|
302
316
|
const callIdToWs = new Map<string, AnyOwnerWs>()
|
|
@@ -563,6 +577,8 @@ export function createServer({
|
|
|
563
577
|
activeClaimCode: null,
|
|
564
578
|
runtimeSnapshot: runtimeSnapshot ?? null,
|
|
565
579
|
unsubTurnOutcome: null,
|
|
580
|
+
todoOutcomeWrite: Promise.resolve(true),
|
|
581
|
+
todoContinuation: { recordTurnOutcome: todoOutcomeWriter, runIdleContinuation: todoIdleContinuation },
|
|
566
582
|
lastUsage: null,
|
|
567
583
|
dispose,
|
|
568
584
|
}
|
|
@@ -573,7 +589,7 @@ export function createServer({
|
|
|
573
589
|
}
|
|
574
590
|
|
|
575
591
|
if (agentDir !== undefined) {
|
|
576
|
-
state.unsubTurnOutcome = subscribeTurnOutcome(
|
|
592
|
+
state.unsubTurnOutcome = subscribeTurnOutcome(state, agentDir, logger)
|
|
577
593
|
}
|
|
578
594
|
|
|
579
595
|
liveSessionRegistry?.register({
|
|
@@ -976,28 +992,56 @@ function forwardSessionEvents(ws: Ws, state: SessionState, logger: ServerLogger,
|
|
|
976
992
|
}
|
|
977
993
|
|
|
978
994
|
// Record each completed turn's stopReason for the todo-continuation guard.
|
|
979
|
-
//
|
|
980
|
-
//
|
|
981
|
-
//
|
|
982
|
-
//
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
sessionFileId: string,
|
|
988
|
-
logger: ServerLogger,
|
|
989
|
-
): Unsubscribe {
|
|
990
|
-
return session.subscribe((event) => {
|
|
995
|
+
// Writes are serialized per session on `state.todoOutcomeWrite`, so an older
|
|
996
|
+
// outcome can never land after a newer one. Pi emits every assistant
|
|
997
|
+
// `message_end` before `prompt()` resolves, so by idle the turn's writes are
|
|
998
|
+
// all enqueued; the idle path awaits the latest tail before deciding. An
|
|
999
|
+
// unrecognized stopReason classifies as 'unknown', which the idle path treats
|
|
1000
|
+
// as not-safe-to-continue (fail closed).
|
|
1001
|
+
function subscribeTurnOutcome(state: SessionState, agentDir: string, logger: ServerLogger): Unsubscribe {
|
|
1002
|
+
return state.session.subscribe((event) => {
|
|
991
1003
|
const usage = extractTurnUsage(event)
|
|
992
1004
|
if (usage === null) return
|
|
993
|
-
void
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1005
|
+
void enqueueTodoOutcomeWrite(
|
|
1006
|
+
state,
|
|
1007
|
+
{
|
|
1008
|
+
agentDir,
|
|
1009
|
+
origin: state.origin,
|
|
1010
|
+
turnId: state.sessionFileId,
|
|
1011
|
+
stopReason: usage.stopReason,
|
|
1012
|
+
...(usage.tokens !== undefined ? { tokens: usage.tokens } : {}),
|
|
1013
|
+
},
|
|
1014
|
+
logger,
|
|
1015
|
+
)
|
|
1016
|
+
})
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
function enqueueTodoOutcomeWrite(
|
|
1020
|
+
state: SessionState,
|
|
1021
|
+
args: Parameters<typeof recordTurnOutcome>[0],
|
|
1022
|
+
logger: ServerLogger,
|
|
1023
|
+
): Promise<boolean> {
|
|
1024
|
+
const write = state.todoOutcomeWrite.then(async () => {
|
|
1025
|
+
try {
|
|
1026
|
+
await state.todoContinuation.recordTurnOutcome(args)
|
|
1027
|
+
return true
|
|
1028
|
+
} catch (err) {
|
|
1029
|
+
logger.error(`[server] ${state.sessionFileId}: todo outcome capture failed: ${describeErr(err)}`)
|
|
1030
|
+
return false
|
|
1031
|
+
}
|
|
1000
1032
|
})
|
|
1033
|
+
state.todoOutcomeWrite = write
|
|
1034
|
+
return write
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// Resolves once the tail observed is still the latest, i.e. no write was
|
|
1038
|
+
// chained while awaiting. Returns whether that latest write succeeded.
|
|
1039
|
+
async function awaitLatestTodoOutcomeWrite(state: SessionState): Promise<boolean> {
|
|
1040
|
+
while (true) {
|
|
1041
|
+
const write = state.todoOutcomeWrite
|
|
1042
|
+
const succeeded = await write
|
|
1043
|
+
if (write === state.todoOutcomeWrite) return succeeded
|
|
1044
|
+
}
|
|
1001
1045
|
}
|
|
1002
1046
|
|
|
1003
1047
|
function forwardAssistantError(ws: Ws, message: unknown, logger: ServerLogger, sessionFileId: string): void {
|
|
@@ -1118,6 +1162,9 @@ async function drain(
|
|
|
1118
1162
|
send(ws, { type: 'prompt_started', messageId: item.streamMessageId, text: item.text })
|
|
1119
1163
|
|
|
1120
1164
|
if (agentDir !== undefined) {
|
|
1165
|
+
// recordTurnStart is a read-modify-write of the same state file; let
|
|
1166
|
+
// pending outcome writes land first so neither overwrites the other.
|
|
1167
|
+
await awaitLatestTodoOutcomeWrite(state)
|
|
1121
1168
|
await recordTurnStart({
|
|
1122
1169
|
agentDir,
|
|
1123
1170
|
origin: state.origin,
|
|
@@ -1174,9 +1221,16 @@ async function drain(
|
|
|
1174
1221
|
// the nested call would no-op and the continuation would stall until some
|
|
1175
1222
|
// unrelated event woke the loop again. Enqueuing here lets the same `while`
|
|
1176
1223
|
// consume it on the next iteration. Only fires when the queue is otherwise
|
|
1177
|
-
// empty so a real user turn is never preempted by a continuation
|
|
1224
|
+
// empty so a real user turn is never preempted by a continuation; the
|
|
1225
|
+
// queue is re-checked after awaiting this turn's outcome writes because a
|
|
1226
|
+
// user prompt can arrive meanwhile. A failed latest outcome write fails
|
|
1227
|
+
// closed: the stored outcome may be a stale earlier turn's.
|
|
1178
1228
|
if (state.drainQueue.length === 0) {
|
|
1179
|
-
await
|
|
1229
|
+
if (!(await awaitLatestTodoOutcomeWrite(state))) {
|
|
1230
|
+
logger.warn(`[server] ${state.sessionFileId}: skipping todo continuation after failed outcome write`)
|
|
1231
|
+
} else if (state.drainQueue.length === 0) {
|
|
1232
|
+
await maybeContinueTodos(state, agentDir, logger)
|
|
1233
|
+
}
|
|
1180
1234
|
}
|
|
1181
1235
|
}
|
|
1182
1236
|
} finally {
|
|
@@ -1188,30 +1242,37 @@ async function drain(
|
|
|
1188
1242
|
// prompt directly onto this session's drainQueue, tagged TODO_CONTINUATION_SOURCE
|
|
1189
1243
|
// so the next drain iteration treats it as an injected (non-user) turn that does
|
|
1190
1244
|
// not reset the episode budget. The enclosing drain loop consumes it; this never
|
|
1191
|
-
// calls drain() itself.
|
|
1245
|
+
// calls drain() itself. The decision joins the outcome-write chain so a
|
|
1246
|
+
// concurrent abort's outcome write is ordered after the decision's own state
|
|
1247
|
+
// write instead of racing it.
|
|
1192
1248
|
async function maybeContinueTodos(
|
|
1193
1249
|
state: SessionState,
|
|
1194
1250
|
agentDir: string | undefined,
|
|
1195
1251
|
logger: ServerLogger,
|
|
1196
1252
|
): Promise<void> {
|
|
1197
1253
|
if (agentDir === undefined) return
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1254
|
+
const decision = state.todoOutcomeWrite.then(async () => {
|
|
1255
|
+
try {
|
|
1256
|
+
await state.todoContinuation.runIdleContinuation({
|
|
1257
|
+
agentDir,
|
|
1258
|
+
origin: state.origin,
|
|
1259
|
+
deliver: (text) => {
|
|
1260
|
+
state.drainQueue.push({
|
|
1261
|
+
streamMessageId: `todo-continuation-${crypto.randomUUID()}` as StreamMessageId,
|
|
1262
|
+
text,
|
|
1263
|
+
delivery: 'queue',
|
|
1264
|
+
ts: Date.now(),
|
|
1265
|
+
source: TODO_CONTINUATION_SOURCE,
|
|
1266
|
+
})
|
|
1267
|
+
},
|
|
1268
|
+
})
|
|
1269
|
+
} catch (err) {
|
|
1270
|
+
logger.error(`[server] ${state.sessionFileId}: todo continuation failed: ${describeErr(err)}`)
|
|
1271
|
+
}
|
|
1272
|
+
return true
|
|
1273
|
+
})
|
|
1274
|
+
state.todoOutcomeWrite = decision
|
|
1275
|
+
await decision
|
|
1215
1276
|
}
|
|
1216
1277
|
|
|
1217
1278
|
function describeErr(err: unknown): string {
|