diff --git a/e2e/test-plan.ts b/e2e/test-plan.ts index 486c54ea03..9dab3784dd 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -8157,6 +8157,17 @@ export const scenarios = plan({ local: na("Hosted MCP scenario"), }, }, + mcpSearchKeptOrders: { + fixtures: "actors", + file: "mcp-search-kept-orders.spec.ts", + title: + "A search being paged keeps its order beside 40 new searches at once and beside a search too large to keep", + targets: { + "self-host": scheduled, + cloud: na("This scenario reads search spans from the self-host Motel collector."), + local: na("Hosted MCP scenario"), + }, + }, mcpSearchStatements: { fixtures: "actors", file: "mcp-search-statements.spec.ts", diff --git a/e2e/tests/mcp-execute-step-budget.spec.ts b/e2e/tests/mcp-execute-step-budget.spec.ts index 205d63cc79..01dae50bcd 100644 --- a/e2e/tests/mcp-execute-step-budget.spec.ts +++ b/e2e/tests/mcp-execute-step-budget.spec.ts @@ -9,7 +9,9 @@ * operators that walk or coerce a whole value. A value a call returns that the program already * holds, such as a group `Map.get` returns, is not charged, so grouping many rows into few groups * completes. Storing a value walks it once to reject cycles; that walk is charged, and a value - * holding no objects is not walked again, so attaching one shared value to every row completes. + * whose graph was walked is not walked again until something changes inside it, so attaching one + * shared value to every row completes, even one of 1,000 records, while storing a row into that + * shared value is still rejected as a cycle. * Storing the value a property already holds skips that walk, so grouping row objects with * `g[k] = g[k] || []` completes. Joining strings with `+` or a template is charged by the length * of the result, so a loop that joins a megabyte string stops in seconds. @@ -160,6 +162,26 @@ const rows = Array.from({ length: 20000 }, (_, i) => ({ id: i })); for (const r of rows) { r.meta = meta } return rows.length;`; +/** + * Attaches one shared value of 1,000 records, each an object, to each of 20,000 rows. Each store + * walked all 1,000 records, about 500 steps, and the program stopped at the budget halfway. + */ +const sharedRecordsMeta = ` +const meta = { records: Array.from({ length: 1000 }, (_, i) => ({ id: 'm' + i, label: 'L' + i, w: i })) }; +const rows = []; +for (let i = 0; i < 20000; i++) rows.push({ id: i, meta }); +return rows.length;`; + +/** + * Stores rows holding the shared value, then a row into one of its records: the walk must not stop + * at the shared value it walked before, because the record it changes is inside it. + */ +const sharedMetaCycle = ` +const meta = { records: Array.from({ length: 1000 }, (_, i) => ({ id: 'm' + i })) }; +const rows = []; +for (let i = 0; i < 10; i++) rows.push({ id: i, meta }); +try { meta.records[5].back = rows[3]; return 'stored'; } catch (e) { return String(e) }`; + /** * The review's proof that string `+` escaped the budget: joining a megabyte string to a number is * one step, and reading the result copies the megabyte. Uncharged, it ran to the wall clock. @@ -325,6 +347,16 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget const shared = yield* execute(runner, "shared-meta", sharedMeta); expect(shared.execution).toMatchObject({ ok: true, value: 20000 }); expect(shared.ms).toBeLessThan(20_000); + // Also when that value holds 1,000 records, each an object. + const sharedRecords = yield* execute(runner, "shared-records-meta", sharedRecordsMeta); + expect(sharedRecords.execution).toMatchObject({ ok: true, value: 20000 }); + expect(sharedRecords.ms).toBeLessThan(5_000); + // Storing a row into the shared value it already walked is still a cycle. + const cycle = yield* execute(runner, "shared-meta-cycle", sharedMetaCycle); + expect(cycle.execution).toMatchObject({ + ok: true, + value: expect.stringContaining("contains a circular value"), + }); // Grouping row objects by storing each group back into itself completes, in both forms. const groupedRows = yield* execute(runner, "group-rows-or-push", groupRowsOrPush); @@ -382,6 +414,7 @@ layer(HostedLive, { excludeTestServices: true })("Hosted MCP execute step budget yield* evidence.measure("group-by-50-ms", grouped50.ms); yield* evidence.measure("group-by-object-ms", groupedObject.ms); yield* evidence.measure("shared-meta-ms", shared.ms); + yield* evidence.measure("shared-records-meta-ms", sharedRecords.ms); yield* evidence.measure("concat-runaway-ms", concat.ms); yield* evidence.measure("group-rows-or-push-ms", groupedRows.ms); yield* evidence.measure("group-rows-reduce-ms", reducedRows.ms); diff --git a/e2e/tests/mcp-search-kept-orders.spec.ts b/e2e/tests/mcp-search-kept-orders.spec.ts new file mode 100644 index 0000000000..4529a6c008 --- /dev/null +++ b/e2e/tests/mcp-search-kept-orders.spec.ts @@ -0,0 +1,182 @@ +/** + * A search past its first page keeps its order for its later pages (see mcp-search-walk), in a + * cache of at most 32 searches and 6 MB. Two ways a new search could push out one still being + * paged: many new searches ranking at once each found room before their ranking and evicted the + * least recently read search after it, idle or not; and a search with a key too large to keep was + * remembered in the same cache with its key's bytes, past the 6 MB, which evicted every kept + * search. A search read in the last 30 s is now never evicted, and a search too large to keep is + * remembered apart from kept orders, or not at all when its key alone is too large. + */ +import { expect, layer } from "@effect/vitest"; +import { Effect, Schedule, Schema } from "effect"; +import { randomUUID } from "node:crypto"; +import { Actors } from "../support/actors.ts"; +import { Api, body } from "../support/api.ts"; +import { appsManifest } from "../support/apps-release.ts"; +import { HostedLive, withHostedCase } from "../support/case.ts"; +import { App } from "../support/contracts.ts"; +import { Evidence, Telemetry } from "../support/evidence.ts"; +import { McpClient } from "../support/mcp-client.ts"; +import { scenarios } from "../test-plan.ts"; + +const apps = 2; +const toolsPerApp = 60; + +/** An app of `toolsPerApp` small tools, generated when it evaluates. */ +const probeSource = ( + index: number, +) => `import { defineApp, object, query, router, string } from "apps"; +export default defineApp({}, async () => { + const tools = {}; + for (let t = 0; t < ${toolsPerApp}; t++) + tools["keptprobe" + t] = query( + { description: "Kept probe " + t + " of app ${index}", input: object({ id: string() }) }, + async () => t, + ); + return { tools: router(tools) }; +});`; + +/** Pages a search to its second page, so its order is kept, and returns the third page's request. */ +const secondPage = (query: string) => ` +const first = await tools.search({ query: ${JSON.stringify(query)}, limit: 10 }); +const second = await tools.search(first.next); +return second.next;`; + +/** Reads the page a request returned by {@link secondPage} names. */ +const thirdPage = (next: unknown) => ` +const third = await tools.search(${JSON.stringify(next)}); +return third.items.length;`; + +/** + * Pages one search to its second page, then pages a search whose 500 distinct 8,000-character + * words make a key of about 4 million characters, then reads the first search's third page. + */ +const huge = ` +const first = await tools.search({ query: 'kept probe again', limit: 10 }); +const second = await tools.search(first.next); +const words = Array.from({ length: 500 }, (_, i) => String(i).padStart(8000, 'w')); +const big = await tools.search({ query: 'kept probe ' + words.join(' '), limit: 10 }); +await tools.search(big.next); +const third = await tools.search(second.next); +return third.items.length;`; + +const Token = Schema.Struct({ key: Schema.RedactedFromValue(Schema.String), id: Schema.String }); +const Ran = Schema.Struct({ + structuredContent: Schema.Struct({ + status: Schema.Literal("completed"), + execution: Schema.Struct({ ok: Schema.Literal(true), value: Schema.Unknown }), + }), +}); + +layer(HostedLive, { excludeTestServices: true })("MCP search kept orders", (it) => { + it.effect(scenarios.mcpSearchKeptOrders.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const api = yield* Api, + actors = yield* Actors, + evidence = yield* Evidence, + telemetry = yield* Telemetry, + mcp = yield* McpClient; + const organization = actors.organization.id; + const prefix = `/api/organizations/${organization}`; + const run = randomUUID().slice(0, 8); + yield* Effect.forEach( + Array.from({ length: apps }, (_, index) => index), + (index) => + Effect.gen(function* () { + const deployed = yield* api.request(actors.owner, "POST", `${prefix}/apps/deploy`, { + name: `Kept orders ${index} ${run}`, + files: [{ path: "index.ts", content: probeSource(index) }, appsManifest], + }); + expect(deployed.status, JSON.stringify(deployed.body)).toBe(200); + const app = yield* body(App, deployed); + yield* Effect.addFinalizer(() => + api.request(actors.owner, "DELETE", `${prefix}/apps/${app.id}`).pipe( + Effect.map((response) => expect(response.status).toBe(200)), + Effect.orDie, + ), + ); + }), + { concurrency: 2, discard: true }, + ); + const created = yield* api.request(actors.owner, "POST", "/api/auth/api-key/create", { + name: "MCP search kept orders", + }); + expect(created.status).toBe(200); + const token = yield* body(Token, created); + yield* Effect.addFinalizer(() => + api + .request(actors.owner, "POST", "/api/auth/api-key/delete", { keyId: token.id }) + .pipe(Effect.orDie), + ); + const client = yield* mcp.connect(token.key, "pat", { organization }); + + /** Run a program and return its value. */ + const execute = (operation: string, code: string) => + client + .use(operation, (client, signal) => + client.callTool({ name: "execute", arguments: { code } }, undefined, { signal }), + ) + .pipe( + Effect.flatMap(Schema.decodeUnknownEffect(Ran)), + Effect.map((ran) => ran.structuredContent.execution.value), + ); + /** + * Run a program alone and read its value and how each of its searches got its page, in + * order, from the trace of the last MCP request. + */ + const orders = (operation: string, code: string, searches: number) => + Effect.gen(function* () { + const value = yield* execute(operation, code); + const request = (yield* evidence.requests) + .filter((request) => request.path === "/mcp") + .at(-1); + if (request === undefined) + return yield* Effect.die(new Error("MCP request evidence missing")); + const traced = yield* telemetry.query(request.traceId).pipe( + Effect.flatMap((result) => { + const spans = result.data + .map(({ span }) => span) + .filter((span) => span.operationName === "sdk.tools.search") + .sort((a, b) => a.startTime.localeCompare(b.startTime)) + .map((span) => span.tags["executor.search.order"] ?? ""); + return spans.length === searches + ? Effect.succeed(spans) + : Effect.fail(new Error("Every search span must reach Motel")); + }), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + Effect.timeout("25 seconds"), + ); + return { value, orders: traced }; + }); + + // A search with a key too large to keep ranks each page and evicts nothing. + const large = yield* orders("Page a search beside one too large to keep", huge, 5); + yield* evidence.json("huge-orders.json", large.orders); + expect(large.value).toBe(10); + expect(large.orders).toEqual(["ranked", "ordered", "ranked", "ranked", "kept"]); + + // Forty new searches keeping their orders at once leave the one being paged kept. + const paged = yield* orders( + "Page a search to its second page", + secondPage("kept probe"), + 2, + ); + expect(paged.orders).toEqual(["ranked", "ordered"]); + yield* Effect.forEach( + Array.from({ length: 40 }, (_, index) => index), + (index) => execute(`Page new search ${index}`, secondPage(`kept probe z${index}`)), + { concurrency: "unbounded", discard: true }, + ); + const third = yield* orders( + "Read the paged search's third page", + thirdPage(paged.value), + 1, + ); + expect(third.value).toBe(10); + expect(third.orders, "The search being paged keeps its order").toEqual(["kept"]); + }).pipe(Effect.provide(McpClient.layer)), + ), + ); +}); diff --git a/packages/sdk/src/implementation/tool-search-store.ts b/packages/sdk/src/implementation/tool-search-store.ts index 992907f7ae..4c2b9229d5 100644 --- a/packages/sdk/src/implementation/tool-search-store.ts +++ b/packages/sdk/src/implementation/tool-search-store.ts @@ -131,22 +131,28 @@ interface Kept { /** What the entry holds, its key included, as counted against {@link keptBytes}. */ readonly bytes: number; } -/** A search whose order is larger than {@link keptOrderBytes}: its pages rank as before. */ -interface Oversized { - readonly bytes: number; -} -/** Most searches kept at once. */ +/** Most searches kept at once, and most searches remembered as too large to keep. */ const keptOrders = 32; -/** Most bytes all kept searches hold together; the least recently used go first. */ +/** Most bytes all kept searches hold together. */ const keptBytes = 6_000_000; -/** Most bytes one kept search holds: about 160,000 tools. A larger one is not kept. */ +/** + * Most bytes one kept search holds: about 160,000 tools. A larger one is not kept, and a search + * whose key alone is larger is neither kept nor remembered: each of its pages ranks, as before. + */ const keptOrderBytes = 2_000_000; +/** Most bytes the keys of searches remembered as too large to keep hold together. */ +const oversizedBytes = 2_000_000; /** - * How long a kept search goes unread before a new one may evict it. While every kept search is - * being paged, a new one is not kept: its pages rank as before, so more concurrent walks than - * {@link keptOrders} cost what they did before orders were kept, instead of evicting each other. + * How long a kept search goes unread before a new one may evict it; a search read since is never + * evicted. While every kept search is being paged, a new one is not kept: its pages rank as + * before, so more concurrent walks than {@link keptOrders} cost what they did before orders were + * kept, instead of evicting each other. Once 32 searches have each been read within 30 s, or + * their orders fill {@link keptBytes}, every further walk goes uncached until one of them idles. + * Times are monotonic, so a wall-clock step neither evicts a search in use nor keeps an idle one. */ const keptIdleMillis = 30_000; +/** Milliseconds on the monotonic clock, for {@link keptIdleMillis}. */ +const monotonicMillis = Effect.map(Clock.monotonicTimeNanos, (nanos) => Number(nanos / 1_000_000n)); /** Bytes an entry takes beyond its arrays: its key, stamp and object headers. */ const entryBytes = (key: string, stamp: string, missing: number) => 256 + 2 * (key.length + stamp.length) + 8 * missing; @@ -191,30 +197,70 @@ export const supersededRetentionMillis = 60 * 60_000; const BM25 = { k1: 1.2, b: 0.75 } as const; export const makeToolSearchStore = (sql: SqlClient.SqlClient) => { - /** Kept orders by search, least recently used first; see {@link Kept}. */ - const orders = new Map(); + /** Kept orders by search, least recently read first; see {@link Kept}. */ + const orders = new Map(); let ordersBytes = 0; + /** + * Searches whose order is larger than {@link keptOrderBytes}, least recently read first, with + * the bytes their key takes: their pages rank without the order. They are bounded apart from + * {@link orders}, so remembering one never evicts a kept order. + */ + const oversized = new Map(); + let oversizedHeld = 0; const forget = (key: string) => { const kept = orders.get(key); if (kept === undefined) return; orders.delete(key); - ordersBytes -= kept.entry.bytes; + ordersBytes -= kept.order.bytes; }; - /** Keep an entry as the most recently used, evicting the least recently used past the bounds. */ - const keep = (key: string, entry: Kept | Oversized, used: number) => { - forget(key); - orders.set(key, { entry, used }); - ordersBytes += entry.bytes; - for (const old of orders.keys()) { - if (orders.size <= keptOrders && ordersBytes <= keptBytes) break; - forget(old); + /** Mark a kept order read now, as the most recently read. */ + const touch = (key: string, order: Kept, used: number) => { + orders.delete(key); + orders.set(key, { order, used }); + }; + /** + * The searches to evict for an order of `bytes` to fit, least recently read first, or nothing + * when it fits only by evicting a search read in the last {@link keptIdleMillis}. + */ + const evictable = (bytes: number, now: number) => { + let count = orders.size + 1; + let held = ordersBytes + bytes; + const idle: Array = []; + for (const [key, { order, used }] of orders) { + if (count <= keptOrders && held <= keptBytes) break; + if (now - used < keptIdleMillis) continue; + idle.push(key); + count -= 1; + held -= order.bytes; } + return count <= keptOrders && held <= keptBytes ? idle : undefined; + }; + /** + * Keep an order if it fits by evicting only idle searches. It is decided when the order is in + * hand, after the ranking it waited on, since other searches may have been kept meanwhile. + */ + const admit = (key: string, order: Kept, now: number) => { + forget(key); + const idle = evictable(order.bytes, now); + if (idle === undefined) return; + for (const old of idle) forget(old); + orders.set(key, { order, used: now }); + ordersBytes += order.bytes; }; - /** Whether a new order fits without evicting a search read in the last `keptIdleMillis`. */ - const room = (now: number) => { - if (orders.size < keptOrders && ordersBytes + keptOrderBytes <= keptBytes) return true; - const [oldest] = orders.values(); - return oldest !== undefined && now - oldest.used >= keptIdleMillis; + /** Remember a search as too large to keep, dropping the least recently read past the bound. */ + const remember = (key: string, bytes: number) => { + const held = oversized.get(key); + if (held !== undefined) { + oversized.delete(key); + oversizedHeld -= held; + } + oversized.set(key, bytes); + oversizedHeld += bytes; + for (const [old, oldBytes] of oversized) { + if (oversized.size <= keptOrders && oversizedHeld <= oversizedBytes) break; + oversized.delete(old); + oversizedHeld -= oldBytes; + } }; const failed = (effect: Effect.Effect) => effect.pipe(Effect.mapError(() => new StorageError())); @@ -666,24 +712,24 @@ export const makeToolSearchStore = (sql: SqlClient.SqlClient) => { return pageOf(yield* ranked()); } const key = JSON.stringify([input.targets, input.terms]); - const now = yield* Clock.currentTimeMillis; - const remembered = orders.get(key)?.entry; - if (remembered !== undefined && !("cells" in remembered)) { + const keyBytes = entryBytes(key, "", 0); + if (keyBytes > keptOrderBytes || oversized.has(key)) { // Too large to keep: each page ranks, as before. - keep(key, remembered, now); + if (oversized.has(key)) remember(key, keyBytes); yield* Effect.annotateCurrentSpan("executor.search.order", "ranked"); return pageOf(yield* ranked()); } + const remembered = orders.get(key)?.order; if (remembered !== undefined) { const page = yield* kept(remembered); if (page !== undefined) { - keep(key, remembered, now); + touch(key, remembered, yield* monotonicMillis); yield* Effect.annotateCurrentSpan("executor.search.order", "kept"); return page; } forget(key); } - if (!room(now)) { + if (evictable(keptOrderBytes, yield* monotonicMillis) === undefined) { // Every kept search is in use: rank this page as before rather than evict one. yield* Effect.annotateCurrentSpan("executor.search.order", "ranked"); return pageOf(yield* ranked()); @@ -702,13 +748,14 @@ export const makeToolSearchStore = (sql: SqlClient.SqlClient) => { parsed === undefined ? undefined : overhead + parsed.cells.byteLength + parsed.extras.byteLength; - keep( - key, - parsed === undefined || bytes === undefined || bytes > keptOrderBytes - ? { bytes: entryBytes(key, "", 0) } - : { total: row.total, missing: row.missing, stamp: row.stamp, ...parsed, bytes }, - now, - ); + if (parsed === undefined || bytes === undefined || bytes > keptOrderBytes) + remember(key, keyBytes); + else + admit( + key, + { total: row.total, missing: row.missing, stamp: row.stamp, ...parsed, bytes }, + yield* monotonicMillis, + ); return pageOf(row); }).pipe(failed), }; diff --git a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch index 375ee40b51..11d47e67e6 100644 --- a/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch +++ b/patches/@opencode-ai%2Fcodemode@0.0.0-dev-19272.patch @@ -769,10 +769,10 @@ index 7b1492c21fbe95d6b995327ad7aec31ba345bf6b..a2ebe70b30121ff5e4b6ca60e0b6109a +export declare const rejectCircularInsertion: (container: object, value: unknown, label: string, node: AstNode, seen?: Set, meter?: (visited: number) => void) => void; export declare const typeofValue: (value: unknown) => string; diff --git a/dist/interpreter/references.js b/dist/interpreter/references.js -index 314a7c3752745de0b8680531b64cf37bc584a812..a4bf237d4e716d3259b0f617695268cc47c5b289 100644 +index 314a7c3752745de0b8680531b64cf37bc584a812..7ed7bd6c4563e35524453655e9f161b1256ffc7d 100644 --- a/dist/interpreter/references.js +++ b/dist/interpreter/references.js -@@ -70,23 +70,82 @@ export const containsOpaqueReference = (value) => { +@@ -70,23 +70,110 @@ export const containsOpaqueReference = (value) => { } return false; }; @@ -797,10 +797,7 @@ index 314a7c3752745de0b8680531b64cf37bc584a812..a4bf237d4e716d3259b0f617695268cc + meter(visited); + visited = 0; + } - const current = next.value; -- if (current === container) -- throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue"); -- if (current === null || typeof current !== "object" || isRuntimeReference(current) || seen.has(current)) ++ const current = next.value; + if (isCodeModeValue(current)) + continue; + if (isRuntimeReference(current)) { @@ -808,53 +805,83 @@ index 314a7c3752745de0b8680531b64cf37bc584a812..a4bf237d4e716d3259b0f617695268cc + return true; + } + if (current === null || typeof current !== "object" || seen.has(current)) - continue; - seen.add(current); - pending.push(childValues(current)); - } ++ continue; ++ seen.add(current); ++ pending.push(childValues(current)); ++ } + meter(visited); + return false; +}; +/** An interpreter reference that is not CodeMode data, such as a function or the tools root. */ +export const isOpaqueReference = (value) => !isCodeModeValue(value) && isRuntimeReference(value); +// Reject cycles before mutation so later boundary walks remain safe. -+// Data objects whose own values are all primitives or interpreter references, so a cycle walk -+// that reaches one can stop there: the container it looks for is a data object and cannot be among -+// them. An object leaves the set when an object is inserted into it, and every insertion of a -+// value into existing program data runs rejectCircularInsertion first, so membership stays exact. -+const leafData = new WeakSet(); ++// Data objects a cycle walk has already checked, so a later walk can stop at one. The invariant: ++// every data object inside a walked object is walked too, so the container a walk looks for, which ++// it removes from `walked` first, is never inside one. A leaf, an object holding no objects, maps ++// to LEAF and stays walked until an object is stored into it. Any other object maps to the epoch ++// it was walked in and counts as walked only during that epoch. Every insertion of a value into ++// existing program data runs rejectCircularInsertion first, so a store into a walked object that ++// sits inside another walked object (`insideWalked`) starts a new epoch, which drops every ++// non-leaf entry at once, as their graphs may now hold new values. A store into an object no ++// walked object holds, such as the array rows are pushed into, keeps the epoch. ++const LEAF = -1; ++const walked = new WeakMap(); ++const insideWalked = new WeakSet(); ++let walkEpoch = 0; ++const isWalked = (value) => { ++ const epoch = walked.get(value); ++ return epoch !== undefined && (epoch === LEAF || epoch === walkEpoch); ++}; +// The walk visits each value once and reports visits to `meter` in batches of METER_BATCH, so the -+// caller can charge it and stop it partway; a leaf seen before costs one visit. ++// caller can charge it and stop it partway; a walked object costs one visit. +export const rejectCircularInsertion = (container, value, label, node, seen = new Set(), meter) => { + if (value === null || typeof value !== "object") + return; -+ leafData.delete(container); -+ const pending = [{ children: [value].values(), owner: undefined, leaf: true }]; ++ if (insideWalked.has(container) && isWalked(container)) ++ walkEpoch++; ++ walked.delete(container); ++ const pending = [{ children: [value].values(), owner: undefined, leaf: true, complete: true }]; + let visited = 0; + while (pending.length > 0) { + const frame = pending.at(-1); + const next = frame.children.next(); + if (next.done) { + pending.pop(); -+ if (frame.owner !== undefined && frame.leaf) -+ leafData.add(frame.owner); ++ if (frame.owner === undefined) ++ continue; ++ if (frame.complete) ++ walked.set(frame.owner, frame.leaf ? LEAF : walkEpoch); ++ else ++ pending.at(-1).complete = false; + continue; + } + if (meter !== undefined && ++visited === METER_BATCH) { + meter(visited); + visited = 0; + } -+ const current = next.value; -+ if (current === container) -+ throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue"); + const current = next.value; + if (current === container) + throw new InterpreterRuntimeError(`${label} contains a circular value.`, node, "InvalidDataValue"); +- if (current === null || typeof current !== "object" || isRuntimeReference(current) || seen.has(current)) + if (current === null || typeof current !== "object" || isRuntimeReference(current)) + continue; + frame.leaf = false; -+ if (seen.has(current) || leafData.has(current)) ++ // Only a walked object's own values need the mark, and the stored value's parent is the ++ // container, which is not walked; marking every child of a frame keeps the check simple. ++ if (frame.owner !== undefined) ++ insideWalked.add(current); ++ if (isWalked(current)) + continue; ++ // Seen earlier in this walk, or by the caller, without being marked walked: its owner ++ // cannot be marked either. ++ if (seen.has(current)) { ++ frame.complete = false; + continue; -+ seen.add(current); -+ pending.push({ children: childValues(current), owner: current, leaf: true }); -+ } ++ } + seen.add(current); +- pending.push(childValues(current)); ++ pending.push({ children: childValues(current), owner: current, leaf: true, complete: true }); + } + if (meter !== undefined) + meter(visited); }; diff --git a/patches/codemode-step-budget.md b/patches/codemode-step-budget.md index 16a1cca4f1..b3b621c3bc 100644 --- a/patches/codemode-step-budget.md +++ b/patches/codemode-step-budget.md @@ -80,15 +80,25 @@ Operators and patterns that read a whole value are charged too: - Storing a value walks it to reject cycles: assignment to a property or index, `push`, `unshift`, `splice`, `fill` and `Object.assign`. `switch` walks its discriminant and case values to reject interpreter references. Both walks are charged as they go, one step per 2 - values visited. The store walk also remembers, in a `WeakSet`, every object it finds holding no - objects, and stops there next time; storing an object into one removes it, and every store into - existing data runs this walk first, so the walk still finds every cycle. The one store that - skips it is a property or index assignment of the value the slot already holds, which cannot - change the graph: `g[k] = g[k] || []` followed by `g[k].push(row)` walked the whole group, every - row in it, on every row, and grouping 50,000 row objects stopped at the budget in about 2 s. Before this, attaching - one shared `meta` of 50,000 columns to each of 20,000 rows walked all the columns on each store, - uncharged, and ran for minutes; it now takes 0.3 s. Grouping 50,000 rows into an object with - `a = g[k] || []; a.push(v); g[k] = a` walked each group on each store; it now takes 0.7 s. + values visited. The store walk also remembers every object whose whole graph it has checked + and stops there next time, as the container it looks for is never inside one. An object holding + no objects stays remembered until an object is stored into it. Any other object is remembered for + the current epoch: storing an object into a remembered object that another remembered object + holds starts a new epoch, which forgets every non-leaf object at once, because their graphs may + now reach the new value. Every store into existing data runs this walk first, so the walk still + finds every cycle; a differential fuzz of 3.9 million stores against a fresh reachability check + agreed on all of them, and fails at once when the epoch is not advanced. The one store that + skips the walk is a property or index assignment of the value the slot already holds, which + cannot change the graph: `g[k] = g[k] || []` followed by `g[k].push(row)` walked the whole group, + every row in it, on every row, and grouping 50,000 row objects stopped at the budget in about 2 s. + Before this, attaching one shared `meta` of 50,000 columns to each of 20,000 rows walked all the + columns on each store, uncharged, and ran for minutes; it now takes 0.3 s. A shared `meta` of + 1,000 records, each an object, was walked record by record on every store, about 500 steps + each and 10 million for 20,000 rows, and stopped at the budget in about 1 s; it is now walked + once and the 20,000 stores take 0.25 s in Node. Grouping 50,000 rows into an object with `a = g[k] || []; a.push(v); g[k] = a` walked + each group on each store; it now takes 0.7 s. A program that changes the shared value between + stores, such as setting `meta.records[i].seen` for each row, still has it walked again on each + store, one step per 2 records. - A template interpolating an array or object is charged for copying and joining it. - Rest patterns: an object rest is charged for the object, an array rest one step per element it takes. `for...in` is charged for its key list.