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
16 changes: 12 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ ANTHROPIC_API_KEY=
# harness produced the run.
OPENAI_API_KEY=

# A managed Outpost project on the Hookdeck platform, for the Outpost scenarios
# (BM12, BM13). Separate from HOOKDECK_API_KEY: Outpost has its own API, and the
# Event Gateway scenarios do not touch it. Scenarios that need it are skipped
# when it is unset, so leaving it blank costs nothing.
# A project key for a dedicated Outpost project on the Hookdeck platform, for
# the Outpost scenarios. Separate from HOOKDECK_API_KEY: Outpost has its own API.
# Scenarios that need it are skipped when it is unset, so leaving it blank costs
# nothing.
#
# Use a project that holds nothing you want to keep. When this is set, every
# run of every scenario, Event Gateway ones included, deletes all Event Gateway
# resources in this project (sources, destinations, connections, transformations
# and issue triggers) before the seed and again on release, including resources
# that were there before the run. Runs also delete the tenants they create, and
# on release put the project's operator event destinations and managed config
# back as they found them.
OUTPOST_API_KEY=

# Vercel AI Gateway. The opencode harness routes through this. Not needed until
Expand Down
35 changes: 28 additions & 7 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1144,13 +1144,27 @@ collide. Do not try to serialise via a GitHub Actions
concurrency group: a group holds one running and one pending job and cancels the
rest. Shard instead.

**Outpost state is not reset between runs.** `FixedProjectSource` snapshots and
restores the Hookdeck project, and knows nothing about Outpost. Tenants and
destinations an Outpost run creates survive into the next one, so the second run
of a scenario finds `acme` already there and may score a previous run's work.
`FixedProjectSource` now deletes tenants that were not present when it acquired
the lease, on release — but that runs in a `catch`-and-ignore, so a run that dies
before release still leaks and the next run inherits it.
**Outpost tenants are only reset on release.** `FixedProjectSource` snapshots
and restores the Hookdeck project, and Outpost has no snapshot to restore from.
Tenants and destinations an Outpost run creates survive into the next one, so
the second run of a scenario finds `acme` already there and may score a previous
run's work. `FixedProjectSource` now deletes tenants that were not present when
it acquired the lease, on release — but that runs in a `catch`-and-ignore, so a
run that dies before release still leaks and the next run inherits it.

**The Outpost project has an Event Gateway side, and agents write to it.**
`OUTPOST_API_KEY` is an ordinary project key, so it works on `api.hookdeck.com`
and the CLI too. A source, two connections, two destinations and two issue
triggers were left there on 24 and 25 August and nothing removed them. On 5
November an `outpost-005` attempt added a body `$nin` filter to one of those
connections instead of changing the Outpost destination's topics. The filter
carried into the cell's next two attempts, because nothing reset that side.
`FixedProjectSource` now deletes every gateway resource in the Outpost project
on every acquire, for every scenario, before the seed, and again on release. It
refuses if the Outpost key can see anything in the gateway project's snapshot,
because that means both keys reach one project. So `OUTPOST_API_KEY` must
belong to a dedicated project, and a gateway API error on that key now fails
the acquire of every cell, Event Gateway scenarios included.

So cleanup on release is not enough on its own, and `applyOutpostSeed` deletes
each tenant it is about to create. That makes seeding idempotent without
Expand Down Expand Up @@ -1258,6 +1272,13 @@ queue URL and leaves `credentials` empty.
default issue triggers. The first acquire snapshots what the project contains,
and every reset deletes only what a run added.

The Outpost project's gateway side is reset to empty, and that follows the same
rule: pristine there *is* empty. Platform project creation gives an Outpost project no issue
triggers, and no seed creates gateway resources in it. So that baseline is
declared in code (`OUTPOST_GATEWAY_PRISTINE`), not captured. A captured one
would come from whatever the last CI job left, because every job is a fresh
checkout, and it would have recorded the August leftovers as pristine.

**Never sleep for ingestion, poll for it.** Every scorer that sends something
and reads it back used a fixed `setTimeout`, 8 or 12 seconds. Ingestion is
asynchronous, so when the platform is slower than the sleep the scorer reads too
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Every run follows the same shape:
Runs need a Docker daemon, and `HOOKDECK_API_KEY` for the project scoring runs
against. See `.env.example`.

**Reset is to pristine, not to empty.** A new Hookdeck project ships with four default
**Reset is to pristine, not to empty.** A new Hookdeck project ships with default
issue triggers. Deleting them would leave the project unlike any real customer's. The
first acquire snapshots what the project contains and every reset deletes only what a
run added.
Expand All @@ -245,9 +245,16 @@ is deliberately separate from `ctx.api`: Outpost inverts the gateway's model, in
tenant is your customer, the tenant owns destinations, and events are published to
topics rather than routed from sources.

**Outpost state is not reset between runs yet.** The provisioner restores the Hookdeck
project and does not touch Outpost, so tenants an Outpost run creates persist. See
`AGENTS.md`.
**`OUTPOST_API_KEY` must belong to a dedicated Outpost project, because every run
wipes its Event Gateway side.** The key is an ordinary project key, so it also works on
`api.hookdeck.com` and agents can create sources and connections there with it. When
the key is set, every run of every scenario, not only the Outpost ones, deletes all
Event Gateway resources in that project before the seed and again on release, including
ones that existed before the run.

**Outpost's own state is only partly reset.** A run deletes the tenants it created and
restores operator event destinations and managed config on release. A run that dies
before release can still leave a tenant behind. See `AGENTS.md`.

## Skills

Expand Down
154 changes: 145 additions & 9 deletions packages/hookdeck/src/project-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,46 @@ export interface PristineSnapshot {
ids: Record<string, string[]>;
}

/**
* What the Event Gateway side of the Outpost project contains when nothing has
* touched it: nothing.
*
* `OUTPOST_API_KEY` is an ordinary project API key, so it authenticates
* `api.hookdeck.com` and the CLI as well as the Outpost subdomain, and an agent
* can create sources and connections in the Outpost project with it. Agents
* did: on 24 and 25 August a source, two destinations, two connections and two
* issue triggers appeared there — one destination on `outpost-003`'s alert URL,
* the other on a `mock.hookdeck.com` path of the kind `outpost-001` accepted
* before its endpoint was fixed — and nothing reset them; three of them were
* updated again in September. On 5 November `outpost-005` found one of those
* connections, added a topic filter to it instead of changing the Outpost
* destination's topics, and the filter survived from attempt 1 into attempts 2
* and 3 of the same cell.
*
* **Declared rather than captured.** The gateway project is reset to a snapshot
* taken on first acquire, but in CI every job is a fresh checkout, so a captured
* snapshot is taken from whatever the last job left — here it would have
* recorded those leftovers as pristine and protected them. The tenant cleanup
* below gives the same reason for not persisting a tenant baseline. A declared
* baseline cannot be poisoned by a run that died.
*
* **Empty, unlike the gateway project, and that is not a contradiction of
* "pristine, not empty".** A new Event Gateway project ships with default issue
* triggers; an Outpost project ships with none, because the platform's project
* creation sets up an Outpost project without the gateway defaults (read in the
* Event Gateway implementation, not inferred from the API). Consistent with
* that, the two issue triggers found here were created within a minute of
* the leftover source and connection. No Outpost seed creates gateway
* resources either. So for this
* project, pristine *is* empty. If a scenario ever needs a gateway resource in
* the Outpost project, its seed creates it, and the reset before the next seed
* removes it.
*/
export const OUTPOST_GATEWAY_PRISTINE: PristineSnapshot = {
capturedAt: 'declared',
ids: {},
};

export interface FixedProjectSourceOptions {
apiKey: string;
projectId?: string;
Expand All @@ -85,6 +125,7 @@ export class FixedProjectSource implements ProjectSource {
private readonly outpostConfigPath: string;
private cachedClient?: HookdeckClient;
private cachedOutpostClient?: OutpostClient;
private cachedOutpostGatewayClient?: HookdeckClient;
private outpostTenantsAtAcquire?: Set<string>;
private outpostConfigAtAcquire?: Record<string, unknown>;
private operatorEventDestinationsAtAcquire?: OperatorEventDestination[];
Expand Down Expand Up @@ -143,9 +184,35 @@ export class FixedProjectSource implements ProjectSource {
return this.cachedOutpostClient;
}

/**
* The Event Gateway API, authenticated as the Outpost project.
*
* Same host as `client`, different key, so a different project. Undefined
* when Outpost is off, and when the two keys are the same string — that is
* one project, already reset by `client`, and resetting it again to the
* Outpost baseline would delete the gateway project's default issue triggers.
*/
private get outpostGatewayClient(): HookdeckClient | undefined {
const apiKey = this.options.outpostApiKey ?? process.env.OUTPOST_API_KEY;
if (!apiKey || apiKey === this.options.apiKey) return undefined;
if (!this.cachedOutpostGatewayClient) {
this.cachedOutpostGatewayClient = new HookdeckClient({
apiKey,
baseUrl: this.options.baseUrl,
});
}
return this.cachedOutpostGatewayClient;
}

async acquire(): Promise<LeasedProject> {
const snapshot = await this.loadOrCaptureSnapshot();
await this.resetToPristine(snapshot);
await this.resetToPristine(this.client, snapshot);
// Before the seed, not only on release: a run that dies before release
// must not hand its gateway resources in the Outpost project to the next
// one, and in CI the next one is usually a different job. Throws, like the
// reset above, because a scenario started on top of a leftover produces a
// result that looks valid and is not, and nothing has been paid for yet.
await this.resetOutpostGateway(snapshot);
this.outpostTenantsAtAcquire = await this.listOutpostTenants();
this.outpostConfigAtAcquire = await this.loadOrCaptureOutpostConfig();
this.operatorEventDestinationsAtAcquire =
Expand All @@ -165,13 +232,24 @@ export class FixedProjectSource implements ProjectSource {
* rather than from a per-run timestamp.
*/
async release(_project: LeasedProject): Promise<void> {
let snapshot: PristineSnapshot | undefined;
try {
const snapshot = await this.loadOrCaptureSnapshot();
await this.resetToPristine(snapshot);
snapshot = await this.loadOrCaptureSnapshot();
await this.resetToPristine(this.client, snapshot);
} catch {
// Swallowed deliberately: a failed release must not fail the run, and
// the next acquire will clean up regardless.
}
if (snapshot) {
try {
await this.resetOutpostGateway(snapshot);
} catch (error) {
// Same reasoning, and the next acquire resets this side too.
console.warn(
`outpost project gateway reset: release did not complete (${(error as Error).message})`
);
}
}
await this.releaseOutpostTenants();
await this.restoreOperatorEventDestinations();
await this.restoreOutpostConfig();
Expand Down Expand Up @@ -502,20 +580,78 @@ export class FixedProjectSource implements ProjectSource {
}
}

/** Delete everything the snapshot does not list, in dependency order. */
private async resetToPristine(snapshot: PristineSnapshot): Promise<void> {
/**
* Delete everything the snapshot does not list, in dependency order.
*
* `onDelete` is called after each deletion rather than once at the end, so a
* reset that fails partway still leaves a record of what it had already
* removed.
*/
private async resetToPristine(
client: HookdeckClient,
snapshot: PristineSnapshot,
onDelete?: (kind: ResourceKind, resource: HookdeckResource) => void
): Promise<void> {
// Connections reference sources and destinations, so they go first.
for (const kind of RESOURCE_KINDS) {
const keep = new Set(snapshot.ids[kind] ?? []);
const existing: HookdeckResource[] = await this.client.list(
kind as ResourceKind
);
const existing: HookdeckResource[] = await client.list(kind);
for (const resource of existing) {
if (keep.has(resource.id)) continue;
await this.client.delete(kind as ResourceKind, resource.id);
await client.delete(kind, resource.id);
onDelete?.(kind, resource);
}
}
}

/**
* Reset the Event Gateway side of the Outpost project to
* `OUTPOST_GATEWAY_PRISTINE`, which is empty.
*
* Refuses when the Outpost key turns out to reach the gateway project after
* all — two different keys for one project, say. The baseline here is "delete
* everything", so pointing it at the gateway project would delete its default
* issue triggers. The test is whether anything this key can see is in the
* gateway project's own snapshot: two projects never share a resource id.
*
* Logs each deletion as it happens. Release normally leaves nothing for
* acquire to find, so a deletion at acquire means a run died before releasing
* or something outside the harness wrote to the project — both worth seeing
* in a log.
*/
private async resetOutpostGateway(
gatewaySnapshot: PristineSnapshot
): Promise<void> {
const client = this.outpostGatewayClient;
if (!client) return;

const gatewayIds = new Set(Object.values(gatewaySnapshot.ids).flat());
for (const kind of RESOURCE_KINDS) {
const shared = (await client.list(kind)).filter((r) =>
gatewayIds.has(r.id)
);
if (shared.length > 0) {
console.warn(
`outpost project gateway reset: skipped, because OUTPOST_API_KEY can see ${kind} ` +
`${shared.map((r) => r.id).join(', ')} from the gateway project's pristine ` +
'snapshot. The two keys appear to belong to one project, and resetting it ' +
"to the Outpost baseline would delete that project's defaults."
);
return;
}
}

await this.resetToPristine(
client,
OUTPOST_GATEWAY_PRISTINE,
(kind, resource) => {
console.warn(
`outpost project gateway reset: deleted ${kind} ${resource.id}` +
(resource.name ? ` (${resource.name})` : '')
);
}
);
}
}

export interface OperatorEventDestination {
Expand Down
Loading
Loading