tjs-lang 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +9 -3
- package/demo/examples.test.ts +6 -2
- package/demo/src/playground-shared.ts +48 -16
- package/demo/src/playground-test-results.test.ts +112 -0
- package/demo/src/tjs-playground.ts +11 -5
- package/dist/examples/modules/dist/main.d.ts +34 -0
- package/dist/examples/modules/dist/math.d.ts +120 -0
- package/dist/index.js +115 -112
- package/dist/index.js.map +4 -4
- package/dist/src/lang/core.d.ts +1 -1
- package/dist/src/lang/dialect.d.ts +35 -0
- package/dist/src/lang/emitters/ast.d.ts +1 -1
- package/dist/src/lang/emitters/js-tests.d.ts +7 -0
- package/dist/src/lang/emitters/js.d.ts +12 -0
- package/dist/src/lang/index.d.ts +2 -1
- package/dist/src/lang/parser-types.d.ts +17 -0
- package/dist/src/lang/parser.d.ts +18 -0
- package/dist/src/lang/transpiler.d.ts +1 -0
- package/dist/src/lang/types.d.ts +18 -0
- package/dist/src/vm/runtime.d.ts +18 -0
- package/dist/src/vm/vm.d.ts +15 -1
- package/dist/tjs-batteries.js +2 -2
- package/dist/tjs-batteries.js.map +2 -2
- package/dist/tjs-eval.js +43 -43
- package/dist/tjs-eval.js.map +3 -3
- package/dist/tjs-from-ts.js +1 -1
- package/dist/tjs-from-ts.js.map +2 -2
- package/dist/tjs-lang.js +76 -73
- package/dist/tjs-lang.js.map +4 -4
- package/dist/tjs-vm.js +49 -49
- package/dist/tjs-vm.js.map +3 -3
- package/llms.txt +1 -0
- package/package.json +20 -1
- package/src/batteries/audit.ts +3 -2
- package/src/cli/commands/check.ts +3 -2
- package/src/cli/commands/emit.ts +4 -2
- package/src/cli/commands/run.ts +6 -2
- package/src/cli/commands/types.ts +2 -2
- package/src/lang/codegen.test.ts +4 -1
- package/src/lang/core.ts +6 -4
- package/src/lang/dialect.test.ts +63 -0
- package/src/lang/dialect.ts +50 -0
- package/src/lang/emitters/ast.ts +145 -2
- package/src/lang/emitters/js-tests.ts +46 -37
- package/src/lang/emitters/js.ts +19 -2
- package/src/lang/features.test.ts +6 -5
- package/src/lang/index.ts +18 -5
- package/src/lang/parser-types.ts +17 -0
- package/src/lang/parser.test.ts +12 -6
- package/src/lang/parser.ts +113 -3
- package/src/lang/subset-invariant.test.ts +90 -0
- package/src/lang/transpiler.ts +8 -0
- package/src/lang/types.ts +18 -0
- package/src/lang/wasm.test.ts +8 -2
- package/src/linalg/linalg.test.ts +8 -2
- package/src/use-cases/batteries.test.ts +4 -0
- package/src/use-cases/local-helpers.test.ts +219 -0
- package/src/use-cases/timeout-overrides.test.ts +169 -0
- package/src/vm/atoms/batteries.ts +17 -3
- package/src/vm/runtime.ts +89 -4
- package/src/vm/vm.ts +47 -9
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { describe, it, expect } from 'bun:test'
|
|
2
|
+
import { ajs } from '../transpiler'
|
|
3
|
+
import { transpile } from '../lang'
|
|
4
|
+
import { AgentVM } from '../vm'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Local helper functions: an agent source file may declare multiple top-level
|
|
8
|
+
* functions. The LAST is the entry point; the preceding ones are helpers,
|
|
9
|
+
* invoked via the `callLocal` atom. Helpers are top-level siblings — isolated
|
|
10
|
+
* scopes that see only their parameters, like ordinary functions.
|
|
11
|
+
*/
|
|
12
|
+
describe('Local helper functions', () => {
|
|
13
|
+
const VM = new AgentVM()
|
|
14
|
+
const run = async (src: string, args: Record<string, any>) =>
|
|
15
|
+
(await VM.run(ajs(src), args)).result
|
|
16
|
+
|
|
17
|
+
it('invokes a scalar-returning helper from the entry function', async () => {
|
|
18
|
+
const result = await run(
|
|
19
|
+
`
|
|
20
|
+
function double(x) { return x * 2 }
|
|
21
|
+
function main(n) {
|
|
22
|
+
const d = double(n)
|
|
23
|
+
return { d }
|
|
24
|
+
}`,
|
|
25
|
+
{ n: 5 }
|
|
26
|
+
)
|
|
27
|
+
expect(result.d).toBe(10)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('lets a helper call another helper', async () => {
|
|
31
|
+
const result = await run(
|
|
32
|
+
`
|
|
33
|
+
function double(x) { return x * 2 }
|
|
34
|
+
function addOne(x) {
|
|
35
|
+
const d = double(x)
|
|
36
|
+
return d + 1
|
|
37
|
+
}
|
|
38
|
+
function main(n) {
|
|
39
|
+
const a = double(n)
|
|
40
|
+
const b = addOne(n)
|
|
41
|
+
return { a, b }
|
|
42
|
+
}`,
|
|
43
|
+
{ n: 5 }
|
|
44
|
+
)
|
|
45
|
+
expect(result).toEqual({ a: 10, b: 11 })
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('allows helpers to return arrays and objects', async () => {
|
|
49
|
+
const result = await run(
|
|
50
|
+
`
|
|
51
|
+
function pair(x) { return [x, x] }
|
|
52
|
+
function wrap(x) { return { value: x } }
|
|
53
|
+
function main(n) {
|
|
54
|
+
const p = pair(n)
|
|
55
|
+
const w = wrap(n)
|
|
56
|
+
return { p, w }
|
|
57
|
+
}`,
|
|
58
|
+
{ n: 3 }
|
|
59
|
+
)
|
|
60
|
+
expect(result).toEqual({ p: [3, 3], w: { value: 3 } })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('supports idiomatic TJS typed/example params (colon shorthand)', async () => {
|
|
64
|
+
// Examples are representative values: floats for a fractional-scaling fn.
|
|
65
|
+
const result = await run(
|
|
66
|
+
`
|
|
67
|
+
function scale(x: 1.5, factor: 0.5): 0.75 { return x * factor }
|
|
68
|
+
function main(n: 1.5): 0.75 {
|
|
69
|
+
const s = scale(n, 0.5)
|
|
70
|
+
return { s }
|
|
71
|
+
}`,
|
|
72
|
+
{ n: 3 }
|
|
73
|
+
)
|
|
74
|
+
expect(result.s).toBe(1.5)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('supports multiple parameters', async () => {
|
|
78
|
+
const result = await run(
|
|
79
|
+
`
|
|
80
|
+
function add(a, b) { return a + b }
|
|
81
|
+
function main(x, y) {
|
|
82
|
+
const sum = add(x, y)
|
|
83
|
+
return { sum }
|
|
84
|
+
}`,
|
|
85
|
+
{ x: 7, y: 8 }
|
|
86
|
+
)
|
|
87
|
+
expect(result.sum).toBe(15)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('isolates helper scope — helpers cannot see the caller locals', async () => {
|
|
91
|
+
// `secret` is a local in main (= 42). The helper references a bare
|
|
92
|
+
// `secret`; under isolation it is NOT in scope, so AJS falls back to the
|
|
93
|
+
// bare-string literal "secret". A scope leak would instead expose main's
|
|
94
|
+
// 42 — so the guarantee is simply: the helper never sees the caller value.
|
|
95
|
+
const result = await run(
|
|
96
|
+
`
|
|
97
|
+
function leak(x) { return { x, secret } }
|
|
98
|
+
function main(n) {
|
|
99
|
+
const secret = 42
|
|
100
|
+
const r = leak(n)
|
|
101
|
+
return r
|
|
102
|
+
}`,
|
|
103
|
+
{ n: 1 }
|
|
104
|
+
)
|
|
105
|
+
expect(result.x).toBe(1)
|
|
106
|
+
expect(result.secret).not.toBe(42) // no leak of caller's local
|
|
107
|
+
expect(result.secret).toBe('secret') // AJS bare-string fallback
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('does not let helper return exit the caller agent', async () => {
|
|
111
|
+
// The helper returns, then main keeps running and returns its own object.
|
|
112
|
+
const result = await run(
|
|
113
|
+
`
|
|
114
|
+
function step(x) { return x + 100 }
|
|
115
|
+
function main(n) {
|
|
116
|
+
const a = step(n)
|
|
117
|
+
const b = step(a)
|
|
118
|
+
return { a, b }
|
|
119
|
+
}`,
|
|
120
|
+
{ n: 1 }
|
|
121
|
+
)
|
|
122
|
+
expect(result).toEqual({ a: 101, b: 201 })
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// --- transpile-time guards -------------------------------------------------
|
|
126
|
+
|
|
127
|
+
const expectTranspileError = (src: string, match: RegExp) =>
|
|
128
|
+
expect(() => transpile(src, { vmTarget: true })).toThrow(match)
|
|
129
|
+
|
|
130
|
+
it('rejects helper calls nested inside expressions', () => {
|
|
131
|
+
expectTranspileError(
|
|
132
|
+
`
|
|
133
|
+
function double(x) { return x * 2 }
|
|
134
|
+
function main(n) { return { v: double(n) + 1 } }`,
|
|
135
|
+
/cannot be called inside an expression/
|
|
136
|
+
)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('supports recursion (bounded by fuel/timeout, not rejected)', async () => {
|
|
140
|
+
// Recursive factorial — by-reference dispatch makes this a runtime loop.
|
|
141
|
+
const result = await run(
|
|
142
|
+
`
|
|
143
|
+
function fact(n) {
|
|
144
|
+
if (n <= 1) { return 1 }
|
|
145
|
+
const prev = fact(n - 1)
|
|
146
|
+
return n * prev
|
|
147
|
+
}
|
|
148
|
+
function main(n) {
|
|
149
|
+
const f = fact(n)
|
|
150
|
+
return { f }
|
|
151
|
+
}`,
|
|
152
|
+
{ n: 5 }
|
|
153
|
+
)
|
|
154
|
+
expect(result.f).toBe(120)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('supports mutual recursion', async () => {
|
|
158
|
+
const result = await run(
|
|
159
|
+
`
|
|
160
|
+
function isEven(n) {
|
|
161
|
+
if (n == 0) { return true }
|
|
162
|
+
const r = isOdd(n - 1)
|
|
163
|
+
return r
|
|
164
|
+
}
|
|
165
|
+
function isOdd(n) {
|
|
166
|
+
if (n == 0) { return false }
|
|
167
|
+
const r = isEven(n - 1)
|
|
168
|
+
return r
|
|
169
|
+
}
|
|
170
|
+
function main(n) {
|
|
171
|
+
const even = isEven(n)
|
|
172
|
+
return { even }
|
|
173
|
+
}`,
|
|
174
|
+
{ n: 10 }
|
|
175
|
+
)
|
|
176
|
+
expect(result.even).toBe(true)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('turns runaway recursion into a clean error, not a host crash', async () => {
|
|
180
|
+
// Unbounded recursion: must surface as a monadic error (depth cap or fuel),
|
|
181
|
+
// never a thrown RangeError that crashes the host VM.
|
|
182
|
+
const out = await VM.run(
|
|
183
|
+
ajs(`
|
|
184
|
+
function loop(n) { const r = loop(n); return r }
|
|
185
|
+
function main(n) { const v = loop(n); return { v } }`),
|
|
186
|
+
{ n: 1 },
|
|
187
|
+
{ fuel: 100000 }
|
|
188
|
+
)
|
|
189
|
+
expect(out.error).toBeDefined()
|
|
190
|
+
expect(out.error?.message).toMatch(/depth|fuel/i)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('rejects arity mismatches', () => {
|
|
194
|
+
expectTranspileError(
|
|
195
|
+
`
|
|
196
|
+
function add(a, b) { return a + b }
|
|
197
|
+
function main(n) { const v = add(n); return { v } }`,
|
|
198
|
+
/expects 2 argument\(s\), got 1/
|
|
199
|
+
)
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('rejects same-name same-arity functions (collision)', () => {
|
|
203
|
+
// Two identical-signature `dup` declarations collide. The polymorphic-merge
|
|
204
|
+
// pass (which runs during parse, before helper extraction) catches this
|
|
205
|
+
// first — either way it's a clear, deterministic rejection.
|
|
206
|
+
expectTranspileError(
|
|
207
|
+
`
|
|
208
|
+
function dup(x) { return x }
|
|
209
|
+
function dup(x) { return x }
|
|
210
|
+
function main(n) { const v = dup(n); return { v } }`,
|
|
211
|
+
/ambiguous signatures|Duplicate helper/
|
|
212
|
+
)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('still transpiles a single-function agent (no helpers)', async () => {
|
|
216
|
+
const result = await run(`function main(n) { return { n } }`, { n: 9 })
|
|
217
|
+
expect(result.n).toBe(9)
|
|
218
|
+
})
|
|
219
|
+
})
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { AgentVM, isAgentError, defineAtom } from '../index'
|
|
3
|
+
import { s } from 'tosijs-schema'
|
|
4
|
+
|
|
5
|
+
// A deliberately slow atom whose builtin timeout would normally trip.
|
|
6
|
+
const slowAtom = defineAtom(
|
|
7
|
+
'slow',
|
|
8
|
+
s.object({ ms: s.number }),
|
|
9
|
+
undefined,
|
|
10
|
+
async ({ ms }) => {
|
|
11
|
+
await new Promise((resolve) => setTimeout(resolve, ms))
|
|
12
|
+
},
|
|
13
|
+
{ docs: 'Slow IO atom for testing', cost: 0.01, timeoutMs: 100 }
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
describe('Per-atom Timeout Overrides', () => {
|
|
17
|
+
it('uses the atom default when no override is provided', async () => {
|
|
18
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
19
|
+
|
|
20
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 200 }).toJSON()
|
|
21
|
+
|
|
22
|
+
const result = await vm.run(ast, {}, { fuel: 100 })
|
|
23
|
+
|
|
24
|
+
expect(result.error).toBeDefined()
|
|
25
|
+
expect(result.error?.message).toContain("Atom 'slow' timed out")
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('static override raises an atom timeout above its default', async () => {
|
|
29
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
30
|
+
|
|
31
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 200 }).toJSON()
|
|
32
|
+
|
|
33
|
+
const result = await vm.run(
|
|
34
|
+
ast,
|
|
35
|
+
{},
|
|
36
|
+
{
|
|
37
|
+
fuel: 100,
|
|
38
|
+
timeoutOverrides: { slow: 5000 }, // raise from 100 → 5s
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
expect(result.error).toBeUndefined()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('static override lowers an atom timeout below its default', async () => {
|
|
46
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
47
|
+
|
|
48
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 100 }).toJSON()
|
|
49
|
+
|
|
50
|
+
const result = await vm.run(
|
|
51
|
+
ast,
|
|
52
|
+
{},
|
|
53
|
+
{
|
|
54
|
+
fuel: 100,
|
|
55
|
+
timeoutOverrides: { slow: 10 }, // lower from 100ms → 10ms
|
|
56
|
+
}
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
expect(result.error).toBeDefined()
|
|
60
|
+
expect(result.error?.message).toContain("Atom 'slow' timed out")
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('dynamic override receives input and ctx', async () => {
|
|
64
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
65
|
+
|
|
66
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 200 }).toJSON()
|
|
67
|
+
|
|
68
|
+
const result = await vm.run(
|
|
69
|
+
ast,
|
|
70
|
+
{},
|
|
71
|
+
{
|
|
72
|
+
fuel: 100,
|
|
73
|
+
timeoutOverrides: {
|
|
74
|
+
// Allow 10x the requested delay
|
|
75
|
+
slow: (input: any) => input.ms * 10,
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
expect(result.error).toBeUndefined()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('override of 0 disables the per-atom timeout', async () => {
|
|
84
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
85
|
+
|
|
86
|
+
// ms > atom default (100), would normally trip
|
|
87
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 300 }).toJSON()
|
|
88
|
+
|
|
89
|
+
const result = await vm.run(
|
|
90
|
+
ast,
|
|
91
|
+
{},
|
|
92
|
+
{
|
|
93
|
+
fuel: 100,
|
|
94
|
+
timeoutOverrides: { slow: 0 },
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
expect(result.error).toBeUndefined()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('atom timeout still fires when no override matches that op', async () => {
|
|
102
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
103
|
+
|
|
104
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 200 }).toJSON()
|
|
105
|
+
|
|
106
|
+
const result = await vm.run(
|
|
107
|
+
ast,
|
|
108
|
+
{},
|
|
109
|
+
{
|
|
110
|
+
fuel: 100,
|
|
111
|
+
timeoutOverrides: { somethingElse: 5000 },
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
expect(result.error).toBeDefined()
|
|
116
|
+
expect(isAgentError(result.error)).toBe(true)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
describe('Default vm.run timeout', () => {
|
|
121
|
+
it('does not derive run timeout from fuel (formula removed)', async () => {
|
|
122
|
+
// Under the old `fuel * 10ms` formula, fuel=1 would timeout in 10ms —
|
|
123
|
+
// far less than the IO needed below. Under the atom-derived default
|
|
124
|
+
// (slowest atom × 2), a 200ms IO atom completes fine with tiny fuel.
|
|
125
|
+
const vm = new AgentVM({ slow: slowAtom })
|
|
126
|
+
|
|
127
|
+
const ast = vm.Agent.step({ op: 'slow', ms: 200 }).toJSON()
|
|
128
|
+
|
|
129
|
+
const result = await vm.run(
|
|
130
|
+
ast,
|
|
131
|
+
{},
|
|
132
|
+
{
|
|
133
|
+
fuel: 1000, // headroom for cost; the run-level timeout is what matters
|
|
134
|
+
timeoutOverrides: { slow: 0 }, // disable per-atom timeout
|
|
135
|
+
// no timeoutMs option — use the default
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
expect(result.error).toBeUndefined()
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('derives the default from the slowest atom × 2 (never below the slowest atom budget)', () => {
|
|
143
|
+
// coreAtoms include 120s atoms (llm/runCode); the run-level default must
|
|
144
|
+
// cover them, else those per-atom budgets are dead config.
|
|
145
|
+
const core = new AgentVM()
|
|
146
|
+
expect(core.defaultRunTimeout).toBe(240_000) // 120s × 2
|
|
147
|
+
|
|
148
|
+
// A slower custom atom raises the default (self-adjusting).
|
|
149
|
+
const slow = defineAtom('slow5m', s.object({}), undefined, async () => {}, {
|
|
150
|
+
timeoutMs: 300_000,
|
|
151
|
+
})
|
|
152
|
+
const vm = new AgentVM({ slow5m: slow })
|
|
153
|
+
expect(vm.defaultRunTimeout).toBe(600_000) // 300s × 2
|
|
154
|
+
// and it always covers the slowest atom's own budget
|
|
155
|
+
expect(vm.defaultRunTimeout).toBeGreaterThanOrEqual(300_000)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('floors the default at 60s for a VM whose atoms are all fast', () => {
|
|
159
|
+
// `seq` etc. have timeoutMs: 0 (no timeout) and are excluded; if the only
|
|
160
|
+
// finite budgets were tiny, the floor keeps a sane backstop.
|
|
161
|
+
const fast = defineAtom('fast', s.object({}), undefined, async () => {}, {
|
|
162
|
+
timeoutMs: 50,
|
|
163
|
+
})
|
|
164
|
+
const vm = new AgentVM({ fast })
|
|
165
|
+
// coreAtoms still contribute their 120s atoms, so this VM is 240s; the floor
|
|
166
|
+
// is exercised conceptually — assert it's at least the 60s minimum.
|
|
167
|
+
expect(vm.defaultRunTimeout).toBeGreaterThanOrEqual(60_000)
|
|
168
|
+
})
|
|
169
|
+
})
|
|
@@ -51,7 +51,12 @@ export const storeVectorize = defineAtom(
|
|
|
51
51
|
const resolvedText = resolveValue(text, ctx)
|
|
52
52
|
return vectorCap.embed(resolvedText)
|
|
53
53
|
},
|
|
54
|
-
|
|
54
|
+
// Network embedding call; a cold model can exceed the 1s atom default.
|
|
55
|
+
{
|
|
56
|
+
docs: 'Generate embeddings using vector battery',
|
|
57
|
+
cost: 20,
|
|
58
|
+
timeoutMs: 60000,
|
|
59
|
+
}
|
|
55
60
|
)
|
|
56
61
|
|
|
57
62
|
// store.createCollection
|
|
@@ -97,7 +102,12 @@ export const storeVectorAdd = defineAtom(
|
|
|
97
102
|
|
|
98
103
|
return storeCap.vectorAdd(resolvedColl, resolvedDoc)
|
|
99
104
|
},
|
|
100
|
-
|
|
105
|
+
// May embed the doc via the store (network IO); allow for a cold model.
|
|
106
|
+
{
|
|
107
|
+
docs: 'Add a document to a vector store collection',
|
|
108
|
+
cost: 5,
|
|
109
|
+
timeoutMs: 60000,
|
|
110
|
+
}
|
|
101
111
|
)
|
|
102
112
|
|
|
103
113
|
// store.search (Vector Search)
|
|
@@ -167,7 +177,11 @@ export const llmPredictBattery = defineAtom(
|
|
|
167
177
|
resolvedFormat
|
|
168
178
|
)
|
|
169
179
|
},
|
|
170
|
-
{
|
|
180
|
+
{
|
|
181
|
+
docs: 'Generate completion using LLM battery',
|
|
182
|
+
cost: 100,
|
|
183
|
+
timeoutMs: 120000,
|
|
184
|
+
}
|
|
171
185
|
)
|
|
172
186
|
|
|
173
187
|
// Vision battery interface (multimodal)
|
package/src/vm/runtime.ts
CHANGED
|
@@ -151,6 +151,11 @@ export type CostOverride =
|
|
|
151
151
|
| number
|
|
152
152
|
| ((input: any, ctx: RuntimeContext) => number)
|
|
153
153
|
|
|
154
|
+
/** Timeout override: static number (ms) or dynamic function. 0 disables. */
|
|
155
|
+
export type TimeoutOverride =
|
|
156
|
+
| number
|
|
157
|
+
| ((input: any, ctx: RuntimeContext) => number)
|
|
158
|
+
|
|
154
159
|
export interface RuntimeContext {
|
|
155
160
|
fuel: { current: number }
|
|
156
161
|
args: Record<string, any>
|
|
@@ -165,8 +170,12 @@ export interface RuntimeContext {
|
|
|
165
170
|
warnings?: string[] // Non-fatal warnings (e.g., console.warn)
|
|
166
171
|
signal?: AbortSignal // External abort signal for timeout enforcement
|
|
167
172
|
costOverrides?: Record<string, CostOverride> // Per-atom cost overrides
|
|
173
|
+
timeoutOverrides?: Record<string, TimeoutOverride> // Per-atom timeout overrides (ms, 0 disables)
|
|
168
174
|
context?: Record<string, any> // Immutable request-scoped metadata (auth, permissions, etc.)
|
|
169
175
|
runCodeDepth?: number // Track nested runCode calls to prevent infinite recursion
|
|
176
|
+
localCall?: boolean // Inside a callLocal helper body — return may be a non-object scalar
|
|
177
|
+
helpers?: Record<string, { steps: any[]; paramNames: string[] }> // Local helper bodies, called by name
|
|
178
|
+
callDepth?: number // Helper call nesting depth — guards against host-stack overflow on deep recursion
|
|
170
179
|
}
|
|
171
180
|
|
|
172
181
|
export type AtomExec = (step: any, ctx: RuntimeContext) => Promise<void>
|
|
@@ -1363,18 +1372,25 @@ export function defineAtom<I extends Record<string, any>, O = any>(
|
|
|
1363
1372
|
return
|
|
1364
1373
|
}
|
|
1365
1374
|
|
|
1366
|
-
// 3. Execution with Timeout
|
|
1375
|
+
// 3. Execution with Timeout (per-atom override > atom default)
|
|
1376
|
+
const overrideTimeout = ctx.timeoutOverrides?.[op]
|
|
1377
|
+
const baseTimeout =
|
|
1378
|
+
overrideTimeout !== undefined ? overrideTimeout : timeoutMs
|
|
1379
|
+
const effectiveTimeout =
|
|
1380
|
+
typeof baseTimeout === 'function'
|
|
1381
|
+
? baseTimeout(inputData, ctx)
|
|
1382
|
+
: baseTimeout
|
|
1367
1383
|
let timer: any
|
|
1368
1384
|
const execute = async () => fn(step as I, ctx)
|
|
1369
1385
|
|
|
1370
1386
|
result =
|
|
1371
|
-
|
|
1387
|
+
effectiveTimeout > 0
|
|
1372
1388
|
? await Promise.race([
|
|
1373
1389
|
execute(),
|
|
1374
1390
|
new Promise<never>((_, reject) => {
|
|
1375
1391
|
timer = setTimeout(
|
|
1376
1392
|
() => reject(new Error(`Atom '${op}' timed out`)),
|
|
1377
|
-
|
|
1393
|
+
effectiveTimeout
|
|
1378
1394
|
)
|
|
1379
1395
|
}),
|
|
1380
1396
|
]).finally(() => clearTimeout(timer))
|
|
@@ -1562,8 +1578,11 @@ export const ret = defineAtom(
|
|
|
1562
1578
|
if ('value' in step) {
|
|
1563
1579
|
const res = resolveValue(step.value, ctx)
|
|
1564
1580
|
|
|
1565
|
-
// Enforce object returns — agents must return objects for composability
|
|
1581
|
+
// Enforce object returns — agents must return objects for composability.
|
|
1582
|
+
// Helper bodies (callLocal) are exempt: they're internal calls and may
|
|
1583
|
+
// return scalars, arrays, etc. like ordinary functions.
|
|
1566
1584
|
if (
|
|
1585
|
+
!ctx.localCall &&
|
|
1567
1586
|
res !== undefined &&
|
|
1568
1587
|
res !== null &&
|
|
1569
1588
|
!isAgentError(res) &&
|
|
@@ -1786,6 +1805,71 @@ export const scope = defineAtom(
|
|
|
1786
1805
|
{ docs: 'Create new scope', timeoutMs: 0, cost: 0.1 }
|
|
1787
1806
|
)
|
|
1788
1807
|
|
|
1808
|
+
/**
|
|
1809
|
+
* Maximum helper-call nesting depth. Fuel + timeout bound the total *work* a
|
|
1810
|
+
* recursive helper can do, but deeply-nested `await seq.exec` would overflow
|
|
1811
|
+
* the host JS stack (an uncatchable RangeError) before fuel runs out. This cap
|
|
1812
|
+
* converts runaway recursion into a clean monadic error instead.
|
|
1813
|
+
*/
|
|
1814
|
+
export const MAX_CALL_DEPTH = 256
|
|
1815
|
+
|
|
1816
|
+
export const callLocal = defineAtom(
|
|
1817
|
+
'callLocal',
|
|
1818
|
+
s.object({
|
|
1819
|
+
name: s.string,
|
|
1820
|
+
args: s.array(s.any),
|
|
1821
|
+
}),
|
|
1822
|
+
undefined,
|
|
1823
|
+
async ({ name, args }, ctx) => {
|
|
1824
|
+
const helper = ctx.helpers?.[name as string]
|
|
1825
|
+
if (!helper) {
|
|
1826
|
+
ctx.error = new AgentError(`Unknown helper: ${name}`, 'callLocal')
|
|
1827
|
+
return ctx.error
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
const depth = (ctx.callDepth ?? 0) + 1
|
|
1831
|
+
if (depth > MAX_CALL_DEPTH) {
|
|
1832
|
+
ctx.error = new AgentError(
|
|
1833
|
+
`Maximum helper call depth (${MAX_CALL_DEPTH}) exceeded — likely infinite recursion in '${name}'`,
|
|
1834
|
+
'callLocal'
|
|
1835
|
+
)
|
|
1836
|
+
return ctx.error
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
// Resolve each argument expression in the caller's scope
|
|
1840
|
+
const resolvedArgs = (args as any[]).map((arg) => resolveValue(arg, ctx))
|
|
1841
|
+
|
|
1842
|
+
// Isolated scope: helpers are top-level sibling functions, not nested
|
|
1843
|
+
// closures, so they see ONLY their params — never the caller's locals.
|
|
1844
|
+
// Capabilities/fuel/resolver/etc. are shared via the spread; state and
|
|
1845
|
+
// consts start fresh. localCall exempts the helper's `return` from the
|
|
1846
|
+
// agent object-return contract (helpers may return scalars/arrays like
|
|
1847
|
+
// ordinary functions). callDepth guards against host-stack overflow.
|
|
1848
|
+
const scopedCtx: RuntimeContext = {
|
|
1849
|
+
...ctx,
|
|
1850
|
+
state: {},
|
|
1851
|
+
consts: new Set(),
|
|
1852
|
+
output: undefined,
|
|
1853
|
+
error: undefined,
|
|
1854
|
+
localCall: true,
|
|
1855
|
+
callDepth: depth,
|
|
1856
|
+
}
|
|
1857
|
+
for (let i = 0; i < helper.paramNames.length; i++) {
|
|
1858
|
+
scopedCtx.state[helper.paramNames[i]] = resolvedArgs[i]
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
await seq.exec({ op: 'seq', steps: helper.steps } as any, scopedCtx)
|
|
1862
|
+
|
|
1863
|
+
// Propagate errors but NOT output — the helper's return becomes this
|
|
1864
|
+
// atom's result, captured into the caller's named result variable by
|
|
1865
|
+
// the standard exec wrapper. Unlike `scope`, it does not bubble up
|
|
1866
|
+
// to ctx.output (so a helper return doesn't exit the caller agent).
|
|
1867
|
+
if (scopedCtx.error) ctx.error = scopedCtx.error
|
|
1868
|
+
return scopedCtx.output
|
|
1869
|
+
},
|
|
1870
|
+
{ docs: 'Invoke a local helper function by name', timeoutMs: 0, cost: 0.1 }
|
|
1871
|
+
)
|
|
1872
|
+
|
|
1789
1873
|
// 3. List (Cost 1)
|
|
1790
1874
|
|
|
1791
1875
|
/*#
|
|
@@ -2991,6 +3075,7 @@ export const coreAtoms = {
|
|
|
2991
3075
|
varsLet,
|
|
2992
3076
|
varsExport,
|
|
2993
3077
|
scope,
|
|
3078
|
+
callLocal,
|
|
2994
3079
|
map,
|
|
2995
3080
|
filter,
|
|
2996
3081
|
reduce,
|
package/src/vm/vm.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type RunResult,
|
|
5
5
|
type RuntimeContext,
|
|
6
6
|
type CostOverride,
|
|
7
|
+
type TimeoutOverride,
|
|
7
8
|
coreAtoms,
|
|
8
9
|
AgentError,
|
|
9
10
|
isProcedureToken,
|
|
@@ -13,16 +14,49 @@ import { TypedBuilder, type BaseNode, type BuilderType } from '../builder'
|
|
|
13
14
|
import { validate } from 'tosijs-schema'
|
|
14
15
|
import { transpile } from '../lang/core'
|
|
15
16
|
|
|
16
|
-
/**
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Floor for the run-level default timeout. The actual default is derived from
|
|
19
|
+
* the registered atoms (slowest atom × 2 — see `defaultRunTimeout`), but never
|
|
20
|
+
* drops below this for a VM whose atoms are all fast.
|
|
21
|
+
*/
|
|
22
|
+
const MIN_DEFAULT_RUN_TIMEOUT_MS = 60_000
|
|
18
23
|
|
|
19
24
|
export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
20
25
|
readonly atoms: typeof coreAtoms & M
|
|
21
26
|
|
|
27
|
+
private _defaultRunTimeout?: number
|
|
28
|
+
|
|
22
29
|
constructor(customAtoms: M = {} as M) {
|
|
23
30
|
this.atoms = { ...coreAtoms, ...customAtoms }
|
|
24
31
|
}
|
|
25
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Default run-level wall-clock timeout when `run()` is given no explicit
|
|
35
|
+
* `timeoutMs`. Derived as `max(per-atom timeoutMs) × 2` over the registered
|
|
36
|
+
* atoms (with headroom for an agent that chains a couple of slow calls), so
|
|
37
|
+
* the run-level backstop can never be shorter than the slowest single atom's
|
|
38
|
+
* own budget — otherwise that per-atom budget would be dead config (e.g.
|
|
39
|
+
* `llmVision`/`llmPredictBattery` are 120s; a fixed 60s run default would kill
|
|
40
|
+
* them mid-call). Atoms with `timeoutMs: 0` (no timeout, e.g. `seq`) are
|
|
41
|
+
* excluded; the result is floored at {@link MIN_DEFAULT_RUN_TIMEOUT_MS}.
|
|
42
|
+
* Self-adjusting: registering a slower custom atom raises the default.
|
|
43
|
+
*/
|
|
44
|
+
get defaultRunTimeout(): number {
|
|
45
|
+
if (this._defaultRunTimeout === undefined) {
|
|
46
|
+
let slowest = 0
|
|
47
|
+
for (const atom of Object.values(this.atoms)) {
|
|
48
|
+
// undefined timeoutMs means the per-atom default (1000ms); 0 means none.
|
|
49
|
+
const t = (atom as any).timeoutMs ?? 1000
|
|
50
|
+
if (t > 0 && t > slowest) slowest = t
|
|
51
|
+
}
|
|
52
|
+
this._defaultRunTimeout = Math.max(
|
|
53
|
+
MIN_DEFAULT_RUN_TIMEOUT_MS,
|
|
54
|
+
slowest * 2
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
return this._defaultRunTimeout
|
|
58
|
+
}
|
|
59
|
+
|
|
26
60
|
get builder(): BuilderType<typeof coreAtoms & M> {
|
|
27
61
|
return new TypedBuilder(this.atoms) as any
|
|
28
62
|
}
|
|
@@ -77,9 +111,10 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
77
111
|
fuel?: number
|
|
78
112
|
capabilities?: Capabilities
|
|
79
113
|
trace?: boolean
|
|
80
|
-
timeoutMs?: number //
|
|
114
|
+
timeoutMs?: number // Wall-clock cap on the whole run (default: slowest atom × 2, min 60s — see defaultRunTimeout)
|
|
81
115
|
signal?: AbortSignal // External abort signal (e.g., from caller)
|
|
82
116
|
costOverrides?: Record<string, CostOverride> // Per-atom fuel cost overrides
|
|
117
|
+
timeoutOverrides?: Record<string, TimeoutOverride> // Per-atom timeout overrides (ms, 0 disables)
|
|
83
118
|
context?: Record<string, any> // Request-scoped metadata (auth, permissions, etc.)
|
|
84
119
|
} = {}
|
|
85
120
|
): Promise<RunResult> {
|
|
@@ -105,9 +140,10 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
105
140
|
|
|
106
141
|
const startFuel = options.fuel ?? 1000
|
|
107
142
|
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
|
|
143
|
+
// Run-level wall-clock timeout. Agents are typically IO-bound; the default
|
|
144
|
+
// is derived from the registered atoms (slowest × 2) so it always covers the
|
|
145
|
+
// slowest atom's own budget. See `defaultRunTimeout`.
|
|
146
|
+
const timeoutMs = options.timeoutMs ?? this.defaultRunTimeout
|
|
111
147
|
|
|
112
148
|
// Default Capabilities
|
|
113
149
|
const capabilities = options.capabilities ?? {}
|
|
@@ -160,8 +196,10 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
160
196
|
output: undefined,
|
|
161
197
|
signal: controller.signal,
|
|
162
198
|
costOverrides: options.costOverrides,
|
|
199
|
+
timeoutOverrides: options.timeoutOverrides,
|
|
163
200
|
context: options.context,
|
|
164
201
|
warnings, // Shared warnings array
|
|
202
|
+
helpers: (ast as any).helpers, // Local helper bodies, called by name via callLocal
|
|
165
203
|
}
|
|
166
204
|
|
|
167
205
|
if (options.trace) {
|
|
@@ -197,7 +235,7 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
197
235
|
controller.signal.addEventListener('abort', () => {
|
|
198
236
|
reject(
|
|
199
237
|
new Error(
|
|
200
|
-
`Execution timeout after ${timeoutMs}ms
|
|
238
|
+
`Execution timeout after ${timeoutMs}ms. Pass a higher \`timeoutMs\` to vm.run() or set per-atom \`timeoutOverrides\` for slow IO atoms.`
|
|
201
239
|
)
|
|
202
240
|
)
|
|
203
241
|
})
|
|
@@ -205,7 +243,7 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
205
243
|
if (controller.signal.aborted) {
|
|
206
244
|
reject(
|
|
207
245
|
new Error(
|
|
208
|
-
`Execution timeout after ${timeoutMs}ms
|
|
246
|
+
`Execution timeout after ${timeoutMs}ms. Pass a higher \`timeoutMs\` to vm.run() or set per-atom \`timeoutOverrides\` for slow IO atoms.`
|
|
209
247
|
)
|
|
210
248
|
)
|
|
211
249
|
}
|
|
@@ -219,7 +257,7 @@ export class AgentVM<M extends Record<string, Atom<any, any>>> {
|
|
|
219
257
|
controller.signal.aborted
|
|
220
258
|
) {
|
|
221
259
|
ctx.error = new AgentError(
|
|
222
|
-
`Execution timeout after ${timeoutMs}ms
|
|
260
|
+
`Execution timeout after ${timeoutMs}ms. Pass a higher \`timeoutMs\` to vm.run() or set per-atom \`timeoutOverrides\` for slow IO atoms.`,
|
|
223
261
|
'vm.run'
|
|
224
262
|
)
|
|
225
263
|
} else {
|