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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Check unwired pack foundation with offline durable ports
run: pnpm test:pack-foundation-workflow

- name: Check unwired pack admission wait and restart
run: pnpm test:pack-admission-workflow

- name: Check actual unwired pack foundation port compilation
run: |
PACK_FOUNDATION_PORTS_COMPILE_OUTPUT=./pack-foundation-ports-compile.json node scripts/pack-foundation-durable-ports-compile.mjs
Expand Down
23 changes: 23 additions & 0 deletions app/workflows/packs/foundation/readPackAdmissionStep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getWorkflowMetadata } from "workflow";
import { runPackCommand } from "@/lib/supabase/release_pack_jobs/runPackCommand";
import type { PackActor } from "@/lib/content/packs/packTypes";
import type { PackAdmissionObservation } from "@/lib/content/packs/foundation/packAdmissionTypes";
export async function readPackAdmissionStep(
actor: PackActor,
jobId: string,
): Promise<PackAdmissionObservation> {
"use step";
const { job } = await runPackCommand(actor, "read", jobId);
return {
workflowRunId: getWorkflowMetadata().workflowRunId,
observedAt: Date.now(),
job: {
id: job.id,
owner_id: job.owner_id,
status: job.status,
workflow_run_id: job.workflow_run_id,
hold_expires_at: job.hold_expires_at,
},
};
}
readPackAdmissionStep.maxRetries = 0;
7 changes: 7 additions & 0 deletions app/workflows/packs/foundation/startPackAdmissionStep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getWorkflowMetadata } from "workflow";
/** After this result is durably completed, replay preserves its candidate deadline. */
export async function startPackAdmissionStep() {
"use step";
return { workflowRunId: getWorkflowMetadata().workflowRunId, startedAt: Date.now() };
}
startPackAdmissionStep.maxRetries = 0;
10 changes: 10 additions & 0 deletions app/workflows/packs/foundation/waitForPackAdmission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { sleep } from "workflow";
import { pollPackAdmission } from "@/lib/content/packs/foundation/pollPackAdmission";
import type { PackActor } from "@/lib/content/packs/packTypes";
import { startPackAdmissionStep } from "./startPackAdmissionStep";
import { readPackAdmissionStep } from "./readPackAdmissionStep";
/** Unwired: no production caller, self-admission, dispatch, provider work or settlement. */
export async function waitForPackAdmission(actor: PackActor, jobId: string) {
const start = await startPackAdmissionStep();
return pollPackAdmission(actor, jobId, start, { read: readPackAdmissionStep, sleepUntil: sleep });
}
8 changes: 6 additions & 2 deletions lib/content/packs/foundation/ADMISSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ non-running jobs and expired holds must fail closed. A cached step result must n
be mistaken for a fresh read. Read reconciliation retains the database's existing
expired-hold behavior; no hold extension or funding action is implied.

Before selecting this candidate, define a finite wait deadline and the observable
owner continuation when dispatch persistence or its acknowledgement is lost. Prove
Production use still requires selecting timeout policy and the observable owner
continuation when dispatch persistence or its acknowledgement is lost. Required evidence covers
early execution, delayed persistence, restart during wait, wrong-run rejection,
expiry, read failures, timeout and no second dispatch/provider attempt. A wait solves
only the initial ordering gap; it does not solve cross-run takeover after admission.
Expand Down Expand Up @@ -66,3 +66,7 @@ synthetic effects, not a hosted scheduling guarantee. Negative observer tests re
outside-delivery reads, incomplete loads, unrelated failures, failed handlers and
combining events from concurrent deliveries. Keep the original failed CI evidence;
a passing deterministic fixture does not retroactively prove its complete event order.

The separately qualified [finite admission candidate](./ADMISSION_WAIT.md) implements
an unwired 30-second admission cutoff with durable fresh reads. It does not select
production timeout policy, change dispatch or solve atomic ownership.
126 changes: 126 additions & 0 deletions lib/content/packs/foundation/ADMISSION_WAIT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Unwired finite admission candidate

`waitForPackAdmission` is an unregistered composition helper. Nothing in production
calls it, and it does not call the foundation factory. The 30-second cutoff and
scheduled poll interval of at most one second are engineering qualification values,
not a selected production timeout or service-level guarantee.

`startPackAdmissionStep` journals the executing Workflow ID and a server clock once.
`pollPackAdmission` derives one absolute deadline from that saved result. Every poll
invokes `readPackAdmissionStep` separately, producing a distinct durable step result.
Completed reads replay from their own journal entries; the next poll is a new read,
not reuse of a cached initial snapshot. Absolute durable sleeps stop at the earlier
of the next poll time, original hold expiry and admission deadline. Once the start-step result is durably completed, restart cannot reset that saved
clock or extend its deadline. A crash before completion can execute the clock step
again; the fixture does not qualify that pre-journal window.

The read step calls only the existing owner/org-scoped read RPC. The loop tolerates
only an absent saved workflow ID on the same running, authorized job with a valid
future hold. It requires the current step's Workflow ID to remain the original ID.
A different saved ID, empty ID, wrong owner/job, non-running status, expired/invalid
hold or read/access failure returns `needs-reconciliation` without another poll.
Admission observed at or after the deadline returns reconciliation even if the ID
matches. A backward or invalid clock is also rejected. No step automatically retries.

Results retain the original job/run IDs, absolute deadline and first authorized,
valid hold expiry. If the first read fails before an expiry is known, that field is
null; no expiry is invented. A later extension cannot extend this candidate's original
hold boundary. No `run`, `approve`, provider, context write, settlement or dispatch
operation is available through the coordinator's ports. The existing database read
may reconcile an already-expired hold under its existing policy; this candidate does
not add a hold release, renewal or funding action.

The cutoff governs admission eligibility, not maximum wall-clock response time.
An in-flight RPC, stalled infrastructure or delayed Workflow delivery can return
later; cancellation and hosted scheduling have not been qualified. The next fresh
observation still cannot admit after the original cutoff. Reconciliation is an
explicit result, not permission to start another run or retry paid work.

## Qualification

Unit tests exercise exact deadline and hold boundaries, immediate/delayed/missing ID,
wrong identity/status, read failure and clock anomalies. Adapter tests use the actual
RPC wrapper with a mocked server client to verify exact account/org/job forwarding,
read-only calls and zero retry settings.

The separate `scripts/pack-admission-workflow-fixture` compiles the actual composition,
coordinator and durable steps, replacing only the read RPC with a typed synthetic
boundary. It uses a fixture-local adapter around the installed SDK, plus unchanged shared
failure-delivery, polling and strict-type helpers. Its local network guard extends
the unchanged shared guard to reject WebSocket construction and HTTP/2 connections. Scenarios cover immediate admission, delayed persistence across
process restart, deadline across restart, different ID, read/access failure and
expired hold, and expiry while waiting. The fixture retains the decoded original journaled start-clock result
and checks the exact deadline, distinct read-step correlations, one original start,
no other step/effect, unchanged hold and exact scheduled wake times against every
decoded read result. Pre-crash trace entries must remain an unchanged prefix; each
effect carries its durable step ID and must occur exactly once across restart and
terminal replay. Fresh polls already due at restart are allowed distinct IDs.
The delayed case simulates persistence of the original dispatch ID outside the
workflow; it does not perform self-admission or start a second workflow.

On an outcome assertion failure, `PACK_ADMISSION_ASSERTION_FAILURE` in the CI log
retains the synthetic read trace, decoded observations and journal event IDs,
correlations, timestamps and wait lifecycle before temporary files are removed.
The resumed worker also saves raw events, delivery counts and the pre-crash trace
before decoding or assertions, then adds decoded observations before validation.
On child failure, the parent emits `PACK_ADMISSION_SAVED_FAILURE` before cleanup;
missing capture is reported explicitly. This distinguishes repeated storage events
from distinct logical waits. No events
or wake timestamps are deduplicated; extra, early and late waits still fail.

This is local compiled replay with synthetic database reads. It does not establish
SQL atomicity, authenticated hosting, provider execution, production timeout policy,
or owner-facing continuation. The existing unconditional `run` write, reused preparation
write and point-in-time authorization still do not fence cross-run takeover. Production
wiring and those authority/continuation contracts remain separate prerequisites.


## Local SDK concurrency boundary and fixture recovery

The local fixture resumes its saved run through `world.start()` recovery exactly
once. It does not also enqueue that active run explicitly. The separate explicit
terminal replay remains mandatory. A passive observer records SDK queue message
IDs, attempts, per-handler AsyncLocalStorage context, event create requests/results,
list cursors and returned event IDs. The queue observer is installed before startup
through an isolated loader with an exact SDK source hash; installed files are not
changed. It returns original SDK promises/results and adds no locks, retries or
serialization. JSONL persistence is asynchronous and capped at20,000 entries/2MiB. Successful
qualification closes the SDK, drains late log writes, checks a fresh snapshot for
dropped entries/persistence errors, then writes the receipt. A last-append failure
cannot produce a success receipt; an earlier operation error remains primary.
The start phase likewise drains and checks its snapshot before writing the
pre-crash trace or ready receipt, without closing the SDK before intentional SIGKILL.
The drain returns a frozen snapshot only after all writes queued during the drain
have settled; later observations belong to a later checkpoint.

`probeRecoveryEnqueues.mjs` uses identical saved-run bytes and the actual SDK queue
with a bounded synthetic receiver. Startup-only and explicit-only each produce one
message; combining them produces two distinct messages. This probes enqueue
provenance, not generated workflow replay. `probeWaitCreation.mjs` separately gates
the actual installed filesystem writer after both wait-path absence checks, through
a byte-verified isolated loader seam. Serial duplicate creation conflicts; controlled
overlap can store distinct event IDs for the same correlation, including conflicting
wake times. The write logic and installed files remain unchanged. These deterministic
controls reproduce an SDK mechanism; they cannot retrospectively classify the earlier
CI failure whose log lacked event IDs. Removing a redundant fixture enqueue does not
establish general SDK concurrency fencing or production safety.

Run either bounded probe with `node --import
./scripts/pack-admission-workflow-fixture/installAdmissionObserver.mjs` followed by
its script path. Receipts distinguish scope, actual requests, returned IDs, raw
records, the seam and source hashes. The exact scheduled-wake and read-effect
oracles remain strict; repeated records are retained, and contradictory payloads
for one event ID fail. Distinct waits at the same timestamp are not deduplicated.

Failure capture now also covers parent timeout, signal and early child exit. The
parent requests a bounded live snapshot, drains incremental operation records and
reads raw journal/wait files before owned-child cleanup. It preserves the original
error and explicitly marks unavailable or truncated evidence. Snapshots from these
sources are not an atomic view of a live process. Tests force these paths and verify
retained evidence and child cleanup; they do not claim a hung event loop can answer
an IPC request.

Diagnostic file reads are capped before parsing:1MiB per checkpoint/operation trace,
64KiB per raw event/wait file and4MiB aggregate per raw directory. Truncation and
partial lines remain explicit evidence. These bounds supplement the one-second live
snapshot request; capture is best-effort, not a wall-clock guarantee for filesystem I/O.
60 changes: 60 additions & 0 deletions lib/content/packs/foundation/__tests__/packAdmissionSteps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { startPackAdmissionStep } from "@/app/workflows/packs/foundation/startPackAdmissionStep";
import { readPackAdmissionStep } from "@/app/workflows/packs/foundation/readPackAdmissionStep";
const boundary = vi.hoisted(() => ({ rpc: vi.fn(), runId: "executing-run" }));
vi.mock("@/lib/supabase/serverClient", () => ({ default: { rpc: boundary.rpc } }));
vi.mock("workflow", () => ({ getWorkflowMetadata: () => ({ workflowRunId: boundary.runId }) }));
describe("actual admission durable steps at mocked RPC boundary", () => {
beforeEach(() => {
vi.clearAllMocks();
boundary.runId = "executing-run";
});
it("journals a start clock and executing run without a database call", async () => {
const before = Date.now();
const value = await startPackAdmissionStep();
expect(value.workflowRunId).toBe("executing-run");
expect(value.startedAt).toBeGreaterThanOrEqual(before);
expect(value.startedAt).toBeLessThanOrEqual(Date.now());
expect(boundary.rpc).not.toHaveBeenCalled();
expect(startPackAdmissionStep.maxRetries).toBe(0);
});
it("forwards exact account/org/job authority through the existing read RPC", async () => {
const job = {
id: "job",
owner_id: "ORG",
status: "running",
workflow_run_id: null,
hold_expires_at: "2099-01-01T00:00:00Z",
unrelated: "not returned",
};
boundary.rpc.mockResolvedValue({ data: { job, assets: [] }, error: null });
const result = await readPackAdmissionStep(
{ accountId: "member", organizationId: "ORG" },
"job",
);
expect(result.job).toEqual({
id: "job",
owner_id: "ORG",
status: "running",
workflow_run_id: null,
hold_expires_at: job.hold_expires_at,
});
expect(result.workflowRunId).toBe("executing-run");
expect(Number.isFinite(result.observedAt)).toBe(true);
expect(boundary.rpc).toHaveBeenCalledExactlyOnceWith("release_pack_command", {
p_actor: "member",
p_org: "ORG",
p_action: "read",
p_job: "job",
p_input: {},
});
expect(readPackAdmissionStep.maxRetries).toBe(0);
});
it("propagates denied reads without admission writes or local retries", async () => {
boundary.rpc.mockResolvedValue({ data: null, error: { message: "access denied" } });
await expect(readPackAdmissionStep({ accountId: "member" }, "job")).rejects.toThrow(
"access denied",
);
expect(boundary.rpc).toHaveBeenCalledTimes(1);
});
});
Loading
Loading