Key the limit/active-account markers and spend attribution by token fingerprint, not host letter A/B #200

Closed
opened 2026-10-15 02:59:07 +00:00 by ben · 3 comments
Owner

Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against main at 3e50274.

Symptom

limit-lib.sh:90-91,185-187 and run-swarm.sh:267: the limit marker and the active-account marker are host-global and repo-agnostic, and store only the letter A or B. limit-lib.sh:198-200 therefore forbids a repo-level Claude token override ("must never return") — because repo X exhausting ITS account would park every repo on the host and X's failover would flip Y's letter. swarm-db.py:94 stores only the letter; record-run-result.sh:45-47 reads the marker AFTER the run, racing a mid-run failover (earlier iterations misattributed). The TUI reads the letter (tui/data/identity.py:168-169).

This blocks per-project tokens (Forgejo repo-level secrets override org secrets by name) and makes per-device attribution guesswork.

Proposed change

  • Fingerprint fp = sha256(token)[:12], computed in claude_select_token, never the token itself.
  • Markers become /…/matou-swarm-claude-limit-<fp> and …-active-token-<fp-pair>; claude_limit_parked consults the marker for the token THIS caller would use.
  • spend.account gains a sibling account_fp; record-run-result.sh captures the fp per iteration from the worker log (the shim can print claude-account-fp=<fp> as a protocol line) rather than reading the marker after the run.
  • swarm_git_identity callers add a commit trailer Factory-Account: <fp> so attribution survives into git.
  • Retire the limit-lib.sh:198-200 prohibition on repo-level tokens once the above lands; document how a repo declares its own token (repo-level CLAUDE_CODE_OAUTH_TOKEN secret, or repo-env for cron legs).
  • TUI/sampler readers updated; swarm.db migration additive (ADR 0004 point 5).

Acceptance

  • tests/limit-lib-test.sh: two tokens on one host park independently.
  • tests/swarm-db-test.sh: spend rows carry fp; limit-lost groups by fp.
  • tests/record-run-result-test.sh: a mid-run failover attributes iterations to two different fps.

depends_on

#198

_Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against `main` at 3e50274._ ## Symptom `limit-lib.sh:90-91,185-187` and `run-swarm.sh:267`: the limit marker and the active-account marker are host-global and repo-agnostic, and store only the letter A or B. `limit-lib.sh:198-200` therefore forbids a repo-level Claude token override ("must never return") — because repo X exhausting ITS account would park every repo on the host and X's failover would flip Y's letter. `swarm-db.py:94` stores only the letter; `record-run-result.sh:45-47` reads the marker AFTER the run, racing a mid-run failover (earlier iterations misattributed). The TUI reads the letter (`tui/data/identity.py:168-169`). This blocks per-project tokens (Forgejo repo-level secrets override org secrets by name) and makes per-device attribution guesswork. ## Proposed change - Fingerprint `fp = sha256(token)[:12]`, computed in `claude_select_token`, never the token itself. - Markers become `/…/matou-swarm-claude-limit-<fp>` and `…-active-token-<fp-pair>`; `claude_limit_parked` consults the marker for the token THIS caller would use. - `spend.account` gains a sibling `account_fp`; `record-run-result.sh` captures the fp per iteration from the worker log (the shim can print `claude-account-fp=<fp>` as a protocol line) rather than reading the marker after the run. - `swarm_git_identity` callers add a commit trailer `Factory-Account: <fp>` so attribution survives into git. - Retire the `limit-lib.sh:198-200` prohibition on repo-level tokens once the above lands; document how a repo declares its own token (repo-level `CLAUDE_CODE_OAUTH_TOKEN` secret, or `repo-env` for cron legs). - TUI/sampler readers updated; swarm.db migration additive (ADR 0004 point 5). ## Acceptance - `tests/limit-lib-test.sh`: two tokens on one host park independently. - `tests/swarm-db-test.sh`: spend rows carry fp; `limit-lost` groups by fp. - `tests/record-run-result-test.sh`: a mid-run failover attributes iterations to two different fps. ## depends_on #198

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime.

  • Ruling: ready-for-agent.
  • Why this is a two-way door: fingerprinting the markers and spend by sha256(token)[:12] is pure attribution/telemetry over an existing token — no new secret is provisioned, the token itself is never stored, and the DB migration is additive (ADR 0004 point 5). The one one-way bullet — retiring the deliberately-removed repo-level-token prohibition — is scoped OUT and deferred to #199's human decision.
  • What proves it: tests/limit-lib-test.sh (two tokens on one host park independently), the swarm-db test (spend rows carry the fingerprint; limit-lost groups by it), and the record-run-result test (a mid-run failover attributes iterations to two fingerprints).

Agent Brief

Category: enhancement

Summary: Key the limit marker, the active-account marker, and spend attribution by a token fingerprint (sha256(token)[:12], never the token) instead of the host letter A/B, and capture the fingerprint per iteration from the worker log rather than reading a host-global marker after the run.

Current behavior: The limit and active-account markers are host-global, repo-agnostic, and store only the letter A or B; spend stores the same letter; and the post-run recorder reads the marker once after the sandbox exits, misattributing a mid-run failover. This also forces the host-global prohibition on repo-level token overrides.

Desired behavior: Compute a fingerprint at token-selection time and name the markers by fingerprint, so the limit-parked check consults the marker for the token THIS caller would use. Spend gains a sibling fingerprint column alongside the account letter, captured per iteration from a protocol line the worker log emits (so attribution is per-iteration, not a racy post-run marker read). Git identity callers add a Factory-Account: <fp> commit trailer so attribution survives into history. The DB migration is additive; TUI/sampler readers are updated. The letter-less-marker distrust (#788) and sticky-account ruling (2026-08-26) are preserved.

Key interfaces:

  • Fingerprint = sha256(token)[:12], computed where the token is selected; the token is never stored.
  • Marker names keyed by fingerprint (limit) and fingerprint-pair (active); the limit-parked check consults the caller's own token marker.
  • spend gains an account_fp column (additive); limit-lost groups by fingerprint.
  • Per-iteration fingerprint captured from a worker-log protocol line, not a post-run marker read.
  • A Factory-Account: <fp> commit trailer on identity-stamped commits.

Acceptance criteria:

  • Two tokens on one host park independently (limit-lib test).
  • Spend rows carry the fingerprint and limit-lost groups by fingerprint (swarm-db test).
  • A mid-run failover attributes iterations to two different fingerprints (record-run-result test).
  • Migration is additive; letter-less-marker distrust and sticky-account behavior preserved.

Out of scope: Retiring the repo-level-token prohibition and documenting a repo-declared token — that re-enablement is a security-posture decision governed by #199's human ruling and is NOT part of this issue. Delivering the token as a file (#198, prerequisite/dependency).

> *This was generated by AI during triage.* **Ruled by agent under ADR 0174 — veto anytime.** - **Ruling:** `ready-for-agent`. - **Why this is a two-way door:** fingerprinting the markers and spend by `sha256(token)[:12]` is pure attribution/telemetry over an existing token — no new secret is provisioned, the token itself is never stored, and the DB migration is additive (ADR 0004 point 5). The one one-way bullet — retiring the deliberately-removed repo-level-token prohibition — is scoped OUT and deferred to #199's human decision. - **What proves it:** `tests/limit-lib-test.sh` (two tokens on one host park independently), the swarm-db test (spend rows carry the fingerprint; `limit-lost` groups by it), and the record-run-result test (a mid-run failover attributes iterations to two fingerprints). ## Agent Brief **Category:** enhancement **Summary:** Key the limit marker, the active-account marker, and spend attribution by a token fingerprint (`sha256(token)[:12]`, never the token) instead of the host letter A/B, and capture the fingerprint per iteration from the worker log rather than reading a host-global marker after the run. **Current behavior:** The limit and active-account markers are host-global, repo-agnostic, and store only the letter A or B; spend stores the same letter; and the post-run recorder reads the marker once after the sandbox exits, misattributing a mid-run failover. This also forces the host-global prohibition on repo-level token overrides. **Desired behavior:** Compute a fingerprint at token-selection time and name the markers by fingerprint, so the limit-parked check consults the marker for the token THIS caller would use. Spend gains a sibling fingerprint column alongside the account letter, captured per iteration from a protocol line the worker log emits (so attribution is per-iteration, not a racy post-run marker read). Git identity callers add a `Factory-Account: <fp>` commit trailer so attribution survives into history. The DB migration is additive; TUI/sampler readers are updated. The letter-less-marker distrust (#788) and sticky-account ruling (2026-08-26) are preserved. **Key interfaces:** - Fingerprint = `sha256(token)[:12]`, computed where the token is selected; the token is never stored. - Marker names keyed by fingerprint (limit) and fingerprint-pair (active); the limit-parked check consults the caller's own token marker. - `spend` gains an `account_fp` column (additive); `limit-lost` groups by fingerprint. - Per-iteration fingerprint captured from a worker-log protocol line, not a post-run marker read. - A `Factory-Account: <fp>` commit trailer on identity-stamped commits. **Acceptance criteria:** - [ ] Two tokens on one host park independently (limit-lib test). - [ ] Spend rows carry the fingerprint and `limit-lost` groups by fingerprint (swarm-db test). - [ ] A mid-run failover attributes iterations to two different fingerprints (record-run-result test). - [ ] Migration is additive; letter-less-marker distrust and sticky-account behavior preserved. **Out of scope:** Retiring the repo-level-token prohibition and documenting a repo-declared token — that re-enablement is a security-posture decision governed by #199's human ruling and is NOT part of this issue. Delivering the token as a file (#198, prerequisite/dependency).

swarm-claim host=matou-dev-server-01 run=21840
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=21840 (automated multi-host claim — lowest live claim id works this ticket)

close-report for #200 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 200,
  "status": "success",
  "commits": [
    "46be5407229f8a173585476f38c0ce7fac558f45"
  ],
  "changed_files": [
    "STATUS.md",
    "claude-fence",
    "docs/journal/2026-10-15-200.md",
    "limit-lib.sh",
    "record-run-result.sh",
    "run-swarm.sh",
    "swarm-db-lib.sh",
    "swarm-db.py",
    "tests/claude-fence-test.sh",
    "tests/execute-lib-test.sh",
    "tests/limit-lib-test.sh",
    "tests/record-run-result-test.sh",
    "tests/run-triage-test.sh",
    "tests/swarm-db-test.sh",
    "tui/data/limits.py",
    "tui/tests/test_limits.py"
  ],
  "tests": [
    {
      "command": "bash tests/limit-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/swarm-db-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/record-run-result-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/claude-fence-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/execute-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-triage-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/tui-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/docs-budget-test.sh",
      "exit_code": 0
    },
    {
      "command": "full offline suite (tests/*.sh + onboarding/tests/*.sh)",
      "exit_code": 0
    }
  ],
  "criteria": {
    "tests/limit-lib-test.sh: two tokens on one host park independently": "tests/limit-lib-test.sh new '#200: two tokens on one host park INDEPENDENTLY' group — A parks on <base>-<fpA>, B (window still good) reads NOT parked, then parks on <base>-<fpB>; limit-lib.sh claude_limit_marker_path/claude_active_fp",
    "tests/swarm-db-test.sh: spend rows carry fp; limit-lost groups by fp": "swarm-db.py additive spend.account_fp column + --account-fp; q_limit_lost groups by fp=<fp> (account fallback); tests/swarm-db-test.sh new account_fp verbatim/NULL/migration + 'limit-lost groups by fingerprint' group",
    "tests/record-run-result-test.sh: a mid-run failover attributes iterations to two different fps": "record-run-result.sh parses per-iteration claude-account-fp=<fp> from stdout (positional zip) into --account-fp; tests/record-run-result-test.sh new '#200: a mid-run failover' group — issue 301=fpA, 302=fpB, 303=NULL",
    "Migration is additive; letter-less-marker distrust and sticky-account behavior preserved": "ADDITIVE_COLUMNS gains (spend,account_fp,TEXT) guarded by table_info; swarm-db-test migration test covers account_fp on a pre-existing table reading NULL; all fp additions conditional on non-empty fp so token-less path is byte-identical — the untouched limit-lib distrust + sticky failover test groups (24 total) stay green"
  },
  "summary": "Keyed the Claude limit/active markers and spend attribution by token fingerprint (sha256(token)[:12]) instead of host letter A/B, additively and backward-compatibly, with per-iteration fp capture.",
  "reason": "",
  "blockers": [],
  "no_code_change": ""
}
**close-report** for #200 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 200, "status": "success", "commits": [ "46be5407229f8a173585476f38c0ce7fac558f45" ], "changed_files": [ "STATUS.md", "claude-fence", "docs/journal/2026-10-15-200.md", "limit-lib.sh", "record-run-result.sh", "run-swarm.sh", "swarm-db-lib.sh", "swarm-db.py", "tests/claude-fence-test.sh", "tests/execute-lib-test.sh", "tests/limit-lib-test.sh", "tests/record-run-result-test.sh", "tests/run-triage-test.sh", "tests/swarm-db-test.sh", "tui/data/limits.py", "tui/tests/test_limits.py" ], "tests": [ { "command": "bash tests/limit-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/swarm-db-test.sh", "exit_code": 0 }, { "command": "bash tests/record-run-result-test.sh", "exit_code": 0 }, { "command": "bash tests/claude-fence-test.sh", "exit_code": 0 }, { "command": "bash tests/execute-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/run-triage-test.sh", "exit_code": 0 }, { "command": "bash tests/tui-test.sh", "exit_code": 0 }, { "command": "bash tests/docs-budget-test.sh", "exit_code": 0 }, { "command": "full offline suite (tests/*.sh + onboarding/tests/*.sh)", "exit_code": 0 } ], "criteria": { "tests/limit-lib-test.sh: two tokens on one host park independently": "tests/limit-lib-test.sh new '#200: two tokens on one host park INDEPENDENTLY' group — A parks on <base>-<fpA>, B (window still good) reads NOT parked, then parks on <base>-<fpB>; limit-lib.sh claude_limit_marker_path/claude_active_fp", "tests/swarm-db-test.sh: spend rows carry fp; limit-lost groups by fp": "swarm-db.py additive spend.account_fp column + --account-fp; q_limit_lost groups by fp=<fp> (account fallback); tests/swarm-db-test.sh new account_fp verbatim/NULL/migration + 'limit-lost groups by fingerprint' group", "tests/record-run-result-test.sh: a mid-run failover attributes iterations to two different fps": "record-run-result.sh parses per-iteration claude-account-fp=<fp> from stdout (positional zip) into --account-fp; tests/record-run-result-test.sh new '#200: a mid-run failover' group — issue 301=fpA, 302=fpB, 303=NULL", "Migration is additive; letter-less-marker distrust and sticky-account behavior preserved": "ADDITIVE_COLUMNS gains (spend,account_fp,TEXT) guarded by table_info; swarm-db-test migration test covers account_fp on a pre-existing table reading NULL; all fp additions conditional on non-empty fp so token-less path is byte-identical — the untouched limit-lib distrust + sticky failover test groups (24 total) stay green" }, "summary": "Keyed the Claude limit/active markers and spend attribution by token fingerprint (sha256(token)[:12]) instead of host letter A/B, additively and backward-compatibly, with per-iteration fp capture.", "reason": "", "blockers": [], "no_code_change": "" } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Matou/dev-factory#200
No description provided.