Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agents): bundle transcript fold worker explicitly (#7849)

Wolf Mermelstein4d591e9bcce5
diff --git a/apps/web/scripts/agent-fold-packaging.test.ts b/apps/web/scripts/agent-fold-packaging.test.ts
new file mode 100644
index 0000000000..4f467ef0ea
--- /dev/null
+++ b/apps/web/scripts/agent-fold-packaging.test.ts
@@ -0,0 +1,56 @@
+import { fileURLToPath } from 'node:url';
+import { build, type Rollup } from 'vite';
+import { expect, it } from 'vitest';
+import { createAppViteConfig } from '../vite.base';
+
+it('bundles the agent fold client URL as a compiled worker in production', async () => {
+ const root = fileURLToPath(new URL('..', import.meta.url));
+ const config = await createAppViteConfig()({
+ command: 'build',
+ mode: 'production',
+ });
+ const workerPlugins = config.worker?.plugins;
+ const result = (await build({
+ ...config,
+ configFile: false,
+ root,
+ publicDir: false,
+ logLevel: 'silent',
+ worker: {
+ ...config.worker,
+ plugins: () => [
+ ...(workerPlugins?.() ?? []),
+ {
+ // Exercise the real worker entry and its imports without requiring
+ // generated Rust artifacts for this packaging regression test.
+ name: 'test-agent-fold-wasm-loader',
+ load(id) {
+ if (id.endsWith('/agent-fold/wasm-module.ts')) {
+ return 'export async function loadAgentFoldWasm() { throw new Error("test loader"); }';
+ }
+ },
+ },
+ ],
+ },
+ build: {
+ ...config.build,
+ write: false,
+ rollupOptions: {
+ input: `${root}/src/lib/core/agent-fold/client.ts`,
+ preserveEntrySignatures: 'strict',
+ },
+ },
+ })) as Rollup.RollupOutput;
+
+ const worker = result.output.find((file) =>
+ /^fold\.worker-[\w-]+\.js$/.test(file.fileName)
+ );
+ expect(worker).toBeDefined();
+ const client = result.output.find(
+ (file) => file.type === 'chunk' && file.isEntry
+ );
+ expect(client?.type).toBe('chunk');
+ if (client?.type !== 'chunk') throw new Error('missing client entry');
+ expect(client.code).toContain(worker!.fileName);
+ expect(client.code).not.toContain('data:video/mp2t');
+}, 30_000);
diff --git a/apps/web/src/lib/core/agent-fold/client.test.ts b/apps/web/src/lib/core/agent-fold/client.test.ts
index 175243ebdb..bdce69ecc5 100644
--- a/apps/web/src/lib/core/agent-fold/client.test.ts
+++ b/apps/web/src/lib/core/agent-fold/client.test.ts
@@ -93,12 +93,7 @@ describe('agent fold preloading', () => {
await expect(pushed).resolves.toEqual([]);
});
- // The three ways a worker can fail are distinguishable, and the browser
- // tells them apart only by the error event's message. Measured in Chrome: a
- // script that 404s reports no message at all; one that loads and throws
- // reports what it threw; a failure inside the worker never reaches `error`
- // and comes back as an ordinary reply.
- it('reports a script that never loaded as the build being gone', async () => {
+ it('reports an unavailable script with its URL without assuming a stale build', async () => {
const { AgentFoldWorkerUnavailable, pushSession } = await import(
'./client'
);
@@ -108,8 +103,7 @@ describe('agent fold preloading', () => {
const error = await pushed.catch((reason: unknown) => reason);
expect(error).toBeInstanceOf(AgentFoldWorkerUnavailable);
- expect((error as Error).message).toContain('no longer served');
- // The chunk names the build, which is the whole diagnosis.
+ expect((error as Error).message).not.toContain('no longer served');
expect((error as Error).message).toContain('fold.worker');
});
diff --git a/apps/web/src/lib/core/agent-fold/client.ts b/apps/web/src/lib/core/agent-fold/client.ts
index c616bd6345..0b734211e4 100644
--- a/apps/web/src/lib/core/agent-fold/client.ts
+++ b/apps/web/src/lib/core/agent-fold/client.ts
@@ -11,6 +11,7 @@ import type {
FoldedStreamEvent,
SessionMetadata,
} from '@service-agent-fold/generated/types';
+import workerUrl from './fold.worker.ts?worker&url';
import type { FoldInput, FoldRequest, FoldResponse } from './protocol';
export type { FoldInput } from './protocol';
@@ -29,24 +30,12 @@ interface Pending {
/**
* The worker's script never ran.
*
- * A dedicated worker reports a script it could not load as an `error` event
- * carrying no message at all; one that loaded and then threw reports what it
- * threw, and anything failing *inside* the worker comes back as an ordinary
- * `{ok: false}` reply. So an empty message is a precise signal, and it has
- * one overwhelmingly likely cause: this tab is running a build whose assets
- * are no longer served. Deploys replace the content-hashed bundle and the
- * worker chunk is fetched lazily, on the first fold, which is often after
- * the build that owns it has been superseded.
- *
- * Named rather than folded into a generic failure so it groups on its own in
- * error tracking: the fix for it is a deploy/retention question, not a bug
- * in the fold.
+ * Browsers can omit the error message for missing scripts, blocked scripts,
+ * or invalid packaging. Keep the URL for diagnosis without assuming a cause.
*/
export class AgentFoldWorkerUnavailable extends Error {
constructor(readonly workerUrl: string) {
- super(
- `agent fold worker script could not be loaded (${workerUrl}) - this tab is running a build that is no longer served; reloading will pick up the current one`
- );
+ super(`agent fold worker script could not be loaded (${workerUrl})`);
this.name = 'AgentFoldWorkerUnavailable';
}
}
@@ -74,10 +63,10 @@ let nextId = 0;
function ensureWorker(): Worker {
if (worker) return worker;
- // Held so a failure can name the file that did not load: its content hash
- // identifies the build this tab is running, which is the whole diagnosis.
- const url = new URL('./fold.worker.ts', import.meta.url);
- const started = new Worker(url, { type: 'module' });
+ // Explicitly bundle the worker while retaining its URL for diagnostics.
+ // A standalone new URL('./fold.worker.ts', import.meta.url) makes Vite
+ // emit raw TypeScript as an asset instead of compiling a worker entry.
+ const started = new Worker(workerUrl, { type: 'module' });
started.addEventListener('message', (event: MessageEvent<FoldResponse>) => {
const response = event.data;
@@ -95,7 +84,7 @@ function ensureWorker(): Worker {
// The worker itself failed, so nothing in flight will ever be answered.
// One error event takes down every fold in flight, which is why a single
// failure reports as many: the count is concurrent folds, not causes.
- const error = workerStartupFailure(url.href, event.message ?? '');
+ const error = workerStartupFailure(workerUrl, event.message ?? '');
for (const waiting of pending.values()) waiting.reject(error);
pending.clear();
// Dropped so the next call starts a fresh one rather than waiting on a
diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.md
index 4c9fdbcd23..8674af7d73 100644
--- a/docs/AGENT_GUIDE/ai-chat.md
+++ b/docs/AGENT_GUIDE/ai-chat.md
@@ -56,6 +56,12 @@ must not send a prompt. Record navigation and focus time separately from typing
and Enter-to-first-answer time. Check the first readable agent words in the DOM,
not a loading indicator or a bare Markdown delimiter.
+Verify transcript startup against a production build as well as the dev server.
+The first fold should load a compiled `fold.worker-*.js` script and its WASM
+assets; importing the client alone should not construct a worker. A
+`data:video/mp2t` worker URL indicates raw TypeScript was packaged as an asset.
+An empty worker startup error does not by itself establish that the tab is stale.
+
Repeat from Home, Agents, and the create menu with an already-ready session.
The first prompt can be accepted before its destination mounts; navigating into
the session must keep the same streamed turn without restarting its load. Also