Skip to content

refactor: consolidate gateway secrets into agentflare.db (closes #138) - #146

Merged
getappz merged 3 commits into
masterfrom
refactor/db-consolidate-secrets
Jul 11, 2026
Merged

refactor: consolidate gateway secrets into agentflare.db (closes #138)#146
getappz merged 3 commits into
masterfrom
refactor/db-consolidate-secrets

Conversation

@getappz

@getappz getappz commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Move gateway_secrets table out of standalone ~/.agentflare/gateway.db into shared ~/.agentflare/agentflare.db
  • Add gateway_secrets::migrate(conn) called from db::open() (next to claims::migrate)
  • Repoint cli/gateway.rs and mcp_server.rs::resolve_gateway_secrets to use crate::db::open()
  • One-time migration: copies raw ciphertext rows from old gateway.db on first open of agentflare.db (leaves old file intact)
  • Tests use mem_migrated() helper instead of raw SQL table creation
  • All 268 existing tests pass

Closes #138

Summary by CodeRabbit

  • New Features

    • Gateway secrets now use the shared application database.
    • Existing secrets are automatically migrated from the legacy database when available.
    • Migration safely avoids duplicate imports and prevents deleted secrets from reappearing.
  • Bug Fixes

    • Legacy databases with missing or incompatible data are skipped without disrupting application startup.
    • Secret access continues to support listing, setting, removing, and passphrase validation.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Gateway secrets are migrated into the shared agentflare.db. Database opening initializes the secrets table, imports legacy gateway.db entries, and marks migration completion. CLI and MCP secret access now use the shared database connection.

Changes

Shared gateway secrets database

Layer / File(s) Summary
Schema and legacy database migration
src/db.rs, src/gateway_secrets.rs
The shared database creates the gateway_secrets table, imports compatible legacy rows with duplicate-safe inserts, renames the legacy file, and tests missing or incompatible databases plus deletion persistence.
CLI and MCP database access
src/cli/gateway.rs, src/mcp_server.rs
Secret commands and MCP gateway-secret resolution open agentflare.db through crate::db::open() and update failure messages and path handling.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SecretClients
  participant crate_db_open
  participant LegacyGatewayDB
  participant GatewaySecretsTable
  SecretClients->>crate_db_open: Open agentflare.db
  crate_db_open->>GatewaySecretsTable: Ensure gateway_secrets schema
  crate_db_open->>LegacyGatewayDB: Read legacy secrets
  LegacyGatewayDB-->>crate_db_open: Return compatible rows
  crate_db_open->>GatewaySecretsTable: Insert rows with INSERT OR IGNORE
  crate_db_open->>LegacyGatewayDB: Rename gateway.db to gateway.db.migrated
  crate_db_open-->>SecretClients: Return shared connection
Loading

Possibly related PRs

  • getappz/agentflare#104: Introduces the encrypted gateway-secret store and its CLI/MCP integration used by this database migration.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: consolidating gateway secrets into agentflare.db.
Description check ✅ Passed It covers the summary and rationale, though the template's test plan and reviewer notes sections are not filled out.
Linked Issues check ✅ Passed The changes satisfy #138 by moving gateway secrets into agentflare.db, repointing callers, and adding best-effort legacy migration.
Out of Scope Changes check ✅ Passed No clear out-of-scope changes are evident; the edits stay focused on shared DB consolidation and legacy secret migration.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/db-consolidate-secrets

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/db.rs`:
- Around line 34-36: Update migrate_old_gateway_db and the db::open()
initialization flow to persist a migration-complete marker in agentflare.db only
after the legacy secrets copy transaction succeeds, and skip importing when that
marker already exists. Add a regression test that migrates a secret, removes it,
reopens the database, and verifies the deleted secret is not restored.
- Around line 65-70: Update the legacy gateway-secrets migration flow around the
statement preparation and `rows` iteration so schema, query, and row-decoding
failures are treated as best-effort failures rather than propagated from
`db::open()`. Skip incompatible or malformed legacy entries/database reads while
allowing shared database initialization and unrelated claims access to continue,
and add coverage for an old schema missing the expected columns.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4cea5b36-fa4e-48aa-a996-1256085a9148

📥 Commits

Reviewing files that changed from the base of the PR and between 2e463c5 and 591be68.

📒 Files selected for processing (4)
  • src/cli/gateway.rs
  • src/db.rs
  • src/gateway_secrets.rs
  • src/mcp_server.rs

Comment thread src/db.rs
Comment on lines +34 to +36
// One-time migration: copy secrets from old gateway.db
// (pre-#138 separate file) into agentflare.db.
migrate_old_gateway_db(&conn)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Persist migration completion so deleted secrets stay deleted.

Because the legacy file remains, every db::open() imports it again. After removing a migrated secret, the next CLI or MCP open restores it through INSERT OR IGNORE.

Record a migration-complete marker in agentflare.db after a successful transactional copy, then skip future imports. Add a remove-then-reopen regression test.

Also applies to: 43-75

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/db.rs` around lines 34 - 36, Update migrate_old_gateway_db and the
db::open() initialization flow to persist a migration-complete marker in
agentflare.db only after the legacy secrets copy transaction succeeds, and skip
importing when that marker already exists. Add a regression test that migrates a
secret, removes it, reopens the database, and verifies the deleted secret is not
restored.

Comment thread src/db.rs Outdated
- rename gateway.db -> gateway.db.migrated after a successful import so a
  deleted secret can't resurrect via INSERT OR IGNORE on the next open()
- make legacy reads best-effort: an incompatible/malformed old schema is
  skipped instead of propagating and bricking every db::open() (which would
  also disable unrelated claims access)
- regression tests: deleted-stays-deleted, incompatible-schema-non-fatal

Addresses CodeRabbit review on #146.
@getappz

getappz commented Jul 11, 2026

Copy link
Copy Markdown
Owner Author

Addressed both findings in e95e41c:

  • Deleted secrets resurrecting (db.rs:36): after a successful import, gateway.db is renamed to gateway.db.migrated — the migration is now one-shot, so INSERT OR IGNORE can no longer re-add a secret the user deleted. Chose a file-rename marker over a new marker table/column to keep it zero-schema. Regression test: deleted_secret_stays_deleted_after_reopen.
  • Incompatible legacy schema bricking open() (db.rs:65-70): legacy reads (prepare/query_map/row decode) are now best-effort and skip on error instead of propagating out of db::open(); malformed rows are dropped via .flatten(). Only writes into our own healthy shared db stay fatal. Regression test: incompatible_legacy_schema_does_not_brick_open.

Base automatically changed from feat/claim-ledger to master July 11, 2026 13:29
@getappz
getappz enabled auto-merge July 11, 2026 13:35

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/db.rs`:
- Around line 45-56: The legacy database migration must persist completion in
agentflare.db even when renaming gateway.db fails. Update the migration logic
around the legacy import and the related open flow to check a durable migration
marker before reading gateway.db, record that marker in the same transaction as
successful secret imports, and treat the rename as best-effort cleanup only. Add
coverage for forced rename failure followed by secret deletion and a retry,
ensuring the deleted secret is not re-imported.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5a01ec97-fc1d-40b8-bcae-e8be0a681e79

📥 Commits

Reviewing files that changed from the base of the PR and between 591be68 and 4d3eeba.

📒 Files selected for processing (2)
  • src/db.rs
  • src/mcp_server.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/mcp_server.rs

Comment thread src/db.rs
Comment on lines +45 to +56
/// Copy secrets from a legacy `gateway.db` (pre-#138 separate file) into the
/// shared db, then rename the legacy file so it's imported exactly once.
///
/// Reads from the legacy db are best-effort: a missing file, an unopenable
/// db, or an incompatible/malformed `gateway_secrets` schema all skip the
/// import rather than failing `open()` — otherwise one bad legacy file would
/// brick unrelated claims access too. Only writes into our own (healthy)
/// shared db are fatal.
///
/// Renaming to `gateway.db.migrated` on success is the migration-complete
/// marker: without it every `open()` re-imports via `INSERT OR IGNORE`, so a
/// secret the user deleted would resurrect on the next run.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Persist completion even when the legacy-file rename fails.

Ignoring rename errors leaves gateway.db active. After a user deletes an imported secret, a later open() resurrects it, so the fallback is not idempotent.

Commit a migration marker in agentflare.db with the imports, consult it before reading the legacy file, and treat the rename only as cleanup. Add coverage that forces rename failure, deletes the secret, and retries migration.

Also applies to: 80-83, 136-157

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/db.rs` around lines 45 - 56, The legacy database migration must persist
completion in agentflare.db even when renaming gateway.db fails. Update the
migration logic around the legacy import and the related open flow to check a
durable migration marker before reading gateway.db, record that marker in the
same transaction as successful secret imports, and treat the rename as
best-effort cleanup only. Add coverage for forced rename failure followed by
secret deletion and a retry, ensuring the deleted secret is not re-imported.

@getappz
getappz merged commit d8f51d4 into master Jul 11, 2026
10 checks passed
@getappz
getappz deleted the refactor/db-consolidate-secrets branch July 11, 2026 13:39
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.

refactor(db): consolidate source-of-truth DBs into ~/.agentflare/agentflare.db

1 participant