Skip to content

feat(cli): make agentkeys init idempotent#167

Merged
hanwencheng merged 2 commits into
mainfrom
codex/issue-3-init-idempotent
Jun 2, 2026
Merged

feat(cli): make agentkeys init idempotent#167
hanwencheng merged 2 commits into
mainfrom
codex/issue-3-init-idempotent

Conversation

@YoshiyukiSakura
Copy link
Copy Markdown
Contributor

@YoshiyukiSakura YoshiyukiSakura commented Jun 1, 2026

Change:

  • Implements the Stage 8 CLI defensive deliverable for idempotent agentkeys init.
  • Adds agentkeys init --force to intentionally re-run initialization when a usable local session already exists.
  • Keeps existing library callers compatible through cmd_init, with cmd_init_with_force for forced paths.

Objective Evidence:

  • cargo fmt --check (exit code 0)
  • cargo test -p agentkeys-cli init_ (exit code 0)
  • cargo test -p agentkeys-cli --tests (exit code 0)

Visual Evidence:

  • n.a.

Reviewer:

Risks / Not Covered:

Refs #3

yoshiyuki and others added 2 commits June 2, 2026 02:33
…t is discoverable

The idempotent init path printed only "Already initialized as <wallet>",
giving an operator no on-screen hint for how to deliberately re-initialize.
Per the project's no-remembered-flags preference (auto-detect over flags;
never make the operator recall a flag), surface the override at the exact
moment it's relevant:

  Already initialized as 0x...  ->
  Already initialized as 0x.... Run 'agentkeys init --force' to re-initialize.

The default path stays flagless and idempotent; --force remains the single
explicit, now self-advertising, escape hatch (its intent — discard a still
-valid session — is not derivable from state, so it cannot be auto-detected).
Updates the idempotent-init test assertion to match.

Refs #3
@hanwencheng
Copy link
Copy Markdown
Member

Pushed a small follow-up to this branch (63b492c) addressing one point from review.

What changed — the idempotent path now names the override in its own output:

- Already initialized as 0x1234…
+ Already initialized as 0x1234…. Run 'agentkeys init --force' to re-initialize.

Why — this repo's standing preference is auto-detect over flags; never make the operator recall a flag (the CLAUDE.md "idempotent remote-setup" rule, e.g. why we never pass --upgrade). The idempotent default here is exactly right and needs no flag, so that part is great.

--force itself is worth keeping: its intent — deliberately discarding a still-valid session (switching identity, recovering a suspected-bad-but-unexpired session) — is not derivable from state, so unlike --upgrade/--with-mcp it can't be auto-detected, and idempotency needs exactly one escape hatch. (The expired/invalid/missing cases already re-init with no flag.)

The only gap was that a dead-end "Already initialized" message forced the operator to remember the flag. Now the tool surfaces it at the moment of need — zero memory required, default stays flagless.

cargo fmt --check + cargo test -p agentkeys-cli init_ green (3 passed); the idempotent-init test assertion was updated to match.

Non-blocking follow-up (optional): is_usable_session's expiry branch (created_at != 0 && expired) has no unit coverage — InProcessBackend hardcodes created_at: 0, so the tests only exercise the "usable" shortcut, while the real broker path (build_session_from_jwt, 5h TTL) does set a real timestamp. A test that persists a Session { created_at: now - ttl - 1, .. } and asserts init re-initializes would pin that security-relevant branch.

@hanwencheng hanwencheng merged commit e0ee9d4 into main Jun 2, 2026
6 checks passed
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