Skip to content

feat(bin): add quota-balanced Claude account profiles - #5288

Open
MLA82 wants to merge 2 commits into
kunchenguid:mainfrom
MLA82:fm/firstmate-claude-dual-account-routing
Open

MLA82 wants to merge 2 commits into
kunchenguid:mainfrom
MLA82:fm/firstmate-claude-dual-account-routing

Conversation

@MLA82

@MLA82 MLA82 commented Sep 22, 2026

Copy link
Copy Markdown

Intent

Den zweiten Claude-Account [email protected] dauerhaft und parallel zum bestehenden Claude-Account in Firstmate integrieren, ohne wiederholtes Ab- und Anmelden. Die ganze Flotte soll beide Accounts kennen, bei jedem neuen Claude-Arbeiter beide Kontingente sauber in die Auswahl einbeziehen und den Account mit der besseren tatsächlichen Restkapazität so wählen, dass beide Kontingente sinnvoll und möglichst gleichmäßig genutzt werden. Ein laufender Auftrag darf niemals still auf den anderen Account wechseln. Keine Tokens kopieren oder bestehende Profile überschreiben.

What Changed

  • Added bin/fm-claude-profile-lib.sh, a new library that validates config/claude-profiles.json, measures each declared Claude account directory in isolation via quota-axi --provider claude --profile-only --json, and selects the profile with the best applicable spendPriority (ties broken lexically by name); exhausted profiles are excluded and a single invalid/unreachable/unauthenticated profile refuses the whole selection rather than being skipped.
  • Wired the new library into bin/fm-spawn.sh: fresh Claude launches select a profile from current isolated quota, relaunches and secondmate recoveries instead re-validate and reuse the previously bound claude_profile/claude_config_dir from task metadata (never reselecting), a legacy task with no prior binding keeps the historical ambient CLAUDE_CONFIG_DIR behavior, and switching harness away from and back to Claude clears the binding and reselects fresh. The chosen store is exported as CLAUDE_CONFIG_DIR for both trust registration and the launched worker.
  • Propagated the account binding through remote secondmate routing (bin/fm-remote-secondmate-control.sh now reports claude_profile=/claude_config_dir= in print_route) and through local config inheritance (bin/fm-config-inherit-lib.sh adds claude-profiles.json to FM_INHERITABLE_CONFIG so secondmate homes inherit the same account references without copying credentials).
  • Documented the new schema and selection contract in docs/configuration.md ("Claude account profiles"), cross-referenced it from docs/architecture.md, AGENTS.md, and .agents/skills/harness-adapters/references/harness/claude.md, and recorded isolated-measurement verification evidence in docs/verification/dispatch-auth.md.
  • Added/extended tests: tests/fm-spawn-dispatch-profile.test.sh (new, covers per-profile quota calls, best-capacity selection, deterministic ties, durable metadata, trust registration, unknown-quota handling, and absent-config backward compatibility), tests/fm-control-relaunch.test.sh (new, proves a bound assignment is not reconsidered on relaunch even if another account later has more capacity), and tests/fm-secondmate-harness.test.sh (updated for the inherited reference-only profile declaration).

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The fix round's change is a minimal, well-targeted one-line gate (bin/fm-spawn.sh:3185) plus a matching doc update and a genuine behavioral regression test; it fully closes the previously flagged stale-binding gap (verified against the meta-write path at bin/fm-spawn.sh:4545-4546 and preserve_relaunch_meta at bin/fm-spawn.sh:4524-4531, which now omit/clear claude_profile and claude_config_dir when HARNESS != claude) without introducing new scope or defects.

Testing

Ran the three test suites that exercise this change's product surface (fm-spawn.sh's Claude account-profile selection/binding and fm-control.sh's relaunch/harness-switch handling) as real subprocesses against an isolated FM_HOME with faked external binaries (tmux, quota-axi) — the standard and only practical way to drive this bash CLI orchestrator end-to-end; all passed. To confirm the review-fix commit (a288f15) genuinely fixes the flagged stale-binding defect rather than the test being coincidentally green, I temporarily reverted bin/fm-spawn.sh's one-line guard to its pre-fix form and reran the suite: the new regression test failed exactly as the review finding predicted (binding carried into a non-Claude harness), then restored the fix and confirmed all tests pass again with a clean worktree. No live Herdr-lab session was needed since this change's logic is entirely in meta-file bookkeeping and quota-selection bash logic, not Herdr pane/TUI rendering, and the existing tests already drive the real fm-spawn.sh/fm-control.sh binaries as an end user would invoke them.

  • Live validation: ✅ go - 9 of 9 scenarios driven live against the product
Scenario Result Live Evidence
Fresh Claude spawn selects the account with better real remaining capacity ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_choose_best_isolated_quota_and_bind_metadata — real fm-spawn.sh subprocess measures two profiles in isolation via faked quota-axi and bi…
Equal-quota tie-break is deterministic by profile name, independent of declaration order ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_break_equal_quota_by_name_not_array_order
An account with unmeasurable quota stays eligible-but-uncertain and a rankable account can still win ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_keep_unmeasurable_account_as_uncertainty
Malformed, unreadable, or unauthenticated profile entries refuse the launch rather than silently falling back to another account (no silent account substitution) ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_refuse_unsafe_or_unauthenticated_entries
A running/relaunched Claude task reuses its already-bound account without re-measuring quota (a live task never silently switches accounts mid-run) ✅ pass live tests/fm-control-relaunch.test.sh — 'Claude relaunch validates and reuses its persisted account binding without reselection'
Switching a task's harness away from Claude clears the stale claude_profile/claude_config_dir binding instead of carrying it into the non-Claude meta record (the review-flagged defect fixed in commit… ✅ pass live tests/fm-control-relaunch.test.sh :: test_harness_switch_away_and_back_reselects_claude_profile_by_quota (first half); additionally verified as a genuine regression test by reverting bin/fm-spawn.sh t…
Switching a task's harness back to Claude after an intervening non-Claude leg reselects fresh from current isolated quota across both accounts, rather than silently reusing whatever account was bound… ✅ pass live tests/fm-control-relaunch.test.sh :: test_harness_switch_away_and_back_reselects_claude_profile_by_quota (second half) — asserts both profiles are freshly measured (quota_log has 2 entries, one per…
With no config/claude-profiles.json declared, spawn/relaunch preserves the historical single-account CLAUDE_CONFIG_DIR forwarding byte-for-byte (existing single-account operators are unaffected) ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_no_profile_keeps_claude_profile_defaults, test_claude_forwards_firstmate_config_dir_when_set, test_claude_omits_config_dir_prefix_when_unset
config/claude-profiles.json is inherited into secondmate homes so the whole fleet knows both accounts, including propagation, convergence on change, and absence-mirroring ✅ pass live tests/fm-secondmate-harness.test.sh :: B1 propagate_inheritable_config suite (lines ~304-366) explicitly exercises config/claude-profiles.json copy, change-convergence, and absence-mirroring into a se…

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ bin/fm-spawn.sh:3177 - Per docs/configuration.md ("Claude account profiles") this is documented as deliberate: 'Switching an existing non-Claude task to Claude selects and records an account once; switching away preserves the binding so a later switch back returns to the same account.' Concretely: task T spawns fresh on Claude, config/claude-profiles.json has two accounts, quota-based selection binds T to profile 'work' (meta: harness=claude, claude_profile=work). User runs fm-control.sh T relaunch --harness codex (fm-control.sh:913 always passes --harness explicitly on relaunch, and its own comment at line 51 calls harness-switching 'one ordinary use' of relaunch) — this is a fully supported, reachable, ordinary path. At bin/fm-spawn.sh:3186 CLAUDE_PROFILE_NAME is read unconditionally from the pre-relaunch meta (still 'work') before the HARNESS gate at line 3194; because HARNESS is now 'codex', that gate is skipped and CLAUDE_PROFILE_NAME is never cleared, so it is written back into the new meta record (line 4545) even though this task is not running Claude at all. Later, fm-control.sh T relaunch --harness claude reads RELAUNCH_PRIOR_HARNESS='codex' (not 'claude'), but CLAUDE_PROFILE_NAME is still 'work' from the carried-over field, so bin/fm-claude-profile-lib.sh's fm_claude_profile_validate_binding is used instead of fm_claude_profile_select — this brand-new Claude worker reuses whatever account was bound before the codex interlude with NO fresh quota comparison, even though the other account's quota may now be far better. This is reachable during the change's own documented ordinary usage (not a hypothetical path), and it works against the required criterion 'bei jedem neuen Claude-Arbeiter beide Kontingente sauber in die Auswahl einbeziehen und den Account mit der besseren tatsächlichen Restkapazität so wählen ... möglichst gleichmäßig genutzt' — this relaunch is starting a new Claude worker process (the prior Claude worker for this task already exited when it was relaunched onto codex), yet it does not get the quota-based reselection the intent requires for every new Claude worker. The same code path (bin/fm-spawn.sh:3180-3183) is also reachable via non-relaunch secondmate recovery spawns that pass a different explicit harness than the one recorded. Whether task-id identity should durably pin the Claude account across an intervening non-Claude harness leg, or whether that counts as a fresh Claude-worker assignment requiring reselection, is a product-behavior decision already made and documented by the author one way; flagging so the author can confirm it against the stated intent rather than resolving it unilaterally, since the fix (clearing the binding fields when this launch's HARNESS != claude) would remove the currently-documented 'switching away preserves the binding' behavior.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 9 of 9 scenarios driven live against the product
Scenario Result Live Evidence
Fresh Claude spawn selects the account with better real remaining capacity ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_choose_best_isolated_quota_and_bind_metadata — real fm-spawn.sh subprocess measures two profiles in isolation via faked quota-axi and bi…
Equal-quota tie-break is deterministic by profile name, independent of declaration order ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_break_equal_quota_by_name_not_array_order
An account with unmeasurable quota stays eligible-but-uncertain and a rankable account can still win ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_keep_unmeasurable_account_as_uncertainty
Malformed, unreadable, or unauthenticated profile entries refuse the launch rather than silently falling back to another account (no silent account substitution) ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_claude_profiles_refuse_unsafe_or_unauthenticated_entries
A running/relaunched Claude task reuses its already-bound account without re-measuring quota (a live task never silently switches accounts mid-run) ✅ pass live tests/fm-control-relaunch.test.sh — 'Claude relaunch validates and reuses its persisted account binding without reselection'
Switching a task's harness away from Claude clears the stale claude_profile/claude_config_dir binding instead of carrying it into the non-Claude meta record (the review-flagged defect fixed in commit… ✅ pass live tests/fm-control-relaunch.test.sh :: test_harness_switch_away_and_back_reselects_claude_profile_by_quota (first half); additionally verified as a genuine regression test by reverting bin/fm-spawn.sh t…
Switching a task's harness back to Claude after an intervening non-Claude leg reselects fresh from current isolated quota across both accounts, rather than silently reusing whatever account was bound… ✅ pass live tests/fm-control-relaunch.test.sh :: test_harness_switch_away_and_back_reselects_claude_profile_by_quota (second half) — asserts both profiles are freshly measured (quota_log has 2 entries, one per…
With no config/claude-profiles.json declared, spawn/relaunch preserves the historical single-account CLAUDE_CONFIG_DIR forwarding byte-for-byte (existing single-account operators are unaffected) ✅ pass live tests/fm-spawn-dispatch-profile.test.sh :: test_no_profile_keeps_claude_profile_defaults, test_claude_forwards_firstmate_config_dir_when_set, test_claude_omits_config_dir_prefix_when_unset
config/claude-profiles.json is inherited into secondmate homes so the whole fleet knows both accounts, including propagation, convergence on change, and absence-mirroring ✅ pass live tests/fm-secondmate-harness.test.sh :: B1 propagate_inheritable_config suite (lines ~304-366) explicitly exercises config/claude-profiles.json copy, change-convergence, and absence-mirroring into a se…
  • bash tests/fm-control-relaunch.test.sh
  • bash tests/fm-spawn-dispatch-profile.test.sh
  • bash tests/fm-secondmate-harness.test.sh
  • manual regression proof: reverted bin/fm-spawn.sh:3185 to the pre-fix unconditional guard, reran tests/fm-control-relaunch.test.sh, confirmed the new test fails, then restored the fixed file and confirmed the full suite passes again
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@max-metaplanet

Copy link
Copy Markdown
Contributor

A design question affecting this PR's account-file inheritance has been opened: #5481

MLA82 added a commit to MLA82/firstmate that referenced this pull request Sep 28, 2026
…nt profiles

# Conflicts:
#	AGENTS.md
#	bin/fm-config-inherit-lib.sh
#	bin/fm-remote-secondmate-control.sh
#	bin/fm-spawn.sh
#	docs/architecture.md
#	docs/configuration.md
MLA82 added a commit to MLA82/firstmate that referenced this pull request Sep 28, 2026
…guid#4366 and PR kunchenguid#4683

- tests/fm-control-relaunch.test.sh: read the whole recorded launch command
  instead of the retired inline 'encode launch-brief' grep shape (needed by
  PR kunchenguid#5288's own new test).
- tests/fm-backend-herdr-launcher-workspace-e2e.test.sh: pass FM_HOME to the
  teardown call explicitly, kept as a precaution.

Dropped: the 10s->20s Herdr server poll-budget widening in
bin/backends/herdr.sh. It was PR kunchenguid#4683's own fix for its own setsid-wrapped
server launch; without kunchenguid#4683, bin/backends/herdr.sh reverts to the original,
unmodified 10s poll and needs no widening.
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.

2 participants