From 71de7331cbe4ee090a17af3e801ecf894699e653 Mon Sep 17 00:00:00 2001 From: Sebastian Herrlinger Date: Wed, 18 Nov 2026 05:12:43 +0100 Subject: [PATCH] test(codemode): record search parked inside running sync generators --- .../codemode-rust/conformance/streaming.json | 207 ++++++++++++++++++ .../codemode-rust/conformance/streaming.ts | 12 + 2 files changed, 219 insertions(+) diff --git a/packages/codemode-rust/conformance/streaming.json b/packages/codemode-rust/conformance/streaming.json index e35211cba98..a04237bad3d 100644 --- a/packages/codemode-rust/conformance/streaming.json +++ b/packages/codemode-rust/conformance/streaming.json @@ -16668,6 +16668,182 @@ }, "log": [] }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = (() => { function* g() { const a = S(1); yield a; yield a + 1 } const it = g(); return [it.next(), it.next(), it.next()] })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + { + "value": 1, + "done": false + }, + { + "value": 2, + "done": false + }, + { + "done": true + } + ], + [ + "call 1", + "job", + "answer 1" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = (() => { function* g() { yield S(1); yield S(2) + 1; return 9 } const out = []; for (const x of g()) out.push(x); return out })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + 1, + 2 + ], + [ + "call 1", + "job", + "answer 1", + "call 2", + "answer 2" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = (() => { function* g() { yield S(1); yield 5; yield S(2) } return [...g()] })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + 1, + 5, + 1 + ], + [ + "call 1", + "job", + "answer 1", + "call 2", + "answer 2" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = (() => { function* inner() { yield S(1); yield S(2) * 10 } const first = () => inner().next().value; function* outer() { yield* inner(); yield first() + 100; for (const x of inner()) yield x + 1000 } return [...outer()] })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + 1, + 10, + 101, + 1001, + 1010 + ], + [ + "call 1", + "job", + "answer 1", + "call 2", + "answer 2", + "call 1", + "answer 1", + "call 1", + "answer 1", + "call 2", + "answer 2" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = await (async () => { function* g() { yield S(1); yield S(2) + 1 } const it = g(); const a = it.next().value; await null; return [a, it.next().value, [...g()]] })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + 1, + 2, + [ + 1, + 2 + ] + ], + [ + "call 1", + "job", + "answer 1", + "call 2", + "answer 2", + "call 1", + "answer 1", + "call 2", + "answer 2" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = (() => { function* g() { try { yield S(1) } finally { yield 'cleanup' } } const it = g(); return [it.next(), it.return(4), it.next(), it.next()] })(); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + { + "value": 1, + "done": false + }, + { + "value": "cleanup", + "done": false + }, + { + "value": 4, + "done": true + }, + { + "done": true + } + ], + [ + "call 1", + "job", + "answer 1" + ] + ] + }, + "log": [] + }, + { + "code": "const log = []; const S = (x) => { log.push('call ' + x); const n = search({ query: Promise.resolve('echo') }).items.length; log.push('answer ' + x); return n }\n Promise.resolve().then(() => log.push('job')); const r = Array.from((function* () { yield S(1); yield S(2) })(), (x) => x + 1); return [r, log]", + "outcome": { + "ok": true, + "value": [ + [ + 2, + 2 + ], + [ + "call 1", + "job", + "answer 1", + "call 2", + "answer 2" + ] + ] + }, + "log": [] + }, { "code": "const r = await fetch({\"chunks\":[\"hel\",\"lo\"],\"status\":201,\"statusText\":\"Created\",\"headers\":{\"Content-Type\":\"text/plain\",\"X-A\":\"1\"}}); let flip = -1; let k = 0\n const see = (x) => { k++; if (flip < 0 && r.body.locked) flip = k; return x }\n const a = []; for (let i = 0; i < 30000; i++) if (i % 3) a[i] = i; a.length = 30000; const d = a.filter(() => true); \n for (let j = 0; j < 37; j++) await tools.t.echo({ p: Promise.resolve(j), a: [1, { b: 2 }, [3]], s: 'x' }); const p = r.text(); for (let i = 0; i < 30000; i++) see(i)\n await p.catch(() => 0); return flip", "outcome": { @@ -210799,6 +210975,37 @@ }, "log": [] }, + { + "code": "function* g() { yield search({ query: tools.wait({}).then(() => 'echo') }).items.length; yield 7 }\n const caller = (it) => [it.next().value, it.next().value, it.next().done]; return [caller(g()), [...g()].length]", + "suspend": { + "ok": true + }, + "resume": { + "ok": true, + "value": [ + [ + 1, + 7, + true + ], + 2 + ] + }, + "log": [] + }, + { + "code": "async function f() { function* g() { yield search({ query: tools.wait({}).then(() => 'echo') }).items.length }\n return [...g()] } return await f()", + "suspend": { + "ok": true + }, + "resume": { + "ok": true, + "value": [ + 1 + ] + }, + "log": [] + }, { "code": "const t = { toJSON: () => 9 }; const r = tools.t.echo({ t, n: [1, { w: tools.wait({}) }], z: t }); return await r", "suspend": { diff --git a/packages/codemode-rust/conformance/streaming.ts b/packages/codemode-rust/conformance/streaming.ts index daf4282d049..143fd5df26c 100644 --- a/packages/codemode-rust/conformance/streaming.ts +++ b/packages/codemode-rust/conformance/streaming.ts @@ -608,6 +608,14 @@ programs.push( "JSON.parse('[1,[2,{}],3]', (k, v) => (typeof v === 'number' ? S(v) + v : v))", "(function (x) { return S(x) + x + this.y }).call({ y: 10 }, 1)", "((x, y) => S(x) + x + y).apply(null, [1, 2])", + // Inside a running sync generator the park takes the generator and the frames driving it. + "(() => { function* g() { const a = S(1); yield a; yield a + 1 } const it = g(); return [it.next(), it.next(), it.next()] })()", + "(() => { function* g() { yield S(1); yield S(2) + 1; return 9 } const out = []; for (const x of g()) out.push(x); return out })()", + "(() => { function* g() { yield S(1); yield 5; yield S(2) } return [...g()] })()", + "(() => { function* inner() { yield S(1); yield S(2) * 10 } const first = () => inner().next().value; function* outer() { yield* inner(); yield first() + 100; for (const x of inner()) yield x + 1000 } return [...outer()] })()", + "await (async () => { function* g() { yield S(1); yield S(2) + 1 } const it = g(); const a = it.next().value; await null; return [a, it.next().value, [...g()]] })()", + "(() => { function* g() { try { yield S(1) } finally { yield 'cleanup' } } const it = g(); return [it.next(), it.return(4), it.next(), it.next()] })()", + "Array.from((function* () { yield S(1); yield S(2) })(), (x) => x + 1)", ].map(searching), ) @@ -664,6 +672,10 @@ const suspensions: Array = [ `function f() { return search({ query: tools.wait({}).then(() => 'echo') }).items.length } return [f(), 1]`, `return [1, 2].map((x) => search({ query: tools.wait({}).then(() => 'echo') }).items.length + x)`, `return await Promise.resolve(1).then((x) => search({ query: tools.wait({}).then(() => 'echo') }).items.length + x)`, + `function* g() { yield search({ query: tools.wait({}).then(() => 'echo') }).items.length; yield 7 } + const caller = (it) => [it.next().value, it.next().value, it.next().done]; return [caller(g()), [...g()].length]`, + `async function f() { function* g() { yield search({ query: tools.wait({}).then(() => 'echo') }).items.length } + return [...g()] } return await f()`, `const t = { toJSON: () => 9 }; const r = tools.t.echo({ t, n: [1, { w: tools.wait({}) }], z: t }); return await r`, ]