typeclaw 0.45.0 → 0.46.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 (147) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -2
  3. package/src/agent/git-nudge.ts +2 -1
  4. package/src/agent/index.ts +67 -25
  5. package/src/agent/live-subagents.ts +18 -0
  6. package/src/agent/loop-guard.ts +208 -10
  7. package/src/agent/multimodal/index.ts +1 -1
  8. package/src/agent/multimodal/look-at.ts +78 -44
  9. package/src/agent/multimodal/looker.ts +151 -39
  10. package/src/agent/network/response-body.ts +26 -0
  11. package/src/agent/network/safe-http.ts +198 -0
  12. package/src/agent/plugin-tools.ts +438 -162
  13. package/src/agent/provider-error.ts +62 -4
  14. package/src/agent/restart/index.ts +61 -40
  15. package/src/agent/restart-handoff/index.ts +73 -4
  16. package/src/agent/retry-same-ref.ts +66 -0
  17. package/src/agent/reviewer-bash-policy.ts +42 -1
  18. package/src/agent/session-origin.ts +2 -0
  19. package/src/agent/subagent-drain.ts +117 -7
  20. package/src/agent/subagents.ts +77 -18
  21. package/src/agent/throttle-circuit.ts +25 -5
  22. package/src/agent/tool-error-remediation.ts +72 -0
  23. package/src/agent/tool-file-safety.ts +1050 -0
  24. package/src/agent/tools/channel-fetch-attachment.ts +10 -3
  25. package/src/agent/tools/channel-reply.ts +15 -1
  26. package/src/agent/tools/channel-send.ts +17 -1
  27. package/src/agent/tools/ddg.ts +16 -2
  28. package/src/agent/tools/post-github-review.ts +199 -0
  29. package/src/agent/tools/spawn-subagent.ts +21 -18
  30. package/src/agent/tools/webfetch/fetch.ts +143 -183
  31. package/src/agent/tools/webfetch/tool.ts +2 -3
  32. package/src/agent/tools/websearch.ts +23 -2
  33. package/src/agent/tools-without-local-file-operands.ts +27 -0
  34. package/src/agent/turn-runner.ts +109 -15
  35. package/src/bundled-plugins/backup/index.ts +2 -1
  36. package/src/bundled-plugins/backup/runner.ts +2 -1
  37. package/src/bundled-plugins/doc-render/render.ts +93 -12
  38. package/src/bundled-plugins/doc-render/skills/typeclaw-render-pdf/SKILL.md +13 -13
  39. package/src/bundled-plugins/github-cli-auth/effective-approval.ts +16 -6
  40. package/src/bundled-plugins/github-cli-auth/gh-command.ts +555 -15
  41. package/src/bundled-plugins/github-cli-auth/index.ts +131 -135
  42. package/src/bundled-plugins/github-cli-auth/review-checkout.ts +138 -0
  43. package/src/bundled-plugins/guard/policies/uncommitted-changes.ts +2 -1
  44. package/src/bundled-plugins/memory/README.md +23 -11
  45. package/src/bundled-plugins/memory/append-tool.ts +15 -8
  46. package/src/bundled-plugins/memory/citations.ts +22 -9
  47. package/src/bundled-plugins/memory/dreaming.ts +13 -8
  48. package/src/bundled-plugins/memory/find-entry-tool.ts +4 -0
  49. package/src/bundled-plugins/memory/index.ts +0 -4
  50. package/src/bundled-plugins/memory/load-memory.ts +8 -3
  51. package/src/bundled-plugins/memory/provenance-index.ts +717 -0
  52. package/src/bundled-plugins/memory/provenance-sanitize.ts +40 -0
  53. package/src/bundled-plugins/memory/references/store-reference-tool.ts +4 -0
  54. package/src/bundled-plugins/memory/search-tool.ts +163 -60
  55. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  56. package/src/bundled-plugins/memory/vector/hybrid.ts +126 -13
  57. package/src/bundled-plugins/researcher/write-report.ts +6 -3
  58. package/src/bundled-plugins/reviewer/index.ts +2 -2
  59. package/src/bundled-plugins/reviewer/reviewer.ts +10 -3
  60. package/src/bundled-plugins/reviewer/skills/code-review.ts +24 -9
  61. package/src/bundled-plugins/scout/scout.ts +8 -0
  62. package/src/bundled-plugins/security/index.ts +4 -4
  63. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +1 -0
  64. package/src/bundled-plugins/security/policies/private-surface-read.ts +220 -29
  65. package/src/bundled-plugins/security/policies/secret-exfil-read.ts +1 -0
  66. package/src/bundled-plugins/security/policies/ssrf.ts +29 -9
  67. package/src/channels/adapters/discord-bot-channel-resolver.ts +26 -14
  68. package/src/channels/adapters/discord-bot-thread-room.ts +44 -12
  69. package/src/channels/adapters/discord-bot.ts +91 -10
  70. package/src/channels/adapters/discord-channel-resolver.ts +91 -23
  71. package/src/channels/adapters/discord-id.ts +4 -0
  72. package/src/channels/adapters/discord-resolver-cache.ts +41 -0
  73. package/src/channels/adapters/discord.ts +57 -7
  74. package/src/channels/adapters/github/auth-app.ts +41 -12
  75. package/src/channels/adapters/github/index.ts +23 -11
  76. package/src/channels/adapters/github/review-submitter.ts +302 -0
  77. package/src/channels/adapters/kakaotalk-channel-resolver.ts +12 -1
  78. package/src/channels/adapters/kakaotalk-fetch-attachment.ts +8 -4
  79. package/src/channels/adapters/kakaotalk-typing.ts +127 -0
  80. package/src/channels/adapters/kakaotalk.ts +19 -0
  81. package/src/channels/adapters/slack-attachment-download.ts +71 -0
  82. package/src/channels/adapters/slack-bot.ts +19 -8
  83. package/src/channels/adapters/slack-classify.ts +21 -2
  84. package/src/channels/adapters/slack.ts +59 -7
  85. package/src/channels/adapters/telegram-bot.ts +18 -5
  86. package/src/channels/adapters/webex-bot.ts +8 -3
  87. package/src/channels/adapters/webex.ts +8 -3
  88. package/src/channels/fetch-attachment.ts +43 -0
  89. package/src/{bundled-plugins/github-cli-auth/approve-idempotency.ts → channels/github-review-verdict-coordinator.ts} +25 -1
  90. package/src/channels/github-token-bridge.ts +10 -3
  91. package/src/channels/manager.ts +9 -0
  92. package/src/channels/router.ts +568 -12
  93. package/src/channels/types.ts +28 -1
  94. package/src/cli/channel.ts +1 -1
  95. package/src/cli/init.ts +1 -1
  96. package/src/cli/model.ts +57 -7
  97. package/src/compose/stop.ts +17 -5
  98. package/src/config/config.ts +6 -6
  99. package/src/config/models-mutation.ts +13 -1
  100. package/src/config/providers.ts +176 -0
  101. package/src/container/start.ts +49 -10
  102. package/src/doctor/checks.ts +3 -1
  103. package/src/doctor/commit.ts +2 -1
  104. package/src/doctor/file-ownership.ts +52 -0
  105. package/src/dreams/git.ts +2 -1
  106. package/src/git/hookless.ts +5 -0
  107. package/src/git/reconcile-ignored.ts +18 -5
  108. package/src/git/secret-history.ts +371 -0
  109. package/src/git/system-commit.ts +12 -6
  110. package/src/init/dockerfile.ts +86 -5
  111. package/src/init/index.ts +17 -5
  112. package/src/init/models-dev.ts +4 -0
  113. package/src/init/validate-api-key.ts +2 -0
  114. package/src/mcp/tools.ts +16 -3
  115. package/src/permissions/builtins.ts +3 -1
  116. package/src/plugin/context.ts +2 -0
  117. package/src/plugin/index.ts +1 -0
  118. package/src/plugin/manager.ts +4 -0
  119. package/src/plugin/types.ts +21 -0
  120. package/src/run/index.ts +40 -0
  121. package/src/run/llm-fetch-observer.ts +188 -13
  122. package/src/sandbox/build.ts +37 -15
  123. package/src/sandbox/canonical-secrets.ts +18 -0
  124. package/src/sandbox/env-exposure.ts +80 -0
  125. package/src/sandbox/errors.ts +13 -6
  126. package/src/sandbox/hidden-paths.ts +102 -44
  127. package/src/sandbox/index.ts +22 -4
  128. package/src/sandbox/package-install.ts +3 -27
  129. package/src/sandbox/policy.ts +11 -25
  130. package/src/sandbox/privileged-runtime.ts +255 -0
  131. package/src/sandbox/session-tmp.ts +8 -2
  132. package/src/sandbox/writable-zones.ts +204 -140
  133. package/src/secrets/index.ts +8 -0
  134. package/src/secrets/refresh-provider-oauth.ts +128 -0
  135. package/src/server/command-runner.ts +48 -4
  136. package/src/server/index.ts +4 -0
  137. package/src/skills/typeclaw-channel-github/SKILL.md +49 -56
  138. package/src/skills/typeclaw-claude-code/SKILL.md +24 -60
  139. package/src/skills/typeclaw-claude-code/references/auth-flow.md +17 -158
  140. package/src/skills/typeclaw-codex-cli/SKILL.md +19 -49
  141. package/src/skills/typeclaw-codex-cli/references/auth-flow.md +17 -154
  142. package/src/skills/typeclaw-config/SKILL.md +5 -5
  143. package/src/skills/typeclaw-config/references/recommended-mounts.md +24 -24
  144. package/src/skills/typeclaw-github-contributing/SKILL.md +23 -26
  145. package/src/skills/typeclaw-memory/SKILL.md +9 -3
  146. package/src/skills/typeclaw-permissions/SKILL.md +19 -15
  147. package/typeclaw.schema.json +20 -0
@@ -0,0 +1,371 @@
1
+ import { isAbsolute, join } from 'node:path'
2
+
3
+ import { CANONICAL_AGENT_SECRET_DIRS, CANONICAL_AGENT_SECRET_FILES } from '@/sandbox/canonical-secrets'
4
+
5
+ import { hooklessGitArgs } from './hookless'
6
+ import { resolveAgentGit } from './resolve-agent-git'
7
+
8
+ type ScanResult = { ok: true } | { ok: false; paths: string[] }
9
+
10
+ const contaminatedCache = new Map<string, string[]>()
11
+
12
+ export class GitSecretHistoryError extends Error {
13
+ constructor(paths: readonly string[]) {
14
+ super(
15
+ [
16
+ `Model-driven Git/bash is disabled because Git metadata is contaminated or can conceal objects: ${paths.join(', ')}.`,
17
+ 'Assume credentials in the repository were exposed and rotate them first. Purge canonical secret paths and every refs/replace entry, expire all reflogs, and run Git garbage collection to remove unreachable objects before retrying.',
18
+ 'Suggested operator sequence: rewrite/purge the affected history and replacement refs, run `git reflog expire --expire=now --all`, then `git gc --prune=now`, and restart TypeClaw before retrying.',
19
+ 'TypeClaw inspects path names and object reachability without reading blob contents. Reachable, reflog-referenced, and unreachable commits are path-attributable via their root tree and block only when a canonical secret path is present; standalone dangling trees and blobs are not path-attributable and are ignored; a live ref, worktree HEAD, or pseudoref (including FETCH_HEAD) pointing at a non-commit still fails closed.',
20
+ ].join(' '),
21
+ )
22
+ this.name = 'GitSecretHistoryError'
23
+ }
24
+ }
25
+
26
+ export async function assertNoCanonicalSecretsInGit(agentDir: string): Promise<void> {
27
+ const contaminated = contaminatedCache.get(agentDir)
28
+ if (contaminated !== undefined) throw new GitSecretHistoryError(contaminated)
29
+
30
+ const scan = await scanCanonicalSecretsInGit(agentDir)
31
+ if (!scan.ok) {
32
+ contaminatedCache.set(agentDir, scan.paths)
33
+ throw new GitSecretHistoryError(scan.paths)
34
+ }
35
+ }
36
+
37
+ export async function scanCanonicalSecretsInGit(agentDir: string): Promise<ScanResult> {
38
+ const layout = resolveAgentGit(agentDir)
39
+ if (layout === null) return { ok: true }
40
+ const gitArgs = layout.gitArgs
41
+ const inside = await runGit(agentDir, gitArgs, ['rev-parse', '--is-inside-work-tree'])
42
+ if (inside.trim() !== 'true') throw new GitSecretHistoryError(['Git metadata did not resolve to a work tree'])
43
+
44
+ const replacements = await runGit(agentDir, gitArgs, ['for-each-ref', '--format=%(refname)', 'refs/replace'])
45
+ if (replacements.trim() !== '') return { ok: false, paths: ['replacement refs exist under refs/replace'] }
46
+
47
+ const nonCommitRoots = await scanNonCommitRefRoots(agentDir, gitArgs)
48
+ if (!nonCommitRoots.ok) return nonCommitRoots
49
+
50
+ const index = await runGit(agentDir, gitArgs, ['ls-files', '--cached', '-z'])
51
+ const reachablePaths = matchingCanonicalPaths(splitNul(index))
52
+
53
+ const objects = await runGit(agentDir, gitArgs, ['rev-list', '--objects', '--all', '--reflog'])
54
+ for (const line of objects.split('\n')) {
55
+ const separator = line.indexOf(' ')
56
+ if (separator >= 0) reachablePaths.push(...matchingCanonicalPaths([decodeGitPath(line.slice(separator + 1))]))
57
+ }
58
+
59
+ const unreachableMatches = await scanUnreachableObjects(agentDir, gitArgs)
60
+ if (!unreachableMatches.ok) return unreachableMatches
61
+
62
+ const matches = [...new Set([...reachablePaths, ...unreachableMatches.paths])].sort()
63
+ if (matches.length > 0) return { ok: false, paths: matches }
64
+
65
+ return { ok: true }
66
+ }
67
+
68
+ type UnreachableScan = { ok: false; paths: string[] } | { ok: true; paths: string[] }
69
+
70
+ // FETCH_HEAD and MERGE_HEAD are the two root refs `for-each-ref --include-root-refs` deliberately
71
+ // omits (Git classifies them as pseudorefs). They are filesystem-only and multi-line (one oid per
72
+ // line), so they are always read from their files directly rather than enumerated.
73
+ const GIT_MULTILINE_PSEUDOREFS = ['FETCH_HEAD', 'MERGE_HEAD'] as const
74
+
75
+ // Single-oid root-ref fallback for Git < 2.45, which lacks `for-each-ref --include-root-refs`. Not
76
+ // exhaustive by design — any all-caps `*_HEAD`-style root ref (e.g. a custom one) is caught by the
77
+ // flag on modern Git; this list only backstops the ones Git itself commonly writes on old versions.
78
+ const GIT_FALLBACK_ROOT_REFS = [
79
+ 'HEAD',
80
+ 'ORIG_HEAD',
81
+ 'CHERRY_PICK_HEAD',
82
+ 'REVERT_HEAD',
83
+ 'REBASE_HEAD',
84
+ 'BISECT_HEAD',
85
+ 'AUTO_MERGE',
86
+ ] as const
87
+
88
+ // A ref, worktree HEAD, or root ref/pseudoref pointing at (or a tag peeling to) a non-commit makes
89
+ // that tree/blob live, so neither fsck mode reports it and rev-list cannot root-anchor its path.
90
+ // Such a live root has no repository path prefix, so it fails closed exactly like an orphan tree.
91
+ // Ordinary commit-ish roots are handled by the reachable/reflog path scans.
92
+ async function scanNonCommitRefRoots(agentDir: string, gitArgs: readonly string[]): Promise<UnreachableScan> {
93
+ const listing = await runGit(agentDir, gitArgs, ['worktree', 'list', '--porcelain'])
94
+ const headRefs: string[] = []
95
+ const linkedWorktrees: string[] = []
96
+ for (const line of listing.split('\n')) {
97
+ if (line.startsWith('HEAD ')) headRefs.push(line.slice('HEAD '.length).trim())
98
+ else if (line.startsWith('worktree ')) {
99
+ const path = line.slice('worktree '.length).trim()
100
+ if (path !== '' && path !== agentDir) linkedWorktrees.push(path)
101
+ }
102
+ }
103
+ const refs = await runGit(agentDir, gitArgs, ['for-each-ref', '--format=%(objectname)'])
104
+ const rootRefOids = await collectRootRefOids(agentDir, gitArgs, linkedWorktrees)
105
+ const roots = [...new Set([...headRefs, ...refs.split('\n'), ...rootRefOids].map((oid) => oid.trim()))].filter(
106
+ (oid) => oid !== '' && !/^0+$/.test(oid),
107
+ )
108
+
109
+ for (const root of roots) {
110
+ // A pruning `git gc` deletes the object a reset/rebase left in a pseudoref (ORIG_HEAD, etc.) but
111
+ // never rewrites the ref, so it dangles at an oid that is now absent from the object DB. A gone
112
+ // object cannot conceal a secret, so skip it rather than failing the whole scan closed on the
113
+ // peel error. Only a root whose object still EXISTS and is a non-commit remains blocking.
114
+ if (!(await objectExists(agentDir, gitArgs, root))) continue
115
+ const target = await peelTag(agentDir, gitArgs, root)
116
+ if (target.type !== 'commit') return { ok: false, paths: ['unattributable dangling Git objects'] }
117
+ }
118
+ return { ok: true, paths: [] }
119
+ }
120
+
121
+ // Collects every oid pinned by a top-level root ref across the main worktree AND every linked one
122
+ // (each linked worktree owns its own root-ref files under `$GIT_COMMON_DIR/worktrees/<id>/`, so a
123
+ // non-commit pinned only there must be probed too). The main worktree keeps the caller's gitArgs so
124
+ // the .gitstore --git-dir/--work-tree layout still resolves (that layout reports no linked
125
+ // worktrees); linked worktrees are discovered from their own path.
126
+ //
127
+ // Enumeration is exhaustive rather than a fixed allowlist: `for-each-ref --include-root-refs` lists
128
+ // ALL root refs Git recognizes — including arbitrary `*_HEAD`-style ones like CUSTOM_HEAD — so no
129
+ // unlisted root ref can slip a non-commit past the guard. On Git < 2.45 (no flag) we fall back to
130
+ // probing the common single-oid root refs by name. FETCH_HEAD/MERGE_HEAD are always read from their
131
+ // files (multi-line; the flag omits them) via `rev-parse --git-path`. Absent refs are skipped.
132
+ async function collectRootRefOids(
133
+ agentDir: string,
134
+ gitArgs: readonly string[],
135
+ linkedWorktrees: readonly string[],
136
+ ): Promise<string[]> {
137
+ const probes: { cwd: string; args: readonly string[] }[] = [
138
+ { cwd: agentDir, args: gitArgs },
139
+ ...linkedWorktrees.map((path) => ({ cwd: path, args: [] as readonly string[] })),
140
+ ]
141
+ const oids: string[] = []
142
+ for (const probe of probes) {
143
+ const rootRefs = await tryGit(probe.cwd, probe.args, [
144
+ 'for-each-ref',
145
+ '--include-root-refs',
146
+ '--format=%(objectname)',
147
+ ])
148
+ if (rootRefs.ok) {
149
+ for (const line of rootRefs.stdout.split('\n')) oids.push(line.trim())
150
+ } else {
151
+ for (const name of GIT_FALLBACK_ROOT_REFS) {
152
+ const resolved = await tryGit(probe.cwd, probe.args, ['rev-parse', '--verify', '--quiet', name])
153
+ if (resolved.ok) oids.push(resolved.stdout.trim())
154
+ }
155
+ }
156
+ for (const name of GIT_MULTILINE_PSEUDOREFS) {
157
+ const path = (await runGit(probe.cwd, probe.args, ['rev-parse', '--git-path', name])).trim()
158
+ if (path === '') continue
159
+ const file = Bun.file(isAbsolute(path) ? path : join(probe.cwd, path))
160
+ if (!(await file.exists())) continue
161
+ for (const line of (await file.text()).split('\n')) {
162
+ const oid = line.split(/[\s\t]/, 1)[0]?.trim()
163
+ if (oid !== undefined && /^[0-9a-f]{40,64}$/.test(oid)) oids.push(oid)
164
+ }
165
+ }
166
+ }
167
+ return oids
168
+ }
169
+
170
+ // Only an unreachable commit is safely attributable: it records a repository root tree, so we walk
171
+ // it (and tags peeling to a commit) and report the actual canonical secret paths it carries.
172
+ //
173
+ // Standalone unreachable trees/blobs (and tags peeling only to them) are deliberately NOT blocked.
174
+ // A bare blob never stored its filename and an orphan tree lost the parent that carried its prefix,
175
+ // so their names cannot prove they sat under a canonical secret dir. Failing closed on them bricked
176
+ // all model-driven bash on benign debris that TypeClaw's own backup rebase and history rewrites
177
+ // leave behind and never garbage-collect. Accepted residual: a secret surviving ONLY as pathless
178
+ // objects (e.g. `git add -f secrets.json` then `git reset`, which leaves no reflog entry, or a
179
+ // committed secret whose every path-bearing commit has since vanished). While any path-bearing
180
+ // commit survives — reachable, reflog, or unreachable — the caller's `rev-list --all --reflog` scan
181
+ // and this unreachable-commit walk still report the path; `--no-reflogs` keeps this a superset that
182
+ // routes benign reflog-reachable commits through commit attribution instead of double-reporting.
183
+ async function scanUnreachableObjects(agentDir: string, gitArgs: readonly string[]): Promise<UnreachableScan> {
184
+ const fsck = await runGit(agentDir, gitArgs, ['fsck', '--unreachable', '--no-reflogs', '--no-progress'])
185
+ const unreachable = parseUnreachableObjects(fsck)
186
+ if (unreachable.length === 0) return { ok: true, paths: [] }
187
+
188
+ const commits = new Set<string>()
189
+ for (const object of unreachable) {
190
+ if (object.type === 'commit') commits.add(object.oid)
191
+ else if (object.type === 'tag') {
192
+ const target = await peelTag(agentDir, gitArgs, object.oid)
193
+ if (target.type === 'commit') commits.add(target.oid)
194
+ }
195
+ }
196
+
197
+ const attributed = new Set<string>()
198
+ const matches: string[] = []
199
+ for (const commit of commits) {
200
+ const rootTree = (await runGit(agentDir, gitArgs, ['rev-parse', `${commit}^{tree}`])).trim()
201
+ const entries = await runGit(agentDir, gitArgs, ['ls-tree', '-r', '-t', '-z', '--full-tree', rootTree])
202
+ attributed.add(rootTree)
203
+ matches.push(...collectTreeEntries(entries, attributed))
204
+ }
205
+
206
+ return matches.length > 0 ? { ok: false, paths: [...new Set(matches)].sort() } : { ok: true, paths: [] }
207
+ }
208
+
209
+ // `cat-file -e` exits 0 when the object is present and exactly 1 when it is absent. Any other exit
210
+ // code is an operational Git error (corruption, bad invocation), which must fail closed like every
211
+ // other scan step rather than being misread as a benign "object gone".
212
+ async function objectExists(agentDir: string, gitArgs: readonly string[], oid: string): Promise<boolean> {
213
+ const { exitCode, stderr } = await spawnGit(agentDir, gitArgs, ['cat-file', '-e', oid])
214
+ if (exitCode === 0) return true
215
+ if (exitCode === 1) return false
216
+ throw new GitSecretHistoryError([`Git metadata scan failed (cat-file): ${redactGitError(stderr)}`])
217
+ }
218
+
219
+ type PeeledTag = { type: 'commit' | 'tree' | 'blob' | 'unknown'; oid: string }
220
+
221
+ // `<tag>^{}` recursively peels tag-of-tag chains to the final non-tag object. A malformed or cyclic
222
+ // tag makes rev-parse/cat-file exit non-zero, which throws in runGit and fails the whole scan closed.
223
+ async function peelTag(agentDir: string, gitArgs: readonly string[], tag: string): Promise<PeeledTag> {
224
+ const oid = (await runGit(agentDir, gitArgs, ['rev-parse', `${tag}^{}`])).trim()
225
+ const type = (await runGit(agentDir, gitArgs, ['cat-file', '-t', oid])).trim()
226
+ if (type === 'commit' || type === 'tree' || type === 'blob') return { type, oid }
227
+ return { type: 'unknown', oid }
228
+ }
229
+
230
+ type UnreachableObject = { type: 'commit' | 'tree' | 'blob' | 'tag'; oid: string }
231
+
232
+ function parseUnreachableObjects(fsck: string): UnreachableObject[] {
233
+ const objects: UnreachableObject[] = []
234
+ for (const line of fsck.split('\n')) {
235
+ const match = /^unreachable (commit|tree|blob|tag) ([0-9a-f]{40,64})$/.exec(line.trim())
236
+ if (match) objects.push({ type: match[1] as UnreachableObject['type'], oid: match[2] as string })
237
+ }
238
+ return objects
239
+ }
240
+
241
+ function collectTreeEntries(nulSeparated: string, attributed: Set<string>): string[] {
242
+ const matches: string[] = []
243
+ for (const entry of splitNul(nulSeparated)) {
244
+ const tab = entry.indexOf('\t')
245
+ if (tab < 0) continue
246
+ const meta = entry.slice(0, tab).split(/\s+/)
247
+ const oid = meta[2]
248
+ const path = decodeGitPath(entry.slice(tab + 1))
249
+ if (oid !== undefined) attributed.add(oid)
250
+ if (matchesKnownRootPath(path)) matches.push(path)
251
+ }
252
+ return matches
253
+ }
254
+
255
+ function decodeGitPath(raw: string): string {
256
+ if (!raw.startsWith('"') || !raw.endsWith('"')) return raw
257
+ const bytes: number[] = []
258
+ const encoder = new TextEncoder()
259
+ const escaped = raw.slice(1, -1)
260
+ for (let index = 0; index < escaped.length; index += 1) {
261
+ const character = escaped[index] as string
262
+ if (character !== '\\') {
263
+ bytes.push(...encoder.encode(character))
264
+ continue
265
+ }
266
+ const next = escaped[++index]
267
+ if (next === undefined) return raw
268
+ const simple: Record<string, number> = {
269
+ a: 7,
270
+ b: 8,
271
+ t: 9,
272
+ n: 10,
273
+ v: 11,
274
+ f: 12,
275
+ r: 13,
276
+ '"': 34,
277
+ '\\': 92,
278
+ }
279
+ const simpleByte = simple[next]
280
+ if (simpleByte !== undefined) {
281
+ bytes.push(simpleByte)
282
+ continue
283
+ }
284
+ if (/[0-7]/.test(next)) {
285
+ let octal = next
286
+ while (octal.length < 3 && index + 1 < escaped.length && /[0-7]/.test(escaped[index + 1] as string)) {
287
+ octal += escaped[++index]
288
+ }
289
+ bytes.push(Number.parseInt(octal, 8))
290
+ continue
291
+ }
292
+ bytes.push(...encoder.encode(next))
293
+ }
294
+ return new TextDecoder().decode(Uint8Array.from(bytes))
295
+ }
296
+
297
+ export function resetGitSecretHistoryCacheForTests(): void {
298
+ contaminatedCache.clear()
299
+ }
300
+
301
+ function matchingCanonicalPaths(paths: readonly string[]): string[] {
302
+ return paths.filter((raw) => matchesKnownRootPath(normalizeGitPath(raw)))
303
+ }
304
+
305
+ function normalizeGitPath(raw: string): string {
306
+ return raw.replaceAll('\\', '/').replace(/^\.\//, '')
307
+ }
308
+
309
+ function matchesKnownRootPath(raw: string): boolean {
310
+ const path = normalizeGitPath(raw)
311
+ if (CANONICAL_AGENT_SECRET_FILES.some((secret) => path === secret)) return true
312
+ return CANONICAL_AGENT_SECRET_DIRS.some((dir) => path.startsWith(`${dir}/`))
313
+ }
314
+
315
+ function splitNul(value: string): string[] {
316
+ return value.split('\0').filter((entry) => entry !== '')
317
+ }
318
+
319
+ async function spawnGit(
320
+ agentDir: string,
321
+ gitArgs: readonly string[],
322
+ args: readonly string[],
323
+ ): Promise<{ stdout: string; stderr: string; exitCode: number }> {
324
+ const proc = Bun.spawn(['git', ...hooklessGitArgs(['-C', agentDir, ...gitArgs, ...args])], {
325
+ stdout: 'pipe',
326
+ stderr: 'pipe',
327
+ env: {
328
+ PATH: process.env.PATH ?? '/usr/local/bin:/usr/bin:/bin',
329
+ HOME: process.env.HOME ?? '/tmp',
330
+ GIT_TERMINAL_PROMPT: '0',
331
+ GIT_PAGER: 'cat',
332
+ GIT_CONFIG_GLOBAL: '/dev/null',
333
+ GIT_CONFIG_SYSTEM: '/dev/null',
334
+ GIT_NO_REPLACE_OBJECTS: '1',
335
+ // Keep the scan strictly local. GIT_NO_LAZY_FETCH stops a promisor/partial-clone fetch on
336
+ // Git >=2.45 (and backported maints), but is silently ignored on older Git; the empty
337
+ // GIT_ALLOW_PROTOCOL whitelist (a since-2.20 control) denies every transport, so even the
338
+ // internal lazy `git fetch` those versions still attempt fails closed before touching a remote.
339
+ GIT_NO_LAZY_FETCH: '1',
340
+ GIT_ALLOW_PROTOCOL: '',
341
+ },
342
+ })
343
+ const [stdout, stderr, exitCode] = await Promise.all([
344
+ new Response(proc.stdout).text(),
345
+ new Response(proc.stderr).text(),
346
+ proc.exited,
347
+ ])
348
+ return { stdout, stderr, exitCode }
349
+ }
350
+
351
+ async function runGit(agentDir: string, gitArgs: readonly string[], args: readonly string[]): Promise<string> {
352
+ const { stdout, stderr, exitCode } = await spawnGit(agentDir, gitArgs, args)
353
+ if (exitCode === 0) return stdout
354
+ throw new GitSecretHistoryError([`Git metadata scan failed (${args[0] ?? 'unknown'}): ${redactGitError(stderr)}`])
355
+ }
356
+
357
+ // Non-throwing variant for probes whose failure is a valid signal (an absent root ref, or an
358
+ // `--include-root-refs` flag unsupported on old Git) rather than a contamination error.
359
+ async function tryGit(
360
+ agentDir: string,
361
+ gitArgs: readonly string[],
362
+ args: readonly string[],
363
+ ): Promise<{ ok: true; stdout: string } | { ok: false }> {
364
+ const { stdout, exitCode } = await spawnGit(agentDir, gitArgs, args)
365
+ return exitCode === 0 ? { ok: true, stdout } : { ok: false }
366
+ }
367
+
368
+ function redactGitError(stderr: string): string {
369
+ const firstLine = stderr.split(/\r?\n/, 1)[0]?.trim()
370
+ return firstLine === undefined || firstLine === '' ? 'unknown Git error' : firstLine.slice(0, 200)
371
+ }
@@ -1,3 +1,4 @@
1
+ import { hooklessGitArgs } from './hookless'
1
2
  import { resolveAgentGit } from './resolve-agent-git'
2
3
 
3
4
  // Commits TypeClaw-owned tracked files (.gitignore, package.json,
@@ -27,7 +28,7 @@ export async function commitSystemFile(cwd: string, file: string | readonly stri
27
28
  if (!repo) return
28
29
 
29
30
  const status = bun.spawn({
30
- cmd: ['git', ...repo.gitArgs, 'status', '--porcelain', '--', ...files],
31
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'status', '--porcelain', '--', ...files])],
31
32
  cwd,
32
33
  stdout: 'pipe',
33
34
  stderr: 'pipe',
@@ -36,11 +37,16 @@ export async function commitSystemFile(cwd: string, file: string | readonly stri
36
37
  const dirty = (await new Response(status.stdout).text()).trim().length > 0
37
38
  if (!dirty) return
38
39
 
39
- const add = bun.spawn({ cmd: ['git', ...repo.gitArgs, 'add', '--', ...files], cwd, stdout: 'pipe', stderr: 'pipe' })
40
+ const add = bun.spawn({
41
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'add', '--', ...files])],
42
+ cwd,
43
+ stdout: 'pipe',
44
+ stderr: 'pipe',
45
+ })
40
46
  if ((await add.exited) !== 0) return
41
47
 
42
48
  const commit = bun.spawn({
43
- cmd: ['git', ...repo.gitArgs, 'commit', '-m', message, '--only', '--', ...files],
49
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'commit', '-m', message, '--only', '--', ...files])],
44
50
  cwd,
45
51
  stdout: 'pipe',
46
52
  stderr: 'pipe',
@@ -73,7 +79,7 @@ export function commitSystemFileSync(cwd: string, file: string | readonly string
73
79
  if (!repo) return
74
80
 
75
81
  const status = bun.spawnSync({
76
- cmd: ['git', ...repo.gitArgs, 'status', '--porcelain', '--', ...files],
82
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'status', '--porcelain', '--', ...files])],
77
83
  cwd,
78
84
  stdout: 'pipe',
79
85
  stderr: 'pipe',
@@ -82,7 +88,7 @@ export function commitSystemFileSync(cwd: string, file: string | readonly string
82
88
  if (new TextDecoder().decode(status.stdout).trim().length === 0) return
83
89
 
84
90
  const add = bun.spawnSync({
85
- cmd: ['git', ...repo.gitArgs, 'add', '--', ...files],
91
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'add', '--', ...files])],
86
92
  cwd,
87
93
  stdout: 'pipe',
88
94
  stderr: 'pipe',
@@ -90,7 +96,7 @@ export function commitSystemFileSync(cwd: string, file: string | readonly string
90
96
  if (add.exitCode !== 0) return
91
97
 
92
98
  bun.spawnSync({
93
- cmd: ['git', ...repo.gitArgs, 'commit', '-m', message, '--only', '--', ...files],
99
+ cmd: ['git', ...hooklessGitArgs([...repo.gitArgs, 'commit', '-m', message, '--only', '--', ...files])],
94
100
  cwd,
95
101
  stdout: 'pipe',
96
102
  stderr: 'pipe',
@@ -416,8 +416,8 @@ link_persistent_home_files() {
416
416
  }
417
417
 
418
418
  # link_configured_symlinks creates the operator's \`sandbox.symlinks\` at the real
419
- # container $HOME for the UNSANDBOXED (trusted/owner) bash path; low-trust bash
420
- # gets an equivalent in-jail symlink from the per-tool bwrap builder instead
419
+ # container $HOME for runtime-owned processes. Model-driven bash runs in bwrap
420
+ # for every role and gets an equivalent in-jail symlink from the per-tool builder
421
421
  # (src/sandbox/build.ts). TYPECLAW_SANDBOX_SYMLINKS is base64-encoded JSON of
422
422
  # [{from,to}] (set by start.ts only when non-empty). The whole job is done in
423
423
  # \`bun -e\` rather than POSIX shell because \`from\`/\`to\` are arbitrary operator
@@ -477,6 +477,37 @@ link_configured_symlinks() {
477
477
  ' || true
478
478
  }
479
479
 
480
+ # seed_runtime_home copies the image-baked Claude Code / Codex CLI settings from
481
+ # the build-time HOME (/root) into the managed runtime HOME the non-root re-exec
482
+ # switches to. The Dockerfile writes ~/.claude.json, ~/.claude/settings.json,
483
+ # and ~/.codex/hooks.json against /root during \`docker build\`; once the runtime
484
+ # runs under HOME=$runtime_home those files would be invisible, silently
485
+ # disabling Claude onboarding state and both CLIs' completion hooks for
486
+ # claudeCode/codexCli users. link_persistent_home_files only links CREDENTIALS,
487
+ # not this config, so we seed it here.
488
+ #
489
+ # Runs in the root setup phase (HOME is still /root), BEFORE the chown so the
490
+ # copies inherit the host ownership. \`cp -n\` never clobbers a file the operator
491
+ # already persisted into the runtime home (the bind-mounted persist root survives
492
+ # restarts), so seeding is idempotent and one-way: image defaults fill only what
493
+ # is missing.
494
+ seed_runtime_home() {
495
+ _src="\${1:-/root}"
496
+ _dst="$2"
497
+ [ "$_src" = "$_dst" ] && return 0
498
+ mkdir -p "$_dst"
499
+ [ -f "$_src/.claude.json" ] && cp -n "$_src/.claude.json" "$_dst/.claude.json" 2>/dev/null || true
500
+ if [ -f "$_src/.claude/settings.json" ]; then
501
+ mkdir -p "$_dst/.claude"
502
+ cp -n "$_src/.claude/settings.json" "$_dst/.claude/settings.json" 2>/dev/null || true
503
+ fi
504
+ if [ -f "$_src/.codex/hooks.json" ]; then
505
+ mkdir -p "$_dst/.codex"
506
+ cp -n "$_src/.codex/hooks.json" "$_dst/.codex/hooks.json" 2>/dev/null || true
507
+ fi
508
+ unset _src _dst
509
+ }
510
+
480
511
  start_xvfb() {
481
512
  if ! command -v Xvfb >/dev/null 2>&1; then
482
513
  return 0
@@ -491,9 +522,23 @@ start_xvfb() {
491
522
  rm -f "$xvfb_status"
492
523
  (
493
524
  set +e
494
- setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin \\
495
- -- Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
496
- >/dev/null 2>&1
525
+ # The nested setpriv strips NET_ADMIN so Xvfb never inherits it (invariant
526
+ # 1). But when we've ALREADY re-exec'd into the non-root runtime phase
527
+ # (TYPECLAW_ENTRYPOINT_RUNTIME=1), the outer handoff emptied the bounding
528
+ # set and dropped CAP_SETPCAP — so a second \`setpriv --bounding-set\` here
529
+ # can no longer modify the bounding set and exits non-zero (127), which the
530
+ # liveness probe would misreport as "Xvfb exited immediately". There is also
531
+ # nothing left to strip: NET_ADMIN is already gone. So in the runtime phase
532
+ # launch Xvfb DIRECTLY; keep the nested drop only for the root fallback
533
+ # paths (where this shim is still PID 1 with the full capability set).
534
+ if [ "\${TYPECLAW_ENTRYPOINT_RUNTIME:-0}" = "1" ]; then
535
+ Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
536
+ >/dev/null 2>&1
537
+ else
538
+ setpriv --bounding-set -net_admin --inh-caps -net_admin --ambient-caps -net_admin \\
539
+ -- Xvfb :99 -screen 0 1920x1080x24 -ac +extension RANDR -nolisten tcp \\
540
+ >/dev/null 2>&1
541
+ fi
497
542
  printf '%s\\n' "$?" > "$xvfb_status"
498
543
  ) &
499
544
  xvfb_pid=$!
@@ -524,7 +569,35 @@ start_xvfb() {
524
569
  exit 1
525
570
  }
526
571
 
572
+ if [ "\${TYPECLAW_ENTRYPOINT_RUNTIME:-0}" = "1" ]; then
573
+ link_persistent_home_files
574
+ link_configured_symlinks
575
+ start_xvfb
576
+ exec bun run ${TYPECLAW_CLI_ENTRY} "$@"
577
+ fi
578
+
579
+ persist_root="\${TYPECLAW_PERSIST_HOME_ROOT:-/agent/.typeclaw/home}"
580
+ runtime_home="$persist_root/runtime"
581
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
582
+ mkdir -p "$runtime_home"
583
+ seed_runtime_home "$HOME" "$runtime_home"
584
+ chown -R "$TYPECLAW_HOST_UID:$TYPECLAW_HOST_GID" "$persist_root"
585
+ fi
586
+
527
587
  if [ "\${TYPECLAW_NETWORK_BLOCK_INTERNAL:-0}" != "1" ]; then
588
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
589
+ # NOTE: no \`--reset-env\`. setpriv --reset-env clears every non-terminal
590
+ # variable, which would wipe the runtime's own configuration: --env-file
591
+ # values (provider credentials, etc.) plus TYPECLAW_SANDBOX_SYMLINKS,
592
+ # TYPECLAW_TUI_TOKEN, TYPECLAW_CONTAINER_NAME, TYPECLAW_HOSTD_URL,
593
+ # TYPECLAW_MODEL_CACHE, and TZ — all injected by start.ts and read after the
594
+ # re-exec. We only need to OVERRIDE two variables for the runtime phase, and
595
+ # the trailing \`env HOME=… TYPECLAW_ENTRYPOINT_RUNTIME=1\` does exactly that
596
+ # while forwarding everything else unchanged.
597
+ exec setpriv --reuid="$TYPECLAW_HOST_UID" --regid="$TYPECLAW_HOST_GID" --clear-groups \\
598
+ --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
599
+ -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
600
+ fi
528
601
  link_persistent_home_files
529
602
  link_configured_symlinks
530
603
  start_xvfb
@@ -572,6 +645,14 @@ ip6tables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
572
645
  ip6tables -A OUTPUT -o lo -j ACCEPT
573
646
  ${ipv6Rules.join('\n')}
574
647
 
648
+ if [ -n "\${TYPECLAW_HOST_UID:-}" ] && [ -n "\${TYPECLAW_HOST_GID:-}" ]; then
649
+ # No \`--reset-env\` — see the network-off handoff above: it would strip the
650
+ # --env-file values and the TypeClaw-injected runtime variables. The trailing
651
+ # \`env\` overrides only HOME + TYPECLAW_ENTRYPOINT_RUNTIME and forwards the rest.
652
+ exec setpriv --reuid="$TYPECLAW_HOST_UID" --regid="$TYPECLAW_HOST_GID" --clear-groups \\
653
+ --bounding-set=-all --inh-caps=-all --ambient-caps=-all \\
654
+ -- env HOME="$runtime_home" TYPECLAW_ENTRYPOINT_RUNTIME=1 "$0" "$@"
655
+ fi
575
656
  link_persistent_home_files
576
657
  link_configured_symlinks
577
658
  start_xvfb
package/src/init/index.ts CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  type StartResult,
28
28
  stop,
29
29
  } from '@/container'
30
+ import { hooklessGitArgs } from '@/git/hookless'
30
31
  import { commitSystemFile } from '@/git/system-commit'
31
32
  import { createSecretsStoreForAgent, type Channels, type Secret, SecretsBackend } from '@/secrets'
32
33
  import { hostLocaleIsCjk } from '@/shared/host-locale'
@@ -653,7 +654,12 @@ export async function isHatched(dir: string): Promise<boolean> {
653
654
  const bun = (globalThis as { Bun?: { spawn: typeof Bun.spawn } }).Bun
654
655
  if (!bun) return false
655
656
  try {
656
- const proc = bun.spawn({ cmd: ['git', 'log', '--format=%s'], cwd: dir, stdout: 'pipe', stderr: 'pipe' })
657
+ const proc = bun.spawn({
658
+ cmd: ['git', ...hooklessGitArgs(['log', '--format=%s'])],
659
+ cwd: dir,
660
+ stdout: 'pipe',
661
+ stderr: 'pipe',
662
+ })
657
663
  if ((await proc.exited) !== 0) return false
658
664
  const subjects = (await new Response(proc.stdout).text()).split('\n')
659
665
  return subjects.includes(HATCHED_COMMIT_SUBJECT)
@@ -858,7 +864,7 @@ export async function initGitRepo(cwd: string): Promise<GitInitResult> {
858
864
  try {
859
865
  if (hasGit) {
860
866
  const head = bun.spawn({
861
- cmd: ['git', 'rev-parse', '--verify', 'HEAD'],
867
+ cmd: ['git', ...hooklessGitArgs(['rev-parse', '--verify', 'HEAD'])],
862
868
  cwd,
863
869
  env,
864
870
  stdout: 'pipe',
@@ -866,21 +872,27 @@ export async function initGitRepo(cwd: string): Promise<GitInitResult> {
866
872
  })
867
873
  if ((await head.exited) === 0) return { ok: true, skipped: true }
868
874
  } else {
869
- const init = bun.spawn({ cmd: ['git', 'init', '-b', 'main'], cwd, env, stdout: 'pipe', stderr: 'pipe' })
875
+ const init = bun.spawn({
876
+ cmd: ['git', ...hooklessGitArgs(['init', '-b', 'main'])],
877
+ cwd,
878
+ env,
879
+ stdout: 'pipe',
880
+ stderr: 'pipe',
881
+ })
870
882
  if ((await init.exited) !== 0) {
871
883
  const stderr = await new Response(init.stderr).text()
872
884
  return { ok: false, reason: `git init failed: ${stderr.trim() || 'no stderr'}` }
873
885
  }
874
886
  }
875
887
 
876
- const add = bun.spawn({ cmd: ['git', 'add', '.'], cwd, env, stdout: 'pipe', stderr: 'pipe' })
888
+ const add = bun.spawn({ cmd: ['git', ...hooklessGitArgs(['add', '.'])], cwd, env, stdout: 'pipe', stderr: 'pipe' })
877
889
  if ((await add.exited) !== 0) {
878
890
  const stderr = await new Response(add.stderr).text()
879
891
  return { ok: false, reason: `git add failed: ${stderr.trim() || 'no stderr'}` }
880
892
  }
881
893
 
882
894
  const commit = bun.spawn({
883
- cmd: ['git', 'commit', '-m', 'Initial commit 🥚'],
895
+ cmd: ['git', ...hooklessGitArgs(['commit', '-m', 'Initial commit 🥚'])],
884
896
  cwd,
885
897
  env,
886
898
  stdout: 'pipe',
@@ -32,6 +32,10 @@ const PROVIDER_TO_MODELS_DEV: Record<KnownProviderId, string> = {
32
32
  xai: 'xai',
33
33
  minimax: 'minimax',
34
34
  deepseek: 'deepseek',
35
+ // Upstage is not listed on models.dev (checked api.json — no `upstage` lab
36
+ // entry, only HuggingFace open-model ids). Mapping to `upstage` is harmless:
37
+ // the upstream lookup misses and the curated Solar entries surface anyway.
38
+ upstage: 'upstage',
35
39
  moonshot: 'moonshot',
36
40
  // moonshot-coding (Kimi Code subscription) is a billing surface, not a
37
41
  // separate model catalog. models.dev tracks the underlying Kimi model
@@ -10,6 +10,7 @@ const PROVIDER_PROBE: Partial<Record<KnownProviderId, { url: string; authHeader:
10
10
  xai: { url: 'https://api.x.ai/v1/models', authHeader: 'bearer' },
11
11
  minimax: { url: 'https://api.minimax.io/v1/models', authHeader: 'bearer' },
12
12
  deepseek: { url: 'https://api.deepseek.com/models', authHeader: 'bearer' },
13
+ upstage: { url: 'https://api.upstage.ai/v1/models', authHeader: 'bearer' },
13
14
  moonshot: { url: 'https://api.moonshot.ai/v1/models', authHeader: 'bearer' },
14
15
  'moonshot-coding': { url: 'https://api.kimi.com/coding/v1/models', authHeader: 'bearer' },
15
16
  }
@@ -167,6 +168,7 @@ export const API_KEY_DASHBOARD_URL: Partial<Record<KnownProviderId, string>> = {
167
168
  xai: 'https://console.x.ai',
168
169
  minimax: 'https://platform.minimax.io/user-center/basic-information/interface-key',
169
170
  deepseek: 'https://platform.deepseek.com/api_keys',
171
+ upstage: 'https://console.upstage.ai/api-keys',
170
172
  moonshot: 'https://platform.moonshot.ai/console/api-keys',
171
173
  'moonshot-coding': 'https://www.kimi.com/code/console',
172
174
  }