typeclaw 0.50.0 → 0.51.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/dist/types/agent/bash-sandbox-env.d.ts +15 -0
- package/dist/types/agent/bash-sandbox-proc.d.ts +8 -0
- package/dist/types/agent/bash-sandbox.d.ts +94 -0
- package/dist/types/agent/compaction.d.ts +4 -4
- package/dist/types/agent/idle-compaction.d.ts +1 -0
- package/dist/types/agent/index.d.ts +5 -17
- package/dist/types/agent/output-allowance.d.ts +4 -0
- package/dist/types/agent/plugin-tools.d.ts +4 -75
- package/dist/types/agent/system-prompt-composition.d.ts +17 -0
- package/dist/types/agent/tools/ddg.d.ts +9 -0
- package/dist/types/agent/tools/stream-snapshot.d.ts +1 -1
- package/dist/types/agent/tools/webfetch/tool.d.ts +1 -1
- package/dist/types/channels/github-review-check.d.ts +1 -1
- package/dist/types/channels/router.d.ts +11 -49
- package/dist/types/config/config.d.ts +24 -11
- package/dist/types/config/providers.d.ts +3 -1
- package/dist/types/container/start.d.ts +1 -0
- package/dist/types/eval/cell.d.ts +26 -0
- package/dist/types/eval/config.d.ts +19 -0
- package/dist/types/eval/dispatch.d.ts +7 -0
- package/dist/types/eval/kernel.d.ts +28 -0
- package/dist/types/eval/process.d.ts +23 -0
- package/dist/types/eval/protocol.d.ts +34 -0
- package/dist/types/eval/sandbox.d.ts +10 -0
- package/dist/types/eval/tool.d.ts +17 -0
- package/dist/types/eval/wire.d.ts +33 -0
- package/dist/types/sandbox/env-exposure.d.ts +2 -0
- package/package.json +5 -5
- package/src/agent/bash-sandbox-env.ts +104 -0
- package/src/agent/bash-sandbox-proc.ts +70 -0
- package/src/agent/bash-sandbox.ts +225 -0
- package/src/agent/compaction.ts +22 -10
- package/src/agent/idle-compaction.ts +7 -3
- package/src/agent/index.ts +66 -157
- package/src/agent/output-allowance.ts +7 -0
- package/src/agent/plugin-tools.ts +29 -379
- package/src/agent/session-origin.ts +2 -1
- package/src/agent/subagent-completion-reminder.ts +2 -2
- package/src/agent/system-prompt-composition.ts +141 -0
- package/src/agent/system-prompt.ts +4 -4
- package/src/agent/todo/scope.ts +1 -1
- package/src/agent/tools/ddg.ts +71 -11
- package/src/agent/tools-without-local-file-operands.ts +4 -0
- package/src/browser/fill-secret.ts +307 -0
- package/src/bundled-plugins/explorer/explorer.ts +1 -1
- package/src/bundled-plugins/github-cli-auth/gh-review-detect.ts +40 -16
- package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +12 -7
- package/src/bundled-plugins/github-cli-auth/index.ts +10 -1
- package/src/bundled-plugins/managed-browser/index.ts +107 -2
- package/src/bundled-plugins/memory/dreaming.ts +4 -1
- package/src/bundled-plugins/memory/load-memory.ts +2 -4
- package/src/bundled-plugins/memory/memory-logger.ts +74 -2
- package/src/bundled-plugins/memory/transcript-preload.ts +103 -0
- package/src/bundled-plugins/memory/vector/hybrid.ts +25 -14
- package/src/bundled-plugins/security/policies/private-surface-read.ts +166 -201
- package/src/channels/adapters/discord-bot-reference.ts +5 -4
- package/src/channels/adapters/discord-bot.ts +1 -0
- package/src/channels/adapters/discord-classify.ts +8 -3
- package/src/channels/adapters/discord.ts +29 -0
- package/src/channels/github-review-check.ts +5 -1
- package/src/channels/github-review-verdict-coordinator.ts +68 -2
- package/src/channels/router.ts +88 -107
- package/src/channels/subagent-completion-bridge.ts +3 -2
- package/src/cli/hostd.ts +3 -0
- package/src/config/config.ts +15 -0
- package/src/config/providers.ts +10 -4
- package/src/container/start.ts +37 -1
- package/src/doctor/checks.ts +25 -16
- package/src/doctor/index.ts +48 -12
- package/src/doctor/operational-incidents.ts +16 -1
- package/src/doctor/plugin-bridge.ts +14 -1
- package/src/doctor/stage.ts +14 -0
- package/src/doctor/types.ts +6 -0
- package/src/eval/cell.ts +93 -0
- package/src/eval/config.ts +32 -0
- package/src/eval/dispatch.ts +67 -0
- package/src/eval/entry.ts +261 -0
- package/src/eval/kernel.ts +154 -0
- package/src/eval/process.ts +113 -0
- package/src/eval/protocol.ts +26 -0
- package/src/eval/redeclare.ts +174 -0
- package/src/eval/sandbox.ts +114 -0
- package/src/eval/tool.ts +98 -0
- package/src/eval/wire.ts +93 -0
- package/src/operations/classify-tool-outcome.ts +1 -1
- package/src/plugin/core-tool-names.ts +6 -1
- package/src/sandbox/env-exposure.ts +20 -0
- package/src/server/index.ts +46 -11
- package/src/skills/typeclaw-config/SKILL.md +20 -2
- package/typeclaw.schema.json +52 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { validateToolArguments } from '@earendil-works/pi-ai'
|
|
2
|
+
import type { ToolDefinition } from '@earendil-works/pi-coding-agent'
|
|
3
|
+
|
|
4
|
+
import type { EvalDispatch } from './cell'
|
|
5
|
+
import { EvalError } from './protocol'
|
|
6
|
+
|
|
7
|
+
// These operations need their own model-visible call: recursion would re-enter
|
|
8
|
+
// a busy kernel; orchestration/lifecycle tools can outlive or terminate its
|
|
9
|
+
// foreground lease; posting and role changes need an explicit separate action.
|
|
10
|
+
// MCP dispatch is excluded because its chosen target is not statically known.
|
|
11
|
+
// Ordinary active plugin tools retain their normal wrapped execution path.
|
|
12
|
+
const refused = new Set([
|
|
13
|
+
'eval',
|
|
14
|
+
'spawn_subagent',
|
|
15
|
+
'coding_agent',
|
|
16
|
+
'subagent_cancel',
|
|
17
|
+
'channel_send',
|
|
18
|
+
'channel_reply',
|
|
19
|
+
'channel_edit',
|
|
20
|
+
'channel_react',
|
|
21
|
+
'channel_handoff',
|
|
22
|
+
'channel_disengage',
|
|
23
|
+
'post_github_review',
|
|
24
|
+
'skip_response',
|
|
25
|
+
'restart',
|
|
26
|
+
'reload',
|
|
27
|
+
'grant_role',
|
|
28
|
+
'mcp_call',
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
export function createEvalDispatch(options: {
|
|
32
|
+
readonly tools: readonly ToolDefinition[]
|
|
33
|
+
readonly getActiveNames: () => readonly string[]
|
|
34
|
+
readonly context: Parameters<ToolDefinition['execute']>[4]
|
|
35
|
+
}): EvalDispatch {
|
|
36
|
+
const tools = new Map(options.tools.map((tool) => [tool.name, tool]))
|
|
37
|
+
return async (request, signal) => {
|
|
38
|
+
signal.throwIfAborted()
|
|
39
|
+
if (refused.has(request.name) || !options.getActiveNames().includes(request.name)) {
|
|
40
|
+
throw new EvalError('refused', `tool "${request.name}" is not active or is not batchable in eval`)
|
|
41
|
+
}
|
|
42
|
+
const tool = tools.get(request.name)
|
|
43
|
+
if (tool === undefined) throw new EvalError('refused', `tool "${request.name}" is unavailable`)
|
|
44
|
+
// Use Pi's direct-call validator, followed by the EXACT post-budget
|
|
45
|
+
// definition supplied to createAgentSession. Never rebuild raw builtins.
|
|
46
|
+
const args = validateToolArguments(tool, {
|
|
47
|
+
type: 'toolCall',
|
|
48
|
+
id: request.callId,
|
|
49
|
+
name: request.name,
|
|
50
|
+
arguments: request.args,
|
|
51
|
+
})
|
|
52
|
+
try {
|
|
53
|
+
return await tool.execute(request.callId, args, signal, undefined, options.context)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (request.name === 'grep' && error instanceof Error && error.message.includes('regex parse error')) {
|
|
56
|
+
throw new Error(error.message + GREP_REGEX_HINT, { cause: error })
|
|
57
|
+
}
|
|
58
|
+
throw error
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Cells keep failing on patterns like '\(' even with the rules in the tool
|
|
64
|
+
// description: inside a JS string literal that is just '(', so rg sees an
|
|
65
|
+
// unclosed group. Name the cause where the failure surfaces.
|
|
66
|
+
const GREP_REGEX_HINT =
|
|
67
|
+
"\nHint: in a JS string '\\(' is just '(' - write '\\\\(' or String.raw`\\(`, or pass literal: true for plain text. Lookaround is unsupported."
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// Container-stage child entry. Never import this into the trusted runtime:
|
|
2
|
+
// arbitrary code, including native Bun APIs, executes only inside bwrap.
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
4
|
+
import { start } from 'node:repl'
|
|
5
|
+
import { PassThrough, Writable } from 'node:stream'
|
|
6
|
+
import { format } from 'node:util'
|
|
7
|
+
|
|
8
|
+
import type { HostFrame, KernelFrame } from './protocol'
|
|
9
|
+
import { rewriteCell } from './redeclare'
|
|
10
|
+
import { EVAL_LIMITS, EvalError, FrameLines, FRAME_PREFIX, encodeFrame, assertNever } from './wire'
|
|
11
|
+
|
|
12
|
+
const write = process.stdout.write.bind(process.stdout)
|
|
13
|
+
// A leading delimiter also terminates ordinary stdout writes that omitted a
|
|
14
|
+
// newline. Raw output cannot accidentally swallow the following control frame.
|
|
15
|
+
const send = (frame: KernelFrame) => write('\n' + encodeFrame(frame))
|
|
16
|
+
const pending = new Map<string, { resolve(value: unknown): void; reject(error: Error): void }>()
|
|
17
|
+
const outstanding = new Set<Promise<unknown>>()
|
|
18
|
+
let cellId: string | undefined
|
|
19
|
+
// Timers and abandoned promises keep the async context of the cell that
|
|
20
|
+
// created them. Comparing it with the running cell stops a failed cell's
|
|
21
|
+
// leftover work from calling tools or printing under a later cell's id.
|
|
22
|
+
const origin = new AsyncLocalStorage<string>()
|
|
23
|
+
const currentCell = (): string | undefined => {
|
|
24
|
+
const id = origin.getStore()
|
|
25
|
+
return id !== undefined && id === cellId ? id : undefined
|
|
26
|
+
}
|
|
27
|
+
let sequence = 0
|
|
28
|
+
let rejectEvaluation: ((error: unknown) => void) | undefined
|
|
29
|
+
|
|
30
|
+
function print(...values: unknown[]): void {
|
|
31
|
+
const cellId = currentCell()
|
|
32
|
+
if (cellId === undefined) return
|
|
33
|
+
const text = format(...values) + '\n'
|
|
34
|
+
// Chunk before JSON framing so large printed values still reach the host's
|
|
35
|
+
// output cap instead of overflowing a single protocol frame.
|
|
36
|
+
let chunk = ''
|
|
37
|
+
for (const character of text) {
|
|
38
|
+
chunk += character
|
|
39
|
+
if (chunk.length >= 4096) {
|
|
40
|
+
send({ type: 'output', cellId, text: chunk })
|
|
41
|
+
chunk = ''
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (chunk !== '') send({ type: 'output', cellId, text: chunk })
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type ToolArgs = Extract<KernelFrame, { type: 'tool-call' }>['args']
|
|
48
|
+
const tool = new Proxy<Record<string, (args: ToolArgs) => Promise<unknown>>>(
|
|
49
|
+
{},
|
|
50
|
+
{
|
|
51
|
+
get(_target, name) {
|
|
52
|
+
if (typeof name !== 'string' || name === 'then') return undefined
|
|
53
|
+
return (args: ToolArgs) => {
|
|
54
|
+
const activeCellId = currentCell()
|
|
55
|
+
if (activeCellId === undefined) return Promise.reject(new EvalError('closed', 'eval cell has already ended'))
|
|
56
|
+
const id = String(++sequence)
|
|
57
|
+
const result = new Promise<unknown>((resolve, reject) => {
|
|
58
|
+
pending.set(id, { resolve, reject })
|
|
59
|
+
send({ type: 'tool-call', cellId: activeCellId, id, name, args })
|
|
60
|
+
})
|
|
61
|
+
outstanding.add(result)
|
|
62
|
+
void result.then(
|
|
63
|
+
() => outstanding.delete(result),
|
|
64
|
+
() => outstanding.delete(result),
|
|
65
|
+
)
|
|
66
|
+
return result
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
async function parallel<T>(thunks: readonly (() => Promise<T>)[]): Promise<T[]> {
|
|
73
|
+
const notThunk = thunks.findIndex((thunk) => typeof thunk !== 'function')
|
|
74
|
+
if (notThunk !== -1) {
|
|
75
|
+
throw new TypeError(
|
|
76
|
+
`parallel() takes functions, but item ${notThunk} is not one; wrap each call: parallel([() => tool.read({...})])`,
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
let next = 0
|
|
80
|
+
const values: T[] = []
|
|
81
|
+
const failures: unknown[] = []
|
|
82
|
+
// A failed cell keeps the kernel, so every worker must settle before the
|
|
83
|
+
// failure propagates; a still-sleeping worker would otherwise dispatch tool
|
|
84
|
+
// calls under the next cell's id.
|
|
85
|
+
await Promise.all(
|
|
86
|
+
Array.from({ length: Math.min(EVAL_LIMITS.concurrency, thunks.length) }, async () => {
|
|
87
|
+
while (next < thunks.length && failures.length === 0) {
|
|
88
|
+
const index = next++
|
|
89
|
+
const thunk = thunks[index]
|
|
90
|
+
if (thunk === undefined) continue
|
|
91
|
+
try {
|
|
92
|
+
values[index] = await thunk()
|
|
93
|
+
} catch (error) {
|
|
94
|
+
failures.push(error)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}),
|
|
98
|
+
)
|
|
99
|
+
if (failures.length > 0) throw failures[0]
|
|
100
|
+
return values
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Printing a whole tool result dumps its content array and details into the
|
|
104
|
+
// transcript, defeating the point of a cell. text() pulls just the text parts.
|
|
105
|
+
function text(result: unknown, maxChars = 2000): string {
|
|
106
|
+
const content = (result as { content?: unknown } | null)?.content
|
|
107
|
+
const joined = Array.isArray(content)
|
|
108
|
+
? content
|
|
109
|
+
.filter(
|
|
110
|
+
(part): part is { type: 'text'; text: string } => part?.type === 'text' && typeof part.text === 'string',
|
|
111
|
+
)
|
|
112
|
+
.map((part) => part.text)
|
|
113
|
+
.join('\n')
|
|
114
|
+
: typeof result === 'string'
|
|
115
|
+
? result
|
|
116
|
+
: format(result)
|
|
117
|
+
return joined.length > maxChars ? `${joined.slice(0, maxChars)}\n…[${joined.length - maxChars} more chars]` : joined
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const replOptions = {
|
|
121
|
+
input: new PassThrough(),
|
|
122
|
+
output: new Writable({
|
|
123
|
+
write(chunk: Buffer, _encoding: BufferEncoding, callback: (error?: Error | null) => void) {
|
|
124
|
+
if (cellId !== undefined) send({ type: 'output', cellId, text: chunk.toString('utf8') })
|
|
125
|
+
callback()
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
terminal: false,
|
|
129
|
+
prompt: '',
|
|
130
|
+
// Bun's Node-compatible REPL routes uncaught awaited errors here instead of
|
|
131
|
+
// invoking eval's completion callback. Subscribe before executing each cell.
|
|
132
|
+
handleError(error: unknown) {
|
|
133
|
+
if (rejectEvaluation === undefined) process.exit(1)
|
|
134
|
+
rejectEvaluation(error)
|
|
135
|
+
return 'ignore' as const
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
const repl = start(replOptions)
|
|
139
|
+
const helpers = {
|
|
140
|
+
tool,
|
|
141
|
+
parallel,
|
|
142
|
+
print,
|
|
143
|
+
text,
|
|
144
|
+
display: print,
|
|
145
|
+
console: { log: print, info: print, warn: print, error: print, debug: print },
|
|
146
|
+
}
|
|
147
|
+
Object.assign(repl.context, helpers)
|
|
148
|
+
|
|
149
|
+
// Passing parallel()-style thunks to Promise.all resolves to the functions
|
|
150
|
+
// themselves, so the cell "succeeds" with no tool call made. Fail it instead.
|
|
151
|
+
function rejectFunctionItems(promise: PromiseConstructor): void {
|
|
152
|
+
for (const name of ['all', 'allSettled', 'any', 'race'] as const) {
|
|
153
|
+
const original = promise[name] as (this: PromiseConstructor, values: unknown[]) => Promise<unknown>
|
|
154
|
+
Object.defineProperty(promise, name, {
|
|
155
|
+
configurable: true,
|
|
156
|
+
writable: true,
|
|
157
|
+
value(this: PromiseConstructor, values: Iterable<unknown>) {
|
|
158
|
+
const items = Array.from(values)
|
|
159
|
+
const index = items.findIndex((item) => typeof item === 'function')
|
|
160
|
+
if (index !== -1) {
|
|
161
|
+
return Promise.reject(
|
|
162
|
+
new TypeError(
|
|
163
|
+
`Promise.${name}() got a function at item ${index}; call it (tool.read({...})) or use parallel([() => ...])`,
|
|
164
|
+
),
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
return original.call(this, items)
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function run(id: string, code: string): Promise<void> {
|
|
174
|
+
if (cellId !== undefined) throw new EvalError('busy', 'eval kernel is busy')
|
|
175
|
+
cellId = id
|
|
176
|
+
const cell = rewriteCell(code)
|
|
177
|
+
const shadowed = cell.declared.filter((name) => Object.hasOwn(helpers, name))
|
|
178
|
+
const failure =
|
|
179
|
+
shadowed.length > 0
|
|
180
|
+
? `${shadowed.map((name) => `\`${name}\``).join(', ')} is an eval helper and cannot be declared at top level; rename the variable`
|
|
181
|
+
: await origin.run(id, () => evaluate(cell.code))
|
|
182
|
+
// A forgotten await must not detach tool work from the foreground cell.
|
|
183
|
+
await Promise.allSettled(outstanding)
|
|
184
|
+
cellId = undefined
|
|
185
|
+
send({ type: 'done', cellId: id, ...(failure === undefined ? {} : { error: failure.slice(0, 4096) }) })
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function evaluate(code: string): Promise<string | undefined> {
|
|
189
|
+
try {
|
|
190
|
+
await new Promise<void>((resolve, reject) => {
|
|
191
|
+
rejectEvaluation = reject
|
|
192
|
+
repl.eval(code + '\n', repl.context, 'eval-cell', (error: Error | null) => {
|
|
193
|
+
if (error !== null && error !== undefined) reject(error)
|
|
194
|
+
else resolve()
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
return undefined
|
|
198
|
+
} catch (error) {
|
|
199
|
+
return error instanceof Error ? error.message : String(error)
|
|
200
|
+
} finally {
|
|
201
|
+
rejectEvaluation = undefined
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const lines = new FrameLines()
|
|
206
|
+
// No package imports in the child: dev mode exposes src/, not a checkout's
|
|
207
|
+
// node_modules. The trusted host has already constructed these frames; the
|
|
208
|
+
// reverse direction is fully schema-validated by the host before dispatch.
|
|
209
|
+
function parseHostFrame(line: string): HostFrame {
|
|
210
|
+
const value: unknown = JSON.parse(line)
|
|
211
|
+
if (typeof value !== 'object' || value === null || !('type' in value)) {
|
|
212
|
+
throw new EvalError('protocol', 'invalid host frame')
|
|
213
|
+
}
|
|
214
|
+
switch (value.type) {
|
|
215
|
+
case 'run':
|
|
216
|
+
if ('cellId' in value && typeof value.cellId === 'string' && 'code' in value && typeof value.code === 'string') {
|
|
217
|
+
return { type: 'run', cellId: value.cellId, code: value.code }
|
|
218
|
+
}
|
|
219
|
+
break
|
|
220
|
+
case 'tool-result':
|
|
221
|
+
if ('id' in value && typeof value.id === 'string' && 'value' in value) {
|
|
222
|
+
return { type: 'tool-result', id: value.id, value: value.value }
|
|
223
|
+
}
|
|
224
|
+
break
|
|
225
|
+
case 'tool-error':
|
|
226
|
+
if ('id' in value && typeof value.id === 'string' && 'error' in value && typeof value.error === 'string') {
|
|
227
|
+
return { type: 'tool-error', id: value.id, error: value.error }
|
|
228
|
+
}
|
|
229
|
+
break
|
|
230
|
+
}
|
|
231
|
+
throw new EvalError('protocol', 'invalid host frame')
|
|
232
|
+
}
|
|
233
|
+
process.stdin.on('data', (chunk: Buffer) => {
|
|
234
|
+
for (const line of lines.push(chunk)) {
|
|
235
|
+
if (!line.startsWith(FRAME_PREFIX)) throw new EvalError('protocol', 'invalid host frame')
|
|
236
|
+
const frame = parseHostFrame(line.slice(FRAME_PREFIX.length))
|
|
237
|
+
switch (frame.type) {
|
|
238
|
+
case 'run':
|
|
239
|
+
void run(frame.cellId, frame.code)
|
|
240
|
+
break
|
|
241
|
+
case 'tool-result':
|
|
242
|
+
pending.get(frame.id)?.resolve(frame.value)
|
|
243
|
+
pending.delete(frame.id)
|
|
244
|
+
break
|
|
245
|
+
case 'tool-error':
|
|
246
|
+
pending.get(frame.id)?.reject(new EvalError('refused', frame.error))
|
|
247
|
+
pending.delete(frame.id)
|
|
248
|
+
break
|
|
249
|
+
default:
|
|
250
|
+
assertNever(frame)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
process.stdin.on('end', () => process.exit(0))
|
|
255
|
+
// Cells run in the REPL's own realm, whose Promise is reachable only from
|
|
256
|
+
// inside it; patch that one before accepting the first cell.
|
|
257
|
+
repl.eval('Promise\n', repl.context, 'eval-init', (error: Error | null, value: unknown) => {
|
|
258
|
+
if (error !== null && error !== undefined) throw error
|
|
259
|
+
rejectFunctionItems(value as PromiseConstructor)
|
|
260
|
+
send({ type: 'ready' })
|
|
261
|
+
})
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { EvalCell, type EvalDispatch } from './cell'
|
|
2
|
+
import { KernelProcess, type KernelLaunch } from './process'
|
|
3
|
+
import { EVAL_LIMITS, EvalError } from './protocol'
|
|
4
|
+
|
|
5
|
+
export type EvalResult = {
|
|
6
|
+
readonly text: string
|
|
7
|
+
readonly isError: boolean
|
|
8
|
+
readonly truncated: boolean
|
|
9
|
+
readonly restarted: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Owns one serial, foreground-only process. Preparing again before each cell
|
|
13
|
+
// rechecks bash's live masks and environment; changed authority loses bindings
|
|
14
|
+
// rather than reusing a kernel with an earlier principal's capabilities.
|
|
15
|
+
export class EvalKernel {
|
|
16
|
+
private worker: { process: KernelProcess; launch: KernelLaunch } | undefined
|
|
17
|
+
private active: EvalCell | undefined
|
|
18
|
+
private controller: AbortController | undefined
|
|
19
|
+
private idle: ReturnType<typeof setTimeout> | undefined
|
|
20
|
+
private closed = false
|
|
21
|
+
private used = false
|
|
22
|
+
private stopping: Promise<void> = Promise.resolve()
|
|
23
|
+
private disposal: Promise<void> | undefined
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
private readonly prepare: () => Promise<KernelLaunch>,
|
|
27
|
+
private readonly idleMs: number = EVAL_LIMITS.idleMs,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
async run(
|
|
31
|
+
code: string,
|
|
32
|
+
options: {
|
|
33
|
+
readonly dispatch: EvalDispatch
|
|
34
|
+
readonly signal?: AbortSignal
|
|
35
|
+
readonly timeoutMs?: number
|
|
36
|
+
},
|
|
37
|
+
): Promise<EvalResult> {
|
|
38
|
+
if (this.closed) throw new EvalError('closed', 'eval session has ended')
|
|
39
|
+
if (this.controller !== undefined) throw new EvalError('busy', 'another eval cell is running')
|
|
40
|
+
if (Buffer.byteLength(code) > EVAL_LIMITS.codeBytes) throw new EvalError('refused', 'eval code exceeds 64 KiB')
|
|
41
|
+
clearTimeout(this.idle)
|
|
42
|
+
const controller = new AbortController()
|
|
43
|
+
this.controller = controller
|
|
44
|
+
const signal =
|
|
45
|
+
options.signal === undefined ? controller.signal : AbortSignal.any([controller.signal, options.signal])
|
|
46
|
+
const cell = new EvalCell(options.dispatch, signal)
|
|
47
|
+
this.active = cell
|
|
48
|
+
// Attach rejection handling before any startup event can fail the cell.
|
|
49
|
+
const failure = new Promise<never>((_resolve, reject) => {
|
|
50
|
+
const abort = () => reject(signal.reason)
|
|
51
|
+
if (signal.aborted) abort()
|
|
52
|
+
else signal.addEventListener('abort', abort, { once: true })
|
|
53
|
+
})
|
|
54
|
+
const timeout = setTimeout(
|
|
55
|
+
() => controller.abort(new EvalError('timeout', 'eval cell timed out; kernel killed and bindings lost')),
|
|
56
|
+
Math.min(Math.max(options.timeoutMs ?? EVAL_LIMITS.timeoutMs, 1), EVAL_LIMITS.maxTimeoutMs),
|
|
57
|
+
)
|
|
58
|
+
let restarted = false
|
|
59
|
+
const execute = async () => {
|
|
60
|
+
const launch = await this.prepare()
|
|
61
|
+
if (signal.aborted || this.closed) {
|
|
62
|
+
await launch.cleanup()
|
|
63
|
+
signal.throwIfAborted()
|
|
64
|
+
throw new EvalError('closed', 'eval session has ended')
|
|
65
|
+
}
|
|
66
|
+
if (this.worker?.process.alive === false || this.worker?.launch.fingerprint !== launch.fingerprint) {
|
|
67
|
+
await this.stopWorker()
|
|
68
|
+
}
|
|
69
|
+
if (signal.aborted || this.closed) {
|
|
70
|
+
await launch.cleanup()
|
|
71
|
+
signal.throwIfAborted()
|
|
72
|
+
throw new EvalError('closed', 'eval session has ended')
|
|
73
|
+
}
|
|
74
|
+
if (this.worker === undefined) {
|
|
75
|
+
restarted = this.used
|
|
76
|
+
this.used = true
|
|
77
|
+
const process = new KernelProcess(launch, {
|
|
78
|
+
frame: (frame) => cell.receive(frame, (reply) => process.send(reply)),
|
|
79
|
+
output: (text) => cell.output.append(text),
|
|
80
|
+
failure: (error) => {
|
|
81
|
+
if (this.worker?.process === process) this.active?.completed.reject(error)
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
this.worker = { process, launch }
|
|
85
|
+
} else {
|
|
86
|
+
await launch.cleanup()
|
|
87
|
+
}
|
|
88
|
+
const process = this.worker.process
|
|
89
|
+
await process.ready
|
|
90
|
+
signal.throwIfAborted()
|
|
91
|
+
// Keep old output attached to its original cell throughout preparation
|
|
92
|
+
// and replacement. Only transfer the lease immediately before execution.
|
|
93
|
+
process.onFrame = (frame) => cell.receive(frame, (reply) => process.send(reply))
|
|
94
|
+
process.onOutput = (text) => cell.output.append(text)
|
|
95
|
+
process.send({ type: 'run', cellId: cell.id, code })
|
|
96
|
+
await cell.completed.promise
|
|
97
|
+
}
|
|
98
|
+
// completed can reject during startup, before execute awaits it.
|
|
99
|
+
void cell.completed.promise.catch((error: unknown) => controller.abort(error))
|
|
100
|
+
const running = execute()
|
|
101
|
+
try {
|
|
102
|
+
await Promise.race([running, failure])
|
|
103
|
+
return {
|
|
104
|
+
text: (restarted ? '[eval kernel restarted; previous bindings lost]\n' : '') + cell.output.render(),
|
|
105
|
+
isError: false,
|
|
106
|
+
truncated: cell.output.truncated,
|
|
107
|
+
restarted,
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
controller.abort(error)
|
|
111
|
+
// The child reported a thrown or rejected cell after settling its tool
|
|
112
|
+
// calls, so the process is healthy and its bindings are still valid.
|
|
113
|
+
const cellFailed = error instanceof EvalError && error.kind === 'cell'
|
|
114
|
+
if (!cellFailed) await this.stopWorker()
|
|
115
|
+
cell.output.append('\n' + (error instanceof Error ? error.message : String(error)))
|
|
116
|
+
return {
|
|
117
|
+
text:
|
|
118
|
+
cell.output.render() +
|
|
119
|
+
(cellFailed ? '\n[eval cell failed; kernel and bindings kept]' : '\n[eval kernel stopped; bindings lost]'),
|
|
120
|
+
isError: true,
|
|
121
|
+
truncated: cell.output.truncated,
|
|
122
|
+
restarted,
|
|
123
|
+
}
|
|
124
|
+
} finally {
|
|
125
|
+
clearTimeout(timeout)
|
|
126
|
+
this.active = undefined
|
|
127
|
+
this.controller = undefined
|
|
128
|
+
if (!this.closed)
|
|
129
|
+
this.idle = setTimeout(() => {
|
|
130
|
+
void this.stopWorker()
|
|
131
|
+
}, this.idleMs)
|
|
132
|
+
this.idle?.unref()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async dispose(): Promise<void> {
|
|
137
|
+
if (this.disposal !== undefined) return await this.disposal
|
|
138
|
+
this.closed = true
|
|
139
|
+
clearTimeout(this.idle)
|
|
140
|
+
this.controller?.abort(new EvalError('closed', 'eval session ended; bindings lost'))
|
|
141
|
+
this.disposal = this.stopWorker()
|
|
142
|
+
await this.disposal
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private async stopWorker(): Promise<void> {
|
|
146
|
+
const worker = this.worker
|
|
147
|
+
this.worker = undefined
|
|
148
|
+
if (worker === undefined) return await this.stopping
|
|
149
|
+
worker.process.stop()
|
|
150
|
+
const stopped = worker.process.exited.then(worker.launch.cleanup)
|
|
151
|
+
this.stopping = Promise.all([this.stopping, stopped]).then(() => {})
|
|
152
|
+
await this.stopping
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
|
|
2
|
+
|
|
3
|
+
import { withToolOomPriorityCommand } from '@/container/tool-oom-priority'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
EvalError,
|
|
7
|
+
FrameLines,
|
|
8
|
+
FRAME_PREFIX,
|
|
9
|
+
encodeFrame,
|
|
10
|
+
kernelFrameSchema,
|
|
11
|
+
type HostFrame,
|
|
12
|
+
type KernelFrame,
|
|
13
|
+
} from './protocol'
|
|
14
|
+
import { assertNever } from './wire'
|
|
15
|
+
|
|
16
|
+
export type KernelLaunch = {
|
|
17
|
+
readonly command: string
|
|
18
|
+
readonly cwd: string
|
|
19
|
+
readonly env: Record<string, string>
|
|
20
|
+
readonly fingerprint: string
|
|
21
|
+
readonly cleanup: () => Promise<void>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class KernelProcess {
|
|
25
|
+
readonly ready: Promise<void>
|
|
26
|
+
readonly exited: Promise<void>
|
|
27
|
+
onFrame: (frame: KernelFrame) => void
|
|
28
|
+
onOutput: (text: string) => void
|
|
29
|
+
alive = true
|
|
30
|
+
private readonly child: ChildProcessWithoutNullStreams
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
launch: KernelLaunch,
|
|
34
|
+
callbacks: {
|
|
35
|
+
readonly frame: (frame: KernelFrame) => void
|
|
36
|
+
readonly output: (text: string) => void
|
|
37
|
+
readonly failure: (error: Error) => void
|
|
38
|
+
},
|
|
39
|
+
) {
|
|
40
|
+
const ready = Promise.withResolvers<void>()
|
|
41
|
+
this.ready = ready.promise
|
|
42
|
+
this.onFrame = callbacks.frame
|
|
43
|
+
this.onOutput = callbacks.output
|
|
44
|
+
// exec retains the supervised PID. The new outer process group also covers
|
|
45
|
+
// unshare's helper in real-proc mode; bwrap's PID namespace and
|
|
46
|
+
// --die-with-parent own teardown of processes that create their own groups.
|
|
47
|
+
this.child = spawn('bash', ['-c', withToolOomPriorityCommand(`exec ${launch.command}`)], {
|
|
48
|
+
cwd: launch.cwd,
|
|
49
|
+
env: launch.env,
|
|
50
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
51
|
+
detached: true,
|
|
52
|
+
})
|
|
53
|
+
const lines = new FrameLines()
|
|
54
|
+
const fail = (error: Error) => {
|
|
55
|
+
ready.reject(error)
|
|
56
|
+
callbacks.failure(error)
|
|
57
|
+
this.stop()
|
|
58
|
+
}
|
|
59
|
+
this.child.stdout.on('data', (chunk: Buffer) => {
|
|
60
|
+
try {
|
|
61
|
+
for (const line of lines.push(chunk)) {
|
|
62
|
+
if (line === '') continue
|
|
63
|
+
if (!line.startsWith(FRAME_PREFIX)) {
|
|
64
|
+
this.onOutput(line + '\n')
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
const frame = kernelFrameSchema.parse(JSON.parse(line.slice(FRAME_PREFIX.length)))
|
|
68
|
+
switch (frame.type) {
|
|
69
|
+
case 'ready':
|
|
70
|
+
ready.resolve()
|
|
71
|
+
break
|
|
72
|
+
case 'output':
|
|
73
|
+
case 'tool-call':
|
|
74
|
+
case 'done':
|
|
75
|
+
this.onFrame(frame)
|
|
76
|
+
break
|
|
77
|
+
default:
|
|
78
|
+
assertNever(frame)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
fail(error instanceof Error ? error : new EvalError('protocol', String(error)))
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
this.child.stderr.on('data', (chunk: Buffer) => this.onOutput(chunk.toString('utf8')))
|
|
86
|
+
this.child.stdin.on('error', fail)
|
|
87
|
+
this.child.on('error', fail)
|
|
88
|
+
this.exited = new Promise((resolve) => {
|
|
89
|
+
this.child.on('close', () => {
|
|
90
|
+
this.alive = false
|
|
91
|
+
const error = new EvalError('closed', 'eval kernel exited; bindings lost')
|
|
92
|
+
ready.reject(error)
|
|
93
|
+
callbacks.failure(error)
|
|
94
|
+
resolve()
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
send(frame: HostFrame): void {
|
|
100
|
+
this.child.stdin.write(encodeFrame(frame))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
stop(): void {
|
|
104
|
+
if (!this.alive) return
|
|
105
|
+
const pid = this.child.pid
|
|
106
|
+
if (pid === undefined) return
|
|
107
|
+
try {
|
|
108
|
+
process.kill(-pid, 'SIGKILL')
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (!(error instanceof Error && 'code' in error && error.code === 'ESRCH')) throw error
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
import { EVAL_LIMITS } from './wire'
|
|
4
|
+
export { EVAL_LIMITS, FRAME_PREFIX, EvalError, encodeFrame, FrameLines, CellOutput } from './wire'
|
|
5
|
+
|
|
6
|
+
const id = z.string().min(1).max(128)
|
|
7
|
+
export const kernelFrameSchema = z.discriminatedUnion('type', [
|
|
8
|
+
z.strictObject({ type: z.literal('ready') }),
|
|
9
|
+
z.strictObject({ type: z.literal('output'), cellId: id, text: z.string() }),
|
|
10
|
+
z.strictObject({
|
|
11
|
+
type: z.literal('tool-call'),
|
|
12
|
+
cellId: id,
|
|
13
|
+
id,
|
|
14
|
+
name: z.string().min(1).max(128),
|
|
15
|
+
args: z.record(z.string(), z.json()),
|
|
16
|
+
}),
|
|
17
|
+
z.strictObject({ type: z.literal('done'), cellId: id, error: z.string().optional() }),
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
export type KernelFrame = z.infer<typeof kernelFrameSchema>
|
|
21
|
+
export const hostFrameSchema = z.discriminatedUnion('type', [
|
|
22
|
+
z.strictObject({ type: z.literal('run'), cellId: id, code: z.string().max(EVAL_LIMITS.codeBytes) }),
|
|
23
|
+
z.strictObject({ type: z.literal('tool-result'), id, value: z.unknown() }),
|
|
24
|
+
z.strictObject({ type: z.literal('tool-error'), id, error: z.string() }),
|
|
25
|
+
])
|
|
26
|
+
export type HostFrame = z.infer<typeof hostFrameSchema>
|