Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions services/platform/backend/auth/org.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from 'hono';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { currentRunnerTenant } from '../../lib/engine/runners/tenant.ts';
import { trustedRoleHolds } from '../domains/trusted_headers/service.ts';
import { evaluateTwoFactorEnforcement } from '../domains/two_factor/service.ts';
import {
Expand Down Expand Up @@ -123,3 +124,32 @@ describe('a trusted-headers role on the session [THDR-R10]', () => {
expect(trustedRoleHolds).not.toHaveBeenCalled();
});
});

describe("a request's automation code", () => {
it("queues as its organization's in the runner", async () => {
vi.mocked(requireOrganizationMembership).mockResolvedValue({
member: {
id: 'member-a',
organizationId: 'org-a',
userId: 'user-a',
role: 'member',
},
organizationIds: ['org-a'],
});
vi.mocked(evaluateTwoFactorEnforcement).mockResolvedValue({
decision: 'allowed',
} as never);
const app = new Hono<OrgEnv>();
app.use(async (c, next) => {
c.set('sessionBundle', {
user: { id: 'user-a', email: 'a@example.invalid', name: 'A' },
session: { id: 'session-a' },
});
await next();
});
app.use(requireOrgMember({} as never));
app.get('/me', (c) => c.json({ tenant: currentRunnerTenant() }));
const response = await app.request('/me?orgId=org-a');
expect(await response.json()).toEqual({ tenant: 'org-a' });
});
});
5 changes: 4 additions & 1 deletion services/platform/backend/auth/org.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MiddlewareHandler } from 'hono';
import type { Sql } from 'postgres';

import { withRunnerTenant } from '../../lib/engine/runners/tenant.ts';
import {
defineAbilityFor,
type AppAction,
Expand Down Expand Up @@ -117,7 +118,9 @@ export function requireOrgMember<E extends OrgEnv>(
}
throw error;
}
return next();
// Automation code the request evaluates queues as its organization's,
// so the runner serves organizations in turn (`runners/tenant.ts`).
return withRunnerTenant(orgId, next);
};
}

Expand Down
5 changes: 5 additions & 0 deletions services/platform/backend/rest/v1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono, type Context, type Env } from 'hono';
import type { Sql } from 'postgres';

import { withRunnerTenant } from '../../lib/engine/runners/tenant.ts';
import {
isValidOrgSlug,
MAX_ORG_SLUG_LENGTH,
Expand Down Expand Up @@ -416,6 +417,10 @@ export function createRestV1Routes(deps: {
return next();
});

// Automation code a request evaluates queues as its organization's, so the
// runner serves organizations in turn (`runners/tenant.ts`).
app.use((c, next) => withRunnerTenant(c.get('organizationId'), next));

// Authorization stays with the member; every audit append made during
// this verified request instead names the key and its maker. A person's
// own key has no binding row: that verified user is also its maker.
Expand Down
2 changes: 1 addition & 1 deletion services/platform/tests/manual/reference/automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ Legend: ✅ fully automated · 🔶 partially automated · ⛔ manual-only (no s
| [automations](../suites/automations.md) | `AUTO-F59`, `AUTO-B14` and `AUTO-A6` backend halves: the engine side of the effect ledger, the per-run approval gate and the run's saved-progress format. A live connector write and a live model call each begin a ledger attempt before the call and record how it ended after; a resumed run reuses a finished call's output (sent once, its effect still logged), replays a recorded failure, parks `in_doubt:<node>` on the item and pass it was on for a write that may already have happened (a write inside a subautomation parks its calling node, under its nested path), makes a model call or an action declared `idempotent` again under the key its first attempt presented, and carries out a person's Run it again, Skip it (the step returns nothing) and Fail the run (`effect_in_doubt`, whatever `onError` says); a walker that lost its claim starts no write, and reads and mock runs never touch the ledger. A connector key gains the pass from the second repeat pass on and the nested path inside a subautomation, the same in the in-memory executor. Each run asks its own organization's gate when two organizations' runs are stepped at once, and an installed gate decides for every run until it is taken out. Saved progress the engine cannot read fails the run `engine_incompatible` without running a step, and a progress write answering anything but a live status ends the walk. A subautomation node fixes the versions of its whole chain the first time it runs and walks them after a later deploy; an explicit `name@v` stays as written. | ✅ unit | `backend/core/automations/stepper.ledger.test.ts`, `backend/core/automations/stepper.approval-gate.test.ts`, `backend/core/automations/stepper.progress.test.ts`, `backend/core/automations/stepper.subautomation-pins.test.ts`, `backend/core/automations/checkpoints.test.ts`, `lib/engine/core/execute/execute.test.ts`, `packages/shared/src/schemas/connectors.test.ts`; a worker stopped mid-write in a real stack, two organizations on one worker, and the card by keyboard, screen reader and zoom remain manual |
| [automations](../suites/automations.md) | `AUTO-F60` backend half: a stopping process hands its automation runs on instead of having them killed mid-step. Shutdown begins before anything waits; a walker hands its run on (lease released, the next step queued, the run counted as resumed after a shutdown) at its next step boundary or between two forEach items, before its first step when it was claimed during shutdown, and when its replica is drained for a deploy; a step still running at the grace (20 s, or a third of a shorter `SHUTDOWN_DRAIN_MS`) is cut — its model call and its connector request are aborted, a live connector body is raced and answers `INTERRUPTED` — and handed on as interrupted, never recorded failed or done, its ledger attempt left open so a write parks in doubt and a model call is made again; a long loop saves its place every ten items or ten seconds. The process then waits for its walkers, releases what it still holds, waits out its other jobs up to `SHUTDOWN_DRAIN_MS` (15 s api, 90 s worker) and closes its pools; a job fetched after shutdown began is handed over unstarted. An agent turn's drive window ends `running` on shutdown and schedules its next window without cutting the exec; drive jobs carry a 60 s heartbeat. A drain counts the automation runs the drained colour holds a live lease on and the drive windows that began before it, and the CLI names that breakdown. Both compose pipelines give `backend-api` 30 s and `backend-worker` 120 s to stop, and a colour's containers are stopped together. | 🔶 unit | `backend/lib/shutdown.test.ts`, `backend/lib/drain-probe.test.ts`, `backend/shutdown-sequence.test.ts`, `backend/core/automations/stepper.shutdown.test.ts`, `backend/core/automations/agent_host.shutdown.test.ts`, `backend/core/automations/llm_call.test.ts`, `lib/connectors/dispatcher.test.ts`, `backend/core/chat/external_turn_shared.test.ts`, `backend/domains/control/service.test.ts`, `backend/env.test.ts`, `backend/jobs/task-policies.test.ts`, `tools/cli/src/lib/compose/services/compose-parity.test.ts`, `tools/cli/src/lib/compose/generators/generate-color-compose.test.ts`, `tools/cli/src/lib/actions/color-lifecycle.test.ts`, `tools/cli/src/lib/actions/drain-backend.test.ts`; a `docker stop` of the worker stepping a real run, two workers and a deploy's drain remain manual |
| [automations](../suites/automations.md) | `AUTO-P3` backend half: automation steps run in one-job slots (`WORKER_CONCURRENCY` of them), so a long step holds no other run's step; every step job counts against its organization (its pg-boss group, derived from the payload in `addJobInTx` for every enqueue site), a worker passes `AUTOMATION_ORG_CONCURRENCY` as the queue's database-counted group limit (0 lifts it, no other queue has one), a drained worker's hand-over keeps the job's group, and the step's 30 s heartbeat stops a killed worker's step counting within about a minute and a half. A booting worker queues one liveness sweep at once, and a failed send never fails its boot. | 🔶 unit | `backend/jobs/runner.test.ts`, `backend/jobs/enqueue.test.ts`, `backend/jobs/task-policies.test.ts`, `backend/jobs/schedules.test.ts`, `backend/env.test.ts`; pg-boss's own group count across two real workers, and the burst in a browser, remain manual |
| [automations](../suites/automations.md) | The runner pool: automation code (templates the runner reads, transform bodies) runs in up to `AUTOMATION_RUNNER_PROCESSES` processes per backend process — one at first, another only while every process holds its pipeline (two evaluations), one beyond the first retired after five idle minutes; a kill or a crash fails only what its own process held; waiting evaluations queue per organization (each stepper turn runs as its run's organization) and are served in turn; the defaults per role. The template fast path (paths, literals and `??`/`||` fallbacks read without the runner, a seeded property holding it to the runner) and scope views (server suites hand out frozen copies, so a write to a scope throws). | ✅ unit | `lib/engine/runners/node-vm.pool.test.ts`, `backend/lib/code-runner.test.ts`, `lib/engine/core/template-fast.test.ts`, `lib/engine/core/execute/scope.test.ts`; the API's own requests still share one queue |
| [automations](../suites/automations.md) | The runner pool: automation code (templates the runner reads, transform bodies) runs in up to `AUTOMATION_RUNNER_PROCESSES` processes per backend process — one at first, another only while every process holds its pipeline (two evaluations), one beyond the first retired after five idle minutes; a kill or a crash fails only what its own process held; waiting evaluations queue per organization (each stepper turn as its run's organization, each API request as its own) and are served in turn; the defaults per role. The template fast path (paths, literals and `??`/`||` fallbacks read without the runner, a seeded property holding it to the runner) and scope views (server suites hand out frozen copies, so a write to a scope throws). | ✅ unit | `lib/engine/runners/node-vm.pool.test.ts`, `backend/lib/code-runner.test.ts`, `backend/auth/org.test.ts`, `lib/engine/core/template-fast.test.ts`, `lib/engine/core/execute/scope.test.ts` |
| [automations](../suites/automations.md) | `AUTO-F58`, `AUTO-F59` and `AUTO-F61` backend and component halves of what the review of run leases closed: a running run an image without leases left is turned by the sweep into a lapsed lease and taken over, never refused for good; a walker whose own lease lapsed begins no write; a write that ends after another walker parked the run on it wakes the run (and its park, its poll, both find it due), so the next walker reads that end instead of anyone being asked; a poll moves nothing on a run its park no longer holds; a decision lands only on the write the card shows, and only on the attempt of it the card showed: once Run it again sent the write again and that attempt was interrupted too, a late choice about the earlier attempt is refused (`IN_DOUBT_ALREADY_RESOLVED` 409) and writes nothing; an agent node's park keeps a result, a new exec or a later deadline written while its walker was on its way, and wakes the run for a result; inside a subautomation every live call goes through the ledger, so a walk after an interruption reads what the first one read and no item is skipped or sent twice; a turn claimed during shutdown hands a loop on before its next item; a walker cut and not settled 30 s later stops renewing its lease; two processes sharing a host name and pid get two lease owners; the `effect_in_doubt` sentence names no nested path; `tale deploy` keeps each backend role's stop grace 15 s above a longer `SHUTDOWN_DRAIN_MS`. In the app the canvas and the task's run timeline read a person's wait as `waiting` and a stalled run's step as `interrupted`, never spinning; the stalled badge is blue; the resumed line waits for a server; the in-doubt card is a labelled `Card` section with its body mounted while loading, is told apart from an earlier decision by attempt number, sends the attempt number with a choice, withdraws an open confirmation once the read answers another attempt or none (never confirming the newer one), and shows in the task panel for someone who can work the task. | 🔶 unit + component | `backend/domains/automations/store.liveness-sweep.test.ts`, `store.claim-lease.test.ts`, `store.continue-suspend.test.ts`, `node-attempts.test.ts`, `routes.in-doubt.test.ts`, `app/lib/backend/automations.test.ts`, `backend/core/automations/checkpoints.test.ts`, `stepper.ledger.test.ts`, `stepper.shutdown.test.ts`, `backend/lib/instance.test.ts`, `tools/cli/src/lib/compose/generators/generate-color-compose.test.ts`, `app/features/automations/lib/run-view.test.ts`, `app/features/automations/components/run-status-badge.test.tsx`, `run-detail.test.tsx`, `run-in-doubt-card.test.tsx`, `app/features/tasks/components/task-subject-panel.test.tsx`; the real-Postgres takeover of an image-without-leases row and the late-finish wake remain for `backend:integration`, where `backend/integration-check.ts` (`checkAutomationRunLifecycle` #4g3) runs the retry → re-park → late-choice refusal on real Postgres, and the interleavings with a real worker for the boxes |
| [automations](../suites/automations.md) | Each expanded sequential agent step reads its own transcript and recorded output; single-agent, non-agent and missing-transcript controls; node-scoped cache keys and HTTP selectors, durable execution identity and unchanged run/project authorization | ✅ component + unit + route | `app/features/automations/components/run-step-timeline.test.tsx`, `app/lib/backend/automations.test.ts`, `backend/domains/sandbox/sessions.agent-node-op.test.ts`, `backend/domains/sandbox/routes.test.ts`, `backend/core/automations/stepper.agent-retry.test.ts` |
| [automations](../suites/automations.md) | `AUTO-B8` transcript and run-bound connector-approval reads; `AUTO-B11` approval decisions: a hidden or missing run fails closed, a project reader can read but cannot decide, a writer can decide, and genuine non-run or organization approvals retain their existing policy | 🔶 route unit | `backend/domains/sandbox/routes.test.ts`, `backend/domains/approvals/routes.project-scope.test.ts`; the real two-session presentation and approval integration remain manual |
Expand Down