diff --git a/.github/workflows/postgresql-integration.yml b/.github/workflows/postgresql-integration.yml deleted file mode 100644 index bad716be3c..0000000000 --- a/.github/workflows/postgresql-integration.yml +++ /dev/null @@ -1,118 +0,0 @@ -name: PostgreSQL Integration - -# The Python Tests coverage job runs without PostgreSQL, so the provider-backed -# integration suites skip there. This workflow qualifies the same commit against -# a disposable PostgreSQL server: the ladder row reports `unverified` when the -# URL is missing and the service suites are asserted by name, so a silent skip -# cannot pass. Remote commands additionally exercise TLS clients, a restricted -# database role, response loss and service restart. - -on: - pull_request: - paths: - - ".github/workflows/postgresql-integration.yml" - - "loopx/control_plane/**" - - "tests/control_plane_ts/**" - - "examples/coordination/remote-authority-*.ts" - - "package.json" - - "package-lock.json" - workflow_dispatch: - push: - branches: - - main - paths: - - ".github/workflows/postgresql-integration.yml" - - "loopx/control_plane/**" - - "tests/control_plane_ts/**" - - "examples/coordination/remote-authority-*.ts" - - "package.json" - - "package-lock.json" - -permissions: - contents: read - -concurrency: - group: postgresql-integration-${{ github.ref }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -jobs: - postgresql-authority: - name: postgresql-authority (real server) - runs-on: ubuntu-latest - timeout-minutes: 10 - services: - postgres: - image: postgres:16.15 - env: - POSTGRES_USER: loopx - POSTGRES_PASSWORD: loopx - POSTGRES_DB: loopx_store - ports: - - 5432:5432 - options: >- - --health-cmd "pg_isready -U loopx -d loopx_store" - --health-interval 5s - --health-timeout 5s - --health-retries 12 - steps: - - name: Check out repository - uses: actions/checkout@v7 - - - name: Set up the qualified Node.js runtime - uses: actions/setup-node@v6 - with: - node-version: "22.22.3" - cache: npm - cache-dependency-path: package-lock.json - - - name: Install locked dependencies - run: npm ci --ignore-scripts - - - name: Create the disposable service database - env: - PGPASSWORD: loopx - run: >- - psql --host 127.0.0.1 --username loopx --dbname postgres - --set ON_ERROR_STOP=1 --command "CREATE DATABASE loopx_service" - - - name: Qualify the PostgreSQL store conformance ladder row - env: - LOOPX_TEST_POSTGRES_URL: postgresql://loopx:loopx@127.0.0.1:5432/loopx_store - run: >- - python3 -m loopx.control_plane.testing.authority_e2e_ladder - --row s2b.postgresql_conformance_live - --report-json postgresql-conformance-report.json - - - name: Assert the service admission suite is available - run: | - set -euo pipefail - node -e 'const scripts = require("./package.json").scripts; if (!scripts["test:postgresql-authority-service"]) { console.error("package.json must expose test:postgresql-authority-service: the service admission path has no CI qualification without it"); process.exit(1); }' - - - name: Qualify the PostgreSQL service admission path - env: - LOOPX_TEST_POSTGRES_SERVICE_URL: postgresql://loopx:loopx@127.0.0.1:5432/loopx_service - LOOPX_TEST_POSTGRES_URL: postgresql://loopx:loopx@127.0.0.1:5432/loopx_store - run: | - set -euo pipefail - npm run test:postgresql-authority-service 2>&1 | tee postgresql-service-admission.log - grep -Fq "PostgreSQL service admits an authorized tenant and rotates a restored incarnation" postgresql-service-admission.log - - - name: Qualify authenticated remote task commands - env: - LOOPX_TEST_POSTGRES_URL: postgresql://loopx:loopx@127.0.0.1:5432/loopx_store - run: | - set -euo pipefail - npm run test:remote-authority 2>&1 | tee remote-authority.log - grep -Fq "lost response and server restart recover the exact receipt" remote-authority.log - - - name: Upload bounded PostgreSQL qualification evidence - if: always() - uses: actions/upload-artifact@v7 - with: - name: postgresql-conformance-report - path: | - postgresql-conformance-report.json - postgresql-service-admission.log - remote-authority.log - if-no-files-found: ignore - retention-days: 7 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b772c03d17..ecf95ce976 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -193,7 +193,6 @@ touch, mention it in the PR instead of fixing it there. | `dco.yml` | every PR | yes (`Sign-off`) | `Signed-off-by` on contribution commits; verified GitHub-generated two-parent merges are exempt | | `ci-maintenance.yml` | opened, reopened, edited, updated or closed PR (`pull_request_target`, trusted base code only) | no | inspects potential stale PR comparisons without mutating the head or target; cancels obsolete Python qualification runs after rechecking the PR and source branch | | `dependency-review.yml` | every PR | no | dependency changes introduced by the PR | -| `postgresql-integration.yml` | control-plane or npm lockfile paths | no | PostgreSQL authority store and service on a temporary instance | | `package-smoke.yml` | `loopx-core` and extension package paths | no | `loopx-core` and extension packages install, entrypoints, and example schemas | | `release-artifacts.yml` | `loopx/`, packaging, and lockfile paths | no | release identity and a release build from this source | | `runtime-snapshots.yml` | runtime artifact, installer, and updater paths | no | qualified, revision-addressed runtime archives and the main channel pointer | diff --git a/README.ja.md b/README.ja.md index 8382eccc47..29deb6c6a4 100644 --- a/README.ja.md +++ b/README.ja.md @@ -169,7 +169,7 @@ LoopX はそのオープンでハーネス非依存のコントロールレイ | 規模 | LoopX の動かし方 | 権限、連携、ガバナンスの姿勢 | | --- | --- | --- | | **個人開発者とローカルエージェント** | 外部インフラなしで `python3 -m pip install --upgrade loopx`。既存の CLI/デスクトップのランタイムを接続します | ハードリースを備えた正規のローカル **SQLite** ストア(既存の File セットアップも利用可)。アイドル時のトークン消費ゼロで、再起動やコンパクションを乗り越えます | -| **エンジニアリングチームとエンタープライズへの道筋** | 対等なワーカーが todo を claim し、Desktop Workspace、GitHub、Lark を通じて共有ゴール上でレビュー済みの成果物を返します([3 エージェントのデモ](examples/collaboration-delivery/README.md)) | ワーカーのオーナーシップと人間の承認ゲートを維持します。段階的な **PostgreSQL** リモート権限([プロバイダー](loopx/control_plane/coordination/postgresql_authority_store.ts))や、計画中の組織ガバナンス(RBAC、SSO、クォータ、署名付き監査)へと拡張できます。詳しくは[導入と SaaS のロードマップ](docs/product/roadmaps/saas-opportunity-assessment.md)をご覧ください | +| **エンジニアリングチームとエンタープライズへの道筋** | 対等なワーカーが todo を claim し、Desktop Workspace、GitHub、Lark を通じて共有ゴール上でレビュー済みの成果物を返します([3 エージェントのデモ](examples/collaboration-delivery/README.md)) | ワーカーのオーナーシップと人間の承認ゲートを維持します。ローカルの **File**/**SQLite** 権限およびアウトバウンド HTTPS **RemoteAuthority** クライアント契約([ワイヤー契約](loopx/control_plane/coordination/remote_authority_contract.ts))は `loopx` 内で Apache-2.0 のまま維持され、サーバーサイドの PostgreSQL 権限、分散コーディネーション、マルチテナント組織ガバナンス(RBAC、SSO、クォータ、署名付き監査)は `loopx-project/loopx-cloud` に配置されます。詳しくは[ライセンス境界](docs/project/licensing.md#open-core-and-commercial-boundaries)と[導入と SaaS のロードマップ](docs/product/roadmaps/saas-opportunity-assessment.md)をご覧ください | ## LoopX の動作を見る diff --git a/README.md b/README.md index bcf0f3ad6c..1f9b698b84 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ operate 24/7 with explicit durability and human gates: | Scale | How LoopX runs | Authority, collaboration & governance posture | | --- | --- | --- | | **Solo developer & local agent** | `python3 -m pip install --upgrade loopx` with zero external infrastructure; connect your existing CLI/Desktop runtime | Canonical local **SQLite** store with hard leases (plus existing File setups), surviving restarts and compaction at zero idle tokens | -| **Engineering team & enterprise path** | Peer workers claim todos and return reviewed artifacts on shared Goals via Desktop Workspace, GitHub, and Lark ([three-agent demo](examples/collaboration-delivery/README.md)) | Preserves worker ownership and human approval gates; extends to staged **PostgreSQL** remote authority ([provider](loopx/control_plane/coordination/postgresql_authority_store.ts)) and planned org governance—RBAC, SSO, quotas, signed audit—in the [adoption & SaaS roadmap](docs/product/roadmaps/saas-opportunity-assessment.md) | +| **Engineering team & enterprise path** | Peer workers claim todos and return reviewed artifacts on shared Goals via Desktop Workspace, GitHub, and Lark ([three-agent demo](examples/collaboration-delivery/README.md)) | Preserves worker ownership and human approval gates; keeps local **File**/**SQLite** authority and the outbound HTTPS **RemoteAuthority** client contract ([wire contract](loopx/control_plane/coordination/remote_authority_contract.ts)) Apache-2.0 in `loopx`, while server-side PostgreSQL authority, distributed coordination, and multi-tenant org governance—RBAC, SSO, quotas, signed audit—live in `loopx-project/loopx-cloud` ([licensing boundary](docs/project/licensing.md#open-core-and-commercial-boundaries), [adoption & SaaS roadmap](docs/product/roadmaps/saas-opportunity-assessment.md)) | ## See LoopX in Action diff --git a/README.zh-CN.md b/README.zh-CN.md index ee67ed5cb2..ab37ffc989 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -152,7 +152,7 @@ LoopX 把这套**开源、Harness 中立**的控制面直接做成可运行基 | 使用形态 | LoopX 运行方式 | 权威存储、协作与治理形态 | | --- | --- | --- | | **个人开发者 & 本地 Agent** | `python3 -m pip install --upgrade loopx` 零外部依赖直接运行,接入已有 CLI / 桌面端 | 默认本地权威 **SQLite** + hard lease(兼顾既有 File 配置),跨重启、跨压缩保留现场且闲置零 Token | -| **工程团队 & 组织级扩展** | 多 Agent 认领 Todo、基于同一 Goal 经工作区 / GitHub / 飞书协同交付与互审([三 Agent 协作示例](examples/collaboration-delivery/README.md)) | 保留各自责任边界与人工审批门;支持向预置的 **PostgreSQL** 远程共享权威([源码实现](loopx/control_plane/coordination/postgresql_authority_store.ts))以及[组织治理路线图](docs/product/roadmaps/saas-opportunity-assessment.zh-CN.md)中的 RBAC、SSO 与审计能力平滑演进 | +| **工程团队 & 组织级扩展** | 多 Agent 认领 Todo、基于同一 Goal 经工作区 / GitHub / 飞书协同交付与互审([三 Agent 协作示例](examples/collaboration-delivery/README.md)) | 保留各自责任边界与人工审批门;本地 **File**/**SQLite** 权威及出站 HTTPS **RemoteAuthority** 客户端契约([协议契约](loopx/control_plane/coordination/remote_authority_contract.ts))在 `loopx` 中保持 Apache-2.0 开源,服务端 PostgreSQL 权威、分布式协调与多租户组织治理(RBAC、SSO、配额与签名审计)承载于 `loopx-project/loopx-cloud`(见[许可证边界](docs/project/licensing.md#open-core-and-commercial-boundaries)与[组织治理路线图](docs/product/roadmaps/saas-opportunity-assessment.zh-CN.md)) | ## 看看 LoopX 如何工作 diff --git a/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.md b/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.md index 31be540ac3..e550b66e75 100644 --- a/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.md +++ b/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.md @@ -347,7 +347,8 @@ now rejects unsupported providers before storage or writer-fence effects; [CLI controls](../../../tests/control_plane/test_canonical_authority_fixture.py) verify actual File/SQLite readback and preserve state on rejection. Previously, a PostgreSQL label silently initialized File authority. The existing -[PostgreSQL remote task candidate](../../reference/remote-authority-task-service-v0.md) +[outbound `RemoteAuthority` client contract](../../../loopx/control_plane/coordination/remote_authority_contract.ts) +(and the [`v2.4.0` PostgreSQL remote task candidate](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md), whose server-side authority now lives in `loopx-project/loopx-cloud`) is separately qualified through real TLS, restricted roles, task lifecycle and read-only quota observation. It admits no ordinary host Turn. These service checks therefore do not qualify an ordinary PostgreSQL feedback Turn or justify diff --git a/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.zh-CN.md b/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.zh-CN.md index 1f0a377aa7..eed7afcc87 100644 --- a/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.zh-CN.md +++ b/docs/architecture/rfcs/mechanism-convergence-and-learning-loop-v0.zh-CN.md @@ -324,7 +324,7 @@ Mapping 拒绝顺序、精确整数 IO、会话 ID 解码、轮询时钟及 pend [CLI 对照](../../../tests/control_plane/test_canonical_authority_fixture.py) 验证真实 File/SQLite 回读及拒绝时状态不变。此前 PostgreSQL 标签会静默 初始化 File authority。既有 -[PostgreSQL remote task 候选](../../reference/remote-authority-task-service-v0.md) +[出站 `RemoteAuthority` 客户端契约](../../../loopx/control_plane/coordination/remote_authority_contract.ts)(以及服务端权威现已归入 `loopx-project/loopx-cloud` 的 [`v2.4.0` PostgreSQL remote task 候选](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md)) 另经真实 TLS、受限角色、任务生命周期和只读 quota 观察验证,仍不准入普通 host Turn。因此这些服务检查不能取得普通 PostgreSQL 反馈 Turn 资格, 也不构成本切片新增执行 profile 的理由。 diff --git a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md index d52d780fe1..406c55f7ca 100644 --- a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md +++ b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md @@ -138,7 +138,7 @@ PR to the remaining work. [Decision and exits](ledger/shared-goal-authority-stat [Roadmap](loopx-overall-roadmap-v0.md) R5 reuses D1 projection, D2 real-backend/capacity/applicable ten-day soak and D3 fenced cutover. R6 connects the selected shared profile to authenticated local/cloud execution. R1–R3 can advance on supported profiles without waiting for PostgreSQL or whole-Goal default promotion. -`e94759d88` adds [PostgreSQL service admission](../../reference/postgresql-authority-service-v0.md), with authentication/tenant verification injection and identity rotation. The [candidate remote task service](../../reference/remote-authority-task-service-v0.md) reuses that seam for TLS claim/progress/lease/read commands, server-owned scoped credentials, bounded pooling, incarnation pinning and receipt recovery. Its source-checkout tests use independent client processes and a restricted role on real PostgreSQL, including lost responses and service restart. This does not qualify deployed local/cloud hosts, shared budget ownership, Host supervision, App/CLI integration, failover, capacity/soak or promotion; these remain with the P lane and R6/D1–D3 owners. R7 must separately report registration, active executors and measured capacity. Directory, presence, a plan or one source read grants no shared authority. Existing fail-closed source, receipt/replay and rollback contracts remain. +`e94759d88` added [PostgreSQL service admission (`v2.4.0`)](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/postgresql-authority-service-v0.md), with authentication/tenant verification injection and identity rotation, and the [`v2.4.0` candidate remote task service](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) reused that seam for TLS claim/progress/lease/read commands, server-owned scoped credentials, bounded pooling, incarnation pinning and receipt recovery (in the Open-Core boundary split, the outbound HTTPS [`RemoteAuthority` wire contract](../../../loopx/control_plane/coordination/remote_authority_contract.ts) and client remain Apache-2.0 in `loopx`, while server-side PostgreSQL authority and multi-tenant services live in `loopx-project/loopx-cloud`). This does not qualify deployed local/cloud hosts, shared budget ownership, Host supervision, App/CLI integration, failover, capacity/soak or promotion; these remain with the P lane and R6/D1–D3 owners. R7 must separately report registration, active executors and measured capacity. Directory, presence, a plan or one source read grants no shared authority. Existing fail-closed source, receipt/replay and rollback contracts remain. **Observation retirement (2026-09-24).** [Current delivery inventory](ledger/shared-goal-authority-state-provider-v0/2026-09-24-observation-retirement.md) separates merged code, open PRs and qualification gates. This delivery removes @@ -1656,7 +1656,8 @@ Use two distinct lifetimes of the same Goal alias in access qualification. | Client omits lifetime, server independently binds current lifetime and audience | Evaluate the bound resource and action through the ordinary ACL. Deny outside that scope. | | Explicit stale lifetime or different resource/version | Deny the mismatched action, even with a valid key and a current store connection. | -The [v0 remote task service](../../reference/remote-authority-task-service-v0.md) +The [`v2.4.0` v0 remote task service](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) +(whose outbound [`RemoteAuthority` wire contract](../../../loopx/control_plane/coordination/remote_authority_contract.ts) remains in `loopx` while server-side implementation lives in `loopx-project/loopx-cloud`) is a source-checkout candidate. Its strict request and policy accept tenant/Goal/actor/operation grants and provider incarnation. They cannot express the resource or Goal lifetime ACL above. A scoped key for that service diff --git a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md index 487aef8cc5..8bd1043537 100644 --- a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md +++ b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md @@ -121,7 +121,7 @@ SQLite 跨 provider 恢复复用逻辑归档。这不晋升 provider/默认值 [统一路线](loopx-overall-roadmap-v0.zh-CN.md) R5 复用本 RFC 的 D1 投影、D2 真实 backend/容量/适用十日 soak、D3 fenced cutover;R6 再把所选 shared profile 接入认证的本地/云端路径。R1–R3 可在已支持 profile 上前进,不等待 PostgreSQL 或整 Goal 默认晋升。 -`e94759d88` 已增加 [PostgreSQL service admission](../../reference/postgresql-authority-service-v0.zh-CN.md),包含认证/tenant 验证注入与 identity rotation。[候选远程任务服务](../../reference/remote-authority-task-service-v0.md) 复用该边界,提供 TLS 认领、进度、租约与读取命令,以及服务端拥有的限定范围凭证、有界连接池、incarnation 绑定和回执恢复。源码入口测试使用独立客户端进程、真实 PostgreSQL 与受限数据库角色,覆盖响应丢失和服务重启。这不代表已通过本地/云端实机、共享预算所有权、Host 监督、App/CLI 接入、故障切换、容量/soak 或晋升资格;这些仍由 P lane 与 R6/D1–D3 owner 承接。R7 百 Agent 资格需独立报告注册数、活跃执行器和实测容量;目录、presence、管家计划或一次 source read 不授予 shared authority。既有 source-failure fail-closed、receipt/replay 和 rollback 合同保持。 +`e94759d88` 曾增加 [PostgreSQL service admission(`v2.4.0`)](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/postgresql-authority-service-v0.zh-CN.md),包含认证/tenant 验证注入与 identity rotation,[`v2.4.0` 候选远程任务服务](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) 复用该边界提供 TLS 认领、进度、租约与读取命令、服务端限定范围凭证、有界连接池、incarnation 绑定与回执恢复(在 Open-Core 架构分层后,出站 HTTPS [`RemoteAuthority` 协议契约](../../../loopx/control_plane/coordination/remote_authority_contract.ts)与客户端保留在 Apache-2.0 的 `loopx` 中,服务端 PostgreSQL 权威与多租户协调迁移至 `loopx-project/loopx-cloud`)。这不代表已通过本地/云端实机、共享预算所有权、Host 监督、App/CLI 接入、故障切换、容量/soak 或晋升资格;这些仍由 P lane 与 R6/D1–D3 owner 承接。R7 百 Agent 资格需独立报告注册数、活跃执行器和实测容量;目录、presence、管家计划或一次 source read 不授予 shared authority。既有 source-failure fail-closed、receipt/replay 和 rollback 合同保持。 ## 当前实现事实 @@ -1310,7 +1310,7 @@ stdio MCP 桥接 HTTPS。原生远程 MCP 需通过宿主身份和认证资格 | 客户端省略生命周期,服务端独立绑定当前生命周期及受众 | 按普通 ACL 检查已绑定资源和动作。范围外拒绝。 | | 显式提供过期生命周期或错误资源或版本 | 即使密钥有效且连接的是当前存储,仍拒绝不匹配的动作。 | -[v0 远程任务服务](../../reference/remote-authority-task-service-v0.md) 是源码候选。 +[`v2.4.0` v0 远程任务服务](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md)(其出站 [`RemoteAuthority` 协议契约](../../../loopx/control_plane/coordination/remote_authority_contract.ts)保留于 `loopx`,服务端实现承载于 `loopx-project/loopx-cloud`)是源码候选。 其严格请求与策略支持租户、Goal、actor、动作范围和存储 incarnation,无法表达上述资源或 Goal 生命周期 ACL。凭该服务的 scoped key,本身无法验证跨人阅读及证据交付。开放跨人 接入前,通过已有服务和资源 owner 补齐检查。本地入口、provider 选择、Goal 身份激活及 diff --git a/docs/product/releases/v2.4.0.md b/docs/product/releases/v2.4.0.md index 57218f78ee..78685027c9 100644 --- a/docs/product/releases/v2.4.0.md +++ b/docs/product/releases/v2.4.0.md @@ -131,7 +131,9 @@ loopx authority-archive upgrade --all-known --require-current ### Remote task gateway -**Activation:** From a source checkout with a qualified Node runtime and `npm ci --ignore-scripts`, provision an isolated PostgreSQL authority schema, a restricted runtime role, a service policy, and TLS credentials (or start the self-hosted PostgreSQL/TLS Compose bundle in `examples/coordination/remote-authority-compose`). Start the service as shown below. Optional scheduler observation requires an explicit `remote_authority.enabled: true` for one registered Goal plus its HTTPS origin, verified tenant and store identity, scoped actor, stable existing probe Todo, and private CA path when applicable. [#8508](https://github.com/loopx-project/loopx/pull/8508), [#8765](https://github.com/loopx-project/loopx/pull/8765), [#8887](https://github.com/loopx-project/loopx/pull/8887) +> **Post-`v2.4.0` Open-Core note:** On `main` (`2.4 -> 3.0`), the outbound HTTPS `RemoteAuthority` wire contract (`loopx/control_plane/coordination/remote_authority_contract.ts`), verified TLS client (`examples/coordination/remote-authority-client.ts`), and read-only scheduler gateway (`loopx/control_plane/coordination/remote_authority_gateway.py`) remain Apache-2.0 in `loopx-project/loopx`, while server-side PostgreSQL authority, `remote-authority-service.ts`, and Compose deployment bundles live in `loopx-project/loopx-cloud` (the `v2.4.0` tag snapshot remains available at the pinned tag links below). + +**Activation (`v2.4.0` snapshot):** From the `v2.4.0` source tag with a qualified Node runtime and `npm ci --ignore-scripts`, provision an isolated PostgreSQL authority schema, a restricted runtime role, a service policy, and TLS credentials (or start the `v2.4.0` self-hosted PostgreSQL/TLS Compose bundle). Optional scheduler observation in `loopx` requires an explicit `remote_authority.enabled: true` for one registered Goal plus its HTTPS origin, verified tenant and store identity, scoped actor, stable existing probe Todo, and private CA path when applicable. [#8508](https://github.com/loopx-project/loopx/pull/8508), [#8765](https://github.com/loopx-project/loopx/pull/8765), [#8887](https://github.com/loopx-project/loopx/pull/8887) **Validation:** Recover a known operation receipt with the example client and a prepared `command.json`. For the configured Goal, use `quota should-run` to read the bounded remote observation through the normal scheduler. @@ -139,12 +141,10 @@ loopx authority-archive upgrade --all-known --require-current **Authority boundary:** The scoped principal and restricted PostgreSQL role define task mutation rights. Host observation is read-only. For a resource-scoped read-only listener, select `loopx_remote_authority_scoped_read_policy_v1` and authorize each exact Goal lifetime, Todo, and audience. Supply its original creation or first-adoption receipt; absent proof stays unavailable. That listener rejects writes and withholds free-form notes, lease keys, and global provider revisions. Remove the matching grant or ACL entry to revoke subsequent admissions. Local admission, owner Stop, and quota remain authoritative. [#8861](https://github.com/loopx-project/loopx/pull/8861) -**Docs:** [Remote authority task service](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) +**Docs:** [`v2.4.0` Remote authority task service](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) · [Open-Core outbound client contract](../../../loopx/control_plane/coordination/remote_authority_contract.ts) ```bash -node --experimental-strip-types examples/coordination/remote-authority-service.ts \ - --policy "$SERVICE_POLICY" --cert "$TLS_CERT" --key "$TLS_KEY" --host 127.0.0.1 --port 8443 -# In the scoped client environment, with the service credential loaded privately: +# Outbound HTTPS client and read-only scheduler observation (retained in loopx): node --experimental-strip-types examples/coordination/remote-authority-client.ts \ --endpoint "$SERVICE_HTTPS_ORIGIN" --ca "$TLS_CA" --recover < command.json loopx --format json quota should-run --goal-id "$LOOPX_GOAL_ID" --agent-id "$LOOPX_AGENT_ID" @@ -520,7 +520,9 @@ loopx doctor #### Remote task gateway -**启用:** 从源码检出准备合格的 Node、`npm ci --ignore-scripts`、隔离 PostgreSQL authority schema、受限角色、服务策略与 TLS 证书,运行服务示例(或使用 `examples/coordination/remote-authority-compose` 自托管 Compose 部署包)。调度器观测另需在某个已注册 Goal 显式设置 `remote_authority.enabled: true`,绑定 HTTPS origin、已核验的 tenant 和 store identity、授权 Agent、既有 probe Todo 和所需私有 CA。 [#8508](https://github.com/loopx-project/loopx/pull/8508), [#8765](https://github.com/loopx-project/loopx/pull/8765), [#8887](https://github.com/loopx-project/loopx/pull/8887) +> **`v2.4.0` 之后的 Open-Core 边界说明:** 在 `main`(`2.4 -> 3.0`)上,出站 HTTPS `RemoteAuthority` 协议契约(`loopx/control_plane/coordination/remote_authority_contract.ts`)、TLS 客户端(`examples/coordination/remote-authority-client.ts`)与调度器只读网关(`loopx/control_plane/coordination/remote_authority_gateway.py`)继续在 `loopx-project/loopx` 中保持 Apache-2.0 开源;服务端 PostgreSQL 权威、`remote-authority-service.ts` 与 Compose 部署包归入 `loopx-project/loopx-cloud`(`v2.4.0` 发布快照继续保留在下方 tag 链接中)。 + +**启用(`v2.4.0` 快照):** 从 `v2.4.0` 源码 tag 准备合格的 Node、`npm ci --ignore-scripts`、隔离 PostgreSQL authority schema、受限角色、服务策略与 TLS 证书并运行服务(或使用 `v2.4.0` 自托管 Compose 包)。`loopx` 中的调度器只读观测需在某个已注册 Goal 显式设置 `remote_authority.enabled: true`,绑定 HTTPS origin、已核验的 tenant 和 store identity、授权 Agent、既有 probe Todo 和所需私有 CA。 [#8508](https://github.com/loopx-project/loopx/pull/8508), [#8765](https://github.com/loopx-project/loopx/pull/8765), [#8887](https://github.com/loopx-project/loopx/pull/8887) **验证:** 用示例客户端和已准备的 `command.json` 读取既有操作回执。通过 `quota should-run` 查看当前 Goal 的远程只读观测。 @@ -528,12 +530,10 @@ loopx doctor **权限边界:** 远程写入由受限 principal、数据库角色和策略控制。Host 观测提供只读输入。逐资源只读 listener 选择 `loopx_remote_authority_scoped_read_policy_v1`,精确授权 Goal lifetime、Todo 与接收者,并提供原创建或首次采用回执;缺少来源证明时保持不可用。该 listener 拒绝写命令,不返回自由文本、租约密钥或全局 provider revision。移除对应 grant 或 ACL 项可撤销后续准入,继续遵循本地准入、Stop 和额度约束。 [#8861](https://github.com/loopx-project/loopx/pull/8861) -**文档:** [远程任务网关](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) +**文档:** [`v2.4.0` 远程任务网关](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md) · [Open-Core 出站客户端契约](../../../loopx/control_plane/coordination/remote_authority_contract.ts) ```bash -node --experimental-strip-types examples/coordination/remote-authority-service.ts \ - --policy "$SERVICE_POLICY" --cert "$TLS_CERT" --key "$TLS_KEY" --host 127.0.0.1 --port 8443 -# 另在受授权的客户端环境私下加载服务凭据后执行: +# 出站 HTTPS 客户端与调度器只读观测(保留于 loopx): node --experimental-strip-types examples/coordination/remote-authority-client.ts \ --endpoint "$SERVICE_HTTPS_ORIGIN" --ca "$TLS_CA" --recover < command.json loopx --format json quota should-run --goal-id "$LOOPX_GOAL_ID" --agent-id "$LOOPX_AGENT_ID" diff --git a/docs/product/use-cases/community/faq.md b/docs/product/use-cases/community/faq.md index 7f72192147..03222d1d57 100644 --- a/docs/product/use-cases/community/faq.md +++ b/docs/product/use-cases/community/faq.md @@ -84,15 +84,16 @@ and your installed host for the supported native path. Later changes on ## Can different people share PostgreSQL and wait for approvals? -The [remote authority task service](../../../reference/remote-authority-task-service-v0.md) -documents an opt-in source-checkout candidate. It accepts authenticated -claim, progress, lease and read operations through a service. PostgreSQL holds -the authoritative transactions and receipts. A compare-and-set (CAS) check -prevents an outdated writer from committing against a newer state. If a -response is lost, read back the original operation under the same identity -before retrying. Evidence for the candidate includes real-backend tests with -independent clients, but enabling cross-host execution requires its own deployed -host and service qualification. The [current scoped-read candidate](https://github.com/loopx-project/loopx/blob/c3e3ac56586fbbc686e3e2d01b979fe59a62dd19/docs/reference/remote-authority-task-service-v0.md#scoped-todo-reads) +The outbound HTTPS [`RemoteAuthority` wire contract and client](../../../../loopx/control_plane/coordination/remote_authority_contract.ts) +stay Apache-2.0 in `loopx`, while server-side PostgreSQL authority, distributed +coordination, and multi-tenant governance live in `loopx-project/loopx-cloud` +(historical `v2.4.0` candidate notes remain at [`v2.4.0` remote-authority-task-service-v0.md](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md)). +Clients submit authenticated claim, progress, lease and read intentions over +verified HTTPS; the server holds authoritative transactions and receipts with +compare-and-set (CAS) checks so an outdated writer cannot commit against a newer +state. If a response is lost, read back the original operation under the same +identity before retrying. Enabling cross-host execution requires its own deployed +host and service qualification. The [historical scoped-read candidate](https://github.com/loopx-project/loopx/blob/c3e3ac56586fbbc686e3e2d01b979fe59a62dd19/docs/reference/remote-authority-task-service-v0.md#scoped-todo-reads) adds explicit Todo and audience checks. Its scoped listener rejects writes. [PR #8747](https://github.com/loopx-project/loopx/pull/8747) added the broader cross-person access design to the diff --git a/docs/product/use-cases/community/faq.zh-CN.md b/docs/product/use-cases/community/faq.zh-CN.md index 9c31988e1c..07de9fc423 100644 --- a/docs/product/use-cases/community/faq.zh-CN.md +++ b/docs/product/use-cases/community/faq.zh-CN.md @@ -68,12 +68,12 @@ Goal,LoopX 会为它创建一个 SQLite 权威数据库,保存 Todo、租约 ## 多人能否共用 PostgreSQL、异步等待审批 -[远程任务服务](../../../reference/remote-authority-task-service-v0.md)是可主动启用的 -源码候选。客户端通过带认证的服务领取任务、提交进度、续租和读回结果, -由 PostgreSQL 保存事务与回执。写入时用 CAS 比较当前版本,已有更新便拒绝本次 -过期写入。服务若在提交后丢了响应,先用原操作身份查回执,再决定是否重试。 -候选已有独立客户端的真实后端测试;跨机器部署仍需该宿主与服务组合的实际验收。 -[当前源码的限定读取候选](https://github.com/loopx-project/loopx/blob/c3e3ac56586fbbc686e3e2d01b979fe59a62dd19/docs/reference/remote-authority-task-service-v0.md#scoped-todo-reads) +出站 HTTPS [`RemoteAuthority` 协议契约与客户端](../../../../loopx/control_plane/coordination/remote_authority_contract.ts) +在 `loopx` 中保持 Apache-2.0 开源,服务端 PostgreSQL 权威、分布式协调与多租户治理承载于 +`loopx-project/loopx-cloud`(`v2.4.0` 历史候选文档保留于 [`v2.4.0` 远程任务服务说明](https://github.com/loopx-project/loopx/blob/v2.4.0/docs/reference/remote-authority-task-service-v0.md))。 +客户端经已校验证书的 HTTPS 提交认领、进度、续租和读取意图,服务端保存事务与回执并用 CAS +比较当前版本,已有更新便拒绝过期写入;若在提交后丢失响应,先用原操作身份查回执再决定是否重试。 +跨机器部署仍需该宿主与服务组合的实际验收。[历史限定读取候选](https://github.com/loopx-project/loopx/blob/c3e3ac56586fbbc686e3e2d01b979fe59a62dd19/docs/reference/remote-authority-task-service-v0.md#scoped-todo-reads) 校验明确获准的 Todo 与访问者,并拒绝写入。 [PR #8747](https://github.com/loopx-project/loopx/pull/8747) 已将跨人资源访问设计合入 diff --git a/docs/project/licensing.md b/docs/project/licensing.md index 48f2af5b13..b66faf654b 100644 --- a/docs/project/licensing.md +++ b/docs/project/licensing.md @@ -99,10 +99,26 @@ amends those grants. ## Open Core And Commercial Boundaries -This repository's unified open source core is Apache-2.0. A separately -delivered Managed Control Plane, enterprise-only modules, hosted operations, -or implementation and support services may use separate commercial terms when -they are not part of this repository's Apache-2.0 core distribution. +This repository's unified open source core (`loopx-project/loopx`) is 100% +Apache-2.0. In particular: + +- Local `File` and `SQLite` authority stores, portable state export/archive, + the provider injection SPI (`local_authority_provider.ts`), and the outbound + HTTPS `RemoteAuthority` wire contract and client + ([`remote_authority_contract.ts`](../../loopx/control_plane/coordination/remote_authority_contract.ts), + [`remote_authority_http.ts`](../../loopx/control_plane/coordination/remote_authority_http.ts), + [`remote_authority_probe.ts`](../../loopx/control_plane/coordination/remote_authority_probe.ts), + [`remote_authority_gateway.py`](../../loopx/control_plane/coordination/remote_authority_gateway.py), + and [`remote-authority-client.ts`](../../examples/coordination/remote-authority-client.ts)) + remain Apache-2.0 in `loopx-project/loopx`. +- Server-side PostgreSQL authority, distributed lease coordination, and + multi-tenant governance live in `loopx-project/loopx-cloud` (preserving full + Apache-2.0 `LICENSE` and `NOTICE` attributions on any migrated base files). + +A separately delivered Managed Control Plane (`loopx-project/loopx-cloud`), +enterprise-only modules, hosted operations, or implementation and support +services may use separate commercial terms when they are not part of this +repository's Apache-2.0 core distribution. Apache-2.0 itself permits commercial use, modification, and distribution subject to its terms. The separate project-name and logo guidance in diff --git a/docs/reference/README.md b/docs/reference/README.md index e65ae7b7a9..a15ef0a037 100644 --- a/docs/reference/README.md +++ b/docs/reference/README.md @@ -11,10 +11,10 @@ Current groups: registration, extension packaging, readiness, and lifecycle boundaries. - [Project skill delivery](../../loopx/capabilities/project_skill_delivery/README.md): release-owned, project-local skill discovery and managed-copy lifecycle. -- [PostgreSQL authority service admission v0](postgresql-authority-service-v0.md): opt-in - authentication, tenant authorization, and restore-incarnation rotation. -- [Remote authority task service v0](remote-authority-task-service-v0.md): candidate - HTTPS task commands, scoped identity policy and durable receipt recovery. +- [Remote authority client wire contract v0](../../loopx/control_plane/coordination/remote_authority_contract.ts): + Apache-2.0 outbound HTTPS task-command wire contract and TLS client in `loopx` + (server-side PostgreSQL authority and multi-tenant coordination live in + `loopx-project/loopx-cloud`). - [Usage ping](usage-ping.md): default-on basic usage statistics, disclosure/opt-out and exact payload, opt-out switches, and the published active-installation counts. diff --git a/docs/reference/postgresql-authority-service-v0.md b/docs/reference/postgresql-authority-service-v0.md deleted file mode 100644 index ad98c679f1..0000000000 --- a/docs/reference/postgresql-authority-service-v0.md +++ /dev/null @@ -1,106 +0,0 @@ -# PostgreSQL authority service admission v0 - -This document defines the service-owned admission boundary for the switchable -PostgreSQL authority provider. It is an opt-in Stage 2B seam: it does not make -PostgreSQL the default, change file/SQLite selection, add a runtime caller, or -claim promotion readiness. - -## Boundary - -`PostgreSqlAuthorityService` receives an opaque transport credential, a tenant, -a goal, and the expected database-incarnation identity. It calls injected -authentication and tenant-authorization functions before opening the provider. -The credential is never persisted, passed to PostgreSQL, or exposed through the -provider-neutral `AuthorityStore` contract. A denied or unverifiable principal -fails closed before a database connection is opened. - -The service returns one of these typed outcomes: - -| Outcome | Meaning | -| --- | --- | -| `opened` | The principal is authenticated, authorized for the tenant, and the database identity matches the requested binding. | -| `principal_unauthenticated` | Authentication rejected the opaque credential or returned an invalid principal. | -| `principal_verification_unavailable` | Authentication could not be completed. | -| `tenant_unauthorized` | The authenticated principal is not allowed to use the tenant. | -| `tenant_authorization_unavailable` | Tenant policy could not be evaluated. | -| `store_identity_unavailable` | Provider metadata could not be read. | -| `store_identity_mismatch` | The requested incarnation is not the database's current incarnation. | - -The service is intentionally in-process. A deployment supplies its own -transport, credential verifier, tenant policy, connection pool, and secret -handling. This module does not grant network access, actor ownership, lease -ownership, cross-host synchronization, or promotion authority. - -## Restore-incarnation rotation - -`rotatePostgreSqlAuthorityStoreIdentity` is an administrative operation owned by -the authenticated service deployment. It locks the singleton metadata row, -verifies the expected identity, and atomically writes a newly minted -`postgresql:<32 lowercase hex>` identity. It does not rewrite heads, commits, -events, receipts, goals, or operation IDs. - -Provider revision tokens contain the database identity. Consequently, tokens -minted before a restore rotation become stale and conflict, while the durable -head and receipt history remain readable through a store opened with the new -identity. A wrong expected identity is rejected without a write. If the server -loses the response after `COMMIT`, the result is `ambiguous`; the service must -read metadata before retrying, rather than guessing whether rotation applied. - -## Retained history and synthetic seed - -PostgreSQL schema v1 keeps the latest head, an append-only checkpoint at -cursors 1, 65, 129, and so on, and one delta for each other new commit. Every -new journal row binds its resulting projection digest. A historical read -starts at the covering checkpoint (or a retained v0 full projection) and -checks the full cursor interval, including any prefix before the page. A -missing checkpoint, gap, or digest mismatch fails the scan. Events and -receipts remain tied to the original transaction. - -Only an administrator runs `loopx postgres setup --execute`. When upgrading -v0, pass that database's existing `--store-identity`. Setup preserves full -projection rows and carries forward the journal's table-level `SELECT` and -`INSERT` grants to checkpoints, including grants through group roles. Setup -applies any explicit `--runtime-role` provisioning in the same transaction as -the format change under the lock used by writers and identity rotation. For -deployments with custom column-level privileges, provision the runtime role -explicitly. An already initialized v1 store can also use its existing identity -and `--runtime-role` to provision access. Keep a verified pre-upgrade backup and -quiesce old callers before upgrading. Verify historical reads and a new -checkpoint append with the restricted runtime credentials in an isolated -restore before activation. A rollback to v0 requires the verified v0 backup. - -Use `loopx postgres local-to-pg-seed --goal-id ` to preview -a sanitized File or SQLite head at cursor 1. Preview never opens the target. -`--output` optionally creates a private file or reuses an identical regular -file. Existing different content and symbolic links cause the command to fail -before connecting to the target. With `--execute`, use a provisioned restricted connection via -`LOOPX_POSTGRES_URL` and optionally pin it with `--store-identity`. The seed -command neither installs a schema nor changes roles. It writes only to an empty -target Goal. Repeating the same seed command, including the same `--output`, -recovers its original receipt. Private -direction declarations and their dependent usage evidence are omitted together -if the declaration cannot be retained. Review the synthetic input before use: -the privacy filter looks for known private records and tokens, and cannot -classify arbitrary sensitive prose. The seed deliberately discards prior -history and does not migrate or activate a real Goal; use the retained journal -archive for history export, audit, or recovery. - -## Validation - -The public synthetic fixture is -[`postgresql_authority_service_v0.json`](../../tests/fixtures/control_plane/postgresql_authority_service_v0.json). -It covers authenticated admission, authentication denial, tenant denial, -identity drift, and restore rotation without credentials or private database -details. - -Run the deterministic seam tests with: - -```sh -npm run typecheck:control-plane -npm run test:postgresql-authority-service -``` - -Run the real PostgreSQL path against an isolated disposable database by setting -`LOOPX_TEST_POSTGRES_SERVICE_URL` and invoking the same script. The database -must be disposable and separate from any active goal or other PostgreSQL test -schema; the integration test mutates only its own tenant/goal and metadata. diff --git a/docs/reference/postgresql-authority-service-v0.zh-CN.md b/docs/reference/postgresql-authority-service-v0.zh-CN.md deleted file mode 100644 index 74b356f11d..0000000000 --- a/docs/reference/postgresql-authority-service-v0.zh-CN.md +++ /dev/null @@ -1,83 +0,0 @@ -# PostgreSQL authority service 准入 v0 - -本文定义可切换 PostgreSQL authority provider 的服务侧准入边界。它是 -Stage 2B 的 opt-in seam:不会把 PostgreSQL 变成默认 provider,不会改变 -file/SQLite 选择逻辑,不会增加 runtime caller,也不表示已经满足 promotion -条件。 - -## 边界 - -`PostgreSqlAuthorityService` 接收不透明的传输凭证、tenant、goal 和期望的 -数据库 incarnation identity,然后先调用注入的认证函数和 tenant 授权函数, -再打开 provider。凭证不会持久化、不会传给 PostgreSQL,也不会进入 -provider-neutral 的 `AuthorityStore` 合同。principal 被拒绝或无法验证时, -服务会在建立数据库连接前 fail closed。 - -服务返回以下类型化结果之一: - -| 结果 | 含义 | -| --- | --- | -| `opened` | principal 已认证、被授权使用该 tenant,且数据库 identity 与请求绑定一致。 | -| `principal_unauthenticated` | 凭证被拒绝,或认证函数返回了非法 principal。 | -| `principal_verification_unavailable` | 认证无法完成。 | -| `tenant_unauthorized` | 已认证 principal 无权使用该 tenant。 | -| `tenant_authorization_unavailable` | tenant policy 无法评估。 | -| `store_identity_unavailable` | 无法读取 provider metadata。 | -| `store_identity_mismatch` | 请求的 incarnation 不是数据库当前 incarnation。 | - -该模块刻意保持为进程内边界。部署方负责传输、凭证校验、tenant policy、连接池 -和 secret 处理;本模块不授予网络访问、actor ownership、lease ownership、跨主机 -同步或 promotion 权限。 - -## restore-incarnation 轮换 - -`rotatePostgreSqlAuthorityStoreIdentity` 是认证服务部署拥有的管理操作。它锁定 -singleton metadata 行,校验旧 identity,并原子写入新生成的 -`postgresql:<32 位小写十六进制>` identity。它不会重写 head、commit、event、 -receipt、goal 或 operation ID。 - -provider revision token 包含数据库 identity。因此 restore 轮换前签发的 token -会变成 stale 并产生 conflict;使用新 identity 打开的 store 仍能读取持久化的 -head 和 receipt 历史。期望 identity 错误时不会写入。如果 PostgreSQL 在 -`COMMIT` 后丢失响应,结果为 `ambiguous`;服务必须先重新读取 metadata 再重试, -不能猜测轮换是否已经生效。 - -## 历史保留与合成种子 - -PostgreSQL schema v1 在游标 1、65、129 等位置追加检查点,其余新提交保留增量。 -历史扫描从覆盖请求游标的检查点或保留的 v0 完整投影开始,校验游标连续性和投影 -摘要。事件与回执继续绑定原提交。 - -由管理员执行 `loopx postgres setup --execute`。从 v0 升级时传入数据库现有的 -`--store-identity`。安装保留完整投影历史,将原 journal 的表级 `SELECT` 和 -`INSERT` 授权延续到检查点,继承这些授权的组成员也可继续使用。同一数据库事务完成 -显式 `--runtime-role` 配置与格式切换。使用自定义列级授权的部署应显式配置运行角色, -已建成的 v1 库也可用原 identity 和 `--runtime-role` 配置角色。升级前验证备份并停止 -旧调用方,在隔离恢复库以受限角色读取历史并验证新检查点追加后再决定是否启用。 -回退到 v0 需要使用经验证的 v0 备份。 - -`loopx postgres local-to-pg-seed --goal-id ` 从合成 File 或 SQLite -状态预览脱敏种子,预览期间不连接目标。可通过 `--output` 创建私有文件或复用内容 -完全相同的普通文件。内容不同或路径是符号链接时,命令在连接目标前失败。使用 -`--execute` 时通过 `LOOPX_POSTGRES_URL` 传入已配置的受限连接,可选 -`--store-identity` 固定目标库身份。种子写入空目标 Goal,原样重试(含同一 -`--output`)可读回原提交回执。种子从游标 1 开始,仅用于合成数据。完整历史的 -导出、核验或恢复使用 journal 归档。 - -## 验证 - -公开 synthetic fixture 是 -[`postgresql_authority_service_v0.json`](../../tests/fixtures/control_plane/postgresql_authority_service_v0.json), -覆盖认证准入、认证拒绝、tenant 拒绝、identity 漂移和 restore 轮换,不包含凭证 -或私有数据库信息。 - -确定性 seam 测试: - -```sh -npm run typecheck:control-plane -npm run test:postgresql-authority-service -``` - -真实 PostgreSQL 路径需要将 `LOOPX_TEST_POSTGRES_SERVICE_URL` 指向隔离的临时 -数据库,再执行同一命令。该数据库必须与活动 goal 及其他 PostgreSQL 测试 schema -分离;集成测试只修改自身的 tenant/goal 和 metadata。 diff --git a/docs/reference/remote-authority-task-service-v0.md b/docs/reference/remote-authority-task-service-v0.md deleted file mode 100644 index 44fcf18f45..0000000000 --- a/docs/reference/remote-authority-task-service-v0.md +++ /dev/null @@ -1,308 +0,0 @@ -# Remote authority task service v0 (candidate) - -This opt-in source-checkout service lets independent clients claim a canonical -task, record progress, renew/release its execution lease and read back the result -over authenticated HTTPS. It reuses PostgreSQL service admission and the existing -Todo/lease transaction owners. It does not select a provider, seed or migrate a -Goal, expose arbitrary authority commits, or allow the host scheduler to admit remote turns. - -## Ownership and access - -`RemoteAuthorityService` owns transport admission, bounded result projection and -request-to-receipt binding. `PostgreSqlAuthorityService` opens the tenant/Goal -store; `executeCoordinationTodoClaim`, `executeCoordinationTodoUpdate` and -`executeCanonicalTaskLeaseLifecycle` remain the business decision owners. -The PostgreSQL store retains atomic head/event/receipt persistence and CAS. - -The service operator supplies TLS, a private policy file and a database role. -Clients receive only a service credential and their explicit scope. They cannot -submit database credentials, a principal identity, a clock, a registry, a raw -projection or a local filesystem path. Each policy grant binds an authenticated -principal to a tenant, Goal, actor and allowed operations. Each scope also pins -the expected database incarnation and its service-owned registered actor roster. -This explicit candidate roster does not mirror or promote a live local registry. - -The policy schema is `loopx_remote_authority_policy_v0`: - -```json -{ - "schema_version": "loopx_remote_authority_policy_v0", - "scopes": [{ - "tenant_id": "tenant-one", - "goal_id": "goal-one", - "store_identity": "postgresql:cccccccccccccccccccccccccccccccc", - "registered_agents": ["actor-a"] - }], - "principals": [{ - "principal_id": "principal-a", - "credential_sha256": "", - "grants": [{ - "tenant_id": "tenant-one", - "goal_id": "goal-one", - "actor_id": "actor-a", - "operations": ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"] - }] - }] -} -``` - -These are synthetic scope values; replace the identity and credential digest -before use. Generate a high-entropy credential (at least 32 printable ASCII -characters) and deliver it privately. Keep the policy, TLS private key and -database configuration outside version control. Publish policy changes by -atomically renaming a complete replacement file. Missing, malformed or unreadable -policy fails closed; every request rereads it, including reused TLS connections. -Revocation prevents subsequent admissions. A request already admitted under a -policy snapshot may finish and retains that snapshot's revision in its receipt; -revocation does not claim to undo an in-flight commit. - -### Scoped Todo reads - -For a read-only listener with resource authorization, set `schema_version` to -`loopx_remote_authority_scoped_read_policy_v1` in the existing service policy. -Each scope requires `todo_read_acl`, an array of entries with `goal_ref`, -`todo_id`, `principal_id` and `actor_id`. Each credential grant also requires -`goal_ref` with the exact `goal_id` and `goal_instance_id`. Grant `read` or -`snapshot` for the actions the caller needs. The operator must approve each -Todo and audience for that lifetime. Grant and ACL entries must agree with the -service scope, authenticated principal and actor. An empty ACL grants no access. - -For current Todo or snapshot reads, each entry also needs -`todo_origin_operation_id`: the canonical operation id of that Todo's successful -creation under this exact lifetime, or the trusted source owner's successful -first `bind_existing` transaction that adopted the existing Todo. The service -verifies that original receipt and projection through the existing PostgreSQL -checkpoint owner, bounded by the admitted current head. An ACL is permission, -not proof of ownership: a claim, update, successor activation, no-op rebind, -missing receipt or predecessor's receipt cannot move retained work into a new -lifetime. Entries without a valid origin are excluded; a direct/targeted read -without proof, or a snapshot with no proven resources, is rejected. Shared -first-bind proofs are read once per request; no authorization is cached between -requests. The origin operation id stays in the private policy and is not -returned to callers. Existing bound work without such evidence stays unavailable -until the source owner supplies valid historical proof; do not rebind or rewrite -its history to manufacture one. - -Launch the service with the scoped policy and use the current TLS client with -the existing v0 request JSON. The server supplies the exact Goal reference -from its private policy and verifies it against the active lifecycle on the -same PostgreSQL head used to compose the response. Unbound, retiring and -mismatched lifetimes deny access. A direct read or targeted snapshot outside -the ACL returns rejection without revealing whether the Todo exists. The -scoped listener denies write commands. - -Scoped successes use `loopx_remote_authority_scoped_read_response_v1`. They -return the exact Goal reference, authorized Todo status and assignment, and -matching lease metadata. Snapshots also include role and priority. Free-form -notes and lease keys are withheld. Scoped clients reject unknown nested Todo -and lease fields even when a snapshot digest matches; the trusted v0 decoder -keeps its existing compatibility behavior. The snapshot receipt digest covers only the -authorized projection and its scope. The global provider revision is withheld, -so changing a hidden Todo leaves the returned view and digest intact. Clients -that lack support for this response version report an unavailable read. - -Revoke access by replacing the policy with a new version of the same schema -that removes the matching ACL entry or grant. The service pins the policy -schema at startup and fails closed on a schema change during reload. Stop the -listener to disable it. Keep the v0 policy for an explicitly configured -legacy listener with trusted access to the whole Goal. - -#### Historical receipt proof - -To authorize recovery of a specific remote operation, add its canonical -`operation_id` to `receipt_operation_ids` on the Todo's read ACL entry. The -credential also needs `read` in its grant. The ID comes from the original -operation or its trusted service receipt. An omitted or empty list grants -no receipt access. Use `--recover` with the complete original command. - -The service checks the active Goal and allowed Todo before it opens the -receipt. The original request digest, principal and actor must match the -receipt. The original committed projection must prove the same Goal lifetime. -The service reuses the provider's bounded checkpoint and delta reader for that -exact cursor. Missing, unbound or cross-lifetime receipts are rejected, even -with a matching operation ID. The scoped service role needs read access to the -existing tenant-scoped `authority_checkpoints` table as part of this journal read. - -A scoped success returns the exact Goal, Todo and operation, `historical_only` -and the original receipt digest. It withholds the original result payload, -lease key, cursor and provider revision. The original receipt confirms -recorded history. Execution authority still comes from a current lease and -the existing mutation owner. Remove the operation ID to revoke recovery while -leaving the authorized Todo read available. - -Artifact versions, delegated sources and cross-person writes require separate -qualification before they can be exposed. - -## Commands and recovery - -POST JSON to `/v0/tasks/execute` with a `Bearer` credential. Every command includes -`schema_version: "loopx_remote_authority_request_v0"`, `tenant_id`, `goal_id`, -`actor_id`, `store_identity` and `operation`. Supply `todo_id` for individual -task commands; omit it on `snapshot` for a whole Goal read. Task IDs use -canonical `todo_` syntax. Unknown fields and operations are rejected. - -| Operation | Additional fields | Behavior | -| --- | --- | --- | -| `read` | None | Bounded current task and lease state; no lease key. | -| `claim` | `request_id` | Atomically claim and acquire managed execution under `hard_lease`. | -| `progress` | `request_id`, `claim_request_id`, `note` | Update a note using the original claim and current execution fence. | -| `renew` | `idempotency_key`, `expected_version`, `ttl_seconds` | Renew the current lease through the existing lifecycle owner. | -| `release` | `idempotency_key`, `expected_version` | Release execution; this does not clear the task's assignment or complete it. | -| `complete` | `request_id`, `claim_request_id`, `evidence`, `note?`, `no_followup?` | Complete the task with evidence, preserve a recorded successor and release the execution lease. | -| `block` | `request_id`, `reason`, `claim_request_id?`, `resume_when?`, `note?` | Pause task as blocked with explicit reason, releasing execution lease. | -| `defer` | `request_id`, `reason`, `resume_when`, `claim_request_id?`, `note?` | Defer task with resumption condition and rationale, releasing execution lease. | -| `todo_add` | `request_id`, `text`, `priority?`, `role?`, ... | Create a canonical task under policy grant and tenant fence. | -| `snapshot` | `todo_id?` (optional) | Read-only projection of Goal todos, leases, and deterministic cross-machine receipt digest. | - -Use the lease key/version returned by claim or renewal for lifecycle commands. -Progress names the original claim's client `request_id`; normal activity can -maintain that execution and advance its lease version. Before a later lifecycle -command, use `read` to confirm the same owner/epoch and obtain the current version -while retaining the original lease key. Stale versions fail without an automatic -rebase. Versions are non-negative safe integers, including `0` on existing -canonical leases. Releasing an absent lease with version `0` seals a successful -no-op receipt with `missing: true`, `released: false` and `changed: false`; -replay and recovery retain that outcome without creating a lease. -Progress cannot adopt another execution generation. -Remote claims and lease lifecycle commands require `hard_lease` in the canonical -observation used to prepare the commit, including every claim CAS retry. A -concurrent switch to `soft_claim` rejects the remote operation without changing -work or recording a receipt. Local soft claims and cleanup retain their existing -behavior; historical hard-lease receipts keep the same identity. -Read responses retain current lease facts even when a canonical task has a long -note. If the note's JSON encoding exceeds 4 KiB, the response omits `note` and -sets `todo.note_omitted: true`; the stored note remains unchanged. The overall -response limit remains 64 KiB. -Claim/progress request IDs identify immutable intent within -the principal/tenant/Goal/actor scope. Reusing one with different intent fails. -Renew/release retain the canonical lease operation identity. Their receipts also -verify the admitted principal, actor and request digest before reuse. - -Ordinary `complete` follows the Todo's continuation policy. A recorded successor -stays linked. With neither a successor nor a terminal declaration, completion -records `active_goal`. Supply `no_followup: true` only -for an explicit terminal completion. A recorded successor conflicts with that -intent, so the operation fails before writing a completion receipt or releasing -the lease. Existing successful completions replay their original continuation -when clients supply the same request. - -For `block` and `defer`, use the existing Todo resume condition syntax, such as -`todo_done:todo_dependency` or `resume_at:2026-12-01T00:00:00Z`. A reason alone -can block a task; deferral requires a valid condition. - -A snapshot observes one provider revision. The client recomputes its digest from -the tenant, Goal, store incarnation, revision and ordered Todo and lease rows, -and rejects rows outside the requested scope or without a matching Todo. Two -authorized clients reading the same revision and scope get the same digest. -The example client exits successfully for a valid snapshot. A response over -64 KiB is unavailable; retry a narrower read if applicable. A snapshot has no -recovery operation, produces no write, and never includes lease keys. - -The service clock decides expiry. Mutations require `hard_lease`; normal task -eligibility, write-scope and lease checks still apply. A receipt is historical -evidence, not permission to execute after expiry, release or takeover. - -A timeout, lost response or unverified reply is **ambiguous** for a mutation. -The client checks the operation-specific result evidence as well as the response -schema and request digest. Missing or invalid operation, task, lease or receipt -fields cannot produce a successful client exit; recovery/read failures remain -`unavailable` and mutation replies remain `ambiguous`. -The client never retries automatically. Retain the exact command and send it to -`/v0/tasks/recover` (the example client uses `--recover`). A `found` response -includes `historical_only: true` and a digest of the stored receipt; `missing` -does not execute anything or prove that another in-flight attempt cannot commit. -Reconcile current state before explicitly retrying the same intent/identity; -never substitute a fresh ID to hide uncertainty. Even a successful historical -recovery does not reacquire execution authority. - -Admission metadata is written inside the existing business receipt transaction, -not to a second ledger. A request pins every read and commit to its admitted -database incarnation. Receipt presence or absence is checked against the identity -from the same PostgreSQL read transaction. Rotation makes an old binding unavailable; the provider's -transactional CAS also fences rotation after a read. Following a reviewed restore, -the operator must explicitly authorize the new identity. The same principal and -intent can then query retained historical receipts with that new binding. - -## Running the candidate - -For a local PostgreSQL 16 server and HTTPS gateway, build the -[self-hosted Compose bundle](../../examples/coordination/remote-authority-compose/README.en.md) -from a pinned commit. The bundle boots an isolated starter Goal with two -independent client credentials. [简体中文](../../examples/coordination/remote-authority-compose/README.md) - -Use the repository's qualified Node runtime and `npm ci --ignore-scripts`. -Provision an isolated PostgreSQL authority schema/head using the existing -provider administration path. The listener never installs schemas or seeds -state. Use a restricted runtime role: metadata SELECT, tenant tables -SELECT/INSERT and heads UPDATE, with existing forced RLS; never a database owner -or superuser. Schema setup and incarnation rotation remain separate administration. - -Set `LOOPX_AUTHORITY_DATABASE_URL` privately on the server, then run: - -```sh -node --experimental-strip-types examples/coordination/remote-authority-service.ts \ - --policy "$SERVICE_POLICY" --cert "$TLS_CERT" --key "$TLS_KEY" \ - --host 127.0.0.1 --port 8443 -``` - -For a reachable remote listener, choose an explicit bind address and a certificate -valid for its hostname. The client always verifies TLS; private CAs use `--ca`. -Set `LOOPX_AUTHORITY_CREDENTIAL` privately on the client, put the complete command -JSON in a file, and run: - -```sh -node --experimental-strip-types examples/coordination/remote-authority-client.ts \ - --endpoint "$SERVICE_HTTPS_ORIGIN" --ca "$TLS_CA" < command.json -``` - -Add `--recover` for a read-only receipt query. Credentials do not enter arguments, -command JSON or receipts. Responses contain typed outcomes and reason codes, -not raw provider errors or filesystem details. No browser/cookie authentication, -CORS or redirects are supported. Input/output are bounded to 16/64 KiB; the -default listener admits at most 16 concurrent requests and the launcher pools -at most eight database connections with query, statement and lock deadlines. -SIGTERM/SIGINT stop new dispatches and allow sockets five seconds to drain, -then close remaining connections, including partial TLS/HTTP requests. Already -dispatched work still finishes before the pool closes, even if its response -socket has gone away. Disconnecting a client does not establish that its write rolled back. -Stop the listener and remove access grants to disable this candidate; existing -local provider selection is unaffected and durable receipts remain in PostgreSQL. - -## Optional host observation - -The local registry can explicitly opt one registered Goal into a **read-only** -health observation. Set `remote_authority.enabled: true` on that Goal with a -credential-free HTTPS `endpoint`, verified `tenant_id`, `store_identity`, -registered `actor_id` and a stable existing `probe_todo_id`. Set `ca_file` to -an absolute path to a trusted CA when using a private CA. Use the service -credential environment variable above; the legacy environment-only canary flag -does not activate any Goal. The binding and credentials stay private. - -A full `heartbeat-prompt` can describe the configuration as unverified. -`quota should-run` runs one bounded read of the known Todo for the current -registered actor. Missing credentials, wrong scope, an invalid TLS certificate, -a missing probe Todo, revocation and an unreachable service never count as -verified. A verified read still waits for shared Turn admission. The scheduler -does not select local work, spend quota, or acquire a remote lease from this -observation. Owner Stop and paused quota keep their precedence. Disabling the -setting removes the observation; local File/SQLite behavior is unchanged. - -## Qualification and remaining work - -`npm run test:remote-authority` runs decoding/policy/TLS tests. With -`LOOPX_TEST_POSTGRES_URL` pointing to a **disposable administrative test server**, -it additionally creates a private test database and a restricted runtime role, -starts real TLS server/client processes, and checks races, replay, revocation, -expiry/takeover, incarnation rotation and response loss followed by restart. -The suite also invokes the real quota CLI against that isolated TLS service; -it checks revocation, disconnection and recovery without changing the remote -head. It removes its own database and role afterwards. The dedicated PostgreSQL CI -workflow requires a real integration subtest; a skipped backend is not a pass. - -This qualifies a source candidate on one host with independent client processes -and real PostgreSQL. It does not qualify two physical hosts/cloud deployment, -distributed clock discipline, high availability, capacity/soak, shared budgets, -Host supervision, App/CLI integration or provider/default promotion. Those remain -with the existing shared-authority P lane and roadmap R6/D1–D3 owners under -[#3245](https://github.com/loopx-project/loopx/issues/3245). No new scheduler, -registry, storage implementation or broad runtime RPC is introduced. diff --git a/examples/control_plane/authority-lease-lifecycle-rehearsal.py b/examples/control_plane/authority-lease-lifecycle-rehearsal.py index efa44b0d4c..4722d6c823 100644 --- a/examples/control_plane/authority-lease-lifecycle-rehearsal.py +++ b/examples/control_plane/authority-lease-lifecycle-rehearsal.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -"""Replay a read-only Goal snapshot through an immutable legacy and three providers. +"""Replay a read-only Goal snapshot through an immutable legacy and local providers. Only disposable copies receive a synthetic open Todo/lease and mutations. The report contains counts, source/observation digests and explicit semantic deltas; -source text, actor IDs, paths and connection strings are never printed. +source text, actor IDs and paths are never printed. """ from __future__ import annotations @@ -26,12 +26,11 @@ NODE_REHEARSAL = r""" import assert from 'node:assert/strict'; -import {createHash, randomUUID} from 'node:crypto'; +import {createHash} from 'node:crypto'; import {mkdtemp, mkdir, readFile, rm, writeFile} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import {join} from 'node:path'; import {pathToFileURL} from 'node:url'; -import {Pool} from 'pg'; let raw=''; for await (const chunk of process.stdin) raw+=chunk; const input=JSON.parse(raw); const moduleAt=(root,path)=>import(pathToFileURL(join(root,'loopx/control_plane',path)).href); @@ -42,10 +41,9 @@ const {executeCoordinationTodoUpdate: update}=await moduleAt(input.repo,'coordination/todo_update.ts'); const {FileAuthorityStore}=await moduleAt(input.repo,'coordination/file_authority_store.ts'); const {SqliteAuthorityStore}=await moduleAt(input.repo,'coordination/sqlite_authority_store.ts'); -const {PostgreSqlAuthorityStore,installPostgreSqlAuthorityStoreSchema}=await moduleAt(input.repo,'coordination/postgresql_authority_store.ts'); const {selectLocalSqliteAuthority}=await moduleAt(input.repo,'coordination/local_authority_provider.ts'); const {coordinationTodoReadModel}=await moduleAt(input.repo,'coordination/coordination_projection.ts'); -const {canonicalAuthorityBytes,canonicalAuthoritySha256}=await moduleAt(input.repo,'coordination/authority_store_codec.ts'); +const {canonicalAuthoritySha256}=await moduleAt(input.repo,'coordination/authority_store_codec.ts'); const {engageLegacyCoordinationWriterFence}=await moduleAt(input.repo,'coordination/legacy_writer_fence.ts'); const digest=value=>canonicalAuthoritySha256(value); const goal=input.goal_id, target='todo_lifecycle_rehearsal', acquisition='todo_acquire_rehearsal', handover='todo_claim_transfer_rehearsal'; @@ -67,20 +65,16 @@ initial.leases.push(original,{...original,todo_id:handover,idempotency_key:'handover-a',write_scopes:['handover/**']}); initial.leases.sort((a,b)=>a.todo_idb.todo_id?1:0); const root=await mkdtemp(join(tmpdir(),'loopx-lease-rehearsal-')); -const pool=new Pool({connectionString:process.env.LOOPX_TEST_POSTGRES_URL,max:4}); -const database={connect:async()=>{const c=await pool.connect();return {query:async(t,v)=>c.query(t,v),release:e=>c.release(e)};}}; -const tenant=`lease-rehearsal-${randomUUID()}`; const report={}; const observations={}; const finalHeads={}; try { - await installPostgreSqlAuthorityStoreSchema(database,`postgresql:${'b'.repeat(32)}`); - for (const arm of ['legacy','file','sqlite','postgresql']) { + for (const arm of ['legacy','file','sqlite']) { const runtime=join(root,arm); await mkdir(runtime,{recursive:true}); const source=join(runtime,'facts.json'), bytes=JSON.stringify({registered_agents:['agent-a','agent-b']}); await writeFile(source,bytes); const authority={handoff_mode:'hard_lease',registered_agent_candidates:[['agent-a','agent-b']], todos:initial.todos, todo_projection_error:null, source_receipts:[{source_id:'registry',path:source,state:'file',sha256:createHash('sha256').update(bytes).digest('hex')}]}; - let store=null, dependencies={}; + let store=null; const dependencies={}; const leaseDir=join(runtime,'goals',goal,'task-leases'); await mkdir(leaseDir,{recursive:true}); // Every arm carries identical contradictory legacy files after promotion. for (const row of initial.leases) await writeFile(join(leaseDir,`${row.todo_id}.json`),JSON.stringify(row)); @@ -90,16 +84,6 @@ assert.equal((await selectLocalSqliteAuthority(runtime,goal,true)).ok,true); store=new SqliteAuthorityStore(join(runtime,'authority/sqlite-v0'),goal); } - if (arm==='postgresql') { - store=new PostgreSqlAuthorityStore(database,{tenant_id:tenant,goal_id:goal}); - const identity=await store.storeIdentity(); assert.equal(identity.status,'available'); - await mkdir(join(runtime,'authority'),{recursive:true}); - await writeFile(join(runtime,'authority',`provider-${createHash('sha256').update(goal).digest('hex')}.json`),JSON.stringify({ - schema_version:'loopx_local_authority_provider_v0',provider:'postgresql',goal_id:goal, - tenant_id:tenant,store_identity:identity.store_identity})); - dependencies={authorityProvider:{openPostgresqlStore:selection=>{ - assert.equal(selection.tenant_id,tenant);assert.equal(selection.goal_id,goal);return store;}}}; - } if (store) { assert.equal((await store.commitAuthority({expected_provider_revision:null,operation_id:'seed',events:[],receipts:[],next_projection:initial})).status,'applied'); const h=await store.loadAuthority();assert.equal(h.status,'loaded'); @@ -185,16 +169,14 @@ } observations[arm]=[acquired,takeover,...results].map(r=>({lease:r.lease})); } - for (const arm of ['file','sqlite','postgresql']) assert.deepEqual(observations[arm],observations.legacy); - assert.deepEqual(finalHeads.file,finalHeads.sqlite); assert.deepEqual(finalHeads.file,finalHeads.postgresql); + for (const arm of ['file','sqlite']) assert.deepEqual(observations[arm],observations.legacy); + assert.deepEqual(finalHeads.file,finalHeads.sqlite); process.stdout.write(JSON.stringify({schema_version:'authority_lease_lifecycle_rehearsal_v0', initial_todos:initial.todos.length,initial_leases:initial.leases.length,fixture_sha256:digest(initial), observation_sha256:digest(observations.file),provider_head_sha256:digest(finalHeads.file),arms:report, intentional_delta:'canonical_receipt_recovery_and_current_acquire_proof;_explicit_create_CAS_retry_no_longer_mismatches;_explicit_atomic_claim_transfer',non_target_records_unchanged:true})); } finally { - for (const table of ['authority_receipts','authority_events','authority_commits','authority_heads']) - await pool.query(`DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1`,[tenant]); - await pool.end(); await rm(root,{recursive:true,force:true}); + await rm(root,{recursive:true,force:true}); } """ @@ -204,12 +186,9 @@ def main() -> int: parser.add_argument("--registry", type=Path, required=True) parser.add_argument("--goal-id", required=True) parser.add_argument("--baseline-repo", type=Path, required=True) - parser.add_argument("--execute-isolated-postgresql", action="store_true") parser.add_argument("--private-diagnostics", type=Path, help="optional owner-only file for raw failure diagnostics; never publish it") args = parser.parse_args() - if not args.execute_isolated_postgresql or not os.environ.get("LOOPX_TEST_POSTGRES_URL"): - raise SystemExit("an explicitly isolated PostgreSQL server is required") baseline = args.baseline_repo.resolve() def git(*command): return subprocess.check_output(["git", "-C", str(baseline), *command], text=True).strip() diff --git a/examples/control_plane/authority-monitor-poll-rehearsal.py b/examples/control_plane/authority-monitor-poll-rehearsal.py index 9de8a3896d..c01de6961d 100644 --- a/examples/control_plane/authority-monitor-poll-rehearsal.py +++ b/examples/control_plane/authority-monitor-poll-rehearsal.py @@ -27,12 +27,11 @@ NODE_REHEARSAL = r""" import assert from 'node:assert/strict'; -import {createHash, randomUUID} from 'node:crypto'; +import {createHash} from 'node:crypto'; import {mkdtemp, mkdir, rm, writeFile} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import {join} from 'node:path'; import {pathToFileURL} from 'node:url'; -import {Pool} from 'pg'; let raw=''; for await (const chunk of process.stdin) raw+=chunk; const input=JSON.parse(raw); const moduleAt=(root,path)=>import(pathToFileURL(join(root,'loopx/control_plane',path)).href); @@ -40,8 +39,6 @@ const {pollLocalCoordinationMonitor: baseline,updateLocalCoordinationTodo: baselineUpdate}=await moduleAt(input.baseline_repo,'coordination/local_authority_runtime.ts'); const {FileAuthorityStore}=await moduleAt(input.repo,'coordination/file_authority_store.ts'); const {SqliteAuthorityStore}=await moduleAt(input.repo,'coordination/sqlite_authority_store.ts'); -const {PostgreSqlAuthorityStore,installPostgreSqlAuthorityStoreSchema}=await moduleAt(input.repo,'coordination/postgresql_authority_store.ts'); -const {PostgreSqlAuthorityService}=await moduleAt(input.repo,'coordination/postgresql_authority_service.ts'); const {selectLocalSqliteAuthority}=await moduleAt(input.repo,'coordination/local_authority_provider.ts'); const {executeCanonicalTaskLeaseAcquire}=await moduleAt(input.repo,'coordination/task_lease_acquire.ts'); const {coordinationTodoReadModel}=await moduleAt(input.repo,'coordination/coordination_projection.ts'); @@ -62,36 +59,16 @@ idempotency_key:proof.idempotency_key,version:3,lease_epoch:2,status:'active',write_scopes:[], acquired_at:'2026-01-01T00:00:00Z',updated_at:'2026-01-01T00:00:00Z',expires_at:'2099-01-01T00:00:00Z'}; const root=await mkdtemp(join(tmpdir(),'loopx-monitor-rehearsal-')); -const pool=new Pool({connectionString:process.env.LOOPX_TEST_POSTGRES_URL,max:4}); -const database={connect:async()=>{const c=await pool.connect();return {query:async(t,v)=>c.query(t,v),release:e=>c.release(e)};}}; -const tenant=`monitor-rehearsal-${randomUUID()}`; const heads={}, compatible={}, report={}; try { - await installPostgreSqlAuthorityStoreSchema(database,`postgresql:${'b'.repeat(32)}`); - for (const arm of ['baseline','file','sqlite','postgresql']) { + for (const arm of ['baseline','file','sqlite']) { const runtime=join(root,arm); await mkdir(runtime,{recursive:true}); const display=join(runtime,'state.md'); await writeFile(display,'# Disposable display\n'); - let store=new FileAuthorityStore(join(runtime,'authority/file-v0'),goal), dependencies={}; + let store=new FileAuthorityStore(join(runtime,'authority/file-v0'),goal); const dependencies={}; if(arm==='sqlite') { assert.equal((await selectLocalSqliteAuthority(runtime,goal,true)).ok,true); store=new SqliteAuthorityStore(join(runtime,'authority/sqlite-v0'),goal); } - if(arm==='postgresql') { - store=new PostgreSqlAuthorityStore(database,{tenant_id:tenant,goal_id:goal}); - const identity=await store.storeIdentity(); assert.equal(identity.status,'available'); - const selection={schema_version:'loopx_local_authority_provider_v0',provider:'postgresql',goal_id:goal, - tenant_id:tenant,store_identity:identity.store_identity}; - await mkdir(join(runtime,'authority'),{recursive:true}); - await writeFile(join(runtime,'authority',`provider-${createHash('sha256').update(goal).digest('hex')}.json`),JSON.stringify(selection)); - const service=new PostgreSqlAuthorityService({database, - authenticatePrincipal:()=>({status:'authenticated',principal:{principal_id:'isolated-rehearsal'}}), - authorizeTenant:(principal,selected)=>principal==='isolated-rehearsal'&&selected===tenant?{status:'allowed'}: - {status:'denied',reason_code:'wrong_tenant',reason:'outside disposable tenant'}}); - dependencies={openPostgresqlStore:async selected=>{ - const opened=await service.openStore({credential:null,...selected}); - assert.equal(opened.status,'opened'); return opened.store; - }}; - } const seed=await store.commitAuthority({expected_provider_revision:null,operation_id:'seed',events:[],receipts:[],next_projection:initial}); assert.equal(seed.status,'applied'); assert.equal((await engageLegacyCoordinationWriterFence({schema_version:'loopx_legacy_coordination_writer_fence_engage_request_v0', @@ -189,15 +166,14 @@ report[arm]={poll_parity:true,retained_lease_reactivation:'applied',fresh_execution_epoch:3,same_hash_reactivation_generation:2, historical_replay_keeps_completion:true,stale_observation:'rejected',non_target_unchanged:true}; } - for(const arm of ['file','sqlite','postgresql']) assert.deepEqual(compatible[arm],compatible.baseline); - for(const arm of ['file','sqlite','postgresql']) assert.deepEqual(heads[arm],heads.baseline); + for(const arm of ['file','sqlite']) assert.deepEqual(compatible[arm],compatible.baseline); + for(const arm of ['file','sqlite']) assert.deepEqual(heads[arm],heads.baseline); process.stdout.write(JSON.stringify({schema_version:'authority_monitor_rehearsal_v0', source_todos:input.projection.todos.length,source_leases:input.projection.leases.length, compatible_head_sha256:digest(compatible.baseline),final_head_sha256:digest(heads.file), provider_heads_equal:true,arms:report})); } finally { - await pool.query('DELETE FROM loopx_control_plane.authority_heads WHERE tenant_id=$1',[tenant]); - await pool.end(); await rm(root,{recursive:true,force:true}); + await rm(root,{recursive:true,force:true}); } """ @@ -207,11 +183,8 @@ def main() -> int: parser.add_argument("--registry", type=Path, required=True) parser.add_argument("--goal-id", required=True) parser.add_argument("--baseline-repo", type=Path, required=True) - parser.add_argument("--execute-isolated-postgresql", action="store_true") parser.add_argument("--private-diagnostics", type=Path) args = parser.parse_args() - if not args.execute_isolated_postgresql or not os.environ.get("LOOPX_TEST_POSTGRES_URL"): - raise SystemExit("an explicitly isolated PostgreSQL server is required") baseline = args.baseline_repo.resolve() def git(*command): return subprocess.check_output(["git", "-C", str(baseline), *command], text=True).strip() diff --git a/examples/control_plane/authority-three-arm-rehearsal.py b/examples/control_plane/authority-three-arm-rehearsal.py index 399982d692..f6301993e7 100644 --- a/examples/control_plane/authority-three-arm-rehearsal.py +++ b/examples/control_plane/authority-three-arm-rehearsal.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -"""Compare legacy, file, and PostgreSQL archive semantics from one live snapshot. +"""Compare legacy, file, and SQLite archive semantics from one live snapshot. The selected Goal is read only. All mutations happen in a temporary Markdown -clone, an isolated file store, and a caller-supplied disposable PostgreSQL -tenant. Output contains bounded counts and digest prefixes, never Todo text, -identifiers, paths, connection strings, or raw projections. +clone, an isolated file store, and an isolated SQLite store. Output contains +bounded counts and digest prefixes, never Todo text, identifiers, paths, or raw +projections. """ from __future__ import annotations @@ -13,7 +13,6 @@ import copy import hashlib import json -import os import shutil import subprocess import sys @@ -36,13 +35,12 @@ NODE_REHEARSAL = r""" import assert from 'node:assert/strict'; -import {createHash, randomUUID} from 'node:crypto'; +import {createHash} from 'node:crypto'; import {mkdtemp, rm} from 'node:fs/promises'; import {tmpdir} from 'node:os'; import {join} from 'node:path'; -import {Pool} from 'pg'; import {FileAuthorityStore} from '__FILE_STORE__'; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from '__PG_STORE__'; +import {SqliteAuthorityStore} from '__SQLITE_STORE__'; import {executeCoordinationTodoArchiveCompleted} from '__ARCHIVE__'; import {canonicalAuthorityBytes} from '__CODEC__'; import {evaluateTodoResumeConditions} from '__RESUME__'; @@ -85,26 +83,11 @@ } return result; }; -const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 4}); -const database = {connect: async () => { - const client = await pool.connect(); - return { - query: async (text, values) => await client.query(text, values), - release: (error) => client.release(error), - }; -}}; -await installPostgreSqlAuthorityStoreSchema( - database, - `postgresql:${'b'.repeat(32)}`, -); -const tenant = `three-arm-${randomUUID()}`; const fileRoot = await mkdtemp(join(tmpdir(), 'loopx-three-arm-file-')); +const sqliteRoot = await mkdtemp(join(tmpdir(), 'loopx-three-arm-sqlite-')); const stores = [ ['file', new FileAuthorityStore(fileRoot, request.goal_id)], - ['postgresql', new PostgreSqlAuthorityStore(database, { - tenant_id: tenant, - goal_id: request.goal_id, - })], + ['sqlite', new SqliteAuthorityStore(sqliteRoot, request.goal_id)], ]; const results = {}; try { @@ -176,8 +159,8 @@ assert.equal( digest(results.file.head), - digest(results.postgresql.head), - 'file and PostgreSQL heads differ', + digest(results.sqlite.head), + 'file and SQLite heads differ', ); const initialById = new Map(request.initial.todos.map((todo) => [todo.todo_id, todo])); const providerById = new Map(results.file.head.todos.map((todo) => [todo.todo_id, todo])); @@ -274,19 +257,8 @@ provider_receipts_found: true, })); } finally { - for (const table of [ - 'authority_receipts', - 'authority_events', - 'authority_commits', - 'authority_heads', - ]) { - await pool.query( - `DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1 AND goal_id=$2`, - [tenant, request.goal_id], - ); - } - await pool.end(); await rm(fileRoot, {recursive: true, force: true}); + await rm(sqliteRoot, {recursive: true, force: true}); } """ @@ -305,10 +277,10 @@ def _node_script(repository: Path) -> str: ), ) .replace( - "__PG_STORE__", + "__SQLITE_STORE__", _module_uri( repository, - "loopx/control_plane/coordination/postgresql_authority_store.ts", + "loopx/control_plane/coordination/sqlite_authority_store.ts", ), ) .replace( @@ -335,20 +307,11 @@ def _parse_args() -> argparse.Namespace: parser.add_argument("--goal-id", required=True) parser.add_argument("--role", choices=("agent", "user"), default="agent") parser.add_argument("--max-active-done", type=int, default=5) - parser.add_argument( - "--execute-isolated-postgresql", - action="store_true", - help="acknowledge that LOOPX_TEST_POSTGRES_URL names a disposable server", - ) return parser.parse_args() def main() -> int: args = _parse_args() - if not args.execute_isolated_postgresql: - raise SystemExit("--execute-isolated-postgresql is required") - if not os.environ.get("LOOPX_TEST_POSTGRES_URL"): - raise SystemExit("LOOPX_TEST_POSTGRES_URL is required") if args.max_active_done < 0: raise SystemExit("--max-active-done must be non-negative") diff --git a/examples/coordination/remote-authority-compose/.dockerignore b/examples/coordination/remote-authority-compose/.dockerignore deleted file mode 100644 index 322bd34d38..0000000000 --- a/examples/coordination/remote-authority-compose/.dockerignore +++ /dev/null @@ -1,5 +0,0 @@ -* -!loopx-source.tar.gz -!Dockerfile -!provision.mjs -!launch.mjs diff --git a/examples/coordination/remote-authority-compose/Dockerfile b/examples/coordination/remote-authority-compose/Dockerfile deleted file mode 100644 index f1f1186164..0000000000 --- a/examples/coordination/remote-authority-compose/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM public.ecr.aws/docker/library/node:24.21.0-bookworm@sha256:3d27e5c11e5786e309ec3e03f93ae536eb36e6e5eb3714d5eb3300a36157add0 -ARG LOOPX_REVISION -ARG LOOPX_SOURCE_SHA256 -WORKDIR /opt/loopx -LABEL org.opencontainers.image.source="https://github.com/loopx-project/loopx" \ - org.opencontainers.image.revision="${LOOPX_REVISION}" -COPY loopx-source.tar.gz /tmp/loopx-source.tar.gz -RUN printf '%s %s\n' "$LOOPX_SOURCE_SHA256" /tmp/loopx-source.tar.gz | sha256sum -c - \ - && tar -xzf /tmp/loopx-source.tar.gz \ - && rm /tmp/loopx-source.tar.gz \ - && command -v openssl \ - && npm ci --ignore-scripts --no-audit --no-fund --include=dev -COPY provision.mjs launch.mjs ./ -USER 1000:1000 diff --git a/examples/coordination/remote-authority-compose/README.en.md b/examples/coordination/remote-authority-compose/README.en.md deleted file mode 100644 index abd42cbe9e..0000000000 --- a/examples/coordination/remote-authority-compose/README.en.md +++ /dev/null @@ -1,58 +0,0 @@ -# Self-hosted LoopX coordination - -Extract a built bundle on a machine with Docker Engine and Compose, then run the commands below. The first start uses container registries and npm. Select a free loopback port with `LOOPX_PORT`, which defaults to 8443. - -```sh -./start.sh -./client.sh a read -./client.sh b read -./client.sh reader read -``` - -Two trusted writers have distinct credentials and can hand off the same starter Todo. An additional `reader` has its own credential and can read only the allowed starter task. The first start provisions PostgreSQL authority state, a private CA and clients. Later starts reuse the task, store identity and policies from the volumes. The database runs on an internal network. - -Use `./client.sh a descriptor` to get the tenant, Goal, actor and store identity. Pass the request on standard input to `./client.sh a request`. When a reply is lost, keep the original request and `request_id` and pass them to `./client.sh a recover`. - -A remote client can forward the server's loopback port over SSH and use the same revision of `remote-authority-client.ts` with `--ca`. Export the CA with `./client.sh a ca` and deliver the client's `credential` and `request.json` privately from its volume. The CLI reads the credential from `LOOPX_AUTHORITY_CREDENTIAL`. [Gateway requests and recovery](https://github.com/loopx-project/loopx/blob/main/docs/reference/remote-authority-task-service-v0.md) - -Both TLS gateways publish loopback ports: trusted writers use `LOOPX_PORT` (8443 by default), while the scoped reader uses `LOOPX_READ_PORT` (8444 by default). Its private policy binds the exact Goal lifetime, Todo and original creation/binding receipt. Unlisted resources are denied. Snapshots return only allowed state, without notes, lease keys or a global cursor. Todo access does not authorize historical operation receipts. - -The trusted writer endpoint retains v0 scope/operation grants; a/b can read other tasks in the same scope. The v1 resource ACL supports read/snapshot, not resource-scoped writes. Give observers only the reader credential and read port. Both listeners reuse the existing service and restricted database role. Reader isolation does not qualify resource isolation for writers. TLS covers client-to-gateway connections; database connections use the isolated backend network. - -## Build and qualify - -Run this from the reviewed template checkout. Supply the complete Git commit to use for the gateway build. - -```sh -python3 examples/coordination/remote-authority-compose/build_bundle.py \ - --source-revision "$(git rev-parse HEAD)" --output /tmp/loopx-authority.tar.gz -``` - -The same source commit and template contents produce the same archive. The bundle contains `LICENSE`, `NOTICE`, the source digest and pinned image manifests. Docker verifies the digest before unpacking the source. Keep generated bundles, credentials, data and backups in their respective release or runtime storage. - -The source revision must include scoped v1 reads (#8861). Qualify from an unpacked bundle on a disposable Docker host with a unique project name and two free loopback ports. On amd64, verify the architecture of the host and running services. Time cold and cached starts through image pull, build, readiness and client reads. Check that PostgreSQL has no published port, clients have no route to the backend network and they have no database credential. - -Run the rehearsal from the repository. It creates a temporary project, exercises writer claim, expiry, takeover, original receipt recovery and replay after restart for creation and completion, plus reader filtering, write denial, persistent revocation and missing/malformed policy refusal, then removes its own containers and volumes. - -```sh -node examples/coordination/remote-authority-compose/rehearse.mjs /path/to/unpacked-bundle -``` - -Compose limits CPU, memory, processes and retained logs. Check the Docker data disk and current load before a shared-host trial. Use dedicated projects, ports, networks and volumes. Tests with two local clients qualify the protocol. Qualifying two physical hosts requires a client on the second host. - -## Back up, restore and rotate - -```sh -./state.sh backup /absolute/path/to/new-backup -./start.sh -COMPOSE_PROJECT_NAME=loopx-restored ./state.sh restore /absolute/path/to/new-backup -COMPOSE_PROJECT_NAME=loopx-restored LOOPX_PORT=8445 LOOPX_READ_PORT=8446 ./start.sh -``` - -Backup stops the project and captures its database and credential volumes together. Keep backups encrypted and access-controlled. Restore verifies the digests and bundle version, and requires a new project with no existing target volumes. Read back tasks and original operation receipts before switching clients, and stop admitting requests on the old project first. - -- Revoke access by atomically replacing `policy.json` in the relevant gateway volume: `reader-gateway` for scoped reads and `gateway` for trusted writes. Removing a resource entry or credential rejects the next request. Replacing a running v1 listener's policy with v0 cannot downgrade its permissions. Rotate high-entropy credentials together with their private policy digest and client file. Earlier backups still contain earlier credentials. -- The generated leaf certificate lasts 397 days. Reissue it before expiry with the CA in `pg-admin` and the original SANs. Before switching, verify the new certificate with the existing CA on the clients and confirm their clocks have reached its `notBefore` time. Replace the matching `cert.pem` and `key.pem` in both the `gateway` and `reader-gateway` volumes. With the same Compose project settings, run `docker compose restart gateway reader-gateway`. Use `docker compose ps gateway reader-gateway` to confirm both services are healthy, then verify both TLS endpoints and read back the task with `./client.sh a read`, `./client.sh b read` and `./client.sh reader read`. Distribute trust before rotating a CA. -- Back up before changing database credentials or versions. Rehearse migration and rollback in an isolated project. Update database roles and credential files together. Follow PostgreSQL's supported migration procedure across major versions. - -[简体中文](README.md) diff --git a/examples/coordination/remote-authority-compose/README.md b/examples/coordination/remote-authority-compose/README.md deleted file mode 100644 index 32965aead0..0000000000 --- a/examples/coordination/remote-authority-compose/README.md +++ /dev/null @@ -1,58 +0,0 @@ -# LoopX 远程协同自托管 - -在装有 Docker Engine 和 Compose 的机器上解压构建好的包,进入目录运行。首次启动需要访问镜像仓库和 npm。选一个空闲的回环端口,默认使用 8443。 - -```sh -./start.sh -./client.sh a read -./client.sh b read -./client.sh reader read -``` - -两个可信写客户端拥有不同凭据,可以交接同一个示例任务。额外的 `reader` 使用独立凭据,只能读取获准的示例任务。首次启动创建 PostgreSQL 权威库、私有 CA 和客户端。重复启动沿用卷里的任务、存储身份和撤权策略。数据库在内部网络中运行。 - -用 `./client.sh a descriptor` 取得请求的租户、Goal、Actor 和存储实例。把请求放在标准输入上,用 `./client.sh a request` 执行。发送超时后,保留原请求及 `request_id`,通过 `./client.sh a recover` 查询回执。 - -远端客户端可用 SSH 将网关的回环端口转发到本机,再用同一版本的 `remote-authority-client.ts` 和 `--ca` 连接。管理员可导出 `./client.sh a ca`,并私下交付对应客户端卷中的 `credential` 和 `request.json`。客户端通过 `LOOPX_AUTHORITY_CREDENTIAL` 接收凭据。 [网关请求与重试说明(英文)](https://github.com/loopx-project/loopx/blob/main/docs/reference/remote-authority-task-service-v0.md) - -两个 TLS 网关都只发布回环端口:可信写客户端使用 `LOOPX_PORT`(默认 8443),受限读者使用 `LOOPX_READ_PORT`(默认 8444)。`reader` 的私有策略绑定当前 Goal 实例、具体 Todo 及其原始创建/绑定回执;未列入的资源默认拒绝。snapshot 仅返回获准状态,不含 note、租约密钥或全局游标。读授权不授予历史操作回执访问。 - -可信写入口保留 v0 的范围和操作授权,a/b 可读取同范围任务;当前 v1 资源 ACL 只支持 read/snapshot,不能提供资源级写隔离。向观察者仅交付 `reader` 的凭据和只读端口。两入口共用原服务和受限数据库角色,不能将读者隔离表述为写客户端也被资源 ACL 限制。客户端到网关使用 TLS,数据库连接走隔离 backend 网络。 - -## 构建和验收 - -从经审阅的模板 checkout 运行。选择用于构建网关的完整 Git 提交。 - -```sh -python3 examples/coordination/remote-authority-compose/build_bundle.py \ - --source-revision "$(git rev-parse HEAD)" --output /tmp/loopx-authority.tar.gz -``` - -同一源码提交和模板内容生成相同的归档。归档内含 `LICENSE`、`NOTICE`、源码摘要及固定镜像清单。Docker 构建前会校验源码摘要。生成物、运行凭据、数据库和备份应存放在各自的发布或运行位置。 - -源码版本须包含 scoped v1 读取实现(#8861)。在专用测试 Docker 环境中解压验证,使用唯一的 Compose 项目名和两个空闲回环端口。在 amd64 主机上验证本机架构和实际运行架构。冷启动与缓存启动分别记录镜像拉取、构建、就绪与客户端首次读回时间。确认数据库端口未发布,客户端无法访问 backend 网络,网关不向客户端提供数据库凭据。 - -从仓库运行下列演练。它会创建临时项目,验证写客户端的认领、到期接管、新增任务与完成操作的原回执恢复及重启重放,以及只读客户端的资源过滤、拒写、撤权重启和策略缺失/损坏拒绝,最后清理自己的容器和卷。 - -```sh -node examples/coordination/remote-authority-compose/rehearse.mjs /path/to/unpacked-bundle -``` - -Compose 为数据库、网关及辅助容器设置 CPU、内存、进程数和日志轮转。共享主机还需核对 Docker 数据盘空间与已有负载。选择独立项目名、端口、网络和卷。两个同主机容器可以验证协议,跨物理主机资格需从另一台主机连接验证。 - -## 备份、恢复和轮换 - -```sh -./state.sh backup /absolute/path/to/new-backup -./start.sh -COMPOSE_PROJECT_NAME=loopx-restored ./state.sh restore /absolute/path/to/new-backup -COMPOSE_PROJECT_NAME=loopx-restored LOOPX_PORT=8445 LOOPX_READ_PORT=8446 ./start.sh -``` - -备份会停止当前 Compose 项目,保存同一时点的数据库和凭据卷。请将含私钥及数据库凭据的备份放在加密受限的位置。恢复前校验摘要和包版本,并要求新项目的目标卷尚未创建。启动后读回任务和原操作回执,确认旧项目已停止接收请求再切换入口。 - -- 撤权时原子替换对应网关卷中的 `policy.json`。只读策略位于 `reader-gateway` 卷,写策略位于 `gateway` 卷;撤销资源条目或凭据后下一次请求即拒绝。不能把 v0 策略放到已启动的 v1 监听器来降级权限。轮换密钥须生成新的高熵凭据,同步更新私有策略和客户端凭据。备份也含历史凭据。 -- 自动生成的服务叶证书有效期为 397 天。到期前用 `pg-admin` 中的 CA 续签,保留原 SAN。切换前在客户端用原 CA 校验新证书,确认各客户端时钟已到新证书的 `notBefore` 时间。将配对的 `cert.pem` 和 `key.pem` 更新到 `gateway` 与 `reader-gateway` 两个卷中。沿用同一 Compose 项目配置运行 `docker compose restart gateway reader-gateway`。用 `docker compose ps gateway reader-gateway` 确认两个服务均已就绪(`healthy`),再校验两个 TLS 入口,再用 `./client.sh a read`、`./client.sh b read` 和 `./client.sh reader read` 读回任务。更换 CA 时提前分发新信任。 -- 更新 PostgreSQL 口令或版本前先备份,在隔离项目演练迁移和回退。同步修改数据库角色与对应凭据文件。PostgreSQL 跨大版本需使用其正式迁移流程。 - -[English](README.en.md) diff --git a/examples/coordination/remote-authority-compose/build_bundle.py b/examples/coordination/remote-authority-compose/build_bundle.py deleted file mode 100644 index baa8f4beb0..0000000000 --- a/examples/coordination/remote-authority-compose/build_bundle.py +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env python3 -"""Build a source-pinned, reproducible LoopX self-hosting bundle.""" - -import argparse -import gzip -import hashlib -import io -import os -from pathlib import Path -import re -import subprocess -import tarfile -import tempfile - - -TEMPLATE = Path(__file__).resolve().parent -REPOSITORY = TEMPLATE.parents[2] -BUNDLE_NAME = "loopx-remote-authority-compose" -SOURCE_PATHS = ( - "LICENSE", - "NOTICE", - "package.json", - "package-lock.json", - "examples/coordination/remote-authority-client.ts", - "examples/coordination/remote-authority-service.ts", - "loopx/control_plane", -) -BUNDLE_FILES = ( - ".dockerignore", - "Dockerfile", - "README.en.md", - "README.md", - "client.sh", - "compose.yaml", - "launch.mjs", - "provision.mjs", - "start.sh", - "state.sh", -) - - -def git(*args: str) -> bytes: - return subprocess.check_output(["git", *args], cwd=REPOSITORY) - - -def gzip_archive(data: bytes) -> bytes: - encoded = io.BytesIO() - with gzip.GzipFile(filename="", fileobj=encoded, mode="wb", mtime=0) as compressed: - compressed.write(data) - return encoded.getvalue() - - -def bundle(revision: str) -> bytes: - source_tar = git("archive", "--format=tar", revision, "--", *SOURCE_PATHS) - source_archive = gzip_archive(source_tar) - source_digest = hashlib.sha256(source_archive).hexdigest() - files = {name: (TEMPLATE / name).read_bytes() for name in BUNDLE_FILES} - image_id = hashlib.sha256(b"".join( - hashlib.sha256(data).digest() - for data in (source_archive, files["Dockerfile"], files["compose.yaml"], - files["provision.mjs"], files["launch.mjs"]) - )).hexdigest() - files[".env"] = ( - f"LOOPX_REVISION={revision}\nLOOPX_SOURCE_SHA256={source_digest}\n" - f"LOOPX_IMAGE_ID={image_id}\n" - ).encode("ascii") - files["loopx-source.tar.gz"] = source_archive - for name in ("LICENSE", "NOTICE"): - files[name] = git("show", f"{revision}:{name}") - - archive = io.BytesIO() - with tarfile.open(fileobj=archive, mode="w", format=tarfile.USTAR_FORMAT) as output: - for name, contents in sorted(files.items()): - entry = tarfile.TarInfo(f"{BUNDLE_NAME}/{name}") - entry.size = len(contents) - entry.mode = 0o755 if name.endswith(".sh") else 0o644 - output.addfile(entry, io.BytesIO(contents)) - return gzip_archive(archive.getvalue()) - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--source-revision", required=True, help="full LoopX Git commit") - parser.add_argument("--output", type=Path, required=True, help="new tar.gz file") - args = parser.parse_args() - if not re.fullmatch(r"[0-9a-f]{40}", args.source_revision): - parser.error("--source-revision must be a full lowercase Git commit SHA") - try: - if git("cat-file", "-t", args.source_revision).strip() != b"commit": - parser.error("--source-revision must identify a commit") - contents = bundle(args.source_revision) - with tempfile.TemporaryDirectory(dir=args.output.parent) as work: - temporary = Path(work) / "bundle.tar.gz" - temporary.write_bytes(contents) - os.link(temporary, args.output) - except (OSError, subprocess.CalledProcessError) as error: - parser.exit(1, f"Could not build bundle: {error}\n") - print(f"{hashlib.sha256(contents).hexdigest()} {args.output}") - - -if __name__ == "__main__": - main() diff --git a/examples/coordination/remote-authority-compose/client.sh b/examples/coordination/remote-authority-compose/client.sh deleted file mode 100755 index 97c343ddfb..0000000000 --- a/examples/coordination/remote-authority-compose/client.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh -set -eu -case "${1:-}" in - a|b|reader) actor=$1; shift ;; - *) printf '%s\n' "Usage: ./client.sh a|b|reader [read|request|recover|descriptor|ca]" >&2; exit 2 ;; -esac -directory=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) -cd "$directory" -[ "$#" -gt 0 ] || set -- read -exec docker compose run --rm --no-deps -T "client-${actor}" "$@" diff --git a/examples/coordination/remote-authority-compose/compose.yaml b/examples/coordination/remote-authority-compose/compose.yaml deleted file mode 100644 index 79b17816d4..0000000000 --- a/examples/coordination/remote-authority-compose/compose.yaml +++ /dev/null @@ -1,147 +0,0 @@ -name: loopx-authority -x-app: &app - image: loopx/remote-authority:${LOOPX_IMAGE_ID:?unpack a generated bundle} - pull_policy: never - read_only: true - cap_drop: [ALL] - security_opt: [no-new-privileges:true] - cpus: "0.50" - mem_limit: 512m - pids_limit: 128 - logging: &bounded-logs - driver: local - options: - max-size: "10m" - max-file: "3" -services: - secrets: - <<: *app - build: - context: . - args: - LOOPX_REVISION: ${LOOPX_REVISION:?unpack a generated bundle} - LOOPX_SOURCE_SHA256: ${LOOPX_SOURCE_SHA256:?unpack a generated bundle} - user: "0:0" - cap_add: [CHOWN, DAC_OVERRIDE] - network_mode: none - tmpfs: [/tmp] - command: [node, --experimental-strip-types, provision.mjs, secrets] - volumes: - - pg-admin:/secrets/admin - - pg-auth:/secrets/pg-auth - - pg-data:/secrets/database:ro - - gateway:/secrets/gateway - - reader-gateway:/secrets/reader-gateway - - client-a:/secrets/client-a - - client-b:/secrets/client-b - - reader:/secrets/reader - - health:/secrets/health - db: - image: public.ecr.aws/docker/library/postgres:16.16-bookworm@sha256:a49f7438ba03589f9b1ca062f2da82e899b48ea1dc50d2449fa09a70be83ebca - depends_on: - secrets: - condition: service_completed_successfully - environment: - POSTGRES_DB: authority - POSTGRES_USER: bootstrap - POSTGRES_PASSWORD_FILE: /secrets/pg-auth/postgres-password - volumes: - - pg-auth:/secrets/pg-auth:ro - - pg-data:/var/lib/postgresql/data - networks: [backend] - cpus: "0.75" - mem_limit: 768m - pids_limit: 128 - logging: *bounded-logs - restart: unless-stopped - healthcheck: - test: [CMD, pg_isready, -h, 127.0.0.1, -U, bootstrap, -d, authority] - interval: 2s - timeout: 2s - retries: 40 - setup: - <<: *app - user: "0:0" - cap_add: [DAC_OVERRIDE] - depends_on: - db: - condition: service_healthy - command: [node, --experimental-strip-types, provision.mjs, database] - volumes: - - pg-admin:/secrets/admin - - pg-auth:/secrets/pg-auth:ro - - gateway:/secrets/gateway:ro - networks: [backend] - gateway: - <<: *app - depends_on: - setup: - condition: service_completed_successfully - command: [node, --experimental-strip-types, launch.mjs, gateway] - volumes: - - gateway:/secrets/gateway:ro - - health:/secrets/health:ro - networks: [frontend, backend] - ports: - - "127.0.0.1:${LOOPX_PORT:-8443}:8443" - restart: unless-stopped - healthcheck: - test: [CMD, node, --experimental-strip-types, launch.mjs, health] - interval: 3s - timeout: 4s - retries: 25 - client-a: - <<: *app - profiles: [clients] - entrypoint: [node, --experimental-strip-types, launch.mjs, client] - volumes: - - client-a:/secrets/client:ro - networks: [frontend] - client-b: - <<: *app - profiles: [clients] - entrypoint: [node, --experimental-strip-types, launch.mjs, client] - volumes: - - client-b:/secrets/client:ro - networks: [frontend] - reader-gateway: - <<: *app - depends_on: - setup: - condition: service_completed_successfully - command: [node, --experimental-strip-types, launch.mjs, reader-gateway] - volumes: - - reader-gateway:/secrets/reader-gateway:ro - - reader:/secrets/client:ro - networks: [frontend, backend] - ports: - - "127.0.0.1:${LOOPX_READ_PORT:-8444}:8443" - restart: unless-stopped - healthcheck: - test: [CMD, node, --experimental-strip-types, launch.mjs, reader-health] - interval: 3s - timeout: 4s - retries: 25 - client-reader: - <<: *app - profiles: [clients] - entrypoint: [node, --experimental-strip-types, launch.mjs, client] - environment: - LOOPX_SERVICE_HOST: reader-gateway - volumes: - - reader:/secrets/client:ro - networks: [frontend] -networks: - backend: - internal: true - frontend: -volumes: - pg-admin: - pg-auth: - pg-data: - gateway: - reader-gateway: - client-a: - client-b: - reader: - health: diff --git a/examples/coordination/remote-authority-compose/launch.mjs b/examples/coordination/remote-authority-compose/launch.mjs deleted file mode 100644 index 33bc880fe6..0000000000 --- a/examples/coordination/remote-authority-compose/launch.mjs +++ /dev/null @@ -1,44 +0,0 @@ -import {readFileSync} from "node:fs"; -import {callRemoteAuthority} from "./loopx/control_plane/coordination/remote_authority_http.ts"; - -const [mode, action = "read"] = process.argv.slice(2); -const port = "8443"; -const read = (directory, file) => readFileSync(`/secrets/${directory}/${file}`, "utf8"); - -async function main() { - if (mode === "gateway" || mode === "reader-gateway") { - const password = read(mode, "database-password"); - process.env.LOOPX_AUTHORITY_DATABASE_URL = `postgresql://loopx_gateway:${encodeURIComponent(password)}@db:5432/authority`; - process.argv.splice(2, Infinity, "--policy", `/secrets/${mode}/policy.json`, - "--cert", `/secrets/${mode}/cert.pem`, "--key", `/secrets/${mode}/key.pem`, - "--host", "0.0.0.0", "--port", port); - await import("./examples/coordination/remote-authority-service.ts"); - return; - } - const directory = mode === "health" ? "health" : "client"; - const descriptor = read(directory, "request.json"); - const ca = read(directory, "ca.pem"); - if (action === "descriptor" || action === "ca") { - process.stdout.write(action === "descriptor" ? descriptor + "\n" : ca); - return; - } - const endpoint = mode === "health" || mode === "reader-health" ? `https://127.0.0.1:${port}` - : `https://${process.env.LOOPX_SERVICE_HOST ?? "gateway"}:${port}`; - const credential = read(directory, "credential"); - if (action === "request" || action === "recover") { - process.env.LOOPX_AUTHORITY_CREDENTIAL = credential; - process.argv.splice(2, Infinity, "--endpoint", endpoint, "--ca", `/secrets/${directory}/ca.pem`); - if (action === "recover") process.argv.push("--recover"); - await import("./examples/coordination/remote-authority-client.ts"); - return; - } - if (action !== "read") throw new Error("Unknown command"); - const result = await callRemoteAuthority(endpoint, credential, JSON.parse(descriptor), {ca: Buffer.from(ca)}); - if (mode === "client") process.stdout.write(JSON.stringify(result) + "\n"); - if (result.status !== "found") process.exitCode = 1; -} - -main().catch(() => { - console.error("LoopX command failed"); - process.exitCode = 1; -}); diff --git a/examples/coordination/remote-authority-compose/provision.mjs b/examples/coordination/remote-authority-compose/provision.mjs deleted file mode 100644 index 0d1d68c718..0000000000 --- a/examples/coordination/remote-authority-compose/provision.mjs +++ /dev/null @@ -1,223 +0,0 @@ -import {createHash, randomBytes} from "node:crypto"; -import {execFileSync} from "node:child_process"; -import {chmodSync, chownSync, existsSync, linkSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync} from "node:fs"; -import {dirname, join} from "node:path"; -import {REMOTE_AUTHORITY_POLICY, REMOTE_AUTHORITY_SCOPED_READ_POLICY} from "./loopx/control_plane/coordination/remote_authority_policy.ts"; - -const admin = "/secrets/admin"; -const pgAuth = "/secrets/pg-auth"; -const generationFile = join(admin, "generation.json"); -const provisionedFlag = join(admin, "volumes-ready"); -const installedFlag = join(admin, "database-ready"); -const tenant = "local"; -const goal = "collaboration"; -const todo = "todo_welcome"; -const actors = ["client-a", "client-b", "health", "reader"]; -const bindingOperation = "compose-bind-starter-goal"; -const goalRef = identity => ({goal_id: goal, goal_instance_id: `ginst_${identity.slice("postgresql:".length)}`}); -const secret = () => randomBytes(32).toString("hex"); -const read = path => readFileSync(path, "utf8"); -const policyDigest = value => createHash("sha256").update(value).digest("hex"); - -function createFile(path, value, uid = 0, replace = false) { - const temporary = mkdtempSync(join(dirname(path), ".init-")); - const entry = join(temporary, "file"); - try { - writeFileSync(entry, value, {mode: 0o400, flag: "wx", flush: true}); - chownSync(entry, uid, uid); - try { - if (replace) renameSync(entry, path); - else linkSync(entry, path); - } catch (error) { - if (error.code !== "EEXIST") throw error; - } - } finally { - rmSync(temporary, {recursive: true, force: true}); - } -} - -function newGeneration() { - const scratch = mkdtempSync("/tmp/authority-certs-"); - const openssl = (...args) => execFileSync("openssl", args, {cwd: scratch, stdio: "ignore", timeout: 15000}); - try { - openssl("req", "-x509", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256", "-nodes", - "-subj", "/CN=LoopX Local CA", "-days", "3650", "-sha256", - "-addext", "basicConstraints=critical,CA:TRUE,pathlen:0", - "-addext", "keyUsage=critical,keyCertSign,cRLSign", - "-keyout", "ca.key", "-out", "ca.pem"); - openssl("req", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:P-256", "-nodes", - "-subj", "/CN=gateway", "-keyout", "server.key", "-out", "server.csr"); - writeFileSync(join(scratch, "server.ext"), "basicConstraints=critical,CA:FALSE\nkeyUsage=critical,digitalSignature\nextendedKeyUsage=serverAuth\nsubjectAltName=DNS:gateway,DNS:reader-gateway,DNS:localhost,IP:127.0.0.1\n"); - openssl("x509", "-req", "-in", "server.csr", "-CA", "ca.pem", "-CAkey", "ca.key", - "-CAcreateserial", "-days", "397", "-sha256", "-extfile", "server.ext", "-out", "server.pem"); - return { - identity: `postgresql:${randomBytes(16).toString("hex")}`, - databasePassword: secret(), - runtimePassword: secret(), - credentials: Object.fromEntries(actors.map(actor => [actor, secret()])), - root: read(join(scratch, "ca.pem")), - rootKey: read(join(scratch, "ca.key")), - key: read(join(scratch, "server.key")), - cert: read(join(scratch, "server.pem")), - }; - } finally { - rmSync(scratch, {recursive: true, force: true}); - } -} - -function prepareSecretVolumes() { - const requiredFiles = { - gateway: ["database-password", "policy.json", "cert.pem", "key.pem"], - "reader-gateway": ["database-password", "policy.json", "cert.pem", "key.pem"], - ...Object.fromEntries(actors.map(actor => [actor, ["credential", "ca.pem", "request.json"]])), - "pg-auth": ["postgres-password"], - }; - const hasDatabase = existsSync("/secrets/database/PG_VERSION"); - if (!existsSync(generationFile)) { - const previous = hasDatabase || Object.keys(requiredFiles).some(volume => existsSync(`/secrets/${volume}/identity`)); - if (previous) throw new Error("bootstrap identity missing for existing state"); - chmodSync(admin, 0o700); - createFile(generationFile, JSON.stringify(newGeneration())); - } - const generation = JSON.parse(read(generationFile)); - const {identity} = generation; - const initialized = existsSync(provisionedFlag); - if ((initialized && read(provisionedFlag) !== identity) || (existsSync(installedFlag) && !hasDatabase)) { - throw new Error("mismatched bootstrap identity or missing existing database volume"); - } - if (!initialized) { - populateSecretVolumes(generation); - createFile(join(admin, "ca.key"), generation.rootKey); - createFile(join(admin, "ca.pem"), generation.root); - } - for (const [volume, files] of Object.entries(requiredFiles)) { - const location = `/secrets/${volume}`; - if (read(join(location, "identity")) !== identity || files.some(file => !existsSync(join(location, file)))) { - throw new Error("credential volume needs restoration"); - } - } - createFile(provisionedFlag, identity); - // The generation record needs plaintext copies only to resume an interrupted first start. - if (generation.credentials) createFile(generationFile, JSON.stringify({identity}), 0, true); -} - -function populateSecretVolumes({identity, credentials, root, cert, key, runtimePassword, databasePassword}) { - const scopes = [{tenant_id: tenant, goal_id: goal, store_identity: identity, registered_agents: actors}]; - const principals = actors.filter(actor => actor !== "reader").map(actor => ({ - principal_id: `principal-${actor}`, - credential_sha256: policyDigest(credentials[actor]), - grants: [{ - tenant_id: tenant, goal_id: goal, actor_id: actor, - operations: actor === "health" ? ["read"] : [ - "read", "claim", "progress", "renew", "release", "complete", ...(actor === "client-a" ? ["todo_add"] : []), - ], - }], - })); - const files = { - gateway: {"database-password": runtimePassword, "policy.json": JSON.stringify({ - schema_version: REMOTE_AUTHORITY_POLICY, scopes, principals, - }), "cert.pem": cert, "key.pem": key}, - "reader-gateway": {"database-password": runtimePassword, "policy.json": JSON.stringify({ - schema_version: REMOTE_AUTHORITY_SCOPED_READ_POLICY, - scopes: [{...scopes[0], registered_agents: ["reader"], todo_read_acl: [{ - goal_ref: goalRef(identity), todo_id: todo, principal_id: "principal-reader", actor_id: "reader", - todo_origin_operation_id: bindingOperation, - }]}], - principals: [{principal_id: "principal-reader", credential_sha256: policyDigest(credentials.reader), - grants: [{tenant_id: tenant, goal_id: goal, actor_id: "reader", goal_ref: goalRef(identity), - operations: ["read", "snapshot"]}]}], - }), "cert.pem": cert, "key.pem": key}, - ...Object.fromEntries(actors.map(actor => [actor, { - credential: credentials[actor], "ca.pem": root, - "request.json": JSON.stringify({ - schema_version: "loopx_remote_authority_request_v0", tenant_id: tenant, goal_id: goal, - store_identity: identity, actor_id: actor, todo_id: todo, operation: "read", - }), - }])), - "pg-auth": {"postgres-password": databasePassword}, - }; - for (const [volume, contents] of Object.entries(files)) { - const location = `/secrets/${volume}`; - const owner = volume === "pg-auth" ? 0 : 1000; - chmodSync(location, 0o700); - chownSync(location, owner, owner); - const marker = join(location, "identity"); - createFile(marker, identity, owner); - if (read(marker) !== identity) throw new Error("mismatched credential volume"); - for (const [file, value] of Object.entries(contents)) createFile(join(location, file), value, owner); - } -} - -async function prepareDatabase() { - const {Pool} = await import("pg"); - const {setupPostgreSqlAuthoritySchemaAndRole} = await import("./loopx/control_plane/coordination/postgresql_authority_seed.ts"); - const {PostgreSqlAuthorityStore} = await import("./loopx/control_plane/coordination/postgresql_authority_store.ts"); - const {coordinationTodoReadModel} = await import("./loopx/control_plane/coordination/coordination_projection.ts"); - const {canonicalTodoDomainRecord, TODO_DOMAIN_ITEM_SCHEMA, TODO_DOMAIN_READ_RECORD_SCHEMA} = - await import("./loopx/control_plane/coordination/coordination_state_contract.ts"); - const {materializeTodoRecordForSchema} = await import("./loopx/control_plane/coordination/todo_presentation.ts"); - const {transitionGoalAcceptanceLifecycle} = await import("./loopx/control_plane/goals/acceptance_authority.ts"); - const {identity} = JSON.parse(read(generationFile)); - const runtimePassword = read("/secrets/gateway/database-password"); - const installed = existsSync(installedFlag); - if (read(provisionedFlag) !== identity || (installed && read(installedFlag) !== identity) - || !/^[a-f0-9]{64}$/.test(runtimePassword)) throw new Error("mismatched database initialization state"); - const opts = {host: "db", port: 5432, database: "authority", connectionTimeoutMillis: 5000, statement_timeout: 15000}; - const scopedStore = pool => new PostgreSqlAuthorityStore({connect: async () => { - const client = await pool.connect(); - return {query: (sql, args) => client.query(sql, args ? [...args] : undefined), release: error => client.release(error)}; - }}, {tenant_id: tenant, goal_id: goal, expected_store_identity: identity}); - const setup = new Pool({...opts, user: "bootstrap", password: read(join(pgAuth, "postgres-password"))}); - const runtime = new Pool({...opts, user: "loopx_gateway", password: runtimePassword}); - try { - if (!installed) { - const database = {connect: async () => { - const client = await setup.connect(); - return {query: (sql, args) => client.query(sql, args ? [...args] : undefined), release: error => client.release(error)}; - }}; - const schema = await setupPostgreSqlAuthoritySchemaAndRole({database, storeIdentity: identity, runtimeRole: "loopx_gateway"}); - if (schema.status !== "installed") throw new Error("authority schema installation failed"); - const role = await setup.query("SELECT rolcanlogin FROM pg_roles WHERE rolname = 'loopx_gateway'"); - if (!role.rows[0]?.rolcanlogin) { - await setup.query(`ALTER ROLE loopx_gateway LOGIN PASSWORD '${runtimePassword}'`); - } - await setup.query("REVOKE CONNECT, TEMPORARY ON DATABASE authority FROM PUBLIC; GRANT CONNECT ON DATABASE authority TO loopx_gateway"); - const store = scopedStore(runtime); - const before = await store.loadAuthority(); - if (before.status === "missing") { - const record = materializeTodoRecordForSchema(canonicalTodoDomainRecord({ - schema_version: TODO_DOMAIN_ITEM_SCHEMA, todo_id: todo, role: "agent", status: "open", - done: false, archive_state: "active", task_class: "advancement_task", - text: "Verify the two-client handoff from claim to accepted result", - }), TODO_DOMAIN_READ_RECORD_SCHEMA, "starter Todo"); - const todos = [record]; - const seeded = await store.commitAuthority({operation_id: "compose-initial-state", expected_provider_revision: null, - next_projection: {goal_id: goal, handoff_mode: "hard_lease", todos, leases: [], - todo_read_model: coordinationTodoReadModel(todos, TODO_DOMAIN_READ_RECORD_SCHEMA)}, events: [], receipts: []}); - if (seeded.status !== "applied") throw new Error("initial task could not be committed"); - } - const bound = await transitionGoalAcceptanceLifecycle(store, { - goal_id: goal, actor_agent_id: null, operation_id: bindingOperation, - transition: {kind: "bind_existing", goal_ref: goalRef(identity)}, - }); - if (bound.status !== "applied" && bound.status !== "replayed") throw new Error("starter Goal could not be bound"); - } - const after = await scopedStore(runtime).loadAuthority(); - if (after.status !== "loaded") throw new Error("initialized database is unavailable"); - createFile(installedFlag, identity); - } finally { - await Promise.all([setup.end(), runtime.end()]); - } -} - -const phase = process.argv[2]; -try { - if (phase === "secrets") prepareSecretVolumes(); - else if (phase === "database") await prepareDatabase(); - else throw new Error("unknown bootstrap command"); - process.stdout.write(`LoopX ${phase} ready\n`); -} catch (error) { - // Database/crypto errors may reveal credentials. Expose the phase and stable code only. - console.error(`LoopX ${phase} failed (${error.code ?? "init_state"})`); - process.exitCode = 1; -} diff --git a/examples/coordination/remote-authority-compose/rehearse.mjs b/examples/coordination/remote-authority-compose/rehearse.mjs deleted file mode 100644 index 5cc85c59b2..0000000000 --- a/examples/coordination/remote-authority-compose/rehearse.mjs +++ /dev/null @@ -1,175 +0,0 @@ -#!/usr/bin/env node -// Exercise the packaged entry points against a disposable Compose project. -import assert from "node:assert/strict"; -import {spawnSync} from "node:child_process"; -import {randomBytes} from "node:crypto"; -import {createServer} from "node:net"; -import {resolve} from "node:path"; -import {performance} from "node:perf_hooks"; -import {setTimeout as sleep} from "node:timers/promises"; - -const [bundle] = process.argv.slice(2); -if (!bundle || process.argv.length !== 3 || bundle === "--help") { - console.log("Usage: node rehearse.mjs "); - process.exit(bundle === "--help" ? 0 : 2); -} - -const project = `loopx-rehearsal-${randomBytes(6).toString("hex")}`; -const cwd = resolve(bundle); -const env = {...process.env, COMPOSE_PROJECT_NAME: project, LOOPX_PORT: await freeLoopbackPort(), - LOOPX_READ_PORT: await freeLoopbackPort()}; -const started = performance.now(); -let readySeconds; -let operationError; - -async function freeLoopbackPort() { - const server = createServer(); - await new Promise((resolve, reject) => { - server.once("error", reject); - server.listen(0, "127.0.0.1", resolve); - }); - const port = server.address().port; - await new Promise(resolve => server.close(resolve)); - return String(port); -} - -function run(command, args, {input, allowRejected = false} = {}) { - const result = spawnSync(command, args, {cwd, env, input, encoding: "utf8", timeout: 150_000}); - if (result.error) throw result.error; - if (result.signal || (result.status !== 0 && !allowRejected)) { - throw new Error(`${command} ${args.join(" ")} exited ${result.status ?? result.signal}: ${result.stderr.slice(-2048)}`); - } - return result.stdout; -} - -function client(actor, action, request) { - return JSON.parse(run("./client.sh", [actor, action], { - input: request === undefined ? undefined : JSON.stringify(request), - allowRejected: true, - })); -} - -try { - const startup = run("./start.sh", []); - const a = client("a", "descriptor"); - const b = client("b", "descriptor"); - const reader = client("reader", "descriptor"); - const aStart = client("a", "read"); - const bStart = client("b", "read"); - assert.equal(aStart.status, "found"); - assert.equal(bStart.status, "found"); - assert.equal(aStart.provider_revision, bStart.provider_revision); - assert.deepEqual(aStart.todo, bStart.todo); - assert.notEqual(a.actor_id, b.actor_id); - assert.equal(client("reader", "read").schema_version, "loopx_remote_authority_scoped_read_response_v1"); - readySeconds = Math.round((performance.now() - started) / 100) / 10; - const wrongStore = {...b, store_identity: "postgresql:" + "0".repeat(32)}; - assert.equal(client("b", "request", wrongStore).status, "rejected"); - assert.equal(client("b", "request", {...b, actor_id: a.actor_id}).status, "rejected"); - const hidden = "todo_outside_read_grant"; - const createHidden = {...a, todo_id: hidden, operation: "todo_add", - request_id: "rehearsal-create-hidden", text: "Task outside the reader grant", - note: "Private writer-only note"}; - assert.equal(client("a", "request", createHidden).result.status, "applied"); - const createdReceipt = client("a", "recover", createHidden); - assert.equal(createdReceipt.status, "found"); - assert.equal(createdReceipt.historical_only, true); - assert.equal(createdReceipt.result.changed, true); - assert.equal(createdReceipt.result.todo.todo_id, hidden); - const beforeReadChecks = client("a", "read").provider_revision; - assert.equal(client("reader", "request", {...reader, todo_id: hidden}).status, "rejected"); - assert.equal(client("reader", "request", {...reader, operation: "claim", request_id: "reader-cannot-claim"}).status, "rejected"); - for (const scope of [{tenant_id: "other"}, {goal_id: "other"}, {actor_id: a.actor_id}, - {store_identity: "postgresql:" + "0".repeat(32)}]) { - assert.equal(client("reader", "request", {...reader, ...scope}).status, "rejected"); - } - const {todo_id: ignored, ...readerScope} = reader; - const snapshot = client("reader", "request", {...readerScope, operation: "snapshot"}); - assert.equal(snapshot.status, "snapshot"); - assert.deepEqual(snapshot.todos.map(row => row.todo_id), [reader.todo_id]); - assert.equal(Object.hasOwn(snapshot, "provider_revision"), false); - assert.equal(JSON.stringify(snapshot).includes("Private writer-only note"), false); - assert.equal(client("a", "read").provider_revision, beforeReadChecks); - - const claimA = {...a, operation: "claim", request_id: "rehearsal-claim-a"}; - const acquired = client("a", "request", claimA).result; - assert.equal(acquired.status, "applied"); - const progressA = {...a, operation: "progress", request_id: "rehearsal-progress-a", - claim_request_id: claimA.request_id, note: "First client prepared the handoff"}; - assert.equal(client("a", "request", progressA).result.status, "applied"); - const readA = client("a", "read"); - const renewed = client("a", "request", {...a, operation: "renew", ttl_seconds: 20, - idempotency_key: acquired.lease.idempotency_key, expected_version: readA.lease.version}).result; - assert.equal(renewed.status, "applied"); - const beforeTakeover = client("b", "request", {...b, operation: "claim", request_id: "rehearsal-too-early"}); - assert.equal(beforeTakeover.result.status, "failed"); - // Both timestamps belong to the authority; the host clock may differ. - const leaseDuration = Date.parse(renewed.lease.expires_at) - Date.parse(renewed.lease.updated_at); - assert(Number.isFinite(leaseDuration) && leaseDuration > 0 && leaseDuration <= 21_000); - await sleep(leaseDuration + 250); - - const claimB = {...b, operation: "claim", request_id: "rehearsal-claim-b"}; - const takeover = client("b", "request", claimB).result; - assert.equal(takeover.status, "applied"); - assert(takeover.lease.lease_epoch > renewed.lease.lease_epoch); - assert.equal(client("a", "request", {...progressA, request_id: "rehearsal-stale-owner"}).result.status, "failed"); - const complete = {...b, operation: "complete", request_id: "rehearsal-complete", - claim_request_id: claimB.request_id, evidence: "evidence:compose-rehearsal"}; - assert.equal(client("b", "request", complete).result.status, "applied"); - const recover = client("b", "recover", complete); - assert.equal(recover.status, "found"); - assert.equal(recover.historical_only, true); - assert.match(recover.receipt_sha256, /^[a-f0-9]{64}$/); - - run("docker", ["compose", "down", "--timeout", "5"]); - run("./start.sh", []); - assert.equal(client("a", "recover", createHidden).receipt_sha256, createdReceipt.receipt_sha256); - assert.equal(client("a", "request", createHidden).result.status, "replayed"); - assert.equal(client("b", "recover", complete).receipt_sha256, recover.receipt_sha256); - assert.equal(client("b", "request", complete).result.status, "replayed"); - const aEnd = client("a", "read"); - const bEnd = client("b", "read"); - assert.equal(aEnd.todo.status, "done"); - assert.deepEqual(aEnd.todo, bEnd.todo); - assert.equal(aEnd.provider_revision, bEnd.provider_revision); - assert.equal(client("reader", "read").todo.status, "done"); - assert.equal(client("reader", "recover", {...complete, actor_id: reader.actor_id}).status, "rejected"); - function policyScript(code) { - run("docker", ["compose", "run", "--rm", "--no-deps", "-T", "--entrypoint", "node", "secrets", "-e", code]); - } - const readerPolicy = "/secrets/reader-gateway/policy.json"; - policyScript(`const fs = require('node:fs'); const p = '${readerPolicy}'; - fs.renameSync(p, p + '.saved'); fs.writeFileSync(p + '.next', JSON.stringify({ - ...JSON.parse(fs.readFileSync(p + '.saved')), principals: []})); fs.renameSync(p + '.next', p);`); - assert.equal(client("reader", "read").status, "rejected"); - run("docker", ["compose", "restart", "reader-gateway"]); - const listeningDeadline = performance.now() + 15_000; - let revoked = client("reader", "read"); - while (revoked.status === "unavailable" && performance.now() < listeningDeadline) { - await sleep(200); - revoked = client("reader", "read"); - } - assert.equal(revoked.status, "rejected"); - assert.equal(revoked.reason_code, "principal_unauthenticated"); - policyScript(`const fs = require('node:fs'); const p = '${readerPolicy}'; fs.unlinkSync(p);`); - assert.equal(client("reader", "read").status, "unavailable"); - policyScript(`const fs = require('node:fs'); const p = '${readerPolicy}'; fs.writeFileSync(p, '{');`); - assert.equal(client("reader", "read").status, "unavailable"); - policyScript(`const fs = require('node:fs'); const p = '${readerPolicy}'; fs.renameSync(p + '.saved', p);`); - assert.equal(client("reader", "read").todo.status, "done"); - assert.equal(client("a", "read").provider_revision, aEnd.provider_revision); - const architecture = run("docker", ["compose", "exec", "-T", "gateway", "uname", "-m"]).trim(); - console.log(JSON.stringify({ok: true, architecture, twoClientReadySeconds: readySeconds, - startup: startup.trim(), leaseTakeover: true, receiptPreserved: true, - scopedReadDefaultDeny: true, scopedWriteUnavailable: true, revocationPreserved: true})); -} catch (error) { - operationError = error; - throw error; -} finally { - try { - run("docker", ["compose", "down", "--timeout", "5", "--volumes"]); - } catch (error) { - if (!operationError) throw error; - console.error(`Cleanup of ${project} also failed: ${error.message}`); - } -} diff --git a/examples/coordination/remote-authority-compose/start.sh b/examples/coordination/remote-authority-compose/start.sh deleted file mode 100755 index d9052909a9..0000000000 --- a/examples/coordination/remote-authority-compose/start.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -set -eu -directory=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) -cd "$directory" -started=$(date +%s) -docker compose pull --policy missing db -pulled=$(date +%s) -docker compose build secrets -built=$(date +%s) -docker compose up --detach --no-build --wait --wait-timeout 120 -ready=$(date +%s) -printf 'LoopX ready at https://127.0.0.1:%s (DB image %ss, build %ss, startup %ss, total %ss)\n' \ - "${LOOPX_PORT:-8443}" "$((pulled - started))" "$((built - pulled))" "$((ready - built))" "$((ready - started))" -printf 'Inspect the starter Todo with ./client.sh a read\n' -printf 'Scoped reads: ./client.sh reader read (https://127.0.0.1:%s)\n' "${LOOPX_READ_PORT:-8444}" diff --git a/examples/coordination/remote-authority-compose/state.sh b/examples/coordination/remote-authority-compose/state.sh deleted file mode 100755 index db4433a831..0000000000 --- a/examples/coordination/remote-authority-compose/state.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/sh -set -eu - -usage() { - printf '%s\n' 'Usage: ./state.sh backup /absolute/new-directory' \ - ' COMPOSE_PROJECT_NAME=new-name ./state.sh restore /absolute/backup-directory' >&2 - exit 2 -} - -[ "$#" -eq 2 ] || usage -operation=$1 -archive=$2 -case "$archive" in /*) ;; *) usage ;; esac -directory=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) -cd "$directory" -project=${COMPOSE_PROJECT_NAME:-loopx-authority} -case "$project" in ''|*[!a-z0-9_-]*) usage ;; esac -image=$(docker compose config --images | awk '/^loopx\/remote-authority:/ { print; exit }') -[ -n "$image" ] || { printf '%s\n' 'Cannot resolve the packaged gateway image' >&2; exit 1; } -volumes='pg-admin pg-auth pg-data gateway reader-gateway client-a client-b reader health' -umask 077 - -digest() { - if command -v sha256sum >/dev/null 2>&1; then - sha256sum < "$1" - else - shasum -a 256 < "$1" - fi -} - -case "$operation" in - backup) - for volume in $volumes; do - label=$(docker volume inspect --format '{{index .Labels "com.docker.compose.project"}}/{{index .Labels "com.docker.compose.volume"}}' "${project}_${volume}") - [ "$label" = "$project/$volume" ] || { printf '%s\n' 'Unexpected Compose volume ownership' >&2; exit 1; } - done - docker image inspect "$image" >/dev/null - mkdir -m 700 "$archive" - docker compose --project-name "$project" down - for volume in $volumes; do - docker run --rm --pull never --read-only --network none --user 0:0 \ - --cpus 0.25 --memory 256m --pids-limit 64 --log-driver none \ - --mount "type=volume,src=${project}_${volume},dst=/state,readonly" \ - --entrypoint tar "$image" --numeric-owner -C /state -cpf - . > "$archive/$volume.tar" - digest "$archive/$volume.tar" > "$archive/$volume.sha256" - done - printf '%s\n' "$image" > "$archive/COMPLETE" - printf '%s\n' 'Backup complete. Run ./start.sh to bring this project back online.' - ;; - restore) - [ -n "${COMPOSE_PROJECT_NAME:-}" ] || usage - [ "$(cat "$archive/COMPLETE")" = "$image" ] || - { printf '%s\n' 'Incomplete backup or bundle version mismatch' >&2; exit 1; } - for volume in $volumes; do - [ "$(digest "$archive/$volume.tar")" = "$(cat "$archive/$volume.sha256")" ] || - { printf 'Checksum mismatch for %s\n' "$volume" >&2; exit 1; } - if docker volume inspect "${project}_${volume}" >/dev/null 2>&1; then - printf 'Remove or choose a different restore project for %s\n' "${project}_${volume}" >&2 - exit 1 - fi - done - docker compose --project-name "$project" build secrets - for volume in $volumes; do - docker volume create --label "com.docker.compose.project=$project" \ - --label "com.docker.compose.volume=$volume" "${project}_${volume}" >/dev/null - docker run --rm --pull never --interactive --read-only --network none --user 0:0 \ - --cpus 0.25 --memory 256m --pids-limit 64 --log-driver none \ - --mount "type=volume,src=${project}_${volume},dst=/state" \ - --entrypoint tar "$image" --numeric-owner --same-owner --same-permissions -C /state -xpf - < "$archive/$volume.tar" - done - printf '%s\n' 'Restore complete. Run ./start.sh with the same COMPOSE_PROJECT_NAME.' - ;; - *) usage ;; -esac diff --git a/examples/coordination/remote-authority-service.ts b/examples/coordination/remote-authority-service.ts deleted file mode 100644 index f4b9507dee..0000000000 --- a/examples/coordination/remote-authority-service.ts +++ /dev/null @@ -1,42 +0,0 @@ -/** Explicit candidate launcher; never changes local provider selection or data. */ -import {readFile} from "node:fs/promises"; -import {parseArgs} from "node:util"; -import {Pool} from "pg"; -import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts"; -import {fileRemoteAuthorityAdmission, readRemoteAuthorityPolicy} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; -import {createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts"; - -const {values} = parseArgs({options: {policy: {type: "string"}, cert: {type: "string"}, key: {type: "string"}, - host: {type: "string", default: "127.0.0.1"}, port: {type: "string", default: "0"}}}); -const port = Number(values.port); -if (!values.policy || !values.cert || !values.key || !Number.isSafeInteger(port) || port < 0 || port > 65535 || - !process.env.LOOPX_AUTHORITY_DATABASE_URL) throw new Error("Provide policy, TLS certificate/key and service-owned database configuration"); -// Validate before listening; updates are read again for each admission. -const policy = await readRemoteAuthorityPolicy(values.policy); -const pool = new Pool({connectionString: process.env.LOOPX_AUTHORITY_DATABASE_URL, - max: 8, connectionTimeoutMillis: 2000, query_timeout: 6000, idleTimeoutMillis: 10000, - statement_timeout: 5000, lock_timeout: 2000, idle_in_transaction_session_timeout: 10000}); -pool.on("error", () => process.stderr.write("Authority database connection unavailable\n")); -const service = new RemoteAuthorityService({admit: fileRemoteAuthorityAdmission(values.policy, String(policy.schema_version)), - database: {connect: async () => { - const connection = await pool.connect(); - return {query: (sql, args) => connection.query(sql, args ? [...args] : undefined), - release: error => connection.release(error)}; - }}}); -const listener = createRemoteAuthorityServer(service, {cert: await readFile(values.cert), key: await readFile(values.key)}); -try { - const actualPort = await listener.listen(values.host, port); - process.stdout.write(`${JSON.stringify({status: "listening", host: values.host, port: actualPort})}\n`); -} catch { - await pool.end(); - throw new Error("Authority service listener unavailable"); -} -let stopping = false; -async function stop() { - if (stopping) return; - stopping = true; - await listener.close(); - await pool.end(); -} -process.on("SIGINT", () => {void stop();}); -process.on("SIGTERM", () => {void stop();}); diff --git a/examples/github-actions-runtime-smoke.py b/examples/github-actions-runtime-smoke.py index 42dd427f09..ff2f78621f 100644 --- a/examples/github-actions-runtime-smoke.py +++ b/examples/github-actions-runtime-smoke.py @@ -70,10 +70,6 @@ def main() -> int: FORWARD_NODE_VERSION, SQLITE_NODE_VERSION, } - elif name == "postgresql-integration.yml": - # The authority ladder drives the same kernel runtime as the - # SQLite-dependent jobs, so it pins the one qualified 22.x build. - expected = {PRIMARY_NODE_VERSION, SQLITE_NODE_VERSION} else: expected = {PRIMARY_NODE_VERSION} assert set(versions) <= expected, (name, versions) @@ -90,10 +86,6 @@ def main() -> int: )) for name in ("typescript-core", "node-minimum-compatibility", "dashboard-acceptance", "windows-powershell"): assert SQLITE_NODE_VERSION in node_version_pattern.findall(jobs[name]), name - postgresql_versions = node_version_pattern.findall( - workflows["postgresql-integration.yml"] - ) - assert postgresql_versions == [SQLITE_NODE_VERSION], postgresql_versions assert "node-forward-compatibility:" in python_workflow assert "continue-on-error: true" in python_workflow assert "needs: [changes, checks, pytest, node-minimum-compatibility," in python_workflow diff --git a/examples/shared-goal-authority-e2e/README.md b/examples/shared-goal-authority-e2e/README.md index c6465f7128..0514376b08 100644 --- a/examples/shared-goal-authority-e2e/README.md +++ b/examples/shared-goal-authority-e2e/README.md @@ -28,7 +28,6 @@ The standalone ladder always runs every selected row. | `s0.native_sqlite_conformance` | 0 | `store_direct` | deterministic | The current `SqliteAuthorityStore` passes the same complete conformance contract against real SQLite files. | | `s1.cli_document_decodes_through_ts_store` | 1 | `real_cli` | deterministic | Public CLI bootstrap creates canonical File authority. Todo add, lease acquire, and Todo update commit at cursors 2 through 4. An independent TypeScript process reads the four ordered transactions and the first business receipt from `runtime/authority/file-v0`. | | `s2a.nokv_live_qualification` | 2a | `store_direct` | `env:nokv_authority` | The NoKV candidate passes the live single-node qualification with SDK `0.11.1`, API `1`, and both stale-incarnation fence checks. | -| `s2b.postgresql_conformance_live` | 2b | `store_direct` | `env:postgresql` | The PostgreSQL candidate passes the provider integration suite with at least nine TAP passes and no failures or skips. | Stage 1 checks the complete public path. It applies File creation defaults, bootstraps a new Goal, configures its registered agents, performs three @@ -85,7 +84,6 @@ qualified. | Gate | Required input | Unverified result | | --- | --- | --- | | `deterministic` | Node.js on `PATH` for native execution and readback | `node_missing` | -| `env:postgresql` | `LOOPX_TEST_POSTGRES_URL` and the installed `pg` package | `postgres_url_missing`, `pg_dependency_missing`, or `node_missing` | | `env:nokv_authority` | `LOOPX_NOKV_AUTHORITY_LIVE=1`, absolute config and Python paths, an existing workbench, and Node.js | A specific missing-input or disabled-live reason | The NoKV probe creates a fresh tenant and Goal in the selected workbench. diff --git a/loopx/cli.py b/loopx/cli.py index 9f039398bb..ac2802660e 100644 --- a/loopx/cli.py +++ b/loopx/cli.py @@ -123,7 +123,6 @@ register_canary_commands, register_coordination_shadow_command, register_authority_archive_command, - register_postgres_commands, register_capability_commands, register_doctor_command, register_dreaming_commands, @@ -326,7 +325,6 @@ def build_parser() -> LoopXArgumentParser: register_coordination_shadow_command(sub, add_subcommand_format) register_automation_cadence_command(sub, add_subcommand_format) register_authority_archive_command(sub, add_subcommand_format) - register_postgres_commands(sub, add_subcommand_format) register_task_lease_command(sub, add_subcommand_format) register_authority_shadow_command(sub, add_subcommand_format) register_todo_continuation(sub, add_subcommand_format) diff --git a/loopx/cli_commands/__init__.py b/loopx/cli_commands/__init__.py index 769bf2372f..c10f761175 100644 --- a/loopx/cli_commands/__init__.py +++ b/loopx/cli_commands/__init__.py @@ -46,7 +46,6 @@ def _load_exports() -> None: ) from .canary import handle_canary_command, register_canary_commands from .authority_archive import handle_authority_archive_command, register_authority_archive_command - from .postgres import handle_postgres_command, register_postgres_commands from .coordination_shadow import ( handle_coordination_shadow_command, register_coordination_shadow_command, @@ -165,7 +164,6 @@ def _load_exports() -> None: "handle_dash_command", "handle_project_lifecycle_command", "handle_project_command", - "handle_postgres_command", "handle_pr_review_command", "handle_deepresearch_command", "handle_quota_command", @@ -195,7 +193,6 @@ def _load_exports() -> None: "register_canary_commands", "register_coordination_shadow_command", "register_authority_archive_command", - "register_postgres_commands", "register_capability_commands", "register_extension_commands", "register_doctor_command", diff --git a/loopx/cli_commands/postgres.py b/loopx/cli_commands/postgres.py deleted file mode 100644 index ef8e64ea12..0000000000 --- a/loopx/cli_commands/postgres.py +++ /dev/null @@ -1,178 +0,0 @@ -"""CLI commands for PostgreSQL authority administration, schema/role setup, and genesis seed export.""" -from __future__ import annotations - -import argparse -import os -from collections.abc import Callable -from pathlib import Path - -from ..control_plane.effect_runtime import effect_runtime_result -from ..paths import resolve_runtime_root -from ..control_plane.projects.registry_codec import load_registry - - -def register_postgres_commands( - subparsers: argparse._SubParsersAction[argparse.ArgumentParser], - add_subcommand_format: Callable[[argparse.ArgumentParser], None], -) -> None: - parser = subparsers.add_parser( - "postgres", - help="PostgreSQL authority schema setup, restricted role assembly, and genesis seed export.", - ) - add_subcommand_format(parser) - actions = parser.add_subparsers(dest="postgres_action", required=True) - - # 1. setup: Admin PG Schema and restricted role assembly - setup_parser = actions.add_parser( - "setup", - help="Explicitly assemble PostgreSQL schema, tables, RLS policies, metadata and optional restricted role.", - ) - setup_parser.add_argument( - "--database-url", - default=os.environ.get("LOOPX_POSTGRES_URL", ""), - help="PostgreSQL connection string (or LOOPX_POSTGRES_URL).", - ) - setup_parser.add_argument( - "--store-identity", - help="Optional explicit store identity (postgresql:<32 lowercase hex>). Generated if omitted.", - ) - setup_parser.add_argument( - "--runtime-role", - help="Optional runtime role to provision within the schema transaction.", - ) - setup_parser.add_argument( - "--execute", - action="store_true", - help="Execute the schema and role setup against the database.", - ) - - # 2. local-to-pg-seed / seed: Read-only local-to-pg-seed genesis snapshot sanitizer and exporter - for seed_cmd in ("local-to-pg-seed", "seed"): - seed_parser = actions.add_parser( - seed_cmd, - help="Export a local goal into a sanitized cursor=1 genesis seed (stripping expired leases, paths, .local/, Feishu IDs) and optionally seed target PG.", - ) - seed_parser.add_argument("--goal-id", required=True, help="Goal ID to export.") - seed_parser.add_argument( - "--tenant-id", - default="default", - help="Target tenant ID for the PostgreSQL store (default: default).", - ) - seed_parser.add_argument( - "--database-url", - default=os.environ.get("LOOPX_POSTGRES_URL", ""), - help="Optional target PostgreSQL connection string for direct seeding.", - ) - seed_parser.add_argument( - "--store-identity", - help="Optionally pin the already installed target PostgreSQL store identity.", - ) - seed_parser.add_argument( - "--output", - type=Path, - help="Optional file to create or reuse if it contains the identical seed JSON.", - ) - seed_parser.add_argument( - "--execute", - action="store_true", - help="Execute the seed commit to target PostgreSQL; otherwise read-only preview/export.", - ) - - -def handle_postgres_command( - args: argparse.Namespace, - *, - registry_path: Path, - runtime_root_arg: str | None, - print_payload: Callable[[dict[str, object], str, Callable[[dict[str, object]], str]], None], - output_format: Callable[..., str], -) -> int | None: - if args.command != "postgres": - return None - - action = args.postgres_action - if action == "setup": - database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "") - if not database_url: - payload = { - "status": "failed", - "reason": "--database-url or LOOPX_POSTGRES_URL environment variable is required for setup", - } - print_payload(payload, output_format(args), lambda v: f"Postgres setup: failed\n{v.get('reason')}") - return 1 - - if not args.execute: - payload = { - "status": "dry_run", - "schema_version": "loopx_postgresql_authority_store_v1", - "database_url_configured": bool(database_url), - "store_identity": args.store_identity or "", - "runtime_role": args.runtime_role or None, - "note": "Pass --execute to actually install schema and assemble roles in PostgreSQL", - } - print_payload(payload, output_format(args), lambda v: f"Postgres setup (dry-run):\n{v}") - return 0 - - request = { - "databaseUrl": database_url, - "storeIdentity": args.store_identity, - "runtimeRole": args.runtime_role, - } - try: - result = effect_runtime_result( - "coordination.postgresql.setup", request, timeout=120.0, retry_safe=False - ) - except Exception as error: - result = {"status": "failed", "reason": str(error)} - - print_payload( - result, - output_format(args), - lambda v: ( - f"PostgreSQL setup: {v.get('status')}\n" - f"Schema version: {v.get('schema_version')}\n" - f"Store identity: {v.get('store_identity')}\n" - f"Runtime role: {v.get('runtime_role')}\n" - f"{v.get('reason', '')}" - ), - ) - return 0 if result.get("status") == "installed" else 1 - - elif action in ("local-to-pg-seed", "seed"): - registry = load_registry(registry_path) - resolved_root = str(resolve_runtime_root(registry, runtime_root_arg, registry_path=registry_path)) - - database_url = args.database_url or os.environ.get("LOOPX_POSTGRES_URL", "") - output_path = str(args.output.expanduser().absolute()) if args.output else None - - request = { - "runtimeRoot": resolved_root, - "goalId": args.goal_id, - "tenantId": args.tenant_id, - "outputPath": output_path, - "databaseUrl": database_url if args.execute else None, - "storeIdentity": args.store_identity, - "execute": args.execute, - } - try: - result = effect_runtime_result( - "coordination.postgresql.seed", request, timeout=180.0, retry_safe=False - ) - except Exception as error: - result = {"status": "failed", "reason": str(error)} - - print_payload( - result, - output_format(args), - lambda v: ( - f"PostgreSQL genesis seed: {v.get('status')}\n" - f"Goal: {v.get('goal_id')} (tenant: {v.get('tenant_id')})\n" - f"Cursor: {v.get('cursor')} (revision: {v.get('provider_revision')})\n" - f"Seed digest: {v.get('seed_digest')}\n" - f"Output: {v.get('output_path') or 'memory'}\n" - f"{v.get('reason', '')}" - ), - ) - return 0 if result.get("status") in ("exported", "seeded") else 1 - - return None diff --git a/loopx/cli_runtime.py b/loopx/cli_runtime.py index e70f2fb937..3831197205 100644 --- a/loopx/cli_runtime.py +++ b/loopx/cli_runtime.py @@ -48,7 +48,6 @@ "sync-global", "uninstall-project", "version", - "postgres", "host-mode-plan", "migrate-local-state", } @@ -64,7 +63,7 @@ "todo", "quota", "task-lease", "change-window", "delegation", "turn", "doctor", "commands", "version", "bootstrap", "connect", "configure-goal", "start-goal", "bootstrap-command-pack", "register-agent", "bind-agent-thread", - "authority-archive", "postgres", "coordination-shadow", "extension", "slash-commands", + "authority-archive", "coordination-shadow", "extension", "slash-commands", "manager-inbox", "agent-context", "agent-directory", "agent-capabilities", "handoff", "handoff-mode", @@ -494,10 +493,6 @@ def _build_selected_parser(command: str) -> LoopXArgumentParser: from .cli_commands.authority_archive import register_authority_archive_command register_authority_archive_command(subparsers, add_subcommand_format) - elif command == "postgres": - from .cli_commands.postgres import register_postgres_commands - - register_postgres_commands(subparsers, add_subcommand_format) elif command == "coordination-shadow": from .cli_commands.coordination_shadow import register_coordination_shadow_command @@ -702,13 +697,6 @@ def _dispatch_common_command( args, registry_path=registry_path, runtime_root_arg=args.runtime_root, output_format=output_format, print_payload=print_payload, ) - if args.command == "postgres": - from .cli_commands.postgres import handle_postgres_command - - return handle_postgres_command( - args, registry_path=registry_path, runtime_root_arg=args.runtime_root, - output_format=output_format, print_payload=print_payload, - ) if args.command == "coordination-shadow": from .cli_commands.coordination_shadow import handle_coordination_shadow_command diff --git a/loopx/control_plane/coordination/postgresql_authority_seed.ts b/loopx/control_plane/coordination/postgresql_authority_seed.ts deleted file mode 100644 index 19e5c5abb5..0000000000 --- a/loopx/control_plane/coordination/postgresql_authority_seed.ts +++ /dev/null @@ -1,643 +0,0 @@ -/** - * Administrative PostgreSQL schema setup, restricted role assembly, and - * local-to-pg-seed genesis snapshot sanitizer and exporter. - * - * Requirements: - * 1. Administrator-explicit PG schema and restricted role assembly. - * 2. Read-only local-to-pg-seed snapshot sanitization (cursor=1 clean seed): - * - Strips expired leases - * - Strips local absolute paths (/Users/..., /home/..., /private/...) - * - Strips .local/ paths and private material references - * - Strips Feishu/Lark user/chat/app/union IDs (ou_*, oc_*, cli_*, on_*) - * - Fail-closed if any privacy/credential pattern remains. - */ -import { randomBytes, randomUUID } from "node:crypto"; -import { constants } from "node:fs"; -import { link, lstat, open, unlink } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import type { JsonObject } from "../effect_program.ts"; -import { - canonicalAuthoritySha256, - isAuthorityJsonObject, - requireAuthorityStoreId, -} from "./authority_store_codec.ts"; -import { - POSTGRESQL_RUNTIME_ROLE_PATTERN, - POSTGRESQL_SCHEMA_VERSION, - POSTGRESQL_STORE_IDENTITY_PATTERN, - installPostgreSqlAuthorityStoreSchema, - PostgreSqlAuthorityStore, - type PostgreSqlAuthorityDatabase, -} from "./postgresql_authority_store.ts"; -import { syncAuthorityDirectory } from "./file_authority_store.ts"; -import { openLocalAuthorityStoreHandle } from "./local_authority_provider.ts"; - -export const PG_GENESIS_SEED_SCHEMA_VERSION = "loopx_pg_genesis_seed_v0"; - -// Privacy token / ID patterns -const FEISHU_ID_PATTERNS = [ - /(?]+/g, -]; - -const SECRET_PATTERNS = [ - /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36,255}\b/, - /\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\b/, - /\bsk-[a-zA-Z0-9_-]{20,}\b/, - /\bAKIA[0-9A-Z]{16}\b/, - /\bBearer\s+[A-Za-z0-9\-._~+/]+=*\b/i, - /\b(?:password|passwd|secret|api_key|access_token|private_key)\s*[:=]\s*["']?[^\s"']{8,}["']?/i, -]; - -export interface GenesisSanitizationReport { - leases_stripped: number; - private_materials_stripped: number; - paths_redacted: number; - feishu_ids_redacted: number; - todos_sanitized: number; -} - -export class GenesisPrivacyBoundaryViolation extends Error { - readonly reasonCode = "genesis_privacy_boundary_violation"; - constructor(message: string) { - super(message); - this.name = "GenesisPrivacyBoundaryViolation"; - } -} - -export function sanitizeText( - text: string, - report: GenesisSanitizationReport, -): string { - let result = text; - - // Redact Feishu IDs - for (const pattern of FEISHU_ID_PATTERNS) { - result = result.replace(pattern, () => { - report.feishu_ids_redacted += 1; - return "[feishu_id_redacted]"; - }); - } - - // Redact local absolute paths in text - for (const pattern of LOCAL_PATH_PATTERNS) { - result = result.replace(pattern, () => { - report.paths_redacted += 1; - return "[local_path_redacted]"; - }); - } - - // Remove .local/ references - if (isLocalReference(result)) { - result = result.replace(/\.local[\\/][^\s)"']*/g, () => { - report.paths_redacted += 1; - return "[local_scope_redacted]"; - }); - } - - return result; -} - -export function sanitizeObjectRecursively( - val: unknown, - report: GenesisSanitizationReport, -): unknown { - if (typeof val === "string") { - return sanitizeText(val, report); - } - if (Array.isArray(val)) { - return val.map((item) => sanitizeObjectRecursively(item, report)); - } - if (val !== null && typeof val === "object") { - return Object.fromEntries(Object.entries(val).map(([key, value]) => - [key, sanitizeObjectRecursively(value, report)])); - } - return val; -} - -function isLocalReference(value: string): boolean { - return /\.local[\\/]/.test(value); -} - -function matchesRedaction(pattern: RegExp, value: string): boolean { - // search starts at zero even for the global patterns used by replace. - return value.search(pattern) !== -1; -} - -export function scanForbiddenPrivacyTokens(value: unknown, path = "projection"): void { - if (typeof value === "string") { - for (const pattern of SECRET_PATTERNS) { - if (pattern.test(value)) { - throw new GenesisPrivacyBoundaryViolation( - `Privacy boundary violation at ${path}: matched sensitive credential pattern`, - ); - } - } - if (isLocalReference(value)) { - throw new GenesisPrivacyBoundaryViolation( - `Privacy boundary violation at ${path}: contains raw unredacted .local/ reference`, - ); - } - for (const pattern of LOCAL_PATH_PATTERNS) { - if (matchesRedaction(pattern, value)) { - throw new GenesisPrivacyBoundaryViolation( - `Privacy boundary violation at ${path}: contains raw unredacted local absolute path`, - ); - } - } - for (const pattern of FEISHU_ID_PATTERNS) { - if (matchesRedaction(pattern, value)) { - throw new GenesisPrivacyBoundaryViolation( - `Privacy boundary violation at ${path}: contains raw unredacted Feishu ID`, - ); - } - } - return; - } - - if (Array.isArray(value)) { - for (let i = 0; i < value.length; i++) { - scanForbiddenPrivacyTokens(value[i], `${path}[${i}]`); - } - return; - } - - if (value !== null && typeof value === "object") { - for (const [index, [key, field]] of Object.entries(value).entries()) { - // A field name can itself be a private ID or secret. Never echo it. - const location = path + "{" + index + "}"; - scanForbiddenPrivacyTokens(key, location); - scanForbiddenPrivacyTokens(field, location); - } - } -} - -/** - * Sanitizes a projection for genesis PG export. - * Resets metadata, strips expired leases, sanitizes paths and Feishu IDs, - * strips private materials, and runs a fail-closed boundary scan. - */ -export function sanitizeGenesisSeedProjection(rawProjection: JsonObject): { - projection: JsonObject; - report: GenesisSanitizationReport; -} { - const report: GenesisSanitizationReport = { - leases_stripped: 0, - private_materials_stripped: 0, - paths_redacted: 0, - feishu_ids_redacted: 0, - todos_sanitized: 0, - }; - - const projection = structuredClone(rawProjection); - - // 1. Strip expired leases (support both leases array and task_leases object/array) - const nowMs = Date.now(); - if (Array.isArray(projection.leases)) { - const originalLeaseCount = projection.leases.length; - const retainedLeases = (projection.leases as JsonObject[]).filter((lease) => { - if (!lease || typeof lease !== "object") return false; - const expiresAt = lease.expires_at; - if (typeof expiresAt === "number" && expiresAt <= nowMs) return false; - if (typeof expiresAt === "string") { - const parsed = Date.parse(expiresAt); - if (!isNaN(parsed) && parsed <= nowMs) return false; - } - return true; - }); - projection.leases = retainedLeases; - report.leases_stripped += originalLeaseCount - retainedLeases.length; - } - if (projection.task_leases && typeof projection.task_leases === "object") { - const taskLeases = projection.task_leases as JsonObject; - if (taskLeases.leases && typeof taskLeases.leases === "object" && !Array.isArray(taskLeases.leases)) { - const remaining: Record = Object.create(null); - for (const [k, v] of Object.entries(taskLeases.leases as Record)) { - if (!v || typeof v !== "object") continue; - const expiresAt = v.expires_at; - if (typeof expiresAt === "number" && expiresAt <= nowMs) { - report.leases_stripped += 1; - continue; - } - if (typeof expiresAt === "string") { - const parsed = Date.parse(expiresAt); - if (!isNaN(parsed) && parsed <= nowMs) { - report.leases_stripped += 1; - continue; - } - } - remaining[k] = v; - } - taskLeases.leases = remaining; - } - } - - // 2. Strip private materials (support project_materials and registered_materials) - for (const matKey of ["project_materials", "registered_materials"] as const) { - if (Array.isArray(projection[matKey])) { - const originalMaterialCount = (projection[matKey] as JsonObject[]).length; - projection[matKey] = ( - projection[matKey] as JsonObject[] - ).filter((material) => { - if (!material || typeof material !== "object") return false; - return !privateMaterial(material); - }); - report.private_materials_stripped += - originalMaterialCount - (projection[matKey] as JsonObject[]).length; - } - } - - // The direction registry, scope digest and usage receipts are one lineage. - // A redacted registry cannot retain source attestations or be re-signed as - // original evidence in a synthetic seed. - const registry = projection.authority_registry; - if (isAuthorityJsonObject(registry) && isAuthorityJsonObject(registry.project_materials)) { - const materials = Object.values(registry.project_materials); - const removed = materials.filter(row => isAuthorityJsonObject(row) && privateMaterial(row)).length; - const sanitizedRegistry = sanitizeObjectRecursively(registry, {...report}); - if (removed > 0 || canonicalAuthoritySha256(sanitizedRegistry) !== canonicalAuthoritySha256(registry)) { - report.private_materials_stripped += removed; - delete projection.authority_registry; - delete projection.goal_direction_scope; - delete projection.material_usage_receipts; - } - } - - // 3. Sanitize Todos (required_write_scopes, text, title, note, evidence) - if (Array.isArray(projection.todos)) { - for (const todo of projection.todos as JsonObject[]) { - if (!todo || typeof todo !== "object") continue; - report.todos_sanitized += 1; - - // Sanitize required_write_scopes: strip .local/ and make paths relative - if (Array.isArray(todo.required_write_scopes)) { - todo.required_write_scopes = ( - todo.required_write_scopes as string[] - ) - .filter((scope) => typeof scope === "string" && !isLocalReference(scope)) - .map((scope) => { - // Strip machine-specific user prefix - for (const pattern of LOCAL_PATH_PATTERNS) { - if (matchesRedaction(pattern, scope)) { - report.paths_redacted += 1; - // Convert e.g. /Users/.../loopx/packages/foo -> packages/foo - const parts = scope.split("/"); - const loopxIdx = parts.indexOf("loopx"); - if (loopxIdx >= 0 && loopxIdx < parts.length - 1) { - return parts.slice(loopxIdx + 1).join("/"); - } - return parts[parts.length - 1] ?? ""; - } - } - return scope; - }); - } - } - } - - // 4. Sanitize topic_authority and direction - if (Array.isArray(projection.topic_authority)) { - projection.topic_authority = ( - projection.topic_authority as JsonObject[] - ).filter((topic) => { - if (!topic || typeof topic !== "object") return false; - const boundary = String(topic.boundary ?? ""); - return boundary !== "local_private" && boundary !== "private"; - }); - } - - // 5. Recursively sanitize all strings (Feishu IDs, local paths, .local references) - const sanitized = sanitizeObjectRecursively(projection, report) as JsonObject; - - // 6. Fail-closed scan: any remaining credential or sensitive token triggers immediate failure - scanForbiddenPrivacyTokens(sanitized, "projection"); - - return { projection: sanitized, report }; -} - -function privateMaterial(material: JsonObject): boolean { - const boundary = String(material.boundary ?? material.visibility ?? ""); - return ["local_private", "private", "restricted"].includes(boundary) || - isLocalReference(String(material.source_url ?? material.url ?? "")) || - isLocalReference(String(material.source_path ?? material.path ?? "")); -} - -export interface PostgreSqlAdminSetupOptions { - database?: PostgreSqlAuthorityDatabase; - databaseUrl?: string; - storeIdentity?: string; - runtimeRole?: string; -} - -export interface PostgreSqlAdminSetupResult { - status: "installed" | "failed"; - schema_version: string; - store_identity: string; - runtime_role: string | null; - reason?: string; -} - -async function resolveDatabase(options: { - database?: PostgreSqlAuthorityDatabase; - targetDatabase?: PostgreSqlAuthorityDatabase; - databaseUrl?: string; -}): Promise<{ database: PostgreSqlAuthorityDatabase; close: () => Promise } | null> { - const db = options.database ?? options.targetDatabase; - if (db) { - return { database: db, close: async () => {} }; - } - if (options.databaseUrl) { - const { Pool } = await import("pg"); - const pool = new Pool({ connectionString: options.databaseUrl }); - const database: PostgreSqlAuthorityDatabase = { - connect: async () => { - const client = await pool.connect(); - return { - query: async (text: string, values?: readonly unknown[]) => - await client.query(text, values ? [...values] : undefined), - release: (error?: Error) => client.release(error), - }; - }, - }; - return { database, close: async () => await pool.end() }; - } - return null; -} - -/** - * Administrative explicit schema and restricted role assembly. - */ -export async function setupPostgreSqlAuthoritySchemaAndRole( - databaseOrOptions: PostgreSqlAuthorityDatabase | PostgreSqlAdminSetupOptions, - maybeOptions?: PostgreSqlAdminSetupOptions, -): Promise { - const options: PostgreSqlAdminSetupOptions = - databaseOrOptions && "connect" in databaseOrOptions - ? { ...maybeOptions, database: databaseOrOptions as PostgreSqlAuthorityDatabase } - : (databaseOrOptions as PostgreSqlAdminSetupOptions); - - const storeIdentity = - options.storeIdentity ?? - `postgresql:${randomBytes(16).toString("hex").toLowerCase()}`; - - if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) { - return { - status: "failed", - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - runtime_role: options.runtimeRole ?? null, - reason: "PostgreSQL store identity must match postgresql:<32 lowercase hex>", - }; - } - - const role = options.runtimeRole?.trim(); - if (role !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(role)) { - return { - status: "failed", - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - runtime_role: null, - reason: "runtime role must be a valid lowercase SQL identifier", - }; - } - - const resolved = await resolveDatabase(options); - if (!resolved) { - return { - status: "failed", - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - runtime_role: options.runtimeRole ?? null, - reason: "PostgreSQL database or databaseUrl must be provided for schema setup", - }; - } - - try { - await installPostgreSqlAuthorityStoreSchema(resolved.database, storeIdentity, role); - - return { - status: "installed", - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - runtime_role: role ?? null, - }; - } catch (error) { - return { - status: "failed", - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - runtime_role: options.runtimeRole ?? null, - reason: error instanceof Error ? error.message : String(error), - }; - } finally { - await resolved.close(); - } -} - -export interface LocalToPgSeedExportOptions { - runtimeRoot: string; - goalId: string; - tenantId?: string; - outputPath?: string; - localStore?: import("./authority_store.ts").AuthorityStore; - targetDatabase?: PostgreSqlAuthorityDatabase; - databaseUrl?: string; - storeIdentity?: string; - execute?: boolean; -} - -export interface LocalToPgSeedExportResult { - status: "exported" | "seeded" | "failed"; - schema_version: string; - goal_id: string; - tenant_id: string; - cursor: string; - provider_revision: string; - operation_id: string; - seed_digest?: string; - sanitization_report?: GenesisSanitizationReport; - output_path?: string; - reason?: string; - seed_payload?: JsonObject; -} - -class GenesisSeedOutputConflict extends Error { - constructor() { - super("Genesis seed output must be absent or an identical regular file"); - } -} - -async function existingSeedMatches(path: string, expected: Buffer): Promise { - let declared; - try { - declared = await lstat(path); - } catch (error) { - if ((error as NodeJS.ErrnoException).code === "ENOENT") return false; - throw error; - } - if (!declared.isFile() || declared.size !== expected.length) throw new GenesisSeedOutputConflict(); - const file = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0) | (constants.O_NOFOLLOW ?? 0)); - try { - const stat = await file.stat(); - if (!stat.isFile() || stat.dev !== declared.dev || stat.ino !== declared.ino) throw new GenesisSeedOutputConflict(); - let offset = 0; - for await (const part of file.createReadStream({autoClose: false})) { - if (!part.equals(expected.subarray(offset, offset + part.length))) { - throw new GenesisSeedOutputConflict(); - } - offset += part.length; - } - if (offset !== expected.length) throw new GenesisSeedOutputConflict(); - return true; - } finally { - await file.close(); - } -} - -async function writeSeedOutput(path: string, bytes: Buffer): Promise { - const directory = dirname(path); - if (!await existingSeedMatches(path, bytes)) { - // Publish a complete private file without replacing a concurrent writer's - // output. A concurrent replay may reuse the winner's identical bytes. - const temporary = join(directory, `.loopx-seed-${randomUUID()}.partial`); - const file = await open(temporary, "wx", 0o600); - try { - try { - await file.writeFile(bytes); - await file.sync(); - } finally { - await file.close(); - } - try { - await link(temporary, path); - } catch (error) { - if ((error as NodeJS.ErrnoException).code !== "EEXIST" || - !await existingSeedMatches(path, bytes)) throw error; - } - } finally { - await unlink(temporary); - } - } - await syncAuthorityDirectory(directory); -} - -/** - * Exports a local authority store into a sanitized genesis seed (cursor=1). - * When targetDatabase and execute=true are provided, atomically commits into PG. - */ -export async function exportLocalToPgGenesisSeed( - options: LocalToPgSeedExportOptions, -): Promise { - const tenantId = options.tenantId ?? "default"; - const operationId = `${options.goalId}-genesis-seed`; - const base = { - schema_version: PG_GENESIS_SEED_SCHEMA_VERSION, - goal_id: options.goalId, - tenant_id: tenantId, - cursor: "1", - provider_revision: "1", - operation_id: operationId, - }; - let scopeValid = false; - const failed = (reason: string): LocalToPgSeedExportResult => - ({...base, ...(!scopeValid ? {goal_id: "", tenant_id: "", operation_id: ""} : {}), status: "failed", reason}); - - try { - for (const value of [options.goalId, tenantId, operationId]) { - requireAuthorityStoreId(value, "genesis scope id"); - scanForbiddenPrivacyTokens(value, "genesis scope"); - } - scopeValid = true; - if (options.execute && !options.targetDatabase && !options.databaseUrl) { - return failed("Executing a genesis seed requires a target PostgreSQL database"); - } - if (options.storeIdentity !== undefined && - !POSTGRESQL_STORE_IDENTITY_PATTERN.test(options.storeIdentity)) { - return failed("Target PostgreSQL store identity is invalid"); - } - - const store = options.localStore ?? - (await openLocalAuthorityStoreHandle(options.runtimeRoot, options.goalId, {}, {existingOnly: true})).store; - if (store.providerKind === "postgresql") return failed("Genesis export requires a local File or SQLite source"); - const loaded = await store.loadAuthority(); - if (loaded.status !== "loaded") return failed("Local authority head is not available"); - - const {projection, report} = sanitizeGenesisSeedProjection(loaded.head); - const seedDigest = canonicalAuthoritySha256(projection); - const seedPayload: JsonObject = { - ...base, seed_digest: seedDigest, sanitization_report: {...report}, projection, - }; - const exported = { - ...base, seed_digest: seedDigest, sanitization_report: report, - output_path: options.outputPath, seed_payload: seedPayload, - }; - if (options.outputPath) { - await writeSeedOutput(options.outputPath, Buffer.from(JSON.stringify(seedPayload, null, 2))); - } - // Export never opens the target or installs anything in either store. - if (!options.execute) return {...exported, status: "exported"}; - - const resolved = await resolveDatabase(options); - if (!resolved) return failed("Target PostgreSQL database is not available for execution"); - try { - // Discover the provisioned incarnation if the caller did not pin one. - // Subsequent writes check this identity inside the commit's rotation lock. - const scope = {tenant_id: tenantId, goal_id: options.goalId}; - const identity = await new PostgreSqlAuthorityStore(resolved.database, { - ...scope, expected_store_identity: options.storeIdentity, - }).storeIdentity(); - if (identity.status !== "available") return failed("Target PostgreSQL store identity is unavailable or mismatched"); - const pgStore = new PostgreSqlAuthorityStore(resolved.database, { - ...scope, expected_store_identity: identity.store_identity, - }); - - const original = async () => { - const receipt = await pgStore.readReceipt(operationId); - if (receipt.status !== "found" || receipt.cursor !== "1" || receipt.receipts.length !== 1) return null; - const record = receipt.receipts[0]; - return record.schema_version === "genesis_seed_receipt_v0" && - record.source_goal_id === options.goalId && record.seed_digest === seedDigest ? receipt : null; - }; - const existing = await pgStore.loadAuthority(); - if (existing.status === "loaded") { - const receipt = await original(); - return receipt ? {...exported, status: "seeded", provider_revision: receipt.provider_revision} - : failed("Target PostgreSQL goal already has a different committed authority"); - } - if (existing.status !== "missing") return failed("Target PostgreSQL authority state is unavailable"); - - const committed = await pgStore.commitAuthority({ - operation_id: operationId, - expected_provider_revision: null, - events: [], - receipts: [{ - schema_version: "genesis_seed_receipt_v0", - source: "local-to-pg-seed", - source_goal_id: options.goalId, - seed_digest: seedDigest, - sanitization_report: {...report}, - }], - next_projection: projection, - }); - if (committed.status === "applied") { - return {...exported, status: "seeded", provider_revision: committed.provider_revision}; - } - if (committed.status === "ambiguous" || committed.status === "conflict") { - const receipt = await original(); - if (receipt) return {...exported, status: "seeded", provider_revision: receipt.provider_revision}; - if (committed.status === "ambiguous") { - return failed("PostgreSQL seed outcome is unknown; retry the same seed to resolve the original receipt"); - } - } - return failed(`PostgreSQL genesis seed was not applied (${committed.status})`); - } finally { - await resolved.close(); - } - } catch (error) { - return failed(error instanceof GenesisPrivacyBoundaryViolation || error instanceof GenesisSeedOutputConflict - ? error.message : "Genesis seed could not be prepared or written"); - } -} diff --git a/loopx/control_plane/coordination/postgresql_authority_service.ts b/loopx/control_plane/coordination/postgresql_authority_service.ts deleted file mode 100644 index 43305b8cb5..0000000000 --- a/loopx/control_plane/coordination/postgresql_authority_service.ts +++ /dev/null @@ -1,212 +0,0 @@ -import type { AuthorityStore } from "./authority_store.ts"; -import { - POSTGRESQL_STORE_IDENTITY_PATTERN, - PostgreSqlAuthorityStore, - type PostgreSqlAuthorityDatabase, - type PostgreSqlAuthorityStoreOptions, -} from "./postgresql_authority_store.ts"; -import { requireAuthorityStoreId } from "./authority_store_codec.ts"; - -/** A principal already verified by the service's authentication layer. */ -export interface PostgreSqlAuthenticatedPrincipal { - principal_id: string; -} - -export type PostgreSqlPrincipalAuthenticationResult = - | { status: "authenticated"; principal: PostgreSqlAuthenticatedPrincipal } - | { status: "rejected"; reason_code: string; reason: string }; - -export type PostgreSqlTenantAuthorizationResult = - | { status: "allowed" } - | { status: "denied"; reason_code: string; reason: string }; - -/** - * The service owns authentication and authorization. The provider only sees - * an already verified principal id and the authorized tenant binding. - */ -export interface PostgreSqlAuthorityServiceDependencies { - database: PostgreSqlAuthorityDatabase; - authenticatePrincipal( - credential: unknown, - ): Promise | PostgreSqlPrincipalAuthenticationResult; - authorizeTenant( - principalId: string, - tenantId: string, - ): Promise | PostgreSqlTenantAuthorizationResult; - max_commit_bytes?: PostgreSqlAuthorityStoreOptions["max_commit_bytes"]; -} - -export interface PostgreSqlAuthorityServiceOpenRequest { - /** Opaque transport credential; never persisted or passed to PostgreSQL. */ - credential: unknown; - tenant_id: string; - goal_id: string; - store_identity: string; -} - -export type PostgreSqlAuthorityServiceOpenResult = - | { - status: "opened"; - provider: "postgresql"; - principal_id: string; - tenant_id: string; - goal_id: string; - store_identity: string; - store: AuthorityStore; - } - | { - status: "rejected"; - reason_code: - | "invalid_service_request" - | "principal_unauthenticated" - | "principal_verification_unavailable" - | "tenant_unauthorized" - | "tenant_authorization_unavailable" - | "store_identity_unavailable" - | "store_identity_mismatch"; - reason: string; - principal_id?: string; - tenant_id?: string; - goal_id?: string; - }; - -type PostgreSqlAuthorityServiceRejected = Extract< - PostgreSqlAuthorityServiceOpenResult, - {status: "rejected"} ->; - -function rejected( - reasonCode: PostgreSqlAuthorityServiceRejected["reason_code"], - reason: string, - facts: Partial = {}, -): PostgreSqlAuthorityServiceRejected { - return {status: "rejected", reason_code: reasonCode, reason, ...facts}; -} - -function validIdentity(value: string): boolean { - return POSTGRESQL_STORE_IDENTITY_PATTERN.test(value); -} - -/** - * Service-owned PostgreSQL admission. This is intentionally an in-process - * boundary: transport authentication, pool lifecycle, and credentials stay - * outside the provider-neutral AuthorityStore contract. - */ -export class PostgreSqlAuthorityService { - readonly #dependencies: PostgreSqlAuthorityServiceDependencies; - - constructor(dependencies: PostgreSqlAuthorityServiceDependencies) { - this.#dependencies = dependencies; - } - - async openStore( - request: PostgreSqlAuthorityServiceOpenRequest, - ): Promise { - let tenantId: string; - let goalId: string; - if ( - typeof request !== "object" || request === null || - typeof request.tenant_id !== "string" || - typeof request.goal_id !== "string" || - typeof request.store_identity !== "string" - ) { - return rejected("invalid_service_request", "PostgreSQL service request is invalid"); - } - try { - tenantId = requireAuthorityStoreId(request.tenant_id, "tenant id"); - goalId = requireAuthorityStoreId(request.goal_id, "goal id"); - } catch (error) { - return rejected( - "invalid_service_request", - error instanceof Error ? error.message : "PostgreSQL service request is invalid", - ); - } - if (!validIdentity(request.store_identity)) { - return rejected( - "invalid_service_request", - "PostgreSQL store identity must match postgresql:<32 lowercase hex>", - {tenant_id: tenantId, goal_id: goalId}, - ); - } - - let authentication: PostgreSqlPrincipalAuthenticationResult; - try { - authentication = await this.#dependencies.authenticatePrincipal(request.credential); - } catch { - return rejected( - "principal_verification_unavailable", - "PostgreSQL service could not verify the principal", - {tenant_id: tenantId, goal_id: goalId}, - ); - } - if (authentication.status !== "authenticated") { - return rejected( - "principal_unauthenticated", - "PostgreSQL service principal authentication was rejected", - {tenant_id: tenantId, goal_id: goalId}, - ); - } - - let principalId: string; - try { - principalId = requireAuthorityStoreId( - authentication.principal.principal_id, - "principal id", - ); - } catch { - return rejected( - "principal_unauthenticated", - "PostgreSQL service returned an invalid authenticated principal", - {tenant_id: tenantId, goal_id: goalId}, - ); - } - - let tenantDecision: PostgreSqlTenantAuthorizationResult; - try { - tenantDecision = await this.#dependencies.authorizeTenant(principalId, tenantId); - } catch { - return rejected( - "tenant_authorization_unavailable", - "PostgreSQL service could not authorize the tenant", - {principal_id: principalId, tenant_id: tenantId, goal_id: goalId}, - ); - } - if (tenantDecision.status !== "allowed") { - return rejected( - "tenant_unauthorized", - "PostgreSQL principal is not authorized for the requested tenant", - {principal_id: principalId, tenant_id: tenantId, goal_id: goalId}, - ); - } - - const store = new PostgreSqlAuthorityStore(this.#dependencies.database, { - tenant_id: tenantId, - goal_id: goalId, - max_commit_bytes: this.#dependencies.max_commit_bytes, - }); - const identity = await store.storeIdentity(); - if (identity.status !== "available") { - return rejected( - "store_identity_unavailable", - "PostgreSQL service could not verify the database incarnation", - {principal_id: principalId, tenant_id: tenantId, goal_id: goalId}, - ); - } - if (identity.store_identity !== request.store_identity) { - return rejected( - "store_identity_mismatch", - "PostgreSQL database incarnation does not match the requested binding", - {principal_id: principalId, tenant_id: tenantId, goal_id: goalId}, - ); - } - return { - status: "opened", - provider: "postgresql", - principal_id: principalId, - tenant_id: tenantId, - goal_id: goalId, - store_identity: identity.store_identity, - store, - }; - } -} diff --git a/loopx/control_plane/coordination/postgresql_authority_store.ts b/loopx/control_plane/coordination/postgresql_authority_store.ts deleted file mode 100644 index d0c7146f79..0000000000 --- a/loopx/control_plane/coordination/postgresql_authority_store.ts +++ /dev/null @@ -1,1148 +0,0 @@ -import {AuthorityJournalScan} from "./authority_journal_scan.ts"; -import type { JsonObject } from "../effect_program.ts"; -import { - authorityStateCheckpointCursor, - authorityStateDelta, - isAuthorityStateCheckpoint, - AuthorityStateReplay, -} from "./authority_state_log.ts"; -import type { - AuthorityStore, - AuthorityStoreCommit, - AuthorityStoreCommitResult, - AuthorityStoreCommittedTransaction, - AuthorityStoreIdentityResult, - AuthorityStoreLoadResult, - AuthorityStoreReadFailure, - AuthorityStoreReceiptBatchResult, - AuthorityStoreReceiptResult, - AuthorityStoreReceiptSnapshotResult, - AuthorityStoreScanResult, -} from "./authority_store.ts"; -import { - AuthorityStoreProtocolError, - canonicalAuthorityBytes, - canonicalAuthorityObject, - canonicalAuthorityObjectList, - canonicalAuthoritySha256, - normalizeAuthorityStoreCommit, - parseAuthorityCursor, - requireAuthorityStoreId, -} from "./authority_store_codec.ts"; - -export const POSTGRESQL_STORE_IDENTITY_PATTERN = /^postgresql:[0-9a-f]{32}$/; -const POSTGRESQL_PROVIDER_REVISION_PATTERN = /^postgresql:([0-9a-f]{32}):([1-9]\d*)$/; -const POSTGRESQL_STORE_IDENTITY_LOCK_KEY = "5522768808912313929"; -const FULL_PROJECTION_SCHEMA_VERSION = "loopx_postgresql_authority_store_v0"; -export const POSTGRESQL_SCHEMA_VERSION = "loopx_postgresql_authority_store_v1"; -export const POSTGRESQL_RUNTIME_ROLE_PATTERN = /^[a-z][a-z0-9_]{1,62}$/; -export const DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES = 16 * 1024 * 1024; - -/** - * Structural subset of a server-owned PostgreSQL driver connection. - * A node-postgres PoolClient can be wrapped without making `pg` a LoopX-core - * dependency. The authenticated service owns the pool; Agents never receive it. - */ -export interface PostgreSqlAuthorityConnection { - query(text: string, values?: readonly unknown[]): Promise<{ - rows: readonly unknown[]; - rowCount: number | null; - }>; - /** A supplied error marks unknown session state and must evict the connection. */ - release(error?: Error): void | Promise; -} - -export interface PostgreSqlAuthorityDatabase { - connect(): Promise; -} - -export interface PostgreSqlAuthorityStoreOptions { - tenant_id: string; - goal_id: string; - /** Bind an explicit target even when the expected Goal revision is empty. */ - expected_store_identity?: string; - /** - * Maximum canonical bytes admitted for one atomic commit. Deployments may - * lower this envelope; promotion still requires separately measured - * retention and sustained-capacity evidence. - */ - max_commit_bytes?: number; -} - -interface HeadRow extends JsonObject { - provider_revision: string; - cursor: string; - head: JsonObject | null; -} - -export const POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL = ` -CREATE SCHEMA IF NOT EXISTS loopx_control_plane; - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_store_metadata ( - singleton boolean PRIMARY KEY DEFAULT TRUE CHECK (singleton), - schema_version text NOT NULL, - store_identity text NOT NULL UNIQUE, - created_at timestamptz NOT NULL DEFAULT transaction_timestamp() -); - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_heads ( - tenant_id text NOT NULL, - goal_id text NOT NULL, - provider_revision bigint NOT NULL DEFAULT 0 CHECK (provider_revision >= 0), - cursor bigint NOT NULL DEFAULT 0 CHECK (cursor >= 0), - head jsonb, - updated_at timestamptz NOT NULL DEFAULT transaction_timestamp(), - PRIMARY KEY (tenant_id, goal_id), - CHECK ((cursor = 0 AND provider_revision = 0 AND head IS NULL) OR - (cursor > 0 AND provider_revision > 0 AND head IS NOT NULL)) -); - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints ( - tenant_id text NOT NULL, - goal_id text NOT NULL, - cursor bigint NOT NULL CHECK (cursor > 0), - projection jsonb NOT NULL, - projection_digest text NOT NULL CHECK (length(projection_digest) = 64), - created_at timestamptz NOT NULL DEFAULT transaction_timestamp(), - PRIMARY KEY (tenant_id, goal_id, cursor), - FOREIGN KEY (tenant_id, goal_id) - REFERENCES loopx_control_plane.authority_heads (tenant_id, goal_id) - ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits ( - tenant_id text NOT NULL, - goal_id text NOT NULL, - cursor bigint NOT NULL CHECK (cursor > 0), - provider_revision bigint NOT NULL CHECK (provider_revision > 0), - operation_id text NOT NULL, - delta jsonb, - projection jsonb, - projection_digest text, - committed_at timestamptz NOT NULL DEFAULT transaction_timestamp(), - PRIMARY KEY (tenant_id, goal_id, cursor), - UNIQUE (tenant_id, goal_id, operation_id), - FOREIGN KEY (tenant_id, goal_id) - REFERENCES loopx_control_plane.authority_heads (tenant_id, goal_id) - ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_events ( - tenant_id text NOT NULL, - goal_id text NOT NULL, - cursor bigint NOT NULL, - event_index integer NOT NULL CHECK (event_index >= 0), - event jsonb NOT NULL, - PRIMARY KEY (tenant_id, goal_id, cursor, event_index), - FOREIGN KEY (tenant_id, goal_id, cursor) - REFERENCES loopx_control_plane.authority_commits (tenant_id, goal_id, cursor) - ON DELETE CASCADE -); - -CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_receipts ( - tenant_id text NOT NULL, - goal_id text NOT NULL, - cursor bigint NOT NULL, - receipt_index integer NOT NULL CHECK (receipt_index >= 0), - receipt jsonb NOT NULL, - PRIMARY KEY (tenant_id, goal_id, cursor, receipt_index), - FOREIGN KEY (tenant_id, goal_id, cursor) - REFERENCES loopx_control_plane.authority_commits (tenant_id, goal_id, cursor) - ON DELETE CASCADE -); - -ALTER TABLE loopx_control_plane.authority_heads ENABLE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_heads FORCE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_checkpoints ENABLE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_checkpoints FORCE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_commits ENABLE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_commits FORCE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_events ENABLE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_events FORCE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_receipts ENABLE ROW LEVEL SECURITY; -ALTER TABLE loopx_control_plane.authority_receipts FORCE ROW LEVEL SECURITY; - -DROP POLICY IF EXISTS authority_heads_tenant_scope - ON loopx_control_plane.authority_heads; -CREATE POLICY authority_heads_tenant_scope - ON loopx_control_plane.authority_heads - USING (tenant_id = current_setting('loopx.tenant_id', TRUE)) - WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE)); - -DROP POLICY IF EXISTS authority_checkpoints_tenant_scope - ON loopx_control_plane.authority_checkpoints; -CREATE POLICY authority_checkpoints_tenant_scope - ON loopx_control_plane.authority_checkpoints - USING (tenant_id = current_setting('loopx.tenant_id', TRUE)) - WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE)); - -DROP POLICY IF EXISTS authority_commits_tenant_scope - ON loopx_control_plane.authority_commits; -CREATE POLICY authority_commits_tenant_scope - ON loopx_control_plane.authority_commits - USING (tenant_id = current_setting('loopx.tenant_id', TRUE)) - WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE)); - -DROP POLICY IF EXISTS authority_events_tenant_scope - ON loopx_control_plane.authority_events; -CREATE POLICY authority_events_tenant_scope - ON loopx_control_plane.authority_events - USING (tenant_id = current_setting('loopx.tenant_id', TRUE)) - WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE)); - -DROP POLICY IF EXISTS authority_receipts_tenant_scope - ON loopx_control_plane.authority_receipts; -CREATE POLICY authority_receipts_tenant_scope - ON loopx_control_plane.authority_receipts - USING (tenant_id = current_setting('loopx.tenant_id', TRUE)) - WITH CHECK (tenant_id = current_setting('loopx.tenant_id', TRUE)); -`; - -const SELECT_HEAD_SQL = ` -SELECT provider_revision::text AS provider_revision, - cursor::text AS cursor, - head -FROM loopx_control_plane.authority_heads -WHERE tenant_id = $1 AND goal_id = $2 -`; - -const SELECT_TRANSACTION_COLUMNS_SQL = ` -SELECT commit.cursor::text AS cursor, - commit.provider_revision::text AS provider_revision, - commit.operation_id, - commit.delta, - commit.projection, - commit.projection_digest, - checkpoint.projection AS checkpoint_projection, - checkpoint.projection_digest AS checkpoint_digest, - COALESCE(( - SELECT jsonb_agg(event.event ORDER BY event.event_index) - FROM loopx_control_plane.authority_events AS event - WHERE event.tenant_id = commit.tenant_id - AND event.goal_id = commit.goal_id - AND event.cursor = commit.cursor - ), '[]'::jsonb) AS events, - COALESCE(( - SELECT jsonb_agg(receipt.receipt ORDER BY receipt.receipt_index) - FROM loopx_control_plane.authority_receipts AS receipt - WHERE receipt.tenant_id = commit.tenant_id - AND receipt.goal_id = commit.goal_id - AND receipt.cursor = commit.cursor - ), '[]'::jsonb) AS receipts -FROM loopx_control_plane.authority_commits AS commit -LEFT JOIN loopx_control_plane.authority_checkpoints AS checkpoint - ON checkpoint.tenant_id = commit.tenant_id AND checkpoint.goal_id = commit.goal_id - AND checkpoint.cursor = commit.cursor -`; - -const SELECT_RECEIPT_COLUMNS_SQL = ` -SELECT commit.cursor::text AS cursor, - commit.provider_revision::text AS provider_revision, - commit.operation_id, - COALESCE(( - SELECT jsonb_agg(receipt.receipt ORDER BY receipt.receipt_index) - FROM loopx_control_plane.authority_receipts AS receipt - WHERE receipt.tenant_id = commit.tenant_id - AND receipt.goal_id = commit.goal_id - AND receipt.cursor = commit.cursor - ), '[]'::jsonb) AS receipts -FROM loopx_control_plane.authority_commits AS commit -`; - -function rows(result: { rows: readonly unknown[] }): readonly JsonObject[] { - return result.rows.map((row) => canonicalAuthorityObject(row, "PostgreSQL row")); -} - -function oneRow( - result: { rows: readonly unknown[] }, - name: string, -): JsonObject | null { - const values = rows(result); - if (values.length > 1) { - throw new AuthorityStoreProtocolError(`${name} returned more than one row`); - } - return values[0] ?? null; -} - -function requiredRowString(row: JsonObject, field: string): string { - return requireAuthorityStoreId(row[field], field); -} - -function decodeHeadRow(value: JsonObject): HeadRow { - const providerRevision = requiredRowString(value, "provider_revision"); - const cursor = requiredRowString(value, "cursor"); - if (!/^\d+$/.test(providerRevision) || !/^\d+$/.test(cursor)) { - throw new AuthorityStoreProtocolError("PostgreSQL head revision or cursor is invalid"); - } - const parsedCursor = BigInt(cursor); - const parsedRevision = BigInt(providerRevision); - if (parsedCursor === 0n && parsedRevision === 0n && value.head === null) { - return { provider_revision: providerRevision, cursor, head: null }; - } - if (parsedCursor < 1n || parsedRevision < 1n) { - throw new AuthorityStoreProtocolError("PostgreSQL initialized head is invalid"); - } - return { - provider_revision: providerRevision, - cursor, - head: canonicalAuthorityObject(value.head, "PostgreSQL authority head"), - }; -} - -function providerRevisionToken(storeIdentity: string, revision: string): string { - if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) { - throw new AuthorityStoreProtocolError("PostgreSQL store identity is invalid"); - } - if (!/^[1-9]\d*$/.test(revision)) { - throw new AuthorityStoreProtocolError("PostgreSQL provider revision is invalid"); - } - return `${storeIdentity}:${revision}`; -} - -function decodeReceipt(row: JsonObject, storeIdentity: string): Extract { - const cursor = requiredRowString(row, "cursor"); - if (parseAuthorityCursor(cursor) < 1n) { - throw new AuthorityStoreProtocolError("PostgreSQL receipt cursor must be positive"); - } - return {status: "found", cursor, provider_revision: providerRevisionToken( - storeIdentity, requiredRowString(row, "provider_revision"), - ), receipts: canonicalAuthorityObjectList(row.receipts, "receipts")}; -} - -function parseExpectedRevision(value: string | null): { - store_identity: string; - revision: string; -} | null { - if (value === null) return null; - const match = POSTGRESQL_PROVIDER_REVISION_PATTERN.exec(value); - if (!match) { - throw new AuthorityStoreProtocolError("expected PostgreSQL provider revision is invalid"); - } - return { store_identity: `postgresql:${match[1]!}`, revision: match[2]! }; -} - -function canonicalCommitEnvelopeBytes(commit: AuthorityStoreCommit): number { - return canonicalAuthorityBytes({ - operation_id: commit.operation_id, - events: commit.events, - next_projection: commit.next_projection, - receipts: commit.receipts, - }).byteLength; -} - -function readFailure(error: unknown): AuthorityStoreReadFailure { - if (error instanceof AuthorityStoreProtocolError || error instanceof SyntaxError) { - return { - status: "failed", - reason_code: "provider_protocol_violation", - reason: error.message, - }; - } - return { - status: "unavailable", - reason_code: "provider_read_unavailable", - reason: "PostgreSQL authority store is unavailable", - }; -} - -function asError(error: unknown): Error { - return error instanceof Error ? error : new Error("PostgreSQL connection failed"); -} - -async function rollback(connection: PostgreSqlAuthorityConnection): Promise { - try { - await connection.query("ROLLBACK"); - return null; - } catch (error) { - // A failed connection also causes PostgreSQL to discard an uncommitted - // transaction. The caller must evict that connection from its pool. - return asError(error); - } -} - -async function beginTenantTransaction( - connection: PostgreSqlAuthorityConnection, - tenantId: string, - options: { readOnly: boolean }, -): Promise { - await connection.query(options.readOnly ? "BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY" : "BEGIN"); - try { - const context = oneRow(await connection.query( - "SELECT set_config('loopx.tenant_id', $1, TRUE) AS tenant_id", - [tenantId], - ), "PostgreSQL tenant context"); - if (context?.tenant_id !== tenantId) { - throw new AuthorityStoreProtocolError( - "PostgreSQL transaction did not retain the requested tenant context", - ); - } - } catch (error) { - const rollbackError = await rollback(connection); - if (rollbackError !== null) throw rollbackError; - throw error; - } -} - -async function requireStoreIdentity( - connection: PostgreSqlAuthorityConnection, -): Promise { - const metadata = oneRow(await connection.query( - `SELECT schema_version, store_identity - FROM loopx_control_plane.authority_store_metadata - WHERE singleton = TRUE`, - ), "PostgreSQL store metadata"); - if ( - metadata === null || metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION || - !POSTGRESQL_STORE_IDENTITY_PATTERN.test(String(metadata.store_identity)) - ) { - throw new AuthorityStoreProtocolError("PostgreSQL store metadata is not initialized"); - } - return String(metadata.store_identity); -} - -async function lockStoreIdentityTransaction( - connection: PostgreSqlAuthorityConnection, - mode: "shared" | "exclusive", -): Promise { - const lockFunction = mode === "shared" - ? "pg_advisory_xact_lock_shared" - : "pg_advisory_xact_lock"; - await connection.query( - `SELECT ${lockFunction}($1::bigint)`, - [POSTGRESQL_STORE_IDENTITY_LOCK_KEY], - ); -} - -/** - * Administrative schema installation and provisioning under one transaction. - * A new store uses a new identity. A format upgrade binds the current identity - * and keeps the existing journal access available through the checkpoint table. - */ -export async function installPostgreSqlAuthorityStoreSchema( - database: PostgreSqlAuthorityDatabase, - storeIdentity: string, - runtimeRole?: string, -): Promise { - if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity)) { - throw new AuthorityStoreProtocolError( - "PostgreSQL store identity must match postgresql:<32 lowercase hex>", - ); - } - if (runtimeRole !== undefined && !POSTGRESQL_RUNTIME_ROLE_PATTERN.test(runtimeRole)) { - throw new AuthorityStoreProtocolError("runtime role must be a valid lowercase SQL identifier"); - } - const connection = await database.connect(); - let commitStarted = false; - let releaseError: Error | undefined; - try { - await connection.query("BEGIN"); - // Fence old writers before publishing the first journal format they - // cannot read. Rotation and ordinary commits take the same lock. - await lockStoreIdentityTransaction(connection, "exclusive"); - await connection.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL); - await connection.query( - `INSERT INTO loopx_control_plane.authority_store_metadata - (singleton, schema_version, store_identity) - VALUES (TRUE, $1, $2) - ON CONFLICT (singleton) DO NOTHING`, - [POSTGRESQL_SCHEMA_VERSION, storeIdentity], - ); - const metadata = oneRow(await connection.query( - `SELECT schema_version, store_identity - FROM loopx_control_plane.authority_store_metadata - WHERE singleton = TRUE - FOR UPDATE`, - ), "PostgreSQL store metadata"); - if ( - metadata === null || metadata.store_identity !== storeIdentity || - (metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION && - metadata.schema_version !== FULL_PROJECTION_SCHEMA_VERSION) - ) { - throw new AuthorityStoreProtocolError( - "PostgreSQL store identity does not match the installed database incarnation", - ); - } - if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) { - // The released v0 schema required every projection. A nullable v0 - // journal is the unsupported pre-release format; do not bless it as v1. - const releasedLayout = oneRow(await connection.query( - `SELECT attnotnull FROM pg_attribute - WHERE attrelid = 'loopx_control_plane.authority_commits'::regclass - AND attname = 'projection' AND NOT attisdropped`, - ), "PostgreSQL v0 projection layout"); - if (releasedLayout?.attnotnull !== true) { - throw new AuthorityStoreProtocolError("PostgreSQL v0 journal layout is not the released full-projection format"); - } - // The checkpoint is part of the same append-only journal. Preserve - // table grants, including group grants, without copying history - // mutation privileges or grant options. - await connection.query(` - DO $migration$ - DECLARE journal_grant record; - BEGIN - FOR journal_grant IN - SELECT DISTINCT acl.grantee, acl.privilege_type - FROM pg_catalog.pg_class AS journal - CROSS JOIN LATERAL pg_catalog.aclexplode(journal.relacl) AS acl - WHERE journal.oid = 'loopx_control_plane.authority_commits'::regclass - AND acl.privilege_type IN ('SELECT', 'INSERT') - LOOP - EXECUTE pg_catalog.format( - 'GRANT %s ON loopx_control_plane.authority_checkpoints TO %s', - journal_grant.privilege_type, - CASE WHEN journal_grant.grantee = 0 THEN 'PUBLIC' - ELSE pg_catalog.quote_ident(pg_catalog.pg_get_userbyid(journal_grant.grantee)) - END - ); - END LOOP; - END - $migration$;`); - } - await connection.query( - `ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS delta jsonb; - ALTER TABLE loopx_control_plane.authority_commits ADD COLUMN IF NOT EXISTS projection_digest text; - ALTER TABLE loopx_control_plane.authority_commits ALTER COLUMN projection DROP NOT NULL;`, - ); - if (runtimeRole !== undefined) { - await connection.query(` - DO $role$ - BEGIN - IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${runtimeRole}') THEN - CREATE ROLE "${runtimeRole}" NOLOGIN; - END IF; - END - $role$; - GRANT USAGE ON SCHEMA loopx_control_plane TO "${runtimeRole}"; - GRANT SELECT ON loopx_control_plane.authority_store_metadata TO "${runtimeRole}"; - GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO "${runtimeRole}"; - GRANT SELECT, INSERT ON - loopx_control_plane.authority_checkpoints, - loopx_control_plane.authority_commits, - loopx_control_plane.authority_events, - loopx_control_plane.authority_receipts TO "${runtimeRole}";`); - } - if (metadata.schema_version === FULL_PROJECTION_SCHEMA_VERSION) { - await connection.query( - "UPDATE loopx_control_plane.authority_store_metadata SET schema_version = $1 WHERE singleton = TRUE", - [POSTGRESQL_SCHEMA_VERSION], - ); - } - commitStarted = true; - await connection.query("COMMIT"); - } catch (error) { - if (commitStarted) { - releaseError = asError(error); - } else { - releaseError = (await rollback(connection)) ?? undefined; - } - throw error; - } finally { - await connection.release(releaseError); - } -} - -export type PostgreSqlAuthorityIdentityRotationResult = - | { - status: "rotated"; - previous_store_identity: string; - store_identity: string; - } - | { - status: "ambiguous"; - reason_code: "store_identity_rotation_outcome_unknown"; - reason: string; - } - | { - status: "failed"; - reason_code: - | "invalid_store_identity" - | "store_identity_unchanged" - | "provider_connection_unavailable" - | "store_identity_mismatch" - | "provider_protocol_violation" - | "provider_transaction_failed"; - reason: string; - }; - -class PostgreSqlAuthorityIdentityRotationRejected extends Error { - readonly reasonCode: "store_identity_mismatch"; - - constructor(reason: string) { - super(reason); - this.name = "PostgreSqlAuthorityIdentityRotationRejected"; - this.reasonCode = "store_identity_mismatch"; - } -} - -/** - * Rotate the service-managed database incarnation after a restore. Revision - * tokens minted before the rotation become unusable because their opaque - * identity no longer matches; no Goal or operation state is rewritten. - */ -export async function rotatePostgreSqlAuthorityStoreIdentity( - database: PostgreSqlAuthorityDatabase, - expectedStoreIdentity: string, - nextStoreIdentity: string, -): Promise { - if (!POSTGRESQL_STORE_IDENTITY_PATTERN.test(expectedStoreIdentity) || - !POSTGRESQL_STORE_IDENTITY_PATTERN.test(nextStoreIdentity)) { - return { - status: "failed", - reason_code: "invalid_store_identity", - reason: "PostgreSQL store identities must match postgresql:<32 lowercase hex>", - }; - } - if (expectedStoreIdentity === nextStoreIdentity) { - return { - status: "failed", - reason_code: "store_identity_unchanged", - reason: "PostgreSQL store identity rotation requires a new incarnation", - }; - } - - let connection: PostgreSqlAuthorityConnection; - try { - connection = await database.connect(); - } catch { - return { - status: "failed", - reason_code: "provider_connection_unavailable", - reason: "PostgreSQL connection was unavailable before identity rotation", - }; - } - - let commitStarted = false; - let releaseError: Error | undefined; - try { - await connection.query("BEGIN"); - await lockStoreIdentityTransaction(connection, "exclusive"); - const metadata = oneRow(await connection.query( - `SELECT schema_version, store_identity - FROM loopx_control_plane.authority_store_metadata - WHERE singleton = TRUE - FOR UPDATE`, - ), "PostgreSQL store metadata"); - if ( - metadata === null || metadata.schema_version !== POSTGRESQL_SCHEMA_VERSION || - !POSTGRESQL_STORE_IDENTITY_PATTERN.test(String(metadata.store_identity)) || - metadata.store_identity !== expectedStoreIdentity - ) { - throw new PostgreSqlAuthorityIdentityRotationRejected( - "PostgreSQL store identity does not match the expected database incarnation", - ); - } - await connection.query( - `UPDATE loopx_control_plane.authority_store_metadata - SET store_identity = $1 - WHERE singleton = TRUE`, - [nextStoreIdentity], - ); - commitStarted = true; - await connection.query("COMMIT"); - return { - status: "rotated", - previous_store_identity: expectedStoreIdentity, - store_identity: nextStoreIdentity, - }; - } catch (error) { - if (commitStarted) { - releaseError = asError(error); - return { - status: "ambiguous", - reason_code: "store_identity_rotation_outcome_unknown", - reason: "PostgreSQL identity rotation outcome is unknown; read store metadata before retrying", - }; - } - releaseError = (await rollback(connection)) ?? undefined; - if (error instanceof PostgreSqlAuthorityIdentityRotationRejected) { - return {status: "failed", reason_code: error.reasonCode, reason: error.message}; - } - return { - status: "failed", - reason_code: error instanceof AuthorityStoreProtocolError - ? "provider_protocol_violation" - : "provider_transaction_failed", - reason: error instanceof AuthorityStoreProtocolError - ? error.message - : "PostgreSQL identity rotation failed before COMMIT", - }; - } finally { - await connection.release(releaseError); - } -} - -/** PostgreSQL Stage 2B store; domain decisions remain in LoopX authority. */ -export class PostgreSqlAuthorityStore implements AuthorityStore { - readonly providerKind = "postgresql" as const; - readonly database: PostgreSqlAuthorityDatabase; - readonly tenantId: string; - readonly goalId: string; - readonly maxCommitBytes: number; - readonly #expectedStoreIdentity: string | undefined; - - constructor( - database: PostgreSqlAuthorityDatabase, - options: PostgreSqlAuthorityStoreOptions, - ) { - this.database = database; - this.tenantId = requireAuthorityStoreId(options.tenant_id, "tenant id"); - this.goalId = requireAuthorityStoreId(options.goal_id, "goal id"); - this.#expectedStoreIdentity = options.expected_store_identity; - if (this.#expectedStoreIdentity !== undefined && - !POSTGRESQL_STORE_IDENTITY_PATTERN.test(this.#expectedStoreIdentity)) { - throw new AuthorityStoreProtocolError("expected PostgreSQL store identity is invalid"); - } - this.maxCommitBytes = options.max_commit_bytes ?? DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES; - if (!Number.isSafeInteger(this.maxCommitBytes) || this.maxCommitBytes < 1) { - throw new AuthorityStoreProtocolError( - "PostgreSQL max commit bytes must be a positive safe integer", - ); - } - } - - private async connect(): Promise { - return await this.database.connect(); - } - - private async readStoreIdentity(connection: PostgreSqlAuthorityConnection): Promise { - const identity = await requireStoreIdentity(connection); - if (this.#expectedStoreIdentity !== undefined && identity !== this.#expectedStoreIdentity) { - throw new AuthorityStoreProtocolError("PostgreSQL store identity does not match the expected database incarnation"); - } - return identity; - } - - private async readInTenantTransaction( - operation: (connection: PostgreSqlAuthorityConnection) => Promise, - ): Promise { - const connection = await this.connect(); - let transactionOpen = false; - let releaseError: Error | undefined; - try { - await beginTenantTransaction(connection, this.tenantId, { readOnly: true }); - transactionOpen = true; - const result = await operation(connection); - const rollbackError = await rollback(connection); - transactionOpen = false; - if (rollbackError !== null) { - releaseError = rollbackError; - throw rollbackError; - } - return result; - } catch (error) { - if (transactionOpen) { - releaseError = (await rollback(connection)) ?? undefined; - } else if (releaseError === undefined) { - // BEGIN or tenant-context setup may have failed after reaching the - // server. Do not return a session with unknown state to the pool. - releaseError = asError(error); - } - throw error; - } finally { - await connection.release(releaseError); - } - } - - async storeIdentity(): Promise { - let connection: PostgreSqlAuthorityConnection | null = null; - let releaseError: Error | undefined; - try { - connection = await this.connect(); - return { status: "available", store_identity: await this.readStoreIdentity(connection) }; - } catch (error) { - releaseError = asError(error); - const failure = readFailure(error); - return failure.status === "failed" - ? { ...failure, reason_code: "store_identity_invalid" } - : { ...failure, reason_code: "store_identity_unavailable" }; - } finally { - await connection?.release(releaseError); - } - } - - async loadAuthority(): Promise { - try { - return await this.readInTenantTransaction(async (connection) => { - const storeIdentity = await this.readStoreIdentity(connection); - const value = oneRow( - await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]), - "PostgreSQL authority head", - ); - if (value === null) return { status: "missing" } as const; - const head = decodeHeadRow(value); - if (head.head === null) return { status: "missing" } as const; - return { - status: "loaded", - head: structuredClone(head.head), - provider_revision: providerRevisionToken(storeIdentity, head.provider_revision), - cursor: head.cursor, - } as const; - }); - } catch (error) { - return readFailure(error); - } - } - - async commitAuthority(commit: AuthorityStoreCommit): Promise { - let normalized: AuthorityStoreCommit; - let expectedRevision: ReturnType; - try { - normalized = normalizeAuthorityStoreCommit(commit); - expectedRevision = parseExpectedRevision(normalized.expected_provider_revision); - } catch (error) { - return { - status: "failed", - reason_code: "invalid_commit_request", - reason: error instanceof Error ? error.message : "invalid commit request", - }; - } - const commitBytes = canonicalCommitEnvelopeBytes(normalized); - if (commitBytes > this.maxCommitBytes) { - return { - status: "failed", - reason_code: "store_capacity_exhausted", - reason: - `PostgreSQL authority commit is ${commitBytes} canonical bytes; configured maximum is ${this.maxCommitBytes}`, - }; - } - - let connection: PostgreSqlAuthorityConnection; - try { - connection = await this.connect(); - } catch { - return { - status: "failed", - reason_code: "provider_connection_unavailable", - reason: "PostgreSQL connection was unavailable before the transaction", - }; - } - - let commitStarted = false; - let releaseError: Error | undefined; - try { - await beginTenantTransaction(connection, this.tenantId, { readOnly: false }); - // Keep the identity lock ahead of the per-Goal head lock. Shared mode - // preserves cross-Goal concurrency while rotation takes exclusive mode. - await lockStoreIdentityTransaction(connection, "shared"); - const storeIdentity = await this.readStoreIdentity(connection); - await connection.query( - `INSERT INTO loopx_control_plane.authority_heads - (tenant_id, goal_id, provider_revision, cursor, head) - VALUES ($1, $2, 0, 0, NULL) - ON CONFLICT (tenant_id, goal_id) DO NOTHING`, - [this.tenantId, this.goalId], - ); - const rawHead = oneRow(await connection.query( - `${SELECT_HEAD_SQL} FOR UPDATE`, - [this.tenantId, this.goalId], - ), "locked PostgreSQL authority head"); - if (rawHead === null) { - throw new AuthorityStoreProtocolError("PostgreSQL authority head lock disappeared"); - } - const current = decodeHeadRow(rawHead); - const currentRevision = current.head === null ? null : current.provider_revision; - if ( - currentRevision !== (expectedRevision?.revision ?? null) || - (expectedRevision !== null && expectedRevision.store_identity !== storeIdentity) - ) { - releaseError = (await rollback(connection)) ?? undefined; - return { - status: "conflict", - conflict_kind: "provider_revision_mismatch", - current_provider_revision: currentRevision === null - ? null - : providerRevisionToken(storeIdentity, currentRevision), - current_cursor: current.head === null ? null : current.cursor, - }; - } - - const existing = oneRow(await connection.query( - `SELECT cursor::text AS cursor, provider_revision::text AS provider_revision - FROM loopx_control_plane.authority_commits - WHERE tenant_id = $1 AND goal_id = $2 AND operation_id = $3`, - [this.tenantId, this.goalId, normalized.operation_id], - ), "PostgreSQL operation identity"); - if (existing !== null) { - releaseError = (await rollback(connection)) ?? undefined; - return { - status: "conflict", - conflict_kind: "operation_id_exists", - current_provider_revision: currentRevision === null - ? null - : providerRevisionToken(storeIdentity, currentRevision), - current_cursor: current.head === null ? null : current.cursor, - }; - } - - const nextRevision = (BigInt(current.provider_revision) + 1n).toString(); - const nextCursor = (BigInt(current.cursor) + 1n).toString(); - const isCheckpoint = isAuthorityStateCheckpoint(BigInt(nextCursor)); - const projectionDigest = canonicalAuthoritySha256(normalized.next_projection); - - if (isCheckpoint) { - await connection.query( - `INSERT INTO loopx_control_plane.authority_checkpoints - (tenant_id, goal_id, cursor, projection, projection_digest) - VALUES ($1, $2, $3::bigint, $4::jsonb, $5)`, - [ - this.tenantId, - this.goalId, - nextCursor, - JSON.stringify(normalized.next_projection), - projectionDigest, - ], - ); - } - - const delta = isCheckpoint - ? null - : JSON.stringify(authorityStateDelta(current.head ?? {}, normalized.next_projection)); - - await connection.query( - `INSERT INTO loopx_control_plane.authority_commits - (tenant_id, goal_id, cursor, provider_revision, operation_id, delta, projection_digest) - VALUES ($1, $2, $3::bigint, $4::bigint, $5, $6::jsonb, $7)`, - [ - this.tenantId, - this.goalId, - nextCursor, - nextRevision, - normalized.operation_id, - delta, - projectionDigest, - ], - ); - for (const [index, event] of normalized.events.entries()) { - await connection.query( - `INSERT INTO loopx_control_plane.authority_events - (tenant_id, goal_id, cursor, event_index, event) - VALUES ($1, $2, $3::bigint, $4, $5::jsonb)`, - [this.tenantId, this.goalId, nextCursor, index, JSON.stringify(event)], - ); - } - for (const [index, receipt] of normalized.receipts.entries()) { - await connection.query( - `INSERT INTO loopx_control_plane.authority_receipts - (tenant_id, goal_id, cursor, receipt_index, receipt) - VALUES ($1, $2, $3::bigint, $4, $5::jsonb)`, - [this.tenantId, this.goalId, nextCursor, index, JSON.stringify(receipt)], - ); - } - const updated = await connection.query( - `UPDATE loopx_control_plane.authority_heads - SET provider_revision = $3::bigint, - cursor = $4::bigint, - head = $5::jsonb, - updated_at = transaction_timestamp() - WHERE tenant_id = $1 AND goal_id = $2 AND provider_revision = $6::bigint`, - [ - this.tenantId, - this.goalId, - nextRevision, - nextCursor, - JSON.stringify(normalized.next_projection), - current.provider_revision, - ], - ); - if (updated.rowCount !== 1) { - throw new AuthorityStoreProtocolError("locked PostgreSQL head revision changed"); - } - commitStarted = true; - await connection.query("COMMIT"); - return { - status: "applied", - provider_revision: providerRevisionToken(storeIdentity, nextRevision), - cursor: nextCursor, - }; - } catch (error) { - if (commitStarted) { - releaseError = asError(error); - return { - status: "ambiguous", - reason_code: "commit_outcome_unknown", - reason: "PostgreSQL COMMIT outcome is unknown; reconcile by operation receipt", - }; - } - releaseError = (await rollback(connection)) ?? undefined; - return { - status: "failed", - reason_code: error instanceof AuthorityStoreProtocolError - ? "provider_protocol_violation" - : "provider_transaction_failed", - reason: error instanceof AuthorityStoreProtocolError - ? error.message - : "PostgreSQL transaction failed before COMMIT", - }; - } finally { - await connection.release(releaseError); - } - } - - async readReceipt(operationId: string): Promise { - const result = await this.readReceiptSnapshot(operationId); - return result.status === "snapshot" ? result.receipt : result; - } - - async readReceiptSnapshot(operationId: string): Promise { - let normalized: string; - try { - normalized = requireAuthorityStoreId(operationId, "operation id"); - } catch (error) { - return { - status: "failed", - reason_code: "invalid_operation_id", - reason: error instanceof Error ? error.message : "invalid operation id", - }; - } - try { - return await this.readInTenantTransaction(async (connection) => { - const storeIdentity = await this.readStoreIdentity(connection); - const value = oneRow(await connection.query( - `${SELECT_RECEIPT_COLUMNS_SQL} - WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = $3`, - [this.tenantId, this.goalId, normalized], - ), "PostgreSQL authority receipt"); - if (value === null) return {status: "snapshot", store_identity: storeIdentity, - receipt: {status: "missing"}} as const; - return { - status: "snapshot", store_identity: storeIdentity, - receipt: decodeReceipt(value, storeIdentity), - } as const; - }); - } catch (error) { - return readFailure(error); - } - } - - async readReceipts(operationIds: readonly string[]): Promise { - try { - if (!Array.isArray(operationIds) || operationIds.length < 1 || operationIds.length > 64) { - throw new AuthorityStoreProtocolError("receipt batch requires 1..64 operations"); - } - for (const id of operationIds) requireAuthorityStoreId(id, "operation id"); - return await this.readInTenantTransaction(async connection => { - const identity = await this.readStoreIdentity(connection); - const values = rows(await connection.query( - `${SELECT_RECEIPT_COLUMNS_SQL} - WHERE commit.tenant_id = $1 AND commit.goal_id = $2 AND commit.operation_id = ANY($3::text[])`, - [this.tenantId, this.goalId, [...new Set(operationIds)]], - )); - const byOperation = new Map(values.map(row => [ - requiredRowString(row, "operation_id"), decodeReceipt(row, identity), - ])); - return {status: "receipts", results: operationIds.map(id => - structuredClone(byOperation.get(id) ?? {status: "missing"}))}; - }); - } catch (error) { - return readFailure(error); - } - } - - async scanCommitted( - afterCursor: string | null, - limit: number, - ): Promise { - const scan = AuthorityJournalScan.prepare(afterCursor, limit); - if (!(scan instanceof AuthorityJournalScan)) return scan; - try { - return await this.readInTenantTransaction(async (connection) => { - const storeIdentity = await this.readStoreIdentity(connection); - const current = oneRow( - await connection.query(SELECT_HEAD_SQL, [this.tenantId, this.goalId]), - "PostgreSQL authority head", - ); - if (current === null) return scan.page([], null); - const head = decodeHeadRow(current); - const snapshot = head.head === null ? null : { - cursor: head.cursor, - provider_revision: providerRevisionToken(storeIdentity, head.provider_revision), - head: head.head, - }; - const range = scan.rangeFailure(snapshot?.cursor ?? null); - if (range) return range; - - const headCursorBigInt = BigInt(head.cursor); - const fromCursor = scan.offset + 1n; - if (fromCursor > headCursorBigInt) { - return scan.page([], snapshot); - } - const requestedCount = BigInt(limit) + 1n; - const remaining = headCursorBigInt - scan.offset; - const countToFetch = remaining < requestedCount ? remaining : requestedCount; - const toCursor = fromCursor + countToFetch - 1n; - - // Fetch at most one checkpoint window before the visible page. Validate - // that prefix too: a missing row could otherwise silently change replay. - const checkpointCursor = authorityStateCheckpointCursor(fromCursor); - const rawCommits = rows( - await connection.query( - `${SELECT_TRANSACTION_COLUMNS_SQL} - WHERE commit.tenant_id = $1 AND commit.goal_id = $2 - AND commit.cursor >= $3::bigint AND commit.cursor <= $4::bigint - ORDER BY commit.cursor`, - [this.tenantId, this.goalId, checkpointCursor.toString(), toCursor.toString()], - ), - ); - - if (BigInt(rawCommits.length) !== toCursor - checkpointCursor + 1n) { - throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is incomplete"); - } - - let replay: AuthorityStateReplay | null = null; - const transactions: AuthorityStoreCommittedTransaction[] = []; - for (const [index, raw] of rawCommits.entries()) { - const cursor = requiredRowString(raw, "cursor"); - const rowCursor = parseAuthorityCursor(cursor); - if (rowCursor !== checkpointCursor + BigInt(index)) { - throw new AuthorityStoreProtocolError("PostgreSQL authority commit window is not contiguous"); - } - - const digest = raw.projection_digest; - if (raw.projection !== null) { - // Pre-upgrade rows retained the full projection. Their original - // operation, event and receipt identities are unchanged. - if (raw.delta !== null || digest !== null || raw.checkpoint_projection !== null) { - throw new AuthorityStoreProtocolError("PostgreSQL legacy commit has a conflicting replay record"); - } - replay = new AuthorityStateReplay(raw.projection); - } else { - if (isAuthorityStateCheckpoint(rowCursor)) { - if (raw.checkpoint_projection === null || raw.delta !== null || - digest !== raw.checkpoint_digest) { - throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent checkpoint"); - } - replay = new AuthorityStateReplay(raw.checkpoint_projection); - } else { - if (replay === null || raw.delta === null || raw.checkpoint_projection !== null) { - throw new AuthorityStoreProtocolError("PostgreSQL commit has a missing or inconsistent delta"); - } - replay.apply(raw.delta); - } - if (typeof digest !== "string" || replay.stateDigest() !== digest) { - throw new AuthorityStoreProtocolError("PostgreSQL commit projection digest mismatch"); - } - } - - const providerRevision = providerRevisionToken( - storeIdentity, requiredRowString(raw, "provider_revision"), - ); - const operationId = requireAuthorityStoreId(raw.operation_id, "PostgreSQL operation id"); - if (rowCursor >= fromCursor) { - transactions.push({ - cursor, - provider_revision: providerRevision, - operation_id: operationId, - projection: replay.snapshot(), - events: canonicalAuthorityObjectList(raw.events, "events"), - receipts: canonicalAuthorityObjectList(raw.receipts, "receipts"), - }); - } - } - - return scan.page(transactions, snapshot); - }); - } catch (error) { - return readFailure(error); - } - } -} diff --git a/loopx/control_plane/coordination/remote_authority_contract.ts b/loopx/control_plane/coordination/remote_authority_contract.ts index 131b35fb73..b97085d971 100644 --- a/loopx/control_plane/coordination/remote_authority_contract.ts +++ b/loopx/control_plane/coordination/remote_authority_contract.ts @@ -2,7 +2,6 @@ import type {JsonObject} from "../effect_program.ts"; import {canonicalAuthorityObject, canonicalAuthoritySha256, hasExactAuthorityKeys, requireAuthorityStoreId} from "./authority_store_codec.ts"; -import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts"; import {taskLeaseOperationIdentity} from "../work_items/task_lease_operation_identity.ts"; import {normalizeTodoId, normalizeGoalId, normalizeOwner} from "../work_items/task_lease_acquire.ts"; import {BARE_SHA256_PATTERN} from "../content_digest.ts"; @@ -11,6 +10,7 @@ import {normalizeTodoResumeWhen, TODO_RESUME_NORMALIZE_REQUEST_SCHEMA_VERSION} f import {normalizeTodoPriority} from "../todos/priority.ts"; import {parseWireExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; +export const POSTGRESQL_STORE_IDENTITY_PATTERN = /^postgresql:[0-9a-f]{32}$/; export const REMOTE_AUTHORITY_REQUEST = "loopx_remote_authority_request_v0"; export const REMOTE_AUTHORITY_RESPONSE = "loopx_remote_authority_response_v0"; export const REMOTE_AUTHORITY_SCOPED_READ_RESPONSE = "loopx_remote_authority_scoped_read_response_v1"; diff --git a/loopx/control_plane/coordination/remote_authority_http.ts b/loopx/control_plane/coordination/remote_authority_http.ts index 55aca59b90..bb3d6839f8 100644 --- a/loopx/control_plane/coordination/remote_authority_http.ts +++ b/loopx/control_plane/coordination/remote_authority_http.ts @@ -1,100 +1,20 @@ -/** TLS-only, bounded transport for the candidate task-command service. */ -import {createServer, request as httpsRequest, type ServerOptions, type RequestOptions} from "node:https"; -import type {AddressInfo} from "node:net"; -import type {Duplex} from "node:stream"; +/** Outbound TLS-only, bounded client transport for the remote task-command wire contract. */ +import {request as httpsRequest, type RequestOptions} from "node:https"; import type {JsonObject} from "../effect_program.ts"; import {decodeRemoteAuthorityRequest, decodeRemoteAuthorityResponse, remoteAuthorityResponse, type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; -import type {RemoteAuthorityService} from "./remote_authority_service.ts"; -const MAX_REQUEST_BYTES = 16 * 1024; const MAX_RESPONSE_BYTES = 64 * 1024; -const SHUTDOWN_SOCKET_DRAIN_MS = 5000; -export function createRemoteAuthorityServer(service: RemoteAuthorityService, - tls: Pick, maxInFlight = 16) { - if (!tls.key || !tls.cert || !Number.isSafeInteger(maxInFlight) || maxInFlight < 1 || maxInFlight > 128) throw new TypeError("TLS and a bounded admission limit are required"); - let active = 0, draining = false; - let drained: (() => void) | undefined; - const sockets = new Set(); - const server = createServer({...tls, minVersion: "TLSv1.2", handshakeTimeout: 10000}, async (request, response) => { - response.setHeader("Content-Type", "application/json"); - response.setHeader("Cache-Control", "no-store"); - const reject = (status: number, code: string) => { - response.writeHead(status, {Connection: "close"}); - response.end(JSON.stringify({status: "rejected", reason_code: code})); - }; - const mode = request.url === "/v0/tasks/execute" ? "execute" : request.url === "/v0/tasks/recover" ? "recover" : null; - if (request.method !== "POST" || !mode) return reject(404, "route_unavailable"); - if (draining || active >= maxInFlight) return reject(503, "remote_authority_busy"); - const authorization = request.headers.authorization; - // No cookie authentication or browser CORS surface; credentials never enter - // request JSON, receipts, logs or redirect targets. - if (typeof authorization !== "string" || !/^Bearer [\x21-\x7e]{32,512}$/u.test(authorization) || - request.headers.origin !== undefined || request.headers["content-type"]?.split(";")[0] !== "application/json") return reject(401, "request_authentication_required"); - if (request.headers["content-encoding"] !== undefined) return reject(415, "encoded_body_unsupported"); - active++; - let dispatched = false; - try { - const chunks: Buffer[] = []; let length = 0; - for await (const chunk of request) { - length += chunk.length; - if (length > MAX_REQUEST_BYTES) return reject(413, "request_too_large"); - chunks.push(Buffer.from(chunk)); - } - const raw = JSON.parse(Buffer.concat(chunks).toString("utf8")); - const decoded = decodeRemoteAuthorityRequest(raw); - if (draining) return reject(503, "remote_authority_busy"); - dispatched = true; - const result = await service.dispatch(authorization.slice(7), decoded, mode); - const encoded = JSON.stringify(result); - if (Buffer.byteLength(encoded) > MAX_RESPONSE_BYTES) { - response.end(JSON.stringify(remoteAuthorityResponse(decoded, mode, {status: - mode === "recover" || decoded.operation === "read" || decoded.operation === "snapshot" ? "unavailable" : "ambiguous", - reason_code: "remote_authority_response_too_large"}))); - } else response.end(encoded); - } catch { - // Once dispatched, a broken response never establishes a rollback. - if (dispatched) response.destroy(); - else if (!response.destroyed) reject(400, "invalid_remote_authority_request"); - } finally {active--; if (active === 0) drained?.();} - }); - // Include sockets that have not completed the TLS handshake or HTTP headers. - server.on("connection", socket => { - sockets.add(socket); - socket.once("close", () => sockets.delete(socket)); - }); - server.requestTimeout = 15000; - server.headersTimeout = 10000; - server.keepAliveTimeout = 1000; - server.maxConnections = maxInFlight * 2; - return {server, - async listen(host = "127.0.0.1", port = 0): Promise { - await new Promise((resolve, reject) => { - const onError = (error: Error) => reject(error); - server.once("error", onError); - server.listen(port, host, () => {server.off("error", onError); resolve();}); - }); - return (server.address() as AddressInfo).port; - }, - async close(): Promise { - draining = true; - const completed = active ? new Promise(resolve => {drained = resolve;}) : Promise.resolve(); - // server.close() can stop Node's request timeout checker. Bound network - // draining ourselves; a disconnected response never cancels dispatched - // business work or permits the caller to close its database pool early. - const deadline = setTimeout(() => {for (const socket of sockets) socket.destroy();}, SHUTDOWN_SOCKET_DRAIN_MS); - try { - await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); - } finally {clearTimeout(deadline);} - await completed; - }, - }; +export interface RemoteAuthorityCallOptions { + mode?: RemoteAuthorityMode; + ca?: RequestOptions["ca"]; + timeoutMs?: number; } /** No automatic retry, redirects, or certificate-verification bypass. Recovery * reuses the complete original command; missing receipt is not a new operation. */ export async function callRemoteAuthority(endpoint: string, credential: string, - raw: RemoteAuthorityRequest, options: {mode?: RemoteAuthorityMode; ca?: RequestOptions["ca"]; timeoutMs?: number} = {}): Promise { + raw: RemoteAuthorityRequest, options: RemoteAuthorityCallOptions = {}): Promise { const request = decodeRemoteAuthorityRequest(raw), mode = options.mode ?? "execute"; const base = new URL(endpoint); if (base.protocol !== "https:" || base.username || base.password || base.search || base.hash || base.pathname !== "/") throw new TypeError("A credential-free HTTPS origin is required"); diff --git a/loopx/control_plane/coordination/remote_authority_policy.ts b/loopx/control_plane/coordination/remote_authority_policy.ts deleted file mode 100644 index 77f1b17e23..0000000000 --- a/loopx/control_plane/coordination/remote_authority_policy.ts +++ /dev/null @@ -1,159 +0,0 @@ -/** Explicit candidate service policy, reread atomically for every admission. - * It owns service-scoped peers only; it does not promote a local Goal registry. */ -import {createHash, timingSafeEqual} from "node:crypto"; -import {open} from "node:fs/promises"; -import type {JsonObject} from "../effect_program.ts"; -import {BARE_SHA256_PATTERN} from "../content_digest.ts"; -import {parseWireExactGoalRef, sameExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; -import {normalizeTodoId} from "../work_items/task_lease_acquire.ts"; -import {canonicalAuthorityObject, canonicalAuthoritySha256, hasExactAuthorityKeys} from "./authority_store_codec.ts"; -import {POSTGRESQL_STORE_IDENTITY_PATTERN} from "./postgresql_authority_store.ts"; -import {REMOTE_AUTHORITY_OPERATIONS, remoteAuthorityId, remoteAuthorityOperationId, - type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; -import type {RemoteAuthorityAdmission} from "./remote_authority_service.ts"; - -export const REMOTE_AUTHORITY_POLICY = "loopx_remote_authority_policy_v0"; -export const REMOTE_AUTHORITY_SCOPED_READ_POLICY = "loopx_remote_authority_scoped_read_policy_v1"; -const sameScope = (a: JsonObject, b: {tenant_id: string; goal_id: string; store_identity: string}) => - a.tenant_id === b.tenant_id && a.goal_id === b.goal_id && a.store_identity === b.store_identity; -function object(value: unknown, keys: string[], name: string, optionalKeys: string[] = []): JsonObject { - const result = canonicalAuthorityObject(value, name); - if (!hasExactAuthorityKeys(result, [...keys, ...optionalKeys.filter(key => Object.hasOwn(result, key))])) { - throw new TypeError(`Invalid ${name}`); - } - return result; -} -export function decodeRemoteAuthorityPolicy(value: unknown): JsonObject { - const policy = object(value, ["schema_version", "scopes", "principals"], "service policy"); - const scoped = policy.schema_version === REMOTE_AUTHORITY_SCOPED_READ_POLICY; - if (!(scoped || policy.schema_version === REMOTE_AUTHORITY_POLICY) || !Array.isArray(policy.scopes) || - !Array.isArray(policy.principals) || policy.scopes.length > 128 || policy.principals.length > 256) throw new TypeError("Invalid service policy"); - const scopes = policy.scopes.map(raw => { - const scope = object(raw, ["tenant_id", "goal_id", "store_identity", "registered_agents", - ...(scoped ? ["todo_read_acl"] : [])], "service scope"); - remoteAuthorityId(scope.tenant_id, "tenant"); remoteAuthorityId(scope.goal_id, "Goal"); - if (typeof scope.store_identity !== "string" || !POSTGRESQL_STORE_IDENTITY_PATTERN.test(scope.store_identity) || - !Array.isArray(scope.registered_agents) || scope.registered_agents.length === 0 || scope.registered_agents.length > 128) throw new TypeError("Invalid service scope"); - scope.registered_agents.forEach(a => remoteAuthorityId(a, "registered actor")); - if (new Set(scope.registered_agents).size !== scope.registered_agents.length) throw new TypeError("Duplicate registered actor"); - if (scoped) { - if (!Array.isArray(scope.todo_read_acl)) throw new TypeError("Invalid Todo read ACL"); - const actors = new Set(scope.registered_agents), entries = new Set(); - for (const rawEntry of scope.todo_read_acl) { - const entry = object(rawEntry, ["goal_ref", "todo_id", "principal_id", "actor_id"], - "Todo read ACL entry", ["receipt_operation_ids", "todo_origin_operation_id"]); - const goal = parseWireExactGoalRef(entry.goal_ref, "Todo read ACL Goal"); - remoteAuthorityId(entry.principal_id, "ACL principal"); - remoteAuthorityId(entry.todo_id, "ACL Todo"); - if (goal.goal_id !== scope.goal_id || !actors.has(entry.actor_id) || normalizeTodoId(entry.todo_id) !== entry.todo_id) { - throw new TypeError("Invalid Todo read ACL scope"); - } - const key = JSON.stringify([goal.goal_instance_id, entry.todo_id, entry.principal_id, entry.actor_id]); - if (entries.has(key)) throw new TypeError("Duplicate Todo read ACL entry"); - entries.add(key); - if (Object.hasOwn(entry, "todo_origin_operation_id")) { - remoteAuthorityId(entry.todo_origin_operation_id, "Todo origin operation"); - } - if (Object.hasOwn(entry, "receipt_operation_ids")) { - const ids = entry.receipt_operation_ids; - if (!Array.isArray(ids) || new Set(ids).size !== ids.length) throw new TypeError("Invalid receipt read ACL"); - ids.forEach(id => remoteAuthorityId(id, "receipt ACL operation")); - } - } - } - return scope; - }); - const scopeKeys = scopes.map(s => JSON.stringify([s.tenant_id, s.goal_id])); - if (new Set(scopeKeys).size !== scopeKeys.length) throw new TypeError("Duplicate service scope"); - const principals = policy.principals.map(raw => { - const principal = object(raw, ["principal_id", "credential_sha256", "grants"], "principal"); - remoteAuthorityId(principal.principal_id, "principal"); - if (typeof principal.credential_sha256 !== "string" || !BARE_SHA256_PATTERN.test(principal.credential_sha256) || - !Array.isArray(principal.grants) || principal.grants.length > 128) throw new TypeError("Invalid principal"); - const seen = new Set(); - principal.grants.forEach(rawGrant => { - const grant = object(rawGrant, ["tenant_id", "goal_id", "actor_id", "operations", - ...(scoped ? ["goal_ref"] : [])], "grant"); - const scope = scopes.find(s => s.tenant_id === grant.tenant_id && s.goal_id === grant.goal_id); - if (!scope || !(scope.registered_agents as string[]).includes(String(grant.actor_id)) || - !Array.isArray(grant.operations) || grant.operations.length === 0 || - grant.operations.some(op => !REMOTE_AUTHORITY_OPERATIONS.includes(op as typeof REMOTE_AUTHORITY_OPERATIONS[number])) || - new Set(grant.operations).size !== grant.operations.length) throw new TypeError("Invalid grant"); - if (scoped && (parseWireExactGoalRef(grant.goal_ref, "read grant Goal").goal_id !== grant.goal_id || - grant.operations.some(op => op !== "read" && op !== "snapshot"))) throw new TypeError("Invalid scoped read grant"); - const key = JSON.stringify([grant.tenant_id, grant.goal_id, grant.actor_id]); - if (seen.has(key)) throw new TypeError("Duplicate grant"); - seen.add(key); - }); - return principal; - }); - for (const field of ["principal_id", "credential_sha256"]) { - if (new Set(principals.map(p => p[field])).size !== principals.length) throw new TypeError("Duplicate principal or credential"); - } - return policy; -} - -export function remoteAuthorityPolicyAdmission(policy: JsonObject, credential: string, - request: RemoteAuthorityRequest, mode: RemoteAuthorityMode = "execute"): RemoteAuthorityAdmission { - const digest = createHash("sha256").update(credential).digest(); - const principal = (policy.principals as JsonObject[]).find(p => { - const candidate = Buffer.from(String(p.credential_sha256), "hex"); - return candidate.length === digest.length && timingSafeEqual(candidate, digest); - }); - if (!principal) return {status: "rejected", reason_code: "principal_unauthenticated"}; - const scope = (policy.scopes as JsonObject[]).find(s => sameScope(s, request)); - const scoped = policy.schema_version === REMOTE_AUTHORITY_SCOPED_READ_POLICY; - const recoverScoped = scoped && mode === "recover"; - const operation = recoverScoped ? "read" : request.operation; - const grant = (principal.grants as JsonObject[]).find(g => g.tenant_id === request.tenant_id && - g.goal_id === request.goal_id && g.actor_id === request.actor_id && (g.operations as string[]).includes(operation)); - if (!scope || !grant) return {status: "rejected", reason_code: "remote_authority_scope_denied"}; - const allowed = {status: "allowed" as const, principal_id: String(principal.principal_id), - policy_revision: canonicalAuthoritySha256(policy), registered_agents: [...scope.registered_agents as string[]]}; - if (!scoped) return allowed; - - const goalRef = grant.goal_ref as WireExactGoalRef; - const entries = (scope.todo_read_acl as JsonObject[]) - .filter(entry => entry.principal_id === principal.principal_id && entry.actor_id === request.actor_id && - sameExactGoalRef(entry.goal_ref as WireExactGoalRef, goalRef)); - const todoIds = entries.map(entry => String(entry.todo_id)); - if (todoIds.length === 0 || request.todo_id !== undefined && !todoIds.includes(request.todo_id)) { - return {status: "rejected", reason_code: "remote_authority_scope_denied"}; - } - const readScope = {goal_ref: goalRef, todo_ids: todoIds, - todo_origins: Object.fromEntries(entries.filter(entry => entry.todo_origin_operation_id !== undefined) - .map(entry => [String(entry.todo_id), String(entry.todo_origin_operation_id)]))}; - if (!recoverScoped) return {...allowed, read_scope: readScope}; - const id = remoteAuthorityOperationId(request, allowed.principal_id); - if (!entries.some(entry => entry.todo_id === request.todo_id && - (entry.receipt_operation_ids as string[] | undefined)?.includes(id))) { - return {status: "rejected", reason_code: "remote_authority_scope_denied"}; - } - return {...allowed, read_scope: {...readScope, receipt_operation_id: id}}; -} - -export function fileRemoteAuthorityAdmission(path: string, expectedSchema: string = REMOTE_AUTHORITY_POLICY) { - return async (credential: string, request: RemoteAuthorityRequest, - mode: RemoteAuthorityMode = "execute"): Promise => { - // A rename publishes one complete policy. Missing, malformed or unreadable - // policy fails closed; never retain a stale last-known-good grant. - const policy = await readRemoteAuthorityPolicy(path); - if (policy.schema_version !== expectedSchema) throw new TypeError("Service policy schema changed; restart the listener"); - return remoteAuthorityPolicyAdmission(policy, credential, request, mode); - }; -} - -export async function readRemoteAuthorityPolicy(path: string): Promise { - const file = await open(path, "r"); - try { - const bytes = Buffer.alloc(256 * 1024 + 1); - let length = 0; - while (length < bytes.length) { - const {bytesRead} = await file.read(bytes, length, bytes.length - length); - if (bytesRead === 0) break; - length += bytesRead; - } - if (length === bytes.length) throw new TypeError("Service policy is too large"); - return decodeRemoteAuthorityPolicy(JSON.parse(bytes.subarray(0, length).toString("utf8"))); - } finally {await file.close();} -} diff --git a/loopx/control_plane/coordination/remote_authority_service.ts b/loopx/control_plane/coordination/remote_authority_service.ts deleted file mode 100644 index 19fec31001..0000000000 --- a/loopx/control_plane/coordination/remote_authority_service.ts +++ /dev/null @@ -1,405 +0,0 @@ -/** Authenticated task commands over the existing semantic and persistence owners. */ -import type {JsonObject} from "../effect_program.ts"; -import type {AuthorityStore, AuthorityStoreReadFailure} from "./authority_store.ts"; -import {canonicalAuthoritySha256, parseAuthorityCursor} from "./authority_store_codec.ts"; -import {PostgreSqlAuthorityService} from "./postgresql_authority_service.ts"; -import type {PostgreSqlAuthorityDatabase} from "./postgresql_authority_store.ts"; -import {executeCoordinationTodoClaim} from "./todo_claim.ts"; -import {executeCoordinationTodoUpdate} from "./todo_update.ts"; -import {decodeCoordinationTodoCreateReceipt, executeCoordinationTodoCreate, COORDINATION_TODO_CREATE_RECEIPT_SCHEMA} from "./todo_create.ts"; -import {executeCoordinationTodoTerminalLifecycle, type CoordinationTodoTerminalLifecycleInput} from "./todo_terminal_lifecycle.ts"; -import {executeCanonicalTaskLeaseLifecycle} from "./task_lease_lifecycle.ts"; -import {leaseVersion, leaseEpoch} from "../work_items/task_lease_acquire.ts"; -import {TODO_DOMAIN_ITEM_SCHEMA} from "./coordination_state_contract.ts"; -import {CoordinationProjectionRead} from "./coordination_projection.ts"; -import {GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA, readGoalAcceptanceLifecycle, sameExactGoalRef, type WireExactGoalRef} from "../goals/acceptance_lifecycle.ts"; -import {decodeRemoteAuthorityRequest, remoteAuthorityOperationId, remoteAuthorityResponse, remoteAuthoritySnapshotDigest, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, - type RemoteAuthorityMode, type RemoteAuthorityRequest} from "./remote_authority_contract.ts"; - -export type RemoteAuthorityAdmission = - | {status: "allowed"; principal_id: string; policy_revision: string; registered_agents: readonly string[]; - read_scope?: {goal_ref: WireExactGoalRef; todo_ids: readonly string[]; - todo_origins: Readonly>; receipt_operation_id?: string}} - | {status: "rejected"; reason_code: string}; -export interface RemoteAuthorityDependencies { - database: PostgreSqlAuthorityDatabase; - /** One immutable, server-owned authorization decision per request. Revocation - * affects later admissions; it never lies about an already admitted commit. */ - admit(credential: string, request: RemoteAuthorityRequest, mode: RemoteAuthorityMode): Promise; - now?: () => Date; -} -const incarnationFailure: AuthorityStoreReadFailure = {status: "unavailable", - reason_code: "remote_authority_incarnation_changed", reason: "Rebind the authorized service scope before continuing"}; -const scopeDenied = {status: "rejected", reason_code: "remote_authority_scope_denied"} as const; -const MAX_READ_NOTE_BYTES = 4096; - -/** Pin every observation and write to the admitted incarnation. The provider's - * transactional revision check also fences rotation after our last read. */ -function pinnedStore(store: AuthorityStore, identity: string, admission: JsonObject, - operationId: string): AuthorityStore { - const matches = (revision: string) => revision.startsWith(`${identity}:`); - return { - providerKind: "postgresql", - async storeIdentity() { - const result = await store.storeIdentity(); - return result.status === "available" && result.store_identity !== identity ? incarnationFailure : result; - }, - async loadAuthority() { - const result = await store.loadAuthority(); - return result.status === "loaded" && !matches(result.provider_revision) ? incarnationFailure : result; - }, - async readReceipt(id) { - // Both found and missing must come from the admitted incarnation. Never - // manufacture a snapshot from separate identity and receipt reads. - if (!store.readReceiptSnapshot) return {status: "unavailable", - reason_code: "remote_authority_receipt_snapshot_unavailable", reason: "Atomic receipt observation is required"}; - const snapshot = await store.readReceiptSnapshot(id); - if (snapshot.status !== "snapshot") return snapshot; - if (snapshot.store_identity !== identity) return incarnationFailure; - const result = snapshot.receipt; - if (result.status !== "found") return result; - if (!matches(result.provider_revision)) return incarnationFailure; - if (id === operationId) { - const stamp = result.receipts[0]?.remote_admission as JsonObject | undefined; - if (result.receipts.length !== 1 || !stamp || ["principal_id", "actor_id", "request_sha256"].some(k => - stamp[k] !== admission[k])) return {status: "failed", reason_code: "remote_authority_operation_mismatch", - reason: "Operation belongs to a different admitted request"}; - } - return result; - }, - async scanCommitted(after, limit) { - const result = await store.scanCommitted(after, limit); - return result.status === "page" && result.transactions.some(row => !matches(row.provider_revision)) ? incarnationFailure : result; - }, - async commitAuthority(commit) { - if (commit.operation_id !== operationId || commit.expected_provider_revision === null || - !matches(commit.expected_provider_revision)) return {status: "failed", - reason_code: "remote_authority_commit_binding_invalid", reason: "Command does not match admitted authority"}; - if (commit.next_projection.handoff_mode !== "hard_lease") return { - status: "failed", reason_code: "remote_authority_requires_hard_lease", - reason: "The committing authority must use hard leases", - }; - // Metadata shares the existing business receipt transaction; no second ledger. - return store.commitAuthority({...commit, receipts: commit.receipts.map(receipt => - ({...receipt, remote_admission: admission}))}); - }, - }; -} - -function projectReadTodo(todo: JsonObject, includePlanning: boolean, includeNote: boolean): JsonObject { - const fields = ["todo_id", "status", "claimed_by", ...(includeNote ? ["note"] : []), - ...(includePlanning ? ["role", "priority"] : [])]; - const output = Object.fromEntries(fields.filter(field => Object.hasOwn(todo, field)).map(field => [field, todo[field]])); - if (Object.hasOwn(output, "note") && Buffer.byteLength(JSON.stringify(output.note)) > MAX_READ_NOTE_BYTES) { - delete output.note; - output.note_omitted = true; - } - return output; -} - -function isActiveGoal(head: JsonObject, goalRef: WireExactGoalRef): boolean { - const lifecycle = readGoalAcceptanceLifecycle(head, goalRef.goal_id); - return lifecycle?.state === "active" && sameExactGoalRef(lifecycle.goal_ref, goalRef); -} - -/** Reconstruct one exact committed operation through the existing checkpoint - * owner. Proof is bounded by the admitted head; neither current state nor a - * later receipt can substitute for missing historical evidence. */ -async function readScopedTransaction(store: AuthorityStore, headCursor: string, - goalRef: WireExactGoalRef, operationId: string) { - const unavailable = {status: "unavailable", reason_code: "remote_authority_read_unavailable"} as const; - const receipt = await store.readReceipt(operationId); - if (receipt.status !== "found") return receipt.status === "missing" || - receipt.reason_code === "remote_authority_operation_mismatch" ? scopeDenied : unavailable; - const cursor = parseAuthorityCursor(receipt.cursor); - if (cursor > parseAuthorityCursor(headCursor) || receipt.receipts.length !== 1) return scopeDenied; - const offset = cursor - 1n; - const page = await store.scanCommitted(offset === 0n ? null : offset.toString(), 1); - if (page.status !== "page" || page.transactions.length !== 1) return unavailable; - const transaction = page.transactions[0]!; - const original = receipt.receipts[0]!; - const digest = canonicalAuthoritySha256(original); - if (transaction.cursor !== receipt.cursor || transaction.provider_revision !== receipt.provider_revision || - transaction.operation_id !== operationId || transaction.receipts.length !== 1 || - canonicalAuthoritySha256(transaction.receipts[0]) !== digest) return unavailable; - if (!isActiveGoal(transaction.projection, goalRef)) return scopeDenied; - return {status: "found" as const, original, projection: transaction.projection, digest}; -} - -/** The policy selects evidence, not resource ownership. Only a successful Todo - * creation or the trusted first bind of existing work can prove its lifetime. - * A claim/update, successor activation or idempotent rebind cannot relabel it. */ -function provesTodoOrigin(proof: Extract>, {status: "found"}>, - goalRef: WireExactGoalRef, todoId: string): boolean { - const read = new CoordinationProjectionRead(proof.projection, goalRef.goal_id); - read.validateTodoReadModel(); - const todo = read.todoIndex.todos.get(todoId); - if (!todo || proof.original.goal_id !== goalRef.goal_id) return false; - if (proof.original.schema_version === COORDINATION_TODO_CREATE_RECEIPT_SCHEMA) { - return proof.original.todo_id === todoId && canonicalAuthoritySha256(proof.original.todo) === canonicalAuthoritySha256(todo); - } - const result = proof.original.result; - return proof.original.schema_version === GOAL_ACCEPTANCE_LIFECYCLE_OPERATION_SCHEMA && - proof.original.request_sha256 === canonicalAuthoritySha256({transition: {kind: "bind_existing", goal_ref: goalRef}}) && - result !== null && typeof result === "object" && !Array.isArray(result) && (result as JsonObject).changed === true; -} - -/** Authorize from the current pinned head, then prove the original transaction - * belonged to the same Goal lifetime. Operation access stays separate from - * current Todo access and never returns the original execution payload. */ -async function readScopedReceipt(store: AuthorityStore, goalRef: WireExactGoalRef, - todoId: string, operationId: string): Promise { - const head = await store.loadAuthority(); - if (head.status !== "loaded") return {status: "unavailable", reason_code: "remote_authority_read_unavailable"}; - if (!isActiveGoal(head.head, goalRef)) return scopeDenied; - const current = new CoordinationProjectionRead(head.head, goalRef.goal_id); - current.validateTodoReadModel(); - if (!current.todoIndex.todos.has(todoId)) return scopeDenied; - const proof = await readScopedTransaction(store, head.cursor, goalRef, operationId); - if (proof.status !== "found") return proof; - return {status: "found", historical_only: true, todo_id: todoId, operation_id: operationId, receipt_sha256: proof.digest}; -} - -/** Deliberately bounded response projection: no SQL errors, paths, arbitrary - * canonical fields, workspace observations or provider credentials cross here. */ -function commandResult(result: JsonObject): JsonObject { - const output: JsonObject = {}; - for (const key of ["status", "reason_code", "changed", "todo_id", "claimed_by", "operation_id", - "claim_operation_id", "provider_revision", "cursor", "handoff_mode", "released", "renewed", "missing", - "completed", "completed_at", "completion_receipt_id"]) { - if (Object.hasOwn(result, key)) output[key] = result[key]; - } - if (result.lease && typeof result.lease === "object" && !Array.isArray(result.lease)) { - const lease = result.lease as JsonObject; - output.lease = {...Object.fromEntries(["goal_id", "todo_id", "owner", "idempotency_key", "status", - "acquired_at", "updated_at", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])), - version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)}; - } - if (result.todo && typeof result.todo === "object" && !Array.isArray(result.todo)) { - const todo = result.todo as JsonObject; - output.todo = Object.fromEntries(["todo_id", "status", "role", "priority", "claimed_by", "note"].filter(k => k in todo).map(k => [k, todo[k]])); - } - for (const key of ["blocked_lifecycle_transition", "deferred_transition"]) { - const raw = result[key]; - if (raw && typeof raw === "object" && !Array.isArray(raw)) { - const transition = raw as JsonObject; - output[key] = Object.fromEntries(["kind", "lease_retirement", "execution_authority_granted"] - .filter(field => Object.hasOwn(transition, field)).map(field => [field, transition[field]])); - } - } - return output; -} - -export class RemoteAuthorityService { - readonly #dependencies: RemoteAuthorityDependencies; - constructor(dependencies: RemoteAuthorityDependencies) { this.#dependencies = dependencies; } - - async dispatch(credential: string, raw: unknown, mode: RemoteAuthorityMode = "execute"): Promise { - let request: RemoteAuthorityRequest; - try { request = decodeRemoteAuthorityRequest(raw); } - catch { return {status: "rejected", reason_code: "invalid_remote_authority_request"}; } - const reply = (payload: JsonObject) => remoteAuthorityResponse(request, mode, payload); - if (mode !== "execute" && mode !== "recover" || mode === "recover" && (request.operation === "read" || request.operation === "snapshot")) { - return reply({status: "rejected", reason_code: "invalid_remote_authority_mode"}); - } - let admission: RemoteAuthorityAdmission; - try { admission = structuredClone(await this.#dependencies.admit(credential, request, mode)); } - catch { return reply({status: "unavailable", reason_code: "remote_authority_policy_unavailable"}); } - if (admission.status !== "allowed") return reply(admission); - const isRead = request.operation === "read" || request.operation === "snapshot"; - const readScope = admission.read_scope; - const allowedTodos = readScope && new Set(readScope.todo_ids); - const operationId = remoteAuthorityOperationId(request, admission.principal_id); - if (allowedTodos && (allowedTodos.size === 0 || request.todo_id && !allowedTodos.has(request.todo_id))) return reply(scopeDenied); - if (readScope && !isRead && (mode !== "recover" || request.todo_id === undefined || - readScope.receipt_operation_id !== operationId)) return reply(scopeDenied); - // Open through the established service seam with the already authenticated - // snapshot. Never re-evaluate a different policy halfway through a request. - const service = new PostgreSqlAuthorityService({database: this.#dependencies.database, - authenticatePrincipal: () => ({status: "authenticated", principal: {principal_id: admission.principal_id}}), - authorizeTenant: () => ({status: "allowed"})}); - const opened = await service.openStore({...request, credential: null}); - if (opened.status !== "opened") return reply({status: "unavailable", reason_code: opened.reason_code}); - // Explicitly reauthorizing a restored incarnation may recover old history. - // The incarnation gates current access; it is not part of business intent. - const {store_identity: admittedIdentity, ...intent} = request; - const binding: JsonObject = {principal_id: admission.principal_id, actor_id: request.actor_id, - request_sha256: canonicalAuthoritySha256(intent), admitted_store_identity: admittedIdentity, - policy_revision: admission.policy_revision}; - const store = pinnedStore(opened.store, request.store_identity, binding, operationId); - let dispatched = false; - try { - if (readScope && mode === "recover" && request.todo_id !== undefined) { - const result = await readScopedReceipt(store, readScope.goal_ref, request.todo_id, operationId); - return result.status === "found" - ? remoteAuthorityResponse(request, mode, {...result, goal_ref: readScope.goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE) - : reply(result); - } - const previous = isRead ? null : await store.readReceipt(operationId); - if (previous?.status === "found") { - const original = previous.receipts[0]; - const recorded = original?.remote_admission as JsonObject | undefined; - if (previous.receipts.length !== 1 || !recorded || recorded.principal_id !== admission.principal_id || - recorded.actor_id !== request.actor_id || recorded.request_sha256 !== binding.request_sha256) { - return reply({status: "rejected", reason_code: "remote_authority_operation_mismatch"}); - } - if (mode === "recover") { - // Creation receipts store the original Todo, not command flags. Use - // their owner's decoder while retaining the original receipt digest. - const creation = request.operation === "todo_add" && original.schema_version === COORDINATION_TODO_CREATE_RECEIPT_SCHEMA - ? decodeCoordinationTodoCreateReceipt(original, request.todo_id) : null; - const historical = creation ? {...original, ...creation.fields, changed: creation.changed} - : original.result && typeof original.result === "object" ? original.result as JsonObject : original; - return reply({status: "found", operation_id: operationId, - provider_revision: previous.provider_revision, cursor: previous.cursor, - receipt_sha256: canonicalAuthoritySha256(original), historical_only: true, result: commandResult(historical)}); - } - } else if (previous && previous.status !== "missing") { - return reply({status: previous.reason_code === "remote_authority_operation_mismatch" ? "rejected" : "unavailable", - reason_code: previous.reason_code}); - } - if (mode === "recover") return reply({status: "missing", operation_id: operationId}); - const head = await store.loadAuthority(); - if (head.status !== "loaded") return reply({status: "unavailable", reason_code: "remote_authority_head_unavailable"}); - if (readScope && !isActiveGoal(head.head, readScope.goal_ref)) return reply(scopeDenied); - const projection = new CoordinationProjectionRead(head.head, request.goal_id); - projection.validateTodoReadModel(); - const index = projection.coordinationIndex; - const provenTodos = readScope ? new Set() : undefined; - if (readScope && provenTodos) { - // One request-local proof per origin, shared by Todos first bound in - // the same transaction. Never cache authorization across admissions. - const proofs = new Map>>(); - for (const id of readScope.todo_ids) { - if (request.todo_id && request.todo_id !== id || !index.todos.has(id)) continue; - const origin = readScope.todo_origins[id]; - if (!origin) continue; - let proof = proofs.get(origin); - if (!proof) { proof = await readScopedTransaction(store, head.cursor, readScope.goal_ref, origin); proofs.set(origin, proof); } - if (proof.status === "unavailable") return reply(proof); - if (proof.status === "found" && provesTodoOrigin(proof, readScope.goal_ref, id)) provenTodos.add(id); - } - if (request.todo_id ? !provenTodos.has(request.todo_id) : provenTodos.size === 0) return reply(scopeDenied); - } - const replyRead = (payload: JsonObject) => readScope - ? remoteAuthorityResponse(request, mode, {...payload, goal_ref: readScope.goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE) - : reply({...payload, provider_revision: head.provider_revision}); - if (request.operation === "read") { - const todo = index.todos.get(request.todo_id), lease = index.leases.get(request.todo_id); - if (!todo) return reply({status: "missing"}); - return replyRead({status: "found", - todo: projectReadTodo(todo, false, readScope === undefined), - lease: lease ? {...Object.fromEntries(["owner", "status", "expires_at"].filter(k => k in lease).map(k => [k, lease[k]])), - version: leaseVersion(lease), lease_epoch: leaseEpoch(lease)} : null}); - } - if (request.operation === "snapshot") { - const visible = (id: string) => (!request.todo_id || id === request.todo_id) && (!allowedTodos || allowedTodos.has(id)) && (!provenTodos || provenTodos.has(id)); - const todos = index.todo_ids - .filter(visible) - .map(id => projectReadTodo(index.todos.get(id)!, true, readScope === undefined)); - const leases = index.lease_todo_ids - .filter(visible) - .map(id => { - const l = index.leases.get(id)!; - return { - ...Object.fromEntries(["goal_id", "todo_id", "owner", "status", "expires_at"].filter(k => k in l).map(k => [k, l[k]])), - version: leaseVersion(l), - lease_epoch: leaseEpoch(l), - }; - }); - return replyRead({ - status: "snapshot", - store_identity: request.store_identity, - goal_id: request.goal_id, - receipt_sha256: remoteAuthoritySnapshotDigest(request, head.provider_revision, todos, leases, readScope?.goal_ref), - todos, - leases, - }); - } - if (head.head.handoff_mode !== "hard_lease") return reply({status: "rejected", reason_code: "remote_authority_requires_hard_lease"}); - const now = this.#dependencies.now?.() ?? new Date(); - if (!Number.isFinite(now.valueOf())) return reply({status: "unavailable", reason_code: "remote_authority_clock_unavailable"}); - const common = {goal_id: request.goal_id, todo_id: request.todo_id, - registered_agents: admission.registered_agents, now}; - let result: JsonObject; - dispatched = true; - if (request.operation === "claim") result = await executeCoordinationTodoClaim(store, {...common, - operation_id: operationId, claimed_by: request.actor_id, actor_agent_id: request.actor_id, - expected_role: "agent", manage_execution: true, required_handoff_mode: "hard_lease", dry_run: false}); - else if (request.operation === "progress") result = await executeCoordinationTodoUpdate(store, {...common, - operation_id: operationId, actor_agent_id: request.actor_id, expected_role: "agent", dry_run: false, - claim_operation_id: remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id), - patch: {note: request.note}, clear_fields: []}); - else if (request.operation === "complete") { - const claimOpId = remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id); - const input: CoordinationTodoTerminalLifecycleInput = { - goal_id: request.goal_id, todo_id: request.todo_id, command: "complete", - expected_role: "agent", actor_agent_id: request.actor_id, - registered_agents: admission.registered_agents, lifecycle_grants: [], authority_reason: null, - decision_outcome: null, operation_identity: {kind: "explicit", operation_id: operationId}, - claim_operation_id: claimOpId, lease_idempotency_key: null, lease_expected_version: null, - allow_user_gate_auto_acquire: false, requested_no_followup: request.no_followup === true, - requested_completion_turn_key: null, requested_completion_identity_source: null, - linked_successor_todo_ids: [], successor_intents: [], note: request.note ?? null, - evidence: request.evidence, reason: null, clear_claim: true, validation_declaration: null, - validation_receipt: null, completion_policy_request: null, dry_run: false, now, - }; - result = await executeCoordinationTodoTerminalLifecycle(store, input); - // Older complete requests implied no_followup. Use that intent only to - // replay an already committed receipt for the exact authenticated request. - if (previous?.status === "found" && request.no_followup === undefined && - result.reason_code === "coordination_operation_identity_mismatch") { - result = await executeCoordinationTodoTerminalLifecycle(store, {...input, requested_no_followup: true}); - } - if (result.status === "applied") result.completed = true; - } else if (request.operation === "block" || request.operation === "defer") { - const claimOpId = request.claim_request_id - ? remoteAuthorityOperationId(request, admission.principal_id, request.claim_request_id) - : undefined; - result = await executeCoordinationTodoUpdate(store, { - ...common, operation_id: operationId, actor_agent_id: request.actor_id, - expected_role: "agent", dry_run: false, ...(claimOpId ? {claim_operation_id: claimOpId} : {}), - patch: {...(request.note ? {note: request.note} : {})}, clear_fields: [], - planning_intent: { - status: request.operation === "block" ? "blocked" : "deferred", - reason: request.reason, - ...(request.resume_when ? {resume_when: request.resume_when} : {}), - }, - }); - } else if (request.operation === "todo_add") { - const todoRecord = { - schema_version: TODO_DOMAIN_ITEM_SCHEMA, - todo_id: request.todo_id, - role: request.role ?? "agent", - status: "open", - done: false, - text: request.text, - archive_state: "active", - ...(request.priority ? {priority: request.priority} : {}), - ...(request.task_class ? {task_class: request.task_class} : {}), - ...(request.action_kind ? {action_kind: request.action_kind} : {}), - ...(request.task_repository ? {task_repository: request.task_repository} : {}), - ...(request.required_write_scopes ? {required_write_scopes: [...request.required_write_scopes]} : {}), - ...(request.claimed_by ? {claimed_by: request.claimed_by} : {}), - ...(request.note ? {note: request.note} : {}), - }; - result = await executeCoordinationTodoCreate(store, { - goal_id: request.goal_id, todo: todoRecord, actor_agent_id: request.actor_id, - registered_agents: admission.registered_agents, operation_id: operationId, dry_run: false, now, - }); - // The create owner can return a different, semantically duplicate - // Todo. That is a valid local no-op but not proof of this requested id. - if (result.status === "no_change" && result.todo_id !== request.todo_id) { - result = {status: "failed", reason_code: "remote_authority_todo_id_not_created"}; - } - } else result = await executeCanonicalTaskLeaseLifecycle(store, {...common, operation: request.operation, - owner: request.actor_id, idempotency_key: request.idempotency_key, required_handoff_mode: "hard_lease", - expected_version: request.expected_version, ttl_seconds: request.operation === "renew" ? request.ttl_seconds : null}); - return reply({status: "result", operation_id: operationId, result: commandResult(result)}); - } catch { - if (readScope) return reply({status: "unavailable", reason_code: "remote_authority_read_unavailable"}); - return reply({status: dispatched ? "ambiguous" : "unavailable", operation_id: operationId, - reason_code: dispatched ? "remote_authority_recovery_required" : "remote_authority_read_unavailable"}); - } - } -} diff --git a/loopx/control_plane/effect_runtime_handlers.ts b/loopx/control_plane/effect_runtime_handlers.ts index 88df41aed7..3a0f266099 100644 --- a/loopx/control_plane/effect_runtime_handlers.ts +++ b/loopx/control_plane/effect_runtime_handlers.ts @@ -415,20 +415,6 @@ export function createEffectRuntimeHandlers( ["coordination.runtime_shadow.rollback", lazyHandler(() => Promise.all([import("./coordination/runtime_shadow.ts"), import("./coordination/source_transfer.ts")]), ([{rollbackCoordinationRuntimeShadow}, {withCoordinationSourceTransfer}]) => withCoordinationSourceTransfer("coordination.runtime_shadow.rollback", rollbackCoordinationRuntimeShadow))], ["coordination.local_authority.promote", lazyHandler(() => import("./coordination/local_authority_runtime.ts"), ({promoteLocalCoordinationAuthority}) => promoteLocalCoordinationAuthority)], ["coordination.authority_archive.manage", lazyHandler(() => import("./coordination/local_authority_archive.ts"), ({manageLocalAuthorityArchive}) => manageLocalAuthorityArchive)], - ["coordination.postgresql.setup", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({setupPostgreSqlAuthoritySchemaAndRole}) => params => setupPostgreSqlAuthoritySchemaAndRole({ - databaseUrl: requiredString(params.databaseUrl, "databaseUrl"), - storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined, - runtimeRole: optionalString(params.runtimeRole, "runtimeRole") ?? undefined, - }))], - ["coordination.postgresql.seed", lazyHandler(() => import("./coordination/postgresql_authority_seed.ts"), ({exportLocalToPgGenesisSeed}) => params => exportLocalToPgGenesisSeed({ - runtimeRoot: requiredString(params.runtimeRoot, "runtimeRoot"), - goalId: requiredString(params.goalId, "goalId"), - tenantId: optionalString(params.tenantId, "tenantId") ?? undefined, - outputPath: optionalString(params.outputPath, "outputPath") ?? undefined, - databaseUrl: optionalString(params.databaseUrl, "databaseUrl") ?? undefined, - storeIdentity: optionalString(params.storeIdentity, "storeIdentity") ?? undefined, - execute: requireBoolean(params.execute, "execute"), - }))], ["configuration.backup", lazyHandler(() => import("./configuration_backup.ts"), ({configurationBackupOperation}) => configurationBackupOperation)], ["runtime.checkpoint_artifacts", lazyHandler(() => import("./runtime/checkpoint_artifacts.ts"), ({checkpointArtifacts}) => checkpointArtifacts)], ["state.recovery.archive.verify", lazyHandler(() => import("./state_recovery.ts"), ({verifyStateRecoveryArchive}) => verifyStateRecoveryArchive)], diff --git a/loopx/control_plane/testing/authority_e2e_ladder.py b/loopx/control_plane/testing/authority_e2e_ladder.py index 7f078be5c9..f2c27d0028 100644 --- a/loopx/control_plane/testing/authority_e2e_ladder.py +++ b/loopx/control_plane/testing/authority_e2e_ladder.py @@ -2,10 +2,10 @@ One ladder, one row per completed RFC stage claim, one exit policy: the run is green only when every selected row passed. A row that cannot run in this -environment (no PostgreSQL, no NoKV stack, no POSIX signals) reports -``unverified`` and, unless ``--allow-unverified`` is given, the ladder exits -non-zero. Future obligations may be declared as ``pending`` so the report never -silently claims them. +environment (no NoKV stack, no POSIX signals) reports ``unverified`` and, +unless ``--allow-unverified`` is given, the ladder exits non-zero. Future +obligations may be declared as ``pending`` so the report never silently claims +them. Rows drive the product through ``python -m loopx.cli`` (``real_cli``) or run the retained store-level probes (``store_direct``); this module adds no @@ -59,11 +59,10 @@ REPORT_SCHEMA = "loopx_shared_goal_authority_e2e_report_v0" LIST_SCHEMA = "loopx_shared_goal_authority_e2e_rows_v0" -STAGES: tuple[str, ...] = ("0", "1", "2a", "2b") +STAGES: tuple[str, ...] = ("0", "1", "2a") PRODUCT_PATHS: tuple[str, ...] = ("real_cli", "store_direct") GATES: tuple[str, ...] = ( "deterministic", - "env:postgresql", "env:nokv_authority", ) ROW_STATUSES: tuple[str, ...] = ("pass", "fail", "unverified") @@ -72,7 +71,6 @@ "and (unverified == 0 or allow_unverified) and (pending == 0 or allow_pending)" ) -POSTGRES_URL_VARIABLE = "LOOPX_TEST_POSTGRES_URL" # Stage 2A qualification inputs: the probe writes durable test data into an # existing workbench, so it needs an explicit opt-in flag plus the ignored # client configuration file, the Python executable that resolves the qualified @@ -89,11 +87,9 @@ LIVE_OPT_IN_FLAGS: tuple[str, ...] = (NOKV_AUTHORITY_LIVE_FLAG,) GATE_REQUIREMENTS: dict[str, tuple[str, ...]] = { "deterministic": (), - "env:postgresql": (POSTGRES_URL_VARIABLE,), "env:nokv_authority": (NOKV_AUTHORITY_LIVE_FLAG, *NOKV_AUTHORITY_VARIABLES), } GATE_UNVERIFIED_REASON: dict[str, str] = { - "env:postgresql": "postgres_url_missing", "env:nokv_authority": "nokv_authority_env_missing", } @@ -101,9 +97,6 @@ "file": Path("tests/control_plane_ts/authority_store.test.ts"), "sqlite": Path("tests/control_plane_ts/sqlite_authority_store.test.ts"), } -PG_INTEGRATION_TEST = ( - Path("tests") / "control_plane_ts" / "postgresql_authority_store.integration.test.ts" -) NOKV_QUALIFICATION_SCRIPT = Path("examples") / "nokv-authority-store" / "live-qualification.ts" NOKV_HELPER = Path("loopx") / "control_plane" / "coordination" / "nokv_jsonl_helper.py" NOKV_QUALIFICATION_REPORT_SCHEMA = "loopx_nokv_authority_live_qualification_v0" @@ -120,14 +113,12 @@ *LOCAL_CONFORMANCE_TESTS.values(), Path("tests/control_plane_ts/authority_store_conformance.ts"), TS_READBACK_PROBE, - PG_INTEGRATION_TEST, NOKV_QUALIFICATION_SCRIPT, NOKV_HELPER, Path("loopx") / "control_plane" / "testing" / "authority_e2e_ladder.py", Path("loopx") / "control_plane" / "testing" / "authority_e2e_fixtures.py", Path("loopx") / "control_plane" / "testing" / "authority_e2e_row_support.py", ) -MINIMUM_POSTGRES_TAP_PASSES = 9 @dataclass(frozen=True) class LadderRow: id: str @@ -429,57 +420,6 @@ def _row_nokv_live_qualification(context: RowContext) -> RowOutcome: ) -# --------------------------------------------------------------------------- -# Stage 2B: PostgreSQL candidate conformance against a live database -# --------------------------------------------------------------------------- - - -def _pg_package_version() -> str | None: - manifest = REPO_ROOT / "node_modules" / "pg" / "package.json" - if not manifest.exists(): - return None - try: - version = json.loads(manifest.read_text(encoding="utf-8")).get("version") - except (OSError, ValueError, AttributeError): - return None - return str(version) if version else None - - -def _row_postgresql_conformance_live(context: RowContext) -> RowOutcome: - node = node_executable() - if node is None: - return unverified("node_missing") - pg_version = _pg_package_version() - if pg_version is None: - return unverified("pg_dependency_missing") - summary = tap_summary( - [ - node, - "--no-warnings", - "--experimental-strip-types", - "--test", - "--test-reporter=tap", - str(PG_INTEGRATION_TEST), - ], - env=context.environ, - timeout=900, - ) - expect(summary.failed == 0, "PostgreSQL conformance must report zero TAP failures") - expect(summary.skipped == 0, "PostgreSQL conformance must not skip; the URL was not honoured") - expect( - summary.passed is not None and summary.passed >= MINIMUM_POSTGRES_TAP_PASSES, - "PostgreSQL conformance must pass the conformance suite plus provider tests", - ) - expect(summary.returncode == 0, "node test runner must exit 0") - return passed( - tap_pass=summary.passed, - tap_fail=summary.failed, - tap_skipped=summary.skipped, - postgres_url_sha256_prefix=sha256_hex(context.environ.get(POSTGRES_URL_VARIABLE, ""))[:12], - pg_package_version=pg_version, - ) - - # --------------------------------------------------------------------------- # Registry # --------------------------------------------------------------------------- @@ -522,15 +462,6 @@ def _row_postgresql_conformance_live(context: RowContext) -> RowOutcome: posix_only=False, run=_row_nokv_live_qualification, ), - LadderRow( - id="s2b.postgresql_conformance_live", - stage="2b", - title="PostgreSQL candidate passes the conformance suite against a live database", - product_path="store_direct", - gate="env:postgresql", - posix_only=False, - run=_row_postgresql_conformance_live, - ), ) PENDING_ROWS: tuple[PendingRow, ...] = () @@ -572,7 +503,7 @@ def default_forbidden_tokens(roots: Iterable[Path], environ: Mapping[str, str]) tokens.update({str(temp_root), str(temp_root.resolve())}) tokens.add(environ.get("HOME") or str(Path.home())) tokens.update({str(REPO_ROOT), str(REPO_ROOT.resolve())}) - for name in (POSTGRES_URL_VARIABLE, *NOKV_AUTHORITY_VARIABLES): + for name in NOKV_AUTHORITY_VARIABLES: value = environ.get(name) if value: tokens.add(value) @@ -724,15 +655,12 @@ def collect_bindings(environ: Mapping[str, str]) -> JsonObject: commit = _git_output("rev-parse", "HEAD") status = _git_output("status", "--porcelain") - postgres_url = environ.get(POSTGRES_URL_VARIABLE) return { "loopx_commit": commit.strip() if commit else None, "loopx_tree_dirty": bool(status.strip()) if status is not None else None, "probe_sha256": _probe_digests(), "nokv_client_config_sha256": _nokv_client_config_digest(environ), "nokv_sdk_version": _nokv_sdk_version(), - "postgres_url_sha256_prefix": sha256_hex(postgres_url)[:12] if postgres_url else None, - "pg_package_version": _pg_package_version(), } @@ -978,7 +906,6 @@ def main(argv: Sequence[str] | None = None) -> int: "NOKV_AUTHORITY_VARIABLES", "NOKV_AUTHORITY_WORKBENCH_VARIABLE", "PENDING_ROWS", - "POSTGRES_URL_VARIABLE", "PRODUCT_PATHS", "REPORT_SCHEMA", "ROW_STATUSES", diff --git a/loopx/help_surface.py b/loopx/help_surface.py index 1cc28e9ecd..cc219921c2 100644 --- a/loopx/help_surface.py +++ b/loopx/help_surface.py @@ -395,7 +395,6 @@ def register_command_reference(subparsers: argparse._SubParsersAction[argparse.A "ml-experiment", "opencode2-goal-worker", "operator-gate", - "postgres", "pr-review", "promotion-gate", "read-only-map", diff --git a/package-lock.json b/package-lock.json index b931bffe41..b4f31c91bf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,12 +10,10 @@ "license": "Apache-2.0", "devDependencies": { "@types/node": "^24.3.0", - "@types/pg": "^8.23.1", "c8": "^11.0.0", "istanbul-lib-coverage": "^3.2.2", "istanbul-lib-report": "^3.0.1", "istanbul-reports": "^3.2.0", - "pg": "^8.23.0", "typescript": "^6.0.3", "v8-to-istanbul": "^9.3.0" }, @@ -88,18 +86,6 @@ "undici-types": "~7.18.0" } }, - "node_modules/@types/pg": { - "version": "8.23.1", - "resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.23.1.tgz", - "integrity": "sha512-fKVHpikPdg4GKks3JuLEhvwSyvwzF23hnabPy6DD8ljVbC7+6J5dQzdv4arV6jqq57djnMgs1HKBxX4P8aBI3A==", - "dev": true, - "license": "MIT", - "dependencies": { - "@types/node": "*", - "pg-protocol": "*", - "pg-types": "^2.2.0" - } - }, "node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", @@ -529,146 +515,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/pg": { - "version": "8.23.0", - "resolved": "https://registry.npmjs.org/pg/-/pg-8.23.0.tgz", - "integrity": "sha512-Ip2EQCngowJLGOfCwkFhPXU7/ljlhn6Rxlmy4XYfL2Y+vyRM59+8uR2xqRWKdYmbXmxCFOAmKxBuSUCdF34qLg==", - "dev": true, - "license": "MIT", - "dependencies": { - "pg-connection-string": "^2.14.0", - "pg-pool": "^3.14.0", - "pg-protocol": "^1.16.0", - "pg-types": "2.2.0", - "pgpass": "1.0.5" - }, - "engines": { - "node": ">= 16.0.0" - }, - "optionalDependencies": { - "pg-cloudflare": "^1.4.0" - }, - "peerDependencies": { - "pg-native": ">=3.0.1" - }, - "peerDependenciesMeta": { - "pg-native": { - "optional": true - } - } - }, - "node_modules/pg-cloudflare": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.4.0.tgz", - "integrity": "sha512-Vo7z/6rrQYxpNRylp4Tlob2elzbh+N/MOQbxFVWCxS7oEx6jF53GTJFxK2WWpKuBRkmiin4Mt+xofFDjx09R0A==", - "dev": true, - "license": "MIT", - "optional": true - }, - "node_modules/pg-connection-string": { - "version": "2.14.0", - "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.14.0.tgz", - "integrity": "sha512-XwWDGcLRGCXAR8F/AM5bG7Q+A3Wm2s6QeEjlOKZLlH3UYcguiqCWKyWXVag5TLTIjR7oOJUY8kcADaZgWPyLeg==", - "dev": true, - "license": "MIT" - }, - "node_modules/pg-int8": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", - "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==", - "dev": true, - "license": "ISC", - "engines": { - "node": ">=4.0.0" - } - }, - "node_modules/pg-pool": { - "version": "3.14.0", - "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.14.0.tgz", - "integrity": "sha512-gKtPkFdQPU3DksooVLi9LsjZxrsBUZIpa+7aVx+LV5pNh0KzP4Zleud2po+ConrxbuXGBJ6Hfer6hdgpIBpBaw==", - "dev": true, - "license": "MIT", - "peerDependencies": { - "pg": ">=8.0" - } - }, - "node_modules/pg-protocol": { - "version": "1.16.0", - "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.16.0.tgz", - "integrity": "sha512-sILXutLVjCLjcDuOmvhX5e2Z4cS5qG/6Bu3VkpFwdf/633ElGLpEh9bgmuI5I4sqKqkifQiGyiCcx1HdtrK7tg==", - "dev": true, - "license": "MIT" - }, - "node_modules/pg-types": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", - "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", - "dev": true, - "license": "MIT", - "dependencies": { - "pg-int8": "1.0.1", - "postgres-array": "~2.0.0", - "postgres-bytea": "~1.0.0", - "postgres-date": "~1.0.4", - "postgres-interval": "^1.1.0" - }, - "engines": { - "node": ">=4" - } - }, - "node_modules/pgpass": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz", - "integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==", - "dev": true, - "license": "MIT", - "dependencies": { - "split2": "^4.1.0" - } - }, - "node_modules/postgres-array": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", - "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=4" - } - }, - "node_modules/postgres-bytea": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.1.tgz", - "integrity": "sha512-5+5HqXnsZPE65IJZSMkZtURARZelel2oXUEO8rH83VS/hxH5vv1uHquPg5wZs8yMAfdv971IU+kcPUczi7NVBQ==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/postgres-date": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", - "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/postgres-interval": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", - "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "xtend": "^4.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -728,16 +574,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/split2": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz", - "integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==", - "dev": true, - "license": "ISC", - "engines": { - "node": ">= 10.x" - } - }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -864,16 +700,6 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, - "node_modules/xtend": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", - "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.4" - } - }, "node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", diff --git a/package.json b/package.json index 3d457d81b9..f87d381c5a 100644 --- a/package.json +++ b/package.json @@ -9,21 +9,17 @@ }, "scripts": { "test:control-plane": "node --no-warnings --experimental-sqlite --experimental-strip-types --test --test-concurrency=4 tests/control_plane_ts/*.test.ts", - "test:postgresql-authority-service": "node --no-warnings --experimental-strip-types --test tests/control_plane_ts/postgresql_authority_service.test.ts tests/control_plane_ts/postgresql_authority_service.integration.test.ts", - "test:remote-authority": "node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/remote_authority.test.ts tests/control_plane_ts/remote_authority.integration.test.ts", + "test:remote-authority": "node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/remote_authority_client.test.ts", "test:control-plane:coverage": "c8 --all --include=loopx/control_plane/**/*.ts --exclude=loopx/control_plane/**/*.generated.ts --reporter=lcov --reporter=text --reports-dir=coverage/control-plane node --no-warnings --experimental-sqlite --experimental-strip-types --test --test-concurrency=4 tests/control_plane_ts/*.test.ts", "test:dashboard:coverage": "tsc --ignoreConfig --target ES2022 --module ES2022 --moduleResolution Bundler --skipLibCheck --strict --sourceMap --inlineSources --outDir apps/presentation/dashboard/node_modules/.cache/loopx-agent-family apps/presentation/dashboard/src/features/personal-workspace/agent-family.ts && cd apps/presentation/dashboard && npx vite build --ssr smoke/decision-research-surface-coverage.tsx --outDir node_modules/.cache/loopx-decision-research-coverage --emptyOutDir --sourcemap && cd ../../.. && c8 --exclude-node-modules=false --include=apps/presentation/dashboard/node_modules/.cache/loopx-agent-family/agent-family.js --include=apps/presentation/dashboard/node_modules/.cache/loopx-decision-research-coverage/decision-research-surface-coverage.js --reporter=lcov --reporter=text --reports-dir=coverage/dashboard node apps/presentation/dashboard/smoke/dashboard-coverage-runner.mjs", - "test:postgresql-authority-store": "node --no-warnings --experimental-sqlite --experimental-strip-types --test tests/control_plane_ts/postgresql_authority_store.integration.test.ts", "typecheck:control-plane": "tsc --project tsconfig.control-plane.json --noEmit" }, "devDependencies": { "@types/node": "^24.3.0", - "@types/pg": "^8.23.1", "c8": "^11.0.0", "istanbul-lib-coverage": "^3.2.2", "istanbul-lib-report": "^3.0.1", "istanbul-reports": "^3.2.0", - "pg": "^8.23.0", "typescript": "^6.0.3", "v8-to-istanbul": "^9.3.0" } diff --git a/tests/control_plane/test_remote_authority_gateway.py b/tests/control_plane/test_remote_authority_gateway.py index a597f39431..362db7ce19 100644 --- a/tests/control_plane/test_remote_authority_gateway.py +++ b/tests/control_plane/test_remote_authority_gateway.py @@ -59,3 +59,90 @@ def read_local(goal): for key in ("endpoint", "tenant_id", "actor_id", "store_identity", "probe_todo_id"): assert key not in other["remote_authority"] assert cfg["endpoint"] not in other["task_body"] and cfg["tenant_id"] not in other["task_body"] + + +def test_remote_authority_gateway_probe_verifies_against_standard_https_mock(tmp_path, monkeypatch): + import hashlib + import http.server + import shutil + import ssl + import subprocess + import threading + + from loopx.control_plane.coordination.remote_authority_gateway import ( + check_remote_authority_gateway_status, + ) + + if shutil.which("openssl") is None: + pytest.skip("openssl required for disposable TLS certificate") + + key_path = tmp_path / "key.pem" + cert_path = tmp_path / "cert.pem" + subprocess.run( + [ + "openssl", "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", + "-subj", "/CN=localhost", "-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1", + "-keyout", str(key_path), "-out", str(cert_path), + ], + check=True, + capture_output=True, + ) + credential = "synthetic-gateway-credential-" + "a" * 32 + + class _MockHandler(http.server.BaseHTTPRequestHandler): + def log_message(self, _format: str, *_args: object) -> None: + return + + def do_POST(self) -> None: # noqa: N802 + if self.path != "/v0/tasks/execute" or self.headers.get("Authorization") != f"Bearer {credential}": + self.send_response(401) + self.end_headers() + return + length = int(self.headers.get("Content-Length", "0")) + req = json.loads(self.rfile.read(length).decode("utf-8")) + req_digest = hashlib.sha256( + json.dumps({"mode": "execute", "request": req}, sort_keys=True, separators=(",", ":")).encode("utf-8") + ).hexdigest() + payload = { + "schema_version": "loopx_remote_authority_response_v0", + "request_sha256": req_digest, + "status": "found", + "provider_revision": f"{req['store_identity']}:1", + "todo": {"todo_id": req["todo_id"], "status": "open"}, + "lease": None, + } + encoded = json.dumps(payload).encode("utf-8") + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(encoded))) + self.end_headers() + self.wfile.write(encoded) + + server = http.server.HTTPServer(("127.0.0.1", 0), _MockHandler) + ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + ctx.load_cert_chain(certfile=str(cert_path), keyfile=str(key_path)) + server.socket = ctx.wrap_socket(server.socket, server_side=True) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + cfg = { + "enabled": True, + "endpoint": f"https://127.0.0.1:{server.server_port}", + "tenant_id": "synthetic-tenant", + "actor_id": SCOPED_AGENT_ID, + "store_identity": "postgresql:" + "b" * 32, + "probe_todo_id": "todo_probe", + "ca_file": str(cert_path), + } + monkeypatch.setenv("LOOPX_AUTHORITY_CREDENTIAL", credential) + verified = check_remote_authority_gateway_status("full-speed", SCOPED_AGENT_ID, cfg) + assert verified["status"] == "verified" + assert "endpoint" not in verified and "tenant_id" not in verified + + monkeypatch.setenv("LOOPX_AUTHORITY_CREDENTIAL", "wrong-synthetic-credential-" + "b" * 32) + denied = check_remote_authority_gateway_status("full-speed", SCOPED_AGENT_ID, cfg) + assert denied["status"] == "unavailable" + assert denied["reason_code"] == "remote_authority_read_unverified" + finally: + server.shutdown() + server.server_close() diff --git a/tests/control_plane/test_shared_goal_authority_e2e.py b/tests/control_plane/test_shared_goal_authority_e2e.py index 702d308611..8085aaa61e 100644 --- a/tests/control_plane/test_shared_goal_authority_e2e.py +++ b/tests/control_plane/test_shared_goal_authority_e2e.py @@ -19,13 +19,11 @@ LIVE_ENVIRONMENT_VARIABLES = ( - ladder.POSTGRES_URL_VARIABLE, ladder.NOKV_AUTHORITY_LIVE_FLAG, *ladder.NOKV_AUTHORITY_VARIABLES, ) GATED_ROW_IDS = ( "s2a.nokv_live_qualification", - "s2b.postgresql_conformance_live", ) LOCAL_FILE_ROW_ID = "s0.native_file_conformance" FULL_LADDER_VARIABLE = "LOOPX_LADDER_FULL" @@ -74,7 +72,7 @@ def test_registry_vocabulary_and_pending_rows_are_declared_not_claimed() -> None assert len(set(row_ids)) == len(row_ids) assert set(row_ids).isdisjoint(pending_ids) assert pending_ids == [] - assert {row.stage for row in ladder.LADDER_ROWS} == {"0", "1", "2a", "2b"} + assert {row.stage for row in ladder.LADDER_ROWS} == {"0", "1", "2a"} assert all(row.gate in ladder.GATES for row in ladder.LADDER_ROWS) assert all(row.product_path in ladder.PRODUCT_PATHS for row in ladder.LADDER_ROWS) with pytest.raises(ValueError): @@ -107,15 +105,14 @@ def test_main_never_reports_green_while_unverified( assert report["summary"] == { "pass": 0, "fail": 0, - "unverified": 2, + "unverified": 1, "pending": 0, - "executed": 2, + "executed": 1, "privacy_violations": 0, } assert {row["status"] for row in report["rows"]} == {"unverified"} assert {row["reason_code"] for row in report["rows"]} == { "nokv_authority_env_missing", - "postgres_url_missing", } assert report["exit_policy"] == { "allow_unverified": False, @@ -123,7 +120,6 @@ def test_main_never_reports_green_while_unverified( "exit_code": 1, "rule": ladder.EXIT_POLICY_RULE, } - assert report["bindings"]["postgres_url_sha256_prefix"] is None assert report["bindings"]["nokv_client_config_sha256"] is None assert report["bindings"]["loopx_commit"] is None or len(report["bindings"]["loopx_commit"]) == 40 captured = capsys.readouterr() @@ -131,7 +127,7 @@ def test_main_never_reports_green_while_unverified( assert ladder.main([*argv, "--allow-unverified"]) == 0 relaxed = json.loads(report_path.read_text(encoding="utf-8")) - assert relaxed["summary"]["unverified"] == 2 + assert relaxed["summary"]["unverified"] == 1 assert relaxed["exit_policy"]["allow_unverified"] is True assert relaxed["exit_policy"]["exit_code"] == 0 capsys.readouterr() diff --git a/tests/control_plane_ts/authority_archive_postgresql.integration.test.ts b/tests/control_plane_ts/authority_archive_postgresql.integration.test.ts deleted file mode 100644 index 593d09ef58..0000000000 --- a/tests/control_plane_ts/authority_archive_postgresql.integration.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import assert from "node:assert/strict"; -import {randomUUID} from "node:crypto"; -import {mkdtemp, rm} from "node:fs/promises"; -import {tmpdir} from "node:os"; -import {join} from "node:path"; -import test from "node:test"; -import {Pool} from "pg"; -import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; -import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; -import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {installPostgreSqlAuthorityStoreSchema, PostgreSqlAuthorityStore, - type PostgreSqlAuthorityDatabase} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import {exportAuthorityArchive, restoreAuthorityArchive} from "../../loopx/control_plane/coordination/authority_archive.ts"; -import {auditAuthorityArchive} from "../../loopx/control_plane/coordination/authority_archive_audit.ts"; -import {productionScaleCoordinationFixture} from "./production_scale_coordination_fixture.ts"; - -const url = process.env.LOOPX_TEST_POSTGRES_URL; -for (const local of ["file", "sqlite"] as const) { - for (const direction of ["to-postgresql", "from-postgresql"] as const) { - test(`${local} ${direction}: portable complete graph and retained receipts`, {skip: !url}, async () => { - const pool = new Pool({connectionString: url, max: 3}); - const database: PostgreSqlAuthorityDatabase = {connect: async () => { - const client = await pool.connect(); - return {query: (sql, params) => client.query(sql, params ? [...params] : undefined), release: () => client.release()}; - }}; - const root = await mkdtemp(join(tmpdir(), "authority-archive-pg-")); - try { - await installPostgreSqlAuthorityStoreSchema(database, "postgresql:" + "b".repeat(32)); - const options = {tenant_id: `archive-${randomUUID()}`, goal_id: "goal"}; - const pg = new PostgreSqlAuthorityStore(database, options); - const file = local === "file" ? new FileAuthorityStore(join(root, "store"), "goal") - : new SqliteAuthorityStore(join(root, "store"), "goal"); - const [source, target]: [AuthorityStore, AuthorityStore] = direction === "to-postgresql" ? [file, pg] : [pg, file]; - const projection = productionScaleCoordinationFixture("goal", "native").projection; - let previous: string | null = null; - for (let i = 1; i <= 3; i++) { - const committed = await source.commitAuthority({expected_provider_revision: previous, - operation_id: `historical-${i}`, events: [{kind: "observation", i}], - next_projection: {...projection, observation: i}, receipts: [{request: `same-key-${i}`, changed: false}]}); - assert.equal(committed.status, "applied"); - if (committed.status === "applied") previous = committed.provider_revision; - } - const archive = join(root, "archive.ndjson"); - const exported = await exportAuthorityArchive(source, "goal", archive, {pageSize: 1}); - const restored = await restoreAuthorityArchive(archive, target, exported.archive_sha256); - assert.equal(restored.status, "restored"); - const reopened = direction === "to-postgresql" ? new PostgreSqlAuthorityStore(database, options) : target; - assert.equal((await auditAuthorityArchive(archive, reopened, exported.archive_sha256)).status, "matched"); - const rows = await reopened.scanCommitted(null, 10); - assert.equal(rows.status, "page"); - if (rows.status !== "page") throw new Error("readback failed"); - assert.equal(rows.transactions.length, 3); - for (const [index, row] of rows.transactions.entries()) { - assert.deepEqual(row.projection, {...projection, observation: index + 1}); - const receipt = await reopened.readReceipt(`historical-${index + 1}`); - assert.equal(receipt.status, "found"); - if (receipt.status === "found") assert.deepEqual(receipt.receipts, - [{request: `same-key-${index + 1}`, changed: false}]); - } - // Prove the restored native provider can continue its own CAS chain. - assert.equal((await reopened.commitAuthority({expected_provider_revision: restored.target_provider_revision, - operation_id: "isolated-recovery-probe", events: [], receipts: [{probe: true}], - next_projection: {...projection, observation: 4}})).status, "applied"); - await assert.rejects(restoreAuthorityArchive(archive, reopened, exported.archive_sha256), /extra commits/); - assert.equal((await auditAuthorityArchive(archive, reopened, exported.archive_sha256, "retained_prefix")).status, "matched"); - const unchanged = await source.loadAuthority(); - assert.equal(unchanged.status, "loaded"); - if (unchanged.status === "loaded") assert.equal(unchanged.cursor, "3"); - } finally { await pool.end(); await rm(root, {recursive: true, force: true}); } - }); - } -} diff --git a/tests/control_plane_ts/canonical_snapshot_page.test.ts b/tests/control_plane_ts/canonical_snapshot_page.test.ts index cf780cd507..bc28e87b41 100644 --- a/tests/control_plane_ts/canonical_snapshot_page.test.ts +++ b/tests/control_plane_ts/canonical_snapshot_page.test.ts @@ -1,14 +1,11 @@ import assert from "node:assert/strict"; import test from "node:test"; -import {randomUUID} from "node:crypto"; import {mkdtemp, rm, readdir, writeFile, rename} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {sqliteRuntimeIdentity} from "../../loopx/control_plane/coordination/sqlite_runtime.ts"; import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; import {readCanonicalSnapshotFromStore, readCanonicalSnapshotPage, readCanonicalSnapshot, @@ -32,17 +29,6 @@ async function fixture(t: test.TestContext, kind: "file" | "sqlite" = "file") { const Store = kind === "file" ? FileAuthorityStore : SqliteAuthorityStore; return {store: new Store(root, "goal-a"), contender: new Store(root, "goal-a")}; } -async function postgresqlFixture(t: test.TestContext): Promise { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}); - t.after(() => pool.end()); - const database = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - return new PostgreSqlAuthorityStore(database, {tenant_id: `snapshot-${randomUUID()}`, goal_id: "goal-a"}); -} async function verifyLeaseSelection(store: AuthorityStore): Promise { const {projection} = productionScaleCoordinationFixture("goal-a", "native"); await seedSnapshot(store, projection); @@ -107,10 +93,6 @@ for (const provider of (qualifiedSqlite ? ["file", "sqlite"] : ["file"]) as ("fi }); } } -test("postgresql: lease selection preserves matching lease records and binds continuation scope", - {skip: !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { - await verifyLeaseSelection(await postgresqlFixture(t)); - }); if (qualifiedSqlite) { test("selected SQLite snapshot retains the complete independent read contract", async t => { const {store} = await fixture(t, "sqlite"); diff --git a/tests/control_plane_ts/content_digest_single_owner.test.ts b/tests/control_plane_ts/content_digest_single_owner.test.ts index eccc73e8ae..69bc9e4e61 100644 --- a/tests/control_plane_ts/content_digest_single_owner.test.ts +++ b/tests/control_plane_ts/content_digest_single_owner.test.ts @@ -134,7 +134,6 @@ const CANONICAL_CONSUMERS = [ "control_plane/coordination/local_authority_shadow.ts", "control_plane/coordination/local_authority_shadow_outbox.ts", "control_plane/coordination/remote_authority_contract.ts", - "control_plane/coordination/remote_authority_policy.ts", "control_plane/coordination/reviewed_promotion_plan.ts", "control_plane/coordination/runtime_shadow.ts", "control_plane/coordination/shadow_drain_files.ts", diff --git a/tests/control_plane_ts/direction_authority.test.ts b/tests/control_plane_ts/direction_authority.test.ts index 7661cea06e..fb6b8b37f0 100644 --- a/tests/control_plane_ts/direction_authority.test.ts +++ b/tests/control_plane_ts/direction_authority.test.ts @@ -6,8 +6,6 @@ import {join} from "node:path"; import {tmpdir} from "node:os"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {Pool} from "pg"; -import {PostgreSqlAuthorityStore,installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {executeDirectionOperation} from "../../loopx/control_plane/goals/direction_authority.ts"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; @@ -20,16 +18,10 @@ const material={material_id:"contract",purpose:"Preserve the agreed outcome",top boundary:"local_private",source_ref_sha256:sha("source.md"),revision:`sha256:${sha("original material")}`}; function request(operation:string,fields:JsonObject={}):JsonObject{return {operation,goal_ref,source,actor_agent_id:"agent-a", observed_at:"2026-10-13T01:00:00.001Z",dry_run:false,...fields};} -for(const provider of ["file","sqlite","postgresql"]){ - test(`${provider}: acknowledged delivery replays only its own exact source and preserves work`,{skip:provider==="postgresql"&&!process.env.LOOPX_TEST_POSTGRES_URL},async t=>{ +for(const provider of ["file","sqlite"] as const){ + test(`${provider}: acknowledged delivery replays only its own exact source and preserves work`,async t=>{ const folder=await mkdtemp(join(tmpdir(),"direction-owner-"));t.after(()=>rm(folder,{recursive:true,force:true})); - let store:AuthorityStore; - if(provider==="postgresql"){ - const pool=new Pool({connectionString:process.env.LOOPX_TEST_POSTGRES_URL});t.after(()=>pool.end()); - const database={connect:async()=>{const client=await pool.connect();return {query:(sql:string,values?:readonly unknown[])=>client.query(sql,values?[...values]:undefined),release:()=>client.release()};}}; - await installPostgreSqlAuthorityStoreSchema(database,"postgresql:"+"b".repeat(32)); - store=new PostgreSqlAuthorityStore(database,{tenant_id:folder,goal_id:goal_ref.goal_id}); - }else store=provider==="file"?new FileAuthorityStore(folder,goal_ref.goal_id):new SqliteAuthorityStore(folder,goal_ref.goal_id); + const store:AuthorityStore=provider==="file"?new FileAuthorityStore(folder,goal_ref.goal_id):new SqliteAuthorityStore(folder,goal_ref.goal_id); const work={goal_id:goal_ref.goal_id,todos:[{todo_id:"existing",text:"Retain independent work"}],leases:[]}; const initial=await store.commitAuthority({expected_provider_revision:null,operation_id:"seed",events:[],receipts:[],next_projection:work}); assert.equal(initial.status,"applied");if(initial.status!=="applied")throw Error("seed"); diff --git a/tests/control_plane_ts/goal_acceptance_authority.test.ts b/tests/control_plane_ts/goal_acceptance_authority.test.ts index a491260cae..09eda297f0 100644 --- a/tests/control_plane_ts/goal_acceptance_authority.test.ts +++ b/tests/control_plane_ts/goal_acceptance_authority.test.ts @@ -5,12 +5,10 @@ import {tmpdir} from "node:os"; import {join} from "node:path"; import {spawnSync} from "node:child_process"; import test, {type TestContext} from "node:test"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; import {prepareCoordinationProjectionCommit, validateCoordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; import {openLocalAuthorityStore, selectLocalSqliteAuthority} from "../../loopx/control_plane/coordination/local_authority_provider.ts"; @@ -202,37 +200,18 @@ async function update(store: AuthorityStore, todoId: string, patch: JsonObject) mutations: [{kind: "todo_upsert", todo: previous ? {...previous, ...patch} : patch}]})); assert.equal(result.status, "applied"); } -const providers = ["file", "sqlite", "postgresql"] as const; +const providers = ["file", "sqlite"] as const; // Developer runtimes outside the supported Node range may lack the WAL-reset // driver. CI asserts SQLite qualification and runs every provider row. const sqliteQualified = sqliteRuntimeIdentity().sqlite_authority_qualified === true; async function fixture(t: TestContext, provider: typeof providers[number]): Promise { - if (provider !== "postgresql") { - const dir = await mkdtemp(join(tmpdir(), "goal-acceptance-")); - t.after(() => rm(dir, {recursive: true, force: true})); - return provider === "file" ? new FileAuthorityStore(dir, goal) : new SqliteAuthorityStore(dir, goal); - } - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 4}); - const db = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - const tenant = `acceptance-${randomUUID()}`; - t.after(async () => { - try { - for (const table of ["authority_commits", "authority_heads"]) { - await pool.query(`DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1 AND goal_id=$2`, [tenant, goal]); - } - } finally { await pool.end(); } - }); - await installPostgreSqlAuthorityStoreSchema(db, `postgresql:${"b".repeat(32)}`); - return new PostgreSqlAuthorityStore(db, {tenant_id: tenant, goal_id: goal}); + const dir = await mkdtemp(join(tmpdir(), "goal-acceptance-")); + t.after(() => rm(dir, {recursive: true, force: true})); + return provider === "file" ? new FileAuthorityStore(dir, goal) : new SqliteAuthorityStore(dir, goal); } for (const provider of providers) { - const options = {skip: (provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL) || - (provider === "sqlite" && !sqliteQualified)}; + const options = {skip: provider === "sqlite" && !sqliteQualified}; test(`${provider}: a scoped reviewed plan survives unrelated progress with exact native replay`, options, async t => { const store = await fixture(t, provider); await seed(store); const doc = {...document(), scope: {kind: "selected_work", todo_ids: ["todo_first"]}, @@ -988,7 +967,7 @@ test("legacy omitted scope preserves document bytes, bindings and global admissi for (const provider of providers) { test(`${provider}: exact stale-declaration restoration preserves owner authority and lease lineage`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL || provider === "sqlite" && !sqliteQualified}, async t => { + {skip: provider === "sqlite" && !sqliteQualified}, async t => { const {executeCoordinationTodoUpdate} = await import("../../loopx/control_plane/coordination/todo_update.ts"); const store = await fixture(t, provider); const wait = "resume_at:2020-01-01T00:00:00Z"; diff --git a/tests/control_plane_ts/goal_acceptance_runtime.test.ts b/tests/control_plane_ts/goal_acceptance_runtime.test.ts index 7492567866..bf51763be2 100644 --- a/tests/control_plane_ts/goal_acceptance_runtime.test.ts +++ b/tests/control_plane_ts/goal_acceptance_runtime.test.ts @@ -1,16 +1,15 @@ import {compileActionReviewPlan} from "../../loopx/control_plane/presentation/action_review_plan.ts"; import assert from "node:assert/strict"; -import {randomUUID} from "node:crypto"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; import test, {type TestContext} from "node:test"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import type {HandoffMode} from "../../loopx/control_plane/coordination/handoff_mode_vocabulary.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; +import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; +import {sqliteRuntimeIdentity} from "../../loopx/control_plane/coordination/sqlite_runtime.ts"; import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; import {configureGoalAcceptance} from "../../loopx/control_plane/goals/acceptance_authority.ts"; @@ -68,21 +67,12 @@ async function replace(store: AuthorityStore, head: JsonObject, operation_id: st assert.equal((await store.commitAuthority({operation_id, expected_provider_revision: current.provider_revision, next_projection: head, events: [], receipts: []})).status, "applied"); } -async function seeded(t: TestContext, provider: string, state: "bound" | "unbound" | "off", extra: JsonObject = {}, mode: HandoffMode = "soft_claim") { +async function seeded(t: TestContext, provider: "file" | "sqlite", state: "bound" | "unbound" | "off", extra: JsonObject = {}, mode: HandoffMode = "soft_claim") { const root = await mkdtemp(join(tmpdir(), "loopx-acceptance-runtime-")); t.after(() => rm(root, {recursive: true, force: true})); - let store: AuthorityStore; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}); - t.after(() => pool.end()); - const database = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - store = new PostgreSqlAuthorityStore(database, {tenant_id: `acceptance-${randomUUID()}`, goal_id: "goal-a"}); - } else store = new FileAuthorityStore(join(root, "authority", "file-v0"), "goal-a"); + const store: AuthorityStore = provider === "sqlite" + ? new SqliteAuthorityStore(join(root, "authority", "sqlite-v0"), "goal-a") + : new FileAuthorityStore(join(root, "authority", "file-v0"), "goal-a"); const records = [todo(extra)]; const head = {...projection(records, state === "off" ? undefined : acceptance(records, state === "bound")), handoff_mode: mode}; @@ -91,7 +81,8 @@ async function seeded(t: TestContext, provider: string, state: "bound" | "unboun return {store, root, head}; } -for (const provider of ["file", ...(process.env.LOOPX_TEST_POSTGRES_URL ? ["postgresql"] : [])]) { +const sqliteQualified = sqliteRuntimeIdentity().sqlite_authority_qualified === true; +for (const provider of (sqliteQualified ? ["file", "sqlite"] : ["file"]) as ("file" | "sqlite")[]) { test(`${provider}: scoped probe permits an independent claim/lease but preserves ordinary completion validation`, async t => { const {store, head} = await seeded(t, provider, "unbound"); const declaration = {validation_command: null, validation_command_argv: ["true"], diff --git a/tests/control_plane_ts/goal_amendment_authority.test.ts b/tests/control_plane_ts/goal_amendment_authority.test.ts index 4256d6c454..4e1dcddd6f 100644 --- a/tests/control_plane_ts/goal_amendment_authority.test.ts +++ b/tests/control_plane_ts/goal_amendment_authority.test.ts @@ -1,7 +1,5 @@ import assert from "node:assert/strict"; import test from "node:test"; -import {Pool} from "pg"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; @@ -33,20 +31,12 @@ function basis(view: JsonObject): JsonObject { const intent = view.intent as JsonObject; return {base_goal_revision: intent.goal_revision, base_intent_digest: intent.intent_digest, base_work_digest: view.work_digest}; } -for (const provider of ["file", "sqlite", "postgresql"] as const) { +for (const provider of ["file", "sqlite"] as const) { for (const winner of ["commit", "abort_request", "correction"] as const) { - test(`${provider}: one request terminal wins ${winner}, including concurrent retry and lost linkage`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { + test(`${provider}: one request terminal wins ${winner}, including concurrent retry and lost linkage`, async t => { const directory = await mkdtemp(join(tmpdir(), "request-amendment-owner-")); t.after(() => rm(directory, {recursive: true, force: true})); - let store: AuthorityStore; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL}); t.after(() => pool.end()); - const database = {connect: async () => {const client = await pool.connect(); return { - query: (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), release: () => client.release()};}}; - await installPostgreSqlAuthorityStoreSchema(database, "postgresql:" + "b".repeat(32)); - store = new PostgreSqlAuthorityStore(database, {tenant_id: directory, goal_id: goalRef.goal_id}); - } else store = provider === "file" ? new FileAuthorityStore(directory, goalRef.goal_id) : new SqliteAuthorityStore(directory, goalRef.goal_id); + const store: AuthorityStore = provider === "file" ? new FileAuthorityStore(directory, goalRef.goal_id) : new SqliteAuthorityStore(directory, goalRef.goal_id); await store.commitAuthority({expected_provider_revision: null, operation_id: "seed", events: [], receipts: [], next_projection: { goal_id: goalRef.goal_id, todos: [], leases: [], todo_read_model: coordinationTodoReadModel([], TODO_DOMAIN_READ_RECORD_SCHEMA)}}); const initial = await store.loadAuthority(); if (initial.status !== "loaded") throw new Error("missing"); @@ -99,17 +89,9 @@ for (const provider of ["file", "sqlite", "postgresql"] as const) { assert.equal((receipt.receipts as unknown[]).length, 1); }); } - test(`${provider}: governed commit publishes one work item and basis; response loss replays original receipt`, {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { + test(`${provider}: governed commit publishes one work item and basis; response loss replays original receipt`, async t => { const directory = await mkdtemp(join(tmpdir(), "amendment-owner-"));t.after(() => rm(directory, {recursive: true, force: true})); - let store: AuthorityStore; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL});t.after(() => pool.end()); - const database = {connect: async () => { - const client = await pool.connect();return {query: (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, "postgresql:" + "b".repeat(32)); - store = new PostgreSqlAuthorityStore(database, {tenant_id: directory, goal_id: goalRef.goal_id}); - } else store = provider === "file" ? new FileAuthorityStore(directory, goalRef.goal_id) : new SqliteAuthorityStore(directory, goalRef.goal_id); + const store: AuthorityStore = provider === "file" ? new FileAuthorityStore(directory, goalRef.goal_id) : new SqliteAuthorityStore(directory, goalRef.goal_id); const existing = [{schema_version: "todo_domain_record_v0", todo_id: "todo-running", role: "agent", status: "open", done: false, archive_state: "active", text: "Previously authorized work", task_class: "advancement_task"}]; const seed = await store.commitAuthority({expected_provider_revision: null, operation_id: "seed", events: [], receipts: [], next_projection: { diff --git a/tests/control_plane_ts/goal_outcome_capacity.test.ts b/tests/control_plane_ts/goal_outcome_capacity.test.ts index 49db02d02f..ab2f5dbcb0 100644 --- a/tests/control_plane_ts/goal_outcome_capacity.test.ts +++ b/tests/control_plane_ts/goal_outcome_capacity.test.ts @@ -3,12 +3,9 @@ import test from "node:test"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; -import {Pool} from "pg"; -import {randomUUID} from "node:crypto"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {refineGoalOutcome, GoalOutcomeValidationError, projectGoalOutcomes, serializeGoalOutcomes} from "../../loopx/control_plane/goals/outcome_requirements.ts"; import {inspectGoalAcceptance, refineGoalAcceptanceOutcome} from "../../loopx/control_plane/goals/acceptance_authority.ts"; import {authorityProjectionFixture} from "./authority_projection_fixture.ts"; @@ -86,20 +83,9 @@ test("review capacity measures UTF-8 bytes across all retained reviews", () => { assert.equal(refineGoalOutcome(head, goal, null, "author", {outcome_id: "outcome-2", review: fullReview}).outcomes.length, 3); }); -for (const provider of ["file", "sqlite", "postgresql"] as const) test(`${provider}: crowded review survives restart and replay; overflow is atomic`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { +for (const provider of ["file", "sqlite"] as const) test(`${provider}: crowded review survives restart and replay; overflow is atomic`, async t => { const root = await mkdtemp(join(tmpdir(), "outcome-capacity-")); t.after(() => rm(root, {recursive: true, force: true})); - const pool = provider === "postgresql" ? new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}) : null; - if (pool) t.after(() => pool.end()); - const database = pool ? {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }} : null; - if (database) await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - const scope = {tenant_id: `outcome-capacity-${randomUUID()}`, goal_id: goal}; - const open = () => database ? new PostgreSqlAuthorityStore(database, scope) - : provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); + const open = () => provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); const store = open(), head = crowded(); await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, next_projection: head, events: [], receipts: []}); const prior = await inspectGoalAcceptance(store, goal); @@ -197,18 +183,10 @@ test("the aggregate limit counts other outcomes while the source-count limit rem }); }); -// The shared owner must reject before any real PostgreSQL transaction commits. -// Run with the existing isolated integration database, never an active Goal. -test("postgresql: rejected capacity update retains state and recovers once", {skip: !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}); - t.after(() => pool.end()); - const database = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - const store = new PostgreSqlAuthorityStore(database, {tenant_id: `capacity-${randomUUID()}`, goal_id: goal}); +for (const provider of ["file", "sqlite"] as const) test(`${provider}: rejected capacity update retains state and recovers once`, async t => { + const root = await mkdtemp(join(tmpdir(), "outcome-rejected-capacity-")); + t.after(() => rm(root, {recursive: true, force: true})); + const store = provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); const state = declared(); await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, next_projection: {...original, goal_outcomes: state}, events: [], receipts: []}); @@ -266,20 +244,9 @@ function saturated() { const correction = {outcome_id: "outcome-0", storage_format: "records", requirements: [{id: "correction", description: "Preserve continuation and independently check the revised delivery."}], source: {ref: "owner:correction", summary: "An authorized additional acceptance condition"}}; -for (const provider of ["file", "sqlite", "postgresql"] as const) test(`${provider}: explicit record evolution retains a saturated Goal and subsequent adoption`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { +for (const provider of ["file", "sqlite"] as const) test(`${provider}: explicit record evolution retains a saturated Goal and subsequent adoption`, async t => { const root = await mkdtemp(join(tmpdir(), "outcome-records-")); t.after(() => rm(root, {recursive: true, force: true})); - const pool = provider === "postgresql" ? new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}) : null; - if (pool) t.after(() => pool.end()); - const database = pool ? {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }} : null; - if (database) await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - const scope = {tenant_id: `outcome-records-${randomUUID()}`, goal_id: goal}; - const open = () => database ? new PostgreSqlAuthorityStore(database, scope) - : provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); + const open = () => provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); const head = saturated(), original = structuredClone(head), store = open(); await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, next_projection: head, events: [], receipts: []}); const seedRead = await inspectGoalAcceptance(store, goal); diff --git a/tests/control_plane_ts/leased_continuation.test.ts b/tests/control_plane_ts/leased_continuation.test.ts index 1592915407..12dac918db 100644 --- a/tests/control_plane_ts/leased_continuation.test.ts +++ b/tests/control_plane_ts/leased_continuation.test.ts @@ -8,10 +8,8 @@ import {execFile} from "node:child_process"; import {promisify} from "node:util"; import {openLocalAuthorityStore, selectLocalSqliteAuthority} from "../../loopx/control_plane/coordination/local_authority_provider.ts"; import {engageLegacyCoordinationWriterFence} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {configureGoalAcceptance} from "../../loopx/control_plane/goals/acceptance_authority.ts"; import {executeTodoContinuation} from "../../loopx/control_plane/coordination/todo_continuation.ts"; import {executeCanonicalTaskLeaseLifecycle} from "../../loopx/control_plane/coordination/task_lease_lifecycle.ts"; @@ -33,24 +31,11 @@ async function loaded(store: AuthorityStore) { return head; } -async function fixture(t: test.TestContext, provider: string) { +async function fixture(t: test.TestContext, provider: "file" | "sqlite") { const root = await mkdtemp(join(tmpdir(), "leased-continuation-")); t.after(() => rm(root, {recursive: true, force: true})); - let store: AuthorityStore; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 2}); - t.after(() => pool.end()); - const database = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - store = new PostgreSqlAuthorityStore(database, {tenant_id: `continuation-${randomUUID()}`, goal_id: "goal-a"}); - } else { - if (provider === "sqlite") await selectLocalSqliteAuthority(root, "goal-a", true); - store = await openLocalAuthorityStore(root, "goal-a"); - } + if (provider === "sqlite") await selectLocalSqliteAuthority(root, "goal-a", true); + const store: AuthorityStore = await openLocalAuthorityStore(root, "goal-a"); const scale = productionScaleCoordinationFixture("goal-a", "native"); assert.equal((await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, next_projection: scale.projection, events: [], receipts: []})).status, "applied"); @@ -81,9 +66,8 @@ async function patch(store: AuthorityStore, todoId: string, extra: JsonObject) { projection: head.head, mutations: [{kind: "todo_upsert", todo: {...todo, ...extra}}]}))).status, "applied"); } -for (const provider of ["file", "sqlite", "postgresql"]) { - test(`${provider}: leased context crosses claim transfer, durable adoption and current execution readback`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL ? "isolated PostgreSQL URL required" : false}, async t => { +for (const provider of ["file", "sqlite"] as const) { + test(`${provider}: leased context crosses claim transfer, durable adoption and current execution readback`, async t => { const f = await fixture(t, provider); const before = await loaded(f.store); const prepared = await executeTodoContinuation(f.store, f.prepare); @@ -125,8 +109,7 @@ for (const provider of ["file", "sqlite", "postgresql"]) { }); } -for (const provider of ["file", "sqlite", "postgresql"]) test(`${provider}: leased preparation requires exact proof; context cannot inject operational proof`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL ? "isolated PostgreSQL URL required" : false}, async t => { +for (const provider of ["file", "sqlite"] as const) test(`${provider}: leased preparation requires exact proof; context cannot inject operational proof`, async t => { const f = await fixture(t, provider); const before = await loaded(f.store); for (const proof of [null, {...f.sourceProof, expected_version: 999}, {...f.sourceProof, idempotency_key: "wrong"}]) { diff --git a/tests/control_plane_ts/outcome_context_navigation.test.ts b/tests/control_plane_ts/outcome_context_navigation.test.ts index 07ba50deb2..9deb049135 100644 --- a/tests/control_plane_ts/outcome_context_navigation.test.ts +++ b/tests/control_plane_ts/outcome_context_navigation.test.ts @@ -1,8 +1,5 @@ import assert from "node:assert/strict"; import test from "node:test"; -import {randomUUID} from "node:crypto"; -import {Pool} from "pg"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; @@ -96,27 +93,9 @@ for (const kind of ["missing_relation", "unbound_work", "goal_wide", "lifetime"] assert.match(String(result.instruction), /not an automatic full-file read/); }); -for (const provider of ["file", "sqlite", "postgresql"]) test(`${provider}: inspect derives navigation from one actual canonical snapshot without mutations`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { +for (const provider of ["file", "sqlite"] as const) test(`${provider}: inspect derives navigation from one actual canonical snapshot without mutations`, async t => { const root = await mkdtemp(join(tmpdir(), "scope-provider-")); t.after(() => rm(root, {recursive: true, force: true})); - let store; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 4}); - const db = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - const tenant = `outcome-navigation-${randomUUID()}`; - t.after(async () => { - try { - for (const table of ["authority_commits", "authority_heads"]) - await pool.query(`DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1 AND goal_id=$2`, [tenant, goal]); - } finally { await pool.end(); } - }); - await installPostgreSqlAuthorityStoreSchema(db, `postgresql:${"b".repeat(32)}`); - store = new PostgreSqlAuthorityStore(db, {tenant_id: tenant, goal_id: goal}); - } else store = provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); + const store = provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); const seeded = await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, next_projection: fixture(), events: [], receipts: []}); assert.equal(seeded.status, "applied", JSON.stringify(seeded)); diff --git a/tests/control_plane_ts/postgresql_authority_seed.test.ts b/tests/control_plane_ts/postgresql_authority_seed.test.ts deleted file mode 100644 index 5c3fb39c88..0000000000 --- a/tests/control_plane_ts/postgresql_authority_seed.test.ts +++ /dev/null @@ -1,255 +0,0 @@ -import assert from "node:assert/strict"; -import { mkdtemp, readFile, rm } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import test from "node:test"; -import { - exportLocalToPgGenesisSeed, - sanitizeGenesisSeedProjection, - scanForbiddenPrivacyTokens, - setupPostgreSqlAuthoritySchemaAndRole, -} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts"; -import { FileAuthorityStore } from "../../loopx/control_plane/coordination/file_authority_store.ts"; -import type {PostgreSqlAuthorityDatabase, PostgreSqlAuthorityConnection} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; - -test("sanitizeGenesisSeedProjection strips expired leases", () => { - const past = new Date(Date.now() - 3600_000).toISOString(); - const future = new Date(Date.now() + 3600_000).toISOString(); - - const projection = { - schema_version: "loopx_authority_projection_v0", - goal_id: "test-goal", - task_leases: { - leases: { - "lease-expired": { - lease_id: "lease-expired", - holder: "agent-1", - expires_at: past, - }, - "lease-active": { - lease_id: "lease-active", - holder: "agent-2", - expires_at: future, - }, - }, - }, - }; - - const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection); - const leases = (sanitized.task_leases as { leases?: Record })?.leases ?? {}; - assert.equal(leases["lease-expired"], undefined); - assert.ok(leases["lease-active"] !== undefined); - assert.equal(report.leases_stripped, 1); -}); - -test("sanitizeGenesisSeedProjection redacts local paths and .local references", () => { - const projection = { - schema_version: "loopx_authority_projection_v0", - goal_id: "test-goal", - runtime_root: "/Users/alice/Developer/loopx/.local/runtime", - project_root: "/Users/alice/Developer/my-repo", - registered_materials: [ - { - material_id: "mat-1", - path: "/Users/alice/Developer/my-repo/docs/readme.md", - boundary: "public", - }, - { - material_id: "mat-2", - path: "/Users/alice/.local/notes/secret.md", - boundary: "local_private", - }, - ], - }; - - const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection); - // .local path should be redacted - assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/.local")); - assert.ok(!JSON.stringify(sanitized).includes("/Users/alice/Developer")); - // private boundary material must be stripped - const materials = (sanitized.registered_materials as Array<{ material_id: string }>) ?? []; - assert.equal(materials.length, 1); - assert.equal(materials[0].material_id, "mat-1"); - assert.equal(report.private_materials_stripped, 1); - assert.ok(report.paths_redacted > 0); -}); - -test("sanitizeGenesisSeedProjection redacts Feishu IDs", () => { - const feishuUser = "ou_abcdef1234567890abcdef12345678"; - const feishuChat = "oc_1234567890abcdef1234567890abcd"; - const projection = { - schema_version: "loopx_authority_projection_v0", - goal_id: "test-goal", - owner_identity: feishuUser, - channel: { - chat_id: feishuChat, - app_id: "cli_a1b2c3d4e5f6g7h8", - }, - }; - - const { projection: sanitized, report } = sanitizeGenesisSeedProjection(projection); - const serialized = JSON.stringify(sanitized); - assert.ok(!serialized.includes(feishuUser)); - assert.ok(!serialized.includes(feishuChat)); - assert.ok(!serialized.includes("cli_a1b2c3d4e5f6g7h8")); - assert.ok(serialized.includes("[feishu_id_redacted]")); - assert.ok(report.feishu_ids_redacted >= 3); -}); - -test("scanForbiddenPrivacyTokens fails closed on sensitive credentials", () => { - const clean = { - goal_id: "clean-goal", - objective: "Just a clean goal with safe public description", - }; - assert.doesNotThrow(() => scanForbiddenPrivacyTokens(clean)); - - const withSecret = { - goal_id: "leaky-goal", - apiKey: ["sk-", "fixture", "0".repeat(32)].join(""), - }; - assert.throws(() => scanForbiddenPrivacyTokens(withSecret), /Privacy boundary violation/); - - const withGithubToken = { - token: ["ghp", "fixture".repeat(6)].join("_"), - }; - assert.throws(() => scanForbiddenPrivacyTokens(withGithubToken), /Privacy boundary violation/); - - const privateKey = "ou_123456abcdef"; - for (let i = 0; i < 2; i++) { - assert.throws(() => sanitizeGenesisSeedProjection({[privateKey]: {safe: true}}), error => { - assert.ok(error instanceof Error); - return /Privacy boundary violation/.test(error.message) && !error.message.includes(privateKey); - }); - } - assert.throws(() => sanitizeGenesisSeedProjection({[withGithubToken.token]: 1}), /Privacy boundary violation/); - const assignment = ["password", "fixture-only-value"].join("="); - assert.throws(() => sanitizeGenesisSeedProjection({payload: assignment}), /Privacy boundary violation/); -}); - -test("setupPostgreSqlAuthoritySchemaAndRole installs DDL, RLS, and restricted role", async () => { - const executedStatements: string[] = []; - let connects = 0; - const storeId = "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - const connection: PostgreSqlAuthorityConnection = { - query: async (sql: string, params?: readonly unknown[]) => { - executedStatements.push(sql); - if (sql.includes("FROM loopx_control_plane.authority_store_metadata")) { - return { - rows: [{schema_version: "loopx_postgresql_authority_store_v1", store_identity: storeId}], - rowCount: 1, - }; - } - return {rows: [], rowCount: 1}; - }, - release: () => {}, - }; - - const database: PostgreSqlAuthorityDatabase = { - connect: async () => { - connects += 1; - return connection; - }, - }; - - const result = await setupPostgreSqlAuthoritySchemaAndRole(database, { - storeIdentity: storeId, - runtimeRole: "loopx_runtime_worker", - }); - - assert.equal(result.status, "installed"); - assert.equal(result.store_identity, "postgresql:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); - assert.equal(result.runtime_role, "loopx_runtime_worker"); - assert.equal(connects, 1); - - // Verify key DDL statements were executed - assert.ok(executedStatements.some(s => s.includes("CREATE SCHEMA IF NOT EXISTS loopx_control_plane"))); - assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_checkpoints"))); - assert.ok(executedStatements.some(s => s.includes("CREATE TABLE IF NOT EXISTS loopx_control_plane.authority_commits"))); - assert.ok(executedStatements.some(s => s.includes("CREATE ROLE \"loopx_runtime_worker\""))); - assert.ok(executedStatements.some(s => s.includes("GRANT USAGE ON SCHEMA loopx_control_plane TO \"loopx_runtime_worker\""))); - const grants = executedStatements.findIndex(s => - /GRANT SELECT, INSERT ON\s+loopx_control_plane\.authority_checkpoints/.test(s)); - assert.ok(grants > executedStatements.indexOf("BEGIN") && grants < executedStatements.indexOf("COMMIT")); -}); - -test("exportLocalToPgGenesisSeed exports sanitized cursor=1 genesis seed JSON", async () => { - const root = await mkdtemp(join(tmpdir(), "genesis-seed-test-")); - try { - const goalId = "export-goal"; - const storePath = join(root, "goals", goalId, "authority"); - const store = new FileAuthorityStore(storePath, goalId); - - const past = new Date(Date.now() - 3600_000).toISOString(); - const future = new Date(Date.now() + 3600_000).toISOString(); - - const commit1 = await store.commitAuthority({ - operation_id: "op-1", - expected_provider_revision: null, - events: [], - receipts: [], - next_projection: { - schema_version: "loopx_authority_projection_v0", - goal_id: goalId, - leases: [ - { todo_id: "todo-1", expires_at: past, owner: "agent-old" }, - { todo_id: "todo-2", expires_at: future, owner: "agent-active" }, - ], - todos: [ - { - id: "todo-1", - title: "Task with Feishu ID ou_1234567890abcdef1234567890", - required_write_scopes: ["/Users/developer/loopx/packages/core", ".local/temp"], - }, - ], - authority_registry: { - project_materials: {internal: {boundary: "local_private", purpose: "private synthetic fixture"}}, - topic_authority: {internal: ["internal"]}, - }, - goal_direction_scope: {enabled: true}, - material_usage_receipts: [{purpose: "private usage witness"}], - }, - }); - assert.equal(commit1.status, "applied"); - - const outputPath = join(root, "genesis-seed.json"); - const sourceBefore = await store.loadAuthority(); - let attemptedTargetConnection = false; - const result = await exportLocalToPgGenesisSeed({ - runtimeRoot: root, - goalId, - localStore: store, - outputPath, - tenantId: "custom-tenant", - targetDatabase: {connect: async () => { - attemptedTargetConnection = true; - throw new Error("preview must never connect"); - }}, - }); - if (result.status !== "exported") { - console.error("Export failed:", result); - } - - assert.equal(result.status, "exported"); - assert.equal(result.cursor, "1"); - assert.equal(result.goal_id, goalId); - assert.equal(result.tenant_id, "custom-tenant"); - assert.ok(result.seed_digest); - assert.equal(result.sanitization_report?.leases_stripped, 1); - assert.equal(result.sanitization_report?.private_materials_stripped, 1); - assert.equal(attemptedTargetConnection, false); - assert.deepEqual(await store.loadAuthority(), sourceBefore); - - const writtenJson = JSON.parse(await readFile(outputPath, "utf8")); - assert.equal(writtenJson.schema_version, "loopx_pg_genesis_seed_v0"); - assert.equal(writtenJson.cursor, "1"); - assert.equal(writtenJson.projection.leases.length, 1); - assert.equal(writtenJson.projection.leases[0].todo_id, "todo-2"); - assert.ok(!JSON.stringify(writtenJson).includes("ou_1234567890abcdef1234567890")); - assert.ok(!JSON.stringify(writtenJson).includes(".local/temp")); - assert.equal(writtenJson.projection.authority_registry, undefined); - assert.equal(writtenJson.projection.goal_direction_scope, undefined); - assert.equal(writtenJson.projection.material_usage_receipts, undefined); - } finally { - await rm(root, { recursive: true, force: true }); - } -}); diff --git a/tests/control_plane_ts/postgresql_authority_service.integration.test.ts b/tests/control_plane_ts/postgresql_authority_service.integration.test.ts deleted file mode 100644 index 9a09ab7d1c..0000000000 --- a/tests/control_plane_ts/postgresql_authority_service.integration.test.ts +++ /dev/null @@ -1,273 +0,0 @@ -import assert from "node:assert/strict"; -import {randomUUID} from "node:crypto"; -import test from "node:test"; -import {Pool, type PoolClient} from "pg"; - -import { - installPostgreSqlAuthorityStoreSchema, - type PostgreSqlAuthorityConnection, - type PostgreSqlAuthorityDatabase, - PostgreSqlAuthorityStore, - rotatePostgreSqlAuthorityStoreIdentity, -} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import { - PostgreSqlAuthorityService, -} from "../../loopx/control_plane/coordination/postgresql_authority_service.ts"; -import {authorityStoreCommitFixture as commit} from "./authority_store_conformance.ts"; - -const connectionString = process.env.LOOPX_TEST_POSTGRES_SERVICE_URL; -const pool = connectionString ? new Pool({connectionString, max: 4}) : null; -const STORE_IDENTITY = `postgresql:${"c".repeat(32)}`; -const NEXT_STORE_IDENTITY = `postgresql:${"d".repeat(32)}`; - -function databaseFromPool(value: Pool): PostgreSqlAuthorityDatabase { - return { - connect: async () => { - const client: PoolClient = await value.connect(); - const connection: PostgreSqlAuthorityConnection = { - query: async (text, values) => - await client.query(text, values ? [...values] : undefined), - release: (error) => client.release(error), - }; - return connection; - }, - }; -} - -function pauseAfterStoreIdentityRead(database: PostgreSqlAuthorityDatabase): { - database: PostgreSqlAuthorityDatabase; - paused: Promise; - release: () => void; -} { - let reportPaused: (() => void) | undefined; - const paused = new Promise(resolve => { - reportPaused = resolve; - }); - let release: (() => void) | undefined; - const gate = new Promise(resolve => { - release = resolve; - }); - let shouldPause = true; - return { - database: { - connect: async () => { - const connection = await database.connect(); - return { - ...connection, - query: async (text, values) => { - const result = await connection.query(text, values); - if (shouldPause && text.includes("authority_store_metadata")) { - shouldPause = false; - reportPaused?.(); - await gate; - } - return result; - }, - }; - }, - }, - paused, - release: () => release?.(), - }; -} - -function databaseWithTransactionLockTimeout( - database: PostgreSqlAuthorityDatabase, - timeout: string, -): PostgreSqlAuthorityDatabase { - return { - connect: async () => { - const connection = await database.connect(); - return { - ...connection, - query: async (text, values) => { - const result = await connection.query(text, values); - if (text === "BEGIN") { - await connection.query("SELECT set_config('lock_timeout', $1, TRUE)", [timeout]); - } - return result; - }, - }; - }, - }; -} - -async function cleanScope(tenantId: string, goalId: string): Promise { - if (!pool) return; - await pool.query( - `DELETE FROM loopx_control_plane.authority_heads - WHERE tenant_id = $1 AND goal_id = $2`, - [tenantId, goalId], - ); -} - -if (pool) { - const database = databaseFromPool(pool); - const installed = installPostgreSqlAuthorityStoreSchema(database, STORE_IDENTITY); - - test("PostgreSQL commits fence identity rotation without serializing different Goals", async t => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const heldGoalId = `goal-held-${randomUUID()}`; - const parallelGoalId = `goal-parallel-${randomUUID()}`; - t.after(() => Promise.all([ - cleanScope(tenantId, heldGoalId), - cleanScope(tenantId, parallelGoalId), - ])); - - const held = pauseAfterStoreIdentityRead(database); - const heldStore = new PostgreSqlAuthorityStore(held.database, { - tenant_id: tenantId, - goal_id: heldGoalId, - }); - const heldCommitPromise = heldStore.commitAuthority( - commit(null, "held-before-rotation", 1, 1), - ); - await held.paused; - - const boundedDatabase = databaseWithTransactionLockTimeout(database, "500ms"); - let parallelCommit; - let blockedRotation; - try { - const parallelStore = new PostgreSqlAuthorityStore(boundedDatabase, { - tenant_id: tenantId, - goal_id: parallelGoalId, - }); - parallelCommit = await parallelStore.commitAuthority( - commit(null, "parallel-goal", 1, 1), - ); - blockedRotation = await rotatePostgreSqlAuthorityStoreIdentity( - boundedDatabase, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - } finally { - held.release(); - } - const heldCommit = await heldCommitPromise; - - assert.equal(parallelCommit?.status, "applied", JSON.stringify(parallelCommit)); - assert.equal(blockedRotation?.status, "failed", JSON.stringify(blockedRotation)); - if (blockedRotation?.status === "failed") { - assert.equal(blockedRotation.reason_code, "provider_transaction_failed"); - } - assert.equal(heldCommit.status, "applied", JSON.stringify(heldCommit)); - if (heldCommit.status === "applied") { - assert.equal(heldCommit.provider_revision, `${STORE_IDENTITY}:1`); - } - const readback = await new PostgreSqlAuthorityStore(database, { - tenant_id: tenantId, - goal_id: heldGoalId, - }).loadAuthority(); - assert.equal(readback.status, "loaded", JSON.stringify(readback)); - if (readback.status === "loaded") { - assert.equal(readback.provider_revision, `${STORE_IDENTITY}:1`); - } - }); - - test("PostgreSQL service admits an authorized tenant and rotates a restored incarnation", async t => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(() => cleanScope(tenantId, goalId)); - - const service = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: credential => - credential === "fixture-credential" - ? {status: "authenticated", principal: {principal_id: "principal-fixture"}} - : { - status: "rejected", - reason_code: "credential_invalid", - reason: "fixture credential rejected", - }, - authorizeTenant: (principalId, requestedTenant) => - principalId === "principal-fixture" && requestedTenant === tenantId - ? {status: "allowed"} - : { - status: "denied", - reason_code: "tenant_not_granted", - reason: "fixture tenant policy rejected", - }, - }); - - const opened = await service.openStore({ - credential: "fixture-credential", - tenant_id: tenantId, - goal_id: goalId, - store_identity: STORE_IDENTITY, - }); - assert.equal(opened.status, "opened", JSON.stringify(opened)); - if (opened.status !== "opened") return; - const first = await opened.store.commitAuthority(commit(null, "service-operation", 1, 1)); - assert.equal(first.status, "applied", JSON.stringify(first)); - if (first.status !== "applied") return; - - const rotated = await rotatePostgreSqlAuthorityStoreIdentity( - database, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - assert.deepEqual(rotated, { - status: "rotated", - previous_store_identity: STORE_IDENTITY, - store_identity: NEXT_STORE_IDENTITY, - }); - - const stale = await opened.store.commitAuthority( - commit(first.provider_revision, "stale-after-restore", 2, 2), - ); - assert.equal(stale.status, "conflict", JSON.stringify(stale)); - if (stale.status === "conflict") { - assert.equal(stale.conflict_kind, "provider_revision_mismatch"); - assert.equal(stale.current_provider_revision, `${NEXT_STORE_IDENTITY}:1`); - } - assert.equal((await opened.store.readReceipt("service-operation")).status, "found"); - - const oldBinding = await service.openStore({ - credential: "fixture-credential", - tenant_id: tenantId, - goal_id: goalId, - store_identity: STORE_IDENTITY, - }); - assert.equal(oldBinding.status, "rejected"); - if (oldBinding.status === "rejected") { - assert.equal(oldBinding.reason_code, "store_identity_mismatch"); - } - - const newBinding = await service.openStore({ - credential: "fixture-credential", - tenant_id: tenantId, - goal_id: goalId, - store_identity: NEXT_STORE_IDENTITY, - }); - assert.equal(newBinding.status, "opened", JSON.stringify(newBinding)); - if (newBinding.status === "opened") { - const loaded = await newBinding.store.loadAuthority(); - assert.equal(loaded.status, "loaded", JSON.stringify(loaded)); - if (loaded.status === "loaded") { - assert.equal(loaded.provider_revision, `${NEXT_STORE_IDENTITY}:1`); - } - } - - const wrongExpected = await rotatePostgreSqlAuthorityStoreIdentity( - database, - STORE_IDENTITY, - `postgresql:${"e".repeat(32)}`, - ); - assert.equal(wrongExpected.status, "failed"); - if (wrongExpected.status === "failed") { - assert.equal(wrongExpected.reason_code, "store_identity_mismatch"); - } - }); -} else { - test( - "PostgreSQL service real-path integration (set LOOPX_TEST_POSTGRES_SERVICE_URL)", - {skip: true}, - () => {}, - ); -} - -test.after(async () => { - await pool?.end(); -}); diff --git a/tests/control_plane_ts/postgresql_authority_service.test.ts b/tests/control_plane_ts/postgresql_authority_service.test.ts deleted file mode 100644 index d6ef261efa..0000000000 --- a/tests/control_plane_ts/postgresql_authority_service.test.ts +++ /dev/null @@ -1,372 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; - -import { - POSTGRESQL_SCHEMA_VERSION, - POSTGRESQL_STORE_IDENTITY_PATTERN, - rotatePostgreSqlAuthorityStoreIdentity, - type PostgreSqlAuthorityConnection, - type PostgreSqlAuthorityDatabase, -} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import { - PostgreSqlAuthorityService, - type PostgreSqlPrincipalAuthenticationResult, - type PostgreSqlAuthorityServiceOpenRequest, - type PostgreSqlTenantAuthorizationResult, -} from "../../loopx/control_plane/coordination/postgresql_authority_service.ts"; -import { - POSTGRESQL_AUTHORITY_SERVICE_FIXTURE_SCHEMA, - postgresqlAuthorityServiceFixture, -} from "./postgresql_authority_service_fixture.ts"; - -const STORE_IDENTITY = `postgresql:${"a".repeat(32)}`; -const OTHER_STORE_IDENTITY = `postgresql:${"b".repeat(32)}`; -const TENANT_ID = "tenant-service-fixture"; -const GOAL_ID = "goal-service-fixture"; - -function metadataDatabase( - storeIdentity: string, - options: {onConnect?: () => void; onQuery?: (text: string) => void} = {}, -): PostgreSqlAuthorityDatabase { - return { - connect: async () => { - options.onConnect?.(); - const connection: PostgreSqlAuthorityConnection = { - query: async (text) => { - options.onQuery?.(text); - if (text.includes("authority_store_metadata")) { - return { - rows: [{ - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: storeIdentity, - }], - rowCount: 1, - }; - } - throw new Error(`unexpected PostgreSQL service query: ${text}`); - }, - release: () => {}, - }; - return connection; - }, - }; -} - -function principalResult( - principalId = "principal-service-fixture", -): PostgreSqlPrincipalAuthenticationResult { - return {status: "authenticated", principal: {principal_id: principalId}}; -} - -function allowedTenant(): PostgreSqlTenantAuthorizationResult { - return {status: "allowed"}; -} - -test("PostgreSQL service fixture is public-safe and versioned", () => { - assert.equal( - postgresqlAuthorityServiceFixture.schema_version, - POSTGRESQL_AUTHORITY_SERVICE_FIXTURE_SCHEMA, - ); - assert.equal(postgresqlAuthorityServiceFixture.source_authority, "postgresql_v0"); - assert.equal(postgresqlAuthorityServiceFixture.credential_persistence, "forbidden"); - assert.equal(postgresqlAuthorityServiceFixture.agent_database_access, "forbidden"); - assert.deepEqual( - postgresqlAuthorityServiceFixture.principal_cases.map(item => item.id), - ["authenticated-tenant", "unauthenticated", "tenant-denied", "identity-drift"], - ); -}); - -test("PostgreSQL service rejects malformed requests before authentication", async () => { - let authenticationCalls = 0; - const service = new PostgreSqlAuthorityService({ - database: metadataDatabase(STORE_IDENTITY), - authenticatePrincipal: () => { - authenticationCalls += 1; - return principalResult(); - }, - authorizeTenant: allowedTenant, - }); - - const result = await service.openStore({ - credential: {secret: "opaque"}, - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: "postgresql:not-an-incarnation", - }); - assert.equal(result.status, "rejected"); - if (result.status === "rejected") assert.equal(result.reason_code, "invalid_service_request"); - assert.equal(authenticationCalls, 0); -}); - -test("PostgreSQL service authenticates and authorizes before opening the provider", async () => { - let connectionAttempts = 0; - let seenCredential: unknown; - let authorizedPrincipal: string | null = null; - let authorizedTenant: string | null = null; - const credential = {kind: "opaque", token: "never-persist"}; - const service = new PostgreSqlAuthorityService({ - database: metadataDatabase(STORE_IDENTITY, {onConnect: () => connectionAttempts += 1}), - authenticatePrincipal: received => { - seenCredential = received; - return principalResult(); - }, - authorizeTenant: (principalId, tenantId) => { - authorizedPrincipal = principalId; - authorizedTenant = tenantId; - return allowedTenant(); - }, - }); - - const result = await service.openStore({ - credential, - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: STORE_IDENTITY, - }); - assert.equal(result.status, "opened"); - if (result.status !== "opened") return; - assert.equal(result.provider, "postgresql"); - assert.equal(result.principal_id, "principal-service-fixture"); - assert.equal(result.store_identity, STORE_IDENTITY); - assert.strictEqual(seenCredential, credential); - assert.equal(authorizedPrincipal, "principal-service-fixture"); - assert.equal(authorizedTenant, TENANT_ID); - assert.equal(connectionAttempts, 1); -}); - -test("PostgreSQL service fails closed for authentication, tenant, and identity drift", async () => { - let connectionAttempts = 0; - const database = metadataDatabase(STORE_IDENTITY, {onConnect: () => connectionAttempts += 1}); - - const unauthenticated = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => ({ - status: "rejected", - reason_code: "credential_invalid", - reason: "synthetic credential is invalid", - }), - authorizeTenant: allowedTenant, - }); - const rejectedPrincipal = await unauthenticated.openStore({ - credential: "invalid", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: STORE_IDENTITY, - }); - assert.deepEqual( - rejectedPrincipal, - { - status: "rejected", - reason_code: "principal_unauthenticated", - reason: "PostgreSQL service principal authentication was rejected", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - }, - ); - assert.equal(connectionAttempts, 0); - - const denied = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => principalResult(), - authorizeTenant: () => ({ - status: "denied", - reason_code: "tenant_not_granted", - reason: "synthetic tenant policy denied the request", - }), - }); - const rejectedTenant = await denied.openStore({ - credential: "valid", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: STORE_IDENTITY, - }); - assert.equal(rejectedTenant.status, "rejected"); - if (rejectedTenant.status === "rejected") { - assert.equal(rejectedTenant.reason_code, "tenant_unauthorized"); - assert.equal(rejectedTenant.principal_id, "principal-service-fixture"); - } - assert.equal(connectionAttempts, 0); - - const drifted = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => principalResult(), - authorizeTenant: allowedTenant, - }); - const rejectedIdentity = await drifted.openStore({ - credential: "valid", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: OTHER_STORE_IDENTITY, - }); - assert.equal(rejectedIdentity.status, "rejected"); - if (rejectedIdentity.status === "rejected") { - assert.equal(rejectedIdentity.reason_code, "store_identity_mismatch"); - assert.equal(rejectedIdentity.principal_id, "principal-service-fixture"); - } - assert.equal(connectionAttempts, 1); -}); - -test("PostgreSQL service rejects malformed memberships before opening the provider", async () => { - let connectionAttempts = 0; - const database = metadataDatabase(STORE_IDENTITY, { - onConnect: () => connectionAttempts += 1, - }); - const request = { - credential: {kind: "opaque", token: "never-persist"}, - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: STORE_IDENTITY, - }; - - const malformed = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => principalResult(), - authorizeTenant: allowedTenant, - }); - const nullRequest = await malformed.openStore( - null as unknown as PostgreSqlAuthorityServiceOpenRequest, - ); - assert.deepEqual(nullRequest, { - status: "rejected", - reason_code: "invalid_service_request", - reason: "PostgreSQL service request is invalid", - }); - const untrimmedTenant = await malformed.openStore({ - ...request, - tenant_id: ` ${TENANT_ID}`, - }); - assert.equal(untrimmedTenant.status, "rejected"); - if (untrimmedTenant.status === "rejected") { - assert.equal(untrimmedTenant.reason_code, "invalid_service_request"); - assert.equal(untrimmedTenant.reason, "tenant id must be a non-empty trimmed string"); - } - - const unverified = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => { - throw new Error("synthetic verifier outage"); - }, - authorizeTenant: allowedTenant, - }); - assert.deepEqual(await unverified.openStore(request), { - status: "rejected", - reason_code: "principal_verification_unavailable", - reason: "PostgreSQL service could not verify the principal", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - }); - - const invalidPrincipal = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => principalResult(""), - authorizeTenant: allowedTenant, - }); - assert.deepEqual(await invalidPrincipal.openStore(request), { - status: "rejected", - reason_code: "principal_unauthenticated", - reason: "PostgreSQL service returned an invalid authenticated principal", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - }); - - const unavailableAuthorization = new PostgreSqlAuthorityService({ - database, - authenticatePrincipal: () => principalResult(), - authorizeTenant: () => { - throw new Error("synthetic authorization outage"); - }, - }); - assert.deepEqual(await unavailableAuthorization.openStore(request), { - status: "rejected", - reason_code: "tenant_authorization_unavailable", - reason: "PostgreSQL service could not authorize the tenant", - principal_id: "principal-service-fixture", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - }); - assert.equal(connectionAttempts, 0); -}); - -test("PostgreSQL service treats provider metadata failures as unavailable", async () => { - const service = new PostgreSqlAuthorityService({ - database: { - connect: async () => { - throw new Error("synthetic unavailable provider"); - }, - }, - authenticatePrincipal: () => principalResult(), - authorizeTenant: allowedTenant, - }); - const result = await service.openStore({ - credential: "valid", - tenant_id: TENANT_ID, - goal_id: GOAL_ID, - store_identity: STORE_IDENTITY, - }); - assert.equal(result.status, "rejected"); - if (result.status === "rejected") assert.equal(result.reason_code, "store_identity_unavailable"); -}); - -test("PostgreSQL identity rotation reports a lost COMMIT response as ambiguous", async () => { - let releasedWith: Error | undefined; - const database: PostgreSqlAuthorityDatabase = { - connect: async () => { - const connection: PostgreSqlAuthorityConnection = { - query: async text => { - if ( - text === "BEGIN" || - text.includes("pg_advisory_xact_lock") || - text.includes("UPDATE loopx_control_plane.authority_store_metadata") - ) { - return {rows: [], rowCount: 1}; - } - if (text.includes("authority_store_metadata")) { - return { - rows: [{ - schema_version: POSTGRESQL_SCHEMA_VERSION, - store_identity: STORE_IDENTITY, - }], - rowCount: 1, - }; - } - if (text === "COMMIT") throw new Error("synthetic response loss after COMMIT"); - throw new Error(`unexpected PostgreSQL rotation query: ${text}`); - }, - release: error => { - releasedWith = error; - }, - }; - return connection; - }, - }; - - const result = await rotatePostgreSqlAuthorityStoreIdentity( - database, - STORE_IDENTITY, - OTHER_STORE_IDENTITY, - ); - assert.deepEqual(result, { - status: "ambiguous", - reason_code: "store_identity_rotation_outcome_unknown", - reason: "PostgreSQL identity rotation outcome is unknown; read store metadata before retrying", - }); - assert.match(releasedWith?.message ?? "", /response loss/); -}); - -test("PostgreSQL service identity format stays provider-specific", () => { - assert.equal(POSTGRESQL_STORE_IDENTITY_PATTERN.test(STORE_IDENTITY), true); - assert.equal(POSTGRESQL_STORE_IDENTITY_PATTERN.test(OTHER_STORE_IDENTITY), true); - assert.equal(POSTGRESQL_STORE_IDENTITY_PATTERN.test("sqlite:abc"), false); -}); - -const realPathUrl = process.env.LOOPX_TEST_POSTGRES_URL; -if (realPathUrl) { - test("PostgreSQL service real-path integration (set LOOPX_TEST_POSTGRES_URL)", async () => { - // The disposable PostgreSQL integration is intentionally kept in the - // provider integration suite; this test documents the required real-path - // command without sharing mutable singleton metadata with that suite. - assert.match(realPathUrl, /^postgres(?:ql)?:\/\//); - }); -} else { - test("PostgreSQL service real-path integration (set LOOPX_TEST_POSTGRES_URL)", {skip: true}, () => {}); -} diff --git a/tests/control_plane_ts/postgresql_authority_service_fixture.ts b/tests/control_plane_ts/postgresql_authority_service_fixture.ts deleted file mode 100644 index 3353b0b6a4..0000000000 --- a/tests/control_plane_ts/postgresql_authority_service_fixture.ts +++ /dev/null @@ -1,32 +0,0 @@ -import {readFileSync} from "node:fs"; - -const envelope = JSON.parse(readFileSync(new URL( - "../fixtures/control_plane/postgresql_authority_service_v0.json", - import.meta.url, -), "utf8")) as PostgreSqlAuthorityServiceFixture; - -export const POSTGRESQL_AUTHORITY_SERVICE_FIXTURE_SCHEMA = - "loopx_postgresql_authority_service_fixture_v0"; - -export interface PostgreSqlAuthorityServiceFixtureCase { - readonly id: string; - readonly credential_kind?: "opaque"; - readonly expected: "opened" | "rejected" | "rotated" | "failed"; - readonly reason_code?: string; -} - -export interface PostgreSqlAuthorityServiceFixture { - readonly schema_version: string; - readonly source_authority: "postgresql_v0"; - readonly principal_cases: readonly PostgreSqlAuthorityServiceFixtureCase[]; - readonly rotation_cases: readonly PostgreSqlAuthorityServiceFixtureCase[]; - readonly credential_persistence: "forbidden"; - readonly agent_database_access: "forbidden"; -} - -if (envelope.schema_version !== POSTGRESQL_AUTHORITY_SERVICE_FIXTURE_SCHEMA) { - throw new Error("PostgreSQL authority service fixture schema version is invalid"); -} - -export const postgresqlAuthorityServiceFixture: PostgreSqlAuthorityServiceFixture = - structuredClone(envelope); diff --git a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts b/tests/control_plane_ts/postgresql_authority_store.integration.test.ts deleted file mode 100644 index 7d5d7d1bbf..0000000000 --- a/tests/control_plane_ts/postgresql_authority_store.integration.test.ts +++ /dev/null @@ -1,1013 +0,0 @@ -import {executeCoordinationTodoUpdate} from "../../loopx/control_plane/coordination/todo_update.ts"; -import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; -import {TODO_DOMAIN_READ_RECORD_SCHEMA, TODO_DOMAIN_RECORD_CONTRACT} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; -import assert from "node:assert/strict"; -import { createHash, randomUUID } from "node:crypto"; -import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; -import { join } from "node:path"; -import { tmpdir } from "node:os"; -import test from "node:test"; -import { Pool, type PoolClient } from "pg"; - -import { - DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES, - installPostgreSqlAuthorityStoreSchema, - POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL, - PostgreSqlAuthorityStore, - type PostgreSqlAuthorityConnection, - type PostgreSqlAuthorityDatabase, -} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import { openLocalAuthorityStoreHandle } from "../../loopx/control_plane/coordination/local_authority_provider.ts"; -import {readLocalCoordinationTodoSource} from "../../loopx/control_plane/coordination/local_authority_read.ts"; -import {CANONICAL_SNAPSHOT_PAGE_REQUEST, readCanonicalSnapshotFromStore, readCanonicalSnapshotPage} from "../../loopx/control_plane/coordination/canonical_snapshot_page.ts"; -import {projectTodoContextPage} from "../../loopx/control_plane/todos/context_projection.ts"; -import {readCanonicalTodoProjection, readCanonicalQuotaProjection} from "../../loopx/control_plane/todos/read_transaction.ts"; -import {canonicalReadRoot} from "./local_promotion_fixture.ts"; -import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; -import {inspectGoalAcceptance, refineGoalAcceptanceOutcome} from "../../loopx/control_plane/goals/acceptance_authority.ts"; -import {authorityProjectionFixture} from "./authority_projection_fixture.ts"; -import {exportLocalToPgGenesisSeed, setupPostgreSqlAuthoritySchemaAndRole} from "../../loopx/control_plane/coordination/postgresql_authority_seed.ts"; -import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import { - authorityStoreCommitFixture as commit, - registerAuthorityStoreConformance, -} from "./authority_store_conformance.ts"; - -const connectionString = process.env.LOOPX_TEST_POSTGRES_URL; -const pool = connectionString ? new Pool({ connectionString, max: 12 }) : null; -const STORE_IDENTITY = `postgresql:${"b".repeat(32)}`; -const TENANT_SCOPED_TABLES = [ - "authority_heads", - "authority_checkpoints", - "authority_commits", - "authority_events", - "authority_receipts", -] as const; - -function wrapClient(client: PoolClient): PostgreSqlAuthorityConnection { - return { - query: async (text, values) => await client.query(text, values ? [...values] : undefined), - release: (error) => client.release(error), - }; -} - -function databaseFromPool(value: Pool): PostgreSqlAuthorityDatabase { - return { connect: async () => wrapClient(await value.connect()) }; -} - -function quotedRole(role: string): string { - assert.match(role, /^[a-z][a-z0-9_]+$/); - return `"${role}"`; -} - -async function createRuntimeRole(role: string): Promise { - const result = await setupPostgreSqlAuthoritySchemaAndRole(database!, { - storeIdentity: STORE_IDENTITY, - runtimeRole: role, - }); - assert.equal(result.status, "installed", result.reason); -} - -function databaseForRuntimeRole(value: Pool, role: string): PostgreSqlAuthorityDatabase { - const identifier = quotedRole(role); - return { - connect: async () => { - const client = await value.connect(); - await client.query("RESET ROLE"); - await client.query(`SET ROLE ${identifier}`); - return { - query: async (text, values) => - await client.query(text, values ? [...values] : undefined), - release: async (error) => { - try { - await client.query("RESET ROLE"); - client.release(error); - } catch (resetError) { - client.release(resetError as Error); - } - }, - }; - }, - }; -} - -async function queryAsRuntimeRole( - value: Pool, - role: string, - operation: (client: PoolClient) => Promise, -): Promise { - const client = await value.connect(); - try { - await client.query(`SET ROLE ${quotedRole(role)}`); - await operation(client); - } finally { - await client.query("RESET ROLE"); - client.release(); - } -} - -const database = pool ? databaseFromPool(pool) : null; -const installed = database - ? installPostgreSqlAuthorityStoreSchema(database, STORE_IDENTITY) - : null; - -async function cleanScope(tenantId: string, goalId: string): Promise { - if (!pool) return; - await pool.query( - `DELETE FROM loopx_control_plane.authority_heads - WHERE tenant_id = $1 AND goal_id = $2`, - [tenantId, goalId], - ); -} - -if (database && installed) { - test("PostgreSQL canonical quota transaction binds one provider read and complete caller differences", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const root = await canonicalReadRoot(t, options.goal_id); - const store = new PostgreSqlAuthorityStore(database, options); - const projection = authorityProjectionFixture(options.goal_id, [{todo_id: "todo_native_quota", role: "agent", - status: "open", done: false, archive_state: "active", text: "Deliver the complete required artifact", priority: "P1", - task_class: "advancement_task", required_write_scopes: ["output.json"], note: "Retained source note", evidence: "artifact:retained"}]); - const seeded = await store.commitAuthority({operation_id: "quota-native-seed", expected_provider_revision: null, - next_projection: projection, events: [], receipts: []}); - assert.equal(seeded.status, "applied"); - let loads = 0; - const observed = new Proxy(store, {get(target, property) { - if (property === "loadAuthority") return async () => {loads++; return target.loadAuthority();}; - const value = Reflect.get(target, property); - return typeof value === "function" ? value.bind(target) : value; - }}); - const dependencies = {createStore: () => observed}; - const read = {schema_version: "todo_read_request_v0", kind: "canonical_fields", - source: {runtime_root: root, goal_id: options.goal_id, include_leases: false, projection_readback: null}, - observed_at: 1790812800, evaluated_at: "2026-10-01T00:00:00Z", rollout_events: []}; - const initial = await readCanonicalTodoProjection(read, dependencies); - const fields = initial.fields as JsonObject, lineage = initial.source_projection as JsonObject; - const request = (field: string, changes: JsonObject[], displays: boolean): JsonObject => ({ - schema_version: "todo_quota_planning_request_v4", current_time: "2026-10-01T00:00:00Z", - summary_request: {kind: "user", materialize_source: true, materialize_resume: true, consume_display_views: displays, - source_reference: {read, expected_source: lineage.source, field, field_sha256: (lineage.field_digests as JsonObject)[field], changes, python_values: []}, - integer_counts: {source: null, warning: null}}, - source_contract: fields[field], handoff_items: [], route_items: [], - resume_options: {agent_id: "agent-a", item_limit: 8, available_capabilities: []}, - selection: {observed_at: 1790812800, available: [], agent_id: "agent-a", profile: null, user_gate_scope: field === "user_todos", - monitor_supported: false, items: [], active_items: [], active_executable_items: [], source_open_count: 1, source_complete: true, - diagnostic_limit: 3, backlog_limit: 8, visibility_limit: 16}, - }); - const requests = [request("user_todos", [], false), request("agent_todos", [{path: ["vision_wait_states"], value: [{agent_id: "agent-a", state: "waiting"}]}], true)]; - loads = 0; - const result = await readCanonicalQuotaProjection({requests}, dependencies); - assert.equal(loads, 1, "both role requests share this one native provider read only"); - const wires = result.quota_wires as JsonObject[]; - assert.equal(wires[0]!.display_views, undefined, "the User caller did not request three displays"); - const views = wires[1]!.display_views as JsonObject; - assert.equal((views.terminal as JsonObject[])[0]!.note, "Retained source note"); - assert.deepEqual((views.summary as JsonObject[])[0]!.required_write_scopes, ["output.json"]); - const forged = structuredClone(requests); - ((forged[1]!.summary_request as JsonObject).source_reference as JsonObject).changes = [{path: ["items", 0, "note"], value: "Forged source fact"}]; - await assert.rejects(readCanonicalQuotaProjection({requests: forged}, dependencies), /canonical Todo facts/); - const agentField = fields.agent_todos as JsonObject; - const sourceItem = (agentField.items as JsonObject[])[0]!; - for (const changes of [ - [{path: ["items", 0, "required_write_scopes", 0], value: "different.json"}], - [{path: ["open_count"], value: Number(agentField.open_count) + 1}], - [{path: ["items"], delete: true}], - ]) { - const changed = structuredClone(requests); - ((changed[1]!.summary_request as JsonObject).source_reference as JsonObject).changes = changes; - await assert.rejects(readCanonicalQuotaProjection({requests: changed}, dependencies), /canonical Todo facts/); - } - // Source protection compares the final complete value. Temporary changes - // that are restored, including a parent replacement and deletion, retain - // the same read result and never mutate provider data. - const restored = structuredClone(requests); - const originalChanges = ((requests[1]!.summary_request as JsonObject).source_reference as JsonObject).changes as JsonObject[]; - ((restored[1]!.summary_request as JsonObject).source_reference as JsonObject).changes = [ - {path: ["items"], value: structuredClone(agentField.items)}, - {path: ["items", 0, "note"], delete: true}, - {path: ["items", 0, "note"], value: sourceItem.note}, - ...structuredClone(originalChanges), - ]; - assert.deepEqual(await readCanonicalQuotaProjection({requests: restored}, dependencies), result); - const untouched = await store.loadAuthority(); - assert.equal(untouched.status, "loaded"); - if (untouched.status !== "loaded") throw new Error("synthetic provider disappeared"); - assert.deepEqual(untouched.head, projection); - const changed = await store.commitAuthority({operation_id: "quota-native-source-change", expected_provider_revision: untouched.provider_revision, - next_projection: authorityProjectionFixture(options.goal_id, (projection.todos as JsonObject[]).map(row => ({...row, text: "A new requirement"}))), events: [], receipts: []}); - assert.equal(changed.status, "applied"); - await assert.rejects(readCanonicalQuotaProjection({requests}, dependencies), /canonical source changed/); - }); - test("PostgreSQL planning reads the Agent declaration and work links from the canonical head", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const projection = authorityProjectionFixture(options.goal_id, [{todo_id: "todo_plan", role: "agent", - status: "open", done: false, archive_state: "active", text: "Implement the requested outcome", - task_class: "advancement_task", claimed_by: "author"}]); - await store.commitAuthority({expected_provider_revision: null, operation_id: "seed-planning", - events: [], receipts: [], next_projection: projection}); - const inspect = () => inspectGoalAcceptance(store, options.goal_id, undefined, undefined, false, "author"); - const before = await inspect(); - assert.equal((before.planning_requirements as JsonObject).declaration_current, false); - const result = await refineGoalAcceptanceOutcome(store, {goal_id: options.goal_id, - actor_agent_id: "author", expected_provider_revision: before.provider_revision, operation_id: "declare-planning", - refinement: {outcome_id: "requested", owner_agent_id: "author", objective: "Deliver the requested outcome", - requirements: [{id: "complete", description: "Complete the requested implementation"}], - todo_ids: ["todo_plan"], source: {ref: "message:request", summary: "The owner requested this implementation"}}}); - assert.equal(result.status, "applied"); - const after = await inspect(); - const loaded = await store.loadAuthority(); - assert.equal(loaded.status, "loaded"); - if (loaded.status !== "loaded") return; - assert.equal(after.provider_revision, loaded.provider_revision); - assert.deepEqual(after.planning_requirements, {agent_id: "author", declaration_current: true, - outcome_ids: ["requested"], linked_todo_ids: ["todo_plan"]}); - assert.deepEqual((after.goal_acceptance_contract as JsonObject).criteria ?? [], [], "Independent executable checks remain optional"); - }); - - test("PostgreSQL historical Todo source retains the original frontier", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const todos = [{schema_version: "todo_domain_record_v0", todo_id: "todo-source", role: "agent", - status: "open", done: false, archive_state: "active", text: "Synthetic retained work", - title: "Synthetic retained work", priority: "P2", task_class: "advancement_task"}]; - const projection = (rows: typeof todos) => ({goal_id: options.goal_id, todos: rows, leases: [], - todo_read_model: coordinationTodoReadModel(rows, TODO_DOMAIN_READ_RECORD_SCHEMA)}); - const first = await store.commitAuthority({expected_provider_revision: null, - operation_id: "source-seed", events: [], receipts: [], next_projection: projection(todos)}); - assert.equal(first.status, "applied"); - if (first.status !== "applied") return; - const identity = await store.storeIdentity(); - assert.equal(identity.status, "available"); - if (identity.status !== "available") return; - const source = {source_authority: "postgresql_v0", store_identity: identity.store_identity, - provider_revision: first.provider_revision, cursor: first.cursor}; - const later = await store.commitAuthority({expected_provider_revision: first.provider_revision, - operation_id: "source-later", events: [], receipts: [], - next_projection: projection(todos.map(row => ({...row, status: "done", done: true})))}); - assert.equal(later.status, "applied"); - const request = {schema_version: "loopx_local_coordination_todo_source_request_v0", - runtime_root: tmpdir(), goal_id: options.goal_id, source}; - const historical = await readLocalCoordinationTodoSource(request, {createStore: () => store}); - assert.equal(historical.status, "loaded", JSON.stringify(historical)); - assert.equal((historical.todos as Record[])[0]?.status, "open"); - assert.equal(historical.provider_revision, first.provider_revision); - assert.equal((await readLocalCoordinationTodoSource({...request, - source: {...source, store_identity: "foreign"}}, {createStore: () => store})).reason_code, - "todo_source_lineage_mismatch"); - }); - - test("PostgreSQL snapshot reports a rejected field contract without changing authority", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const projection = authorityProjectionFixture(options.goal_id, []); - ((projection.todo_read_model as JsonObject).contract_fields as string[]).push("future_field"); - const committed = await store.commitAuthority({expected_provider_revision: null, - operation_id: "seed-unsupported-contract", events: [], receipts: [], next_projection: projection}); - assert.equal(committed.status, "applied"); - const before = await store.loadAuthority(); - const result = await readCanonicalSnapshotPage({schema_version: CANONICAL_SNAPSHOT_PAGE_REQUEST, - runtime_root: await canonicalReadRoot(t, options.goal_id), goal_id: options.goal_id, - include_leases: true, projection_readback: null, after: null}, {createStore: () => store}); - assert.equal(result.status, "failed"); - assert.equal(result.reason_code, "canonical_snapshot_projection_invalid"); - assert.match(String(result.reason), /field contract mismatch/); - assert.equal(result.legacy_fallback_used, false); - assert.equal("todos" in result, false); - assert.deepEqual(await store.loadAuthority(), before); - }); - - test("PostgreSQL canonical snapshot feeds complete display views without becoming a fresh authority read", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const retainedNote = "Keep every acceptance clause.\nRecover the original artifact and settlement identity."; - const todos = [ - {todo_id: "todo_01_open", role: "agent", status: "open", done: false, archive_state: "active", - text: "Implement the requested outcome", task_class: "advancement_task", - required_write_scopes: ["result.json"], note: retainedNote}, - {todo_id: "todo_02_blocker", role: "agent", status: "blocked", done: false, archive_state: "active", - text: "Wait for the required input", task_class: "blocker", claimed_by: "author"}, - {todo_id: "todo_03_completed", role: "agent", status: "done", done: true, archive_state: "active", - text: "Preserve the verified artifact", task_class: "advancement_task", - note: retainedNote, evidence: "artifact:verified-result", completed_at: "2026-01-01T00:00:00Z"}, - ]; - const projection = authorityProjectionFixture(options.goal_id, todos); - const committed = await store.commitAuthority({expected_provider_revision: null, - operation_id: "seed-display-source", events: [], receipts: [], next_projection: projection}); - assert.equal(committed.status, "applied"); - if (committed.status !== "applied") return; - const request = {schema_version: CANONICAL_SNAPSHOT_PAGE_REQUEST, runtime_root: tmpdir(), - goal_id: options.goal_id, include_leases: true, projection_readback: null, after: null}; - const first = await readCanonicalSnapshotFromStore(request, store); - assert.equal(first.status, "page"); - assert.equal(first.next, null); - const source = {source: {record: {items: first.todos}}, handoff_gates: []}; - const before = JSON.stringify(first); - const views = projectTodoContextPage({summary_display_views: source}); - const ids = (rows: unknown) => (rows as JsonObject[]).map(row => row.todo_id); - assert.deepEqual(ids(views.summary), ["todo_01_open", "todo_02_blocker"]); - assert.deepEqual(ids(views.planning), ["todo_01_open", "todo_02_blocker"]); - assert.deepEqual(ids(views.terminal), ["todo_01_open", "todo_02_blocker", "todo_03_completed"]); - assert.deepEqual((views.planning as JsonObject[])[0].required_write_scopes, ["result.json"]); - const terminal = (views.terminal as JsonObject[])[2]; - assert.equal(terminal.note, retainedNote); - assert.equal(terminal.evidence, "artifact:verified-result"); - assert.equal(terminal.completed_at, "2026-01-01T00:00:00Z"); - assert.equal(JSON.stringify(first), before, "display cannot mutate the complete source snapshot"); - const unchanged = await store.loadAuthority(); - assert.equal(unchanged.status, "loaded"); - if (unchanged.status !== "loaded") return; - assert.equal(unchanged.provider_revision, committed.provider_revision); - assert.deepEqual(unchanged.head, projection, "display cannot write or compact provider records"); - - const changed = authorityProjectionFixture(options.goal_id, todos.map(row => - row.todo_id === "todo_01_open" ? {...row, status: "done", done: true} : row)); - const later = await store.commitAuthority({expected_provider_revision: committed.provider_revision, - operation_id: "advance-display-source", events: [], receipts: [], next_projection: changed}); - assert.equal(later.status, "applied"); - if (later.status !== "applied") return; - const fresh = await readCanonicalSnapshotFromStore(request, store); - assert.equal(fresh.status, "page"); - assert.notEqual((fresh.snapshot as JsonObject).provider_revision, (first.snapshot as JsonObject).provider_revision); - assert.deepEqual((fresh.todos as JsonObject[]).filter(row => row.todo_id !== "todo_01_open"), - (first.todos as JsonObject[]).filter(row => row.todo_id !== "todo_01_open"), "non-target provider rows stay unchanged"); - assert.deepEqual(ids(projectTodoContextPage({summary_display_views: { - source: {record: {items: fresh.todos}}, handoff_gates: [], - }}).planning), ["todo_02_blocker"]); - assert.deepEqual(ids(projectTodoContextPage({summary_display_views: source}).planning), ["todo_01_open", "todo_02_blocker"], - "the retained source is historical evidence, never a newly read provider head"); - await assert.rejects(readCanonicalSnapshotFromStore({...request, - after: {snapshot: first.snapshot, todo_offset: 1, lease_offset: 0}}, store), /canonical snapshot changed/); - }); - - registerAuthorityStoreConformance("PostgreSQL provider", async (t) => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(() => cleanScope(tenantId, goalId)); - return { - store: new PostgreSqlAuthorityStore(database, { - tenant_id: tenantId, - goal_id: goalId, - }), - contender: new PostgreSqlAuthorityStore(database, { - tenant_id: tenantId, - goal_id: goalId, - }), - }; - }); - - test("PostgreSQL Todo priority edit survives reopen, replay and conflicting intent", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const todos = [{schema_version: "todo_domain_record_v0", todo_id: "todo_priority", role: "agent", - status: "open", done: false, archive_state: "active", text: "[P2] Existing work", - title: "Existing work", priority: "P2", task_class: "advancement_task"}]; - await store.commitAuthority({operation_id: "seed-priority", expected_provider_revision: null, - events: [], receipts: [], next_projection: {goal_id: options.goal_id, handoff_mode: "soft_claim", todos, leases: [], - todo_read_model: {schema_version: TODO_DOMAIN_READ_RECORD_SCHEMA, todo_count: 1, - records_sha256: canonicalAuthoritySha256(todos), contract_fields: [...TODO_DOMAIN_RECORD_CONTRACT.fields]}}}); - const request = {goal_id: options.goal_id, todo_id: "todo_priority", expected_role: "agent", - actor_agent_id: null, registered_agents: [], operation_id: "edit-priority", - patch: {text: "Renamed work"}, clear_fields: [], planning_intent: {priority: "P4"}, - dry_run: false, now: new Date("2030-01-01T00:00:00Z")}; - const result = await executeCoordinationTodoUpdate(store, request); - assert.equal(result.status, "applied", JSON.stringify(result)); - const reopened = new PostgreSqlAuthorityStore(database, options); - assert.equal((await executeCoordinationTodoUpdate(reopened, request)).status, "replayed"); - const before = await reopened.loadAuthority(); - assert.equal(before.status, "loaded"); - if (before.status !== "loaded") return; - assert.equal((before.head.todos as {priority: string}[])[0]!.priority, "P4"); - assert.equal((await executeCoordinationTodoUpdate(reopened, {...request, - planning_intent: {priority: "P0"}})).status, "failed"); - assert.deepEqual(await reopened.loadAuthority(), before); - assert.equal((await executeCoordinationTodoUpdate(reopened, {...request, - operation_id: "clear-priority", patch: {}, planning_intent: {clear_priority: true}})).status, "applied"); - const cleared = await reopened.loadAuthority(); - assert.equal(cleared.status, "loaded"); - if (cleared.status === "loaded") assert.equal((cleared.head.todos as {priority?: string}[])[0]!.priority, undefined); - }); - - test("PostgreSQL owner deferral atomically retires the lease and replays after reopen", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - const todos = [{schema_version: "todo_domain_record_v0", todo_id: "todo_wait", role: "agent", - status: "open", done: false, archive_state: "active", text: "Wait for dependency", - task_class: "advancement_task", claimed_by: "agent-a"}]; - await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, - events: [], receipts: [], next_projection: {goal_id: options.goal_id, handoff_mode: "hard_lease", todos, - leases: [{schema_version: "task_lease_v0", goal_id: options.goal_id, todo_id: "todo_wait", - owner: "agent-a", idempotency_key: "execution", version: 1, lease_epoch: 1, - status: "active", expires_at: "2030-01-01T01:00:00Z", write_scopes: []}], - todo_read_model: {schema_version: TODO_DOMAIN_READ_RECORD_SCHEMA, todo_count: 1, - records_sha256: canonicalAuthoritySha256(todos), contract_fields: [...TODO_DOMAIN_RECORD_CONTRACT.fields]}}}); - const request = {goal_id: options.goal_id, todo_id: "todo_wait", expected_role: "agent", - actor_agent_id: "agent-a", registered_agents: ["agent-a"], operation_id: "defer", - patch: {}, clear_fields: [], planning_intent: {status: "deferred", - resume_when: "todo_done:todo_dependency", reason: "Dependency pending"}, - lease_idempotency_key: "execution", lease_expected_version: 1, - dry_run: false, now: new Date("2030-01-01T00:00:00Z")}; - const before = await store.loadAuthority(); - assert.equal((await executeCoordinationTodoUpdate(store, {...request, lease_expected_version: 2})).status, "failed"); - assert.deepEqual(await store.loadAuthority(), before); - assert.equal((await executeCoordinationTodoUpdate(store, request)).status, "applied"); - const reopened = new PostgreSqlAuthorityStore(database, options); - const after = await reopened.loadAuthority(); - assert.equal(after.status, "loaded"); - if (after.status !== "loaded") return; - assert.equal((after.head.todos as {status: string}[])[0].status, "deferred"); - assert.equal((after.head.leases as {status: string}[])[0].status, "released"); - assert.equal((await executeCoordinationTodoUpdate(reopened, request)).status, "replayed"); - assert.deepEqual(await reopened.loadAuthority(), after); - }); - - test("PostgreSQL scan binds head and rows to one snapshot during concurrent commit", async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const writer = new PostgreSqlAuthorityStore(database, options); - const first = await writer.commitAuthority(commit(null, "snapshot-first", 1, 1)); - assert.equal(first.status, "applied"); - if (first.status !== "applied") return; - let interleaved = false; - const reader = new PostgreSqlAuthorityStore({connect: async () => { - const connection = await database.connect(); - return {...connection, query: async (sql, values) => { - const result = await connection.query(sql, values); - if (!interleaved && sql.includes("FROM loopx_control_plane.authority_heads")) { - interleaved = true; - assert.equal((await writer.commitAuthority(commit(first.provider_revision, - "snapshot-second", 2, 2))).status, "applied"); - } - return result; - }}; - }}, options); - const page = await reader.scanCommitted(null, 10); - assert.equal(interleaved, true); - assert.equal(page.status, "page", JSON.stringify(page)); - if (page.status !== "page") return; - assert.deepEqual(page.transactions.map(row => row.operation_id), ["snapshot-first"]); - assert.equal(page.has_more, false); - const next = await reader.scanCommitted(page.next_cursor, 10); - assert.equal(next.status, "page"); - if (next.status === "page") assert.deepEqual(next.transactions.map(row => row.operation_id), ["snapshot-second"]); - }); - - for (const removedCursor of [2, 3]) { - test(`PostgreSQL scan rejects a missing retained row at cursor ${removedCursor}`, async t => { - await installed; - const options = {tenant_id: `tenant-${randomUUID()}`, goal_id: `goal-${randomUUID()}`}; - t.after(() => cleanScope(options.tenant_id, options.goal_id)); - const store = new PostgreSqlAuthorityStore(database, options); - let revision: string | null = null; - for (let index = 1; index <= 3; index++) { - const result = await store.commitAuthority(commit(revision, `gap-${index}`, index, index)); - assert.equal(result.status, "applied"); - if (result.status !== "applied") return; - revision = result.provider_revision; - } - await pool!.query("DELETE FROM loopx_control_plane.authority_commits WHERE tenant_id=$1 AND goal_id=$2 AND cursor=$3", - [options.tenant_id, options.goal_id, removedCursor]); - for (const limit of [1, 10]) { - const result = await store.scanCommitted(removedCursor === 3 ? "1" : null, limit); - assert.equal(result.status, "failed", JSON.stringify(result)); - if (result.status === "failed") assert.equal(result.reason_code, "provider_protocol_violation"); - } - const head = await store.loadAuthority(); - assert.equal(head.status, "loaded"); - if (head.status === "loaded") assert.equal(head.provider_revision, revision); - }); - } - - test("PostgreSQL provider scopes identical goals and operations by tenant", async (t) => { - await installed; - const goalId = `goal-${randomUUID()}`; - const firstTenant = `tenant-${randomUUID()}`; - const secondTenant = `tenant-${randomUUID()}`; - t.after(async () => { - await cleanScope(firstTenant, goalId); - await cleanScope(secondTenant, goalId); - }); - const first = new PostgreSqlAuthorityStore(database, { - tenant_id: firstTenant, - goal_id: goalId, - }); - const second = new PostgreSqlAuthorityStore(database, { - tenant_id: secondTenant, - goal_id: goalId, - }); - - const results = await Promise.all([ - first.commitAuthority(commit(null, "shared-operation", 1, 1)), - second.commitAuthority(commit(null, "shared-operation", 1, 1)), - ]); - assert.deepEqual(results.map((result) => result.status), ["applied", "applied"]); - assert.equal((await first.readReceipt("shared-operation")).status, "found"); - assert.equal((await second.readReceipt("shared-operation")).status, "found"); - }); - - test("local provider selector switches to a real PostgreSQL tenant", async (t) => { - await installed; - const root = await mkdtemp(join(tmpdir(), "loopx-local-provider-pg-")); - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(async () => { - await cleanScope(tenantId, goalId); - await rm(root, {recursive: true, force: true}); - }); - await mkdir(join(root, "authority"), {recursive: true}); - const marker = join(root, "authority", `provider-${createHash("sha256").update(goalId).digest("hex")}.json`); - await writeFile(marker, JSON.stringify({ - schema_version: "loopx_local_authority_provider_v0", - provider: "postgresql", - goal_id: goalId, - tenant_id: tenantId, - store_identity: STORE_IDENTITY, - })); - const handle = await openLocalAuthorityStoreHandle(root, goalId, { - openPostgresqlStore: selection => new PostgreSqlAuthorityStore(database, { - tenant_id: selection.tenant_id, - goal_id: selection.goal_id, - }), - }); - assert.equal(handle.provider, "postgresql"); - assert.equal(handle.sourceAuthority, "postgresql_v0"); - assert.equal(handle.store.providerKind, "postgresql"); - const applied = await handle.store.commitAuthority(commit(null, "selector-operation", 1, 1)); - assert.equal(applied.status, "applied"); - assert.equal((await handle.store.readReceipt("selector-operation")).status, "found"); - }); - - test("PostgreSQL provider rolls back head, events, and receipts together", async (t) => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(async () => { - await pool!.query( - "DROP TRIGGER IF EXISTS authority_commit_test_failure ON loopx_control_plane.authority_commits", - ); - await pool!.query( - "DROP FUNCTION IF EXISTS loopx_control_plane.reject_test_authority_commit()", - ); - await cleanScope(tenantId, goalId); - }); - await pool!.query(` - CREATE OR REPLACE FUNCTION loopx_control_plane.reject_test_authority_commit() - RETURNS trigger LANGUAGE plpgsql AS $$ - BEGIN - IF NEW.operation_id = 'force-rollback' THEN - RAISE EXCEPTION 'injected transaction failure'; - END IF; - RETURN NEW; - END; - $$ - `); - await pool!.query(` - CREATE TRIGGER authority_commit_test_failure - BEFORE INSERT ON loopx_control_plane.authority_commits - FOR EACH ROW EXECUTE FUNCTION loopx_control_plane.reject_test_authority_commit() - `); - const store = new PostgreSqlAuthorityStore(database, { - tenant_id: tenantId, - goal_id: goalId, - }); - const result = await store.commitAuthority(commit(null, "force-rollback", 1, 1)); - assert.equal(result.status, "failed"); - assert.deepEqual(await store.loadAuthority(), { status: "missing" }); - assert.deepEqual(await store.readReceipt("force-rollback"), { status: "missing" }); - }); - - test("PostgreSQL COMMIT response loss is ambiguous and receipt-recoverable", async (t) => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(() => cleanScope(tenantId, goalId)); - let loseCommitResponse = true; - const lossyDatabase: PostgreSqlAuthorityDatabase = { - connect: async () => { - const client = await pool!.connect(); - return { - query: async (text, values) => { - const result = await client.query(text, values ? [...values] : undefined); - if (text === "COMMIT" && loseCommitResponse) { - loseCommitResponse = false; - throw new Error("simulated response loss after PostgreSQL commit"); - } - return result; - }, - release: (error) => client.release(error), - }; - }, - }; - const store = new PostgreSqlAuthorityStore(lossyDatabase, { - tenant_id: tenantId, - goal_id: goalId, - }); - const result = await store.commitAuthority(commit(null, "operation-ambiguous", 1, 4)); - assert.equal(result.status, "ambiguous"); - const receipt = await store.readReceipt("operation-ambiguous"); - assert.equal(receipt.status, "found"); - if (receipt.status === "found") assert.equal(receipt.receipts[0]?.lease_epoch, 4); - const loaded = await store.loadAuthority(); - assert.equal(loaded.status, "loaded"); - }); - - test("PostgreSQL database incarnation binds revisions and cannot be rebound", async (t) => { - await installed; - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - t.after(() => cleanScope(tenantId, goalId)); - const store = new PostgreSqlAuthorityStore(database, { - tenant_id: tenantId, - goal_id: goalId, - }); - const first = await store.commitAuthority(commit(null, "operation-a", 1, 1)); - assert.equal(first.status, "applied"); - if (first.status !== "applied") return; - const foreignRevision = first.provider_revision.replace( - STORE_IDENTITY, - `postgresql:${"c".repeat(32)}`, - ); - const crossLineage = await store.commitAuthority( - commit(foreignRevision, "operation-b", 2, 2), - ); - assert.equal(crossLineage.status, "conflict"); - assert.equal((await store.readReceipt("operation-b")).status, "missing"); - - await assert.rejects( - installPostgreSqlAuthorityStoreSchema( - database, - `postgresql:${"c".repeat(32)}`, - ), - /database incarnation/, - ); - }); - - test("PostgreSQL runtime role is confined by transaction-local tenant RLS", async (t) => { - await installed; - const role = `loopx_test_runtime_${randomUUID().replaceAll("-", "")}`; - const firstTenant = `tenant-${randomUUID()}`; - const secondTenant = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - const runtimePool = new Pool({ connectionString, max: 1 }); - await createRuntimeRole(role); - t.after(async () => { - await runtimePool.end(); - await cleanScope(firstTenant, goalId); - await cleanScope(secondTenant, goalId); - await pool!.query(`DROP OWNED BY ${quotedRole(role)}`); - await pool!.query(`DROP ROLE ${quotedRole(role)}`); - }); - - const runtimeDatabase = databaseForRuntimeRole(runtimePool, role); - const first = new PostgreSqlAuthorityStore(runtimeDatabase, { - tenant_id: firstTenant, - goal_id: goalId, - }); - const second = new PostgreSqlAuthorityStore(runtimeDatabase, { - tenant_id: secondTenant, - goal_id: goalId, - }); - const root = await mkdtemp(join(tmpdir(), "pg-genesis-source-")); - t.after(() => rm(root, {recursive: true, force: true})); - const source = new FileAuthorityStore(root, goalId); - const synthetic = {goal_id: goalId, counter: 1, detail: "stable fixture"}; - assert.equal((await source.commitAuthority({ - operation_id: "local-fixture", expected_provider_revision: null, - events: [], receipts: [], next_projection: synthetic, - })).status, "applied"); - const seed = {runtimeRoot: root, goalId, tenantId: firstTenant, localStore: source, - outputPath: join(root, "seed.json"), - storeIdentity: STORE_IDENTITY, targetDatabase: runtimeDatabase, execute: true}; - const genesis = await exportLocalToPgGenesisSeed(seed); - assert.equal(genesis.status, "seeded", genesis.reason); - const originalSeedReceipt = await first.readReceipt(genesis.operation_id); - assert.equal(originalSeedReceipt.status, "found"); - const retried = await exportLocalToPgGenesisSeed(seed); - assert.equal(retried.status, "seeded", retried.reason); - assert.equal(retried.provider_revision, genesis.provider_revision); - assert.equal(retried.seed_digest, genesis.seed_digest); - assert.deepEqual(await first.readReceipt(genesis.operation_id), originalSeedReceipt); - assert.equal((await exportLocalToPgGenesisSeed({...seed, storeIdentity: `postgresql:${"d".repeat(32)}`})).status, "failed"); - assert.equal((await second.commitAuthority(commit(null, "operation-b", 1, 1))).status, "applied"); - assert.equal((await second.readReceipt("operation-b")).status, "found"); - let revision = genesis.provider_revision; - for (let cursor = 2; cursor <= 67; cursor++) { - const result = await first.commitAuthority({ - expected_provider_revision: revision, - operation_id: `append-${cursor}`, - events: [{counter: cursor}], - receipts: [{verified_cursor: cursor}], - next_projection: {...synthetic, counter: cursor}, - }); - assert.equal(result.status, "applied", JSON.stringify(result)); - if (result.status === "applied") revision = result.provider_revision; - } - const page = await first.scanCommitted("62", 4); - assert.equal(page.status, "page"); - if (page.status !== "page") assert.fail(JSON.stringify(page)); - assert.deepEqual(page.transactions.map(row => row.projection.counter), [63, 64, 65, 66]); - assert.deepEqual(page.transactions[2]?.events, [{counter: 65}]); - assert.deepEqual(page.transactions[2]?.receipts, [{verified_cursor: 65}]); - assert.equal(page.has_more, true); - assert.equal((await first.readReceipt("append-65")).status, "found"); - const original = await first.readReceipt("append-65"); - assert.deepEqual(await first.readReceipts(["append-65", "operation-b", "append-65"]), { - status: "receipts", results: [original, {status: "missing"}, original], - }); - - await queryAsRuntimeRole(runtimePool, role, async (client) => { - for (const table of TENANT_SCOPED_TABLES) { - const unscoped = await client.query( - `SELECT tenant_id FROM loopx_control_plane.${table}`, - ); - assert.equal(unscoped.rowCount, 0, `${table} must fail closed without tenant context`); - } - - await client.query("BEGIN READ ONLY"); - await client.query( - "SELECT set_config('loopx.tenant_id', $1, TRUE)", - [firstTenant], - ); - for (const table of TENANT_SCOPED_TABLES) { - const scoped = await client.query( - `SELECT DISTINCT tenant_id FROM loopx_control_plane.${table}`, - ); - assert.deepEqual(scoped.rows, [{ tenant_id: firstTenant }]); - } - await client.query("ROLLBACK"); - - for (const statement of [ - "UPDATE loopx_control_plane.authority_checkpoints SET projection = '{}'::jsonb", - "DELETE FROM loopx_control_plane.authority_checkpoints", - "UPDATE loopx_control_plane.authority_commits SET projection_digest = ''", - "DELETE FROM loopx_control_plane.authority_commits", - "CREATE TABLE loopx_control_plane.forbidden (id text)", - ]) await assert.rejects(client.query(statement), /permission denied/); - - await client.query("BEGIN"); - await client.query( - "SELECT set_config('loopx.tenant_id', $1, TRUE)", - [firstTenant], - ); - await assert.rejects( - client.query( - `INSERT INTO loopx_control_plane.authority_heads - (tenant_id, goal_id, provider_revision, cursor, head) - VALUES ($1, $2, 0, 0, NULL)`, - [secondTenant, `forbidden-${randomUUID()}`], - ), - /row-level security policy/, - ); - await client.query("ROLLBACK"); - - await assert.rejects( - client.query( - `UPDATE loopx_control_plane.authority_store_metadata - SET store_identity = $1 WHERE singleton = TRUE`, - [`postgresql:${"d".repeat(32)}`], - ), - /permission denied/, - ); - }); - }); - - test("PostgreSQL v0 upgrade preserves restricted history access and rejects corrupt prefixes", async t => { - const suffix = randomUUID().replaceAll("-", ""); - const name = `upgrade_${suffix}`; - const role = `worker_${suffix}`; - const grantRole = `journal_${suffix}`; - const url = new URL(connectionString!); - url.pathname = `/${name}`; - const legacy = new Pool({connectionString: url.href}); - await pool!.query(`CREATE DATABASE ${name}`); - t.after(async () => { - await legacy.end(); - await pool!.query(`DROP DATABASE ${name} WITH (FORCE)`); - await pool!.query(`DROP ROLE IF EXISTS ${quotedRole(role)}, ${quotedRole(grantRole)}`); - }); - await legacy.query(POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL); - // Restore the released v0 shape in the disposable database. There are no - // v1 rows or checkpoints at this point. - await legacy.query(` - DROP TABLE loopx_control_plane.authority_checkpoints; - ALTER TABLE loopx_control_plane.authority_commits - DROP COLUMN delta, DROP COLUMN projection_digest, ALTER COLUMN projection SET NOT NULL; - INSERT INTO loopx_control_plane.authority_store_metadata (schema_version, store_identity) - VALUES ('loopx_postgresql_authority_store_v0', '${STORE_IDENTITY}'); - CREATE ROLE ${quotedRole(grantRole)} NOLOGIN; - CREATE ROLE ${quotedRole(role)} NOLOGIN; - GRANT ${quotedRole(grantRole)} TO ${quotedRole(role)}; - GRANT USAGE ON SCHEMA loopx_control_plane TO ${quotedRole(role)}; - GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${quotedRole(role)}; - GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${quotedRole(role)}; - GRANT SELECT, INSERT ON loopx_control_plane.authority_commits TO ${quotedRole(grantRole)}; - GRANT SELECT, INSERT ON loopx_control_plane.authority_events, - loopx_control_plane.authority_receipts TO ${quotedRole(role)}; - `); - const tenantId = `tenant-${randomUUID()}`; - const goalId = `goal-${randomUUID()}`; - const store = new PostgreSqlAuthorityStore(databaseForRuntimeRole(legacy, role), {tenant_id: tenantId, goal_id: goalId}); - const projection = (counter: number) => ({goal_id: goalId, counter}); - const scope = [tenantId, goalId]; - await legacy.query( - `INSERT INTO loopx_control_plane.authority_heads (tenant_id, goal_id, provider_revision, cursor, head) - VALUES ($1, $2, 2, 2, $3)`, [...scope, projection(2)]); - for (const cursor of [1, 2]) { - await legacy.query( - `INSERT INTO loopx_control_plane.authority_commits - (tenant_id, goal_id, cursor, provider_revision, operation_id, projection) - VALUES ($1, $2, $3, $3, $4, $5)`, [...scope, cursor, `legacy-${cursor}`, projection(cursor)]); - } - await legacy.query( - `INSERT INTO loopx_control_plane.authority_receipts (tenant_id, goal_id, cursor, receipt_index, receipt) - VALUES ($1, $2, 2, 0, $3)`, [...scope, {before_upgrade: true}]); - const upgraded = await setupPostgreSqlAuthoritySchemaAndRole(databaseFromPool(legacy), {storeIdentity: STORE_IDENTITY}); - assert.equal(upgraded.status, "installed", upgraded.reason); - const original = await store.readReceipt("legacy-2"); - assert.equal(original.status, "found"); - if (original.status !== "found") assert.fail(JSON.stringify(original)); - assert.equal(original.cursor, "2"); - assert.equal(original.provider_revision, `${STORE_IDENTITY}:2`); - assert.deepEqual(original.receipts, [{before_upgrade: true}]); - await queryAsRuntimeRole(legacy, role, async (client) => { - const result = await client.query(` - SELECT - has_table_privilege('loopx_control_plane.authority_checkpoints', 'SELECT') AS can_read, - has_table_privilege('loopx_control_plane.authority_checkpoints', 'INSERT') AS can_append, - has_table_privilege('loopx_control_plane.authority_checkpoints', 'UPDATE,DELETE') AS can_mutate - `); - assert.deepEqual(result.rows, [{can_read: true, can_append: true, can_mutate: false}]); - }); - let revision = `${STORE_IDENTITY}:2`; - for (const cursor of [3, 4, 5]) { - const result = await store.commitAuthority({operation_id: `new-${cursor}`, - expected_provider_revision: revision, next_projection: projection(cursor), - events: [], receipts: [{after_upgrade: cursor}]}); - if (result.status !== "applied") assert.fail(JSON.stringify(result)); - revision = result.provider_revision; - } - assert.deepEqual(await store.readReceipt("legacy-2"), original); - const page = await store.scanCommitted("1", 3); - if (page.status !== "page") assert.fail(JSON.stringify(page)); - assert.deepEqual(page.transactions.map(row => row.projection), [2, 3, 4].map(projection)); - - const tamper = {schema_version: "authority_state_delta_v0", operations: [{op: "set", path: ["counter"], value: 999}]}; - await legacy.query( - `UPDATE loopx_control_plane.authority_commits SET delta=$3 - WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3`, [...scope, tamper]); - const corrupt = await store.scanCommitted("3", 1); - assert.equal(corrupt.status, "failed"); - assert.match("reason" in corrupt ? corrupt.reason : "", /digest mismatch/); - await legacy.query( - "DELETE FROM loopx_control_plane.authority_commits WHERE tenant_id=$1 AND goal_id=$2 AND cursor=3", scope); - const missing = await store.scanCommitted("3", 1); - assert.equal(missing.status, "failed"); - assert.match("reason" in missing ? missing.reason : "", /window is incomplete/); - }); -} else { - test("PostgreSQL authority-store integration (set LOOPX_TEST_POSTGRES_URL)", { skip: true }, () => {}); -} - -test.after(async () => { - await pool?.end(); -}); - -test("PostgreSQL schema declares fail-closed tenant RLS on every scoped table", () => { - for (const table of TENANT_SCOPED_TABLES) { - assert.match( - POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL, - new RegExp(`ALTER TABLE loopx_control_plane\\.${table} FORCE ROW LEVEL SECURITY`), - ); - assert.match( - POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL, - new RegExp(`CREATE POLICY ${table}_tenant_scope`), - ); - } - assert.match( - POSTGRESQL_AUTHORITY_STORE_SCHEMA_SQL, - /current_setting\('loopx\.tenant_id', TRUE\)/, - ); -}); - -test("PostgreSQL provider rejects an oversized commit before opening a connection", async () => { - let connectionAttempts = 0; - const disconnectedDatabase: PostgreSqlAuthorityDatabase = { - connect: async () => { - connectionAttempts += 1; - throw new Error("capacity admission must run before connect"); - }, - }; - const store = new PostgreSqlAuthorityStore(disconnectedDatabase, { - tenant_id: "tenant-capacity", - goal_id: "goal-capacity", - max_commit_bytes: 256, - }); - const oversized = commit(null, "operation-oversized", 1, 1); - oversized.next_projection.payload = "x".repeat(1024); - - const result = await store.commitAuthority(oversized); - assert.equal(result.status, "failed"); - if (result.status === "failed") { - assert.equal(result.reason_code, "store_capacity_exhausted"); - assert.match(result.reason, /configured maximum is 256/); - } - assert.equal(connectionAttempts, 0); -}); - -test("PostgreSQL provider validates its commit capacity envelope", () => { - const disconnectedDatabase: PostgreSqlAuthorityDatabase = { - connect: async () => { - throw new Error("not reached"); - }, - }; - const defaultStore = new PostgreSqlAuthorityStore(disconnectedDatabase, { - tenant_id: "tenant-capacity", - goal_id: "goal-capacity", - }); - assert.equal(defaultStore.maxCommitBytes, DEFAULT_POSTGRESQL_MAX_COMMIT_BYTES); - assert.throws( - () => new PostgreSqlAuthorityStore(disconnectedDatabase, { - tenant_id: "tenant-capacity", - goal_id: "goal-capacity", - max_commit_bytes: 0, - }), - /max commit bytes must be a positive safe integer/, - ); -}); - -test("PostgreSQL provider evicts and awaits cleanup-uncertain read connections", async () => { - const tenantId = "tenant-cleanup-fault"; - const rollbackFailure = new Error("injected rollback failure"); - const queries: string[] = []; - let finishRelease: (() => void) | undefined; - let resultSettled = false; - const releaseGate = new Promise((resolve) => { - finishRelease = resolve; - }); - let reportRelease: ((error: Error | undefined) => void) | undefined; - const releaseStarted = new Promise((resolve) => { - reportRelease = resolve; - }); - const faultingDatabase: PostgreSqlAuthorityDatabase = { - connect: async () => ({ - query: async (text) => { - queries.push(text); - if (text === "ROLLBACK") throw rollbackFailure; - if (text.includes("set_config")) { - return { rows: [{ tenant_id: tenantId }], rowCount: 1 }; - } - if (text.includes("authority_store_metadata")) { - return { - rows: [{ schema_version: "loopx_postgresql_authority_store_v1", store_identity: STORE_IDENTITY }], - rowCount: 1, - }; - } - if (text.includes("authority_heads")) { - return { rows: [], rowCount: 0 }; - } - return { rows: [], rowCount: 0 }; - }, - release: async (error) => { - reportRelease?.(error); - await releaseGate; - }, - }), - }; - const store = new PostgreSqlAuthorityStore(faultingDatabase, { - tenant_id: tenantId, - goal_id: "goal-cleanup-fault", - }); - - const resultPromise = store.loadAuthority().then((result) => { - resultSettled = true; - return result; - }); - assert.strictEqual(await releaseStarted, rollbackFailure); - assert.equal(resultSettled, false, "read result must wait for asynchronous connection eviction"); - assert.ok(finishRelease); - finishRelease(); - - assert.deepEqual(await resultPromise, { - status: "unavailable", - reason_code: "provider_read_unavailable", - reason: "PostgreSQL authority store is unavailable", - }); - assert.equal(queries.filter((query) => query === "ROLLBACK").length, 1); -}); diff --git a/tests/control_plane_ts/postgresql_authority_store.test.ts b/tests/control_plane_ts/postgresql_authority_store.test.ts deleted file mode 100644 index 0540b62ab9..0000000000 --- a/tests/control_plane_ts/postgresql_authority_store.test.ts +++ /dev/null @@ -1,218 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; - -import { - POSTGRESQL_SCHEMA_VERSION, - rotatePostgreSqlAuthorityStoreIdentity, - type PostgreSqlAuthorityConnection, - type PostgreSqlAuthorityDatabase, -} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; - -const STORE_IDENTITY = `postgresql:${"a".repeat(32)}`; -const NEXT_STORE_IDENTITY = `postgresql:${"b".repeat(32)}`; - -type RotationQueryResult = {rows: readonly unknown[]; rowCount: number | null}; - -interface RotationHarness { - readonly database: PostgreSqlAuthorityDatabase; - readonly calls: {text: string; values: readonly unknown[] | undefined}[]; - readonly releases: (Error | undefined)[]; - readonly connectAttempts: () => number; -} - -/** Scripted provider so rotation admission and transaction outcomes stay unit-testable. */ -function rotationHarness( - handler: (text: string, values: readonly unknown[] | undefined) => RotationQueryResult, -): RotationHarness { - const calls: {text: string; values: readonly unknown[] | undefined}[] = []; - const releases: (Error | undefined)[] = []; - let connectAttempts = 0; - const connection: PostgreSqlAuthorityConnection = { - query: async (text, values) => { - calls.push({text, values}); - return handler(text, values); - }, - release: error => { - releases.push(error); - }, - }; - return { - database: { - connect: async () => { - connectAttempts += 1; - return connection; - }, - }, - calls, - releases, - connectAttempts: () => connectAttempts, - }; -} - -function metadataResult(storeIdentity: string): RotationQueryResult { - return { - rows: [{schema_version: POSTGRESQL_SCHEMA_VERSION, store_identity: storeIdentity}], - rowCount: 1, - }; -} - -test("PostgreSQL identity rotation rejects invalid and unchanged incarnations", async () => { - const harness = rotationHarness(() => { - throw new Error("a rejected rotation must not query the provider"); - }); - - assert.deepEqual( - await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - "postgresql:not-an-incarnation", - NEXT_STORE_IDENTITY, - ), - { - status: "failed", - reason_code: "invalid_store_identity", - reason: "PostgreSQL store identities must match postgresql:<32 lowercase hex>", - }, - ); - assert.deepEqual( - await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - STORE_IDENTITY, - STORE_IDENTITY, - ), - { - status: "failed", - reason_code: "store_identity_unchanged", - reason: "PostgreSQL store identity rotation requires a new incarnation", - }, - ); - assert.equal(harness.connectAttempts(), 0); -}); - -test("PostgreSQL identity rotation reports an unavailable provider before BEGIN", async () => { - const result = await rotatePostgreSqlAuthorityStoreIdentity( - { - connect: async () => { - throw new Error("synthetic unavailable provider"); - }, - }, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - - assert.deepEqual(result, { - status: "failed", - reason_code: "provider_connection_unavailable", - reason: "PostgreSQL connection was unavailable before identity rotation", - }); -}); - -test("PostgreSQL identity rotation fails closed when the stored incarnation drifted", async () => { - const harness = rotationHarness(text => - text.includes("authority_store_metadata") - ? metadataResult(NEXT_STORE_IDENTITY) - : {rows: [], rowCount: 1} - ); - - const result = await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - - assert.deepEqual(result, { - status: "failed", - reason_code: "store_identity_mismatch", - reason: "PostgreSQL store identity does not match the expected database incarnation", - }); - assert.equal(harness.calls[0]?.text, "BEGIN"); - assert.equal(harness.calls.at(-1)?.text, "ROLLBACK"); - assert.equal( - harness.calls.some(call => call.text.includes("UPDATE loopx_control_plane")), - false, - ); - assert.deepEqual(harness.releases, [undefined]); -}); - -test("PostgreSQL identity rotation maps provider protocol violations without committing", async () => { - const harness = rotationHarness(text => - text.includes("authority_store_metadata") - ? { - rows: [ - {schema_version: POSTGRESQL_SCHEMA_VERSION, store_identity: STORE_IDENTITY}, - {schema_version: POSTGRESQL_SCHEMA_VERSION, store_identity: STORE_IDENTITY}, - ], - rowCount: 2, - } - : {rows: [], rowCount: 1} - ); - - const result = await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - - assert.equal(result.status, "failed"); - if (result.status === "failed") { - assert.equal(result.reason_code, "provider_protocol_violation"); - assert.equal(result.reason, "PostgreSQL store metadata returned more than one row"); - } - assert.equal(harness.calls.some(call => call.text === "COMMIT"), false); - assert.deepEqual(harness.releases, [undefined]); -}); - -test("PostgreSQL identity rotation reports a transaction that failed before COMMIT", async () => { - const harness = rotationHarness(text => { - if (text === "BEGIN") throw new Error("synthetic BEGIN failure"); - return {rows: [], rowCount: 1}; - }); - - const result = await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - - assert.deepEqual(result, { - status: "failed", - reason_code: "provider_transaction_failed", - reason: "PostgreSQL identity rotation failed before COMMIT", - }); - assert.equal(harness.calls.at(-1)?.text, "ROLLBACK"); - assert.deepEqual(harness.releases, [undefined]); -}); - -test("PostgreSQL identity rotation commits the next incarnation", async () => { - const harness = rotationHarness(text => - text.includes("authority_store_metadata") - ? metadataResult(STORE_IDENTITY) - : {rows: [], rowCount: 1} - ); - - const result = await rotatePostgreSqlAuthorityStoreIdentity( - harness.database, - STORE_IDENTITY, - NEXT_STORE_IDENTITY, - ); - - assert.deepEqual(result, { - status: "rotated", - previous_store_identity: STORE_IDENTITY, - store_identity: NEXT_STORE_IDENTITY, - }); - assert.equal(harness.calls[0]?.text, "BEGIN"); - const identityLock = harness.calls.findIndex(call => - call.text.includes("pg_advisory_xact_lock") - ); - const metadataRead = harness.calls.findIndex(call => - call.text.includes("FROM loopx_control_plane.authority_store_metadata") - ); - assert.ok(identityLock > 0); - assert.ok(metadataRead > identityLock); - const update = harness.calls.find(call => - call.text.includes("UPDATE loopx_control_plane.authority_store_metadata") - ); - assert.deepEqual(update?.values, [NEXT_STORE_IDENTITY]); - assert.equal(harness.calls.at(-1)?.text, "COMMIT"); - assert.deepEqual(harness.releases, [undefined]); -}); diff --git a/tests/control_plane_ts/remote_authority.integration.test.ts b/tests/control_plane_ts/remote_authority.integration.test.ts deleted file mode 100644 index 0625560f9c..0000000000 --- a/tests/control_plane_ts/remote_authority.integration.test.ts +++ /dev/null @@ -1,512 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import {createServer, request as httpsRequest} from "node:https"; -import type {AddressInfo} from "node:net"; -import {mkdir, writeFile} from "node:fs/promises"; -import {join} from "node:path"; -import {execFile} from "node:child_process"; -import {promisify} from "node:util"; -import {Client} from "pg"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts"; -import {fileRemoteAuthorityAdmission} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; -import {callRemoteAuthority, createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts"; -import {PostgreSqlAuthorityStore, rotatePostgreSqlAuthorityStoreIdentity, type PostgreSqlAuthorityDatabase} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import {remoteAuthorityOperationId, type RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; -import {postgresRemoteFixture, claimRequest, credentials, serviceIdentity, servicePolicy} from "./remote_authority_fixture.ts"; -import {openLocalAuthorityStore} from "../../loopx/control_plane/coordination/local_authority_provider.ts"; -import {engageLegacyCoordinationWriterFence} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts"; -import {authorityProjectionFixture} from "./authority_projection_fixture.ts"; -import {resolveTestPython} from "../../scripts/test-python.mjs"; -import {registerRemoteAuthorityModeConformance} from "./remote_authority_mode_conformance.ts"; -import {registerRemoteAuthorityLifecycleConformance} from "./remote_authority_lifecycle_conformance.ts"; -import {registerRemoteAuthorityReadAclConformance} from "./remote_authority_read_acl_conformance.ts"; - -const connectionString = process.env.LOOPX_TEST_POSTGRES_URL; -registerRemoteAuthorityModeConformance(connectionString); -registerRemoteAuthorityLifecycleConformance(connectionString); -registerRemoteAuthorityReadAclConformance(connectionString); -test("quota CLI verifies the exact remote binding and never grants local execution", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - await fixture.seed(["todo_gateway_probe"], {todo_gateway_probe: "private-synthetic-note"}); - const initialHead = await fixture.store.loadAuthority(); - const running = await fixture.start(); - const python = resolveTestPython(); - const invoke = promisify(execFile); - const workspace = join(fixture.root, "workspace"), runtime = join(fixture.root, "runtime"); - const registry = join(workspace, "registry.json"), state = join(workspace, "STATE.md"); - await mkdir(workspace); - await writeFile(state, "---\nstatus: active\nupdated_at: 2026-01-01T00:00:00Z\n---\n# Synthetic Goal\n"); - // The candidate observes a remote authority alongside its established - // private host state. It does not auto-initialize or migrate a Goal. - const local = await openLocalAuthorityStore(runtime, "goal-one"); - const localHead = authorityProjectionFixture("goal-one", [], [], "native", {handoff_mode: "soft_claim"}); - const localSeed = await local.commitAuthority({operation_id: "gateway-local-fixture", - expected_provider_revision: null, next_projection: localHead, events: [], receipts: []}); - assert.equal(localSeed.status, "applied"); - if (localSeed.status !== "applied") throw new Error("local fixture did not initialize"); - const fence = await engageLegacyCoordinationWriterFence({schema_version: "loopx_legacy_coordination_writer_fence_engage_request_v0", - runtime_root: runtime, goal_id: "goal-one", state_path: state, fence: { - schema_version: "loopx_legacy_coordination_writer_fence_v0", state: "engaged", goal_id: "goal-one", - fence_id: "gateway-local-fixture", source_version: "gateway-local-fixture", - source_projection_sha256: canonicalAuthoritySha256(localHead), expected_shadow_provider_revision: localSeed.provider_revision, - }}); - assert.equal(fence.status, "applied"); - const cfg = {enabled: true, endpoint: running.endpoint, actor_id: "actor-a", - tenant_id: "tenant-one", store_identity: serviceIdentity, probe_todo_id: "todo_gateway_probe", ca_file: fixture.certPath}; - const env: NodeJS.ProcessEnv = {...process.env, LOOPX_CONTEXT_DELIVERY: "off", LOOPX_AUTHORITY_CREDENTIAL: credentials.a}; - delete env.LOOPX_TEST_POSTGRES_URL; - const writeBinding = async (overrides: JsonObject = {}) => writeFile(registry, JSON.stringify({ - schema_version: "0.1", common_runtime_root: runtime, - goals: [{id: "goal-one", status: "active", repo: workspace, state_file: "STATE.md", - coordination: {agent_model: "peer_v1", registered_agents: ["actor-a", "actor-b"]}, - quota: {compute: 1}, remote_authority: {...cfg, ...overrides}}], - })); - const args = ["-m", "loopx.cli", "--registry", registry, "--runtime-root", runtime, "--format", "json"]; - const scope = ["--goal-id", "goal-one", "--agent-id", "actor-a"]; - async function check(expected: string, credential = credentials.a) { - const {stdout} = await invoke(python, [...args, "quota", "should-run", "--runtime-profile", "outer_controller", ...scope], - {env: {...env, LOOPX_AUTHORITY_CREDENTIAL: credential}, timeout: 25_000, maxBuffer: 1024 * 1024}); - const decision = JSON.parse(stdout); - assert.equal(decision.remote_gateway?.status, expected, JSON.stringify({ - reason: decision.reason, code: decision.reason_code, fields: Object.keys(decision), - })); - assert.equal(decision.effective_action, "blocked_wait"); - assert.equal(decision.should_run, false); - assert.equal(decision.normal_delivery_allowed, false); - assert.equal(decision.interaction_contract.agent_channel.must_attempt, false); - assert.equal(decision.interaction_contract.agent_channel.delivery_allowed, false); - assert.equal(decision.interaction_contract.cli_channel.spend_allowed_now, false); - assert(!decision.selected_todo); - for (const value of [cfg.endpoint, cfg.tenant_id, cfg.store_identity, cfg.ca_file, credential, "private-synthetic-note"]) { - assert(!stdout.includes(value)); - } - } - await writeBinding(); await check("verified"); - await check("unavailable", credentials.b); // Valid credential, wrong actor authorization. - for (const binding of [{tenant_id: "wrong-tenant"}, {store_identity: `postgresql:${"f".repeat(32)}`}, - {ca_file: null}, {probe_todo_id: "todo_missing"}, {actor_id: "actor-b"}]) { - await writeBinding(binding); await check("unavailable"); - } - await writeBinding(); - await fixture.writePolicy({...servicePolicy(), principals: []}); await check("unavailable"); - await fixture.writePolicy(); await check("verified"); - await running.stop(); await check("unavailable"); - await fixture.start(running.port); await check("verified"); - assert.deepEqual(await fixture.store.loadAuthority(), initialHead, "gateway observations never change the remote head"); -}); - -test("remote authority real TLS/PostgreSQL task journey and recovery", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - let running = await fixture.start(); - const call = (request: RemoteAuthorityRequest, mode: "execute" | "recover" = "execute", credential = credentials.a) => - callRemoteAuthority(running.endpoint, credential, request, {ca: fixture.cert, mode}); - const result = (value: JsonObject) => value.result as JsonObject; - const lease = (value: JsonObject) => result(value).lease as JsonObject; - const base = (todo: string) => { - const {operation: _, request_id: __, ...scope} = claimRequest("a", todo) as Extract; - return scope; - }; - await t.test("independent CLI clients have exactly one complete winner for one task", async () => { - await fixture.seed(["todo_race"]); - const responses = await Promise.all([fixture.client(running.endpoint, "a", claimRequest("a", "todo_race")), - fixture.client(running.endpoint, "b", claimRequest("b", "todo_race"))]); - assert.equal(responses.filter(r => result(r.value).status === "applied").length, 1); - assert.equal(responses.filter(r => r.code === 0).length, 1); - const head = await fixture.store.loadAuthority(); assert.equal(head.status, "loaded"); - if (head.status !== "loaded") return; - assert.equal((head.head.leases as JsonObject[]).length, 1); - const receipts = await Promise.all(["a", "b"].map(actor => fixture.store.readReceipt( - remoteAuthorityOperationId(claimRequest(actor as "a" | "b", "todo_race"), `principal-${actor}`)))); - assert.equal(receipts.filter(r => r.status === "found").length, 1); - }); - await t.test("independent clients keep both unrelated tasks", async () => { - await fixture.seed(["todo_left", "todo_right"]); - const responses = await Promise.all([fixture.client(running.endpoint, "a", claimRequest("a", "todo_left")), - fixture.client(running.endpoint, "b", claimRequest("b", "todo_right"))]); - assert(responses.every(r => r.code === 0 && result(r.value).status === "applied")); - }); - await t.test("concurrent identical commands return one durable outcome", async () => { - await fixture.seed(["todo_retry"]); - const request = claimRequest("a", "todo_retry"); - const responses = await Promise.all([fixture.client(running.endpoint, "a", request), - fixture.client(running.endpoint, "a", request)]); - assert(responses.every(r => r.code === 0)); - assert.equal(responses.filter(r => result(r.value).status === "applied").length, 1); - const receipt = await fixture.store.readReceipt(remoteAuthorityOperationId(request, "principal-a")); - assert.equal(receipt.status, "found"); - if (receipt.status === "found") assert.equal(receipt.receipts.length, 1); - assert.deepEqual(lease(responses[0].value), lease(responses[1].value)); - }); - await t.test("claim, progress, renew, release and readback use the existing task owners", async () => { - await fixture.seed(["todo_journey"]); - const request = claimRequest("a", "todo_journey"); - const claimed = await call(request); assert.equal(result(claimed).status, "applied"); - const progress: RemoteAuthorityRequest = {...base("todo_journey"), operation: "progress", request_id: "progress-one", - claim_request_id: "claim-todo_journey", note: "Validated the requested artifact"}; - assert.equal(result(await call(progress)).status, "applied"); - const beforeReplay = await fixture.store.loadAuthority(); - assert.equal(result(await call(progress)).status, "replayed"); - assert.deepEqual(await fixture.store.loadAuthority(), beforeReplay); - assert.equal((await call({...progress, note: "Different operation intent"})).reason_code, "remote_authority_operation_mismatch"); - const renewed = await call({...base("todo_journey"), operation: "renew", idempotency_key: String(lease(claimed).idempotency_key), - expected_version: Number(lease(claimed).version), ttl_seconds: 600}); - assert.equal(result(renewed).status, "applied", JSON.stringify(renewed)); - const followupNote = "Validated readback after lease renewal"; - assert.equal(result(await call({...progress, request_id: "progress-two", note: followupNote})).status, "applied"); - const staleRelease: RemoteAuthorityRequest = {...base("todo_journey"), operation: "release", - idempotency_key: String(lease(renewed).idempotency_key), expected_version: Number(lease(renewed).version)}; - // Normal progress may maintain its active execution, advancing the version. - // The transport must not silently rebase a stale lifecycle command. - const beforeRelease = await fixture.store.loadAuthority(); - assert.equal(result(await call(staleRelease)).status, "failed"); - assert.deepEqual(await fixture.store.loadAuthority(), beforeRelease); - const current = (await call({...base("todo_journey"), operation: "read"})).lease as JsonObject; - assert.equal(current.owner, "actor-a"); - assert.equal(current.lease_epoch, lease(renewed).lease_epoch); - assert(Number(current.version) > Number(lease(renewed).version)); - const release: RemoteAuthorityRequest = {...staleRelease, expected_version: Number(current.version)}; - const released = await call(release); assert.equal(result(released).status, "applied"); - assert.equal(result(await call(release)).status, "replayed"); - const read = await call({...base("todo_journey"), operation: "read"}); - assert.equal((read.todo as JsonObject).note, followupNote); - assert.equal((read.lease as JsonObject).status, "released"); - assert.equal(Object.hasOwn(read.lease as JsonObject, "idempotency_key"), false); - assert.equal((await call(release, "recover")).historical_only, true); - }); - await t.test("bad credential and wrong tenant/Goal/actor fail without a write", async () => { - const before = await fixture.store.loadAuthority(); - for (const patch of [{tenant_id: "tenant-other"}, {goal_id: "goal-other"}, {actor_id: "actor-b"}]) { - assert.equal((await call({...claimRequest("a"), ...patch})).status, "rejected"); - } - assert.equal((await call(claimRequest("a"), "execute", `invalid-${"x".repeat(48)}`)).reason_code, "principal_unauthenticated"); - assert.deepEqual(await fixture.store.loadAuthority(), before); - }); - await t.test("CLI reads current lease facts and releases tasks regardless of note size", async () => { - const notes = {todo_short_note: "s".repeat(1024), todo_long_note: "l".repeat(66560)}; - await fixture.seed(Object.keys(notes), notes); - for (const [todo, note] of Object.entries(notes)) { - const unclaimed = await fixture.client(running.endpoint, "a", {...base(todo), operation: "read"}); - assert.equal(unclaimed.code, 0); assert.equal(unclaimed.value.lease, null); - const claimed = await fixture.client(running.endpoint, "a", claimRequest("a", todo)); - assert.equal(claimed.code, 0); - if (todo === "todo_short_note") { - // Existing canonical records also admit decimal-string lease counters. - // The wire projection keeps them usable as numeric command arguments. - const head = await fixture.store.loadAuthority(); assert.equal(head.status, "loaded"); - if (head.status !== "loaded") return; - const projection = structuredClone(head.head); - const original = (projection.leases as JsonObject[]).find(row => row.todo_id === todo)!; - original.version = String(original.version); original.lease_epoch = String(original.lease_epoch); - assert.equal((await fixture.store.commitAuthority({operation_id: "legacy-lease-counters", - expected_provider_revision: head.provider_revision, next_projection: projection, events: [], receipts: []})).status, "applied"); - } - const before = await fixture.store.loadAuthority(); - const read = await fixture.client(running.endpoint, "a", {...base(todo), operation: "read"}); - assert.equal(read.code, 0, "a valid note must not hide the lease needed for release"); - assert.equal(read.value.status, "found"); - const current = read.value.lease as JsonObject; - assert.equal(current.owner, "actor-a"); assert.equal(current.status, "active"); - assert.equal(current.version, lease(claimed.value).version); - assert.equal(current.lease_epoch, lease(claimed.value).lease_epoch); - const projected = read.value.todo as JsonObject; - if (todo === "todo_short_note") assert.equal(projected.note, note); - else {assert.equal(projected.note_omitted, true); assert.equal(Object.hasOwn(projected, "note"), false);} - assert(Buffer.byteLength(JSON.stringify(read.value)) <= 64 * 1024); - assert.deepEqual(await fixture.store.loadAuthority(), before); - const released = await fixture.client(running.endpoint, "a", {...base(todo), operation: "release", - idempotency_key: String(lease(claimed.value).idempotency_key), expected_version: Number(current.version)}); - assert.equal(released.code, 0); assert.equal(lease(released.value).status, "released"); - } - }); - await t.test("CLI rejects a correlated but incomplete success and recovers the original intent", async () => { - await fixture.seed(["todo_incomplete_reply"]); - const request = claimRequest("a", "todo_incomplete_reply"); - let reply: JsonObject = {schema_version: "loopx_remote_authority_response_v0", - request_sha256: canonicalAuthoritySha256({mode: "execute", request}), status: "result", result: {status: "applied"}}; - const proxy = createServer({key: fixture.key, cert: fixture.cert}, (incoming, outgoing) => { - incoming.resume(); outgoing.setHeader("Content-Type", "application/json"); outgoing.end(JSON.stringify(reply)); - }); - await new Promise(resolve => proxy.listen(0, "127.0.0.1", resolve)); - try { - const endpoint = `https://127.0.0.1:${(proxy.address() as AddressInfo).port}`; - const malformed = await fixture.client(endpoint, "a", request); - assert.equal(malformed.code, 1, "request correlation alone does not establish a completed claim"); - assert.equal(malformed.value.status, "ambiguous"); - reply = {...reply, request_sha256: "0".repeat(64)}; - assert.equal((await fixture.client(endpoint, "a", request)).code, 1); - const recovered = await fixture.client(running.endpoint, "a", request, true); - assert.equal(recovered.code, 0); assert.equal(recovered.value.status, "missing"); - const valid = await fixture.client(running.endpoint, "a", request); - assert.equal(valid.code, 0); assert.equal(lease(valid.value).owner, "actor-a"); - reply = valid.value; - assert.equal((await fixture.client(endpoint, "a", request)).code, 0); - - const progress: RemoteAuthorityRequest = {...base(request.todo_id), operation: "progress", - request_id: "verified-progress", claim_request_id: `claim-${request.todo_id}`, note: "Validated response evidence"}; - const progressed = await fixture.client(running.endpoint, "a", progress); assert.equal(progressed.code, 0); - const read: RemoteAuthorityRequest = {...base(request.todo_id), operation: "read"}; - const current = await fixture.client(running.endpoint, "a", read); assert.equal(current.code, 0); - const renew: RemoteAuthorityRequest = {...base(request.todo_id), operation: "renew", ttl_seconds: 600, - idempotency_key: String(lease(valid.value).idempotency_key), expected_version: Number((current.value.lease as JsonObject).version)}; - const renewed = await fixture.client(running.endpoint, "a", renew); assert.equal(renewed.code, 0); - const release: RemoteAuthorityRequest = {...base(request.todo_id), operation: "release", - idempotency_key: renew.idempotency_key, expected_version: Number(lease(renewed.value).version)}; - const released = await fixture.client(running.endpoint, "a", release); assert.equal(released.code, 0); - const cases: {request: RemoteAuthorityRequest; reply: JsonObject; required: string[]}[] = [ - {request, reply: valid.value, required: ["operation_id", "result.lease", "result.claim_operation_id", "result.claimed_by"]}, - {request: progress, reply: progressed.value, required: ["operation_id", "result.todo_id", "result.changed", "result.cursor"]}, - {request: renew, reply: renewed.value, required: ["result.renewed", "result.lease.version", "result.lease.idempotency_key"]}, - {request: release, reply: released.value, required: ["result.released", "result.lease", "result.provider_revision"]}, - {request: read, reply: current.value, required: ["todo", "lease", "lease.owner", "lease.lease_epoch", "lease.version"]}, - ]; - function removeField(value: JsonObject, path: string) { - const keys = path.split("."), last = keys.pop()!; - let parent = value; - for (const key of keys) parent = parent[key] as JsonObject; - assert(Object.hasOwn(parent, last), path); delete parent[last]; - } - for (const entry of cases) { - reply = entry.reply; - assert.equal((await fixture.client(endpoint, "a", entry.request)).code, 0, entry.request.operation); - for (const field of entry.required) { - reply = structuredClone(entry.reply); removeField(reply, field); - const invalid = await fixture.client(endpoint, "a", entry.request); - assert.equal(invalid.code, 1, `${entry.request.operation}: missing ${field}`); - assert.equal(invalid.value.status, entry.request.operation === "read" ? "unavailable" : "ambiguous"); - } - if (entry.request.operation === "read") continue; - // Historical receipt payloads have no execution status and must remain - // readable after release; they cannot be promoted to current authority. - const history = await fixture.client(running.endpoint, "a", entry.request, true); - assert.equal(history.code, 0); assert.equal(history.value.historical_only, true); - reply = history.value; - assert.equal((await fixture.client(endpoint, "a", entry.request, true)).code, 0); - for (const field of ["operation_id", "receipt_sha256", "historical_only", "result"]) { - reply = structuredClone(history.value); removeField(reply, field); - const invalid = await fixture.client(endpoint, "a", entry.request, true); - assert.equal(invalid.code, 1, `recovery: missing ${field}`); assert.equal(invalid.value.status, "unavailable"); - } - } - for (const patch of [ - (value: JsonObject) => {result(value).status = ["applied"];}, - (value: JsonObject) => {result(value).todo_id = "todo_other";}, - (value: JsonObject) => {lease(value).owner = "actor-b";}, - (value: JsonObject) => {lease(value).version = "1";}, - (value: JsonObject) => {lease(value).expires_at = "2026-02-31T00:00:00Z";}, - (value: JsonObject) => {value.status = "missing";}, - ]) { - reply = structuredClone(valid.value); patch(reply); - assert.equal((await fixture.client(endpoint, "a", request)).code, 1); - } - } finally {await new Promise(resolve => proxy.close(() => resolve()));} - }); - await t.test("revocation and unreadable policy reject new admissions, including cached TLS connections", async () => { - const policy = servicePolicy(); policy.principals = policy.principals.slice(1); - await fixture.writePolicy(policy); - const before = await fixture.store.loadAuthority(); - assert.equal((await call(claimRequest("a"))).reason_code, "principal_unauthenticated"); - await writeFile(fixture.policyPath, "not JSON"); - assert.equal((await call(claimRequest("a"))).reason_code, "remote_authority_policy_unavailable"); - assert.deepEqual(await fixture.store.loadAuthority(), before); - await fixture.writePolicy(); - }); - await t.test("lost response and server restart recover the exact receipt without repeating the write", async () => { - await fixture.seed(["todo_disconnect"]); - const commands: RemoteAuthorityRequest[] = [claimRequest("a", "todo_disconnect"), - {...base("todo_created_after_disconnect"), operation: "todo_add", request_id: "create-disconnect", - text: "Synthetic task whose creation response is lost"}]; - for (const original of commands) { - // Relay the real TLS request, wait for the complete committed response, - // then discard it. Each command retains its owner's actual receipt shape. - const proxy = createServer({key: fixture.key, cert: fixture.cert}, (incoming, outgoing) => { - const upstream = httpsRequest(new URL(incoming.url!, running.endpoint), {method: "POST", ca: fixture.cert, - headers: incoming.headers}, response => {response.resume(); response.on("end", () => outgoing.destroy());}); - upstream.on("error", () => outgoing.destroy()); incoming.pipe(upstream); - }); - await new Promise(resolve => proxy.listen(0, "127.0.0.1", resolve)); - let lost: JsonObject; - try {lost = await callRemoteAuthority(`https://127.0.0.1:${(proxy.address() as AddressInfo).port}`, credentials.a, original, {ca: fixture.cert});} - finally {await new Promise(resolve => proxy.close(() => resolve()));} - assert.equal(lost.status, "ambiguous", original.operation); - const operationId = remoteAuthorityOperationId(original, "principal-a"); - const receipt = await fixture.store.readReceipt(operationId); - assert.equal(receipt.status, "found"); if (receipt.status !== "found") return; - const before = await fixture.store.loadAuthority(); - const port = running.port; await running.stop(); running = await fixture.start(port); - const recovered = await fixture.client(running.endpoint, "a", original, true); - assert.equal(recovered.code, 0, `${original.operation}: ${JSON.stringify(recovered.value)}`); - assert.equal(recovered.value.status, "found"); - assert.equal(recovered.value.historical_only, true); - assert.equal(recovered.value.receipt_sha256, canonicalAuthoritySha256(receipt.receipts[0])); - assert.equal((recovered.value.result as JsonObject).changed, true); - assert.equal(result(await call(original)).status, "replayed"); - if (original.operation === "todo_add") { - assert.equal(((recovered.value.result as JsonObject).todo as JsonObject).todo_id, original.todo_id); - const changed = await fixture.client(running.endpoint, "a", {...original, text: "Different creation intent"}, true); - assert.equal(changed.code, 1); assert.equal(changed.value.reason_code, "remote_authority_operation_mismatch"); - assert.equal((await fixture.client(running.endpoint, "b", original, true)).code, 1); - } - assert.deepEqual(await fixture.store.readReceipt(operationId), receipt, "recovery preserves the original receipt bytes"); - assert.deepEqual(await fixture.store.loadAuthority(), before, "recovery and replay commit no additional effect"); - } - }); - await t.test("unknown receipt query is read-only and does not turn into a claim", async () => { - const before = await fixture.store.loadAuthority(); - assert.equal((await call(claimRequest("a", "todo_never-created", "unknown"), "recover")).status, "missing"); - assert.deepEqual(await fixture.store.loadAuthority(), before); - }); - await t.test("shutdown waits for an admitted transaction after dropping its response socket", {timeout: 20000}, async t => { - await fixture.seed(["todo_shutdown"]); - let reached!: () => void, release!: () => void; - const committing = new Promise(resolve => {reached = resolve;}); - const gate = new Promise(resolve => {release = resolve;}); - const delayed: PostgreSqlAuthorityDatabase = {connect: async () => { - const connection = await fixture.runtimeDatabase.connect(); - return {...connection, query: async (sql, args) => { - if (sql === "COMMIT") {reached(); await gate;} - return connection.query(sql, args); - }}; - }}; - const service = new RemoteAuthorityService({database: delayed, admit: fileRemoteAuthorityAdmission(fixture.policyPath)}); - const listener = createRemoteAuthorityServer(service, fixture); - const endpoint = `https://127.0.0.1:${await listener.listen()}`; - let stopping: Promise | undefined, closed = false; - t.after(async () => {release(); await (stopping ?? listener.close());}); - const original = claimRequest("a", "todo_shutdown"); - const pending = callRemoteAuthority(endpoint, credentials.a, original, {ca: fixture.cert}); - await committing; - stopping = listener.close().then(() => {closed = true;}); - assert.equal((await pending).status, "ambiguous"); - assert.equal(closed, false, "the database must stay available until COMMIT settles"); - release(); await stopping; - const receipt = await fixture.store.readReceipt(remoteAuthorityOperationId(original, "principal-a")); - assert.equal(receipt.status, "found"); - if (receipt.status === "found") { - const recovered = await call(original, "recover"); - assert.equal(recovered.status, "found"); - assert.equal(recovered.receipt_sha256, canonicalAuthoritySha256(receipt.receipts[0])); - } - }); - await t.test("admitted work completes truthfully when its credential is revoked in flight", async () => { - await fixture.seed(["todo_admitted"]); - let release!: () => void, admitted!: () => void; - const gate = new Promise(resolve => {release = resolve;}); - const started = new Promise(resolve => {admitted = resolve;}); - const admission = fileRemoteAuthorityAdmission(fixture.policyPath); - const service = new RemoteAuthorityService({database: fixture.runtimeDatabase, admit: async (credential, request) => { - const snapshot = await admission(credential, request); admitted(); await gate; return snapshot; - }}); - const pending = service.dispatch(credentials.a, claimRequest("a", "todo_admitted")); - await started; - const policy = servicePolicy(); policy.principals = policy.principals.slice(1); await fixture.writePolicy(policy); - release(); - assert.equal(result(await pending).status, "applied"); - assert.equal((await call(claimRequest("a", "todo_admitted"))).status, "rejected"); - await fixture.writePolicy(); - }); - await t.test("server clock and canonical lease fence prevent stale execution after takeover", async () => { - await fixture.seed(["todo_takeover"]); - let now = new Date("2026-11-10T10:00:00Z"); - const service = new RemoteAuthorityService({database: fixture.runtimeDatabase, - admit: fileRemoteAuthorityAdmission(fixture.policyPath), now: () => now}); - const first = claimRequest("a", "todo_takeover"); - assert.equal(result(await service.dispatch(credentials.a, first)).status, "applied"); - now = new Date("2026-11-11T10:00:00Z"); - assert.equal(result(await service.dispatch(credentials.b, claimRequest("b", "todo_takeover"))).status, "applied"); - const before = await fixture.store.loadAuthority(); - const late: RemoteAuthorityRequest = {...base("todo_takeover"), operation: "progress", request_id: "stale-progress", - claim_request_id: "claim-todo_takeover", note: "Must not be written"}; - const stale = await service.dispatch(credentials.a, late); - assert.equal(result(stale).status, "failed"); - assert.equal(result(await service.dispatch(credentials.a, first)).status, "failed"); - assert.deepEqual(await fixture.store.loadAuthority(), before); - assert.equal((await service.dispatch(credentials.a, first, "recover")).historical_only, true); - }); - await t.test("rotation between opening and reading cannot let a stale session adopt a new incarnation", async () => { - await fixture.seed(["todo_rotation"]); - let reached!: () => void, release!: () => void, opened = 0; - const paused = new Promise(resolve => {reached = resolve;}); - const gate = new Promise(resolve => {release = resolve;}); - const delayed: PostgreSqlAuthorityDatabase = {connect: async () => { - const connection = await fixture.runtimeDatabase.connect(); - return {...connection, query: async (sql, args) => { - const result = await connection.query(sql, args); - if (opened < 2 && sql.includes("authority_store_metadata")) { - opened++; if (opened === 2) reached(); await gate; - } - return result; - }}; - }}; - const service = new RemoteAuthorityService({database: delayed, admit: fileRemoteAuthorityAdmission(fixture.policyPath)}); - const before = await fixture.store.loadAuthority(); - const pending = service.dispatch(credentials.a, claimRequest("a", "todo_rotation")); - const recovering = service.dispatch(credentials.a, claimRequest("a", "todo_rotation", "unknown-operation"), "recover"); - await paused; - const next = `postgresql:${"d".repeat(32)}`; - try {assert.equal((await rotatePostgreSqlAuthorityStoreIdentity(fixture.database, serviceIdentity, next)).status, "rotated");} - finally {release();} - assert.equal((await pending).status, "unavailable"); - const recovery = await recovering; - assert.equal(recovery.status, "unavailable", "negative receipt evidence must belong to the admitted incarnation"); - assert.equal(recovery.reason_code, "remote_authority_incarnation_changed"); - const after = await fixture.store.loadAuthority(); - assert.equal(after.status, "loaded"); - if (before.status === "loaded" && after.status === "loaded") assert.deepEqual(after.head, before.head); - assert.equal((await call(claimRequest("a", "todo_rotation"))).status, "unavailable"); - await fixture.writePolicy(servicePolicy(next)); - assert.equal((await call(claimRequest("a", "todo_rotation"))).status, "rejected"); - const historic = {...claimRequest("a", "todo_disconnect"), store_identity: next}; - assert.equal((await call(historic, "recover")).historical_only, true); - assert.equal((await call({...claimRequest("a", "todo_rotation", "unknown-operation"), store_identity: next}, "recover")).status, "missing"); - assert.equal(result(await call({...claimRequest("a", "todo_rotation"), store_identity: next})).status, "applied"); - }); - await t.test("TLS verification failures never trigger a retry or expose a credential", async () => { - const before = await fixture.store.loadAuthority(); - const failed = await callRemoteAuthority(running.endpoint, credentials.a, claimRequest("a"), {timeoutMs: 1000}); - assert.equal(failed.status, "ambiguous"); - assert(!JSON.stringify(failed).includes(credentials.a)); - assert.deepEqual(await fixture.store.loadAuthority(), before); - }); - await t.test("negative receipt and identity share one PostgreSQL snapshot across mid-read rotation", async () => { - const before = await fixture.store.storeIdentity(); assert.equal(before.status, "available"); - if (before.status !== "available") return; - const next = `postgresql:${"e".repeat(32)}`; - let rotated = false; - const delayed: PostgreSqlAuthorityDatabase = {connect: async () => { - const connection = await fixture.runtimeDatabase.connect(); - return {...connection, query: async (sql, args) => { - const result = await connection.query(sql, args); - if (!rotated && sql.includes("authority_store_metadata")) { - rotated = true; - assert.equal((await rotatePostgreSqlAuthorityStoreIdentity(fixture.database, before.store_identity, next)).status, "rotated"); - } - return result; - }}; - }}; - const store = new PostgreSqlAuthorityStore(delayed, {tenant_id: "tenant-one", goal_id: "goal-one"}); - assert.deepEqual(await store.readReceiptSnapshot("unknown-operation"), { - status: "snapshot", store_identity: before.store_identity, receipt: {status: "missing"}, - }); - assert.deepEqual(await fixture.store.storeIdentity(), {status: "available", store_identity: next}); - assert.deepEqual(await store.readReceiptSnapshot("unknown-operation"), { - status: "snapshot", store_identity: next, receipt: {status: "missing"}, - }); - assert.deepEqual(await store.readReceipt("unknown-operation"), {status: "missing"}); - }); -}); - -test("disposable database cleanup waits for client disconnects", {skip: !connectionString}, async t => { - // Widen the same Pool.end()/socket-close race seen in CI, using real PG - // connections. An early DROP DATABASE FORCE would emit an uncaught error. - const end = Client.prototype.end; - const delayed = t.mock.method(Client.prototype, "end", function(this: Client, ...args: Parameters) { - setTimeout(() => end.apply(this, args), 100); - }); - await t.test("owns an idle connection until fixture teardown", async child => { - const fixture = await postgresRemoteFixture(child, connectionString!); - await fixture.seed(["todo_cleanup"]); - }); - assert(delayed.mock.callCount() > 0); -}); diff --git a/tests/control_plane_ts/remote_authority.test.ts b/tests/control_plane_ts/remote_authority.test.ts deleted file mode 100644 index 0c4d8012d7..0000000000 --- a/tests/control_plane_ts/remote_authority.test.ts +++ /dev/null @@ -1,435 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import {request as httpsRequest} from "node:https"; -import {connect as tlsConnect} from "node:tls"; -import {connect, type Socket} from "node:net"; -import {once} from "node:events"; -import {mkdtemp, writeFile, rm} from "node:fs/promises"; -import {tmpdir} from "node:os"; -import {join} from "node:path"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import {decodeRemoteAuthorityRequest, decodeRemoteAuthorityResponse, remoteAuthorityOperationId, - remoteAuthorityResponse, remoteAuthoritySnapshotDigest, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; -import {decodeRemoteAuthorityPolicy, remoteAuthorityPolicyAdmission, readRemoteAuthorityPolicy} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; -import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts"; -import {callRemoteAuthority, createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts"; -import {claimRequest, credentials, servicePolicy, tlsFixture} from "./remote_authority_fixture.ts"; - -test("remote commands reject caller authority, time, raw state and unknown operations", () => { - const request = claimRequest("a"); - assert.deepEqual(decodeRemoteAuthorityRequest(request), request); - for (const field of ["now", "observed_at", "registered_agents", "principal_id", "next_projection", "runtime_root", "credential", "__proto__"]) { - assert.throws(() => decodeRemoteAuthorityRequest({...request, [field]: "untrusted"})); - } - for (const patch of [{operation: "commitAuthority"}, {goal_id: ""}, {todo_id: "todo-invalid"}, {request_id: " a"}, {store_identity: "file:invalid"}]) { - assert.throws(() => decodeRemoteAuthorityRequest({...request, ...patch})); - } - const {request_id: _, ...base} = request as ReturnType & {request_id: string}; - assert.throws(() => decodeRemoteAuthorityRequest({...base, operation: "renew", idempotency_key: "key", expected_version: 1, ttl_seconds: -1})); -}); - -test("policy binds credentials, tenant, Goal, actor, operation and incarnation before database access", async () => { - const policy = decodeRemoteAuthorityPolicy(servicePolicy()), request = claimRequest("a"); - let connections = 0; - const service = new RemoteAuthorityService({database: {connect: async () => {connections++; throw new Error("must not connect");}}, - admit: async (credential, command) => remoteAuthorityPolicyAdmission(policy, credential, command)}); - for (const patch of [{tenant_id: "tenant-two"}, {goal_id: "goal-two"}, {actor_id: "actor-b"}, - {store_identity: `postgresql:${"d".repeat(32)}`}]) { - assert.equal((await service.dispatch(credentials.a, {...request, ...patch})).status, "rejected"); - } - assert.equal((await service.dispatch(credentials.b, request)).status, "rejected"); - assert.equal((await service.dispatch("wrong", request)).reason_code, "principal_unauthenticated"); - const readOnly = servicePolicy(); readOnly.principals[0].grants[0].operations = ["read"]; - assert.equal(remoteAuthorityPolicyAdmission(decodeRemoteAuthorityPolicy(readOnly), credentials.a, request).status, "rejected"); - assert.equal(connections, 0); -}); - -for (const operation of ["renew", "release"] as const) { - test(`remote ${operation} versions use the canonical non-negative safe-integer range`, () => { - const {request_id: _, ...scope} = claimRequest("a") as ReturnType & {request_id: string}; - const request = {...scope, operation, idempotency_key: "existing-key", ...(operation === "renew" ? {ttl_seconds: 600} : {})}; - for (const expected_version of [0, 1, Number.MAX_SAFE_INTEGER]) { - const valid = {...request, expected_version}; - assert.deepEqual(decodeRemoteAuthorityRequest(valid), valid); - } - for (const expected_version of [-1, 0.5, Number.MAX_SAFE_INTEGER + 1, Number.NaN, Infinity, "0", null, true]) { - assert.throws(() => decodeRemoteAuthorityRequest({...request, expected_version})); - } - }); -} - -test("missing-lease release responses require explicit no-op evidence", () => { - const {request_id: _, ...scope} = claimRequest("a") as ReturnType & {request_id: string}; - const request = decodeRemoteAuthorityRequest({...scope, operation: "release", idempotency_key: "existing-key", expected_version: 0}); - const result: JsonObject = {status: "no_change", changed: false, released: false, missing: true, - handoff_mode: "hard_lease", provider_revision: `${request.store_identity}:3`, cursor: "3"}; - const wrap = (payload: JsonObject) => remoteAuthorityResponse(request, "execute", { - status: "result", operation_id: remoteAuthorityOperationId(request, "principal-a"), result: payload}); - assert.deepEqual(decodeRemoteAuthorityResponse(wrap(result), request, "execute"), wrap(result)); - for (const field of ["missing", "released", "changed", "provider_revision", "cursor"]) { - const incomplete = {...result}; delete incomplete[field]; - assert.throws(() => decodeRemoteAuthorityResponse(wrap(incomplete), request, "execute")); - } - for (const patch of [{released: true}, {changed: true}, {missing: false}, {lease: {}}, {status: "applied"}]) { - assert.throws(() => decodeRemoteAuthorityResponse(wrap({...result, ...patch}), request, "execute")); - } -}); - -test("scoped read responses require their Goal reference and exclude global authority metadata", () => { - const {request_id: _, ...scope} = claimRequest("a"); - const request = decodeRemoteAuthorityRequest({...scope, operation: "read"}); - const goal_ref = {goal_id: request.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; - const payload = {status: "found", goal_ref, todo: {todo_id: request.todo_id, status: "open"}, lease: null}; - const wrap = (value: JsonObject) => remoteAuthorityResponse(request, "execute", value, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); - assert.deepEqual(decodeRemoteAuthorityResponse(wrap(payload), request, "execute"), wrap(payload)); - for (const invalid of [ - {...payload, goal_ref: {goal_id: request.goal_id}}, - {...payload, goal_ref: {...goal_ref, goal_id: "other-goal"}}, - {...payload, provider_revision: `${request.store_identity}:2`}, - {...payload, cursor: "2"}, - ]) assert.throws(() => decodeRemoteAuthorityResponse(wrap(invalid), request, "execute")); -}); - -test("scoped response nested records reject fields outside their read projection", () => { - const {request_id: _, ...scope} = claimRequest("a"); - const goal_ref = {goal_id: scope.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; - const todo = {todo_id: scope.todo_id, status: "open", claimed_by: "actor-a"}; - const lease = {owner: "actor-a", status: "active", expires_at: "2099-01-01T00:00:00Z", version: 1, lease_epoch: 1}; - const read = decodeRemoteAuthorityRequest({...scope, operation: "read"}); - const snapshot = decodeRemoteAuthorityRequest({...scope, operation: "snapshot"}); - const wrap = (request: typeof read, payload: JsonObject) => remoteAuthorityResponse(request, "execute", - {...payload, goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); - const found = (row: JsonObject, held: JsonObject) => wrap(read, {status: "found", todo: row, lease: held}); - const snap = (row: JsonObject, held: JsonObject) => wrap(snapshot, {status: "snapshot", goal_id: scope.goal_id, - store_identity: scope.store_identity, todos: [row], leases: [held], - receipt_sha256: remoteAuthoritySnapshotDigest(snapshot, undefined, [row], [held], goal_ref)}); - const snapshotTodo = {...todo, role: "agent", priority: "P1"}; - const snapshotLease = {...lease, goal_id: scope.goal_id, todo_id: scope.todo_id}; - assert.deepEqual(decodeRemoteAuthorityResponse(found(todo, lease), read, "execute"), found(todo, lease)); - assert.deepEqual(decodeRemoteAuthorityResponse(snap(snapshotTodo, snapshotLease), snapshot, "execute"), snap(snapshotTodo, snapshotLease)); - for (const field of ["note", "note_omitted", "text", "idempotency_key", "provider_revision", "cursor", "unknown"]) { - const value = field === "note_omitted" ? true : "must-not-cross-the-read-boundary"; - assert.throws(() => decodeRemoteAuthorityResponse(found({...todo, [field]: value}, lease), read, "execute"), `read Todo ${field}`); - assert.throws(() => decodeRemoteAuthorityResponse(found(todo, {...lease, [field]: value}), read, "execute"), `read lease ${field}`); - // A matching digest cannot authorize extra fields in a scoped snapshot. - assert.throws(() => decodeRemoteAuthorityResponse(snap({...snapshotTodo, [field]: value}, snapshotLease), snapshot, "execute"), `snapshot Todo ${field}`); - assert.throws(() => decodeRemoteAuthorityResponse(snap(snapshotTodo, {...snapshotLease, [field]: value}), snapshot, "execute"), `snapshot lease ${field}`); - } - const trusted = remoteAuthorityResponse(read, "execute", {status: "found", provider_revision: `${scope.store_identity}:3`, - todo: {...todo, note: "trusted v0 note"}, lease: {...lease, idempotency_key: "trusted-v0-key"}}); - assert.deepEqual(decodeRemoteAuthorityResponse(trusted, read, "execute"), trusted); -}); - -test("scoped historical receipt responses expose proof for the original Todo without its execution payload", () => { - const request = claimRequest("a"); - const goal_ref = {goal_id: request.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; - const payload = {status: "found", historical_only: true, goal_ref, todo_id: request.todo_id, - operation_id: remoteAuthorityOperationId(request, "principal-a"), receipt_sha256: "d".repeat(64)}; - const wrap = (value: JsonObject) => remoteAuthorityResponse(request, "recover", value, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); - assert.deepEqual(decodeRemoteAuthorityResponse(wrap(payload), request, "recover"), wrap(payload)); - for (const invalid of [ - {...payload, todo_id: "todo_other"}, - {...payload, historical_only: false}, - {...payload, result: {lease: {idempotency_key: "out-of-scope"}}}, - {...payload, cursor: "2"}, - ]) assert.throws(() => decodeRemoteAuthorityResponse(wrap(invalid), request, "recover")); -}); - -test("invalid policy cannot silently broaden or retain grants", async t => { - for (const mutate of [(p: ReturnType) => p.principals.push(p.principals[0]), - (p: ReturnType) => p.scopes.push(p.scopes[0]), - (p: ReturnType) => p.principals[0].grants[0].operations.push("arbitrary-write"), - (p: ReturnType) => p.principals[0].grants[0].actor_id = "unregistered"]) { - const policy = servicePolicy(); mutate(policy); assert.throws(() => decodeRemoteAuthorityPolicy(policy)); - } - const root = await mkdtemp(join(tmpdir(), "loopx-remote-policy-")), path = join(root, "policy.json"); - t.after(() => rm(root, {recursive: true, force: true})); - await writeFile(path, JSON.stringify(servicePolicy())); - assert.deepEqual(await readRemoteAuthorityPolicy(path), servicePolicy()); - await writeFile(path, JSON.stringify(servicePolicy()) + " ".repeat(256 * 1024)); - await assert.rejects(readRemoteAuthorityPolicy(path), /too large/u); -}); - -test("client requires verified TLS and does not put credentials in URLs", async () => { - for (const endpoint of ["http://localhost/", "https://user:secret@localhost/", "https://localhost/?credential=x", "https://localhost/path"]) { - await assert.rejects(callRemoteAuthority(endpoint, credentials.a, claimRequest("a"))); - } - assert.throws(() => createRemoteAuthorityServer({} as RemoteAuthorityService, {})); -}); - -test("TLS listener bounds input and admission, and drains admitted requests", async t => { - const tls = await tlsFixture(t); - let admissions = 0, release!: () => void, entered!: () => void; - const gate = new Promise(resolve => {release = resolve;}); - const started = new Promise(resolve => {entered = resolve;}); - const service = new RemoteAuthorityService({database: {connect: async () => {throw new Error("Unexpected database access");}}, - admit: async () => {admissions++; entered(); await gate; return {status: "rejected", reason_code: "fixture_denial"};}}); - const listener = createRemoteAuthorityServer(service, tls, 1); - const endpoint = `https://127.0.0.1:${await listener.listen()}`; - t.after(async () => {release(); if (listener.server.listening) await listener.close();}); - async function raw(body: string, headers: Record = {}) { - return new Promise((resolve, reject) => { - const outgoing = httpsRequest(new URL("/v0/tasks/execute", endpoint), {method: "POST", ca: tls.cert, - headers: {Authorization: `Bearer ${credentials.a}`, "Content-Type": "application/json", ...headers}}, response => { - response.resume(); response.on("end", () => resolve(response.statusCode!)); - }); - outgoing.on("error", reject); outgoing.end(body); - }); - } - assert.equal(await raw(JSON.stringify({...claimRequest("a"), now: "caller-clock"})), 400); - const {request_id: _, ...scope} = claimRequest("a") as ReturnType & {request_id: string}; - for (const operation of ["renew", "release"]) { - assert.equal(await raw(JSON.stringify({...scope, operation, idempotency_key: "existing-key", expected_version: -1, - ...(operation === "renew" ? {ttl_seconds: 600} : {})})), 400); - } - assert.equal(await raw("x".repeat(17 * 1024)), 413); - assert.equal(await raw(JSON.stringify(claimRequest("a")), {Origin: "https://unrelated.invalid"}), 401); - assert.equal(admissions, 0); - const pending = callRemoteAuthority(endpoint, credentials.a, claimRequest("a"), {ca: tls.cert}); - await started; - assert.equal((await callRemoteAuthority(endpoint, credentials.a, claimRequest("a"), {ca: tls.cert})).status, "ambiguous"); - assert.equal(admissions, 1); - let closed = false; - const stopping = listener.close().then(() => {closed = true;}); - await new Promise(resolve => setImmediate(resolve)); - assert.equal(closed, false); - release(); assert.equal((await pending).reason_code, "fixture_denial"); - await stopping; assert.equal(closed, true); -}); - -test("shutdown bounds incomplete connections while still waiting for dispatched work", {timeout: 20000}, async t => { - const tls = await tlsFixture(t); - let admissions = 0, entered!: () => void, release!: () => void; - const started = new Promise(resolve => {entered = resolve;}); - const gate = new Promise(resolve => {release = resolve;}); - const service = new RemoteAuthorityService({database: {connect: async () => {throw new Error("Unexpected database access");}}, - admit: async () => {admissions++; entered(); await gate; return {status: "rejected", reason_code: "fixture_denial"};}}); - const listener = createRemoteAuthorityServer(service, tls, 4), port = await listener.listen(); - const sockets: Socket[] = []; - let stopping: Promise | undefined; - t.after(async () => {release(); sockets.forEach(socket => socket.destroy()); await (stopping ?? listener.close());}); - const own = (socket: T) => {sockets.push(socket); socket.on("error", () => {}); socket.resume(); return socket;}; - // A peer can stall before TLS, before the HTTP header terminator, or in the - // body. None of these peers has submitted a complete command to dispatch. - const handshake = own(connect({host: "127.0.0.1", port})); - await once(handshake, "connect"); - const headers = own(tlsConnect({host: "127.0.0.1", port, ca: tls.cert})); - await once(headers, "secureConnect"); - headers.write("POST /v0/tasks/execute HTTP/1.1\r\nHost: localhost\r\n"); - const body = own(tlsConnect({host: "127.0.0.1", port, ca: tls.cert})); - await once(body, "secureConnect"); - const receiving = once(listener.server, "request"); - body.write(`POST /v0/tasks/execute HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer ${credentials.a}\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n{`); - await receiving; - const pending = callRemoteAuthority(`https://127.0.0.1:${port}`, credentials.a, claimRequest("a"), {ca: tls.cert}); - await started; - let closed = false; - const peersClosed = Promise.all(sockets.map(socket => new Promise(resolve => socket.once("close", resolve)))); - stopping = listener.close().then(() => {closed = true;}); - let deadline: ReturnType | undefined; - try { - await Promise.race([peersClosed, new Promise((_, reject) => { - deadline = setTimeout(() => reject(new Error("incomplete peers outlived shutdown drain")), 10000); - })]); - // A lost socket is not a rollback: close must retain the database pool - // until the already-dispatched service operation has actually settled. - assert.equal(closed, false); - assert.equal(admissions, 1); - release(); - assert.equal((await pending).status, "ambiguous"); - await stopping; - assert.equal(closed, true); - } finally {clearTimeout(deadline);} -}); - -test("a body completed after shutdown starts cannot dispatch a new command", async t => { - const tls = await tlsFixture(t); - let admissions = 0; - const service = new RemoteAuthorityService({database: {connect: async () => {throw new Error("Unexpected database access");}}, - admit: async () => {admissions++; return {status: "rejected", reason_code: "fixture_denial"};}}); - const listener = createRemoteAuthorityServer(service, tls), port = await listener.listen(); - const socket = tlsConnect({host: "127.0.0.1", port, ca: tls.cert}); - socket.on("error", () => {}); socket.resume(); - let stopping: Promise | undefined; - t.after(async () => {socket.destroy(); await (stopping ?? listener.close());}); - await once(socket, "secureConnect"); - const body = JSON.stringify(claimRequest("a")), receiving = once(listener.server, "request"); - socket.write(`POST /v0/tasks/execute HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer ${credentials.a}\r\nContent-Type: application/json\r\nContent-Length: ${Buffer.byteLength(body)}\r\nConnection: close\r\n\r\n${body.slice(0, 1)}`); - await receiving; - stopping = listener.close(); - socket.write(body.slice(1)); - await stopping; - assert.equal(admissions, 0); -}); - -test("complete, block, defer, todo_add and snapshot request decoding validates typed lifecycle semantics", () => { - const base = { - schema_version: "loopx_remote_authority_request_v0" as const, - tenant_id: "tenant-one", - goal_id: "goal-one", - actor_id: "actor-a", - store_identity: `postgresql:${"c".repeat(32)}`, - todo_id: "todo_lifecycle_fixture", - }; - - // Complete - const completeReq = { - ...base, - operation: "complete" as const, - request_id: "req-complete-1", - claim_request_id: "req-claim-1", - evidence: "sha256:abcd verified delivery receipt", - note: "optional completion note", - }; - assert.deepEqual(decodeRemoteAuthorityRequest(completeReq), completeReq); - assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, evidence: ""})); - assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: ""})); - assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, claim_request_id: 12})); - assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, request_id: null})); - - // Block - const blockReq = { - ...base, - operation: "block" as const, - request_id: "req-block-1", - reason: "waiting on upstream schema", - resume_when: "todo_done:todo_schema_fixture", - note: "paused temporarily", - }; - assert.deepEqual(decodeRemoteAuthorityRequest(blockReq), blockReq); - assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, reason: ""})); - assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, resume_when: "schema_applied"})); - - // Defer - const deferReq = { - ...base, - operation: "defer" as const, - request_id: "req-defer-1", - reason: "resuming next week", - resume_when: "resume_at:2026-11-20T00:00:00Z", - }; - assert.deepEqual(decodeRemoteAuthorityRequest(deferReq), deferReq); - assert.throws(() => decodeRemoteAuthorityRequest({...deferReq, resume_when: ""})); - - // Todo add - const addReq = { - ...base, - operation: "todo_add" as const, - request_id: "req-add-1", - text: "[P1] new remote task", - priority: "P1", - role: "agent" as const, - task_class: "advancement_task", - required_write_scopes: ["loopx/control_plane/**"], - }; - assert.deepEqual(decodeRemoteAuthorityRequest(addReq), addReq); - assert.throws(() => decodeRemoteAuthorityRequest({...addReq, priority: "P9"})); - assert.throws(() => decodeRemoteAuthorityRequest({...addReq, role: "invalid"})); - - // Snapshot (todo_id is optional) - const snapshotGoal = { - schema_version: "loopx_remote_authority_request_v0" as const, - tenant_id: "tenant-one", - goal_id: "goal-one", - actor_id: "actor-a", - store_identity: `postgresql:${"c".repeat(32)}`, - operation: "snapshot" as const, - }; - assert.deepEqual(decodeRemoteAuthorityRequest(snapshotGoal), snapshotGoal); - const snapshotTodo = {...snapshotGoal, todo_id: "todo_lifecycle_fixture"}; - assert.deepEqual(decodeRemoteAuthorityRequest(snapshotTodo), snapshotTodo); - assert.throws(() => decodeRemoteAuthorityRequest({...snapshotTodo, todo_id: false})); -}); - -test("policy admits complete, block, defer, todo_add and snapshot grants and rejects ungranted operations", () => { - const policy = servicePolicy(); - const grant = policy.principals[0].grants[0]; - grant.operations = ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"]; - const decodedPolicy = decodeRemoteAuthorityPolicy(policy); - - const base = { - schema_version: "loopx_remote_authority_request_v0" as const, - tenant_id: "tenant-one", - goal_id: "goal-one", - actor_id: "actor-a", - store_identity: `postgresql:${"c".repeat(32)}`, - todo_id: "todo_one", - }; - - const completeReq = decodeRemoteAuthorityRequest({ - ...base, operation: "complete", request_id: "req-comp", claim_request_id: "req-claim", evidence: "evidence-1", - }); - const snapshotReq = decodeRemoteAuthorityRequest({ - ...base, operation: "snapshot", - }); - - const admComplete = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, completeReq); - assert.equal(admComplete.status, "allowed"); - - const admSnapshot = remoteAuthorityPolicyAdmission(decodedPolicy, credentials.a, snapshotReq); - assert.equal(admSnapshot.status, "allowed"); - - // Default-deny check: principal b does not have "complete" granted - policy.principals[1].grants[0].operations = ["read", "claim"]; - const decodedPolicyWithRestrictedB = decodeRemoteAuthorityPolicy(policy); - assert.equal(policy.principals[1].grants[0].operations.includes("complete" as any), false); - const admDenied = remoteAuthorityPolicyAdmission(decodedPolicyWithRestrictedB, credentials.b, { - ...completeReq, actor_id: "actor-b", - }); - assert.equal(admDenied.status, "rejected"); - assert.equal(admDenied.reason_code, "remote_authority_scope_denied"); -}); - -test("decodeRemoteAuthorityResponse validates snapshot and complete response payloads", () => { - const base = { - schema_version: "loopx_remote_authority_request_v0" as const, - tenant_id: "tenant-one", - goal_id: "goal-one", - actor_id: "actor-a", - store_identity: `postgresql:${"c".repeat(32)}`, - todo_id: "todo_one", - }; - const snapshotReq = decodeRemoteAuthorityRequest({...base, operation: "snapshot"}); - const snapResponse = (todos: JsonObject[], leases: JsonObject[] = []) => { - const facts = {provider_revision: `${base.store_identity}:10`, store_identity: base.store_identity, - goal_id: base.goal_id, todos, leases}; - return remoteAuthorityResponse(snapshotReq, "execute", {status: "snapshot", ...facts, - receipt_sha256: canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0", - tenant_id: base.tenant_id, ...facts})}); - }; - const todo = {todo_id: "todo_one", status: "open", role: "agent"}; - const valid = snapResponse([todo]); - assert.deepEqual(decodeRemoteAuthorityResponse(valid, snapshotReq, "execute"), valid); - assert.throws(() => decodeRemoteAuthorityResponse({...valid, receipt_sha256: "a".repeat(64)}, snapshotReq, "execute")); - assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([{...todo, todo_id: "todo_wrong"}]), snapshotReq, "execute")); - assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo, todo]), snapshotReq, "execute")); - const lease = {todo_id: "todo_one", goal_id: base.goal_id, owner: base.actor_id, status: "released", - version: 1, lease_epoch: 1}; - assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, goal_id: "wrong-goal"}]), snapshotReq, "execute")); - assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([], [lease]), snapshotReq, "execute")); - assert.throws(() => decodeRemoteAuthorityResponse(snapResponse([todo], [{...lease, idempotency_key: "must-not-leak"}]), snapshotReq, "execute")); - assert.throws(() => decodeRemoteAuthorityResponse(valid, snapshotReq, "recover")); - - const completeReq = decodeRemoteAuthorityRequest({ - ...base, operation: "complete", request_id: "req-c", claim_request_id: "req-cl", evidence: "proof", - }); - const opId = remoteAuthorityOperationId(completeReq, "principal-a"); - const compResponse = remoteAuthorityResponse(completeReq, "execute", { - status: "result", - operation_id: opId, - result: { - status: "applied", - changed: true, - completed_at: "2026-12-01T12:00:00Z", - completion_receipt_id: "receipt-1", - todo_id: "todo_one", - provider_revision: `${base.store_identity}:11`, - cursor: "11", - }, - }); - assert.deepEqual(decodeRemoteAuthorityResponse(compResponse, completeReq, "execute"), compResponse); - const incomplete = structuredClone(compResponse); - delete (incomplete.result as JsonObject).completion_receipt_id; - assert.throws(() => decodeRemoteAuthorityResponse(incomplete, completeReq, "execute")); -}); diff --git a/tests/control_plane_ts/remote_authority_client.test.ts b/tests/control_plane_ts/remote_authority_client.test.ts new file mode 100644 index 0000000000..62d5b5bd4a --- /dev/null +++ b/tests/control_plane_ts/remote_authority_client.test.ts @@ -0,0 +1,313 @@ +import assert from "node:assert/strict"; +import test, {type TestContext} from "node:test"; +import {createServer} from "node:https"; +import type {AddressInfo} from "node:net"; +import {execFile, spawn} from "node:child_process"; +import {promisify} from "node:util"; +import {mkdtemp, readFile, rm} from "node:fs/promises"; +import {tmpdir} from "node:os"; +import {join} from "node:path"; +import {fileURLToPath} from "node:url"; +import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; +import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; +import { + decodeRemoteAuthorityRequest, + decodeRemoteAuthorityResponse, + POSTGRESQL_STORE_IDENTITY_PATTERN, + REMOTE_AUTHORITY_REQUEST, + REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, + remoteAuthorityOperationId, + remoteAuthorityResponse, + remoteAuthoritySnapshotDigest, + type RemoteAuthorityMode, + type RemoteAuthorityRequest, +} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; +import {callRemoteAuthority} from "../../loopx/control_plane/coordination/remote_authority_http.ts"; + +const storeIdentity = `postgresql:${"c".repeat(32)}`; +const credential = `fixture-${"a".repeat(48)}`; +const repoRoot = fileURLToPath(new URL("../../", import.meta.url)); + +function claimRequest(todo = "todo_one", id = `claim-${todo}`): Extract { + return { + schema_version: REMOTE_AUTHORITY_REQUEST, + tenant_id: "tenant-one", + goal_id: "goal-one", + actor_id: "actor-a", + store_identity: storeIdentity, + todo_id: todo, + operation: "claim", + request_id: id, + }; +} + +async function tlsFixture(t: TestContext) { + const root = await mkdtemp(join(tmpdir(), "loopx-remote-client-tls-")); + t.after(() => rm(root, {recursive: true, force: true})); + const keyPath = join(root, "key.pem"), certPath = join(root, "cert.pem"); + await promisify(execFile)("openssl", [ + "req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", + "-subj", "/CN=localhost", "-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1", + "-keyout", keyPath, "-out", certPath, + ]); + return {root, keyPath, certPath, key: await readFile(keyPath), cert: await readFile(certPath)}; +} + +test("remote commands reject caller authority, time, raw state and unknown operations", () => { + assert.equal(POSTGRESQL_STORE_IDENTITY_PATTERN.test(storeIdentity), true); + assert.equal(POSTGRESQL_STORE_IDENTITY_PATTERN.test("file:invalid"), false); + const request = claimRequest(); + assert.deepEqual(decodeRemoteAuthorityRequest(request), request); + for (const field of ["now", "observed_at", "registered_agents", "principal_id", "next_projection", "runtime_root", "credential", "__proto__"]) { + assert.throws(() => decodeRemoteAuthorityRequest({...request, [field]: "untrusted"})); + } + for (const patch of [{operation: "commitAuthority"}, {goal_id: ""}, {todo_id: "todo-invalid"}, {request_id: " a"}, {store_identity: "file:invalid"}]) { + assert.throws(() => decodeRemoteAuthorityRequest({...request, ...patch})); + } + const {request_id: _, ...base} = request as ReturnType & {request_id: string}; + assert.throws(() => decodeRemoteAuthorityRequest({...base, operation: "renew", idempotency_key: "key", expected_version: 1, ttl_seconds: -1})); +}); + +for (const operation of ["renew", "release"] as const) { + test(`remote ${operation} versions use the canonical non-negative safe-integer range`, () => { + const {request_id: _, ...scope} = claimRequest() as ReturnType & {request_id: string}; + const request = {...scope, operation, idempotency_key: "existing-key", ...(operation === "renew" ? {ttl_seconds: 600} : {})}; + for (const expected_version of [0, 1, Number.MAX_SAFE_INTEGER]) { + const valid = {...request, expected_version}; + assert.deepEqual(decodeRemoteAuthorityRequest(valid), valid); + } + for (const expected_version of [-1, 0.5, Number.MAX_SAFE_INTEGER + 1, Number.NaN, Infinity, "0", null, true]) { + assert.throws(() => decodeRemoteAuthorityRequest({...request, expected_version})); + } + }); +} + +test("missing-lease release responses require explicit no-op evidence", () => { + const {request_id: _, ...scope} = claimRequest() as ReturnType & {request_id: string}; + const request = decodeRemoteAuthorityRequest({...scope, operation: "release", idempotency_key: "existing-key", expected_version: 0}); + const result: JsonObject = { + status: "no_change", changed: false, released: false, missing: true, + handoff_mode: "hard_lease", provider_revision: `${request.store_identity}:3`, cursor: "3", + }; + const wrap = (payload: JsonObject) => remoteAuthorityResponse(request, "execute", { + status: "result", operation_id: remoteAuthorityOperationId(request, "principal-a"), result: payload, + }); + assert.deepEqual(decodeRemoteAuthorityResponse(wrap(result), request, "execute"), wrap(result)); + for (const field of ["missing", "released", "changed", "provider_revision", "cursor"]) { + const incomplete = {...result}; delete incomplete[field]; + assert.throws(() => decodeRemoteAuthorityResponse(wrap(incomplete), request, "execute")); + } + for (const patch of [{released: true}, {changed: true}, {missing: false}, {lease: {}}, {status: "applied"}]) { + assert.throws(() => decodeRemoteAuthorityResponse(wrap({...result, ...patch}), request, "execute")); + } +}); + +test("scoped read and historical receipt responses enforce Goal binding and projection boundaries", () => { + const {request_id: _, ...scope} = claimRequest(); + const goal_ref = {goal_id: scope.goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; + const todo = {todo_id: scope.todo_id, status: "open", claimed_by: "actor-a"}; + const lease = {owner: "actor-a", status: "active", expires_at: "2099-01-01T00:00:00Z", version: 1, lease_epoch: 1}; + const read = decodeRemoteAuthorityRequest({...scope, operation: "read"}); + const snapshot = decodeRemoteAuthorityRequest({...scope, operation: "snapshot"}); + const wrap = (request: typeof read, payload: JsonObject) => remoteAuthorityResponse( + request, "execute", {...payload, goal_ref}, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, + ); + const found = (row: JsonObject, held: JsonObject) => wrap(read, {status: "found", todo: row, lease: held}); + const snap = (row: JsonObject, held: JsonObject) => wrap(snapshot, { + status: "snapshot", goal_id: scope.goal_id, store_identity: scope.store_identity, + todos: [row], leases: [held], + receipt_sha256: remoteAuthoritySnapshotDigest(snapshot, undefined, [row], [held], goal_ref), + }); + const snapshotTodo = {...todo, role: "agent", priority: "P1"}; + const snapshotLease = {...lease, goal_id: scope.goal_id, todo_id: scope.todo_id}; + assert.deepEqual(decodeRemoteAuthorityResponse(found(todo, lease), read, "execute"), found(todo, lease)); + assert.deepEqual(decodeRemoteAuthorityResponse(snap(snapshotTodo, snapshotLease), snapshot, "execute"), snap(snapshotTodo, snapshotLease)); + for (const field of ["note", "note_omitted", "text", "idempotency_key", "provider_revision", "cursor", "unknown"]) { + const value = field === "note_omitted" ? true : "must-not-cross-the-read-boundary"; + assert.throws(() => decodeRemoteAuthorityResponse(found({...todo, [field]: value}, lease), read, "execute")); + assert.throws(() => decodeRemoteAuthorityResponse(found(todo, {...lease, [field]: value}), read, "execute")); + assert.throws(() => decodeRemoteAuthorityResponse(snap({...snapshotTodo, [field]: value}, snapshotLease), snapshot, "execute")); + assert.throws(() => decodeRemoteAuthorityResponse(snap(snapshotTodo, {...snapshotLease, [field]: value}), snapshot, "execute")); + } + const claim = claimRequest(); + const historical = { + status: "found", historical_only: true, goal_ref, todo_id: claim.todo_id, + operation_id: remoteAuthorityOperationId(claim, "principal-a"), receipt_sha256: "d".repeat(64), + }; + const wrapRecover = (value: JsonObject) => remoteAuthorityResponse(claim, "recover", value, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); + assert.deepEqual(decodeRemoteAuthorityResponse(wrapRecover(historical), claim, "recover"), wrapRecover(historical)); + for (const invalid of [ + {...historical, todo_id: "todo_other"}, + {...historical, historical_only: false}, + {...historical, result: {lease: {idempotency_key: "out-of-scope"}}}, + {...historical, cursor: "2"}, + ]) assert.throws(() => decodeRemoteAuthorityResponse(wrapRecover(invalid), claim, "recover")); +}); + +test("complete, block, defer, todo_add and snapshot request/response decoding validates lifecycle semantics", () => { + const base = { + schema_version: REMOTE_AUTHORITY_REQUEST, + tenant_id: "tenant-one", + goal_id: "goal-one", + actor_id: "actor-a", + store_identity: storeIdentity, + todo_id: "todo_lifecycle_fixture", + }; + const completeReq = decodeRemoteAuthorityRequest({ + ...base, operation: "complete", request_id: "req-complete-1", + claim_request_id: "req-claim-1", evidence: "sha256:abcd verified delivery receipt", note: "optional completion note", + }); + assert.throws(() => decodeRemoteAuthorityRequest({...completeReq, evidence: ""})); + + const blockReq = decodeRemoteAuthorityRequest({ + ...base, operation: "block", request_id: "req-block-1", + reason: "waiting on upstream schema", resume_when: "todo_done:todo_schema_fixture", note: "paused temporarily", + }); + assert.throws(() => decodeRemoteAuthorityRequest({...blockReq, resume_when: "schema_applied"})); + + const deferReq = decodeRemoteAuthorityRequest({ + ...base, operation: "defer", request_id: "req-defer-1", + reason: "resuming next week", resume_when: "resume_at:2026-11-20T00:00:00Z", + }); + assert.throws(() => decodeRemoteAuthorityRequest({...deferReq, resume_when: ""})); + + const addReq = decodeRemoteAuthorityRequest({ + ...base, operation: "todo_add", request_id: "req-add-1", + text: "[P1] new remote task", priority: "P1", role: "agent", + task_class: "advancement_task", required_write_scopes: ["loopx/control_plane/**"], + }); + assert.throws(() => decodeRemoteAuthorityRequest({...addReq, priority: "P9"})); + + const snapshotReq = decodeRemoteAuthorityRequest({ + schema_version: REMOTE_AUTHORITY_REQUEST, + tenant_id: "tenant-one", goal_id: "goal-one", actor_id: "actor-a", + store_identity: storeIdentity, operation: "snapshot", todo_id: "todo_lifecycle_fixture", + }); + const todo = {todo_id: "todo_lifecycle_fixture", status: "open", role: "agent"}; + const facts = {provider_revision: `${base.store_identity}:10`, store_identity: base.store_identity, + goal_id: base.goal_id, todos: [todo], leases: []}; + const validSnap = remoteAuthorityResponse(snapshotReq, "execute", { + status: "snapshot", ...facts, + receipt_sha256: canonicalAuthoritySha256({schema_version: "loopx_goal_snapshot_receipt_v0", tenant_id: base.tenant_id, ...facts}), + }); + assert.deepEqual(decodeRemoteAuthorityResponse(validSnap, snapshotReq, "execute"), validSnap); + + const opId = remoteAuthorityOperationId(completeReq, "principal-a"); + const compResponse = remoteAuthorityResponse(completeReq, "execute", { + status: "result", operation_id: opId, + result: { + status: "applied", changed: true, completed_at: "2026-12-01T12:00:00Z", + completion_receipt_id: "receipt-1", todo_id: base.todo_id, + provider_revision: `${base.store_identity}:11`, cursor: "11", + }, + }); + assert.deepEqual(decodeRemoteAuthorityResponse(compResponse, completeReq, "execute"), compResponse); +}); + +test("callRemoteAuthority, CLI client and probe verify TLS and classify transport uncertainty", async t => { + for (const endpoint of ["http://localhost/", "https://user:secret@localhost/", "https://localhost/?credential=x", "https://localhost/path"]) { + await assert.rejects(callRemoteAuthority(endpoint, credential, claimRequest())); + } + await assert.rejects(callRemoteAuthority("https://localhost/", "short", claimRequest())); + + const tls = await tlsFixture(t); + let statusCode = 200; + let customBody: string | null = null; + const server = createServer({key: tls.key, cert: tls.cert, minVersion: "TLSv1.2"}, async (req, res) => { + res.setHeader("Content-Type", "application/json"); + if (req.headers.authorization !== `Bearer ${credential}`) { + res.writeHead(401).end(JSON.stringify({status: "rejected", reason_code: "unauthorized"})); + return; + } + const mode: RemoteAuthorityMode = req.url === "/v0/tasks/recover" ? "recover" : "execute"; + const chunks: Buffer[] = []; + for await (const chunk of req) chunks.push(Buffer.from(chunk)); + const parsed = decodeRemoteAuthorityRequest(JSON.parse(Buffer.concat(chunks).toString("utf8"))); + if (customBody !== null || statusCode !== 200) { + res.writeHead(statusCode).end(customBody ?? "{}"); + return; + } + if (parsed.operation === "read") { + res.writeHead(200).end(JSON.stringify(remoteAuthorityResponse(parsed, mode, { + status: "found", provider_revision: `${parsed.store_identity}:1`, + todo: {todo_id: parsed.todo_id, status: "open"}, lease: null, + }))); + return; + } + res.writeHead(200).end(JSON.stringify(remoteAuthorityResponse(parsed, mode, { + status: "found", historical_only: true, operation_id: remoteAuthorityOperationId(parsed, "principal-a"), + receipt_sha256: "b".repeat(64), provider_revision: `${parsed.store_identity}:2`, cursor: "2", + result: { + changed: true, todo_id: parsed.todo_id, claimed_by: parsed.actor_id, + claim_operation_id: remoteAuthorityOperationId(parsed, "principal-a"), handoff_mode: "hard_lease", + lease: { + goal_id: parsed.goal_id, todo_id: parsed.todo_id, owner: parsed.actor_id, + idempotency_key: "lease-1", status: "active", expires_at: "2099-01-01T00:00:00Z", version: 1, lease_epoch: 1, + }, + }, + }))); + }); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", () => resolve()); + }); + t.after(() => new Promise(resolve => server.close(() => resolve()))); + const endpoint = `https://127.0.0.1:${(server.address() as AddressInfo).port}`; + + // Untrusted self-signed cert without CA fails closed to transport_uncertain + const untrusted = await callRemoteAuthority(endpoint, credential, claimRequest()); + assert.equal(untrusted.status, "ambiguous"); + assert.equal(untrusted.reason_code, "remote_authority_transport_uncertain"); + + // Verified CA succeeds for read & recover + const readReq = decodeRemoteAuthorityRequest({ + schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: "tenant-one", goal_id: "goal-one", + actor_id: "actor-a", store_identity: storeIdentity, todo_id: "todo_probe", operation: "read", + }); + const readRes = await callRemoteAuthority(endpoint, credential, readReq, {ca: tls.cert}); + assert.equal(readRes.status, "found"); + + // Non-200 and oversized payloads map mutations to ambiguous and reads/recoveries to unavailable + statusCode = 503; + assert.equal((await callRemoteAuthority(endpoint, credential, readReq, {ca: tls.cert})).status, "unavailable"); + assert.equal((await callRemoteAuthority(endpoint, credential, claimRequest(), {ca: tls.cert})).status, "ambiguous"); + statusCode = 200; + customBody = "x".repeat(70 * 1024); + assert.equal((await callRemoteAuthority(endpoint, credential, claimRequest(), {ca: tls.cert, mode: "recover"})).status, "unavailable"); + customBody = null; + + // Subprocess smoke for examples/coordination/remote-authority-client.ts and remote_authority_probe.ts + const runChild = (script: string, args: string[], stdinText: string, envExtra: Record = {}) => + new Promise<{code: number | null; stdout: string}>((resolve, reject) => { + const child = spawn(process.execPath, ["--no-warnings", "--experimental-strip-types", script, ...args], { + cwd: repoRoot, env: {PATH: process.env.PATH ?? "", ...envExtra}, + }); + let stdout = ""; + child.stdout.setEncoding("utf8"); + child.stdout.on("data", chunk => {stdout += chunk;}); + child.on("error", reject); + child.on("close", code => resolve({code, stdout})); + child.stdin.end(stdinText); + }); + + const cliResult = await runChild( + "examples/coordination/remote-authority-client.ts", + ["--endpoint", endpoint, "--ca", tls.certPath, "--recover"], + JSON.stringify(claimRequest()), + {LOOPX_AUTHORITY_CREDENTIAL: credential}, + ); + assert.equal(cliResult.code, 0); + assert.equal(JSON.parse(cliResult.stdout).status, "found"); + + const probeResult = await runChild( + "loopx/control_plane/coordination/remote_authority_probe.ts", + [], + JSON.stringify({ + endpoint, credential, ca_file: tls.certPath, + tenant_id: "tenant-one", goal_id: "goal-one", actor_id: "actor-a", + store_identity: storeIdentity, probe_todo_id: "todo_probe", + }), + ); + assert.equal(probeResult.code, 0); + assert.equal(probeResult.stdout, "verified\n"); +}); diff --git a/tests/control_plane_ts/remote_authority_fixture.ts b/tests/control_plane_ts/remote_authority_fixture.ts deleted file mode 100644 index 892ebbf116..0000000000 --- a/tests/control_plane_ts/remote_authority_fixture.ts +++ /dev/null @@ -1,156 +0,0 @@ -import assert from "node:assert/strict"; -import {createHash, randomBytes} from "node:crypto"; -import {execFile, spawn, type ChildProcess} from "node:child_process"; -import {promisify} from "node:util"; -import {mkdtemp, readFile, writeFile, rename, rm} from "node:fs/promises"; -import {tmpdir} from "node:os"; -import {join} from "node:path"; -import {fileURLToPath} from "node:url"; -import type {TestContext} from "node:test"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import {Pool, type PoolConfig} from "pg"; -import {installPostgreSqlAuthorityStoreSchema, PostgreSqlAuthorityStore, - type PostgreSqlAuthorityDatabase} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import {authorityProjectionFixture} from "./authority_projection_fixture.ts"; -import {REMOTE_AUTHORITY_REQUEST, type RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {REMOTE_AUTHORITY_POLICY} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; - -export const serviceIdentity = `postgresql:${"c".repeat(32)}`; -export const credentials = {a: `fixture-${"a".repeat(48)}`, b: `fixture-${"b".repeat(48)}`}; -const repo = fileURLToPath(new URL("../../", import.meta.url)); -const nodeFlags = ["--no-warnings", "--experimental-sqlite", "--experimental-strip-types"]; -// Real clients get no inherited database URL or administrative fixture secrets. -const childEnvironment = Object.fromEntries(["PATH", "SystemRoot", "TEMP", "TMP", "TMPDIR"] - .flatMap(key => process.env[key] === undefined ? [] : [[key, process.env[key]!]])); -export function servicePolicy(identity = serviceIdentity) { - return {schema_version: REMOTE_AUTHORITY_POLICY, - scopes: [{tenant_id: "tenant-one", goal_id: "goal-one", store_identity: identity, registered_agents: ["actor-a", "actor-b"]}], - principals: ["a", "b"].map(actor => ({principal_id: `principal-${actor}`, - credential_sha256: createHash("sha256").update(credentials[actor as "a" | "b"]).digest("hex"), - grants: [{tenant_id: "tenant-one", goal_id: "goal-one", actor_id: `actor-${actor}`, - operations: ["read", "claim", "progress", "renew", "release", "complete", "block", "defer", "todo_add", "snapshot"]}]}))}; -} -export function claimRequest(actor: "a" | "b", todo = "todo_one", id = `claim-${todo}`): Extract { - return {schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: "tenant-one", goal_id: "goal-one", - actor_id: `actor-${actor}`, store_identity: serviceIdentity, todo_id: todo, operation: "claim", request_id: id}; -} -export async function tlsFixture(t: TestContext) { - const root = await mkdtemp(join(tmpdir(), "loopx-authority-tls-")); - t.after(() => rm(root, {recursive: true, force: true})); - const keyPath = join(root, "key.pem"), certPath = join(root, "cert.pem"); - await promisify(execFile)("openssl", ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "1", - "-subj", "/CN=localhost", "-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1", "-keyout", keyPath, "-out", certPath]); - return {root, keyPath, certPath, key: await readFile(keyPath), cert: await readFile(certPath)}; -} -export async function postgresRemoteFixture(t: TestContext, connectionString: string) { - const tls = await tlsFixture(t), suffix = randomBytes(8).toString("hex"); - const name = `remote_${suffix}`, role = `remote_role_${suffix}`, password = randomBytes(24).toString("hex"); - const disconnects = new Map>>(); - function trackedPool(options: PoolConfig) { - const pool = new Pool(options), pending = new Set>(); - disconnects.set(pool, pending); - pool.on("connect", client => { - const closed = new Promise(resolve => client.once("end", resolve)); - pending.add(closed); void closed.then(() => pending.delete(closed)); - }); - return pool; - } - async function closePool(pool?: Pool) { - if (!pool) return; - // Pool.end can resolve after removing clients from its bookkeeping but - // before their sockets close. Never race database removal with that gap. - await pool.end(); await Promise.all(disconnects.get(pool)!); - } - const admin = trackedPool({connectionString, max: 2}); - const processes = new Set(); - let databaseCreated = false, roleCreated = false; - let setup: Pool | undefined, runtime: Pool | undefined; - t.after(async () => { - try { - await Promise.all([...processes].map(stop)); - await closePool(runtime); await closePool(setup); - if (databaseCreated) await admin.query(`DROP DATABASE ${name}`); - if (roleCreated) await admin.query(`DROP ROLE ${role}`); - } finally {await closePool(admin);} - }); - await admin.query(`CREATE DATABASE ${name}`); - databaseCreated = true; - const url = new URL(connectionString); url.pathname = `/${name}`; - setup = trackedPool({connectionString: url.href, max: 8}); - const db = (pool: Pool): PostgreSqlAuthorityDatabase => ({connect: async () => { - const client = await pool.connect(); - return {query: (sql, args) => client.query(sql, args ? [...args] : undefined), release: error => client.release(error)}; - }}); - const database = db(setup); - await installPostgreSqlAuthorityStoreSchema(database, serviceIdentity); - await setup.query(`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT`); - roleCreated = true; - await setup.query(`GRANT CONNECT ON DATABASE ${name} TO ${role}`); - await setup.query(`GRANT USAGE ON SCHEMA loopx_control_plane TO ${role}`); - await setup.query(`GRANT SELECT ON loopx_control_plane.authority_store_metadata TO ${role}`); - await setup.query(`GRANT SELECT ON loopx_control_plane.authority_checkpoints TO ${role}`); - await setup.query(`GRANT SELECT, INSERT, UPDATE ON loopx_control_plane.authority_heads TO ${role}`); - await setup.query(`GRANT SELECT, INSERT ON loopx_control_plane.authority_commits, - loopx_control_plane.authority_events, loopx_control_plane.authority_receipts TO ${role}`); - const runtimeUrl = new URL(url); runtimeUrl.username = role; runtimeUrl.password = password; - runtime = trackedPool({connectionString: runtimeUrl.href, max: 8, connectionTimeoutMillis: 2000}); - async function stop(process: ChildProcess) { - if (process.exitCode !== null || process.signalCode !== null) return; - await new Promise(resolve => { - const timer = setTimeout(() => process.kill("SIGKILL"), 10000); - process.once("exit", () => {clearTimeout(timer); processes.delete(process); resolve();}); - process.kill("SIGTERM"); - }); - } - const policyPath = join(tls.root, "policy.json"); - async function writePolicy(policy: unknown = servicePolicy()) { - await writeFile(`${policyPath}.next`, JSON.stringify(policy), {mode: 0o600}); - await rename(`${policyPath}.next`, policyPath); - } - await writePolicy(); - const store = new PostgreSqlAuthorityStore(database, {tenant_id: "tenant-one", goal_id: "goal-one"}); - async function seed(ids: string[], notes: Record = {}) { - const prior = await store.loadAuthority(); - const projection = authorityProjectionFixture("goal-one", ids.map(todo_id => ({todo_id, role: "agent", - status: "open", done: false, archive_state: "active", text: "Synthetic remote work", task_class: "advancement_task", - required_write_scopes: [`${todo_id}/**`], ...(notes[todo_id] === undefined ? {} : {note: notes[todo_id]})})), [], "native", {handoff_mode: "hard_lease"}); - const result = await store.commitAuthority({operation_id: `seed-${randomBytes(8).toString("hex")}`, - expected_provider_revision: prior.status === "loaded" ? prior.provider_revision : null, - next_projection: projection, events: [], receipts: []}); - assert.equal(result.status, "applied"); - } - async function start(port = 0) { - const child = spawn(process.execPath, [...nodeFlags, "examples/coordination/remote-authority-service.ts", - "--policy", policyPath, "--cert", tls.certPath, "--key", tls.keyPath, "--port", String(port)], - {cwd: repo, env: {...childEnvironment, LOOPX_AUTHORITY_DATABASE_URL: runtimeUrl.href}, stdio: ["ignore", "pipe", "pipe"]}); - processes.add(child); - const actualPort = await new Promise((resolve, reject) => { - let output = "", error = ""; - const timer = setTimeout(() => {child.kill("SIGKILL"); reject(new Error("Service startup timed out"));}, 15000); - child.stderr.on("data", chunk => {error += chunk;}); - child.once("exit", () => {clearTimeout(timer); reject(new Error(`Service stopped before listening: ${error}`));}); - child.stdout.on("data", chunk => { - output += chunk; - if (output.includes("\n")) {clearTimeout(timer); resolve(JSON.parse(output.split("\n")[0]).port);} - }); - }); - return {endpoint: `https://127.0.0.1:${actualPort}`, port: actualPort, stop: () => stop(child)}; - } - async function client(endpoint: string, actor: "a" | "b", request: RemoteAuthorityRequest, recover = false) { - const child = spawn(process.execPath, [...nodeFlags, "examples/coordination/remote-authority-client.ts", "--endpoint", endpoint, - "--ca", tls.certPath, ...(recover ? ["--recover"] : [])], - {cwd: repo, env: {...childEnvironment, LOOPX_AUTHORITY_CREDENTIAL: credentials[actor]}, stdio: ["pipe", "pipe", "pipe"]}); - processes.add(child); - let output = "", error = ""; - child.stdout.on("data", chunk => {output += chunk;}); child.stderr.on("data", chunk => {error += chunk;}); - const result = new Promise<{code: number | null; value: JsonObject}>((resolve, reject) => { - const timer = setTimeout(() => child.kill("SIGKILL"), 20000); - child.once("exit", code => { - clearTimeout(timer); processes.delete(child); - try {resolve({code, value: JSON.parse(output)});} catch {reject(new Error(`Client did not return a result: ${error}`));} - }); - }); - child.stdin.end(JSON.stringify(request)); return result; - } - return {...tls, database, runtimeDatabase: db(runtime), store, policyPath, writePolicy, seed, start, client}; -} diff --git a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts b/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts deleted file mode 100644 index 37f8271eac..0000000000 --- a/tests/control_plane_ts/remote_authority_lifecycle_conformance.ts +++ /dev/null @@ -1,315 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import {prepareCoordinationProjectionCommit, type CoordinationProjectionMutation} from "../../loopx/control_plane/coordination/coordination_projection.ts"; -import {executeCoordinationTodoTerminalLifecycle} from "../../loopx/control_plane/coordination/todo_terminal_lifecycle.ts"; -import {executeCanonicalTaskLeaseLifecycle} from "../../loopx/control_plane/coordination/task_lease_lifecycle.ts"; -import type {RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {postgresRemoteFixture, claimRequest, serviceIdentity} from "./remote_authority_fixture.ts"; - -function lifecycle(todo: string, operation: "renew" | "release", version = 0): RemoteAuthorityRequest { - const {request_id: _, ...scope} = claimRequest("a", todo) as Extract; - return {...scope, operation, idempotency_key: `existing-${todo}`, expected_version: version, - ...(operation === "renew" ? {ttl_seconds: 600} : {})} as RemoteAuthorityRequest; -} -function localInput(todo: string, operation: "renew" | "release") { - return {goal_id: "goal-one", todo_id: todo, operation, owner: "actor-a", registered_agents: ["actor-a", "actor-b"], - idempotency_key: `existing-${todo}`, expected_version: 0, ttl_seconds: operation === "renew" ? 600 : null, now: new Date()}; -} - -export function registerRemoteAuthorityLifecycleConformance(connectionString: string | undefined) { - test("remote lifecycle preserves existing canonical version-zero leases", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const running = await fixture.start(); - await fixture.seed(["todo_remote_renew", "todo_remote_release", "todo_local_renew", "todo_local_release"]); - const head = await fixture.store.loadAuthority(); assert.equal(head.status, "loaded"); - if (head.status !== "loaded") return; - const now = new Date(), mutations: CoordinationProjectionMutation[] = []; - for (const todo of head.head.todos as JsonObject[]) { - mutations.push({kind: "todo_upsert", todo: {...todo, claimed_by: "actor-a"}}, {kind: "lease_upsert", lease: { - schema_version: "task_lease_v0", goal_id: "goal-one", todo_id: todo.todo_id, owner: "actor-a", status: "active", - idempotency_key: `existing-${todo.todo_id}`, version: 0, lease_epoch: 1, write_scopes: [`${todo.todo_id}/**`], - acquired_at: now.toISOString(), updated_at: now.toISOString(), expires_at: new Date(+now + 3600000).toISOString(), - }}); - } - assert.equal((await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({goal_id: "goal-one", - operation_id: "existing-zero-leases", expected_provider_revision: head.provider_revision, projection: head.head, mutations}))).status, "applied"); - for (const operation of ["renew", "release"] as const) { - await t.test(`read then ${operation} accepts the same version zero as the canonical owner`, async () => { - const control = await executeCanonicalTaskLeaseLifecycle(fixture.store, localInput(`todo_local_${operation}`, operation)); - assert.equal(control.status, "applied"); - assert.equal((control.lease as JsonObject).version, operation === "renew" ? 1 : 0); - const todo = `todo_remote_${operation}`, original = lifecycle(todo, operation); - const {request_id: _, ...scope} = claimRequest("a", todo) as Extract; - const read = await fixture.client(running.endpoint, "a", {...scope, operation: "read"}); - assert.equal(read.code, 0); assert.equal((read.value.lease as JsonObject).version, 0); - assert.equal((read.value.lease as JsonObject).status, "active"); - const completed = await fixture.client(running.endpoint, "a", original); - assert.equal(completed.code, 0); - assert.equal((completed.value.result as JsonObject).status, "applied"); - const lease = (completed.value.result as JsonObject).lease as JsonObject; - assert.equal(lease.version, operation === "renew" ? 1 : 0); - assert.equal(lease.status, operation === "renew" ? "active" : "released"); - if (operation === "renew") { - const before = await fixture.store.loadAuthority(); - const stale = await fixture.client(running.endpoint, "a", lifecycle(todo, "release", 0)); - assert.equal(stale.code, 1); assert.equal((stale.value.result as JsonObject).reason_code, "version_mismatch"); - const staleRenew = {...original, ttl_seconds: 300}; - assert.equal((await fixture.client(running.endpoint, "a", staleRenew)).code, 1); - assert.deepEqual(await fixture.store.loadAuthority(), before); - assert.equal((await fixture.client(running.endpoint, "a", lifecycle(todo, "release", 1))).code, 0); - } - const beforeRecovery = await fixture.store.loadAuthority(); - const recovered = await fixture.client(running.endpoint, "a", original, true); - assert.equal(recovered.code, 0); assert.equal(recovered.value.historical_only, true); - assert.equal(((recovered.value.result as JsonObject).lease as JsonObject).version, lease.version); - assert.deepEqual(await fixture.store.loadAuthority(), beforeRecovery); - }); - } - }); - - test("remote version-zero release preserves the canonical missing-lease no-op", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const running = await fixture.start(); - await fixture.seed(["todo_remote_missing", "todo_local_missing"]); - const control = await executeCanonicalTaskLeaseLifecycle(fixture.store, localInput("todo_local_missing", "release")); - assert.equal(control.status, "no_change"); assert.equal(control.missing, true); assert.equal(control.released, false); - const request = lifecycle("todo_remote_missing", "release"), before = await fixture.store.loadAuthority(); - const completed = await fixture.client(running.endpoint, "a", request); - assert.equal(completed.code, 0); - const result = completed.value.result as JsonObject; - assert.equal(result.status, "no_change"); assert.equal(result.changed, false); - assert.equal(result.missing, true); assert.equal(result.released, false); assert.equal(result.lease, undefined); - const committed = await fixture.store.loadAuthority(); - assert.equal(committed.status, "loaded"); - if (committed.status === "loaded" && before.status === "loaded") assert.deepEqual(committed.head, before.head); - const replay = await fixture.client(running.endpoint, "a", request); - assert.equal(replay.code, 0); assert.equal((replay.value.result as JsonObject).status, "replayed"); - const recovered = await fixture.client(running.endpoint, "a", request, true); - assert.equal(recovered.code, 0); assert.equal(recovered.value.historical_only, true); - assert.equal((recovered.value.result as JsonObject).missing, true); - assert.deepEqual(await fixture.store.loadAuthority(), committed, "replay and recovery retain the original no-op receipt"); - }); - - test("remote completion keeps a recorded successor available to the next actor", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const running = await fixture.start(); - await fixture.seed(["todo_remote", "todo_local", "todo_next_remote", "todo_next_local"]); - const seed = await fixture.store.loadAuthority(); - assert.equal(seed.status, "loaded"); - if (seed.status !== "loaded") return; - const links: Record = { - todo_remote: {successor_todo_ids: ["todo_next_remote"]}, - todo_local: {successor_todo_ids: ["todo_next_local"]}, - todo_next_remote: {resume_when: "todo_done:todo_remote"}, - todo_next_local: {resume_when: "todo_done:todo_local"}, - }; - const mutations: CoordinationProjectionMutation[] = (seed.head.todos as JsonObject[]).map(todo => ({ - kind: "todo_upsert", todo: {...todo, ...links[String(todo.todo_id)]}, - })); - const linked = await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({goal_id: "goal-one", - operation_id: "existing-successors", expected_provider_revision: seed.provider_revision, projection: seed.head, mutations})); - assert.equal(linked.status, "applied"); - - const client = (actor: "a" | "b", request: RemoteAuthorityRequest, recover = false) => - fixture.client(running.endpoint, actor, request, recover); - const localClaim = await client("a", claimRequest("a", "todo_local")); - assert.equal(localClaim.code, 0); - const control = await executeCoordinationTodoTerminalLifecycle(fixture.store, { - goal_id: "goal-one", todo_id: "todo_local", command: "complete", - expected_role: "agent", actor_agent_id: "actor-a", registered_agents: ["actor-a", "actor-b"], - lifecycle_grants: [], authority_reason: null, decision_outcome: null, - operation_identity: {kind: "explicit", operation_id: "finish-local"}, - claim_operation_id: String((localClaim.value.result as JsonObject).claim_operation_id), - lease_idempotency_key: null, lease_expected_version: null, allow_user_gate_auto_acquire: false, - requested_no_followup: false, requested_completion_turn_key: null, requested_completion_identity_source: null, - linked_successor_todo_ids: [], successor_intents: [], note: null, evidence: "verified fixture result", - reason: null, clear_claim: true, validation_declaration: null, validation_receipt: null, - completion_policy_request: null, dry_run: false, now: new Date(), - }); - assert.equal(control.status, "applied"); - assert.equal(control.completion_continuation, "successor"); - - const next = claimRequest("b", "todo_next_remote"); - const pending = await client("b", next); - assert.equal(pending.code, 1); - assert.equal((pending.value.result as JsonObject).reason_code, "todo_dependency_pending"); - const {request_id: claim_request_id, ...scope} = claimRequest("a", "todo_remote"); - assert.equal((await client("a", {...scope, request_id: claim_request_id})).code, 0); - const command = {...scope, operation: "complete" as const, request_id: "finish-remote", - claim_request_id, evidence: "verified fixture result"}; - const finished = await client("a", command); - assert.equal(finished.code, 0, JSON.stringify(finished.value)); - assert.equal((finished.value.result as JsonObject).status, "applied"); - const receipt = (finished.value.result as JsonObject).completion_receipt_id; - - const committed = await fixture.store.loadAuthority(); - assert.equal(committed.status, "loaded"); - if (committed.status !== "loaded") return; - const todo = (committed.head.todos as JsonObject[]).find(row => row.todo_id === command.todo_id); - assert.equal(todo?.completion_continuation, control.completion_continuation); - assert.deepEqual(todo?.successor_todo_ids, [next.todo_id]); - assert.equal(todo?.completion_receipt_id, receipt); - const replay = await client("a", command); - assert.equal(replay.code, 0); - assert.equal((replay.value.result as JsonObject).status, "replayed"); - const recovered = await client("a", command, true); - assert.equal(recovered.code, 0); - assert.equal(recovered.value.historical_only, true); - assert.equal((recovered.value.result as JsonObject).completion_receipt_id, receipt); - const changedIntent = await client("a", {...command, no_followup: true}); - assert.equal(changedIntent.code, 1); - assert.equal(changedIntent.value.reason_code, "remote_authority_operation_mismatch"); - assert.deepEqual(await fixture.store.loadAuthority(), committed); - - const {request_id: _, ...reader} = claimRequest("b", command.todo_id); - const observed = await client("b", {...reader, operation: "read"}); - assert.equal(observed.code, 0); - assert.equal((observed.value.todo as JsonObject).status, "done"); - assert.equal((observed.value.lease as JsonObject).status, "released"); - assert.equal((await client("b", next)).code, 0); - }); - - test("remote authority full lifecycle: snapshot, todo_add, complete, block, and defer", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const running = await fixture.start(); - await fixture.seed(["todo_for_complete", "todo_for_terminal", "todo_for_block", "todo_for_defer"]); - - const baseScope = { - schema_version: "loopx_remote_authority_request_v0" as const, - tenant_id: "tenant-one", - goal_id: "goal-one", - actor_id: "actor-a", - store_identity: serviceIdentity, - }; - - // 1. Snapshot - const snap = await fixture.client(running.endpoint, "a", { - ...baseScope, - operation: "snapshot", - }); - assert.equal(snap.code, 0); - assert.equal(snap.value.status, "snapshot"); - assert(Array.isArray(snap.value.todos)); - assert(Array.isArray(snap.value.leases)); - assert.equal(typeof snap.value.receipt_sha256, "string"); - - // 2. Todo add - const added = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_created_remotely", - operation: "todo_add", - request_id: "req-add-remotely", - text: "[P1] remotely created task", - priority: "P1", - role: "agent", - }); - assert.equal(added.code, 0); - assert.equal((added.value.result as JsonObject).status, "applied"); - assert.equal((added.value.result as JsonObject).todo_id, "todo_created_remotely"); - - // 3. Claim -> Complete - const claimComp = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_complete", - operation: "claim", - request_id: "claim-for-comp", - }); - assert.equal(claimComp.code, 0); - assert.equal((claimComp.value.result as JsonObject).status, "applied"); - - const comp = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_complete", - operation: "complete", - request_id: "complete-req-1", - claim_request_id: "claim-for-comp", - evidence: "sha256:1111 delivery validated", - }); - assert.equal(comp.code, 0); - assert.equal((comp.value.result as JsonObject).status, "applied"); - assert.equal((comp.value.result as JsonObject).completed, true); - - const {request_id: terminalClaimId, ...terminalScope} = claimRequest("a", "todo_for_terminal"); - assert.equal((await fixture.client(running.endpoint, "a", {...terminalScope, request_id: terminalClaimId})).code, 0); - const terminal = await fixture.client(running.endpoint, "a", { - ...terminalScope, operation: "complete", request_id: "finish-terminal", claim_request_id: terminalClaimId, - evidence: "verified fixture result", no_followup: true, - }); - assert.equal(terminal.code, 0); - const completedHead = await fixture.store.loadAuthority(); - assert.equal(completedHead.status, "loaded"); - if (completedHead.status !== "loaded") return; - const completedTodos = completedHead.head.todos as JsonObject[]; - assert.equal(completedTodos.find(row => row.todo_id === "todo_for_complete")?.completion_continuation, "active_goal"); - assert.equal(completedTodos.find(row => row.todo_id === "todo_for_terminal")?.completion_continuation, "no_followup"); - - // 4. Claim -> Block - const claimBlock = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_block", - operation: "claim", - request_id: "claim-for-block", - }); - assert.equal(claimBlock.code, 0); - - const block = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_block", - operation: "block", - request_id: "block-req-1", - claim_request_id: "claim-for-block", - reason: "external dependency blocked", - }); - assert.equal(block.code, 0); - assert.equal((block.value.result as JsonObject).status, "applied"); - - // 5. Claim -> Defer - const claimDefer = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_defer", - operation: "claim", - request_id: "claim-for-defer", - }); - assert.equal(claimDefer.code, 0); - - const defer = await fixture.client(running.endpoint, "a", { - ...baseScope, - todo_id: "todo_for_defer", - operation: "defer", - request_id: "defer-req-1", - claim_request_id: "claim-for-defer", - reason: "deferred until milestone", - resume_when: "resume_at:2026-12-01T00:00:00Z", - }); - assert.equal(defer.code, 0); - assert.equal((defer.value.result as JsonObject).status, "applied"); - }); - - test("snapshot is bounded, targeted, and read only across independent clients", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const running = await fixture.start(); - const ids = Array.from({length: 24}, (_, i) => `todo_snapshot_${String(i).padStart(2, "0")}`); - await fixture.seed(ids, Object.fromEntries(ids.map(id => [id, "x".repeat(3100)]))); - const initialHead = await fixture.store.loadAuthority(); - const {request_id: _, todo_id, ...base} = claimRequest("a", ids[0]); - const wide: RemoteAuthorityRequest = {...base, operation: "snapshot"}; - const tooLarge = await fixture.client(running.endpoint, "a", wide); - assert.equal(tooLarge.code, 1); - assert.equal(tooLarge.value.status, "unavailable"); - assert.equal(tooLarge.value.reason_code, "remote_authority_response_too_large"); - - const targeted: RemoteAuthorityRequest = {...wide, todo_id}; - const first = await fixture.client(running.endpoint, "a", targeted); - const second = await fixture.client(running.endpoint, "b", {...targeted, actor_id: "actor-b"}); - assert.equal(first.code, 0); assert.equal(second.code, 0); - assert.deepEqual((first.value.todos as JsonObject[]).map(todo => todo.todo_id), [todo_id]); - assert.equal(first.value.receipt_sha256, second.value.receipt_sha256); - const wrongGoal = await fixture.client(running.endpoint, "a", {...targeted, goal_id: "another-goal"}); - assert.equal(wrongGoal.code, 1); assert.equal(wrongGoal.value.status, "rejected"); - const query = await fixture.client(running.endpoint, "a", targeted, true); - assert.equal(query.code, 1); assert.equal(query.value.status, "rejected"); - assert.deepEqual(await fixture.store.loadAuthority(), initialHead); - }); - -} diff --git a/tests/control_plane_ts/remote_authority_mode_conformance.ts b/tests/control_plane_ts/remote_authority_mode_conformance.ts deleted file mode 100644 index ea5dc9b6ea..0000000000 --- a/tests/control_plane_ts/remote_authority_mode_conformance.ts +++ /dev/null @@ -1,79 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import type {PostgreSqlAuthorityDatabase} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; -import {executeHandoffModeSet} from "../../loopx/control_plane/coordination/handoff_mode_transaction.ts"; -import {RemoteAuthorityService} from "../../loopx/control_plane/coordination/remote_authority_service.ts"; -import {fileRemoteAuthorityAdmission} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; -import {createRemoteAuthorityServer} from "../../loopx/control_plane/coordination/remote_authority_http.ts"; -import {remoteAuthorityOperationId, type RemoteAuthorityRequest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {postgresRemoteFixture, claimRequest} from "./remote_authority_fixture.ts"; - -export function registerRemoteAuthorityModeConformance(connectionString: string | undefined) { - test("remote authority requires hard leases on the committing observation", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - for (const operation of ["claim", "release"] as const) for (const timing of ["already-soft", "after-preflight", "before-commit"]) { - await t.test(`mode change ${timing} cannot commit a remote ${operation} under soft mode`, async child => { - const claim = claimRequest("a", `todo_${operation}_${timing.replaceAll("-", "_")}`); - const {request_id: _, ...scope} = claim as Extract; - const request = operation === "claim" ? claim - : {...scope, operation, idempotency_key: "missing-lease-key", expected_version: 0}; - await fixture.seed([request.todo_id]); - let switched = false, headReads = 0, commitAttempts = 0; - let switchedHead: Awaited>; - async function switchMode() { - switched = true; - assert.equal((await executeHandoffModeSet(fixture.store, {goal_id: request.goal_id, - operation_id: `mode-${operation}-${timing}`, requested_mode: "soft_claim", - observed_at: new Date().toISOString(), dry_run: false})).status, "applied"); - switchedHead = await fixture.store.loadAuthority(); - } - if (timing === "already-soft") await switchMode(); - // Only schedule the race. Reads, mode changes, conflicts and commits - // still use the real PostgreSQL owners and the restricted runtime role. - const database: PostgreSqlAuthorityDatabase = {connect: async () => { - const connection = await fixture.runtimeDatabase.connect(); - return {...connection, query: async (sql, args) => { - if (sql.includes("INSERT INTO loopx_control_plane.authority_heads")) { - commitAttempts++; - if (!switched && timing === "before-commit") await switchMode(); - } - const rows = await connection.query(sql, args); - if (sql.includes("FROM loopx_control_plane.authority_heads") && !sql.includes("FOR UPDATE")) { - headReads++; - if (!switched && timing === "after-preflight") await switchMode(); - } - return rows; - }}; - }}; - const service = new RemoteAuthorityService({database, admit: fileRemoteAuthorityAdmission(fixture.policyPath)}); - const listener = createRemoteAuthorityServer(service, fixture); - const endpoint = `https://127.0.0.1:${await listener.listen()}`; - child.after(() => listener.close()); - const response = await fixture.client(endpoint, "a", request); - assert.equal(response.code, 1); - assert.equal(switched, true); - assert.deepEqual(await fixture.store.loadAuthority(), switchedHead!, "rejected remote work must not write"); - assert.equal((await fixture.store.readReceipt(remoteAuthorityOperationId(request, "principal-a"))).status, "missing"); - if (timing === "already-soft") { - assert.equal(response.value.status, "rejected"); - assert.equal(response.value.reason_code, "remote_authority_requires_hard_lease"); - } else { - const result = response.value.result as JsonObject; - assert.equal(response.value.status, "result"); - if (operation === "release" && timing === "before-commit") { - assert.equal(result.status, "conflict"); // Lifecycle commands do not retry a stale observation. - } else { - assert.equal(result.status, "failed"); - assert.equal(result.reason_code, `${operation === "claim" ? "claim" : "lease"}_handoff_mode_mismatch`); - } - assert(headReads >= (operation === "claim" && timing === "before-commit" ? 3 : 2)); - } - assert.equal(commitAttempts, timing === "before-commit" ? 1 : 0); - const recovered = await fixture.client(endpoint, "a", request, true); - assert.equal(recovered.code, 0); assert.equal(recovered.value.status, "missing"); - assert.deepEqual(await fixture.store.loadAuthority(), switchedHead!); - }); - } - }); -} diff --git a/tests/control_plane_ts/remote_authority_read_acl_conformance.ts b/tests/control_plane_ts/remote_authority_read_acl_conformance.ts deleted file mode 100644 index fe4260876e..0000000000 --- a/tests/control_plane_ts/remote_authority_read_acl_conformance.ts +++ /dev/null @@ -1,277 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import {createServer} from "node:https"; -import type {AddressInfo} from "node:net"; -import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; -import {transitionGoalAcceptanceLifecycle} from "../../loopx/control_plane/goals/acceptance_authority.ts"; -import type {GoalAcceptanceLifecycleTransition} from "../../loopx/control_plane/goals/acceptance_lifecycle.ts"; -import {prepareCoordinationProjectionCommit} from "../../loopx/control_plane/coordination/coordination_projection.ts"; -import {TODO_DOMAIN_ITEM_SCHEMA} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; -import {executeCoordinationTodoCreate} from "../../loopx/control_plane/coordination/todo_create.ts"; -import {REMOTE_AUTHORITY_REQUEST, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, - remoteAuthorityOperationId, remoteAuthoritySnapshotDigest} from "../../loopx/control_plane/coordination/remote_authority_contract.ts"; -import {REMOTE_AUTHORITY_SCOPED_READ_POLICY} from "../../loopx/control_plane/coordination/remote_authority_policy.ts"; -import {claimRequest, postgresRemoteFixture, serviceIdentity, servicePolicy} from "./remote_authority_fixture.ts"; - -export function registerRemoteAuthorityReadAclConformance(connectionString: string | undefined) { - test("remote read ACL pins the Goal lifetime and projects only the caller's resources", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const goal_id = "goal-one"; - const a = {goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; - const b = {goal_id, goal_instance_id: "ginst_" + "b".repeat(32)}; - const {scopes, principals} = servicePolicy(); - const policy = {schema_version: REMOTE_AUTHORITY_SCOPED_READ_POLICY, - scopes: scopes.map(scope => ({...scope, todo_read_acl: [ - {goal_ref: a, todo_id: "todo_visible", principal_id: "principal-a", actor_id: "actor-a", - todo_origin_operation_id: "bind-first-goal"}, - {goal_ref: b, todo_id: "todo_next", principal_id: "principal-b", actor_id: "actor-b", - todo_origin_operation_id: "create-successor-todo"}, - // A new grant must not relabel retained predecessor resources. - {goal_ref: b, todo_id: "todo_visible", principal_id: "principal-b", actor_id: "actor-b", - todo_origin_operation_id: "bind-first-goal"}, - ]})), - principals: principals.map((principal, index) => ({...principal, grants: principal.grants.map(grant => ({ - ...grant, goal_ref: index === 0 ? a : b, operations: ["read", "snapshot"], - }))})), - }; - await fixture.seed(["todo_visible", "todo_hidden"], {todo_visible: "unqualified source text", todo_hidden: "private hidden note"}); - const legacy = await fixture.start(); - const originalClaim = claimRequest("a", "todo_visible"); - assert.equal((await fixture.client(legacy.endpoint, "a", originalClaim)).code, 0); - assert.equal((await fixture.client(legacy.endpoint, "b", claimRequest("b", "todo_hidden"))).code, 0); - await legacy.stop(); - await fixture.writePolicy(policy); - const service = await fixture.start(); - const scope = (actor: "a" | "b") => ({schema_version: REMOTE_AUTHORITY_REQUEST, goal_id, - tenant_id: "tenant-one", actor_id: `actor-${actor}`, store_identity: serviceIdentity} as const); - const snapshot = (actor: "a" | "b", todo_id?: string) => - fixture.client(service.endpoint, actor, {...scope(actor), operation: "snapshot", ...(todo_id ? {todo_id} : {})}); - const read = (actor: "a" | "b", todo_id: string) => - fixture.client(service.endpoint, actor, {...scope(actor), operation: "read", todo_id}); - function denied(response: Awaited>) { - assert.equal(response.code, 1); - assert.equal(response.value.status, "rejected"); - assert.equal(response.value.reason_code, "remote_authority_scope_denied"); - assert.deepEqual(Object.keys(response.value).sort(), ["reason_code", "request_sha256", "schema_version", "status"]); - } - async function transition(operation_id: string, step: GoalAcceptanceLifecycleTransition, expected = "applied") { - const result = await transitionGoalAcceptanceLifecycle(fixture.store, { - goal_id, actor_agent_id: null, operation_id, transition: step, - }); - assert.equal(result.status, expected); - } - - const unbound = await fixture.store.loadAuthority(); - denied(await read("a", "todo_visible")); - denied(await snapshot("a")); - assert.deepEqual(await fixture.store.loadAuthority(), unbound); - await transition("bind-first-goal", {kind: "bind_existing", goal_ref: a}); - const head = await fixture.store.loadAuthority(); - assert.equal(head.status, "loaded"); - if (head.status !== "loaded") throw new Error("Missing fixture head"); - - const visible = await read("a", "todo_visible"); - assert.equal(visible.code, 0); - assert.equal(visible.value.schema_version, REMOTE_AUTHORITY_SCOPED_READ_RESPONSE); - assert.deepEqual(visible.value.goal_ref, a); - assert.deepEqual(visible.value.todo, {todo_id: "todo_visible", status: "open", claimed_by: "actor-a"}); - assert.equal((visible.value.lease as JsonObject).owner, "actor-a"); - assert(!("idempotency_key" in (visible.value.lease as JsonObject))); - const first = await snapshot("a"); - assert.equal(first.code, 0); - assert.deepEqual(first.value.goal_ref, a); - for (const field of ["todos", "leases"]) { - assert.deepEqual((first.value[field] as JsonObject[]).map(row => row.todo_id), ["todo_visible"]); - } - assert(!Object.hasOwn(first.value, "provider_revision")); - assert(!JSON.stringify(first.value).includes("unqualified source text")); - await t.test("TLS CLI rejects nested disclosure even with correlation and a recomputed snapshot digest", async () => { - let reply: JsonObject = visible.value; - const proxy = createServer({key: fixture.key, cert: fixture.cert}, (incoming, outgoing) => { - incoming.resume(); outgoing.setHeader("Content-Type", "application/json"); outgoing.end(JSON.stringify(reply)); - }); - await new Promise(resolve => proxy.listen(0, "127.0.0.1", resolve)); - try { - const endpoint = `https://127.0.0.1:${(proxy.address() as AddressInfo).port}`; - const request = {...scope("a"), operation: "read" as const, todo_id: "todo_visible"}; - assert.equal((await fixture.client(endpoint, "a", request)).code, 0); - for (const [record, field] of [["todo", "note"], ["lease", "idempotency_key"]]) { - reply = {...visible.value, [record!]: {...visible.value[record!] as JsonObject, [field!]: "private-response-marker"}}; - const rejected = await fixture.client(endpoint, "a", request); - assert.equal(rejected.code, 1); assert.equal(rejected.value.status, "unavailable"); - assert(!JSON.stringify(rejected.value).includes("private-response-marker")); - } - const snapshotRequest = {...scope("a"), operation: "snapshot" as const}; - for (const [collection, field] of [["todos", "note"], ["leases", "idempotency_key"]]) { - reply = structuredClone(first.value); - (reply[collection!] as JsonObject[])[0]![field!] = "private-response-marker"; - reply.receipt_sha256 = remoteAuthoritySnapshotDigest(snapshotRequest, undefined, - reply.todos as JsonObject[], reply.leases as JsonObject[], a); - const rejected = await fixture.client(endpoint, "a", snapshotRequest); - assert.equal(rejected.code, 1); assert.equal(rejected.value.status, "unavailable"); - assert(!JSON.stringify(rejected.value).includes("private-response-marker")); - } - reply = first.value; - assert.equal((await fixture.client(endpoint, "a", snapshotRequest)).code, 0); - } finally { await new Promise(resolve => proxy.close(() => resolve())); } - }); - for (const origin of [undefined, "missing-origin-operation", remoteAuthorityOperationId(originalClaim, "principal-a")]) { - await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, - todo_read_acl: s.todo_read_acl.map(entry => ({...entry, todo_origin_operation_id: origin}))}))}); - denied(await read("a", "todo_visible")); - denied(await snapshot("a")); - } - await fixture.writePolicy(policy); - denied(await read("a", "todo_hidden")); - denied(await read("a", "todo_missing")); - denied(await snapshot("a", "todo_hidden")); - denied(await snapshot("b")); - denied(await fixture.client(service.endpoint, "a", {...scope("b"), operation: "snapshot"})); - denied(await fixture.client(service.endpoint, "a", originalClaim)); - denied(await fixture.client(service.endpoint, "a", originalClaim, true)); - assert.deepEqual(await fixture.store.loadAuthority(), head); - - const hidden = (head.head.todos as JsonObject[]).find(todo => todo.todo_id === "todo_hidden")!; - assert.equal((await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({ - goal_id, projection: head.head, expected_provider_revision: head.provider_revision, operation_id: "change-hidden-todo", - mutations: [{kind: "todo_upsert", todo: {...hidden, note: "different private hidden note"}}], - }))).status, "applied"); - assert.deepEqual((await snapshot("a")).value, first.value); - - await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, - todo_read_acl: s.todo_read_acl.filter(entry => entry.principal_id !== "principal-a")}))}); - denied(await snapshot("a")); - await fixture.writePolicy(servicePolicy()); - const downgrade = await snapshot("a"); - assert.equal(downgrade.value.status, "unavailable"); - assert.equal(downgrade.value.reason_code, "remote_authority_policy_unavailable"); - await fixture.writePolicy(policy); - assert.deepEqual((await snapshot("a")).value, first.value); - - await transition("retire-first-goal", {kind: "retire", goal_ref: a}); - denied(await snapshot("a")); - await transition("activate-second-goal", {kind: "activate_successor", retired_goal_ref: a, goal_ref: b}); - const added = await executeCoordinationTodoCreate(fixture.store, { - goal_id, actor_agent_id: "actor-b", registered_agents: ["actor-a", "actor-b"], - operation_id: "create-successor-todo", dry_run: false, now: new Date(), - todo: {schema_version: TODO_DOMAIN_ITEM_SCHEMA, todo_id: "todo_next", role: "agent", - status: "open", done: false, archive_state: "active", text: "Second lifetime task"}, - }); - assert.equal(added.status, "applied"); - const successor = await fixture.store.loadAuthority(); - denied(await snapshot("a")); - denied(await read("a", "todo_visible")); - denied(await read("a", "todo_next")); - denied(await read("b", "todo_visible")); - const second = await snapshot("b"); - assert.equal(second.code, 0); - assert.deepEqual(second.value.goal_ref, b); - assert.deepEqual((second.value.todos as JsonObject[]).map(todo => todo.todo_id), ["todo_next"]); - assert.deepEqual(second.value.leases, []); - assert.equal((await read("b", "todo_next")).code, 0); - assert.deepEqual(await fixture.store.loadAuthority(), successor); - await transition("bind-current-again", {kind: "bind_existing", goal_ref: b}, "no_change"); - for (const origin of ["activate-second-goal", "bind-current-again", "create-successor-todo"]) { - await fixture.writePolicy({...policy, scopes: policy.scopes.map(s => ({...s, - todo_read_acl: s.todo_read_acl.map(entry => entry.goal_ref === b && entry.todo_id === "todo_visible" - ? {...entry, todo_origin_operation_id: origin} : entry)}))}); - denied(await read("b", "todo_visible")); - assert.deepEqual(((await snapshot("b")).value.todos as JsonObject[]).map(todo => todo.todo_id), ["todo_next"]); - } - }); - - test("scoped receipt recovery proves the original Goal lifetime and current exact operation access", {skip: !connectionString}, async t => { - const fixture = await postgresRemoteFixture(t, connectionString!); - const goal_id = "goal-one"; - const a = {goal_id, goal_instance_id: "ginst_" + "a".repeat(32)}; - const b = {goal_id, goal_instance_id: "ginst_" + "b".repeat(32)}; - const unboundClaim = claimRequest("a", "todo_legacy"); - const boundClaim = claimRequest("a", "todo_bound"); - const missingClaim = claimRequest("a", "todo_bound", "never-committed"); - const id = (request: typeof boundClaim) => remoteAuthorityOperationId(request, "principal-a"); - async function transition(operation_id: string, step: GoalAcceptanceLifecycleTransition) { - const result = await transitionGoalAcceptanceLifecycle(fixture.store, {goal_id, actor_agent_id: null, - operation_id, transition: step}); - assert.equal(result.status, "applied"); - } - - await fixture.seed(["todo_legacy", "todo_bound", "todo_hidden"]); - const legacy = await fixture.start(); - assert.equal((await fixture.client(legacy.endpoint, "a", unboundClaim)).code, 0); - await transition("bind-receipt-goal", {kind: "bind_existing", goal_ref: a}); - assert.equal((await fixture.client(legacy.endpoint, "a", boundClaim)).code, 0); - const original = await fixture.client(legacy.endpoint, "a", boundClaim, true); - assert.equal(original.code, 0); - assert("cursor" in original.value); - assert("lease" in (original.value.result as JsonObject)); - await legacy.stop(); - - // Read an earlier receipt after the authority has crossed a checkpoint. - // This uses the normal journal and keeps the original receipt intact. - for (let sequence = 0; sequence < 64; sequence++) { - const head = await fixture.store.loadAuthority(); - if (head.status !== "loaded") throw new Error("Missing fixture head"); - const hidden = (head.head.todos as JsonObject[]).find(todo => todo.todo_id === "todo_hidden")!; - const result = await fixture.store.commitAuthority(prepareCoordinationProjectionCommit({ - goal_id, projection: head.head, expected_provider_revision: head.provider_revision, - operation_id: `append-receipt-fixture-${sequence}`, - mutations: [{kind: "todo_upsert", todo: {...hidden, note: `Hidden fixture update ${sequence}`}}], - })); - assert.equal(result.status, "applied"); - } - function policy(goalRef: typeof a, allowed: readonly string[], operations = ["read", "snapshot"]) { - const {scopes, principals} = servicePolicy(); - return {schema_version: REMOTE_AUTHORITY_SCOPED_READ_POLICY, - scopes: scopes.map(scope => ({...scope, todo_read_acl: [ - {goal_ref: goalRef, todo_id: "todo_bound", principal_id: "principal-a", actor_id: "actor-a", - todo_origin_operation_id: "bind-receipt-goal", receipt_operation_ids: allowed}, - {goal_ref: goalRef, todo_id: "todo_legacy", principal_id: "principal-a", actor_id: "actor-a", - receipt_operation_ids: [id(unboundClaim), id(boundClaim)]}, - ]})), - principals: principals.slice(0, 1).map(principal => ({...principal, grants: principal.grants.map(grant => - ({...grant, goal_ref: goalRef, operations}))})), - }; - } - await fixture.writePolicy(policy(a, [id(boundClaim), id(missingClaim)])); - const service = await fixture.start(); - const recover = (request: typeof boundClaim) => fixture.client(service.endpoint, "a", request, true); - function denied(result: Awaited>) { - assert.equal(result.code, 1); - assert.equal(result.value.status, "rejected"); - assert.equal(result.value.reason_code, "remote_authority_scope_denied"); - assert.deepEqual(Object.keys(result.value).sort(), ["reason_code", "request_sha256", "schema_version", "status"]); - } - const current = await fixture.store.loadAuthority(); - const allowed = await recover(boundClaim); - assert.equal(allowed.code, 0); - assert.deepEqual(allowed.value, { - schema_version: REMOTE_AUTHORITY_SCOPED_READ_RESPONSE, status: "found", historical_only: true, - todo_id: boundClaim.todo_id, goal_ref: a, operation_id: original.value.operation_id, - receipt_sha256: original.value.receipt_sha256, request_sha256: original.value.request_sha256, - }); - denied(await recover(unboundClaim)); - denied(await recover(missingClaim)); - denied(await recover({...boundClaim, todo_id: unboundClaim.todo_id})); - denied(await fixture.client(service.endpoint, "a", boundClaim)); // Execute stays outside the read grant. - await fixture.writePolicy(policy(a, [])); - denied(await recover(boundClaim)); - const currentTodo = await fixture.client(service.endpoint, "a", { - operation: "read", schema_version: REMOTE_AUTHORITY_REQUEST, tenant_id: boundClaim.tenant_id, - goal_id, actor_id: boundClaim.actor_id, store_identity: serviceIdentity, todo_id: boundClaim.todo_id, - }); - assert.equal(currentTodo.code, 0); - await fixture.writePolicy(policy(a, [id(boundClaim)], ["snapshot"])); - denied(await recover(boundClaim)); - assert.deepEqual(await fixture.store.loadAuthority(), current); - - await fixture.writePolicy(policy(a, [id(boundClaim)])); - await transition("retire-receipt-goal", {kind: "retire", goal_ref: a}); - denied(await recover(boundClaim)); - await transition("activate-next-receipt-goal", {kind: "activate_successor", retired_goal_ref: a, goal_ref: b}); - const successor = await fixture.store.loadAuthority(); - denied(await recover(boundClaim)); // Stale credential. - await fixture.writePolicy(policy(b, [id(boundClaim)])); - denied(await recover(boundClaim)); // Current ACL cannot reassign a receipt from A to B. - assert.deepEqual(await fixture.store.loadAuthority(), successor); - }); -} diff --git a/tests/control_plane_ts/task_lease_inspection.test.ts b/tests/control_plane_ts/task_lease_inspection.test.ts index 40972353a4..5fc4362ef2 100644 --- a/tests/control_plane_ts/task_lease_inspection.test.ts +++ b/tests/control_plane_ts/task_lease_inspection.test.ts @@ -1,17 +1,15 @@ -/** Inspection is observational; all three real providers share admission facts. */ +/** Inspection is observational; local canonical providers share admission facts. */ import assert from "node:assert/strict"; -import {createHash, randomUUID} from "node:crypto"; +import {createHash} from "node:crypto"; import {writeFileSync, unlinkSync} from "node:fs"; import {mkdtemp, mkdir, writeFile, readFile, readdir, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; import test from "node:test"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {selectLocalSqliteAuthority, type LocalAuthorityProviderDependencies} from "../../loopx/control_plane/coordination/local_authority_provider.ts"; import {engageLegacyCoordinationWriterFence, legacyCoordinationWriterFencePath} from "../../loopx/control_plane/coordination/legacy_writer_fence.ts"; import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; @@ -22,16 +20,8 @@ import {productionScaleLeaseLifecycleFixture} from "./production_scale_coordinat const NOW = new Date("2026-09-13T10:05:00Z"); const sqliteQualified = sqliteRuntimeIdentity().sqlite_authority_qualified === true; -const pool = process.env.LOOPX_TEST_POSTGRES_URL ? new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL}) : null; -const database = pool ? {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: (error?: Error) => client.release(error)}; -}} : null; -const installed = database ? installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`) : null; -test.after(async () => {await pool?.end();}); -type Provider = "legacy" | "file" | "sqlite" | "postgresql"; +type Provider = "legacy" | "file" | "sqlite"; async function fixture(t: test.TestContext, provider: Provider, schema: "native" | "legacy" = "native", mode = "hard_lease") { const root = await mkdtemp(join(tmpdir(), "lease-inspect-")), goal = "inspect-goal"; t.after(() => rm(root, {recursive: true, force: true})); @@ -83,7 +73,7 @@ async function fixture(t: test.TestContext, provider: Provider, schema: "native" const authority = {handoff_mode: "hard_lease", registered_agent_candidates: [fixture.registered_agents], todos: todos.filter(row => row.archive_state === "active"), todo_projection_error: null, source_receipts: [{source_id: "registry", path: registry, state: "file", sha256: createHash("sha256").update(registryText).digest("hex")}]}; - let store: AuthorityStore | null = null, authorityProvider: LocalAuthorityProviderDependencies = {}; + let store: AuthorityStore | null = null; const authorityProvider: LocalAuthorityProviderDependencies = {}; if (provider === "legacy") { const directory = join(root, "goals", goal, "task-leases"); await mkdir(directory, {recursive: true}); for (const row of leases) await writeFile(join(directory, `${row.todo_id}.json`), JSON.stringify(row)); @@ -91,21 +81,6 @@ async function fixture(t: test.TestContext, provider: Provider, schema: "native" if (provider === "sqlite") { assert.equal((await selectLocalSqliteAuthority(root, goal, true)).ok, true); store = new SqliteAuthorityStore(join(root, "authority/sqlite-v0"), goal); - } else if (provider === "postgresql") { - await installed; - const tenant = `inspection-${randomUUID()}`; - store = new PostgreSqlAuthorityStore(database!, {tenant_id: tenant, goal_id: goal}); - t.after(async () => { - for (const table of ["authority_receipts", "authority_events", "authority_commits", "authority_heads"]) { - await pool!.query(`DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1`, [tenant]); - } - }); - const identity = await store.storeIdentity(); assert.equal(identity.status, "available"); - if (identity.status !== "available") throw new Error("fixture identity missing"); - await mkdir(join(root, "authority"), {recursive: true}); - await writeFile(join(root, "authority", `provider-${createHash("sha256").update(goal).digest("hex")}.json`), JSON.stringify({ - schema_version: "loopx_local_authority_provider_v0", provider, goal_id: goal, tenant_id: tenant, store_identity: identity.store_identity})); - const selected = store; authorityProvider = {openPostgresqlStore: () => selected}; } else store = new FileAuthorityStore(join(root, "authority/file-v0"), goal); assert.equal((await store.commitAuthority({expected_provider_revision: null, operation_id: "seed", next_projection: projection, events: [], receipts: []})).status, "applied"); @@ -122,9 +97,9 @@ async function fixture(t: test.TestContext, provider: Provider, schema: "native" return {root, goal, registry, store, request, authorityProvider, cases}; } -for (const provider of ["legacy", "file", "sqlite", "postgresql"] as const) { +for (const provider of ["legacy", "file", "sqlite"] as const) { for (const schema of provider === "legacy" ? ["legacy"] as const : ["legacy", "native"] as const) { - const skip = (provider === "postgresql" && !pool) || (provider === "sqlite" && !sqliteQualified); + const skip = provider === "sqlite" && !sqliteQualified; test(`${provider} ${schema} full mixed head reports effective ownership without writes`, {skip}, async t => { const {request, store, authorityProvider, cases, root, goal} = await fixture(t, provider, schema); const before = await store?.loadAuthority(); @@ -184,9 +159,9 @@ test("retained source inspection ignores retired Todo admission and validates th assert.equal((await inspectTaskLease(request, {now: () => NOW})).error_code, "corrupt_lease"); }); -for (const provider of ["file", "sqlite", "postgresql"] as const) { +for (const provider of ["file", "sqlite"] as const) { test(`${provider} retired legacy policy reports retained leases without effective execution`, - {skip: (provider === "postgresql" && !pool) || (provider === "sqlite" && !sqliteQualified)}, async t => { + {skip: provider === "sqlite" && !sqliteQualified}, async t => { const {request, store, authorityProvider} = await fixture(t, provider, "native", "legacy"); const before = await store!.loadAuthority(); const result = await inspectTaskLease(request, {now: () => NOW, authorityProvider}); diff --git a/tests/control_plane_ts/task_lease_workspace.test.ts b/tests/control_plane_ts/task_lease_workspace.test.ts index c08905499a..bf3fffd33c 100644 --- a/tests/control_plane_ts/task_lease_workspace.test.ts +++ b/tests/control_plane_ts/task_lease_workspace.test.ts @@ -397,22 +397,21 @@ test("worktree identity cannot weaken Todo ownership or repository identity", () assert.throws(() => leaseWorkspace({...workspace, unchecked_path: "/tmp"})); }); -test("PostgreSQL retains worktree identity and overlap receipts across reload", {skip: !process.env.LOOPX_TEST_POSTGRES_URL}, async () => { - const {Pool} = await import("pg"); - const {randomUUID} = await import("node:crypto"); - const {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} = await import("../../loopx/control_plane/coordination/postgresql_authority_store.ts"); - const {executeCanonicalTaskLeaseAcquire} = await import("../../loopx/control_plane/coordination/task_lease_acquire.ts"); - const {authorityProjectionFixture} = await import("./authority_projection_fixture.ts"); - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL}); - const database = {connect: async () => { - const client = await pool.connect(); - return {query: async (text: string, values?: readonly unknown[]) => client.query(text, values ? [...values] : undefined), - release: () => client.release()}; - }}; - const goal = `workspace-${randomUUID()}`, options = {goal_id: goal, tenant_id: `tenant-${randomUUID()}`}; - try { - await installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); - const store = new PostgreSqlAuthorityStore(database, options); +for (const provider of ["file", "sqlite"] as const) { + test(`${provider} retains worktree identity and overlap receipts across reload`, async t => { + const {randomUUID} = await import("node:crypto"); + const {mkdtemp, rm} = await import("node:fs/promises"); + const {tmpdir} = await import("node:os"); + const {join} = await import("node:path"); + const {FileAuthorityStore} = await import("../../loopx/control_plane/coordination/file_authority_store.ts"); + const {SqliteAuthorityStore} = await import("../../loopx/control_plane/coordination/sqlite_authority_store.ts"); + const {executeCanonicalTaskLeaseAcquire} = await import("../../loopx/control_plane/coordination/task_lease_acquire.ts"); + const {authorityProjectionFixture} = await import("./authority_projection_fixture.ts"); + const root = await mkdtemp(join(tmpdir(), "workspace-reload-")); + t.after(() => rm(root, {recursive: true, force: true})); + const goal = `workspace-${randomUUID()}`; + const open = () => provider === "file" ? new FileAuthorityStore(root, goal) : new SqliteAuthorityStore(root, goal); + const store = open(); const todos = ["alpha", "beta", "legacy"].map(key => ({todo_id: `todo_${key}`, role: "agent", status: "open", done: false, archive_state: "active", task_class: "advancement_task", text: "Independent code edits", claimed_by: "agent-a", task_repository: repo})); assert.equal((await store.commitAuthority({expected_provider_revision: null, operation_id: "seed", events: [], receipts: [], @@ -425,7 +424,7 @@ test("PostgreSQL retains worktree identity and overlap receipts across reload", const first = await executeCanonicalTaskLeaseAcquire(store, command); assert.equal(first.status, "applied"); assert.equal((first.integration_overlap_advisories as unknown[]).length, 1); - const reopened = new PostgreSqlAuthorityStore(database, options); + const reopened = open(); const replay = await executeCanonicalTaskLeaseAcquire(reopened, command); assert.deepEqual(replay.original_receipt, first.original_receipt); assert.deepEqual(replay.lease, first.lease); @@ -436,10 +435,5 @@ test("PostgreSQL retains worktree identity and overlap receipts across reload", const retained = await executeCanonicalTaskLeaseAcquire(reopened, legacyCommand); assert.deepEqual(retained.original_receipt, legacy.original_receipt); assert.deepEqual(retained.lease, legacy.lease); - } finally { - for (const table of ["authority_receipts", "authority_events", "authority_commits", "authority_heads"]) { - await pool.query(`DELETE FROM loopx_control_plane.${table} WHERE tenant_id=$1 AND goal_id=$2`, [options.tenant_id, goal]); - } - await pool.end(); - } -}); + }); +} diff --git a/tests/control_plane_ts/team_plan.test.ts b/tests/control_plane_ts/team_plan.test.ts index a2b4dad6e5..bbe7036463 100644 --- a/tests/control_plane_ts/team_plan.test.ts +++ b/tests/control_plane_ts/team_plan.test.ts @@ -1,14 +1,13 @@ import assert from "node:assert/strict"; -import {randomUUID} from "node:crypto"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; import test, {type TestContext} from "node:test"; -import {Pool} from "pg"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; +import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; +import {sqliteRuntimeIdentity} from "../../loopx/control_plane/coordination/sqlite_runtime.ts"; import {canonicalAuthoritySha256} from "../../loopx/control_plane/coordination/authority_store_codec.ts"; import {TODO_DOMAIN_READ_RECORD_SCHEMA, TODO_DOMAIN_RECORD_CONTRACT} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; import {prepareCoordinationProjectionCommit} from "../../loopx/control_plane/coordination/coordination_projection.ts"; @@ -33,25 +32,15 @@ async function seed(store: AuthorityStore) { todo_read_model: {schema_version: TODO_DOMAIN_READ_RECORD_SCHEMA, todo_count: 0, records_sha256: canonicalAuthoritySha256([]), contract_fields: [...TODO_DOMAIN_RECORD_CONTRACT.fields]}}})).status, "applied"); } -const providers = ["file", "postgresql"] as const; +const providers = ["file", "sqlite"] as const; +const sqliteQualified = sqliteRuntimeIdentity().sqlite_authority_qualified === true; async function fixture(t: TestContext, provider: typeof providers[number]): Promise { - if (provider === "file") { - const dir = await mkdtemp(join(tmpdir(), "team-plan-store-")); - t.after(() => rm(dir, {recursive: true, force: true})); - return new FileAuthorityStore(dir, goal); - } - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL, max: 4}); - t.after(() => pool.end()); - const db = {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(db, `postgresql:${"b".repeat(32)}`); - return new PostgreSqlAuthorityStore(db, {tenant_id: `team-${randomUUID()}`, goal_id: goal}); + const dir = await mkdtemp(join(tmpdir(), "team-plan-store-")); + t.after(() => rm(dir, {recursive: true, force: true})); + return provider === "file" ? new FileAuthorityStore(dir, goal) : new SqliteAuthorityStore(dir, goal); } for (const provider of providers) { - const options = {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}; + const options = {skip: provider === "sqlite" && !sqliteQualified}; test(`${provider}: equal-text lane assignments commit once; concurrent replay preserves receiver identity`, options, async t => { const store = await fixture(t, provider); await seed(store); const input = request(); diff --git a/tests/control_plane_ts/todo_administrative_edits.test.ts b/tests/control_plane_ts/todo_administrative_edits.test.ts index eab6daf951..f906d53e0d 100644 --- a/tests/control_plane_ts/todo_administrative_edits.test.ts +++ b/tests/control_plane_ts/todo_administrative_edits.test.ts @@ -1,37 +1,23 @@ import assert from "node:assert/strict"; -import {randomUUID} from "node:crypto"; import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; import test from "node:test"; -import {Pool} from "pg"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import type {JsonObject} from "../../loopx/control_plane/effect_program.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; import {TODO_DOMAIN_READ_RECORD_SCHEMA} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; import {executeCoordinationTodoUpdate, type CoordinationTodoUpdateInput} from "../../loopx/control_plane/coordination/todo_update.ts"; const GOAL = "administrative-edits"; const NOW = new Date("2026-09-25T06:00:00Z"); -const pool = process.env.LOOPX_TEST_POSTGRES_URL ? new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL}) : null; -const database = pool && {connect: async () => { - const client = await pool.connect(); - return {query: async (sql: string, values?: readonly unknown[]) => client.query(sql, values ? [...values] : undefined), - release: (error?: Error) => client.release(error)}; -}}; -const installed = database && installPostgreSqlAuthorityStoreSchema(database, `postgresql:${"b".repeat(32)}`); -test.after(async () => { await pool?.end(); }); -async function fixture(t: test.TestContext, provider: "file" | "sqlite" | "postgresql", leaseState: string | null = "active") { +async function fixture(t: test.TestContext, provider: "file" | "sqlite", leaseState: string | null = "active") { const root = await mkdtemp(join(tmpdir(), "loopx-administrative-")); t.after(() => rm(root, {recursive: true, force: true})); - await installed; - const store: AuthorityStore = provider === "postgresql" - ? new PostgreSqlAuthorityStore(database!, {tenant_id: `test-${randomUUID()}`, goal_id: GOAL}) - : provider === "file" ? new FileAuthorityStore(root, GOAL) : new SqliteAuthorityStore(root, GOAL); + const store: AuthorityStore = provider === "file" ? new FileAuthorityStore(root, GOAL) : new SqliteAuthorityStore(root, GOAL); const todo = {schema_version: "todo_item_v0", todo_id: "todo_work", source_section: "Agent Todo", role: "agent", status: "open", done: false, archive_state: "active", claimed_by: "agent-a", text: "Original task", task_class: "advancement_task", task_repository: "git:github.com/example/project", required_write_scopes: ["src/**"], completion_validation_required: true}; @@ -58,8 +44,8 @@ async function head(store: AuthorityStore) { return read; } -for (const provider of ["file", "sqlite", "postgresql"] as const) { - const options = {skip: provider === "postgresql" && !pool ? "requires isolated real PostgreSQL" : false}; +for (const provider of ["file", "sqlite"] as const) { + const options = {}; for (const state of [null, "active", "expired", "released"]) test(`${provider}: owner metadata needs no execution grant (${state})`, options, async t => { const {store, input} = await fixture(t, provider, state); const before = await head(store); diff --git a/tests/control_plane_ts/todo_create_reviewed.test.ts b/tests/control_plane_ts/todo_create_reviewed.test.ts index 73afeacc9e..394d4212ab 100644 --- a/tests/control_plane_ts/todo_create_reviewed.test.ts +++ b/tests/control_plane_ts/todo_create_reviewed.test.ts @@ -3,12 +3,9 @@ import {mkdtemp, rm} from "node:fs/promises"; import {tmpdir} from "node:os"; import {join} from "node:path"; import test from "node:test"; -import {Pool} from "pg"; import type {AuthorityStore} from "../../loopx/control_plane/coordination/authority_store.ts"; import {FileAuthorityStore} from "../../loopx/control_plane/coordination/file_authority_store.ts"; import {SqliteAuthorityStore} from "../../loopx/control_plane/coordination/sqlite_authority_store.ts"; -import {PostgreSqlAuthorityStore, installPostgreSqlAuthorityStoreSchema, - type PostgreSqlAuthorityDatabase} from "../../loopx/control_plane/coordination/postgresql_authority_store.ts"; import {coordinationTodoReadModel} from "../../loopx/control_plane/coordination/coordination_projection.ts"; import {TODO_DOMAIN_READ_RECORD_SCHEMA} from "../../loopx/control_plane/coordination/coordination_state_contract.ts"; import {executeCoordinationTodoCreate, decodeCoordinationTodoCreateReceipt, @@ -31,22 +28,11 @@ test("historical creation decoding preserves the receipt and rejects mismatched ]) assert.throws(() => decodeCoordinationTodoCreateReceipt(malformed, "todo-created"), /identity mismatch/); }); -for (const provider of ["file", "sqlite", "postgresql"] as const) { - test(`${provider}: reviewed create fences concurrent commits and recovers its bound identity`, - {skip: provider === "postgresql" && !process.env.LOOPX_TEST_POSTGRES_URL}, async t => { +for (const provider of ["file", "sqlite"] as const) { + test(`${provider}: reviewed create fences concurrent commits and recovers its bound identity`, async t => { const root = await mkdtemp(join(tmpdir(), "loopx-reviewed-create-")); t.after(() => rm(root, {recursive: true, force: true})); - let store: AuthorityStore; - if (provider === "postgresql") { - const pool = new Pool({connectionString: process.env.LOOPX_TEST_POSTGRES_URL}); - t.after(() => pool.end()); - const database: PostgreSqlAuthorityDatabase = {connect: async () => { - const client = await pool.connect(); - return {query: (sql, values) => client.query(sql, values ? [...values] : undefined), release: () => client.release()}; - }}; - await installPostgreSqlAuthorityStoreSchema(database, "postgresql:" + "b".repeat(32)); - store = new PostgreSqlAuthorityStore(database, {tenant_id: root, goal_id: "goal-a"}); - } else store = provider === "file" ? new FileAuthorityStore(root, "goal-a") : new SqliteAuthorityStore(root, "goal-a"); + const store: AuthorityStore = provider === "file" ? new FileAuthorityStore(root, "goal-a") : new SqliteAuthorityStore(root, "goal-a"); const seed = await store.commitAuthority({operation_id: "seed", expected_provider_revision: null, events: [], receipts: [], next_projection: {goal_id: "goal-a", handoff_mode: "soft_claim", todos: [], leases: [], todo_read_model: coordinationTodoReadModel([], TODO_DOMAIN_READ_RECORD_SCHEMA)}}); diff --git a/tests/fixtures/control_plane/postgresql_authority_service_v0.json b/tests/fixtures/control_plane/postgresql_authority_service_v0.json deleted file mode 100644 index b55473dd68..0000000000 --- a/tests/fixtures/control_plane/postgresql_authority_service_v0.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "schema_version": "loopx_postgresql_authority_service_fixture_v0", - "source_authority": "postgresql_v0", - "principal_cases": [ - { - "id": "authenticated-tenant", - "credential_kind": "opaque", - "expected": "opened" - }, - { - "id": "unauthenticated", - "credential_kind": "opaque", - "expected": "rejected", - "reason_code": "principal_unauthenticated" - }, - { - "id": "tenant-denied", - "credential_kind": "opaque", - "expected": "rejected", - "reason_code": "tenant_unauthorized" - }, - { - "id": "identity-drift", - "credential_kind": "opaque", - "expected": "rejected", - "reason_code": "store_identity_mismatch" - } - ], - "rotation_cases": [ - { - "id": "new-incarnation", - "expected": "rotated" - }, - { - "id": "wrong-incarnation", - "expected": "failed", - "reason_code": "store_identity_mismatch" - } - ], - "credential_persistence": "forbidden", - "agent_database_access": "forbidden" -} diff --git a/tests/test_audit_cli_and_runtime_fixes.py b/tests/test_audit_cli_and_runtime_fixes.py index aa08921c4c..930c7d361f 100644 --- a/tests/test_audit_cli_and_runtime_fixes.py +++ b/tests/test_audit_cli_and_runtime_fixes.py @@ -17,15 +17,12 @@ REPO_ROOT = Path(__file__).resolve().parents[1] -def test_cli_build_parser_accepts_postgres_subcommands() -> None: +def test_cli_build_parser_rejects_removed_postgres_subcommand() -> None: from loopx.cli import build_parser parser = build_parser() - parsed = parser.parse_args(["postgres", "--format", "json", "setup", "--execute"]) - assert parsed.command == "postgres" - assert parsed.postgres_action == "setup" - assert parsed.subcommand_format == "json" - assert parsed.execute is True + with pytest.raises(SystemExit): + parser.parse_args(["postgres", "--format", "json", "setup", "--execute"]) def test_opencode2_goal_worker_routes_stderr_and_preserves_failure_exit( diff --git a/tests/test_postgres_cli.py b/tests/test_postgres_cli.py deleted file mode 100644 index d4eba70622..0000000000 --- a/tests/test_postgres_cli.py +++ /dev/null @@ -1,168 +0,0 @@ -"""Unit and integration tests for loopx postgres CLI commands.""" -import json -import subprocess -import sys -from pathlib import Path - -REPO = Path(__file__).resolve().parents[1] - - -def test_postgres_setup_dry_run(): - process = subprocess.run( - [ - sys.executable, - "-m", - "loopx.entrypoint", - "--format", - "json", - "postgres", - "setup", - "--database-url", - "postgresql://mock_user:mock_pass@localhost:5432/mock_db", - "--runtime-role", - "loopx_worker", - ], - cwd=REPO, - capture_output=True, - text=True, - timeout=30, - check=False, - ) - assert process.returncode == 0, process.stderr - payload = json.loads(process.stdout) - assert payload["status"] == "dry_run" - assert payload["schema_version"] == "loopx_postgresql_authority_store_v1" - assert payload["runtime_role"] == "loopx_worker" - assert payload["database_url_configured"] is True - - -def test_postgres_setup_missing_url(monkeypatch): - monkeypatch.delenv("LOOPX_POSTGRES_URL", raising=False) - process = subprocess.run( - [ - sys.executable, - "-m", - "loopx.entrypoint", - "--format", - "json", - "postgres", - "setup", - ], - cwd=REPO, - capture_output=True, - text=True, - timeout=30, - check=False, - ) - assert process.returncode == 1 - payload = json.loads(process.stdout) - assert payload["status"] == "failed" - assert "required" in payload.get("reason", "").lower() - - -def test_postgres_local_to_pg_seed_export(tmp_path): - runtime = tmp_path / "runtime" - registry = tmp_path / "registry.json" - state = tmp_path / "state.md" - goal = "seed-export-goal" - - runtime.mkdir(parents=True, exist_ok=True) - state.write_text("# Synthetic state for seed export\n") - registry.write_text( - json.dumps( - { - "common_runtime_root": str(runtime), - "goals": [{"id": goal, "repo": str(tmp_path), "state_file": state.name}], - } - ) - ) - - # Initialize local authority store using FileAuthorityStore via Node - module = REPO / "loopx/control_plane/coordination/file_authority_store.ts" - store_dir = runtime / "authority" / "file-v0" - store_dir.mkdir(parents=True, exist_ok=True) - - script = ( - f"import {{FileAuthorityStore}} from {json.dumps(module.as_uri())};" - "const store = new FileAuthorityStore(process.argv[1], process.argv[2]);" - "await store.commitAuthority({" - " operation_id: 'op-init'," - " expected_provider_revision: null," - " events: []," - " receipts: []," - " next_projection: {" - " schema_version: 'loopx_authority_projection_v0'," - " goal_id: process.argv[2]," - " owner_identity: 'ou_1234567890abcdef1234567890'," - " channel: { chat_id: 'oc_abcdef1234567890abcdef12345678' }," - " todos: [" - " {" - " id: 'todo-1'," - " title: 'Work on feature with .local/path and Feishu contact'," - " required_write_scopes: ['/Users/test/Developer/loopx/packages/core']" - " }" - " ]" - " }" - "});" - ) - subprocess.run( - ["node", "--no-warnings", "--experimental-strip-types", "--input-type=module", "-e", script, str(store_dir), goal], - capture_output=True, - text=True, - timeout=30, - check=True, - ) - - output_seed = tmp_path / "genesis_seed.json" - command = [ - sys.executable, "-m", "loopx.entrypoint", "--registry", str(registry), - "--format", "json", "postgres", "local-to-pg-seed", "--goal-id", goal, - "--tenant-id", "tenant-alpha", "--output", str(output_seed), - ] - - def export(): - return subprocess.run(command, cwd=REPO, capture_output=True, text=True, timeout=45, check=False) - - process = export() - assert process.returncode == 0, process.stderr - payload = json.loads(process.stdout) - assert payload["status"] == "exported" - assert payload["cursor"] == "1" - assert payload["goal_id"] == goal - assert payload["tenant_id"] == "tenant-alpha" - assert bool(payload.get("seed_digest")) - - assert output_seed.is_file() - seed_content = json.loads(output_seed.read_text()) - assert seed_content["schema_version"] == "loopx_pg_genesis_seed_v0" - assert seed_content["cursor"] == "1" - # Verify Feishu IDs and local paths were sanitized - raw_str = json.dumps(seed_content) - assert "ou_1234567890abcdef1234567890" not in raw_str - assert "oc_abcdef1234567890abcdef12345678" not in raw_str - assert "/Users/test/Developer" not in raw_str - - original = output_seed.read_bytes() - inode = output_seed.stat().st_ino - repeated = export() - assert repeated.returncode == 0, repeated.stderr - assert json.loads(repeated.stdout)["seed_digest"] == payload["seed_digest"] - assert output_seed.stat().st_ino == inode - assert output_seed.read_bytes() == original - - different = original.replace(b"tenant-alpha", b"tenant-other", 1) - assert different != original and len(different) == len(original) - output_seed.write_bytes(different) - refused = export() - assert refused.returncode == 1 - assert "identical regular file" in json.loads(refused.stdout)["reason"] - assert output_seed.read_bytes() == different - - alternative = tmp_path / "seed_target.json" - alternative.write_bytes(original) - output_seed.unlink() - output_seed.symlink_to(alternative) - symlink = export() - assert symlink.returncode == 1 - assert output_seed.is_symlink() - assert alternative.read_bytes() == original diff --git a/tests/test_postgresql_integration_workflow.py b/tests/test_postgresql_integration_workflow.py deleted file mode 100644 index 675ef708d7..0000000000 --- a/tests/test_postgresql_integration_workflow.py +++ /dev/null @@ -1,100 +0,0 @@ -"""Thin pytest: the PostgreSQL workflow keeps the real-server path qualified.""" - -from pathlib import Path - -import yaml - -from loopx.control_plane.testing.authority_e2e_ladder import LADDER_ROWS - - -REPO_ROOT = Path(__file__).resolve().parents[1] -WORKFLOW_PATH = REPO_ROOT / ".github" / "workflows" / "postgresql-integration.yml" -WORKFLOW_TEXT = WORKFLOW_PATH.read_text(encoding="utf-8") -WORKFLOW = yaml.safe_load(WORKFLOW_TEXT) -JOB = WORKFLOW["jobs"]["postgresql-authority"] -RELEVANT_PATHS = ( - ".github/workflows/postgresql-integration.yml", - "loopx/control_plane/**", - "tests/control_plane_ts/**", - "package.json", - "package-lock.json", -) - - -def _triggers() -> dict: - # PyYAML resolves the bare `on:` key to True. - return WORKFLOW.get("on") or WORKFLOW[True] - - -def _steps() -> list[dict]: - return JOB["steps"] - - -def _step_running(fragment: str) -> dict: - matches = [step for step in _steps() if fragment in step.get("run", "")] - assert len(matches) == 1, (fragment, len(matches)) - return matches[0] - - -def _step_named(name: str) -> dict: - matches = [step for step in _steps() if step.get("name") == name] - assert len(matches) == 1, (name, len(matches)) - return matches[0] - - -def test_workflow_qualifies_the_authority_paths() -> None: - triggers = _triggers() - assert "workflow_dispatch" in triggers - for event in ("pull_request", "push"): - for path in RELEVANT_PATHS: - assert path in triggers[event]["paths"], (event, path) - assert triggers["push"]["branches"] == ["main"] - - -def test_workflow_stays_read_only_and_secret_free() -> None: - assert WORKFLOW["permissions"] == {"contents": "read"} - assert "secrets." not in WORKFLOW_TEXT - assert "pull_request_target" not in WORKFLOW_TEXT - - -def test_workflow_runs_a_pinned_bounded_disposable_server() -> None: - assert JOB["runs-on"] == "ubuntu-latest" - assert JOB["timeout-minutes"] <= 10 - service = JOB["services"]["postgres"] - assert service["image"] == "postgres:16.15" - assert "pg_isready" in service["options"] - assert service["env"]["POSTGRES_DB"] == "loopx_store" - - -def test_workflow_uses_the_canonical_ladder_row() -> None: - rows = {row.id: row for row in LADDER_ROWS} - row = rows["s2b.postgresql_conformance_live"] - assert row.gate == "env:postgresql" - step = _step_running("authority_e2e_ladder") - assert "--row s2b.postgresql_conformance_live" in step["run"] - assert "--report-json postgresql-conformance-report.json" in step["run"] - assert step["env"]["LOOPX_TEST_POSTGRES_URL"].endswith("/loopx_store") - - -def test_workflow_asserts_the_service_path_by_name() -> None: - step = _step_running("npm run test:postgresql-authority-service") - assert step["env"]["LOOPX_TEST_POSTGRES_SERVICE_URL"].endswith("/loopx_service") - assert step["env"]["LOOPX_TEST_POSTGRES_URL"].endswith("/loopx_store") - # The suite keeps a placeholder test that stays green without a URL, so the - # real-server case must be required by name: a skip is an evidence gap. - assert "grep -Fq" in step["run"] - assert ( - "PostgreSQL service admits an authorized tenant and rotates a restored incarnation" - in step["run"] - ) - # A renamed or dropped script is a coverage loss, not a silent skip. - installed = _step_named("Assert the service admission suite is available") - assert "test:postgresql-authority-service" in installed["run"] - - -def test_workflow_keeps_bounded_evidence() -> None: - uploads = [step for step in _steps() if "upload-artifact" in str(step.get("uses", ""))] - assert len(uploads) == 1 - paths = uploads[0]["with"]["path"] - assert "postgresql-conformance-report.json" in paths - assert "postgresql-service-admission.log" in paths diff --git a/tests/test_remote_authority_compose_bundle.py b/tests/test_remote_authority_compose_bundle.py deleted file mode 100644 index 1e7c1e42d8..0000000000 --- a/tests/test_remote_authority_compose_bundle.py +++ /dev/null @@ -1,55 +0,0 @@ -"""The public self-hosting artifact preserves provenance without runtime secrets.""" - -import hashlib -import io -from pathlib import Path -import subprocess -import sys -import tarfile - - -ROOT = Path(__file__).resolve().parents[1] -BUILDER = ROOT / "examples/coordination/remote-authority-compose/build_bundle.py" - - -def build(output: Path, revision: str) -> subprocess.CompletedProcess[str]: - return subprocess.run( - [sys.executable, str(BUILDER), "--source-revision", revision, "--output", str(output)], - cwd=ROOT, capture_output=True, text=True, check=False, - ) - - -def test_bundle_is_reproducible_and_preserves_exact_source_and_license(tmp_path: Path) -> None: - revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip() - first, second = tmp_path / "first.tar.gz", tmp_path / "second.tar.gz" - assert build(first, revision).returncode == 0 - assert build(second, revision).returncode == 0 - assert first.read_bytes() == second.read_bytes() - with tarfile.open(first) as archive: - prefix = "loopx-remote-authority-compose/" - for name in ("LICENSE", "NOTICE"): - assert archive.extractfile(prefix + name).read() == subprocess.check_output( - ["git", "show", f"{revision}:{name}"], cwd=ROOT, - ) - metadata = dict(line.split("=", 1) for line in archive.extractfile(prefix + ".env").read().decode().splitlines()) - source = archive.extractfile(prefix + "loopx-source.tar.gz").read() - assert metadata["LOOPX_REVISION"] == revision - assert hashlib.sha256(source).hexdigest() == metadata["LOOPX_SOURCE_SHA256"] - with tarfile.open(fileobj=io.BytesIO(source)) as code: - path = "loopx/control_plane/coordination/remote_authority_policy.ts" - assert code.extractfile(path).read() == subprocess.check_output(["git", "show", f"{revision}:{path}"], cwd=ROOT) - assert archive.getmember(prefix + "start.sh").mode == 0o755 - assert all(member.isfile() and not member.name.startswith("/") and ".." not in Path(member.name).parts - for member in archive.getmembers()) - assert not any(Path(member.name).name in {"credential", "policy.json", "key.pem", "generation.json"} - for member in archive.getmembers()) - - -def test_builder_refuses_bad_revision_and_preserves_existing_output(tmp_path: Path) -> None: - output = tmp_path / "bundle.tar.gz" - assert build(output, "main").returncode != 0 - assert not output.exists() - revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip() - output.write_bytes(b"existing artifact") - assert build(output, revision).returncode != 0 - assert output.read_bytes() == b"existing artifact" diff --git a/tsconfig.control-plane.json b/tsconfig.control-plane.json index cc7bab26b1..cf24c077ca 100644 --- a/tsconfig.control-plane.json +++ b/tsconfig.control-plane.json @@ -30,11 +30,8 @@ "examples/coordination/sqlite-capacity.ts", "examples/coordination/local-provider-comparison.ts", "examples/coordination/sqlite-authority-migration.ts", - "examples/coordination/remote-authority-service.ts", "examples/coordination/remote-authority-client.ts", - "tests/control_plane_ts/remote_authority.test.ts", - "tests/control_plane_ts/remote_authority.integration.test.ts", - "tests/control_plane_ts/remote_authority_fixture.ts", + "tests/control_plane_ts/remote_authority_client.test.ts", "tests/control_plane_ts/sqlite_capacity.test.ts", "tests/control_plane_ts/local_provider_comparison.test.ts", "tests/control_plane_ts/effect_program.test.ts", @@ -83,10 +80,6 @@ "tests/control_plane_ts/nokv_jsonl_transport.test.ts", "tests/control_plane_ts/nokv_stage2a_qualification_harness.test.ts", "tests/control_plane_ts/authority_store_readback_probe.ts", - "tests/control_plane_ts/postgresql_authority_store.integration.test.ts", - "tests/control_plane_ts/postgresql_authority_service.test.ts", - "tests/control_plane_ts/postgresql_authority_service.integration.test.ts", - "tests/control_plane_ts/postgresql_authority_service_fixture.ts", "tests/control_plane_ts/delivery_continuity.test.ts", "tests/control_plane_ts/delivery_history.test.ts", "tests/control_plane_ts/delivery_response.test.ts",