Tasks: 008 — Agent API Authentication¶
Conventions¶
- Status:
[ ]todo /[~]in-progress /[x]done. - Owner:
@copilot. - Each task references stable IDs from spec.md.
T-001 — app.core.security dependency¶
- Refs: FR-1, FR-2, NFR-SEC-1, NFR-SEC-2, NFR-PERF-1, AC-5
- Owner: @copilot
- Status:
[x] - Files:
backend/app/core/security.py(new)backend/app/core/config.py(edit — addAGENT_API_KEY: str | None = None)backend/tests/core/test_security.py(new)- Steps:
- Add
AGENT_API_KEYtoSettings(env-driven, defaultNone). - Implement
require_api_key(x_api_key: str | None = Header(None)) -> Noneusingsecrets.compare_digestfor the equality check. - Cover all four branches (unset, missing, wrong, correct) plus a
compare_digest-was-used assertion. - Verify:
cd backend && pytest tests/core/test_security.py -qpasses;mypy app/core/security.py --strictclean.
T-002 — Mount dependency on /api/agents/* + startup warning¶
- Refs: FR-3, FR-4, NFR-AUDIT-1, AC-1, AC-2, AC-3, AC-4, AC-7
- Owner: @copilot
- Status:
[x] - Files:
backend/app/main.py(edit —dependencies=[Depends(require_api_key)]on the agents router; add lifespan startup warning when key unset andENVIRONMENT != "development")backend/tests/api/test_agents.py(edit — add_isolate_authfixture; add four parametrized cases for AC-1..AC-4; addcaplog-based AC-7 test)- Steps:
- Import
Dependsandrequire_api_keyinapp/main.py; passdependencies=[...]toapp.include_router(agents.router, ...). - Inside
lifespan, after registry registration, checksettings.AGENT_API_KEY+settings.ENVIRONMENT; log a single WARNING when unset outside development. - Update existing
test_agents.pytests so they explicitly run with no key (default), preserving green suite. - Add new tests covering missing / invalid / valid keys.
- Verify:
cd backend && pytest tests/api/test_agents.py -qpasses;pytest --cov=app.api.agents --cov=app.core.security≥ 95 % on both; manualcurl -i http://localhost:8000/api/agentswith and withoutX-API-Keyconfirms the gate.
T-003 — Frontend: forward X-API-Key for agent-ops calls¶
- Refs: FR-5, AC-6, NFR-COVERAGE-1
- Owner: @copilot
- Status:
[x] - Files:
frontend/lib/api.ts(edit — internalagentFetchwrapsfetchand addsX-API-Keyfromprocess.env.NEXT_PUBLIC_AGENT_API_KEYwhen set; only used byfetchAgents/fetchRuns/fetchBacklog/fetchSpecs)frontend/__tests__/components/AgentDashboard.test.tsx(edit — setprocess.env.NEXT_PUBLIC_AGENT_API_KEYper-test; MSW handlers assert the header is forwarded)- Steps:
- Refactor the four
fetchAgents/fetchRuns/fetchBacklog/fetchSpecshelpers to call a new privateagentFetch(path)instead offetch(...)directly. The helper attachesX-API-Keyonly whenprocess.env.NEXT_PUBLIC_AGENT_API_KEYis non-empty. - Add a vitest case that sets
vi.stubEnv('NEXT_PUBLIC_AGENT_API_KEY', 's3cret'), mountsAgentDashboard, and asserts that all four MSW handlers receivedX-API-Key: s3cret. - Keep an existing case with the env var unset to assert no header is sent (regression for default-open mode).
- Verify:
cd frontend && npm test -- --rungreen; coverage stays ≥ 70 %.
T-004 — Document new env vars¶
- Refs: FR-6
- Owner: @copilot
- Status:
[x] - Files:
.env.example(edit — addAGENT_API_KEY=andNEXT_PUBLIC_AGENT_API_KEY=with comments)- Steps:
- Add a
### Auth (optional, recommended for non-localhost)block to.env.exampledocumenting both keys and the local-dev "leave blank" behaviour. - Verify:
grep -n AGENT_API_KEY .env.exampleshows both entries;python scripts/spec_validator.pyandpython scripts/backlog_lint.pyexit 0.
T-005 — Spec / BACKLOG hygiene¶
- Refs: constitution §11
- Owner: @copilot
- Status:
[x] - Files:
BACKLOG.md(edit — move BL-20260429-13 from## Opento## Done; append new follow-up BL items)specs/008-agent-api-auth/spec.md(editStatus:→shippedafter green CI)- Steps:
- After T-001..T-004 land green, set
Status: shippedin spec.md. - Move BL-20260429-13 to
## Donewith a one-line outcome and PR link. - Mirror new follow-ups (see below) into
BACKLOG.md ## Open. - Verify:
python scripts/spec_validator.py && python scripts/backlog_lint.py && cd backend && pytest --cov=app --cov-fail-under=65all exit 0.
Done-When (Definition of Done)¶
- All tasks
[x]. - All
AC-*fromspec.mdcovered by passing tests (AC-1..AC-7). make lint && make testgreen (backend ≥ 75 % cov on changed files, frontend ≥ 70 % cov).- BL-20260429-13 moved to
## Donein BACKLOG.md with PR link. ## Follow-ups Discoveredsection below populated.
Follow-ups Discovered¶
Append-only. Mirror each item into BACKLOG.md with a
BL-YYYYMMDD-NNid. CI (scripts/spec_validator.py) checks this section exists. CI (scripts/backlog_lint.py) checks the BACKLOG mirror.
- BL-20260430-07 — feature, med — Move the agent-ops API key out of the browser bundle by introducing a Next.js route handler (
app/agents/api/[...path]/route.ts) that proxies/api/agents/*from the server, soNEXT_PUBLIC_AGENT_API_KEYcan be replaced with a server-onlyAGENT_API_KEY. Surfaced in: specs/008-agent-api-auth (R-2). - BL-20260430-08 — feature, med — Extend
require_api_key(or add per-router variants) to gate/api/analysis,/api/graph, and/api/lsat. Today only/api/agents/*is protected. Surfaced in: specs/008-agent-api-auth (NG3). - BL-20260430-09 — feature, low — Add per-key rate limiting on
/api/agents/*(e.g. SlowAPI / Starlette middleware) so a leaked key cannot scrape the audit log at line speed. Surfaced in: specs/008-agent-api-auth (NG2). - BL-20260430-10 — doc-gap, low — Add a
docs/security.mdcapturing the threat model, the single-key MVP, and the upgrade path to per-user auth. Referenced by spec 008 §12 R-2. Surfaced in: specs/008-agent-api-auth.