From 319b0a64591ec3c07504abfc5e369cf97a6f2723 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 17:07:35 +0800 Subject: [PATCH] layerfs 0.10.2: the page shows its empty state before the first project, not Connecting for good MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AlanWang1027713 on X (magpie v0.1.4026, macOS): Plugins -> LayerFS 轨迹 stays on "Connecting to the local trajectory service…". The page's boot.js jumps its frame to the host's service on 43717, and the service started only once a store with a project existed. With no store the jump is refused. The README said the frame then shows the browser's refused-to-connect page; that is Chromium. A WebKit frame (magpie's window on macOS and Linux) keeps the old document when its jump is refused, so it said Connecting for good. The service now starts when the host loads, store or no store. With no store (or an empty projects folder) it answers an empty recording without building the backend, so nothing is written under ~ until a tool call makes the first project. The empty state now reloads itself every 5s like the full page, so the first provision shows up without reopening the page. The retry on a held port no longer waits for a store, and the store-race recheck it needed is gone. Run on mote (Bun 1.4.2, temp HOME): - bun test packages/layerfs: 69 pass, 0 fail. bun scripts/check.mjs: 0. - Broken by hand: 0.10.1's index.mjs and app.js turn the two new tests red (the no-store service test and the empty-state reload). - Real browsers (Playwright 1.62.0): a sandboxed frame doing boot.js's jump against a no-store host. 0.10.1: WebKit's frame stays on "Connecting to the local trajectory service…", Chromium's shows chrome-error. 0.10.2: both show the zh empty state ("还没有记录到轨迹…"). Not tried: the page inside the real magpie window with the plugin installed. --- packages/layerfs/README.md | 30 +++++--- packages/layerfs/index.mjs | 94 +++++++++--------------- packages/layerfs/layerfs.test.mjs | 26 ++++--- packages/layerfs/package.json | 2 +- packages/layerfs/page-lifecycle.test.mjs | 2 +- packages/layerfs/page-service.mjs | 9 ++- packages/layerfs/page.test.mjs | 6 ++ packages/layerfs/page/app.js | 3 + 8 files changed, 88 insertions(+), 84 deletions(-) diff --git a/packages/layerfs/README.md b/packages/layerfs/README.md index 611c0c6..54ee294 100644 --- a/packages/layerfs/README.md +++ b/packages/layerfs/README.md @@ -178,8 +178,9 @@ serves its chat page: thing: it reads the port from the plugin's options (`pagePort`, default 43717) and `location.replace`s its frame to `http://127.0.0.1:/`. The install's folder is never - renamed, linked, or written into. A user with no store gets no - service and nothing written anywhere. + renamed, linked, or written into. The service starts when the + host loads, store or no store; a user with no store gets the + page's empty state and nothing written anywhere. - **The service returns the page with its data inside.** Each request rebuilds `backend.trajectory()` on demand and answers one route, GET /, as HTML: the trajectory UI with the recording @@ -187,18 +188,17 @@ serves its chat page: the page reads from the DOM — never by fetch. The page reloads itself every few seconds, keeping its view across each reload (the project, the scroll, and the replay position ride in - `location.hash`), so the picture stays live. With no store there - is no service at all, so the frame shows the browser's - refused-to-connect page until the first project is provisioned; - a store with no recorded checkpoints yet shows the page's honest - empty state. + `location.hash`), so the picture stays live. With no store, or a + store with no recorded checkpoints yet, the page shows its empty + state and keeps reloading, so the first provision appears on its + own. - **The service recovers after a host handover.** A retired plugin host may still hold the page port while finishing a request. The replacement retries a failed bind automatically, starting after one second and backing off to at most 30 seconds. A closed listener is retried too; concurrent tool calls share one startup attempt. - Retries begin only after a project store exists, write no extra - checkpoints, and do not keep an otherwise finished host alive. + Retries write nothing and do not keep an otherwise finished host + alive. An already refused frame may need a reload after the listener recovers; neither the gateway nor an agent task needs restarting. - **The service is closed to other sites.** It emits no CORS @@ -237,6 +237,18 @@ node tools/gen-trajectory.mjs # regenerate the page's bundled sample trajector Zero runtime dependencies; the arguments are JSON Schema, like the other tools packages. MIT. +## Changes in 0.10.2 + +The page no longer hangs on "Connecting to the local trajectory +service…" before the first project. Up to 0.10.1 the service started +only once a store existed, so the page's jump to it was refused; a +WebKit frame (magpie on macOS and Linux) keeps the old document when +its jump is refused, so the page said Connecting for good. The service +now starts when the host loads. With no store it serves the page's +empty state without building the backend, so nothing is written under +~ until a tool call makes the first project, and the empty page +reloads itself every 5 seconds until that project appears. + ## Changes in 0.10.1 The page service now survives Magpie replacing its plugin host. A diff --git a/packages/layerfs/index.mjs b/packages/layerfs/index.mjs index 27fada8..be38332 100644 --- a/packages/layerfs/index.mjs +++ b/packages/layerfs/index.mjs @@ -9,8 +9,8 @@ // plugin-private files, so the installed page/index.html only navigates // its frame to the service, which embeds backend.trajectory() — the // page's real commits, branches, storage and dedup accounting, with the -// real calling agent on each — in the HTML it returns. The service -// starts only once a store exists; see the README's page section. +// real calling agent on each — in the HTML it returns. With no store +// it serves the empty state; see the README's page section. // // Backend: a local content-addressed store with the LayerFS model, in // layerfs.mjs. The native backend (the real LayerFS SDK) is not shipped @@ -38,16 +38,14 @@ const callingAgent = (ctx) => { } // ---- the page's live channel: the host's own service --------------------- -// The service starts once per plugin instance, and only once a store -// exists — two read-only gates, so a user with no store gets no service -// and nothing written anywhere: a projects folder that holds entries -// (not merely exists; the backend's constructor mkdirs it), and a -// non-empty answer from the backend that will serve the calls. A host -// whose store gains its first project later starts the service after -// the call that created it (mutate, below). The start is off the tool -// calls' path and every failure is swallowed: a held port or a failed -// read leaves the tools untouched, and the page's redirect simply finds -// nothing listening. +// The service starts once per plugin instance, at load, store or no +// store: the installed page jumps its frame to it, and a WebKit frame +// whose jump is refused keeps showing "Connecting…" for good (0.10.1 and +// before started it only once a store existed). With no store it serves +// the page's empty state and builds no backend, so nothing is written +// under ~ until a tool call makes the first project. The start is off +// the tool calls' path and every failure is swallowed: a held port or a +// failed read leaves the tools untouched. export const LayerfsTools = async (ctx = {}, options = {}) => { let backendPromise @@ -64,17 +62,14 @@ export const LayerfsTools = async (ctx = {}, options = {}) => { } // A new host may start before the retired host releases this port. // Keep one start in flight, and remember a service only after it - // actually listens. Retry while a real store exists, without keeping - // an otherwise finished host alive or polling before the first project. + // actually listens. Retry with backoff, without keeping an otherwise + // finished host alive. let service = null let starting = null let retryTimer = null let retryDelay = 1000 - let storeVersion = 0 - let recheck = false - let hasStore = false const retry = () => { - if (!hasStore || retryTimer || service?.server.listening) return + if (retryTimer || service?.server.listening) return retryTimer = setTimeout(() => { retryTimer = null ensureService().catch(() => { /* the page never blocks a tool */ }) @@ -82,38 +77,32 @@ export const LayerfsTools = async (ctx = {}, options = {}) => { retryTimer.unref?.() retryDelay = Math.min(retryDelay * 2, 30000) } + // What the service serves: the store's trajectory once a store exists, + // else an empty recording, so the page shows its empty state (and + // reloads itself into the first project) instead of a frame that never + // lands. The store is looked for without building the backend: its + // constructor is what creates the store's folders, and a user with no + // store gets nothing written. + const empty = () => ({ projects: [], events: [], generatedAt: new Date().toISOString() }) + const pageBackend = { + trajectory: async () => { + try { + if (!readdirSync(join(storeHome(), "projects")).length) return empty() + } catch { + return empty() + } + return (await backend()).trajectory() + }, + } const ensureService = () => { if (service?.server.listening) return Promise.resolve() - if (starting) { - recheck = true - return starting - } + if (starting) return starting if (retryTimer) { clearTimeout(retryTimer) retryTimer = null } - const version = storeVersion - recheck = false starting = (async () => { - let hasEntries - try { - hasEntries = readdirSync(join(storeHome(), "projects")).length > 0 - } catch (error) { - hasStore = false - return - } - if (!hasEntries) { - hasStore = false - return - } - const b = await backend() - const data = await b.trajectory() - if (!data.projects.length) { - hasStore = false - return - } - hasStore = true - const candidate = startPageService({ backend: b, port: resolvePagePort(options) }) + const candidate = startPageService({ backend: pageBackend, port: resolvePagePort(options) }) const stopped = () => { if (service !== candidate || candidate.server.listening) return service = null @@ -131,26 +120,13 @@ export const LayerfsTools = async (ctx = {}, options = {}) => { retry() }).finally(() => { starting = null - if ((recheck || storeVersion !== version) && - !service?.server.listening && !retryTimer) { - recheck = false - queueMicrotask(() => ensureService().catch(() => { /* the tools stay usable */ })) - } }) return starting } ensureService().catch(() => { /* no service: the tools are untouched */ }) - // A mutating call may have created the store's first project: offer - // the service its start, off the call's path. - const mutate = async (fn) => { - const b = await backend() - const out = await fn(b) - // The first mutation may race the factory's empty-store check. Let the - // in-flight check finish, then run one more against the new store. - storeVersion++ - ensureService().catch(() => { /* never on the tool call's path */ }) - return out - } + // A mutating call: the service, up since load, reads the store it + // writes on the page's next reload. + const mutate = call return { tool: { diff --git a/packages/layerfs/layerfs.test.mjs b/packages/layerfs/layerfs.test.mjs index 440bffc..a4c8b0d 100644 --- a/packages/layerfs/layerfs.test.mjs +++ b/packages/layerfs/layerfs.test.mjs @@ -789,28 +789,31 @@ test("0.10.0: the served trajectory carries per-agent tool-call events", async ( expect(p.path).toContain("website") }, 30000) -test("0.10.0: the service starts only once a store exists — empty or foreign folders start nothing", async () => { +test("0.10.2: the service answers with no store — the empty state, nothing written — so the page never hangs on Connecting", async () => { const home = mkTmp("layerfs-home-") + rmSync(home, { recursive: true, force: true }) process.env.MAGPIE_LAYERFS_HOME = home process.env.MAGPIE_LAYERFS_BACKEND = "sim" delete process.env.MAGPIE_LAYERFS_INSTALL_DIR delete process.env.MAGPIE_LAYERFS_DATA_DIR delete process.env.MAGPIE_LAYERFS_PAGE_DIR - // (a) a projects folder holding nothing: no service, and the backend - // is never built — its constructor is what creates the rest of the store - mkdirSync(join(home, "projects")) + // (a) no store at all: the service still serves the page, with no + // projects, and builds no backend — nothing appears under the home const portA = await freePort() await LayerfsTools({ directory: mkTmp("magpie-config-") }, { pagePort: portA }) - await new Promise((r) => setTimeout(r, 300)) - await expect(fetch(`http://127.0.0.1:${portA}/`)).rejects.toThrow() + const a = await serviceData(`http://127.0.0.1:${portA}/`, (x) => Array.isArray(x.projects)) + expect(a.data.projects).toEqual([]) + expect(a.html).toContain('id="empty"') + expect(existsSync(home)).toBe(false) + // (b) a projects folder holding nothing, or an entry that is no + // project: the same empty answer, and still no store built + mkdirSync(join(home, "projects"), { recursive: true }) + expect((await serviceData(`http://127.0.0.1:${portA}/`, (x) => Array.isArray(x.projects))).data.projects).toEqual([]) expect(existsSync(join(home, "store"))).toBe(false) - // (b) an entry that is no project: the backend's own answer decides, - // so nothing listens writeFileSync(join(home, "projects", "not-a-project.txt"), "x") const portB = await freePort() await LayerfsTools({ directory: mkTmp("magpie-config-") }, { pagePort: portB }) - await new Promise((r) => setTimeout(r, 300)) - await expect(fetch(`http://127.0.0.1:${portB}/`)).rejects.toThrow() + expect((await serviceData(`http://127.0.0.1:${portB}/`, (x) => Array.isArray(x.projects))).data.projects).toEqual([]) // (c) a real project: one instance provisions it, and a fresh instance // on the same home serves it from the factory call, no tool call needed const portD = await freePort() @@ -820,4 +823,7 @@ test("0.10.0: the service starts only once a store exists — empty or foreign f await LayerfsTools({ directory: mkTmp("magpie-config-") }, { pagePort: portE }) const { data } = await serviceData(`http://127.0.0.1:${portE}/`, (x) => !!x.projects.find((p) => p.name === "website")) expect(data.projects.at(-1).name).toBe("website") + // and the service that started with no store serves the new project + // on its next answer: the page's reload lands on it + await serviceData(`http://127.0.0.1:${portA}/`, (x) => !!x.projects.find((p) => p.name === "website")) }, 30000) diff --git a/packages/layerfs/package.json b/packages/layerfs/package.json index f22b677..5819f9c 100644 --- a/packages/layerfs/package.json +++ b/packages/layerfs/package.json @@ -49,5 +49,5 @@ "directory": "packages/layerfs" }, "type": "module", - "version": "0.10.1" + "version": "0.10.2" } diff --git a/packages/layerfs/page-lifecycle.test.mjs b/packages/layerfs/page-lifecycle.test.mjs index 1a2e975..8d9b303 100644 --- a/packages/layerfs/page-lifecycle.test.mjs +++ b/packages/layerfs/page-lifecycle.test.mjs @@ -147,7 +147,7 @@ test("concurrent reused provisions wait for the trajectory check", async () => { } }, 15_000) -test("an absent project store writes nothing and starts no polling server", async () => { +test("an absent project store writes nothing", async () => { const f = await fixture() try { await delay(1400) diff --git a/packages/layerfs/page-service.mjs b/packages/layerfs/page-service.mjs index 4e9644e..3d22aec 100644 --- a/packages/layerfs/page-service.mjs +++ b/packages/layerfs/page-service.mjs @@ -19,10 +19,11 @@ // (pageHost / plugin.Warm), so the service is there by the time the // frame lands. // -// The service starts only once a store exists (index.mjs's gates: a -// projects folder holding entries, and a backend answering real -// projects). A user with no store gets no service, no files, nothing -// written under ~. +// The service starts when the host loads, store or no store (a WebKit +// frame whose jump is refused keeps saying "Connecting…" for good). +// With no store, index.mjs answers an empty recording without building +// the backend: the page shows its empty state, and nothing is written +// under ~. // // Security — the trajectory names workspace paths and file contents, // and any website can point a sandboxed iframe at 127.0.0.1, so: diff --git a/packages/layerfs/page.test.mjs b/packages/layerfs/page.test.mjs index 2c687f5..82ffb0e 100644 --- a/packages/layerfs/page.test.mjs +++ b/packages/layerfs/page.test.mjs @@ -406,3 +406,9 @@ test("the service no longer appends its own blind reload timer", () => { const svc = readFileSync(join(dir, "page-service.mjs"), "utf8") expect(svc).not.toContain("setTimeout(() => location.reload(), 5000)") }) + +test("0.10.2: the empty state reloads itself, so the first project appears without reopening the page", () => { + const app = readFileSync(join(dir, "page", "app.js"), "utf8") + const empty = app.slice(app.indexOf("if (!projects.length) {"), app.indexOf("$(\"stamp\")")) + expect(empty).toContain("location.reload()") +}) diff --git a/packages/layerfs/page/app.js b/packages/layerfs/page/app.js index f9732a2..1859ed4 100644 --- a/packages/layerfs/page/app.js +++ b/packages/layerfs/page/app.js @@ -60,6 +60,9 @@ if (!projects.length) { $("stage").hidden = true; $("controls").hidden = true; $("stats").hidden = true; $("agents").hidden = true; $("empty").hidden = false; $("empty").textContent = t.noData; + // the service's empty answer reloads too, so the first provision + // shows up without the reader reopening the page + if (embedded) setTimeout(() => location.reload(), 5000); return; } $("stamp").textContent = data.sample ? t.sample : `${t.recorded} ${String(data.generatedAt || "").replace("T", " ").slice(0, 19)} · ${data.backend || ""}`;