Skip to content

Commit 6f33ca5

Browse files
committed
feat(server): server-side scheduled tasks — start agent runs automatically
Adds task.schedule / task.cancel commands, a scheduler layer that fires due tasks through the orchestration engine with deterministic idempotent command ids, and a reactor that turns each fire into a normal thread.turn.start on the anchor thread. Includes projection table + queries, listTasks RPC, web settings panel section, and docs. Fixes #7966
1 parent 2433f4c commit 6f33ca5

52 files changed

Lines changed: 2278 additions & 18 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/server/integration/OrchestrationEngineHarness.integration.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
type OrchestrationEngineShape,
6464
} from "../src/orchestration/Services/OrchestrationEngine.ts";
6565
import { ThreadDeletionReactor } from "../src/orchestration/Services/ThreadDeletionReactor.ts";
66+
import { TaskFireReactor } from "../src/orchestration/Services/TaskFireReactor.ts";
6667
import { OrchestrationReactor } from "../src/orchestration/Services/OrchestrationReactor.ts";
6768
import { ProjectionSnapshotQuery } from "../src/orchestration/Services/ProjectionSnapshotQuery.ts";
6869
import {
@@ -375,6 +376,12 @@ export const makeOrchestrationIntegrationHarness = (
375376
drain: Effect.void,
376377
}),
377378
),
379+
Layer.provideMerge(
380+
Layer.succeed(TaskFireReactor, {
381+
start: () => Effect.void,
382+
drain: Effect.void,
383+
}),
384+
),
378385
Layer.provideMerge(
379386
Layer.succeed(AgentAwarenessRelay.AgentAwarenessRelay, {
380387
publishThread: () => Effect.void,

‎apps/server/integration/orphanedProviderSessionStartup.integration.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { OrchestrationLayerLive } from "../src/orchestration/runtimeLayer.ts";
2828
import * as OrchestrationEngine from "../src/orchestration/Services/OrchestrationEngine.ts";
2929
import * as OrchestrationReactor from "../src/orchestration/Services/OrchestrationReactor.ts";
3030
import * as ProjectionSnapshotQuery from "../src/orchestration/Services/ProjectionSnapshotQuery.ts";
31+
import * as TaskScheduler from "../src/orchestration/Services/TaskScheduler.ts";
3132
import { makeSqlitePersistenceLive } from "../src/persistence/Layers/Sqlite.ts";
3233
import * as ProviderSessionRuntime from "../src/persistence/ProviderSessionRuntime.ts";
3334
import * as ExternalLauncher from "../src/process/externalLauncher.ts";
@@ -75,6 +76,10 @@ const startupDependencies = Layer.mergeAll(
7576
Layer.succeed(ProviderSessionReaper.ProviderSessionReaper, {
7677
start: () => Effect.void,
7778
}),
79+
Layer.succeed(TaskScheduler.TaskScheduler, {
80+
start: () => Effect.void,
81+
tick: () => Effect.succeed(0),
82+
}),
7883
ServerLifecycleEvents.layer,
7984
Layer.succeed(ServerEnvironment.ServerEnvironment, {
8085
getEnvironmentId: Effect.succeed(EnvironmentId.make("environment-startup-orphan")),

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const RPC_REQUIRED_SCOPES = {
2626
[ORCHESTRATION_WS_METHODS.getTurnDiff]: AuthOrchestrationReadScope,
2727
[ORCHESTRATION_WS_METHODS.getFullThreadDiff]: AuthOrchestrationReadScope,
2828
[ORCHESTRATION_WS_METHODS.searchThreads]: AuthOrchestrationReadScope,
29+
[ORCHESTRATION_WS_METHODS.listTasks]: AuthOrchestrationReadScope,
2930
[ORCHESTRATION_WS_METHODS.subscribeShell]: AuthOrchestrationReadScope,
3031
[ORCHESTRATION_WS_METHODS.getArchivedShellSnapshot]: AuthOrchestrationReadScope,
3132
[ORCHESTRATION_WS_METHODS.subscribeThread]: AuthOrchestrationReadScope,

‎apps/server/src/checkpointing/CheckpointDiffQuery.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ describe("CheckpointDiffQuery.layer", () => {
109109
getThreadDetailById: () => Effect.succeed(Option.none()),
110110
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
111111
searchThreads: () => Effect.succeed({ matches: [] }),
112+
listTasks: () => Effect.succeed([]),
113+
listDueTasks: () => Effect.succeed([]),
112114
}),
113115
),
114116
);
@@ -203,6 +205,8 @@ describe("CheckpointDiffQuery.layer", () => {
203205
getThreadDetailById: () => Effect.succeed(Option.none()),
204206
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
205207
searchThreads: () => Effect.succeed({ matches: [] }),
208+
listTasks: () => Effect.succeed([]),
209+
listDueTasks: () => Effect.succeed([]),
206210
}),
207211
),
208212
);
@@ -287,6 +291,8 @@ describe("CheckpointDiffQuery.layer", () => {
287291
getThreadDetailById: () => Effect.succeed(Option.none()),
288292
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
289293
searchThreads: () => Effect.succeed({ matches: [] }),
294+
listTasks: () => Effect.succeed([]),
295+
listDueTasks: () => Effect.succeed([]),
290296
}),
291297
),
292298
);
@@ -356,6 +362,8 @@ describe("CheckpointDiffQuery.layer", () => {
356362
getThreadDetailById: () => Effect.succeed(Option.none()),
357363
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
358364
searchThreads: () => Effect.succeed({ matches: [] }),
365+
listTasks: () => Effect.succeed([]),
366+
listDueTasks: () => Effect.succeed([]),
359367
}),
360368
),
361369
);
@@ -410,6 +418,8 @@ describe("CheckpointDiffQuery.layer", () => {
410418
getThreadDetailById: () => Effect.succeed(Option.none()),
411419
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
412420
searchThreads: () => Effect.succeed({ matches: [] }),
421+
listTasks: () => Effect.succeed([]),
422+
listDueTasks: () => Effect.succeed([]),
413423
}),
414424
),
415425
);

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export const make = Effect.gen(function* () {
151151
threadSnooze: true,
152152
threadPinning: true,
153153
threadPinReorder: true,
154+
taskScheduling: true,
154155
threadTitleRegeneration: true,
155156
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
156157
...(serverSelfUpdate === "boot-service" ? { serverSelfUpdateProgress: true } : {}),

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe("OrchestrationEngine", () => {
116116
};
117117

118118
const projectionSnapshot = {
119+
tasks: [],
119120
snapshotSequence: 7,
120121
updatedAt: "2026-03-03T00:00:04.000Z",
121122
projects: [
@@ -208,6 +209,8 @@ describe("OrchestrationEngine", () => {
208209
getThreadDetailById: () => Effect.succeed(Option.none()),
209210
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
210211
searchThreads: () => Effect.succeed({ matches: [] }),
212+
listTasks: () => Effect.succeed([]),
213+
listDueTasks: () => Effect.succeed([]),
211214
}),
212215
),
213216
Layer.provide(

‎apps/server/src/orchestration/Layers/OrchestrationEngine.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
OrchestrationEvent,
44
OrchestrationReadModel,
55
ProjectId,
6+
TaskId,
67
ThreadId,
78
} from "@t3tools/contracts";
89
import { OrchestrationCommand } from "@t3tools/contracts";
@@ -61,8 +62,8 @@ interface CommandEnvelope {
6162
}
6263

6364
function commandToAggregateRef(command: OrchestrationCommand): {
64-
readonly aggregateKind: "project" | "thread";
65-
readonly aggregateId: ProjectId | ThreadId;
65+
readonly aggregateKind: "project" | "thread" | "task";
66+
readonly aggregateId: ProjectId | ThreadId | TaskId;
6667
} {
6768
switch (command.type) {
6869
case "project.create":
@@ -72,6 +73,13 @@ function commandToAggregateRef(command: OrchestrationCommand): {
7273
aggregateKind: "project",
7374
aggregateId: command.projectId,
7475
};
76+
case "task.schedule":
77+
case "task.cancel":
78+
case "task.fire":
79+
return {
80+
aggregateKind: "task",
81+
aggregateId: command.taskId,
82+
};
7583
default:
7684
return {
7785
aggregateKind: "thread",

‎apps/server/src/orchestration/Layers/OrchestrationReactor.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CheckpointReactor } from "../Services/CheckpointReactor.ts";
99
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
1010
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
1111
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
12+
import { TaskFireReactor } from "../Services/TaskFireReactor.ts";
1213
import { OrchestrationReactor } from "../Services/OrchestrationReactor.ts";
1314
import { makeOrchestrationReactor } from "./OrchestrationReactor.ts";
1415
import * as AgentAwarenessRelay from "../../relay/AgentAwarenessRelay.ts";
@@ -23,7 +24,7 @@ describe("OrchestrationReactor", () => {
2324
runtime = null;
2425
});
2526

26-
it("starts provider ingestion, provider command, checkpoint, and thread deletion reactors", async () => {
27+
it("starts provider ingestion, provider command, checkpoint, thread deletion, and task fire reactors", async () => {
2728
const started: string[] = [];
2829

2930
runtime = ManagedRuntime.make(
@@ -64,6 +65,15 @@ describe("OrchestrationReactor", () => {
6465
drain: Effect.void,
6566
}),
6667
),
68+
Layer.provideMerge(
69+
Layer.succeed(TaskFireReactor, {
70+
start: () => {
71+
started.push("task-fire-reactor");
72+
return Effect.void;
73+
},
74+
drain: Effect.void,
75+
}),
76+
),
6777
Layer.provideMerge(
6878
Layer.succeed(AgentAwarenessRelay.AgentAwarenessRelay, {
6979
publishThread: () => Effect.void,
@@ -85,6 +95,7 @@ describe("OrchestrationReactor", () => {
8595
"provider-command-reactor",
8696
"checkpoint-reactor",
8797
"thread-deletion-reactor",
98+
"task-fire-reactor",
8899
"agent-awareness-relay",
89100
]);
90101

‎apps/server/src/orchestration/Layers/OrchestrationReactor.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { CheckpointReactor } from "../Services/CheckpointReactor.ts";
99
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
1010
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
11+
import { TaskFireReactor } from "../Services/TaskFireReactor.ts";
1112
import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts";
1213
import * as AgentAwarenessRelay from "../../relay/AgentAwarenessRelay.ts";
1314

@@ -16,13 +17,15 @@ export const makeOrchestrationReactor = Effect.gen(function* () {
1617
const providerCommandReactor = yield* ProviderCommandReactor;
1718
const checkpointReactor = yield* CheckpointReactor;
1819
const threadDeletionReactor = yield* ThreadDeletionReactor;
20+
const taskFireReactor = yield* TaskFireReactor;
1921
const agentAwarenessRelay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
2022

2123
const start: OrchestrationReactorShape["start"] = Effect.fn("start")(function* () {
2224
yield* providerRuntimeIngestion.start();
2325
yield* providerCommandReactor.start();
2426
yield* checkpointReactor.start();
2527
yield* threadDeletionReactor.start();
28+
yield* taskFireReactor.start();
2629
yield* agentAwarenessRelay.start();
2730
});
2831

‎apps/server/src/orchestration/Layers/ProjectionPipeline.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
type ChatAttachment,
44
type OrchestrationEvent,
55
type OrchestrationSessionStatus,
6+
TaskScheduleSpec,
67
ThreadId,
78
} from "@t3tools/contracts";
89
import * as Effect from "effect/Effect";
910
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
1112
import * as Option from "effect/Option";
1213
import * as Path from "effect/Path";
14+
import * as Schema from "effect/Schema";
1315
import * as Stream from "effect/Stream";
1416
import * as SqlClient from "effect/unstable/sql/SqlClient";
1517

@@ -34,6 +36,7 @@ import {
3436
ProjectionTurnRepository,
3537
} from "../../persistence/Services/ProjectionTurns.ts";
3638
import { ProjectionThreadRepository } from "../../persistence/Services/ProjectionThreads.ts";
39+
import { ProjectionTaskRepository } from "../../persistence/Services/ProjectionTasks.ts";
3740
import { ProjectionPendingApprovalRepositoryLive } from "../../persistence/Layers/ProjectionPendingApprovals.ts";
3841
import { ProjectionProjectRepositoryLive } from "../../persistence/Layers/ProjectionProjects.ts";
3942
import { ProjectionStateRepositoryLive } from "../../persistence/Layers/ProjectionState.ts";
@@ -43,6 +46,7 @@ import { ProjectionThreadProposedPlanRepositoryLive } from "../../persistence/La
4346
import { ProjectionThreadSessionRepositoryLive } from "../../persistence/Layers/ProjectionThreadSessions.ts";
4447
import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts";
4548
import { ProjectionThreadRepositoryLive } from "../../persistence/Layers/ProjectionThreads.ts";
49+
import { ProjectionTaskRepositoryLive } from "../../persistence/Layers/ProjectionTasks.ts";
4650
import { ServerConfig } from "../../config.ts";
4751
import {
4852
OrchestrationProjectionPipeline,
@@ -55,6 +59,8 @@ import {
5559
toSafeThreadAttachmentSegment,
5660
} from "../../attachmentStore.ts";
5761

62+
const encodeTaskScheduleJson = Schema.encodeEffect(Schema.fromJsonString(TaskScheduleSpec));
63+
5864
export const ORCHESTRATION_PROJECTOR_NAMES = {
5965
projects: "projection.projects",
6066
threads: "projection.threads",
@@ -65,6 +71,7 @@ export const ORCHESTRATION_PROJECTOR_NAMES = {
6571
threadTurns: "projection.thread-turns",
6672
checkpoints: "projection.checkpoints",
6773
pendingApprovals: "projection.pending-approvals",
74+
tasks: "projection.tasks",
6875
} as const;
6976

7077
type ProjectorName =
@@ -480,6 +487,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
480487
const projectionThreadSessionRepository = yield* ProjectionThreadSessionRepository;
481488
const projectionTurnRepository = yield* ProjectionTurnRepository;
482489
const projectionPendingApprovalRepository = yield* ProjectionPendingApprovalRepository;
490+
const projectionTaskRepository = yield* ProjectionTaskRepository;
483491

484492
const fileSystem = yield* FileSystem.FileSystem;
485493
const path = yield* Path.Path;
@@ -1606,6 +1614,69 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16061614
}
16071615
});
16081616

1617+
const applyTasksProjection: ProjectorDefinition["apply"] = Effect.fn("applyTasksProjection")(
1618+
function* (event, _attachmentSideEffects) {
1619+
switch (event.type) {
1620+
case "task.scheduled": {
1621+
// The payload was schema-decoded off the wire, so encoding cannot
1622+
// fail here; a failure is a defect, not a projection error.
1623+
const scheduleJson = yield* encodeTaskScheduleJson(event.payload.schedule).pipe(
1624+
Effect.orDie,
1625+
);
1626+
yield* projectionTaskRepository.upsert({
1627+
taskId: event.payload.taskId,
1628+
projectId: event.payload.projectId,
1629+
threadId: event.payload.threadId,
1630+
name: event.payload.name ?? null,
1631+
prompt: event.payload.prompt,
1632+
scheduleJson,
1633+
createdAt: event.payload.createdAt,
1634+
updatedAt: event.payload.updatedAt,
1635+
lastFiredAt: null,
1636+
nextFireAt: event.payload.nextFireAt,
1637+
cancelledAt: null,
1638+
});
1639+
return;
1640+
}
1641+
1642+
case "task.fired": {
1643+
const existingRow = yield* projectionTaskRepository.getById({
1644+
taskId: event.payload.taskId,
1645+
});
1646+
if (Option.isNone(existingRow)) {
1647+
return;
1648+
}
1649+
yield* projectionTaskRepository.upsert({
1650+
...existingRow.value,
1651+
lastFiredAt: event.payload.firedAt,
1652+
nextFireAt: event.payload.nextFireAt,
1653+
updatedAt: event.payload.updatedAt,
1654+
});
1655+
return;
1656+
}
1657+
1658+
case "task.cancelled": {
1659+
const existingRow = yield* projectionTaskRepository.getById({
1660+
taskId: event.payload.taskId,
1661+
});
1662+
if (Option.isNone(existingRow)) {
1663+
return;
1664+
}
1665+
yield* projectionTaskRepository.upsert({
1666+
...existingRow.value,
1667+
nextFireAt: null,
1668+
cancelledAt: event.payload.cancelledAt,
1669+
updatedAt: event.payload.updatedAt,
1670+
});
1671+
return;
1672+
}
1673+
1674+
default:
1675+
return;
1676+
}
1677+
},
1678+
);
1679+
16091680
const projectors: ReadonlyArray<ProjectorDefinition> = [
16101681
{
16111682
name: ORCHESTRATION_PROJECTOR_NAMES.projects,
@@ -1643,6 +1714,10 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
16431714
name: ORCHESTRATION_PROJECTOR_NAMES.threads,
16441715
apply: applyThreadsProjection,
16451716
},
1717+
{
1718+
name: ORCHESTRATION_PROJECTOR_NAMES.tasks,
1719+
apply: applyTasksProjection,
1720+
},
16461721
];
16471722

16481723
const runProjectorForEvent = Effect.fn("runProjectorForEvent")(function* (
@@ -1746,4 +1821,5 @@ export const OrchestrationProjectionPipelineLive = Layer.effect(
17461821
Layer.provideMerge(ProjectionTurnRepositoryLive),
17471822
Layer.provideMerge(ProjectionPendingApprovalRepositoryLive),
17481823
Layer.provideMerge(ProjectionStateRepositoryLive),
1824+
Layer.provideMerge(ProjectionTaskRepositoryLive),
17491825
);

0 commit comments

Comments
 (0)