typeclaw 0.46.0 → 0.46.2

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 (75) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/src/agent/multimodal/looker.ts +11 -1
  4. package/src/agent/network/safe-http.ts +17 -8
  5. package/src/agent/plugin-tools.ts +1 -3
  6. package/src/agent/tool-file-safety.ts +414 -145
  7. package/src/agent/tools/webfetch/fetch.ts +22 -2
  8. package/src/agent/tools/webfetch/strategies/jq.ts +1 -2
  9. package/src/agent/tools/webfetch/strategies/readability.ts +39 -16
  10. package/src/agent/tools/webfetch/strategies/selector.ts +4 -2
  11. package/src/agent/tools/webfetch/strategies/snapshot.ts +3 -2
  12. package/src/agent/tools/webfetch/tool.ts +3 -3
  13. package/src/agent/tools-without-local-file-operands.ts +9 -6
  14. package/src/bundled-plugins/backup/README.md +23 -10
  15. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  16. package/src/bundled-plugins/backup/index.ts +104 -47
  17. package/src/bundled-plugins/backup/runner.ts +102 -5
  18. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  19. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  20. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  21. package/src/bundled-plugins/github-cli-auth/index.ts +110 -31
  22. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  23. package/src/bundled-plugins/memory/README.md +9 -0
  24. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  25. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  26. package/src/bundled-plugins/memory/vector/hybrid.ts +10 -2
  27. package/src/bundled-plugins/memory/vector/store.ts +21 -9
  28. package/src/bundled-plugins/researcher/write-report.ts +3 -3
  29. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +8 -3
  30. package/src/bundled-plugins/security/policies/private-surface-read.ts +616 -50
  31. package/src/bundled-plugins/security/policies/ssrf.ts +119 -34
  32. package/src/channels/adapters/github/inbound.ts +11 -0
  33. package/src/channels/adapters/github/index.ts +48 -54
  34. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  35. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  36. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  37. package/src/channels/adapters/slack-bot.ts +7 -2
  38. package/src/channels/adapters/slack-format.ts +245 -0
  39. package/src/channels/github-token-bridge.ts +2 -3
  40. package/src/cli/inspect.ts +16 -0
  41. package/src/container/docker-app.ts +42 -13
  42. package/src/container/shared.ts +19 -6
  43. package/src/container/start.ts +13 -0
  44. package/src/cron/bridge.ts +9 -2
  45. package/src/doctor/checks.ts +5 -2
  46. package/src/git/bound-gc-config.ts +82 -0
  47. package/src/hostd/spawn.ts +133 -47
  48. package/src/init/checkpoint.ts +4 -4
  49. package/src/init/dockerfile.ts +100 -88
  50. package/src/init/gitignore.ts +5 -6
  51. package/src/init/run-bun-install.ts +16 -2
  52. package/src/init/validate-api-key.ts +1 -0
  53. package/src/inspect/item-list.ts +12 -6
  54. package/src/inspect/replay.ts +21 -9
  55. package/src/inspect/session-list.ts +71 -33
  56. package/src/mcp/tools.ts +1 -0
  57. package/src/network/internal-destinations.ts +267 -0
  58. package/src/permissions/permissions.ts +93 -2
  59. package/src/plugin/types.ts +3 -2
  60. package/src/role-claim/code.ts +9 -5
  61. package/src/run/index.ts +45 -36
  62. package/src/sandbox/canonical-secrets.ts +6 -0
  63. package/src/sandbox/env-exposure.ts +18 -15
  64. package/src/sandbox/index.ts +1 -0
  65. package/src/sandbox/policy.ts +5 -3
  66. package/src/sandbox/privileged-runtime.ts +1 -0
  67. package/src/sandbox/symlinks.ts +1 -1
  68. package/src/secrets/export-claude-credentials-file.ts +13 -22
  69. package/src/secrets/export-codex-auth-file.ts +12 -28
  70. package/src/skills/typeclaw-config/SKILL.md +22 -6
  71. package/src/skills/typeclaw-permissions/SKILL.md +22 -9
  72. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  73. package/src/usage/scan.ts +11 -2
  74. package/typeclaw.schema.json +5 -0
  75. package/src/git/secret-history.ts +0 -371
@@ -0,0 +1,267 @@
1
+ import { isIP } from 'node:net'
2
+
3
+ export const MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV = 'TYPECLAW_MODEL_HTTP_ALLOW_INTERNAL_HOSTS'
4
+ export const MODEL_HTTP_ALLOW_INTERNAL_CIDRS_ENV = 'TYPECLAW_MODEL_HTTP_ALLOW_INTERNAL_CIDRS'
5
+ export const MAX_MODEL_HTTP_POLICY_ENTRIES = 64
6
+ const MAX_MODEL_HTTP_POLICY_VALUE_LENGTH = 8192
7
+
8
+ type ParsedAddress = { readonly family: 4 | 6; readonly value: bigint; readonly bits: 32 | 128 }
9
+ type ParsedCidr = ParsedAddress & { readonly prefix: number }
10
+
11
+ const RFC6052_PREFIX_LENGTHS = [32, 40, 48, 56, 64, 96] as const
12
+
13
+ export type InternalDestinationPolicy = {
14
+ readonly hostnames: readonly string[]
15
+ readonly cidrs: readonly ParsedCidr[]
16
+ }
17
+
18
+ export type InternalDestinationPolicyInput = {
19
+ allowInternalHosts?: readonly string[]
20
+ allowInternalCidrs?: readonly string[]
21
+ }
22
+
23
+ export function createInternalDestinationPolicy(input: InternalDestinationPolicyInput): InternalDestinationPolicy {
24
+ assertBounded(input.allowInternalHosts ?? [], MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV)
25
+ assertBounded(input.allowInternalCidrs ?? [], MODEL_HTTP_ALLOW_INTERNAL_CIDRS_ENV)
26
+ const hostnames = (input.allowInternalHosts ?? []).map((value, index) => {
27
+ const normalized = normalizeExactHostname(value)
28
+ if (normalized === undefined) throw invalidEntry(MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV, index, value)
29
+ return normalized
30
+ })
31
+ const cidrs = (input.allowInternalCidrs ?? []).map((value, index) => {
32
+ const parsed = parseCidr(value)
33
+ if (parsed === undefined) throw invalidEntry(MODEL_HTTP_ALLOW_INTERNAL_CIDRS_ENV, index, value)
34
+ return Object.freeze({ ...parsed, value: parsed.value & prefixMask(parsed.bits, parsed.prefix) })
35
+ })
36
+ return Object.freeze({ hostnames: Object.freeze([...new Set(hostnames)]), cidrs: Object.freeze(cidrs) })
37
+ }
38
+
39
+ export function buildInternalDestinationPolicyFromEnv(
40
+ env: Readonly<Record<string, string | undefined>>,
41
+ ): InternalDestinationPolicy {
42
+ return createInternalDestinationPolicy({
43
+ allowInternalHosts: parseEnvList(env[MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV], MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV),
44
+ allowInternalCidrs: parseEnvList(env[MODEL_HTTP_ALLOW_INTERNAL_CIDRS_ENV], MODEL_HTTP_ALLOW_INTERNAL_CIDRS_ENV),
45
+ })
46
+ }
47
+
48
+ const BOOT_INTERNAL_DESTINATION_POLICY = buildInternalDestinationPolicyFromEnv(process.env)
49
+
50
+ export function getBootInternalDestinationPolicy(): InternalDestinationPolicy {
51
+ return BOOT_INTERNAL_DESTINATION_POLICY
52
+ }
53
+
54
+ export function normalizeExactHostname(value: string): string | undefined {
55
+ const candidate = value.trim().replace(/\.$/, '')
56
+ if (candidate === '' || candidate.includes('[') || candidate.includes(']') || /[\s*:/\\@?#]/.test(candidate)) {
57
+ return undefined
58
+ }
59
+ let hostname: string
60
+ try {
61
+ hostname = new URL(`http://${candidate}/`).hostname.toLowerCase().replace(/\.$/, '')
62
+ } catch {
63
+ return undefined
64
+ }
65
+ if (hostname === '' || isIP(hostname) !== 0 || hostname.length > 253) return undefined
66
+ const labels = hostname.split('.')
67
+ if (
68
+ labels.some(
69
+ (label) =>
70
+ label.length === 0 ||
71
+ label.length > 63 ||
72
+ !/^[a-z0-9-]+$/.test(label) ||
73
+ label.startsWith('-') ||
74
+ label.endsWith('-'),
75
+ )
76
+ ) {
77
+ return undefined
78
+ }
79
+ return hostname
80
+ }
81
+
82
+ export function normalizeIpCidr(value: string): string | undefined {
83
+ if (value.includes('%')) return undefined
84
+ const parsed = parseCidr(value)
85
+ if (parsed === undefined) return undefined
86
+ const network = parsed.value & prefixMask(parsed.bits, parsed.prefix)
87
+ return `${formatAddress({ family: parsed.family, bits: parsed.bits, value: network })}/${parsed.prefix}`
88
+ }
89
+
90
+ export function hostnameMatchesPolicy(hostname: string, policy: InternalDestinationPolicy): boolean {
91
+ const normalized = normalizeExactHostname(hostname)
92
+ return normalized !== undefined && policy.hostnames.includes(normalized)
93
+ }
94
+
95
+ export function addressMatchesPolicy(address: string, policy: InternalDestinationPolicy): boolean {
96
+ const parsed = parseAddress(address)
97
+ if (parsed === undefined) return false
98
+ for (const cidr of policy.cidrs) {
99
+ if (matchesCidr(parsed, cidr)) return true
100
+ const embeddedIpv4 = ipv4FromEmbeddedIpv6(parsed)
101
+ if (embeddedIpv4 !== undefined && matchesCidr(embeddedIpv4, cidr)) return true
102
+ }
103
+ return false
104
+ }
105
+
106
+ export function extractEmbeddedIpv4Address(address: string): string | undefined {
107
+ const parsed = parseAddress(address)
108
+ if (parsed === undefined || parsed.family !== 6) return undefined
109
+ const ipv4 = ipv4FromEmbeddedIpv6(parsed)
110
+ return ipv4 === undefined ? undefined : formatAddress(ipv4)
111
+ }
112
+
113
+ export function extractRfc6052Ipv4Addresses(address: string): readonly string[] {
114
+ const parsed = parseAddress(address)
115
+ if (parsed === undefined || parsed.family !== 6 || extractAddressBits(parsed.value, 64, 8) !== 0n) return []
116
+ const candidates = RFC6052_PREFIX_LENGTHS.map((prefix) => {
117
+ if (prefix === 96) return extractAddressBits(parsed.value, 96, 32)
118
+ const leadingIpv4Bits = 64 - prefix
119
+ const trailingIpv4Bits = 32 - leadingIpv4Bits
120
+ return (
121
+ (extractAddressBits(parsed.value, prefix, leadingIpv4Bits) << BigInt(trailingIpv4Bits)) |
122
+ extractAddressBits(parsed.value, 72, trailingIpv4Bits)
123
+ )
124
+ })
125
+ return [...new Set(candidates.map((value) => formatAddress({ family: 4, bits: 32, value })))]
126
+ }
127
+
128
+ function parseEnvList(raw: string | undefined, name: string): string[] {
129
+ if (raw === undefined || raw === '') return []
130
+ if (raw.length > MAX_MODEL_HTTP_POLICY_VALUE_LENGTH) {
131
+ throw new Error(`${name} exceeds the ${MAX_MODEL_HTTP_POLICY_VALUE_LENGTH}-character limit`)
132
+ }
133
+ const entries = raw.split(',').map((entry) => entry.trim())
134
+ if (entries.some((entry) => entry === '')) throw new Error(`${name} contains an empty comma-separated entry`)
135
+ assertBounded(entries, name)
136
+ return entries
137
+ }
138
+
139
+ function assertBounded(entries: readonly string[], name: string): void {
140
+ if (entries.length > MAX_MODEL_HTTP_POLICY_ENTRIES) {
141
+ throw new Error(`${name} contains ${entries.length} entries; maximum is ${MAX_MODEL_HTTP_POLICY_ENTRIES}`)
142
+ }
143
+ }
144
+
145
+ function invalidEntry(name: string, index: number, value: string): Error {
146
+ const expected =
147
+ name === MODEL_HTTP_ALLOW_INTERNAL_HOSTS_ENV
148
+ ? 'an exact hostname without wildcards, a URL, or an IP literal'
149
+ : 'an IPv4 or IPv6 CIDR with an explicit prefix'
150
+ return new Error(`${name} entry ${index + 1} (${JSON.stringify(value)}) must be ${expected}`)
151
+ }
152
+
153
+ function parseCidr(value: string): ParsedCidr | undefined {
154
+ if (value.includes('%')) return undefined
155
+ const slash = value.indexOf('/')
156
+ if (slash <= 0 || slash !== value.lastIndexOf('/')) return undefined
157
+ const address = parseAddress(value.slice(0, slash))
158
+ const prefixText = value.slice(slash + 1)
159
+ if (address === undefined || !/^\d{1,3}$/.test(prefixText)) return undefined
160
+ const prefix = Number(prefixText)
161
+ if (!Number.isInteger(prefix) || prefix < 0 || prefix > address.bits) return undefined
162
+ return { ...address, prefix }
163
+ }
164
+
165
+ function parseAddress(rawAddress: string): ParsedAddress | undefined {
166
+ const address = rawAddress.toLowerCase().split('%')[0] ?? ''
167
+ const family = isIP(address)
168
+ if (family === 4) {
169
+ const octets = address.split('.').map(Number)
170
+ if (octets.length !== 4) return undefined
171
+ const value = octets.reduce((total, octet) => (total << 8n) | BigInt(octet), 0n)
172
+ return { family: 4, value, bits: 32 }
173
+ }
174
+ if (family !== 6) return undefined
175
+ const hextets = expandIpv6(address)
176
+ if (hextets === undefined) return undefined
177
+ const value = hextets.reduce((total, hextet) => (total << 16n) | BigInt(hextet), 0n)
178
+ return { family: 6, value, bits: 128 }
179
+ }
180
+
181
+ function expandIpv6(address: string): number[] | undefined {
182
+ let normalized = address
183
+ const dottedTail = normalized.match(/(?:^|:)(\d+\.\d+\.\d+\.\d+)$/)?.[1]
184
+ if (dottedTail !== undefined) {
185
+ const ipv4 = parseAddress(dottedTail)
186
+ if (ipv4 === undefined || ipv4.family !== 4) return undefined
187
+ const hi = Number((ipv4.value >> 16n) & 0xffffn).toString(16)
188
+ const lo = Number(ipv4.value & 0xffffn).toString(16)
189
+ normalized = `${normalized.slice(0, -dottedTail.length)}${hi}:${lo}`
190
+ }
191
+ const halves = normalized.split('::')
192
+ if (halves.length > 2) return undefined
193
+ const left = halves[0] === '' ? [] : halves[0]!.split(':')
194
+ const right = halves.length === 1 || halves[1] === '' ? [] : halves[1]!.split(':')
195
+ if ([...left, ...right].some((part) => !/^[0-9a-f]{1,4}$/.test(part))) return undefined
196
+ const missing = 8 - left.length - right.length
197
+ if ((halves.length === 1 && missing !== 0) || (halves.length === 2 && missing < 1)) return undefined
198
+ return [
199
+ ...left.map((part) => Number.parseInt(part, 16)),
200
+ ...Array(missing).fill(0),
201
+ ...right.map((part) => Number.parseInt(part, 16)),
202
+ ]
203
+ }
204
+
205
+ function prefixMask(bits: 32 | 128, prefix: number): bigint {
206
+ if (prefix === 0) return 0n
207
+ return ((1n << BigInt(prefix)) - 1n) << BigInt(bits - prefix)
208
+ }
209
+
210
+ function extractAddressBits(value: bigint, start: number, length: number): bigint {
211
+ if (length === 0) return 0n
212
+ return (value >> BigInt(128 - start - length)) & ((1n << BigInt(length)) - 1n)
213
+ }
214
+
215
+ function matchesCidr(address: ParsedAddress, cidr: ParsedCidr): boolean {
216
+ if (address.family !== cidr.family) return false
217
+ const mask = prefixMask(cidr.bits, cidr.prefix)
218
+ return (address.value & mask) === cidr.value
219
+ }
220
+
221
+ function ipv4FromEmbeddedIpv6(address: ParsedAddress): ParsedAddress | undefined {
222
+ if (address.family !== 6) return undefined
223
+ const high96 = address.value >> 32n
224
+ const isMapped = high96 === 0xffffn
225
+ const isTranslated = high96 === 0xffff0000n
226
+ const isCompatible = high96 === 0n && address.value > 1n
227
+ const nat64Prefix = 0x64ff9b0000000000000000n
228
+ const isWellKnownNat64 = high96 === nat64Prefix
229
+ if (isMapped || isTranslated || isCompatible || isWellKnownNat64) {
230
+ return { family: 4, bits: 32, value: address.value & 0xffffffffn }
231
+ }
232
+ const is6to4 = address.value >> 112n === 0x2002n
233
+ if (is6to4) return { family: 4, bits: 32, value: (address.value >> 80n) & 0xffffffffn }
234
+ return undefined
235
+ }
236
+
237
+ function formatAddress(address: ParsedAddress): string {
238
+ if (address.family === 4) {
239
+ return [24n, 16n, 8n, 0n].map((shift) => Number((address.value >> shift) & 0xffn)).join('.')
240
+ }
241
+ const hextets = Array.from({ length: 8 }, (_, index) => Number((address.value >> BigInt((7 - index) * 16)) & 0xffffn))
242
+ let bestStart = -1
243
+ let bestLength = 0
244
+ for (let start = 0; start < hextets.length; ) {
245
+ if (hextets[start] !== 0) {
246
+ start++
247
+ continue
248
+ }
249
+ let end = start
250
+ while (end < hextets.length && hextets[end] === 0) end++
251
+ if (end - start > bestLength && end - start >= 2) {
252
+ bestStart = start
253
+ bestLength = end - start
254
+ }
255
+ start = end
256
+ }
257
+ if (bestStart === -1) return hextets.map((part) => part.toString(16)).join(':')
258
+ const left = hextets
259
+ .slice(0, bestStart)
260
+ .map((part) => part.toString(16))
261
+ .join(':')
262
+ const right = hextets
263
+ .slice(bestStart + bestLength)
264
+ .map((part) => part.toString(16))
265
+ .join(':')
266
+ return `${left}::${right}`
267
+ }
@@ -129,6 +129,40 @@ function levenshtein(a: string, b: string): number {
129
129
  return prev[lb]!
130
130
  }
131
131
 
132
+ // Cap on the memoized role-resolution cache. Channel keys derive from
133
+ // externally-controlled coordinates, so the cache must be bounded — see the
134
+ // roleCache comment in createPermissionService.
135
+ export const ROLE_CACHE_MAX = 4096
136
+
137
+ export type BoundedCache<V> = {
138
+ get(key: string): V | undefined
139
+ set(key: string, value: V): void
140
+ clear(): void
141
+ readonly size: number
142
+ }
143
+
144
+ // Insertion-ordered cache with a hard capacity. On overflow it evicts the
145
+ // oldest inserted entry (JS Map preserves insertion order), so size never
146
+ // exceeds `max`. Extracted as a standalone factory so the bound is unit-testable
147
+ // in isolation — the in-service cache is otherwise unobservable.
148
+ export function createBoundedCache<V>(max: number): BoundedCache<V> {
149
+ const store = new Map<string, V>()
150
+ return {
151
+ get: (key) => store.get(key),
152
+ set(key, value) {
153
+ if (!store.has(key) && store.size >= max) {
154
+ const oldest = store.keys().next().value
155
+ if (oldest !== undefined) store.delete(oldest)
156
+ }
157
+ store.set(key, value)
158
+ },
159
+ clear: () => store.clear(),
160
+ get size() {
161
+ return store.size
162
+ },
163
+ }
164
+ }
165
+
132
166
  export function createPermissionService(opts: CreatePermissionServiceOptions = {}): PermissionService {
133
167
  let pluginPermissions = opts.pluginPermissions ?? []
134
168
  let ownerWildcardExclusions = opts.ownerWildcardExclusions ?? []
@@ -136,9 +170,24 @@ export function createPermissionService(opts: CreatePermissionServiceOptions = {
136
170
  let resolved = buildRoleTable(lastRoles, pluginPermissions, ownerWildcardExclusions)
137
171
  let byName = new Map(resolved.map((r) => [r.name, r]))
138
172
 
139
- function resolveRole(origin: SessionOrigin | undefined): string {
140
- if (origin === undefined) return 'guest'
173
+ // resolveRole is called several times per tool invocation (the bash sandbox
174
+ // alone resolves it up to 6x through resolveHiddenPaths) and once per channel
175
+ // admission, and each call re-scans the whole role table via matchesOrigin.
176
+ // The result is a pure function of (role table, resolved origin fields), so
177
+ // memoize it. The cache holds only the CURRENT role table's verdicts; both
178
+ // role-table rebuilds (replaceRoles / replacePluginPermissions) drop it, so a
179
+ // stale entry can never survive a config change and hand back a now-wrong
180
+ // (e.g. over-privileged) role. See roleCacheKey for exactly which fields the
181
+ // key covers — it must stay a superset of what computeRole reads.
182
+ //
183
+ // Bounded because channel cache keys derive from externally-controlled
184
+ // coordinates (workspace/chat/author): a stream of distinct GitHub issue or
185
+ // chat ids — even denied traffic that resolves to guest — would otherwise
186
+ // grow the map without limit until the next role-table rebuild. See
187
+ // createBoundedCache for the eviction policy.
188
+ const roleCache = createBoundedCache<string>(ROLE_CACHE_MAX)
141
189
 
190
+ function computeRole(origin: SessionOrigin): string {
142
191
  // Runtime-owned infrastructure (memory, backup) acts on the operator's
143
192
  // behalf over operator-owned state. It is constructed only by runtime/
144
193
  // bundled code — inbound channel/cron content cannot produce this kind —
@@ -168,6 +217,21 @@ export function createPermissionService(opts: CreatePermissionServiceOptions = {
168
217
  return 'guest'
169
218
  }
170
219
 
220
+ function resolveRole(origin: SessionOrigin | undefined): string {
221
+ if (origin === undefined) return 'guest'
222
+ const key = roleCacheKey(origin)
223
+ if (key === null) return computeRole(origin)
224
+ const cached = roleCache.get(key)
225
+ if (cached !== undefined) return cached
226
+ const role = computeRole(origin)
227
+ roleCache.set(key, role)
228
+ return role
229
+ }
230
+
231
+ function invalidateRoleCache(): void {
232
+ roleCache.clear()
233
+ }
234
+
171
235
  function roleSeverity(name: string): number | undefined {
172
236
  if (name === 'owner') return 4
173
237
  if (name === 'trusted') return 3
@@ -214,12 +278,14 @@ export function createPermissionService(opts: CreatePermissionServiceOptions = {
214
278
  lastRoles = roles ?? {}
215
279
  resolved = buildRoleTable(lastRoles, pluginPermissions, ownerWildcardExclusions)
216
280
  byName = new Map(resolved.map((r) => [r.name, r]))
281
+ invalidateRoleCache()
217
282
  },
218
283
  replacePluginPermissions(next) {
219
284
  pluginPermissions = next.pluginPermissions
220
285
  ownerWildcardExclusions = next.ownerWildcardExclusions
221
286
  resolved = buildRoleTable(lastRoles, pluginPermissions, ownerWildcardExclusions)
222
287
  byName = new Map(resolved.map((r) => [r.name, r]))
288
+ invalidateRoleCache()
223
289
  },
224
290
  }
225
291
  }
@@ -294,6 +360,31 @@ function resolveOne(
294
360
  }
295
361
  }
296
362
 
363
+ // Cache key for the memoized role resolution. It MUST enumerate exactly the
364
+ // origin fields that computeRole/matchesOrigin consult, and NOTHING that varies
365
+ // turn-to-turn without changing the verdict (participants, membership, thread,
366
+ // reactionRef, *Name display fields). Returning null opts an origin OUT of the
367
+ // cache — used for kinds where the answer is already O(1) (system/cron/subagent,
368
+ // which read only a role string off the origin) so there is no scan to save and
369
+ // no reason to risk an under-specified key. tui and channel are the scan-heavy
370
+ // kinds worth caching:
371
+ // - tui: verdict depends only on kind (matchesOrigin's tui rule ignores
372
+ // sessionId), so every tui origin shares one entry.
373
+ // - channel: verdict depends on adapter + workspace + chat + lastInboundAuthorId
374
+ // (the exact inputs matchesChannel/matchesBucket/matchesAuthor read).
375
+ // The `c` prefix + NUL separators keep channel keys unambiguous even if a field
376
+ // contained the delimiter of another.
377
+ function roleCacheKey(origin: SessionOrigin): string | null {
378
+ switch (origin.kind) {
379
+ case 'tui':
380
+ return 'tui'
381
+ case 'channel':
382
+ return ['c', origin.adapter, origin.workspace, origin.chat, origin.lastInboundAuthorId ?? ''].join('\u0000')
383
+ default:
384
+ return null
385
+ }
386
+ }
387
+
297
388
  function toMatchable(origin: SessionOrigin): Parameters<typeof matchesOrigin>[1] | null {
298
389
  switch (origin.kind) {
299
390
  case 'tui':
@@ -28,11 +28,12 @@ export type ToolContext = {
28
28
  export type ToolFileOperands = {
29
29
  input?: readonly string[]
30
30
  output?: readonly string[]
31
+ create?: readonly string[]
31
32
  destructive?: readonly string[]
32
33
  // Operand paths whose string values are control tokens or remote identifiers,
33
34
  // never a local file — the file-operand scanner skips them (like a first-party
34
- // prose operand) instead of rejecting a `word.ext`-shaped id or a value that
35
- // collides with an agent-root dir name. Scoped per operand path and survives
35
+ // prose operand) instead of rejecting a value that collides with an existing
36
+ // agent-root entry. Scoped per operand path and survives
36
37
  // the runtime `__plugin_*` tool-name prefix that a static in-scanner table
37
38
  // cannot. Declare a real file input under `input` (it gets pinned), not here.
38
39
  nonFile?: readonly string[]
@@ -19,6 +19,14 @@ const ALPHABET = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
19
19
  const BLOCK_SIZE = 4
20
20
  const BLOCK_COUNT = 2
21
21
 
22
+ // Shape is fixed by the block constants, so compile once at module load rather
23
+ // than per inbound message — extractClaimCode runs on every channel message
24
+ // carrying a potential claim.
25
+ const CLAIM_CODE_RE = new RegExp(
26
+ `${CLAIM_CODE_PREFIX}([0-9a-zA-Z]{${BLOCK_SIZE}}(?:-[0-9a-zA-Z]{${BLOCK_SIZE}}){${BLOCK_COUNT - 1}})`,
27
+ 'i',
28
+ )
29
+
22
30
  export function generateClaimCode(): string {
23
31
  const bytes = randomBytes(BLOCK_SIZE * BLOCK_COUNT)
24
32
  const chars: string[] = []
@@ -37,11 +45,7 @@ export function generateClaimCode(): string {
37
45
  // whitespace, punctuation, and case — chat clients may auto-correct case
38
46
  // or surround pastes with quotes/backticks.
39
47
  export function extractClaimCode(text: string): string | null {
40
- const pattern = new RegExp(
41
- `${CLAIM_CODE_PREFIX}([0-9a-zA-Z]{${BLOCK_SIZE}}(?:-[0-9a-zA-Z]{${BLOCK_SIZE}}){${BLOCK_COUNT - 1}})`,
42
- 'i',
43
- )
44
- const match = pattern.exec(text)
48
+ const match = CLAIM_CODE_RE.exec(text)
45
49
  if (!match) return null
46
50
  return `${CLAIM_CODE_PREFIX}${match[1]!.toUpperCase()}`
47
51
  }
package/src/run/index.ts CHANGED
@@ -122,6 +122,10 @@ export type StartAgentOptions = {
122
122
  // over the real env + <cwd>/secrets.json. Injectable so tests can supply a
123
123
  // fake secrets provider without touching process.env.
124
124
  caps?: RuntimeCapabilities
125
+ // Boot credential exporters are injectable so ordering tests can prove they
126
+ // observe secrets only after the awaited provider-OAuth refresh settles.
127
+ exportCodexAuthFile?: typeof exportCodexAuthFileForAgent
128
+ exportClaudeCredentialsFile?: typeof exportClaudeCredentialsFileForAgent
125
129
  // Boot-time proactive provider-OAuth refresh. Injectable so a test can supply
126
130
  // a refresh that holds the real secrets lock on a gate, proving the boot
127
131
  // barrier settles this before any session-producing consumer starts.
@@ -186,6 +190,8 @@ async function startAgentRuntime(
186
190
  createChannelManager: createChannelManagerFor = createChannelManager,
187
191
  createTunnelManager: createTunnelManagerFor = createTunnelManager,
188
192
  caps = createRuntimeCapabilities(process.env, join(cwd, 'secrets.json')),
193
+ exportCodexAuthFile = exportCodexAuthFileForAgent,
194
+ exportClaudeCredentialsFile = exportClaudeCredentialsFileForAgent,
189
195
  refreshProviderOAuth = refreshProviderOAuthForAgent,
190
196
  }: StartAgentOptions,
191
197
  registerBootCleanup: (cleanup: () => void | Promise<void>) => void,
@@ -355,32 +361,6 @@ async function startAgentRuntime(
355
361
  // stay in env, the file stays user-owned. See src/secrets/hydrate.ts.
356
362
  hydrateChannelEnvFromSecrets({ agentDir: cwd })
357
363
 
358
- // When the user has `docker.file.codexCli: true` AND a typeclaw-managed
359
- // openai-codex OAuth credential in secrets.json, write ~/.codex/auth.json
360
- // so the Codex CLI in the container can run without a second login. The
361
- // exporter is failure-tolerant by design: any error (gate miss, fs error,
362
- // corrupt file) returns a non-fatal result and the agent boot continues.
363
- // See src/secrets/export-codex-auth-file.ts for the newer-wins compare
364
- // that prevents clobbering Codex CLI's in-place token refreshes.
365
- exportCodexAuthFileForAgent({
366
- agentDir: cwd,
367
- codexCliEnabled: cwdConfig.docker.file.codexCli,
368
- log: (message) => console.warn(message),
369
- })
370
-
371
- // Same shape as the codex exporter above, gated on `docker.file.claudeCode`
372
- // and `secrets.json#providers.anthropic`. Writes ~/.claude/.credentials.json
373
- // so the Claude Code CLI in the container can run without the user pasting
374
- // a CLAUDE_CODE_OAUTH_TOKEN. See src/secrets/export-claude-credentials-
375
- // file.ts for the newer-wins compare that prevents clobbering Claude
376
- // Code's in-place token refreshes, and the read-merge-write that preserves
377
- // any mcpOAuth state in the file.
378
- exportClaudeCredentialsFileForAgent({
379
- agentDir: cwd,
380
- claudeCodeEnabled: cwdConfig.docker.file.claudeCode,
381
- log: (message) => console.warn(message),
382
- })
383
-
384
364
  // Proactively refresh any stored provider-OAuth token now. The SDK's own
385
365
  // refresh is lazy (fires on the first LLM call), so a container that restarts
386
366
  // with an already-expired access token otherwise discovers the staleness —
@@ -389,21 +369,50 @@ async function startAgentRuntime(
389
369
  // log. Channel-adapter OAuth already has host-side renewal crons; this is the
390
370
  // provider-OAuth equivalent.
391
371
  //
392
- // MUST be awaited, and MUST run before the first session-producing consumer
393
- // (subagentConsumer.start / cronConsumer.start / channelManager.start / the
394
- // websocket server). The SDK refresh holds SecretsBackend's async file lock
395
- // across its network request; getAuthFor()'s synchronous lock read gives up
396
- // after ~200ms and process.exit(1)s on ELOCKED. Fire-and-forget here would let
397
- // a slow refresh still own the lock when a consumer creates a session — worse
398
- // than the lazy path. Awaiting behind this barrier guarantees no synchronous
399
- // auth reader exists while the refresh owns the lock. Never throws (the
400
- // wrapper swallows and logs), so a probe failure can't block boot; a refresh
401
- // that hangs on a wedged network hangs the first turn today anyway.
372
+ // MUST be awaited, and MUST run before the credential-file exporters below
373
+ // and the first session-producing consumer (subagentConsumer.start /
374
+ // cronConsumer.start / channelManager.start / the websocket server). The SDK
375
+ // refresh holds SecretsBackend's async file lock across its network request;
376
+ // getAuthFor()'s synchronous lock read gives up after ~200ms and
377
+ // process.exit(1)s on ELOCKED. Fire-and-forget here would let a slow refresh
378
+ // still own the lock when an exporter or consumer reads secrets — worse than
379
+ // the lazy path. Awaiting behind this barrier guarantees no synchronous auth
380
+ // reader exists while the refresh owns the lock. Never throws (the wrapper
381
+ // swallows and logs), so a probe failure can't block boot; a refresh that
382
+ // hangs on a wedged network hangs the first turn today anyway.
402
383
  await refreshProviderOAuth({
403
384
  agentDir: cwd,
404
385
  log: (message) => console.warn(message),
405
386
  })
406
387
 
388
+ // When the user has `docker.file.codexCli: true` AND a typeclaw-managed
389
+ // openai-codex OAuth credential in secrets.json, write ~/.codex/auth.json
390
+ // so the Codex CLI in the container can run without a second login. This runs
391
+ // after the awaited boot refresh so the fresh ephemeral HOME receives the
392
+ // refreshed token, never the stale pre-refresh value. The exporter is
393
+ // failure-tolerant by design: any error (gate miss, fs error, corrupt file)
394
+ // returns a non-fatal result and the agent boot continues. See
395
+ // src/secrets/export-codex-auth-file.ts for the newer-wins compare that
396
+ // prevents clobbering Codex CLI's in-place token refreshes.
397
+ exportCodexAuthFile({
398
+ agentDir: cwd,
399
+ codexCliEnabled: cwdConfig.docker.file.codexCli,
400
+ log: (message) => console.warn(message),
401
+ })
402
+
403
+ // Same shape as the codex exporter above, gated on `docker.file.claudeCode`
404
+ // and `secrets.json#providers.anthropic`. It likewise runs after the boot
405
+ // refresh, then writes ~/.claude/.credentials.json so the Claude Code CLI in
406
+ // the container can run without the user pasting a CLAUDE_CODE_OAUTH_TOKEN.
407
+ // See src/secrets/export-claude-credentials-file.ts for the newer-wins compare
408
+ // that prevents clobbering Claude Code's in-place token refreshes, and the
409
+ // read-merge-write that preserves any mcpOAuth state in the file.
410
+ exportClaudeCredentialsFile({
411
+ agentDir: cwd,
412
+ claudeCodeEnabled: cwdConfig.docker.file.claudeCode,
413
+ log: (message) => console.warn(message),
414
+ })
415
+
407
416
  const claimController = createClaimController({
408
417
  cwd,
409
418
  permissions: pluginsLoaded.permissions,
@@ -6,6 +6,12 @@ export const CANONICAL_AGENT_SECRET_DIRS = [
6
6
 
7
7
  export const CANONICAL_AGENT_SECRET_FILES = ['.env', 'secrets.json', 'auth.json'] as const
8
8
 
9
+ // Trusted runtime processes use this ephemeral container HOME. bwrap builds an
10
+ // empty root and never binds /home, so model bash cannot see it; non-bash tools
11
+ // run in-process and use this fixed path as an unconditional denial root in
12
+ // addition to os.homedir() (which resolves here in the container).
13
+ export const CONTAINER_RUNTIME_HOME = '/home/agent'
14
+
9
15
  export const CANONICAL_HOME_SECRET_DIRS = [
10
16
  '.ssh',
11
17
  '.config/gh',
@@ -6,21 +6,23 @@ import { DEFAULT_SANDBOX_ENV } from './policy'
6
6
  // safe fixed values — a PATH/loader hijack of the sandbox mechanism itself.
7
7
  const RESERVED_SANDBOX_ENV_NAMES = new Set(Object.keys(DEFAULT_SANDBOX_ENV))
8
8
 
9
- // Names TypeClaw's own runtime/broker OWNS: github-cli-auth gates GH_TOKEN /
10
- // GITHUB_TOKEN behind a capability + command-scoped overlay (only a validated
11
- // `gh` call gets the token, out of argv), and the TYPECLAW_* tokens are
12
- // host/container-injected auth. Ambient inheritance would bypass the broker and
13
- // hand a reusable credential to arbitrary bash — so these are withheld even when
14
- // declared in `.env`. Not a credential-name registry: these five names are ones
15
- // the runtime CLAIMS, distinct from operator credentials (which belong in
16
- // secrets.json). A matching `.env` line would otherwise make the runtime value
17
- // eligible; the parsed-value gate (below) also blocks the empty-declaration case.
9
+ // Names the runtime claims for its own control plane. The token values are
10
+ // injected at container start. The model-HTTP policy values are operator-authored
11
+ // in raw-masked `.env`, but are runtime controls rather than model inputs; exposing
12
+ // them to bash would let the model discover the private network exceptions. Not a
13
+ // credential-name registry: ordinary operator credentials still belong in
14
+ // secrets.json when they must stay hidden. GH_TOKEN / GITHUB_TOKEN are NOT here:
15
+ // `.env` is the operator's expose-to-the-agent surface, so an operator who
16
+ // declares a GitHub token there has chosen to hand it to model bash (e.g. so plain
17
+ // `gh` / `git` / `curl` work in agents with no GitHub channel). The github-cli-auth
18
+ // broker still injects a narrower per-repo token via a command-scoped overlay, and
19
+ // when both exist for the same name the overlay is deduplicated and its value wins.
18
20
  const RUNTIME_OWNED_ENV_NAMES = new Set<string>([
19
- 'GH_TOKEN',
20
- 'GITHUB_TOKEN',
21
21
  'TYPECLAW_TUI_TOKEN',
22
22
  'TYPECLAW_HOSTD_TOKEN',
23
23
  'TYPECLAW_HOSTD_BROKER_TOKEN',
24
+ 'TYPECLAW_MODEL_HTTP_ALLOW_INTERNAL_HOSTS',
25
+ 'TYPECLAW_MODEL_HTTP_ALLOW_INTERNAL_CIDRS',
24
26
  ])
25
27
 
26
28
  // Process-hijack vectors: an inherited value here changes how the shell, loader,
@@ -52,10 +54,11 @@ const EXECUTION_CONTROL_ENV_NAMES = new Set<string>([
52
54
 
53
55
  const EXECUTION_CONTROL_ENV_PREFIXES = ['GIT_CONFIG', 'BASH_FUNC_'] as const
54
56
 
55
- // Withheld ONLY when the name would compromise sandbox integrity or the runtime
56
- // credential broker — not for being credential-shaped. `.env` is the operator's
57
- // expose-to-the-agent surface: every value they declare there reaches model bash
58
- // by design. Credentials that must stay hidden belong in secrets.json.
57
+ // Withheld ONLY when the name would compromise sandbox integrity or claim a
58
+ // host/container-injected runtime token — not for being credential-shaped. `.env`
59
+ // is the operator's expose-to-the-agent surface: every value they declare there
60
+ // reaches model bash by design, INCLUDING GH_TOKEN / GITHUB_TOKEN. Credentials
61
+ // that must stay hidden belong in secrets.json.
59
62
  function isWithheldEnvName(name: string): boolean {
60
63
  if (RESERVED_SANDBOX_ENV_NAMES.has(name)) return true
61
64
  if (RUNTIME_OWNED_ENV_NAMES.has(name)) return true
@@ -4,6 +4,7 @@ export {
4
4
  CANONICAL_AGENT_SECRET_FILES,
5
5
  CANONICAL_HOME_SECRET_DIRS,
6
6
  CANONICAL_HOME_SECRET_FILES,
7
+ CONTAINER_RUNTIME_HOME,
7
8
  } from './canonical-secrets'
8
9
  export {
9
10
  buildProcBindProbeScript,
@@ -137,9 +137,11 @@ export type SandboxPolicy = {
137
137
 
138
138
  // The env the sandbox always re-introduces after `--clearenv`. Anything not
139
139
  // listed here (or explicitly named in `env.set` / `env.passthrough` by the
140
- // consumer) is invisible inside the sandbox. This is the load-bearing leak
141
- // guard: the container env holds OPENAI_API_KEY and GH_TOKEN, and env
142
- // inheritance is the single highest-risk exfil path for prompt-injected bash.
140
+ // consumer — including operator `.env` vars re-introduced via inherit) is
141
+ // invisible inside the sandbox. This is the load-bearing leak guard: the
142
+ // container env holds credentials the operator never declared in `.env` (e.g.
143
+ // OPENAI_API_KEY, host-injected TYPECLAW_* tokens), and env inheritance is the
144
+ // single highest-risk exfil path for prompt-injected bash.
143
145
  // HOME points at /tmp because the sandbox mounts /tmp as a fresh tmpfs.
144
146
  //
145
147
  // BUN_TMPDIR / BUN_INSTALL both point under /tmp because `--clearenv` strips