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.
Files changed (90) hide show
  1. package/dist/types/agent/bash-sandbox-env.d.ts +15 -0
  2. package/dist/types/agent/bash-sandbox-proc.d.ts +8 -0
  3. package/dist/types/agent/bash-sandbox.d.ts +94 -0
  4. package/dist/types/agent/compaction.d.ts +4 -4
  5. package/dist/types/agent/idle-compaction.d.ts +1 -0
  6. package/dist/types/agent/index.d.ts +5 -17
  7. package/dist/types/agent/output-allowance.d.ts +4 -0
  8. package/dist/types/agent/plugin-tools.d.ts +4 -75
  9. package/dist/types/agent/system-prompt-composition.d.ts +17 -0
  10. package/dist/types/agent/tools/ddg.d.ts +9 -0
  11. package/dist/types/agent/tools/stream-snapshot.d.ts +1 -1
  12. package/dist/types/agent/tools/webfetch/tool.d.ts +1 -1
  13. package/dist/types/channels/github-review-check.d.ts +1 -1
  14. package/dist/types/channels/router.d.ts +11 -49
  15. package/dist/types/config/config.d.ts +24 -11
  16. package/dist/types/config/providers.d.ts +3 -1
  17. package/dist/types/container/start.d.ts +1 -0
  18. package/dist/types/eval/cell.d.ts +26 -0
  19. package/dist/types/eval/config.d.ts +19 -0
  20. package/dist/types/eval/dispatch.d.ts +7 -0
  21. package/dist/types/eval/kernel.d.ts +28 -0
  22. package/dist/types/eval/process.d.ts +23 -0
  23. package/dist/types/eval/protocol.d.ts +34 -0
  24. package/dist/types/eval/sandbox.d.ts +10 -0
  25. package/dist/types/eval/tool.d.ts +17 -0
  26. package/dist/types/eval/wire.d.ts +33 -0
  27. package/dist/types/sandbox/env-exposure.d.ts +2 -0
  28. package/package.json +5 -5
  29. package/src/agent/bash-sandbox-env.ts +104 -0
  30. package/src/agent/bash-sandbox-proc.ts +70 -0
  31. package/src/agent/bash-sandbox.ts +225 -0
  32. package/src/agent/compaction.ts +22 -10
  33. package/src/agent/idle-compaction.ts +7 -3
  34. package/src/agent/index.ts +66 -157
  35. package/src/agent/output-allowance.ts +7 -0
  36. package/src/agent/plugin-tools.ts +29 -379
  37. package/src/agent/session-origin.ts +2 -1
  38. package/src/agent/subagent-completion-reminder.ts +2 -2
  39. package/src/agent/system-prompt-composition.ts +141 -0
  40. package/src/agent/system-prompt.ts +4 -4
  41. package/src/agent/todo/scope.ts +1 -1
  42. package/src/agent/tools/ddg.ts +71 -11
  43. package/src/agent/tools-without-local-file-operands.ts +4 -0
  44. package/src/browser/fill-secret.ts +307 -0
  45. package/src/bundled-plugins/explorer/explorer.ts +1 -1
  46. package/src/bundled-plugins/github-cli-auth/gh-review-detect.ts +40 -16
  47. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +12 -7
  48. package/src/bundled-plugins/github-cli-auth/index.ts +10 -1
  49. package/src/bundled-plugins/managed-browser/index.ts +107 -2
  50. package/src/bundled-plugins/memory/dreaming.ts +4 -1
  51. package/src/bundled-plugins/memory/load-memory.ts +2 -4
  52. package/src/bundled-plugins/memory/memory-logger.ts +74 -2
  53. package/src/bundled-plugins/memory/transcript-preload.ts +103 -0
  54. package/src/bundled-plugins/memory/vector/hybrid.ts +25 -14
  55. package/src/bundled-plugins/security/policies/private-surface-read.ts +166 -201
  56. package/src/channels/adapters/discord-bot-reference.ts +5 -4
  57. package/src/channels/adapters/discord-bot.ts +1 -0
  58. package/src/channels/adapters/discord-classify.ts +8 -3
  59. package/src/channels/adapters/discord.ts +29 -0
  60. package/src/channels/github-review-check.ts +5 -1
  61. package/src/channels/github-review-verdict-coordinator.ts +68 -2
  62. package/src/channels/router.ts +88 -107
  63. package/src/channels/subagent-completion-bridge.ts +3 -2
  64. package/src/cli/hostd.ts +3 -0
  65. package/src/config/config.ts +15 -0
  66. package/src/config/providers.ts +10 -4
  67. package/src/container/start.ts +37 -1
  68. package/src/doctor/checks.ts +25 -16
  69. package/src/doctor/index.ts +48 -12
  70. package/src/doctor/operational-incidents.ts +16 -1
  71. package/src/doctor/plugin-bridge.ts +14 -1
  72. package/src/doctor/stage.ts +14 -0
  73. package/src/doctor/types.ts +6 -0
  74. package/src/eval/cell.ts +93 -0
  75. package/src/eval/config.ts +32 -0
  76. package/src/eval/dispatch.ts +67 -0
  77. package/src/eval/entry.ts +261 -0
  78. package/src/eval/kernel.ts +154 -0
  79. package/src/eval/process.ts +113 -0
  80. package/src/eval/protocol.ts +26 -0
  81. package/src/eval/redeclare.ts +174 -0
  82. package/src/eval/sandbox.ts +114 -0
  83. package/src/eval/tool.ts +98 -0
  84. package/src/eval/wire.ts +93 -0
  85. package/src/operations/classify-tool-outcome.ts +1 -1
  86. package/src/plugin/core-tool-names.ts +6 -1
  87. package/src/sandbox/env-exposure.ts +20 -0
  88. package/src/server/index.ts +46 -11
  89. package/src/skills/typeclaw-config/SKILL.md +20 -2
  90. package/typeclaw.schema.json +52 -0
@@ -0,0 +1,174 @@
1
+ // Kernel-child helper, so it must stay dependency-free (see entry.ts).
2
+ //
3
+ // Models habitually reuse names like `const [a, b] = ...` across cells, and a
4
+ // REPL keeps top-level lexical bindings, so a later cell fails with "Can't
5
+ // create duplicate variable". A var cannot shadow an existing const either, so
6
+ // every cell's top-level const/let becomes var before it runs. Nested scopes
7
+ // keep their block semantics.
8
+ //
9
+ // The scan also reports top-level binding names: a top-level declaration of a
10
+ // helper like `tool` shadows it for the rest of the session, so the kernel
11
+ // refuses those cells. Initializers are skipped and collection resumes at each
12
+ // declarator comma; destructuring keys or defaults may be over-reported, which
13
+ // is acceptable for a refusal that only asks the model to rename.
14
+ const REGEX_PRECEDERS = new Set('(,=:[!&|?{};+-*%<>~^'.split(''))
15
+ const REGEX_KEYWORDS = new Set(['return', 'typeof', 'await', 'yield', 'case', 'in', 'of', 'void', 'delete', 'new'])
16
+ const CONTROL_HEADERS = new Set(['if', 'while', 'for', 'with'])
17
+
18
+ export function topLevelDeclarationsToVar(code: string): string {
19
+ return rewriteCell(code).code
20
+ }
21
+
22
+ export function rewriteCell(code: string): { code: string; declared: string[] } {
23
+ const declared: string[] = []
24
+ // A top-level declaration runs until `;` or a line end not following a comma;
25
+ // binding names are read until each declarator's `=`.
26
+ let inDeclaration = false
27
+ let collecting = false
28
+ let out = ''
29
+ let depth = 0
30
+ // Brace depths at which an open `${` returns to its enclosing template.
31
+ const templates: number[] = []
32
+ // Whether each open `(` is a control header: `/` after `if (x)` starts a
33
+ // regex, while `/` after a call like `f(x)` is division.
34
+ const parens: boolean[] = []
35
+ let previous = ''
36
+ let word = ''
37
+ let i = 0
38
+ const isIdent = (c: string | undefined) => c !== undefined && /[\p{L}\p{N}_$]/u.test(c)
39
+
40
+ const readTemplate = () => {
41
+ while (i < code.length) {
42
+ const c = code[i]
43
+ if (c === '\\') {
44
+ out += code.slice(i, i + 2)
45
+ i += 2
46
+ continue
47
+ }
48
+ if (c === '`') {
49
+ out += c
50
+ i += 1
51
+ return
52
+ }
53
+ if (c === '$' && code[i + 1] === '{') {
54
+ out += '${'
55
+ i += 2
56
+ templates.push(depth)
57
+ depth += 1
58
+ return
59
+ }
60
+ out += c
61
+ i += 1
62
+ }
63
+ }
64
+
65
+ while (i < code.length) {
66
+ const c = code[i] as string
67
+ const next = code[i + 1]
68
+ if (c === '/' && next === '/') {
69
+ const end = code.indexOf('\n', i)
70
+ const stop = end === -1 ? code.length : end
71
+ out += code.slice(i, stop)
72
+ i = stop
73
+ continue
74
+ }
75
+ if (c === '/' && next === '*') {
76
+ const end = code.indexOf('*/', i + 2)
77
+ const stop = end === -1 ? code.length : end + 2
78
+ out += code.slice(i, stop)
79
+ i = stop
80
+ continue
81
+ }
82
+ if (c === '"' || c === "'") {
83
+ let j = i + 1
84
+ while (j < code.length && code[j] !== c && code[j] !== '\n') j += code[j] === '\\' ? 2 : 1
85
+ out += code.slice(i, j + 1)
86
+ i = j + 1
87
+ previous = c
88
+ word = ''
89
+ continue
90
+ }
91
+ if (c === '`') {
92
+ out += c
93
+ i += 1
94
+ readTemplate()
95
+ previous = '`'
96
+ word = ''
97
+ continue
98
+ }
99
+ if (c === '/' && (previous === '' || REGEX_PRECEDERS.has(previous) || REGEX_KEYWORDS.has(word))) {
100
+ let j = i + 1
101
+ let inClass = false
102
+ while (j < code.length && code[j] !== '\n') {
103
+ if (code[j] === '\\') j += 1
104
+ else if (code[j] === '[') inClass = true
105
+ else if (code[j] === ']') inClass = false
106
+ else if (code[j] === '/' && !inClass) break
107
+ j += 1
108
+ }
109
+ j += 1
110
+ while (isIdent(code[j])) j += 1
111
+ out += code.slice(i, j)
112
+ i = j
113
+ previous = '/'
114
+ word = ''
115
+ continue
116
+ }
117
+ if (isIdent(c)) {
118
+ let j = i
119
+ while (isIdent(code[j])) j += 1
120
+ const token = code.slice(i, j)
121
+ const declaration =
122
+ depth === 0 &&
123
+ (token === 'const' || token === 'let' || token === 'var') &&
124
+ code[i - 1] !== '.' &&
125
+ /^\s*[[{\p{L}_$]/u.test(code.slice(j))
126
+ if (declaration) {
127
+ out += 'var'
128
+ inDeclaration = true
129
+ collecting = true
130
+ } else {
131
+ if (collecting) declared.push(token)
132
+ out += token
133
+ }
134
+ i = j
135
+ previous = token.at(-1) as string
136
+ word = token
137
+ continue
138
+ }
139
+ if (c === '(') parens.push(CONTROL_HEADERS.has(word))
140
+ if (c === '(' || c === '[' || c === '{') depth += 1
141
+ if (c === ')' && parens.pop() === true) {
142
+ depth -= 1
143
+ out += c
144
+ i += 1
145
+ previous = ';'
146
+ word = ''
147
+ continue
148
+ }
149
+ if (c === ')' || c === ']' || c === '}') {
150
+ depth -= 1
151
+ if (c === '}' && templates.at(-1) === depth) {
152
+ templates.pop()
153
+ out += c
154
+ i += 1
155
+ readTemplate()
156
+ previous = '`'
157
+ word = ''
158
+ continue
159
+ }
160
+ }
161
+ if (inDeclaration && depth === 0) {
162
+ if (c === '=') collecting = false
163
+ else if (c === ',') collecting = true
164
+ else if (c === ';' || (c === '\n' && previous !== ',')) inDeclaration = collecting = false
165
+ }
166
+ out += c
167
+ i += 1
168
+ if (!/\s/.test(c)) {
169
+ previous = c
170
+ word = ''
171
+ }
172
+ }
173
+ return { code: out, declared }
174
+ }
@@ -0,0 +1,114 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { stat } from 'node:fs/promises'
3
+ import { dirname } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+
6
+ import { prepareBashSandbox } from '@/agent/bash-sandbox'
7
+ import type { SessionOrigin } from '@/agent/session-origin'
8
+ import type { PermissionService } from '@/permissions/permissions'
9
+ import { shellQuote } from '@/sandbox/quote'
10
+
11
+ import { EvalKernel } from './kernel'
12
+
13
+ // Retained bindings survive while the principal and audience are unchanged.
14
+ // A channel origin also carries conversational state (participant counters,
15
+ // timestamps, the reaction target, the review round, display names) that
16
+ // changes on every inbound message without changing authority, so it must not
17
+ // reset the kernel.
18
+ export function authorityOrigin(origin: SessionOrigin | undefined): unknown {
19
+ if (origin === undefined) return undefined
20
+ switch (origin.kind) {
21
+ case 'channel': {
22
+ const {
23
+ participants: _participants,
24
+ membership: _membership,
25
+ reactionRef: _reactionRef,
26
+ githubReviewRound: _githubReviewRound,
27
+ workspaceName: _workspaceName,
28
+ chatName: _chatName,
29
+ parentChatName: _parentChatName,
30
+ ...authority
31
+ } = origin
32
+ return authority
33
+ }
34
+ case 'subagent':
35
+ return { ...origin, spawnedByOrigin: authorityOrigin(origin.spawnedByOrigin) }
36
+ default:
37
+ return origin
38
+ }
39
+ }
40
+
41
+ export function createSandboxedEvalKernel(options: {
42
+ readonly agentDir: string
43
+ readonly sessionId: string
44
+ readonly permissions: PermissionService
45
+ readonly getOrigin: () => SessionOrigin | undefined
46
+ }): EvalKernel {
47
+ return new EvalKernel(async () => {
48
+ const origin = options.getOrigin()
49
+ const entry = fileURLToPath(new URL('./entry.ts', import.meta.url))
50
+ const prepared = await prepareBashSandbox({
51
+ command: `exec ${shellQuote(process.execPath)} --no-install ${shellQuote(entry)}`,
52
+ agentDir: options.agentDir,
53
+ sessionId: options.sessionId,
54
+ permissions: options.permissions,
55
+ origin,
56
+ // The bridge uses stdio, never a reachable loopback server. Network work
57
+ // goes through tool.bash/web_fetch with their normal guards.
58
+ network: 'none',
59
+ })
60
+ try {
61
+ await prepared.verify()
62
+ const policy = prepared.policy
63
+ const paths = new Set([
64
+ ...prepared.withheld.paths.dirs,
65
+ ...prepared.withheld.paths.files,
66
+ ...(policy.protected?.dirs ?? []),
67
+ ...(policy.protected?.files ?? []),
68
+ ...(policy.writable?.dirs ?? []),
69
+ ...(policy.writable?.files ?? []),
70
+ ...(policy.mounts ?? []).flatMap((mount) => ('source' in mount ? [mount.source] : [])),
71
+ ])
72
+ // File bind mounts pin identities, not names. An atomic replacement can
73
+ // leave a persistent process with a detached mask or protection. Include
74
+ // ancestors too: moving a directory can retain its children's inodes.
75
+ for (const path of paths) {
76
+ let parent = dirname(path)
77
+ while (!paths.has(parent)) {
78
+ paths.add(parent)
79
+ const next = dirname(parent)
80
+ if (next === parent) break
81
+ parent = next
82
+ }
83
+ }
84
+ const identities = await Promise.all(
85
+ [...paths].sort().map(async (path) => {
86
+ const identity = await stat(path, { bigint: true })
87
+ return [path, String(identity.dev), String(identity.ino)]
88
+ }),
89
+ )
90
+ return {
91
+ command: prepared.commandString,
92
+ cwd: options.agentDir,
93
+ env: prepared.spawnEnv,
94
+ // Includes the live principal, even when two principals have the same
95
+ // role. Never share retained values across a channel author change.
96
+ fingerprint: createHash('sha256')
97
+ .update(
98
+ JSON.stringify({
99
+ origin: authorityOrigin(origin),
100
+ role: options.permissions.describe(origin),
101
+ command: prepared.commandString,
102
+ env: prepared.spawnEnv,
103
+ identities,
104
+ }),
105
+ )
106
+ .digest('hex'),
107
+ cleanup: prepared.cleanup,
108
+ }
109
+ } catch (error) {
110
+ await prepared.cleanup()
111
+ throw error
112
+ }
113
+ })
114
+ }
@@ -0,0 +1,98 @@
1
+ import { defineTool, type ToolDefinition } from '@earendil-works/pi-coding-agent'
2
+ import { Type } from 'typebox'
3
+ import type { z } from 'zod'
4
+
5
+ import type { WrapSystemToolOptions } from '@/agent/plugin-tools'
6
+ import { wrapSystemTool } from '@/agent/plugin-tools'
7
+ import { wrapToolDefinitionWithBudget, type BudgetState, type ToolResultBudget } from '@/agent/tool-result-budget'
8
+ import { noopPermissionService } from '@/permissions/permissions'
9
+
10
+ import { evalEnabledForSession, type evalConfigSchema } from './config'
11
+ import { createEvalDispatch } from './dispatch'
12
+ import { EVAL_LIMITS, EvalError } from './protocol'
13
+ import { createSandboxedEvalKernel } from './sandbox'
14
+
15
+ export const EVAL_PROMPT_HINT =
16
+ 'Use eval to keep large tool output out of your context: fetch, filter or compute in the cell, and print only the answer; unprinted data never reaches you. ' +
17
+ 'It also runs steps where the next call depends on the last result in one call: ' +
18
+ "const root = 'src'; const hits = await tool.grep({ pattern: 'openDoor', path: root }); " +
19
+ "const files = [...new Set(text(hits).split('\\n').filter((l) => l.includes(':')).map((l) => root + '/' + l.split(':')[0]))].slice(0, 5); " +
20
+ 'const bodies = await parallel(files.map((f) => () => tool.read({ path: f }))); ' +
21
+ 'for (const [i, b] of bodies.entries()) print(files[i], text(b, 1500)). ' +
22
+ 'Independent reads alone do not need eval; call those tools directly. ' +
23
+ "text(result, maxChars) extracts a tool result's text; never print whole result objects. " +
24
+ 'JavaScript bindings persist in this session until idle expiry, authority changes, or kernel failure. ' +
25
+ 'Only printed/displayed output is returned. Await all work; no background cells, install helpers, or Python cells.'
26
+
27
+ export function registerEvalTool(options: {
28
+ readonly settings: z.infer<typeof evalConfigSchema>
29
+ readonly tools: ToolDefinition[]
30
+ readonly activeNames: string[]
31
+ readonly getActiveNames: () => readonly string[]
32
+ readonly wrappers: WrapSystemToolOptions
33
+ readonly budget?: ToolResultBudget
34
+ readonly budgetState?: BudgetState
35
+ }): { dispose(): Promise<void> } | undefined {
36
+ const eligible = () =>
37
+ evalEnabledForSession({
38
+ settings: options.settings,
39
+ origin: options.wrappers.getOrigin?.(),
40
+ tools: options.getActiveNames(),
41
+ bashPolicy: options.wrappers.bashPolicy,
42
+ })
43
+ if (!eligible()) return undefined
44
+ const kernel = createSandboxedEvalKernel({
45
+ agentDir: options.wrappers.agentDir,
46
+ sessionId: options.wrappers.sessionId,
47
+ permissions: options.wrappers.permissions ?? noopPermissionService,
48
+ getOrigin: () => options.wrappers.getOrigin?.(),
49
+ })
50
+ const tool = wrapSystemTool(
51
+ defineTool({
52
+ name: 'eval',
53
+ label: 'eval',
54
+ description:
55
+ 'Run a foreground JavaScript cell in a persistent, bash-sandboxed Bun kernel. ' +
56
+ 'Use await tool.read/grep/find/ls/bash/write/edit/web_search/web_fetch with their normal arguments, ' +
57
+ "parallel(thunks) (up to 8 workers), print/display(value), and text(result, maxChars = 2000) to extract a result's text. " +
58
+ 'Tool calls return the normal content/details object; print selected text, not whole results. ' +
59
+ 'A failed tool call rejects; wrap risky calls in try/catch so one failure does not discard a batch. ' +
60
+ 'tool.grep patterns are ripgrep regex: escape ( ) [ ] and . as \\\\(, lookaround is unsupported, and literal:true matches plain text. ' +
61
+ 'Only active tools are callable; recursion, orchestration, posting, role changes and MCP dispatch are refused. ' +
62
+ '30s default, 60s hard maximum; timeout kills the kernel. Output is capped at 64 KiB / 1000 lines. ' +
63
+ 'No background cells, package installation helpers or Python. Use await import(...) for modules.',
64
+ parameters: Type.Object({
65
+ code: Type.String({ maxLength: EVAL_LIMITS.codeBytes }),
66
+ timeoutMs: Type.Optional(Type.Integer({ minimum: 1, maximum: EVAL_LIMITS.maxTimeoutMs })),
67
+ }),
68
+ async execute(_id, args, signal, _onUpdate, context) {
69
+ if (!eligible()) {
70
+ await kernel.dispose()
71
+ throw new EvalError('refused', 'eval requires active, unrestricted bash in an enabled origin')
72
+ }
73
+ const result = await kernel.run(args.code, {
74
+ signal,
75
+ timeoutMs: args.timeoutMs,
76
+ dispatch: createEvalDispatch({
77
+ tools: options.tools,
78
+ getActiveNames: options.getActiveNames,
79
+ context,
80
+ }),
81
+ })
82
+ if (result.isError) throw new EvalError('closed', result.text)
83
+ return {
84
+ content: [{ type: 'text', text: result.text || '(no output)' }],
85
+ details: { truncated: result.truncated, restarted: result.restarted },
86
+ }
87
+ },
88
+ }),
89
+ options.wrappers,
90
+ )
91
+ options.tools.push(
92
+ options.budget && options.budgetState
93
+ ? wrapToolDefinitionWithBudget(tool, options.budget, options.budgetState)
94
+ : tool,
95
+ )
96
+ options.activeNames.push('eval')
97
+ return { dispose: () => kernel.dispose() }
98
+ }
@@ -0,0 +1,93 @@
1
+ import type { HostFrame, KernelFrame } from './protocol'
2
+
3
+ export const EVAL_LIMITS = {
4
+ codeBytes: 64 * 1024,
5
+ frameBytes: 1024 * 1024,
6
+ outputBytes: 64 * 1024,
7
+ outputLines: 1000,
8
+ outputWarnBytes: 16 * 1024,
9
+ timeoutMs: 30_000,
10
+ maxTimeoutMs: 60_000,
11
+ idleMs: 5 * 60_000,
12
+ calls: 128,
13
+ concurrency: 8,
14
+ } as const
15
+
16
+ export const FRAME_PREFIX = '@typeclaw-eval '
17
+ export class EvalError extends Error {
18
+ override readonly name = 'EvalError'
19
+ constructor(
20
+ readonly kind: 'protocol' | 'closed' | 'timeout' | 'busy' | 'refused' | 'cell',
21
+ message: string,
22
+ ) {
23
+ super(message)
24
+ }
25
+ }
26
+
27
+ export function assertNever(value: never): never {
28
+ throw new EvalError('protocol', `unexpected eval frame: ${String(value)}`)
29
+ }
30
+
31
+ export function encodeFrame(frame: HostFrame | KernelFrame): string {
32
+ const line = FRAME_PREFIX + JSON.stringify(frame) + '\n'
33
+ if (Buffer.byteLength(line) > EVAL_LIMITS.frameBytes) {
34
+ throw new EvalError('protocol', 'eval bridge frame exceeds its byte limit')
35
+ }
36
+ return line
37
+ }
38
+
39
+ // Both complete lines and an unterminated flood are bounded before JSON.parse.
40
+ export class FrameLines {
41
+ private pending = Buffer.alloc(0)
42
+
43
+ push(chunk: Uint8Array): string[] {
44
+ const bytes = Buffer.concat([this.pending, chunk])
45
+ const lines: string[] = []
46
+ let start = 0
47
+ let end = bytes.indexOf(10)
48
+ while (end !== -1) {
49
+ if (end - start > EVAL_LIMITS.frameBytes) throw new EvalError('protocol', 'eval bridge line is too large')
50
+ lines.push(bytes.subarray(start, end).toString('utf8'))
51
+ start = end + 1
52
+ end = bytes.indexOf(10, start)
53
+ }
54
+ if (bytes.length - start > EVAL_LIMITS.frameBytes) {
55
+ throw new EvalError('protocol', 'eval bridge unterminated line is too large')
56
+ }
57
+ this.pending = Buffer.from(bytes.subarray(start))
58
+ return lines
59
+ }
60
+ }
61
+
62
+ const FILTER_ADVICE = 'filter in the cell and print only what you need'
63
+
64
+ export class CellOutput {
65
+ private text = ''
66
+ private bytes = 0
67
+ private lines = 0
68
+ truncated = false
69
+
70
+ append(text: string): void {
71
+ if (this.truncated) return
72
+ for (const character of text) {
73
+ const size = Buffer.byteLength(character)
74
+ if (this.bytes + size > EVAL_LIMITS.outputBytes || this.lines >= EVAL_LIMITS.outputLines) {
75
+ this.truncated = true
76
+ return
77
+ }
78
+ this.text += character
79
+ this.bytes += size
80
+ if (character === '\n') this.lines += 1
81
+ }
82
+ }
83
+
84
+ render(): string {
85
+ // Everything printed lands in the model's context; past the warning size
86
+ // the cell is no longer saving tokens over a direct tool call.
87
+ if (this.truncated) return `${this.text}\n[eval output truncated: limit reached; ${FILTER_ADVICE}]\n`
88
+ if (this.bytes > EVAL_LIMITS.outputWarnBytes) {
89
+ return `${this.text}\n[eval printed ${Math.round(this.bytes / 1024)} KB; ${FILTER_ADVICE}]\n`
90
+ }
91
+ return this.text
92
+ }
93
+ }
@@ -41,7 +41,7 @@ const COMMAND_NOT_FOUND = new RegExp(
41
41
 
42
42
  // Pi's grep/find reject with these fixed literals when `ensureTool` returns no
43
43
  // rg/fd path (pi packages/coding-agent/src/core/tools/{grep,find}.ts; published
44
- // dist/core/tools/{grep,find}.js, identical in 0.87.1, 0.99.2, and 1.0.0).
44
+ // dist/core/tools/{grep,find}.js, identical in 0.87.1, 0.99.2, 1.0.0, and 1.4.0).
45
45
  // Classification accepts the literal alone or followed by the exact `: `
46
46
  // separator and untrusted context. Context is never parsed or persisted; the
47
47
  // bin comes only from the closed grep->rg/find->fd tool mapping. This contract
@@ -12,4 +12,9 @@ export const PI_BUILTIN_TOOL_NAMES: readonly PiBuiltinToolName[] = [
12
12
 
13
13
  export const CORE_SYSTEM_TOOL_NAMES = ['web_search', 'web_fetch', 'look_at', 'coding_agent'] as const
14
14
 
15
- export const CORE_TOOL_NAMES: ReadonlySet<string> = new Set([...PI_BUILTIN_TOOL_NAMES, ...CORE_SYSTEM_TOOL_NAMES])
15
+ // eval is conditional and must not enter the always-registered system catalog.
16
+ export const CORE_TOOL_NAMES: ReadonlySet<string> = new Set([
17
+ ...PI_BUILTIN_TOOL_NAMES,
18
+ ...CORE_SYSTEM_TOOL_NAMES,
19
+ 'eval',
20
+ ])
@@ -54,6 +54,11 @@ const EXECUTION_CONTROL_ENV_NAMES = new Set<string>([
54
54
 
55
55
  const EXECUTION_CONTROL_ENV_PREFIXES = ['GIT_CONFIG', 'BASH_FUNC_'] as const
56
56
 
57
+ // Operator opt-in for values the model may type into a page but never read:
58
+ // withheld from bash, reachable only through the runtime-side
59
+ // `browser_fill_secret` tool, which types the value without returning it.
60
+ export const SECRET_ENV_PREFIX = 'TYPECLAW_SECRET_'
61
+
57
62
  const GIT_IDENTITY_ENV_NAMES = [
58
63
  'GIT_AUTHOR_NAME',
59
64
  'GIT_AUTHOR_EMAIL',
@@ -70,6 +75,7 @@ function isWithheldEnvName(name: string): boolean {
70
75
  if (RESERVED_SANDBOX_ENV_NAMES.has(name)) return true
71
76
  if (RUNTIME_OWNED_ENV_NAMES.has(name)) return true
72
77
  if (EXECUTION_CONTROL_ENV_NAMES.has(name)) return true
78
+ if (name.startsWith(SECRET_ENV_PREFIX)) return true
73
79
  return EXECUTION_CONTROL_ENV_PREFIXES.some((prefix) => name === prefix || name.startsWith(prefix))
74
80
  }
75
81
 
@@ -94,3 +100,17 @@ export function resolveExposableEnvNames(
94
100
  }
95
101
  return out
96
102
  }
103
+
104
+ // Names `browser_fill_secret` may type: everything bash already sees, plus the
105
+ // withheld `TYPECLAW_SECRET_*` tier. Runtime controls and sandbox-owned names
106
+ // stay out, so the tool can't plant a runtime token into a page for read-back.
107
+ export function resolveFillableEnvNames(
108
+ declaredEnv: ReadonlyMap<string, string>,
109
+ runtimeEnv: Readonly<Record<string, string | undefined>> = {},
110
+ ): string[] {
111
+ const out = resolveExposableEnvNames(declaredEnv, runtimeEnv)
112
+ for (const [name, fileValue] of declaredEnv) {
113
+ if (fileValue.length > 0 && name.startsWith(SECRET_ENV_PREFIX)) out.push(name)
114
+ }
115
+ return out
116
+ }
@@ -220,6 +220,7 @@ type SessionState = {
220
220
  // generation that ran session.start. A plugin reload mid-connection does
221
221
  // not re-target this session's lifecycle hooks.
222
222
  runtimeSnapshot: PluginRuntimeState | null
223
+ sessionStarted: boolean
223
224
  unsubTurnOutcome: Unsubscribe | null
224
225
  // Serializes outcome persistence and the idle continuation decision. The
225
226
  // tail always fulfills so one disk failure cannot poison every later turn;
@@ -313,6 +314,7 @@ export function createServer({
313
314
  runIdleContinuation: todoIdleContinuation = runIdleContinuation,
314
315
  }: ServerOptions) {
315
316
  const sessionStates = new WeakMap<Ws, SessionState>()
317
+ const initializingSessions = new WeakMap<Ws, Promise<void>>()
316
318
  const callIdToWs = new Map<string, AnyOwnerWs>()
317
319
 
318
320
  // The first TUI WS open per container lifetime checks for
@@ -513,8 +515,11 @@ export function createServer({
513
515
  if (rawWs.data.kind === 'tunnel-logs') return
514
516
  if (rawWs.data.kind === 'inspect') return
515
517
  const ws = rawWs as Ws
518
+ const initialized = Promise.withResolvers<void>()
519
+ initializingSessions.set(ws, initialized.promise)
516
520
  try {
517
521
  const handoff = await takeRestartHandoff()
522
+ if (ws.readyState !== WebSocket.OPEN) return
518
523
  const resumed = handoff !== null ? resumeFromHandoff(handoff, sessionFactory) : null
519
524
  const sessionManager = resumed ?? sessionFactory?.createPersisted()
520
525
  const sessionFileId = sessionManager?.getSessionId() ?? ws.data.sessionId
@@ -576,6 +581,7 @@ export function createServer({
576
581
  unsubClaim: null,
577
582
  activeClaimCode: null,
578
583
  runtimeSnapshot: runtimeSnapshot ?? null,
584
+ sessionStarted: false,
579
585
  unsubTurnOutcome: null,
580
586
  todoOutcomeWrite: Promise.resolve(true),
581
587
  todoContinuation: { recordTurnOutcome: todoOutcomeWriter, runIdleContinuation: todoIdleContinuation },
@@ -583,10 +589,14 @@ export function createServer({
583
589
  dispose,
584
590
  }
585
591
  sessionStates.set(ws, state)
592
+ // Close owns disposal, including a factory result that arrives after disconnect.
593
+ if (ws.readyState !== WebSocket.OPEN) return
586
594
 
587
595
  if (runtimeSnapshot !== undefined && agentDir !== undefined) {
596
+ state.sessionStarted = true
588
597
  await runtimeSnapshot.hooks.runSessionStart({ sessionId: sessionFileId, agentDir })
589
598
  }
599
+ if (ws.readyState !== WebSocket.OPEN) return
590
600
 
591
601
  if (agentDir !== undefined) {
592
602
  state.unsubTurnOutcome = subscribeTurnOutcome(state, agentDir, logger)
@@ -653,6 +663,7 @@ export function createServer({
653
663
  logger.error(`[server] ${sessionFileId}: clear abort suppression failed: ${describeErr(err)}`),
654
664
  )
655
665
  }
666
+ if (ws.readyState !== WebSocket.OPEN) return
656
667
  stream.publish({
657
668
  target: { kind: 'session', sessionId: sessionFileId },
658
669
  payload: { kind: 'prompt', text: ' ', delivery: 'queue' },
@@ -664,6 +675,9 @@ export function createServer({
664
675
  console.error(`session ${ws.data.sessionId}: open failed: ${message}`)
665
676
  send(ws, { type: 'error', message })
666
677
  ws.close()
678
+ } finally {
679
+ initialized.resolve()
680
+ initializingSessions.delete(ws)
667
681
  }
668
682
  },
669
683
  async message(rawWs, raw) {
@@ -903,26 +917,47 @@ export function createServer({
903
917
  return
904
918
  }
905
919
  const ws = rawWs as Ws
906
- const state = sessionStates.get(ws)
907
- state?.unsubBroadcast?.()
908
- state?.unsubPrompts?.()
909
- state?.unsubClaim?.()
910
- if (state?.activeClaimCode !== null && state?.activeClaimCode !== undefined && claimController) {
911
- claimController.cancelClaim(state.activeClaimCode)
912
- }
913
920
  commandRunner?.abortForOwner(ws)
914
921
  for (const [callId, owner] of callIdToWs) {
915
922
  if (owner === ws) callIdToWs.delete(callId)
916
923
  }
924
+ // Do not run session.end/dispose while session creation or session.start is pending.
925
+ await initializingSessions.get(ws)
926
+ const state = sessionStates.get(ws)
917
927
  try {
918
- if (state && state.runtimeSnapshot !== null) {
928
+ state?.unsubBroadcast?.()
929
+ state?.unsubPrompts?.()
930
+ state?.unsubClaim?.()
931
+ if (state?.activeClaimCode !== null && state?.activeClaimCode !== undefined && claimController) {
932
+ claimController.cancelClaim(state.activeClaimCode)
933
+ }
934
+ if (state?.sessionStarted && state.runtimeSnapshot !== null) {
919
935
  await state.runtimeSnapshot.hooks.runSessionEnd({ sessionId: state.sessionFileId, origin: state.origin })
920
936
  }
937
+ } catch (err) {
938
+ logger.error(
939
+ `[server] ${state?.sessionFileId ?? ws.data.sessionId}: close cleanup failed: ${describeErr(err)}`,
940
+ )
941
+ }
942
+ try {
943
+ try {
944
+ state?.unsubTurnOutcome?.()
945
+ } finally {
946
+ state?.session.dispose()
947
+ }
948
+ } catch (err) {
949
+ logger.error(
950
+ `[server] ${state?.sessionFileId ?? ws.data.sessionId}: session disposal failed: ${describeErr(err)}`,
951
+ )
952
+ }
953
+ try {
954
+ await state?.dispose()
955
+ } catch (err) {
956
+ logger.error(
957
+ `[server] ${state?.sessionFileId ?? ws.data.sessionId}: resource disposal failed: ${describeErr(err)}`,
958
+ )
921
959
  } finally {
922
960
  if (state) {
923
- state.unsubTurnOutcome?.()
924
- state.session.dispose()
925
- await state.dispose()
926
961
  liveSessionRegistry?.unregister(state.sessionFileId)
927
962
  }
928
963
  sessionStates.delete(ws)