Skip to content

worker: reject wrong-org, expired and replayed WorkerLeases - #45

Merged
stone16 merged 2 commits into
mainfrom
codex/issue-17-worker-leases
Jul 21, 2026
Merged

worker: reject wrong-org, expired and replayed WorkerLeases#45
stone16 merged 2 commits into
mainfrom
codex/issue-17-worker-leases

Conversation

@stone16

@stone16 stone16 commented Jul 21, 2026

Copy link
Copy Markdown
Owner

Summary

  • add a canonical signed WorkerLease and bounded persistent no-op durable-job carrier
  • make lease redemption one-shot, tenant/job/audience-bound, replay-safe, and concurrency-safe
  • seal the PostgreSQL boundary behind a fixed worker receiver with no direct table privileges
  • support atomic expired-lease reissue after post-commit token delivery loss
  • record the bounded Issue worker: reject wrong-org, expired and replayed WorkerLeases #17 activation without publishing the deferred full ServiceActor/ACCEPT-008 contract

Verification

  • make check
    • build passed
    • Ruff passed
    • strict mypy passed (95 source files)
    • unit: 756 passed
    • catalog: 48 passed; validator: 15 invariants / 12 fixtures
    • process smoke: 4 passed
    • real PostgreSQL integration/security: 106 passed
  • final terminology fix: real PostgreSQL integration 106 passed
  • mandatory Standards, Security, and Spec reviews reached a zero-finding fixed point
  • migration downgrade/upgrade: 0006 -> 0005 -> head passed

Closes #17

Summary by CodeRabbit

  • New Features
    • Added signed, time-limited worker leases for durable no-op job completion.
    • Ensured each lease is bound to the intended job, service, workload, and worker.
    • Added atomic completion, replay protection, expiration handling, and safe audit receipts.
    • Added database-backed persistence and strict tenant/security boundaries.
  • Documentation
    • Documented WorkerLease activation, limitations, security semantics, and future expansion boundaries.
  • Security
    • Improved protection against tampering, cross-tenant access, credential exposure, and unauthorized database operations.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a bounded signed WorkerLease flow for persistent no-op jobs, including canonical HMAC tokens, PostgreSQL-backed issuance and atomic completion, dedicated RLS roles, Issue #17 catalog activation, documentation, and extensive unit/integration tests.

Changes

Persistent no-op WorkerLease

Layer / File(s) Summary
Lease contract and application seam
engine/supply/*, applications/worker.py, tests/unit/test_worker_lease.py
Defines validated versioned HMAC WorkerLease tokens, keyrings, rejection artifacts, persistence request/result types, and the worker completion entry point.
Persistence schema and authority boundary
migrations/versions/20260722_0006_worker_lease_noop.py, engine/persistence/*, scripts/provision_database_roles.py, infra/postgres/init/*
Adds durable service-principal and no-op job tables, RLS policies, security-definer issue/complete functions, dedicated roles, and PostgreSQL issuer/authority implementations.
Security catalog activation
eval/catalogs/*, scripts/validate_security_catalog.py, tests/catalog/*
Adds the Issue #17 activation, canonical evidence constraints, and independent ACCEPT-008 future/fail-closed carrier validation.
Database integration proof
tests/integration/test_worker_lease.py, tests/integration/test_postgres_harness.py, tests/integration/test_migrations.py
Validates issuance, completion, replay rejection, concurrency, expiry takeover, tenant isolation, permissions, RLS, rollback, and migration state.
Documentation and process support
README.md, CONTEXT.md, docs/decisions/*, tests/process/test_processes.py
Documents the bounded activation and improves worker readiness and database harness checks.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related issues

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is related to the worker lease changes and highlights key rejection cases, though it understates the broader new lease issuance/completion system.
✨ 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 codex/issue-17-worker-leases

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.

@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: 3

🧹 Nitpick comments (1)
tests/integration/test_worker_lease.py (1)

399-403: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use raw strings for the anchored match= regexes (Ruff RUF043).

The pytest.raises(..., match="^work not available$") patterns are regexes but aren't raw strings; Ruff RUF043 flags every occurrence (lines 401, 466, 532, 557, 574, 599, 632, 663, 928, 1002). Behavior is unchanged, but the raw-string form documents regex intent and clears the lint.

♻️ Example (apply to each flagged line)
-        match="^work not available$",
+        match=r"^work not available$",
🤖 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 `@tests/integration/test_worker_lease.py` around lines 399 - 403, Update every
flagged pytest.raises match argument in the test module to use raw string
literals for the anchored regex patterns, including the occurrence in
issue_noop_lease and the other listed locations. Preserve the existing regex
text and test behavior while satisfying Ruff RUF043.

Source: Linters/SAST tools

🤖 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 `@docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md`:
- Around line 44-50: Make ServiceActor authorization a prerequisite for worker
database activation alongside the exact-job signed WorkerLease; otherwise keep
activation deferred and fail-closed. Update
docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md
(44-50), CONTEXT.md (45 and 317-328), README.md (21-25 and 109-123), and
docs/decisions/README.md (30) to require the registered least-privilege
ServiceActor, include it in WorkerLease scope, and remove claims that the
ServicePrincipal-only carrier or active lease is sufficient.
- Line 12: Update the ADR metadata date in the decision record to the actual
acceptance date; if it has not yet been accepted, remove or change the accepted
status instead of retaining a future date.

In `@README.md`:
- Around line 85-88: Update the README status section to remove the stale
“WorkerLease: NOT_ACTIVE” claim. Align the WorkerLease wording with the
activated Issue `#17` persistent no-op seam and PostgreSQL authority, or
explicitly qualify NOT_ACTIVE as applying only to the full production carrier.

---

Nitpick comments:
In `@tests/integration/test_worker_lease.py`:
- Around line 399-403: Update every flagged pytest.raises match argument in the
test module to use raw string literals for the anchored regex patterns,
including the occurrence in issue_noop_lease and the other listed locations.
Preserve the existing regex text and test behavior while satisfying Ruff RUF043.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 352c1c62-c670-48d5-b8a0-402d49e7c4f0

📥 Commits

Reviewing files that changed from the base of the PR and between 25dc813 and ec1bc1e.

📒 Files selected for processing (30)
  • CONTEXT.md
  • README.md
  • applications/worker.py
  • docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md
  • docs/decisions/README.md
  • engine/persistence/__init__.py
  • engine/persistence/configuration.py
  • engine/persistence/database.py
  • engine/persistence/role_guard.py
  • engine/persistence/schema_security_manifest.yaml
  • engine/persistence/worker_jobs.py
  • engine/supply/__init__.py
  • engine/supply/jobs.py
  • eval/catalogs/security-catalog.schema.json
  • eval/catalogs/security-invariants.yaml
  • infra/postgres/init/10-security-roles.sh
  • migrations/versions/20260722_0006_worker_lease_noop.py
  • scripts/provision_database_roles.py
  • scripts/validate_security_catalog.py
  • tests/catalog/test_validate_security_catalog.py
  • tests/integration/conftest.py
  • tests/integration/test_migrations.py
  • tests/integration/test_postgres_harness.py
  • tests/integration/test_worker_lease.py
  • tests/process/test_processes.py
  • tests/unit/test_database_configuration.py
  • tests/unit/test_database_harness_contract.py
  • tests/unit/test_schema_security_manifest.py
  • tests/unit/test_worker_job_authority.py
  • tests/unit/test_worker_lease.py

# 0029. Bound the first WorkerLease to persistent no-op jobs

- Status: accepted
- Date: 2026-07-22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md"

echo "== file metadata =="
wc -l "$file"
echo

echo "== lines 1-25 =="
cat -n "$file" | sed -n '1,25p'
echo

echo "== nearby decision docs dates/status patterns =="
rg -n "^Date:|^Status:" docs/decisions/0028* docs/decisions/0029* docs/decisions/0030* 2>/dev/null || true

Repository: stone16/context-engine

Length of output: 1500


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
p = Path("docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md")
text = p.read_text()
for i, line in enumerate(text.splitlines(), 1):
    if i <= 25 or "accepted" in line.lower() or "Date:" in line or "Status:" in line:
        pass
print("contains accepted:", "accepted" in text.lower())
print("contains 2026-07-22:", "2026-07-22" in text)
print("contains 2026-07-21:", "2026-07-21" in text)
PY

Repository: stone16/context-engine

Length of output: 237


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Capture the exact snippet around the date and status for the ADR.
nl -ba docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md | sed -n '1,30p'

Repository: stone16/context-engine

Length of output: 200


Use the actual acceptance date

- Date: 2026-07-22 is in the future relative to this review. Update it to the real acceptance date, or drop accepted until the ADR is actually accepted.

🤖 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 `@docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md` at
line 12, Update the ADR metadata date in the decision record to the actual
acceptance date; if it has not yet been accepted, remove or change the accepted
status instead of retaining a future date.

Comment on lines +44 to +50
The token binds the Issue #17 fields that exist: Organization, durable job,
registered `ServicePrincipal` binding, workload, worker audience, the exact
persistent no-op operation, issued-at, expiry, and nonce. The current durable
job row is the authority for those same values. A valid signature alone never
authorizes work. This is a bounded `ServicePrincipal` receiver binding, not the
full canonical `ServiceActor`: Issue #17 has no source/allowed-operation set or
Policy Epoch, so it neither publishes nor claims that broader ActorContext arm.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

Require the registered ServiceActor before activating this worker database boundary.

The repository rule requires worker database access to use a registered least-privilege ServiceActor together with the exact-job signed WorkerLease. These changes instead activate a ServicePrincipal-only carrier and explicitly state that it is not the canonical ServiceActor. Either add the required trusted actor context and enforcement, or keep this activation deferred/fail-closed.

  • docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md#L44-L50: make ServiceActor authorization a prerequisite.
  • CONTEXT.md#L45-L45: include the required ServiceActor in WorkerLease scope.
  • CONTEXT.md#L317-L328: do not describe the ServicePrincipal-only carrier as sufficient.
  • README.md#L21-L25: revise the active-lease claim.
  • README.md#L109-L123: align the Issue #17 activation description with the required actor boundary.
  • docs/decisions/README.md#L30-L30: include the ServiceActor prerequisite in the accepted baseline.

As per coding guidelines: “Worker database access must use a registered least-privilege ServiceActor and a server-minted signed WorkerLease with exact durable-job binding; never impersonate the triggering user or treat ingestion authority as delivery authority.”

📍 Affects 4 files
  • docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md#L44-L50 (this comment)
  • CONTEXT.md#L45-L45
  • CONTEXT.md#L317-L328
  • README.md#L21-L25
  • README.md#L109-L123
  • docs/decisions/README.md#L30-L30
🤖 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 `@docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md`
around lines 44 - 50, Make ServiceActor authorization a prerequisite for worker
database activation alongside the exact-job signed WorkerLease; otherwise keep
activation deferred and fail-closed. Update
docs/decisions/0029-bound-first-worker-lease-to-persistent-no-op-jobs.md
(44-50), CONTEXT.md (45 and 317-328), README.md (21-25 and 109-123), and
docs/decisions/README.md (30) to require the registered least-privilege
ServiceActor, include it in WorkerLease scope, and remove claims that the
ServicePrincipal-only carrier or active lease is sufficient.

Source: Coding guidelines

Comment thread README.md
Comment on lines +85 to +88
健康响应中的 `runtime_delivery: NOT_ACTIVE` 表示默认进程没有生产认证入口。worker
输出中的 `job_behavior: NOT_ACTIVE` 特指默认 CLI 尚未配置生产签名密钥来源、queue/job
loop 或真实 ingestion/publication handler;Issue #17 的 persistent no-op 应用 seam 与
PostgreSQL authority 已激活并由 integration suite 调用。当前数据库测试证明 `compose.yaml` 固定的

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Remove the stale WorkerLease: NOT_ACTIVE claim.

This section activates Issue #17, but README.md line 163 still says WorkerLease is not active. Update the stale status or qualify it as referring only to the full production carrier.

🤖 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 `@README.md` around lines 85 - 88, Update the README status section to remove
the stale “WorkerLease: NOT_ACTIVE” claim. Align the WorkerLease wording with
the activated Issue `#17` persistent no-op seam and PostgreSQL authority, or
explicitly qualify NOT_ACTIVE as applying only to the full production carrier.

@stone16
stone16 merged commit dcf5669 into main Jul 21, 2026
2 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.

worker: reject wrong-org, expired and replayed WorkerLeases

1 participant