Skip to content

Commit fd798ec

Browse files
engineerCopilot
andcommitted
feat(workflow): engine-core port from anomalyco#32390
Port core workflow runtime pieces into fork without rebasing onto upstream. Keep local session and shell behavior where needed, but add workflow engine, turn-budget accounting, workflow schema/storage, plugin workflow contract, and subagent directory support needed for isolated workflow agent steps. Also carry supporting core package schema/config additions required by the workflow run table and runtime wiring. Co-authored-by: Copilot <[email protected]>
1 parent b981eec commit fd798ec

29 files changed

Lines changed: 16901 additions & 908 deletions
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { DatabaseMigration } from "./migration"
2+
3+
export const migrations = (
4+
await Promise.all([
5+
import("./migration/20260127222353_familiar_lady_ursula"),
6+
import("./migration/20260211171708_add_project_commands"),
7+
import("./migration/20260213144116_wakeful_the_professor"),
8+
import("./migration/20260225215848_workspace"),
9+
import("./migration/20260227213759_add_session_workspace_id"),
10+
import("./migration/20260228203230_blue_harpoon"),
11+
import("./migration/20260303231226_add_workspace_fields"),
12+
import("./migration/20260309230000_move_org_to_state"),
13+
import("./migration/20260312043431_session_message_cursor"),
14+
import("./migration/20260323234822_events"),
15+
import("./migration/20260410174513_workspace-name"),
16+
import("./migration/20260413175956_chief_energizer"),
17+
import("./migration/20260423070820_add_icon_url_override"),
18+
import("./migration/20260427172553_slow_nightmare"),
19+
import("./migration/20260428004200_add_session_path"),
20+
import("./migration/20260501142318_next_venus"),
21+
import("./migration/20260504145000_add_sync_owner"),
22+
import("./migration/20260507164347_add_workspace_time"),
23+
import("./migration/20260510033149_session_usage"),
24+
import("./migration/20260511000411_data_migration_state"),
25+
import("./migration/20260511173437_session-metadata"),
26+
import("./migration/20260601010001_normalize_storage_paths"),
27+
import("./migration/20260601202201_amazing_prowler"),
28+
import("./migration/20260602002951_lowly_union_jack"),
29+
import("./migration/20260602182828_add_project_directories"),
30+
import("./migration/20260603001617_session_message_projection_indexes"),
31+
import("./migration/20260603040000_session_message_projection_order"),
32+
import("./migration/20260603141458_session_input_inbox"),
33+
import("./migration/20260603160727_jittery_ezekiel_stane"),
34+
import("./migration/20260604172448_event_sourced_session_input"),
35+
import("./migration/20260605003541_add_session_context_snapshot"),
36+
import("./migration/20260605042240_add_context_epoch_agent"),
37+
import("./migration/20260611035744_credential"),
38+
import("./migration/20260611192811_lush_chimera"),
39+
import("./migration/20260612174303_project_dir_strategy"),
40+
import("./migration/20260615050011_real_rachel_grey"),
41+
])
42+
).map((module) => module.default) satisfies DatabaseMigration.Migration[]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Effect } from "effect"
2+
import type { DatabaseMigration } from "../migration"
3+
4+
export default {
5+
id: "20260615050011_real_rachel_grey",
6+
up(tx) {
7+
return Effect.gen(function* () {
8+
yield* tx.run(`
9+
CREATE TABLE \`workflow_run\` (
10+
\`id\` text PRIMARY KEY,
11+
\`session_id\` text,
12+
\`directory\` text DEFAULT '' NOT NULL,
13+
\`workflow\` text NOT NULL,
14+
\`status\` text NOT NULL,
15+
\`started_at\` integer NOT NULL,
16+
\`completed_at\` integer,
17+
\`current_phase\` text,
18+
\`args\` text,
19+
\`definition\` text,
20+
\`logs\` text NOT NULL,
21+
\`agents\` text NOT NULL,
22+
\`result\` text,
23+
\`error\` text,
24+
\`resume_of\` text,
25+
\`pending_question\` text,
26+
\`time_created\` integer NOT NULL,
27+
\`time_updated\` integer NOT NULL
28+
);
29+
`)
30+
yield* tx.run(`CREATE INDEX \`workflow_run_started_at_idx\` ON \`workflow_run\` (\`started_at\`);`)
31+
yield* tx.run(`CREATE INDEX \`workflow_run_status_started_at_idx\` ON \`workflow_run\` (\`status\`,\`started_at\`);`)
32+
})
33+
},
34+
} satisfies DatabaseMigration.Migration
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
import { Effect } from "effect"
2+
import type { DatabaseMigration } from "./migration"
3+
4+
export default {
5+
up(tx) {
6+
return Effect.gen(function* () {
7+
yield* tx.run(`
8+
CREATE TABLE \`workspace\` (
9+
\`id\` text PRIMARY KEY,
10+
\`type\` text NOT NULL,
11+
\`name\` text DEFAULT '' NOT NULL,
12+
\`branch\` text,
13+
\`directory\` text,
14+
\`extra\` text,
15+
\`project_id\` text NOT NULL,
16+
\`time_used\` integer NOT NULL,
17+
CONSTRAINT \`fk_workspace_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
18+
);
19+
`)
20+
yield* tx.run(`
21+
CREATE TABLE \`data_migration\` (
22+
\`name\` text PRIMARY KEY,
23+
\`time_completed\` integer NOT NULL
24+
);
25+
`)
26+
yield* tx.run(`
27+
CREATE TABLE \`account_state\` (
28+
\`id\` integer PRIMARY KEY,
29+
\`active_account_id\` text,
30+
\`active_org_id\` text,
31+
CONSTRAINT \`fk_account_state_active_account_id_account_id_fk\` FOREIGN KEY (\`active_account_id\`) REFERENCES \`account\`(\`id\`) ON DELETE SET NULL
32+
);
33+
`)
34+
yield* tx.run(`
35+
CREATE TABLE \`account\` (
36+
\`id\` text PRIMARY KEY,
37+
\`email\` text NOT NULL,
38+
\`url\` text NOT NULL,
39+
\`access_token\` text NOT NULL,
40+
\`refresh_token\` text NOT NULL,
41+
\`token_expiry\` integer,
42+
\`time_created\` integer NOT NULL,
43+
\`time_updated\` integer NOT NULL
44+
);
45+
`)
46+
yield* tx.run(`
47+
CREATE TABLE \`control_account\` (
48+
\`email\` text NOT NULL,
49+
\`url\` text NOT NULL,
50+
\`access_token\` text NOT NULL,
51+
\`refresh_token\` text NOT NULL,
52+
\`token_expiry\` integer,
53+
\`active\` integer NOT NULL,
54+
\`time_created\` integer NOT NULL,
55+
\`time_updated\` integer NOT NULL,
56+
CONSTRAINT \`control_account_pk\` PRIMARY KEY(\`email\`, \`url\`)
57+
);
58+
`)
59+
yield* tx.run(`
60+
CREATE TABLE \`credential\` (
61+
\`id\` text PRIMARY KEY,
62+
\`integration_id\` text,
63+
\`label\` text NOT NULL,
64+
\`value\` text NOT NULL,
65+
\`connector_id\` text,
66+
\`method_id\` text,
67+
\`active\` integer,
68+
\`time_created\` integer NOT NULL,
69+
\`time_updated\` integer NOT NULL
70+
);
71+
`)
72+
yield* tx.run(`
73+
CREATE TABLE \`event_sequence\` (
74+
\`aggregate_id\` text PRIMARY KEY,
75+
\`seq\` integer NOT NULL,
76+
\`owner_id\` text
77+
);
78+
`)
79+
yield* tx.run(`
80+
CREATE TABLE \`event\` (
81+
\`id\` text PRIMARY KEY,
82+
\`aggregate_id\` text NOT NULL,
83+
\`seq\` integer NOT NULL,
84+
\`type\` text NOT NULL,
85+
\`data\` text NOT NULL,
86+
CONSTRAINT \`fk_event_aggregate_id_event_sequence_aggregate_id_fk\` FOREIGN KEY (\`aggregate_id\`) REFERENCES \`event_sequence\`(\`aggregate_id\`) ON DELETE CASCADE
87+
);
88+
`)
89+
yield* tx.run(`
90+
CREATE TABLE \`permission\` (
91+
\`id\` text PRIMARY KEY,
92+
\`project_id\` text NOT NULL,
93+
\`action\` text NOT NULL,
94+
\`resource\` text NOT NULL,
95+
\`time_created\` integer NOT NULL,
96+
\`time_updated\` integer NOT NULL,
97+
CONSTRAINT \`fk_permission_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
98+
);
99+
`)
100+
yield* tx.run(`
101+
CREATE TABLE \`project_directory\` (
102+
\`project_id\` text NOT NULL,
103+
\`directory\` text NOT NULL,
104+
\`type\` text,
105+
\`strategy\` text,
106+
\`time_created\` integer NOT NULL,
107+
CONSTRAINT \`project_directory_pk\` PRIMARY KEY(\`project_id\`, \`directory\`),
108+
CONSTRAINT \`fk_project_directory_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
109+
);
110+
`)
111+
yield* tx.run(`
112+
CREATE TABLE \`project\` (
113+
\`id\` text PRIMARY KEY,
114+
\`worktree\` text NOT NULL,
115+
\`vcs\` text,
116+
\`name\` text,
117+
\`icon_url\` text,
118+
\`icon_url_override\` text,
119+
\`icon_color\` text,
120+
\`time_created\` integer NOT NULL,
121+
\`time_updated\` integer NOT NULL,
122+
\`time_initialized\` integer,
123+
\`sandboxes\` text NOT NULL,
124+
\`commands\` text
125+
);
126+
`)
127+
yield* tx.run(`
128+
CREATE TABLE \`message\` (
129+
\`id\` text PRIMARY KEY,
130+
\`session_id\` text NOT NULL,
131+
\`time_created\` integer NOT NULL,
132+
\`time_updated\` integer NOT NULL,
133+
\`data\` text NOT NULL,
134+
CONSTRAINT \`fk_message_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
135+
);
136+
`)
137+
yield* tx.run(`
138+
CREATE TABLE \`part\` (
139+
\`id\` text PRIMARY KEY,
140+
\`message_id\` text NOT NULL,
141+
\`session_id\` text NOT NULL,
142+
\`time_created\` integer NOT NULL,
143+
\`time_updated\` integer NOT NULL,
144+
\`data\` text NOT NULL,
145+
CONSTRAINT \`fk_part_message_id_message_id_fk\` FOREIGN KEY (\`message_id\`) REFERENCES \`message\`(\`id\`) ON DELETE CASCADE
146+
);
147+
`)
148+
yield* tx.run(`
149+
CREATE TABLE \`session_context_epoch\` (
150+
\`session_id\` text PRIMARY KEY,
151+
\`baseline\` text NOT NULL,
152+
\`agent\` text DEFAULT 'build' NOT NULL,
153+
\`snapshot\` text NOT NULL,
154+
\`baseline_seq\` integer NOT NULL,
155+
\`replacement_seq\` integer,
156+
\`revision\` integer DEFAULT 0 NOT NULL,
157+
CONSTRAINT \`fk_session_context_epoch_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
158+
);
159+
`)
160+
yield* tx.run(`
161+
CREATE TABLE \`session_input\` (
162+
\`id\` text PRIMARY KEY,
163+
\`session_id\` text NOT NULL,
164+
\`prompt\` text NOT NULL,
165+
\`delivery\` text NOT NULL,
166+
\`admitted_seq\` integer NOT NULL,
167+
\`promoted_seq\` integer,
168+
\`time_created\` integer NOT NULL,
169+
CONSTRAINT \`fk_session_input_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
170+
);
171+
`)
172+
yield* tx.run(`
173+
CREATE TABLE \`session_message\` (
174+
\`id\` text PRIMARY KEY,
175+
\`session_id\` text NOT NULL,
176+
\`type\` text NOT NULL,
177+
\`seq\` integer NOT NULL,
178+
\`time_created\` integer NOT NULL,
179+
\`time_updated\` integer NOT NULL,
180+
\`data\` text NOT NULL,
181+
CONSTRAINT \`fk_session_message_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
182+
);
183+
`)
184+
yield* tx.run(`
185+
CREATE TABLE \`session\` (
186+
\`id\` text PRIMARY KEY,
187+
\`project_id\` text NOT NULL,
188+
\`workspace_id\` text,
189+
\`parent_id\` text,
190+
\`slug\` text NOT NULL,
191+
\`directory\` text NOT NULL,
192+
\`path\` text,
193+
\`title\` text NOT NULL,
194+
\`version\` text NOT NULL,
195+
\`share_url\` text,
196+
\`summary_additions\` integer,
197+
\`summary_deletions\` integer,
198+
\`summary_files\` integer,
199+
\`summary_diffs\` text,
200+
\`metadata\` text,
201+
\`cost\` real DEFAULT 0 NOT NULL,
202+
\`tokens_input\` integer DEFAULT 0 NOT NULL,
203+
\`tokens_output\` integer DEFAULT 0 NOT NULL,
204+
\`tokens_reasoning\` integer DEFAULT 0 NOT NULL,
205+
\`tokens_cache_read\` integer DEFAULT 0 NOT NULL,
206+
\`tokens_cache_write\` integer DEFAULT 0 NOT NULL,
207+
\`revert\` text,
208+
\`permission\` text,
209+
\`agent\` text,
210+
\`model\` text,
211+
\`time_created\` integer NOT NULL,
212+
\`time_updated\` integer NOT NULL,
213+
\`time_compacting\` integer,
214+
\`time_archived\` integer,
215+
CONSTRAINT \`fk_session_project_id_project_id_fk\` FOREIGN KEY (\`project_id\`) REFERENCES \`project\`(\`id\`) ON DELETE CASCADE
216+
);
217+
`)
218+
yield* tx.run(`
219+
CREATE TABLE \`todo\` (
220+
\`session_id\` text NOT NULL,
221+
\`content\` text NOT NULL,
222+
\`status\` text NOT NULL,
223+
\`priority\` text NOT NULL,
224+
\`position\` integer NOT NULL,
225+
\`time_created\` integer NOT NULL,
226+
\`time_updated\` integer NOT NULL,
227+
CONSTRAINT \`todo_pk\` PRIMARY KEY(\`session_id\`, \`position\`),
228+
CONSTRAINT \`fk_todo_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
229+
);
230+
`)
231+
yield* tx.run(`
232+
CREATE TABLE \`session_share\` (
233+
\`session_id\` text PRIMARY KEY,
234+
\`id\` text NOT NULL,
235+
\`secret\` text NOT NULL,
236+
\`url\` text NOT NULL,
237+
\`time_created\` integer NOT NULL,
238+
\`time_updated\` integer NOT NULL,
239+
CONSTRAINT \`fk_session_share_session_id_session_id_fk\` FOREIGN KEY (\`session_id\`) REFERENCES \`session\`(\`id\`) ON DELETE CASCADE
240+
);
241+
`)
242+
yield* tx.run(`
243+
CREATE TABLE \`workflow_run\` (
244+
\`id\` text PRIMARY KEY,
245+
\`session_id\` text,
246+
\`directory\` text DEFAULT '' NOT NULL,
247+
\`workflow\` text NOT NULL,
248+
\`status\` text NOT NULL,
249+
\`started_at\` integer NOT NULL,
250+
\`completed_at\` integer,
251+
\`current_phase\` text,
252+
\`args\` text,
253+
\`definition\` text,
254+
\`logs\` text NOT NULL,
255+
\`agents\` text NOT NULL,
256+
\`result\` text,
257+
\`error\` text,
258+
\`resume_of\` text,
259+
\`pending_question\` text,
260+
\`time_created\` integer NOT NULL,
261+
\`time_updated\` integer NOT NULL
262+
);
263+
`)
264+
yield* tx.run(`CREATE UNIQUE INDEX \`event_aggregate_seq_idx\` ON \`event\` (\`aggregate_id\`,\`seq\`);`)
265+
yield* tx.run(`CREATE INDEX \`event_aggregate_type_seq_idx\` ON \`event\` (\`aggregate_id\`,\`type\`,\`seq\`);`)
266+
yield* tx.run(`CREATE UNIQUE INDEX \`permission_project_action_resource_idx\` ON \`permission\` (\`project_id\`,\`action\`,\`resource\`);`)
267+
yield* tx.run(`CREATE INDEX \`message_session_time_created_id_idx\` ON \`message\` (\`session_id\`,\`time_created\`,\`id\`);`)
268+
yield* tx.run(`CREATE INDEX \`part_message_id_id_idx\` ON \`part\` (\`message_id\`,\`id\`);`)
269+
yield* tx.run(`CREATE INDEX \`part_session_idx\` ON \`part\` (\`session_id\`);`)
270+
yield* tx.run(`CREATE INDEX \`session_input_session_pending_delivery_seq_idx\` ON \`session_input\` (\`session_id\`,\`promoted_seq\`,\`delivery\`,\`admitted_seq\`);`)
271+
yield* tx.run(`CREATE UNIQUE INDEX \`session_input_session_admitted_seq_idx\` ON \`session_input\` (\`session_id\`,\`admitted_seq\`);`)
272+
yield* tx.run(`CREATE UNIQUE INDEX \`session_input_session_promoted_seq_idx\` ON \`session_input\` (\`session_id\`,\`promoted_seq\`);`)
273+
yield* tx.run(`CREATE UNIQUE INDEX \`session_message_session_seq_idx\` ON \`session_message\` (\`session_id\`,\`seq\`);`)
274+
yield* tx.run(`CREATE INDEX \`session_message_session_type_seq_idx\` ON \`session_message\` (\`session_id\`,\`type\`,\`seq\`);`)
275+
yield* tx.run(`CREATE INDEX \`session_message_session_time_created_id_idx\` ON \`session_message\` (\`session_id\`,\`time_created\`,\`id\`);`)
276+
yield* tx.run(`CREATE INDEX \`session_message_time_created_idx\` ON \`session_message\` (\`time_created\`);`)
277+
yield* tx.run(`CREATE INDEX \`session_project_idx\` ON \`session\` (\`project_id\`);`)
278+
yield* tx.run(`CREATE INDEX \`session_workspace_idx\` ON \`session\` (\`workspace_id\`);`)
279+
yield* tx.run(`CREATE INDEX \`session_parent_idx\` ON \`session\` (\`parent_id\`);`)
280+
yield* tx.run(`CREATE INDEX \`todo_session_idx\` ON \`todo\` (\`session_id\`);`)
281+
yield* tx.run(`CREATE INDEX \`workflow_run_started_at_idx\` ON \`workflow_run\` (\`started_at\`);`)
282+
yield* tx.run(`CREATE INDEX \`workflow_run_status_started_at_idx\` ON \`workflow_run\` (\`status\`,\`started_at\`);`)
283+
})
284+
},
285+
} satisfies Omit<DatabaseMigration.Migration, "id">

0 commit comments

Comments
 (0)