diff --git a/packages/app/e2e/regression/tabs.spec.ts b/packages/app/e2e/regression/tabs.spec.ts index dddb9d25374a..5966c6750dda 100644 --- a/packages/app/e2e/regression/tabs.spec.ts +++ b/packages/app/e2e/regression/tabs.spec.ts @@ -504,7 +504,7 @@ test("option arrows move to the previous or next tab, and with shift to the next await expectPath(page, sessionHref(c.id)) }) -for (const navigation of ["horizontal", "vertical", "keyboard"] as const) { +for (const navigation of ["horizontal", "vertical", "keyboard", "numbered"] as const) { test(`switching session tabs restores the caret and pastes into the destination draft (${navigation})`, async ({ page, context, @@ -525,14 +525,24 @@ for (const navigation of ["horizontal", "vertical", "keyboard"] as const) { const tabB = strip.locator(`[data-titlebar-tab-link][href="${sessionHref(b.id)}"]`) await editor.fill("Draft A") - await (navigation === "keyboard" ? page.keyboard.press("Control+Alt+ArrowRight") : tabB.click()) + if (navigation === "keyboard") await page.keyboard.press("Control+Alt+ArrowRight") + + if (navigation === "numbered") { + // A titlebar control stays mounted across sessions, so it still holds focus when the destination composer mounts. + const home = page.locator('[data-slot="titlebar-v2"]').getByRole("button", { name: "Home", exact: true }) + await home.focus() + await expect(home).toBeFocused() + await page.keyboard.press("ControlOrMeta+2") + } + + if (navigation === "horizontal" || navigation === "vertical") await tabB.click() await expectPath(page, sessionHref(b.id)) await expect(editor).toBeFocused() await page.evaluate(() => navigator.clipboard.writeText("Draft B")) await page.keyboard.press("ControlOrMeta+V") await expect(editor).toHaveText("Draft B") - await page.keyboard.press("Control+Alt+ArrowLeft") + await page.keyboard.press(navigation === "numbered" ? "ControlOrMeta+1" : "Control+Alt+ArrowLeft") await expectPath(page, sessionHref(a.id)) await expect(editor).toBeFocused() await expect(editor).toHaveText("Draft A") @@ -540,7 +550,7 @@ for (const navigation of ["horizontal", "vertical", "keyboard"] as const) { await page.keyboard.press("ControlOrMeta+V") await expect(editor).toHaveText("Draft A appended") - await tabB.click() + await (navigation === "numbered" ? page.keyboard.press("ControlOrMeta+2") : tabB.click()) await expectPath(page, sessionHref(b.id)) await expect(editor).toBeFocused() await expect(editor).toHaveText("Draft B") @@ -814,7 +824,7 @@ test("Recent shows the last 7 days, and Show more reveals 30 more rows, fetching await expect(more).toBeHidden() }) -test("Recent keeps the server's order from its last reload, which a run end or window focus triggers", async ({ +test("Recent keeps the server's order through renames, run ends and window focus, and reloads for a new session", async ({ page, }) => { const t = Date.now() - 60_000 @@ -831,6 +841,11 @@ test("Recent keeps the server's order from its last reload, which a run end or w }, }) + let pageOneLoads = 0 + page.on("request", (request) => { + if (/\/api\/session\?.*archived=false/.test(request.url())) pageOneLoads++ + }) + await page.clock.install() await page.goto( `/e2e/utils/windows-menu.html?${new URLSearchParams({ server: SERVER, path: sessionHref(a.id, "sidecar") })}`, ) @@ -864,22 +879,57 @@ test("Recent keeps the server's order from its last reload, which a run end or w await expect(rows.first()).toContainText("Tab B session") await expect(rows.last()).toContainText("Renamed A session") - // The window coming back reloads the list, as the iOS app does when it returns to the foreground. - await page.evaluate(() => window.dispatchEvent(new Event("focus"))) - await expect(rows).toHaveText([/Renamed A session/, /Tab B session/]) - - advance(b.id, { updated: t + 3, idle: t + 3 }) + // A run end and the window coming back leave the order alone; the rename after the run end shows it arrived. + advance(a.id, { updated: t + 3, idle: t + 3 }, "Finished A session") await workspace.push([ { id: "evt_recent_order_run_end", created: t + 3, type: "session.execution.succeeded", location: { directory: workspace.directory }, - durable: { aggregateID: b.id, seq: 1, version: 1 }, - data: { sessionID: b.id }, + durable: { aggregateID: a.id, seq: 2, version: 1 }, + data: { sessionID: a.id }, } satisfies Extract, + { + id: "evt_recent_order_rename_after_run", + created: t + 3, + type: "session.renamed", + location: { directory: workspace.directory }, + durable: { aggregateID: a.id, seq: 3, version: 1 }, + data: { sessionID: a.id, title: "Finished A session" }, + } satisfies Extract, + ]) + await expect(rows).toHaveText([/Tab B session/, /Finished A session/]) + await page.evaluate(() => { + window.dispatchEvent(new Event("focus")) + document.dispatchEvent(new Event("visibilitychange")) + }) + // Past the reload debounce, so a reload either trigger had queued would have requested page one by now. + await page.clock.runFor(1_000) + await expect(rows).toHaveText([/Tab B session/, /Finished A session/]) + expect(pageOneLoads).toBe(1) + + // A new root session reloads page one, which brings in A's newer server time too. + const created = session({ id: "ses_tab_new", title: "Fresh session", directory: workspace.directory, created: t + 4 }) + workspace.sessions.push(created) + await workspace.push([ + { + id: "evt_recent_order_created", + created: t + 4, + type: "session.created", + location: { directory: workspace.directory }, + durable: { aggregateID: created.id, seq: 1, version: 1 }, + data: { + sessionID: created.id, + projectID: workspace.projectID, + location: { directory: workspace.directory }, + slug: created.slug, + version: created.version, + }, + } satisfies Extract, ]) - await expect(rows).toHaveText([/Tab B session/, /Renamed A session/]) + await expect(rows).toHaveText([/Fresh session/, /Finished A session/, /Tab B session/]) + expect(pageOneLoads).toBe(2) }) test("an unfocused window preserves a restored manual unread reminder", async ({ page }) => { diff --git a/packages/app/src/components-lab/dialog-story-runtime.tsx b/packages/app/src/components-lab/dialog-story-runtime.tsx index 23b0a41d06e1..8ad8d06cc8a0 100644 --- a/packages/app/src/components-lab/dialog-story-runtime.tsx +++ b/packages/app/src/components-lab/dialog-story-runtime.tsx @@ -1,4 +1,4 @@ -import type { IntegrationInfo } from "@opencode/client/promise" +import type { IntegrationInfo, PluginInfo } from "@opencode/client/promise" import { QueryClient, QueryClientProvider } from "@tanstack/solid-query" import type { ParentProps } from "solid-js" import { LocalProvider } from "@/providers/models/selection" @@ -503,6 +503,28 @@ type DialogStoryRuntimeOptions = { const REDIRECT_PASTE = ["openai", "gitlab", "snowflake-cortex"] +/** Package plugins for the Plugins tab: two with a newer version, so its update controls show. */ +const storyPlugins: PluginInfo[] = [ + { + id: "wakatime", + source: { type: "package", target: "opencode-wakatime@latest", version: "1.2.0", outdated: true }, + features: { server: true }, + state: { status: "active" }, + }, + { + id: "notifier", + source: { type: "package", target: "opencode-notifier@latest", version: "0.4.1", outdated: true }, + features: { server: true, tui: true }, + state: { status: "active" }, + }, + { + id: "review", + source: { type: "package", target: "@acme/opencode-review@latest", version: "2.0.3" }, + features: { server: true }, + state: { status: "active" }, + }, +] + export function createStoryFetch(overrides: IntegrationInfo[] = []): typeof fetch { const integrations = [ ...overrides, @@ -715,6 +737,14 @@ export function createStoryFetch(overrides: IntegrationInfo[] = []): typeof fetc if (pathname === "/api/session/active") return jsonResponse({ data: {} }) + if (pathname === "/api/plugin") return jsonResponse({ location, data: storyPlugins, ready: true }) + + if (pathname === "/api/plugin/check") return jsonResponse({ location, data: storyPlugins }) + + // Long enough to see Updating; the list keeps its flags, as a server whose check has not caught up would. + if (pathname === "/api/plugin/update") + return new Promise((resolve) => setTimeout(() => resolve(noContentResponse()), 1500)) + if (pathname === "/api/config" || pathname === "/api/config/shell") return jsonResponse([]) if (pathname === "/api/mcp/resource") return jsonResponse({ location, data: { resources: [], templates: [] } }) diff --git a/packages/app/src/runtime/i18n/en.ts b/packages/app/src/runtime/i18n/en.ts index ab2712371296..09217244b779 100644 --- a/packages/app/src/runtime/i18n/en.ts +++ b/packages/app/src/runtime/i18n/en.ts @@ -927,6 +927,11 @@ export const dict = { "sidebar.view.markAllRead": "Mark all as read", "sidebar.view.noMatch": "No other sessions match", "settings.plugins.reload": "Reload", + "settings.plugins.check": "Check for updates", + "settings.plugins.checking": "Checking for updates", + "settings.plugins.update": "Update", + "settings.plugins.updating": "Updating", + "settings.plugins.update.label": "Update {{name}}", "settings.plugins.status.loading": "Loading", "settings.plugins.status.failed": "Failed", "settings.plugins.error.notFound": "Plugin not found", diff --git a/packages/app/src/runtime/plugin/extension-plugins.tsx b/packages/app/src/runtime/plugin/extension-plugins.tsx index 27cdb45f7268..c8b58897bdfa 100644 --- a/packages/app/src/runtime/plugin/extension-plugins.tsx +++ b/packages/app/src/runtime/plugin/extension-plugins.tsx @@ -10,16 +10,28 @@ import { useLanguage } from "@/runtime/i18n/language" import { usePlatform } from "@/runtime/platform/platform" import { SettingsList } from "@/settings/list" import { showToast } from "@/shell/notifications/toast" +import type { PluginUpdateView } from "@/settings/providers/plugin-update-machine" import "@/settings/settings.css" import { builtins } from "./builtins" import { usePluginHost } from "./host" import { useReservedPlugins } from "./root" +/** Update controls for the listed plugins' packages; without them the rows offer no update. */ +export type ExtensionPluginUpdates = { + readonly view: (source: Extract) => PluginUpdateView + readonly update: (target: string) => void +} + /** * The plugins a server lists, for the Plugins tab of Extensions. Only the desktop app's own server (`own`) runs its * plugins' GUIs in this window, so only its rows add the window's controls; other servers and the web list names. + * Any server's package plugins can be updated. */ -export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: boolean }) { +export function ExtensionPlugins(props: { + plugins: readonly PluginInfo[] + own: boolean + updates?: ExtensionPluginUpdates +}) { const listed = () => props.plugins.filter((plugin) => plugin.source.type !== "builtin") return ( @@ -34,13 +46,14 @@ export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: b {pluginLabel(plugin)} + )} } > - + ) } @@ -50,7 +63,7 @@ export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: b * switch that turns off only the desktop UI, Reload, status and errors, and rows for plugins main lists that the * server list does not name. Built-ins always run and stay hidden unless they failed, which only this list reports. */ -function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { +function OwnServerPlugins(props: { plugins: readonly PluginInfo[]; updates?: ExtensionPluginUpdates }) { const language = useLanguage() const host = usePluginHost() const bridge = usePlatform().plugins @@ -136,7 +149,7 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { run(bridge?.manager.reload(id)) } - const row = (input: { id: string | undefined; name: string; builtin?: boolean }) => { + const row = (input: { id: string | undefined; name: string; builtin?: boolean; plugin?: PluginInfo }) => { const state = () => (input.builtin ? "loaded" : kind(input.id)) const part = input.builtin ? "builtin" : "config" const live = () => (state() === "loaded" ? input.id : undefined) @@ -184,9 +197,15 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { + + {(plugin) => } + {(id) => (
+ + {(plugin) => } + @@ -206,7 +225,7 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { return ( <> - {(plugin) => row({ id: plugin.id, name: pluginLabel(plugin) })} + {(plugin) => row({ id: plugin.id, name: pluginLabel(plugin), plugin })} {(id) => row({ id, name: id })} {(id) => row({ id, name: id, builtin: true })} @@ -217,6 +236,38 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { ) } +/** Update for a package plugin with a newer version, or the update in progress; nothing otherwise. */ +function PluginUpdateButton(props: { plugin: PluginInfo; updates?: ExtensionPluginUpdates }) { + const language = useLanguage() + + const view = createMemo(() => { + const source = props.plugin.source + + if (source.type !== "package" || !props.updates) return undefined + + const current = props.updates.view(source) + + return current.outdated || current.updating ? { ...current, target: source.target } : undefined + }) + + return ( + + {(current) => ( + + )} + + ) +} + /** A plugin's error: its first line, the message, with the rest (such as a stack trace) behind a toggle. */ function PluginError(props: { error: string }) { const language = useLanguage() diff --git a/packages/app/src/settings/providers/extensions.tsx b/packages/app/src/settings/providers/extensions.tsx index 56533f7f082e..d57e39944ce7 100644 --- a/packages/app/src/settings/providers/extensions.tsx +++ b/packages/app/src/settings/providers/extensions.tsx @@ -1,5 +1,16 @@ import type { McpServer } from "@opencode/client/promise" -import { Component, For, Show, createEffect, createMemo, onCleanup, type JSX } from "solid-js" +import { + Component, + For, + Show, + createEffect, + createMemo, + createSignal, + on, + onCleanup, + untrack, + type JSX, +} from "solid-js" import { useDialog } from "@opencode/ui/context/dialog" import { Icon } from "@opencode/ui/icon" import { Button } from "@opencode/ui/button" @@ -10,6 +21,8 @@ import { useLanguage } from "@/runtime/i18n/language" import { useData } from "@/runtime/server/current" import { useServerSDK } from "@/runtime/server/client" import { ServerConnection } from "@/runtime/server/registry" +import { formatServerError } from "@/runtime/server/errors" +import { showToast } from "@/shell/notifications/toast" import { useMcpController } from "@/providers/connect/mcp" import { mcpRowView } from "@/providers/connect/mcp-machine" import { DialogMcp } from "@/providers/connect/mcp-dialog" @@ -20,6 +33,14 @@ import { SettingsList } from "@/settings/list" import { createExtensionLoad, ExtensionLoadFailed } from "@/settings/extension-load" import type { SettingsView } from "@/settings/surface" import { ConsoleName, ConsoleReplaced, createConsoleSources } from "./console-link" +import { + createPluginUpdateState, + pluginUpdateChecking, + pluginUpdateView, + transitionPluginUpdate, + type PluginUpdateEffect, + type PluginUpdateEvent, +} from "./plugin-update-machine" import "@/settings/settings.css" import "@/shell/search-list.css" @@ -72,11 +93,9 @@ export const SettingsExtensions: Component<{ task: (_, signal) => serverSdk.api.plugin.list(undefined, { signal }).then((result) => result.data), }) - onCleanup( - serverSdk.event.on("plugin.updated", (event) => { - if (!event.location) plugins.invalidate() - }), - ) + // Plugin registries are per Location, so every `plugin.updated` names one; the default Location's names its resolved + // directory rather than none, and a filter on a missing location never matched. Listing again is cheap and coalesced. + onCleanup(serverSdk.event.on("plugin.updated", () => plugins.invalidate())) const pluginList = createMemo(() => { const current = plugins.view() @@ -84,6 +103,66 @@ export const SettingsExtensions: Component<{ return current.status === "ready" ? current.value : undefined }) + const [updateState, setUpdateState] = createSignal(createPluginUpdateState()) + + const dispatchUpdate = (event: PluginUpdateEvent) => { + const previous = untrack(updateState) + const result = transitionPluginUpdate(previous, event) + + if (result.state !== previous) setUpdateState(() => result.state) + result.effects.forEach(runUpdate) + } + + const runUpdate = (effect: PluginUpdateEffect) => { + if (effect.type === "refresh") return plugins.invalidate() + + if (effect.type === "failed") + return showToast({ + title: language.t("common.requestFailed"), + description: effect.message, + }) + + if (effect.type === "check") { + const attempt = effect.attempt + + return void serverSdk.api.plugin.check().then( + (result) => + dispatchUpdate({ + type: "check.succeeded", + attempt, + outdated: Object.fromEntries( + result.data.flatMap((plugin) => + plugin.source.type === "package" ? [[plugin.source.target, plugin.source.outdated === true]] : [], + ), + ), + }), + (error) => dispatchUpdate({ type: "check.failed", attempt, message: formatServerError(error, language.t) }), + ) + } + + const id = effect.id + + void serverSdk.api.plugin.update({ targets: [effect.target] }).then( + () => dispatchUpdate({ type: "update.succeeded", id }), + (error) => dispatchUpdate({ type: "update.failed", id, message: formatServerError(error, language.t) }), + ) + } + + createEffect( + on( + () => serverSdk.scope, + () => dispatchUpdate({ type: "server.changed" }), + { defer: true }, + ), + ) + createEffect(on(pluginList, (list) => list && dispatchUpdate({ type: "list.loaded" }), { defer: true })) + onCleanup(() => dispatchUpdate({ type: "disposed" })) + + const pluginUpdates = { + view: (source: Parameters[1]) => pluginUpdateView(updateState(), source), + update: (target: string) => dispatchUpdate({ type: "update.requested", target }), + } + // The desktop window runs the GUIs of its own server's plugins, so only that server's rows control them. const own = () => !!platform.plugins && ServerConnection.builtin(serverSdk.server) @@ -247,13 +326,26 @@ export const SettingsExtensions: Component<{ {language.t("settings.extensions.availableAll")} - - {language.t("settings.extensions.manageConfig")} - +
+ + {language.t("settings.extensions.manageConfig")} + + +
{/* The desktop's own server also lists the plugins main runs, so its rows stay while the list loads. */} - +

{language.t("common.loading")}

diff --git a/packages/app/src/settings/providers/plugin-update-machine.test.ts b/packages/app/src/settings/providers/plugin-update-machine.test.ts new file mode 100644 index 000000000000..0cbb1f3886cb --- /dev/null +++ b/packages/app/src/settings/providers/plugin-update-machine.test.ts @@ -0,0 +1,121 @@ +import { expect, test } from "bun:test" +import { + createPluginUpdateState, + pluginUpdateChecking, + pluginUpdateView, + transitionPluginUpdate, + type PluginUpdateEffect, + type PluginUpdateEvent, +} from "./plugin-update-machine" + +/** Drives the machine as the Extensions page does, recording every effect it asks for. */ +function harness() { + const machine = { state: createPluginUpdateState() } + const effects: PluginUpdateEffect[] = [] + + const dispatch = (event: PluginUpdateEvent) => { + const next = transitionPluginUpdate(machine.state, event) + + machine.state = next.state + effects.push(...next.effects) + + return next + } + + return { machine, effects, dispatch } +} + +const source = { target: "opencode-plugin-a@latest" } as const + +test("a check claims itself at once, ignores a second request, and its result overrides the listed flags", () => { + const run = harness() + + run.dispatch({ type: "check.requested" }) + const checking = run.machine.state + run.dispatch({ type: "check.requested" }) + + expect(run.machine.state).toBe(checking) + expect(pluginUpdateChecking(run.machine.state)).toBe(true) + expect(run.effects).toEqual([{ type: "check", attempt: 0 }]) + + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + + expect(pluginUpdateChecking(run.machine.state)).toBe(false) + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: true, updating: false }) + + run.dispatch({ type: "list.loaded" }) + + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) + expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ + outdated: true, + updating: false, + }) +}) + +test("a failed check ends idle and reports the error", () => { + const run = harness() + const message = "offline" + + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "check.failed", attempt: 0, message }) + + expect(pluginUpdateChecking(run.machine.state)).toBe(false) + expect(run.effects.at(-1)).toEqual({ type: "failed", message }) +}) + +test("an update runs once per package, then refreshes the list and hides the button", () => { + const run = harness() + const outdated = { ...source, outdated: true } as const + + run.dispatch({ type: "update.requested", target: source.target }) + const updating = run.machine.state + run.dispatch({ type: "update.requested", target: source.target }) + + expect(run.machine.state).toBe(updating) + expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: true, updating: true }) + + run.dispatch({ type: "update.succeeded", id: 0 }) + + expect(run.effects).toEqual([{ type: "update", id: 0, target: source.target }, { type: "refresh" }]) + // The list still carries the old flag until it loads again. + expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: false, updating: false }) +}) + +test("a failed update keeps the package outdated and reports the error", () => { + const run = harness() + const message = "npm failed" + + run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "update.failed", id: 0, message }) + + expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ + outdated: true, + updating: false, + }) + expect(run.effects.at(-1)).toEqual({ type: "failed", message }) +}) + +test("results from before a server change or after dispose are ignored", () => { + const run = harness() + + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "server.changed" }) + const changed = run.machine.state + + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + run.dispatch({ type: "update.failed", id: 1, message: "late" }) + + expect(run.machine.state).toBe(changed) + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) + + run.dispatch({ type: "disposed" }) + const disposed = run.machine.state + run.dispatch({ type: "check.requested" }) + + expect(run.machine.state).toBe(disposed) + expect(run.effects).toEqual([ + { type: "check", attempt: 0 }, + { type: "update", id: 1, target: source.target }, + ]) +}) diff --git a/packages/app/src/settings/providers/plugin-update-machine.ts b/packages/app/src/settings/providers/plugin-update-machine.ts new file mode 100644 index 000000000000..078cc994dee9 --- /dev/null +++ b/packages/app/src/settings/providers/plugin-update-machine.ts @@ -0,0 +1,123 @@ +/** One package update on its way; `id` tells it apart from an update of the same package on an earlier server. */ +export type RunningPluginUpdate = { readonly id: number; readonly target: string } + +/** + * Checking for and installing plugin package updates from Settings › Extensions. `outdated` holds what the latest + * check or update learned per package, which is fresher than the listed plugins until the list loads again. + */ +export type PluginUpdateState = + | { + readonly status: "idle" + readonly nextId: number + readonly outdated: Readonly> + readonly updating: readonly RunningPluginUpdate[] + } + | { + readonly status: "checking" + readonly attempt: number + readonly nextId: number + readonly outdated: Readonly> + readonly updating: readonly RunningPluginUpdate[] + } + | { readonly status: "disposed" } + +export type PluginUpdateEvent = + | { readonly type: "check.requested" } + /** `outdated` maps every package the check covered to whether it has a newer version. */ + | { readonly type: "check.succeeded"; readonly attempt: number; readonly outdated: Readonly> } + | { readonly type: "check.failed"; readonly attempt: number; readonly message: string } + | { readonly type: "update.requested"; readonly target: string } + | { readonly type: "update.succeeded"; readonly id: number } + | { readonly type: "update.failed"; readonly id: number; readonly message: string } + /** The plugin list loaded again, so its own flags are current. */ + | { readonly type: "list.loaded" } + /** Another server: forget what this one's checks and updates learned. */ + | { readonly type: "server.changed" } + | { readonly type: "disposed" } + +export type PluginUpdateEffect = + | { readonly type: "check"; readonly attempt: number } + | { readonly type: "update"; readonly id: number; readonly target: string } + | { readonly type: "refresh" } + | { readonly type: "failed"; readonly message: string } + +export type PluginUpdateTransition = { + readonly state: PluginUpdateState + readonly effects: readonly PluginUpdateEffect[] +} + +const none: readonly PluginUpdateEffect[] = [] + +const step = (state: PluginUpdateState, effects = none): PluginUpdateTransition => ({ state, effects }) + +export function createPluginUpdateState(): PluginUpdateState { + return { status: "idle", nextId: 0, outdated: {}, updating: [] } +} + +export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUpdateEvent): PluginUpdateTransition { + if (state.status === "disposed") return step(state) + + if (event.type === "disposed") return step({ status: "disposed" }) + + if (event.type === "server.changed") return step({ status: "idle", nextId: state.nextId, outdated: {}, updating: [] }) + + if (event.type === "list.loaded") { + if (Object.keys(state.outdated).length === 0) return step(state) + + return step({ ...state, outdated: {} }) + } + + if (event.type === "check.requested") { + if (state.status === "checking") return step(state) + + const attempt = state.nextId + + return step({ ...state, status: "checking", attempt, nextId: attempt + 1 }, [{ type: "check", attempt }]) + } + + if (event.type === "check.succeeded" || event.type === "check.failed") { + if (state.status !== "checking" || state.attempt !== event.attempt) return step(state) + + const idle = { status: "idle", nextId: state.nextId, outdated: state.outdated, updating: state.updating } as const + + if (event.type === "check.failed") return step(idle, [{ type: "failed", message: event.message }]) + + return step({ ...idle, outdated: event.outdated }) + } + + if (event.type === "update.requested") { + if (state.updating.some((item) => item.target === event.target)) return step(state) + + const id = state.nextId + + return step({ ...state, nextId: id + 1, updating: [...state.updating, { id, target: event.target }] }, [ + { type: "update", id, target: event.target }, + ]) + } + + const running = state.updating.find((item) => item.id === event.id) + + if (!running) return step(state) + + const updating = state.updating.filter((item) => item !== running) + + if (event.type === "update.failed") return step({ ...state, updating }, [{ type: "failed", message: event.message }]) + + return step({ ...state, updating, outdated: { ...state.outdated, [running.target]: false } }, [{ type: "refresh" }]) +} + +export type PluginUpdateView = { readonly outdated: boolean; readonly updating: boolean } + +/** What the Plugins tab shows for a package, given the flags the listed plugin carries. */ +export function pluginUpdateView( + state: PluginUpdateState, + source: { readonly target: string; readonly outdated?: true; readonly updating?: true }, +): PluginUpdateView { + if (state.status === "disposed") return { outdated: false, updating: false } + + const updating = source.updating === true || state.updating.some((item) => item.target === source.target) + + return { outdated: state.outdated[source.target] ?? source.outdated === true, updating } +} + +export const pluginUpdateChecking = (state: PluginUpdateState) => state.status === "checking" diff --git a/packages/app/src/settings/shell.standalone.stories.tsx b/packages/app/src/settings/shell.standalone.stories.tsx index c0ca4d9c26e6..e8142fb76c10 100644 --- a/packages/app/src/settings/shell.standalone.stories.tsx +++ b/packages/app/src/settings/shell.standalone.stories.tsx @@ -5,11 +5,13 @@ import { SettingsScreen } from "@/settings/shell" import { SettingsSurfaceProvider, type SettingsRootTab } from "@/settings/surface" import { LayoutProvider } from "@/shell/state/layout" -function SettingsStory(props: { tab: SettingsRootTab; locked?: boolean }) { +function SettingsStory(props: { tab: SettingsRootTab; subtab?: string; locked?: boolean }) { const history = createMemoryHistory() const query = new URLSearchParams({ tab: props.tab }) + if (props.subtab) query.set("subtab", props.subtab) + if (props.locked) query.set("server", ServerConnection.key(STORY_LOCKED_SERVER)) history.set({ value: `/settings?${query}`, replace: true, scroll: false }) @@ -51,4 +53,8 @@ export const Models = { render: () => , } +export const Plugins = { + render: () => , +} + export const UnavailableServer = { render: () => } diff --git a/packages/app/src/shell/titlebar/recent-sessions-model.test.ts b/packages/app/src/shell/titlebar/recent-sessions-model.test.ts index 6a163f2d850e..8ba7396af7d5 100644 --- a/packages/app/src/shell/titlebar/recent-sessions-model.test.ts +++ b/packages/app/src/shell/titlebar/recent-sessions-model.test.ts @@ -611,36 +611,13 @@ test("a reload supersedes Show more's page and keeps the older rows already load }) }) -test("run ends reload Recent except a known subagent's or a shutdown, as do new root sessions and archiving", () => { - const sessions = new Map([ - ["ses_root", session("ses_root", 10)], - ["ses_child", session("ses_child", 10, { parentID: "ses_root" })], - ]) - +test("only a new root session reloads Recent", () => { const envelope = (sessionID: string) => ({ id: `evt_${sessionID}`, created: 1, durable: { aggregateID: sessionID, seq: 1, version: 1 as const }, }) - const ended = (sessionID: string) => ({ - ...envelope(sessionID), - type: "session.execution.succeeded" as const, - data: { sessionID }, - }) - - const failed = (sessionID: string) => ({ - ...envelope(sessionID), - type: "session.execution.failed" as const, - data: { sessionID, error: { type: "unknown", message: "failed" } }, - }) - - const interrupted = (sessionID: string, reason: "user" | "shutdown") => ({ - ...envelope(sessionID), - type: "session.execution.interrupted" as const, - data: { sessionID, reason }, - }) - const created = (sessionID: string, parentID?: string) => ({ ...envelope(sessionID), type: "session.created" as const, @@ -648,24 +625,27 @@ test("run ends reload Recent except a known subagent's or a shutdown, as do new }) const cases: [OpenCodeEvent, boolean][] = [ - [ended("ses_root"), true], - [failed("ses_root"), true], - [interrupted("ses_root", "user"), true], - [ended("ses_unknown"), true], - [ended("ses_child"), false], - [failed("ses_child"), false], - [interrupted("ses_child", "user"), false], - [interrupted("ses_root", "shutdown"), false], [created("ses_new"), true], [created("ses_sub", "ses_root"), false], - [{ ...envelope("ses_root"), type: "session.archived", data: { sessionID: "ses_root" } }, true], - [{ ...envelope("ses_root"), type: "session.unarchived", data: { sessionID: "ses_root" } }, true], + [{ ...envelope("ses_root"), type: "session.execution.succeeded", data: { sessionID: "ses_root" } }, false], + [ + { + ...envelope("ses_root"), + type: "session.execution.failed", + data: { sessionID: "ses_root", error: { type: "unknown", message: "failed" } }, + }, + false, + ], + [ + { ...envelope("ses_root"), type: "session.execution.interrupted", data: { sessionID: "ses_root", reason: "user" } }, + false, + ], + [{ ...envelope("ses_root"), type: "session.archived", data: { sessionID: "ses_root" } }, false], + [{ ...envelope("ses_root"), type: "session.unarchived", data: { sessionID: "ses_root" } }, false], [{ ...envelope("ses_root"), type: "session.renamed", data: { sessionID: "ses_root", title: "Renamed" } }, false], ] - expect(cases.map(([value]) => recentReloadsOn(value, (id) => sessions.get(id)))).toEqual( - cases.map(([, expected]) => expected), - ) + expect(cases.map(([value]) => recentReloadsOn(value))).toEqual(cases.map(([, expected]) => expected)) }) test("removing a server clears its page", () => { diff --git a/packages/app/src/shell/titlebar/recent-sessions-model.ts b/packages/app/src/shell/titlebar/recent-sessions-model.ts index c9774f6840cb..10f77a89fff2 100644 --- a/packages/app/src/shell/titlebar/recent-sessions-model.ts +++ b/packages/app/src/shell/titlebar/recent-sessions-model.ts @@ -136,7 +136,7 @@ type RecentPageBase = { /** * How a load joins the rows already held. `refresh` is a reconnect snapshot, which also re-reads pins it does not - * cover; `reload` is the same snapshot taken while connected (run end, new session, focus, archive). + * cover; `reload` is the same snapshot taken while connected (a new root session). */ type RecentPageKind = "first" | "next" | "refresh" | "reload" @@ -288,26 +288,11 @@ function finishLoading( } /** - * Whether an event changes which root sessions page one holds or their order, so Recent reloads it: a run that ended - * (except a shutdown, which leaves the server's times alone), a new root session, archive and unarchive. A run end - * from a known subagent session does not; an unknown session may be a root, so it does. + * Whether an event makes Recent reload page one: only a new root session. Other changes (run ends, renames, archive) + * reach the rows through the live store, so the order stays put until the next reconnect refresh. */ -export function recentReloadsOn(event: OpenCodeEvent, session: (sessionID: string) => SessionInfo | undefined) { - if (event.type === "session.created") return !event.data.parentID - - if (event.type === "session.archived" || event.type === "session.unarchived") return true - - if (event.type === "session.execution.interrupted" && event.data.reason === "shutdown") return false - - if ( - event.type !== "session.execution.succeeded" && - event.type !== "session.execution.failed" && - event.type !== "session.execution.interrupted" - ) { - return false - } - - return !session(event.data.sessionID)?.parentID +export function recentReloadsOn(event: OpenCodeEvent) { + return event.type === "session.created" && !event.data.parentID } /** One server's page lifecycle. I/O lives in the driver; this only says what to fetch and what the rows become. */ diff --git a/packages/app/src/shell/titlebar/recent-sessions.tsx b/packages/app/src/shell/titlebar/recent-sessions.tsx index 98535720e4db..e6d85bb7cf69 100644 --- a/packages/app/src/shell/titlebar/recent-sessions.tsx +++ b/packages/app/src/shell/titlebar/recent-sessions.tsx @@ -130,8 +130,7 @@ export function RecentSessions() { reloads.delete(server) } - // Order follows each server's last snapshot, so the moments the iOS app reloads its list reload page one here: - // run end, a new root session, archive and unarchive, and the window coming back. Bursts coalesce per server. + // Order follows each server's last snapshot, so only a new root session reloads page one. Bursts coalesce per server. const reload = (item: Connection) => { cancelReload(item.server) reloads.set( @@ -331,19 +330,6 @@ export function RecentSessions() { reloads.forEach((timer) => clearTimeout(timer)) }) - const reloadAll = () => connections().forEach(reload) - - const onVisible = () => { - if (document.visibilityState === "visible") reloadAll() - } - - window.addEventListener("focus", reloadAll) - document.addEventListener("visibilitychange", onVisible) - onCleanup(() => { - window.removeEventListener("focus", reloadAll) - document.removeEventListener("visibilitychange", onVisible) - }) - // The store is local and long-lived; drop duplicates a previous version or a manual edit left behind. createEffect(() => { if (!ready()) return @@ -477,7 +463,7 @@ export function RecentSessions() { connections().forEach((item) => { onCleanup( item.ctx.data.listen(({ details }) => { - if (recentReloadsOn(details, item.ctx.data.session.get)) reload(item) + if (recentReloadsOn(details)) reload(item) }), ) }) diff --git a/packages/app/src/shell/titlebar/tab-strip.tsx b/packages/app/src/shell/titlebar/tab-strip.tsx index 819e354fad51..35518ea358fb 100644 --- a/packages/app/src/shell/titlebar/tab-strip.tsx +++ b/packages/app/src/shell/titlebar/tab-strip.tsx @@ -352,10 +352,17 @@ export function createSessionRowNavigation(input: { const command = useCommand() const language = useLanguage() + // The destination composer leaves focus on a control that still holds it (session/composer/focus.tsx), so a shortcut + // that opens another session releases focus first, as clicking its tab does. + const navigate = (key: string, reveal: boolean) => { + if (key !== input.current() && document.activeElement instanceof HTMLElement) document.activeElement.blur() + input.navigate(key, reveal) + } + const selectAdjacent = (offset: -1 | 1, include?: (key: string) => boolean) => { const key = adjacentTabKey(input.keys(), input.current(), offset, include) - if (key) input.navigate(key, false) + if (key) navigate(key, false) } command.register("titlebar-tab-cycle", () => [ @@ -415,7 +422,7 @@ export function createSessionRowNavigation(input: { title: "", keybind: `mod+${index + 1}`, hidden: true, - onSelect: () => input.navigate(key, true), + onSelect: () => navigate(key, true), })), ) diff --git a/packages/client/src/effect/api/api.ts b/packages/client/src/effect/api/api.ts index 54d8d8c5edc0..f9fe7de3727a 100644 --- a/packages/client/src/effect/api/api.ts +++ b/packages/client/src/effect/api/api.ts @@ -99,7 +99,11 @@ export interface AgentApi { } export type PluginListInput = { readonly location?: { readonly directory?: string | undefined } | undefined } -export type PluginListOutput = { readonly location: Location.PublicRef; readonly data: ReadonlyArray } +export type PluginListOutput = { + readonly location: Location.PublicRef + readonly ready?: boolean | undefined + readonly data: ReadonlyArray +} export type PluginListOperation = (input?: PluginListInput) => Effect.Effect export type PluginGuiInput = { diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 3011fc79e8fe..c81d50cec16f 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -3398,7 +3398,7 @@ export type AgentGetOutput = { location: LocationPublicRef; data: AgentInfo } export type PluginListInput = { readonly location?: { readonly directory?: string | undefined } | undefined } -export type PluginListOutput = { location: LocationPublicRef; data: Array } +export type PluginListOutput = { location: LocationPublicRef; ready?: boolean; data: Array } export type PluginGuiInput = { readonly location?: { readonly directory?: string | undefined } | undefined diff --git a/packages/codemode/AGENTS.md b/packages/codemode/AGENTS.md index 66f962e081ee..75f76b24bf9e 100644 --- a/packages/codemode/AGENTS.md +++ b/packages/codemode/AGENTS.md @@ -1,6 +1,7 @@ # @opencode/codemode +- `src/language/` is the language layer (compiler, VM, runtime, stdlib, prelude, scheduler, snapshots, and the Effect driver); `src/codemode/` is Code Mode, built on it (tools, schemas, catalog, `search`, `scanTools`, OpenAPI, hooks, `maxToolCalls`). Nothing in `src/language/` imports from the rest of `src` (`test/language-boundary.test.ts`), and the language never knows what a tool is: Code Mode gives programs its functions through `Host` and `Runtime.make`. - `interpreter-support.md` is a checklist, not a changelog: check off newly added support and add unchecked items for significant gaps. Leave it alone for fixes and refactors. -- Program values are `Value` (`src/runtime/objects.ts`). Host values are `unknown` and enter only through `fromHost` and `fromJson`; do not widen program-facing signatures to `unknown`. +- Program values are `Value` (`src/language/runtime/objects.ts`). Host values are `unknown` and enter only through `fromHost` and `fromJson`; do not widen program-facing signatures to `unknown`. - Each built-in object kind is one `Obj` subclass (`Wrapper` or `Opaque`) that overrides the hooks it needs. Ask the object instead of adding `instanceof` ladders. -- The core VM, compiler, prelude, snapshot, and stdlib modules must not import `effect`; Effect lives only in the host boundary and driver (`src/runtime/`, `src/openapi/`, and the top-level tool/runtime entrypoints). +- The core VM, compiler, prelude, snapshot, and stdlib modules must not import `effect`; in `src/language/`, Effect lives only in the host boundary and driver (`runtime.ts`, `host-function.ts`, `runtime/driver.ts`, `runtime/execute.ts`, `runtime/extensions.ts`). `src/codemode/` uses Effect freely. diff --git a/packages/codemode/README.md b/packages/codemode/README.md index 2294f286c121..c4a3f5afec33 100644 --- a/packages/codemode/README.md +++ b/packages/codemode/README.md @@ -24,6 +24,18 @@ The idea of code mode was originally introduced by Cloudflare. See Unsupported syntax returns an `UnsupportedSyntax` diagnostic with a source location. Current gaps are tracked in the [interpreter support checklist](./interpreter-support.md). +## Layout + +The package is two layers, as folders under `src/`: + +- `src/language/` is the language: the compiler, VM, runtime, standard library, prelude, scheduler, snapshots, and the + Effect driver. It knows nothing about tools. A host gives programs its functions with `Host` and runs them with + `Runtime.make` (see [The language layer](#the-language-layer)). +- `src/codemode/` is Code Mode, built on the language: tools and their schemas, the catalog and namespaces, `search`, + `scanTools`, OpenAPI, hooks, `maxToolCalls`, `CodeMode.make`, and `Extension.make`. + +Nothing in `src/language/` imports from `src/codemode/`; a test checks it. The package's public API is Code Mode's. + ## Quick Start ```ts @@ -101,6 +113,12 @@ runs before the call reaches the host, and a failing one denies it: the host fun catches the failure as a thrown error. A denied call fails the way the call itself would: a tool call or asynchronous global rejects its promise, and a synchronous global throws. +Tool arguments cross into the host the way extension arguments do, as copies (plain data, `Date`, `Map`, `Set`, `URL`, +`Headers`, errors), and Code Mode then turns them into JSON next to the tool's schema: a `Date` is its ISO string, a +`Set` its items, a `URLSearchParams` its query string, an `Error` `{ name, message, ...own fields }`, and `Map` and +`RegExp` values `{}`. A `Uint8Array` asks to be encoded as text first. A program's own `toJSON` is not called, and a +promise or function inside an argument fails the call: await it, or pass its data. + ### Suspending and resuming a run `CodeMode.make` returns a `CodeMode.Runtime`, which can also run a program step by step: @@ -198,10 +216,11 @@ the state, and checking it against `options.maxBytes` all happen before anything can't be saved or is too large fails the suspend the same way. A resumed run keeps the limits it was saved with: `timeoutMs` counts active time across every resume, and `maxToolCalls` -counts every admitted call of the run. A state resumes only on the same build of this package and with the same -extensions (and the same extension `snapshot` hooks). The state is checked when the resumed program runs: its `run()` -fails with a `CodeMode.SnapshotError` when it can't be resumed here or is malformed. Built-ins are saved by name and -only their changes are copied. +counts every admitted call of the run (Code Mode keeps them in the run's `metadata`, saved with the state). A state +resumes only on the same build of this package and with the same host globals (the same extension globals, with the +same `snapshot` hooks). The state is checked when the resumed program runs: its `run()` fails with a +`CodeMode.SnapshotError` when it can't be resumed here or is malformed. Built-ins are saved by name and only their +changes are copied. Set `CODEMODE_ROUNDTRIP=1` (`bun run test:roundtrip`) to run the tests with every run saved to bytes and restored into a brand-new runtime at every host call and every few steps. @@ -244,8 +263,17 @@ const result = yield* CodeMode.make({ extensions: [web] }).execute(code).pipe(Ef ``` Every value crossing in either direction is converted, never shared: arguments come in as copies, results go out as -copies, and a function inside a result is callable the same way. A global that shadows a built-in or another -extension throws at `CodeMode.make`. +copies, and a function inside a result is callable the same way. When the program passes such a function back to an +extension, it arrives as the host function itself, so `copy(dest, file(src))` can tell what it was given. Globals and +the program's own functions never cross back: they are left out, as in tool arguments. A global that shadows a +built-in or another extension throws at `CodeMode.make`. + +Mark a global with `Extension.constructible` to let programs call it with `new`, as they would a class. `new` calls +it the same way a plain call does, and the program gets what it returns: + +```ts +Extension.make({ name: "glob", globals: { Glob: Extension.constructible((pattern: string) => makeGlob(pattern)) } }) +``` Functions inside a result have no data form, so a suspended run loses them by default: after a resume, calling one throws a `TypeError`. An extension can preserve them across suspend and resume with `snapshot` hooks. `encode` receives @@ -276,7 +304,7 @@ const web = Extension.make({ ``` A suspend fails (and the run continues) when `encode` throws or returns non-plain data. A snapshot saved with an -extension's hooks resumes only in a runtime whose extension of the same name also provides `snapshot` hooks. +extension's hooks resumes only in a runtime whose globals of the same names also have `snapshot` hooks. A global can also be an object of functions, such as `fs`. Every function reachable from it is an entry point like a global, so calls fire the `extension` hooks with their path as the name (`fs.readFile`, `fs.promises.readFile`). @@ -308,7 +336,58 @@ without JSON content, WebSocket or SSE semantics, or binary responses are report Authentication is resolved by the host and never shown to the model. Generated tools require `HttpClient.HttpClient`. Request signatures omit `readOnly` properties; response signatures omit `writeOnly` properties. These JSON Schemas shape model-visible signatures but do not filter runtime values: nested JSON body properties and decoded server -responses pass through unchanged. See `src/openapi/types.ts` for option details. +responses pass through unchanged. See `src/codemode/openapi/types.ts` for option details. + +## The language layer + +Code Mode is built on the language layer in `src/language/`, which runs programs over any host globals. It is not part +of the package's public API yet. + +```ts +import { Host, Runtime } from "./src/language/index.js" + +const runtime = Runtime.make({ + // Called when a run starts, and again when it resumes, so globals can keep the run's own state. + globals: (run) => ({ + hash: Host.fn(([text]) => sha256(text as string)), // "sync" (the default): the program waits for it + sleep: Host.fn(([ms]) => Effect.sleep(ms as number), { type: "async" }), // the program gets a promise + find: Host.fn(([query]) => searchIndex(query), { type: "sync-thenable" }), // the object now, also awaitable + lookup: Host.fn(([id], { key }) => Db.get(id, key), { type: "async", idempotent: true }), // rerun on resume + review: Host.durableFn({ start, wait, cancel }), // keeps running across a suspend + api: Host.namespace({ + keys: (path) => childrenOf(path), // Object.keys and for...in + resolve: (path) => fnAt(path) ?? Host.missing(`No api.${path.join(".")}`, suggestionsFor(path)), + }), + fs: { readFile: Host.fn(([file]) => read(file), { type: "async" }) }, // plain objects of functions work too + }), + limits: { timeoutMs: 30_000, maxOutputBytes: 1_000_000 }, +}) + +const program = runtime.program(code, { bindings: { repo: "…" }, runId }) +yield* program.run() // Result | Suspended +yield* program.suspend({ maxBytes }) +yield* runtime.resume(bytes).run() +``` + +- **`Host.fn(f, { type, idempotent })`.** `f` gets the program's arguments, as they crossed, and the call, `{ key }` + (`:`). `sync` (the default) blocks the program: an Effect runs until it ends, and a returned Promise + is a promise. `async` hands the program a promise. `sync-thenable` returns the result at once, with hidden + `then`/`catch`/`finally`. `idempotent: true`, only with `async`, lets a suspend interrupt a call in progress, which + runs again on resume under the same key; any other call in progress refuses a suspend. `constructible: true` lets + the program call it with `new`. `takesFunctions: true` lets the program pass back a function that was inside a + result, which arrives as the host function itself, and a call holding one refuses a suspend until it completes; + without it, such a function is treated like any other function in an argument: refused, or left out when the + runtime's `translate.functions` is `"drop"`. +- **`Host.durableFn({ start, wait, cancel })`.** `start` gives JSON data, saved with the run; `wait(data)` runs right + after, and again after a resume; `cancel(data)` only when the run is interrupted or ends with the work still running. +- **`Host.namespace({ keys, resolve })`.** A lazy object of functions addressed by path, for catalogs too large to + build as objects. Any path can be read; calling one asks `resolve` for its function, or a failure it throws. +- **`Host.fail(kind, message, suggestions?)`.** A failure with its own diagnostic kind, to throw or fail with. +- **`run.metadata`.** The host's own JSON state for the run: saved with a suspended run, given back on resume, and in + the run's result. Code Mode keeps `toolCalls` and `maxToolCalls` there. + +Every host function gets the same structured translation: arguments and results cross as copies (plain data, `Date`, +`Map`, `Set`, `Uint8Array`, `URL`, `Headers`, errors), and a function inside a result is callable. ## Results diff --git a/packages/codemode/interpreter-support.md b/packages/codemode/interpreter-support.md index 993f5276ce9a..3006c498ed9c 100644 --- a/packages/codemode/interpreter-support.md +++ b/packages/codemode/interpreter-support.md @@ -18,16 +18,18 @@ the ultimate source of truth. Upstream test262 files run verbatim from `test/tes TypeScript-only syntax is rejected rather than stripped before execution. - [x] Top-level `await` and `return` through the program's implicit async-function scope. - [x] Explicit `return`, final top-level expression as a REPL-style result, and `null` when no value is produced. -- [x] The host boundary is `JSON.stringify` plus a short table. The program result and tool arguments cross as +- [x] The result boundary is `JSON.stringify` plus a short table. The program result crosses as what `JSON.stringify` would serialize: `toJSON` is honored, functions and `undefined` properties vanish, `undefined` array elements and non-finite numbers become `null`, a cyclic value throws the same `TypeError`, and Map, RegExp, and generators serialize as `{}`. A bare `undefined` result is `null`. Tool results come back the way `JSON.parse(JSON.stringify(result))` would. The table, where a value cannot be JSON but what the program meant is clear: a promise is awaited (a rejection fails the program), a Set - crosses as an array, a URLSearchParams as its query string, an Error as `{ name, message, ...own enumerable }`, a - Uint8Array is rejected with a hint to encode as text, and own `__proto__` keys are dropped so merging tool - inputs or results cannot replace a prototype. In-program `JSON.stringify` keeps JS behavior except for the - Error form and a promise, which is a `TypeError` with an await hint rather than a silent `{}`. + crosses as an array, a URLSearchParams as its query string, an Error as `{ name, message, ...own enumerable }`, + and own `__proto__` keys are dropped so merging results cannot replace a prototype. Tool arguments cross as + copies, like extension arguments, and the tool layer turns them into JSON with the same table (a Uint8Array is + rejected with a hint to encode as text); a program's own `toJSON` is not called there, and a promise or function + inside an argument fails the call. In-program `JSON.stringify` keeps JS behavior except for the Error form and a + promise, which is a `TypeError` with an await hint rather than a silent `{}`. - [x] Live Date, RegExp, Map, Set, URL, URLSearchParams, Headers, and Uint8Array values inside CodeMode. - [x] Tool calls through the host-provided `tools` tree only. - [x] Imports of globals by Node module name: `import x from "m"`, `import { a, b as c } from "m"`, diff --git a/packages/codemode/script/bench.ts b/packages/codemode/script/bench.ts index 0d9027846b6e..e930be3d1c10 100644 --- a/packages/codemode/script/bench.ts +++ b/packages/codemode/script/bench.ts @@ -6,7 +6,7 @@ import { numberOfDFGCompiles, reoptimizationRetryCount } from "bun:jsc" import { Effect, Schema } from "effect" import { CodeMode, Tool } from "../src/index.js" -import { Vm } from "../src/vm.js" +import { Vm } from "../src/language/vm.js" const cases: ReadonlyArray = [ [ diff --git a/packages/codemode/script/disasm.ts b/packages/codemode/script/disasm.ts index d3e114270ff5..18e3a6420862 100644 --- a/packages/codemode/script/disasm.ts +++ b/packages/codemode/script/disasm.ts @@ -1,9 +1,9 @@ #!/usr/bin/env bun // The bytecode disassembler: pcs, ops, constants, handler and finally tables, local names, source lines. -import { sourceLocation } from "../src/runtime/model.js" -import type { Code } from "../src/code.js" -import { compileSource } from "../src/compiler/compile.js" +import { sourceLocation } from "../src/language/runtime/model.js" +import type { Code } from "../src/language/code.js" +import { compileSource } from "../src/language/compiler/compile.js" import { BinOpSymbols, CheckBaseKind, @@ -13,8 +13,8 @@ import { OpNames, PutKind, UnaryOpSymbols, -} from "../src/opcodes.js" -import { parseProgram } from "../src/runtime/execute.js" +} from "../src/language/opcodes.js" +import { parseProgram } from "../src/language/runtime/execute.js" const formatFlags = (flags: number): string => { const names: Array = [] diff --git a/packages/codemode/script/prelude.ts b/packages/codemode/script/prelude.ts index b57e1ff0783d..ec2ebd61610d 100644 --- a/packages/codemode/script/prelude.ts +++ b/packages/codemode/script/prelude.ts @@ -1,15 +1,15 @@ #!/usr/bin/env bun -// Compiles the built-in sources (the prelude, the VM's internal helpers) ahead of time into src/prelude/bytecode.json, -// which a process loads instead of parsing and compiling them (see src/prelude/bytecode.ts). Run it after changing +// Compiles the built-in sources (the prelude, the VM's internal helpers) ahead of time into src/language/prelude/bytecode.json, +// which a process loads instead of parsing and compiling them (see src/language/prelude/bytecode.ts). Run it after changing // either source or the compiler; test/prelude-bytecode.test.ts fails until then. import { parse } from "acorn" -import { Source } from "../src/runtime/source.js" -import { saveCodes } from "../src/prelude/bytecode.js" -import { code } from "../src/prelude/source.js" -import { compileSource } from "../src/compiler/compile.js" -import { INTERNAL_SOURCE } from "../src/internal-prelude.js" +import { Source } from "../src/language/runtime/source.js" +import { saveCodes } from "../src/language/prelude/bytecode.js" +import { code } from "../src/language/prelude/source.js" +import { compileSource } from "../src/language/compiler/compile.js" +import { INTERNAL_SOURCE } from "../src/language/internal-prelude.js" export const compileBuiltins = () => Object.fromEntries( @@ -23,5 +23,8 @@ export const compileBuiltins = () => ) if (import.meta.main) { - await Bun.write(new URL("../src/prelude/bytecode.json", import.meta.url), `${JSON.stringify(compileBuiltins())}\n`) + await Bun.write( + new URL("../src/language/prelude/bytecode.json", import.meta.url), + `${JSON.stringify(compileBuiltins())}\n`, + ) } diff --git a/packages/codemode/script/setup.ts b/packages/codemode/script/setup.ts index 49603b79586d..8743326bd465 100644 --- a/packages/codemode/script/setup.ts +++ b/packages/codemode/script/setup.ts @@ -5,15 +5,13 @@ import { Effect } from "effect" import { CodeMode } from "../src/index.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { ToolRuntime } from "../src/tool-runtime.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" const index = process.argv.indexOf("--iterations") const iterations = index === -1 ? 2000 : Number(process.argv[index + 1]) const runtime = CodeMode.make({ tools: {} }) -const prepared = ToolRuntime.prepare({}) const measure = async (name: string, work: () => unknown) => { for (let at = 0; at < 200; at++) await work() @@ -30,8 +28,5 @@ const measure = async (name: string, work: () => unknown) => { ) } -await measure( - "runtime", - () => new Interpreter({ tools: ToolRuntime.make(prepared, undefined, {}), builtins: createBuiltins() }), -) +await measure("runtime", () => new Interpreter({ builtins: createBuiltins() })) await measure("trivial program", () => Effect.runPromise(runtime.execute("return 1"))) diff --git a/packages/codemode/src/codemode.ts b/packages/codemode/src/codemode/codemode.ts similarity index 61% rename from packages/codemode/src/codemode.ts rename to packages/codemode/src/codemode/codemode.ts index e02e9c6c92e3..805b383ff268 100644 --- a/packages/codemode/src/codemode.ts +++ b/packages/codemode/src/codemode/codemode.ts @@ -1,20 +1,19 @@ -import { Deferred, Effect, Exit, Schema } from "effect" -import type { Extension } from "./extension.js" +import { Effect, Schema } from "effect" +import { type Extension, isAsync, isConstructible } from "./extension.js" import { scanTools } from "./scan.js" -import { type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js" +import { hooked, type Services, type ToolDescription, ToolRuntime } from "./tool-runtime.js" import type { Tools } from "./tools.js" -import { fromJson } from "./data.js" import { - finishExecution, - resumeProgram, - type Running, - type Setup, - startProgram, + fromBytes, + globalNames, + Host, + Language, + type Snapshot, + SnapshotError, + type SnapshotHeader, SuspendError, -} from "./runtime/execute.js" -import { extensionGlobals } from "./runtime/extensions.js" -import { globalNames } from "./runtime/globals.js" -import { fromBytes, type Snapshot, SnapshotError, type SnapshotHeader, toBytes } from "./snapshot/snapshot.js" + toBytes, +} from "../language/index.js" export { fromBytes, scanTools, SnapshotError, SuspendError, toBytes } export type { Snapshot, SnapshotHeader } @@ -208,17 +207,10 @@ export type Program = { readonly suspend: (options?: SuspendOptions) => Effect.Effect } -const validateLimit = (name: keyof ExecutionLimits, value: number | undefined, minimum: number): number | undefined => { - if (value !== undefined && (!Number.isSafeInteger(value) || value < minimum)) { - throw new RangeError(`${name} must be a safe integer greater than or equal to ${minimum}.`) - } - return value -} - const resolveExecutionLimits = (limits?: ExecutionLimits): ResolvedExecutionLimits => ({ - timeoutMs: validateLimit("timeoutMs", limits?.timeoutMs, 1), - maxToolCalls: validateLimit("maxToolCalls", limits?.maxToolCalls, 0), - maxOutputBytes: validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0), + timeoutMs: Language.Runtime.validateLimit("timeoutMs", limits?.timeoutMs, 1), + maxToolCalls: Language.Runtime.validateLimit("maxToolCalls", limits?.maxToolCalls, 0), + maxOutputBytes: Language.Runtime.validateLimit("maxOutputBytes", limits?.maxOutputBytes, 0), }) /** Executes one Effect-native CodeMode program without constructing a reusable runtime. */ @@ -238,15 +230,18 @@ export function make< type R = Environment const prepared = ToolRuntime.prepare((options.tools ?? {}) as Tools>) const limits = resolveExecutionLimits(options.limits) + const hooks = options.hooks ?? {} const extensions: ReadonlyArray = options.extensions ?? [] - const bound = new Set(globalNames) + const bound = new Set([...globalNames, "tools", "search"]) for (const extension of extensions) { for (const name of Object.keys(extension.globals)) { if (bound.has(name)) throw new TypeError(`Extension "${extension.name}" global "${name}" is already defined.`) bound.add(name) } } - const setupFor = (selected: ReadonlyArray = extensions): Setup => { + // One language runtime for each set of the runtime's extensions a run picks, made when first picked. + const runtimes = new Map>() + const runtimeFor = (selected: ReadonlyArray = extensions) => { for (const extension of selected) { if (!extensions.includes(extension)) { throw new TypeError(`Extension "${extension.name}" isn't one this runtime was made with.`) @@ -254,66 +249,128 @@ export function make< } // In the runtime's order, so the same extensions picked in any order give the same globals, and resume alike. const picked = extensions.filter((extension) => selected.includes(extension)) - return { - prepared, - limits, - hooks: options.hooks ?? {}, - globals: picked.length === 0 ? undefined : (ctx) => extensionGlobals(ctx, picked), - lossy: picked.some((extension) => extension.snapshot === undefined), - } - } - const setup = setupFor() - const data = (bindings: Readonly> | undefined) => - bindings === undefined - ? undefined - : (ctx: Parameters[0]) => - Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)] as const) - const program = (code: string, bindings?: Readonly>, programOptions?: ProgramOptions) => { - const run = programOptions?.extensions === undefined ? setup : setupFor(programOptions.extensions) - return makeProgram(() => Effect.succeed(startProgram(run, code, data(bindings), programOptions?.runId))) + const key = picked.map((extension) => extensions.indexOf(extension)).join(",") + const known = runtimes.get(key) + if (known !== undefined) return known + // The tools' and extensions' environment, which the language runtime can't infer through them. + const made = Language.Runtime.make({ + globals: (run) => ({ + ...ToolRuntime.globals(prepared, hooks, run, limits.maxToolCalls), + ...Object.fromEntries( + picked.flatMap((extension) => + Object.entries(extension.globals).map(([name, value]) => [ + name, + extensionValue(extension, name, value, hooks), + ]), + ), + ), + }), + limits: { timeoutMs: limits.timeoutMs, maxOutputBytes: limits.maxOutputBytes }, + // Arguments cross as JSON would carry them: functions are left out and promises are awaited. + translate: { functions: "drop", promises: "await" }, + }) as unknown as Language.Runtime.Runtime + runtimes.set(key, made) + return made } + const program = (code: string, bindings?: Readonly>, programOptions?: ProgramOptions) => + reported(runtimeFor(programOptions?.extensions).program(code, { bindings, runId: programOptions?.runId })) return { get catalog() { return prepared.catalog }, execute: (code, bindings, runOptions) => - Effect.suspend(() => finishExecution(program(code, bindings, runOptions).run())), + Effect.suspend(() => + Effect.map(program(code, bindings, runOptions).run(), (ended): Result => { + if (ended.ok !== "suspended") return ended + return { + ok: false, + error: { kind: "ExecutionFailure", message: "The program suspended, which isn't supported here." }, + toolCalls: ToolRuntime.toolCalls(ended.state.header.metadata), + } + }), + ), program, - resume: (state, runOptions) => { - const run = runOptions?.extensions === undefined ? setup : setupFor(runOptions.extensions) - return makeProgram(() => resumeProgram(run, state)) - }, + resume: (state, runOptions) => reported(runtimeFor(runOptions?.extensions).resume(state)), } } -// A program's one run: `start` makes it when `run()` is called, and a suspend waits for it. -const makeProgram = (start: () => Effect.Effect, E>): Program => { - let claimed = false - const started = Deferred.makeUnsafe, SuspendError>() - return { - run: () => - Effect.suspend(() => { - if (claimed) { - return Effect.die( - new Error("A CodeMode program runs once; make another with runtime.program or runtime.resume."), - ) - } - claimed = true - return start().pipe( - Effect.onExit((exit) => - Exit.isSuccess(exit) - ? Deferred.succeed(started, exit.value) - : Deferred.fail(started, new SuspendError({ message: "The program didn't start.", running: [] })), - ), - Effect.flatMap((running) => running.run()), - ) - }), - suspend: (options) => { - const maxBytes = options?.maxBytes - if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 0)) { - throw new RangeError("maxBytes must be a safe integer greater than or equal to 0.") - } - return Effect.flatMap(Deferred.await(started), (running) => running.suspend(maxBytes)) - }, +/** + * An extension's global as host functions: each function, at any depth of a global object, fires the extension hooks + * around its calls and saves the values it returns with the extension's `snapshot` hooks. Other values pass as they are. + */ +const extensionValue = ( + extension: Extension, + label: string, + value: unknown, + hooks: ToolRuntime.Hooks, +): unknown => { + if (typeof value === "function") return extensionFunction(extension, label, value, hooks) + if (typeof value !== "object" || value === null) return value + const prototype = Object.getPrototypeOf(value) + if (prototype !== Object.prototype && prototype !== null) return value + return Object.fromEntries( + Object.entries(value).map(([key, item]) => [key, extensionValue(extension, `${label}.${key}`, item, hooks)]), + ) +} + +/** + * One extension function as a host function, synchronous unless it is marked `async`. The `extension.before` hook runs + * before the function, so a denied call never reaches it, and fails the way the function would: an async one rejects, + * a sync one throws. `extension.after` sees how the call ended, once what it returned has settled. + */ +const extensionFunction = ( + extension: Extension, + name: string, + value: Function, + hooks: ToolRuntime.Hooks, +): Host.Fn => { + const before = hooks["extension.before"] + const after = hooks["extension.after"] + const type = isAsync(value) ? "async" : "sync" + const snapshot = extension.snapshot + const named = (run: (args: ReadonlyArray) => unknown) => { + // The program sees the extension function's own name and length. + Object.defineProperty(run, "name", { value: value.name }) + Object.defineProperty(run, "length", { value: value.length }) + return Host.fn(run, { type, snapshot, constructible: isConstructible(value), takesFunctions: true }) } + if (before === undefined && after === undefined) return named((args) => value(...args)) + return named((args) => + Effect.gen(function* () { + const call: ToolRuntime.ExtensionInvocation = { extension: extension.name, name, args } + if (before !== undefined) yield* before(call) + let result: unknown + try { + result = value(...args) + } catch (reason) { + if (after !== undefined) yield* after(call, { status: "failure", error: reason }) + return yield* Effect.fail(reason) + } + const settled = Effect.isEffect(result) + ? (result as Effect.Effect) + : result instanceof Promise + ? Effect.tryPromise({ try: () => result as Promise, catch: (reason) => reason }) + : undefined + if (settled === undefined) { + if (after !== undefined) yield* after(call, { status: "success", value: result }) + return result + } + const observed = hooked(call, undefined, after, settled) + // A returned Promise is still pending once the call has run: the program holds a promise for it. + return type === "sync" && result instanceof Promise ? observed : yield* observed + }), + ) +} + +// A language program whose outcomes report the run's tool calls in place of its metadata. +const reported = (program: Language.Runtime.Program): Program => ({ + run: () => Effect.map(program.run(), outcome), + suspend: (options) => Effect.map(program.suspend(options), outcome), +}) + +// A run's outcome as Code Mode reports it: the tool calls in place of the run's metadata. +const outcome = (ended: Language.Runtime.Outcome): Outcome => { + if (ended.ok === "suspended") return ended + const { metadata, ...result } = ended + return { ...result, toolCalls: ToolRuntime.toolCalls(metadata) } as Result } diff --git a/packages/codemode/src/extension.ts b/packages/codemode/src/codemode/extension.ts similarity index 79% rename from packages/codemode/src/extension.ts rename to packages/codemode/src/codemode/extension.ts index c6864c1a1487..49ff9ffb0435 100644 --- a/packages/codemode/src/extension.ts +++ b/packages/codemode/src/codemode/extension.ts @@ -5,8 +5,10 @@ import type { Effect } from "effect" /** * Host values a program reaches directly as globals: functions, or objects of functions such as `fs`. Values crossing * in either direction are converted, never shared: arguments come in as copies, results go out as copies, and a - * function inside a result is callable the same way. Extension calls are not tool calls. A global may return a value, - * a Promise, or an Effect, and an Effect's requirements join the runtime's environment. + * function inside a result is callable the same way. When the program passes such a function back, it arrives as + * itself, so `Bun.write(dest, Bun.file(src))` can tell what it was given; a global never crosses back, and is left out + * like the program's own functions. Extension calls are not tool calls. A global may return a value, a Promise, or an + * Effect, and an Effect's requirements join the runtime's environment. * * A global is synchronous unless it is an `async` function or is marked with `async`. A synchronous global that * returns an Effect blocks the program until the Effect completes and returns its value, as `fs.readFileSync` does; @@ -64,13 +66,7 @@ export const make = >>( const asyncGlobals = new WeakSet() -/** A live fetch response whose I/O is owned by the host for the lifetime of the program. */ -export class StreamingResponse { - constructor( - readonly response: Response, - readonly redirected = response.redirected, - ) {} -} +export { StreamingResponse } from "../language/streaming-response.js" const AsyncFunction = (async () => {}).constructor /** Marks a global that returns a Promise or an Effect, so a denied call rejects its promise instead of throwing. */ @@ -80,3 +76,16 @@ export const async = (global: F): F => { } export const isAsync = (global: Function) => global instanceof AsyncFunction || asyncGlobals.has(global) + +const constructibleGlobals = new WeakSet() + +/** + * Lets a program call a function with `new`, as it would a class such as `Bun.Glob`. `new` calls it the same way a + * plain call does, and the program gets what it returns. + */ +export const constructible = (global: F): F => { + constructibleGlobals.add(global) + return global +} + +export const isConstructible = (global: Function) => constructibleGlobals.has(global) diff --git a/packages/codemode/src/namespace.ts b/packages/codemode/src/codemode/namespace.ts similarity index 100% rename from packages/codemode/src/namespace.ts rename to packages/codemode/src/codemode/namespace.ts diff --git a/packages/codemode/src/openapi/TODO.md b/packages/codemode/src/codemode/openapi/TODO.md similarity index 100% rename from packages/codemode/src/openapi/TODO.md rename to packages/codemode/src/codemode/openapi/TODO.md diff --git a/packages/codemode/src/openapi/index.ts b/packages/codemode/src/codemode/openapi/index.ts similarity index 100% rename from packages/codemode/src/openapi/index.ts rename to packages/codemode/src/codemode/openapi/index.ts diff --git a/packages/codemode/src/openapi/runtime.ts b/packages/codemode/src/codemode/openapi/runtime.ts similarity index 100% rename from packages/codemode/src/openapi/runtime.ts rename to packages/codemode/src/codemode/openapi/runtime.ts diff --git a/packages/codemode/src/openapi/spec.ts b/packages/codemode/src/codemode/openapi/spec.ts similarity index 100% rename from packages/codemode/src/openapi/spec.ts rename to packages/codemode/src/codemode/openapi/spec.ts diff --git a/packages/codemode/src/openapi/types.ts b/packages/codemode/src/codemode/openapi/types.ts similarity index 100% rename from packages/codemode/src/openapi/types.ts rename to packages/codemode/src/codemode/openapi/types.ts diff --git a/packages/codemode/src/scan.ts b/packages/codemode/src/codemode/scan.ts similarity index 96% rename from packages/codemode/src/scan.ts rename to packages/codemode/src/codemode/scan.ts index a932cff4dd21..6610f7fa1acf 100644 --- a/packages/codemode/src/scan.ts +++ b/packages/codemode/src/codemode/scan.ts @@ -1,9 +1,9 @@ import type { AnyNode, BlockStatement, ModuleDeclaration, Pattern, Program, Statement } from "acorn" import { Result } from "effect" import type { Diagnostic } from "./codemode.js" -import { parseProgram } from "./runtime/execute.js" -import { formatLocation } from "./runtime/model.js" -import { moduleName } from "./runtime/modules.js" +import { parseProgram } from "../language/runtime/execute.js" +import { formatLocation } from "../language/runtime/model.js" +import { moduleName } from "../language/runtime/modules.js" /** * Extracts every `tools.` referenced by a Code Mode program, in first-seen order. Fails on syntax errors, @@ -14,7 +14,8 @@ export function scanTools(code: string): Result.Result, Di return Result.fail({ kind: "ParseError", message: "Code cannot be empty." }) } const parsed = parseProgram(code) - if (!parsed.ok) return Result.fail(parsed.error) + // A parse failure's kind is always ParseError. + if (!parsed.ok) return Result.fail(parsed.error as Diagnostic) const found = new Set() const scopes: Array> = [collectScope(parsed.program, true)] diff --git a/packages/codemode/src/tool-error.ts b/packages/codemode/src/codemode/tool-error.ts similarity index 100% rename from packages/codemode/src/tool-error.ts rename to packages/codemode/src/codemode/tool-error.ts diff --git a/packages/codemode/src/tool-runtime.ts b/packages/codemode/src/codemode/tool-runtime.ts similarity index 67% rename from packages/codemode/src/tool-runtime.ts rename to packages/codemode/src/codemode/tool-runtime.ts index d01bc9a8165e..eabe07d080cb 100644 --- a/packages/codemode/src/tool-runtime.ts +++ b/packages/codemode/src/codemode/tool-runtime.ts @@ -1,7 +1,11 @@ import { Cause, Effect, Exit, Formatter, Schema } from "effect" -import type { Json } from "./data.js" +import type { Json } from "../language/data.js" +import { compareText } from "../language/runtime/operations.js" import type { DiagnosticKind } from "./codemode.js" -import { toolError } from "./tool-error.js" +import { ToolError, toolError } from "./tool-error.js" +import { Host } from "../language/host-function.js" +import { HostFailure } from "../language/runtime/model.js" +import type { Run } from "../language/runtime/execute.js" import { decodeInput as decodeToolInput, decodeOutput as decodeToolOutput, @@ -13,11 +17,9 @@ import { stringParameter, } from "./tool-schema.js" import { isNamespace, type Namespace } from "./namespace.js" -import { type Durable, type Idempotent, isTool, type Tool, type ToolContext } from "./tool.js" +import { type Durable, type ExecuteFn, isTool, type Tool, type ToolContext } from "./tool.js" import type { Tools } from "./tools.js" -export const compareText = (left: string, right: string) => (left < right ? -1 : left > right ? 1 : 0) - export type Services = ServicesOf type ServicesOf> = Depth["length"] extends 8 @@ -120,10 +122,6 @@ export const toolExpression = (path: string) => .map((segment) => (identifierSegment.test(segment) ? `.${segment}` : `[${JSON.stringify(segment)}]`)) .join("") -export class ToolReference { - constructor(readonly path: ReadonlyArray) {} -} - // Dots in tool names are namespace separators; the last tool for a canonical path wins. type ToolNode = { tool?: Tool @@ -520,12 +518,17 @@ const namespaceKeys = (root: ToolNode, path: ReadonlyArray): Reado const segments = canonicalSegments(path) const node = lookup(root, segments) if (node === undefined) { - throw new ToolRuntimeError("UnknownTool", `Unknown tool namespace '${segments.join(".")}'.`) + throw toolRuntimeError("UnknownTool", `Unknown tool namespace '${segments.join(".")}'.`) } return Array.from(node.children.keys()) } -const resolve = (root: ToolNode, path: ReadonlyArray, index: ReadonlyArray): Tool => { +// The tool at `path`, or why there is none. +const resolve = ( + root: ToolNode, + path: ReadonlyArray, + index: ReadonlyArray, +): Tool | HostFailure => { const segments = canonicalSegments(path) const node = lookup(root, segments) if (node === undefined) { @@ -536,7 +539,7 @@ const resolve = (root: ToolNode, path: ReadonlyArray, index: Reado ns ? segments.slice(1).join(" ") : name, index, )[0] - throw new ToolRuntimeError( + return toolRuntimeError( "UnknownTool", closest ? `Unknown tool '${name}'. Did you mean ${toolExpression(closest.description.path)}?` @@ -552,7 +555,7 @@ const resolve = (root: ToolNode, path: ReadonlyArray, index: Reado .slice(0, 3) .map((entry) => toolExpression(entry.description.path)) const more = under.length - shown.length - throw new ToolRuntimeError( + return toolRuntimeError( "UnknownTool", shown.length === 0 ? `Tool '${name}' is not callable.` @@ -567,107 +570,77 @@ const resolve = (root: ToolNode, path: ReadonlyArray, index: Reado return node.tool } -export class ToolRuntimeError extends Error { - constructor( - readonly kind: Extract< - DiagnosticKind, - "UnknownTool" | "InvalidToolInput" | "InvalidToolOutput" | "ToolCallLimitExceeded" - >, - message: string, - readonly suggestions: ReadonlyArray = [], - ) { - super(message) - this.name = "ToolRuntimeError" - } -} +/** A tool-layer failure the program sees with its own diagnostic kind. */ +const toolRuntimeError = ( + kind: Extract, + message: string, + suggestions: ReadonlyArray = [], +) => new HostFailure(kind, message, suggestions) + +// A ToolError (a tool's failure, or a hook's) is reported as `ToolFailure`. +const diagnosed = (effect: Effect.Effect) => + Effect.mapError(effect, (error) => + error instanceof ToolError ? new HostFailure("ToolFailure", error.message) : error, + ) -export type ExecuteCallOptions = { - readonly key: string - readonly admit: (name: string) => void - readonly detaching?: (() => boolean) | undefined +/** What a run keeps in `run.metadata`: its tool-call budget and the calls admitted so far, by key. */ +type State = { + readonly maxToolCalls: number | null + readonly calls: Array<{ readonly name: string; readonly key: string }> } -/** The tool bridge of one execution. Arguments arrive and results leave as JSON; program values never enter. */ -export type ToolRuntime = { - readonly calls: Array - readonly hooks: Hooks - readonly invocations: Map - readonly record: (name: string) => void - readonly execute: ( - path: ReadonlyArray, - args: Array, - options: ExecuteCallOptions, - ) => Effect.Effect - readonly search: ( - args: Array, - options: ExecuteCallOptions, - ) => Effect.Effect - readonly keys: (path: ReadonlyArray) => ReadonlyArray - /** How the tool at `path` executes on suspend (`undefined` when unknown or plain). */ - readonly mode: ( - path: ReadonlyArray, - ) => Idempotent | Durable | undefined - /** Admits a call to a durable tool, runs `tool.before`, and starts its work under `key`: its JSON data. */ - readonly start: ( - path: ReadonlyArray, - args: Array, - key: string, - admit: (name: string) => void, - ) => Effect.Effect - /** - * Waits for a durable tool's running work: its output, as `execute` gives it. `detaching` says whether an - * interruption detaches to suspend the run, which `tool.after` doesn't see, or ends the call, which it sees as - * interrupted. - */ - readonly wait: ( - path: ReadonlyArray, - args: Array, - key: string, - data: Json, - detaching: () => boolean, - ) => Effect.Effect - /** Cancels a durable tool's started work when the run is interrupted or ends. */ - readonly cancel: (path: ReadonlyArray, data: Json) => Effect.Effect -} +/** The tool calls a run admitted, from its `run.metadata`. */ +export const toolCalls = (metadata: Json): Array => + metadata === null ? [] : (metadata as unknown as State).calls.map((call) => ({ name: call.name })) -/** Per-execution call state over tools prepared once for the runtime. */ -export const make = ( +/** + * The `tools` and `search` globals of one run, over tools prepared once for the runtime. The run's budget and its + * admitted calls live in `run.metadata`, so a resumed run goes on counting where it was saved. Arguments arrive as + * host values and are converted to JSON here; results leave as JSON. + */ +export const globals = ( prepared: Prepared, + hooks: Hooks, + run: Run, maxToolCalls: number | undefined, - hooks: Hooks = {}, -): ToolRuntime => { - const calls: Array = [] +): { readonly tools: Host.Namespace; readonly search: Host.Fn } => { + const state = (run.metadata ??= { maxToolCalls: maxToolCalls ?? null, calls: [] }) as unknown as State + // The input of each durable call started in this runtime, for `tool.after` once it ends. const invocations = new Map() const root = prepared.root - const recordCall = (call: ToolCall): void => { - if (maxToolCalls !== undefined && calls.length >= maxToolCalls) { - throw new ToolRuntimeError("ToolCallLimitExceeded", `Execution exceeded its tool-call limit of ${maxToolCalls}.`) + // Counts a call against `maxToolCalls`. An idempotent call run again on resume was counted when it first ran. + const admit = (name: string, key: string, again: boolean): void => { + if (again && state.calls.some((call) => call.key === key)) return + if (state.maxToolCalls !== null && state.calls.length >= state.maxToolCalls) { + throw toolRuntimeError( + "ToolCallLimitExceeded", + `Execution exceeded its tool-call limit of ${state.maxToolCalls}.`, + ) } - calls.push(call) + state.calls.push({ name, key }) } - const decode = (name: string, tool: Tool, args: Array) => - Effect.gen(function* () { - const normalized = args.length === 0 ? [{}] : args - if (normalized.length !== 1) - throw new ToolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`) - const input = normalized[0] - const field = typeof input === "string" ? stringParameter(tool)?.name : undefined - return yield* Effect.try({ - try: () => decodeToolInput(tool, field === undefined ? input : { [field]: input }), - catch: (cause) => - new ToolRuntimeError( - "InvalidToolInput", - `Invalid input for tool '${name}': ${String(cause)}`, - name === "search" - ? [ - 'Pass search a query string or an object, such as search("issues") or search({ namespace: "github" }).', - ] - : ["The signature may have changed. Use search to get the current signature."], - ), - }) - }) + // A call's arguments as the tool's decoded input; throws the failure the call fails with. + const decode = (name: string, tool: Tool, args: ReadonlyArray): unknown => { + const json = args.map(toJson) + const normalized = json.length === 0 ? [{}] : json + if (normalized.length !== 1) + throw toolRuntimeError("InvalidToolInput", `Tool '${name}' expects at most one input object.`) + const input = normalized[0] + const field = typeof input === "string" ? stringParameter(tool)?.name : undefined + try { + return decodeToolInput(tool, field === undefined ? input : { [field]: input }) + } catch (cause) { + throw toolRuntimeError( + "InvalidToolInput", + `Invalid input for tool '${name}': ${String(cause)}`, + name === "search" + ? ['Pass search a query string or an object, such as search("issues") or search({ namespace: "github" }).'] + : ["The signature may have changed. Use search to get the current signature."], + ) + } + } // A tool's own failure becomes a ToolError; interruption passes through. const failure = (effect: Effect.Effect) => @@ -691,147 +664,197 @@ export const make = ( const text = JSON.stringify(decodeToolOutput(tool, raw)) return text === undefined ? undefined : JSON.parse(text) }, - catch: (cause) => new ToolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`), + catch: (cause) => toolRuntimeError("InvalidToolOutput", `Invalid output from tool '${name}': ${cause}`), }) - const executeTool = (name: string, tool: Tool, args: Array, options: ExecuteCallOptions) => - Effect.gen(function* () { - if ("_tag" in tool.execute && tool.execute._tag === "CodeModeDurable") { - throw new TypeError(`Tool '${name}' is a durable tool; start it with ToolRuntime.start.`) - } - const run = "_tag" in tool.execute ? tool.execute.execute : tool.execute - const key = options.key - const input = yield* decode(name, tool, args) - yield* Effect.sync(() => options.admit(name)) - return yield* hooked( - { name, input, key }, - hooks["tool.before"], - hooks["tool.after"], - Effect.gen(function* () { - const raw = yield* failure(Effect.suspend(() => run(input, { key }))) + const executeTool = ( + name: string, + tool: Tool, + run: ExecuteFn, + args: ReadonlyArray, + call: Host.Call, + again: boolean, + ) => + diagnosed( + Effect.suspend(() => { + const key = call.key + let input: unknown + try { + input = decode(name, tool, args) + admit(name, key, again) + } catch (error) { + return Effect.fail(error) + } + return hooked( + { name, input, key }, + hooks["tool.before"], + hooks["tool.after"], + Effect.flatMap(failure(Effect.suspend(() => run(input, { key }))), (raw) => output(name, tool, raw)), + call.suspending, + ) + }), + ) + + const durableFn = (name: string, tool: Tool, durable: Durable) => + Host.durableFn({ + start: (args, call) => + diagnosed( + Effect.gen(function* () { + const key = call.key + const input = yield* Effect.try({ try: () => decode(name, tool, args), catch: (error) => error }) + yield* Effect.try({ try: () => admit(name, key, false), catch: (error) => error }) + const invocation: ToolInvocation = { name, input, key } + invocations.set(key, invocation) + const before = hooks["tool.before"] + const after = hooks["tool.after"] + const started = failure(Effect.suspend(() => durable.start(input, { key }))).pipe( + Effect.flatMap((raw) => + Effect.try({ + try: (): Json => { + const text = JSON.stringify(raw) + return text === undefined ? null : JSON.parse(text) + }, + catch: (cause) => + toolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${name}': ${cause}`), + }), + ), + ) + const observed = + after === undefined + ? started + : Effect.onError(started, (cause) => { + invocations.delete(key) + if (Cause.hasInterruptsOnly(cause)) return after(invocation, { status: "interrupted" }) + return after(invocation, { status: "failure", error: Cause.squash(cause) }) + }) + return yield* before === undefined ? observed : Effect.andThen(before(invocation), observed) + }), + ), + wait: (data, call) => { + const key = call.key + const wait = durable.wait as (data: Json, context: ToolContext) => Effect.Effect + const after = hooks["tool.after"] + const waited = Effect.gen(function* () { + const raw = yield* failure(Effect.suspend(() => wait(data, { key }))) return yield* output(name, tool, raw) - }), - options.detaching, - ) + }) + if (after === undefined) return diagnosed(waited) + const known = invocations.get(key) + const invocation = + known !== undefined + ? Effect.succeed(known) + : Effect.try({ + try: (): ToolInvocation => ({ name, input: decode(name, tool, call.args), key }), + catch: (error) => error, + }) + return diagnosed( + Effect.flatMap(invocation, (invocation) => + Effect.onExit(waited, (exit) => { + if (call.suspending()) return Effect.void + invocations.delete(key) + if (Exit.isSuccess(exit)) return after(invocation, { status: "success", value: exit.value }) + if (Cause.hasInterruptsOnly(exit.cause)) return after(invocation, { status: "interrupted" }) + return after(invocation, { status: "failure", error: Cause.squash(exit.cause) }) + }), + ), + ) + }, + cancel: (data) => diagnosed(failure((durable.cancel as (data: Json) => Effect.Effect)(data))), }) + // Each tool's host function, made once per run when it is first called. + const functions = new Map, Host.Fn | Host.DurableFn>() + const toolFunction = (name: string, tool: Tool): Host.Fn | Host.DurableFn => { + const known = functions.get(tool) + if (known !== undefined) return known + const execute = tool.execute + const made = + "_tag" in execute && execute._tag === "CodeModeDurable" + ? durableFn(name, tool, execute) + : Host.fn( + (args, call) => + executeTool( + name, + tool, + "_tag" in execute ? execute.execute : execute, + args, + call, + "_tag" in execute && execute._tag === "CodeModeIdempotent", + ), + { type: "async", idempotent: "_tag" in execute && execute._tag === "CodeModeIdempotent" }, + ) + functions.set(tool, made as Host.Fn | Host.DurableFn) + return made as Host.Fn | Host.DurableFn + } + // `search("notes")` is shorthand for `search({ query: "notes" })`. - const search = (args: Array, options: ExecuteCallOptions) => + const searchCall = (args: ReadonlyArray, call: Host.Call) => executeTool( "search", makeSearchTool(prepared.searchIndex), + makeSearchTool(prepared.searchIndex).execute as ExecuteFn, typeof args[0] === "string" ? [{ query: args[0] }, ...args.slice(1)] : args, - options, + call, + false, ) - - const durableTool = ( - path: ReadonlyArray, - ): { - readonly name: string - readonly tool: Tool - readonly durable: Durable - } => { - const segments = canonicalSegments(path) - const name = segments.join(".") - const tool = lookup(root, segments)?.tool - if (tool === undefined || !("_tag" in tool.execute) || tool.execute._tag !== "CodeModeDurable") { - throw new TypeError(`Tool '${name}' is not a durable tool.`) - } - return { name, tool, durable: tool.execute } - } + const search = Host.fn(searchCall, { type: "sync-thenable" }) + // Models often write `tools.search(...)` for the bare `search(...)`; it is honored unless a tool owns that path. + const searchAlias = Host.fn(searchCall, { type: "async" }) return { - calls, - hooks, - invocations, - record: (name) => recordCall({ name }), - keys: (path) => namespaceKeys(root, path), - search: (args, options) => Effect.suspend(() => search(args, options)), - execute: (path, args, options) => - Effect.suspend(() => { + tools: Host.namespace({ + keys: (path) => namespaceKeys(root, path), + resolve: (path) => { const segments = canonicalSegments(path) - // Models often write `tools.search(...)` for the bare `search(...)`; honor it unless a tool owns that path. - if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined) - return search(args, options) - return executeTool(segments.join("."), resolve(root, path, prepared.searchIndex), args, options) - }), - mode: (path) => { - const segments = canonicalSegments(path) - if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined) return undefined - const execute = lookup(root, segments)?.tool?.execute - return execute !== undefined && "_tag" in execute ? execute : undefined - }, - start: (path, args, key, admit) => - Effect.suspend(() => { - const found = durableTool(path) - return Effect.gen(function* () { - const input = yield* decode(found.name, found.tool, args) - yield* Effect.sync(() => admit(found.name)) - const invocation: ToolInvocation = { name: found.name, input, key } - invocations.set(key, invocation) - const before = hooks["tool.before"] - const after = hooks["tool.after"] - const run = failure(Effect.suspend(() => found.durable.start(input, { key }))).pipe( - Effect.flatMap((raw) => - Effect.try({ - try: (): Json => { - const text = JSON.stringify(raw) - return text === undefined ? null : JSON.parse(text) - }, - catch: (cause) => - new ToolRuntimeError("InvalidToolOutput", `Invalid start data from tool '${found.name}': ${cause}`), - }), - ), - ) - const observed = - after === undefined - ? run - : Effect.onError(run, (cause) => { - invocations.delete(key) - if (Cause.hasInterruptsOnly(cause)) return after(invocation, { status: "interrupted" }) - return after(invocation, { status: "failure", error: Cause.squash(cause) }) - }) - return yield* before === undefined ? observed : Effect.andThen(before(invocation), observed) - }) - }), - wait: (path, args, key, data, detaching) => - Effect.suspend(() => { - const found = durableTool(path) - const wait = found.durable.wait as (data: Json, context: ToolContext) => Effect.Effect - const after = hooks["tool.after"] - const run = Effect.gen(function* () { - const raw = yield* failure(Effect.suspend(() => wait(data, { key }))) - return yield* output(found.name, found.tool, raw) - }) - if (after === undefined) return run - const known = invocations.get(key) - const callEffect = - known !== undefined - ? Effect.succeed(known) - : Effect.map( - decode(found.name, found.tool, args), - (input): ToolInvocation => ({ - name: found.name, - input, - key, - }), - ) - return Effect.flatMap(callEffect, (call) => - Effect.onExit(run, (exit) => { - if (detaching()) return Effect.void - invocations.delete(key) - if (Exit.isSuccess(exit)) return after(call, { status: "success", value: exit.value }) - if (Cause.hasInterruptsOnly(exit.cause)) return after(call, { status: "interrupted" }) - return after(call, { status: "failure", error: Cause.squash(exit.cause) }) - }), - ) - }), - cancel: (path, data) => - Effect.suspend(() => { - const cancel = durableTool(path).durable.cancel as (data: Json) => Effect.Effect - return failure(cancel(data)) - }), + if (segments.length === 1 && segments[0] === "search" && lookup(root, segments) === undefined) { + return searchAlias as Host.Fn + } + const found = resolve(root, path, prepared.searchIndex) + // Tools are async, so a call to a missing one rejects its promise rather than throwing at the call. + return found instanceof HostFailure + ? Host.fn(() => Effect.fail(found), { type: "async" }) + : toolFunction(segments.join("."), found) + }, + }), + search: search as Host.Fn, + } +} + +/** + * A tool argument as JSON, as `JSON.stringify` would give it, with a few departures so a mistake is not a silent `{}`: + * an Error gives its name, message, and own fields, a Set its items, a URLSearchParams its query string, and a + * Uint8Array asks to be encoded as text first. A `__proto__` key is dropped. + */ +const toJson = (value: unknown): Json | undefined => { + if (value === undefined || typeof value === "function") return undefined + if (value === null || typeof value === "string" || typeof value === "boolean") return value + if (typeof value === "number") return Number.isFinite(value) ? value : null + if (typeof value !== "object") return undefined + if (value instanceof Date) return Number.isFinite(value.getTime()) ? value.toISOString() : null + if (value instanceof URL) return value.href + if (value instanceof URLSearchParams) return value.toString() + if (value instanceof Headers) return Object.fromEntries(value) + if (value instanceof Uint8Array) { + throw new HostFailure( + "InvalidDataValue", + "A Uint8Array cannot cross to a tool; pass text instead, e.g. `new TextDecoder().decode(bytes)` or `bytes.toBase64()`.", + ) + } + const items = Array.isArray(value) ? value : value instanceof Set ? [...value] : undefined + if (items !== undefined) return Array.from(items, (item) => toJson(item) ?? null) + const out: Record = {} + const fields: Array = + value instanceof Error + ? [["name", value.name], ["message", value.message], ...Object.entries(value)] + : value instanceof Map || value instanceof RegExp + ? [] + : Object.entries(value) + for (const [key, item] of fields) { + if (key === "__proto__") continue + const json = toJson(item) + if (json !== undefined) out[key] = json } + return out } export * as ToolRuntime from "./tool-runtime.js" diff --git a/packages/codemode/src/tool-schema.ts b/packages/codemode/src/codemode/tool-schema.ts similarity index 100% rename from packages/codemode/src/tool-schema.ts rename to packages/codemode/src/codemode/tool-schema.ts diff --git a/packages/codemode/src/tool.ts b/packages/codemode/src/codemode/tool.ts similarity index 100% rename from packages/codemode/src/tool.ts rename to packages/codemode/src/codemode/tool.ts diff --git a/packages/codemode/src/tools.ts b/packages/codemode/src/codemode/tools.ts similarity index 100% rename from packages/codemode/src/tools.ts rename to packages/codemode/src/codemode/tools.ts diff --git a/packages/codemode/src/host.ts b/packages/codemode/src/host.ts deleted file mode 100644 index 7ddad97120a1..000000000000 --- a/packages/codemode/src/host.ts +++ /dev/null @@ -1,318 +0,0 @@ -import type { Diagnostic } from "./codemode.js" -import { fromJson, type Json } from "./data.js" -import { startExtensionCall } from "./runtime/extensions.js" -import type { Interpreter } from "./runtime/interpreter.js" -import { type AstNode, Lost, lostError } from "./runtime/model.js" -import { Arr, PromiseObj, type Value } from "./runtime/objects.js" -import { promiseLike } from "./runtime/promises.js" -import type { Completion } from "./runtime/signals.js" -import { type ExtensionRequest, HostCall, type HostRequest, Resolving } from "./scheduler.js" -import type { ExtensionInvocation } from "./tool-runtime.js" -import type { Code } from "./code.js" -import { internalCode, tryFastBoundaryOrJson, VmIntrinsic } from "./internal-prelude.js" -import { type FrameMode, Mode, type Vm } from "./vm.js" - -// The VM's side of host work (plan §4.10): tool calls, `search`, and extension calls, the host calls they queue, and -// the blocking call a job may wait on. - -/** What host calls need from the promise scheduler. */ -export type Promises = { - newPromise(): PromiseObj - resolving(promise: PromiseObj, site?: AstNode): Resolving - /** Unhandled rejections, in promise creation order. */ - diagnostics(): Array -} - -/** A native's answer asking the VM to stop the job until the blocking host call it queued completes. */ -export const BLOCK: unique symbol = Symbol("vm.block") -/** A host operation pushed an internal frame that produces its result. */ -export const ENTERED: unique symbol = Symbol("vm.entered") - -export type HostResult = Value | typeof BLOCK | typeof ENTERED - -/** What the VM gives host operations: its runtime, and a way to run an internal prelude function at the call. */ -export type HostVm = { - readonly ctx: Interpreter - /** - * Runs an internal prelude function at the call `origin`; its result is the call's. In another `mode` than `CALLER`, - * it settles `target`, and the call's result is `answer`: in `REJECT_ONLY` mode, a failure in it rejects `target`. - */ - pushInternal( - code: Code, - args: ReadonlyArray, - origin: AstNode | undefined, - mode?: FrameMode, - target?: unknown, - answer?: Value, - ): unknown -} - -type Delivery = { readonly call: HostCall; readonly completion: Completion } - -/** Visited nodes a native fast path may spend before it hands the work to the prelude walk, which slices. */ -export const VISIT_BUDGET = 2048 - -/** - * The host calls of one run: queued (written ahead, for the driver to start at the next stop), running, completed and - * waiting in the inbox, and the blocking call the current job waits on. Ids count up in program order. - */ -export class HostCalls { - /** Names this run's host calls (`:`). */ - runId = "" - queued: Array = [] - readonly running = new Map() - readonly inbox: Array = [] - nextID = 0 - /** The blocking call the job on the stack waits on. */ - blocked: HostCall | undefined = undefined - /** A completed blocking call's outcome, which continues the job on the stack before anything else runs. */ - input: { readonly call: HostCall; readonly completion: Completion } | undefined = undefined - /** The synchronous extension call the program is in the middle of: see `Vm.activeExtension`. */ - extension: { readonly key: string; readonly path: string } | undefined = undefined - extensionResume: ((completion: Completion) => HostResult) | undefined = undefined - - constructor( - private readonly ctx: Interpreter, - readonly promises: Promises, - ) {} - - /** Queues concurrent host work; `resolving` settles its promise in a later job, in completion order. */ - host( - request: HostRequest, - call: { - readonly label: string - readonly result: "json" | "value" - readonly resolving: Resolving - readonly site: AstNode | undefined - }, - ): HostCall { - return this.queue(request, call.label, false, call.result, call.resolving, call.site) - } - - /** Queues blocking `search` work: the job stops until it completes, then continues with its result. */ - blockSearch(args: Array): typeof BLOCK { - return this.block({ kind: "search", args }, "search", undefined) - } - - /** Queues blocking extension work: the job stops until it completes, then continues with `resume`. */ - blockExtension(call: ExtensionRequest, resume: (completion: Completion) => HostResult): typeof BLOCK { - this.block({ kind: "extension", call }, "extension", resume) - this.extension = { key: this.blocked!.key, path: call.label } - return BLOCK - } - - private block( - request: HostRequest, - label: string, - resume: ((completion: Completion) => HostResult) | undefined, - ): typeof BLOCK { - this.blocked = this.queue(request, label, true, "value", undefined, undefined) - this.extensionResume = resume - return BLOCK - } - - private queue( - request: HostRequest, - label: string, - blocking: boolean, - result: "json" | "value", - resolving: Resolving | undefined, - site: AstNode | undefined, - ): HostCall { - const id = this.nextID++ - const call = new HostCall(id, `${this.runId}:${id}`, request, label, blocking, result, resolving, site) - this.queued.push(call) - return call - } - - /** Continues the job stopped at a blocking call with its outcome. */ - resume(input: { readonly call: HostCall; readonly completion: Completion }): HostResult { - const resume = this.extensionResume - if (resume !== undefined) { - this.extensionResume = undefined - return resume(input.completion) - } - if (!input.completion.ok) throw input.completion.error - return promiseLike(this.ctx, fromJson(this.ctx, input.completion.value as Json | undefined)) - } - - /** Hands the queued calls to the driver: they are running from now on. */ - handOver(): Array { - const calls = this.queued - this.queued = [] - for (const call of calls) this.running.set(call.id, call) - return calls - } - - complete(id: number, completion: Completion): void { - const call = this.running.get(id) - if (call === undefined) return - this.running.delete(id) - // Its outcome is all that is left: the host function, its arguments, and what it returned are dropped. - call.data = undefined - const request = call.request - if (request.kind === "extension") { - request.call.invocation = undefined - request.call.host = undefined - request.call.args = [] - } - if (call.blocking) { - call.status = "completed" - if (this.blocked !== call) return - this.blocked = undefined - this.input = { call, completion } - return - } - this.inbox.push({ call, completion }) - } - - /** Settles the oldest completed concurrent call's promise; false when the inbox is empty. */ - deliver(): boolean { - const delivery = this.inbox.shift() - if (delivery === undefined) return false - const call = delivery.call - const completion = delivery.completion - call.status = "completed" - const resolving = call.resolving! - if (!completion.ok) resolving.reject(completion.error) - else { - resolving.resolve( - call.result === "json" ? fromJson(this.ctx, completion.value as Json | undefined) : (completion.value as Value), - ) - } - return true - } -} - -// --- Tool calls --- - -/** - * A tool call returns its promise at once. Plain-data arguments cross on the native fast path; anything else - * (`toJSON`, a promise, a deep or shared value) crosses through `$vmCross` at the call, which queues the call with the - * same `Resolving` or rejects it. Either way the call is queued once its arguments have crossed, and the driver starts - * it before any other job runs. - */ -export const callTool = ( - vm: HostVm, - hosts: HostCalls, - path: ReadonlyArray, - args: Array, - site: AstNode | undefined, -): HostResult => { - const resolving = hosts.promises.resolving(hosts.promises.newPromise()) - const json = crossFast(vm.ctx, args) - if (json === undefined) { - // A failure while crossing rejects the promise, which the caller receives either way. - vm.pushInternal( - internalCode("$vmCross"), - [arrayOf(vm, args), arrayOf(vm, [...path]), resolving], - site, - Mode.REJECT_ONLY, - resolving, - resolving.promise, - ) - return ENTERED - } - if (json instanceof Failure) { - resolving.reject(json.error) - return resolving.promise - } - hosts.host({ kind: "tool", path, args: json }, { label: path.join("."), result: "json", resolving, site }) - return resolving.promise -} - -/** `search(...)`: blocking; its result can be used directly, or awaited and chained like a Promise. */ -export const callSearch = (vm: HostVm, hosts: HostCalls, args: Array, site: AstNode | undefined): HostResult => { - const json = crossFast(vm.ctx, args) - if (json === undefined) { - vm.pushInternal(internalCode("$vmCross"), [arrayOf(vm, args), "search"], site) - return ENTERED - } - if (json instanceof Failure) throw json.error - return hosts.blockSearch(json) -} - -class Failure { - constructor(readonly error: unknown) {} -} - -// Every argument as host JSON on the budgeted fast path; undefined when one needs the prelude walk. -const crossFast = (ctx: Interpreter, args: Array): Array | Failure | undefined => { - const json: Array = [] - try { - for (const arg of args) { - const crossed = tryFastBoundaryOrJson(ctx.builtins, arg, true, VISIT_BUDGET) - if (!crossed.ok) return undefined - json.push(crossed.json) - } - } catch (error) { - return new Failure(error) - } - return json -} - -const arrayOf = (vm: HostVm, values: Array) => new Arr(vm.ctx.builtins.Array, [...values]) - -// --- Extension calls --- - -/** - * A synchronous extension function's call, staged here so its host values never enter a frame: the `extension.before` - * hook (blocking), then the host function; what it returns settles now (blocking, an Effect - * included) unless it is a Promise, which the program holds a promise for. The call is active, holding live host values, until its result is a program - * value or its concurrent call is queued. - */ -export const callExtension = ( - ctx: Interpreter, - hosts: HostCalls, - slots: ReadonlyArray, - site: AstNode | undefined, -): HostResult => { - const label = slots[2] as string - const call = { - host: slots[0], - args: slots[1] as ReadonlyArray, - label, - invocation: slots[3] as ExtensionInvocation | undefined, - extension: slots[4] as string | undefined, - } - if (call.host instanceof Lost) throw lostError(`The function ${label}`) - const run = (): HostResult => { - const started = startExtensionCall(ctx, call.host as Function, call.args, call, site) - if (started instanceof PromiseObj) { - hosts.extension = undefined - return started - } - return hosts.blockExtension(started, (completion) => { - hosts.extension = undefined - if (!completion.ok) throw completion.error - return completion.value as Value - }) - } - if (call.invocation === undefined || ctx.tools.hooks["extension.before"] === undefined) return run() - const before: ExtensionRequest = { stage: "before", ...call, host: undefined, threw: false } - return hosts.blockExtension(before, (completion) => { - if (!completion.ok) { - hosts.extension = undefined - throw completion.error - } - return run() - }) -} - -/** The intrinsics the boundary walk calls to queue a tool or search host call. */ -export const registerHostIntrinsics = (map: Map, vm: Vm): void => { - const json = (value: Value) => (value as Arr).items as Array - map.set( - "$HostTool", - new VmIntrinsic("$HostTool", ([path, args, resolving]) => { - const keys = (path as Arr).items as Array - const site = vm.frames[vm.frames.length - 1]?.origin - vm.hosts.host( - { kind: "tool", path: keys, args: json(args) }, - { label: keys.join("."), result: "json", resolving: resolving as unknown as Resolving, site }, - ) - return undefined - }), - ) - map.set("$HostSearch", new VmIntrinsic("$HostSearch", ([args]) => vm.hosts.blockSearch(json(args)) as never)) -} diff --git a/packages/codemode/src/index.ts b/packages/codemode/src/index.ts index 9ed61ecf60ab..c5c8e8b0c1b6 100644 --- a/packages/codemode/src/index.ts +++ b/packages/codemode/src/index.ts @@ -1,8 +1,8 @@ -export * as CodeMode from "./codemode.js" -export * as Extension from "./extension.js" -export * as Namespace from "./namespace.js" -export * as Tool from "./tool.js" -export * as OpenAPI from "./openapi/index.js" +export * as CodeMode from "./codemode/codemode.js" +export * as Extension from "./codemode/extension.js" +export * as Namespace from "./codemode/namespace.js" +export * as Tool from "./codemode/tool.js" +export * as OpenAPI from "./codemode/openapi/index.js" export { fromBytes, searchSignature, @@ -19,5 +19,5 @@ export { type SnapshotHeader, type Suspended, type SuspendOptions, -} from "./codemode.js" -export { ToolError, toolError } from "./tool-error.js" +} from "./codemode/codemode.js" +export { ToolError, toolError } from "./codemode/tool-error.js" diff --git a/packages/codemode/src/async.ts b/packages/codemode/src/language/async.ts similarity index 99% rename from packages/codemode/src/async.ts rename to packages/codemode/src/language/async.ts index 3eae192d5bea..9e3576ecee1f 100644 --- a/packages/codemode/src/async.ts +++ b/packages/codemode/src/language/async.ts @@ -1,4 +1,4 @@ -import type { Diagnostic } from "./codemode.js" +import type { Diagnostic } from "./runtime/model.js" import { normalizeError, locate } from "./runtime/errors.js" import { MAX_PENDING_PROMISES } from "./runtime/limits.js" import { type AstNode, IteratorSymbol, rangeError, typeError } from "./runtime/model.js" diff --git a/packages/codemode/src/code.ts b/packages/codemode/src/language/code.ts similarity index 100% rename from packages/codemode/src/code.ts rename to packages/codemode/src/language/code.ts diff --git a/packages/codemode/src/compiler/compile.ts b/packages/codemode/src/language/compiler/compile.ts similarity index 100% rename from packages/codemode/src/compiler/compile.ts rename to packages/codemode/src/language/compiler/compile.ts diff --git a/packages/codemode/src/compiler/scope.ts b/packages/codemode/src/language/compiler/scope.ts similarity index 100% rename from packages/codemode/src/compiler/scope.ts rename to packages/codemode/src/language/compiler/scope.ts diff --git a/packages/codemode/src/data.ts b/packages/codemode/src/language/data.ts similarity index 100% rename from packages/codemode/src/data.ts rename to packages/codemode/src/language/data.ts diff --git a/packages/codemode/src/generators.ts b/packages/codemode/src/language/generators.ts similarity index 100% rename from packages/codemode/src/generators.ts rename to packages/codemode/src/language/generators.ts diff --git a/packages/codemode/src/language/host-function.ts b/packages/codemode/src/language/host-function.ts new file mode 100644 index 000000000000..78547ac8368c --- /dev/null +++ b/packages/codemode/src/language/host-function.ts @@ -0,0 +1,154 @@ +export * as Host from "./host-function.js" + +import type { Effect } from "effect" +import type { Json } from "./data.js" +import { HostFailure } from "./runtime/model.js" + +// Host functions a program calls as globals. Arguments cross into the host as copies (`Date`, `Map`, `Set`, +// `Uint8Array`, `URL`, `Headers`, errors, plain data), and results cross back the same way; a function inside a +// result is callable too. A host function may return a value, a Promise, or an Effect, and an Effect's requirements +// join the runtime's environment. + +/** What every call of a host function gets. */ +export type Call = { + /** The call's key, `:`: the same across a suspended run's resumes. */ + readonly key: string + /** Whether the run is being suspended: an interruption now detaches the call instead of ending it. */ + readonly suspending: () => boolean +} + +/** What every call of a durable function gets: also the arguments it was called with, as they crossed. */ +export type DurableCall = Call & { readonly args: ReadonlyArray } + +/** + * Lets a saved run keep the functions inside values a host function returned: `encode` gets such a value and gives + * plain data (JSON values and `Uint8Array`s); `decode` gets that data back on resume and gives a value of the same + * shape, whose functions the program's copies call from then on. Without it, those functions are lost on resume. + */ +export type Snapshot = { + readonly encode: (value: unknown) => unknown + readonly decode: (data: unknown) => unknown +} + +/** + * How a call reaches the program: + * - `sync`: the program waits for the function. An Effect runs until it ends; whatever is still pending then (a + * returned Promise, or a Promise or Effect the Effect produced) reaches the program as a promise. + * - `async`: the program gets a promise at once, and the function runs once the program's current step ends. + * - `sync-thenable`: as `sync`, and an object result also has hidden `then`, `catch` and `finally`, so the program can + * use it directly or await it. + */ +export type Type = "sync" | "async" | "sync-thenable" + +export type Options = + | { + readonly type?: "sync" | "sync-thenable" + readonly idempotent?: false + readonly snapshot?: Snapshot | undefined + /** + * Lets a program call the function with `new`, as it would a class. `new` calls it the same way a plain call + * does, and the program gets what it returns. + */ + readonly constructible?: boolean + /** + * Lets the program pass back a function that was inside a result, on its own or inside an argument: it arrives + * as the host function itself, and the run can't be suspended until the call completes. Without it, such a + * function is treated like any other function in an argument (see `Runtime.Translate`). + */ + readonly takesFunctions?: boolean + } + | { + readonly type: "async" + /** + * Safe to run twice: a suspend interrupts a call in progress, and the resumed run calls it again with the same + * key. Without it, a suspend fails while a call is in progress. + */ + readonly idempotent?: boolean + readonly snapshot?: Snapshot | undefined + readonly constructible?: boolean + readonly takesFunctions?: boolean + } + +/** A host function; see `fn`. */ +export class Fn { + declare readonly requirements?: (_: never) => R + constructor( + readonly run: (args: ReadonlyArray, call: Call) => unknown, + readonly type: Type, + readonly idempotent: boolean, + readonly snapshot: Snapshot | undefined, + readonly constructible = false, + readonly takesFunctions = false, + ) {} +} + +/** A host function whose work keeps running across a suspend; see `durableFn`. */ +export class DurableFn { + declare readonly requirements?: (_: never) => R + constructor( + readonly start: (args: ReadonlyArray, call: Call) => Effect.Effect, + readonly wait: (data: Json, call: DurableCall) => Effect.Effect, + readonly cancel: (data: Json, call: DurableCall) => Effect.Effect, + ) {} +} + +/** A lazy object of host functions, addressed by path; see `namespace`. */ +export class Namespace { + declare readonly requirements?: (_: never) => R + constructor( + readonly keys: (path: ReadonlyArray) => ReadonlyArray, + readonly resolve: (path: ReadonlyArray) => Fn | DurableFn | HostFailure, + ) {} +} + +export { HostFailure as Failure } + +type Requirements = A extends Effect.Effect ? R : never + +/** A host function: `f` gets the program's arguments, as they crossed, and the call. */ +export const fn = , call: Call) => unknown>( + f: F, + options: Options = {}, +): Fn>> => + new Fn( + f, + options.type ?? "sync", + options.type === "async" && options.idempotent === true, + options.snapshot, + options.constructible === true, + options.takesFunctions === true, + ) + +/** + * A host function whose work keeps running across a suspend. `start` begins it and gives JSON data, kept in a saved + * run; `wait(data)` runs right after, and again after a resume, for the result. `cancel(data)` runs only when the run + * is interrupted, or ends with the work still running. The program always gets a promise. + */ +export const durableFn = (options: { + readonly start: (args: ReadonlyArray, call: Call) => Effect.Effect + readonly wait: (data: Data, call: DurableCall) => Effect.Effect + readonly cancel: (data: Data, call: DurableCall) => Effect.Effect +}): DurableFn => + new DurableFn( + options.start, + options.wait as (data: Json, call: DurableCall) => Effect.Effect, + options.cancel as (data: Json, call: DurableCall) => Effect.Effect, + ) + +/** + * A lazy object of host functions, for catalogs too large to build as objects. `keys(path)` lists the names under a + * path, for `Object.keys` and `for...in`; `resolve(path)` gives the function a call reaches, or a failure (see + * `missing`) the call throws. Any path can be read, so `ns.a.b` is a path whether or not anything is there. + */ +export const namespace = (options: { + readonly keys: (path: ReadonlyArray) => ReadonlyArray + readonly resolve: (path: ReadonlyArray) => Fn | DurableFn | HostFailure +}): Namespace => new Namespace(options.keys, options.resolve) + +/** A failure with its own diagnostic `kind` and `suggestions`, to throw or fail with from a host function. */ +export const fail = (kind: string, message: string, suggestions?: ReadonlyArray): HostFailure => + new HostFailure(kind, message, suggestions) + +/** What `resolve` gives for a path with no function: calling it throws this failure. */ +export const missing = (message: string, suggestions?: ReadonlyArray, kind = "ExecutionFailure"): HostFailure => + new HostFailure(kind, message, suggestions) diff --git a/packages/codemode/src/language/host.ts b/packages/codemode/src/language/host.ts new file mode 100644 index 000000000000..7f0dc56eeef9 --- /dev/null +++ b/packages/codemode/src/language/host.ts @@ -0,0 +1,239 @@ +import type { Code } from "./code.js" +import { DurableFn, Fn } from "./host-function.js" +import type { Interpreter } from "./runtime/interpreter.js" +import type { AstNode, Diagnostic } from "./runtime/model.js" +import { internalCode } from "./internal-prelude.js" +import { Arr, type PromiseObj, type Value } from "./runtime/objects.js" +import { promiseLike } from "./runtime/promises.js" +import type { Completion } from "./runtime/signals.js" +import { HostCall, type HostPolicy, Resolving } from "./scheduler.js" +import { type FrameMode, Mode } from "./vm.js" + +// The VM's side of host work (plan §4.10): calls to host functions, the host calls they queue, and the blocking call +// a job may wait on. + +/** What host calls need from the promise scheduler. */ +export type Promises = { + newPromise(): PromiseObj + resolving(promise: PromiseObj, site?: AstNode): Resolving + /** Unhandled rejections, in promise creation order. */ + diagnostics(): Array +} + +/** A native's answer asking the VM to stop the job until the blocking host call it queued completes. */ +export const BLOCK: unique symbol = Symbol("vm.block") +/** A host operation pushed an internal frame that produces its result. */ +export const ENTERED: unique symbol = Symbol("vm.entered") + +export type HostResult = Value | typeof BLOCK | typeof ENTERED + +/** What the VM gives host operations: its runtime, and a way to run an internal prelude function at the call. */ +export type HostVm = { + readonly ctx: Interpreter + /** + * Runs an internal prelude function at the call `origin`; its result is the call's. In another `mode` than `CALLER`, + * it settles `target`, and the call's result is `answer`: in `REJECT_ONLY` mode, a failure in it rejects `target`. + */ + pushInternal( + code: Code, + args: ReadonlyArray, + origin: AstNode | undefined, + mode?: FrameMode, + target?: unknown, + answer?: Value, + ): unknown +} + +/** Where a host function is, and how its calls are named and saved. */ +export type HostTarget = { + /** How the program names it in errors: `fetch`, `fs.readFile`, `api.issues.list`. */ + readonly label: string + /** Where it is among the run's host globals, so a resumed run can find it again; none inside a result. */ + readonly path: ReadonlyArray | undefined + /** The host function whose `snapshot` hooks save the values this call returns, by label; see `Host.Snapshot`. */ + readonly owner: string | undefined +} + +type Delivery = { readonly call: HostCall; readonly completion: Completion } + +/** Visited nodes a native fast path may spend before it hands the work to the prelude walk, which slices. */ +export const VISIT_BUDGET = 2048 + +/** + * The host calls of one run: queued (written ahead, for the driver to start at the next stop), running, completed and + * waiting in the inbox, and the blocking call the current job waits on. Ids count up in program order, and every call + * of a host function takes one, so its key is `:`. + */ +export class HostCalls { + /** Names this run's host calls (`:`). */ + runId = "" + queued: Array = [] + readonly running = new Map() + readonly inbox: Array = [] + nextID = 0 + /** The blocking call the job on the stack waits on. */ + blocked: HostCall | undefined = undefined + /** A completed blocking call's outcome, which continues the job on the stack before anything else runs. */ + input: { readonly call: HostCall; readonly completion: Completion } | undefined = undefined + + constructor( + private readonly ctx: Interpreter, + readonly promises: Promises, + ) {} + + /** + * Calls a host function from the program, which reached it as `callee`. Its arguments cross to the host now. An + * `async` or durable function's call is queued and the program gets its promise; a failure crossing rejects it. Any + * other is called now: its value crosses back at once, a Promise it returned is a promise, and an Effect blocks the + * job until it completes. When the runtime awaits promises in arguments and one hasn't fulfilled, the call waits for + * them (see `wait`) first. + */ + call( + fn: Fn | DurableFn, + values: Array, + site: AstNode | undefined, + target: HostTarget, + callee: Value, + ): HostResult { + const hosts = this.ctx.hosts! + const waiting: Array = [] + // The host functions that cross back among the arguments, when the function takes them. + const back: Array | undefined = fn instanceof Fn && fn.takesFunctions ? [] : undefined + if (fn instanceof DurableFn || fn.type === "async") { + const resolving = this.promises.resolving(this.promises.newPromise(), site) + let args: ReadonlyArray + try { + args = hosts.toHost(values, target.label, waiting, back) + } catch (error) { + resolving.reject(error) + return resolving.promise + } + if (waiting.length > 0) return this.wait(waiting, callee, values, site, resolving) + const policy = fn instanceof DurableFn ? "durable" : fn.idempotent ? "idempotent" : "async" + const call = this.queue(target, policy, args, false, resolving, site) + call.fn = fn + call.holdsFunctions = back !== undefined && back.length > 0 + return resolving.promise + } + const args = hosts.toHost(values, target.label, waiting, back) + if (waiting.length > 0) return this.wait(waiting, callee, values, site) + const id = this.nextID++ + const result = hosts.invoke(fn, args, `${this.runId}:${id}`, target.label) + if (result.kind === "value") { + const value = hosts.fromHost(result.value, target.label, target.owner) + return fn.type === "sync-thenable" ? promiseLike(this.ctx, value) : value + } + if (result.kind === "pending") return this.pending(target, result.work, site, id) + const call = new HostCall(id, `${this.runId}:${id}`, target, fn.type, [], true, undefined, site) + call.work = result.work + this.queued.push(call) + this.blocked = call + return BLOCK + } + + // Waits in the program for the promises, then calls `callee` again with the same arguments, which cross with their + // values now. A sync function's call waits where it is, as an `await` would; an async one's settles `resolving`. + private wait( + promises: Array, + callee: Value, + values: Array, + site: AstNode | undefined, + resolving?: Resolving, + ): HostResult { + const builtins = this.ctx.builtins + this.ctx.vm.pushInternal( + internalCode("$vmHostWait"), + [new Arr(builtins.Array, promises), callee, new Arr(builtins.Array, values)], + site, + resolving === undefined ? Mode.CALLER : Mode.ASYNC, + resolving, + resolving?.promise, + ) + return ENTERED + } + + // What a host function left pending (a Promise, or an Effect): the program holds a promise for it. + private pending(target: HostTarget, work: unknown, site: AstNode | undefined, id = this.nextID++): PromiseObj { + const resolving = this.promises.resolving(this.promises.newPromise(), site) + const call = new HostCall(id, `${this.runId}:${id}`, target, "sync", [], false, resolving, site) + call.work = work + this.queued.push(call) + return resolving.promise + } + + private queue( + target: HostTarget, + policy: HostPolicy, + args: ReadonlyArray, + blocking: boolean, + resolving: Resolving | undefined, + site: AstNode | undefined, + ): HostCall { + const id = this.nextID++ + const call = new HostCall(id, `${this.runId}:${id}`, target, policy, args, blocking, resolving, site) + this.queued.push(call) + return call + } + + /** Continues the job stopped at a blocking call with its outcome. */ + resume(input: { readonly call: HostCall; readonly completion: Completion }): HostResult { + const call = input.call + const completion = input.completion + const hosts = this.ctx.hosts! + if (!completion.ok) throw hosts.failure(completion.error, call.label) + if (hosts.isPending(completion.value)) return this.pending(call, completion.value, call.site) + const value = hosts.fromHost(completion.value, call.label, call.owner) + return call.policy === "sync-thenable" ? promiseLike(this.ctx, value) : value + } + + /** Hands the queued calls to the driver: they are running from now on. */ + handOver(): Array { + const calls = this.queued + this.queued = [] + for (const call of calls) this.running.set(call.id, call) + return calls + } + + complete(id: number, completion: Completion): void { + const call = this.running.get(id) + if (call === undefined) return + this.running.delete(id) + // Its outcome is all that is left: the host function, its arguments, and what it returned are dropped. + call.data = undefined + call.fn = undefined + call.work = undefined + call.args = [] + if (call.blocking) { + call.status = "completed" + if (this.blocked !== call) return + this.blocked = undefined + this.input = { call, completion } + return + } + this.inbox.push({ call, completion }) + } + + /** Settles the oldest completed concurrent call's promise; false when the inbox is empty. */ + deliver(): boolean { + const delivery = this.inbox.shift() + if (delivery === undefined) return false + const call = delivery.call + const completion = delivery.completion + call.status = "completed" + const resolving = call.resolving! + const hosts = this.ctx.hosts! + if (!completion.ok) { + resolving.reject(hosts.failure(completion.error, call.label)) + return true + } + let value: Value + try { + value = hosts.fromHost(completion.value, call.label, call.owner) + } catch (error) { + resolving.reject(error) + return true + } + resolving.resolve(value) + return true + } +} diff --git a/packages/codemode/src/language/index.ts b/packages/codemode/src/language/index.ts new file mode 100644 index 000000000000..45336a00dcaf --- /dev/null +++ b/packages/codemode/src/language/index.ts @@ -0,0 +1,12 @@ +export * as Language from "./index.js" + +// The language layer: the compiler, VM, runtime, standard library, snapshots, and the Effect driver. It knows nothing +// about tools; a host gives programs its functions through `Host` and runs them with `Runtime.make`. +export { Host } from "./host-function.js" +export { Runtime } from "./runtime.js" +export { StreamingResponse } from "./streaming-response.js" +export { fromBytes, SnapshotError, toBytes, type Snapshot, type SnapshotHeader } from "./snapshot/snapshot.js" +export { SuspendError } from "./runtime/execute.js" +export { globalNames } from "./runtime/globals.js" +export type { Diagnostic, DiagnosticKind } from "./runtime/model.js" +export type { Json } from "./data.js" diff --git a/packages/codemode/src/internal-prelude.ts b/packages/codemode/src/language/internal-prelude.ts similarity index 96% rename from packages/codemode/src/internal-prelude.ts rename to packages/codemode/src/language/internal-prelude.ts index 305fc669d951..f60fee452866 100644 --- a/packages/codemode/src/internal-prelude.ts +++ b/packages/codemode/src/language/internal-prelude.ts @@ -132,7 +132,7 @@ function $vmJsonVisitor(boundary, replacer, properties) { let val = holder[key] if ($IsPromise(val)) { if (!boundary) { - $Throw("InvalidData", "JSON.stringify received an un-awaited Promise; await it first - e.g. \x60const result = await tools.ns.tool(...)\x60.") + $Throw("InvalidData", "JSON.stringify received an un-awaited Promise; await it first - e.g. \x60const result = await load(...)\x60.") } val = yield val } @@ -223,21 +223,22 @@ function $vmAsyncFromSyncClose(iter) { if (ret !== undefined && ret !== null) $Call(ret, iter) } -async function $vmCross(roots, path, resolving) { - const crossed = [] - let r = 0 - while (r < roots.length) { - let root = roots[r] - if (path === undefined && $IsThenable(root)) root = await root - const walk = $vmJsonVisitor(true, undefined, null)({ "": root }, "", 0) - let step = walk.next() - while (!step.done) step = walk.next(await step.value) - crossed[r] = $JsonFinish(true, step.value, null, undefined) - r++ +// A host call whose arguments hold promises that haven't fulfilled: waits for each, then calls again. +async function $vmHostWait(promises, callee, args) { + let i = 0 + while (i < promises.length) { + await promises[i] + i++ } - if (path === undefined) return crossed[0] - if (path === "search") return $HostSearch(crossed) - $HostTool(path, crossed, resolving) + return $Apply(callee, undefined, args) +} + +async function $vmResult(value) { + const root = $IsThenable(value) ? await value : value + const walk = $vmJsonVisitor(true, undefined, null)({ "": root }, "", 0) + let step = walk.next() + while (!step.done) step = walk.next(await step.value) + return $JsonFinish(true, step.value, null, undefined) } ` diff --git a/packages/codemode/src/opcodes.ts b/packages/codemode/src/language/opcodes.ts similarity index 99% rename from packages/codemode/src/opcodes.ts rename to packages/codemode/src/language/opcodes.ts index d81ef7a6f9fc..0b49a44944d6 100644 --- a/packages/codemode/src/opcodes.ts +++ b/packages/codemode/src/language/opcodes.ts @@ -1,4 +1,4 @@ -import type { DiagnosticKind } from "./codemode.js" +import type { DiagnosticKind } from "./runtime/model.js" import type { ErrorType } from "./runtime/intrinsics.js" /** Uninitialized lexical binding sentinel (temporal dead zone). */ diff --git a/packages/codemode/src/prelude/bytecode.json b/packages/codemode/src/language/prelude/bytecode.json similarity index 89% rename from packages/codemode/src/prelude/bytecode.json rename to packages/codemode/src/language/prelude/bytecode.json index 086ea5d6e5c6..1bfeb23f5d3f 100644 --- a/packages/codemode/src/prelude/bytecode.json +++ b/packages/codemode/src/language/prelude/bytecode.json @@ -1 +1 @@ -{"@prelude":{"text":"f135511dc7fc0839","codes":[{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":1,"name":"arrayFromIterable","flags":72,"length":3,"params":3,"locals":8,"maxStack":6,"code":[36,196875,3,262411,327692,10,2293834,2293819,393483,3212876,2490427,327692,327947,196618,262154,31,32,543,65546,0,50,1572925,327690,1900603,131082,65546,327690,262154,582,28,5,262154,56,262155,589884,70,75,524347,70,3212877,720955,459019,2950734,70,5,458762,64,5,2949179,196618,67],"consts":[],"captures":[],"handlers":[11,35,41,0,42,45,47,0],"finallies":[],"finallyOffsets":[],"sites":[0,10,1,8,2,14,3,12,5,19,6,15,12,16,13,24,14,25,15,23,16,25,17,23,18,28,19,29,20,27,21,26,22,30,23,26,24,34,25,33,26,35,27,36,28,31,29,23,30,22,31,39,32,38,33,39,34,15,49,41,50,40],"localNames":["items","mapper","thisArg","result","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":2,"name":"arrayFromLike","flags":72,"length":4,"params":4,"locals":6,"maxStack":6,"code":[20,65352,65546,328,262411,3,327947,327690,65546,46,1900605,262154,327690,31,32,543,196618,131082,10,327690,26,327690,582,28,5,327690,56,327691,458812,262154,67],"consts":["Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,53,1,52,2,54,3,52,4,50,5,58,6,56,7,61,8,62,9,60,10,59,11,67,12,68,13,66,14,68,15,66,16,72,17,71,18,74,19,75,20,73,21,76,22,69,23,66,24,65,25,79,26,78,27,79,28,59,29,81,30,80],"localNames":["source","length","mapper","thisArg","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":3,"name":"Array$prototype$map","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["mapItems","$Receiver","Array","Array.prototype.map","Array.map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,82,2,84,4,85,6,87,7,85,8,90,9,91,10,92,11,93,12,94,13,95,14,96,15,92,16,97,17,98,18,99,19,90,20,89],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":4,"name":"Uint8Array$prototype$map","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.map","$Bytes","mapItems","Uint8Array.map","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,100,2,102,4,103,6,105,7,103,8,110,9,111,10,112,11,113,12,114,13,110,14,108,15,116,16,117,17,118,18,119,19,120,20,121,21,122,22,123,23,118,24,125,25,124,26,127,27,116,28,115],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":5,"name":"mapItems","flags":72,"length":4,"params":4,"locals":7,"maxStack":7,"code":[0,20,65546,196618,582,5,10,65561,262411,131092,65352,262154,328,327947,3,393483,393226,262154,46,2818109,393226,10,33,2555965,327690,393226,31,32,543,131082,65546,10,393226,26,393226,10,838,28,5,393226,56,393227,1048636,327690,67],"consts":["$Callback","length","Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,136,1,137,2,138,3,139,4,136,6,144,7,143,8,141,9,150,10,149,11,151,12,149,13,147,14,155,15,153,16,158,17,159,18,157,19,156,20,163,21,164,22,162,23,161,24,168,25,169,26,167,27,169,28,167,29,173,30,172,31,175,32,176,33,174,34,177,35,178,36,170,37,167,38,166,39,181,40,180,41,181,42,156,43,183,44,182],"localNames":["array","callback","thisArg","label","length","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":6,"name":"Array$prototype$filter","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["filterItems","$Receiver","Array","Array.prototype.filter","Array.filter"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,184,2,186,4,187,6,189,7,187,8,192,9,193,10,194,11,195,12,196,13,197,14,198,15,194,16,199,17,200,18,201,19,192,20,191],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":7,"name":"Uint8Array$prototype$filter","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.filter","$Bytes","filterItems","Uint8Array.filter","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,202,2,204,4,205,6,207,7,205,8,212,9,213,10,214,11,215,12,216,13,212,14,210,15,218,16,219,17,220,18,221,19,222,20,223,21,224,22,225,23,220,24,227,25,226,26,229,27,218,28,217],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":8,"name":"filterItems","flags":72,"length":4,"params":4,"locals":9,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,36,327947,3,393483,3,459019,0,524555,458762,262154,46,3407933,458762,10,33,3145789,10,458762,26,524299,131082,65546,524298,458762,10,838,3145789,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,1114172,327690,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,238,1,239,2,240,3,241,4,238,6,246,7,245,8,243,9,251,10,249,11,255,12,253,13,259,14,257,15,261,17,265,18,266,19,264,20,263,21,270,22,271,23,269,24,268,25,277,26,278,27,276,28,275,29,283,30,282,31,284,32,285,33,286,34,280,35,279,36,290,37,292,39,291,41,292,42,289,43,291,44,289,45,293,46,289,47,288,48,296,49,295,50,296,51,263,52,298,53,297],"localNames":["array","callback","thisArg","label","length","result","count","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":9,"name":"Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Array","Array.prototype.forEach","Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,299,2,301,4,302,6,304,7,302,8,307,9,308,10,309,11,310,12,311,13,312,14,313,15,309,16,314,17,315,18,316,19,307,21,305],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":10,"name":"Uint8Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Uint8Array","Uint8Array.prototype.forEach","Uint8Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,317,2,319,4,320,6,322,7,320,8,325,9,326,10,327,11,328,12,329,13,330,14,331,15,327,16,332,17,333,18,334,19,325,21,323],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":11,"name":"forEachItem","flags":72,"length":4,"params":4,"locals":6,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,3,327947,327690,262154,46,2097213,327690,10,33,1835069,131082,65546,10,327690,26,327690,10,838,5,327690,56,327691,720956,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,343,1,344,2,345,3,346,4,343,6,351,7,350,8,348,9,356,10,354,11,359,12,360,13,358,14,357,15,364,16,365,17,363,18,362,19,370,20,369,21,372,22,373,23,371,24,374,25,375,26,367,28,378,29,377,30,378,31,357,32,341],"localNames":["array","callback","thisArg","label","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":12,"name":"Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.some","Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,379,2,381,4,382,6,384,7,382,8,387,9,388,10,389,11,390,12,391,13,392,14,393,15,389,16,394,17,395,18,396,19,397,20,387,21,386],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":13,"name":"Uint8Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.some","Uint8Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,398,2,400,4,401,6,403,7,401,8,406,9,407,10,408,11,409,12,410,13,411,14,412,15,408,16,413,17,414,18,415,19,416,20,406,21,405],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":14,"name":"Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.every","Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,417,2,419,4,420,6,422,7,420,8,425,9,426,10,427,11,428,12,429,13,430,14,431,15,427,16,432,17,433,18,434,19,435,20,425,21,424],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":15,"name":"Uint8Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.every","Uint8Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,436,2,438,4,439,6,441,7,439,8,444,9,445,10,446,11,447,12,448,13,449,14,450,15,446,16,451,17,452,18,453,19,454,20,444,21,443],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":16,"name":"testItems","flags":72,"length":5,"params":5,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,327947,3,393483,393226,327690,46,2490429,393226,10,33,1966399,131082,65546,10,393226,26,393226,10,838,54,262154,50,2228285,262154,54,67,393226,56,393227,720956,262154,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,464,1,465,2,466,3,467,4,464,6,472,7,471,8,469,9,477,10,475,11,480,12,481,13,479,14,478,15,486,16,487,17,485,18,484,19,493,20,492,21,495,22,496,23,494,24,497,25,498,26,490,27,489,28,499,29,488,30,483,31,502,32,501,33,500,34,505,35,504,36,505,37,478,38,507,39,506],"localNames":["array","callback","thisArg","label","every","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":17,"name":"Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.find","Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,508,2,510,4,511,6,513,7,511,8,516,9,517,10,518,11,519,12,520,13,521,14,522,15,518,16,523,17,524,18,525,19,526,20,527,21,516,22,515],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":18,"name":"Uint8Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.find","Uint8Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,528,2,530,4,531,6,533,7,531,8,536,9,537,10,538,11,539,12,540,13,541,14,542,15,538,16,543,17,544,18,545,19,546,20,547,21,536,22,535],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":19,"name":"Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findIndex","Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,548,2,550,4,551,6,553,7,551,8,556,9,557,10,558,11,559,12,560,13,561,14,562,15,558,16,563,17,564,18,565,19,566,20,567,21,556,22,555],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":20,"name":"Uint8Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findIndex","Uint8Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,568,2,570,4,571,6,573,7,571,8,576,9,577,10,578,11,579,12,580,13,581,14,582,15,578,16,583,17,584,18,585,19,586,20,587,21,576,22,575],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":21,"name":"Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLast","Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,588,2,590,4,591,6,593,7,591,8,596,9,597,10,598,11,599,12,600,13,601,14,602,15,598,16,603,17,604,18,605,19,606,20,607,21,596,22,595],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":22,"name":"Uint8Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLast","Uint8Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,608,2,610,4,611,6,613,7,611,8,616,9,617,10,618,11,619,12,620,13,621,14,622,15,618,16,623,17,624,18,625,19,626,20,627,21,616,22,615],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":23,"name":"Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLastIndex","Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,628,2,630,4,631,6,633,7,631,8,636,9,637,10,638,11,639,12,640,13,641,14,642,15,638,16,643,17,644,18,645,19,646,20,647,21,636,22,635],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":24,"name":"Uint8Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLastIndex","Uint8Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,648,2,650,4,651,6,653,7,651,8,656,9,657,10,658,11,659,12,660,13,661,14,662,15,658,16,663,17,664,18,665,19,666,20,667,21,656,22,655],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":25,"name":"findItem","flags":72,"length":6,"params":6,"locals":10,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,393483,262154,917565,65539,53,983099,65539,459019,262154,1441853,393226,65539,44,1507387,3,524555,0,590091,524298,3,49,2163007,524298,393226,46,3670077,10,524298,26,589835,131082,65546,589834,524298,10,838,3342397,327690,3211325,524298,3276859,589834,67,524298,458762,43,524299,1703996,327690,3997757,65539,53,4063291,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,678,1,679,2,680,3,681,4,678,6,686,7,685,8,683,9,692,10,691,11,694,12,693,13,691,14,695,15,689,16,700,17,699,18,702,19,703,20,701,21,699,22,704,23,697,24,706,26,711,27,712,28,710,29,709,30,714,31,715,32,713,33,708,34,721,35,722,36,720,37,719,38,727,39,726,40,728,41,729,42,730,43,724,44,723,45,733,46,732,47,734,48,732,49,735,50,731,51,738,52,739,53,737,54,738,55,708,56,742,57,741,58,744,59,743,60,741,61,745,62,740],"localNames":["array","callback","thisArg","label","last","wantIndex","length","step","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":26,"name":"Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduce","Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,746,2,748,4,749,6,753,7,754,8,755,9,756,10,757,11,758,12,759,13,755,14,760,15,761,16,762,17,763,18,753,19,752],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":27,"name":"Uint8Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduce","Uint8Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,764,2,766,4,767,6,771,7,772,8,773,9,774,10,775,11,776,12,777,13,773,14,778,15,779,16,780,17,781,18,771,19,770],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":28,"name":"Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduceRight","Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,782,2,784,4,785,6,789,7,790,8,791,9,792,10,793,11,794,12,795,13,791,14,796,15,797,16,798,17,799,18,789,19,788],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":29,"name":"Uint8Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduceRight","Uint8Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,800,2,802,4,803,6,807,7,808,8,809,9,810,10,811,11,812,12,813,13,809,14,814,15,815,16,816,17,817,18,807,19,806],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":30,"name":"reduceItems","flags":72,"length":5,"params":5,"locals":9,"maxStack":6,"code":[0,20,65546,196618,582,5,10,65561,327947,262154,917565,65539,53,983099,65539,393483,262154,1441853,327690,65539,44,1507387,3,459019,0,524555,131082,65561,3,48,2359357,131082,3,26,524299,5111867,458762,3,49,2818367,458762,327690,46,3146047,458762,10,33,54,3539005,458762,393226,43,458763,2359356,458762,3,46,3997759,458762,327690,49,4587581,0,131092,196612,196618,262148,43,582,5,10,458762,26,524299,458762,393226,43,458763,458762,3,49,5570879,458762,327690,46,6881341,458762,10,33,6553661,0,65546,524298,10,458762,26,458762,10,1094,524299,458762,393226,43,458763,5111868,524298,67],"consts":["$Callback","length","$Throw","TypeError"," of an empty array with no initial value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,827,1,828,2,829,3,830,4,827,6,835,7,834,8,832,9,841,10,840,11,843,12,842,13,840,14,844,15,838,16,849,17,848,18,851,19,852,20,850,21,848,22,853,23,846,24,855,26,860,27,859,28,862,29,858,30,857,31,867,32,868,33,866,34,865,35,857,36,874,37,875,38,873,39,872,40,877,41,878,42,876,43,871,44,881,45,882,46,880,47,879,48,870,49,885,50,886,51,884,52,885,53,870,54,890,55,891,56,889,57,888,58,893,59,894,60,892,61,887,62,896,63,897,64,898,65,900,66,901,67,899,68,896,70,906,71,907,72,905,73,904,74,910,75,911,76,909,77,910,78,915,79,916,80,914,81,913,82,918,83,919,84,917,85,912,86,923,87,924,88,922,89,921,90,931,91,930,92,932,93,934,94,935,95,933,96,936,97,937,98,928,99,927,100,940,101,941,102,939,103,940,104,912,105,943,106,942],"localNames":["array","callback","initial","label","right","length","step","index","accumulator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":31,"name":"Array$prototype$flatMap","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,65546,262148,582,5,196618,327705,262411,36,327947,3,393483,3,459019,0,524555,0,590091,458762,262154,46,6684733,458762,196618,33,6422589,131082,65546,196618,458762,26,458762,196618,838,524299,0,393236,524298,326,5636157,3,589835,589834,524298,327705,46,5570621,589834,524298,33,5308477,327690,393226,55,7,56,393227,31,32,543,524298,589834,26,28,5,589834,56,589835,3801148,6422587,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,2228284,327690,67],"consts":["$Receiver","Array","Array.prototype.flatMap","$Callback","Array.flatMap","length","$IsArray"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,944,2,946,4,947,6,949,7,947,8,954,9,955,10,956,11,957,12,958,13,954,14,952,15,960,16,961,17,962,18,963,19,960,21,968,22,967,23,965,24,973,25,971,26,977,27,975,28,981,29,979,30,983,32,986,34,990,35,991,36,989,37,988,38,995,39,996,40,994,41,993,42,1004,43,1003,44,1006,45,1007,46,1005,47,1008,48,1009,49,1001,50,1000,51,1011,52,1012,53,1013,54,1011,55,1010,56,1018,57,1017,58,1021,59,1023,60,1022,61,1020,62,1019,63,1028,64,1029,65,1027,66,1026,67,1033,68,1035,70,1034,72,1035,73,1032,74,1034,75,1032,76,1037,77,1038,78,1036,79,1032,80,1031,81,1041,82,1040,83,1041,84,1019,85,1010,86,1045,87,1047,89,1046,91,1047,92,1044,93,1046,94,1044,95,1048,96,1044,97,1043,98,1051,99,1050,100,1051,101,988,102,1053,103,1052],"localNames":["this","callback","thisArg","array","length","result","count","index","inner","at"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":32,"name":"sortWith","flags":72,"length":7,"params":7,"locals":15,"maxStack":11,"code":[36,459019,3,524555,0,590091,0,655627,0,721163,0,786699,0,852235,0,917771,524298,393226,25,46,7733309,393226,524298,26,720907,393226,524298,65539,43,26,589835,393226,524298,131075,43,26,655371,589834,786443,3,851979,720906,589834,46,3146047,786442,655370,46,6488125,0,196618,262154,720906,26,262154,786442,26,582,309,917515,917514,3,47,4390975,917514,917514,51,5374013,458762,851978,31,32,543,262154,720906,26,28,5,720906,56,720907,6225979,458762,851978,31,32,543,262154,786442,26,28,5,786442,56,786443,851978,56,851979,2687036,0,65556,262154,458762,851978,720906,589834,786442,655370,393226,524298,26,2118,5,524298,196611,43,524299,1048636,0,131092,10,65546,131082,262154,327690,1350,67],"consts":["length","$MergeRest","$Sorted"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1067,1,1065,2,1071,3,1069,4,1073,6,1076,8,1079,10,1082,12,1085,14,1088,16,1092,17,1094,18,1093,19,1091,20,1090,21,1101,22,1102,23,1100,24,1099,25,1107,26,1109,27,1110,28,1108,29,1106,30,1105,31,1115,32,1117,33,1118,34,1116,35,1114,36,1113,37,1122,38,1121,39,1126,40,1125,41,1130,42,1131,43,1129,44,1128,45,1133,46,1134,47,1132,48,1127,49,1143,50,1142,51,1145,52,1146,53,1144,54,1148,55,1149,56,1147,57,1140,58,1139,59,1138,60,1153,61,1154,62,1152,63,1151,64,1156,65,1157,66,1155,67,1150,68,1162,69,1163,70,1161,71,1163,72,1161,73,1165,74,1166,75,1164,76,1161,77,1160,78,1169,79,1168,80,1169,81,1150,82,1174,83,1175,84,1173,85,1175,86,1173,87,1177,88,1178,89,1176,90,1173,91,1172,92,1181,93,1180,94,1181,95,1184,96,1183,97,1184,98,1127,99,1186,100,1187,101,1188,102,1189,103,1190,104,1191,105,1192,106,1193,107,1194,108,1196,109,1197,110,1195,111,1186,113,1200,114,1201,115,1199,116,1200,117,1090,118,1203,119,1204,120,1205,121,1206,122,1207,123,1208,124,1209,125,1203,126,1202],"localNames":["target","into","holes","comparator","work","undefs","merges","merged","merge","middle","high","left","right","count","order"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":33,"name":"sortDefault","flags":72,"length":5,"params":5,"locals":6,"maxStack":9,"code":[0,20,196618,326,327947,0,65556,327690,326,720957,327740,0,131092,10,65546,131082,0,196628,327690,196618,582,262154,1350,67],"consts":["$SortState","$SortStep","$Sorted","$SortResult"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1221,1,1222,2,1223,3,1221,4,1219,5,1225,6,1226,7,1227,8,1225,9,1224,11,1230,12,1231,13,1232,14,1233,15,1234,16,1235,17,1236,18,1237,19,1238,20,1235,21,1239,22,1230,23,1229],"localNames":["target","into","holes","values","undefs","state"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":34,"name":"Array$prototype$toLocaleString","flags":88,"length":0,"params":0,"locals":5,"maxStack":5,"code":[789,267,0,20,10,65540,131076,838,65803,196612,131339,3,196875,0,262411,196618,65546,262169,46,3473469,196618,3,48,1835069,131082,327684,43,131083,65546,196618,26,262155,262154,1,51,2556223,262154,0,51,3211325,131082,0,393236,262154,459033,70,326,43,131083,196618,56,196619,983100,131082,67],"consts":["$Receiver","Array","Array.prototype.toLocaleString","","length",",","String","toLocaleString"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1240,2,1246,3,1247,4,1248,5,1249,6,1250,7,1246,8,1244,9,1254,10,1252,11,1258,12,1256,13,1260,15,1264,16,1266,17,1265,18,1263,19,1262,20,1271,21,1272,22,1270,23,1269,24,1275,25,1276,26,1274,27,1275,28,1281,29,1282,30,1280,31,1279,32,1286,33,1287,34,1285,35,1284,36,1289,37,1290,38,1288,39,1283,40,1293,41,1294,42,1295,43,1298,44,1297,45,1296,46,1294,47,1292,48,1293,49,1302,50,1301,51,1302,52,1262,53,1304,54,1303],"localNames":["this","array","result","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":35,"name":"fillCollection","flags":72,"length":3,"params":3,"locals":7,"maxStack":7,"code":[131082,4,50,458815,131082,65540,50,196875,262156,65546,3801162,3801147,327947,4719948,3997755,262156,262411,196618,54,1835069,0,131092,196612,10,262154,838,5,3735611,0,262164,262154,326,54,2949181,0,327700,393220,458756,131082,43,524292,43,582,5,3735611,0,131092,196612,10,262154,3,26,262154,65539,26,1094,5,852028,70,75,786491,70,4719949,983099,393483,4457806,70,5,393226,64,5,4456507,10,67],"consts":["Map","WeakMap","$Table","add","$IsObject","$Throw","TypeError","new ","(...) expects [key, value] pairs as entry objects."],"captures":[],"handlers":[15,58,64,0,65,68,70,0],"finallies":[],"finallyOffsets":[],"sites":[0,1316,1,1317,2,1315,3,1314,4,1319,5,1320,6,1318,7,1312,9,1325,10,1321,16,1322,17,1329,18,1328,19,1327,20,1331,21,1332,22,1333,23,1334,24,1335,25,1331,27,1327,28,1338,29,1339,30,1340,31,1338,32,1337,33,1336,34,1342,35,1343,36,1344,37,1347,38,1348,39,1346,40,1349,41,1345,42,1342,44,1336,45,1351,46,1352,47,1353,48,1354,49,1356,50,1357,51,1355,52,1359,53,1360,54,1358,55,1351,57,1321,72,1362,73,1361],"localNames":["target","items","kind","pairs","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":36,"name":"Map$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1369,1,1370,2,1371,3,1372,4,1373,5,1369,6,1368],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":37,"name":"Object$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Object"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1380,1,1381,2,1382,3,1383,4,1384,5,1380,6,1379],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":38,"name":"groupBy","flags":72,"length":3,"params":3,"locals":9,"maxStack":6,"code":[10,1,50,458815,10,0,50,1048637,0,20,65540,131082,131076,43,582,5,0,196628,65546,131082,262148,43,582,5,0,327700,393220,131082,582,196875,3,262411,0,327947,393228,10,4915274,4915259,459019,5834572,5111867,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,458756,50,4325437,0,524308,0,327700,589828,327690,582,326,327691,0,327700,655364,196618,327690,393226,1094,5,2555964,70,75,2490427,70,5834573,2687035,524555,5572430,70,5,524298,64,5,5570619,196618,67],"consts":["$Throw","TypeError",".groupBy expects an iterable collection.","$Callback",".groupBy","$Table","groups","Object","String","groupKey","group"],"captures":[],"handlers":[41,75,81,0,82,85,87,0],"finallies":[],"finallyOffsets":[],"sites":[0,1394,1,1395,2,1393,3,1392,4,1397,5,1398,6,1396,7,1391,8,1400,9,1401,10,1402,11,1404,12,1405,13,1403,14,1400,16,1407,17,1408,18,1409,19,1411,20,1412,21,1410,22,1407,24,1416,25,1417,26,1418,27,1419,28,1416,29,1414,30,1423,31,1421,32,1425,35,1431,36,1427,42,1428,43,1436,44,1437,45,1438,46,1440,48,1439,50,1440,51,1436,52,1435,53,1443,54,1444,55,1442,56,1441,57,1448,58,1449,59,1450,60,1451,61,1452,62,1453,63,1450,64,1448,65,1447,66,1455,67,1456,68,1457,69,1458,70,1459,71,1460,72,1455,74,1427,89,1462,90,1461],"localNames":["items","callback","kind","groups","index","key","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":39,"name":"insertComputed","flags":72,"length":3,"params":3,"locals":4,"maxStack":6,"code":[0,131082,65546,326,196875,0,20,65540,10,65546,196618,1094,5,196618,67],"consts":["$Table","add"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1472,1,1473,2,1474,3,1472,4,1470,5,1476,6,1477,7,1478,8,1479,9,1480,10,1481,11,1476,13,1483,14,1482],"localNames":["target","key","callback","value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":40,"name":"Map$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Map","Map.prototype.forEach","Map.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1484,2,1486,4,1487,6,1489,7,1487,8,1492,9,1493,10,1494,11,1495,12,1496,13,1497,14,1498,15,1494,16,1499,17,1500,18,1501,19,1492,21,1490],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":41,"name":"Set$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Set","Set.prototype.forEach","Set.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1502,2,1504,4,1505,6,1507,7,1505,8,1510,9,1511,10,1512,11,1513,12,1514,13,1515,14,1516,15,1512,16,1517,17,1518,18,1519,19,1510,21,1508],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":42,"name":"collectionForEach","flags":72,"length":4,"params":4,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,262156,0,65556,131076,10,582,2031690,2031675,327947,2950476,2228283,262156,262411,131082,65546,262154,65539,26,262154,3,26,10,838,5,983100,70,75,917563,70,2950477,1114171,393483,2688334,70,5,393226,64,5,2687035,0,67],"consts":["$Callback","$Table","entries"],"captures":[],"handlers":[17,31,37,0,38,41,43,0],"finallies":[],"finallyOffsets":[],"sites":[0,1528,1,1529,2,1530,3,1531,4,1528,7,1536,8,1537,9,1538,10,1539,11,1536,12,1532,18,1533,19,1544,20,1543,21,1546,22,1547,23,1545,24,1549,25,1550,26,1548,27,1551,28,1541,30,1532,45,1526],"localNames":["target","callback","thisArg","label","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":43,"name":"Set$prototype$union","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.union","union"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1552,2,1557,3,1558,4,1559,5,1560,6,1561,7,1562,8,1563,9,1559,10,1564,11,1565,12,1557,13,1556],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":44,"name":"Set$prototype$intersection","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.intersection","intersection"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1566,2,1571,3,1572,4,1573,5,1574,6,1575,7,1576,8,1577,9,1573,10,1578,11,1579,12,1571,13,1570],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":45,"name":"Set$prototype$difference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.difference","difference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1580,2,1585,3,1586,4,1587,5,1588,6,1589,7,1590,8,1591,9,1587,10,1592,11,1593,12,1585,13,1584],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":46,"name":"Set$prototype$symmetricDifference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.symmetricDifference","symmetricDifference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1594,2,1599,3,1600,4,1601,5,1602,6,1603,7,1604,8,1605,9,1601,10,1606,11,1607,12,1599,13,1598],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":47,"name":"Set$prototype$isSubsetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSubsetOf","isSubsetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1608,2,1613,3,1614,4,1615,5,1616,6,1617,7,1618,8,1619,9,1615,10,1620,11,1621,12,1613,13,1612],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":48,"name":"Set$prototype$isSupersetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSupersetOf","isSupersetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1622,2,1627,3,1628,4,1629,5,1630,6,1631,7,1632,8,1633,9,1629,10,1634,11,1635,12,1627,13,1626],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":49,"name":"Set$prototype$isDisjointFrom","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isDisjointFrom","isDisjointFrom"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1636,2,1641,3,1642,4,1643,5,1644,6,1645,7,1646,8,1647,9,1643,10,1648,11,1649,12,1641,13,1640],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":50,"name":"setMethod","flags":72,"length":3,"params":3,"locals":18,"maxStack":5,"code":[0,20,65546,326,54,1048637,0,65556,131076,196612,131082,43,262148,43,582,5,65546,327705,309,196875,196618,196618,51,2228285,0,65556,131076,196612,131082,43,393220,43,582,5,196618,3,46,3145789,0,65556,458756,196612,131082,43,524292,43,582,5,65546,589849,262411,65546,655385,327947,0,720916,262154,326,54,4259903,0,720916,327690,326,54,4980797,0,65556,131076,196612,131082,43,786436,43,582,5,0,851988,327684,10,582,393483,131082,917508,50,5833023,393226,196618,48,6029373,2,67,131082,983044,50,6488383,393226,196618,46,6684733,2,67,131082,1048580,50,7209021,1114132,65352,72,8454203,131082,1179652,50,7667775,131082,1245188,50,7929919,131082,1310724,50,8388669,0,851988,1376260,10,582,8454203,0,459019,131082,917508,50,9764927,131082,1048580,50,9240639,131082,1245188,50,9502783,131082,1441796,50,9765183,393226,196618,47,524555,0,590091,524298,16056381,720908,0,851988,1507332,131082,1245188,50,10747965,458762,10813499,10,582,14614602,14614587,852235,15535436,14811195,720908,721163,65546,262154,720906,326,54,54,589835,131082,1048580,50,12714045,589834,12648509,0,851988,1572868,458762,720906,838,5,14549051,131082,1245188,50,13631549,589834,13566013,0,851988,1638404,458762,720906,838,5,14549051,589834,131082,1441796,50,50,14549053,2,983307,14421326,70,1611,5,983050,67,11075644,70,75,11010107,70,15535437,11206715,917771,15273294,70,5,917514,64,5,15269947,458762,0,50,15925309,258,15990843,458762,67,65546,327690,70,655627,0,20,655370,326,54,17367101,0,65556,131076,196612,131082,43,1703940,43,582,5,786444,0,1769492,655370,258,582,23199818,23199803,1048843,24121420,23396411,786444,786699,0,851988,589828,10,786442,838,589835,131082,1179652,50,19464253,0,851988,1572868,458762,786442,838,5,23134267,131082,1048580,50,20381757,589834,20316221,0,851988,1572868,458762,786442,838,5,23134267,131082,1245188,50,21168189,0,851988,1638404,458762,786442,838,5,23134267,131082,1310724,50,22216765,0,851988,589834,21823549,1638404,21889083,1572868,458762,786442,838,5,23134267,589834,131082,1441796,50,50,23134269,2,983307,23007310,70,1611,5,983050,67,17956924,70,75,17891387,70,24121421,18087995,1114379,23859278,70,5,1114122,64,5,23855163,458762,0,50,24510525,258,24576059,458762,67],"consts":["$IsObject","$Throw","TypeError","Set."," expects a Set-like object.","size"," received a Set-like object with an invalid size.","RangeError"," received a Set-like object with a negative size.","has","keys","$IsCallable"," expects callable 'has' and 'keys' methods.","$Table","isSubsetOf","isSupersetOf","intersection","Set","union","difference","symmetricDifference","copy","isDisjointFrom","values","add","delete"," expects 'keys' to return an iterator object.","$Direct"],"captures":[],"handlers":[171,223,229,0,230,233,235,0,276,354,360,0,361,364,366,0],"finallies":[],"finallyOffsets":[],"sites":[0,1658,1,1659,2,1660,3,1658,4,1657,5,1656,6,1662,7,1663,8,1664,9,1667,10,1668,11,1666,12,1669,13,1665,14,1662,16,1675,17,1674,18,1673,19,1671,20,1679,21,1680,22,1678,23,1677,24,1682,25,1683,26,1684,27,1687,28,1688,29,1686,30,1689,31,1685,32,1682,34,1692,35,1693,36,1691,37,1690,38,1695,39,1696,40,1697,41,1700,42,1701,43,1699,44,1702,45,1698,46,1695,48,1707,49,1706,50,1704,51,1713,52,1712,53,1710,54,1718,55,1719,56,1720,57,1718,58,1717,59,1716,60,1722,61,1723,62,1724,63,1722,64,1721,65,1715,66,1726,67,1727,68,1728,69,1731,70,1732,71,1730,72,1733,73,1729,74,1726,76,1737,77,1738,78,1739,79,1740,80,1737,81,1735,82,1744,83,1745,84,1743,85,1742,86,1747,87,1748,88,1746,89,1741,90,1750,91,1749,92,1754,93,1755,94,1753,95,1752,96,1757,97,1758,98,1756,99,1751,100,1760,101,1759,102,1766,103,1767,104,1765,105,1764,106,1769,107,1768,109,1764,110,1774,111,1775,112,1773,113,1772,114,1777,115,1778,116,1776,117,1771,118,1780,119,1781,120,1779,121,1770,122,1782,123,1783,124,1784,125,1785,126,1782,127,1770,128,1786,129,1762,130,1792,131,1793,132,1791,133,1790,134,1798,135,1799,136,1797,137,1796,138,1801,139,1802,140,1800,141,1795,142,1804,143,1805,144,1803,145,1794,146,1807,147,1808,148,1806,149,1788,150,1810,152,1813,153,1812,155,1819,156,1820,157,1821,158,1824,159,1825,160,1823,161,1822,162,1826,163,1822,164,1827,165,1819,166,1815,172,1816,173,1837,174,1836,175,1838,176,1834,177,1833,178,1832,179,1831,180,1841,181,1842,182,1840,183,1839,184,1845,185,1844,186,1847,187,1848,188,1849,189,1850,190,1851,191,1847,193,1839,194,1854,195,1855,196,1853,197,1852,198,1858,199,1857,200,1860,201,1861,202,1862,203,1863,204,1864,205,1860,207,1852,208,1867,209,1869,210,1870,211,1868,212,1866,213,1865,214,1872,215,1871,222,1815,237,1876,238,1877,239,1875,240,1874,241,1878,242,1874,243,1879,244,1873,245,1886,246,1885,247,1883,248,1881,249,1889,250,1890,251,1891,252,1889,253,1888,254,1887,255,1893,256,1894,257,1895,258,1898,259,1899,260,1897,261,1900,262,1896,263,1893,266,1905,267,1906,268,1907,269,1908,270,1905,271,1901,277,1902,278,1913,279,1914,280,1915,281,1916,282,1917,283,1913,284,1912,285,1920,286,1921,287,1919,288,1918,289,1923,290,1924,291,1925,292,1926,293,1927,294,1923,296,1918,297,1930,298,1931,299,1929,300,1928,301,1934,302,1933,303,1936,304,1937,305,1938,306,1939,307,1940,308,1936,310,1928,311,1943,312,1944,313,1942,314,1941,315,1946,316,1947,317,1948,318,1949,319,1950,320,1946,322,1941,323,1953,324,1954,325,1952,326,1951,327,1956,328,1957,329,1959,330,1958,331,1960,332,1958,333,1961,334,1962,335,1963,336,1956,338,1951,339,1966,340,1968,341,1969,342,1967,343,1965,344,1964,345,1971,346,1970,353,1901,368,1975,369,1976,370,1974,371,1973,372,1977,373,1973,374,1978,375,1972],"localNames":["target","other","name","size","has","keys","own","result","asks","found","iterator","item","item","%it","%e","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":51,"name":"Object$fromEntries","flags":72,"length":1,"params":1,"locals":5,"maxStack":7,"code":[35,65803,131084,10,2621514,2621499,196875,3539788,2818107,131084,131339,0,20,131082,258,582,54,1572925,0,65556,131076,196612,582,5,0,262164,65546,0,327700,131082,3,26,326,131082,65539,26,258,1094,5,458812,70,75,393275,70,3539789,589883,262411,3277646,70,5,262154,64,5,3276859,65546,67],"consts":["$IsObject","$Throw","TypeError","Object.fromEntries expects [key, value] entry objects.","$Define","String"],"captures":[],"handlers":[9,40,46,0,47,50,52,0],"finallies":[],"finallyOffsets":[],"sites":[0,1986,1,1984,3,1991,4,1987,10,1988,11,1995,12,1996,13,1997,14,1998,15,1995,16,1994,17,1993,18,2000,19,2001,20,2002,21,2003,22,2000,24,2005,25,2006,26,2007,27,2008,28,2009,29,2011,30,2012,31,2010,32,2008,33,2014,34,2015,35,2013,36,2016,37,2005,39,1987,54,2018,55,2017],"localNames":["entries","result","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":52,"name":"Math$sumPrecise","flags":72,"length":1,"params":1,"locals":5,"maxStack":4,"code":[36,65803,131084,10,2097226,2097211,196875,3015500,2293819,131084,131339,131082,821,4,51,1441853,0,65556,131076,196612,582,5,65546,65546,262169,31,32,543,131082,28,5,458812,70,75,393275,70,3015501,589883,262411,2753358,70,5,262154,64,5,2752571,0,327700,65546,326,67],"consts":["number","$Throw","TypeError","Math.sumPrecise expects an iterable of numbers.","length","$SumPrecise"],"captures":[],"handlers":[9,32,38,0,39,42,44,0],"finallies":[],"finallyOffsets":[],"sites":[0,2026,1,2024,3,2031,4,2027,10,2028,11,2036,12,2035,13,2037,14,2034,15,2033,16,2039,17,2040,18,2041,19,2042,20,2039,22,2046,23,2048,24,2047,25,2045,26,2047,27,2045,28,2050,29,2045,30,2044,31,2027,46,2052,47,2053,48,2054,49,2052,50,2051],"localNames":["items","numbers","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":53,"name":"aggregateError","flags":72,"length":2,"params":2,"locals":6,"maxStack":6,"code":[36,131339,196620,65546,1376330,1376315,262411,2294860,1572923,196620,196875,131082,131082,25,31,32,543,196618,28,5,458812,70,75,393275,70,2294861,589883,327947,2032718,70,5,327690,64,5,2031675,0,65556,10,131076,131082,2,1094,67],"consts":["length","$Define","errors"],"captures":[],"handlers":[9,21,27,0,28,31,33,0],"finallies":[],"finallyOffsets":[],"sites":[0,2063,1,2061,3,2068,4,2064,10,2065,11,2072,12,2074,13,2073,14,2071,15,2073,16,2071,17,2076,18,2071,19,2070,20,2064,35,2078,36,2079,37,2080,38,2081,39,2082,40,2083,41,2078,42,2077],"localNames":["error","errors","list","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":54,"name":"collectBytes","flags":72,"length":6,"params":6,"locals":11,"maxStack":7,"code":[0,20,10,326,54,1376317,0,65556,131076,65546,196612,43,0,262164,10,326,43,327684,43,582,5,36,393483,524300,10,2752586,2752571,590091,3672396,2949179,524300,524555,393226,393226,393241,31,32,543,524298,28,5,1835068,70,75,1769531,70,3672397,1966139,655627,3410254,70,5,655370,64,5,3407931,3,459019,131082,0,51,5570621,458762,393226,393241,46,5570621,393226,458762,31,32,543,196618,131082,393226,458762,26,458762,582,28,5,458762,56,458763,4063292,0,458772,393226,262154,327690,838,67],"consts":["$IsIterable","$Throw","TypeError"," expects a Uint8Array, an array, or an iterable of numbers, received ","$Describe",".","length","$Bytes"],"captures":[],"handlers":[30,42,48,0,49,52,54,0],"finallies":[],"finallyOffsets":[],"sites":[0,2095,1,2096,2,2097,3,2095,4,2094,5,2093,6,2099,7,2100,8,2101,9,2105,10,2106,11,2104,12,2107,13,2108,14,2109,15,2107,16,2103,17,2110,18,2102,19,2099,21,2114,22,2112,24,2119,25,2115,31,2116,32,2123,33,2125,34,2124,35,2122,36,2124,37,2122,38,2127,39,2122,40,2121,41,2115,56,2131,57,2129,58,2134,59,2135,60,2133,61,2132,62,2139,63,2141,64,2140,65,2138,66,2137,67,2147,68,2148,69,2146,70,2148,71,2146,72,2152,73,2151,74,2154,75,2155,76,2153,77,2156,78,2149,79,2146,80,2145,81,2159,82,2158,83,2159,84,2137,85,2161,86,2162,87,2163,88,2164,89,2165,90,2161,91,2160],"localNames":["source","name","mapper","thisArg","into","offset","values","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":55,"name":"readPairs","flags":72,"length":4,"params":4,"locals":12,"maxStack":6,"code":[0,20,10,326,54,917565,0,65556,131082,196618,10,65546,1094,67,36,262411,0,327947,393228,10,5832778,5832763,524555,6752332,6029371,393228,393483,0,20,393226,326,54,2687037,0,131092,196612,65546,262148,43,582,5,36,327691,458764,393226,4259914,4259899,655627,5179980,4456507,458764,459019,327690,327690,327705,31,32,543,0,393236,458762,326,28,5,3145788,70,75,3080251,70,5179981,3276859,721163,4917838,70,5,720906,64,5,4915259,262154,262154,327705,31,32,543,327690,28,5,1507388,70,75,1441851,70,6752333,1638459,590091,6490190,70,5,589834,64,5,6488123,0,65556,131082,196618,262154,65546,1094,67],"consts":["$IsIterable","$Pairs","$Throw","TypeError"," expects iterable [name, value] pairs.","length","String"],"captures":[],"handlers":[50,65,71,0,72,75,77,0,25,89,95,0,96,99,101,0],"finallies":[],"finallyOffsets":[],"sites":[0,2175,1,2176,2,2177,3,2175,4,2174,5,2173,6,2179,7,2180,8,2181,9,2182,10,2183,11,2184,12,2179,13,2178,14,2188,15,2186,16,2190,19,2196,20,2192,26,2193,27,2200,28,2201,29,2202,30,2200,31,2199,32,2198,33,2204,34,2205,35,2206,36,2208,37,2209,38,2207,39,2204,41,2213,42,2212,44,2218,45,2214,51,2215,52,2222,53,2224,54,2223,55,2221,56,2223,57,2221,58,2226,59,2227,60,2228,61,2226,62,2221,63,2220,64,2214,79,2232,80,2234,81,2233,82,2231,83,2233,84,2231,85,2236,86,2231,87,2230,88,2192,103,2238,104,2239,105,2240,106,2241,107,2242,108,2243,109,2238,110,2237],"localNames":["init","label","kind","proto","pairs","strings","pair","item","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":56,"name":"pairsForEach","flags":72,"length":4,"params":4,"locals":5,"maxStack":5,"code":[3,262411,262154,65546,25,46,1703997,196618,131082,65546,262154,26,65539,26,65546,262154,26,3,26,10,838,5,262154,56,262155,131132,0,67],"consts":["length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2254,1,2252,2,2257,3,2259,4,2258,5,2256,6,2255,7,2266,8,2265,9,2269,10,2270,11,2268,12,2271,13,2267,14,2274,15,2275,16,2273,17,2276,18,2272,19,2277,20,2263,22,2280,23,2279,24,2280,25,2255,26,2250],"localNames":["target","pairs","callback","thisArg","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":57,"name":"Iterator$from","flags":72,"length":1,"params":1,"locals":1,"maxStack":10,"code":[0,20,10,326,721215,0,65556,10,131092,196633,582,917565,10,67,0,262164,0,327700,10,0,393236,10,258,458756,838,582,326,67],"consts":["$IsObject","$Inherits","Iterator","prototype","prime","wrapHelper","flattenable","Iterator.from"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2287,1,2288,2,2289,3,2287,4,2286,5,2290,6,2291,7,2292,8,2294,9,2293,10,2290,11,2285,12,2297,13,2296,14,2299,15,2300,16,2301,17,2302,18,2303,19,2304,20,2305,21,2306,22,2307,23,2308,24,2304,25,2301,26,2299,27,2298],"localNames":["value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":58,"name":"wrapHelper","flags":76,"length":2,"params":2,"locals":9,"maxStack":3,"code":[81,2,131339,0,82,786515,393483,393226,327947,131075,262411,3866683,5,258,131083,196620,65546,2555978,2555963,459019,3475276,2752571,196620,196875,196618,82,2424915,393483,2099022,70,1611,5,393226,327947,131075,262411,3866683,5,1310780,70,75,1245243,70,3475277,1441851,524555,3213134,70,5,524298,64,5,3211323,3,262411,3866683,327947,65539,262411,131082,54,4390973,0,20,10,326,5,66,327690,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,39,45,0,46,49,51,0,3,53,56,0],"finallies":[4,5,0,3,70,-1,68],"finallyOffsets":[0],"sites":[0,2309,1,2317,2,2315,3,2321,13,2325,14,2324,16,2330,17,2326,23,2327,24,2333,25,2332,38,2326,53,2318,59,2337,60,2336,61,2335,62,2339,63,2340,64,2341,65,2339,67,2318,70,2313],"localNames":["source","iterator","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":59,"name":"Iterator$prototype$map","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.map","prime","mapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2342,2,2349,3,2350,4,2351,5,2352,6,2349,7,2347,8,2354,9,2355,10,2356,11,2357,12,2354,14,2359,15,2360,16,2361,17,2362,18,2363,19,2364,20,2365,21,2361,22,2359,23,2358],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":60,"name":"mapHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4587579,5,258,196619,3,262411,327692,65546,3276874,3276859,590091,4196684,3473467,327692,327947,0,131082,327690,262154,55,7,56,262155,582,82,3145811,524555,2820430,70,1611,5,524298,459019,131075,393483,4587579,5,1507388,70,75,1441851,70,4196685,1638459,655627,3934542,70,5,655370,64,5,3932219,3,393483,4587579,459019,65539,393483,196618,54,5111869,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,50,56,0,57,60,62,0,3,64,67,0],"finallies":[6,7,0,3,81,-1,79],"finallyOffsets":[0],"sites":[0,2366,1,2375,2,2373,4,2379,14,2383,15,2382,16,2387,17,2385,19,2392,20,2388,26,2389,27,2395,28,2396,29,2397,30,2399,32,2398,34,2399,35,2395,36,2394,49,2388,64,2376,70,2403,71,2402,72,2401,73,2405,74,2406,75,2407,76,2405,78,2376,81,2371],"localNames":["source","iterator","mapper","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":61,"name":"Iterator$prototype$filter","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.filter","prime","filterHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2408,2,2415,3,2416,4,2417,5,2418,6,2415,7,2413,8,2420,9,2421,10,2422,11,2423,12,2420,14,2425,15,2426,16,2427,17,2428,18,2429,19,2430,20,2431,21,2427,22,2425,23,2424],"localNames":["predicate","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":62,"name":"filterHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4718651,5,258,196619,3,262411,327692,65546,3407946,3407931,590091,4327756,3604539,327692,327947,0,131082,327690,262154,55,7,56,262155,582,3342397,327690,82,3276883,524555,2951502,70,1611,5,524298,459019,131075,393483,4718651,5,1507388,70,75,1441851,70,4327757,1638459,655627,4065614,70,5,655370,64,5,4063291,3,393483,4718651,459019,65539,393483,196618,54,5242941,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,52,58,0,59,62,64,0,3,66,69,0],"finallies":[6,7,0,3,83,-1,81],"finallyOffsets":[0],"sites":[0,2432,1,2441,2,2439,4,2445,14,2449,15,2448,16,2453,17,2451,19,2458,20,2454,26,2455,27,2460,28,2461,29,2462,30,2464,32,2463,34,2464,35,2460,36,2459,37,2467,38,2466,51,2454,66,2442,72,2471,73,2470,74,2469,75,2473,76,2474,77,2475,78,2473,80,2442,83,2437],"localNames":["source","iterator","predicate","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":63,"name":"Iterator$prototype$take","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","takeHelper","count","take"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2476,2,2483,3,2484,4,2485,5,2486,6,2483,7,2481,8,2488,9,2489,10,2490,11,2491,12,2492,13,2493,14,2494,15,2495,16,2496,17,2497,18,2494,19,2490,20,2488,21,2487],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":64,"name":"takeHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,5570619,5,258,196619,131082,3,50,1900605,0,20,10,326,459019,458762,393483,131075,327947,5570619,262156,65546,4259914,4259899,524555,5179468,4456507,262156,262411,131082,312,131083,262154,82,3539027,459019,3213390,70,1611,5,458762,393483,131075,327947,5570619,5,131082,3,50,4194365,4130894,70,1611,5,5177403,2228284,70,75,2162747,70,5179469,2359355,590091,4917326,70,5,589834,64,5,4915259,3,327947,5570619,393483,65539,327947,196618,54,6094909,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[36,65,71,0,72,75,77,0,3,79,82,0],"finallies":[5,6,0,3,96,-1,94],"finallyOffsets":[0],"sites":[0,2498,1,2507,2,2505,3,2511,13,2515,14,2514,15,2518,16,2519,17,2517,18,2516,19,2521,20,2522,21,2523,22,2521,23,2520,30,2528,31,2524,37,2525,38,2532,39,2531,40,2532,41,2535,42,2534,55,2538,56,2539,57,2537,58,2536,59,2540,64,2524,79,2508,85,2544,86,2543,87,2542,88,2546,89,2547,90,2548,91,2546,93,2508,96,2503],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":65,"name":"Iterator$prototype$drop","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","dropHelper","count","drop"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2549,2,2556,3,2557,4,2558,5,2559,6,2556,7,2554,8,2561,9,2562,10,2563,11,2564,12,2565,13,2566,14,2567,15,2568,16,2569,17,2570,18,2567,19,2563,20,2561,21,2560],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":66,"name":"dropHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,4390971,5,258,196619,262156,65546,3080266,3080251,524555,3999820,3276859,262156,262411,131082,3,48,2097213,131082,312,131083,3014715,262154,82,2949203,459019,2623566,70,1611,5,458762,393483,131075,327947,4390971,5,1310780,70,75,1245243,70,3999821,1441851,590091,3737678,70,5,589834,64,5,3735611,3,327947,4390971,393483,65539,327947,196618,54,4915261,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,47,53,0,54,57,59,0,3,61,64,0],"finallies":[5,6,0,3,78,-1,76],"finallyOffsets":[0],"sites":[0,2571,1,2580,2,2578,3,2584,13,2588,14,2587,16,2593,17,2589,23,2590,24,2597,25,2598,26,2596,27,2595,28,2601,29,2600,30,2601,31,2595,32,2604,33,2603,46,2589,61,2581,67,2608,68,2607,69,2606,70,2610,71,2611,72,2612,73,2610,75,2581,78,2576],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":67,"name":"Iterator$prototype$flatMap","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.flatMap","prime","flatMapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2613,2,2620,3,2621,4,2622,5,2623,6,2620,7,2618,8,2625,9,2626,10,2627,11,2628,12,2625,14,2630,15,2631,16,2632,17,2633,18,2634,19,2635,20,2636,21,2632,22,2630,23,2629],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":68,"name":"flatMapHelper","flags":76,"length":3,"params":3,"locals":14,"maxStack":7,"code":[81,2,196875,262156,0,82,852051,590091,589834,524555,131075,459019,6750267,5,258,196619,3,262411,327692,65546,5439562,5439547,655627,6359628,5636155,327692,327947,393228,0,20,0,131082,327690,262154,55,7,56,262155,582,2,65540,838,4456522,4456507,786699,5377100,4653115,393228,393483,393226,82,4325459,590091,3738702,70,1611,5,4000334,70,1611,5,589834,524555,131075,459019,6750267,5,2949180,70,75,2883643,70,5377101,3080251,852235,5114958,70,5,851978,64,5,5111867,1507388,70,75,1441851,70,6359629,1638459,721163,6097486,70,5,720906,64,5,6094907,3,459019,6750267,524555,65539,459019,196618,54,7274557,0,131092,10,326,5,66,524298,67,0,67],"consts":["flattenable","Iterator.prototype.flatMap","closeIterator"],"captures":[],"handlers":[47,68,74,0,75,78,80,0,25,83,89,0,90,93,95,0,3,97,100,0],"finallies":[7,8,0,3,114,-1,112],"finallyOffsets":[0],"sites":[0,2637,1,2646,2,2644,4,2650,14,2654,15,2653,16,2658,17,2656,19,2663,20,2659,26,2660,28,2669,29,2670,30,2671,31,2672,32,2673,33,2675,35,2674,37,2675,38,2671,39,2676,40,2677,41,2669,42,2665,48,2666,49,2680,50,2679,67,2665,82,2659,97,2647,103,2684,104,2683,105,2682,106,2686,107,2687,108,2688,109,2686,111,2647,114,2642],"localNames":["source","iterator","mapper","started","counter","item","inner","%ftag0","%fval0","%ret","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":69,"name":"prime","flags":72,"length":1,"params":1,"locals":1,"maxStack":2,"code":[10,281,70,5,10,67],"consts":["next"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2696,1,2695,2,2694,4,2699,5,2698],"localNames":["helper"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":70,"name":"count","flags":72,"length":2,"params":2,"locals":4,"maxStack":5,"code":[10,309,131339,131082,20,50,720959,131082,20,53,50,917565,131082,1245243,131082,131082,65539,308,44,196875,131082,131082,51,1769535,196618,3,46,2752573,0,65556,131076,196612,65546,43,262148,43,131082,43,327684,43,582,5,196618,67],"consts":["Infinity","$Throw","RangeError","Iterator.prototype."," expects a non-negative count, received ","."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2709,1,2708,2,2706,3,2716,4,2717,5,2715,6,2714,7,2719,8,2721,9,2720,10,2718,11,2713,12,2722,13,2713,14,2724,15,2726,16,2727,17,2725,18,2723,19,2711,20,2731,21,2732,22,2730,23,2729,24,2734,25,2735,26,2733,27,2728,28,2737,29,2738,30,2739,31,2744,32,2745,33,2743,34,2746,35,2742,36,2747,37,2741,38,2748,39,2740,40,2737,42,2750,43,2749],"localNames":["limit","name","number","integer"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":71,"name":"flattenable","flags":72,"length":3,"params":3,"locals":4,"maxStack":7,"code":[10,821,4,50,524349,65546,54,852027,0,65556,10,326,54,1900605,0,131092,196612,131082,262148,43,0,327700,10,326,43,393220,43,582,5,10,458772,524313,26,196875,0,589844,196618,0,50,2818111,196618,1,50,3014717,10,3211323,10,196618,70,2,582,67],"consts":["string","$IsObject","$Throw","TypeError"," expects an iterable or iterator, received ","$Describe",".","Symbol","iterator","$Direct"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2761,1,2760,2,2762,3,2759,4,2758,5,2764,6,2763,7,2758,8,2766,9,2767,10,2768,11,2766,12,2765,13,2757,14,2770,15,2771,16,2772,17,2776,18,2777,19,2775,20,2778,21,2779,22,2780,23,2778,24,2774,25,2781,26,2773,27,2770,29,2786,30,2788,31,2787,32,2785,33,2783,34,2791,35,2792,36,2796,37,2797,38,2795,39,2794,40,2799,41,2800,42,2798,43,2793,44,2801,45,2793,46,2805,47,2804,48,2802,49,2806,50,2791,51,2790],"localNames":["value","strings","name","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":72,"name":"closeIterator","flags":72,"length":1,"params":1,"locals":2,"maxStack":4,"code":[10,25,65803,65546,0,51,655679,65546,1,51,1179967,0,65556,10,65546,70,326,54,1638461,0,131092,196612,262148,582,5,0,67],"consts":["return","$IsObject","$Throw","TypeError","Iterator return() must return an object."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2815,1,2814,2,2812,3,2821,4,2822,5,2820,6,2819,7,2824,8,2825,9,2823,10,2818,11,2827,12,2828,13,2832,14,2831,15,2829,16,2827,17,2826,18,2817,19,2834,20,2835,21,2836,22,2837,23,2834,25,2810],"localNames":["iterator","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":73,"name":"Iterator$prototype$toArray","flags":88,"length":0,"params":0,"locals":5,"maxStack":4,"code":[789,267,36,65803,131084,0,20,10,2,582,1769546,1769531,196875,2687820,1966139,131084,131339,65546,65546,65561,31,32,543,131082,28,5,852028,70,75,786491,70,2687821,983099,262411,2425678,70,5,262154,64,5,2424891,65546,67],"consts":["$Direct","length"],"captures":[],"handlers":[15,27,33,0,34,37,39,0],"finallies":[],"finallyOffsets":[],"sites":[0,2838,2,2844,3,2842,5,2849,6,2850,7,2851,8,2852,9,2849,10,2845,16,2846,17,2856,18,2858,19,2857,20,2855,21,2857,22,2855,23,2860,24,2855,25,2854,26,2845,41,2862,42,2861],"localNames":["this","result","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":74,"name":"Iterator$prototype$reduce","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,65813,131339,0,20,10,2,582,196875,0,65556,65546,131076,582,5,131082,3,26,262411,131082,196633,3,48,327947,3,393483,458764,196618,3670090,3670075,524555,4589644,3866683,458764,459019,327690,3145789,0,65546,262154,458762,393226,838,262155,3276859,458762,262155,258,327691,393226,56,393227,2228284,70,75,2162747,70,4589645,2359355,590091,4327502,70,5,589834,64,5,4325435,327690,54,5177405,0,262164,327684,393220,582,5,262154,67],"consts":["$Direct","$Callback","Iterator.prototype.reduce","length","$Throw","TypeError","Iterator.prototype.reduce of an empty iterator with no initial value."],"captures":[],"handlers":[36,56,62,0,63,66,68,0],"finallies":[],"finallyOffsets":[],"sites":[0,2863,2,2865,4,2866,6,2872,7,2873,8,2874,9,2875,10,2872,11,2870,12,2877,13,2878,14,2879,15,2880,16,2877,18,2885,19,2886,20,2884,21,2882,22,2892,23,2891,24,2894,25,2890,26,2888,27,2898,28,2896,30,2903,31,2899,37,2900,38,2906,39,2905,40,2910,41,2911,42,2912,43,2913,44,2914,45,2910,46,2909,47,2905,48,2918,49,2917,50,2922,51,2921,52,2925,53,2924,54,2925,55,2899,70,2928,71,2927,72,2926,73,2930,74,2931,75,2932,76,2933,77,2930,79,2935,80,2934],"localNames":["this","callback","initial","iterator","accumulator","started","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":75,"name":"Iterator$prototype$forEach","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,5,0,67],"consts":["iteratorTest","forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2936,2,2941,3,2942,4,2943,5,2944,6,2945,7,2941,9,2939],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":76,"name":"Iterator$prototype$some","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2946,2,2951,3,2952,4,2953,5,2954,6,2955,7,2951,8,2950],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":77,"name":"Iterator$prototype$every","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2956,2,2961,3,2962,4,2963,5,2964,6,2965,7,2961,8,2960],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":78,"name":"Iterator$prototype$find","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2966,2,2971,3,2972,4,2973,5,2974,6,2975,7,2971,8,2970],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":79,"name":"iteratorTest","flags":72,"length":3,"params":3,"locals":10,"maxStack":5,"code":[0,20,10,2,582,196875,0,65556,65546,131076,131082,43,582,5,3,262411,0,327947,393228,196618,5308490,5308475,459019,6227788,5505083,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,196612,50,2752831,327690,3342397,258,590091,3213134,70,1611,5,589834,67,131082,262148,50,3735871,327690,54,4325437,2,590091,4196174,70,1611,5,589834,67,131082,327684,50,4653375,327690,5242941,393226,590091,5113678,70,1611,5,589834,67,1507388,70,75,1441851,70,6227789,1638459,524555,5965646,70,5,524298,64,5,5963835,131082,262148,50,6619197,258,7077947,131082,196612,50,7012413,2,7077947,0,67],"consts":["$Direct","$Callback","Iterator.prototype.","some","every","find"],"captures":[],"handlers":[25,81,87,0,88,91,93,0],"finallies":[],"finallyOffsets":[],"sites":[0,2985,1,2986,2,2987,3,2988,4,2985,5,2983,6,2990,7,2991,8,2992,9,2994,10,2995,11,2993,12,2990,14,2999,15,2997,16,3001,19,3007,20,3003,26,3004,27,3012,28,3013,29,3014,30,3016,32,3015,34,3016,35,3012,36,3011,37,3020,38,3021,39,3019,40,3018,41,3022,42,3017,43,3024,44,3023,51,3028,52,3029,53,3027,54,3026,55,3031,56,3030,57,3025,58,3033,59,3032,66,3037,67,3038,68,3036,69,3035,70,3039,71,3034,72,3041,73,3040,80,3003,95,3045,96,3046,97,3044,98,3043,99,3047,100,3043,101,3050,102,3051,103,3049,104,3048,105,3052,106,3048,107,3053,108,3042],"localNames":["self","callback","name","iterator","index","result","item","%it","%e","%ret"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":80,"name":"revive","flags":72,"length":3,"params":3,"locals":7,"maxStack":5,"code":[10,65546,26,196875,0,20,196618,326,3932221,262156,327692,393228,0,65556,196618,326,262411,3,327947,0,393483,327690,262154,131097,46,3932221,0,196628,196618,262154,327690,26,131082,838,393227,393226,0,50,3014717,196618,262154,327690,26,30,5,3670075,196618,262154,327690,26,31,32,543,393226,28,5,327690,56,327691,1376316,10,131082,65546,196618,582,67],"consts":["$IsObject","$Keys","length","revive"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3064,1,3065,2,3063,3,3061,4,3067,5,3068,6,3069,7,3067,8,3066,12,3074,13,3075,14,3076,15,3074,16,3072,17,3080,18,3078,19,3082,21,3086,22,3088,23,3087,24,3085,25,3084,26,3094,27,3095,28,3096,29,3098,30,3099,31,3097,32,3100,33,3094,34,3093,35,3103,36,3104,37,3102,38,3101,39,3108,40,3110,41,3111,42,3109,43,3107,44,3106,45,3101,46,3115,47,3117,48,3118,49,3116,50,3114,51,3116,52,3114,53,3119,54,3114,55,3113,56,3122,57,3121,58,3122,59,3084,60,3127,61,3126,62,3128,63,3129,64,3124,65,3123],"localNames":["holder","key","reviver","value","names","index","revived"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":81,"name":"replaceWith","flags":72,"length":3,"params":3,"locals":6,"maxStack":9,"code":[4,196875,3,262411,262154,10,65561,46,3211325,196618,10,262154,26,3,26,0,131092,0,131082,36,10,262154,26,65539,26,2228810,2228283,327947,1967439,2621499,2622796,2424891,37,1966139,70,587,1769531,70,2622797,2097211,71,326,43,43,196619,262154,56,262155,262204,196618,65546,43,67],"consts":["","length","String"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3139,1,3137,2,3143,3,3141,4,3146,5,3148,6,3147,7,3145,8,3144,9,3153,10,3157,11,3158,12,3156,13,3159,14,3155,15,3160,16,3161,17,3162,18,3163,19,3162,20,3167,21,3168,22,3166,23,3169,24,3165,25,3164,40,3162,41,3160,42,3154,43,3152,44,3153,45,3172,46,3171,47,3172,48,3144,49,3175,50,3176,51,3174,52,3173],"localNames":["matches","rest","callback","result","index","%it"],"upvalNames":[]}]},"@vm-internal":{"text":"8cec7c4116877903","codes":[{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":1,"name":"$vmToPrimitive","flags":72,"length":2,"params":2,"locals":8,"maxStack":4,"code":[0,20,10,326,54,524349,10,67,65546,65540,50,1310783,65546,131076,50,1311039,0,196628,10,326,1507389,262148,1572923,327684,131339,131082,262148,50,2031677,327684,2097211,262148,196875,0,393236,10,131082,582,262411,262154,0,51,3670077,393228,10,262154,70,393483,0,458772,393226,326,54,3670077,393226,67,0,393236,10,196618,582,327947,327690,0,51,5177405,458764,10,327690,70,459019,0,458772,458762,326,54,5177405,458762,67,0,524308,589828,655364,582,5,0,67],"consts":["$IsDataObj","string","default","$IsDate","toString","valueOf","$GetMethod","$IsObj","$Throw","TypeError","Cannot convert object to primitive value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,8,1,9,2,10,3,8,4,7,5,6,6,12,7,11,8,19,9,20,10,18,11,17,12,23,13,24,14,22,15,21,16,25,17,26,18,27,19,25,20,16,21,28,22,16,23,29,24,14,25,35,26,36,27,34,28,33,29,37,30,33,31,38,32,31,33,42,34,43,35,44,36,45,37,42,38,40,39,48,40,49,41,47,42,46,44,57,45,56,46,54,47,52,48,60,49,61,50,62,51,60,52,59,53,58,54,64,55,63,56,68,57,69,58,70,59,71,60,68,61,66,62,74,63,75,64,73,65,72,67,83,68,82,69,80,70,78,71,86,72,87,73,88,74,86,75,85,76,84,77,90,78,89,79,92,80,93,81,94,82,95,83,92,85,5],"localNames":["v","hint","first","second","m1","m2","r1","r2"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":2,"name":"$vmToKey","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$PropKey","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,101,1,102,2,103,3,104,4,105,5,106,6,103,7,101,8,100],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":3,"name":"$vmToString","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$CoerceToString","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,112,1,113,2,114,3,115,4,116,5,117,6,114,7,112,8,111],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":4,"name":"$vmToNumeric","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,124,1,125,2,126,3,127,4,128,5,129,6,130,7,127,8,124,9,123],"localNames":["incFlag","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":5,"name":"$vmInc","flags":72,"length":2,"params":2,"locals":3,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,131339,10,3,50,1179709,131082,65539,43,1376315,131082,65539,44,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,139,1,140,2,141,3,142,4,143,5,144,6,145,7,142,8,139,9,137,10,149,11,150,12,148,13,147,14,152,15,153,16,151,17,147,18,155,19,156,20,154,21,146],"localNames":["incFlag","v","n"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":6,"name":"$vmUnary","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyUnary","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,163,1,164,2,165,3,166,4,167,5,168,6,169,7,166,8,163,9,162],"localNames":["sub","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":7,"name":"$vmBinary","flags":72,"length":3,"params":3,"locals":8,"maxStack":5,"code":[0,20,10,65546,131082,838,196875,196618,3,50,1179709,0,65556,10,65546,131082,838,67,196618,65539,50,2359357,458764,0,131092,65546,196612,582,459019,0,65556,10,458762,131082,838,67,196618,131075,50,2752573,262148,2818107,327684,262411,0,131092,65546,262154,582,327947,0,131092,131082,262154,582,393483,0,65556,10,327690,393226,838,67],"consts":["$OpConvKind","$ApplyBinary","$vmToPrimitive","string","default","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,179,1,180,2,181,3,182,4,183,5,179,6,177,7,186,8,187,9,185,10,184,11,189,12,190,13,191,14,192,15,193,16,189,17,188,18,196,19,197,20,195,21,194,23,202,24,203,25,204,26,205,27,202,28,200,29,207,30,208,31,209,32,210,33,211,34,207,35,206,36,217,37,218,38,216,39,215,40,219,41,215,42,220,43,213,44,224,45,225,46,226,47,227,48,224,49,222,50,231,51,232,52,233,53,234,54,231,55,229,56,236,57,237,58,238,59,239,60,240,61,236,62,235],"localNames":["op","lhs","rhs","kind","hint","l","r","key"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":8,"name":"$vmGetElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,26,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,248,1,249,2,250,3,251,4,249,5,247,6,246],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":9,"name":"$vmDeleteElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,30,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,260,1,261,2,262,3,263,4,261,5,259,6,257],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":10,"name":"$vmConvert","flags":72,"length":6,"params":6,"locals":16,"maxStack":7,"code":[258,7274557,393228,458764,524300,589836,655372,720908,786444,0,20,327690,326,393483,0,65556,327690,3,582,459019,0,65556,327690,65539,582,524555,393226,131097,590091,36,655627,3,721163,720906,589834,46,5439549,851980,917516,983052,393226,720906,26,852235,0,196628,327690,720906,582,917771,0,262164,851978,326,3997757,0,327700,851978,917514,582,4063291,851978,983307,655370,720906,31,32,543,458762,4980797,0,393236,917514,983050,582,5046331,983050,28,5,720906,56,720907,2162748,262154,524298,31,32,543,655370,28,5,0,458772,10,65546,131082,196618,262154,1350,786699,0,524308,786442,326,54,7077949,786442,67,786442,327691,60,0,67],"consts":["$ConvValues","$ConvFlag","length","$ConvHint","$IsObj","$vmToPrimitive","$ConvCoerce","$ConvReplay","$IsConvert"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,274,1,273,9,279,10,280,11,281,12,279,13,277,14,285,15,286,16,287,17,288,18,285,19,283,20,292,21,293,22,294,23,295,24,292,25,290,26,300,27,299,28,297,29,305,30,303,31,309,32,307,33,312,34,313,35,311,36,310,40,319,41,320,42,318,43,316,44,324,45,325,46,326,47,327,48,324,49,322,50,332,51,333,52,334,53,332,54,331,55,335,56,336,57,337,58,338,59,335,60,331,61,339,62,329,63,343,64,344,65,342,66,344,67,342,68,346,69,345,70,347,71,348,72,349,73,350,74,347,75,345,76,351,77,342,78,341,79,354,80,353,81,354,82,310,83,358,84,359,85,357,86,359,87,357,88,360,89,357,90,356,91,364,92,365,93,366,94,367,95,368,96,369,97,370,98,364,99,362,100,373,101,374,102,375,103,373,104,372,105,371,106,377,107,376,108,381,109,380,110,273,111,272],"localNames":["callable","target","args","constructing","replay","req","values","coerce","ordinal","len","converted","i","res","val","hint","prim"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":11,"name":"$vmJsonVisitor","flags":72,"length":3,"params":3,"locals":5,"maxStack":1,"code":[269,65805,131341,196621,262157,786454,262416,36,196880,262159,67],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,382,5,392,7,391,8,389,9,639,10,638],"localNames":["boundary","replacer","properties","ancestors","visit"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":12,"name":"visit","flags":76,"length":3,"params":3,"locals":19,"maxStack":6,"code":[81,131082,6553603,48,720957,0,20,65540,131076,582,5,10,65546,26,196875,0,196628,196618,326,2228285,17,54,1900605,0,20,262148,327684,582,5,196618,82,2162771,67,196619,0,393236,196618,326,3473469,458764,196618,458777,459019,0,524308,458762,326,3473469,196618,458762,65546,326,196619,65553,0,51,4128829,10,65553,65546,196618,582,196619,0,589844,196618,17,131089,838,262411,262154,3,50,4980797,0,67,262154,65539,50,5374013,196618,67,262154,131075,50,5767229,1,67,262154,196611,50,6422589,0,655380,196618,17,582,67,262154,262147,50,7012413,0,720916,196618,326,7077947,196618,327947,131089,131089,786457,31,32,543,327690,28,5,0,393483,0,851988,327690,326,13172797,524300,589836,36,393227,327690,786457,524555,3,590091,589834,524298,46,13107261,655372,0,196625,327690,0,917524,589834,326,131082,65539,43,838,11338570,11337787,983307,0,3,7,1048843,11079509,1048586,131075,50,10682429,67,5,11013966,70,1611,5,983105,326,11538518,82,10158140,70,843,9961531,1048586,131075,50,11862077,67,655627,393226,589834,31,32,543,655370,0,50,12648509,1,12714043,655370,28,5,589834,56,589835,8781884,18546747,720908,786444,0,1048596,70,393227,0,1114132,327690,17,262161,838,721163,3,786699,786442,720906,786457,46,18546749,851980,917516,720906,786442,26,852235,0,196625,327690,851978,131082,65539,43,838,16909130,16908347,1114379,0,3,7,1179915,16650581,1179658,131075,50,16252989,67,5,16585038,70,1611,5,1179713,326,17109590,82,15728700,70,843,15532091,1179658,131075,50,17432637,67,917771,917514,0,51,18284605,0,1245204,393226,851978,917514,258,1094,5,786442,56,786443,14155836,0,1310740,131089,326,5,393226,67],"consts":["$Throw","RangeError","Value exceeds the maximum depth of 100.","$IsPromise","InvalidData","JSON.stringify received an un-awaited Promise; await it first - e.g. `const result = await tools.ns.tool(...)`.","$IsObj","toJSON","$IsCallableAny","$JsonClassify","$JsonSpecial","$SetToArray","length","$IsArray","String",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$NewNullProtoObj","$JsonNames",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$Define","$ArrayPop"],"captures":[0,0,0,1,0,3,0,4,0,2],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,392,1,400,2,401,3,399,4,398,5,403,6,404,7,405,8,406,9,403,11,411,12,412,13,410,14,408,15,414,16,415,17,416,18,414,19,413,20,420,21,419,22,418,23,423,24,424,25,425,26,426,27,423,29,431,30,430,33,429,34,433,35,434,36,435,37,433,38,432,40,441,41,440,42,438,43,444,44,445,45,446,46,444,47,443,48,453,49,452,50,454,51,450,52,449,53,457,54,458,55,456,56,455,57,465,58,464,59,466,60,467,61,462,62,461,63,471,64,472,65,473,66,474,67,475,68,471,69,469,70,478,71,479,72,477,73,476,74,481,75,480,76,484,77,485,78,483,79,482,80,487,81,486,82,490,83,491,84,489,85,488,86,493,87,492,88,496,89,497,90,495,91,494,92,499,93,500,94,501,95,502,96,499,97,498,98,508,99,509,100,507,101,506,102,510,103,511,104,512,105,510,106,506,107,513,108,504,109,517,110,519,111,518,112,516,113,518,114,516,115,521,116,516,117,515,118,523,120,526,121,527,122,528,123,526,124,525,127,533,128,532,129,538,130,537,131,535,132,543,133,541,134,546,135,547,136,545,137,544,139,553,140,554,141,555,142,556,143,557,144,558,145,556,146,560,147,561,148,559,149,553,150,552,181,550,182,565,183,566,184,564,185,566,186,564,187,569,188,570,189,568,190,567,191,571,192,567,193,572,194,564,195,563,196,575,197,574,198,575,199,544,200,525,203,580,204,581,205,580,206,579,207,585,208,586,209,587,210,588,211,589,212,585,213,583,214,593,215,591,216,596,217,598,218,597,219,595,220,594,223,605,224,606,225,604,226,602,227,611,228,612,229,613,230,614,231,616,232,617,233,615,234,611,235,610,266,608,267,620,268,621,269,619,270,618,271,623,272,624,273,625,274,626,275,627,276,628,277,623,279,631,280,630,281,631,282,594,283,633,284,634,285,635,286,633,288,637,289,636],"localNames":["holder","key","depth","val","kind","target","out","toJSON","len","i","elem","names","i","k","propVal","%it","%kind","%it","%kind"],"upvalNames":["boundary","replacer","ancestors","visit","properties"]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":13,"name":"$vmJsonWalk","flags":72,"length":4,"params":4,"locals":5,"maxStack":6,"code":[0,0,20,2,10,131082,838,35,65546,65574,65540,3,838,131353,70,196633,262411,0,262164,2,262154,131082,196618,1094,67],"consts":["$vmJsonVisitor","","next","value","$JsonFinish"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,653,1,654,2,655,3,656,4,657,5,658,6,654,7,659,8,662,9,660,10,663,11,664,12,653,13,652,14,651,15,650,16,648,17,668,18,669,19,670,20,671,21,672,22,673,23,668,24,667],"localNames":["replacer","value","properties","indent","json"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":14,"name":"$vmSettleCall","flags":72,"length":3,"params":3,"locals":3,"maxStack":5,"code":[0,20,10,65546,131082,838,67],"consts":["$Apply"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,681,1,682,2,683,3,684,4,685,5,681,6,680],"localNames":["callable","thisVal","args"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":15,"name":"$vmCombine","flags":72,"length":2,"params":2,"locals":6,"maxStack":4,"code":[0,20,10,65546,582,131339,196620,65546,1441866,1441851,262411,2360396,1638459,196620,196875,0,65556,131082,196618,582,5,720956,70,75,655419,70,2360397,852027,327947,2098254,70,5,327690,64,5,2097211,0,131092,131082,326,5,0,67],"consts":["$CombineStart","$CombineAdd","$CombineEnd"],"captures":[],"handlers":[13,22,28,0,29,32,34,0],"finallies":[],"finallyOffsets":[],"sites":[0,694,1,695,2,696,3,697,4,694,5,692,7,702,8,698,14,699,15,705,16,706,17,707,18,708,19,705,21,698,36,710,37,711,38,712,39,710,41,690],"localNames":["name","items","c","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":16,"name":"$vmOpenSyncForAsync","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[0,20,10,65546,70,326,67],"consts":["$WrapAsyncFromSync"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,719,1,720,2,724,3,723,4,721,5,719,6,718],"localNames":["v","method"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":17,"name":"$vmAsyncFromSync","flags":72,"length":4,"params":4,"locals":10,"maxStack":7,"code":[0,20,10,326,262411,65546,65540,50,917565,0,131092,10,326,1310779,0,196628,10,262154,65546,838,327947,327690,0,50,1835071,327690,1,50,4718653,589836,65546,262148,50,2687037,0,327700,196618,326,5,0,67,0,196628,10,262154,262148,838,590091,589834,0,51,3604799,589834,1,51,4259901,0,393236,10,262154,589834,70,262148,838,5,0,458772,10,326,5,0,67,131082,5177405,262154,327690,196618,326,5374011,262154,327690,70,393483,0,393236,10,393226,65546,838,5,393226,524313,54,54,459019,393226,589849,524555,0,655380,10,524298,458762,65546,262148,51,1094,5,0,67],"consts":["$AfsGetIter","next","$AfsGetNext","$AfsGetMethod","return","$AfsResolveDone","$AfsCheckObj","$AfsRejectNoThrow","done","value","$AfsSettle"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,735,1,736,2,737,3,735,4,733,5,743,6,744,7,742,8,741,9,745,10,746,11,747,12,745,13,741,14,748,15,749,16,750,17,751,18,752,19,748,20,739,21,756,22,757,23,755,24,754,25,759,26,760,27,758,28,753,30,764,31,765,32,763,33,762,34,768,35,769,36,770,37,768,39,771,41,775,42,776,43,777,44,778,45,779,46,775,47,773,48,783,49,784,50,782,51,781,52,786,53,787,54,785,55,780,56,790,57,791,58,792,59,796,60,795,61,793,62,797,63,790,65,799,66,800,67,801,68,799,70,802,72,807,73,806,74,811,75,810,76,812,77,808,78,806,79,816,80,815,81,813,82,804,83,818,84,819,85,820,86,821,87,822,88,818,90,829,91,828,92,827,93,826,94,824,95,835,96,834,97,832,98,838,99,839,100,840,101,841,102,842,103,844,104,845,105,843,106,838,108,731],"localNames":["adapter","kind","hasArg","val","iter","method","res","done","value","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":18,"name":"$vmAsyncFromSyncClose","flags":72,"length":1,"params":1,"locals":2,"maxStack":2,"code":[10,25,65803,65546,0,51,655679,65546,1,51,983101,10,65546,70,5,0,67],"consts":["return"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,854,1,853,2,851,3,859,4,860,5,858,6,857,7,862,8,863,9,861,10,856,11,868,12,867,13,865,15,849],"localNames":["iter","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"0d2b321257a5648b"},"index":19,"name":"$vmCross","flags":74,"length":3,"params":3,"locals":8,"maxStack":8,"code":[36,196875,3,262411,262154,10,25,46,5046333,327692,393228,458764,10,262154,26,327947,65546,0,50,1573183,0,65556,327690,326,1835069,327690,340,327691,0,0,131092,258,0,1,838,35,327690,196646,196612,3,838,393483,393226,262425,70,459019,458762,327705,54,3801149,393226,262425,458762,393241,340,326,458763,3014716,196618,262154,31,32,543,0,458772,258,458762,393241,1,0,1094,28,5,262154,56,262155,262204,65546,0,50,5570621,196618,3,26,67,65546,524292,50,6160445,0,589844,196618,326,67,0,655380,65546,196618,131082,838,5,0,67],"consts":["length","$IsThenable","$vmJsonVisitor","","next","done","value","$JsonFinish","search","$HostSearch","$HostTool"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,878,1,876,2,882,3,880,4,885,5,887,6,886,7,884,8,883,12,894,13,895,14,893,15,891,16,899,17,900,18,898,19,897,20,901,21,902,22,903,23,901,24,896,25,908,26,907,27,906,28,912,29,913,30,914,31,915,32,916,33,917,34,913,35,918,36,921,37,919,38,922,39,923,40,912,41,910,42,929,43,928,44,927,45,925,46,934,47,933,48,932,49,931,50,941,51,940,52,945,53,944,54,943,55,939,56,938,57,931,58,950,59,951,60,949,61,951,62,949,63,952,64,953,65,954,66,956,67,955,68,958,69,959,70,952,71,949,72,948,73,962,74,961,75,962,76,883,77,965,78,966,79,964,80,963,81,969,82,970,83,968,84,967,85,973,86,974,87,972,88,971,89,976,90,977,91,978,92,976,93,975,94,980,95,981,96,982,97,983,98,984,99,980,101,874],"localNames":["roots","path","resolving","crossed","r","root","walk","step"],"upvalNames":[]}]}} +{"@prelude":{"text":"f135511dc7fc0839","codes":[{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":1,"name":"arrayFromIterable","flags":72,"length":3,"params":3,"locals":8,"maxStack":6,"code":[36,196875,3,262411,327692,10,2293834,2293819,393483,3212876,2490427,327692,327947,196618,262154,31,32,543,65546,0,50,1572925,327690,1900603,131082,65546,327690,262154,582,28,5,262154,56,262155,589884,70,75,524347,70,3212877,720955,459019,2950734,70,5,458762,64,5,2949179,196618,67],"consts":[],"captures":[],"handlers":[11,35,41,0,42,45,47,0],"finallies":[],"finallyOffsets":[],"sites":[0,10,1,8,2,14,3,12,5,19,6,15,12,16,13,24,14,25,15,23,16,25,17,23,18,28,19,29,20,27,21,26,22,30,23,26,24,34,25,33,26,35,27,36,28,31,29,23,30,22,31,39,32,38,33,39,34,15,49,41,50,40],"localNames":["items","mapper","thisArg","result","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":2,"name":"arrayFromLike","flags":72,"length":4,"params":4,"locals":6,"maxStack":6,"code":[20,65352,65546,328,262411,3,327947,327690,65546,46,1900605,262154,327690,31,32,543,196618,131082,10,327690,26,327690,582,28,5,327690,56,327691,458812,262154,67],"consts":["Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,53,1,52,2,54,3,52,4,50,5,58,6,56,7,61,8,62,9,60,10,59,11,67,12,68,13,66,14,68,15,66,16,72,17,71,18,74,19,75,20,73,21,76,22,69,23,66,24,65,25,79,26,78,27,79,28,59,29,81,30,80],"localNames":["source","length","mapper","thisArg","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":3,"name":"Array$prototype$map","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["mapItems","$Receiver","Array","Array.prototype.map","Array.map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,82,2,84,4,85,6,87,7,85,8,90,9,91,10,92,11,93,12,94,13,95,14,96,15,92,16,97,17,98,18,99,19,90,20,89],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":4,"name":"Uint8Array$prototype$map","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.map","$Bytes","mapItems","Uint8Array.map","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,100,2,102,4,103,6,105,7,103,8,110,9,111,10,112,11,113,12,114,13,110,14,108,15,116,16,117,17,118,18,119,19,120,20,121,21,122,22,123,23,118,24,125,25,124,26,127,27,116,28,115],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":5,"name":"mapItems","flags":72,"length":4,"params":4,"locals":7,"maxStack":7,"code":[0,20,65546,196618,582,5,10,65561,262411,131092,65352,262154,328,327947,3,393483,393226,262154,46,2818109,393226,10,33,2555965,327690,393226,31,32,543,131082,65546,10,393226,26,393226,10,838,28,5,393226,56,393227,1048636,327690,67],"consts":["$Callback","length","Array"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,136,1,137,2,138,3,139,4,136,6,144,7,143,8,141,9,150,10,149,11,151,12,149,13,147,14,155,15,153,16,158,17,159,18,157,19,156,20,163,21,164,22,162,23,161,24,168,25,169,26,167,27,169,28,167,29,173,30,172,31,175,32,176,33,174,34,177,35,178,36,170,37,167,38,166,39,181,40,180,41,181,42,156,43,183,44,182],"localNames":["array","callback","thisArg","label","length","result","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":6,"name":"Array$prototype$filter","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,67],"consts":["filterItems","$Receiver","Array","Array.prototype.filter","Array.filter"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,184,2,186,4,187,6,189,7,187,8,192,9,193,10,194,11,195,12,196,13,197,14,198,15,194,16,199,17,200,18,201,19,192,20,191],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":7,"name":"Uint8Array$prototype$filter","flags":592,"length":1,"params":2,"locals":4,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,0,262164,196618,65546,131082,327684,1094,65556,393241,3,838,67],"consts":["$Receiver","Uint8Array","Uint8Array.prototype.filter","$Bytes","filterItems","Uint8Array.filter","prototype"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,202,2,204,4,205,6,207,7,205,8,212,9,213,10,214,11,215,12,216,13,212,14,210,15,218,16,219,17,220,18,221,19,222,20,223,21,224,22,225,23,220,24,227,25,226,26,229,27,218,28,217],"localNames":["this","callback","thisArg","bytes"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":8,"name":"filterItems","flags":72,"length":4,"params":4,"locals":9,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,36,327947,3,393483,3,459019,0,524555,458762,262154,46,3407933,458762,10,33,3145789,10,458762,26,524299,131082,65546,524298,458762,10,838,3145789,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,1114172,327690,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,238,1,239,2,240,3,241,4,238,6,246,7,245,8,243,9,251,10,249,11,255,12,253,13,259,14,257,15,261,17,265,18,266,19,264,20,263,21,270,22,271,23,269,24,268,25,277,26,278,27,276,28,275,29,283,30,282,31,284,32,285,33,286,34,280,35,279,36,290,37,292,39,291,41,292,42,289,43,291,44,289,45,293,46,289,47,288,48,296,49,295,50,296,51,263,52,298,53,297],"localNames":["array","callback","thisArg","label","length","result","count","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":9,"name":"Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Array","Array.prototype.forEach","Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,299,2,301,4,302,6,304,7,302,8,307,9,308,10,309,11,310,12,311,13,312,14,313,15,309,16,314,17,315,18,316,19,307,21,305],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":10,"name":"Uint8Array$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["forEachItem","$Receiver","Uint8Array","Uint8Array.prototype.forEach","Uint8Array.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,317,2,319,4,320,6,322,7,320,8,325,9,326,10,327,11,328,12,329,13,330,14,331,15,327,16,332,17,333,18,334,19,325,21,323],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":11,"name":"forEachItem","flags":72,"length":4,"params":4,"locals":6,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,262411,3,327947,327690,262154,46,2097213,327690,10,33,1835069,131082,65546,10,327690,26,327690,10,838,5,327690,56,327691,720956,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,343,1,344,2,345,3,346,4,343,6,351,7,350,8,348,9,356,10,354,11,359,12,360,13,358,14,357,15,364,16,365,17,363,18,362,19,370,20,369,21,372,22,373,23,371,24,374,25,375,26,367,28,378,29,377,30,378,31,357,32,341],"localNames":["array","callback","thisArg","label","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":12,"name":"Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.some","Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,379,2,381,4,382,6,384,7,382,8,387,9,388,10,389,11,390,12,391,13,392,14,393,15,389,16,394,17,395,18,396,19,397,20,387,21,386],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":13,"name":"Uint8Array$prototype$some","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.some","Uint8Array.some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,398,2,400,4,401,6,403,7,401,8,406,9,407,10,408,11,409,12,410,13,411,14,412,15,408,16,413,17,414,18,415,19,416,20,406,21,405],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":14,"name":"Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Array","Array.prototype.every","Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,417,2,419,4,420,6,422,7,420,8,425,9,426,10,427,11,428,12,429,13,430,14,431,15,427,16,432,17,433,18,434,19,435,20,425,21,424],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":15,"name":"Uint8Array$prototype$every","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["testItems","$Receiver","Uint8Array","Uint8Array.prototype.every","Uint8Array.every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,436,2,438,4,439,6,441,7,439,8,444,9,445,10,446,11,447,12,448,13,449,14,450,15,446,16,451,17,452,18,453,19,454,20,444,21,443],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":16,"name":"testItems","flags":72,"length":5,"params":5,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,327947,3,393483,393226,327690,46,2490429,393226,10,33,1966399,131082,65546,10,393226,26,393226,10,838,54,262154,50,2228285,262154,54,67,393226,56,393227,720956,262154,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,464,1,465,2,466,3,467,4,464,6,472,7,471,8,469,9,477,10,475,11,480,12,481,13,479,14,478,15,486,16,487,17,485,18,484,19,493,20,492,21,495,22,496,23,494,24,497,25,498,26,490,27,489,28,499,29,488,30,483,31,502,32,501,33,500,34,505,35,504,36,505,37,478,38,507,39,506],"localNames":["array","callback","thisArg","label","every","length","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":17,"name":"Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.find","Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,508,2,510,4,511,6,513,7,511,8,516,9,517,10,518,11,519,12,520,13,521,14,522,15,518,16,523,17,524,18,525,19,526,20,527,21,516,22,515],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":18,"name":"Uint8Array$prototype$find","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.find","Uint8Array.find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,528,2,530,4,531,6,533,7,531,8,536,9,537,10,538,11,539,12,540,13,541,14,542,15,538,16,543,17,544,18,545,19,546,20,547,21,536,22,535],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":19,"name":"Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findIndex","Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,548,2,550,4,551,6,553,7,551,8,556,9,557,10,558,11,559,12,560,13,561,14,562,15,558,16,563,17,564,18,565,19,566,20,567,21,556,22,555],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":20,"name":"Uint8Array$prototype$findIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findIndex","Uint8Array.findIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,568,2,570,4,571,6,573,7,571,8,576,9,577,10,578,11,579,12,580,13,581,14,582,15,578,16,583,17,584,18,585,19,586,20,587,21,576,22,575],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":21,"name":"Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLast","Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,588,2,590,4,591,6,593,7,591,8,596,9,597,10,598,11,599,12,600,13,601,14,602,15,598,16,603,17,604,18,605,19,606,20,607,21,596,22,595],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":22,"name":"Uint8Array$prototype$findLast","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,2,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLast","Uint8Array.findLast"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,608,2,610,4,611,6,613,7,611,8,616,9,617,10,618,11,619,12,620,13,621,14,622,15,618,16,623,17,624,18,625,19,626,20,627,21,616,22,615],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":23,"name":"Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Array","Array.prototype.findLastIndex","Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,628,2,630,4,631,6,633,7,631,8,636,9,637,10,638,11,639,12,640,13,641,14,642,15,638,16,643,17,644,18,645,19,646,20,647,21,636,22,635],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":24,"name":"Uint8Array$prototype$findLastIndex","flags":592,"length":1,"params":2,"locals":3,"maxStack":8,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,258,1606,67],"consts":["findItem","$Receiver","Uint8Array","Uint8Array.prototype.findLastIndex","Uint8Array.findLastIndex"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,648,2,650,4,651,6,653,7,651,8,656,9,657,10,658,11,659,12,660,13,661,14,662,15,658,16,663,17,664,18,665,19,666,20,667,21,656,22,655],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":25,"name":"findItem","flags":72,"length":6,"params":6,"locals":10,"maxStack":5,"code":[0,20,65546,196618,582,5,10,65561,393483,262154,917565,65539,53,983099,65539,459019,262154,1441853,393226,65539,44,1507387,3,524555,0,590091,524298,3,49,2163007,524298,393226,46,3670077,10,524298,26,589835,131082,65546,589834,524298,10,838,3342397,327690,3211325,524298,3276859,589834,67,524298,458762,43,524299,1703996,327690,3997757,65539,53,4063291,0,67],"consts":["$Callback","length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,678,1,679,2,680,3,681,4,678,6,686,7,685,8,683,9,692,10,691,11,694,12,693,13,691,14,695,15,689,16,700,17,699,18,702,19,703,20,701,21,699,22,704,23,697,24,706,26,711,27,712,28,710,29,709,30,714,31,715,32,713,33,708,34,721,35,722,36,720,37,719,38,727,39,726,40,728,41,729,42,730,43,724,44,723,45,733,46,732,47,734,48,732,49,735,50,731,51,738,52,739,53,737,54,738,55,708,56,742,57,741,58,744,59,743,60,741,61,745,62,740],"localNames":["array","callback","thisArg","label","last","wantIndex","length","step","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":26,"name":"Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduce","Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,746,2,748,4,749,6,753,7,754,8,755,9,756,10,757,11,758,12,759,13,755,14,760,15,761,16,762,17,763,18,753,19,752],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":27,"name":"Uint8Array$prototype$reduce","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,2,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduce","Uint8Array.reduce"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,764,2,766,4,767,6,771,7,772,8,773,9,774,10,775,11,776,12,777,13,773,14,778,15,779,16,780,17,781,18,771,19,770],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":28,"name":"Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Array","Array.prototype.reduceRight","Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,782,2,784,4,785,6,789,7,790,8,791,9,792,10,793,11,794,12,795,13,791,14,796,15,797,16,798,17,799,18,789,19,788],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":29,"name":"Uint8Array$prototype$reduceRight","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,65813,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,258,1350,67],"consts":["reduceItems","$Receiver","Uint8Array","Uint8Array.prototype.reduceRight","Uint8Array.reduceRight"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,800,2,802,4,803,6,807,7,808,8,809,9,810,10,811,11,812,12,813,13,809,14,814,15,815,16,816,17,817,18,807,19,806],"localNames":["this","callback","initial"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":30,"name":"reduceItems","flags":72,"length":5,"params":5,"locals":9,"maxStack":6,"code":[0,20,65546,196618,582,5,10,65561,327947,262154,917565,65539,53,983099,65539,393483,262154,1441853,327690,65539,44,1507387,3,459019,0,524555,131082,65561,3,48,2359357,131082,3,26,524299,5111867,458762,3,49,2818367,458762,327690,46,3146047,458762,10,33,54,3539005,458762,393226,43,458763,2359356,458762,3,46,3997759,458762,327690,49,4587581,0,131092,196612,196618,262148,43,582,5,10,458762,26,524299,458762,393226,43,458763,458762,3,49,5570879,458762,327690,46,6881341,458762,10,33,6553661,0,65546,524298,10,458762,26,458762,10,1094,524299,458762,393226,43,458763,5111868,524298,67],"consts":["$Callback","length","$Throw","TypeError"," of an empty array with no initial value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,827,1,828,2,829,3,830,4,827,6,835,7,834,8,832,9,841,10,840,11,843,12,842,13,840,14,844,15,838,16,849,17,848,18,851,19,852,20,850,21,848,22,853,23,846,24,855,26,860,27,859,28,862,29,858,30,857,31,867,32,868,33,866,34,865,35,857,36,874,37,875,38,873,39,872,40,877,41,878,42,876,43,871,44,881,45,882,46,880,47,879,48,870,49,885,50,886,51,884,52,885,53,870,54,890,55,891,56,889,57,888,58,893,59,894,60,892,61,887,62,896,63,897,64,898,65,900,66,901,67,899,68,896,70,906,71,907,72,905,73,904,74,910,75,911,76,909,77,910,78,915,79,916,80,914,81,913,82,918,83,919,84,917,85,912,86,923,87,924,88,922,89,921,90,931,91,930,92,932,93,934,94,935,95,933,96,936,97,937,98,928,99,927,100,940,101,941,102,939,103,940,104,912,105,943,106,942],"localNames":["array","callback","initial","label","right","length","step","index","accumulator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":31,"name":"Array$prototype$flatMap","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,10,65540,131076,838,196875,0,196628,65546,262148,582,5,196618,327705,262411,36,327947,3,393483,3,459019,0,524555,0,590091,458762,262154,46,6684733,458762,196618,33,6422589,131082,65546,196618,458762,26,458762,196618,838,524299,0,393236,524298,326,5636157,3,589835,589834,524298,327705,46,5570621,589834,524298,33,5308477,327690,393226,55,7,56,393227,31,32,543,524298,589834,26,28,5,589834,56,589835,3801148,6422587,327690,393226,55,7,56,393227,31,32,543,524298,28,5,458762,56,458763,2228284,327690,67],"consts":["$Receiver","Array","Array.prototype.flatMap","$Callback","Array.flatMap","length","$IsArray"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,944,2,946,4,947,6,949,7,947,8,954,9,955,10,956,11,957,12,958,13,954,14,952,15,960,16,961,17,962,18,963,19,960,21,968,22,967,23,965,24,973,25,971,26,977,27,975,28,981,29,979,30,983,32,986,34,990,35,991,36,989,37,988,38,995,39,996,40,994,41,993,42,1004,43,1003,44,1006,45,1007,46,1005,47,1008,48,1009,49,1001,50,1000,51,1011,52,1012,53,1013,54,1011,55,1010,56,1018,57,1017,58,1021,59,1023,60,1022,61,1020,62,1019,63,1028,64,1029,65,1027,66,1026,67,1033,68,1035,70,1034,72,1035,73,1032,74,1034,75,1032,76,1037,77,1038,78,1036,79,1032,80,1031,81,1041,82,1040,83,1041,84,1019,85,1010,86,1045,87,1047,89,1046,91,1047,92,1044,93,1046,94,1044,95,1048,96,1044,97,1043,98,1051,99,1050,100,1051,101,988,102,1053,103,1052],"localNames":["this","callback","thisArg","array","length","result","count","index","inner","at"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":32,"name":"sortWith","flags":72,"length":7,"params":7,"locals":15,"maxStack":11,"code":[36,459019,3,524555,0,590091,0,655627,0,721163,0,786699,0,852235,0,917771,524298,393226,25,46,7733309,393226,524298,26,720907,393226,524298,65539,43,26,589835,393226,524298,131075,43,26,655371,589834,786443,3,851979,720906,589834,46,3146047,786442,655370,46,6488125,0,196618,262154,720906,26,262154,786442,26,582,309,917515,917514,3,47,4390975,917514,917514,51,5374013,458762,851978,31,32,543,262154,720906,26,28,5,720906,56,720907,6225979,458762,851978,31,32,543,262154,786442,26,28,5,786442,56,786443,851978,56,851979,2687036,0,65556,262154,458762,851978,720906,589834,786442,655370,393226,524298,26,2118,5,524298,196611,43,524299,1048636,0,131092,10,65546,131082,262154,327690,1350,67],"consts":["length","$MergeRest","$Sorted"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1067,1,1065,2,1071,3,1069,4,1073,6,1076,8,1079,10,1082,12,1085,14,1088,16,1092,17,1094,18,1093,19,1091,20,1090,21,1101,22,1102,23,1100,24,1099,25,1107,26,1109,27,1110,28,1108,29,1106,30,1105,31,1115,32,1117,33,1118,34,1116,35,1114,36,1113,37,1122,38,1121,39,1126,40,1125,41,1130,42,1131,43,1129,44,1128,45,1133,46,1134,47,1132,48,1127,49,1143,50,1142,51,1145,52,1146,53,1144,54,1148,55,1149,56,1147,57,1140,58,1139,59,1138,60,1153,61,1154,62,1152,63,1151,64,1156,65,1157,66,1155,67,1150,68,1162,69,1163,70,1161,71,1163,72,1161,73,1165,74,1166,75,1164,76,1161,77,1160,78,1169,79,1168,80,1169,81,1150,82,1174,83,1175,84,1173,85,1175,86,1173,87,1177,88,1178,89,1176,90,1173,91,1172,92,1181,93,1180,94,1181,95,1184,96,1183,97,1184,98,1127,99,1186,100,1187,101,1188,102,1189,103,1190,104,1191,105,1192,106,1193,107,1194,108,1196,109,1197,110,1195,111,1186,113,1200,114,1201,115,1199,116,1200,117,1090,118,1203,119,1204,120,1205,121,1206,122,1207,123,1208,124,1209,125,1203,126,1202],"localNames":["target","into","holes","comparator","work","undefs","merges","merged","merge","middle","high","left","right","count","order"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":33,"name":"sortDefault","flags":72,"length":5,"params":5,"locals":6,"maxStack":9,"code":[0,20,196618,326,327947,0,65556,327690,326,720957,327740,0,131092,10,65546,131082,0,196628,327690,196618,582,262154,1350,67],"consts":["$SortState","$SortStep","$Sorted","$SortResult"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1221,1,1222,2,1223,3,1221,4,1219,5,1225,6,1226,7,1227,8,1225,9,1224,11,1230,12,1231,13,1232,14,1233,15,1234,16,1235,17,1236,18,1237,19,1238,20,1235,21,1239,22,1230,23,1229],"localNames":["target","into","holes","values","undefs","state"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":34,"name":"Array$prototype$toLocaleString","flags":88,"length":0,"params":0,"locals":5,"maxStack":5,"code":[789,267,0,20,10,65540,131076,838,65803,196612,131339,3,196875,0,262411,196618,65546,262169,46,3473469,196618,3,48,1835069,131082,327684,43,131083,65546,196618,26,262155,262154,1,51,2556223,262154,0,51,3211325,131082,0,393236,262154,459033,70,326,43,131083,196618,56,196619,983100,131082,67],"consts":["$Receiver","Array","Array.prototype.toLocaleString","","length",",","String","toLocaleString"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1240,2,1246,3,1247,4,1248,5,1249,6,1250,7,1246,8,1244,9,1254,10,1252,11,1258,12,1256,13,1260,15,1264,16,1266,17,1265,18,1263,19,1262,20,1271,21,1272,22,1270,23,1269,24,1275,25,1276,26,1274,27,1275,28,1281,29,1282,30,1280,31,1279,32,1286,33,1287,34,1285,35,1284,36,1289,37,1290,38,1288,39,1283,40,1293,41,1294,42,1295,43,1298,44,1297,45,1296,46,1294,47,1292,48,1293,49,1302,50,1301,51,1302,52,1262,53,1304,54,1303],"localNames":["this","array","result","index","item"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":35,"name":"fillCollection","flags":72,"length":3,"params":3,"locals":7,"maxStack":7,"code":[131082,4,50,458815,131082,65540,50,196875,262156,65546,3801162,3801147,327947,4719948,3997755,262156,262411,196618,54,1835069,0,131092,196612,10,262154,838,5,3735611,0,262164,262154,326,54,2949181,0,327700,393220,458756,131082,43,524292,43,582,5,3735611,0,131092,196612,10,262154,3,26,262154,65539,26,1094,5,852028,70,75,786491,70,4719949,983099,393483,4457806,70,5,393226,64,5,4456507,10,67],"consts":["Map","WeakMap","$Table","add","$IsObject","$Throw","TypeError","new ","(...) expects [key, value] pairs as entry objects."],"captures":[],"handlers":[15,58,64,0,65,68,70,0],"finallies":[],"finallyOffsets":[],"sites":[0,1316,1,1317,2,1315,3,1314,4,1319,5,1320,6,1318,7,1312,9,1325,10,1321,16,1322,17,1329,18,1328,19,1327,20,1331,21,1332,22,1333,23,1334,24,1335,25,1331,27,1327,28,1338,29,1339,30,1340,31,1338,32,1337,33,1336,34,1342,35,1343,36,1344,37,1347,38,1348,39,1346,40,1349,41,1345,42,1342,44,1336,45,1351,46,1352,47,1353,48,1354,49,1356,50,1357,51,1355,52,1359,53,1360,54,1358,55,1351,57,1321,72,1362,73,1361],"localNames":["target","items","kind","pairs","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":36,"name":"Map$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Map"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1369,1,1370,2,1371,3,1372,4,1373,5,1369,6,1368],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":37,"name":"Object$groupBy","flags":72,"length":2,"params":2,"locals":2,"maxStack":5,"code":[0,20,10,65546,65540,838,67],"consts":["groupBy","Object"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1380,1,1381,2,1382,3,1383,4,1384,5,1380,6,1379],"localNames":["items","callback"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":38,"name":"groupBy","flags":72,"length":3,"params":3,"locals":9,"maxStack":6,"code":[10,1,50,458815,10,0,50,1048637,0,20,65540,131082,131076,43,582,5,0,196628,65546,131082,262148,43,582,5,0,327700,393220,131082,582,196875,3,262411,0,327947,393228,10,4915274,4915259,459019,5834572,5111867,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,458756,50,4325437,0,524308,0,327700,589828,327690,582,326,327691,0,327700,655364,196618,327690,393226,1094,5,2555964,70,75,2490427,70,5834573,2687035,524555,5572430,70,5,524298,64,5,5570619,196618,67],"consts":["$Throw","TypeError",".groupBy expects an iterable collection.","$Callback",".groupBy","$Table","groups","Object","String","groupKey","group"],"captures":[],"handlers":[41,75,81,0,82,85,87,0],"finallies":[],"finallyOffsets":[],"sites":[0,1394,1,1395,2,1393,3,1392,4,1397,5,1398,6,1396,7,1391,8,1400,9,1401,10,1402,11,1404,12,1405,13,1403,14,1400,16,1407,17,1408,18,1409,19,1411,20,1412,21,1410,22,1407,24,1416,25,1417,26,1418,27,1419,28,1416,29,1414,30,1423,31,1421,32,1425,35,1431,36,1427,42,1428,43,1436,44,1437,45,1438,46,1440,48,1439,50,1440,51,1436,52,1435,53,1443,54,1444,55,1442,56,1441,57,1448,58,1449,59,1450,60,1451,61,1452,62,1453,63,1450,64,1448,65,1447,66,1455,67,1456,68,1457,69,1458,70,1459,71,1460,72,1455,74,1427,89,1462,90,1461],"localNames":["items","callback","kind","groups","index","key","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":39,"name":"insertComputed","flags":72,"length":3,"params":3,"locals":4,"maxStack":6,"code":[0,131082,65546,326,196875,0,20,65540,10,65546,196618,1094,5,196618,67],"consts":["$Table","add"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1472,1,1473,2,1474,3,1472,4,1470,5,1476,6,1477,7,1478,8,1479,9,1480,10,1481,11,1476,13,1483,14,1482],"localNames":["target","key","callback","value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":40,"name":"Map$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Map","Map.prototype.forEach","Map.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1484,2,1486,4,1487,6,1489,7,1487,8,1492,9,1493,10,1494,11,1495,12,1496,13,1497,14,1498,15,1494,16,1499,17,1500,18,1501,19,1492,21,1490],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":41,"name":"Set$prototype$forEach","flags":592,"length":1,"params":2,"locals":3,"maxStack":7,"code":[789,267,1045,65803,66581,459583,0,131339,0,20,0,65556,10,131076,196612,838,65546,131082,262148,1094,5,0,67],"consts":["collectionForEach","$Receiver","Set","Set.prototype.forEach","Set.forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1502,2,1504,4,1505,6,1507,7,1505,8,1510,9,1511,10,1512,11,1513,12,1514,13,1515,14,1516,15,1512,16,1517,17,1518,18,1519,19,1510,21,1508],"localNames":["this","callback","thisArg"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":42,"name":"collectionForEach","flags":72,"length":4,"params":4,"locals":7,"maxStack":5,"code":[0,20,65546,196618,582,5,262156,0,65556,131076,10,582,2031690,2031675,327947,2950476,2228283,262156,262411,131082,65546,262154,65539,26,262154,3,26,10,838,5,983100,70,75,917563,70,2950477,1114171,393483,2688334,70,5,393226,64,5,2687035,0,67],"consts":["$Callback","$Table","entries"],"captures":[],"handlers":[17,31,37,0,38,41,43,0],"finallies":[],"finallyOffsets":[],"sites":[0,1528,1,1529,2,1530,3,1531,4,1528,7,1536,8,1537,9,1538,10,1539,11,1536,12,1532,18,1533,19,1544,20,1543,21,1546,22,1547,23,1545,24,1549,25,1550,26,1548,27,1551,28,1541,30,1532,45,1526],"localNames":["target","callback","thisArg","label","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":43,"name":"Set$prototype$union","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.union","union"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1552,2,1557,3,1558,4,1559,5,1560,6,1561,7,1562,8,1563,9,1559,10,1564,11,1565,12,1557,13,1556],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":44,"name":"Set$prototype$intersection","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.intersection","intersection"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1566,2,1571,3,1572,4,1573,5,1574,6,1575,7,1576,8,1577,9,1573,10,1578,11,1579,12,1571,13,1570],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":45,"name":"Set$prototype$difference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.difference","difference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1580,2,1585,3,1586,4,1587,5,1588,6,1589,7,1590,8,1591,9,1587,10,1592,11,1593,12,1585,13,1584],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":46,"name":"Set$prototype$symmetricDifference","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.symmetricDifference","symmetricDifference"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1594,2,1599,3,1600,4,1601,5,1602,6,1603,7,1604,8,1605,9,1601,10,1606,11,1607,12,1599,13,1598],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":47,"name":"Set$prototype$isSubsetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSubsetOf","isSubsetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1608,2,1613,3,1614,4,1615,5,1616,6,1617,7,1618,8,1619,9,1615,10,1620,11,1621,12,1613,13,1612],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":48,"name":"Set$prototype$isSupersetOf","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isSupersetOf","isSupersetOf"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1622,2,1627,3,1628,4,1629,5,1630,6,1631,7,1632,8,1633,9,1629,10,1634,11,1635,12,1627,13,1626],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":49,"name":"Set$prototype$isDisjointFrom","flags":88,"length":1,"params":1,"locals":2,"maxStack":7,"code":[789,65803,0,20,0,65556,65546,131076,196612,838,10,262148,838,67],"consts":["setMethod","$Receiver","Set","Set.prototype.isDisjointFrom","isDisjointFrom"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,1636,2,1641,3,1642,4,1643,5,1644,6,1645,7,1646,8,1647,9,1643,10,1648,11,1649,12,1641,13,1640],"localNames":["other","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":50,"name":"setMethod","flags":72,"length":3,"params":3,"locals":18,"maxStack":5,"code":[0,20,65546,326,54,1048637,0,65556,131076,196612,131082,43,262148,43,582,5,65546,327705,309,196875,196618,196618,51,2228285,0,65556,131076,196612,131082,43,393220,43,582,5,196618,3,46,3145789,0,65556,458756,196612,131082,43,524292,43,582,5,65546,589849,262411,65546,655385,327947,0,720916,262154,326,54,4259903,0,720916,327690,326,54,4980797,0,65556,131076,196612,131082,43,786436,43,582,5,0,851988,327684,10,582,393483,131082,917508,50,5833023,393226,196618,48,6029373,2,67,131082,983044,50,6488383,393226,196618,46,6684733,2,67,131082,1048580,50,7209021,1114132,65352,72,8454203,131082,1179652,50,7667775,131082,1245188,50,7929919,131082,1310724,50,8388669,0,851988,1376260,10,582,8454203,0,459019,131082,917508,50,9764927,131082,1048580,50,9240639,131082,1245188,50,9502783,131082,1441796,50,9765183,393226,196618,47,524555,0,590091,524298,16056381,720908,0,851988,1507332,131082,1245188,50,10747965,458762,10813499,10,582,14614602,14614587,852235,15535436,14811195,720908,721163,65546,262154,720906,326,54,54,589835,131082,1048580,50,12714045,589834,12648509,0,851988,1572868,458762,720906,838,5,14549051,131082,1245188,50,13631549,589834,13566013,0,851988,1638404,458762,720906,838,5,14549051,589834,131082,1441796,50,50,14549053,2,983307,14421326,70,1611,5,983050,67,11075644,70,75,11010107,70,15535437,11206715,917771,15273294,70,5,917514,64,5,15269947,458762,0,50,15925309,258,15990843,458762,67,65546,327690,70,655627,0,20,655370,326,54,17367101,0,65556,131076,196612,131082,43,1703940,43,582,5,786444,0,1769492,655370,258,582,23199818,23199803,1048843,24121420,23396411,786444,786699,0,851988,589828,10,786442,838,589835,131082,1179652,50,19464253,0,851988,1572868,458762,786442,838,5,23134267,131082,1048580,50,20381757,589834,20316221,0,851988,1572868,458762,786442,838,5,23134267,131082,1245188,50,21168189,0,851988,1638404,458762,786442,838,5,23134267,131082,1310724,50,22216765,0,851988,589834,21823549,1638404,21889083,1572868,458762,786442,838,5,23134267,589834,131082,1441796,50,50,23134269,2,983307,23007310,70,1611,5,983050,67,17956924,70,75,17891387,70,24121421,18087995,1114379,23859278,70,5,1114122,64,5,23855163,458762,0,50,24510525,258,24576059,458762,67],"consts":["$IsObject","$Throw","TypeError","Set."," expects a Set-like object.","size"," received a Set-like object with an invalid size.","RangeError"," received a Set-like object with a negative size.","has","keys","$IsCallable"," expects callable 'has' and 'keys' methods.","$Table","isSubsetOf","isSupersetOf","intersection","Set","union","difference","symmetricDifference","copy","isDisjointFrom","values","add","delete"," expects 'keys' to return an iterator object.","$Direct"],"captures":[],"handlers":[171,223,229,0,230,233,235,0,276,354,360,0,361,364,366,0],"finallies":[],"finallyOffsets":[],"sites":[0,1658,1,1659,2,1660,3,1658,4,1657,5,1656,6,1662,7,1663,8,1664,9,1667,10,1668,11,1666,12,1669,13,1665,14,1662,16,1675,17,1674,18,1673,19,1671,20,1679,21,1680,22,1678,23,1677,24,1682,25,1683,26,1684,27,1687,28,1688,29,1686,30,1689,31,1685,32,1682,34,1692,35,1693,36,1691,37,1690,38,1695,39,1696,40,1697,41,1700,42,1701,43,1699,44,1702,45,1698,46,1695,48,1707,49,1706,50,1704,51,1713,52,1712,53,1710,54,1718,55,1719,56,1720,57,1718,58,1717,59,1716,60,1722,61,1723,62,1724,63,1722,64,1721,65,1715,66,1726,67,1727,68,1728,69,1731,70,1732,71,1730,72,1733,73,1729,74,1726,76,1737,77,1738,78,1739,79,1740,80,1737,81,1735,82,1744,83,1745,84,1743,85,1742,86,1747,87,1748,88,1746,89,1741,90,1750,91,1749,92,1754,93,1755,94,1753,95,1752,96,1757,97,1758,98,1756,99,1751,100,1760,101,1759,102,1766,103,1767,104,1765,105,1764,106,1769,107,1768,109,1764,110,1774,111,1775,112,1773,113,1772,114,1777,115,1778,116,1776,117,1771,118,1780,119,1781,120,1779,121,1770,122,1782,123,1783,124,1784,125,1785,126,1782,127,1770,128,1786,129,1762,130,1792,131,1793,132,1791,133,1790,134,1798,135,1799,136,1797,137,1796,138,1801,139,1802,140,1800,141,1795,142,1804,143,1805,144,1803,145,1794,146,1807,147,1808,148,1806,149,1788,150,1810,152,1813,153,1812,155,1819,156,1820,157,1821,158,1824,159,1825,160,1823,161,1822,162,1826,163,1822,164,1827,165,1819,166,1815,172,1816,173,1837,174,1836,175,1838,176,1834,177,1833,178,1832,179,1831,180,1841,181,1842,182,1840,183,1839,184,1845,185,1844,186,1847,187,1848,188,1849,189,1850,190,1851,191,1847,193,1839,194,1854,195,1855,196,1853,197,1852,198,1858,199,1857,200,1860,201,1861,202,1862,203,1863,204,1864,205,1860,207,1852,208,1867,209,1869,210,1870,211,1868,212,1866,213,1865,214,1872,215,1871,222,1815,237,1876,238,1877,239,1875,240,1874,241,1878,242,1874,243,1879,244,1873,245,1886,246,1885,247,1883,248,1881,249,1889,250,1890,251,1891,252,1889,253,1888,254,1887,255,1893,256,1894,257,1895,258,1898,259,1899,260,1897,261,1900,262,1896,263,1893,266,1905,267,1906,268,1907,269,1908,270,1905,271,1901,277,1902,278,1913,279,1914,280,1915,281,1916,282,1917,283,1913,284,1912,285,1920,286,1921,287,1919,288,1918,289,1923,290,1924,291,1925,292,1926,293,1927,294,1923,296,1918,297,1930,298,1931,299,1929,300,1928,301,1934,302,1933,303,1936,304,1937,305,1938,306,1939,307,1940,308,1936,310,1928,311,1943,312,1944,313,1942,314,1941,315,1946,316,1947,317,1948,318,1949,319,1950,320,1946,322,1941,323,1953,324,1954,325,1952,326,1951,327,1956,328,1957,329,1959,330,1958,331,1960,332,1958,333,1961,334,1962,335,1963,336,1956,338,1951,339,1966,340,1968,341,1969,342,1967,343,1965,344,1964,345,1971,346,1970,353,1901,368,1975,369,1976,370,1974,371,1973,372,1977,373,1973,374,1978,375,1972],"localNames":["target","other","name","size","has","keys","own","result","asks","found","iterator","item","item","%it","%e","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":51,"name":"Object$fromEntries","flags":72,"length":1,"params":1,"locals":5,"maxStack":7,"code":[35,65803,131084,10,2621514,2621499,196875,3539788,2818107,131084,131339,0,20,131082,258,582,54,1572925,0,65556,131076,196612,582,5,0,262164,65546,0,327700,131082,3,26,326,131082,65539,26,258,1094,5,458812,70,75,393275,70,3539789,589883,262411,3277646,70,5,262154,64,5,3276859,65546,67],"consts":["$IsObject","$Throw","TypeError","Object.fromEntries expects [key, value] entry objects.","$Define","String"],"captures":[],"handlers":[9,40,46,0,47,50,52,0],"finallies":[],"finallyOffsets":[],"sites":[0,1986,1,1984,3,1991,4,1987,10,1988,11,1995,12,1996,13,1997,14,1998,15,1995,16,1994,17,1993,18,2000,19,2001,20,2002,21,2003,22,2000,24,2005,25,2006,26,2007,27,2008,28,2009,29,2011,30,2012,31,2010,32,2008,33,2014,34,2015,35,2013,36,2016,37,2005,39,1987,54,2018,55,2017],"localNames":["entries","result","entry","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":52,"name":"Math$sumPrecise","flags":72,"length":1,"params":1,"locals":5,"maxStack":4,"code":[36,65803,131084,10,2097226,2097211,196875,3015500,2293819,131084,131339,131082,821,4,51,1441853,0,65556,131076,196612,582,5,65546,65546,262169,31,32,543,131082,28,5,458812,70,75,393275,70,3015501,589883,262411,2753358,70,5,262154,64,5,2752571,0,327700,65546,326,67],"consts":["number","$Throw","TypeError","Math.sumPrecise expects an iterable of numbers.","length","$SumPrecise"],"captures":[],"handlers":[9,32,38,0,39,42,44,0],"finallies":[],"finallyOffsets":[],"sites":[0,2026,1,2024,3,2031,4,2027,10,2028,11,2036,12,2035,13,2037,14,2034,15,2033,16,2039,17,2040,18,2041,19,2042,20,2039,22,2046,23,2048,24,2047,25,2045,26,2047,27,2045,28,2050,29,2045,30,2044,31,2027,46,2052,47,2053,48,2054,49,2052,50,2051],"localNames":["items","numbers","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":53,"name":"aggregateError","flags":72,"length":2,"params":2,"locals":6,"maxStack":6,"code":[36,131339,196620,65546,1376330,1376315,262411,2294860,1572923,196620,196875,131082,131082,25,31,32,543,196618,28,5,458812,70,75,393275,70,2294861,589883,327947,2032718,70,5,327690,64,5,2031675,0,65556,10,131076,131082,2,1094,67],"consts":["length","$Define","errors"],"captures":[],"handlers":[9,21,27,0,28,31,33,0],"finallies":[],"finallyOffsets":[],"sites":[0,2063,1,2061,3,2068,4,2064,10,2065,11,2072,12,2074,13,2073,14,2071,15,2073,16,2071,17,2076,18,2071,19,2070,20,2064,35,2078,36,2079,37,2080,38,2081,39,2082,40,2083,41,2078,42,2077],"localNames":["error","errors","list","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":54,"name":"collectBytes","flags":72,"length":6,"params":6,"locals":11,"maxStack":7,"code":[0,20,10,326,54,1376317,0,65556,131076,65546,196612,43,0,262164,10,326,43,327684,43,582,5,36,393483,524300,10,2752586,2752571,590091,3672396,2949179,524300,524555,393226,393226,393241,31,32,543,524298,28,5,1835068,70,75,1769531,70,3672397,1966139,655627,3410254,70,5,655370,64,5,3407931,3,459019,131082,0,51,5570621,458762,393226,393241,46,5570621,393226,458762,31,32,543,196618,131082,393226,458762,26,458762,582,28,5,458762,56,458763,4063292,0,458772,393226,262154,327690,838,67],"consts":["$IsIterable","$Throw","TypeError"," expects a Uint8Array, an array, or an iterable of numbers, received ","$Describe",".","length","$Bytes"],"captures":[],"handlers":[30,42,48,0,49,52,54,0],"finallies":[],"finallyOffsets":[],"sites":[0,2095,1,2096,2,2097,3,2095,4,2094,5,2093,6,2099,7,2100,8,2101,9,2105,10,2106,11,2104,12,2107,13,2108,14,2109,15,2107,16,2103,17,2110,18,2102,19,2099,21,2114,22,2112,24,2119,25,2115,31,2116,32,2123,33,2125,34,2124,35,2122,36,2124,37,2122,38,2127,39,2122,40,2121,41,2115,56,2131,57,2129,58,2134,59,2135,60,2133,61,2132,62,2139,63,2141,64,2140,65,2138,66,2137,67,2147,68,2148,69,2146,70,2148,71,2146,72,2152,73,2151,74,2154,75,2155,76,2153,77,2156,78,2149,79,2146,80,2145,81,2159,82,2158,83,2159,84,2137,85,2161,86,2162,87,2163,88,2164,89,2165,90,2161,91,2160],"localNames":["source","name","mapper","thisArg","into","offset","values","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":55,"name":"readPairs","flags":72,"length":4,"params":4,"locals":12,"maxStack":6,"code":[0,20,10,326,54,917565,0,65556,131082,196618,10,65546,1094,67,36,262411,0,327947,393228,10,5832778,5832763,524555,6752332,6029371,393228,393483,0,20,393226,326,54,2687037,0,131092,196612,65546,262148,43,582,5,36,327691,458764,393226,4259914,4259899,655627,5179980,4456507,458764,459019,327690,327690,327705,31,32,543,0,393236,458762,326,28,5,3145788,70,75,3080251,70,5179981,3276859,721163,4917838,70,5,720906,64,5,4915259,262154,262154,327705,31,32,543,327690,28,5,1507388,70,75,1441851,70,6752333,1638459,590091,6490190,70,5,589834,64,5,6488123,0,65556,131082,196618,262154,65546,1094,67],"consts":["$IsIterable","$Pairs","$Throw","TypeError"," expects iterable [name, value] pairs.","length","String"],"captures":[],"handlers":[50,65,71,0,72,75,77,0,25,89,95,0,96,99,101,0],"finallies":[],"finallyOffsets":[],"sites":[0,2175,1,2176,2,2177,3,2175,4,2174,5,2173,6,2179,7,2180,8,2181,9,2182,10,2183,11,2184,12,2179,13,2178,14,2188,15,2186,16,2190,19,2196,20,2192,26,2193,27,2200,28,2201,29,2202,30,2200,31,2199,32,2198,33,2204,34,2205,35,2206,36,2208,37,2209,38,2207,39,2204,41,2213,42,2212,44,2218,45,2214,51,2215,52,2222,53,2224,54,2223,55,2221,56,2223,57,2221,58,2226,59,2227,60,2228,61,2226,62,2221,63,2220,64,2214,79,2232,80,2234,81,2233,82,2231,83,2233,84,2231,85,2236,86,2231,87,2230,88,2192,103,2238,104,2239,105,2240,106,2241,107,2242,108,2243,109,2238,110,2237],"localNames":["init","label","kind","proto","pairs","strings","pair","item","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":56,"name":"pairsForEach","flags":72,"length":4,"params":4,"locals":5,"maxStack":5,"code":[3,262411,262154,65546,25,46,1703997,196618,131082,65546,262154,26,65539,26,65546,262154,26,3,26,10,838,5,262154,56,262155,131132,0,67],"consts":["length"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2254,1,2252,2,2257,3,2259,4,2258,5,2256,6,2255,7,2266,8,2265,9,2269,10,2270,11,2268,12,2271,13,2267,14,2274,15,2275,16,2273,17,2276,18,2272,19,2277,20,2263,22,2280,23,2279,24,2280,25,2255,26,2250],"localNames":["target","pairs","callback","thisArg","index"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":57,"name":"Iterator$from","flags":72,"length":1,"params":1,"locals":1,"maxStack":10,"code":[0,20,10,326,721215,0,65556,10,131092,196633,582,917565,10,67,0,262164,0,327700,10,0,393236,10,258,458756,838,582,326,67],"consts":["$IsObject","$Inherits","Iterator","prototype","prime","wrapHelper","flattenable","Iterator.from"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2287,1,2288,2,2289,3,2287,4,2286,5,2290,6,2291,7,2292,8,2294,9,2293,10,2290,11,2285,12,2297,13,2296,14,2299,15,2300,16,2301,17,2302,18,2303,19,2304,20,2305,21,2306,22,2307,23,2308,24,2304,25,2301,26,2299,27,2298],"localNames":["value"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":58,"name":"wrapHelper","flags":76,"length":2,"params":2,"locals":9,"maxStack":3,"code":[81,2,131339,0,82,786515,393483,393226,327947,131075,262411,3866683,5,258,131083,196620,65546,2555978,2555963,459019,3475276,2752571,196620,196875,196618,82,2424915,393483,2099022,70,1611,5,393226,327947,131075,262411,3866683,5,1310780,70,75,1245243,70,3475277,1441851,524555,3213134,70,5,524298,64,5,3211323,3,262411,3866683,327947,65539,262411,131082,54,4390973,0,20,10,326,5,66,327690,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,39,45,0,46,49,51,0,3,53,56,0],"finallies":[4,5,0,3,70,-1,68],"finallyOffsets":[0],"sites":[0,2309,1,2317,2,2315,3,2321,13,2325,14,2324,16,2330,17,2326,23,2327,24,2333,25,2332,38,2326,53,2318,59,2337,60,2336,61,2335,62,2339,63,2340,64,2341,65,2339,67,2318,70,2313],"localNames":["source","iterator","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":59,"name":"Iterator$prototype$map","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.map","prime","mapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2342,2,2349,3,2350,4,2351,5,2352,6,2349,7,2347,8,2354,9,2355,10,2356,11,2357,12,2354,14,2359,15,2360,16,2361,17,2362,18,2363,19,2364,20,2365,21,2361,22,2359,23,2358],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":60,"name":"mapHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4587579,5,258,196619,3,262411,327692,65546,3276874,3276859,590091,4196684,3473467,327692,327947,0,131082,327690,262154,55,7,56,262155,582,82,3145811,524555,2820430,70,1611,5,524298,459019,131075,393483,4587579,5,1507388,70,75,1441851,70,4196685,1638459,655627,3934542,70,5,655370,64,5,3932219,3,393483,4587579,459019,65539,393483,196618,54,5111869,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,50,56,0,57,60,62,0,3,64,67,0],"finallies":[6,7,0,3,81,-1,79],"finallyOffsets":[0],"sites":[0,2366,1,2375,2,2373,4,2379,14,2383,15,2382,16,2387,17,2385,19,2392,20,2388,26,2389,27,2395,28,2396,29,2397,30,2399,32,2398,34,2399,35,2395,36,2394,49,2388,64,2376,70,2403,71,2402,72,2401,73,2405,74,2406,75,2407,76,2405,78,2376,81,2371],"localNames":["source","iterator","mapper","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":61,"name":"Iterator$prototype$filter","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.filter","prime","filterHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2408,2,2415,3,2416,4,2417,5,2418,6,2415,7,2413,8,2420,9,2421,10,2422,11,2423,12,2420,14,2425,15,2426,16,2427,17,2428,18,2429,19,2430,20,2431,21,2427,22,2425,23,2424],"localNames":["predicate","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":62,"name":"filterHelper","flags":76,"length":3,"params":3,"locals":11,"maxStack":5,"code":[81,2,196875,262156,0,82,852051,524555,524298,459019,131075,393483,4718651,5,258,196619,3,262411,327692,65546,3407946,3407931,590091,4327756,3604539,327692,327947,0,131082,327690,262154,55,7,56,262155,582,3342397,327690,82,3276883,524555,2951502,70,1611,5,524298,459019,131075,393483,4718651,5,1507388,70,75,1441851,70,4327757,1638459,655627,4065614,70,5,655370,64,5,4063291,3,393483,4718651,459019,65539,393483,196618,54,5242941,0,20,10,326,5,66,458762,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[25,52,58,0,59,62,64,0,3,66,69,0],"finallies":[6,7,0,3,83,-1,81],"finallyOffsets":[0],"sites":[0,2432,1,2441,2,2439,4,2445,14,2449,15,2448,16,2453,17,2451,19,2458,20,2454,26,2455,27,2460,28,2461,29,2462,30,2464,32,2463,34,2464,35,2460,36,2459,37,2467,38,2466,51,2454,66,2442,72,2471,73,2470,74,2469,75,2473,76,2474,77,2475,78,2473,80,2442,83,2437],"localNames":["source","iterator","predicate","started","counter","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":63,"name":"Iterator$prototype$take","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","takeHelper","count","take"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2476,2,2483,3,2484,4,2485,5,2486,6,2483,7,2481,8,2488,9,2489,10,2490,11,2491,12,2492,13,2493,14,2494,15,2495,16,2496,17,2497,18,2494,19,2490,20,2488,21,2487],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":64,"name":"takeHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,5570619,5,258,196619,131082,3,50,1900605,0,20,10,326,459019,458762,393483,131075,327947,5570619,262156,65546,4259914,4259899,524555,5179468,4456507,262156,262411,131082,312,131083,262154,82,3539027,459019,3213390,70,1611,5,458762,393483,131075,327947,5570619,5,131082,3,50,4194365,4130894,70,1611,5,5177403,2228284,70,75,2162747,70,5179469,2359355,590091,4917326,70,5,589834,64,5,4915259,3,327947,5570619,393483,65539,327947,196618,54,6094909,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[36,65,71,0,72,75,77,0,3,79,82,0],"finallies":[5,6,0,3,96,-1,94],"finallyOffsets":[0],"sites":[0,2498,1,2507,2,2505,3,2511,13,2515,14,2514,15,2518,16,2519,17,2517,18,2516,19,2521,20,2522,21,2523,22,2521,23,2520,30,2528,31,2524,37,2525,38,2532,39,2531,40,2532,41,2535,42,2534,55,2538,56,2539,57,2537,58,2536,59,2540,64,2524,79,2508,85,2544,86,2543,87,2542,88,2546,89,2547,90,2548,91,2546,93,2508,96,2503],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":65,"name":"Iterator$prototype$drop","flags":88,"length":1,"params":1,"locals":3,"maxStack":10,"code":[789,65803,0,20,65546,2,582,131339,0,65556,0,131092,65546,131082,0,196628,10,262148,582,838,326,67],"consts":["$Direct","prime","dropHelper","count","drop"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2549,2,2556,3,2557,4,2558,5,2559,6,2556,7,2554,8,2561,9,2562,10,2563,11,2564,12,2565,13,2566,14,2567,15,2568,16,2569,17,2570,18,2567,19,2563,20,2561,21,2560],"localNames":["limit","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":66,"name":"dropHelper","flags":76,"length":3,"params":3,"locals":10,"maxStack":3,"code":[81,2,196875,0,82,786515,459019,458762,393483,131075,327947,4390971,5,258,196619,262156,65546,3080266,3080251,524555,3999820,3276859,262156,262411,131082,3,48,2097213,131082,312,131083,3014715,262154,82,2949203,459019,2623566,70,1611,5,458762,393483,131075,327947,4390971,5,1310780,70,75,1245243,70,3999821,1441851,590091,3737678,70,5,589834,64,5,3735611,3,327947,4390971,393483,65539,327947,196618,54,4915261,0,20,10,326,5,66,393226,67,0,67],"consts":["closeIterator"],"captures":[],"handlers":[22,47,53,0,54,57,59,0,3,61,64,0],"finallies":[5,6,0,3,78,-1,76],"finallyOffsets":[0],"sites":[0,2571,1,2580,2,2578,3,2584,13,2588,14,2587,16,2593,17,2589,23,2590,24,2597,25,2598,26,2596,27,2595,28,2601,29,2600,30,2601,31,2595,32,2604,33,2603,46,2589,61,2581,67,2608,68,2607,69,2606,70,2610,71,2611,72,2612,73,2610,75,2581,78,2576],"localNames":["source","iterator","remaining","started","item","%ftag0","%fval0","%ret","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":67,"name":"Iterator$prototype$flatMap","flags":88,"length":1,"params":1,"locals":3,"maxStack":7,"code":[789,65803,0,20,65546,2,582,131339,0,65556,10,131076,582,5,0,196628,0,262164,65546,131082,10,838,326,67],"consts":["$Direct","$Callback","Iterator.prototype.flatMap","prime","flatMapHelper"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2613,2,2620,3,2621,4,2622,5,2623,6,2620,7,2618,8,2625,9,2626,10,2627,11,2628,12,2625,14,2630,15,2631,16,2632,17,2633,18,2634,19,2635,20,2636,21,2632,22,2630,23,2629],"localNames":["mapper","this","iterator"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":68,"name":"flatMapHelper","flags":76,"length":3,"params":3,"locals":14,"maxStack":7,"code":[81,2,196875,262156,0,82,852051,590091,589834,524555,131075,459019,6750267,5,258,196619,3,262411,327692,65546,5439562,5439547,655627,6359628,5636155,327692,327947,393228,0,20,0,131082,327690,262154,55,7,56,262155,582,2,65540,838,4456522,4456507,786699,5377100,4653115,393228,393483,393226,82,4325459,590091,3738702,70,1611,5,4000334,70,1611,5,589834,524555,131075,459019,6750267,5,2949180,70,75,2883643,70,5377101,3080251,852235,5114958,70,5,851978,64,5,5111867,1507388,70,75,1441851,70,6359629,1638459,721163,6097486,70,5,720906,64,5,6094907,3,459019,6750267,524555,65539,459019,196618,54,7274557,0,131092,10,326,5,66,524298,67,0,67],"consts":["flattenable","Iterator.prototype.flatMap","closeIterator"],"captures":[],"handlers":[47,68,74,0,75,78,80,0,25,83,89,0,90,93,95,0,3,97,100,0],"finallies":[7,8,0,3,114,-1,112],"finallyOffsets":[0],"sites":[0,2637,1,2646,2,2644,4,2650,14,2654,15,2653,16,2658,17,2656,19,2663,20,2659,26,2660,28,2669,29,2670,30,2671,31,2672,32,2673,33,2675,35,2674,37,2675,38,2671,39,2676,40,2677,41,2669,42,2665,48,2666,49,2680,50,2679,67,2665,82,2659,97,2647,103,2684,104,2683,105,2682,106,2686,107,2687,108,2688,109,2686,111,2647,114,2642],"localNames":["source","iterator","mapper","started","counter","item","inner","%ftag0","%fval0","%ret","%it","%e","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":69,"name":"prime","flags":72,"length":1,"params":1,"locals":1,"maxStack":2,"code":[10,281,70,5,10,67],"consts":["next"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2696,1,2695,2,2694,4,2699,5,2698],"localNames":["helper"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":70,"name":"count","flags":72,"length":2,"params":2,"locals":4,"maxStack":5,"code":[10,309,131339,131082,20,50,720959,131082,20,53,50,917565,131082,1245243,131082,131082,65539,308,44,196875,131082,131082,51,1769535,196618,3,46,2752573,0,65556,131076,196612,65546,43,262148,43,131082,43,327684,43,582,5,196618,67],"consts":["Infinity","$Throw","RangeError","Iterator.prototype."," expects a non-negative count, received ","."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2709,1,2708,2,2706,3,2716,4,2717,5,2715,6,2714,7,2719,8,2721,9,2720,10,2718,11,2713,12,2722,13,2713,14,2724,15,2726,16,2727,17,2725,18,2723,19,2711,20,2731,21,2732,22,2730,23,2729,24,2734,25,2735,26,2733,27,2728,28,2737,29,2738,30,2739,31,2744,32,2745,33,2743,34,2746,35,2742,36,2747,37,2741,38,2748,39,2740,40,2737,42,2750,43,2749],"localNames":["limit","name","number","integer"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":71,"name":"flattenable","flags":72,"length":3,"params":3,"locals":4,"maxStack":7,"code":[10,821,4,50,524349,65546,54,852027,0,65556,10,326,54,1900605,0,131092,196612,131082,262148,43,0,327700,10,326,43,393220,43,582,5,10,458772,524313,26,196875,0,589844,196618,0,50,2818111,196618,1,50,3014717,10,3211323,10,196618,70,2,582,67],"consts":["string","$IsObject","$Throw","TypeError"," expects an iterable or iterator, received ","$Describe",".","Symbol","iterator","$Direct"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2761,1,2760,2,2762,3,2759,4,2758,5,2764,6,2763,7,2758,8,2766,9,2767,10,2768,11,2766,12,2765,13,2757,14,2770,15,2771,16,2772,17,2776,18,2777,19,2775,20,2778,21,2779,22,2780,23,2778,24,2774,25,2781,26,2773,27,2770,29,2786,30,2788,31,2787,32,2785,33,2783,34,2791,35,2792,36,2796,37,2797,38,2795,39,2794,40,2799,41,2800,42,2798,43,2793,44,2801,45,2793,46,2805,47,2804,48,2802,49,2806,50,2791,51,2790],"localNames":["value","strings","name","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":72,"name":"closeIterator","flags":72,"length":1,"params":1,"locals":2,"maxStack":4,"code":[10,25,65803,65546,0,51,655679,65546,1,51,1179967,0,65556,10,65546,70,326,54,1638461,0,131092,196612,262148,582,5,0,67],"consts":["return","$IsObject","$Throw","TypeError","Iterator return() must return an object."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2815,1,2814,2,2812,3,2821,4,2822,5,2820,6,2819,7,2824,8,2825,9,2823,10,2818,11,2827,12,2828,13,2832,14,2831,15,2829,16,2827,17,2826,18,2817,19,2834,20,2835,21,2836,22,2837,23,2834,25,2810],"localNames":["iterator","method"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":73,"name":"Iterator$prototype$toArray","flags":88,"length":0,"params":0,"locals":5,"maxStack":4,"code":[789,267,36,65803,131084,0,20,10,2,582,1769546,1769531,196875,2687820,1966139,131084,131339,65546,65546,65561,31,32,543,131082,28,5,852028,70,75,786491,70,2687821,983099,262411,2425678,70,5,262154,64,5,2424891,65546,67],"consts":["$Direct","length"],"captures":[],"handlers":[15,27,33,0,34,37,39,0],"finallies":[],"finallyOffsets":[],"sites":[0,2838,2,2844,3,2842,5,2849,6,2850,7,2851,8,2852,9,2849,10,2845,16,2846,17,2856,18,2858,19,2857,20,2855,21,2857,22,2855,23,2860,24,2855,25,2854,26,2845,41,2862,42,2861],"localNames":["this","result","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":74,"name":"Iterator$prototype$reduce","flags":592,"length":1,"params":2,"locals":10,"maxStack":5,"code":[789,267,1045,65803,65813,131339,0,20,10,2,582,196875,0,65556,65546,131076,582,5,131082,3,26,262411,131082,196633,3,48,327947,3,393483,458764,196618,3670090,3670075,524555,4589644,3866683,458764,459019,327690,3145789,0,65546,262154,458762,393226,838,262155,3276859,458762,262155,258,327691,393226,56,393227,2228284,70,75,2162747,70,4589645,2359355,590091,4327502,70,5,589834,64,5,4325435,327690,54,5177405,0,262164,327684,393220,582,5,262154,67],"consts":["$Direct","$Callback","Iterator.prototype.reduce","length","$Throw","TypeError","Iterator.prototype.reduce of an empty iterator with no initial value."],"captures":[],"handlers":[36,56,62,0,63,66,68,0],"finallies":[],"finallyOffsets":[],"sites":[0,2863,2,2865,4,2866,6,2872,7,2873,8,2874,9,2875,10,2872,11,2870,12,2877,13,2878,14,2879,15,2880,16,2877,18,2885,19,2886,20,2884,21,2882,22,2892,23,2891,24,2894,25,2890,26,2888,27,2898,28,2896,30,2903,31,2899,37,2900,38,2906,39,2905,40,2910,41,2911,42,2912,43,2913,44,2914,45,2910,46,2909,47,2905,48,2918,49,2917,50,2922,51,2921,52,2925,53,2924,54,2925,55,2899,70,2928,71,2927,72,2926,73,2930,74,2931,75,2932,76,2933,77,2930,79,2935,80,2934],"localNames":["this","callback","initial","iterator","accumulator","started","index","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":75,"name":"Iterator$prototype$forEach","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,5,0,67],"consts":["iteratorTest","forEach"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2936,2,2941,3,2942,4,2943,5,2944,6,2945,7,2941,9,2939],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":76,"name":"Iterator$prototype$some","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","some"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2946,2,2951,3,2952,4,2953,5,2954,6,2955,7,2951,8,2950],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":77,"name":"Iterator$prototype$every","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","every"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2956,2,2961,3,2962,4,2963,5,2964,6,2965,7,2961,8,2960],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":78,"name":"Iterator$prototype$find","flags":88,"length":1,"params":1,"locals":2,"maxStack":5,"code":[789,65803,0,20,65546,10,65540,838,67],"consts":["iteratorTest","find"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,2966,2,2971,3,2972,4,2973,5,2974,6,2975,7,2971,8,2970],"localNames":["callback","this"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":79,"name":"iteratorTest","flags":72,"length":3,"params":3,"locals":10,"maxStack":5,"code":[0,20,10,2,582,196875,0,65556,65546,131076,131082,43,582,5,3,262411,0,327947,393228,196618,5308490,5308475,459019,6227788,5505083,393228,393483,0,65546,393226,262154,55,7,56,262155,582,327691,131082,196612,50,2752831,327690,3342397,258,590091,3213134,70,1611,5,589834,67,131082,262148,50,3735871,327690,54,4325437,2,590091,4196174,70,1611,5,589834,67,131082,327684,50,4653375,327690,5242941,393226,590091,5113678,70,1611,5,589834,67,1507388,70,75,1441851,70,6227789,1638459,524555,5965646,70,5,524298,64,5,5963835,131082,262148,50,6619197,258,7077947,131082,196612,50,7012413,2,7077947,0,67],"consts":["$Direct","$Callback","Iterator.prototype.","some","every","find"],"captures":[],"handlers":[25,81,87,0,88,91,93,0],"finallies":[],"finallyOffsets":[],"sites":[0,2985,1,2986,2,2987,3,2988,4,2985,5,2983,6,2990,7,2991,8,2992,9,2994,10,2995,11,2993,12,2990,14,2999,15,2997,16,3001,19,3007,20,3003,26,3004,27,3012,28,3013,29,3014,30,3016,32,3015,34,3016,35,3012,36,3011,37,3020,38,3021,39,3019,40,3018,41,3022,42,3017,43,3024,44,3023,51,3028,52,3029,53,3027,54,3026,55,3031,56,3030,57,3025,58,3033,59,3032,66,3037,67,3038,68,3036,69,3035,70,3039,71,3034,72,3041,73,3040,80,3003,95,3045,96,3046,97,3044,98,3043,99,3047,100,3043,101,3050,102,3051,103,3049,104,3048,105,3052,106,3048,107,3053,108,3042],"localNames":["self","callback","name","iterator","index","result","item","%it","%e","%ret"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":80,"name":"revive","flags":72,"length":3,"params":3,"locals":7,"maxStack":5,"code":[10,65546,26,196875,0,20,196618,326,3932221,262156,327692,393228,0,65556,196618,326,262411,3,327947,0,393483,327690,262154,131097,46,3932221,0,196628,196618,262154,327690,26,131082,838,393227,393226,0,50,3014717,196618,262154,327690,26,30,5,3670075,196618,262154,327690,26,31,32,543,393226,28,5,327690,56,327691,1376316,10,131082,65546,196618,582,67],"consts":["$IsObject","$Keys","length","revive"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3064,1,3065,2,3063,3,3061,4,3067,5,3068,6,3069,7,3067,8,3066,12,3074,13,3075,14,3076,15,3074,16,3072,17,3080,18,3078,19,3082,21,3086,22,3088,23,3087,24,3085,25,3084,26,3094,27,3095,28,3096,29,3098,30,3099,31,3097,32,3100,33,3094,34,3093,35,3103,36,3104,37,3102,38,3101,39,3108,40,3110,41,3111,42,3109,43,3107,44,3106,45,3101,46,3115,47,3117,48,3118,49,3116,50,3114,51,3116,52,3114,53,3119,54,3114,55,3113,56,3122,57,3121,58,3122,59,3084,60,3127,61,3126,62,3128,63,3129,64,3124,65,3123],"localNames":["holder","key","reviver","value","names","index","revived"],"upvalNames":[]},{"source":{"id":"@prelude","hash":"a7f6f842e15eb031"},"index":81,"name":"replaceWith","flags":72,"length":3,"params":3,"locals":6,"maxStack":9,"code":[4,196875,3,262411,262154,10,65561,46,3211325,196618,10,262154,26,3,26,0,131092,0,131082,36,10,262154,26,65539,26,2228810,2228283,327947,1967439,2621499,2622796,2424891,37,1966139,70,587,1769531,70,2622797,2097211,71,326,43,43,196619,262154,56,262155,262204,196618,65546,43,67],"consts":["","length","String"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,3139,1,3137,2,3143,3,3141,4,3146,5,3148,6,3147,7,3145,8,3144,9,3153,10,3157,11,3158,12,3156,13,3159,14,3155,15,3160,16,3161,17,3162,18,3163,19,3162,20,3167,21,3168,22,3166,23,3169,24,3165,25,3164,40,3162,41,3160,42,3154,43,3152,44,3153,45,3172,46,3171,47,3172,48,3144,49,3175,50,3176,51,3174,52,3173],"localNames":["matches","rest","callback","result","index","%it"],"upvalNames":[]}]},"@vm-internal":{"text":"8c2dcf90ea103eb5","codes":[{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":0,"name":"","flags":202,"length":0,"params":0,"locals":0,"maxStack":1,"code":[0,87],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,0],"localNames":[],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":1,"name":"$vmToPrimitive","flags":72,"length":2,"params":2,"locals":8,"maxStack":4,"code":[0,20,10,326,54,524349,10,67,65546,65540,50,1310783,65546,131076,50,1311039,0,196628,10,326,1507389,262148,1572923,327684,131339,131082,262148,50,2031677,327684,2097211,262148,196875,0,393236,10,131082,582,262411,262154,0,51,3670077,393228,10,262154,70,393483,0,458772,393226,326,54,3670077,393226,67,0,393236,10,196618,582,327947,327690,0,51,5177405,458764,10,327690,70,459019,0,458772,458762,326,54,5177405,458762,67,0,524308,589828,655364,582,5,0,67],"consts":["$IsDataObj","string","default","$IsDate","toString","valueOf","$GetMethod","$IsObj","$Throw","TypeError","Cannot convert object to primitive value."],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,8,1,9,2,10,3,8,4,7,5,6,6,12,7,11,8,19,9,20,10,18,11,17,12,23,13,24,14,22,15,21,16,25,17,26,18,27,19,25,20,16,21,28,22,16,23,29,24,14,25,35,26,36,27,34,28,33,29,37,30,33,31,38,32,31,33,42,34,43,35,44,36,45,37,42,38,40,39,48,40,49,41,47,42,46,44,57,45,56,46,54,47,52,48,60,49,61,50,62,51,60,52,59,53,58,54,64,55,63,56,68,57,69,58,70,59,71,60,68,61,66,62,74,63,75,64,73,65,72,67,83,68,82,69,80,70,78,71,86,72,87,73,88,74,86,75,85,76,84,77,90,78,89,79,92,80,93,81,94,82,95,83,92,85,5],"localNames":["v","hint","first","second","m1","m2","r1","r2"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":2,"name":"$vmToKey","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$PropKey","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,101,1,102,2,103,3,104,4,105,5,106,6,103,7,101,8,100],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":3,"name":"$vmToString","flags":72,"length":1,"params":1,"locals":1,"maxStack":6,"code":[0,20,0,65556,10,131076,582,326,67],"consts":["$CoerceToString","$vmToPrimitive","string"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,112,1,113,2,114,3,115,4,116,5,117,6,114,7,112,8,111],"localNames":["v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":4,"name":"$vmToNumeric","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,124,1,125,2,126,3,127,4,128,5,129,6,130,7,127,8,124,9,123],"localNames":["incFlag","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":5,"name":"$vmInc","flags":72,"length":2,"params":2,"locals":3,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,131339,10,3,50,1179709,131082,65539,43,1376315,131082,65539,44,67],"consts":["$ApplyToNumeric","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,139,1,140,2,141,3,142,4,143,5,144,6,145,7,142,8,139,9,137,10,149,11,150,12,148,13,147,14,152,15,153,16,151,17,147,18,155,19,156,20,154,21,146],"localNames":["incFlag","v","n"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":6,"name":"$vmUnary","flags":72,"length":2,"params":2,"locals":2,"maxStack":7,"code":[0,20,10,0,65556,65546,131076,582,582,67],"consts":["$ApplyUnary","$vmToPrimitive","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,163,1,164,2,165,3,166,4,167,5,168,6,169,7,166,8,163,9,162],"localNames":["sub","v"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":7,"name":"$vmBinary","flags":72,"length":3,"params":3,"locals":8,"maxStack":5,"code":[0,20,10,65546,131082,838,196875,196618,3,50,1179709,0,65556,10,65546,131082,838,67,196618,65539,50,2359357,458764,0,131092,65546,196612,582,459019,0,65556,10,458762,131082,838,67,196618,131075,50,2752573,262148,2818107,327684,262411,0,131092,65546,262154,582,327947,0,131092,131082,262154,582,393483,0,65556,10,327690,393226,838,67],"consts":["$OpConvKind","$ApplyBinary","$vmToPrimitive","string","default","number"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,179,1,180,2,181,3,182,4,183,5,179,6,177,7,186,8,187,9,185,10,184,11,189,12,190,13,191,14,192,15,193,16,189,17,188,18,196,19,197,20,195,21,194,23,202,24,203,25,204,26,205,27,202,28,200,29,207,30,208,31,209,32,210,33,211,34,207,35,206,36,217,37,218,38,216,39,215,40,219,41,215,42,220,43,213,44,224,45,225,46,226,47,227,48,224,49,222,50,231,51,232,52,233,53,234,54,231,55,229,56,236,57,237,58,238,59,239,60,240,61,236,62,235],"localNames":["op","lhs","rhs","kind","hint","l","r","key"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":8,"name":"$vmGetElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,26,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,248,1,249,2,250,3,251,4,249,5,247,6,246],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":9,"name":"$vmDeleteElem","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[10,0,20,65546,326,30,67],"consts":["$vmToKey"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,260,1,261,2,262,3,263,4,261,5,259,6,257],"localNames":["o","k"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":10,"name":"$vmConvert","flags":72,"length":6,"params":6,"locals":16,"maxStack":7,"code":[258,7274557,393228,458764,524300,589836,655372,720908,786444,0,20,327690,326,393483,0,65556,327690,3,582,459019,0,65556,327690,65539,582,524555,393226,131097,590091,36,655627,3,721163,720906,589834,46,5439549,851980,917516,983052,393226,720906,26,852235,0,196628,327690,720906,582,917771,0,262164,851978,326,3997757,0,327700,851978,917514,582,4063291,851978,983307,655370,720906,31,32,543,458762,4980797,0,393236,917514,983050,582,5046331,983050,28,5,720906,56,720907,2162748,262154,524298,31,32,543,655370,28,5,0,458772,10,65546,131082,196618,262154,1350,786699,0,524308,786442,326,54,7077949,786442,67,786442,327691,60,0,67],"consts":["$ConvValues","$ConvFlag","length","$ConvHint","$IsObj","$vmToPrimitive","$ConvCoerce","$ConvReplay","$IsConvert"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,274,1,273,9,279,10,280,11,281,12,279,13,277,14,285,15,286,16,287,17,288,18,285,19,283,20,292,21,293,22,294,23,295,24,292,25,290,26,300,27,299,28,297,29,305,30,303,31,309,32,307,33,312,34,313,35,311,36,310,40,319,41,320,42,318,43,316,44,324,45,325,46,326,47,327,48,324,49,322,50,332,51,333,52,334,53,332,54,331,55,335,56,336,57,337,58,338,59,335,60,331,61,339,62,329,63,343,64,344,65,342,66,344,67,342,68,346,69,345,70,347,71,348,72,349,73,350,74,347,75,345,76,351,77,342,78,341,79,354,80,353,81,354,82,310,83,358,84,359,85,357,86,359,87,357,88,360,89,357,90,356,91,364,92,365,93,366,94,367,95,368,96,369,97,370,98,364,99,362,100,373,101,374,102,375,103,373,104,372,105,371,106,377,107,376,108,381,109,380,110,273,111,272],"localNames":["callable","target","args","constructing","replay","req","values","coerce","ordinal","len","converted","i","res","val","hint","prim"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":11,"name":"$vmJsonVisitor","flags":72,"length":3,"params":3,"locals":5,"maxStack":1,"code":[269,65805,131341,196621,262157,786454,262416,36,196880,262159,67],"consts":[],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,382,5,392,7,391,8,389,9,639,10,638],"localNames":["boundary","replacer","properties","ancestors","visit"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":12,"name":"visit","flags":76,"length":3,"params":3,"locals":19,"maxStack":6,"code":[81,131082,6553603,48,720957,0,20,65540,131076,582,5,10,65546,26,196875,0,196628,196618,326,2228285,17,54,1900605,0,20,262148,327684,582,5,196618,82,2162771,67,196619,0,393236,196618,326,3473469,458764,196618,458777,459019,0,524308,458762,326,3473469,196618,458762,65546,326,196619,65553,0,51,4128829,10,65553,65546,196618,582,196619,0,589844,196618,17,131089,838,262411,262154,3,50,4980797,0,67,262154,65539,50,5374013,196618,67,262154,131075,50,5767229,1,67,262154,196611,50,6422589,0,655380,196618,17,582,67,262154,262147,50,7012413,0,720916,196618,326,7077947,196618,327947,131089,131089,786457,31,32,543,327690,28,5,0,393483,0,851988,327690,326,13172797,524300,589836,36,393227,327690,786457,524555,3,590091,589834,524298,46,13107261,655372,0,196625,327690,0,917524,589834,326,131082,65539,43,838,11338570,11337787,983307,0,3,7,1048843,11079509,1048586,131075,50,10682429,67,5,11013966,70,1611,5,983105,326,11538518,82,10158140,70,843,9961531,1048586,131075,50,11862077,67,655627,393226,589834,31,32,543,655370,0,50,12648509,1,12714043,655370,28,5,589834,56,589835,8781884,18546747,720908,786444,0,1048596,70,393227,0,1114132,327690,17,262161,838,721163,3,786699,786442,720906,786457,46,18546749,851980,917516,720906,786442,26,852235,0,196625,327690,851978,131082,65539,43,838,16909130,16908347,1114379,0,3,7,1179915,16650581,1179658,131075,50,16252989,67,5,16585038,70,1611,5,1179713,326,17109590,82,15728700,70,843,15532091,1179658,131075,50,17432637,67,917771,917514,0,51,18284605,0,1245204,393226,851978,917514,258,1094,5,786442,56,786443,14155836,0,1310740,131089,326,5,393226,67],"consts":["$Throw","RangeError","Value exceeds the maximum depth of 100.","$IsPromise","InvalidData","JSON.stringify received an un-awaited Promise; await it first - e.g. `const result = await load(...)`.","$IsObj","toJSON","$IsCallableAny","$JsonClassify","$JsonSpecial","$SetToArray","length","$IsArray","String",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$NewNullProtoObj","$JsonNames",{"type":"TypeError","message":"The delegated iterator does not provide a throw() method."},"$Define","$ArrayPop"],"captures":[0,0,0,1,0,3,0,4,0,2],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,392,1,400,2,401,3,399,4,398,5,403,6,404,7,405,8,406,9,403,11,411,12,412,13,410,14,408,15,414,16,415,17,416,18,414,19,413,20,420,21,419,22,418,23,423,24,424,25,425,26,426,27,423,29,431,30,430,33,429,34,433,35,434,36,435,37,433,38,432,40,441,41,440,42,438,43,444,44,445,45,446,46,444,47,443,48,453,49,452,50,454,51,450,52,449,53,457,54,458,55,456,56,455,57,465,58,464,59,466,60,467,61,462,62,461,63,471,64,472,65,473,66,474,67,475,68,471,69,469,70,478,71,479,72,477,73,476,74,481,75,480,76,484,77,485,78,483,79,482,80,487,81,486,82,490,83,491,84,489,85,488,86,493,87,492,88,496,89,497,90,495,91,494,92,499,93,500,94,501,95,502,96,499,97,498,98,508,99,509,100,507,101,506,102,510,103,511,104,512,105,510,106,506,107,513,108,504,109,517,110,519,111,518,112,516,113,518,114,516,115,521,116,516,117,515,118,523,120,526,121,527,122,528,123,526,124,525,127,533,128,532,129,538,130,537,131,535,132,543,133,541,134,546,135,547,136,545,137,544,139,553,140,554,141,555,142,556,143,557,144,558,145,556,146,560,147,561,148,559,149,553,150,552,181,550,182,565,183,566,184,564,185,566,186,564,187,569,188,570,189,568,190,567,191,571,192,567,193,572,194,564,195,563,196,575,197,574,198,575,199,544,200,525,203,580,204,581,205,580,206,579,207,585,208,586,209,587,210,588,211,589,212,585,213,583,214,593,215,591,216,596,217,598,218,597,219,595,220,594,223,605,224,606,225,604,226,602,227,611,228,612,229,613,230,614,231,616,232,617,233,615,234,611,235,610,266,608,267,620,268,621,269,619,270,618,271,623,272,624,273,625,274,626,275,627,276,628,277,623,279,631,280,630,281,631,282,594,283,633,284,634,285,635,286,633,288,637,289,636],"localNames":["holder","key","depth","val","kind","target","out","toJSON","len","i","elem","names","i","k","propVal","%it","%kind","%it","%kind"],"upvalNames":["boundary","replacer","ancestors","visit","properties"]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":13,"name":"$vmJsonWalk","flags":72,"length":4,"params":4,"locals":5,"maxStack":6,"code":[0,0,20,2,10,131082,838,35,65546,65574,65540,3,838,131353,70,196633,262411,0,262164,2,262154,131082,196618,1094,67],"consts":["$vmJsonVisitor","","next","value","$JsonFinish"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,653,1,654,2,655,3,656,4,657,5,658,6,654,7,659,8,662,9,660,10,663,11,664,12,653,13,652,14,651,15,650,16,648,17,668,18,669,19,670,20,671,21,672,22,673,23,668,24,667],"localNames":["replacer","value","properties","indent","json"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":14,"name":"$vmSettleCall","flags":72,"length":3,"params":3,"locals":3,"maxStack":5,"code":[0,20,10,65546,131082,838,67],"consts":["$Apply"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,681,1,682,2,683,3,684,4,685,5,681,6,680],"localNames":["callable","thisVal","args"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":15,"name":"$vmCombine","flags":72,"length":2,"params":2,"locals":6,"maxStack":4,"code":[0,20,10,65546,582,131339,196620,65546,1441866,1441851,262411,2360396,1638459,196620,196875,0,65556,131082,196618,582,5,720956,70,75,655419,70,2360397,852027,327947,2098254,70,5,327690,64,5,2097211,0,131092,131082,326,5,0,67],"consts":["$CombineStart","$CombineAdd","$CombineEnd"],"captures":[],"handlers":[13,22,28,0,29,32,34,0],"finallies":[],"finallyOffsets":[],"sites":[0,694,1,695,2,696,3,697,4,694,5,692,7,702,8,698,14,699,15,705,16,706,17,707,18,708,19,705,21,698,36,710,37,711,38,712,39,710,41,690],"localNames":["name","items","c","item","%it","%e"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":16,"name":"$vmOpenSyncForAsync","flags":72,"length":2,"params":2,"locals":2,"maxStack":4,"code":[0,20,10,65546,70,326,67],"consts":["$WrapAsyncFromSync"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,719,1,720,2,724,3,723,4,721,5,719,6,718],"localNames":["v","method"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":17,"name":"$vmAsyncFromSync","flags":72,"length":4,"params":4,"locals":10,"maxStack":7,"code":[0,20,10,326,262411,65546,65540,50,917565,0,131092,10,326,1310779,0,196628,10,262154,65546,838,327947,327690,0,50,1835071,327690,1,50,4718653,589836,65546,262148,50,2687037,0,327700,196618,326,5,0,67,0,196628,10,262154,262148,838,590091,589834,0,51,3604799,589834,1,51,4259901,0,393236,10,262154,589834,70,262148,838,5,0,458772,10,326,5,0,67,131082,5177405,262154,327690,196618,326,5374011,262154,327690,70,393483,0,393236,10,393226,65546,838,5,393226,524313,54,54,459019,393226,589849,524555,0,655380,10,524298,458762,65546,262148,51,1094,5,0,67],"consts":["$AfsGetIter","next","$AfsGetNext","$AfsGetMethod","return","$AfsResolveDone","$AfsCheckObj","$AfsRejectNoThrow","done","value","$AfsSettle"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,735,1,736,2,737,3,735,4,733,5,743,6,744,7,742,8,741,9,745,10,746,11,747,12,745,13,741,14,748,15,749,16,750,17,751,18,752,19,748,20,739,21,756,22,757,23,755,24,754,25,759,26,760,27,758,28,753,30,764,31,765,32,763,33,762,34,768,35,769,36,770,37,768,39,771,41,775,42,776,43,777,44,778,45,779,46,775,47,773,48,783,49,784,50,782,51,781,52,786,53,787,54,785,55,780,56,790,57,791,58,792,59,796,60,795,61,793,62,797,63,790,65,799,66,800,67,801,68,799,70,802,72,807,73,806,74,811,75,810,76,812,77,808,78,806,79,816,80,815,81,813,82,804,83,818,84,819,85,820,86,821,87,822,88,818,90,829,91,828,92,827,93,826,94,824,95,835,96,834,97,832,98,838,99,839,100,840,101,841,102,842,103,844,104,845,105,843,106,838,108,731],"localNames":["adapter","kind","hasArg","val","iter","method","res","done","value","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":18,"name":"$vmAsyncFromSyncClose","flags":72,"length":1,"params":1,"locals":2,"maxStack":2,"code":[10,25,65803,65546,0,51,655679,65546,1,51,983101,10,65546,70,5,0,67],"consts":["return"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,854,1,853,2,851,3,859,4,860,5,858,6,857,7,862,8,863,9,861,10,856,11,868,12,867,13,865,15,849],"localNames":["iter","ret"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":19,"name":"$vmHostWait","flags":74,"length":3,"params":3,"locals":4,"maxStack":5,"code":[3,196875,196618,10,25,46,1048637,10,196618,26,340,5,196618,56,196619,131132,0,65556,65546,0,131082,838,67],"consts":["length","$Apply"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,878,1,876,2,881,3,883,4,882,5,880,6,879,7,889,8,890,9,888,10,887,12,893,13,892,14,893,15,879,16,895,17,896,18,897,19,898,20,899,21,895,22,894],"localNames":["promises","callee","args","i"],"upvalNames":[]},{"source":{"id":"@vm-internal","hash":"8ef1caadda56e55e"},"index":20,"name":"$vmResult","flags":74,"length":1,"params":1,"locals":4,"maxStack":6,"code":[0,20,10,326,524349,10,340,589883,10,65803,0,0,65556,258,0,1,838,35,65546,131110,131076,3,838,131339,131082,196889,70,196875,196618,262169,54,2621501,131082,196889,196618,327705,340,326,196619,1835068,0,393236,258,196618,327705,1,0,1094,67],"consts":["$IsThenable","$vmJsonVisitor","","next","done","value","$JsonFinish"],"captures":[],"handlers":[],"finallies":[],"finallyOffsets":[],"sites":[0,908,1,909,2,910,3,908,4,907,5,912,6,911,7,907,8,913,9,905,10,917,11,918,12,919,13,920,14,921,15,922,16,918,17,923,18,926,19,924,20,927,21,928,22,917,23,915,24,934,25,933,26,932,27,930,28,939,29,938,30,937,31,936,32,946,33,945,34,950,35,949,36,948,37,944,38,943,39,936,40,953,41,954,42,955,43,957,44,956,45,959,46,960,47,953,48,952],"localNames":["value","root","walk","step"],"upvalNames":[]}]}} diff --git a/packages/codemode/src/prelude/bytecode.ts b/packages/codemode/src/language/prelude/bytecode.ts similarity index 100% rename from packages/codemode/src/prelude/bytecode.ts rename to packages/codemode/src/language/prelude/bytecode.ts diff --git a/packages/codemode/src/prelude/helpers.ts b/packages/codemode/src/language/prelude/helpers.ts similarity index 99% rename from packages/codemode/src/prelude/helpers.ts rename to packages/codemode/src/language/prelude/helpers.ts index 1d02f065d84a..00f7987c5f87 100644 --- a/packages/codemode/src/prelude/helpers.ts +++ b/packages/codemode/src/language/prelude/helpers.ts @@ -33,7 +33,7 @@ import { } from "../runtime/objects.js" import { builtinCursor, requireIteratorObject } from "../runtime/operations.js" import { containsOpaqueReference, describeValue, isOpaque } from "../runtime/references.js" -import { compareText } from "../tool-runtime.js" +import { compareText } from "../runtime/operations.js" import { Invoke } from "../runtime/signals.js" // The prelude's privileged helpers: leaf built-ins (they never call program code) bound only in the prelude's scope. diff --git a/packages/codemode/src/prelude/prelude.ts b/packages/codemode/src/language/prelude/prelude.ts similarity index 100% rename from packages/codemode/src/prelude/prelude.ts rename to packages/codemode/src/language/prelude/prelude.ts diff --git a/packages/codemode/src/prelude/source.ts b/packages/codemode/src/language/prelude/source.ts similarity index 100% rename from packages/codemode/src/prelude/source.ts rename to packages/codemode/src/language/prelude/source.ts diff --git a/packages/codemode/src/language/runtime.ts b/packages/codemode/src/language/runtime.ts new file mode 100644 index 000000000000..b464ec60d02a --- /dev/null +++ b/packages/codemode/src/language/runtime.ts @@ -0,0 +1,176 @@ +export * as Runtime from "./runtime.js" + +import { Deferred, Effect, Exit } from "effect" +import type { Json } from "./data.js" +import type { DurableFn, Fn, Namespace } from "./host-function.js" +import { + finishExecution, + type Outcome, + type Result, + resumeProgram, + type Run, + type Running, + type Setup, + startProgram, + SuspendError, + type Suspended, +} from "./runtime/execute.js" +import type { SnapshotError } from "./snapshot/snapshot.js" + +export { SuspendError } +export type { Outcome, Result, Run, Suspended } + +/** Budgets enforced on each run. Time counts active time only, across every resume of a run. */ +export type Limits = { + /** Wall-clock milliseconds before interruption. No default: absent means no timeout. */ + readonly timeoutMs?: number + /** + * Maximum UTF-8 bytes retained from the result and logs. Warnings have a separate equal budget; truncation notices + * are additional. + */ + readonly maxOutputBytes?: number +} + +/** + * How a host call's arguments carry program values the host can't hold. Every host function in the runtime gets its + * arguments this way; results are not affected. + */ +export type Translate = { + /** + * A program function inside an argument: `"error"` (the default) fails the call; `"drop"` leaves it out, as + * `JSON.stringify` does: an object's key is left out, an array's item is `null`, and an argument is `undefined`. + */ + readonly functions?: "error" | "drop" | undefined + /** + * A program promise inside an argument: `"error"` (the default) fails the call; `"await"` makes the call wait for it + * to settle and use its value. A rejection fails the call with its reason. + */ + readonly promises?: "error" | "await" | undefined +} + +/** A runtime's host globals for one run: `Host.fn`, `Host.durableFn`, `Host.namespace`, and plain values of them. */ +export type Globals = Readonly> + +export type Options = { + /** + * The globals a program reaches, made for each run when it starts and again when it resumes, so they can keep the + * run's own state in `run.metadata`. + */ + readonly globals?: ((run: Run) => G) | undefined + readonly limits?: Limits | undefined + readonly translate?: Translate | undefined +} + +/** Options for `Runtime.program`. */ +export type ProgramOptions = { + /** Read-only JSON values bound in the program's outer scope (shadowable by top-level declarations). */ + readonly bindings?: Readonly> | undefined + /** Names this run's host calls (`:`); a random UUID by default. */ + readonly runId?: string | undefined +} + +/** How `program.suspend` saves a run's state. */ +export type SuspendOptions = { + /** The most bytes a saved state may take; a larger one fails the suspend, and the run goes on. */ + readonly maxBytes?: number +} + +/** A reusable confined runtime over host globals. */ +export type Runtime = { + /** Runs a program to its end. */ + readonly execute: (code: string, options?: ProgramOptions) => Effect.Effect + /** A program ready to run once; nothing runs until `program.run()`. */ + readonly program: (code: string, options?: ProgramOptions) => Program + /** + * A suspended run (its `state`, or that state as bytes) as a program to continue in this runtime, which must be the + * same build with the same host globals. The state is checked when it runs: `run()` fails with a `SnapshotError` + * when it can't be resumed here. + */ + readonly resume: (state: Suspended["state"] | Uint8Array) => Program +} + +/** + * One run of a program. `run()` runs it in the current fiber until it ends or is suspended, and owns its host calls: + * interrupting `run()` interrupts them. A program runs once: running it again is a defect. + */ +export type Program = { + readonly run: () => Effect.Effect + /** + * Saves the run's state and stops it, at once, from another fiber: `run()` then returns the same `Suspended`, and so + * does this. It fails with a `SuspendError`, leaving the run running, while a host call that can't be suspended is + * in progress, or when the state can't be saved or is over `options.maxBytes`. A run that already ended gives its + * result. Before `run()` starts, this waits for it to start. + */ + readonly suspend: (options?: SuspendOptions) => Effect.Effect +} + +/** The Effect environment the host functions among `G` need, at any depth. */ +export type Services = ServicesOf + +type ServicesOf> = Depth["length"] extends 8 + ? never + : T extends Fn | DurableFn | Namespace + ? R + : T extends object + ? ServicesOf + : never + +/** Creates a runtime whose programs reach `globals`. */ +export const make = (options: Options = {}): Runtime> => { + type R = Services + const setup: Setup = { + limits: { + timeoutMs: validateLimit("timeoutMs", options.limits?.timeoutMs, 1), + maxOutputBytes: validateLimit("maxOutputBytes", options.limits?.maxOutputBytes, 0), + }, + globals: options.globals, + translate: { + functions: options.translate?.functions ?? "error", + promises: options.translate?.promises ?? "error", + }, + } + const program = (code: string, programOptions?: ProgramOptions) => + makeProgram(() => Effect.succeed(startProgram(setup, code, programOptions?.bindings, programOptions?.runId))) + return { + execute: (code, programOptions) => Effect.suspend(() => finishExecution(program(code, programOptions).run())), + program, + resume: (state) => makeProgram(() => resumeProgram(setup, state)), + } +} + +export const validateLimit = (name: string, value: number | undefined, minimum: number): number | undefined => { + if (value !== undefined && (!Number.isSafeInteger(value) || value < minimum)) { + throw new RangeError(`${name} must be a safe integer greater than or equal to ${minimum}.`) + } + return value +} + +// A program's one run: `start` makes it when `run()` is called, and a suspend waits for it. +const makeProgram = (start: () => Effect.Effect, E>): Program => { + let claimed = false + const started = Deferred.makeUnsafe, SuspendError>() + return { + run: () => + Effect.suspend(() => { + if (claimed) { + return Effect.die(new Error("A program runs once; make another with runtime.program or runtime.resume.")) + } + claimed = true + return start().pipe( + Effect.onExit((exit) => + Exit.isSuccess(exit) + ? Deferred.succeed(started, exit.value) + : Deferred.fail(started, new SuspendError({ message: "The program didn't start.", running: [] })), + ), + Effect.flatMap((running) => running.run()), + ) + }), + suspend: (options) => { + const maxBytes = options?.maxBytes + if (maxBytes !== undefined && (!Number.isSafeInteger(maxBytes) || maxBytes < 0)) { + throw new RangeError("maxBytes must be a safe integer greater than or equal to 0.") + } + return Effect.flatMap(Deferred.await(started), (running) => running.suspend(maxBytes)) + }, + } +} diff --git a/packages/codemode/src/runtime/callback.ts b/packages/codemode/src/language/runtime/callback.ts similarity index 98% rename from packages/codemode/src/runtime/callback.ts rename to packages/codemode/src/language/runtime/callback.ts index a5a241f8bfea..fadaa766c19c 100644 --- a/packages/codemode/src/runtime/callback.ts +++ b/packages/codemode/src/language/runtime/callback.ts @@ -46,7 +46,7 @@ export const requireCallback = (callback: Value, name: string): Callable => { if (isSupportedCallback(callback)) return callback if (typeofValue(callback) === "function") { throw typeError( - `${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, + `${name} cannot use this callable as a callback; wrap it in an arrow function, e.g. (value) => fn(value).`, ) } throw typeError(`${name} expects a function callback.`) diff --git a/packages/codemode/src/runtime/driver.ts b/packages/codemode/src/language/runtime/driver.ts similarity index 60% rename from packages/codemode/src/runtime/driver.ts rename to packages/codemode/src/language/runtime/driver.ts index e9039c1d43f0..43392cd720ab 100644 --- a/packages/codemode/src/runtime/driver.ts +++ b/packages/codemode/src/language/runtime/driver.ts @@ -1,9 +1,9 @@ import { Cause, type Context, Effect, Exit, Fiber } from "effect" import type { Json } from "../data.js" +import { DurableFn, type Fn } from "../host-function.js" import type { HostCall } from "../scheduler.js" -import { type ToolRuntime, toolExpression } from "../tool-runtime.js" import { locate } from "./errors.js" -import { PendingThrow } from "./model.js" +import { HostFailure, PendingThrow } from "./model.js" import type { Vm } from "../vm.js" import { abrupt, type Completion, Interruption, normal } from "./signals.js" @@ -16,10 +16,10 @@ export const SUSPENDED: unique symbol = Symbol("codemode.suspended") * calls the VM queued, as fibers with the caller's services, reporting each outcome back with `vm.complete`. * Fibers live only here, by host call id, and never become part of the VM's state. * - * A durable tool's call runs as two fibers, `start` then `wait`, so suspending can detach the second and keep the JSON - * data `start` gave. An idempotent tool's running fiber is detached and interrupted on suspend, and its call is saved - * as queued to run again on resume. The VM and tool bridge can be swapped for restored ones between stops (the - * round-trip test mode does); a fiber's outcome goes to whichever VM is current, by host call id. + * A durable call runs as two fibers, `start` then `wait`, so suspending can detach the second and keep the JSON data + * `start` gave. An idempotent call's running fiber is detached and interrupted on suspend, and the call is saved as + * queued to run again on resume. The VM can be swapped for a restored one between stops (the round-trip test mode + * does); a fiber's outcome goes to whichever VM is current, by host call id. */ export class Driver { private readonly fibers = new Map>() @@ -35,8 +35,7 @@ export class Driver { constructor( public vm: Vm, - public tools: ToolRuntime, - /** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */ + /** The services host work runs with: the caller's, so host functions see its context, tracing, and clock. */ private readonly services: Context.Context, ) {} @@ -72,7 +71,7 @@ export class Driver { return abrupt( new PendingThrow( "Error", - "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.", + "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. There are no timers or sleep, so don't wait or poll inside a program.", stop.node, ), ) @@ -85,30 +84,27 @@ export class Driver { } /** - * The host calls that block a suspend now: every call in progress except an idempotent tool call or a durable tool - * call in `started`, plus any queued or synchronous extension call the program is in the middle of, which holds live - * host values. A tool call queued but not started starts on resume. + * The host calls that block a suspend now: every call in progress except an idempotent call, or a durable call in + * `started`. A call queued but not started starts on resume, unless its arguments hold host functions, which + * can't be saved. */ blocking(): Array<{ readonly key: string; readonly path: string }> { - const vm = this.vm - const calls = vm.calls().filter((call) => { - if (call.request.kind === "extension") return true - if (call.status === "queued" || call.status === "started") return false - if (call.request.kind === "tool" && this.tools.mode(call.request.path)?._tag === "CodeModeIdempotent") - return false - return true - }) - const running = calls.map((call) => ({ key: call.key, path: callPath(call) })) - const active = vm.activeExtension - if (active !== undefined && !calls.some((call) => call.blocking && call.request.kind === "extension")) { - running.push(active) - } - return running + return this.vm + .calls() + .filter( + (call) => + call.holdsFunctions || + (call.status !== "queued" && call.status !== "started" && call.policy !== "idempotent"), + ) + .map((call) => ({ key: call.key, path: call.label })) } - /** Whether the run holds live host values: an extension call in progress. */ + /** + * Whether the run holds live host values: a call whose function can't be found again, that waits on its result, or + * whose arguments hold host functions. + */ get holdsHostValues(): boolean { - return this.vm.calls().some((call) => call.request.kind === "extension") || this.vm.activeExtension !== undefined + return this.vm.calls().some((call) => call.work !== undefined || call.path === undefined || call.holdsFunctions) } /** @@ -121,7 +117,7 @@ export class Driver { this.wake?.() } - /** Stops host work still running, waiting for its cleanup; a durable tool's work is cancelled unless detached. */ + /** Stops host work still running, waiting for its cleanup; a durable call's work is cancelled unless detached. */ interrupt(): Effect.Effect { const self = this return Effect.uninterruptible( @@ -131,18 +127,18 @@ export class Driver { // A suspended run's durable work keeps running, to be waited on at resume. if (self.suspending) return for (const call of self.vm.calls()) { - const request = call.request - if (call.status !== "started" || request.kind !== "tool") continue + if (call.status !== "started") continue call.status = "completed" - yield* Effect.ignore(self.tools.cancel(request.path, call.data as Json)).pipe( - Effect.provideContext(self.services), - ) + const fn = self.function(call) + if (!(fn instanceof DurableFn)) continue + const cancel = Effect.suspend(() => fn.cancel(call.data as Json, self.context(call))) + yield* Effect.ignore(cancel as Effect.Effect).pipe(Effect.provideContext(self.services)) } }), ) } - /** Starts tool calls that were queued (including idempotent calls) and waits on durable calls in `started`. */ + /** Starts calls that were queued (including idempotent ones) and waits on durable calls in `started`. */ resume(): void { if (this.suspending) return for (const call of this.vm.calls()) { @@ -151,65 +147,68 @@ export class Driver { } } - private admit(id: number, name: string): void { - const call = this.vm.call(id) - if (call?.admitted) return - this.tools.record(name) - if (call !== undefined) call.admitted = true - } - // Starts host work now; its outcome goes back to the VM: a blocking call's continues the job that made it, a // concurrent call's waits in the inbox and settles its promise in a later job, in completion order. private launch(call: HostCall): void { call.status = "running" - const request = call.request - if (request.kind === "tool") { - if (this.tools.mode(request.path)?._tag === "CodeModeDurable") { - const started = this.tools.start(request.path, request.args, call.key, (name) => this.admit(call.id, name)) - return this.fork(call.id, started, (data) => { - const current = this.vm.call(call.id) - if (current === undefined) return - current.status = "started" - current.data = data as Json - if (this.stopping || this.detached.has(call.id)) return - this.wait(current) - }) - } + if (call.work !== undefined) return this.fork(call.id, outcome(call.work)) + const fn = this.function(call) + if (fn === undefined) { return this.fork( call.id, - this.tools.execute(request.path, request.args, { - key: call.key, - admit: (name) => this.admit(call.id, name), - detaching: () => this.suspending, - }), + Effect.fail(new HostFailure("ExecutionFailure", `${call.label} is not available in this runtime.`)), ) } - if (request.kind === "search") { + if (fn instanceof DurableFn) { return this.fork( call.id, - this.tools.search(request.args, { - key: call.key, - admit: (name) => this.admit(call.id, name), - }), + Effect.suspend(() => fn.start(call.args, this.context(call))), + (data) => { + const current = this.vm.call(call.id) + if (current === undefined) return + current.status = "started" + current.data = data as Json + if (this.stopping || this.detached.has(call.id)) return + this.wait(current) + }, ) } - // An extension's work, made by the extension bridge with the program's services in its requirements. - return this.fork(call.id, this.vm.ctx.extensionWork!(request.call) as Effect.Effect) + this.fork( + call.id, + Effect.suspend(() => { + try { + return outcome(fn.run(call.args, this.context(call))) + } catch (reason) { + return Effect.fail(reason) + } + }), + ) } private wait(call: HostCall): void { - const request = call.request - if (request.kind !== "tool") return + const fn = this.function(call) + if (!(fn instanceof DurableFn)) return this.fork( call.id, - this.tools.wait(request.path, request.args, call.key, call.data as Json, () => this.suspending), + Effect.suspend(() => fn.wait(call.data as Json, this.context(call))), ) } - // Forks `work` with the caller's services; it starts synchronously, so admission and hooks happen before the VM - // runs another job. A success goes to `next` when given, otherwise to the VM. - private fork(id: number, work: Effect.Effect, next?: (value: unknown) => void): void { - const fiber = Effect.runForkWith(this.services)(work) + // The call's host function: the one it was made with, or, in a restored run, the one at its path now. + private function(call: HostCall): Fn | DurableFn | undefined { + if (call.fn !== undefined) return call.fn + if (call.path === undefined) return undefined + return (call.fn = this.vm.ctx.hosts?.resolve(call.path)) + } + + private context(call: HostCall) { + return { key: call.key, args: call.args, suspending: () => this.suspending } + } + + // Forks `work` with the caller's services; it starts synchronously, so host functions run before the VM runs + // another job. A success goes to `next` when given, otherwise to the VM. + private fork(id: number, work: Effect.Effect, next?: (value: unknown) => void): void { + const fiber = Effect.runForkWith(this.services)(work as Effect.Effect) this.fibers.set(id, fiber) fiber.addObserver((exit) => { if (this.fibers.get(id) === fiber) this.fibers.delete(id) @@ -218,10 +217,9 @@ export class Driver { if (this.stopping || this.detached.has(id)) return const call = this.vm.call(id) if (call === undefined) return - if (call.blocking) { + if (call.blocking && Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) { // Blocking work interrupted on its own interrupts the execution. - if (Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) this.interrupted = exit.cause - else this.vm.complete(id, Exit.isSuccess(exit) ? normal(exit.value) : abrupt(Cause.squash(exit.cause))) + this.interrupted = exit.cause this.wake?.() return } @@ -255,9 +253,9 @@ export class Driver { } } -/** A host call as the program wrote it: `tools.x.y`, `search`, or an extension function's path. */ -export const callPath = (call: HostCall): string => { - const request = call.request - if (request.kind === "tool") return toolExpression(request.path.flatMap((segment) => segment.split(".")).join(".")) - return request.kind === "search" ? "search" : request.call.label +// What a host function returned, as the Effect its call waits on: an Effect runs, a Promise settles. +const outcome = (result: unknown): Effect.Effect => { + if (Effect.isEffect(result)) return result as Effect.Effect + if (result instanceof Promise) return Effect.tryPromise({ try: () => result, catch: (reason) => reason }) + return Effect.succeed(result) } diff --git a/packages/codemode/src/runtime/errors.ts b/packages/codemode/src/language/runtime/errors.ts similarity index 93% rename from packages/codemode/src/runtime/errors.ts rename to packages/codemode/src/language/runtime/errors.ts index 4e9ce352fc26..6f60cd3cf288 100644 --- a/packages/codemode/src/runtime/errors.ts +++ b/packages/codemode/src/language/runtime/errors.ts @@ -1,7 +1,12 @@ -import type { Diagnostic } from "../codemode.js" -import { ToolError } from "../tool-error.js" -import { ToolRuntimeError } from "../tool-runtime.js" -import { type AstNode, formatLocation, PendingThrow, Throw, sourceLocation } from "./model.js" +import { + type AstNode, + type Diagnostic, + formatLocation, + HostFailure, + PendingThrow, + Throw, + sourceLocation, +} from "./model.js" import { containsRuntimeReference } from "./references.js" import { createErrorValue, type ErrorType, isErrorType } from "./intrinsics.js" import { constructor, methods, prototypeFrom, receiver } from "./native.js" @@ -54,7 +59,7 @@ export const normalizeError = (error: unknown): Diagnostic => { } } - if (error instanceof ToolRuntimeError) { + if (error instanceof HostFailure) { return { kind: error.kind, message: error.message, @@ -62,10 +67,6 @@ export const normalizeError = (error: unknown): Diagnostic => { } } - if (error instanceof ToolError) { - return { kind: "ToolFailure", message: error.message } - } - if (error instanceof Throw) { const value = error.value if (value instanceof ErrorObj) { @@ -112,7 +113,7 @@ export const locate = (error: unknown, node?: AstNode): unknown => { if ((error.node === undefined || inPrelude(error.node)) && node) error.node = node return error } - if (error instanceof Error && !(error instanceof ToolError) && !(error instanceof ToolRuntimeError)) { + if (error instanceof Error && !(error instanceof HostFailure)) { return new PendingThrow(errorType(error), errorMessage(error), node) } return error diff --git a/packages/codemode/src/runtime/execute.ts b/packages/codemode/src/language/runtime/execute.ts similarity index 75% rename from packages/codemode/src/runtime/execute.ts rename to packages/codemode/src/language/runtime/execute.ts index 20bb53d92622..0797cf7d9088 100644 --- a/packages/codemode/src/runtime/execute.ts +++ b/packages/codemode/src/language/runtime/execute.ts @@ -1,39 +1,81 @@ import { parse, type Program } from "acorn" import { Cause, Deferred, Effect, Option, Schema } from "effect" -import type { DataValue, Diagnostic, Outcome, ResolvedExecutionLimits, Result, Suspended } from "../codemode.js" +import { fromJson, type Json } from "../data.js" +import { DurableFn } from "../host-function.js" import { fromBytes, header, type Snapshot, SnapshotError, toBytes } from "../snapshot/snapshot.js" -import { ToolRuntime } from "../tool-runtime.js" import { normalizeError } from "./errors.js" +import { hostGlobals } from "./extensions.js" import { createBuiltins } from "./intrinsics.js" import { Interpreter } from "./interpreter.js" import type { Value } from "./objects.js" -import { formatLocation, sourceLocation } from "./model.js" -import { callPath, Driver, SUSPENDED } from "./driver.js" +import { type Diagnostic, formatLocation, sourceLocation } from "./model.js" +import { Driver, SUSPENDED } from "./driver.js" import { Interruption } from "./signals.js" import { Source } from "./source.js" import type { Vm } from "../vm.js" +import type { Translate } from "../runtime.js" /** * A suspend that failed, leaving the run running: `running` names each host call that blocked it by its key and its - * path (`tools.x.y`, `search`, or an extension function's path such as `fetch`). Empty when the state couldn't be - * saved or was too large. + * path (`fetch`, `fs.readFile`, `api.issues.list`). Empty when the state couldn't be saved or was too large. */ export class SuspendError extends Schema.TaggedError()("SuspendError", { message: Schema.String, running: Schema.Array(Schema.Struct({ key: Schema.String, path: Schema.String })), }) {} +/** A run that ended: its value, or the diagnostic it failed with, plus its logs and its `run.metadata` at the end. */ +export type Result = + | { + readonly ok: true + readonly value: Json + readonly warnings?: ReadonlyArray + readonly logs?: ReadonlyArray + readonly truncated?: boolean + readonly metadata: Json + } + | { + readonly ok: false + readonly error: Diagnostic + readonly logs?: ReadonlyArray + readonly truncated?: boolean + readonly metadata: Json + } + +/** A run stopped by `program.suspend`: its whole state, to resume with `Runtime.resume`. */ +export type Suspended = { + readonly ok: "suspended" + readonly state: Snapshot +} + +/** How a run of a program ended: its result, or its saved state once suspended. */ +export type Outcome = Result | Suspended + +/** + * One run of a program, as its host globals see it. `metadata` is the host's own state for the run, as JSON: saved + * with a suspended run and given back on resume, and in the run's result. + */ +export type Run = { + readonly id: string + metadata: Json +} + +/** The budgets every run of a runtime gets. Time counts active time only, across every resume of a run. */ +export type Limits = { + readonly timeoutMs: number | undefined + readonly maxOutputBytes: number | undefined +} + type Globals = (ctx: Interpreter) => ReadonlyArray -/** How a runtime runs programs: its tools, limits, hooks, and host globals, shared by every run it starts. */ +/** How a runtime runs programs: its limits and host globals, shared by every run it starts. */ export type Setup = { - readonly prepared: ToolRuntime.Prepared - readonly limits: ResolvedExecutionLimits - readonly hooks: ToolRuntime.Hooks - /** The host's globals, made again for a restored run; see `Interpreter`. */ - readonly globals?: Globals | undefined - /** Whether the host globals can hand the program values with no data form (extensions can). */ - readonly lossy?: boolean + readonly limits: Limits + readonly translate?: Required | undefined + /** Each run's host globals (see `Host`): made when it starts, and again for a restored run. */ + readonly globals?: ((run: Run) => Readonly>) | undefined + /** Globals made as program values (a test harness), made again the same way for a restored run. */ + readonly programGlobals?: Globals | undefined } /** Turns a suspended outcome into a failure, for callers that only run programs to their end. */ @@ -43,7 +85,7 @@ export const finishExecution = (outcome: Effect.Effect): Ef return { ok: false, error: { kind: "ExecutionFailure", message: "The program suspended, which isn't supported here." }, - toolCalls: outcome.state.header.toolCalls, + metadata: outcome.state.header.metadata, } }) @@ -51,25 +93,26 @@ export const finishExecution = (outcome: Effect.Effect): Ef export const startProgram = ( setup: Setup, code: string, - bindings?: Globals, + bindings?: Readonly>, runId: string = crypto.randomUUID(), ): Running => new Running(setup, 0, () => { if (code.trim().length === 0) { - return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, toolCalls: [] } + return { ok: false, error: { kind: "ParseError", message: "Code cannot be empty." }, metadata: null } } const parsed = parseProgram(code) - if (!parsed.ok) return { ok: false, error: parsed.error, toolCalls: [] } - const runtime = instantiate(setup, bindings) + if (!parsed.ok) return { ok: false, error: parsed.error, metadata: null } + const run: Run = { id: runId, metadata: null } + const runtime = instantiate(setup, run, bindings) runtime.vm.hosts.runId = runId runtime.vm.start(parsed.source) - return runtime + return [runtime, run] as const }) /** * Restores a saved run into this runtime, under the limits it was saved with, ready to run as `startProgram`'s. * Fails with a `SnapshotError` when the state can't be read or belongs to another build, other host globals, or other - * sources. + * sources, or when a durable call it waits on has no durable function at its path any more. */ export const resumeProgram = ( setup: Setup, @@ -78,26 +121,26 @@ export const resumeProgram = ( Effect.try({ try: () => { const state = saved instanceof Uint8Array ? fromBytes(saved) : saved - const limits = header(state).limits + const saw = header(state) const resumed: Setup = { ...setup, limits: { - timeoutMs: limits.timeoutMs ?? undefined, - maxToolCalls: limits.maxToolCalls ?? undefined, - maxOutputBytes: limits.maxOutputBytes ?? undefined, + timeoutMs: saw.limits.timeoutMs ?? undefined, + maxOutputBytes: saw.limits.maxOutputBytes ?? undefined, }, } - const fresh = instantiate(resumed) + const run: Run = { id: saw.runId, metadata: saw.metadata } + const fresh = instantiate(resumed, run) const snapshot = restoreInto(state, fresh.vm) for (const call of fresh.vm.calls()) { if (call.status !== "started") continue - if (call.request.kind !== "tool" || fresh.tools.mode(call.request.path)?._tag !== "CodeModeDurable") { + if (call.path === undefined || !(fresh.hosts?.resolve(call.path) instanceof DurableFn)) { throw new SnapshotError( - `The pending durable call ${call.key} (${callPath(call)}) cannot resume because that tool is no longer a durable tool in this runtime.`, + `The pending durable call ${call.key} (${call.label}) cannot resume because that function is no longer a durable function in this runtime.`, ) } } - return new Running(resumed, snapshot.header.budget.activeMs, () => fresh, true) + return new Running(resumed, snapshot.header.budget.activeMs, () => [fresh, run] as const, true) }, catch: (error) => (error instanceof SnapshotError ? error : new SnapshotError(String(error))), }) @@ -119,14 +162,19 @@ const restoreInto = (saved: unknown, vm: Vm): Snapshot => interruption: () => new Interruption(Cause.interrupt()), }) -// A new runtime: intrinsics, the host globals, the program's bindings, a fresh tool bridge, and its VM. -const instantiate = (setup: Setup, bindings?: Globals): Interpreter => { +// A new runtime for a run: intrinsics, the host globals, the program's bindings, and its VM. +const instantiate = (setup: Setup, run: Run, bindings?: Readonly>): Interpreter => { const ctx = new Interpreter({ - tools: ToolRuntime.make(setup.prepared, setup.limits.maxToolCalls, setup.hooks), builtins: createBuiltins(), logs: [], - globals: setup.globals, - bindings, + globals: (ctx) => [ + ...(setup.programGlobals?.(ctx) ?? []), + ...(setup.globals === undefined ? [] : hostGlobals(ctx, setup.globals(run), setup.translate)), + ], + bindings: + bindings === undefined + ? undefined + : (ctx) => Object.entries(bindings).map(([name, value]) => [name, fromJson(ctx, value)] as const), }) if (roundTrip) ctx.vm.tripEvery = ROUND_TRIP_TICKS return ctx @@ -147,6 +195,7 @@ const ROUND_TRIP_MAX = 200 /** One run's state: the runtime it is in now (restoring swaps it), its driver, and how much active time it used. */ export class Running { private ctx!: Interpreter + private run_!: Run private driver!: Driver private startedAt = 0 // Set once the program's body ends, and once a suspend saved the run: either way, `done` gets the outcome next. @@ -162,7 +211,7 @@ export class Running { private readonly setup: Setup, /** Active milliseconds used before this run, by earlier ones of the same execution. */ private readonly activeBefore: number, - private readonly prepare: () => Interpreter | Result, + private readonly prepare: () => readonly [Interpreter, Run] | Result, private readonly resuming = false, ) {} @@ -171,12 +220,14 @@ export class Running { const self = this return Effect.gen(function* () { const prepared = self.prepare() - if (!(prepared instanceof Interpreter)) { + if (!Array.isArray(prepared)) { self.finished = true - return prepared + return prepared as Result } - self.ctx = prepared - self.driver = new Driver(prepared.vm, prepared.tools, yield* Effect.context()) + const [ctx, run] = prepared as readonly [Interpreter, Run] + self.ctx = ctx + self.run_ = run + self.driver = new Driver(ctx.vm, yield* Effect.context()) if (roundTrip) self.driver.checkpoint = Effect.sync(() => self.trip()) self.startedAt = Date.now() return yield* self.execute() @@ -212,10 +263,10 @@ export class Running { const logs = self.ctx.logs return logs.length > 0 ? { logs: [...logs] } : {} } - const toolCalls = () => self.ctx.tools.calls - const failed = (error: Diagnostic): Result => ({ ok: false, error, ...logged(), toolCalls: toolCalls() }) + const metadata = () => self.run_.metadata + const failed = (error: Diagnostic): Result => ({ ok: false, error, ...logged(), metadata: metadata() }) // Set only after copy-out so timeouts cannot report invalid values as completed. - let returned: { value: DataValue; warnings: () => Array } | undefined + let returned: { value: Json; warnings: () => Array } | undefined const base = Effect.gen(function* () { const driver = self.driver @@ -225,7 +276,7 @@ export class Running { if (outcome === SUSPENDED) return self.suspended! self.finished = true if (!outcome.ok) return failed(normalizeError(outcome.error)) - const result = (outcome.value as DataValue | undefined) ?? null + const result = (outcome.value as Json | undefined) ?? null const vm = self.ctx.vm returned = { value: result, warnings: () => vm.diagnostics() } // Host work the program left running is interrupted, not awaited. @@ -236,7 +287,7 @@ export class Running { value: result, ...(warnings.length > 0 ? { warnings } : {}), ...logged(), - toolCalls: toolCalls(), + metadata: metadata(), } satisfies Result }).pipe(Effect.ensuring(Effect.suspend(() => self.driver.interrupt()))) const timeoutMs = limits.timeoutMs @@ -265,7 +316,7 @@ export class Running { ...returned.warnings(), ], ...logged(), - toolCalls: toolCalls(), + metadata: metadata(), } satisfies Result }), }), @@ -312,12 +363,9 @@ export class Running { const limits = this.setup.limits const saved = attempt(() => vm.capture({ - limits: { - timeoutMs: limits.timeoutMs ?? null, - maxToolCalls: limits.maxToolCalls ?? null, - maxOutputBytes: limits.maxOutputBytes ?? null, - }, + limits: { timeoutMs: limits.timeoutMs ?? null, maxOutputBytes: limits.maxOutputBytes ?? null }, activeMs: this.activeBefore + Date.now() - this.startedAt, + metadata: this.run_.metadata, strict: false, }), ) @@ -343,9 +391,10 @@ export class Running { return { ok: "suspended", state: snapshot } } - // The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime. States that - // hold live host values (an extension call in flight, a function an extension without `snapshot` hooks returned) - // can't round-trip, so they are skipped; a lost value anywhere else is a codec gap and fails the run. + // The round-trip test mode's checkpoint: saves the state to bytes and continues in a brand-new runtime, made with + // the run's metadata as it would be on resume. States that hold live host values (a call waiting on what its host + // function returned, a function a host function without `snapshot` hooks returned) can't round-trip, so they are + // skipped; a value with no data form anywhere else is a codec gap and fails the run. private trip(): void { const driver = this.driver if (this.trips >= ROUND_TRIP_MAX) { @@ -358,23 +407,24 @@ export class Running { } if (driver.holdsHostValues) return this.trips++ - const attempted = attempt(() => this.ctx.vm.capture({ strict: !this.setup.lossy })) - // An extension's `snapshot` hooks may refuse to save a value, which fails a suspend, not the run. + const attempted = attempt(() => this.ctx.vm.capture({ metadata: this.run_.metadata, strict: true })) + // A host function's `snapshot` hooks may refuse to save a value, which fails a suspend, not the run. if (!attempted.ok) { const error = attempted.error - if (error instanceof SnapshotError && error.message.startsWith("The extension ")) return + if (error instanceof SnapshotError && error.message.startsWith("The host function ")) return throw error } const saved = attempted.value const bytes = toBytes(saved.snapshot) this.skip = Math.floor(saved.snapshot.records.length / 200 + bytes.byteLength / 100_000) if (saved.lost.length > 0) return - const fresh = instantiate(this.setup) - restoreInto(fromBytes(bytes), fresh.vm) - for (const [key, invocation] of this.ctx.tools.invocations) fresh.tools.invocations.set(key, invocation) + const restored = fromBytes(bytes) + const run = { id: this.run_.id, metadata: header(restored).metadata } + const fresh = instantiate(this.setup, run) + restoreInto(restored, fresh.vm) this.ctx = fresh + this.run_ = run driver.vm = fresh.vm - driver.tools = fresh.tools } } @@ -461,7 +511,7 @@ const boundOutput = (result: Result, maxOutputBytes: number): Result => { const serialized = result.ok ? (JSON.stringify(result.value) ?? "null") : "" const valueBytes = result.ok ? utf8ByteLength(serialized) : 0 const valueTruncated = valueBytes > maxOutputBytes - const value: DataValue = !result.ok + const value: Json = !result.ok ? null : valueTruncated ? `${utf8Truncate(serialized, maxOutputBytes)} [result truncated: ${valueBytes} bytes exceeds the ${maxOutputBytes}-byte output limit; return a smaller value]` @@ -487,6 +537,6 @@ const boundOutput = (result: Result, maxOutputBytes: number): Result => { const warningsPart = keptWarnings.length > 0 ? { warnings: keptWarnings } : {} const logsPart = kept.length > 0 ? { logs: kept } : {} return result.ok - ? { ok: true, value, ...warningsPart, ...logsPart, truncated: true, toolCalls: result.toolCalls } - : { ok: false, error: result.error, ...logsPart, truncated: true, toolCalls: result.toolCalls } + ? { ok: true, value, ...warningsPart, ...logsPart, truncated: true, metadata: result.metadata } + : { ok: false, error: result.error, ...logsPart, truncated: true, metadata: result.metadata } } diff --git a/packages/codemode/src/runtime/extensions.ts b/packages/codemode/src/language/runtime/extensions.ts similarity index 51% rename from packages/codemode/src/runtime/extensions.ts rename to packages/codemode/src/language/runtime/extensions.ts index f0acef90f28d..e9c40edd6667 100644 --- a/packages/codemode/src/runtime/extensions.ts +++ b/packages/codemode/src/language/runtime/extensions.ts @@ -1,12 +1,14 @@ import { Effect } from "effect" -import { type Extension, isAsync, StreamingResponse } from "../extension.js" +import { DurableFn, Fn, Namespace, type Snapshot } from "../host-function.js" +import type { HostTarget } from "../host.js" import { BufferObj } from "../stdlib/buffer.js" -import { hooked } from "../tool-runtime.js" +import { StreamingResponse } from "../streaming-response.js" import { errorMessage, errorType } from "./errors.js" import type { Interpreter } from "./interpreter.js" import { createErrorValue } from "./intrinsics.js" +import { member } from "./intrinsic-names.js" import { MAX_ARRAY_LENGTH, MAX_VALUE_DEPTH } from "./limits.js" -import { type AstNode, AsyncIteratorSymbol, PendingThrow, rangeError, Throw, typeError } from "./model.js" +import { AsyncIteratorSymbol, HostFailure, PendingThrow, rangeError, Throw, typeError } from "./model.js" import { native } from "./native.js" import { define, @@ -20,9 +22,11 @@ import { DateObj, ErrorObj, HeadersObj, - PromiseObj, + HostNamespace, MapObj, Obj, + OMITTED, + PromiseObj, RegExpObj, SetObj, URLObj, @@ -30,45 +34,89 @@ import { coerceToString, type Value, } from "./objects.js" -import { describeValue, isOpaque } from "./references.js" -import { Push } from "./signals.js" -import type { ExtensionRequest } from "../scheduler.js" +import { describeValue, isOpaque, typeofValue } from "./references.js" +import type { Translate } from "../runtime.js" +import { Interruption } from "./signals.js" import { type HostOrigin, SnapshotError } from "../snapshot/codec.js" /** - * Where a value crossing from the host is: under an extension's global object (`root` undefined), whose functions are - * entry points that fire the hooks, or inside a value a function of an extension with `snapshot` hooks returned, at - * `path` (undefined inside a Map or a Set, where no path leads). + * Where a value crossing from the host is: among the run's host globals, at `global` (so a function there can be found + * again on resume), or inside a value a host function with `snapshot` hooks returned (`within`), at `path` + * (undefined inside a Map or a Set, where no path leads). */ +type Place = { + readonly global?: ReadonlyArray | undefined + readonly within?: Within | undefined + /** Whether it is among the globals at all, even where no path of names leads: inside an array, a Map or a Set. */ + readonly installed?: boolean | undefined +} + type Within = { - readonly extension: Extension - readonly root: object | undefined + readonly owner: string + readonly root: object readonly path: ReadonlyArray | undefined } /** - * The global bindings of one run's extensions. Everything crossing the boundary is converted: plain data and built-in - * wrappers are copied, a host function becomes a program function whose calls cross the same way, and a host Promise - * or Effect becomes a program promise. + * The global bindings of one run's host globals (see `Host`), and the bridge its calls cross (`ctx.hosts`). + * Everything crossing the boundary is converted: plain data and built-in wrappers are copied, a host function becomes + * a program function whose calls cross the same way, a namespace becomes a path the program reads into, and a host + * Promise or Effect becomes a program promise. */ -export const extensionGlobals = ( +export const hostGlobals = ( ctx: Interpreter, - extensions: ReadonlyArray, + globals: Readonly>, + translate: Required = { functions: "error", promises: "error" }, ): ReadonlyArray => { const builtins = ctx.builtins - const byName = new Map(extensions.map((extension) => [extension.name, extension])) - // The program functions wrapping functions inside values a hooked extension returned, by where they came from. + // The `snapshot` hooks of the host functions among the globals that have them, by label. + const owners = new Map() + const shape: Array = [] + // The program functions wrapping functions inside values a hooked host function returned, by where they came from. const origins = new WeakMap() + // The host function behind each function inside a result, so passing it back to a host function that takes + // functions hands the host its own function. The functions among the globals are left out: whatever the host runs + // around their calls (permission checks, say) runs when the program calls them, and the host could call one + // without it. + const hosted = new WeakMap() - const toHost = (value: Value, label: string, depth = 0, seen = new Set()): unknown => { + // With `waiting`, a promise that hasn't fulfilled goes there instead, and the copy is to be made again once it settles. + // `back` collects the host functions that cross back; without it they are treated like any function. + const toHost = ( + value: Value, + label: string, + depth: number, + seen: Set, + waiting: Array | undefined, + back: Array | undefined, + ): unknown => { if (depth > MAX_VALUE_DEPTH) throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`) if (isPrimitive(value)) return value + const host = value instanceof Obj ? hosted.get(value) : undefined + if (back !== undefined && host !== undefined) { + back.push(host) + return host + } + if (translate.functions === "drop" && typeofValue(value) === "function") return OMITTED + if (seen.has(value as object)) throw typeError(`${label} contains a circular value.`) + if (waiting !== undefined && value instanceof PromiseObj) { + if (value.state !== "fulfilled") { + waiting.push(value) + return undefined + } + seen.add(value) + const copied = toHost(value.result as Value, label, depth + 1, seen, waiting, back) + seen.delete(value) + return copied + } if (!(value instanceof Obj) || isOpaque(value)) { - throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to an extension.`) + throw typeError(`${label} contains ${describeValue(value)}, which cannot be passed to the host.`) } - if (seen.has(value)) throw typeError(`${label} contains a circular value.`) seen.add(value) - const next = (item: Value) => toHost(item, label, depth + 1, seen) + const next = (item: Value) => { + const copied = toHost(item, label, depth + 1, seen, waiting, back) + return copied === OMITTED && (value instanceof MapObj || value instanceof SetObj) ? undefined : copied + } if (value instanceof ErrorObj) { const name = coerceToString(get(value, "name")) const message = get(value, "message") @@ -78,7 +126,7 @@ export const extensionGlobals = ( for (const key of new Set(["cause", ...keys(value)])) { if (uncrossed.has(key) || !has(value, key)) continue const item = crossing(() => next(get(value, key))) - if (item === left) continue + if (item === left || item === OMITTED) continue Object.defineProperty(copied, key, { value: item, writable: true, @@ -94,23 +142,40 @@ export const extensionGlobals = ( return copied } - // `within` says where the value is, so a function inside it fires the hooks, or can be saved; see `Within`. - const fromHost = (value: unknown, label: string, depth = 0, seen = new Set(), within?: Within): Value => { + const fromHost = (value: unknown, label: string, depth = 0, seen = new Set(), place: Place = {}): Value => { if (depth > MAX_VALUE_DEPTH) throw typeError(`${label} exceeds the maximum value depth of ${MAX_VALUE_DEPTH}.`) if (isPrimitive(value)) return value - if (typeof value === "function") return wrap(value, label, within) + if (place.global !== undefined) { + if (value instanceof Namespace) return namespace(value, label, place.global) + if (value instanceof Fn && value.snapshot !== undefined) { + owners.set(label, value.snapshot) + shape.push(`${label} (snapshot hooks)`) + } + } + if (value instanceof Fn || value instanceof DurableFn) { + const owner = + value instanceof Fn && value.snapshot !== undefined && place.global !== undefined ? label : undefined + return wrap(value, label, { label, path: place.global, owner }) + } + if (typeof value === "function") { + const within = place.within + const made = wrap(plain(value), label, { label, path: place.global, owner: within?.owner }, value) + if (place.installed !== true) hosted.set(made, value) + if (within !== undefined && within.path !== undefined) { + origins.set(made, { owner: within.owner, root: within.root, path: within.path, label }) + } + return made + } if (value !== null && typeof value === "object") { const next = (item: unknown, path: string, key?: string | number) => - fromHost( - item, - path, - depth + 1, - seen, - within && { - ...within, - path: key === undefined || within.path === undefined ? undefined : [...within.path, key], + fromHost(item, path, depth + 1, seen, { + global: place.global !== undefined && typeof key === "string" ? [...place.global, key] : undefined, + within: place.within && { + ...place.within, + path: key === undefined || place.within.path === undefined ? undefined : [...place.within.path, key], }, - ) + installed: place.installed, + }) // Nothing a host value hands back is kept: results are read as primitives or copied. Where the value's own // method could hand back host storage or an object (`getTime`, `slice`, `map`), its internal data is read. if (value instanceof Date) return new DateObj(builtins.Date, Date.prototype.getTime.call(value)) @@ -234,48 +299,133 @@ export const extensionGlobals = ( throw typeError(`${label} produced ${describeHost(value)}, which the program cannot hold.`) } - // A host function as a program function. Arguments cross in; for a global or a global object's function, the - // `extension.before` hook runs before the host function is called, so a call it denies never reaches the host. The - // host's result, or what it threw, crosses out so the program catches a copy, and `extension.after` sees how the - // call ended. Functions inside results are part of a value's API and skip the hooks; one inside a value an extension - // with `snapshot` hooks returned is known by where it came from, so a suspended run can save it. - const wrap = (value: Function, label: string, within: Within | undefined) => { - // The host function's own `name` and `length` can be anything, even getters: each is read once, and only text - // and a count cross. - const name: unknown = value.name - const length: unknown = value.length - const extension = within?.extension.name - const root = within?.root - const wrapped = native(builtins, { - runtime: "extension", + // A host function as a program function: its calls go through the run's host calls (see `HostCalls.call`). The + // name and length come from the host function, read once, and only as text and a count. + // `callee` is the program function calling it, when that is not the one made here (see `rewrap`). + const wrap = ( + fn: Fn | DurableFn, + label: string, + target: HostTarget, + source?: Function, + callee?: Obj, + ) => { + const named = source ?? (fn instanceof Fn ? fn.run : undefined) + const own: unknown = named?.name + // A host function made inline has no name of its own: it is named by where it is. + const name = + source === undefined && (typeof own !== "string" || own === "") ? label.slice(label.lastIndexOf(".") + 1) : own + const length: unknown = named?.length ?? 0 + const made = native(builtins, { + runtime: "host", name: typeof name === "string" ? name : "", length: typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : 0, - call: (_, values, site) => { - const args = values.map((item, index) => toHost(item, `Argument ${index + 1} to ${label}`)) - const invocation = - within !== undefined && root === undefined ? { extension: extension!, name: label, args } : undefined - // A denied call fails the way the global would: an async global rejects its promise, a sync one throws. - if (isAsync(value)) { - return pending(ctx, { stage: "run", label, extension, invocation, host: value, args, threw: false }, site) - } - return new Push("extension call", [value, args, label, invocation, extension]) + call: (_, values, site) => ctx.vm.hosts.call(fn, values, site, target, callee ?? made), + ...(fn instanceof Fn && fn.constructible + ? { construct: (values, _, site) => ctx.vm.hosts.call(fn, values, site, target, callee ?? made) } + : {}), + }) + return made + } + + // A namespace among the globals: a path the program reads into, whose calls ask `resolve` for the function. + const namespace = (value: Namespace, label: string, global: ReadonlyArray) => { + ctx.hostNamespaces.set(label, { + keys: (path) => value.keys(path), + call: (path, values, site) => { + const found = value.resolve(path) + if (found instanceof HostFailure) throw found + return ctx.vm.hosts.call( + found, + values, + site, + { + label: path.reduce((at, key) => member(at, key), label), + path: [...global, ...path], + owner: undefined, + }, + new HostNamespace(label, path), + ) }, }) - if (root !== undefined && within!.path !== undefined) { - origins.set(wrapped, { extension: extension!, root, path: within!.path, label }) + shape.push(`${label} (namespace)`) + return new HostNamespace(label, []) + } + + const failure = (reason: unknown, label: string): unknown => { + if ( + reason instanceof HostFailure || + reason instanceof PendingThrow || + reason instanceof Interruption || + reason instanceof Throw + ) { + return reason + } + try { + return new Throw(fromHost(reason, label)) + } catch (error) { + return error } - return wrapped + } + + const hooks = (owner: string) => { + const snapshot = owners.get(owner) + if (snapshot === undefined) throw new SnapshotError(`The host function ${owner} has no snapshot hooks.`) + return snapshot } ctx.hosts = { - extensions: extensions.map((extension) => ({ name: extension.name, hooked: extension.snapshot !== undefined })), + shape, + toHost: (values, label, waiting, back) => + values.map((item, index) => { + const copied = toHost( + item, + `Argument ${index + 1} to ${label}`, + 0, + new Set(), + translate.promises === "await" ? waiting : undefined, + back, + ) + return copied === OMITTED ? undefined : copied + }), + fromHost: (value, label, owner) => + fromHost(value, label, 0, new Set(), { + within: + owner !== undefined && ((typeof value === "object" && value !== null) || typeof value === "function") + ? { owner, root: value, path: [] } + : undefined, + }), + failure, + isPending: (value) => value instanceof Promise || Effect.isEffect(value), + invoke: (fn, args, key, label) => { + let result: unknown + try { + result = fn.run(args, { key, suspending: () => false }) + } catch (reason) { + throw failure(reason, label) + } + if (Effect.isEffect(result)) return { kind: "block", work: result } + if (result instanceof Promise) return { kind: "pending", work: result } + return { kind: "value", value: result } + }, + resolve: (path) => { + let at: unknown = globals + for (let index = 0; index < path.length; index++) { + if (at instanceof Namespace) { + const found = at.resolve(path.slice(index)) + return found instanceof HostFailure ? undefined : found + } + if (typeof at !== "object" || at === null || !Object.hasOwn(at, path[index]!)) return undefined + at = (at as { readonly [key: string]: unknown })[path[index]!] + } + return at instanceof Fn || at instanceof DurableFn ? at : undefined + }, origin: (value) => origins.get(value), - encode: (name, root) => hostStep(name, "save", () => hooks(name).encode(root)), - decode: (name, data) => { - const root = hostStep(name, "restore", () => hooks(name).decode(data)) + encode: (owner, root) => hostStep(owner, "save", () => hooks(owner).encode(root)), + decode: (owner, data) => { + const root = hostStep(owner, "restore", () => hooks(owner).decode(data)) if ((typeof root !== "object" && typeof root !== "function") || root === null) { throw new SnapshotError( - `The extension "${name}" restored a value it returned as ${typeof root}, not an object.`, + `The host function ${owner} restored a value it returned as ${typeof root}, not an object.`, ) } return root @@ -290,127 +440,44 @@ export const extensionGlobals = ( ) if (typeof host !== "function") { throw new SnapshotError( - `The extension "${origin.extension}" restored a value with no function at ${origin.label}, so this state can't resume.`, + `The host function ${origin.owner} restored a value with no function at ${origin.label}, so this state can't resume.`, ) } - const made = wrap(host, origin.label, { - extension: byName.get(origin.extension)!, - root: origin.root, - path: origin.path, - }) + const made = wrap( + plain(host), + origin.label, + { label: origin.label, path: undefined, owner: origin.owner }, + host, + target, + ) // The restored function keeps its own properties; only what calling it does comes from the new one. Object.assign(target, { tag: made.tag, call: made.call, construct: made.construct, callback: made.callback }) origins.set(target, origin) + hosted.set(target, host) }, } - const hooks = (name: string) => { - const snapshot = byName.get(name)?.snapshot - if (snapshot === undefined) throw new SnapshotError(`The extension "${name}" has no snapshot hooks.`) - return snapshot - } - - // The Effect each step of an extension call runs as, with the program's services in its requirements. - ctx.extensionWork = (request: ExtensionRequest) => { - const label = request.label - const call = request.invocation - const cross = (reason: unknown) => new Throw(fromHost(reason, label)) - const hooks = ctx.tools.hooks - const before = call && hooks["extension.before"]?.(call).pipe(Effect.mapError(cross)) - if (request.stage === "before") return before ?? Effect.void - const after = call && hooks["extension.after"] - const extension = request.extension === undefined ? undefined : byName.get(request.extension) - // What a function returns is a value of its extension's, which its `snapshot` hooks save, when it has them. - const settle = (effect: Effect.Effect) => - (after ? hooked(call, undefined, after, effect) : effect).pipe( - Effect.mapBoth({ - onFailure: cross, - onSuccess: (result) => - fromHost( - result, - label, - 0, - new Set(), - extension?.snapshot !== undefined && - ((typeof result === "object" && result !== null) || typeof result === "function") - ? { extension, root: result, path: [] } - : undefined, - ), - }), - ) - if (request.stage === "settle") return settle(outcome(request.host, request.threw)) - return Effect.andThen( - before ?? Effect.void, - settle(Effect.suspend(() => outcome(...callHost(request.host as Function, request.args)))), - ) - } - - return extensions.flatMap((extension) => - Object.entries(extension.globals).map( - ([name, value]) => - [name, fromHost(value, name, 0, new Set(), { extension, root: undefined, path: undefined })] as const, - ), + return Object.entries(globals).map( + ([name, value]) => [name, fromHost(value, name, 0, new Set(), { global: [name], installed: true })] as const, ) } -// An asynchronous host call: the program holds a promise that settles when the host's work does. -const pending = (ctx: Interpreter, call: ExtensionRequest, site: AstNode | undefined) => { - const resolving = ctx.vm.scheduler.resolving(ctx.vm.scheduler.newPromise(), site) - ctx.vm.hosts.host({ kind: "extension", call }, { label: call.label, result: "value", resolving, site }) - return resolving.promise -} - -/** - * Calls a synchronous extension function's host function, once its `extension.before` hook allowed it: a promise for - * a returned Promise, or the request that settles what it returned or threw. A returned Effect settles through that - * request too, so the program blocks until it completes, as `fs.readFileSync` does. - */ -export const startExtensionCall = ( - ctx: Interpreter, - host: Function, - args: ReadonlyArray, - call: Pick, - site: AstNode | undefined, -): PromiseObj | ExtensionRequest => { - const [result, threw] = callHost(host, args) - const request = { stage: "settle", ...call, host: result, args, threw } as const - if (!threw && result instanceof Promise) return pending(ctx, request, site) - return request -} +// A plain host function (one inside a result, or among the globals) is called like a `sync` host function that takes +// functions. +const plain = (value: Function) => new Fn((args) => value(...args), "sync", false, undefined, false, true) -// Runs an extension's `snapshot` hook; what goes wrong is the state's problem, said as such. -const hostStep = (extension: string, what: "save" | "restore", step: () => A): A => { +// Runs a host function's `snapshot` hook; what goes wrong is the state's problem, said as such. +const hostStep = (owner: string, what: "save" | "restore", step: () => A): A => { try { return step() } catch (error) { if (error instanceof SnapshotError) throw error throw new SnapshotError( - `The extension "${extension}" couldn't ${what} a value it returned: ${error instanceof Error ? error.message : String(error)}`, + `The host function ${owner} couldn't ${what} a value it returned: ${error instanceof Error ? error.message : String(error)}`, ) } } -/** Calls a host function: what it returned, or what it threw. */ -const callHost = (value: Function, args: ReadonlyArray): [result: unknown, threw: boolean] => { - try { - return [value.apply(undefined, args), false] - } catch (reason) { - return [reason, true] - } -} - -/** - * What a host function's call settles as: a returned Effect or Promise settles later; a returned value or a throw - * settles now. - */ -const outcome = (result: unknown, threw: boolean): Effect.Effect => { - if (threw) return Effect.fail(result) - // Extension.Services adds an Effect global's requirements to R. - if (Effect.isEffect(result)) return result as Effect.Effect - if (result instanceof Promise) return Effect.tryPromise({ try: () => result, catch: (reason) => reason }) - return Effect.succeed(result) -} - /** * An error crosses as its name, message, `cause`, and own enumerable fields, such as Node's `code`, `errno`, * `syscall`, and `path`. `stack` stays on its own side, `message` crosses once as text, and no field may shadow an diff --git a/packages/codemode/src/runtime/generators.ts b/packages/codemode/src/language/runtime/generators.ts similarity index 100% rename from packages/codemode/src/runtime/generators.ts rename to packages/codemode/src/language/runtime/generators.ts diff --git a/packages/codemode/src/runtime/globals.ts b/packages/codemode/src/language/runtime/globals.ts similarity index 96% rename from packages/codemode/src/runtime/globals.ts rename to packages/codemode/src/language/runtime/globals.ts index fd92250b0e11..57a560ac31c2 100644 --- a/packages/codemode/src/runtime/globals.ts +++ b/packages/codemode/src/language/runtime/globals.ts @@ -16,7 +16,6 @@ import { headersGlobal } from "../stdlib/headers.js" import { iteratorGlobals } from "../stdlib/iterator.js" import { coercion } from "../stdlib/value.js" import { base64Global, cryptoGlobal, structuredCloneGlobal } from "../stdlib/web.js" -import { ToolReference } from "../tool-runtime.js" import { errorGlobal } from "./errors.js" import { errorTypes } from "./intrinsics.js" import { constants, constructor, methods, native, receiver } from "./native.js" @@ -80,8 +79,6 @@ type Factory = (ctx: Interpreter) => Value // A table rather than a list so the names are known before any runtime exists. const table: Record = { - tools: () => new ToolReference([]), - search: (ctx) => native(ctx.builtins, { name: "search", call: (_, args) => ctx.search(args), callback: false }), require: (ctx) => native(ctx.builtins, { name: "require", diff --git a/packages/codemode/src/runtime/interpreter.ts b/packages/codemode/src/language/runtime/interpreter.ts similarity index 71% rename from packages/codemode/src/runtime/interpreter.ts rename to packages/codemode/src/language/runtime/interpreter.ts index f546d804600a..55fec9169ddb 100644 --- a/packages/codemode/src/runtime/interpreter.ts +++ b/packages/codemode/src/language/runtime/interpreter.ts @@ -1,5 +1,3 @@ -import type { ToolRuntime } from "../tool-runtime.js" -import type { ExtensionRequest } from "../scheduler.js" import type { HostBridge } from "../snapshot/codec.js" import { Vm } from "../vm.js" import { globals } from "./globals.js" @@ -7,14 +5,21 @@ import type { Builtins } from "./intrinsics.js" import { IntrinsicNames } from "./intrinsic-names.js" import { type AstNode, type Binding, PendingThrow } from "./model.js" import { moduleName, resolveModule } from "./modules.js" -import { define, entries, makingIntrinsics, nativeRule, Obj, type Value } from "./objects.js" +import { define, entries, type HostNamespace, makingIntrinsics, nativeRule, Obj, type Value } from "./objects.js" +import type { HostResult } from "../host.js" import { Prelude } from "../prelude/prelude.js" -import { Push } from "./signals.js" import type { NodeId, Source } from "./source.js" -/** One program execution: the tool bridge, the VM that runs it, captured logs, and the global scope built once. */ +/** What a host namespace does, by the global it is under; see `HostNamespace`. */ +export type NamespaceHandler = { + /** The names directly under `path`, for `Object.keys` and `for...in`. */ + readonly keys: (path: ReadonlyArray) => ReadonlyArray + /** Calls the function at `path`. */ + readonly call: (path: ReadonlyArray, args: Array, site: AstNode | undefined) => HostResult +} + +/** One program execution: the VM that runs it, captured logs, and the global scope built once. */ export class Interpreter { - readonly tools: ToolRuntime readonly builtins: Builtins readonly logs: Array /** Runs program code, and keeps the job queue, promises, and host calls the program waits on. */ @@ -23,10 +28,10 @@ export class Interpreter { readonly intrinsics: IntrinsicNames /** The built-ins written in Code Mode, and the scope they close over. */ readonly prelude: Prelude - /** Makes the Effect an extension function's host work runs as; set by the extension bridge. */ - extensionWork: ((request: ExtensionRequest) => unknown) | undefined - /** Saves and restores values returned by extensions with `snapshot` hooks; set by the extension bridge. */ + /** How the program meets its host globals: calls, values crossing, and saving what they return; see `hostGlobals`. */ hosts: HostBridge | undefined + /** The host namespaces among the globals, by the global they are under. */ + readonly hostNamespaces = new Map() /** The parsed sources this execution runs, by id, so a `NodeId` resolves to its node. */ readonly sources = new Map() /** The global bindings: the intrinsics, the host's globals, and the program's bindings. */ @@ -35,7 +40,6 @@ export class Interpreter { private readonly namespaces = new Map() constructor(options: { - readonly tools: ToolRuntime readonly builtins: Builtins readonly logs?: Array /** The host's globals (extensions, a test harness): made again, the same way, when a saved run is restored. */ @@ -43,7 +47,6 @@ export class Interpreter { /** Data bound for this program, part of its saved state; host globals of the same name win. */ readonly bindings?: (ctx: Interpreter) => ReadonlyArray }) { - this.tools = options.tools this.builtins = options.builtins this.logs = options.logs ?? [] // Program code has no receiver: top-level `this` is undefined, as in a module. @@ -84,7 +87,7 @@ export class Interpreter { if (module === undefined) throw new PendingThrow( "Error", - `Cannot import "${specifier}": it isn't available in Code Mode, which can't load packages or files. Use the provided globals, or call tools through \`tools\`.`, + `Cannot import "${specifier}": it isn't available here, where packages and files can't be loaded. Use the provided globals.`, site, ) this.modules.set(name, module) @@ -107,13 +110,24 @@ export class Interpreter { return namespace } + /** The names directly under a namespace path. */ + namespaceKeys(value: HostNamespace): ReadonlyArray { + return this.namespaceHandler(value).keys(value.path) + } + + /** Calls the function a namespace path names. */ + callNamespace(value: HostNamespace, args: Array, site: AstNode | undefined): HostResult { + return this.namespaceHandler(value).call(value.path, args, site) + } + + private namespaceHandler(value: HostNamespace): NamespaceHandler { + const handler = this.hostNamespaces.get(value.root) + if (handler === undefined) throw new PendingThrow("TypeError", `The namespace ${value.root} is not available.`) + return handler + } + /** The node a `NodeId` names, in one of this execution's sources. */ node(id: NodeId): AstNode | undefined { return this.sources.get(id.source)?.node(id) } - - /** The built-in search: its result is usable directly, and can also be awaited or chained like a Promise. */ - search(args: Array): Push { - return new Push("search", [args]) - } } diff --git a/packages/codemode/src/runtime/intrinsic-names.ts b/packages/codemode/src/language/runtime/intrinsic-names.ts similarity index 97% rename from packages/codemode/src/runtime/intrinsic-names.ts rename to packages/codemode/src/language/runtime/intrinsic-names.ts index eb8c8a7093d6..9f31a237dd0d 100644 --- a/packages/codemode/src/runtime/intrinsic-names.ts +++ b/packages/codemode/src/language/runtime/intrinsic-names.ts @@ -182,7 +182,8 @@ const symbolNames = new Map([ [prototypeKey, ".[[Prototype]]"], ]) -const member = (base: string, key: string | symbol): string => { +/** How a program reads `key` from `base`: `base.key`, or `base["key"]` for a key that isn't a name. */ +export const member = (base: string, key: string | symbol): string => { if (typeof key === "symbol") return `${base}${symbolNames.get(key) ?? `[${String(key)}]`}` return /^[A-Za-z_$][\w$]*$/.test(key) ? `${base}.${key}` : `${base}[${JSON.stringify(key)}]` } diff --git a/packages/codemode/src/runtime/intrinsics.ts b/packages/codemode/src/language/runtime/intrinsics.ts similarity index 100% rename from packages/codemode/src/runtime/intrinsics.ts rename to packages/codemode/src/language/runtime/intrinsics.ts diff --git a/packages/codemode/src/runtime/limits.ts b/packages/codemode/src/language/runtime/limits.ts similarity index 100% rename from packages/codemode/src/runtime/limits.ts rename to packages/codemode/src/language/runtime/limits.ts diff --git a/packages/codemode/src/runtime/model.ts b/packages/codemode/src/language/runtime/model.ts similarity index 77% rename from packages/codemode/src/runtime/model.ts rename to packages/codemode/src/language/runtime/model.ts index e40187d78cbf..2f115b5d7705 100644 --- a/packages/codemode/src/runtime/model.ts +++ b/packages/codemode/src/language/runtime/model.ts @@ -1,8 +1,21 @@ import type { Node } from "acorn" import type { ErrorType } from "./intrinsics.js" -import type { DiagnosticKind } from "../codemode.js" import type { ErrorObj, Value } from "./objects.js" +/** + * A diagnostic's category: one of the language's (`ParseError`, `UnsupportedSyntax`, `InvalidDataValue`, + * `TimeoutExceeded`, `ExecutionFailure`, `Truncated`), or one a host function gives with `Host.fail`. + */ +export type DiagnosticKind = string + +/** A program's failure or warning, as plain data. */ +export type Diagnostic = { + readonly kind: DiagnosticKind + readonly message: string + readonly location?: { readonly line: number; readonly column: number } + readonly suggestions?: ReadonlyArray +} + /** Any parsed node; the compiler narrows on `type`, and diagnostics read `loc`. */ export type AstNode = Node @@ -38,6 +51,20 @@ export class PendingThrow { } } +/** + * A failure a host function raises with its own diagnostic `kind` and `suggestions` (see `Host.fail`). It reaches the + * program as an Error with its message, and a result as a diagnostic of its kind. + */ +export class HostFailure extends Error { + constructor( + readonly kind: DiagnosticKind, + message: string, + readonly suggestions: ReadonlyArray = [], + ) { + super(message) + } +} + /** * A host value a restored run cannot have back: a function an extension returned, or what a host function returned, * which have no data form. Using it fails with `lostError`. diff --git a/packages/codemode/src/runtime/modules.ts b/packages/codemode/src/language/runtime/modules.ts similarity index 100% rename from packages/codemode/src/runtime/modules.ts rename to packages/codemode/src/language/runtime/modules.ts diff --git a/packages/codemode/src/runtime/native.ts b/packages/codemode/src/language/runtime/native.ts similarity index 100% rename from packages/codemode/src/runtime/native.ts rename to packages/codemode/src/language/runtime/native.ts diff --git a/packages/codemode/src/runtime/objects.ts b/packages/codemode/src/language/runtime/objects.ts similarity index 96% rename from packages/codemode/src/runtime/objects.ts rename to packages/codemode/src/language/runtime/objects.ts index ad09b37256bd..11e87b3cba8f 100644 --- a/packages/codemode/src/runtime/objects.ts +++ b/packages/codemode/src/language/runtime/objects.ts @@ -1,4 +1,3 @@ -import { ToolReference } from "../tool-runtime.js" import type { Builtins } from "./intrinsics.js" import { checkArrayLength } from "./limits.js" import { @@ -63,6 +62,9 @@ export const readonly: Attributes = { writable: false, enumerable: false, config /** Constants such as `Math.PI` and a constructor's `prototype`. */ export const frozen: Attributes = { writable: false, enumerable: false, configurable: false } +/** What a child converted by `toHost` gives to be left out: an object's key is skipped, an array's item is `null`. */ +export const OMITTED: unique symbol = Symbol("omitted") + /** * An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a * built-in kind of object prints, coerces, iterates, and crosses to the host. @@ -114,9 +116,11 @@ export class Obj { /** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */ toHost(item: (value: Value) => unknown): unknown { return Object.fromEntries( - entries(this) - .filter(([key]) => key !== "__proto__") - .map(([key, value]) => [key, item(value)]), + entries(this).flatMap(([key, value]) => { + if (key === "__proto__") return [] + const copied = item(value) + return copied === OMITTED ? [] : [[key, copied]] + }), ) } @@ -146,7 +150,10 @@ export class Arr extends Obj { return `[${this.items.map(item).join(",")}]` } override toHost(item: (value: Value) => unknown) { - return this.items.map(item) + return this.items.map((value) => { + const copied = item(value) + return copied === OMITTED ? null : copied + }) } override iterator(builtins: Builtins) { return new Position(this, "values", builtins.Array) @@ -258,7 +265,7 @@ export type NativeOptions = { } /** The natives still made at run time, each holding state in a host closure that cannot be saved yet. */ -export type RuntimeNative = "extension" +export type RuntimeNative = "host" /** * Natives are made only with the intrinsics, so each one has an intrinsic name and run state never holds a host @@ -693,16 +700,28 @@ export class Bytes extends Wrapper { } } +/** + * A path into a host namespace (see `Host.namespace`): the global it is under, `root`, and the property names read + * from it. Reading a property makes a longer path, and calling one asks the namespace for the function there. It is not + * an `Obj`, so no object operation reaches it by accident; the few that apply ask it explicitly. + */ +export class HostNamespace { + constructor( + readonly root: string, + readonly path: ReadonlyArray, + ) {} +} + /** Every value a program can hold. Host values never appear here; they are copied in at the boundaries. */ -export type Value = string | number | boolean | null | undefined | symbol | Obj | ToolReference +export type Value = string | number | boolean | null | undefined | symbol | Obj | HostNamespace /** ToString without consulting program-defined methods. */ export const coerceToString = (value: Value): string => (value instanceof Obj ? value.toString() : String(value)) -/** ToNumber without consulting program-defined methods; tool references are not numbers. */ +/** ToNumber without consulting program-defined methods; namespace paths are not numbers. */ export const coerceToNumber = (value: Value): number => { if (value instanceof Obj) return value.toNumber() - return value instanceof ToolReference ? Number.NaN : Number(value) + return value instanceof HostNamespace ? Number.NaN : Number(value) } /** ToIntegerOrInfinity: NaN is 0, fractions truncate. */ @@ -713,7 +732,7 @@ export const coerceToInteger = (value: Value): number => { /** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */ export const isRuntimeReference = (value: Value): boolean => - value instanceof Opaque || value instanceof Wrapper || value instanceof ToolReference + value instanceof Opaque || value instanceof Wrapper || value instanceof HostNamespace const MAX_ARRAY_INDEX = 4_294_967_295 diff --git a/packages/codemode/src/runtime/operations.ts b/packages/codemode/src/language/runtime/operations.ts similarity index 93% rename from packages/codemode/src/runtime/operations.ts rename to packages/codemode/src/language/runtime/operations.ts index 93d772b72e84..a0b9464ff4f5 100644 --- a/packages/codemode/src/runtime/operations.ts +++ b/packages/codemode/src/language/runtime/operations.ts @@ -1,5 +1,4 @@ import type { Expression, MemberExpression, NewExpression, Super } from "acorn" -import { ToolReference } from "../tool-runtime.js" import { type AstNode, invalidData, typeError } from "./model.js" import { checkStringLength } from "./limits.js" import { locate } from "./errors.js" @@ -14,6 +13,7 @@ import { has, hasOwn, hasPrototype, + HostNamespace, IteratorObj, keys, Obj, @@ -31,6 +31,9 @@ import { enumerableSource } from "../stdlib/object.js" // The synchronous semantics the VM's ops share: operators, property references, and the checks around them. Nothing // here runs program code. +/** Orders two strings by UTF-16 code units, as `Array.prototype.sort` does by default. */ +export const compareText = (left: string, right: string) => (left < right ? -1 : left > right ? 1 : 0) + /** The binary operators that convert object operands through ToPrimitive before acting on primitives. */ const primitiveOperators = new Set([ "+", @@ -160,7 +163,7 @@ export const applyBinaryOperator = (operator: string, lhs: Value, rhs: Value, no } } -// IsLooselyEqual: objects (including functions and tool references) compare by identity, and a nullish +// IsLooselyEqual: objects (including functions and namespace paths) compare by identity, and a nullish // primitive never equals an object. const looselyEqual = (lhs: Value, rhs: Value, node: AstNode): boolean => { const lhsObject = lhs !== null && typeof lhs === "object" @@ -183,7 +186,7 @@ export const operatorConversion = ( ): { readonly kind: "key" } | { readonly kind: "primitive"; readonly hint: "default" | "number" } | undefined => { // `in` checks the right operand before ToPropertyKey on the left, so a bad right side wins over a bad key. if (operator === "in" && lhs instanceof Obj && !isOpaque(lhs) && rhs instanceof Obj) return { kind: "key" } - // IsLooselyEqual converts only an object facing a non-nullish primitive; two objects (including tool + // IsLooselyEqual converts only an object facing a non-nullish primitive; two objects (including namespace // references, which are not Obj) compare by identity. const equality = operator === "==" || operator === "!=" const other = lhs instanceof Obj ? rhs : lhs @@ -226,7 +229,7 @@ export const requireIteratorMethod = (value: Value, context: string, node?: AstN // for...in over null/undefined iterates nothing, like JS. After the own keys come each prototype's enumerable // string keys, skipping any key a nearer level has, even as a non-enumerable property. export const forInKeys = (ctx: Interpreter, value: Value, node: AstNode): Array => { - if (value instanceof ToolReference) return [...ctx.tools.keys(value.path)] + if (value instanceof HostNamespace) return [...ctx.namespaceKeys(value)] if (value === null || value === undefined) return [] const source = enumerableSource(ctx, "for...in", value, node) const chain = [source] @@ -256,12 +259,12 @@ export const resolveProperty = ( key: PropertyKey, objectNode: AstNode, propertyNode: AstNode, -): MemberReference | ToolReference | { value: Value } => { - if (objectValue instanceof ToolReference) { +): MemberReference | HostNamespace | { value: Value } => { + if (objectValue instanceof HostNamespace) { if (typeof key !== "string") { - throw typeError("Tool paths must use string property names.", propertyNode) + throw typeError(`${objectValue.root} paths must use string property names.`, propertyNode) } - return new ToolReference([...objectValue.path, key]) + return new HostNamespace(objectValue.root, [...objectValue.path, key]) } if (objectValue instanceof Obj) return { target: objectValue, key, receiver: objectValue } @@ -284,7 +287,7 @@ export const resolveProperty = ( // One destructured property, read the way a member expression would read it (primitives use their prototype). export const destructuredProperty = (builtins: Builtins, source: Value, key: PropertyKey, node: AstNode): Value => { const reference = resolveProperty(builtins, source, key, node, node) - if (reference instanceof ToolReference) return reference + if (reference instanceof HostNamespace) return reference if ("value" in reference) return reference.value return readProperty(reference.target, reference.key, node, reference.receiver) } @@ -293,7 +296,7 @@ export const readReference = (reference: MemberReference, node: MemberExpression // Reject unknown promise properties so a missing await cannot hide. if (reference.target instanceof PromiseObj && !has(reference.target, reference.key)) { throw invalidData( - "This value is an un-awaited Promise; await it first - e.g. `const result = await tools.ns.tool(...)`.", + "This value is an un-awaited Promise; await it first - e.g. `const result = await load(...)`.", node.object, ) } @@ -316,10 +319,10 @@ export const readProperty = ( /** The reference an assignment or update writes through: only a data field of an object. */ export const writableReference = ( - reference: MemberReference | ToolReference | { value: Value } | symbol, + reference: MemberReference | HostNamespace | { value: Value } | symbol, node: MemberExpression, ): MemberReference => { - if (typeof reference === "symbol" || reference instanceof ToolReference || "value" in reference) { + if (typeof reference === "symbol" || reference instanceof HostNamespace || "value" in reference) { throw typeError("Only data fields may be assigned.", node) } if (reference.receiver !== reference.target) { diff --git a/packages/codemode/src/runtime/promises.ts b/packages/codemode/src/language/runtime/promises.ts similarity index 99% rename from packages/codemode/src/runtime/promises.ts rename to packages/codemode/src/language/runtime/promises.ts index 0b91eebb91d0..73aa0382c9e4 100644 --- a/packages/codemode/src/runtime/promises.ts +++ b/packages/codemode/src/language/runtime/promises.ts @@ -248,7 +248,7 @@ const reactionHandler = (value: Value, method: string): Callable | undefined => if (isSupportedCallback(value)) return value if (typeofValue(value) === "function") { throw typeError( - `${method} cannot use this callable as a handler; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`, + `${method} cannot use this callable as a handler; wrap it in an arrow function, e.g. (value) => fn(value).`, ) } return undefined diff --git a/packages/codemode/src/runtime/references.ts b/packages/codemode/src/language/runtime/references.ts similarity index 85% rename from packages/codemode/src/runtime/references.ts rename to packages/codemode/src/language/runtime/references.ts index c2c4a25bcd5c..1a34460c65ce 100644 --- a/packages/codemode/src/runtime/references.ts +++ b/packages/codemode/src/language/runtime/references.ts @@ -1,9 +1,8 @@ -import { ToolReference } from "../tool-runtime.js" import { invalidData } from "./model.js" -import { Callable, getOwn, isRuntimeReference, Obj, Opaque, ownKeys, type Value } from "./objects.js" +import { Callable, getOwn, HostNamespace, isRuntimeReference, Obj, Opaque, ownKeys, type Value } from "./objects.js" /** Interpreter machinery that is never data, unlike a Date or Map, which cross some boundaries as copies. */ -export const isOpaque = (value: Value): boolean => value instanceof Opaque || value instanceof ToolReference +export const isOpaque = (value: Value): boolean => value instanceof Opaque || value instanceof HostNamespace // Depth-first search over a value tree. `match` stops the walk; `skip` prunes a subtree without matching it. const find = ( @@ -54,12 +53,12 @@ export const rejectCircularInsertion = ( export const describeValue = (value: Value): string => { if (value === null || value === undefined) return String(value) if (value instanceof Obj) return value.describe - if (value instanceof ToolReference) return "a tool reference" + if (value instanceof HostNamespace) return "a namespace reference" return `a ${typeof value}` } export const typeofValue = (value: Value): string => { if (value instanceof Callable) return "function" - if (value instanceof ToolReference) return value.path.length > 0 ? "function" : "object" + if (value instanceof HostNamespace) return value.path.length > 0 ? "function" : "object" return typeof value } diff --git a/packages/codemode/src/runtime/signals.ts b/packages/codemode/src/language/runtime/signals.ts similarity index 98% rename from packages/codemode/src/runtime/signals.ts rename to packages/codemode/src/language/runtime/signals.ts index 01af24897cf5..44cd724b8f6f 100644 --- a/packages/codemode/src/runtime/signals.ts +++ b/packages/codemode/src/language/runtime/signals.ts @@ -29,8 +29,6 @@ export type PushKind = | "Promise executor" | "Promise.try" | "Promise combinator" - | "search" - | "extension call" /** Continue a suspended generator's frame with `request`: what `next`/`return`/`throw` return once it can run. */ export class Resume { diff --git a/packages/codemode/src/runtime/source.ts b/packages/codemode/src/language/runtime/source.ts similarity index 100% rename from packages/codemode/src/runtime/source.ts rename to packages/codemode/src/language/runtime/source.ts diff --git a/packages/codemode/src/runtime/table.ts b/packages/codemode/src/language/runtime/table.ts similarity index 100% rename from packages/codemode/src/runtime/table.ts rename to packages/codemode/src/language/runtime/table.ts diff --git a/packages/codemode/src/scheduler.ts b/packages/codemode/src/language/scheduler.ts similarity index 76% rename from packages/codemode/src/scheduler.ts rename to packages/codemode/src/language/scheduler.ts index 3d372013db8a..59ecfd1c1f8b 100644 --- a/packages/codemode/src/scheduler.ts +++ b/packages/codemode/src/language/scheduler.ts @@ -1,5 +1,6 @@ import type { Json } from "./data.js" -import type { ExtensionInvocation } from "./tool-runtime.js" +import type { DurableFn, Fn } from "./host-function.js" +import type { HostTarget } from "./host.js" import { locate } from "./runtime/errors.js" import type { AstNode } from "./runtime/model.js" import type { Callable, Cursor, GeneratorObj, Obj, PromiseObj, Value } from "./runtime/objects.js" @@ -67,61 +68,55 @@ export type Job = | { readonly kind: "thenable"; readonly promise: PromiseObj; readonly thenable: Obj; readonly then: Value } /** - * What host work a call asks for, as data the driver turns into an Effect: a tool call, a search, or a step of an - * extension function's call (see `ExtensionRequest`). + * How a host call runs, from its function's options (see `Host.Type`): `sync` and `sync-thenable` calls block the job + * or settle a promise for what the function left pending, `async` calls run concurrently, `idempotent` ones may run + * again after a suspend, and `durable` ones keep running across it. */ -export type HostRequest = - | { readonly kind: "tool"; readonly path: ReadonlyArray; readonly args: Array } - | { readonly kind: "search"; readonly args: Array } - | { readonly kind: "extension"; readonly call: ExtensionRequest } - -/** - * One step of a call to an extension function: its `extension.before` hook (`before`), settling what the host function - * returned or threw (`settle`: `host` is that), or calling an async host function and settling its result (`run`: - * `host` is the function). `invocation` is what the hooks see, for a global or a global object's function. The host - * function and what it returns are live host values with no data form, dropped once the call completes. - */ -export type ExtensionRequest = { - readonly stage: "before" | "settle" | "run" - readonly label: string - readonly extension?: string | undefined - invocation: ExtensionInvocation | undefined - host: unknown - args: ReadonlyArray - readonly threw: boolean -} +export type HostPolicy = "sync" | "sync-thenable" | "async" | "idempotent" | "durable" /** * Host work the program waits on. The VM queues it and hands it to the driver at its next stop; the driver runs it and * reports how it ended with `Vm.complete`. A concurrent call's outcome waits in the inbox and settles its promise in a - * later job; a blocking call stops the VM, and no other job runs until it ends. + * later job; a blocking call stops the VM, and no other job runs until it ends. Its live parts, the host function and + * what it returned, are never saved: a resumed run finds the function again by `path`. */ export class HostCall { /** - * Where the call is: `queued` (written ahead, not started), `running` (or in `start` for a durable tool), - * `started` (a durable tool whose `start` returned its `data`, now in `wait`), or `completed`. + * Where the call is: `queued` (written ahead, not started), `running` (or in `start` for a durable call), + * `started` (a durable call whose `start` returned its `data`, now in `wait`), or `completed`. */ status: "queued" | "running" | "started" | "completed" = "queued" - /** The JSON data a durable tool's `start` returned, saved so a resumed run can call `wait(data)`. */ + /** The JSON data a durable call's `start` returned, saved so a resumed run can call `wait(data)`. */ data: Json | undefined = undefined - /** Whether a tool call has already been counted against `maxToolCalls` and recorded in `toolCalls`. */ - admitted = false + /** The host function, while the call is in progress. */ + fn: Fn | DurableFn | undefined = undefined + /** What a host function returned and left pending (an Effect, or a Promise), which the driver waits on. */ + work: unknown = undefined + /** Whether host functions crossed back among its arguments: they have no data form, so it blocks a suspend. */ + holdsFunctions = false + /** What the program names it by, where it is among the host globals, and whose hooks save what it returns. */ + readonly label: string + readonly path: ReadonlyArray | undefined + readonly owner: string | undefined constructor( readonly id: number, /** The call's stable idempotency key, `:`. */ readonly key: string, - readonly request: HostRequest, - /** What runs: a tool path, or an extension function's label. */ - readonly label: string, + target: HostTarget, + readonly policy: HostPolicy, + /** The arguments as they crossed to the host. */ + public args: ReadonlyArray, /** The running job waits for it: its outcome continues the frame that asked, before any other job. */ readonly blocking: boolean, - /** How a concurrent call's result becomes a program value: a tool's crosses as JSON, an extension's is one. */ - readonly result: "json" | "value", /** Settles a concurrent call's promise. */ readonly resolving: Resolving | undefined, /** The call that started the work, where a defect it raises is located. */ readonly site: AstNode | undefined, - ) {} + ) { + this.label = target.label + this.path = target.path + this.owner = target.owner + } } /** diff --git a/packages/codemode/src/snapshot.ts b/packages/codemode/src/language/snapshot.ts similarity index 95% rename from packages/codemode/src/snapshot.ts rename to packages/codemode/src/language/snapshot.ts index cc8c5b7835e6..53d347b65f9f 100644 --- a/packages/codemode/src/snapshot.ts +++ b/packages/codemode/src/language/snapshot.ts @@ -1,4 +1,4 @@ -import packageJson from "../package.json" with { type: "json" } +import packageJson from "../../package.json" with { type: "json" } import type { Interruption } from "./runtime/signals.js" import { staticNames, unchanged } from "./runtime/intrinsic-names.js" import { Fn, Obj } from "./runtime/objects.js" @@ -28,6 +28,8 @@ export type CaptureOptions = { readonly limits?: Header["limits"] /** Active milliseconds used so far, across every resume. */ readonly activeMs?: number + /** The run's `run.metadata`. */ + readonly metadata?: Json /** Throw on the first value with no data form, instead of reporting it in `lost`. */ readonly strict: boolean } @@ -63,7 +65,7 @@ const vmSpecs = (): ReadonlyArray => ctor: HostCalls, anchor: "hosts", fields: ["runId", "queued", "running", "inbox", "nextID", "blocked", "input"], - skip: ["ctx", "promises", "extension", "extensionResume"], + skip: ["ctx", "promises"], }, { name: "Frame", @@ -168,9 +170,9 @@ export const captureVm = ( hash: hash(source.code), bytecode: vm.sourceCodes.get(source.id)![0]!.source.hash, })), - limits: options.limits ?? { timeoutMs: null, maxToolCalls: null, maxOutputBytes: null }, - budget: { activeMs: options.activeMs ?? 0, toolCalls: ctx.tools.calls.length }, - toolCalls: ctx.tools.calls.map((call) => ({ name: call.name })), + limits: options.limits ?? { timeoutMs: null, maxOutputBytes: null }, + budget: { activeMs: options.activeMs ?? 0 }, + metadata: options.metadata ?? null, }, roots: encoded.roots, records: encoded.records, @@ -193,9 +195,7 @@ export const restoreVm = (vm: Vm, saved: unknown, options: RestoreOptions): Snap ) } if (header.hosts !== hostsId(ctx)) { - throw new SnapshotError( - "This state was saved by a runtime with other host globals (extensions); resume it with the same ones.", - ) + throw new SnapshotError("This state was saved by a runtime with other host globals; resume it with the same ones.") } for (const source of header.sources) { if (hash(source.code) !== source.hash) throw new SnapshotError(`Source ${source.id} does not match its hash.`) @@ -232,7 +232,6 @@ export const restoreVm = (vm: Vm, saved: unknown, options: RestoreOptions): Snap return undefined }, }) - ctx.tools.calls.push(...header.toolCalls.map((call) => ({ name: call.name }))) return snapshot } diff --git a/packages/codemode/src/snapshot/codec.ts b/packages/codemode/src/language/snapshot/codec.ts similarity index 87% rename from packages/codemode/src/snapshot/codec.ts rename to packages/codemode/src/language/snapshot/codec.ts index 1af62f67aa91..84ef67ad5034 100644 --- a/packages/codemode/src/snapshot/codec.ts +++ b/packages/codemode/src/language/snapshot/codec.ts @@ -1,7 +1,9 @@ +import type { DurableFn, Fn } from "../host-function.js" import { Interpreter } from "../runtime/interpreter.js" import { type AstNode, AsyncIteratorSymbol, + HostFailure, IteratorSymbol, Lost, lostError, @@ -18,6 +20,7 @@ import { ErrorObj, GeneratorObj, HeadersObj, + HostNamespace, IteratorObj, MapObj, Native, @@ -40,8 +43,6 @@ import { AsyncFromSyncIterator } from "../generators.js" import { HostCall, Resolving } from "../scheduler.js" import { BufferObj } from "../stdlib/buffer.js" import { TextDecoderObj } from "../stdlib/bytes.js" -import { ToolError, toolError } from "../tool-error.js" -import { ToolReference, ToolRuntimeError } from "../tool-runtime.js" // A graph codec for a run's state: every object reachable from the roots becomes one record, so identity, sharing, // and cycles survive, and every value inside a record is tagged JSON. Built-ins, AST nodes, bytecode, and the VM's @@ -72,28 +73,60 @@ export class SnapshotError extends Error { } /** - * Where a function inside a value an extension returned came from, so a saved state can name it instead of losing it: - * the extension, the value its function returned (`root`), which the extension's `snapshot` hooks save, and the path + * Where a function inside a value a host function returned came from, so a saved state can name it instead of losing + * it: the host function whose `snapshot` hooks save that value (`owner`, by label), the value (`root`), and the path * from there to the function. */ export type HostOrigin = { - readonly extension: string + readonly owner: string readonly root: object readonly path: ReadonlyArray /** What the program calls it, for its errors (`fetch.text`). */ readonly label: string } -/** The extension bridge's side of saving and restoring those functions; see `extensionGlobals`. */ +/** + * How a run meets its host globals (see `hostGlobals`): values crossing in each direction, calling host functions, and + * saving and restoring the functions inside values they returned. + */ export type HostBridge = { - /** Each extension's name, and whether it has `snapshot` hooks: a state resumes only where both match. */ - readonly extensions: ReadonlyArray<{ readonly name: string; readonly hooked: boolean }> - /** Where a program function wrapping a host function came from, when its extension can save it. */ + /** What a saved state must find again on resume: the host functions with `snapshot` hooks, and the namespaces. */ + readonly shape: ReadonlyArray + /** + * Program values as host values, for a call's arguments. When the runtime awaits promises in arguments, one that + * hasn't fulfilled goes in `waiting` instead, and the arguments are to cross again once it settles. A function + * inside a result crosses back as its host function and is added to `back`; without `back` it is treated like any + * function. + */ + readonly toHost: ( + values: ReadonlyArray, + label: string, + waiting: Array, + back: Array | undefined, + ) => ReadonlyArray + /** A host value as a program value, for what a call returned; `owner`'s `snapshot` hooks can save it. */ + readonly fromHost: (value: unknown, label: string, owner: string | undefined) => Value + /** What a call's failure is to the program. */ + readonly failure: (reason: unknown, label: string) => unknown + /** Whether a call's result is still pending: a Promise or an Effect. */ + readonly isPending: (value: unknown) => boolean + /** Calls a host function now: its value, what it left pending, or the Effect to block on. */ + readonly invoke: ( + fn: Fn, + args: ReadonlyArray, + key: string, + label: string, + ) => + | { readonly kind: "value"; readonly value: unknown } + | { readonly kind: "pending" | "block"; readonly work: unknown } + /** The host function at `path` among the run's host globals, for a resumed call. */ + readonly resolve: (path: ReadonlyArray) => Fn | DurableFn | undefined + /** Where a program function wrapping a host function came from, when its owner can save it. */ readonly origin: (value: object) => HostOrigin | undefined - /** A returned value as its extension saves it. */ - readonly encode: (extension: string, root: object) => unknown + /** A returned value as its owner saves it. */ + readonly encode: (owner: string, root: object) => unknown /** A returned value back from what `encode` gave. */ - readonly decode: (extension: string, data: unknown) => object + readonly decode: (owner: string, data: unknown) => object /** Makes `target`, a restored program function, call the host function `origin` leads to. */ readonly rewrap: (target: Native, origin: HostOrigin) => void } @@ -258,21 +291,39 @@ const builtinSpecs = (): ReadonlyArray => [ { name: "HostCall", ctor: HostCall, - fields: ["status", "data", "admitted", "id", "key", "request", "label", "blocking", "result", "resolving", "site"], + fields: [ + "status", + "data", + "id", + "key", + "label", + "path", + "owner", + "policy", + "args", + "blocking", + "resolving", + "site", + ], + skip: ["fn", "work", "holdsFunctions"], + // A call holding host functions is never saved. + reset: (call: { holdsFunctions: boolean }) => { + call.holdsFunctions = false + }, }, { name: "Resolving", ctor: Resolving, fields: ["done", "scheduler", "promise", "site"] }, { name: "Throw", ctor: Throw, fields: ["value"] }, { name: "PendingThrow", ctor: PendingThrow, fields: ["type", "message", "kind", "suggestions", "node", "value"] }, { name: "Convert", ctor: Convert, fields: ["hints", "values", "ordinal", "coerce"] }, { name: "Invoke", ctor: Invoke, fields: ["callable", "thisValue", "args", "site"] }, - { name: "ToolReference", ctor: ToolReference, fields: ["path"] }, + { name: "HostNamespace", ctor: HostNamespace, fields: ["root", "path"] }, { name: "Lost", ctor: Lost, fields: ["description"] }, { name: "Interpreter", ctor: Interpreter, anchor: "ctx", fields: ["logs", "globalScope", "modules", "namespaces"], - skip: ["tools", "builtins", "vm", "intrinsics", "extensionWork", "hosts", "sources", "prelude"], + skip: ["builtins", "vm", "intrinsics", "hosts", "hostNamespaces", "sources", "prelude"], }, ] @@ -288,7 +339,7 @@ const builtinRegistry = () => { return builtins } -// A function an extension made while the program ran, which has no data form: saved as `LostNative`. +// A function a host function returned while the program ran, which has no data form: saved as `LostNative`. const hostFunction: ClassSpec = { name: "LostNative", ctor: Native, @@ -306,8 +357,7 @@ const errorClasses = new Map([ ["ReferenceError", ReferenceError], ["EvalError", EvalError], ["URIError", URIError], - ["ToolRuntimeError", ToolRuntimeError], - ["ToolError", ToolError], + ["HostFailure", HostFailure], ]) const errorNames = new Map([...errorClasses].map(([name, ctor]) => [ctor, name])) @@ -345,7 +395,7 @@ class Encoder { // A URL's `searchParams` is the same host object its URLSearchParams wraps; it is encoded once every URL is known. private readonly owners = new Map() private readonly params: Array = [] - // One `HostValue` record per value an extension returned, however many of its functions the state holds. + // One `HostValue` record per value a host function returned, however many of its functions the state holds. private readonly roots = new Map() private readonly specs: { readonly byCtor: Map; readonly byName: Map } @@ -448,15 +498,15 @@ class Encoder { return { t: spec.name, ...this.fields(object, spec) } } if (object instanceof Native) { - // A host function made while the program ran: inside a value an extension returned. Its extension saves that - // value when it has `snapshot` hooks; otherwise its name says what was lost. + // A host function made while the program ran: inside a value a host function returned. That function saves the + // value when it has `snapshot` hooks; otherwise its name says what was lost, which never fails a strict save. const origin = this.env.hosts?.origin(object) if (origin !== undefined) { - const root = this.hostValue(origin.extension, origin.root) + const root = this.hostValue(origin.owner, origin.root) const fields = this.fields(object, hostFunction, hostFunction.fields) - return { t: "HostFunction", e: origin.extension, h: root, k: [...origin.path], l: origin.label, ...fields } + return { t: "HostFunction", e: origin.owner, h: root, k: [...origin.path], l: origin.label, ...fields } } - this.lose(`the function ${functionName(object)}`) + this.env.lost.push(`the function ${functionName(object)}`) return { t: "LostNative", ...this.fields(object, hostFunction, hostFunction.fields) } } const proto = Object.getPrototypeOf(object) @@ -475,6 +525,8 @@ class Encoder { this.params.push([id, object]) return undefined } + if (object instanceof Date) return { t: "Date", v: this.value(object.getTime()) } + if (object instanceof RegExp) return { t: "RegExp", s: object.source, f: object.flags } if (object instanceof Headers) return { t: "Headers", e: [...object].map(([key, item]) => [key, item]) } if (object instanceof Uint8Array) { return { t: "Uint8Array", b: this.ref(object.buffer), o: object.byteOffset, l: object.length } @@ -486,16 +538,16 @@ class Encoder { return { t: "Lost", f: [what] } } - // A value an extension returned, as its `snapshot.encode` saves it: plain data, checked before it is copied. - private hostValue(extension: string, root: object): Encoded { + // A value a host function returned, as its `snapshot.encode` saves it: plain data, checked before it is copied. + private hostValue(owner: string, root: object): Encoded { const known = this.roots.get(root) if (known !== undefined) return known - const data = plainData(this.env.hosts!.encode(extension, root), extension, new Set()) + const data = plainData(this.env.hosts!.encode(owner, root), owner, new Set()) const id = this.records.length const ref: Encoded = ["r", id] this.roots.set(root, ref) this.records.push(undefined) - this.records[id] = { t: "HostValue", e: extension, d: this.value(data) } + this.records[id] = { t: "HostValue", e: owner, d: this.value(data) } return ref } @@ -603,9 +655,9 @@ class Decoder { private readonly filled: Array // Derived state is rebuilt once every record is filled in, since it reads other records. private readonly resets: Array<() => void> = [] - // Functions inside values extensions returned, wrapped again once every record is filled in. + // Functions inside values host functions returned, wrapped again once every record is filled in. private readonly hostFunctions: Array = [] - // Those values, given back once each by their extension's `snapshot.decode`, by `HostValue` record. + // Those values, given back once each by their owner's `snapshot.decode`, by `HostValue` record. private readonly hostValues = new Map() private readonly specs: { readonly byCtor: Map; readonly byName: Map } @@ -662,7 +714,6 @@ class Decoder { const name = text(record.c, "an error class") const ctor = errorClasses.get(name) ?? invalid(`unknown error class ${name}.`) const message = text(record.m, "an error message") - if (ctor === ToolError) return toolError(message) const made = Object.create(ctor.prototype) as Error Object.defineProperty(made, "message", { value: message, @@ -672,6 +723,12 @@ class Decoder { }) return made } + case "Date": { + const time = this.value(record.v) + return new Date(typeof time === "number" ? time : invalid(`record ${id} is not a time.`)) + } + case "RegExp": + return new RegExp(text(record.s, "a pattern"), text(record.f, "flags")) case "URL": return new URL(text(record.h, "a URL")) case "URLSearchParams": { @@ -792,6 +849,8 @@ class Decoder { return case "HostValue": case "Interruption": + case "Date": + case "RegExp": case "URL": case "URLSearchParams": case "Headers": @@ -832,30 +891,30 @@ class Decoder { for (const reset of this.resets) reset() if (this.hostFunctions.length === 0) return const hosts = - this.env.hosts ?? invalid("a function from an extension is saved, and this runtime has no extensions.") + this.env.hosts ?? invalid("a function a host function returned is saved, and this runtime has no host globals.") for (const [target, record] of this.hostFunctions) { - const extension = text(record.e, "an extension") + const owner = text(record.e, "a host function") const path = list(record.k, "a path").map((key) => typeof key === "number" ? count(key, "a path index") : text(key, "a path key"), ) hosts.rewrap(target, { - extension, - root: this.hostValue(hosts, record.h, extension), + owner, + root: this.hostValue(hosts, record.h, owner), path, label: text(record.l, "a label"), }) } } - // A value an extension returned, given back once by its `snapshot.decode` from the data it saved. - private hostValue(hosts: HostBridge, ref: unknown, extension: string): object { + // A value a host function returned, given back once by its `snapshot.decode` from the data it saved. + private hostValue(hosts: HostBridge, ref: unknown, owner: string): object { const id = Array.isArray(ref) && ref[0] === "r" ? count(ref[1], "a record id") : invalid("a host value is not a record.") const known = this.hostValues.get(id) if (known !== undefined) return known const record = id < this.records.length ? this.at(id) : invalid(`record ${id} does not exist.`) - if (record.t !== "HostValue" || record.e !== extension) invalid(`record ${id} is not a value of ${extension}.`) - const root = hosts.decode(extension, this.value(record.d)) + if (record.t !== "HostValue" || record.e !== owner) invalid(`record ${id} is not a value of ${owner}.`) + const root = hosts.decode(owner, this.value(record.d)) this.hostValues.set(id, root) return root } @@ -948,9 +1007,9 @@ class Decoder { } } -// What an extension's `snapshot.encode` gives must be plain data the codec copies and hands back to `decode`: JSON +// What a host function's `snapshot.encode` gives must be plain data the codec copies and hands back to `decode`: JSON // values and Uint8Arrays. Views (such as a `Buffer` slice on a shared slab) are copied into standalone `Uint8Array`s. -const plainData = (data: unknown, extension: string, seen: Set): unknown => { +const plainData = (data: unknown, owner: string, seen: Set): unknown => { if (data === null || typeof data === "string" || typeof data === "boolean") return data if (typeof data === "number" && Number.isFinite(data)) return data if (data instanceof Uint8Array) { @@ -960,7 +1019,7 @@ const plainData = (data: unknown, extension: string, seen: Set): unknown } const fail = (what: string): never => { throw new SnapshotError( - `The extension "${extension}" saved a value it returned as data holding ${what}; snapshot.encode must give plain data (JSON values and Uint8Arrays).`, + `The host function ${owner} saved a value it returned as data holding ${what}; snapshot.encode must give plain data (JSON values and Uint8Arrays).`, ) } if (typeof data !== "object") { @@ -974,12 +1033,12 @@ const plainData = (data: unknown, extension: string, seen: Set): unknown } seen.add(data) if (Array.isArray(data)) { - const out = data.map((item) => plainData(item, extension, seen)) + const out = data.map((item) => plainData(item, owner, seen)) seen.delete(data) return out } const out: { [key: string]: unknown } = proto === null ? Object.create(null) : {} - for (const [key, item] of Object.entries(data)) out[key] = plainData(item, extension, seen) + for (const [key, item] of Object.entries(data)) out[key] = plainData(item, owner, seen) seen.delete(data) return out } diff --git a/packages/codemode/src/snapshot/snapshot.ts b/packages/codemode/src/language/snapshot/snapshot.ts similarity index 81% rename from packages/codemode/src/snapshot/snapshot.ts rename to packages/codemode/src/language/snapshot/snapshot.ts index c911ac61f79f..00eaf88f8240 100644 --- a/packages/codemode/src/snapshot/snapshot.ts +++ b/packages/codemode/src/language/snapshot/snapshot.ts @@ -1,6 +1,5 @@ import type { Interpreter } from "../runtime/interpreter.js" -import type { ToolCall } from "../tool-runtime.js" -import { type Encoded, type Record, SnapshotError } from "./codec.js" +import { type Encoded, type Json, type Record, SnapshotError } from "./codec.js" export { SnapshotError } @@ -16,8 +15,8 @@ export const fromBytes = (bytes: Uint8Array): unknown => { } } -/** The snapshot format this build writes and reads (see `src/snapshot.ts`). */ -export const FORMAT = 2 +/** The snapshot format this build writes and reads (see `src/language/snapshot.ts`). */ +export const FORMAT = 3 /** * A suspended run's whole state as JSON: a header that says which build and sources it belongs to and what budget it @@ -35,8 +34,8 @@ export type Header = { /** The build that wrote it: the package version, and a hash of the opcodes, the prelude's bytecode, codecs, and built-ins. */ readonly build: string /** - * A hash of the host's globals (extensions, a test harness), and of each extension's name and whether it has - * `snapshot` hooks, which the restoring runtime must have too. + * A hash of the host's globals (their names, the namespaces among them, and the host functions with `snapshot` + * hooks), which the restoring runtime must have too. */ readonly hosts: string readonly runId: string @@ -49,26 +48,18 @@ export type Header = { }> readonly limits: { readonly timeoutMs: number | null - readonly maxToolCalls: number | null readonly maxOutputBytes: number | null } - /** What the run has used so far, across every resume: active milliseconds, and tool calls admitted. */ - readonly budget: { readonly activeMs: number; readonly toolCalls: number } - /** The tool calls admitted so far. */ - readonly toolCalls: ReadonlyArray + /** What the run has used so far, across every resume: active milliseconds. */ + readonly budget: { readonly activeMs: number } + /** The host's own state for the run (`run.metadata`), given back on resume. */ + readonly metadata: Json } export type SnapshotHeader = Header export const hostsId = (ctx: Interpreter): string => - hash( - [ - ...ctx.intrinsics.hosts(), - ...(ctx.hosts?.extensions ?? []).map( - (extension) => `${extension.name} ${extension.hooked ? "hooks" : "no hooks"}`, - ), - ].join("\n"), - ) + hash([...ctx.intrinsics.hosts(), ...(ctx.hosts?.shape ?? [])].join("\n")) // --- Validation --- @@ -90,6 +81,11 @@ const string = (value: unknown, what: string): string => const number = (value: unknown, what: string): number => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : fail(`${what} is not a number.`) +const json = (value: unknown, what: string): Json => { + if (value === undefined) fail(`${what} is missing.`) + return value as Json +} + const limit = (value: unknown, what: string): number | null => (value === null ? null : number(value, what)) /** A saved state's header, checked in full, before anything is restored. */ @@ -117,13 +113,10 @@ export const validate = (saved: unknown): Snapshot => { }), limits: { timeoutMs: limit(limits.timeoutMs, "timeoutMs"), - maxToolCalls: limit(limits.maxToolCalls, "maxToolCalls"), maxOutputBytes: limit(limits.maxOutputBytes, "maxOutputBytes"), }, - budget: { activeMs: number(budget.activeMs, "the active time"), toolCalls: number(budget.toolCalls, "tool calls") }, - toolCalls: array(header.toolCalls, "the tool calls").map((item) => ({ - name: string(object(item, "a tool call").name, "a tool name"), - })), + budget: { activeMs: number(budget.activeMs, "the active time") }, + metadata: json(header.metadata, "the metadata"), } const roots = array(root.roots, "the roots") if (roots.length < 3) fail("there are fewer than three roots.") diff --git a/packages/codemode/src/stdlib/array.ts b/packages/codemode/src/language/stdlib/array.ts similarity index 99% rename from packages/codemode/src/stdlib/array.ts rename to packages/codemode/src/language/stdlib/array.ts index fbd300a82e48..64bab72678d7 100644 --- a/packages/codemode/src/stdlib/array.ts +++ b/packages/codemode/src/language/stdlib/array.ts @@ -23,7 +23,7 @@ import { import { describeValue, rejectCircularInsertion } from "../runtime/references.js" import { requireCallback, withPrimitives } from "../runtime/callback.js" import type { Interpreter } from "../runtime/interpreter.js" -import { compareText } from "../tool-runtime.js" +import { compareText } from "../runtime/operations.js" const arrayLikeSource = (source: Value): { readonly length: number; readonly source: Obj } => { // JS would treat a promise as an empty array-like; that would hide a missing `await`. diff --git a/packages/codemode/src/stdlib/buffer.ts b/packages/codemode/src/language/stdlib/buffer.ts similarity index 100% rename from packages/codemode/src/stdlib/buffer.ts rename to packages/codemode/src/language/stdlib/buffer.ts diff --git a/packages/codemode/src/stdlib/bytes.ts b/packages/codemode/src/language/stdlib/bytes.ts similarity index 100% rename from packages/codemode/src/stdlib/bytes.ts rename to packages/codemode/src/language/stdlib/bytes.ts diff --git a/packages/codemode/src/stdlib/collections.ts b/packages/codemode/src/language/stdlib/collections.ts similarity index 99% rename from packages/codemode/src/stdlib/collections.ts rename to packages/codemode/src/language/stdlib/collections.ts index 7458be075291..f9803e6e0e3a 100644 --- a/packages/codemode/src/stdlib/collections.ts +++ b/packages/codemode/src/language/stdlib/collections.ts @@ -126,7 +126,7 @@ export const setGlobal = (ctx: Interpreter) => { return set } -// CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again. +// CanBeHeldWeakly: only program objects; namespace paths are rebuilt on every access, so they could never be found again. export const weakKey = (value: Value, label: string) => { if (value instanceof Obj) return value throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`) diff --git a/packages/codemode/src/stdlib/console.ts b/packages/codemode/src/language/stdlib/console.ts similarity index 93% rename from packages/codemode/src/stdlib/console.ts rename to packages/codemode/src/language/stdlib/console.ts index 34ff060368e4..aa335baf6902 100644 --- a/packages/codemode/src/stdlib/console.ts +++ b/packages/codemode/src/language/stdlib/console.ts @@ -1,6 +1,5 @@ import { type Method, methods } from "../runtime/native.js" -import { entries, get, Arr, Obj, type Value } from "../runtime/objects.js" -import { ToolReference } from "../tool-runtime.js" +import { entries, get, Arr, HostNamespace, Obj, type Value } from "../runtime/objects.js" import { containsOpaqueReference } from "../runtime/references.js" import type { Interpreter } from "../runtime/interpreter.js" @@ -46,7 +45,7 @@ export const formatValue = (value: Value): string => { const formatConsoleValue = (value: Value, seen: Set, depth: number): string => { if (value === null || value === undefined) return "null" if (typeof value === "string") return JSON.stringify(value) - if (!(value instanceof Obj)) return value instanceof ToolReference ? "[opaque reference]" : String(value) + if (!(value instanceof Obj)) return value instanceof HostNamespace ? "[opaque reference]" : String(value) if (depth > MAX_CONSOLE_DEPTH) return "..." if (seen.has(value)) return "[Circular]" seen.add(value) diff --git a/packages/codemode/src/stdlib/date.ts b/packages/codemode/src/language/stdlib/date.ts similarity index 100% rename from packages/codemode/src/stdlib/date.ts rename to packages/codemode/src/language/stdlib/date.ts diff --git a/packages/codemode/src/stdlib/headers.ts b/packages/codemode/src/language/stdlib/headers.ts similarity index 100% rename from packages/codemode/src/stdlib/headers.ts rename to packages/codemode/src/language/stdlib/headers.ts diff --git a/packages/codemode/src/stdlib/iterator.ts b/packages/codemode/src/language/stdlib/iterator.ts similarity index 100% rename from packages/codemode/src/stdlib/iterator.ts rename to packages/codemode/src/language/stdlib/iterator.ts diff --git a/packages/codemode/src/stdlib/json.ts b/packages/codemode/src/language/stdlib/json.ts similarity index 100% rename from packages/codemode/src/stdlib/json.ts rename to packages/codemode/src/language/stdlib/json.ts diff --git a/packages/codemode/src/stdlib/math.ts b/packages/codemode/src/language/stdlib/math.ts similarity index 100% rename from packages/codemode/src/stdlib/math.ts rename to packages/codemode/src/language/stdlib/math.ts diff --git a/packages/codemode/src/stdlib/number.ts b/packages/codemode/src/language/stdlib/number.ts similarity index 100% rename from packages/codemode/src/stdlib/number.ts rename to packages/codemode/src/language/stdlib/number.ts diff --git a/packages/codemode/src/stdlib/object.ts b/packages/codemode/src/language/stdlib/object.ts similarity index 96% rename from packages/codemode/src/stdlib/object.ts rename to packages/codemode/src/language/stdlib/object.ts index 60886115eaa5..af2050da5058 100644 --- a/packages/codemode/src/stdlib/object.ts +++ b/packages/codemode/src/language/stdlib/object.ts @@ -7,6 +7,7 @@ import { getOwn, hasOwn, hasPrototype, + HostNamespace, keys, own, ownKeys, @@ -22,7 +23,6 @@ import { primitivePrototype } from "../runtime/intrinsics.js" import { describeValue, rejectCircularInsertion } from "../runtime/references.js" import { invokeMethod } from "../runtime/callback.js" import type { Interpreter } from "../runtime/interpreter.js" -import { ToolReference } from "../tool-runtime.js" // ToObject for enumeration. export const enumerableSource = (ctx: Interpreter, label: string, value: Value, node?: AstNode): Obj => { @@ -32,9 +32,9 @@ export const enumerableSource = (ctx: Interpreter, label: string, value: V if (value instanceof PromiseObj) { throw invalidData(`${label} received an un-awaited Promise; await it before inspecting the result.`, node) } - if (value instanceof ToolReference) { + if (value instanceof HostNamespace) { throw invalidData( - `${label} cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, + `${label} cannot read ${value.root}: it is a namespace of host functions, not plain data. Use Object.keys(${value.root}) for its names.`, node, ) } @@ -128,8 +128,8 @@ export const objectGlobal = (ctx: Interpreter) => { (_, args) => new Arr( builtins.Array, - args[0] instanceof ToolReference - ? [...ctx.tools.keys(args[0].path)] + args[0] instanceof HostNamespace + ? [...ctx.namespaceKeys(args[0])] : keys(enumerableSource(ctx, "Object.keys(...)", args[0])), ), ], diff --git a/packages/codemode/src/stdlib/regexp.ts b/packages/codemode/src/language/stdlib/regexp.ts similarity index 100% rename from packages/codemode/src/stdlib/regexp.ts rename to packages/codemode/src/language/stdlib/regexp.ts diff --git a/packages/codemode/src/stdlib/string.ts b/packages/codemode/src/language/stdlib/string.ts similarity index 99% rename from packages/codemode/src/stdlib/string.ts rename to packages/codemode/src/language/stdlib/string.ts index e5da527e2de7..9dd56b12d780 100644 --- a/packages/codemode/src/stdlib/string.ts +++ b/packages/codemode/src/language/stdlib/string.ts @@ -198,7 +198,7 @@ export const stringGlobal = (ctx: Interpreter) => { } if (typeofValue(replacer) === "function") { throw typeError( - `String.${name} cannot use this callable as a replacer; wrap it in an arrow function, e.g. (match) => tools.ns.tool(match).`, + `String.${name} cannot use this callable as a replacer; wrap it in an arrow function, e.g. (match) => fn(match).`, ) } const primitives = [search, replacement] diff --git a/packages/codemode/src/stdlib/url.ts b/packages/codemode/src/language/stdlib/url.ts similarity index 100% rename from packages/codemode/src/stdlib/url.ts rename to packages/codemode/src/language/stdlib/url.ts diff --git a/packages/codemode/src/stdlib/value.ts b/packages/codemode/src/language/stdlib/value.ts similarity index 100% rename from packages/codemode/src/stdlib/value.ts rename to packages/codemode/src/language/stdlib/value.ts diff --git a/packages/codemode/src/stdlib/web.ts b/packages/codemode/src/language/stdlib/web.ts similarity index 97% rename from packages/codemode/src/stdlib/web.ts rename to packages/codemode/src/language/stdlib/web.ts index 75b62f8c7722..b40ee18e80ef 100644 --- a/packages/codemode/src/stdlib/web.ts +++ b/packages/codemode/src/language/stdlib/web.ts @@ -12,6 +12,7 @@ import { Bytes, DateObj, ErrorObj, + HostNamespace, MapObj, Obj, RegExpObj, @@ -24,7 +25,6 @@ import { import { createErrorValue, isErrorType } from "../runtime/intrinsics.js" import { describeValue } from "../runtime/references.js" import type { Interpreter } from "../runtime/interpreter.js" -import { ToolReference } from "../tool-runtime.js" // WebIDL DOMString conversion: a missing argument is a TypeError, anything else stringifies. Invalid input is a // TypeError as well; browsers throw a DOMException named InvalidCharacterError, which CodeMode does not have. @@ -47,7 +47,7 @@ export const structuredCloneGlobal = (ctx: Interpreter) => const builtins = ctx.builtins const memo = new Map() const clone = (value: Value): Value => { - if (typeof value === "symbol" || value instanceof ToolReference) { + if (typeof value === "symbol" || value instanceof HostNamespace) { throw typeError(`DataCloneError: ${describeValue(value)} could not be cloned.`) } if (!(value instanceof Obj)) return value diff --git a/packages/codemode/src/language/streaming-response.ts b/packages/codemode/src/language/streaming-response.ts new file mode 100644 index 000000000000..a2c0afe11919 --- /dev/null +++ b/packages/codemode/src/language/streaming-response.ts @@ -0,0 +1,11 @@ +/** + * A live web `Response` a host function hands the program, whose body the host streams for the lifetime of the run. + * The program sees a `Response`: `url`, `status`, `statusText`, `ok`, `redirected`, `headers`, a live `bodyUsed`, + * `text()`, `json()`, `bytes()`, `arrayBuffer()`, and a `body` it can read with a reader or `for await`. + */ +export class StreamingResponse { + constructor( + readonly response: Response, + readonly redirected = response.redirected, + ) {} +} diff --git a/packages/codemode/src/vm.ts b/packages/codemode/src/language/vm.ts similarity index 97% rename from packages/codemode/src/vm.ts rename to packages/codemode/src/language/vm.ts index fc6003ed8d12..2d9de83a7d0c 100644 --- a/packages/codemode/src/vm.ts +++ b/packages/codemode/src/language/vm.ts @@ -1,5 +1,5 @@ import { parse, type Expression, type MemberExpression, type NewExpression } from "acorn" -import type { Diagnostic } from "./codemode.js" +import type { Diagnostic } from "./runtime/model.js" import { conversions } from "./runtime/callback.js" import { locate, materialize } from "./runtime/errors.js" import type { Interpreter } from "./runtime/interpreter.js" @@ -32,6 +32,7 @@ import { get, getOwn, hasOwn, + HostNamespace, IteratorObj, Native, type NativeResult, @@ -81,7 +82,6 @@ import type { HostCall, Stop } from "./scheduler.js" import { compiledPrelude } from "./prelude/prelude.js" import { constructRegExp } from "./stdlib/regexp.js" import { enumerableSource } from "./stdlib/object.js" -import { ToolReference } from "./tool-runtime.js" import { registerAsyncIntrinsics, type SettlingTarget, VmScheduler } from "./async.js" import type { Code } from "./code.js" import { type CaptureOptions, captureVm, type RestoreOptions, restoreVm } from "./snapshot.js" @@ -100,17 +100,7 @@ import { resumeVmGenerator, unwindGenerator, } from "./generators.js" -import { - BLOCK, - callExtension, - callSearch, - callTool, - ENTERED, - type HostResult, - HostCalls, - registerHostIntrinsics, - VISIT_BUDGET, -} from "./host.js" +import { BLOCK, ENTERED, type HostResult, HostCalls, VISIT_BUDGET } from "./host.js" import { getInternalPrelude, internalCode, @@ -234,8 +224,8 @@ export class VmInternalFn { * promise in a later job, in completion order. Unknown or completed ids are ignored. `call(id)`/`calls()` list the * calls handed over and not completed, in the order they were made. * - **Snapshots.** `capture` and `restore` run only between `run()`s, synchronously. A capture changes nothing a - * later `run()` sees; it fails (throws) only on a codec gap. A state holding live host values (`calls()` with an - * extension call, or `activeExtension`) can't be restored faithfully. + * later `run()` sees; it fails (throws) only on a codec gap. A state holding live host values (`calls()` with a call + * whose function or result is still live) can't be restored faithfully. */ export class Vm { readonly frames: Array = contiguous() @@ -275,7 +265,6 @@ export class Vm { registerVmIntrinsics(this.intrinsics, this) registerAsyncIntrinsics(this.intrinsics, this) registerGeneratorIntrinsics(this.intrinsics, this) - registerHostIntrinsics(this.intrinsics, this) } private getIntrinsic(name: string): unknown { @@ -339,7 +328,7 @@ export class Vm { } if (this.frames.length > 0) { if (hosts.blocked !== undefined) return { kind: "host", calls: [] } - // Fuel can run out on a call that then stopped for the host (a built-in or sync extension call costs fuel too): + // Fuel can run out on a call that then stopped for the host (a built-in or sync host call costs fuel too): // that's a slice point as well, or a loop of such calls would refill forever without yielding. if (this.fuel <= 0) { this.fuel = this.tripEvery || INITIAL_FUEL @@ -426,14 +415,6 @@ export class Vm { return this.scheduler.diagnostics() } - /** - * A synchronous extension call the program is in the middle of, from its `before` hook until its result is a - * program value or its concurrent call is running: it holds live host values. - */ - get activeExtension(): { readonly key: string; readonly path: string } | undefined { - return this.hosts.extension - } - /** Saves the run's state as it is now. */ capture(options: CaptureOptions) { if (this.intrinsics.size === 0) this.initIntrinsics() @@ -494,7 +475,7 @@ export class Vm { /** * Nearest non-internal frame's AST site, or the bottom internal frame's `origin` (§3.6). A failure the host - * boundary's own walk raises (copying out the result, or a tool call's arguments) has no location: the call that + * boundary's own walk raises (copying out the result) has no location: the call that * crosses reports it. */ currentSite(): AstNode | undefined { @@ -1268,16 +1249,24 @@ export class Vm { if (this.fuel <= 0) return 1 continue } - case 72: // Op.NEW - if (this.execNew(frame, (w >>> 8) & 0xff, pc, sp)) continue outer + case 72: { + // Op.NEW + const made = this.execNew(frame, (w >>> 8) & 0xff, pc, sp) + if (made === 1) continue outer + if (made === 2) return 2 sp = frame.sp pc++ continue - case 73: // Op.NEW_SPREAD - if (this.execNewSpread(frame, pc, sp)) continue outer + } + case 73: { + // Op.NEW_SPREAD + const made = this.execNewSpread(frame, pc, sp) + if (made === 1) continue outer + if (made === 2) return 2 sp = frame.sp pc++ continue + } case 74: // Op.ITER_OPEN frame.pc = pc frame.sp = sp @@ -1721,7 +1710,7 @@ export class Vm { return this.invokeCallable(frame, callee, thisVal, [...argsArr.items], callSiteOf(frame, pc), pc) } - private execNew(frame: Frame, n: number, pc: number, sp: number): boolean { + private execNew(frame: Frame, n: number, pc: number, sp: number): number { const fcode = frame.code const regs = frame.regs if (n === 255) { @@ -1732,7 +1721,7 @@ export class Vm { throw notConstructible(callee, fcode.siteAt(pc) as NewExpression) } frame.sp = sp - return false + return 0 } frame.pc = pc const base = sp - n @@ -1742,7 +1731,7 @@ export class Vm { return this.execConstruct(frame, callee, args, callSiteOf(frame, pc)) } - private execNewSpread(frame: Frame, pc: number, sp: number): boolean { + private execNewSpread(frame: Frame, pc: number, sp: number): number { const regs = frame.regs const argsArr = regs[--sp] as Arr const callee = regs[--sp] as Builtin @@ -1919,12 +1908,7 @@ export class Vm { } this.frames.pop() this.release(frame) - this.pushInternal( - internalCode("$vmCross"), - [new Arr(this.ctx.builtins.Array, [value])], - undefined, - Mode.BOUNDARY_OUT, - ) + this.pushInternal(internalCode("$vmResult"), [value], undefined, Mode.BOUNDARY_OUT) return true } @@ -2007,7 +1991,9 @@ export class Vm { const v = key === "length" ? o.length : this.readObjProp(this.ctx.builtins.String, key, frame.code, pc) return writeRead(regs, sp - 1, mode, o, v) } - if (o instanceof ToolReference) return writeRead(regs, sp - 1, mode, undefined, new ToolReference([...o.path, key])) + if (o instanceof HostNamespace) { + return writeRead(regs, sp - 1, mode, undefined, new HostNamespace(o.root, [...o.path, key])) + } const site = frame.code.siteAt(pc)! if (mode === 2) { regs[sp - 1] = destructuredProperty(this.ctx.builtins, o, key, site) @@ -2071,7 +2057,7 @@ export class Vm { site: AstNode, ): number { const ref = this.resolveMember(o, key, site) - if (ref instanceof ToolReference) return writeRead(regs, at, mode, undefined, ref) + if (ref instanceof HostNamespace) return writeRead(regs, at, mode, undefined, ref) if ("value" in ref) return writeRead(regs, at, mode, undefined, ref.value) return writeRead(regs, at, mode, ref.receiver, readReference(ref, site as MemberExpression)) } @@ -2103,7 +2089,7 @@ export class Vm { private execDeleteProp(fcode: Code, o: Value, key: PropertyKey, pc: number): boolean { const site = fcode.siteAt(pc)! const ref = this.resolveMember(o, key, site) - if (ref instanceof ToolReference || "value" in ref || ref.receiver !== ref.target) { + if (ref instanceof HostNamespace || "value" in ref || ref.receiver !== ref.target) { throw invalidData("Only data fields may be deleted.", site) } if (remove(ref.target, ref.key)) return true @@ -2199,7 +2185,7 @@ export class Vm { const site = callee.callback && callee.call.length < 3 ? undefined : callSiteOf(frame, pc) const res = callee.call(thisVal, args, site) if (this.tripEvery === 0) this.fuel -= NATIVE_FUEL - if (res === null || typeof res !== "object" || res instanceof Obj) { + if ((res as unknown) !== BLOCK && (res as unknown) !== ENTERED && (res === null || typeof res !== "object" || res instanceof Obj)) { normalizeItems(res) regs[base - 2] = res frame.sp = base - 1 @@ -2282,19 +2268,19 @@ export class Vm { return this.settleNativeResult(callerFrame, res, callee, thisVal, args, false, site, pc) } - if (callee instanceof ToolReference) { + if (callee instanceof HostNamespace) { if (callee.path.length === 0) { const callNode = site as { readonly callee?: Expression } | undefined - throw typeError("The tools root is not callable.", callNode?.callee ?? site) + throw typeError(`${callee.root} is not a function.`, callNode?.callee ?? site) } - return this.hostResult(callerFrame, callTool(this, this.hosts, callee.path, args, site ?? this.currentSite())) + return this.hostResult(callerFrame, this.ctx.callNamespace(callee, args, site ?? this.currentSite())) } const callNode = site as { readonly callee?: Expression } | undefined throw notAFunction(callNode?.callee, site ?? this.currentSite()) } - private execConstruct(callerFrame: Frame, ctor: Builtin, args: Array, site: AstNode | undefined): boolean { + private execConstruct(callerFrame: Frame, ctor: Builtin, args: Array, site: AstNode | undefined): number { conversions.count = 0 let res: NativeResult try { @@ -2302,7 +2288,7 @@ export class Vm { } catch (err) { throw locate(err, site ?? this.currentSite()) } - return this.settleNativeResult(callerFrame, res, ctor, ctor, args, true, site, callerFrame.pc) === 1 + return this.settleNativeResult(callerFrame, res, ctor, ctor, args, true, site, callerFrame.pc) } private settleNativeResult( @@ -2325,6 +2311,7 @@ export class Vm { return 1 } if ((res as unknown) === BLOCK) return 2 + if ((res as unknown) === ENTERED) return 1 if (res instanceof Convert) { const builtins = this.ctx.builtins this.pushInternalCall( @@ -2373,15 +2360,6 @@ export class Vm { } const promiseHandled = this.scheduler.handlePromisePush(callerFrame, kindName, slots, site ?? callerFrame.origin) if (promiseHandled !== undefined) return promiseHandled - if (kindName === "search") { - return this.hostResult( - callerFrame, - callSearch(this, this.hosts, slots[0] as Array, site ?? callerFrame.origin), - ) - } - if (kindName === "extension call") { - return this.hostResult(callerFrame, callExtension(this.ctx, this.hosts, slots, site ?? callerFrame.origin)) - } throw new NotYetImplemented(`Push kind: ${kindName}`) } callerFrame.regs[callerFrame.sp++] = res as Value @@ -2935,12 +2913,18 @@ export class Vm { frame.pc = pc + 1 return this.fuel <= 0 ? -3 : pc + 1 } - case Op.NEW: - if (this.execNew(frame, a, pc, sp)) return -1 + case Op.NEW: { + const made = this.execNew(frame, a, pc, sp) + if (made === 1) return -1 + if (made === 2) return -4 return pc + 1 - case Op.NEW_SPREAD: - if (this.execNewSpread(frame, pc, sp)) return -1 + } + case Op.NEW_SPREAD: { + const made = this.execNewSpread(frame, pc, sp) + if (made === 1) return -1 + if (made === 2) return -4 return pc + 1 + } case Op.ITER_OPEN: return this.execIterOpen(frame, a, pc, sp) case Op.ITER_INIT: @@ -3049,8 +3033,7 @@ const WARMUP_SOURCE = [ ' const obj = { a: 1, b: "two", c: [1, r], d: null, e: sorted }', " if (r >= 2) sum += JSON.stringify(JSON.parse(JSON.stringify(obj))).length", " const pres = await Promise.all(Array.from({ length: 2 }, (_, i) => work(i)))", - " const tres = await Promise.all(Array.from({ length: 2 }, (_, i) => tools.echo({ id: i })))", - ' totalSum += sum + total + sorted.length + pres.length + tres.length + out.length + parts.join(",").length + map.size + set.size', + ' totalSum += sum + total + sorted.length + pres.length + out.length + parts.join(",").length + map.size + set.size', " }", " return totalSum", "})()", diff --git a/packages/codemode/test/bytecode-limits.test.ts b/packages/codemode/test/bytecode-limits.test.ts index 7834457af059..4fe329f488d8 100644 --- a/packages/codemode/test/bytecode-limits.test.ts +++ b/packages/codemode/test/bytecode-limits.test.ts @@ -1,18 +1,13 @@ import { describe, expect, test } from "bun:test" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import type { ToolRuntime } from "../src/tool-runtime.js" -import { CodeBuilder, CodeSizeOverflow, hashOf } from "../src/code.js" -import { compileSource } from "../src/compiler/compile.js" -import { CodeFlags, EMPTY_CELLS, Op, packWord, PutKind } from "../src/opcodes.js" -import { Mode } from "../src/vm.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { CodeBuilder, CodeSizeOverflow, hashOf } from "../src/language/code.js" +import { compileSource } from "../src/language/compiler/compile.js" +import { CodeFlags, EMPTY_CELLS, Op, packWord, PutKind } from "../src/language/opcodes.js" +import { Mode } from "../src/language/vm.js" import { parseVmSource, type RunOptions, runProgram } from "./run-program.js" import { verifyAll, verifyCode } from "./verify-bytecode.js" -const emptyTools = { - calls: [], - keys: () => [], -} as unknown as ToolRuntime const compileVerified = (code: string) => { const parsed = parseVmSource(code) @@ -139,7 +134,7 @@ describe("CodeBuilder and packWord encoding bounds", () => { expect(finished.code.length).toBeGreaterThan(66_000) expect(verifyCode(finished)).toEqual([]) - const ctx = new Interpreter({ tools: emptyTools, builtins: createBuiltins(), logs: [] }) + const ctx = new Interpreter({ builtins: createBuiltins(), logs: [] }) const vm = ctx.vm vm.sourceCodes.set("test", [finished]) const root = vm.acquire(finished) @@ -206,7 +201,6 @@ describe("wide `a` operands and `execWide` coverage", () => { expect(verifyAll(wideCodes)).toEqual([]) const ctx = new Interpreter({ - tools: emptyTools, builtins: createBuiltins(), logs: [], }) diff --git a/packages/codemode/test/codemode.test.ts b/packages/codemode/test/codemode.test.ts index c9f4cd10015b..3ecd2df323ea 100644 --- a/packages/codemode/test/codemode.test.ts +++ b/packages/codemode/test/codemode.test.ts @@ -648,6 +648,41 @@ describe("CodeMode schema flexibility", () => { expect(Object.hasOwn(received, "q")).toBe(false) }) + test("tool arguments leave functions out and await promises, as JSON did", async () => { + const observed: Array = [] + const show = Tool.make({ + description: "Show raw input", + input: { type: "object" }, + execute: (input) => + Effect.sync(() => { + observed.push(input) + return "ok" + }), + }) + const runtime = CodeMode.make({ tools: { shop: { show } } }) + for (const code of [ + `await tools.shop.show({ q: "x", format(v) { return v } })`, + `await tools.shop.show({ n: Promise.resolve(5) })`, + // A toJSON method is a function too: left out, not run. + `await tools.shop.show({ item: "book", price: { cents: 1999, toJSON() { return "$19.99" } } })`, + `await tools.shop.show({ later: (async () => { await null; return [1, () => 2] })() })`, + ]) { + const result = await Effect.runPromise(runtime.execute(code)) + expect(result.ok).toBe(true) + } + expect(observed).toStrictEqual([ + { q: "x" }, + { n: 5 }, + { item: "book", price: { cents: 1999 } }, + { later: [1, null] }, + ]) + const rejected = await Effect.runPromise( + runtime.execute(`await tools.shop.show({ n: Promise.reject(new Error("no price")) })`), + ) + expect(rejected.ok ? undefined : rejected.error.message).toContain("no price") + expect(observed).toHaveLength(4) + }) + test("dropping undefined values lets optionalKey schemas accept conditional arguments", async () => { const observed: Array = [] const find = Tool.make({ diff --git a/packages/codemode/test/enumeration.test.ts b/packages/codemode/test/enumeration.test.ts index c46b6048d5be..382ba3bd09ec 100644 --- a/packages/codemode/test/enumeration.test.ts +++ b/packages/codemode/test/enumeration.test.ts @@ -67,11 +67,11 @@ describe("Object.keys over tool references", () => { const failure = await error(`return Object.${method}(tools)`) expect(failure.kind).toBe("InvalidDataValue") expect(failure.message).toContain( - `Object.${method}(...) cannot read tool references: they are not plain data. Use Object.keys(tools) for names, or search({ query }) for signatures.`, + `Object.${method}(...) cannot read tools: it is a namespace of host functions, not plain data. Use Object.keys(tools) for its names.`, ) } const nested = await error(`return Object.entries(tools.github)`) - expect(nested.message).toContain("Use Object.keys(tools) for names") + expect(nested.message).toContain("Use Object.keys(tools) for its names") }) }) diff --git a/packages/codemode/test/extensions.test.ts b/packages/codemode/test/extensions.test.ts index ed24c49735eb..c4b598ae1148 100644 --- a/packages/codemode/test/extensions.test.ts +++ b/packages/codemode/test/extensions.test.ts @@ -73,9 +73,9 @@ describe("extension functions", () => { }) test("a function inside a result is named by its path in diagnostics", async () => { - expect((await failure(`const res = await fetch("https://a.test/"); res.handlers[0](() => 1)`)).message).toContain( - "Argument 1 to fetch.handlers[0] contains a function", - ) + expect( + (await failure(`const res = await fetch("https://a.test/"); res.handlers[0]([1].keys())`)).message, + ).toContain("Argument 1 to fetch.handlers[0] contains an iterator") const target = CodeMode.make({ extensions: [Extension.make({ name: "odd", globals: { make: () => ({ sym: () => Symbol("s") }) } })], }) @@ -92,6 +92,91 @@ describe("extension functions", () => { expect(await value(`return JSON.stringify((await fetch("https://a.test/")).headers)`)).toBe("{}") }) + test("a host function passed back arrives as itself, wherever it sits", async () => { + const files = new WeakMap() + const target = CodeMode.make({ + extensions: [ + Extension.make({ + name: "files", + globals: { + file: (name: string) => { + const text = async () => `text of ${name}` + files.set(text, name) + return { name, text } + }, + copy: async (dest: string, source: { text: () => Promise }) => [ + dest, + files.get(source.text), + await source.text(), + ], + }, + }), + ], + }) + expect(await value(`return await copy("b", file("a"))`, target)).toEqual(["b", "a", "text of a"]) + expect(await value(`const f = file("a"); return await copy("b", { text: f.text })`, target)).toEqual([ + "b", + "a", + "text of a", + ]) + }) + + test("a program function or a global passed to the host is left out, like any function", async () => { + const taken: Array = [] + const target = CodeMode.make({ + extensions: [ + Extension.make({ + name: "gated", + globals: { fs: { rm: () => undefined }, take: (value: unknown) => void taken.push(value) }, + }), + ], + }) + await value( + `take({ text: async () => "x" }); take({ text: take }); take(fs.rm); take({ a: fs.rm }); take(fs)`, + target, + ) + expect(taken).toEqual([{}, {}, undefined, {}, {}]) + }) + + test("a constructible global works with new and returns what the host function returns", async () => { + const target = CodeMode.make({ + extensions: [ + Extension.make({ + name: "glob", + globals: { + Glob: Extension.constructible((pattern: string) => ({ match: (name: string) => name.endsWith(pattern) })), + }, + }), + ], + }) + expect(await value(`const g = new Glob(".ts"); return [g.match("a.ts"), g.match("a.js")]`, target)).toEqual([ + true, + false, + ]) + expect(await value(`return Glob(".ts").match("a.ts")`, target)).toBe(true) + expect(await value(`return new Glob(...[".ts"]).match("a.ts")`, target)).toBe(true) + expect((await failure(`new (new Glob(".ts").match)("a")`, target)).message).toContain("is not supported") + }) + + test("a constructible global that blocks on the host gives new its value", async () => { + const target = CodeMode.make({ + extensions: [ + Extension.make({ + name: "sum", + globals: { + Sum: Extension.constructible((...values: Array) => + Effect.succeed({ total: values.reduce((total, value) => total + value, 0) }), + ), + }, + }), + ], + }) + expect(await value(`return new Sum(1, 2).total + 1`, target)).toBe(4) + expect(await value(`return new Sum(...[1, 2]).total + 1`, target)).toBe(4) + // An argument that is still a promise is awaited first, as in a plain call. + expect(await value(`return new Sum((async () => { await null; return 1 })(), 2).total + 1`, target)).toBe(4) + }) + test("a class global is only a function; calling it throws the host TypeError", async () => { const target = CodeMode.make({ extensions: [Extension.make({ name: "cls", globals: { Bag: class Bag {} } })] }) expect((await failure(`Bag()`, target)).message).toContain("without") @@ -220,10 +305,11 @@ describe("values are converted at the boundary, never shared", () => { expect(held[1]).toBeInstanceOf(Error) }) - test("functions, promises, iterators, and symbols cannot be passed in", async () => { - expect((await failure(`keep(() => 1)`)).message).toContain("Argument 1 to keep contains a function") + test("functions are left out and promises awaited, as in tool arguments; iterators and symbols cannot be passed in", async () => { + held.length = 0 + await value(`keep({ f: () => 1, n: later(1) }); keep([() => 1, later(2)]); keep(() => 1); keep(later(3))`) + expect(held).toEqual([{ n: 2 }, [null, 3], undefined, 4]) expect((await failure(`keep([1].keys())`)).message).toContain("Argument 1 to keep contains an iterator") - expect((await failure(`keep(later(1))`)).message).toContain("un-awaited Promise") expect((await failure(`keep(Symbol.iterator)`)).message).toContain("Argument 1 to keep contains a symbol") }) @@ -519,7 +605,7 @@ describe("imports", () => { test("a module that isn't provided, or a missing export, fails at the import", async () => { const target = CodeMode.make({ extensions: [files] }) expect((await failure(`import yaml from "yaml"`, target)).message).toStartWith( - `Error: Cannot import "yaml": it isn't available in Code Mode, which can't load packages or files.`, + `Error: Cannot import "yaml": it isn't available here, where packages and files can't be loaded.`, ) expect(await value(`try { await import("child_process") } catch (e) { return e.message }`, target)).toStartWith( `Cannot import "child_process"`, @@ -639,7 +725,7 @@ describe("configuration", () => { expect(result.toolCalls).toEqual([]) }) - test("a result handed to a tool is plain data", async () => { + test("a result with functions is handed to a tool as its data", async () => { const tools = CodeMode.make({ extensions: [extension], tools: { @@ -657,6 +743,12 @@ describe("configuration", () => { headers: {}, handlers: [null], }) + expect( + await value( + `const { status, ok } = await fetch("https://a.test/"); return await tools.echo({ v: { status, ok } })`, + tools, + ), + ).toEqual({ status: 200, ok: true }) }) test("a run can get only some of the runtime's extensions", async () => { diff --git a/packages/codemode/test/fixtures/build-id.ts b/packages/codemode/test/fixtures/build-id.ts index 22b5b1018d6d..a897a977e558 100644 --- a/packages/codemode/test/fixtures/build-id.ts +++ b/packages/codemode/test/fixtures/build-id.ts @@ -1,9 +1,8 @@ // Prints this process's snapshot build id, from a runtime made with as few of the package's modules as it takes, so // the id can be compared across entry points (`bun --preload build-id.ts`). -import { Interpreter } from "../../src/runtime/interpreter.js" -import { createBuiltins } from "../../src/runtime/intrinsics.js" -import { buildId } from "../../src/snapshot.js" -import { ToolRuntime } from "../../src/tool-runtime.js" +import { Interpreter } from "../../src/language/runtime/interpreter.js" +import { createBuiltins } from "../../src/language/runtime/intrinsics.js" +import { buildId } from "../../src/language/snapshot.js" -new Interpreter({ tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins() }) +new Interpreter({ builtins: createBuiltins() }) console.log(buildId()) diff --git a/packages/codemode/test/fixtures/prelude-roundtrip.ts b/packages/codemode/test/fixtures/prelude-roundtrip.ts index e3f52f35f73a..d088470852f0 100644 --- a/packages/codemode/test/fixtures/prelude-roundtrip.ts +++ b/packages/codemode/test/fixtures/prelude-roundtrip.ts @@ -2,9 +2,9 @@ // prelude frame was on the stack. import { Effect } from "effect" import { CodeMode } from "../../src/index.js" -import { CodeFlags } from "../../src/opcodes.js" -import { setRoundTrip } from "../../src/runtime/execute.js" -import { Vm } from "../../src/vm.js" +import { CodeFlags } from "../../src/language/opcodes.js" +import { setRoundTrip } from "../../src/language/runtime/execute.js" +import { Vm } from "../../src/language/vm.js" setRoundTrip(true) diff --git a/packages/codemode/test/language-boundary.test.ts b/packages/codemode/test/language-boundary.test.ts new file mode 100644 index 000000000000..2b6316f7b558 --- /dev/null +++ b/packages/codemode/test/language-boundary.test.ts @@ -0,0 +1,25 @@ +import { expect, test } from "bun:test" +import { Glob } from "bun" +import path from "node:path" + +// The language layer (src/language/) knows nothing about Code Mode (src/codemode/), which is built on it. + +test("nothing in the language layer imports Code Mode", async () => { + const root = path.join(import.meta.dir, "..") + const src = path.join(root, "src") + const language = path.join(src, "language") + const files = await Array.fromAsync(new Glob("**/*.ts").scan({ cwd: language })) + expect(files.length).toBeGreaterThan(0) + const outside = [] + for (const file of files) { + const text = await Bun.file(path.join(language, file)).text() + for (const match of text.matchAll(/(?:from|import)\s*\(?\s*"(\.[^"]+)"/g)) { + const target = path.resolve(path.dirname(path.join(language, file)), match[1]!) + // The package's own files (package.json) are shared; only the rest of src is off limits. + if (target.startsWith(src + path.sep) && !target.startsWith(language + path.sep)) { + outside.push(`${file} imports ${match[1]}`) + } + } + } + expect(outside).toEqual([]) +}) diff --git a/packages/codemode/test/language.test.ts b/packages/codemode/test/language.test.ts new file mode 100644 index 000000000000..c5d12d0edf9e --- /dev/null +++ b/packages/codemode/test/language.test.ts @@ -0,0 +1,365 @@ +import { describe, expect, test } from "bun:test" +import { Effect, Fiber } from "effect" +import { Host, Runtime, toBytes } from "../src/language/index.js" + +// The language layer on its own: host functions, namespaces, failures, and per-run state, with no tools. + +const run = (runtime: Runtime.Runtime, code: string) => + Effect.runPromise(runtime.execute(code) as Effect.Effect) + +const value = async (runtime: Runtime.Runtime, code: string) => { + const result = await run(runtime, code) + if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`) + return result.value +} + +/** A latch the test opens by hand. */ +const latch = () => { + let resolve!: (value: A) => void + const promise = new Promise((done) => (resolve = done)) + return { promise, resolve } +} + +describe("Host.fn", () => { + test("a sync function returns its value, or blocks on an Effect, before the next statement", async () => { + const runtime = Runtime.make({ + globals: () => ({ + double: Host.fn(([n]) => (n as number) * 2), + later: Host.fn(([n]) => Effect.succeed((n as number) + 1)), + }), + }) + expect(await value(runtime, `const a = double(2); const b = later(a); return [a, b, typeof double]`)).toEqual([ + 4, + 5, + "function", + ]) + }) + + test("an async function hands the program a promise", async () => { + const runtime = Runtime.make({ globals: () => ({ load: Host.fn(([n]) => Effect.succeed(n), { type: "async" }) }) }) + expect(await value(runtime, `const p = load(1); return [p instanceof Promise, await p]`)).toEqual([true, 1]) + }) + + test("a sync-thenable result can be used directly, awaited, or chained", async () => { + const runtime = Runtime.make({ + globals: () => ({ find: Host.fn(() => Effect.succeed({ items: [1, 2] }), { type: "sync-thenable" }) }), + }) + expect( + await value( + runtime, + `return [find().items.length, (await find()).items.length, await find().then((r) => r.items[1]), Object.keys(find())]`, + ), + ).toEqual([2, 2, 2, ["items"]]) + }) + + test("every call gets its key, :", async () => { + const keys: Array = [] + const runtime = Runtime.make({ globals: () => ({ note: Host.fn((_, call) => void keys.push(call.key)) }) }) + await Effect.runPromise(runtime.program(`note(); note()`, { runId: "r" }).run()) + expect(keys).toEqual(["r:0", "r:1"]) + }) + + test("arguments and results cross as structured copies", async () => { + let seen: unknown + const runtime = Runtime.make({ + globals: () => ({ + keep: Host.fn(([input]) => { + seen = input + return { when: new Date(0), tags: new Set(["a"]), read: () => "called" } + }), + }), + }) + expect( + await value( + runtime, + `const r = keep({ at: new Date(5), m: new Map([["k", 1]]) }); return [r.when.getTime(), r.tags.has("a"), r.read()]`, + ), + ).toEqual([0, true, "called"]) + expect(seen).toEqual({ at: new Date(5), m: new Map([["k", 1]]) }) + }) + + test("a function inside a result crosses back as itself only to a function that takes functions", async () => { + const read = () => "called" + const runtime = Runtime.make({ + globals: () => ({ + open: Host.fn(() => ({ read })), + takes: Host.fn(([input]) => (input as { readonly read: unknown }).read === read, { takesFunctions: true }), + refuses: Host.fn(() => true), + }), + }) + expect(await value(runtime, `return takes({ read: open().read })`)).toBe(true) + for (const code of [`refuses(open().read)`, `takes(takes)`, `takes(() => 1)`]) { + expect(await run(runtime, code)).toMatchObject({ + ok: false, + error: { message: expect.stringContaining("contains a function, which cannot be passed to the host") }, + }) + } + }) + + test("a constructible function is called by new, and gives what it returns", async () => { + const runtime = Runtime.make({ + globals: () => ({ + Box: Host.fn(([item]) => ({ item }), { constructible: true }), + Later: Host.fn(([item]) => Effect.succeed({ item }), { constructible: true }), + plain: Host.fn(() => 1), + }), + }) + expect(await value(runtime, `return [new Box(1).item, new Later(2).item, new Later(...[3]).item]`)).toEqual([ + 1, 2, 3, + ]) + expect(await run(runtime, `new plain()`)).toMatchObject({ ok: false }) + }) + + test("idempotent applies only to async functions", () => { + // @ts-expect-error a sync function can't be idempotent + Host.fn(() => 1, { idempotent: true }) + // @ts-expect-error a sync-thenable function can't be idempotent + Host.fn(() => 1, { type: "sync-thenable", idempotent: true }) + expect(Host.fn(() => 1, { type: "async", idempotent: true }).idempotent).toBe(true) + }) +}) + +describe("Host.namespace and Host.fail", () => { + const api = Host.namespace({ + keys: (path) => (path.length === 0 ? ["issues"] : ["list"]), + resolve: (path) => + path.join(".") === "issues.list" + ? Host.fn(() => Effect.succeed(["#1"]), { type: "async" }) + : Host.missing(`No api.${path.join(".")}`, ["Try api.issues.list."], "NotFound"), + }) + const runtime = Runtime.make({ + globals: () => ({ api, refuse: Host.fn(() => Effect.fail(Host.fail("Refused", "Not today", ["Ask again."]))) }), + }) + + test("a namespace is a lazy, path-addressed object of functions", async () => { + expect( + await value(runtime, `return [await api.issues.list(), Object.keys(api), Object.keys(api.issues), typeof api.x]`), + ).toEqual([["#1"], ["issues"], ["list"], "function"]) + }) + + test("a missing function and a host failure keep their kind and suggestions", async () => { + expect(await run(runtime, `return api.issues.nope()`)).toMatchObject({ + ok: false, + error: { kind: "NotFound", message: "No api.issues.nope", suggestions: ["Try api.issues.list."] }, + }) + expect(await run(runtime, `refuse()`)).toMatchObject({ + ok: false, + error: { kind: "Refused", message: "Not today", suggestions: ["Ask again."] }, + }) + }) +}) + +describe("per-run state", () => { + test("run.metadata is the result's, and a resumed run gets it back with fresh globals", async () => { + const gate = latch() + const waiting = latch() + let made = 0 + const runtime = Runtime.make({ + globals: (run) => { + made++ + const state = (run.metadata ??= { count: 0 }) as { count: number } + return { + count: Host.fn(() => ++state.count), + wait: Host.fn( + () => + Effect.andThen( + Effect.sync(waiting.resolve), + Effect.promise(() => gate.promise), + ), + { type: "async", idempotent: true }, + ), + } + }, + }) + const program = runtime.program(`count(); count(); await wait(); return count()`, { runId: "m" }) + const fiber = Effect.runFork(program.run()) + await waiting.promise + const suspended = await Effect.runPromise(program.suspend()) + if (suspended.ok !== "suspended") throw new Error("expected a suspended run") + expect(suspended.state.header.metadata).toEqual({ count: 2 }) + expect(await Effect.runPromise(Fiber.join(fiber))).toBe(suspended) + gate.resolve() + expect(await Effect.runPromise(runtime.resume(toBytes(suspended.state)).run())).toMatchObject({ + ok: true, + value: 3, + metadata: { count: 3 }, + }) + // Made at the start and at the resume (and at every step in the round-trip test mode). + expect(made).toBeGreaterThanOrEqual(2) + }) + + test("a suspend fails while a call that isn't idempotent runs", async () => { + const called = latch() + const runtime = Runtime.make({ + globals: () => ({ + slow: Host.fn(() => Effect.andThen(Effect.sync(called.resolve), Effect.never), { type: "async" }), + }), + }) + const program = runtime.program(`await slow()`) + const fiber = Effect.runFork(program.run()) + await called.promise + const error = await Effect.runPromise(Effect.flip(program.suspend())) + expect(error.running).toMatchObject([{ path: "slow" }]) + await Effect.runPromise(Fiber.interrupt(fiber)) + }) + + test("a suspend fails while a call holds a host function, even an idempotent one", async () => { + const called = latch() + const runtime = Runtime.make({ + globals: () => ({ + open: Host.fn(() => ({ read: () => "called" })), + slow: Host.fn(() => Effect.andThen(Effect.sync(called.resolve), Effect.never), { + type: "async", + idempotent: true, + takesFunctions: true, + }), + }), + }) + const program = runtime.program(`await slow(open().read)`) + const fiber = Effect.runFork(program.run()) + await called.promise + const error = await Effect.runPromise(Effect.flip(program.suspend())) + expect(error.running).toMatchObject([{ path: "slow" }]) + await Effect.runPromise(Fiber.interrupt(fiber)) + }) + + test("a durable function keeps running across a suspend and is waited on again", async () => { + const jobs = new Map>>() + const waiting = latch() + const runtime = Runtime.make({ + globals: () => ({ + job: Host.durableFn({ + start: (_, call) => Effect.sync(() => (jobs.set(call.key, latch()), call.key)), + wait: (key) => + Effect.andThen( + Effect.sync(waiting.resolve), + Effect.promise(() => jobs.get(key)!.promise), + ), + cancel: () => Effect.void, + }), + }), + }) + const program = runtime.program(`return (await job()) + 1`, { runId: "d" }) + const fiber = Effect.runFork(program.run()) + // In `wait`, so the call is started and its data saved. + await waiting.promise + const suspended = await Effect.runPromise(program.suspend()) + if (suspended.ok !== "suspended") throw new Error("expected a suspended run") + await Effect.runPromise(Fiber.join(fiber)) + const resumed = Effect.runPromise(runtime.resume(suspended.state).run()) + jobs.get("d:0")!.resolve(41) + expect(await resumed).toMatchObject({ ok: true, value: 42 }) + }) +}) + +describe("translate", () => { + const translating = (translate?: Runtime.Translate, limits?: Runtime.Limits) => { + const calls: Array> = [] + const runtime = Runtime.make({ + translate, + limits, + globals: () => ({ + keep: Host.fn((args) => void calls.push(args)), + send: Host.fn((args) => Effect.sync(() => void calls.push(args)), { type: "async" }), + later: Host.fn(([n]) => Effect.succeed(n), { type: "async" }), + never: Host.fn(() => Effect.never, { type: "async" }), + }), + }) + return { runtime, calls } + } + + test("by default, a function or a promise inside an argument fails the call", async () => { + const strict = translating().runtime + for (const [code, message] of [ + ["keep({ f() {} })", "Argument 1 to keep contains a function, which cannot be passed to the host."], + ["keep([Promise.resolve(1)])", "Argument 1 to keep contains an un-awaited Promise, which cannot be passed"], + ["await send(() => 1)", "Argument 1 to send contains a function"], + ] as const) { + const result = await run(strict, code) + expect(result.ok).toBe(false) + if (!result.ok) expect(result.error.message).toContain(message) + } + }) + + test("functions: drop leaves functions out as JSON.stringify does", async () => { + const { runtime, calls } = translating({ functions: "drop" }) + await value( + runtime, + `keep({ q: "x", format(v) { return v }, nested: { f: Math.max } }, [1, () => 2, keep], () => 3, new Map([["k", () => 1]]))`, + ) + expect(calls).toEqual([[{ q: "x", nested: {} }, [1, null, null], undefined, new Map([["k", undefined]])]]) + expect(Object.keys(calls[0]![0] as object)).toEqual(["q", "nested"]) + const result = await run(runtime, `keep({ p: Promise.resolve(1) })`) + expect(result.ok).toBe(false) + if (!result.ok) expect(result.error.message).toContain("un-awaited Promise") + }) + + test("promises: await waits for every promise inside the arguments and uses its value", async () => { + const { runtime, calls } = translating({ promises: "await" }) + expect( + await value( + runtime, + ` + const slow = (async () => { await null; await null; return "slow" })() + const a = keep({ n: Promise.resolve(5), list: [later(1), slow] }, later({ deep: later(2) })) + const b = await send(slow, new Promise((resolve) => resolve(later(3)))) + return [a, b] + `, + ), + ).toEqual([null, null]) + expect(calls).toEqual([[{ n: 5, list: [1, "slow"] }, { deep: 2 }], ["slow", 3]]) + const result = await run(runtime, `keep({ f() {} })`) + expect(result.ok).toBe(false) + if (!result.ok) expect(result.error.message).toContain("contains a function") + }) + + test("promises: await fails the call with a rejection's reason", async () => { + const { runtime, calls } = translating({ promises: "await" }) + expect( + await value( + runtime, + ` + const failing = () => Promise.reject(new RangeError("nope")) + let thrown + try { keep({ n: failing() }) } catch (error) { thrown = error } + const sent = send([later(1), failing()]) + return [thrown instanceof RangeError, thrown.message, sent instanceof Promise, await sent.catch((e) => e.message)] + `, + ), + ).toEqual([true, "nope", true, "nope"]) + expect(calls).toEqual([]) + }) + + test("promises: await waits on a promise that never settles until the timeout", async () => { + const result = await run(translating({ promises: "await" }, { timeoutMs: 50 }).runtime, `keep({ n: never() })`) + expect(result.ok).toBe(false) + if (!result.ok) expect(result.error.kind).toBe("TimeoutExceeded") + }) + + test("promises: await keeps waiting across a suspend", async () => { + const started = latch() + const gate = latch() + const calls: Array> = [] + const runtime = Runtime.make({ + translate: { promises: "await" }, + globals: () => ({ + keep: Host.fn((args) => (calls.push(args), "kept")), + slow: Host.fn( + () => Effect.andThen(Effect.sync(started.resolve), Effect.promise(() => gate.promise)), + { type: "async", idempotent: true }, + ), + }), + }) + const program = runtime.program(`return [keep({ n: slow() }), await slow()]`) + const fiber = Effect.runFork(program.run()) + await started.promise + const suspended = await Effect.runPromise(program.suspend()) + if (suspended.ok !== "suspended") throw new Error("expected a suspended run") + await Effect.runPromise(Fiber.join(fiber)) + expect(calls).toEqual([]) + const resumed = Effect.runPromise(runtime.resume(suspended.state).run()) + gate.resolve(7) + expect(await resumed).toMatchObject({ ok: true, value: ["kept", 7] }) + expect(calls).toEqual([[{ n: 7 }]]) + }) +}) diff --git a/packages/codemode/test/limits.test.ts b/packages/codemode/test/limits.test.ts index 781089e398cc..7388bf9f5835 100644 --- a/packages/codemode/test/limits.test.ts +++ b/packages/codemode/test/limits.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from "bun:test" import { Cause, Deferred, Effect, Exit, Fiber, Schema } from "effect" import { CodeMode, Tool } from "../src/index.js" -import { MAX_ARRAY_LENGTH, MAX_PENDING_PROMISES, MAX_STRING_LENGTH } from "../src/runtime/limits.js" +import { MAX_ARRAY_LENGTH, MAX_PENDING_PROMISES, MAX_STRING_LENGTH } from "../src/language/runtime/limits.js" const runtime = CodeMode.make({ tools: {} }) diff --git a/packages/codemode/test/openapi.test.ts b/packages/codemode/test/openapi.test.ts index e82484ea6c9a..cc3417e360b1 100644 --- a/packages/codemode/test/openapi.test.ts +++ b/packages/codemode/test/openapi.test.ts @@ -2,7 +2,7 @@ import { describe, expect, test } from "bun:test" import { Effect, Layer, Option } from "effect" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" import { CodeMode, OpenAPI, Tool } from "../src/index.js" -import { inputTypeScript, outputTypeScript } from "../src/tool-schema.js" +import { inputTypeScript, outputTypeScript } from "../src/codemode/tool-schema.js" const baseUrl = "http://localhost:4096" type Document = OpenAPI.Document diff --git a/packages/codemode/test/preload.ts b/packages/codemode/test/preload.ts index 2f0994917152..80a20420ecbb 100644 --- a/packages/codemode/test/preload.ts +++ b/packages/codemode/test/preload.ts @@ -1,5 +1,5 @@ -import { setRoundTrip } from "../src/runtime/execute.js" -import { nativeRule } from "../src/runtime/objects.js" +import { setRoundTrip } from "../src/language/runtime/execute.js" +import { nativeRule } from "../src/language/runtime/objects.js" // Every test run checks that natives are made only with the intrinsics. nativeRule.check = true diff --git a/packages/codemode/test/prelude-bytecode.test.ts b/packages/codemode/test/prelude-bytecode.test.ts index 1f8ea7daa9c2..1dd6cecd2547 100644 --- a/packages/codemode/test/prelude-bytecode.test.ts +++ b/packages/codemode/test/prelude-bytecode.test.ts @@ -1,9 +1,9 @@ import { describe, expect, test } from "bun:test" import { compileBuiltins } from "../script/prelude.js" -import saved from "../src/prelude/bytecode.json" with { type: "json" } -import { compiledPrelude } from "../src/prelude/prelude.js" -import { hashOf } from "../src/code.js" -import { getInternalPrelude } from "../src/internal-prelude.js" +import saved from "../src/language/prelude/bytecode.json" with { type: "json" } +import { compiledPrelude } from "../src/language/prelude/prelude.js" +import { hashOf } from "../src/language/code.js" +import { getInternalPrelude } from "../src/language/internal-prelude.js" import { moduleUrl, runProgram } from "./run-program.js" // The built-in sources are compiled ahead of time (script/prelude.ts); a process loads that bytecode instead. @@ -26,7 +26,7 @@ describe("precompiled built-ins", () => { // A fresh process runs a program that calls into the prelude, and its source is still unparsed. const script = ` import { runProgram } from ${moduleUrl("run-program.js")} - import { compiledPrelude } from ${moduleUrl("../src/prelude/prelude.js")} + import { compiledPrelude } from ${moduleUrl("../src/language/prelude/prelude.js")} const result = runProgram("return [3, 1, 2].map((x) => x * 2).sort((a, b) => a - b)") console.log(JSON.stringify([result.ok && result.value, typeof compiledPrelude().codes[0].sourceObj.tree]))` const child = Bun.spawn(["bun", "-e", script], { stdout: "pipe", stderr: "inherit" }) diff --git a/packages/codemode/test/promise.test.ts b/packages/codemode/test/promise.test.ts index 947e3a50d69d..f0a1740edb6b 100644 --- a/packages/codemode/test/promise.test.ts +++ b/packages/codemode/test/promise.test.ts @@ -475,7 +475,7 @@ describe("first-class promise values", () => { }) describe("promises at data boundaries", () => { - test("an un-awaited promise inside a result or tool argument is awaited", async () => { + test("an un-awaited promise inside a result or a tool argument is awaited", async () => { expect(await value(`return { result: tools.host.echo({ id: 1 }) }`)).toEqual({ result: 1 }) expect(await value(`return Array.from([Promise.resolve(1)])`)).toEqual([1]) expect(await value(`return await tools.host.echo({ id: tools.host.echo({ id: 1 }) })`)).toBe(1) @@ -1561,7 +1561,7 @@ describe("job order matches V8", () => { describe("a program that can never finish", () => { const stalled = - "Error: The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program." + "Error: The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. There are no timers or sleep, so don't wait or poll inside a program." // Each program stalls at the await on line `line`, column `column`; an outer await only waits on that one. test.each([ diff --git a/packages/codemode/test/resumable.test.ts b/packages/codemode/test/resumable.test.ts index 8bc794f4e3f2..f24e32fd1b1a 100644 --- a/packages/codemode/test/resumable.test.ts +++ b/packages/codemode/test/resumable.test.ts @@ -1,22 +1,20 @@ import { describe, expect, test } from "bun:test" import path from "node:path" -import { parseProgram } from "../src/runtime/execute.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { getOwn, Native, nativeRule, Obj, remove, set } from "../src/runtime/objects.js" +import { parseProgram } from "../src/language/runtime/execute.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" +import { getOwn, Native, nativeRule, Obj, remove, set } from "../src/language/runtime/objects.js" import { CodeMode } from "../src/index.js" -import { decode, encode } from "../src/snapshot/codec.js" -import { Lost, PendingThrow } from "../src/runtime/model.js" -import { normalizeError } from "../src/runtime/errors.js" +import { decode, encode } from "../src/language/snapshot/codec.js" +import { Lost, PendingThrow } from "../src/language/runtime/model.js" +import { normalizeError } from "../src/language/runtime/errors.js" import { Effect } from "effect" -import { ToolRuntime } from "../src/tool-runtime.js" -import { nodeId, sourceOf } from "../src/runtime/source.js" +import { nodeId, sourceOf } from "../src/language/runtime/source.js" // Groundwork for suspending and resuming a run: everything the state refers to has a stable name. const interpreter = () => new Interpreter({ - tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins(), }) diff --git a/packages/codemode/test/run-program.ts b/packages/codemode/test/run-program.ts index 6277b8d88cad..a2733d14c5cb 100644 --- a/packages/codemode/test/run-program.ts +++ b/packages/codemode/test/run-program.ts @@ -1,26 +1,36 @@ import path from "node:path" import { pathToFileURL } from "node:url" import type { Program } from "acorn" -import type { DataValue, Diagnostic, Result } from "../src/codemode.js" -import { normalizeError } from "../src/runtime/errors.js" -import { parseProgram } from "../src/runtime/execute.js" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import { PendingThrow } from "../src/runtime/model.js" -import type { Value } from "../src/runtime/objects.js" -import { Interruption } from "../src/runtime/signals.js" -import type { Source } from "../src/runtime/source.js" -import type { ToolRuntime } from "../src/tool-runtime.js" -import type { Snapshot } from "../src/snapshot/snapshot.js" +import type { Json as DataValue } from "../src/language/data.js" +import { normalizeError } from "../src/language/runtime/errors.js" +import { parseProgram } from "../src/language/runtime/execute.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { type Diagnostic, PendingThrow } from "../src/language/runtime/model.js" +import type { Value } from "../src/language/runtime/objects.js" +import { Interruption } from "../src/language/runtime/signals.js" +import type { Source } from "../src/language/runtime/source.js" +import type { Snapshot } from "../src/language/snapshot/snapshot.js" /** A quoted `file:` URL for a module relative to `test`, to import from a script run with `bun -e` on any platform. */ export const moduleUrl = (relative: string) => JSON.stringify(pathToFileURL(path.join(import.meta.dir, relative)).href) -const emptyTools = { - calls: [], - keys: () => [], -} as unknown as ToolRuntime +/** How a run ended, shaped like a CodeMode result with no tool calls. */ +export type Result = + | { + readonly ok: true + readonly value: DataValue + readonly warnings?: ReadonlyArray + readonly logs?: ReadonlyArray + readonly toolCalls: ReadonlyArray + } + | { + readonly ok: false + readonly error: Diagnostic + readonly logs?: ReadonlyArray + readonly toolCalls: ReadonlyArray + } export type RunOptions = { readonly globals?: (ctx: Interpreter) => ReadonlyArray @@ -61,7 +71,6 @@ export const runProgram = (code: string, options?: RunOptions): Result => { } const runtime = (bindings: RunOptions["bindings"]) => new Interpreter({ - tools: emptyTools, builtins: createBuiltins(), logs: [], globals: options?.globals, @@ -134,7 +143,7 @@ export const runProgram = (code: string, options?: RunOptions): Result => { error: normalizeError( new PendingThrow( "Error", - "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.", + "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. There are no timers or sleep, so don't wait or poll inside a program.", stuckNode, ), ), diff --git a/packages/codemode/test/signature.test.ts b/packages/codemode/test/signature.test.ts index ed41e9b02c21..4f2ddcb33c5d 100644 --- a/packages/codemode/test/signature.test.ts +++ b/packages/codemode/test/signature.test.ts @@ -7,7 +7,7 @@ import { inputTypeScript, jsonSchemaToTypeScript, outputTypeScript, -} from "../src/tool-schema.js" +} from "../src/codemode/tool-schema.js" // A raw JSON Schema tool in the shape an MCP adapter produces: render-only input schema // whose property descriptions and constraints must surface as JSDoc in pretty signatures. diff --git a/packages/codemode/test/snapshot.test.ts b/packages/codemode/test/snapshot.test.ts index 40cc6f4ee2d7..f4639488cdda 100644 --- a/packages/codemode/test/snapshot.test.ts +++ b/packages/codemode/test/snapshot.test.ts @@ -1,14 +1,13 @@ import { describe, expect, test } from "bun:test" import { Effect } from "effect" import { CodeMode } from "../src/index.js" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import { type Snapshot, SnapshotError } from "../src/snapshot/snapshot.js" -import { ToolRuntime } from "../src/tool-runtime.js" -import { hashOf } from "../src/code.js" -import { compileSource } from "../src/compiler/compile.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { type Snapshot, SnapshotError } from "../src/language/snapshot/snapshot.js" +import { hashOf } from "../src/language/code.js" +import { compileSource } from "../src/language/compiler/compile.js" import { moduleUrl, parseVmSource, runProgram } from "./run-program.js" -import { buildId } from "../src/snapshot.js" +import { buildId } from "../src/language/snapshot.js" // The VM's snapshots: a run saved and restored into a fresh runtime anywhere it slices goes on as if it never stopped. @@ -123,7 +122,6 @@ describe("round trips", () => { /** A runtime the way `execute.ts` makes one, with no tools. */ const runtime = () => new Interpreter({ - tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins(), }) as Interpreter @@ -179,7 +177,7 @@ const refusal = (snapshot: unknown) => { describe("snapshots", () => { test("hold code by source and function index, with the source's bytecode hash, and never bytecode", () => { const snapshot = sliced() - expect(snapshot.format).toBe(2) + expect(snapshot.format).toBe(3) const source = snapshot.header.sources[0]! expect(source.id).toBe("main") expect(source.bytecode).toBe(hashOf(compileSource(parse(source.code)))) @@ -281,14 +279,13 @@ describe("determinism", () => { const codes = Object.values(programs) const local = { hashes: codes.map((code) => hashOf(compileSource(parse(code)))), build: (runtime(), buildId()) } const script = ` - import { hashOf } from ${moduleUrl("../src/code.js")} - import { compileSource } from ${moduleUrl("../src/compiler/compile.js")} + import { hashOf } from ${moduleUrl("../src/language/code.js")} + import { compileSource } from ${moduleUrl("../src/language/compiler/compile.js")} import { parseVmSource } from ${moduleUrl("run-program.js")} - import { buildId } from ${moduleUrl("../src/snapshot.js")} - import { createBuiltins } from ${moduleUrl("../src/runtime/intrinsics.js")} - import { Interpreter } from ${moduleUrl("../src/runtime/interpreter.js")} - import { ToolRuntime } from ${moduleUrl("../src/tool-runtime.js")} - new Interpreter({ tools: ToolRuntime.make(ToolRuntime.prepare({}), undefined, {}), builtins: createBuiltins() }) + import { buildId } from ${moduleUrl("../src/language/snapshot.js")} + import { createBuiltins } from ${moduleUrl("../src/language/runtime/intrinsics.js")} + import { Interpreter } from ${moduleUrl("../src/language/runtime/interpreter.js")} + new Interpreter({ builtins: createBuiltins() }) const codes = ${JSON.stringify(codes)} console.log(JSON.stringify({ hashes: codes.map((code) => hashOf(compileSource(parseVmSource(code).source))), build: buildId() }))` const child = Bun.spawn(["bun", "-e", script], { stdout: "pipe", stderr: "inherit" }) diff --git a/packages/codemode/test/state.test.ts b/packages/codemode/test/state.test.ts index a085113421ba..5f53bd1e6361 100644 --- a/packages/codemode/test/state.test.ts +++ b/packages/codemode/test/state.test.ts @@ -3,18 +3,19 @@ import { Glob } from "bun" import path from "node:path" import { Effect, Schema } from "effect" import { CodeMode, Tool } from "../src/index.js" -import { parseProgram } from "../src/runtime/execute.js" -import { Interpreter } from "../src/runtime/interpreter.js" -import { createBuiltins } from "../src/runtime/intrinsics.js" -import { GeneratorObj, type Native, nativeRule, Obj, PromiseObj, type Value } from "../src/runtime/objects.js" -import { normal } from "../src/runtime/signals.js" -import { ToolRuntime } from "../src/tool-runtime.js" -import { Code } from "../src/code.js" -import { AsyncFromSyncIterator } from "../src/generators.js" -import { VmIntrinsic } from "../src/internal-prelude.js" -import { Cell, CodeFlags } from "../src/opcodes.js" -import type { Stop } from "../src/scheduler.js" -import { CustomIter, Frame, Mode, Vm } from "../src/vm.js" +import { Host } from "../src/language/host-function.js" +import { parseProgram } from "../src/language/runtime/execute.js" +import { hostGlobals } from "../src/language/runtime/extensions.js" +import { Interpreter } from "../src/language/runtime/interpreter.js" +import { createBuiltins } from "../src/language/runtime/intrinsics.js" +import { GeneratorObj, type Native, nativeRule, Obj, PromiseObj, type Value } from "../src/language/runtime/objects.js" +import { normal } from "../src/language/runtime/signals.js" +import { Code } from "../src/language/code.js" +import { AsyncFromSyncIterator } from "../src/language/generators.js" +import { VmIntrinsic } from "../src/language/internal-prelude.js" +import { Cell, CodeFlags } from "../src/language/opcodes.js" +import type { Stop } from "../src/language/scheduler.js" +import { CustomIter, Frame, Mode, Vm } from "../src/language/vm.js" // The VM (`src/vm.ts`) runs programs as bytecode frames, and keeps the jobs, promises, and host calls they wait on as // data: a frame is its code, pc, and registers, and every suspension point is one. The Effect driver only does what @@ -45,12 +46,21 @@ const error = async (code: string) => { return result.error } +// Host globals for a VM stepped by hand: the test completes their calls itself. +const echo = Host.fn(([input]) => (input as { value: number }).value, { type: "async" }) +const vmGlobals = { + tools: Host.namespace({ + keys: () => ["echo"], + resolve: (path) => (path.join(".") === "echo" ? echo : Host.missing(`No tools.${path.join(".")}`)), + }), + search: Host.fn(() => Effect.succeed({ items: [] }), { type: "sync-thenable" }), +} + // A VM for `code`, stepped by hand instead of by the Effect driver. const vmFor = (code: string) => { const parsed = parseProgram(code) if (!parsed.ok) throw new Error(parsed.error.message) - const tools = ToolRuntime.make(ToolRuntime.prepare(runtimeTools), undefined, {}) - const vm = new Interpreter({ tools, builtins: createBuiltins() }).vm + const vm = new Interpreter({ builtins: createBuiltins(), globals: (ctx) => hostGlobals(ctx, vmGlobals) }).vm vm.start(parsed.source) return vm } @@ -254,10 +264,14 @@ describe("vm", () => { const started = stop(vm) if (started.kind !== "host") throw new Error(`expected a host stop, got ${started.kind}`) expect( - started.calls.map((call) => ({ request: call.request, blocking: call.blocking, status: call.status })), + started.calls.map((call) => ({ + call: { label: call.label, path: call.path, args: call.args, policy: call.policy }, + blocking: call.blocking, + status: call.status, + })), ).toEqual( [1, 2, 3].map((value) => ({ - request: { kind: "tool", path: ["echo"], args: [{ value }] }, + call: { label: "tools.echo", path: ["tools", "echo"], args: [{ value }], policy: "async" }, blocking: false, status: "queued", })), @@ -273,7 +287,7 @@ describe("vm", () => { const vm = vmFor(`const found = search({ query: "echo" }); return found.items.length`) const started = stop(vm) if (started.kind !== "host") throw new Error(`expected a host stop, got ${started.kind}`) - expect(started.calls.map((call) => [call.request.kind, call.blocking])).toEqual([["search", true]]) + expect(started.calls.map((call) => [call.label, call.blocking])).toEqual([["search", true]]) expect(stop(vm)).toEqual({ kind: "host", calls: [] }) vm.complete(started.calls[0]!.id, normal({ items: [1, 2], remaining: 0, next: null })) expect(stop(vm)).toEqual({ kind: "done", completion: normal(2) }) @@ -385,23 +399,19 @@ describe("vm", () => { test("the VM does not import Effect", async () => { const root = path.join(import.meta.dir, "..") + // The language's host boundary and driver; Code Mode (src/codemode/) is built on Effect throughout. const host = new Set([ - "src/codemode.ts", - "src/extension.ts", - "src/scan.ts", - "src/tool-error.ts", - "src/tool-runtime.ts", - "src/tool-schema.ts", - "src/tool.ts", - "src/runtime/driver.ts", - "src/runtime/execute.ts", - "src/runtime/extensions.ts", - "src/runtime/signals.ts", + "src/language/host-function.ts", + "src/language/runtime.ts", + "src/language/runtime/driver.ts", + "src/language/runtime/execute.ts", + "src/language/runtime/extensions.ts", + "src/language/runtime/signals.ts", ]) // Glob paths use the platform's separator. - const files = (await Array.fromAsync(new Glob("src/**/*.ts").scan({ cwd: root }))) + const files = (await Array.fromAsync(new Glob("src/language/**/*.ts").scan({ cwd: root }))) .map((file) => file.split(path.sep).join("/")) - .filter((file) => !file.startsWith("src/openapi/") && !host.has(file)) + .filter((file) => !host.has(file)) expect(files.length).toBeGreaterThan(0) for (const file of files) { expect(await Bun.file(path.join(root, file)).text()).not.toMatch(/from "effect"/) @@ -447,7 +457,7 @@ describe("built-ins as data", () => { const tried = await Promise.try(() => work(6)) const thenned = await work(1).then((v) => v + 1).catch(() => 0).finally(() => log.push("finally")) const thenable = await { then: (resolve) => resolve("thenable") } - const tool = await tools.echo({ value: work(10) }) + const tool = await tools.echo({ value: await work(10) }) const found2 = search({ query: "echo" }).items.length async function* agen() { yield 1; yield await work(1) } const collected = [] @@ -467,8 +477,7 @@ describe("built-ins as data", () => { if (next.kind === "stuck") throw new Error("the run is stuck") if (next.kind !== "host") continue for (const call of next.calls) { - const request = call.request - if (request.kind === "tool") vm.complete(call.id, normal((request.args[0] as { value: number }).value)) + if (call.policy === "async") vm.complete(call.id, normal((call.args[0] as { value: number }).value)) else vm.complete(call.id, normal({ items: [1], remaining: 0, next: null })) } } diff --git a/packages/codemode/test/suspend.test.ts b/packages/codemode/test/suspend.test.ts index ef3aec35405c..557d41633668 100644 --- a/packages/codemode/test/suspend.test.ts +++ b/packages/codemode/test/suspend.test.ts @@ -8,6 +8,9 @@ import { CodeMode, Extension, Tool } from "../src/index.js" const Value = Schema.Struct({ value: Schema.Number }) +/** How many tool calls a saved run had admitted, from the run's metadata. */ +const admitted = (state: CodeMode.Snapshot) => (state.header.metadata as { calls: Array }).calls.length + /** Runs a program in a fiber of its own: its outcome, and a way to suspend it. */ const background = (program: CodeMode.Program) => { const fiber = Effect.runFork(program.run()) @@ -410,7 +413,7 @@ describe("suspend", () => { const program = CodeMode.make().program(`return 1`) expect(await Effect.runPromise(program.run())).toMatchObject({ ok: true, value: 1 }) const exit = await Effect.runPromise(Effect.exit(program.run())) - expect(String(exit)).toContain("A CodeMode program runs once") + expect(String(exit)).toContain("A program runs once") }) test("interrupting run interrupts the program and its running calls", async () => { @@ -450,7 +453,9 @@ describe("suspend", () => { test("the snapshot codec does not import Effect", async () => { for (const file of ["codec.ts", "snapshot.ts"]) { - expect(await Bun.file(path.join(import.meta.dir, "../src/snapshot", file)).text()).not.toMatch(/from "effect"/) + expect(await Bun.file(path.join(import.meta.dir, "../src/language/snapshot", file)).text()).not.toMatch( + /from "effect"/, + ) } }) }) @@ -495,7 +500,8 @@ describe("resume", () => { { limits: { maxToolCalls: 2, maxOutputBytes: 10 } }, ) const suspended = suspendedOf(await Effect.runPromise(run.suspend)) - expect(suspended.state.header.limits).toEqual({ timeoutMs: null, maxToolCalls: 2, maxOutputBytes: 10 }) + expect(suspended.state.header.limits).toEqual({ timeoutMs: null, maxOutputBytes: 10 }) + expect(suspended.state.header.metadata).toMatchObject({ maxToolCalls: 2 }) // The resuming runtime has no limits of its own; the saved ones apply. const result = await Effect.runPromise( background(CodeMode.make({ tools: { step: step.tool } }).resume(suspended.state)).result, @@ -528,7 +534,7 @@ describe("resume", () => { await Bun.sleep(210) const suspended = suspendedOf(await Effect.runPromise(run.suspend)) expect(suspended.state.header.budget.activeMs).toBeGreaterThanOrEqual(200) - expect(suspended.state.header.budget.toolCalls).toBe(1) + expect(admitted(suspended.state)).toBe(1) // 200ms were used before the suspend; the next 200ms of work go past the 300ms limit. const timedOut = await Effect.runPromise(background(runtime.resume(suspended.state)).result) expect(timedOut).toMatchObject({ ok: false, error: { kind: "TimeoutExceeded" }, toolCalls: [{ name: "step" }] }) @@ -798,7 +804,7 @@ describe("idempotent and durable tools", () => { ) await read.called() const suspended = suspendedOf(await Effect.runPromise(run.suspend)) - expect(suspended.state.header.budget.toolCalls).toBe(1) + expect(admitted(suspended.state)).toBe(1) read.reset() const resumed = background(runtime().resume(CodeMode.toBytes(suspended.state))) @@ -1314,7 +1320,7 @@ describe("idempotent and durable tools", () => { const toolSets: ReadonlyArray> = [{}, { job: plainTool }, { job: read.tool }] for (const tools of toolSets) { expect((await refusal(CodeMode.make({ tools }).resume(suspendedWhileStarted.state))).message).toContain( - "cannot resume because that tool is no longer a durable tool", + "cannot resume because that function is no longer a durable function", ) } @@ -1449,19 +1455,55 @@ describe("extension snapshot hooks", () => { }) }) + test("a restored function passed back to the host arrives as the restored host function", async () => { + const files = () => { + const texts = new WeakSet() + const file = (name: string) => { + const text = () => name + texts.add(text) + return { name, text } + } + return Extension.make({ + name: "files", + globals: { file, known: (text: Function) => texts.has(text) }, + snapshot: { + encode: (value) => ({ name: (value as { readonly name: string }).name }), + decode: (data) => file((data as { readonly name: string }).name), + }, + }) + } + const jobs = durableJobs() + const run = background( + CodeMode.make({ tools: { job: jobs.tool }, extensions: [files()] }).program( + `const f = file("a"); await tools.job({ value: 1 }); return known(f.text)`, + undefined, + { runId: "h" }, + ), + ) + await jobs.waiting() + const suspended = suspendedOf(await Effect.runPromise(run.suspend)) + jobs.reset() + const resumed = background( + CodeMode.make({ tools: { job: jobs.tool }, extensions: [files()] }).resume(CodeMode.toBytes(suspended.state)), + ) + await jobs.waiting() + jobs.finish("h:1", 1) + expect(await Effect.runPromise(resumed.result)).toMatchObject({ ok: true, value: true }) + }) + test("a suspend fails, and the run goes on, when encode throws or returns non-plain data", async () => { const cases: ReadonlyArray, string]> = [ [ { encode: () => undefined }, - "The run's state couldn't be saved: The extension \"fetch\" saved a value it returned as data holding undefined; snapshot.encode must give plain data (JSON values and Uint8Arrays).", + "The run's state couldn't be saved: The host function fetch saved a value it returned as data holding undefined; snapshot.encode must give plain data (JSON values and Uint8Arrays).", ], [ { encode: () => ({ when: new Date(0) }) }, - "The run's state couldn't be saved: The extension \"fetch\" saved a value it returned as data holding a Date; snapshot.encode must give plain data (JSON values and Uint8Arrays).", + "The run's state couldn't be saved: The host function fetch saved a value it returned as data holding a Date; snapshot.encode must give plain data (JSON values and Uint8Arrays).", ], [ { encode: () => ({ run: () => 1 }) }, - "The run's state couldn't be saved: The extension \"fetch\" saved a value it returned as data holding a function; snapshot.encode must give plain data (JSON values and Uint8Arrays).", + "The run's state couldn't be saved: The host function fetch saved a value it returned as data holding a function; snapshot.encode must give plain data (JSON values and Uint8Arrays).", ], [ { @@ -1469,7 +1511,7 @@ describe("extension snapshot hooks", () => { throw new Error("no") }, }, - "The run's state couldn't be saved: The extension \"fetch\" couldn't save a value it returned: no", + "The run's state couldn't be saved: The host function fetch couldn't save a value it returned: no", ], ] for (const [snapshot, message] of cases) { @@ -1524,16 +1566,15 @@ describe("extension snapshot hooks", () => { }) }) - test("a state that holds a hooked value is refused without that extension or without its hooks", async () => { + test("a state that holds a hooked value is refused without that extension's globals or without its hooks", async () => { const { extension } = web() const { jobs, run } = await suspendWith(extension) const suspended = suspendedOf(await Effect.runPromise(run.suspend)) const unhooked = Extension.make({ name: "fetch", globals: extension.globals }) - const other = Extension.make({ name: "other", globals: extension.globals, snapshot: extension.snapshot }) - for (const extensions of [[], [unhooked], [other]]) { + for (const extensions of [[], [unhooked]]) { expect( (await refusal(CodeMode.make({ tools: { job: jobs.tool }, extensions }).resume(suspended.state))).message, - ).toBe("This state was saved by a runtime with other host globals (extensions); resume it with the same ones.") + ).toBe("This state was saved by a runtime with other host globals; resume it with the same ones.") } }) }) diff --git a/packages/codemode/test/test262.test.ts b/packages/codemode/test/test262.test.ts index ea5a185e7ea0..e368e54510c6 100644 --- a/packages/codemode/test/test262.test.ts +++ b/packages/codemode/test/test262.test.ts @@ -7,7 +7,7 @@ import { test } from "bun:test" import { root, run, skipped } from "./test262/run.js" -for (const file of [...new Bun.Glob("**/*.js").scanSync({ cwd: root })].sort()) { +for (const file of [...new Bun.Glob("**/*.js").scanSync({ cwd: root, followSymlinks: true })].sort()) { const define = skipped.has(file) ? test.skip : test define( file, diff --git a/packages/codemode/test/test262/run.ts b/packages/codemode/test/test262/run.ts index 1f8dd8d91ec5..ebc69d0c40c7 100644 --- a/packages/codemode/test/test262/run.ts +++ b/packages/codemode/test/test262/run.ts @@ -4,13 +4,12 @@ // failure messages are the host's (`$assert`). import path from "node:path" import { Effect } from "effect" -import { finishExecution, startProgram } from "../../src/runtime/execute.js" -import type { Interpreter } from "../../src/runtime/interpreter.js" -import { Throw } from "../../src/runtime/model.js" -import { createErrorValue } from "../../src/runtime/intrinsics.js" -import { constructor, fn, methods } from "../../src/runtime/native.js" -import { Callable, define, get, hidden, Arr, Fn, Obj, type Value } from "../../src/runtime/objects.js" -import { ToolRuntime } from "../../src/tool-runtime.js" +import { finishExecution, type Setup, startProgram } from "../../src/language/runtime/execute.js" +import type { Interpreter } from "../../src/language/runtime/interpreter.js" +import { Throw } from "../../src/language/runtime/model.js" +import { createErrorValue } from "../../src/language/runtime/intrinsics.js" +import { constructor, fn, methods } from "../../src/language/runtime/native.js" +import { Callable, define, get, hidden, Arr, Fn, Obj, type Value } from "../../src/language/runtime/objects.js" export const root = import.meta.dir @@ -29,11 +28,7 @@ type Frontmatter = { negative?: { phase: "parse" | "resolution" | "runtime"; type: string } } -const setup = { - prepared: ToolRuntime.prepare({}), - limits: { timeoutMs: 5000, maxToolCalls: undefined, maxOutputBytes: undefined }, - hooks: {}, -} +const setup: Setup = { limits: { timeoutMs: 5000, maxOutputBytes: undefined } } // One line, on the line of the "use strict" directive, so the test's own line numbers stay as they are. const prelude = [ @@ -56,10 +51,10 @@ export const run = async (file: string): Promise => { const drain = meta.flags?.includes("async") ? "\nfor (let i = 0; i < 100; i++) await null" : "" const result = await Effect.runPromise( finishExecution( - startProgram( + startProgram( { ...setup, - globals: (ctx) => + programGlobals: (ctx) => harness(ctx, (error) => { done ??= { error } }), diff --git a/packages/codemode/test/tool-paths.test.ts b/packages/codemode/test/tool-paths.test.ts index ed80e31e3756..d827a7a5d914 100644 --- a/packages/codemode/test/tool-paths.test.ts +++ b/packages/codemode/test/tool-paths.test.ts @@ -481,3 +481,49 @@ describe("tool references under ==", () => { ).toEqual([false, false, false, 0]) }) }) + +describe("unknown tool calls reject their promise", () => { + const runtime = CodeMode.make({ tools: { shop: { show: echo("Show", "shown"), list: echo("List", "listed") } } }) + + test("try/await catches the rejection", async () => { + expect( + await value(runtime, `try { await tools.shop.nope({}) } catch (error) { return error.message }`), + ).toBe("Unknown tool 'shop.nope'.") + }) + + test(".catch on the call sees the rejection", async () => { + expect(await value(runtime, `return await tools.shop.nope({}).catch((error) => error.message)`)).toBe( + "Unknown tool 'shop.nope'.", + ) + }) + + test("Promise.allSettled settles an unknown tool as rejected", async () => { + expect( + await value( + runtime, + `const settled = await Promise.allSettled([tools.shop.show({}), tools.shop.nope({})]) + return settled.map((result) => result.status)`, + ), + ).toEqual(["fulfilled", "rejected"]) + }) + + test("calling a namespace rejects too", async () => { + expect(await value(runtime, `return await tools.shop({}).catch((error) => error.message)`)).toBe( + "Tool 'shop' is not callable, it is a namespace. Did you mean tools.shop.list or tools.shop.show?", + ) + }) + + test("an unhandled rejection keeps its diagnostic", async () => { + const unknown = await failure(runtime, `return await tools.shop.shows({})`) + expect(unknown.kind).toBe("UnknownTool") + expect(unknown.message).toBe("Unknown tool 'shop.shows'. Did you mean tools.shop.show?") + expect(unknown.suggestions).toEqual(["Use search to find available tools."]) + }) + + test("reading an unknown path is unchanged", async () => { + expect(await value(runtime, `return [typeof tools.shop.nope, Object.keys(tools.shop)]`)).toEqual([ + "function", + ["show", "list"], + ]) + }) +}) diff --git a/packages/codemode/test/verify-bytecode.ts b/packages/codemode/test/verify-bytecode.ts index 9babd65dbc43..ad719ef1976c 100644 --- a/packages/codemode/test/verify-bytecode.ts +++ b/packages/codemode/test/verify-bytecode.ts @@ -1,5 +1,5 @@ -import type { Code } from "../src/code.js" -import { CheckBaseKind, CondKind, ImportKind, Op, OpNames } from "../src/opcodes.js" +import type { Code } from "../src/language/code.js" +import { CheckBaseKind, CondKind, ImportKind, Op, OpNames } from "../src/language/opcodes.js" /** * Stack-depth verifier (C5): runs abstract interpretation over packed bytecode independent of `CodeBuilder`, diff --git a/packages/codemode/test/vm.test.ts b/packages/codemode/test/vm.test.ts index b33c1cfb2c90..aba7a22f2fbf 100644 --- a/packages/codemode/test/vm.test.ts +++ b/packages/codemode/test/vm.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from "bun:test" import { disassembleSource } from "../script/disasm.js" -import { hashOf } from "../src/code.js" -import { compileSource } from "../src/compiler/compile.js" +import { hashOf } from "../src/language/code.js" +import { compileSource } from "../src/language/compiler/compile.js" import { verifyAll } from "./verify-bytecode.js" import { parseVmSource, runProgram } from "./run-program.js" diff --git a/packages/core/src/plugin.ts b/packages/core/src/plugin.ts index d6b11356ed18..7d0ba1c0eb44 100644 --- a/packages/core/src/plugin.ts +++ b/packages/core/src/plugin.ts @@ -9,6 +9,7 @@ import { LayerNode } from "@opencode/util/effect/layer-node" import type { PersistentPty } from "./persistent-pty.js" import { Cause, Context, Effect, Exit, Latch, Layer, Logger, Queue, References, Scope, Semaphore } from "effect" import { Bus } from "./bus.js" +import { Location } from "./location.js" import { PluginHost } from "./plugin/host.js" import { type Failure, type Generation, Service } from "./plugin/service.js" import { State } from "./state.js" @@ -19,6 +20,7 @@ const layer = Layer.effect( Service, Effect.gen(function* () { const bus = yield* Bus.Service + const location = yield* Location.Service const scope = yield* Scope.make() // One slot per requested definition in activation order, including ones whose setup failed, so // the prefix diff below stays index-aligned and a failed revision is not retried until it changes. @@ -27,16 +29,41 @@ const layer = Layer.effect( const ready = yield* Latch.make(true) const pending = new Set() let closed = false + // `plugin.list` reports readiness, so its flips are announced like inventory changes. A failing listener must not + // keep a hold pending or end the worker that releases one. + const announce = bus + .publish( + Plugin.Event.Updated, + {}, + { location: { directory: location.directory, workspaceID: location.workspaceID } }, + ) + .pipe( + Effect.asVoid, + Effect.catchCauseIf( + (cause) => !Cause.hasInterrupts(cause), + (cause) => Effect.logError("failed to announce plugin readiness", { cause }), + ), + ) const holdUnsafe = () => { if (closed) return Effect.void const token = {} pending.add(token) ready.closeUnsafe() - return Effect.sync(() => { - if (pending.delete(token) && pending.size === 0) ready.openUnsafe() + return Effect.suspend(() => { + if (!pending.delete(token) || pending.size > 0) return Effect.void + ready.openUnsafe() + return announce }) } - const hold = () => Effect.sync(holdUnsafe) + const isReady = Effect.sync(() => pending.size === 0) + const hold = () => + Effect.uninterruptible( + Effect.suspend(() => { + const settled = pending.size === 0 + const release = holdUnsafe() + return settled && !closed ? announce.pipe(Effect.as(release)) : Effect.succeed(release) + }), + ) const pendingFailures = yield* Queue.unbounded() let discovered: readonly Failure[] = [] let inventory: Plugin.Info[] = [] @@ -76,7 +103,7 @@ const layer = Layer.effect( release: holdUnsafe(), }) }) - const host = yield* PluginHost.make({ list }, plugin.id).pipe(Effect.provideContext(hostContext)) + const host = yield* PluginHost.make({ list, ready: isReady }, plugin.id).pipe(Effect.provideContext(hostContext)) const exit = yield* Effect.suspend(() => plugin.effect(host)).pipe( grouped, inherit, @@ -269,6 +296,7 @@ const layer = Layer.effect( activate, close, awaitActivation: ready.await, + ready: isReady, hold, list, gui, diff --git a/packages/core/src/plugin/host.ts b/packages/core/src/plugin/host.ts index 19a163d9aee0..8e08779e6840 100644 --- a/packages/core/src/plugin/host.ts +++ b/packages/core/src/plugin/host.ts @@ -50,7 +50,7 @@ const isRpcType = (type: string): type is RpcEvent["type"] => type.startsWith("r const isRpcEvent = (event: Event.Payload): event is RpcEvent => isRpcType(event.type) const isPublicEvent = (event: Event.Payload): event is EventManifest.ServerEvent | RpcEvent => EventManifest.isServer(event) || isRpcEvent(event) -export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick, pluginID: string) { +export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick, pluginID: string) { const app = yield* App.Service const agents = yield* Agent.Service const artifacts = yield* Artifact.Service @@ -466,7 +466,11 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick response(plugin.list()), + list: () => + // Readiness first, so a list read after it is never older than a `true`. + Effect.all([plugin.ready, response(plugin.list())]).pipe( + Effect.map(([ready, listed]) => ({ ...listed, ready })), + ), }, reference: { list: () => response(reference.list()), diff --git a/packages/core/src/plugin/service.ts b/packages/core/src/plugin/service.ts index d647969c8ab3..0f31a474e21b 100644 --- a/packages/core/src/plugin/service.ts +++ b/packages/core/src/plugin/service.ts @@ -14,6 +14,10 @@ export interface Interface { readonly close: (exit: Exit.Exit) => Effect.Effect /** Wait for announced updates and activation to settle; failures remain in the inventory. */ readonly awaitActivation: Effect.Effect + /** + * Whether activation has settled right now, without waiting. `plugin.updated` is published whenever it flips. + */ + readonly ready: Effect.Effect /** Keep readiness pending while preparing an update. Run the returned Effect to release it. */ readonly hold: () => Effect.Effect> } diff --git a/packages/core/src/plugin/supervisor.ts b/packages/core/src/plugin/supervisor.ts index c5f320e4c800..9dd418aebfd8 100644 --- a/packages/core/src/plugin/supervisor.ts +++ b/packages/core/src/plugin/supervisor.ts @@ -194,7 +194,12 @@ export const layer = Layer.effectDiscard( // Make accepted work visible to awaitActivation before coalescing the burst. const notify = Effect.gen(function* () { observed++ - if (!release) release = yield* registry.hold() + // Another source can take the hold while this one waits on it; keep one and give the extra back. + if (!release) { + const held = yield* registry.hold() + if (release) yield* held + else release = held + } yield* Queue.offer(triggers, observed) }) const watch = (stream: Stream.Stream) => diff --git a/packages/core/src/tool/plugin/edit.ts b/packages/core/src/tool/plugin/edit.ts index c449b444b69a..949fc767bef5 100644 --- a/packages/core/src/tool/plugin/edit.ts +++ b/packages/core/src/tool/plugin/edit.ts @@ -272,7 +272,9 @@ export const Plugin = { .transform((editor) => editor.add({ name, - options: { codemode: flags.resolve(loaded.entries).full_codemode.enabled, permission: "edit" }, + options: flags.resolve(loaded.entries).full_codemode.enabled + ? { codemode: true, pinned: true, permission: "edit" } + : { codemode: false, permission: "edit" }, description: "Edit the contents of a file by finding and replacing exact text. When editing text from Read output, preserve the exact indentation (tabs or spaces) and omit the line-number prefix, such as `1: `. Never include the prefix in oldString or newString. Write line breaks as \\n; the file's existing CRLF or LF line endings are matched and preserved. To change line endings themselves, make oldString and newString differ only in \\r; that edit applies to the exact bytes. If there is no exact match, the edit retries while ignoring typographic quote, dash, and space variants, then trailing whitespace on each line. The edit fails if oldString is not found. By default, oldString must identify a UNIQUE location. Multiple matches FAIL unless replaceAll is true. Add more surrounding context to disambiguate, or set replaceAll to true to replace every occurrence. Use replaceAll when the change should apply to every occurrence, such as renaming a variable.", input: Input, diff --git a/packages/core/src/tool/plugin/patch.ts b/packages/core/src/tool/plugin/patch.ts index e08655e3a5be..8b58eaa3bb58 100644 --- a/packages/core/src/tool/plugin/patch.ts +++ b/packages/core/src/tool/plugin/patch.ts @@ -68,7 +68,9 @@ export const Plugin = { .transform((editor) => editor.add({ name, - options: { codemode: flags.resolve(loaded.entries).full_codemode.enabled, permission: "edit" }, + options: flags.resolve(loaded.entries).full_codemode.enabled + ? { codemode: true, pinned: true, permission: "edit" } + : { codemode: false, permission: "edit" }, description: DESCRIPTION, input: Input, freeform: { type: "lark", grammar: GRAMMAR }, diff --git a/packages/core/src/tool/plugin/read.ts b/packages/core/src/tool/plugin/read.ts index e85490bd75d7..d9e1bdce34ee 100644 --- a/packages/core/src/tool/plugin/read.ts +++ b/packages/core/src/tool/plugin/read.ts @@ -53,7 +53,7 @@ export const Plugin = { const codeMode = flags.resolve(loaded.entries).full_codemode.enabled editor.add({ name, - options: { codemode: codeMode }, + options: codeMode ? { codemode: true, pinned: true } : { codemode: false }, description: [ "Read the contents of a file or directory. Supports text files, images, and PDFs.", ...(codeMode diff --git a/packages/core/src/tool/plugin/write.ts b/packages/core/src/tool/plugin/write.ts index 472035af3c6c..882ef22d69a3 100644 --- a/packages/core/src/tool/plugin/write.ts +++ b/packages/core/src/tool/plugin/write.ts @@ -44,7 +44,9 @@ export const Plugin = { .transform((editor) => editor.add({ name, - options: { codemode: flags.resolve(loaded.entries).full_codemode.enabled, permission: "edit" }, + options: flags.resolve(loaded.entries).full_codemode.enabled + ? { codemode: true, pinned: true, permission: "edit" } + : { codemode: false, permission: "edit" }, description: "Writes a file to the local filesystem, overwriting if one exists.\n\nMissing parent directories are created automatically.\n\nUse this tool to create new files or overwrite existing files. For partial changes, use the edit tool instead.", input: Input, diff --git a/packages/core/test/plugin.test.ts b/packages/core/test/plugin.test.ts index e4731c8bc7da..b91eb49ec0c6 100644 --- a/packages/core/test/plugin.test.ts +++ b/packages/core/test/plugin.test.ts @@ -92,6 +92,33 @@ it.effect("updates inventory metadata without restarting an unchanged generation }), ) +it.effect("reports readiness, announcing each flip, and overlapping holds flip it once", () => + Effect.gen(function* () { + const plugins = yield* Plugin.Service + const bus = yield* Bus.Service + const seen: boolean[] = [] + yield* Effect.acquireRelease( + bus.listen((event) => + event.type === Plugin.Event.Updated.type + ? plugins.ready.pipe(Effect.map((ready) => void seen.push(ready))) + : Effect.void, + ), + (unsubscribe) => unsubscribe, + ) + yield* plugins.awaitActivation + expect(yield* plugins.ready).toBe(true) + + const first = yield* plugins.hold() + const second = yield* plugins.hold() + expect(yield* plugins.ready).toBe(false) + yield* first + expect(yield* plugins.ready).toBe(false) + yield* second + expect(yield* plugins.ready).toBe(true) + expect(seen).toEqual([false, true]) + }), +) + it.live("loads a local plugin with its configured options", () => Effect.gen(function* () { const plugins = yield* Plugin.Service @@ -163,7 +190,8 @@ it.live("serves a local plugin's GUI bundle, and reloads it alone when only the expect(edited.revision).toBe(first.revision) expect((yield* plugins.gui(id))?.revision).not.toBe(served?.revision) - expect(updates.length).toBe(published + 1) + // The new bundle, plus readiness flipping to false and back to true around the activation. + expect(updates.length).toBe(published + 3) expect(yield* Effect.promise(() => Bun.file(starts).text())).toBe("x") }).pipe(Effect.scoped), ) diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 01b655fea146..ff413ef89b62 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -493,6 +493,9 @@ "location": { "$ref": "#/components/schemas/Location.PublicRef" }, + "ready": { + "type": "boolean" + }, "data": { "type": "array", "items": { @@ -537,7 +540,7 @@ } } }, - "description": "Retrieve enabled server plugins and their current status.", + "description": "Retrieve enabled server plugins and their current status. `ready` is false while the location's plugins are still loading, so lists such as models, agents and skills may be partial; `plugin.updated` is published whenever it flips.", "summary": "List plugins" } }, diff --git a/packages/protocol/src/groups/plugin.ts b/packages/protocol/src/groups/plugin.ts index 3245b3438475..f12332beba63 100644 --- a/packages/protocol/src/groups/plugin.ts +++ b/packages/protocol/src/groups/plugin.ts @@ -1,5 +1,6 @@ import { Location } from "@opencode/schema/location" import { Plugin } from "@opencode/schema/plugin" +import { optional } from "@opencode/schema/schema" import { Schema } from "effect" import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/http-api" import { InvalidRequestError, PluginNotFoundError, ServiceUnavailableError } from "../errors.js" @@ -9,14 +10,23 @@ export const PluginGroup = HttpApiGroup.make("server.plugin") .add( HttpApiEndpoint.get("plugin.list", "/api/plugin", { query: LocationQuery, - success: Location.response(Schema.Array(Plugin.Info)), + success: Schema.Struct({ + location: Location.PublicRef, + /** + * False while the location's plugins are still loading; lists such as models and agents may be partial. Servers + * from before this field omit it. + */ + ready: optional(Schema.Boolean), + data: Schema.Array(Plugin.Info), + }), }) .annotateMerge(locationQueryOpenApi) .annotateMerge( OpenApi.annotations({ identifier: "plugin.list", summary: "List plugins", - description: "Retrieve enabled server plugins and their current status.", + description: + "Retrieve enabled server plugins and their current status. `ready` is false while the location's plugins are still loading, so lists such as models, agents and skills may be partial; `plugin.updated` is published whenever it flips.", }), ), ) diff --git a/packages/server/src/handlers/plugin.ts b/packages/server/src/handlers/plugin.ts index c083faa100a3..d878e98c8226 100644 --- a/packages/server/src/handlers/plugin.ts +++ b/packages/server/src/handlers/plugin.ts @@ -10,7 +10,9 @@ export const PluginHandler = HttpApiBuilder.group(Api, "server.plugin", (handler handlers .handle("plugin.list", () => Effect.gen(function* () { - return yield* response(Plugin.Service.use((plugin) => plugin.list())) + const plugins = yield* Plugin.Service + const ready = yield* plugins.ready + return { ...(yield* response(plugins.list())), ready } }), ) .handle("plugin.gui", (ctx) => diff --git a/packages/server/test/plugin-ready.test.ts b/packages/server/test/plugin-ready.test.ts new file mode 100644 index 000000000000..9a49e662fb6a --- /dev/null +++ b/packages/server/test/plugin-ready.test.ts @@ -0,0 +1,68 @@ +import { expect } from "bun:test" +import { Bus } from "@opencode/core/bus" +import { SdkPlugins } from "@opencode/core/plugin/sdk" +import { Plugin as PluginSchema } from "@opencode/schema/plugin" +import { Plugin } from "@opencode/plugin/effect" +import { Context, Deferred, Effect, Fiber, Layer, Stream } from "effect" +import { HttpEffect, HttpRouter, HttpServer } from "effect/http" +import { tmpdirScoped } from "../../core/test/fixture/tmpdir" +import { it } from "../../core/test/lib/effect" +import { createRoutes } from "../src/routes" + +it.live( + "plugin.list reports readiness without waiting and plugin.updated announces when it flips", + () => + Effect.gen(function* () { + const tmp = yield* tmpdirScoped("opencode-plugin-ready-") + const started = yield* Deferred.make() + const release = yield* Deferred.make() + const context = yield* Layer.build( + createRoutes({ + password: "secret", + database: { path: ":memory:" }, + models: { fetch: false }, + fs: { filewatcher: false }, + config: { directory: tmp.path, project: false }, + }).pipe(Layer.provide(HttpServer.layerServices)), + ) + yield* Context.get(context, SdkPlugins.Service).register( + Plugin.define({ + id: "slow-plugin", + effect: () => + Effect.gen(function* () { + yield* Deferred.succeed(started, undefined) + yield* Deferred.await(release) + }), + }), + ) + const handler = Context.get(context, HttpRouter.HttpRouter) + .asHttpEffect() + .pipe(HttpEffect.toWebHandlerWith(context)) + const ready = Effect.promise(async () => { + const url = new URL("/api/plugin", "http://opencode.local") + url.searchParams.set("location[directory]", tmp.path) + const response = await handler( + new Request(url, { headers: { authorization: `Basic ${btoa("opencode:secret")}` } }), + ) + return ((await response.json()) as { ready: boolean }).ready + }) + + expect(yield* ready.pipe(Effect.timeout("2 seconds"))).toBe(false) + yield* Deferred.await(started) + expect(yield* ready.pipe(Effect.timeout("2 seconds"))).toBe(false) + + // A client re-reads plugin.list on each plugin.updated, so the last one must come after it turns true. + const settled = yield* Context.get(context, Bus.Service) + .subscribe([PluginSchema.Event.Updated]) + .pipe( + Stream.mapEffect(() => ready), + Stream.takeUntil((value) => value), + Stream.runDrain, + Effect.forkScoped({ startImmediately: true }), + ) + yield* Deferred.succeed(release, undefined) + yield* Fiber.join(settled).pipe(Effect.timeout("5 seconds")) + expect(yield* ready).toBe(true) + }), + 15_000, +) diff --git a/services/site/cloudflare.config.ts b/services/site/cloudflare.config.ts index 515ce62e952b..7b68643f0b0f 100644 --- a/services/site/cloudflare.config.ts +++ b/services/site/cloudflare.config.ts @@ -45,7 +45,7 @@ export default defineConfig(({ mode }) => { : [], // The bare host is a custom domain (it also supplies the DNS record), and cf detaches any domain // not listed here. docs. is the old docs host: the Worker sends its URLs to the same path - // on the V1 docs at /v1/docs. + // under /docs. domains: [host, `docs.${host}`], workersDev: false, previewUrls: false, diff --git a/services/site/src/docs/AGENTS.md b/services/site/src/docs/AGENTS.md index 1ac29d3801ed..2f4c96d68615 100644 --- a/services/site/src/docs/AGENTS.md +++ b/services/site/src/docs/AGENTS.md @@ -27,7 +27,7 @@ - `script/build.ts` runs after the site's `vite build` (from `script/build.ts` at the site root): it builds the docs' stylesheet and script, prerenders every page, every content page's Markdown twin (`/docs/config.md`; the intro's is `/docs/index.md`), `llms.txt` and the sitemap into `.output/public`, builds the Pagefind index, and checks every link, that nothing loads from outside `/docs` and that no page takes a reserved file name. `llms.txt` and the sitemap list opencode.ai's URLs on dev.opencode.ai too, so dev never advertises its own. - Pull requests that touch the site run the same build on its own (`.github/workflows/check-docs.yml`): `bun run generate`, then `bun src/docs/script/build.ts --out `. - The Worker never renders docs. `middleware.ts` serves the prerendered files: docs URLs reach it first (`runWorkerFirst` in `cloudflare.config.ts`), so a page's URL answers `Accept: text/markdown` with its twin. `vite dev` has no prerendered files, so the development server renders on request. -- `/docs` still receives the V1 docs' old links, which had more pages and other languages (`/docs/ja/...`). `middleware.ts` redirects any `/docs` path that isn't a page here (`virtual:docs-pages`, the same list the build prerenders) to the same path on the V1 docs with a 302, so adding a page here takes its URL over. `route.ts` also answers the V1 app's other addresses (its language aliases, share links and schemas) and, ahead of docs.opencode.ai moving to this Worker, sends that host's paths to `/v1/docs`. The old `/v2` URLs redirect to `/docs` permanently (`src/lib/legacy.ts`). +- `/docs` still receives the V1 docs' old links, which had more pages and other languages (`/docs/ja/...`). `middleware.ts` redirects any `/docs` path that isn't a page here (`virtual:docs-pages`, the same list the build prerenders) to the same path on the V1 docs with a 302, so adding a page here takes its URL over. `route.ts` also answers the V1 app's other addresses (its language aliases, share links and schemas) and, ahead of docs.opencode.ai moving to this Worker, sends that host's paths to the same path under `/docs`. The old `/v2` URLs redirect to `/docs` permanently (`src/lib/legacy.ts`). ## Writing Style diff --git a/services/site/src/docs/route.ts b/services/site/src/docs/route.ts index 7f7cb1fa5cac..3900aac36741 100644 --- a/services/site/src/docs/route.ts +++ b/services/site/src/docs/route.ts @@ -34,19 +34,30 @@ export function docsRoute(url: URL, accept: string, slugs: Record/go (about 50k requests a day). diff --git a/services/site/test/docs-route.test.ts b/services/site/test/docs-route.test.ts index 560bea2c8825..d731fe3ff37b 100644 --- a/services/site/test/docs-route.test.ts +++ b/services/site/test/docs-route.test.ts @@ -67,7 +67,7 @@ describe("docs routes", () => { }) test("send paths without a V2 page to the same path on the V1 docs, temporarily", () => { - for (const path of ["/docs/ja/", "/docs/ja/agents/", "/docs/zh-cn/config", "/docs/ide/", "/docs/sdk.md"]) + for (const path of ["/docs/ja/", "/docs/ja/agents/", "/docs/zh-cn/config", "/docs/ide/", "/docs/lsp.md"]) expect(route(path)).toEqual({ type: "redirect", location: `/v1/docs${path.slice("/docs".length)}`, status: 302 }) expect(route("/docs/ide/?ref=app")).toEqual({ type: "redirect", location: "/v1/docs/ide/?ref=app", status: 302 }) }) @@ -80,6 +80,9 @@ describe("docs routes", () => { }) expect(route("/docs/go")).toEqual({ type: "redirect", location: "/docs/console/go/", status: 301 }) expect(route("/docs/zen/")).toEqual({ type: "redirect", location: "/docs/console/models/", status: 301 }) + expect(route("/docs/rules/")).toEqual({ type: "redirect", location: "/docs/instructions/", status: 301 }) + expect(route("/docs/sdk")).toEqual({ type: "redirect", location: "/docs/build/sdk/", status: 301 }) + expect(route("/docs/server/")).toEqual({ type: "redirect", location: "/docs/api", status: 301 }) }) test("leave the docs' own files to the assets, never to the V1 docs", () => { @@ -131,13 +134,13 @@ describe("V1 docs routes", () => { expect(route(path)).toEqual({ type: "missing" }) }) - test("send docs.opencode.ai's addresses to the same path on the V1 docs", () => { + test("send docs.opencode.ai's addresses to the same path under /docs", () => { const host = (url: string) => docsHostRedirect(new URL(url)) - expect(host("https://docs.opencode.ai/")).toBe("https://opencode.ai/v1/docs/") - expect(host("https://docs.opencode.ai/docs")).toBe("https://opencode.ai/v1/docs/") - expect(host("https://docs.opencode.ai/docs/ide/?x=1")).toBe("https://opencode.ai/v1/docs/ide/?x=1") - expect(host("https://docs.opencode.ai/s/abc")).toBe("https://opencode.ai/v1/docs/s/abc") - expect(host("https://docs.dev.opencode.ai/docs/ja/")).toBe("https://dev.opencode.ai/v1/docs/ja/") + expect(host("https://docs.opencode.ai/")).toBe("https://opencode.ai/docs/") + expect(host("https://docs.opencode.ai/docs")).toBe("https://opencode.ai/docs/") + expect(host("https://docs.opencode.ai/docs/ide/?x=1")).toBe("https://opencode.ai/docs/ide/?x=1") + expect(host("https://docs.opencode.ai/s/abc")).toBe("https://opencode.ai/docs/s/abc") + expect(host("https://docs.dev.opencode.ai/docs/ja/")).toBe("https://dev.opencode.ai/docs/ja/") expect(host("https://opencode.ai/docs/")).toBeUndefined() }) }) diff --git a/services/site/test/legacy.test.ts b/services/site/test/legacy.test.ts index 78e6287872c0..6a48905ea0c4 100644 --- a/services/site/test/legacy.test.ts +++ b/services/site/test/legacy.test.ts @@ -31,9 +31,8 @@ describe("legacy redirects", () => { test("moved pages match exact paths only, keep the query and leave the inference API alone", () => { const zen = get("/zen/", { search: "?ref=app" }) - expect(zen?.status).toBe(302) + expect(zen?.status).toBe(301) expect(zen?.headers.get("Location")).toBe("/console/models?ref=app") - expect(zen?.headers.get("Cache-Control")).toBe("no-store") expect(get("/zen/go")?.headers.get("Location")).toBe("/console/go") expect(get("/teams")?.headers.get("Location")).toBe("/console/teams") for (const path of ["/zen/v1/models", "/zen/go/v1/chat/completions", "/go/v1", "/models.json", "/gopher"])