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
26 changes: 26 additions & 0 deletions docs/gen-2416-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# GEN-2416 migration and rollout

Migration `0019_target_configuration_separation.sql` is additive and safe to
rerun. It adds target-owned model, effort, and discovery-cache columns,
preserves legacy fields for compatibility and creates a tenant-scoped
checkpoint table. Empty tags from `0017` are ambiguous: they are retained
unless a reconciliation batch can establish their provenance. Encrypted
connection payloads are not inspected by SQL; an operator report must reconcile
account tag inventory without exposing credentials. Jobs remain the authority
for legacy model/effort until the compatibility reader is removed.

Roll out the additive schema, then deploy readers that prefer target snapshots,
then writers/UI, and finally run bounded tenant batches. Each batch inventories
connections, targets, jobs, and runs; splits targets by the tuple
`(integration, agent, model, effort, tags)` with deterministic UUIDs derived from
tenant, original target, and tuple; and records migrated/reused/split/ambiguous
counts in the checkpoint report. Queued and active runs are never relaunched:
their existing `runs.execution_target` is frozen before the scheduler switches
precedence. Historical rows are read-only.

If a batch is interrupted, rerun it from its checkpoint; deterministic IDs and
upserts prevent duplicates. Reports contain IDs and configuration conflicts but
never credentials. Rollback is limited to application readers/writers before
new-model writes; after target-owned writes, roll forward or restore from a
database backup while retaining snapshots and execution handles. Do not delete
legacy connection fields until all supported REST/MCP clients have migrated.
58 changes: 35 additions & 23 deletions packages/api-client/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2797,14 +2797,10 @@ export interface operations {
content: {
"application/json": {
connectionId: string;
/** @enum {string} */
agentKind: "codex" | "claude" | "pi";
/** @default [] */
tags: string[];
/** @constant */
credentialStatus: "connected";
/** @default [] */
models: string[];
/** @default */
modelsRefreshedAt: string;
accountTags: string[];
}[];
};
};
Expand Down Expand Up @@ -2877,6 +2873,10 @@ export interface operations {
agentKind: string;
integrationId?: string;
tags?: string[];
/** @default */
model: string;
/** @default */
effort: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
Expand Down Expand Up @@ -2911,6 +2911,12 @@ export interface operations {
/** @default [] */
tags?: string[];
agentKind: string;
/** @default */
model?: string;
/** @default */
effort?: string;
/** Format: date-time */
expectedUpdatedAt?: string;
};
};
};
Expand All @@ -2931,6 +2937,10 @@ export interface operations {
agentKind: string;
integrationId?: string;
tags?: string[];
/** @default */
model: string;
/** @default */
effort: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
Expand Down Expand Up @@ -2968,6 +2978,12 @@ export interface operations {
/** @default [] */
tags?: string[];
agentKind: string;
/** @default */
model?: string;
/** @default */
effort?: string;
/** Format: date-time */
expectedUpdatedAt?: string;
};
};
};
Expand All @@ -2988,6 +3004,10 @@ export interface operations {
agentKind: string;
integrationId?: string;
tags?: string[];
/** @default */
model: string;
/** @default */
effort: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
Expand Down Expand Up @@ -3269,25 +3289,17 @@ export interface operations {
} | null;
exe: {
connectionId: string;
/** @enum {string} */
agentKind: "codex" | "claude" | "pi";
/** @default [] */
tags: string[];
/** @constant */
credentialStatus: "connected";
/** @default [] */
models: string[];
/** @default */
modelsRefreshedAt: string;
accountTags: string[];
} | null;
exeConnections: {
connectionId: string;
/** @enum {string} */
agentKind: "codex" | "claude" | "pi";
/** @default [] */
tags: string[];
/** @constant */
credentialStatus: "connected";
/** @default [] */
models: string[];
/** @default */
modelsRefreshedAt: string;
accountTags: string[];
}[];
ampConnections: {
connectionId: string;
Expand Down Expand Up @@ -3334,7 +3346,7 @@ export interface operations {
connectionId?: string;
apiToken: string;
/** @enum {string} */
agentKind: "codex" | "claude" | "pi";
agentKind?: "codex" | "claude" | "pi";
tags?: string[];
};
};
Expand All @@ -3349,7 +3361,7 @@ export interface operations {
"application/json": {
ok: boolean;
connectionId: string;
models: string[];
models?: string[];
};
};
};
Expand Down
61 changes: 61 additions & 0 deletions packages/api/migrations/0019_target_configuration_separation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- GEN-2416: make execution targets the sole owner of executable configuration.
-- Additive and restart-safe: legacy connection fields remain readable until the
-- compatibility window is closed.
ALTER TABLE app.execution_targets
ADD COLUMN IF NOT EXISTS model text NOT NULL DEFAULT '',
ADD COLUMN IF NOT EXISTS effort text NOT NULL DEFAULT '',
ADD COLUMN IF NOT EXISTS models jsonb NOT NULL DEFAULT '[]'::jsonb,
ADD COLUMN IF NOT EXISTS models_refreshed_at timestamptz;

-- Legacy jobs own model/effort. Copy them into the target only for an
-- unambiguous one-job target; the compatibility reader preserves differing
-- job values until the split backfill below can report them.
UPDATE app.execution_targets t
SET model=COALESCE(NULLIF(j.model,''),''), effort=COALESCE(NULLIF(j.effort,''),'')
FROM app.jobs j
WHERE j.tenant_id=t.tenant_id AND j.execution_target->>'targetId'=t.id::text
AND NOT EXISTS (SELECT 1 FROM app.jobs other WHERE other.tenant_id=j.tenant_id
AND other.execution_target->>'targetId'=t.id::text AND (other.model,j.effort)<>(j.model,j.effort));

CREATE TABLE IF NOT EXISTS app.gen_2416_migration_checkpoint (
tenant_id uuid PRIMARY KEY REFERENCES app.tenants(id) ON DELETE CASCADE,
phase text NOT NULL DEFAULT 'inventory',
report jsonb NOT NULL DEFAULT '{}'::jsonb,
updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS app.gen_2416_migration_inventory (
tenant_id uuid PRIMARY KEY REFERENCES app.tenants(id) ON DELETE CASCADE,
connections bigint NOT NULL, targets bigint NOT NULL, jobs bigint NOT NULL,
runs bigint NOT NULL, ambiguous_targets bigint NOT NULL DEFAULT 0,
split_targets bigint NOT NULL DEFAULT 0, updated_at timestamptz NOT NULL DEFAULT now()
);

-- Split shared legacy targets where jobs carried different model/effort values.
-- The UUID and name are deterministic, so interrupted migrations are safe to
-- rerun and unrelated tenant targets cannot collide.
DO $migration$
DECLARE r record; new_id uuid;
BEGIN
FOR r IN SELECT j.tenant_id, j.id job_id, t.id target_id, j.model, j.effort
FROM app.jobs j JOIN app.execution_targets t ON t.tenant_id=j.tenant_id
AND t.id::text=j.execution_target->>'targetId'
WHERE (j.model,j.effort)<>(t.model,t.effort)
LOOP
new_id := md5(r.tenant_id::text || ':gen-2416:' || r.target_id::text || ':' || r.model || ':' || r.effort)::uuid;
INSERT INTO app.execution_targets(tenant_id,id,name,integration_kind,integration_id,tags,agent_kind,model,effort,models,models_refreshed_at)
SELECT tenant_id,new_id,left(name || ' · ' || substr(md5(r.model || ':' || r.effort),1,8),120),integration_kind,integration_id,tags,agent_kind,r.model,r.effort,'[]'::jsonb,NULL
FROM app.execution_targets WHERE tenant_id=r.tenant_id AND id=r.target_id
ON CONFLICT (tenant_id,id) DO NOTHING;
UPDATE app.jobs SET execution_target = execution_target || jsonb_build_object('targetId',new_id), updated_at=updated_at
WHERE tenant_id=r.tenant_id AND id=r.job_id;
END LOOP;
INSERT INTO app.gen_2416_migration_inventory(tenant_id,connections,targets,jobs,runs,ambiguous_targets,split_targets)
SELECT t.id, (SELECT count(*) FROM app.connections c WHERE c.tenant_id=t.id),
(SELECT count(*) FROM app.execution_targets e WHERE e.tenant_id=t.id),
(SELECT count(*) FROM app.jobs j WHERE j.tenant_id=t.id),
(SELECT count(*) FROM app.runs x WHERE x.tenant_id=t.id),
(SELECT count(*) FROM app.execution_targets e WHERE e.tenant_id=t.id AND e.integration_kind='exe' AND e.tags='[]'::jsonb),
(SELECT count(*) FROM app.execution_targets e WHERE e.tenant_id=t.id AND e.name ~ ' · [0-9a-f]{8}$')
FROM app.tenants t ON CONFLICT (tenant_id) DO UPDATE SET connections=excluded.connections,targets=excluded.targets,jobs=excluded.jobs,runs=excluded.runs,ambiguous_targets=excluded.ambiguous_targets,updated_at=now();
END $migration$;
4 changes: 2 additions & 2 deletions packages/api/src/api-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface RouteContract {
const exeBody = z.object({
connectionId: z.string().min(1).optional(),
apiToken: z.string().min(1).meta({ writeOnly: true }),
agentKind: z.enum(["codex", "claude", "pi"]),
agentKind: z.enum(["codex", "claude", "pi"]).optional(),
tags: z
.array(z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/))
.max(20)
Expand Down Expand Up @@ -74,7 +74,7 @@ const tailBody = z.object({
.meta({ writeOnly: true }),
generateSecret: z.boolean().optional(),
});
const executionTargetBody = z.object({ name: z.string().trim().min(1).max(120), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string().min(1), tags: z.array(z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/)).max(20).default([]), agentKind: z.string().min(1).max(40) });
const executionTargetBody = z.object({ name: z.string().trim().min(1).max(120), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string().min(1), tags: z.array(z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/)).max(20).default([]), agentKind: z.string().min(1).max(40), model: z.string().max(200).default(""), effort: z.string().max(40).default(""), expectedUpdatedAt: z.string().datetime().optional() });
const executionTargetUpdateBody = executionTargetBody;
const deliveryQuery = z.object({
provider: z.string().optional(),
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/editor-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from "zod";
export const executionTargetResponse = z.object({
id: z.string(), kind: z.enum(["exe-vm", "amp"]), name: z.string(), workspace: z.string(), cwd: z.string(), agentKind: z.string(), integrationId: z.string().optional(), tags: z.array(z.string()).optional(),
models: z.array(z.string()).optional(), modelsRefreshedAt: z.string().nullable().optional(), efforts: z.array(z.string()).optional(), capabilities: z.array(z.string()),
model: z.string().default(""), effort: z.string().default(""), models: z.array(z.string()).optional(), modelsRefreshedAt: z.string().nullable().optional(), efforts: z.array(z.string()).optional(), capabilities: z.array(z.string()),
});
export const executionTargetResource = z.object({ id: z.string(), name: z.string(), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string(), tags: z.array(z.string()), agentKind: z.string(), createdAt: z.string(), updatedAt: z.string() });
export const executionTargetResource = z.object({ id: z.string(), name: z.string(), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string(), tags: z.array(z.string()), agentKind: z.string(), model: z.string(), effort: z.string(), createdAt: z.string(), updatedAt: z.string() });
export const triggerAvailabilityResponse = z.object({ manual: z.literal(true), schedule: z.literal(true), jobLifecycle: z.literal(true), linear: z.boolean(), clickup: z.boolean(), github: z.boolean(), cloudflareTail: z.boolean() });
export const namedOption = z.object({ id: z.string(), name: z.string() });
export const providerOptions = z.object({ statuses: z.array(namedOption), users: z.array(namedOption), labels: z.array(namedOption) });
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/exe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { AgentKind } from "./agent-driver";

export interface ExeConnection {
apiToken: string;
tags: string[];
agentKind: AgentKind;
/** Legacy fields are read only during the migration window. Targets are authoritative. */
tags?: string[];
agentKind?: AgentKind;
models?: string[];
modelsRefreshedAt?: string;
}
Expand Down
Loading
Loading