Skip to content

feat(security): add archive-wide bulk secret-candidate scan - #3486

Merged
Sinity merged 1 commit into
masterfrom
feat/security/bulk-secret-candidate-scan
Jul 31, 2026
Merged

Sinity merged 1 commit into
masterfrom
feat/security/bulk-secret-candidate-scan

Conversation

@Sinity

@Sinity Sinity commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

polylogue ops scan-secrets required --session <id>, so an operator with no
prior signal had no way to discover secret candidates archive-wide, and
nothing kept coverage current as new sessions were ingested. This adds a
bounded, resumable archive-wide sweep (CLI --all + a daemon catch-up loop)
on top of the existing per-session scanner and non-injectable
SECRET_CANDIDATE assertion write path.

Problem

polylogue/cli/commands/scan_secrets.py:22 had --session as required=True
with no bulk mode, and no daemon convergence/maintenance loop ever called
scan_session_for_secret_candidates — the regex/entropy rules and write
chokepoint existed but only ran when an operator already knew a specific
session id to scan (polylogue-layg.1, filed against the security/privacy
covenant epic polylogue-kwsb after the polylogue-27m per-session fix round
shipped with zero archive-wide callers).

Solution

  • polylogue/security/secret_scan.py: scan_archive_for_secret_candidates
    is a bounded page of the archive-wide sweep. It selects sessions not yet
    covered by a new ops-tier secret_scan_status table at the current
    SECRET_SCAN_VERSION (select_pending_secret_scan_session_ids,
    count_pending_secret_scan_sessions), scans each with the same rules and
    write chokepoint as the existing single-session path, and commits a
    session's findings together with its coverage row in the same
    user.db/ops.db transaction pair. This makes resumption exact: an
    interrupted sweep never leaves a session "covered" without its candidates
    recorded, and a fresh call re-derives the still-pending set from the
    coverage table itself (no separate cursor row, and no duplicate findings —
    the write chokepoint's deterministic assertion ids are already idempotent).
    A SECRET_SCAN_VERSION bump invalidates every existing coverage row and
    schedules an intentional rescan.
  • polylogue/storage/sqlite/archive_tiers/ops.py: additive
    secret_scan_status DDL. Ops tier is disposable by design — losing this
    table only means re-scanning (idempotent), never lost candidates, since
    findings themselves live durably in user.db.
  • polylogue/cli/commands/scan_secrets.py: --session is no longer
    required. --all drains the full pending backlog across as many bounded
    pages as needed; --limit N bounds one invocation to a single page;
    --origin scopes the sweep to one origin token; --status reports
    pending-coverage count without scanning anything.
  • polylogue/daemon/secret_scan_sweep.py: a new plain asyncio periodic
    loop (periodic_secret_scan_sweep, 15-minute cadence, 50 sessions/tick)
    draining one bounded page per tick so newly ingested sessions get
    incremental coverage without operator action. Deliberately not a
    ConvergenceStage — docs/retro/2026-05-24-1498-cascade.md (mandatory
    reading before touching convergence_stages.py/daemon/cli.py) records
    that file's own verdict as "refactor before adding a fourth stage"; this
    follows the same shape as the existing fts_orphan_audit.py/
    periodic_fts_identity_drift_recompute sweeps instead. Registration in
    daemon/cli.py is a two-line addition (one import, one line in the
    periodic_loops list) next to the other maintenance loops, to stay clear
    of the startup-auth and raw-materialization-drain regions other lanes are
    touching concurrently.

Acceptance-criteria matrix (polylogue-layg.1)

AC Status
Scan all sessions or a bounded recent/origin scope without enumerating ids Satisfied — --all (full drain), --limit (bounded page), --origin (scope). since_ms scoping exists in the Python API (scan_archive_for_secret_candidates(..., since_ms=...)) but is not yet exposed as a CLI flag
Daemon can drain new/stale scanner work in bounded pages Satisfied — periodic_secret_scan_sweep, 50 sessions/15min
Killing and resuming continues from a committed cursor without duplicating candidates Satisfied — coverage table is the cursor; see test_interrupt_and_resume_covers_all_sessions_exactly_once
Unchanged sessions at current scanner/evidence version are skipped; version bump schedules rescan Satisfied — SECRET_SCAN_VERSION; see test_scanner_version_bump_schedules_intentional_rescan
Results preserve exact evidence refs and suppression/review state Satisfied — reuses the existing record_secret_candidates/upsert_assertion chokepoint unchanged
Status reports progress, errors, and remaining debt Satisfied — BulkSecretScanResult/SecretScanSweepResult carry scanned/candidates/errors/remaining counts; CLI --status and --all --json surface them
Scale fixture proves bounded memory/query time and no full-archive rescan on an idle tick Deferred — no dedicated large-N benchmark fixture in this PR; the bounded-page tests (max_sessions=2 over 5-6 sessions) prove the mechanism is page-bounded, not a scale/perf assertion. Follow-up if the operator wants a benchmark gate
Security/privacy coverage manifest drops the per-session-only caveat Not done in this PR — docs/plans/security-privacy-coverage.yaml is unchanged; flagging as remaining scope rather than claiming it here

Verification

  • python -m devtools test tests/unit/security/test_secret_scan.py tests/unit/cli/test_scan_secrets.py tests/unit/daemon/test_secret_scan_sweep.py → 38 passed
  • python -m devtools verify --quick → exit_code: 0 (ruff format/check, mypy --strict, render all --check, layering, closure-matrix, schema-versioning policy, schema promotion audit)
  • python -m devtools render all --check → all surfaces sync OK, including the regenerated docs/plans/topology-target.yaml for the new polylogue/daemon/secret_scan_sweep.py module

Anti-vacuity: the production dependency exercised is the real
scan_text_for_secret_candidates regex/entropy rules and the real
upsert_assertion write chokepoint (author_kind="detector" coercing
status=CANDIDATE/non-injectable), run against a real multi-session
index.db fixture with planted AWS-key-shaped spans. Reverting the bulk
scanner to skip a session batch (e.g. stopping after one page without
checking more_pending, or a query that fails to anti-join
secret_scan_status) fails
test_full_sweep_covers_every_session_and_finds_every_planted_secret
because it asserts every seeded session — not just one — ends up covered
with a recorded candidate and remaining_pending == 0.

Not merging

Ref polylogue-layg.1 — leaving this PR open per instruction; not merging or
closing the bead from this branch.

Problem: polylogue ops scan-secrets required --session, so an operator
with no prior signal had no way to discover secret candidates
archive-wide; nothing kept coverage current as new sessions were
ingested (polylogue-layg.1).

What changed:
- polylogue/security/secret_scan.py: scan_archive_for_secret_candidates
  is a bounded, checkpointed sweep over sessions not yet covered by a
  new ops-tier secret_scan_status table at the current
  SECRET_SCAN_VERSION. Coverage rows commit together with their
  session's findings, so an interrupted sweep never leaves a session
  covered without its candidates recorded, and resuming re-derives the
  pending set from the coverage table instead of restarting or
  duplicating findings. A scanner-version bump invalidates all existing
  coverage and schedules an intentional rescan.
- polylogue/storage/sqlite/archive_tiers/ops.py: additive
  secret_scan_status DDL (disposable ops tier -- losing it only means
  re-scanning, never lost candidates, since findings live durably in
  user.db).
- polylogue/cli/commands/scan_secrets.py: --session is no longer
  required; --all drains the full pending backlog across bounded
  pages, --limit bounds one page, --origin scopes the sweep, --status
  reports pending coverage without scanning.
- polylogue/daemon/secret_scan_sweep.py: a plain asyncio periodic loop
  (not a ConvergenceStage -- convergence_stages.py's own retro verdict
  is "refactor before adding a fourth stage", docs/retro/2026-05-24-1498-cascade.md)
  draining one bounded page every 15 minutes, registered as a one-line
  addition to daemon/cli.py's periodic_loops list.

Verification:
- python -m devtools test tests/unit/security/test_secret_scan.py
  tests/unit/cli/test_scan_secrets.py
  tests/unit/daemon/test_secret_scan_sweep.py -> 38 passed
- python -m devtools verify --quick -> exit 0 (ruff format/check, mypy
  --strict, render all --check, layering, closure-matrix, schema
  policy checks)
- docs/plans/layering-surface-baseline.json: added one baselined entry
  for secret_scan_sweep.py's sqlite_locking import, mirroring the
  identical existing entry for fts_orphan_audit.py (same sweep shape).

Ref polylogue-layg.1

Co-Authored-By: Claude <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 43 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f34c0ed1-6a1e-463f-8820-a3caac0ff303

📥 Commits

Reviewing files that changed from the base of the PR and between d3039ff and 4735a0c.

📒 Files selected for processing (10)
  • docs/plans/layering-surface-baseline.json
  • docs/plans/topology-target.yaml
  • polylogue/cli/commands/scan_secrets.py
  • polylogue/daemon/cli.py
  • polylogue/daemon/secret_scan_sweep.py
  • polylogue/security/secret_scan.py
  • polylogue/storage/sqlite/archive_tiers/ops.py
  • tests/unit/cli/test_scan_secrets.py
  • tests/unit/daemon/test_secret_scan_sweep.py
  • tests/unit/security/test_secret_scan.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@Sinity
Sinity merged commit ee1881c into master Jul 31, 2026
3 checks passed
@Sinity
Sinity deleted the feat/security/bulk-secret-candidate-scan branch July 31, 2026 22:25
Sinity added a commit that referenced this pull request Aug 1, 2026
…jtc, bvnz, 5vbs)

Reconciliation pass driven by devtools lab probe bead-pr-reconciliation:
verified each bead's own acceptance criteria against the actual diff/AC
matrix of its referencing merged PR(s), not just the PR's self-description.

Closed (AC fully satisfied):
- polylogue-bvnz: PR #3504, all 5 AC items satisfied.
- polylogue-5vbs: PR #3439, AC's detection/repair branch satisfied via new
  daemon/fts_orphan_audit.py sweep.

Left open with progress notes (partial, residual scope named):
- polylogue-layg.1: PR #3486 satisfies 6/8 AC items; scale fixture + coverage
  manifest update still missing.
- polylogue-hjpx: PR #3485 confirms AC1-5 satisfied (mostly by pre-existing
  landed work whose ancestry the bead's own stale note had missed), but
  bd close refuses on open child polylogue-yla8 (the live closure-gate audit
  AC1-5 doesn't substitute for).
- polylogue-6j9c, polylogue-9kjtc: PR #3511 satisfies 2/3 AC items each;
  live-archive reprice pass over existing session_profiles rows still
  outstanding for both.

Ref polylogue-93xe

Co-Authored-By: Claude <[email protected]>
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