Skip to content

Commit 6bee4fa

Browse files
authored
fix(local-runtime) + feat(webui): migration lease recovery at startup + M3-B10 model/permission facade (#155)
* docs(webui): the engine layer has six files, not five (webui-parity 107) ARCHITECTURE.md:492 counted five files under server/engine/ while the directory has shipped six since #143. The missing one is providers/local-runtime-v2.capabilities.js, the declaration-only module whose sole import is ../capabilities.js — the split that keeps the v2 host's ~4.7 s TypeScript dependency tree off the boot path. The local-runtime-v2.js row credited itself with the declaration it only re-exports, so that credit moves to the file that actually defines it. The zh-CN mirror takes the same edit in the same commit (equal weight); check-docs-alignment.mjs resolves the new bare-path citation against the merged tree. * test(webui): M2 capability-declaration snapshot vs the real host (engine-abstraction M2) Baseline: feat/engine-capabilities (M1, PR #143), NOT main — the server/lib/engine -> server/engine path fix has not landed yet. - test/lib/engine/capability-snapshot.test.js boots ONE real catalogue host on an isolated tmp data dir (MINIMAX_DATA_DIR + every MCODE_WEBUI_* path pinned before the provider import) and audits every full/partial key of both providers against the reflected surfaces: full requires every tracked method (REQUIRED_METHODS, derived from the live prototype chains — 91 adapter / 94 CliService methods — not copied from the design matrix); partial requires the present half to exist, method-named missing items to be genuinely absent (under-declaration goes red), and kebab-case sub-capability names to have no covering method; none is not method-checked. - Mutation tests pin the checker itself: flipped level / deleted method / grown sub-capability each go red (also verified by hand: three file mutations red at exit 1, restored byte-identical). - Registry-driven static guard: every registered provider declares exactly ENGINE_CAPABILITY_KEYS — typo keys cannot pass silently, and M4 providers are swept without editing the test. - Docs: ARCHITECTURE.md/.zh-CN.md M2 section, webui.md/.zh-CN.md migration-state entry; tmp prefix registered in the leak gate. * test(webui): point the capability snapshot at the engine layer's real path The M1 path move took server/lib/engine to server/engine. This file was written against the old one and rebase carried the code forward without carrying the import, so the suite failed on MODULE_NOT_FOUND and said nothing about the capabilities it was meant to check. * fix(webui): stop the shell from carrying one session's state into another Three leaks, all from state that lived outside the component that should have owned it. page.tsx read localStorage during render, so the pre-rendered HTML and the first client frame could not agree — a skeleton screen was hiding it, which is exactly the kind of cover that disappears the moment someone edits the shell. The first frame now uses defaults and one mount effect restores; the three write-back mirrors are gated so a default never overwrites a stored value. Scroll position is read by the same sessionKey effect Chat already had, which reads the same key. The draft store was a module-level bucket, so a draft, a failure banner and a model chip all followed you across sessions — type into one conversation, switch, and your words are in the other. The store is keyed by session now. Isolation is not discarding: switching back finds the draft still there. Clearing was the alternative and it destroys an unread banner every time you return to a conversation. #126 left the accepted-but-unconfirmed banner without anything to consume it when a turn ended. unconfirmedPatchOnTurnEnd clears it on the falling edge of running, and only there — the three-value decision about when to show it is untouched. * refactor(webui): the plugins and turn-diff routes take the host from the engine facade Migration step M3, batch B0 (engine-abstraction). 13 endpoints across routes/plugins.js and routes/turn-diff.js reached the catalogue host by importing lib/acp-client.js#getCatalogueHost directly. They now call getEngineCatalogueHost() from the facade. - engine/host.js: the lazy bridge. Its only import is a dynamic `await import("../lib/acp-client.js")` inside the function body, so engine/index.js gains a function and not a module load. That boundary is the whole point: app.js reaches engine/index.js through routes/engine-capabilities.js, and a static import of acp-client there would put the ACP client tree on every server start — the regression M1 paid for once (209ms -> 2700ms; facade load 4685ms -> 5ms once declaration and construction were split). The value is forwarded verbatim, `null` included, so "host did not boot" stays RUNTIME_UNAVAILABLE and never a second host. - engine/index.js re-exports the getter; the two routes import it from there and no longer name acp-client.js. - No endpoint behaviour changes: same wire shapes, statuses, codes, same `deps.getCliService` / `deps.getDiffApplication` seams, same one process-wide host. Measured on the module graph: routes/plugins.js drops from 13 product files + @mavis/shared to 8 files and zero bare packages; engine/index.js's whole closure is 6 files and 0 bare specifiers. Server start and the facade's own load are unchanged (facade ~1.2ms -> ~3ms, i.e. one more 45-line zero-import file; boot stays in the same 200-300ms band) because lib/state-bus.js already pulls acp-client into app.js's boot graph — closing that edge belongs to the catalogue read/write batches (M3-B1+), not here. Tests: test/lib/engine/host-facade.test.js pins the contract against the real module graph rather than against source text — a resolve hook (module.registerHooks) in a fresh process reports, per parent, which specifiers each entry resolved. It asserts neither route has a direct edge to acp-client/runtime-host/acp.mjs, that loading engine/index.js pulls no host module and no @mavis/* or @minimax/* package, that engine/host.js is in that closure, and the source-shape tripwires (dynamic import only, facade re-export). Mutation-checked: making the facade import statically turns 4 tests red, making plugins.js import directly turns 4 more red. The existing plugins/turn-diff suites pass unchanged under both transports (158 tests x acp and x runtime). Docs: ARCHITECTURE.md + .zh-CN.md — the engine/ file table gains engine/host.js on top of the six files #143 + the doc batch settled, the "one host" rule now names the facade, and the boot-path discipline is stated where the file list lives. docs/webui.md + .zh-CN.md are untouched: no user-visible change. Source inventory regenerated for the two new files (rebase conflict in it was resolved by taking the upstream copy and regenerating, never by hand). * test(webui): make the run-mirror, first-turn-guard and mavis-usage suites immune to the gate's isolation env The webui gate runs with MCODE_WEBUI_DATA_DIR, MCODE_WEBUI_SETTINGS_PATH and MINIMAX_DATA_DIR exported at a scratch directory. Two suites read paths those exports take away from them: - config.js#resolveDataDir reads MINIMAX_DATA_DIR ?? MAVIS_DATA_DIR, so the gate's MINIMAX_DATA_DIR outranked mavis-usage.check.mjs's own MAVIS_DATA_DIR fixture and every DB-backed case resolved null against a scratch dir that holds no runtime-state.sqlite. The suite now exports the name that wins. - config.js resolves SESSIONS_DB as MCODE_WEBUI_SESSIONS_DB || join(WEBUI_DATA_DIR, "sessions.json"). A caller that exports MCODE_WEBUI_SESSIONS_DB redirects the store, while the suite's beforeEach still cleared join(DATA_DIR, "sessions.json") — so each run read the previous run's records and the mid-run switch resolved an id whose workspace belonged to a since-removed tmp dir. Both chat-route suites now pin MCODE_WEBUI_SESSIONS_DB to the same path their cleanup clears. Test-only: no server/ code, no helper under test/helpers/_setup.js, and no assertion weakened or skipped. Verified with the three variables set, with MCODE_WEBUI_SESSIONS_DB additionally set, and bare. * refactor(webui): the plugins and turn-diff routes take the host from the engine facade Migration step M3, batch B0 (engine-abstraction). 13 endpoints across routes/plugins.js and routes/turn-diff.js reached the catalogue host by importing lib/acp-client.js#getCatalogueHost directly. They now call getEngineCatalogueHost() from the facade. - engine/host.js: the lazy bridge. Its only import is a dynamic `await import("../lib/acp-client.js")` inside the function body, so engine/index.js gains a function and not a module load. That boundary is the whole point: app.js reaches engine/index.js through routes/engine-capabilities.js, and a static import of acp-client there would put the ACP client tree on every server start — the regression M1 paid for once (209ms -> 2700ms; facade load 4685ms -> 5ms once declaration and construction were split). The value is forwarded verbatim, `null` included, so "host did not boot" stays RUNTIME_UNAVAILABLE and never a second host. - engine/index.js re-exports the getter; the two routes import it from there and no longer name acp-client.js. - No endpoint behaviour changes: same wire shapes, statuses, codes, same `deps.getCliService` / `deps.getDiffApplication` seams, same one process-wide host. Measured on the module graph: routes/plugins.js drops from 13 product files + @mavis/shared to 8 files and zero bare packages; engine/index.js's whole closure is 6 files and 0 bare specifiers. Server start and the facade's own load are unchanged (facade ~1.2ms -> ~3ms, i.e. one more 45-line zero-import file; boot stays in the same 200-300ms band) because lib/state-bus.js already pulls acp-client into app.js's boot graph — closing that edge belongs to the catalogue read/write batches (M3-B1+), not here. Tests: test/lib/engine/host-facade.test.js pins the contract against the real module graph rather than against source text — a resolve hook (module.registerHooks) in a fresh process reports, per parent, which specifiers each entry resolved. It asserts neither route has a direct edge to acp-client/runtime-host/acp.mjs, that loading engine/index.js pulls no host module and no @mavis/* or @minimax/* package, that engine/host.js is in that closure, and the source-shape tripwires (dynamic import only, facade re-export). Mutation-checked: making the facade import statically turns 4 tests red, making plugins.js import directly turns 4 more red. The existing plugins/turn-diff suites pass unchanged under both transports (158 tests x acp and x runtime). Docs: ARCHITECTURE.md + .zh-CN.md — the engine/ file table gains engine/host.js on top of the six files #143 + the doc batch settled, the "one host" rule now names the facade, and the boot-path discipline is stated where the file list lives. docs/webui.md + .zh-CN.md are untouched: no user-visible change. Source inventory regenerated for the two new files (rebase conflict in it was resolved by taking the upstream copy and regenerating, never by hand). * test(webui): make the run-mirror, first-turn-guard and mavis-usage suites immune to the gate's isolation env The webui gate runs with MCODE_WEBUI_DATA_DIR, MCODE_WEBUI_SETTINGS_PATH and MINIMAX_DATA_DIR exported at a scratch directory. Two suites read paths those exports take away from them: - config.js#resolveDataDir reads MINIMAX_DATA_DIR ?? MAVIS_DATA_DIR, so the gate's MINIMAX_DATA_DIR outranked mavis-usage.check.mjs's own MAVIS_DATA_DIR fixture and every DB-backed case resolved null against a scratch dir that holds no runtime-state.sqlite. The suite now exports the name that wins. - config.js resolves SESSIONS_DB as MCODE_WEBUI_SESSIONS_DB || join(WEBUI_DATA_DIR, "sessions.json"). A caller that exports MCODE_WEBUI_SESSIONS_DB redirects the store, while the suite's beforeEach still cleared join(DATA_DIR, "sessions.json") — so each run read the previous run's records and the mid-run switch resolved an id whose workspace belonged to a since-removed tmp dir. Both chat-route suites now pin MCODE_WEBUI_SESSIONS_DB to the same path their cleanup clears. Test-only: no server/ code, no helper under test/helpers/_setup.js, and no assertion weakened or skipped. Verified with the three variables set, with MCODE_WEBUI_SESSIONS_DB additionally set, and bare. * feat(webui): the five read endpoints ask the engine facade, not the transport (M3-B1) The directory-read family — #9 acp-sessions, #10 acp-session-title, #72 protocol/list-sessions, #74 state, #75 health — reached the engine through whatever MCODE_WEBUI_TRANSPORT happened to be, so "does the engine support this" had no answer anywhere except the absence of a crash. server/engine/ session-reads.js gives it one: each endpoint declares the capability and the provider method it needs, the facade checks the registered provider's declaration first, and a provider that does not offer the read answers 501 through invokeHandler instead of an empty list. Nothing on the wire moves. The facade forwards to the same acp-client exports the routes already called, so the 30s cache, the cwd normalisation, the 30s-stale sidebar push semantics and the catalogue-sessions projection are the same code; handleHealth becomes async because the version now resolves through the facade, which is why app-hono's legacy-parity helper learned to await it. /api/state's snapshot field list is untouched — snapshotViewFields and mcodeSessionsSnapshotFields are the first-frame render contract and this batch adds and removes nothing. Each read also reports where its bytes came from — catalogue, acp, or acp-fallback when the runtime transport asked for a host that never booted. That is metadata, not wire, and it is the difference between a sidebar that degraded and one that pretends. Two things this batch found rather than assumed: the catalogue host exposes no version accessor, so /api/health keeps answering from the ACP initialize mirror and says so rather than inventing a method; and protocol.js#72's old test drove a mock key nothing read, so "the cwd filter works" had never actually been proven. * feat(webui): the session-tree and export endpoints ask the engine facade (M3-B2) Routes #8 GET /api/session-tree and #11 GET /api/sessions/:id/export through the engine facade instead of the transport, keeping every response shape, status code and reason string unchanged. The two families are separate files because their gate policies are opposite. The tree is entirely engine data, so a provider that cannot list sessions genuinely has no tree: assertSessionTreeCapability throws and invokeHandler answers 501. Export's primary source is sessions.json and the engine only contributes a best-effort transcript enrichment the endpoint has always promised never to block on, so checkSessionExportCapability reports and never throws — gating it hard would delete working functionality in response to a declaration about a capability the endpoint does not depend on. The tree route re-throws the capability error, matched with the class's own instanceof helper rather than a `.name` compare: `name` is a writable instance property, so a stray `err.name = "…"` would silently turn that 501 back into the 200 soft-fail the gate exists to prevent. A test pins both halves — the real class propagates, an impostor carrying the right `.name` does not. Verified by exporting the real tree (303 rows, 32 projects, 299 nodes) before and after and diffing every node's id/title/parent/depth: 3289 field comparisons, zero differences. A synthetic fixture covers what the live data does not contain (orphans, cycles, four-level nesting, exotic titles): 165 comparisons, zero differences. Two pre-existing shapes are pinned because a "cleanup" would silently break them — child nodes carry no `children` key (all 66 of them), and the response has no parent_session_id key at all. * feat(webui): the usage endpoints ask the engine facade, and the derived figures get one home (M3-B3) * fix(webui): rebase M3-B3 onto M3-B2, register B2's two tmp prefixes, and record the whole-namespace mock trap * feat(webui): the account, model and capability reads ask the engine facade (M3-B4) #20 /api/account, #57 /api/models and #73 /api/protocol/capabilities now reach the engine through three new engine/ modules instead of naming lib/mcode-rpc.js, lib/models.js, lib/providers-config.js, lib/engine-catalogue.js and lib/acp-client.js themselves. Three modules because the three gate policies are all different: the account read gates HARD on authCredentials.getAccountStatus (the same provider method B3's #15/#16 read, so a provider that drops it takes both down together), the model catalogue gates SOFT (its primary sources are files webui owns, so a hard gate would delete a working picker), and #73 declares nothing at all because it IS the declaration endpoint. The model projection moved whole — three sources, the per-provider dedupe, both builtin-tree annotations and the three derived figures are now named pure functions pinned on their inputs, and #57 is verified by a full snapshot whose oracle was captured from the pre-refactor implementation. Its read stays synchronous so handleGetModels keeps its contract, which is also why engine/model-reads.js is not re-exported from engine/index.js: its four sources reach @mavis/shared and js-yaml, and the boot-path guard is right to refuse that under the shared facade. #73 is the one response body in the migration that changes: it gains an `engine` key carrying the engine-capabilities view, with `providerFor` saying whether the declaration came from the active transport's provider or from the default one standing in. Every pre-existing key keeps its name, position and value, and the ACP wire table is not replaced by the 14 matrix keys. * feat(webui): #73 swaps the ACP wire table for the 14-key engine-capabilities view (user-approved contract change) `GET /api/protocol/capabilities` used to answer from two hand-maintained places: `MCODE_ACP_CAPABILITIES`, a flat `{method: boolean}` table of the ACP JSON-RPC surface, and the `initialize` agentInfo mirror. The engine's DECLARED capability surface already existed — the 14-key per-provider object that `GET /api/engine-capabilities` serves — so webui was carrying two parallel answers to "what can this engine do", able to disagree, with no test able to notice. This makes `capabilities` the declared object and drops the wire table from the endpoint. This is a reviewed, user-authorised endpoint contract change, not a refactor side effect, and it is stated as such in the module header, in `docs/API.md` and in both ARCHITECTURE twins. The twelve old accessors are asserted GONE, so a consumer reading `capabilities.set_mode` gets undefined and fails loudly rather than receiving a truthy object field. No runtime consumer exists: nothing in `webapp/` reads this endpoint, and `engine/capability-reads.js` no longer imports `lib/mcode-rpc.js` at all (pinned by a static tripwire, because an unused import is behaviourally inert and no behavioural test could see it). The `engine` key the previous commit added is REMOVED rather than kept: with `capabilities` already the declaration, an `engine` block would carry the same 14 keys a second time in one response. What survives from that shape is the provenance — `capabilitiesProvider` / `capabilitiesProviderFor`, the honest bit that says whether the declaration came from the active transport's provider or from the default one standing in — plus `capabilitiesUnavailable` for the derived degradation roll-up. A test counts the declaration's occurrences in the serialised body and requires exactly one, so a second carrier is a red bar. `MCODE_ACP_CAPABILITIES` is kept and stays pinned by `test/lib/mcode-rpc.check.mjs`: it is still a true statement about the ENGINE's ACP surface and `docs/CAPABILITIES.md` cites it as one. It has no webui consumer left, recorded as debt in the module header rather than deleted as a side effect. `docs/webui.md` and `docs/webui.zh-CN.md` gain a diff here for the first time in this migration: they carried the old response shape in their endpoint tables, and an authorised contract change has to be documented where the contract is written. * fix(webui): stop two B4 comments describing behaviour the code no longer has Found by the dual-axis code review (Standards + Spec) before B4 merges. No logic changes; both were documentation lying about the code next to it. `engine/index.js` still described #73 as an additive change — "the response body gains a key (`engine`, the engine-capabilities view) … additive rather than a replacement". The second B4 commit made it a REPLACEMENT and deleted the `engine` key, so the facade's own export table was the one place still telling a reader the opposite of what the endpoint does. It now states the replacement, why the `engine` key was removed rather than kept, and what survived from it (the provenance keys and the derived roll-up). `docs/ARCHITECTURE.md` and its zh-CN twin called `engine/account-reads.js`'s read **synchronous**. It is `async` — `readEngineAccount` awaits a `Promise.all` of dynamic imports — and the boot-path note the row pointed at describes model-reads, not this module. The same two documents already listed account-reads correctly under the `await import()` rule a few paragraphs down, so the file contradicted itself in two languages at once. Both rows now say asynchronous and point at the ordinary rule. Also drops a dead `assertEngineCapability` import from `engine/model-reads.js`: the soft gate inspects the declaration inline, so the throwing helper was never called and its presence read as if the soft path could still throw. Replaced by a comment saying why it is absent, so the next reader does not "fix" it back in. And the one comment with Chinese embedded mid-sentence (仓库 review 要求注释用英文) is now English; the header parentheticals naming each family (账户读 / 模型目录读 / 能力声明读) stay, as do the quoted product strings — `本地用户` is the real zh-CN value of `userMenu.localUser` and `shell.tsx` cites it the same way. * feat(webui): move the session write family behind the engine facade M3 batch B5: #7 DELETE /api/sessions/:id, #4 POST /api/sessions/rename and #6 POST /api/sessions/cleanup-orphans stop driving the store, the caches and the engine's own local_runtime_* tables from the route. They ask engine/session-writes.js instead, so the load -> resolve -> authorize -> intent-audit -> mutate ordering — and the resurrection guard inside it — becomes named, testable code rather than a two-line helper a route could call out of order. #7 and #6 gate HARD on sessionCrud.deleteSession, because the rows they destroy are the engine's own; #4 declares no capability at all, because a rename writes webui's own store and touches no engine surface. The policy is decided by who owns the rows the write destroys, which is a different question from the read families' and does not have the same answer twice in a row here. The facade exposes a plan/commit pair rather than one deleteSession(), so the write-ahead audit still lands between "know what the user asked to delete" and "delete it". Response bodies are built in the facade once, which is what lets the #6 and #7 dryRun shapes be pinned byte-for-byte by unit tests. No status code, response body or error code changes. The 32-table delete SQL stays in lib/mcode-session-delete.js and is reached by dynamic import; acp-client.js and four test files bind to that specifier, so collecting it is a later batch's job. Recorded as KNOWN DEBT, along with rename writing a webui-side label only, and delete not detecting an in-flight session. * fix(webui): drop whitespace text nodes in markdown tables and dedupe thinking label * fix(webui): sweep the non-flipping inverted text token off primary surfaces The authorize-button fix (08599472) surfaced five more primary surfaces pairing text-text_default_inverted_static with bg_interaction_primary_default; dark mode inverts that background to pure white while the token stays near-white, so the label composites to white-on-white. Swap all of them to text-text_label_primary_default and add a source-scan guardrail that keeps the pairing out of primary surfaces while pinning the sanctioned status-badge exception (toolbar). * feat(webui): move session switch behind the engine facade M3-B6: #3 POST /api/sessions/switch now asks the engine facade instead of reaching into lib/acp-client.js, lib/transcript.js, lib/mavis-usage.js, lib/models.js and lib/config.js from the route. The new engine/session-switch.js owns the four load-bearing facts the ~290-line handler had accumulated: the mvs-sid-first resolution order (single base-session identity), the backfill decision and its read, the workspace containment gate (which runs before any cs mutation, so a refused switch leaves the client untouched), and the response body. The gate is SOFT — it reports and never throws — because the switch's primary data is webui's own record and both engine touches have a defined degradation. Gating hard would remove a working endpoint over a title and a transcript, and would do it on the default acp transport first. The route keeps what is its own: the "id required" 400, the status mapping, the fail-closed audit and the SSE push — the audit has to land after the switch has already mutated cs, and the push must not fire when it fails. Behaviour is unchanged and pinned: the four red lines (transcript backfill, cumulative detection, workspace containment, single base session identity) each get named tests with their negative half, and the success body's key ORDER is compared as a string. Six mutations of the facade were run to prove the tests are load-bearing. KNOWN DEBT 1 in the new module records what this batch did NOT retire: the 3-candidate transcript probe. The default acp transport has no engine surface to replace it with, getMessages paginates where the probe caps lines, their orderings differ, and export's enrichment is still byte-pinned to the same candidates. What IS retired is the coupling — the route no longer names lib/transcript.js, and the probe list is an implementation detail behind one seam. * chore: allowlist the leak-tripwire fixture in model-reads tests gitleaks' generic-api-key rule flags the deliberate sk-secret-should- never-leak fixture that model-reads.test.js uses as a leak-prevention tripwire (asserting the facade never serializes provider keys). The value is fake and the assertion exists to catch real leaks; allowlist the exact pairing instead of weakening the fixture. * test(webui): pin session-writes cleanup-orphans test to isolated paths * chore: ignore gitleaks fingerprints of deliberate test fixtures The full-history scan flags two synthetic-credential fixtures: the model-reads leak tripwire (fake provider keys asserting the facade never serializes them) and the fs-credential-guard canaries (fake id_rsa/pem bodies asserting the 403 guard). Pin their fingerprints in .gitleaksignore; the .gitleaks.toml path allowlist for the same files stays as a coarse first line. * chore: make the gitleaks fixture allowlists path-only The match-targeted entry missed the byok fixture key (the generic rule's match string differs from the tripwire value the entry was written for). Scope both entries to the two fixture files themselves — every finding in them is synthetic by construction — and keep .gitleaksignore as the precise fingerprint layer. * feat(webui): move interrupt and load endpoints behind the engine facade * fix(webui): take the plan's 5s abort force-kill bound by product call The batch plan transcribed the abort bound as 5s; the migrated file ran 2000ms. The product call (2026-10-03) takes the plan's value: the longer grace gives a stubborn child more time to finalize at the cost of 'already stopped' staying a lie for three extra seconds. The pinning test moves with it and KNOWN DEBT 1 records the resolution. * docs(webui): add session-switch, interrupt and session-load to the architecture map * docs(webui): add the missing zh-CN section for the B5 write family * fix(webui): make webui-only session delete return promptly instead of hanging * fix(webui): retire lossy streaming mirrors when the engine transcript arrives * feat(webui): add the streaming-send capability gate and pure stream bridge M3-B8a (1 of 2) splits M3-B8 at the boundary the plan drew but the original batch crossed. This commit ships the PURE layer of the #12 send family: the streamingSend capability declaration, the HARD gate that enforces it, and the eight derivations that turn the runtime's stream vocabulary into webui's chat-line vocabulary. NO USER-VISIBLE CHANGE. The gate and the derivations are in place and tested, but nothing calls them: POST /api/send is not wired to a runtime branch yet and behaves exactly as it did at 32277c3a on every transport. Both the acp and the runtime failure sets are the pre-existing baseline core. M3-B8b adds the runner and the route branch and lights the transport up. The family gates HARD, the first M3 family to do so, because #12's response is {ok:true} written BEFORE the engine is called: a provider with no send surface could only be answered with an ack for a turn that never runs, and there is no truthful degradation to fall back to. The gate is deliberately unreachable today (v2 declares streamingSend: full; acp has no registered provider until M4), and the suite pins both halves of that so making it reachable is a deliberate edit. * feat(webui): run send on the runtime transport behind the engine facade M3-B8b (2 of 2) lights up POST /api/send on the runtime transport. It adds the half B8a deliberately left out: the runner, the route branch, and the data plane that opens a turn's event stream. The acp path is unchanged. runMcodeAcp and streamAcpPrompt were not edited, and the route's post-run tail — the finalize drain, promoteDraftToMcodeSid, persistCurrentChat, pushStateFor — is untouched, because the runtime runner resolves to the same result shape and writes through the same run-chat buffer. That is what makes run-mirror, the drain and the single-identity promotion structural rather than re-implemented: they cannot differ between transports. MCODE_USE_ACP=0 still outranks the new branch, as lib/config.js documents — the escape hatch exists for the moment a transport misbehaves, so an operator must not have to unset a second variable first. The three route-level test files that install a fake ACP transport now pin MCODE_WEBUI_TRANSPORT=acp at module scope. Before B8b the send endpoint had no sibling to follow, so no test had to declare which transport it was written against; that declaration is what stops an acp-path contract from silently becoming a runtime-path one. Recorded in engine/streaming-send.js: /api/stop cannot stop a runtime turn and says so; attachments reach the runtime without a mime type; the context limit is not bridged from the stream; a first runtime turn does not receive the user's model pick; transport selection is still an env read rather than a registry lookup. * feat(webui): answer set-mode and set-config-option with structured 501 when the capability is absent * docs(webui): add the streaming-send architecture section, bilingual * docs(webui): add the streaming-send architecture section, bilingual * fix(local-runtime): make an abandoned migration lease recoverable at startup The V2 agent cutover takes a dataDir lease before `mcode acp` can serve a prompt, and the lease is a bare directory: mkdir acquires, rmdir releases, a live holder heartbeats the directory mtime every `stale / 2`. A process killed between the two leaves the directory behind, and an abandoned lease is then indistinguishable from a held one except by that mtime. The window was 30 minutes while the retry budget was 120 attempts at this backoff shape — about 55 seconds. A waiter could not outlast the window, so every engine launch during it spent the whole budget and then died with `agent_name_conflict_migration_failed:lock`. One killed process therefore made `mcode acp` unstartable for half an hour, and each blocked launch produced no answer, no engine process and no session — the reported send regression. The stale window drops to 2 minutes. That does not weaken the safety property: proper-lockfile derives the heartbeat from the stale window, so "two missed heartbeats before the lease is called abandoned" is unchanged, and the migration re-inspects under the lease, so the worst a wrongly-considered stale lease costs is one extra inspection rather than a double rewrite. The retry budget rises to ~195s so a waiter survives one expiry and acquires instead of dying at the moment the lease becomes reapable. Verified against the live data directory: an orphaned lease took the engine from 55450ms/exit=1 to 2030ms/exit=0, and an isolated webui instance returned a real model reply with zero `acp exited` events. * feat(webui): move model and permission writes behind the engine facade * Reset dev-lhl to the full local integration line (B9+B10+docs+P13+P14+B8a/B8b) after push-order rollback
1 parent ed61fe6 commit 6bee4fa

13 files changed

Lines changed: 2166 additions & 153 deletions

File tree

‎docs/webui.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,33 @@ Two consequences of that table are deliberate rather than incidental:
258258

259259
**What the user sees.** The permission-mode selector and the model selector are hidden, not disabled and not accompanied by an error message (`webapp/lib/engine-capabilities.ts`, wired in `webapp/components/composer.tsx`). A toast would report a failure for something the user was never able to do, offer nothing to act on, and reappear on every click. The rule is fail-open: the controls are shown until the declaration positively says the engine cannot do it, so a failed or slow `/api/engine-capabilities` request never removes a working control.
260260

261+
### M3-B10: the model and permission writes move behind the facade (no behaviour change)
262+
263+
`POST /api/set-model` (#58) and `POST /api/permissions` (#59) are the second half of the model endpoint family; B4 moved its read, this batch moves the write. The reasoning leaves the route and lands in `packages/webui/server/engine/model-writes.js`, where it is named, exported and tested on its inputs.
264+
265+
**Nothing a client can observe changed.** Every status, response field, field order, warning string and engine push — including which push happens first and what it is allowed to say when it fails — is the one these two endpoints produced before. The boundary is:
266+
267+
| Concern | Home after B10 |
268+
| --- | --- |
269+
| webui id → engine wire value | `resolveEngineModelConfigValue` |
270+
| variant channel vs effort channel, and the push order each implies | `planModelSelectionPush` |
271+
| the `set_config_option` calls | `pushEngineModelSelection` / `pushEnginePermissionMode` |
272+
| permission mode → label / engine value | `resolvePermissionSelection` |
273+
| the rule for when the local `configOptions` snapshot may claim the engine's new effort | `applyThinkingEffortMirror` (the write stays in the route — `cs` is webui's own state) |
274+
| body parsing, the 400s, the `cs.model` / `cs.permissions` writes, `pushStateFor`, the response bodies | `packages/webui/server/routes/model.js` |
275+
276+
Two forms the picker deals with are deliberately different and stay that way. What the **engine** receives is the wire form — `m:<provider>:<model>:u`, or `m:<provider>:<model>:v:<variant>` for a switchable builtin, plus a bare level for `thinkingEffort` and an engine vocabulary word for `permissionMode`. What **webui** records is the user-facing form — `cs.model.name` in `<providerKey>/<engineModelKey>`, `cs.model.thinking`, `cs.permissions` as a label. The map between them is what the suite pins, field by field, over one row per (engine option shape × request shape) in `packages/webui/test/lib/engine/model-writes.test.js`.
277+
278+
**The variant channel (ticket 36) is unchanged and now covered by name.** A switchable builtin (`thinking_config.mode: switchable`, e.g. MiniMax-M3) has no engine effort vocabulary — the engine rejects every `thinkingEffort` value for it — and advertises it only as the wire pair `v:thinking` / `v:none-thinking`. Such a pick is therefore **one** `model` push carrying both the model and the on/off level, with no second push at all. Every other model keeps the two-push contract: `model` first, then `thinkingEffort`, because the engine rejects an effort set when no model is selected. A cleared level on the variant channel means the engine's **default** variant, not "off" — the normaliser only knows `on` and `off`.
279+
280+
**The 4-second SSE race window is unchanged**, and now has both halves pinned. A pick stamps the fields the request actually carried, all with one timestamp, so `applyConfigOptionUpdate`'s ownership-aware mirror (`server/lib/mcode-acp.js`, ticket 08) defers the engine's wire-form echo for 4 seconds instead of letting it overwrite the chip a few milliseconds after the optimistic write. A field the request did *not* carry is not stamped, so a later cross-client change to that field still mirrors immediately.
281+
282+
**`contextWindow` is still recorded and never pushed.** The engine's ACP surface has no channel for it, so the pick is a webui-side preference the picker reflects immediately.
283+
284+
**These two endpoints are not gated, and that is an open decision rather than an oversight.** #59 writes `permissionMode` only, so gating it on `authCredentials.setPermissionMode` would be behaviourally inert today and safe against the shipped UI (the permission selector is already hidden under exactly that declaration) — it is one `assertEngineCapability` call. #58 also writes `thinkingEffort`, which is a *generic* config id: gating it the same way would make the thinking-effort control answer 501 for the same reason #68 does for an unrecognised id. Both branches are costed in the KNOWN DEBT section of `model-writes.js` — bridge `thinkingEffort` as a third bridged id, or accept the 501 and extend the frontend's degradation to a third control. Until that is decided, #58 keeps its pre-B10 behaviour.
285+
286+
**The bridge is no longer an unverified exemption.** `selectModel` and `setPermissionMode` — the two sub-items `MODE_WRITE_BRIDGED_CONFIG_IDS` names — are now in the snapshot audit's `REQUIRED_METHODS`, so a real booted host is checked for both of them on the adapter *and* the CliService surface, and a declaration that stops listing one goes red. Neither surface carries a `setThinkingEffort` / `selectThinkingEffort`, which is the fact the gating decision above turns on.
287+
261288
### Migration state and constraints
262289

263290
- **M1 done in this batch**: host construction (`createCatalogueHost`) moved verbatim into `server/engine/providers/local-runtime-v2.js`; `runtime-host.js` re-exports it, so every existing importer is untouched. No existing route's behaviour changed; `GET /api/engine-capabilities` is a new, additive endpoint.

‎docs/webui.zh-CN.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,33 @@ GET /api/engine-capabilities[?provider=<id>]
258258

259259
**用户看到什么。** 权限模式选择器与模型选择器被**隐藏**,不是禁用,也不配任何错误提示(`webapp/lib/engine-capabilities.ts`,接线在 `webapp/components/composer.tsx`)。toast 会为一件用户从来就做不到的事报一次失败、无从处理、而且每点一次就再报一次。这条规则是 fail-open 的:控件会一直显示,直到声明明确说引擎做不到——因此一次失败或超时的 `/api/engine-capabilities` 请求绝不会拿掉一个本来能用的控件。
260260

261+
### M3-B10:模型与权限写搬进引擎门面(行为零变更)
262+
263+
`POST /api/set-model`(#58)与 `POST /api/permissions`(#59)是模型端点族的另一半:B4 搬了读,本批搬写。推理逻辑离开路由,落进 `packages/webui/server/engine/model-writes.js`,在那里被命名、导出,并按入参测试。
264+
265+
**客户端能观察到的一切都没变。** 每个状态码、每个应答字段、字段顺序、警告文案与引擎推送——包括哪一次推送先发生、失败时它被允许说什么——都与本批之前逐字节一致。边界如下:
266+
267+
| 关注点 | B10 之后的归属 |
268+
| --- | --- |
269+
| webui id → 引擎 wire 值 | `resolveEngineModelConfigValue` |
270+
| variant 通道 vs 强度通道,以及各自蕴含的推送顺序 | `planModelSelectionPush` |
271+
| `set_config_option` 调用 | `pushEngineModelSelection` / `pushEnginePermissionMode` |
272+
| 权限模式 → 标签 / 引擎值 | `resolvePermissionSelection` |
273+
| 本地 `configOptions` 快照何时可以宣称引擎的新强度 | `applyThinkingEffortMirror`(写仍留在路由——`cs` 是 webui 自己的状态) |
274+
| 请求体解析、400、`cs.model` / `cs.permissions` 写入、`pushStateFor`、应答体 | `packages/webui/server/routes/model.js` |
275+
276+
选择器面对的两种形态本就不同,并且刻意保持不同。**引擎**收到的是 wire 形态——`m:<provider>:<model>:u`,可切换内置模型则是 `m:<provider>:<model>:v:<variant>`,另加 `thinkingEffort` 的裸档位与 `permissionMode` 的引擎词汇。**webui** 记录的是面向用户的形态——`<providerKey>/<engineModelKey>` 的 `cs.model.name`、`cs.model.thinking`、作为标签的 `cs.permissions`。两者之间的映射由测试逐字段钉住:`packages/webui/test/lib/engine/model-writes.test.js` 按(引擎选项形态 × 请求形态)每种组合一行。
277+
278+
**variant 通道(ticket 36)语义不变,并且现在被具名覆盖。** 可切换内置模型(`thinking_config.mode: switchable`,如 MiniMax-M3)没有引擎强度词汇——引擎会拒绝它的一切 `thinkingEffort` 取值——只以 `v:thinking` / `v:none-thinking` 这一对 wire 形态公布。因此这样的选择是**一次** `model` 推送,同时带上模型与开/关档位,压根没有第二次推送。其余模型保持双推送契约:先 `model` 后 `thinkingEffort`,因为引擎在未选模型时会拒绝设置强度。在 variant 通道上「清空档位」意味着引擎的**默认** variant,而不是「off」——归一化器只认 `on` 与 `off`。
279+
280+
**4 秒 SSE 竞态窗口不变**,且两个半边都被钉住。一次选择会为请求**实际携带**的字段打戳,全部共用同一个时间戳,于是 `applyConfigOptionUpdate` 的归属感知镜像(`server/lib/mcode-acp.js`,ticket 08)会把引擎的 wire 形态回声推迟 4 秒,而不是让它在乐观写入后几毫秒就覆盖芯片。请求**未**携带的字段不会被打戳,因此该字段后续的跨端变化仍会立即镜像。
281+
282+
**`contextWindow` 依旧只记录、不推送。** 引擎 ACP 面没有它的通道,因此这项选择是 webui 侧的偏好,选择器立刻就能反映。
283+
284+
**这两个端点没有挂门,而这是一个待人拍板的开口,不是疏漏。** #59 只写 `permissionMode`,所以把它挂到 `authCredentials.setPermissionMode` 上,今天在行为上是空转的,而且对已发布 UI 安全(权限选择器本来就按同一条声明被隐藏)——那只是一次 `assertEngineCapability` 调用。#58 还会写 `thinkingEffort`,而它是**通用** config id:照样挂门会让思考强度控件因为与 #68 遇到无法识别的 id 时完全相同的原因开始答 501。两个分支的成本都写在 `model-writes.js` 的 KNOWN DEBT 段——把 `thinkingEffort` 桥接成第三个 id,还是接受 501 并把前端降级扩到第三个控件。在拍板之前,#58 保持 B10 之前的行为。
285+
286+
**桥接不再是未经核实的豁免。** `selectModel` 与 `setPermissionMode`——`MODE_WRITE_BRIDGED_CONFIG_IDS` 点名的两个子项——现已进入快照审计的 `REQUIRED_METHODS`,因此真实启动的 host 会在 adapter **与** CliService 两个面上被检查这两个方法,而停止列出其中之一的声明会变红。两个面都没有 `setThinkingEffort` / `selectThinkingEffort`,这正是上面那个挂门决策所依据的事实。
287+
261288
### 迁移状态与边界
262289

263290
- **本批只做迁移第一步 M1**:host 构造(`createCatalogueHost`)原样移入 `engine/providers/local-runtime-v2.js`,`runtime-host.js` 转发导出,既有引用方零改动;没有任何现有路由行为变化,`GET /api/engine-capabilities` 是纯新增端点。

‎packages/local-runtime/src/persistence/migration/agent-name-conflict-migration.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,37 @@ import {
3636

3737
const MANIFEST_FILE = 'agent-name-conflicts.json';
3838
const EMPTY_REFERENCE_COUNTS = EMPTY_AGENT_NAME_CONFLICT_REFERENCE_COUNTS;
39-
// Synchronous backup/rewrite needs a long-lived dataDir lease.
40-
export const AGENT_NAME_CONFLICT_MIGRATION_LOCK_STALE_MS = 30 * 60_000;
41-
const AGENT_NAME_CONFLICT_MIGRATION_LOCK_RETRIES = {
42-
retries: 120,
39+
// Synchronous backup/rewrite needs a dataDir lease, so the lease outlives the
40+
// critical section rather than the other way round.
41+
//
42+
// The window is 2 minutes, and it was 30. The reason is not "2 minutes is
43+
// enough to rewrite a database" — a live holder refreshes the lease every
44+
// `stale / 2` (proper-lockfile derives the heartbeat from the stale window), so
45+
// the ratio that actually matters — "two missed heartbeats before the lease is
46+
// called abandoned" — is unchanged. What changed is the cost of a lease that
47+
// is abandoned for real.
48+
//
49+
// This lease gates PROCESS STARTUP: `mcode acp` takes it during the V2 cutover
50+
// and cannot serve a prompt without it. The lease is a bare directory, so a
51+
// process killed between `mkdir` and `release` leaves it behind, and the only
52+
// evidence of a live holder is the directory's mtime. A 30-minute window
53+
// therefore made one killed process unstartable for half an hour — and the
54+
// retry budget (55s) was far too short to outlast it, so every launch during
55+
// that window burned 55s of backoff and then died with
56+
// `agent_name_conflict_migration_failed:lock`. That is the outage: repeated
57+
// sends producing no reply, no engine process, and no session.
58+
//
59+
// The migration itself is idempotent and re-inspects under the lease
60+
// (`ensureAgentNameConflictMigrationLocked`), so the worst a wrongly-considered
61+
// stale lease can cost is one extra inspection — not a double rewrite.
62+
export const AGENT_NAME_CONFLICT_MIGRATION_LOCK_STALE_MS = 2 * 60_000;
63+
// The wait must be able to OUTLAST the stale window, or a waiter gives up at
64+
// the moment the abandoned lease becomes reapable. At this backoff shape the
65+
// 120 retries summed to ~55s against a 30-minute window — a waiter could never
66+
// win, only die. 400 retries sum to ~195s, which rides out one full stale
67+
// expiry and then acquires.
68+
export const AGENT_NAME_CONFLICT_MIGRATION_LOCK_RETRIES = {
69+
retries: 400,
4370
factor: 1.2,
4471
minTimeout: 25,
4572
maxTimeout: 500,
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { mkdir, mkdtemp, rm, utimes } from 'node:fs/promises';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'node:path';
4+
import { afterEach, describe, expect, it } from 'vitest';
5+
import lockfile from 'proper-lockfile';
6+
import {
7+
AGENT_NAME_CONFLICT_MIGRATION_LOCK_RETRIES,
8+
AGENT_NAME_CONFLICT_MIGRATION_LOCK_STALE_MS,
9+
withAgentNameConflictMigrationLock,
10+
} from '../../src/persistence/migration/agent-name-conflict-migration.js';
11+
12+
// The lease that gates `mcode acp` startup. `proper-lockfile` represents it as
13+
// a bare directory (`<dataDir>.lock`): mkdir acquires, rmdir releases, and a
14+
// live holder heartbeats the directory mtime every `stale / 2`. A process
15+
// killed between the two leaves the directory behind, and an abandoned lease is
16+
// indistinguishable from a live one except by that mtime.
17+
//
18+
// These tests pin the two properties the outage depended on. An abandoned lease
19+
// must become reapable on a timescale a process launch can wait out, AND a
20+
// lease that really is held must still be respected. Both halves matter: a fix
21+
// that only shortened the window would trade a startup outage for two
22+
// processes inside one critical section.
23+
24+
const cleanups: Array<() => Promise<void | void>> = [];
25+
26+
afterEach(async () => {
27+
while (cleanups.length > 0) await cleanups.pop()!();
28+
});
29+
30+
async function makeDataDir(): Promise<string> {
31+
const dir = await mkdtemp(join(tmpdir(), 'agent-name-conflict-lock-'));
32+
cleanups.push(async () => {
33+
await rm(dir, { recursive: true, force: true });
34+
});
35+
cleanups.push(async () => {
36+
await rm(`${dir}.lock`, { recursive: true, force: true });
37+
});
38+
return dir;
39+
}
40+
41+
/** Create the lease directory and age its mtime to `ageMs` in the past. */
42+
async function plantAgedLease(dataDir: string, ageMs: number): Promise<void> {
43+
const leaseDir = `${dataDir}.lock`;
44+
await mkdir(leaseDir, { recursive: true });
45+
const when = new Date(Date.now() - ageMs);
46+
await utimes(leaseDir, when, when);
47+
}
48+
49+
/**
50+
* An abandoned lease of a fixed, real-world age.
51+
*
52+
* The age is ABSOLUTE and deliberately not derived from the stale constant.
53+
* A fixture aged by `STALE + margin` is reaped instantly under any value of
54+
* the constant, so it passes whatever the constant is — it cannot fail, and a
55+
* test that cannot fail is decoration. Five minutes is the shape of the outage
56+
* this pins: a lease orphaned 15+ minutes earlier by a killed process, still
57+
* blocking every engine launch.
58+
*/
59+
const ABANDONED_LEASE_AGE_MS = 5 * 60_000;
60+
61+
describe('agent name conflict migration lease', () => {
62+
it('takes over an abandoned lease promptly instead of burning the whole retry budget', async () => {
63+
const dataDir = await makeDataDir();
64+
await plantAgedLease(dataDir, ABANDONED_LEASE_AGE_MS);
65+
66+
const startedAt = Date.now();
67+
await expect(withAgentNameConflictMigrationLock(dataDir, () => 'reaped')).resolves.toBe(
68+
'reaped',
69+
);
70+
71+
// Fast, not merely eventually correct. Before the fix a 30-minute stale
72+
// window made this wait out the whole ~55s retry budget and then throw,
73+
// because the budget could not outlast the window — which is what killed
74+
// every engine launch.
75+
expect(Date.now() - startedAt).toBeLessThan(10_000);
76+
});
77+
78+
it('still leaves a lease that is genuinely held alone', async () => {
79+
const dataDir = await makeDataDir();
80+
// A real holder, taken with the same library the migration uses, so the
81+
// lease directory is a genuine one and its mtime is a live heartbeat —
82+
// which is the ONLY thing that separates a held lease from an abandoned
83+
// one. This is the half that guards against over-correcting: if the stale
84+
// window were shortened to the point of reaping a beating lease, the
85+
// takeover would trade a startup outage for two processes inside one
86+
// critical section.
87+
const release = await lockfile.lock(dataDir, {
88+
stale: AGENT_NAME_CONFLICT_MIGRATION_LOCK_STALE_MS,
89+
});
90+
91+
let entered = false;
92+
const acquisition = withAgentNameConflictMigrationLock(dataDir, () => {
93+
entered = true;
94+
});
95+
// Long enough to prove it is waiting rather than barging in.
96+
await new Promise((resolve) => setTimeout(resolve, 3_000));
97+
expect(entered).toBe(false);
98+
99+
// Once the holder releases, the waiter proceeds on its own.
100+
await release();
101+
await acquisition;
102+
expect(entered).toBe(true);
103+
});
104+
105+
it('keeps the retry budget long enough to outlast the stale window', async () => {
106+
// The invariant behind both tests: a waiter must be able to survive one
107+
// abandoned-lease expiry and then acquire, rather than dying at the moment
108+
// the lease becomes reapable. With 120 retries at this backoff shape the
109+
// wait was ~55s against a 30-minute window, so it could only ever lose.
110+
const { retries, factor, minTimeout, maxTimeout } =
111+
AGENT_NAME_CONFLICT_MIGRATION_LOCK_RETRIES;
112+
let total = 0;
113+
let delay = minTimeout;
114+
for (let attempt = 0; attempt < retries; attempt += 1) {
115+
total += delay;
116+
delay = Math.min(delay * factor, maxTimeout);
117+
}
118+
119+
expect(total).toBeGreaterThan(AGENT_NAME_CONFLICT_MIGRATION_LOCK_STALE_MS);
120+
});
121+
});

0 commit comments

Comments
 (0)