Skip to content

fix: re-read the user store on restart; contain credential failures (see Consiliency/pmcp#487) - #489

Merged
ViperJuice merged 3 commits into
mainfrom
t3/pmcp-487-user-store-reread
Nov 24, 2026
Merged

ViperJuice merged 3 commits into
mainfrom
t3/pmcp-487-user-store-reread

Conversation

@ViperJuice

@ViperJuice ViperJuice commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

P1 of the approved plan for #487 (plans/detailed-pmcp-487-op-secret-references-20261108.md rev 5, head 6ee0f59): plan section 3.6 and the P1 row of section 4. No op:// resolution, no 1Password, no token handling (P2+).

What changes

Rotation of plain user-store values

  • Origin registry (env_store._ENV_ORIGINS): every insertion into os.environ is recorded as external (present before the startup load), home_dotenv, user_store or auth_connect_project. _load_operator_text takes the caller's class.
  • credential_value(name, environ=True): a name with origin external/home_dotenv/auth_connect_project (or unrecorded) answers from os.environ as before. Every other name reads the current user store. The read is strict, parsed interpolate=False, then expanded as python-dotenv does at startup, against the external env, the static ~/.env values and the current store (never a stale startup copy). It is cached by os.stat + os.lstat identity. A read within 2 s of the file's mtime is not cached ("racy" timestamps), and ownership of home is checked on every lookup.
  • An absent key, or no store file, continues to the tenant store, then the project .env/.env.pmcp, then the next lookup key, matching a restart (plan section 9). environ=False (tenant isolation) never reads the store.
  • The availability reload (handlers.py:2792-2794) is removed.
  • gateway.auth_connect never overwrites an external or home_dotenv value; it says the environment value still wins. User scope records user_store, so later store rotations apply to it. Project scope records auth_connect_project and keeps its in-process value.

Typed failure, contained (#342)

  • New pmcp/credential_state.py: CredentialUnavailableError(reason) and STORE_UNREADABLE. A store that exists but cannot be read (EACCES, directory/fifo at the path, ELOOP, not UTF-8) raises it. The lookup stops there and never tries the legacy key.
  • New AuthState/AuthEventKind value credential_unavailable, with semantics. _auth_event_for_state maps it. A credential_reason field is added on lifecycle, provision, health and status entries.
  • Containment at every caller:
    • config/loader.py build_resolved_config: the entry is kept with ResolvedServerConfig.credential_unavailable (excluded from dumps) and never started. Previously it was started without its credential.
    • Manifest loop: kept as a CREDENTIAL_UNAVAILABLE skip instead of being dropped.
    • add_config: skips typed entries, both lazy and eager. Its lookup-key loop stops at the first typed answer.
    • resolve_remote_headers records unavailable_env_vars.
  • installer.build_install_child_env / check_api_key refuse the spawn. handlers lifecycle (connect/restart/update via _resolve_lifecycle_target) and provision (including manifest_server_to_config at the old 3333/4383 lines) report credential_unavailable. Provision-status adoption logs it without a traceback. cli.py init and pmcp secrets check (unavailable_keys) report the typed state.
  • gateway.refresh leaves a running server up when its lookup is now typed. restart_server resolves before teardown, so a running instance is left up.

pmcp doctor (names only, never values; new credentials checks):

  • unreadable user store;
  • manifest runtime env_var set as external env and not a user-store name (inherited by every child);
  • every op:// value and OP_* key in a server's own env, by server and config source, and every op:// credential lookup by server and layer;
  • a legacy key next to its namespaced key, or one key in several layers.

Docs: CHANGELOG (Unreleased/Fixed), README (credential scopes), MIGRATING ("A rotated user-store key is used at the next server restart"). Each says that deleting one store entry is not revocation when a lower layer or legacy key holds the same credential.

Tests

New tests/test_user_store_rotation_487.py (33 tests) covers:

  • fleet layout (store with only API_TOKEN serves brightdata);
  • namespaced key added, rotated, then deleted (falls to API_TOKEN, then missing_auth);
  • a credential only in .env.pmcp with a store present;
  • a tenant ${VAR} header with a store present;
  • environ=False never reads the store;
  • no store file, and a store created after start;
  • a symlinked store edited, its target replaced, and the link re-pointed;
  • a cache hit and its invalidation;
  • interpolation of a ~/.env value, and no stale-copy interpolation;
  • key only in ~/.env, key in both, external wins, origin classes;
  • reload removed;
  • user-scope auth_connect then rotation, and project-scope auth_connect in-process;
  • auth_connect never overwrites external or ~/.env values;
  • unreadable store (typed, stops before the legacy key, refuses install env and provision gate);
  • mode-000 store;
  • connect_server/restart_server/provision report credential_unavailable;
  • secrets check unavailable_keys;
  • the doctor checks.

#342 guard extended in tests/test_catalog_overlay_discovery.py:

  • configured brightdata with no env (fleet -c shape) plus a healthy server, with an unreadable store: load_configs keeps both and marks brightdata;
  • startup resolution makes healthy eager and skips brightdata typed;
  • manifest-only brightdata is kept typed;
  • GatewayServer.initialize, gateway.refresh and restart_server of the healthy server succeed, and brightdata reports store_unreadable via connect_server and health;
  • refresh leaves a running brightdata up.

Updated inventories and tests for the removed reload and the new consumers:

  • test_store_reader_inventory (internals, home-memo exemption with reason, handlers no longer a store reader);
  • test_parse_error_echo exemptions;
  • test_credential_roots grid (new rows: lifecycle, config_status predicate, doctor);
  • tests that patched handlers.load_store now patch the live read; conftest restores os.environ after each test.

Mutation controls (scratch, restored from saved copies):

  • dropping the origin check fails 8 rotation tests;
  • dropping the refresh keep fails the refresh guard.

Evidence

  • dev0 (headroom checked each batch; ≤ -n 2; isolated HOME; scripts/pytest_temp.py), all green: test_store_reader_inventory, test_parse_error_echo, test_file_content_echo, test_credential_parity, test_env_leak_229, test_auth, test_store_readers_confined, test_credential_roots, test_manifest, test_config_loader, test_credential_gates_startup, test_credential_gates_handlers, test_startup_resolver, test_lazy_start, test_credential_optionality_e2e, test_manifest_provision, test_tools, test_phase4_e2e, test_catalog_overlay_discovery, test_cli, test_migration_doc, test_code_snippets_execute, test_baseline_constraints, test_env_overlay_provenance, test_feedback_provenance, test_user_store_rotation_487. (test_tools' update_server tests need dev0's npm_config_* vars unset; known host issue, they fail the same way without this change.)
  • ruff check, ruff format --check, mypy src/pmcp --exclude baml_client: clean.
  • Full suite on ai (py3.12, -n 8, systemd-run MemoryMax=16G, isolated HOME):
    • run 1 at f173831: 3 failed, 14716 passed, 57 skipped, 1 xfailed. Two causes, both fixed in b286fca:
      • test_exception_text_sinks: a conditional exc_info= did not go through safe_exc_info.
      • test_trust_boundaries_composition (2 tests): an earlier test's auth_connect write leaked into os.environ past monkeypatch.delenv(raising=False). The next startup load then recorded it as external, which auth_connect correctly refuses to overwrite. New autouse _restore_process_environment fixture in tests/conftest.py.
    • run 2 at b286fca: 14719 passed, 57 skipped, 1 xfailed in 170.72s, exit 0. Logs: ai:/tmp/pmcp-ai-viperjuice/487-p1-full{,-r2}.log.

Not in this PR: anything P2+ (op://, token, grammar, reference_not_allowed_here, the .env.pmcp op:// #342 variant), the secret_refs capability, migrate-op.

🤖 Generated with Claude Code

ViperJuice and others added 2 commits November 23, 2026 23:25
P1 of the approved plan for #487 (plan section 3.6 and
the P1 row of section 4). No op:// resolution, no 1Password.

- Origin registry: every name pmcp puts into os.environ is recorded as
  external, home_dotenv, user_store or auth_connect_project;
  _load_operator_text takes the caller's class.
- credential_value(environ=True) answers user-store names from the
  CURRENT store (strict read, parsed and expanded as at startup against
  the external env, the static ~/.env values and the current store),
  cached by stat + lstat identity. An absent key continues to the
  tenant/project layers and the legacy key, as after a restart.
- An unreadable store is a typed credential_unavailable
  (store_unreadable) that stops the lookup, refuses the spawn and is
  contained to that server's entry (#342): config loads keep the entry
  typed and never start it; connect_server, restart_server, provision,
  health and status report it; refresh leaves a running instance up.
- The availability check's reload of the user store is removed.
- gateway.auth_connect never overwrites an external or ~/.env value;
  user scope records user_store, project scope keeps its value.
- New AuthState / AuthEventKind credential_unavailable with semantics.
- pmcp doctor: inherited runtime variables, op:// values and OP_* keys
  by server and source, and credentials held in more than one place.
- pmcp secrets check reports unavailable_keys.

See #487.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…exc_info

The full suite on ai found two problems with the previous commit:

- A test that monkeypatch.delenv()s an absent key and then drives
  gateway.auth_connect leaked the write into later tests, whose startup
  load then recorded it as external. An autouse fixture now restores
  os.environ to its pre-test state.
- The typed-failure branch of the provision handoff passed exc_info
  through a conditional, which tests/test_exception_text_sinks.py
  rejects. It now uses two logger calls.

See #487.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ViperJuice
ViperJuice force-pushed the t3/pmcp-487-user-store-reread branch from b286fca to b399bb4 Compare November 24, 2026 04:29
Refs #487 (P1, PR #489 review round 1).

- loader: a configured entry's own literal credential satisfies auth
  before any store lookup, so an unreadable user store no longer refuses
  it (ASTRA-1).
- pmcp status: credential_unavailable entries are reported with
  auth_state credential_unavailable and credential_reason, and --probe
  never connects them (CLAUDE-C1).
- provision / configured-duplicate gate: typed store failures report
  credential_unavailable before any ensure_connected, never missing_auth
  (GEMINI-G1, CLAUDE-C2, C4 docstring).
- lifecycle: when a credential is now missing from every layer, restart
  and connect stop a running instance and drop its stale lazy
  registration; disconnect is exempt from missing-auth refusals. An
  unreadable store still leaves the instance up (GROK-B1).
- refresh: a startup copy of a user-store value is not the ambient
  baseline in _refresh_config_unchanged, so a deleted or rotated
  same-name key reaches a running lazy server (GROK-B2, GEMINI-G2).
- doctor: an unreadable store is not reported as holding a key; a tenant
  store holding a key is a named layer (ASTRA-2, GROK-N2).
- refresh summary logs the credential_unavailable count (GROK-N3).
- docs: lazy first start, crash reconnect and connect-on-online reuse the
  credential resolved at the last startup/refresh (CLAUDE-C3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T05:33:13.649800Z 3a57a06 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ViperJuice
ViperJuice merged commit cd2805f into main Nov 24, 2026
15 checks passed

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a57a06f26

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2819 to +2823
if status is None or status.status in (
ServerStatusEnum.OFFLINE,
ServerStatusEnum.LAZY,
):
return failure

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prune stale lazy configs before returning missing_auth

If a configured server is still lazy when its credential is deleted, calling gateway.restart_server or gateway.connect_server reports missing_auth but returns here without removing its previously resolved lazy config. A subsequent tool call reaches _ensure_server_for_tool, and ClientManager.ensure_connected starts that cached config with the deleted credential. Prune the lazy registration even when there is no running instance to disconnect, so an explicit lifecycle call that observes the deletion cannot leave a stale credential available for the next spawn.

Useful? React with 👍 / 👎.

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.

1 participant