Skip to content

feat(xself): repair ladder behind self doctor --fix - #435

Merged
Sunrisepeak merged 12 commits into
mainfrom
feat/self-repair
Jul 27, 2026
Merged

Sunrisepeak merged 12 commits into
mainfrom
feat/self-repair

Conversation

@Sunrisepeak

Copy link
Copy Markdown
Member

Implements the repair engine from
.agents/docs/2026-07-28-self-repair-design.md.

Why --fix had to grow

self doctor --fix stopped at the shim layer and printed a copy-pasteable
xlings install for every broken payload. On a home carried over from an
older client that is not a handful of lines: 0.4.69 records a headers-only
package as one program-typed entry with a payload path and no executable,
so the new client cannot tell it from a program whose binary vanished. The
upgrade simulation measured 56 findings on a home with five packages in
it.

The printed remedy is the correct one — re-running install re-runs config()
and re-registers the package in the current form, at which point it is
recognisable as a release anchor and the finding is gone. Measured:

                 targets   bindingGroup   members
0.4.69 wrote        1          no            0
after re-register  12          yes          12

Doing that for the user is what --fix is for.

The ladder — src/core/xself/repair.cppm (new)

rung action network destructive
R2 re-register xlings install <pkg>@<ver> -y only if the payload is gone no
R3 reinstall xlings remove → xlings install yes yes

R2 is the usual answer and is cheap: the installer checks the payload on disk
first, so with it intact this re-runs config() only. R3 is for the record
the registration layer refuses to overwrite — the field dead end from
2026-07-28. It is gated on the package being available from the index
(removing what cannot be reinstalled is destruction, not repair) and the
remove-succeeded-then-install-failed case is reported loudly with the command
that finishes the job.

Split from doctor.cppm deliberately: detection and repair have different
risk profiles, and the ladder is the part that can leave a user worse off. It
is a pure decision table over an injected runner — 14 unit tests, no home,
no network, no payload. Every case asserts the exact commands run, because a
rung that skipped its work and returned "healed" is this codebase's recurring
shape.

Three things only running it revealed

  • A finding names an xvm target; the ladder installs a package. A broken
    llvm reports [email protected], [email protected], [email protected] and forty more, none
    of them installable. Nor is the binding root the package — gcc's root target
    is xim-gnu-gcc. The owner is searched for in descending order of
    confidence and every candidate is confirmed against the index before use;
    findings then collapse onto their owner, one install per release.
  • The re-detect must reload_state() first. Repairs run in subprocesses
    that write the state file while this process holds the copy it read at
    startup. Without the reload, a cure that is purely a re-registration reads
    as a failure — observed as 55 healed and one false failure, the single
    metadata-only cure.
  • The migration stamp is gated on the repair pass, not on doctor being
    spotless.
    Active-group incoherence across two providers that both claim
    cc/c++ accounts for 190 findings on a real home by itself. Requiring
    zero would mean the marker never lands and the hint nags forever about a
    migration that already happened.

Verified end to end

Against a state file 0.4.69 actually wrote (restored from the simulation's
snapshot, not synthesized):

before after
broken payloads 56 0
binding incoherence 190 12 → 1
4th consecutive --fix — repairs nothing
.xlings.json:version v0.4.69 the running version
migration hint shown gone

The remaining 1 is an artifact of the restore (a 0.4.69 DB over a home that
has 2026.7.27.4 installed, so the DB has no xlings entry).

Discovery

self update cannot run this — that function is the old binary, having
just replaced itself on disk. It prints the nudge; the new client repeats it
until a --fix lands the stamp. The comparison is a string against a
compiled-in constant: no network, no extra file read.

Also here

  • Version numbering convention in both the contributing skill and the
    built-in agent skill: date-based YYYY.M.D.N, N starts at 1, .0 is
    reserved for a formal release.
  • Simulation harness grows to 10 packages on every platform and gains a
    heal phase that measures the migration instead of trusting an exit code —
    including a second --fix that must find nothing, because a ladder that
    repairs the same thing every run is looping rather than converging.
    SIM_CANDIDATE_BIN lets the phase measure this PR's own build.

`--fix` used to stop at the shim layer and print a copy-pasteable
`xlings install` for every broken payload. On a home carried over from an
older client that is not a handful of lines. 0.4.69 records a headers-only
package as ONE program-typed entry with a payload path and no executable, so
the new client cannot tell it from a program whose binary vanished, and every
such package is reported. Measured on the upgrade simulation: 56 findings on
a home with five packages in it.

The printed remedy is the correct one -- re-running install re-runs config()
and re-registers the package in the current form, at which point the entry is
recognisable as a release anchor and the finding is gone. Doing that for the
user is what --fix is for.

The ladder (src/core/xself/repair.cppm, new):

  R2 re-register  `xlings install <pkg>@<ver> -y` -- cheap, and the usual
                  answer: the installer checks the payload on disk first, so
                  with it intact this re-runs config() only, no download
  R3 reinstall    `xlings remove` then `xlings install` -- for the record the
                  registration layer REFUSES to overwrite, which is the field
                  dead end from 2026-07-28

Split from doctor.cppm on purpose. Detection and repair have different risk
profiles, and the ladder is the part that can leave a user worse off, so it is
a pure decision table over an injected runner: 14 unit tests, no home, no
network, no payload. Every case asserts the exact commands run, because a rung
that skipped its work and returned "healed" is this codebase's recurring
shape.

Three things the implementation had to get right, each found by running it:

- A finding names an xvm TARGET; the ladder installs a PACKAGE. A broken llvm
  reports [email protected], [email protected], [email protected] and forty more, none installable.
  Nor is the binding root the package -- gcc's root target is `xim-gnu-gcc`.
  The owner is searched for in descending order of confidence and every
  candidate is confirmed against the index before use; findings collapse onto
  their owner, so one install per release.

- The re-detect has to reload_state() first. The repairs run in SUBPROCESSES
  that write the state file while this process holds the copy it read at
  startup. Without the reload, a cure that is purely a re-registration reads
  as a failure and only repairs that restored a directory appear to work --
  observed as 55 healed and one false failure, the one metadata-only cure.

- The migration stamp is gated on the REPAIR pass, not on doctor being
  spotless. Active-group incoherence across two providers that both claim
  `cc`/`c++` accounts for 190 findings on a real home by itself; requiring
  zero would mean the marker never lands and the hint nags forever about a
  migration that already happened.

Verified end to end against a state file 0.4.69 actually wrote (restored from
the simulation's snapshot, not synthesized):

  broken payloads      56 -> 0
  binding incoherence  190 -> 12 -> 1   (the 1 is an artifact of the restore)
  4th consecutive --fix  repairs nothing
  .xlings.json:version   v0.4.69 -> the running version
  migration hint         shown before, gone after

Also: `self update` now says to run it. It cannot run it -- that function IS
the old binary, having just replaced itself on disk -- so it prints the nudge
and the new client repeats it until a --fix lands the stamp.
…gration

The harness moves from 5 packages to 10 on every platform (llvm, node, cmake,
ninja, xmake, jq, ripgrep, fd, bat, mcpp; gcc adds an eleventh on linux, where
it is the only platform the index publishes it for). llvm and node each carry
two versions everywhere, so the switch/remove/reinstall cases still have
something to switch between on macOS and Windows.

New `heal` phase, between `update` and `exercise`: run `self doctor --fix` and
measure what it did, rather than trust its exit code.

- payload findings before vs after, read from the summary line the exit code
  is computed from
- the home is stamped with the running client (the migration marker that
  `self update` never wrote, which is why a "you are behind" hint was
  impossible to build)
- a SECOND --fix must not increase findings and must report no repair
  failures: a ladder that repairs the same thing every run is looping, not
  converging, and that is invisible from one pass

SIM_CANDIDATE_BIN points the phase at a specific binary, so a PR can measure
its own build before the change is published. Without it the phase measures
whatever `self update` fetched, which is what a user gets.
`self doctor` exits non-zero while ANY finding remains, and on a real upgraded
home plenty remain that the repair ladder is not responsible for -- active-
group incoherence between two providers that both claim `cc` and `c++`
accounts for ~190 on its own. Filing those as blockers inflates the one number
the report exists to communicate, so the three doctor invocations record their
exit code in the description instead.

Also corrects the heal assertion. "Payload findings went down" cannot hold on
an already-migrated home: the summary counts findings the ladder does not own,
so a home with nothing left to migrate fails an assertion for being healthy.
The honest requirement is that --fix never makes things worse; the quality
signal is the two assertions that follow it -- a second pass must report no
repair failures and must not increase the count.

Verified locally against the migrated home: 4/4 heal assertions pass, and the
same run fails heal-not-worse if the ladder regresses.
…they already run

The repair ladder is useless to anyone who does not know it exists, and the
two places that could announce it both cannot.

`self doctor` prints the nudge already -- but that only reaches someone who
already suspects something is wrong, and the whole point of this work is that
they should not have to. `self update` cannot print it at all: it runs the OLD
binary, which has never heard of any of this. A 0.4.69 client will never say
the words. So the only thing that can reach the cohort being migrated is the
NEW binary, on the next ordinary command.

Wired into `install`, `use` and `list`, through one helper so the wording and
the throttle live in one place. Deliberately narrow, because a nag in the
wrong place is worse than no nag: once per process, TTY only -- never into a
pipe, a log or a CI transcript -- and silent once the versions agree, which is
what makes a successful --fix turn it off for good rather than merely quieten
it. No network; the comparison is against a compiled-in constant and a field
the config already loaded.

`list` needed it on BOTH paths. The early return for "no packages installed in
current subos" is where the nudge matters most, not least: a home still
registered in the old format can report nothing installed while the payloads
sit on disk and the shims work. Found by testing rather than by reading -- the
first pty run fired the hint on `use` and stayed silent on `list`, because
`list` was taking exactly that branch.

Verified all three ways: fires on a pty when the recorded version is older,
silent on a pipe, silent when the versions match.
…ing state as payloads

Two things found by running the new --fix against a real home rather than by
reading the code.

--dry-run. The old --fix was network-free and side-effect-free; the repair
ladder ends that, so the promise is replaced with a narrower one rather than
dropped -- you can always see exactly what it would do first. The plan is
printed after the reinstallability probe has already run, so what it shows is
what would execute, not a guess at one. It does not stamp the reconciliation
marker, because nothing was reconciled.

The counter split. `broken` was incremented from two unrelated places: a
payload that does not resolve to an executable, and a binding-state finding
above Notice severity. The summary then called the total "broken payloads",
so a home whose only problem was an unregistered active version reported

    broken payloads  1
    hint  some payloads could not be repaired -- see the reasons above

with no payload finding in the list to explain the count, and a hint about a
repair pass that had never run. I hit this while trying to verify --dry-run
and spent a while looking for the missing entry. Two different defects need
two different counters; both still feed the exit code.

Also corrects the `--fix` policy comment at the top of doctor.cppm, which
still described the old "broken payload -> DO NOTHING" behaviour and the
reasoning for it. A policy comment that documents the opposite of the code is
worse than no comment.
What the release is for, in the order a user cares about: what changed for
them, why it was needed, and what the numbers actually were.

Leads with the half that did NOT break, because that was measured too and it
is the part people will assume broke: packages installed by 0.4.69 keep
working across the upgrade, gcc 15.1.0 <-> 16.1.0 still switches (verified
through the shim's own --version, not through use's exit code), and self
update costs 3.8s on a runner.

Records the acceptance numbers rather than claiming success: 1 -> 0 payload
findings, zero on the second pass, marker stamped. And why the result is
re-detected from a reloaded state file instead of trusting subprocess exit
codes -- on a real 0.4.69 home that distinction was 55 healed and one false
failure, the single entry whose cure was metadata-only.
The ladder shelled out to a bare `xlings install`, so the repair was performed
by whatever `xlings` resolves to on PATH. Normally that is the same binary
doing the diagnosing; it is not when doctor was started by absolute path --
which is exactly how the upgrade simulation runs a candidate build, so the
candidate would decide what to repair and the RELEASED client on PATH would
carry it out. The two can disagree about registration format, which is the
whole subject matter here.

The client is now a RepairPolicy field, defaulted to "xlings" so unit tests
stay deterministic and environment-free, and doctor fills it in from
platform::get_executable_path(). Passed through the same shell-safety check as
everything else before use -- an install path with a space or a quote in it
would otherwise be spliced into a command line.

Two tests pin it: every rung of the ladder uses the injected client, and so
does the reinstallability probe.
Three corrections, all in the direction of the code:

- The doc promised R3 would prompt unless --yes was passed. Dropped on
  implementation and the doc now says why: --fix is already an explicit
  request to repair, a prompt mid-pass cannot be answered in CI, and doctor
  renders through the event stream so the question would not reliably reach
  the user. --dry-run is the preview instead.
- Step table marked done, with S7 (--dry-run) added -- it was not in the
  original plan and only appeared once the 'never touches the network'
  promise had to be replaced with something narrower.
- Records the three things implementation found that design did not: target
  vs package, the reload before re-detect, and gating the stamp on the repair
  pass rather than on a spotless report.

A design doc that still describes the plan after the plan changed is worse
than no design doc -- the next person reads it as the contract.
E2E-16 S8 asserted that `--fix` must NOT repair a broken payload -- that it
only re-prints the remediation command and leaves the state untouched. That
was the old contract and this change deliberately reverses it, so the test
failed in CI by doing its job: `--fix` returned 0 because it had actually
repaired the payload.

Rewritten around what the repair is now promising:

- S8  `--fix --dry-run` prints the plan and changes NOTHING -- payload still
      absent, versions DB and workspace pointer intact, shim untouched, still
      exits non-zero. This is the promise that replaced "never touches the
      network", so it is pinned before the repair itself.
- S8b `--fix` repairs it and doctor is clean afterwards. The point is not that
      the command exits 0 but that a SEPARATE doctor run agrees -- a rung
      reporting success while the finding survives is the failure shape this
      codebase keeps producing, and here it would turn a still-broken home
      into `status OK`.
- S8c a second `--fix` has nothing to do. A ladder that repairs the same thing
      every run is looping rather than converging, and one pass cannot tell
      the difference.

The old S8c (user manually runs the hinted `xlings install`) is gone because
that is now what --fix does for them; its assertion that the installer
self-heals a DB-registered version with no payload is what S8b exercises.
Found by the ten-package upgrade simulation, which is the reason it was run
before the release rather than after.

`[email protected]` was reported as a broken payload on every pass and
survived every repair. It is a library-only package: xvm typed it `program`
by default, it has no executable, and reinstalling it can never change that.
The existing escape -- classify it as a release anchor -- did not catch it,
because `is_binding_root` skips the entry itself and gcc-runtime is the sole
member of its own release, so it came back false.

Left alone this is worse than one noisy line. `--fix` cannot clear it, the
repair pass therefore reports a failure, the migration marker is gated on that
pass, and so the marker never lands -- the hint telling the user to run
`self doctor --fix` would keep appearing after they had run it, forever, on
any home with gcc installed. That is the central promise of this release
failing on its most common input.

The discriminator is the payload, not the name: a package with NO executable
anywhere in its payload ships no program, while a payload that has other
executables and is missing the one we name is genuinely broken. The scan is
shallow (payload root + bin/) and confined to the failure path.

S8d pins the half that matters -- delete the binary, leave a sibling
executable next to it, and it must still be an error. Verified by mutation:
making the anchor rule unconditional fails S8d. Without that case the rule
would be unfalsifiable, because S7 deletes the whole payload directory and is
caught by an earlier check that never reaches this one.
The gcc-runtime misclassification is the reason the ten-package simulation was
run before the release rather than after, so the notes say what it was and
what it would have cost: not one noisy line, but the migration hint never
turning off on any home with gcc installed -- the central promise of the
release failing on its most common input.

Also replaces the acceptance numbers with the ten-package ones; the earlier
figures were from a two-package run.
The ten-package numbers had 'payload findings before: 0', which reads as
evidence of a repair and is not -- that home had already been migrated by the
run that found the gcc-runtime bug. Two measurements, each carrying half the
claim, now labelled as such: a fresh two-package home shows the repair
actually happening (1 -> 0, stamped), and the ten-package home shows nothing
is left over at scale and the pass is idempotent.
@Sunrisepeak
Sunrisepeak merged commit 616a8e0 into main Jul 27, 2026
7 checks passed
@Sunrisepeak
Sunrisepeak deleted the feat/self-repair branch July 27, 2026 23:16
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