Skip to content

Rebuild the decision snapshot from the merged log when two machines' syncs conflict (#4174) - #4220

Merged
capy-ai[bot] merged 4 commits into
mainfrom
fix-decisions-snapshot-merge-conflict
Nov 9, 2026
Merged

capy-ai[bot] merged 4 commits into
mainfrom
fix-decisions-snapshot-merge-conflict

Conversation

@garrytan

@garrytan garrytan commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Two machines that each log a decision between pushes no longer stop one of them from syncing, so gbrain-sync keeps every machine's decision memory in step without a hand repair.

What changed

Before, the second machine's push was rejected, its fetch-and-merge retry conflicted on projects/<slug>/decisions.active.json, and gstack-brain-sync aborted the merge and wrote push_failed on every later drain until someone repaired the clone by hand. Now the merge finishes and the push goes out, with a snapshot rebuilt from the merged decisions.jsonl.

  • bin/gstack-brain-sync: when a stopped retry merge's only conflicts are projects/*/decisions.active.json files whose decisions.jsonl merged cleanly, each snapshot is rebuilt from the merged log, staged, and the merge is committed. The rebuild runs only when every line of the log at the merge base is still on both sides. The log merge driver takes the union of both sides, so a log that lost lines on one side (--compact expunging a redacted decision) would get them back; that merge aborts as before. A conflict in any other file, a log that did not merge cleanly, a missing bun or a failed rebuild also still aborts.
  • bin/gstack-decision-log --rebuild-snapshot <project-slug>: rewrites one project's snapshot from its log with the same rebuildSnapshot every decision write uses. It refuses an empty slug, a path, ./.., or a project with no log.
  • bin/gstack-artifacts-init: new repositories get projects/*/decisions.active.json merge=binary in .gitattributes, so a merge run by hand leaves no conflict markers in the snapshot. The brain-sync fix does not depend on this line.
  • docs/gbrain-sync.md says how the snapshot is resolved, when brain-sync still aborts, and how to finish a hand-run merge.

Why

"Two machines' syncs conflict and one of them stays push_failed for good" (#4174). The snapshot is derived from the event log, and the log already merges through the jsonl-append driver, so rebuilding from the merged log is the one correct resolution, as long as neither side deleted log lines.

Blast radius

Only brain-sync's retry merge after a rejected push, and only when every conflict is a decision snapshot. The stash recovery path after that merge is unchanged from main, so a mismatched log and snapshot there stay held by the decisions-active coupled-group check (tested). A clone whose peer ran --compact since they last agreed still aborts, as on main.

Test Coverage

  • test/brain-sync.test.ts, the brain-sync: a derived decisions.active.json conflicts in the retry merge and stops a machine from publishing #4174 block, 6 tests: two machines each log a decision (current and pre-fix .gitattributes), a clone already holding a conflicting commit recovers, a merge that would bring back a redacted and compacted decision still aborts, a decision log that stash pop brings back with an expunged decision stays held and never reaches the remote, and a conflict in another file still aborts.
  • Failing first: on main 6473a17 three of the original four went red with push_failed and CONFLICT (content): Merge conflict in projects/acme-app/decisions.active.json. The redaction test was red without its guard (status ok, expected push_failed). The hold test was red with the reverted stash-pop rebuild put back (status ok).
  • test/gstack-decision-bins.test.ts covers --rebuild-snapshot and its refusals; all 26 green.
  • CI: free-tests run 42653255504 green at this tree (1654 files carried).

Pre-Landing Review

Pre-Landing Review: 3 blocking found across 2 full passes and 1 fix-check, all fixed; 4 noted, 0 deferred.

  • The first pass's red team (CRITICAL, fixed): a retry merge after another machine's --compact rebuilt the snapshot from the union log and republished a redacted decision. Fixed by the base-line guard.
  • Step 11 adversarial and outside voice (CRITICAL, fixed): a stash-pop snapshot rebuild added in pass 1 made a decision brought back by stash pop consistent and publishable. Reverted to main's stash path, with a hold test.
  • Notes, below the floor: a peer's compaction keeps that case stuck as on main; the jsonl-append driver brings back compacted lines on clean merges (predates this PR); after the hold, the next ordinary decision on that machine rebuilds the snapshot from the brought-back log (predates this PR); criss-cross merge bases and a missing bun are edge cases.

Gate edits: none detected (2 listed, 0 read).

Exploratory QA

Smoke only through the failing-first repro tests above; the QA method sections were not run.

Documentation

Scope: I audited the 7 selected files in the candidate (base 324135d, head fa75630, 4 commits, with nothing staged, unstaged or untracked) against reviewed tree 68807149491999a53fd125c0207a11ffdb9640ff.

Result: The docs are current, and I made no edits. docs/gbrain-sync.md already describes the snapshot rebuild, the exception for logs that lost lines, the merge=binary attribute and the manual --rebuild-snapshot recovery. All of it matches bin/gstack-brain-sync, bin/gstack-decision-log and bin/gstack-artifacts-init. The banned-terms check found nothing, and there are no blockers.

Debt: The new --rebuild-snapshot flag is documented only in the CLI usage text and in docs/gbrain-sync.md. The decision-log CLI has no reference doc.

Test plan

  • bun test test/brain-sync.test.ts -t 4174: all 6 green
  • bun test test/gstack-decision-bins.test.ts: all 26 green
  • free-tests CI run 42653255504 at fa75630: green

Issues

Refs #4174

Agent notes

Lane GSTA-445 for GStack Minder. Decision trail: reports/GSTA-445-decisions.tsv in the Captain drive.

Approvals

Approved: yes (record r-26365ae18dad). The full approval record stays private.
Plan: exempt: bug fix

REVIEW_LOOP: passes=2 blocking_found=3 deferred=0 noted=4 fixed=4 scope=frozen ledgers_read=3 fix_checks=1 dispatches=4

Agent notes
{
  "schema_version": 1,
  "pr": 4220,
  "head": "fa756304dfe8a151823a50a27e7d80962a86bd02",
  "version": "1.98.0.0",
  "base": "main",
  "gate": {
    "test": {
      "pass": 1654,
      "fail": 0,
      "skip": 0
    }
  },
  "ci_carried": [
    {
      "lane": "test",
      "run": 42653255504,
      "attempt": 1,
      "tree": "68807149491999a53fd125c0207a11ffdb9640ff"
    }
  ],
  "session_kind": "unattended",
  "head_tree": "68807149491999a53fd125c0207a11ffdb9640ff",
  "product_patch_id": "085ec512c87952f833c296d41da76dafbfa9a491",
  "gstack_version": "1.98.0.0",
  "review_depth": {
    "depth": "full",
    "class": "code",
    "runtime_lines": 60,
    "rules": "66e5174161eecc2c",
    "base": "89856f3b3a8a8f616f52e297120927cd54ea8ff1",
    "registry": "89856f3b3a8a8f616f52e297120927cd54ea8ff1",
    "gate_scan": {
      "tagged": 0,
      "head": "fa756304dfe8a151823a50a27e7d80962a86bd02"
    },
    "reasons": [
      "risk:security",
      "runtime=60"
    ],
    "risk": [
      "security"
    ],
    "mode": "panel"
  },
  "pregate": "requires-remote cleared @688071494919 (8 checks, current for fa756304dfe8)",
  "requires_remote": [
    "free-tests.yml/macos-named-regressions: https://github.com/garrytan/gstack/actions/runs/42653255504/attempts/1",
    "pregate.json/remote_gate: remote-gate tree=688071494919 tests=36848 skipped=68 runs=42653255504/1 profiles=ci-reuse/free-tests heads=fa756304dfe8 verified=2026-11-09T16:24:39.341Z"
  ],
  "issues": {
    "fixes": [],
    "refs": [
      4174
    ],
    "flakes": []
  },
  "docs": {
    "status": "current",
    "scope": "docs/ and README.md (140 docs files hashed; 7 changed files audited)"
  },
  "review_loop": {
    "passes": 2,
    "blocking_found": 3,
    "deferred": 0,
    "noted": 4,
    "fixed": 4,
    "scope": "frozen",
    "fix_checks": 1,
    "rounds": 3,
    "ledgers_read": 3,
    "dispatches": {
      "specialist": 2,
      "red-team": 2
    }
  }
}

Open in Capy

…syncs conflict (#4174)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@trunk-io

trunk-io Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

😎 This pull request was merged.

garrytan and others added 3 commits November 9, 2026 14:55
The jsonl-append merge driver takes the union of base, ours and theirs, so a
log that lost lines on one side (--compact expunging a redacted decision)
would get them back. The snapshot rebuild now runs only when every base line
of the log is still on both sides; otherwise the merge aborts as before.
A conflicted snapshot when local edits are put back after the merge is now
rebuilt from the working-tree log instead of left at the merged version.

Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: p1/red-team#2 reader=red-team
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Rebuilding a conflicted decision snapshot after stash pop made a decision
that another machine redacted and compacted away consistent and publishable,
because stash pop merges the log as a union. Without the rebuild the
mismatched log and snapshot stay held, as on main. The redaction guard on
the merge-retry rebuild stays. A new test shows the brought-back decision
stays held and never reaches the remote.

Review-Finding: p2/outside-voice#1 reader=outside-voice
Review-Finding: p2/adversarial-native#1 reader=adversarial-native
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
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.

1 participant