typeclaw 0.46.2 → 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 +1 -1
- 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/security/policies/git-exfil.ts +45 -27
- 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/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 +30 -13
- package/src/container/start.ts +78 -63
- package/src/container/stats.ts +86 -0
- package/src/container/stop.ts +63 -36
- package/src/init/restart-deps-preflight.ts +2 -2
- package/src/run/index.ts +5 -0
- package/src/run/resource-report.ts +226 -0
- package/src/sandbox/canonical-secrets.ts +1 -0
- 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
|
+
}
|
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": {
|