Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ claim to cover them; `git log` and the PR history are the record for those.

| change | anchor | what you may have to do | why it was taken |
|---|---|---|---|
| **`cairn-capture` no longer HOLDS a session for a scope the routing table does not name** (on a host with more than one instance): it ships to the DEFAULT instance OWNER-ONLY (`*` declared), as a `*` session does. A session already bound to the DEFAULT instance is no longer withdrawn for it; one bound to ANOTHER instance is withdrawn from there and re-shipped whole to the default, owner-only — the `*` path (before, it was withdrawn and HELD). A scope the table EXPLICITLY routes to an instance this host does not configure is still held — the only routing hold left — and its `held_reason` now says so (`scope X is routed by the table to instance Y, which this host does not configure`). New mode **`--reevaluate-held [-apply]`** recovers the holds the old rule made | [#330](https://github.com/ZacxDev/cairn/pull/330) → `<UNFILLED>` | ⚠ **Sessions previously held stay held** until you run `--reevaluate-held` (read-only listing), then, with `-host`/`-worker`/`-capture-token-file`, `-apply`: it clears only holds whose destination answered the position probe; a 410 is listed `needs-server-restore` and left held — restore it by the server journal. The `routed back to …, which deleted this session's transcript` holds are never touched. ⚠ **If you match `held_reason` text**, the unconfigured-alias hold's wording changed. [`internal/capture/README.md`](internal/capture/README.md) step 3 and § *`--reevaluate-held`* | `scopeuse` reads header-shaped text anywhere in a transcript (test output, a source read, assistant prose) as a scope read, so a fixture name in a grep result held — and withdrew — whole sessions: measured on a two-instance host, 15 held this way, 14 on fixture names. An unnamed scope is a claim about no instance, so there is no leak to stop: [`internal/capture/route.go`](internal/capture/route.go)'s `Decide` |
| **`cairn-capture --clear-held` no longer clears a hold whose session was ever bound or WITHDRAWN, nor any empty-`instance` hold in a state written by an earlier agent.** The state keeps a per-session shipping history (`bound_ever`, `withdrawn_from`, `history_kept`); `--list-held` prints a sixth field, why a `permanent` hold is kept, and `--clear-held` prints it per kept hold. EVERY first binding is now position-probed (one request per new session; a 410 there holds the session), a withdrawal is recorded before it is sent, and a 410 on an instance the session was withdrawn from HOLDS it instead of marking it `deleted`. The hold reason is neutral (`410: withdrawn or deleted there`): restore by the server journal's reason. | [#324](https://github.com/ZacxDev/cairn/pull/324) → `<UNFILLED>` | ⚠ **Existing empty-`instance` holds become `permanent`** (a legacy state cannot prove they never shipped). List them: `cairn-capture --list-held \| awk -F'\t' '$3 == "permanent"'`. ⚠ **If you parse `--list-held`**, one trailing field is added; the first five keep their positions. [`internal/capture/README.md`](internal/capture/README.md) § *`held=` is not the backlog* | DATA LOSS: an empty `instance` was read as "never shipped", but a withdrawal empties it too. `--clear-held` cleared withdrawn sessions, the next pass re-sent them unprobed, the instance answered 410 (a withdrawal is final for its uploader), and each was marked `deleted` for good — measured: 49 sessions on one host |
| **`cairn-capture -foreign-root-base BASE -foreign-claude-root [OWNER=]DIR`** (repeatable, `--run`/`--dry-run`; BASE absolute and required, DIR strictly beneath it; Linux only): more Claude Code projects roots whose sessions ran on another filesystem (a container's volume). They ship like `-claude-root`'s, but their recorded `cwd` is never resolved on this host: one that recorded no scope is placed by OWNER as an `origin` owner (`-route-origin-owner`), else the default instance; each is resolved from BASE with no symlink followed in any component (openat2, or an O_NOFOLLOW walk) and held open for the run; an unreadable one is one `refused=`, an absent one is logged; an id already found under an earlier root is skipped. **Every root, `-claude-root` included, now refuses (logs, `refused=`) a session file or project directory that is a symlink, and never reads a file through one** | `<UNFILLED>` | If you keep session files under `-claude-root` as SYMLINKS, they are no longer captured: `find <root> -type l` first. Otherwise nothing unless you pass the flag | Capturing agents that run in containers from the host, without a client or network path in the container: [`internal/capture/README.md`](internal/capture/README.md) § *A session that recorded no scope* |
| **`cairn-capture`: a session already BOUND to an instance is DEFERRED while F1 is live, for at most 1 hour** (on a host with more than one instance): nothing of it is shipped and nothing withdrawn, counted on a NEW run-line field **`deferred=`** (not `refused=`), logged once when the window starts. The window's start is saved per session as `f1_since` in the state and cleared whenever F1 is false; once it expires with F1 still live, the session is routed exactly as before (F1's `*` forces the default instance, with the usual withdrawal), logged once. A NEW session is still placed on the default instance by F1. **`--dry-run` routes F1 as `--run` does**, reading `f1_since` read-only: `decision=deferred` inside the window, the base rule's answer after it | [#317](https://github.com/ZacxDev/cairn/pull/317) → `<UNFILLED>` | ⚠ **If you parse the run line**, `deferred=` is INSERTED after `export-skipped=`, inside the `cairn-capture: run …` line, so the four fields `--run` prints after the summary — `omitted=`, `recheck-omitted=`, `requests=`, `held-backlog=` — each shift one position right: a parser keyed on field NAMES is unaffected, a POSITIONAL one must update. ⚠ **A bound session's capture can lag by up to an hour** while F1 holds. ⚠ **Two residuals remain**: a session whose F1 stays live past the window is moved by the base rule and can still be held by a later move back (410); and once any ledger record lands, earlier output that had no ledger record of its own ships under that later scope, deferred or not (pre-existing T3). ⚠ **It recovers nothing already held**, and one an older build moved but has not yet held will be held when its ledger line lands — no supported operation un-deletes a root on an instance. [`internal/capture/README.md`](internal/capture/README.md) step 3 and § *A session deleted on its instance (410)* | Claude Code writes a `cairn` call's tool input before the client appends its ledger line, so a pass in between saw F1 and moved a repo-placed session to the default; the next pass's move back answered 410 and held it for good. Bounded because F1's evidence is sticky, so an unbounded deferral stopped some sessions' capture for good: [`internal/capture/f1move_test.go`](internal/capture/f1move_test.go), [`internal/capture/route.go`](internal/capture/route.go)'s `DecideRun` |
Expand Down
17 changes: 16 additions & 1 deletion claudedocs/plan-cairn-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,23 @@ script that prints only counts.
wrote `alpha-notes` (personal) and read `beta-notes` (client) in its first turn → held, 0
requests to either;
- `V` holds `*` or an unroutable name with more than one instance configured → held;
*🔴 Changed by operator decisions — the code (`internal/capture/route.go`, `Decide`) is the
authority, and this bullet and the two-instance one above are SUPERSEDED: `*` and a session
whose scopes route to two instances are placed on the DEFAULT instance, OWNER-ONLY (`*`
declared), not held; and (**2026-12-03**) so is a scope the routing table does NOT NAME at all.
Reason for the 2026-12-03 change: `scopeuse` reads header-shaped text anywhere in a transcript
(grep and test output, a source read, assistant prose) as a scope read, so a fixture name in a
test's output held a session for good — and holding an already-shipped session WITHDREW it
first. Measured on a two-instance host: 15 sessions held this way, 14 on fixture names
(`alpha-notes`, `a`, `b`, `checkout-service`), one on a real scope registered later, and one
live session withdrawn from the default instance. An unnamed scope is a claim about no
instance, so there is no cross-instance leak to stop — only a reader to narrow, which
owner-only does. Existing holds are recovered by `cairn-capture --reevaluate-held`
(`internal/capture/README.md`).*
- a scope the routing table EXPLICITLY sends to an alias this host has no config for → held, at
any instance count: the table says the scope lives elsewhere (S2 review round 1);
any instance count: the table says the scope lives elsewhere (S2 review round 1). *Since
2026-12-03 this is the ONLY routing hold left; its reason reads `scope X is routed by the table
to instance Y, which this host does not configure`.*
- `V` empty (O10) → the DEFAULT instance, where decision 4 makes it owner-only.
- 🔴 **Capture is incremental (60 s), so a prefix may ALREADY be on an instance when the rule
changes its answer.** When a run computes an answer other than the instance holding the
Expand Down
118 changes: 116 additions & 2 deletions cmd/cairn-capture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
// [-host-key FILE] [-allow-plaintext-host [ALIAS=]HOST]... [the --dry-run reading flags]
// cairn-capture --list-held [-state FILE]
// cairn-capture --clear-held [-state FILE]
// cairn-capture --reevaluate-held [-state FILE] [-claude-root DIR] [-host LABEL -worker [ALIAS=]URL -capture-token-file [ALIAS=]FILE]... [-apply]
//
// `--reevaluate-held` is the one-off recovery of the holds the pre-#330 rule made for a scope
// the routing table does not name (`capture.Agent.ReevaluateHeld`): read-only unless `-apply`, and
// `-apply` clears only a hold whose destination answered the position probe — a 410 is reported
// `needs-server-restore` and left held.
//
// 🔴 `held` IS FINAL, SO `--list-held` AND `--clear-held` EXIST: the run line's `held=` counts the
// holds ONE PASS MADE, and a held session is skipped before routing on every later pass, so a
Expand Down Expand Up @@ -129,6 +135,11 @@ func run(args []string, stdout, stderr io.Writer, getenv func(string) string) in
"owner were OWNER (-route-origin-owner), else on the default instance. No file beneath it is read through a "+
"symlink, and a root whose own path runs through one is refused; an unreadable root refuses only itself, an "+
"absent one is logged (repeatable; no default)")
reeval := fs.Bool("reevaluate-held", false, "list the holds the pre-#330 rule made for a scope the routing table "+
"does not name, re-decided under the current rule (and, given -host/-worker/-capture-token-file, probed at their "+
"destination); writes nothing without -apply")
apply := fs.Bool("apply", false, "--reevaluate-held only: clear the holds whose destination answered the probe with a "+
"position (needs -host, -worker and -capture-token-file; under the run lock)")
fs.Var(&ocDirs, "opencode-project", "an opencode project directory to enumerate (repeatable; `session list` lists ONE project's roots)")
fs.Var(&workers, "worker", "--run: [ALIAS=]URL of an instance's worker listener (repeatable; no alias is the default instance)")
fs.Var(&tokenFiles, "capture-token-file", "--run: [ALIAS=]FILE holding that instance's capture token, mode 0600 (repeatable)")
Expand All @@ -149,14 +160,17 @@ func run(args []string, stdout, stderr io.Writer, getenv func(string) string) in
return exitUsage
}
modes := 0
for _, on := range []bool{*selfTest, *dryRun, *runOnce, *listHeld, *clearHeld} {
for _, on := range []bool{*selfTest, *dryRun, *runOnce, *listHeld, *clearHeld, *reeval} {
if on {
modes++
}
}
switch {
case modes != 1:
fmt.Fprintln(stderr, "cairn-capture: name exactly one of --run, --dry-run, --list-held, --clear-held or --self-test")
fmt.Fprintln(stderr, "cairn-capture: name exactly one of --run, --dry-run, --list-held, --clear-held, --reevaluate-held or --self-test")
return exitUsage
case *apply && !*reeval:
fmt.Fprintln(stderr, "cairn-capture: -apply is for --reevaluate-held only")
return exitUsage
case *selfTest:
return redact.SelfTest(stdout, *seed)
Expand All @@ -180,6 +194,10 @@ func run(args []string, stdout, stderr io.Writer, getenv func(string) string) in
if *clearHeld {
return clearHeldSessions(*statePath, stateHome, stdout, stderr)
}
if *reeval {
return reevaluateHeld(runConfig{host: *host, state: *statePath, maxRequest: *maxRequest, workers: workers,
tokenFiles: tokenFiles, plaintext: plaintext, stateHome: stateHome, claudeRoot: *claudeRoot}, *apply, stdout, stderr, getenv)
}
// 🔴 --dry-run WRITES NOTHING, THE HOST KEY INCLUDED. It prints counts by rule, never a tag, so
// an ephemeral in-memory key is all it needs; creating the persistent key file as a side effect
// of a read-only mode was a review finding (round 1).
Expand Down Expand Up @@ -556,6 +574,102 @@ func clearHeldSessions(path, stateHome string, stdout, stderr io.Writer) int {
return exitOK
}

// reevaluateHeld is `--reevaluate-held`: one line per held session, tab-separated
// `<outcome> <root> <runtime> <history-kept|legacy> <target|-> <owner-only|-> <bytes|-> <held reason> <detail>`,
// then the counts on stderr. Without `-apply` it writes NOTHING — the state is opened read-only, no
// lock is taken — and probes only when given `-worker`s (a probe stores nothing on the instance).
//
// 🔴 `-apply` PROBES EVERY CANDIDATE FIRST, AND REFUSES TO RUN WITHOUT THE ENDPOINTS TO DO SO. Every
// hold this mode exists for was legacy — no record of whether it was withdrawn — so the state cannot
// prove a clear safe; only the destination's answer can. A 410 is NOT cleared: cleared, the next pass
// would probe, be answered 410 and hold it again, silently, as `410: withdrawn or deleted there`.
func reevaluateHeld(c runConfig, apply bool, stdout, stderr io.Writer, getenv func(string) string) int {
routing, err := client.Discover(getenv)
if err != nil {
fmt.Fprintf(stderr, "cairn-capture: %v\n", err)
return exitError
}
p := captureStatePath(c.state, c.stateHome)
var sink capture.Sink
var up map[string]bool
if len(c.workers) > 0 || len(c.tokenFiles) > 0 {
if c.host == "" || strings.ContainsAny(c.host, " \t\r\n/") {
fmt.Fprintln(stderr, "cairn-capture: probing needs -host, the label this host's capture token was minted for")
return exitUsage
}
eps, _, _, err := endpoints(c, routing)
if err != nil {
fmt.Fprintf(stderr, "cairn-capture: %v\n", err)
return exitUsage
}
sink, up = &capture.HTTPSink{Host: c.host, Endpoints: eps, MaxRequest: c.maxRequest, Log: stderr}, uploadable(eps)
}
if apply && sink == nil {
fmt.Fprintln(stderr, "cairn-capture: --reevaluate-held -apply needs -host, -worker and -capture-token-file: "+
"no hold is cleared without its destination's answer (a legacy hold may have been withdrawn there)")
return exitUsage
}
if apply {
release, err := capture.LockState(p)
if errors.Is(err, capture.ErrRunning) {
fmt.Fprintln(stderr, "cairn-capture: another run holds the lock; nothing cleared")
return exitError
}
if err != nil {
fmt.Fprintf(stderr, "cairn-capture: %v\n", err)
return exitError
}
defer release()
}
state, err := capture.LoadState(p)
if err != nil {
fmt.Fprintf(stderr, "cairn-capture: %v\n", err)
return exitError
}
agent := &capture.Agent{State: state, ClaudeRoot: c.claudeRoot, Routing: routing, Uploadable: up, Sink: sink, Log: stderr}
rs := agent.ReevaluateHeld()
counts := map[string]int{}
for _, r := range rs {
counts[r.Outcome]++
hist, target, owner, size := "legacy", "-", "-", "-"
if r.HistoryKept {
hist = "history-kept"
}
if r.Target != "" {
target = r.Target
}
if r.OwnerOnly {
owner = "owner-only"
}
if r.Bytes >= 0 {
size = fmt.Sprint(r.Bytes)
}
fmt.Fprintf(stdout, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", r.Outcome, r.Root, r.Runtime, hist, target, owner, size, r.Reason, r.Detail)
}
line := fmt.Sprintf("held=%d", len(rs))
for _, o := range []string{capture.ReevalShip, capture.ReevalUnprobed, capture.ReevalNeedsRestore, capture.ReevalProbeFailed,
capture.ReevalStillHeld, capture.ReevalExcluded} {
line += fmt.Sprintf(" %s=%d", o, counts[o])
}
if !apply {
fmt.Fprintf(stderr, "cairn-capture: %s — dry run: nothing was changed (-apply clears the `ship` ones)\n", line)
return exitOK
}
cleared := state.ApplyReevaluation(rs)
if len(cleared) > 0 {
if err := state.Save(p); err != nil {
fmt.Fprintf(stderr, "cairn-capture: %v\n", err)
return exitError
}
}
for _, r := range cleared {
fmt.Fprintf(stdout, "cleared\t%s\t%s\t%s\n", r.Root, r.Runtime, r.Target)
}
fmt.Fprintf(stderr, "cairn-capture: %s cleared=%d — the next pass reads the cleared sessions again; "+
"a needs-server-restore one is left held\n", line, len(cleared))
return exitOK
}

// runPass is `--run` itself: one pass, under the run lock, saving as it goes.
func runPass(c runConfig, stderr io.Writer, getenv func(string) string) int {
fail := func(err error) int {
Expand Down
Loading
Loading