Skip to content

ui-sdk: attribute the native stage writer behind 'Native output changed before copy' on brain.db #4010

Open

Description

@schlessera

Context

Split out of #2878, which attributes the stale-input refusal of .brain-index/brain.db-shm to a read-only search connection on the live index. Its repair, PR #4011, is merged as main 5af689769: the stage wal-index is no longer copied, and both before-copy refusals now print both stamps.

#2878 also recorded two native-output-copy refusals in the native pi --apply-timing cell (UID 1000, 2,000 notes) while proving #2853, in 2 of 7 runs:

  • Native output changed before copy: .brain-index/brain.db-shm at the drained automatic refresh. Since fix(ui-sdk): a read-only index connection no longer refuses native publication #4011 the stage wal-index is excluded from the copy (WAL_INDEX, packages/ui-sdk/src/server/worker-native-output.ts:231), so this variant can no longer surface. That hides it without explaining it; it was the same stage-side before-copy check as the database-file variant.
  • Native output changed before copy: .brain-index/brain.db at the same-turn brain_search barrier (IndexFlushError, APPLY_TIMING_SEARCH_WAITED_FOR_INDEX). This one is not explained.

The check compares the stage file's stamp (device, inode, mode, size, mtime, ctime) at copy time with the stamp captured when publication inspected the stage (const found = stamp(await input.stat, packages/ui-sdk/src/server/worker-native-output.ts:475-476). Publication runs after the maintenance worker has exited, so something still wrote the stage's database, or replaced it, between inspection and copy. No writer is attributed. Since #4011 the refusal prints both stamps (inspected …, found …). The stamp fields distinguish replacement from in-place modification; they do not by themselves identify the writer. Attribute the process rather than inferring one from a stamp change.

Scope

  • Reproduce or capture one failing run with the two stamps, and name the process that changed the stage database after inspection.
  • Fix that writer, or the drain that should have waited for it, so the refresh publishes.

Out of scope

Acceptance criteria

  • A failing run's error, with both stamps, is recorded on this issue and names the writer, or a test reproduces the refusal with a real concurrent writer on the stage.
  • After the fix, that test, or 7 of 7 runs of the cell, publishes without Native output changed before copy.

Verification

bun scripts/prove-native-host.ts --backend=pi --app-uid=1000 --apply-timing, repeated at least 7 times.

Contract impact

None expected.

Bounded triage attempt — 2026-11-15

On isolated main c09454c338b6c420fedee8e1a66aa338a695bcdd, bun scripts/prove-native-host.ts --backend=pi --app-uid=1000 --apply-timing exits 1 at scripts/prove-native-host.ts:56: Native proof image is absent. No native cell, stage copy or writer was exercised. Do not count this as a reproduction of Native output changed before copy or as one of the seven requested timing runs.

Keep needs: repro. The next qualified native-host run must retain the exact checkout, cell, both stage identities (using the diagnostic when actually delivered, or equivalent bounded instrumentation), failure phase and process evidence. It must distinguish a stage write/replacement from the already-attributed live-side SHM change. No common writer is inferred. The stage-writer investigation can proceed independently of merging #4011; no dependency wait, release commitment or human-only requirement is introduced. Keep standalone: no existing epic’s complete outcome covers this native stage-writer repair merely because it shares the SDK or verification harness.

2026-11-15 follow-up triage at main c09454c338b6c420fedee8e1a66aa338a695bcdd: Separated the active #2878/#4011 proposal from delivered main and repaired the copy-boundary citation. Recorded the missing native-image prerequisite without claiming a runtime reproduction; needs: repro and p2 remain.

2026-11-16 correction: #4011 is merged (main 5af689769). Updated the context to say the stage wal-index is no longer copied and both stamps are printed, and moved the copy-boundary citation from :435 to :475-476. The reproduction-panel findings are in the comment of the same date.

Active evidence after the panel — 2026-11-16

The latest comment forces the before-copy refusal with a real product-configured SQLite writer that has committed frames and closes after inspection but before copy. Read-write/no-write, read-only and no-late-connection controls publish. This distinguishes a viable late-writer mechanism from a mere read connection; it does not identify the production writer or exclude a metadata/identity replacement in the historical native cell.

Retain the panel's exact stamp-pair diagnostic and the next qualified native run's stage identity, worker-helper mode, drain binding/process/descriptor evidence and failure phase. The existing seven-run acceptance remains; the panel’s suggested up-to-thirty search is an investigation cap, not a new passing-proof requirement or permission to infer a writer. Keep needs: repro until the actual writer or a source-qualified equivalent cause is identified. No forced local success or #4011 SHM exclusion substitutes for that attribution.

2026-11-20 triage — Built the proof image from the pinned Dockerfile and ran bun scripts/prove-native-host.ts --backend=pi --app-uid=1000 --apply-timing with that image on main bd751d434. The offline container exited 1 opening /dev/loop-control (PermissionError: [Errno 13]) before the stage/database cell ran. Its cleanup reported no attached loop or mounted filesystem. The stage-writer attribution still needs a qualified native runner and the paired stamps/process evidence above.

Activity

  1. added
    type: fixSomething is broken and must behave the way it is documented.
    area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.
    priority: p2Wanted, not scheduled. Pulled in when a milestone has room.
    needs: reproBlocked on a reproduction.
    on Nov 15, 2026
  2. schlessera commented on Nov 15, 2026

    @schlessera
    OwnerAuthor

    Triage — 2026-11-15. #4011 is still open: its SHM treatment and two-stamp diagnostic are proposed changes, not main behavior. The body now makes that distinction and points at main’s actual before-copy check. The local timing command stopped at the absent native-image prerequisite, before any stage writer was exercised; retain needs: repro. No fix, seven-run result or process attribution is supplied.

  3. schlessera commented on Nov 16, 2026

    @schlessera
    OwnerAuthor

    Reproduction panel findings — 2026-11-16 (main 5af689769)

    Three agents on different models worked this independently, then cross-reviewed each other's write-ups. The refusal class is reproduced; the production writer is still unidentified. needs: repro stays.

    What is reproduced

    A forcing test that holds a real SQLite connection on the stage index, opened with the product's openDatabase pragmas (WAL, persistent WAL), and closes it inside publishLocked's first lock.acquire (packages/ui-sdk/src/server/worker-native-output.ts:407-408). That point is strictly after inspection and before the before-copy check at :475-476.

    Arm Result
    writer with committed frames, closed late refuses with Native output changed before copy: .brain-index/brain.db (inspected …, found …): same dev/ino/mode, later mtime and ctime
    read-write open, no write, closed late publishes
    read-only reader, closed late publishes
    no late connection publishes

    Two panelists ran it independently (4 of 4 and 2 of 2 passes). So the actor must have committed frames to the stage index. A process that only opened it read-write, or a read-only brain_search connection, cannot produce the brain.db variant.

    Ruled out or weakened

    • Writer surviving the sandbox exit: with bwrap 0.11.1, two SIGTERM-immune writer loops left in the namespace wrote nothing after the outer process exited (0 of 20 and 0 of 3 trials). The native image's bwrap version still needs checking.
    • Manager RPCs touching the stage during publication: for the index operation validateGitScaffolds returns immediately (worker-native-output.ts:347-348), and no private reader path is used.
    • Btrfs stamp semantics: the stage upper is tmpfs (packages/ui-sdk/src/server/native-brain-stage.ts:22); only the lower is Btrfs.

    Ranked hypotheses

    1. A stage-index writer that outlives the awaited drain (await executionDrain.get(prepared)?.exited, worker-native-output.ts:368); its late checkpoint or close moves committed frames into brain.db (≈0.40). No in-tree route was found by reading.
    2. OverlayFS identity or metadata flip with no writer (≈0.20). In the failing cell the worker almost certainly ran as the same UID without the identity helper, so prepareMaintenanceIndex returns early (packages/ui-sdk/src/server/maintenance-index-input.ts:44). brain.db then reaches the tmpfs upper by copy-up on the indexer's read-write open, with an origin in the lower.
    3. A second index writer bound through the single-slot drain (≈0.05–0.15, new). Each stage keeps one exited and overwrites it on every bind (bindNativeMaintenanceExit, worker-native-output.ts:142-143). A launch that returned early on an abort or kill path while its child lived would orphan its drain.
    4. Replacement of the stage file (≈0.10).

    The first failing stamp pair separates these:

    • same dev/ino with mtime and ctime moved: an in-place write;
    • dev/ino changed with equal times: an identity flip;
    • only ctime moved: a metadata change;
    • ino changed with times moved: a replacement.

    Next run (needs the native proof image; ≈2 h on a quiet host)

    1. Land the forcing test as the class reproduction. It is the failing-first receipt for whatever fix follows.
    2. Build the native image, then loop bun scripts/prove-native-host.ts --backend=pi --app-uid=1000 --apply-timing up to 30 times, stopping at the first refusal. The brain.db variant appeared once in seven runs.
    3. Instrument that loop:
      • record the stage id, and whether BRAIN_UI_WORKER_IDENTITY_HELPER was set;
      • scan /proc/*/fd for the stage index inodes at drain time and at refusal;
      • run a fanotify watch, which gives the acting pid where inotify does not;
      • log every drain bind with pid and stage id.
    4. Read the branch off the first refusal's stamp pair and the trace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.needs: reproBlocked on a reproduction.priority: p2Wanted, not scheduled. Pulled in when a milestone has room.type: fixSomething is broken and must behave the way it is documented.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions