From 3fac6ed04c9a0bbfecc4e86ccaffb6aed6af5e23 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 21 Nov 2026 17:56:58 +0000 Subject: [PATCH 01/11] Plan Clients, VMs, and Apps implementation --- TODO.md | 102 ++++++++++++++++++-------------------------------------- 1 file changed, 32 insertions(+), 70 deletions(-) diff --git a/TODO.md b/TODO.md index 94859593..88eee734 100644 --- a/TODO.md +++ b/TODO.md @@ -1,84 +1,46 @@ -# Built-in MCP workflows and renewable VM grants +# Factorize Clients, VMs, and Apps -## Execution instructions +Implement the items below in order, from top to bottom. Work on one item until all its acceptance criteria are met and checked, then move to the next item. Check a box only after verifying it with the available VM shell, repository tests, browser tests, or GitHub tools. Record the verification command and result alongside each completed section. Fix recoverable failures and rerun the relevant checks. Do not stop until every criterion is complete. If access or infrastructure genuinely blocks progress, record the exact failed operation and leave the affected boxes open. -Implement each item from top to bottom. An item is complete only when all its acceptance criteria are fulfilled and checked off, with local verification evidence recorded beneath it. Only then start the next item. Continue until all items are complete. Do not mark unverified work complete. +Work in the exe.dev checkout on `codex/clients-vms-apps`. Follow `AGENTS.md` and the current contracts in this checkout. Use fixtures and the isolated local validation database. Do not run tests against production. -All criteria must be achievable by this agent in this VM. Use the existing pinned Factorize validation runner container (Node 24.12.0, npm 11.19.0), isolated loopback PostgreSQL and deterministic local provider/model fixtures. No criterion requires production deployment, a live provider account, external approval or publishing a plugin. Never validate against production/shared databases. Preserve package boundaries and existing client compatibility. Register JSON application operations under /api/v1, document them in OpenAPI and regenerate the public client. Run the complete root npm test after the final code change. +## 0. Verify the development environment -## 1. [x] Canonical build-shipped workflow skills +- [x] Boot the VM from the pinned Factorize validation runner image. +- [x] Clone `GenerationSoftware/factorize` and verify write access from the guest by pushing the feature branch and reading its ref back from GitHub. +- [ ] Verify Node 24.12.0 and npm 11.19.0. Run the image smoke check for PostgreSQL and Chromium and run `npm ci`. -Create five standard, portable SKILL.md files: manage presets; manage skills; run and supervise work; manage triggers; manage environments and access. Explain real MCP operations with useful examples, authorization prerequisites, safe edits/retries and verification. Cover all preset types/defaults, skill frontmatter and dependencies, direct runs and pinned inputs, completion subscriptions and fallback, automation delivery diagnostics, reflection, exe.dev/Fly.io lifecycle and renewable grants. These files are the single source of truth; no plugin manifests or installation artifacts yet. Embed deterministic generated content into the Worker build without reading files at runtime. +## 1. Display existing VM attachments -Acceptance: +- [ ] Add a **VMs** dashboard navigation entry. Show VMs with active Factorize proxy access, with machine name or ID, provider, access status, and links to the authorizing run or client connection. +- [ ] Add **VMs** beside **Trace** and **Info** on run details. Show current and historical run VM access from retained binding records, including terminated runs where a record exists. +- [ ] Make the same tenant-scoped read model support current and historical client VM grants for the Clients screen in item 2. Record attachment source, expiry and revocation where available. Use persisted access eligibility without polling provider power state or changing how VMs are granted or provisioned. +- [ ] Verify with application tests that active and historical records appear in the appropriate views, unrelated machines are excluded, and another tenant cannot read them. Verify navigation and links in Chromium, including the empty state. -- [x] Exactly five canonical skills have valid standard name/description frontmatter and cover the agreed workflows and operation names. -- [x] Local tests compare embedded bytes against the canonical files and validate stable, content-versioned URIs, exact byte sizes and SHA-256 manifests. -- [x] Generation is deterministic; a local drift check rejects stale generated content. No team/session/provider credentials appear in the catalog. +## 2. Move Authorized Clients to Clients -Evidence: Pinned runner npm ci completed; generate:skills and check:skills passed. builtin-skills.test.ts: 4 tests passed, including exact canonical bytes/manifests, deterministic generation, stale-output rejection and invalid URI rejection. +- [ ] Add **Clients** to the dashboard navigation and redirect the old Authorized Clients settings URL. Show one row per MCP connection and put the OAuth app name in its own column. Keep API tokens and registered sign-in app connections outside this list. +- [ ] A connection detail shows its active and historical VM grants, including access state. Verify two connections with the same OAuth client ID keep separate VM histories and can be revoked independently. +- [ ] **Edit permissions** offers the team default via a preset selector. Reuse the repository's team permission default where available and the existing consent and scope definitions. Applying the default cannot grant additional scopes without consent. Keep grants as-is until an edit. Where no applicable default exists, show its unavailable state and preserve the grant. +- [ ] Verify that narrowing permissions takes effect for outstanding requests and any dependent VM access that requires removed permissions. Verify the existing per-connection and compatibility client-wide revoke operations still work. -## 2. [x] Static MCP Skills extension boundary +## 3. Register and manage apps -Publish only the built-in catalog through skills/list and skills/get for active authenticated identities, independent of skills:read. Serve its exact Markdown through resources/read. Editable team skills remain behind existing tools; retain authorized legacy team resources for compatibility. Pinned run packages remain readable resources but never appear in extension discovery. Keep authorization checks on every read and preserve Pi's pinned input references. +- [ ] Add **Apps** to the dashboard navigation with **Connected apps** and **Registered apps** views. Use **Registered apps** for team-owned registrations. The connected view consumes sign-in grants from item 4, with empty-state and fixture coverage in this item. +- [ ] Team owners can register, list, inspect, edit, and disable confidential server-side apps and configure exact redirect URLs. Apps are team-only by default and support an opt-in **Available to everyone** setting. +- [ ] Add dedicated app-management authorization for MCP. Verify a scoped MCP caller can manage non-secret fields for its team and an unscoped or cross-team caller is rejected. +- [ ] Verify client secret creation and rotation are available only to an authorized browser session. Return newly issued secrets once, store a verifier, and never expose secret material in MCP, registration reads, logs, or test output. -Acceptance: +## 4. Implement Sign in with Factorize -- [x] Local MCP tests discover/get/read exactly five built-ins with matching manifests and no team or run entries, including clients without skills:read. -- [x] Different tenants/scopes/runs and team edits/renames/deletes cannot change built-in catalog content; inactive identities and invalid/noncanonical URIs are rejected. -- [x] Local tests prove team list/get tools still work with their existing scopes, legacy resources remain protected, and active runs can read their own immutable pinned supporting files without skills:read. -- [x] Hosted Pi discovery/read tests pass with the static catalog and retained pinned input resources. +- [ ] Implement OpenID Connect for confidential server-side applications with code flow, PKCE S256, registered redirect URLs, client authentication, discovery, JWKS, and signed ID tokens. Verify a sample server app can perform sign-in and validate its ID token. +- [ ] The built-in default is minimal identity with a stable subject. An app can explicitly ask for the current Factorize API scopes and receives only scopes expressly authorized by the user. Verify identity-only tokens cannot exercise Factorize application permissions and an approved scope can access its API operation. +- [ ] Verify private apps reject other teams, public apps can accept other teams, and granted API access applies to the authorizing user's team. Cover invalid redirect, client secret, PKCE, and code reuse, and handling of scope and token audience. +- [ ] **Connected apps** displays authorized sign-in app connections and supports revoking each connection. Verify revocation, app disabling, and secret rotation enforce their respective token and authentication effects. Verify existing MCP OAuth and device-code flows still work. -Evidence: Pinned runner MCP catalog tests (4), package helpers (3), canonical catalog tests (4) passed; hosted-pi-mcp.postgres.test.ts (7) passed after updating the deterministic model fixture to discover built-ins. Tests cover cross-team/scope stability, rejected inactive identities/paths, retained team tools/resources and pinned supporting files after edits/deletion. Logs: /tmp/factorize-workflows-item2.log and /tmp/factorize-workflows-item2-pi.log. +## 5. Finish validation and open the PR -## 3. [x] Explicit renewable one-hour grant leases - -Decouple new VM grant expiry from the issuing OAuth access token. Creation and explicit renewal give one hour from that operation; token refresh alone does not renew a grant. Add renew_vm_grant and POST /api/v1/agent/vm-grants/{grantId}/renew. Only the same active parent OAuth authorization may renew with vm:grant, proxy:use and continuing access to the provider and every selected integration. Expired/revoked grants cannot be renewed or revived by replaying creation; require a new grant/idempotency key. Preserve existing exe.dev grant input and receipt compatibility. Timeout/revocation removes access, never deletes the VM. - -Acceptance: - -- [x] Local database/API tests verify one-hour creation independent of remaining token lifetime, successful renewal and no implicit renewal during ordinary requests/replayed creation. -- [x] Tests reject expired/revoked/foreign-parent/foreign-tenant renewal, missing scopes and revoked/disconnected provider or selected integrations; rejected renewal does not extend expiry. -- [x] Proxy access remains limited to selected integrations and checks current parent authorization; expiry/revocation blocks access and cleanup preserves the VM. -- [x] REST/MCP renewal use the same implementation; OpenAPI and generated client contain the validated operation. - -Evidence: vm-grant-renewal.postgres.test.ts: 12 passed for shared REST/MCP renewal, token-independent leases, replay, ownership, expiry/revocation and integration access changes. Real Worker/OAuth agent-api-runtime.test.mjs: 2 passed with signed exe.dev identity, grant creation/renewal, subset enforcement, expiry/revocation and cleanup. npm run generate passed. Logs: /tmp/factorize-workflows-item3.log and /tmp/factorize-workflows-item3-runtime.log. - -## 4. [x] Fly.io grants using shared OIDC primitives - -Extend grant_vm to Fly.io with an app selector plus exact Machine ID, preserving exe.dev calls. Reuse shared issuer discovery/signature/claim verification, Fly token acquisition and guest execution. Bind grants to the connected organization, app, machine, exact subject, grant-specific audience and guest-local proof. Return usable provider-specific token acquisition instructions. Expose both provider capabilities in reflection. Provider-specific cleanup never destroys the VM. - -Acceptance: - -- [x] Deterministic local provider fixtures enroll both providers and authenticate actual signed OIDC tokens through the proxy boundary; Fly grants renew/revoke/expire like exe.dev grants. -- [x] Tests reject wrong organization/app/machine/subject/audience, invalid/expired signatures, wrong proof, cross-parent access and changed provider identity/configuration. -- [x] Tests verify idempotent enrollment/retries, conflicting idempotency input rejection, selected-integration limits, fresh token renewal and safe cleanup without VM deletion. -- [x] Reflection and generated contracts identify exe.dev and Fly.io grant capabilities; existing exe.dev callers and receipts remain compatible. - -Evidence: Real Worker/OAuth runtime suite: 2 passed, including signed Fly and exe.dev OIDC enrollment/authentication, malformed identities/proofs, changed app organization/machine subject, idempotent enrollment recovery, token refresh plus explicit renewal, parent revocation and non-destructive cleanup. Shared verifier/token-source/lease suites: 29 passed. API TypeScript check passed. Logs: /tmp/factorize-workflows-item4-runtime.log and /tmp/factorize-workflows-item4-tests.log. - -## 5. [x] Documentation, integration review and complete validation - -Update MCP overview/tool reference and integration authorization documentation for static workflow discovery, live team skill tools, pinned resources, both grant providers and explicit one-hour lease renewal. Verify examples and contracts against implementation. Review authorization and compatibility together and complete root validation. - -Acceptance: - -- [x] Documentation validation, generated-content/contract drift checks, TypeScript and package boundary checks pass locally. -- [x] The complete root npm test passes after the final code change in the pinned runner with isolated databases, including real Worker/OAuth and Chromium lanes. -- [x] git diff --check passes; review confirms exact build-owned skills, preserved pinned/team reads, bounded delegation, renewal ownership and provider identity verification. -- [x] All preceding items have local evidence recorded here; the handoff reports changed behavior, validation and any limitations honestly. - - -Evidence: Complete root npm test passed after the final code change on 2026-11-21 in factorize-proxy-validation-final, using the pinned runner image sha256:1333c4a7bb49c4cbcdd28ca2b57331dfcbff7cf611db1aaeaa4830517aacf6d4, Node 24.12.0 and npm 11.19.0. npm ci completed before validation; bootstrap:test created the isolated loopback database and db:migrate applied 0093_fly_vm_grants. Reproduce from the repository root: - -```sh -docker exec -u exedev -w /work \ - -e DATABASE_URL=postgresql://postgres:local-validation-only@127.0.0.1:55434/factorize_workflows_validation \ - -e AUTH_TEST_DATABASE_URL=postgresql://postgres:local-validation-only@127.0.0.1:55434/factorize_workflows_validation \ - -e QUEUE_TEST_DATABASE_URL=postgresql://postgres:local-validation-only@127.0.0.1:55434/factorize_workflows_validation \ - factorize-proxy-validation-final npm test -git diff --check -git diff --cached --check -``` - -Result: all root validation steps passed, including generated skills/contracts and drift checks, builds, documentation/MCP inventory, package/API boundaries, TypeScript, Worker bundles/runtimes and OAuth, compiled app/API/PostgreSQL integration, Chromium, portable proxy and Docker. Backend: 149 files, 892 passed / 5 existing skips. Compiled integration: 40 passed. Functional Chromium: 6 passed. Application/browser checks: 139 passed / 1 existing skip. Proxy: 32 protocol tests and 4 harness tests passed. Docker historical compatibility: 2 passed. Full log: /tmp/factorize-workflows-root-final.log. Separate documentation generation/validation and package boundary checks passed; both whitespace checks passed. Final review covered static exact-byte publication and deployment generation, authenticated built-in reads, protected team/pinned resources, additive migration compatibility, parent ownership and current integration epochs at renewal, exact provider OIDC bindings, guest proof handling and non-destructive cleanup. Provider acceptance uses deterministic local fixtures and signed JWTs, without claiming new live-provider/deployment validation. Plugin packaging remains intentionally outside scope. +- [ ] New JSON application operations use the shared documented `/api/v1` contract. Register routes in `protected-api.ts`, update OpenAPI, regenerate the API client, and verify authorization, validation and contract tests. Keep OAuth and OIDC wire-protocol routes in their protocol namespaces. +- [ ] Document server app sign-in setup, team and public visibility, scopes, client secret rotation, and revocation. Preserve REST and MCP full/detail/trace responses and conditional-write omission compatibility. +- [ ] Run `npm run check:boundaries`, `git diff --check`, and `npm run test:local` on the final code. The last command runs the complete root `npm test` suite with the isolated database. Record results and check every criterion supported by verification. +- [ ] Commit and push the completed branch from the VM. Open a pull request against `main`, verify its URL and head, and include validation results. Update the final checklist state on the branch. From 20ff53955b4eb800ffe724fd678371afe8504420 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 21 Nov 2026 18:03:54 +0000 Subject: [PATCH 02/11] Show active and historical VM proxy attachments --- TODO.md | 14 +- packages/api-client/src/schema.d.ts | 203 ++++++++++++ packages/api/src/agent-api.ts | 2 +- packages/api/src/api-contract.ts | 15 + packages/api/src/flow-service.ts | 21 ++ .../src/postgres/vm-attachment-repository.ts | 109 ++++++ packages/api/src/vm-attachment-contracts.ts | 16 + .../api/test/vm-attachments.postgres.test.ts | 56 ++++ .../app/src/features/auth/account-bar.tsx | 2 +- .../app/src/features/auth/team-navigation.tsx | 2 +- packages/app/src/features/runs/detail.tsx | 12 +- packages/app/src/features/runs/routes.tsx | 4 +- packages/app/src/features/vms/attachments.tsx | 59 ++++ packages/app/src/features/vms/routes.tsx | 16 + packages/app/src/main.tsx | 3 +- packages/app/test/runs.browser.test.mjs | 21 ++ packages/docs/openapi.yaml | 311 ++++++++++++++++++ 17 files changed, 851 insertions(+), 15 deletions(-) create mode 100644 packages/api/src/postgres/vm-attachment-repository.ts create mode 100644 packages/api/src/vm-attachment-contracts.ts create mode 100644 packages/api/test/vm-attachments.postgres.test.ts create mode 100644 packages/app/src/features/vms/attachments.tsx create mode 100644 packages/app/src/features/vms/routes.tsx diff --git a/TODO.md b/TODO.md index 88eee734..69c2070b 100644 --- a/TODO.md +++ b/TODO.md @@ -8,14 +8,18 @@ Work in the exe.dev checkout on `codex/clients-vms-apps`. Follow `AGENTS.md` and - [x] Boot the VM from the pinned Factorize validation runner image. - [x] Clone `GenerationSoftware/factorize` and verify write access from the guest by pushing the feature branch and reading its ref back from GitHub. -- [ ] Verify Node 24.12.0 and npm 11.19.0. Run the image smoke check for PostgreSQL and Chromium and run `npm ci`. +- [x] Verify Node 24.12.0 and npm 11.19.0. Run the image smoke check for PostgreSQL and Chromium and run `npm ci`. + +Verified on `factorize-clients-vms-apps`. The image smoke script passed with the image-provided `/etc/environment` loaded for the non-login guest process. PostgreSQL authenticated on loopback, Chromium launched as `exedev`, and `npm ci` succeeded. ## 1. Display existing VM attachments -- [ ] Add a **VMs** dashboard navigation entry. Show VMs with active Factorize proxy access, with machine name or ID, provider, access status, and links to the authorizing run or client connection. -- [ ] Add **VMs** beside **Trace** and **Info** on run details. Show current and historical run VM access from retained binding records, including terminated runs where a record exists. -- [ ] Make the same tenant-scoped read model support current and historical client VM grants for the Clients screen in item 2. Record attachment source, expiry and revocation where available. Use persisted access eligibility without polling provider power state or changing how VMs are granted or provisioned. -- [ ] Verify with application tests that active and historical records appear in the appropriate views, unrelated machines are excluded, and another tenant cannot read them. Verify navigation and links in Chromium, including the empty state. +- [x] Add a **VMs** dashboard navigation entry. Show VMs with active Factorize proxy access, with machine name or ID, provider, access status, and links to the authorizing run or client connection. +- [x] Add **VMs** beside **Trace** and **Info** on run details. Show current and historical run VM access from retained binding records, including terminated runs where a record exists. +- [x] Make the same tenant-scoped read model support current and historical client VM grants for the Clients screen in item 2. Record attachment source, expiry and revocation where available. Use persisted access eligibility without polling provider power state or changing how VMs are granted or provisioned. +- [x] Verify with application tests that active and historical records appear in the appropriate views, unrelated machines are excluded, and another tenant cannot read them. Verify navigation and links in Chromium, including the empty state. + +Verified with `vitest run test/vm-attachments.postgres.test.ts` (2 passed), `npm run build:app`, the focused Chromium inventory test (1 passed), and `git diff --check`. ## 2. Move Authorized Clients to Clients diff --git a/packages/api-client/src/schema.d.ts b/packages/api-client/src/schema.d.ts index 1d14aa3b..44664d5f 100644 --- a/packages/api-client/src/schema.d.ts +++ b/packages/api-client/src/schema.d.ts @@ -2977,6 +2977,76 @@ export interface paths { patch?: never; trace?: never; }; + "/api/v1/vms": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * List VMs with active Factorize proxy access + * @description Requires an interactive owner session. Reports access status from Factorize records. + * + * Requires the runs:read scope. + * + * Accepts a scoped bearer token, vendor-verified run identity, or interactive owner session. + */ + get: operations["get_api_v1_vms"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/runs/{runId}/vms": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * List a run's current and historical VM access + * @description Requires the runs:read scope. + * + * Accepts a scoped bearer token, vendor-verified run identity, or interactive owner session. + */ + get: operations["get_api_v1_runs_runId_vms"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/api/v1/access/authorizations/{grantId}/vms": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * List current and historical VM access delegated by an MCP connection + * @description Requires an interactive owner session. + * + * Requires the runs:read scope. + * + * Accepts a scoped bearer token, vendor-verified run identity, or interactive owner session. + */ + get: operations["get_api_v1_access_authorizations_grantId_vms"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; "/api/v1/runs/{runId}/status": { parameters: { query?: never; @@ -12836,6 +12906,139 @@ export interface operations { 503: components["responses"]["Error503"]; }; }; + get_api_v1_vms: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Active VM access attachments */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id: string; + /** @enum {string} */ + source: "run" | "client"; + sourceId: string; + sourceName: string | null; + vmId: string; + app: string | null; + /** @enum {string} */ + provider: "exe.dev" | "fly.io"; + /** Format: date-time */ + expiresAt: string; + revokedAt: string | null; + /** @enum {string} */ + status: "active" | "expired" | "revoked" | "inactive" | "pending"; + }[]; + }; + }; + 400: components["responses"]["Error400"]; + 401: components["responses"]["Error401"]; + 403: components["responses"]["Error403"]; + 404: components["responses"]["Error404"]; + 409: components["responses"]["Error409"]; + 429: components["responses"]["Error429"]; + 500: components["responses"]["Error500"]; + 503: components["responses"]["Error503"]; + }; + }; + get_api_v1_runs_runId_vms: { + parameters: { + query?: never; + header?: never; + path: { + runId: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Run VM attachments */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id: string; + /** @enum {string} */ + source: "run" | "client"; + sourceId: string; + sourceName: string | null; + vmId: string; + app: string | null; + /** @enum {string} */ + provider: "exe.dev" | "fly.io"; + /** Format: date-time */ + expiresAt: string; + revokedAt: string | null; + /** @enum {string} */ + status: "active" | "expired" | "revoked" | "inactive" | "pending"; + }[]; + }; + }; + 400: components["responses"]["Error400"]; + 401: components["responses"]["Error401"]; + 403: components["responses"]["Error403"]; + 404: components["responses"]["Error404"]; + 409: components["responses"]["Error409"]; + 429: components["responses"]["Error429"]; + 500: components["responses"]["Error500"]; + 503: components["responses"]["Error503"]; + }; + }; + get_api_v1_access_authorizations_grantId_vms: { + parameters: { + query?: never; + header?: never; + path: { + grantId: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description Connection VM attachments */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": { + id: string; + /** @enum {string} */ + source: "run" | "client"; + sourceId: string; + sourceName: string | null; + vmId: string; + app: string | null; + /** @enum {string} */ + provider: "exe.dev" | "fly.io"; + /** Format: date-time */ + expiresAt: string; + revokedAt: string | null; + /** @enum {string} */ + status: "active" | "expired" | "revoked" | "inactive" | "pending"; + }[]; + }; + }; + 400: components["responses"]["Error400"]; + 401: components["responses"]["Error401"]; + 403: components["responses"]["Error403"]; + 404: components["responses"]["Error404"]; + 409: components["responses"]["Error409"]; + 429: components["responses"]["Error429"]; + 500: components["responses"]["Error500"]; + 503: components["responses"]["Error503"]; + }; + }; get_api_v1_runs_runId_status: { parameters: { query?: never; diff --git a/packages/api/src/agent-api.ts b/packages/api/src/agent-api.ts index 8f287baa..0fc92e72 100644 --- a/packages/api/src/agent-api.ts +++ b/packages/api/src/agent-api.ts @@ -20,7 +20,7 @@ import type { z } from "zod"; export type AgentIntegration = {id: string; kind: "bearer" | "model" | "github" | "linear" | "fly" | "exe" | "amp" | "clickup" | "x"; credential_id: string; name: string; description: string | null; available: boolean; policy: "baseline" | "requestable" | "disabled"; epoch: number; revision: number}; export const requiredIntegrationScope = (row: AgentIntegration) => `integration:${row.id}:${row.epoch}`; type ConnectionPolicy = { integration_overrides: Record; disabled_scopes: string[] }; -function connectionAccess(row: AgentIntegration, scopes: string[], policy: ConnectionPolicy) { +export function connectionAccess(row: AgentIntegration, scopes: string[], policy: ConnectionPolicy) { const scope = requiredIntegrationScope(row); if (policy.disabled_scopes.includes("proxy:use") || policy.integration_overrides[row.id] === false || row.policy === "disabled" || !row.available) return "blocked"; return row.policy === "baseline" || policy.integration_overrides[row.id] === scope || scopes.includes(scope) ? "granted" : "requestable"; diff --git a/packages/api/src/api-contract.ts b/packages/api/src/api-contract.ts index d4bb9335..b37843f6 100644 --- a/packages/api/src/api-contract.ts +++ b/packages/api/src/api-contract.ts @@ -25,6 +25,7 @@ import { runSummaryResponse, runPageResponse, runStatusResponse, fullRunResponse import { triggerPageQuery, triggerSummaryPage, triggerSelectorPage, triggerResponse, triggerUpdateInput } from "./job-contracts"; import { projectGraphResponse } from "./project-graph-contracts"; import { z } from "zod"; +import { vmAttachments } from "./vm-attachment-contracts"; import type { ApiService } from "./flow-service"; import { triggerInputSchema, @@ -1085,6 +1086,20 @@ export const API_ROUTES: RouteContract[] = [ }, (service, { params }) => service.getWebhookDelivery(params.deliveryId), ), + route("GET", "/api/v1/vms", "runs:read", { + summary: "List VMs with active Factorize proxy access", + description: "Requires an interactive owner session. Reports access status from Factorize records.", + responses: { "200": { description: "Active VM access attachments", content: { "application/json": { schema: jsonSchema(vmAttachments, "output") } } } }, + }, service => service.listProxyVms()), + route("GET", "/api/v1/runs/{runId}/vms", "runs:read", { + summary: "List a run's current and historical VM access", + responses: { "200": { description: "Run VM attachments", content: { "application/json": { schema: jsonSchema(vmAttachments, "output") } } } }, + }, (service, { params }) => service.listRunVms(params.runId), { parameters: z.object({ runId: z.guid() }) }), + route("GET", "/api/v1/access/authorizations/{grantId}/vms", "runs:read", { + summary: "List current and historical VM access delegated by an MCP connection", + description: "Requires an interactive owner session.", + responses: { "200": { description: "Connection VM attachments", content: { "application/json": { schema: jsonSchema(vmAttachments, "output") } } } }, + }, (service, { params }) => service.listClientVms(params.grantId), { parameters: z.object({ grantId: z.string().min(1).max(255) }) }), route("GET", "/api/v1/runs/{runId}/status", "runs:read", { summary: "Get lightweight run status", description: "Only bounded encrypted prompt/script previews are decrypted; full prompt, invocation context, artifacts, activity and diagnostics stay in the detail operation. Continue polling terminal runs while finalizing is true. trace_revision changes on generation transition or canonical projection/replay; live appends retain the revision. Full detail remains available at GET /runs/{runId}.", diff --git a/packages/api/src/flow-service.ts b/packages/api/src/flow-service.ts index d5ee7e52..88bc6efa 100644 --- a/packages/api/src/flow-service.ts +++ b/packages/api/src/flow-service.ts @@ -30,6 +30,8 @@ import { submitRun } from "./postgres/run-submission"; import type { DatabaseClient } from "./postgres/database"; import { startRunInput,projectRunDefaults,scopeSchema, teamRunDefaults } from "./flow-schemas"; import { grantVm, renewVmGrant, revokeVmGrant } from "./vm-grants"; +import { VmAttachmentRepository } from "./postgres/vm-attachment-repository"; +import { vmAttachments } from "./vm-attachment-contracts"; import { AgentApiService, listAgentPolicies } from "./agent-api"; import {PhoenixIntegration,runPhoenixStatus} from "./phoenix"; import {phoenixInput,phoenixRevision} from "./phoenix-contracts"; @@ -701,6 +703,25 @@ export class ApiService { } async search(query: string) { await this.authorize("skills:read"); await this.authorize("runs:read"); return publicValue(searchResponse, await new OperationsRepository(databaseFor(this.env), this.auth.tenantId).search(query)); } async eventContextMetadata() { await this.authorize("triggers:read"); return eventContextCatalog; } + async listProxyVms() { + await this.authorizeOwnerSession("runs:read"); + const repository = new VmAttachmentRepository(databaseFor(this.env), this.auth.tenantId); + const [runs, clients] = await Promise.all([repository.runs(), repository.clients(this.auth.userId)]); + return publicValue(vmAttachments, [...runs, ...clients].filter(attachment => attachment.status === "active")); + } + + async listRunVms(runId: string) { + await this.getRunStatus(runId); + const repository = new VmAttachmentRepository(databaseFor(this.env), this.auth.tenantId); + return publicValue(vmAttachments, await repository.runs(runId)); + } + + async listClientVms(grantId: string) { + await this.authorizeOwnerSession("runs:read"); + const repository = new VmAttachmentRepository(databaseFor(this.env), this.auth.tenantId); + return publicValue(vmAttachments, await repository.clients(this.auth.userId, grantId)); + } + async getRunStatus(runId: string) { await this.authorize("runs:read"); const status = await new RunQueryRepository(databaseFor(this.env), this.auth.tenantId).status(runId); if (!status) throw new ServiceError(404, "not_found", "Run not found"); return status; } async reportRunError(runId: string, input: unknown) { await this.authorize("runs:write"); diff --git a/packages/api/src/postgres/vm-attachment-repository.ts b/packages/api/src/postgres/vm-attachment-repository.ts new file mode 100644 index 00000000..889226a5 --- /dev/null +++ b/packages/api/src/postgres/vm-attachment-repository.ts @@ -0,0 +1,109 @@ +import type { AgentIntegration } from "../agent-api"; +import { connectionAccess } from "../agent-api"; +import type { Database } from "./database"; + +type AttachmentStatus = "active" | "expired" | "revoked" | "inactive" | "pending"; +type RunBinding = { + sourceId: string; + vmId: string; + provider: "exe.dev" | "fly.io"; + expiresAt: Date; + revokedAt: Date | null; + runnable: boolean; +}; +type ClientBinding = { + id: string; + sourceId: string; + label: string | null; + vmId: string; + app: string | null; + provider: AgentIntegration; + providerEpoch: number; + expiresAt: Date; + revokedAt: Date | null; + parentRevokedAt: Date | null; + enrolled: boolean; + currentMember: boolean; + parent_scopes: string[]; + disabled_scopes: string[]; + integration_overrides: Record; +}; + +function accessStatus(revoked: Date | null, expiry: Date, now: number, enrolled: boolean, eligible: boolean): AttachmentStatus { + if (revoked) return "revoked"; + if (expiry.getTime() <= now) return "expired"; + if (!enrolled) return "pending"; + return eligible ? "active" : "inactive"; +} + +export class VmAttachmentRepository { + constructor(private db: Database, private tenantId: string) {} + + async runs(runId?: string) { + const { rows } = await this.db.pool.query(` + SELECT b.run_id AS "sourceId", b.vm_id AS "vmId", b.provider, b.expires_at AS "expiresAt", + b.revoked_at AS "revokedAt", + coalesce(t.state IN ('starting','running','blocked') AND r.state IN ('starting','running','blocked') + AND EXISTS (SELECT 1 FROM app.run_authorizations a + JOIN app.members m ON m.tenant_id=a.tenant_id AND m.user_id=a.user_id + JOIN app.auth_users u ON u.id=m.user_id AND u.email_verified + WHERE a.tenant_id=b.tenant_id AND a.run_id=b.run_id AND m.role='owner' + AND m.session_version=a.session_version), false) AS runnable + FROM app.vendor_run_identity_bindings b + JOIN app.trigger_runs t ON t.tenant_id=b.tenant_id AND t.id=b.run_id + LEFT JOIN app.runs r ON r.tenant_id=b.tenant_id AND r.id=b.run_id + WHERE b.tenant_id=$1 AND ($2::uuid IS NULL OR b.run_id=$2) + AND ($2::uuid IS NOT NULL OR (b.revoked_at IS NULL AND b.expires_at>now() + AND t.state IN ('starting','running','blocked'))) + ORDER BY b.issued_at DESC, b.run_id, b.provider`, [this.tenantId, runId ?? null]); + const now = Date.now(); + return rows.map(row => ({ + id: `${row.sourceId}:${row.provider}`, + source: "run" as const, + sourceId: row.sourceId, + sourceName: null, + vmId: row.vmId, + app: null, + provider: row.provider, + expiresAt: row.expiresAt.toISOString(), + revokedAt: row.revokedAt?.toISOString() ?? null, + status: accessStatus(row.revokedAt, row.expiresAt, now, true, row.runnable), + })); + } + + async clients(userId: string, grantId?: string) { + const { rows } = await this.db.pool.query(` + SELECT g.id, p.subject AS "sourceId", p.display_name AS label, g.vm AS "vmId", g.app, + to_jsonb(i) AS provider, g.provider_epoch AS "providerEpoch", g.expires_at AS "expiresAt", + g.revoked_at AS "revokedAt", p.revoked_at AS "parentRevokedAt", g.issuer IS NOT NULL AS enrolled, + EXISTS (SELECT 1 FROM app.members m JOIN app.auth_users u ON u.id=m.user_id AND u.email_verified + WHERE m.tenant_id=p.tenant_id AND m.user_id=p.user_id AND m.role='owner' + AND m.session_version=p.session_version) AS "currentMember", + g.parent_scopes, p.disabled_scopes, p.integration_overrides + FROM app.vm_grants g + JOIN app.agent_identities p ON p.tenant_id=g.tenant_id AND p.id=g.parent_id AND p.kind='oauth' + JOIN app.agent_integrations i ON i.tenant_id=g.tenant_id AND i.id=g.provider_id + WHERE g.tenant_id=$1 AND p.user_id=$2 AND ($3::text IS NULL OR p.subject=$3) + AND ($3::text IS NOT NULL OR (g.revoked_at IS NULL AND p.revoked_at IS NULL AND g.expires_at>now())) + ORDER BY g.expires_at DESC, g.id`, [this.tenantId, userId, grantId ?? null]); + const now = Date.now(); + return rows.map(row => { + const eligible = row.currentMember && row.provider.epoch === row.providerEpoch + && !row.disabled_scopes.includes("vm:grant") + && connectionAccess(row.provider, row.parent_scopes, row) === "granted"; + const revokedAt = row.revokedAt ?? row.parentRevokedAt; + return { + id: row.id, + source: "client" as const, + sourceId: row.sourceId, + sourceName: row.label, + vmId: row.vmId, + app: row.app, + provider: row.app ? "fly.io" as const : "exe.dev" as const, + expiresAt: row.expiresAt.toISOString(), + revokedAt: revokedAt?.toISOString() ?? null, + status: accessStatus(revokedAt, row.expiresAt, now, row.enrolled, eligible), + }; + }); + } +} diff --git a/packages/api/src/vm-attachment-contracts.ts b/packages/api/src/vm-attachment-contracts.ts new file mode 100644 index 00000000..c79fe4e7 --- /dev/null +++ b/packages/api/src/vm-attachment-contracts.ts @@ -0,0 +1,16 @@ +import { z } from "zod"; + +export const vmAttachment = z.object({ + id: z.string(), + source: z.enum(["run", "client"]), + sourceId: z.string(), + sourceName: z.string().nullable(), + vmId: z.string(), + app: z.string().nullable(), + provider: z.enum(["exe.dev", "fly.io"]), + expiresAt: z.iso.datetime(), + revokedAt: z.iso.datetime().nullable(), + status: z.enum(["active", "expired", "revoked", "inactive", "pending"]), +}); + +export const vmAttachments = z.array(vmAttachment); diff --git a/packages/api/test/vm-attachments.postgres.test.ts b/packages/api/test/vm-attachments.postgres.test.ts new file mode 100644 index 00000000..cf2fe7a1 --- /dev/null +++ b/packages/api/test/vm-attachments.postgres.test.ts @@ -0,0 +1,56 @@ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { loadingFixture } from "./helpers/loading-fixture"; +import { VmAttachmentRepository } from "../src/postgres/vm-attachment-repository"; + +vi.mock("cloudflare:workers", () => ({ WorkerEntrypoint: class {}, DurableObject: class {} })); +vi.mock("@cloudflare/workers-oauth-provider", () => ({ OAuthProvider: class {}, AuthorizationError: class extends Error {}, getOAuthApi: vi.fn() })); + +const url = process.env.AUTH_TEST_DATABASE_URL; +describe.skipIf(!url)("VM attachment inventory", () => { + let fixture: Awaited>; + beforeAll(async () => { fixture = await loadingFixture(url!); }); + afterAll(async () => { await fixture?.cleanup(); }); + + it("keeps run access history and scopes reads to the authorizing tenant", async () => { + const { db, tenantId, runId, userId } = fixture; + await db.pool.query("UPDATE app.trigger_runs SET state='running' WHERE tenant_id=$1 AND id=$2", [tenantId, runId]); + await db.pool.query("UPDATE app.runs SET state='running' WHERE tenant_id=$1 AND id=$2", [tenantId, runId]); + await db.pool.query(`INSERT INTO app.run_authorizations(tenant_id,run_id,user_id,session_version,scopes) + SELECT tenant_id,$2,user_id,session_version,'[]' FROM app.members WHERE tenant_id=$1 AND user_id=$3 + ON CONFLICT (tenant_id,run_id) DO NOTHING`, [tenantId, runId, userId]); + await db.pool.query(`INSERT INTO app.vendor_run_identity_bindings + (tenant_id,run_id,provider,vm_id,issuer,subject,audience,issued_at,expires_at) + VALUES ($1,$2,'exe.dev',$3,'https://issuer.example','subject','audience',now(),now()+interval '1 hour')`, [tenantId, runId, `fixture-${runId}`]); + + const inventory = new VmAttachmentRepository(db, tenantId); + expect(await inventory.runs()).toMatchObject([{ sourceId: runId, status: "active" }]); + expect(await new VmAttachmentRepository(db, crypto.randomUUID()).runs(runId)).toEqual([]); + + await db.pool.query("UPDATE app.vendor_run_identity_bindings SET revoked_at=now() WHERE tenant_id=$1 AND run_id=$2", [tenantId, runId]); + expect(await inventory.runs()).toEqual([]); + expect(await inventory.runs(runId)).toMatchObject([{ sourceId: runId, status: "revoked" }]); + }); + + it("isolates delegated grants by OAuth authorization and retains expiry", async () => { + const { db, tenantId, userId } = fixture; + const providerId = crypto.randomUUID(); + await db.pool.query(`INSERT INTO app.agent_integrations(tenant_id,id,kind,credential_id,name,available,policy) + VALUES ($1,$2,'exe','vm-inventory','VM inventory',true,'baseline')`, [tenantId, providerId]); + const creators = [ + { subject: "fixture-connection-one", offset: "1 hour" }, + { subject: "fixture-connection-two", offset: "-1 hour" }, + ]; + for (const { subject, offset } of creators) { + const { rows: [parent] } = await db.pool.query(`INSERT INTO app.agent_identities(tenant_id,kind,subject,user_id,session_version) + SELECT tenant_id,'oauth',$2,user_id,session_version FROM app.members WHERE tenant_id=$1 AND user_id=$3 + RETURNING id`, [tenantId, subject, userId]); + await db.pool.query(`INSERT INTO app.vm_grants(tenant_id,parent_id,provider_id,provider_epoch,vm,permissions,parent_scopes,request_key,issuer,expires_at) + SELECT tenant_id,$2,id,epoch,$3,'{}','["proxy:use","vm:grant"]','inventory','https://issuer.example',now()+$4::interval + FROM app.agent_integrations WHERE tenant_id=$1 AND id=$5`, [tenantId, parent.id, subject, offset, providerId]); + } + const inventory = new VmAttachmentRepository(db, tenantId); + expect(await inventory.clients(userId)).toMatchObject([{ sourceId: creators[0].subject, status: "active" }]); + expect(await inventory.clients(userId, creators[1].subject)).toMatchObject([{ sourceId: creators[1].subject, status: "expired" }]); + expect(await inventory.clients(crypto.randomUUID(), creators[0].subject)).toEqual([]); + }); +}); diff --git a/packages/app/src/features/auth/account-bar.tsx b/packages/app/src/features/auth/account-bar.tsx index a90b8fd8..9b6f9a41 100644 --- a/packages/app/src/features/auth/account-bar.tsx +++ b/packages/app/src/features/auth/account-bar.tsx @@ -85,7 +85,7 @@ export function AccountBar() { : Account} - {authenticated && (["/", "/triggers", "/runs", "/skills", "/integrations"].includes(pathname) || pathname.startsWith("/runs/") || pathname.startsWith("/settings/") || pathname === "/settings") && } + {authenticated && (["/", "/triggers", "/runs", "/skills", "/integrations"].includes(pathname) || pathname.startsWith("/runs/") || pathname === "/vms" || pathname.startsWith("/clients/") || pathname.startsWith("/settings/") || pathname === "/settings") && } {logout.isError &&

{logout.error.message}

} ; } diff --git a/packages/app/src/features/auth/team-navigation.tsx b/packages/app/src/features/auth/team-navigation.tsx index 453a13cf..24a339bc 100644 --- a/packages/app/src/features/auth/team-navigation.tsx +++ b/packages/app/src/features/auth/team-navigation.tsx @@ -2,5 +2,5 @@ import { Link } from "@tanstack/react-router"; import { NavIcon } from "../../shared/nav-icon"; export const tabClasses=(active:boolean)=>`inline-flex min-h-12 shrink-0 items-center gap-2 border-b-2 px-3 text-base font-semibold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-factorize-500 ${active?"border-factorize-500 text-factorize-700 dark:text-factorize-500":"border-transparent text-slate-600 hover:border-stone-300 dark:text-slate-300"}`; export function TeamNavigation({pathname}:{pathname:string}){ - return ; + return ; } diff --git a/packages/app/src/features/runs/detail.tsx b/packages/app/src/features/runs/detail.tsx index a7b1061d..da42eadb 100644 --- a/packages/app/src/features/runs/detail.tsx +++ b/packages/app/src/features/runs/detail.tsx @@ -1,4 +1,5 @@ import {RunTelemetry} from "./telemetry"; +import { VmAttachments } from "../vms/attachments"; import { Button, Breadcrumb, Page, ResourceHeader, RunStatusLabel } from "../../shared/ui"; import { ContinuousTrace } from "./continuous-trace"; import { ExpandedRunDetail } from "./expanded-detail"; @@ -24,9 +25,9 @@ function RuntimeInfo({ startedAt, updatedAt, active }: { startedAt: string | nul export function RunDetail() { const { runId } = useParams({ strict: false }); const location = useLocation(), navigate = useNavigate(), cache = useQueryClient(); - const tab: "trace" | "info" = location.pathname.endsWith("/info") ? "info" : "trace"; + const tab: "trace" | "info" | "vms" = location.pathname.endsWith("/vms") ? "vms" : location.pathname.endsWith("/info") ? "info" : "trace"; const fullTrace = location.pathname.endsWith("/trace"); - const tabPath = (name: "trace" | "info"): "/runs/$runId" | "/runs/$runId/info" => name === "trace" ? "/runs/$runId" : "/runs/$runId/info"; + const tabPath = (name: typeof tab) => ({ trace: "/runs/$runId", info: "/runs/$runId/info", vms: "/runs/$runId/vms" } as const)[name]; const status = useQuery(runStatusQuery(runId!)); const trigger = useQuery({ ...triggerQuery(status.data?.trigger_id ?? ""), enabled: !!status.data?.trigger_id }); const triggerRuns = useQuery({ ...runPageQuery({ triggerId: status.data?.trigger_id ?? undefined, limit: 30 }), enabled: !!status.data?.trigger_id }); @@ -63,13 +64,13 @@ export function RunDetail() { {!status.data && } {stop.error &&

{stop.error.message}

} {!fullTrace && } {!fullTrace && } + {!fullTrace && tab === "vms" && runId &&
+ +
} {(fullTrace || tab === "trace") &&