typeclaw 0.46.1 → 0.47.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/README.md +2 -1
- package/package.json +2 -2
- package/src/agent/tool-file-safety.ts +18 -9
- package/src/agent/tools/webfetch/strategies/jq.ts +1 -2
- package/src/agent/tools/webfetch/strategies/readability.ts +39 -16
- package/src/agent/tools/webfetch/strategies/selector.ts +4 -2
- package/src/agent/tools/webfetch/strategies/snapshot.ts +3 -2
- package/src/agent/tools/webfetch/tool.ts +3 -3
- package/src/bundled-plugins/backup/index.ts +21 -4
- package/src/bundled-plugins/backup/runner.ts +19 -5
- package/src/bundled-plugins/memory/dreaming.ts +2 -1
- package/src/bundled-plugins/memory/load-shards.ts +31 -3
- package/src/bundled-plugins/memory/memory-logger.ts +2 -1
- package/src/bundled-plugins/memory/memory-snapshot.ts +8 -0
- package/src/bundled-plugins/memory/vector/embedder.ts +28 -9
- package/src/bundled-plugins/memory/vector/hybrid.ts +10 -2
- package/src/bundled-plugins/memory/vector/store.ts +21 -9
- package/src/bundled-plugins/security/policies/git-exfil.ts +45 -27
- package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +8 -3
- package/src/bundled-plugins/security/policies/private-surface-read.ts +6 -0
- package/src/channels/adapters/telegram-bot-classify.ts +29 -0
- package/src/channels/router.ts +98 -0
- package/src/channels/types.ts +7 -0
- package/src/cli/command-meta.ts +2 -0
- package/src/cli/compose-stats.ts +103 -0
- package/src/cli/compose.ts +13 -0
- package/src/cli/index.ts +1 -0
- package/src/cli/inspect.ts +16 -0
- package/src/cli/stats.ts +134 -0
- package/src/compose/index.ts +1 -0
- package/src/compose/stats.ts +51 -0
- package/src/config/config.ts +14 -0
- package/src/config/index.ts +4 -0
- package/src/container/controller.ts +6 -0
- package/src/container/index.ts +1 -0
- package/src/container/log-archive.ts +448 -0
- package/src/container/shared.ts +49 -19
- package/src/container/start.ts +78 -63
- package/src/container/stats.ts +86 -0
- package/src/container/stop.ts +63 -36
- package/src/init/checkpoint.ts +4 -4
- package/src/init/dockerfile.ts +87 -88
- package/src/init/gitignore.ts +5 -6
- package/src/init/restart-deps-preflight.ts +2 -2
- package/src/init/run-bun-install.ts +16 -2
- package/src/init/validate-api-key.ts +1 -0
- package/src/inspect/item-list.ts +12 -6
- package/src/permissions/permissions.ts +93 -2
- package/src/role-claim/code.ts +9 -5
- package/src/run/index.ts +50 -36
- package/src/run/resource-report.ts +226 -0
- package/src/sandbox/canonical-secrets.ts +7 -0
- package/src/sandbox/index.ts +1 -0
- package/src/sandbox/symlinks.ts +1 -1
- package/src/secrets/export-claude-credentials-file.ts +13 -22
- package/src/secrets/export-codex-auth-file.ts +12 -28
- package/typeclaw.schema.json +14 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { readFileSync, statfsSync } from 'node:fs'
|
|
2
|
+
import { totalmem } from 'node:os'
|
|
3
|
+
|
|
4
|
+
import { formatMemorySnapshot } from '@/bundled-plugins/memory/memory-snapshot'
|
|
5
|
+
|
|
6
|
+
// The container's memory/cpu ceiling is fixed at `docker run` and never changes
|
|
7
|
+
// while the process lives, so this reports once at boot. It is the missing half
|
|
8
|
+
// of an OOM post-mortem: the per-operation RSS lines show usage climbing, but
|
|
9
|
+
// only this line names the ceiling it climbed toward. Read from INSIDE the
|
|
10
|
+
// container (cgroup + statfs) because the container stage cannot call the host's
|
|
11
|
+
// `docker inspect`.
|
|
12
|
+
//
|
|
13
|
+
// Every read is a tri-state: `finite` (a real limit), `unlimited` (a genuine
|
|
14
|
+
// no-limit sentinel — v2 `max`, v1 `-1`/`LONG_MAX`), or `unknown` (missing,
|
|
15
|
+
// unreadable, or malformed). The distinction is load-bearing: collapsing
|
|
16
|
+
// `unknown` into `unlimited` would turn an unsupported cgroup layout or a read
|
|
17
|
+
// error into a false "no ceiling" claim, which is exactly the wrong signal in an
|
|
18
|
+
// OOM investigation. `unknown` renders as `?`.
|
|
19
|
+
|
|
20
|
+
export type Limit = { kind: 'finite'; value: number } | { kind: 'unlimited' } | { kind: 'unknown' }
|
|
21
|
+
|
|
22
|
+
// v1 writes "unlimited" as LONG_MAX rounded DOWN to a page boundary, so the
|
|
23
|
+
// exact value is page-size dependent: 4 KiB pages → 9223372036854771712,
|
|
24
|
+
// 64 KiB pages → 9223372036854710272. Hard-coding the 4 KiB value would let the
|
|
25
|
+
// 64 KiB sentinel slip through as a ~8 EiB finite limit. Instead treat the whole
|
|
26
|
+
// near-LONG_MAX family as unlimited: any value within one max-page (64 KiB) of
|
|
27
|
+
// LONG_MAX. Real container limits are astronomically smaller, so the band is safe.
|
|
28
|
+
const LONG_MAX = 9223372036854775807n
|
|
29
|
+
const MAX_PAGE_SIZE = 65536n
|
|
30
|
+
const CGROUP_V1_UNLIMITED_FLOOR = LONG_MAX - MAX_PAGE_SIZE
|
|
31
|
+
|
|
32
|
+
export function parseCgroupMemory(raw: string | null): Limit {
|
|
33
|
+
if (raw === null) return { kind: 'unknown' }
|
|
34
|
+
const trimmed = raw.trim()
|
|
35
|
+
if (trimmed === 'max') return { kind: 'unlimited' }
|
|
36
|
+
if (trimmed === '') return { kind: 'unknown' }
|
|
37
|
+
let value: bigint
|
|
38
|
+
try {
|
|
39
|
+
value = BigInt(trimmed)
|
|
40
|
+
} catch {
|
|
41
|
+
return { kind: 'unknown' }
|
|
42
|
+
}
|
|
43
|
+
if (value >= CGROUP_V1_UNLIMITED_FLOOR) return { kind: 'unlimited' }
|
|
44
|
+
if (value <= 0n) return { kind: 'unknown' }
|
|
45
|
+
return { kind: 'finite', value: Number(value) }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function parseCpuMax(raw: string | null): Limit {
|
|
49
|
+
if (raw === null) return { kind: 'unknown' }
|
|
50
|
+
const parts = raw.trim().split(/\s+/)
|
|
51
|
+
// Shape first: exactly two fields with a finite, positive period. A malformed
|
|
52
|
+
// record (`max garbage`, `max 100000 extra`, empty) is `unknown`, never
|
|
53
|
+
// `unlimited` — an unreadable ceiling must not masquerade as "no ceiling".
|
|
54
|
+
if (parts.length !== 2) return { kind: 'unknown' }
|
|
55
|
+
const [quota, period] = parts
|
|
56
|
+
if (quota === undefined || period === undefined) return { kind: 'unknown' }
|
|
57
|
+
const periodN = Number(period)
|
|
58
|
+
if (!Number.isFinite(periodN) || periodN <= 0) return { kind: 'unknown' }
|
|
59
|
+
// Only genuine no-limit sentinels are unlimited: v2 `max`, v1 `-1`.
|
|
60
|
+
if (quota === 'max') return { kind: 'unlimited' }
|
|
61
|
+
const quotaN = Number(quota)
|
|
62
|
+
if (quotaN === -1) return { kind: 'unlimited' }
|
|
63
|
+
if (!Number.isFinite(quotaN) || quotaN <= 0) return { kind: 'unknown' }
|
|
64
|
+
return { kind: 'finite', value: quotaN / periodN }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const BYTES_PER_MB = 1024 * 1024
|
|
68
|
+
|
|
69
|
+
function renderLimit(limit: Limit, label: string, render: (value: number) => string): string {
|
|
70
|
+
if (limit.kind === 'unknown') return `${label}=?`
|
|
71
|
+
if (limit.kind === 'unlimited') return `${label}=unlimited`
|
|
72
|
+
return `${label}=${render(limit.value)}`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function formatCgroupMemory(limit: Limit): string {
|
|
76
|
+
return renderLimit(limit, 'mem_limit_mb', (v) => String(Math.round(v / BYTES_PER_MB)))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function formatCpuMax(limit: Limit): string {
|
|
80
|
+
return renderLimit(limit, 'cpu_quota', (v) => String(v))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The controller's cgroup membership for THIS process, parsed from
|
|
84
|
+
// /proc/self/cgroup. A container is not at the v1 hierarchy root: Docker places
|
|
85
|
+
// it under `/docker/<id>` (cgroupfs) or `/system.slice/docker-<id>.scope`
|
|
86
|
+
// (systemd), so reading the root file would report the HOST limit (usually
|
|
87
|
+
// unlimited) instead of the container's.
|
|
88
|
+
//
|
|
89
|
+
// `mount` is the controller FIELD verbatim (e.g. `cpu,cpuacct`) — the directory
|
|
90
|
+
// name the controller is actually mounted under in /sys/fs/cgroup, which for a
|
|
91
|
+
// combined controller is NOT the bare controller name. `subPath` is the
|
|
92
|
+
// per-process path within that hierarchy. The requested controller is matched as
|
|
93
|
+
// one comma-separated entry, not whole-field equality. Returns null when the
|
|
94
|
+
// controller is absent (e.g. a pure cgroup-v2 host, whose /proc/self/cgroup has
|
|
95
|
+
// a single `0::` line).
|
|
96
|
+
export type CgroupV1Membership = { mount: string; subPath: string }
|
|
97
|
+
|
|
98
|
+
export function resolveCgroupV1Path(procSelfCgroup: string | null, controller: string): CgroupV1Membership | null {
|
|
99
|
+
if (procSelfCgroup === null) return null
|
|
100
|
+
for (const line of procSelfCgroup.split('\n')) {
|
|
101
|
+
if (line === '') continue
|
|
102
|
+
const firstColon = line.indexOf(':')
|
|
103
|
+
const secondColon = line.indexOf(':', firstColon + 1)
|
|
104
|
+
if (firstColon === -1 || secondColon === -1) continue
|
|
105
|
+
const field = line.slice(firstColon + 1, secondColon)
|
|
106
|
+
if (field.split(',').includes(controller)) return { mount: field, subPath: line.slice(secondColon + 1) }
|
|
107
|
+
}
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The absolute path to a v1 controller file for THIS process, resolved through
|
|
112
|
+
// BOTH /proc/self/cgroup (membership) and /proc/self/mountinfo (the actual
|
|
113
|
+
// mountpoint + mount root). The membership path from /proc/self/cgroup is
|
|
114
|
+
// relative to the cgroup HIERARCHY root, which is NOT necessarily the mountpoint:
|
|
115
|
+
// a container commonly has its subtree bind-mounted so hierarchy root
|
|
116
|
+
// `/docker/<id>` appears AT `/sys/fs/cgroup/memory`. Appending the full
|
|
117
|
+
// membership path onto the mountpoint (the previous approach) then reads a
|
|
118
|
+
// non-existent `/sys/fs/cgroup/memory/docker/<id>/...` and reports `?`.
|
|
119
|
+
//
|
|
120
|
+
// mountinfo gives, per mount: field[3]=root, field[4]=mountpoint, and after the
|
|
121
|
+
// ` - ` separator, fstype=`cgroup` with the controller in its super-options. The
|
|
122
|
+
// file lives at `mountpoint + (membershipPath relative to mount root)`.
|
|
123
|
+
export function resolveCgroupV1File(
|
|
124
|
+
procSelfCgroup: string | null,
|
|
125
|
+
mountinfo: string | null,
|
|
126
|
+
controller: string,
|
|
127
|
+
file: string,
|
|
128
|
+
): string | null {
|
|
129
|
+
const membership = resolveCgroupV1Path(procSelfCgroup, controller)
|
|
130
|
+
if (membership === null || mountinfo === null) return null
|
|
131
|
+
|
|
132
|
+
for (const line of mountinfo.split('\n')) {
|
|
133
|
+
if (line === '') continue
|
|
134
|
+
const sep = line.indexOf(' - ')
|
|
135
|
+
if (sep === -1) continue
|
|
136
|
+
const pre = line.slice(0, sep).split(/\s+/)
|
|
137
|
+
const post = line.slice(sep + 3).split(/\s+/)
|
|
138
|
+
const [fstype, , superOptions] = post
|
|
139
|
+
if (fstype !== 'cgroup') continue
|
|
140
|
+
if (superOptions === undefined || !superOptions.split(',').includes(controller)) continue
|
|
141
|
+
const mountRoot = pre[3]
|
|
142
|
+
const mountPoint = pre[4]
|
|
143
|
+
if (mountRoot === undefined || mountPoint === undefined) continue
|
|
144
|
+
const rel = relativeToMountRoot(membership.subPath, mountRoot)
|
|
145
|
+
if (rel === null) continue
|
|
146
|
+
return joinCgroupPath(joinCgroupPath(mountPoint, rel), file)
|
|
147
|
+
}
|
|
148
|
+
return null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The membership sub-path expressed relative to the mount's root. When the mount
|
|
152
|
+
// root IS the membership path (subtree mount), the file sits directly at the
|
|
153
|
+
// mountpoint (rel = ''). When root is `/` (whole-hierarchy mount), rel is the
|
|
154
|
+
// full sub-path. A membership outside the mount root is unreachable → null.
|
|
155
|
+
function relativeToMountRoot(subPath: string, mountRoot: string): string | null {
|
|
156
|
+
if (mountRoot === '/') return subPath === '/' ? '' : subPath
|
|
157
|
+
if (subPath === mountRoot) return ''
|
|
158
|
+
if (subPath.startsWith(`${mountRoot}/`)) return subPath.slice(mountRoot.length)
|
|
159
|
+
return null
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function joinCgroupPath(base: string, segment: string): string {
|
|
163
|
+
if (segment === '' || segment === '/') return base
|
|
164
|
+
return `${base.replace(/\/$/, '')}/${segment.replace(/^\//, '')}`
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readOrNull(path: string): string | null {
|
|
168
|
+
try {
|
|
169
|
+
return readFileSync(path, 'utf-8')
|
|
170
|
+
} catch {
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function readV1ControllerFile(controller: string, file: string): string | null {
|
|
176
|
+
const path = resolveCgroupV1File(
|
|
177
|
+
readOrNull('/proc/self/cgroup'),
|
|
178
|
+
readOrNull('/proc/self/mountinfo'),
|
|
179
|
+
controller,
|
|
180
|
+
file,
|
|
181
|
+
)
|
|
182
|
+
return path === null ? null : readOrNull(path)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function readCgroupMemoryLimit(): Limit {
|
|
186
|
+
const v2 = readOrNull('/sys/fs/cgroup/memory.max')
|
|
187
|
+
if (v2 !== null) return parseCgroupMemory(v2)
|
|
188
|
+
return parseCgroupMemory(readV1ControllerFile('memory', 'memory.limit_in_bytes'))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function readCgroupCpuQuota(): Limit {
|
|
192
|
+
const v2 = readOrNull('/sys/fs/cgroup/cpu.max')
|
|
193
|
+
if (v2 !== null) return parseCpuMax(v2)
|
|
194
|
+
const quota = readV1ControllerFile('cpu', 'cpu.cfs_quota_us')
|
|
195
|
+
const period = readV1ControllerFile('cpu', 'cpu.cfs_period_us')
|
|
196
|
+
if (quota === null || period === null) return { kind: 'unknown' }
|
|
197
|
+
return parseCpuMax(`${quota.trim()} ${period.trim()}`)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function formatStorage(path: string): string {
|
|
201
|
+
try {
|
|
202
|
+
const fs = statfsSync(path)
|
|
203
|
+
const totalMb = Math.round((fs.blocks * fs.bsize) / BYTES_PER_MB)
|
|
204
|
+
const freeMb = Math.round((fs.bavail * fs.bsize) / BYTES_PER_MB)
|
|
205
|
+
return `disk_total_mb=${totalMb} disk_free_mb=${freeMb}`
|
|
206
|
+
} catch {
|
|
207
|
+
return 'disk_total_mb=? disk_free_mb=?'
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function logResourceReport(agentDir = '/agent'): void {
|
|
212
|
+
try {
|
|
213
|
+
const memLimit = formatCgroupMemory(readCgroupMemoryLimit())
|
|
214
|
+
const cpuQuota = formatCpuMax(readCgroupCpuQuota())
|
|
215
|
+
// The host/VM total is the OOM ceiling when no cgroup --memory is set (the
|
|
216
|
+
// default `docker run`): the kernel can still kill the process at host RAM
|
|
217
|
+
// exhaustion even with mem_limit=unlimited, so this gives the RSS lines a
|
|
218
|
+
// real comparison point instead of only an "unlimited" that isn't.
|
|
219
|
+
const hostMem = `host_total_mb=${Math.round(totalmem() / BYTES_PER_MB)}`
|
|
220
|
+
const cores = `cpus_visible=${globalThis.navigator?.hardwareConcurrency ?? '?'}`
|
|
221
|
+
const storage = formatStorage(agentDir)
|
|
222
|
+
console.info(`[resource-report] ${memLimit} ${hostMem} ${cpuQuota} ${cores} ${storage} ${formatMemorySnapshot()}`)
|
|
223
|
+
} catch (err) {
|
|
224
|
+
console.warn(`[resource-report] failed: ${err instanceof Error ? err.message : String(err)}`)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -2,10 +2,17 @@ export const CANONICAL_AGENT_SECRET_DIRS = [
|
|
|
2
2
|
'workspace/.config/gws',
|
|
3
3
|
'workspace/.agent-messenger',
|
|
4
4
|
'.typeclaw/home',
|
|
5
|
+
'.typeclaw/logs',
|
|
5
6
|
] as const
|
|
6
7
|
|
|
7
8
|
export const CANONICAL_AGENT_SECRET_FILES = ['.env', 'secrets.json', 'auth.json'] as const
|
|
8
9
|
|
|
10
|
+
// Trusted runtime processes use this ephemeral container HOME. bwrap builds an
|
|
11
|
+
// empty root and never binds /home, so model bash cannot see it; non-bash tools
|
|
12
|
+
// run in-process and use this fixed path as an unconditional denial root in
|
|
13
|
+
// addition to os.homedir() (which resolves here in the container).
|
|
14
|
+
export const CONTAINER_RUNTIME_HOME = '/home/agent'
|
|
15
|
+
|
|
9
16
|
export const CANONICAL_HOME_SECRET_DIRS = [
|
|
10
17
|
'.ssh',
|
|
11
18
|
'.config/gh',
|
package/src/sandbox/index.ts
CHANGED
package/src/sandbox/symlinks.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type SandboxSymlinkSpec = {
|
|
|
12
12
|
// Resolves config `sandbox.symlinks` into the in-jail `--symlink` ops the bwrap
|
|
13
13
|
// builder consumes. `from` is the symlink LOCATION: a `~/`-prefixed `from` is
|
|
14
14
|
// expanded against the SANDBOX HOME (`/tmp`, where the per-session tmp dir is
|
|
15
|
-
// bound), NOT the
|
|
15
|
+
// bound), NOT the trusted runtime's `/home/agent` — inside the jail a CLI reading
|
|
16
16
|
// `$HOME/.foo` looks under `/tmp`, so the symlink must live there. An absolute
|
|
17
17
|
// `from` is used verbatim. `to` is resolved to the absolute /agent path the
|
|
18
18
|
// symlink points at. Container paths are always POSIX, so this uses posix path
|
|
@@ -43,11 +43,9 @@ export type ExportClaudeCredentialsFileOptions = {
|
|
|
43
43
|
|
|
44
44
|
// Writes typeclaw's anthropic OAuth credential to
|
|
45
45
|
// $CLAUDE_CONFIG_DIR/.credentials.json (or $HOME/.claude/.credentials.json
|
|
46
|
-
// by default) when it's safe to do so.
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
// write follows the symlink and lands on the persistent host-side path —
|
|
50
|
-
// same contract as exportCodexAuthFile.
|
|
46
|
+
// by default) when it's safe to do so. Container HOME is /home/agent and
|
|
47
|
+
// ephemeral; this boot-time export reconstructs the CLI file from secrets.json
|
|
48
|
+
// after provider OAuth refresh has updated the canonical credential.
|
|
51
49
|
//
|
|
52
50
|
// Three guards, cheapest first. The first two return without ever touching
|
|
53
51
|
// the filesystem, which keeps the 90% case (users who don't enable
|
|
@@ -192,16 +190,11 @@ function resolveClaudeConfigDir(configDir: string | undefined): string | null {
|
|
|
192
190
|
// rename is belt-and-suspenders: writeFileSync's `mode` is applied at
|
|
193
191
|
// create time, but umask can mask it down on some filesystems.
|
|
194
192
|
//
|
|
195
|
-
// Symlink preservation
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
//
|
|
199
|
-
//
|
|
200
|
-
// would replace the symlink with a regular file, leaving the persistent
|
|
201
|
-
// path empty and Claude Code's in-place token refresh silently lost on
|
|
202
|
-
// every restart. Resolve the symlink target with readlinkSync and rename
|
|
203
|
-
// against the real path so the symlink itself is preserved. The temp
|
|
204
|
-
// file MUST live alongside the real target (same filesystem) because
|
|
193
|
+
// Symlink preservation remains a safety property even though TypeClaw no longer
|
|
194
|
+
// plants one here. An operator or external tool may preconfigure the credentials
|
|
195
|
+
// path as a symlink; POSIX rename(2) replaces that directory entry instead of
|
|
196
|
+
// following it. Resolve the target and rename there so the caller's symlink
|
|
197
|
+
// survives. The temp file MUST live alongside the real target because
|
|
205
198
|
// renameSync across filesystems fails with EXDEV.
|
|
206
199
|
function writeAtomic(targetPath: string, contents: string): void {
|
|
207
200
|
const realTarget = resolveSymlinkTarget(targetPath)
|
|
@@ -227,13 +220,11 @@ function writeAtomic(targetPath: string, contents: string): void {
|
|
|
227
220
|
}
|
|
228
221
|
}
|
|
229
222
|
|
|
230
|
-
// Returns the absolute path renameSync should target. When `path` is a
|
|
231
|
-
//
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
// throws EINVAL when the path exists but isn't a symlink and ENOENT
|
|
236
|
-
// when nothing is there — both cases fall through to the original path.
|
|
223
|
+
// Returns the absolute path renameSync should target. When `path` is a symlink,
|
|
224
|
+
// returns the resolved absolute target so we write through the link instead of
|
|
225
|
+
// replacing it. Otherwise returns the path unchanged. readlinkSync throws EINVAL
|
|
226
|
+
// when the path exists but isn't a symlink and ENOENT when nothing is there —
|
|
227
|
+
// both cases fall through to the original path.
|
|
237
228
|
function resolveSymlinkTarget(path: string): string {
|
|
238
229
|
let link: string
|
|
239
230
|
try {
|
|
@@ -38,12 +38,9 @@ export type ExportCodexAuthFileOptions = {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// Writes typeclaw's openai-codex OAuth credential to $HOME/.codex/auth.json
|
|
41
|
-
// when it's safe to do so.
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
// host-side path — that's the stable contract from src/init/dockerfile.ts
|
|
45
|
-
// "link_persistent_home_files" and we MUST use it instead of writing to
|
|
46
|
-
// /agent/.typeclaw/home/ directly.
|
|
41
|
+
// when it's safe to do so. Container HOME is /home/agent and ephemeral; this
|
|
42
|
+
// boot-time export reconstructs the CLI file from secrets.json after provider
|
|
43
|
+
// OAuth refresh has had a chance to update the canonical credential.
|
|
47
44
|
//
|
|
48
45
|
// Three guards, cheapest first. The first two return without ever touching
|
|
49
46
|
// the filesystem, which keeps the 90% case (users who don't enable Codex
|
|
@@ -170,22 +167,12 @@ function readCredentialExpiry(credential: { expires?: unknown; access?: unknown
|
|
|
170
167
|
// belt-and-suspenders: writeFileSync's `mode` is applied at create time,
|
|
171
168
|
// but umask can mask it down on some filesystems.
|
|
172
169
|
//
|
|
173
|
-
// Symlink preservation
|
|
174
|
-
//
|
|
175
|
-
// $HOME/.codex/auth.json as a symlink
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
// would replace the symlink with a regular file, leaving the persistent
|
|
180
|
-
// path empty. Next boot the shim recreates the symlink (force-removing
|
|
181
|
-
// our file), the persistent path is still empty, and Codex's in-place
|
|
182
|
-
// token refresh is silently lost on every restart.
|
|
183
|
-
//
|
|
184
|
-
// Fix: resolve the symlink target with readlinkSync and rename against
|
|
185
|
-
// the real path so the symlink itself is preserved. The temp file MUST
|
|
186
|
-
// live alongside the real target (same filesystem) because renameSync
|
|
187
|
-
// across filesystems fails with EXDEV — $HOME is the container's
|
|
188
|
-
// overlayfs, but the symlink target is a bind-mounted host path.
|
|
170
|
+
// Symlink preservation is still a safety property even though TypeClaw no
|
|
171
|
+
// longer plants one here. An operator or external tool may preconfigure
|
|
172
|
+
// $HOME/.codex/auth.json as a symlink; POSIX rename(2) replaces that directory
|
|
173
|
+
// entry instead of following it. Resolve the target and rename there so the
|
|
174
|
+
// caller's symlink survives. The temp file MUST live alongside the real target
|
|
175
|
+
// because renameSync across filesystems fails with EXDEV.
|
|
189
176
|
function writeAtomic(targetPath: string, contents: string): void {
|
|
190
177
|
const realTarget = resolveSymlinkTarget(targetPath)
|
|
191
178
|
const dir = dirname(realTarget)
|
|
@@ -213,12 +200,9 @@ function writeAtomic(targetPath: string, contents: string): void {
|
|
|
213
200
|
}
|
|
214
201
|
}
|
|
215
202
|
|
|
216
|
-
// Returns the absolute path renameSync should target. When `path` is a
|
|
217
|
-
//
|
|
218
|
-
//
|
|
219
|
-
// instead of replacing it. Otherwise (tests, or first boot before the
|
|
220
|
-
// shim installs the symlink — though the shim runs before the agent in
|
|
221
|
-
// production), returns the path unchanged.
|
|
203
|
+
// Returns the absolute path renameSync should target. When `path` is a symlink,
|
|
204
|
+
// returns the resolved absolute target so we write through the link instead of
|
|
205
|
+
// replacing it. Otherwise returns the path unchanged.
|
|
222
206
|
//
|
|
223
207
|
// readlinkSync throws EINVAL when the path exists but isn't a symlink,
|
|
224
208
|
// and ENOENT when nothing is there. Either case → write to the original
|
package/typeclaw.schema.json
CHANGED
|
@@ -2734,6 +2734,20 @@
|
|
|
2734
2734
|
}
|
|
2735
2735
|
}
|
|
2736
2736
|
},
|
|
2737
|
+
"logs": {
|
|
2738
|
+
"default": {
|
|
2739
|
+
"retentionDays": 14
|
|
2740
|
+
},
|
|
2741
|
+
"type": "object",
|
|
2742
|
+
"properties": {
|
|
2743
|
+
"retentionDays": {
|
|
2744
|
+
"default": 14,
|
|
2745
|
+
"type": "integer",
|
|
2746
|
+
"minimum": 1,
|
|
2747
|
+
"maximum": 3650
|
|
2748
|
+
}
|
|
2749
|
+
}
|
|
2750
|
+
},
|
|
2737
2751
|
"roles": {
|
|
2738
2752
|
"type": "object",
|
|
2739
2753
|
"propertyNames": {
|