Skip to content

fix(zcode): store-drift repair re-checks the store's owner before each write (#63) - #73

Merged
anuzzolilli-oss merged 2 commits into
masterfrom
2026-11-15_zcode-drift-repair-rechecks-owner
Nov 15, 2026
Merged

anuzzolilli-oss merged 2 commits into
masterfrom
2026-11-15_zcode-drift-repair-rechecks-owner

Conversation

@anuzzolilli-oss

@anuzzolilli-oss anuzzolilli-oss commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

(ai: anthropic/claude-fable-5-2)

Closes #63.

What changes

repairSharedStoreDrift (src/zcode/native.ts) read the shared store's origin once, then awaited up to two health probes and, on one branch, replacement discovery, and wrote without looking again. A route another process wrote during those awaits could be overwritten with this process's origin or a replacement, or reverted to direct.

Each of the repair's three writes is now allowed only while the store still points at the dead origin first read:

  • Re-point to self, re-point to a replacement: asked under the config lock through the existing shouldWrite hook (added in fix(zcode): decline the watchdog's route write once shutdown has begun (#67) #68), immediately before the write.
  • Revert to direct: re-checked immediately before unrouteZcode. This check also covers a declined route write, which would otherwise fall through and revert the new owner's route.
  • Outcome: a stand-down returns the new value "owner-changed" and logs store drift repair: the store no longer points at <origin> — leaving it untouched. The only production caller (src/zcode/mcp-entry.ts) discards the outcome, so nothing else changes.

A store that went direct during the awaits is also left alone: the predicate is "still the dead origin", not "not someone else's".

Evidence

tests/zcode-native.test.ts, 10 new tests (49 of 49 in the file; zcode-mcp-eof 17, zcode-mcp-entry 9, zcode-mcp-degraded 6 unchanged and passing). Windows 11, Node v24.14.0. Typecheck and both static checks clean.

  • Transfer during the awaits, one test per write path (self, replacement, revert): another owner routes the store inside the probe / findReplacement seam; the store is byte-identical afterwards and the outcome is owner-changed.

  • Went direct during the awaits (self, replacement): the store is not re-wrapped.

  • Transfer while waiting for the config lock (self, replacement): the store still points at the dead origin when the route call starts and changes only while the lock is held by another writer.

  • Controls, one per write path: with nothing changed during the awaits the repair still writes (repointed-self, repointed-replacement, reverted-direct).

  • Ports: the self and replacement origins are loopback servers the test starts on allocated ports, because a successful repair posts runtime info to the origin it routed to (added after review). The dead origin and the new owner are never contacted.

Mutation checks, each restored afterwards:

Change Tests that fail
Original code 7 (all non-control tests)
No shouldWrite on the self route 3 (self path)
No shouldWrite on the replacement route 3 (replacement path)
No re-check before the revert 7
Ownership checked before the route call instead of under the lock 2 (the lock-wait tests)

Not covered

  • The revert is not under the lock. unrouteZcode takes no config lock, so its re-check narrows the cross-process window to a synchronous read-then-write but does not close it. Putting the revert under the lock is the separate decision Zcode store-drift repair can overwrite a route transferred during its health probes #63 defers; not decided here.
  • Same origin comes back. If the dead instance restarts on the same origin and re-routes the store during the awaits, the store reads the same as before and the repair proceeds. Re-pointing to a live self is harmless; a revert to direct in that case would drop a live route. Not reproduced, not addressed.
  • routeZcodeConfig computes its rewrite from text read before it takes the lock (existing behaviour). With the owner check passing, other edits made to the file in that window are still overwritten. Out of scope here.
  • No reproduction against a real ZCode install; no outage is known. Source-level finding, fixed with seam-driven tests.

Assisted-by: Cindy (https://cindy.app)

…h write (#63)

repairSharedStoreDrift read the shared store's origin once, awaited up to two
health probes and replacement discovery, then wrote without looking again. A
route another process wrote during those awaits could be overwritten with this
process's origin or a replacement, or reverted to direct.

Each of the three writes is now allowed only while the store still points at
the dead origin first read. The two route writes ask under the config lock
(the existing shouldWrite hook); the revert re-checks immediately before it,
which also stops a declined route write from falling through into a revert.
New outcome "owner-changed" for the stand-down.

Assisted-by: Cindy (https://cindy.app)
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

📦 Built Package Artifact

Branch: 2026-11-15_zcode-drift-repair-rechecks-owner (00890c2)

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr73.tgz
npm install -g package

Install instructions are refreshed on each push.

@anuzzolilli-oss
anuzzolilli-oss marked this pull request as ready for review November 15, 2026 00:08
Comment thread tests/zcode-native.test.ts Outdated
const dir = dataDir();
try {
await routeZcodeConfig({ origin: DRIFT_DEAD, dataDir: dir, env: {}, log: NO_LOG });
const outcome = await repairDownPath(dir, writePath, () => {});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ai: kilo/openai/gpt-6-astra)

WARNING: Successful control tests POST to unowned fixed ports

The self and replacement controls mock health probes, but this call still reaches activateZcodePluginMode (src/zcode/native.ts:578,587), which calls reportRuntimeInfo and sends a real POST to /__bili/plugin/runtime-info (src/agent/shared.ts:298-303). With DRIFT_SELF/DRIFT_REPLACEMENT, those requests target fixed localhost ports 28787 and 48787. If another bili instance is listening there, the fixture replaces its agent-level zcode runtime-info entry (src/plugin.ts:609-614,676-685); an unresponsive listener instead adds a five-second timeout. Temporary store directories and env: {} do not isolate this network side effect. Use an owned listen(0) server for successful destinations, or stub the runtime-info request with test-scoped restoration.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ai: anthropic/claude-fable-5-2)

Valid, fixed in 00890c2. The self and replacement origins in the new tests are now loopback servers the test starts on allocated ports (withDriftOrigins), so the runtime-info post of a successful repair lands on a server the test owns. The dead origin and the new owner are never contacted: liveness comes from the probe seam and those writes do not reach activateZcodePluginMode. Against the original code the same 7 tests still fail.

The older exit-handoff tests in this file use the fixed origin 127.0.0.1:28787 in the same way (lines 803 to 904 on this head). They predate this PR, so I left them; say if you want them changed here or tracked separately.

@kilo-code-bot

kilo-code-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

(ai: kilo/openai/gpt-6-astra)

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Reviewed the incremental test-fixture changes in tests/zcode-native.test.ts from 9ce97cac to 00890c27, including server lifetimes and runtime-info destinations, using default depth (not a Light pass). No CRITICAL, WARNING, or SUGGESTION findings remain. Static review only; no code or tests were executed.

Previous Review Summary (commit 9ce97ca)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 9ce97ca)

(ai: kilo/openai/gpt-6-astra)

Code Review Summary

Status: 1 Issue Found | Recommendation: Address before merge

Reviewed the shared-store ownership guards and regression tests at 9ce97cac88a21f633a47f00ab36a000bef575fd3 using default depth, not a Light pass. Static review only; no code or tests were executed.

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
tests/zcode-native.test.ts 717 Successful controls send real runtime-info POSTs to fixed localhost ports 28787/48787, potentially overwriting a running bili instance's zcode runtime metadata. Use owned allocated ports or mock the request.
Files Reviewed (2 files)
  • src/zcode/native.ts - 0 issues
  • tests/zcode-native.test.ts - 1 warning

Reviewed by gpt-6-astra · Input: 33 · Output: 4.1K · Cached: 483.9K

Review guidance: REVIEW.md from base branch master

…wn (#63)

A successful repair calls activateZcodePluginMode, which posts runtime info to
the origin it routed to. The new tests used fixed ports for the self and
replacement origins, so a bili instance listening there could have received
that post. Both origins are now loopback servers the test starts on allocated
ports.

Assisted-by: Cindy (https://cindy.app)
@anuzzolilli-oss
anuzzolilli-oss merged commit d4aad61 into master Nov 15, 2026
44 checks passed
@anuzzolilli-oss
anuzzolilli-oss deleted the 2026-11-15_zcode-drift-repair-rechecks-owner branch November 15, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zcode store-drift repair can overwrite a route transferred during its health probes

1 participant