Skip to content

Commit 6fdbf57

Browse files
Heer AmbaviHeer Ambavi
authored andcommitted
add dispatch contract, simplify, fix comments
1 parent 7ecb49d commit 6fdbf57

17 files changed

Lines changed: 1007 additions & 417 deletions

File tree

‎packages/trueforge/src/apis/schedules.ts‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OpenAPIHono, type RouteHandler } from '@hono/zod-openapi';
55
import type { Context } from 'hono';
66
import type { UserContext } from '../auth/identity';
77
import type { IAgentStore } from '../db/agentStore';
8-
import { syncScheduledRun, type IScheduleStore, type ScheduleRecord } from '../db/scheduleStore';
8+
import type { IScheduleStore, ScheduleRecord } from '../db/scheduleStore';
99
import type { WithTransaction } from '../db/transaction';
1010
import {
1111
createScheduleRoute,
@@ -96,19 +96,18 @@ export function createSchedulesRouter<TTransaction>(deps: SchedulesRouterDeps<TT
9696
}
9797

9898
const record = await deps.withTransaction(async transaction => {
99-
const created = await deps.scheduleStore.createSchedule(
99+
const { schedule } = await deps.scheduleStore.createScheduleAndRun(
100100
{
101101
tenant_id: TENANT_ID,
102102
agent_id: body.agent_id,
103103
name: body.name,
104104
manifest: body.manifest,
105105
created_by: user.userRef,
106+
runFrom: new Date(),
106107
},
107108
transaction,
108109
);
109-
// A schedule created paused arms nothing; the first PUT that activates it arms.
110-
await syncScheduledRun(deps.scheduleStore, created, new Date(), transaction);
111-
return created;
110+
return schedule;
112111
});
113112

114113
return c.json({ data: toWireSchedule(record) }, 201);
@@ -139,15 +138,17 @@ export function createSchedulesRouter<TTransaction>(deps: SchedulesRouterDeps<TT
139138
validateManifest(body.manifest);
140139

141140
const record = await deps.withTransaction(async transaction => {
142-
const updated = await deps.scheduleStore.updateSchedule(
143-
{ tenant_id: TENANT_ID, id: scheduleId, name: body.name, manifest: body.manifest },
141+
const result = await deps.scheduleStore.updateScheduleAndRun(
142+
{
143+
tenant_id: TENANT_ID,
144+
id: scheduleId,
145+
name: body.name,
146+
manifest: body.manifest,
147+
runFrom: new Date(),
148+
},
144149
transaction,
145150
);
146-
if (updated === undefined) {
147-
return undefined;
148-
}
149-
await syncScheduledRun(deps.scheduleStore, updated, new Date(), transaction);
150-
return updated;
151+
return result?.schedule;
151152
});
152153

153154
if (record === undefined) {

‎packages/trueforge/src/controller/Controller.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22
* The controller: a set of periodic control loops and the timers that drive them.
33
*
44
* Loops are independent — each has its own interval, its own re-entrancy guard, and
5-
* its own error boundary, so a slow or failing loop cannot stall another. Adding
6-
* work to the controller means adding a {@link ControlLoop}, not touching this file.
7-
*
8-
* Today there is one loop: schedule dispatch. Reconciling stuck runs and re-arming
9-
* schedules that lost their pending row are the obvious next ones.
5+
* its own error boundary, so a slow or failing loop cannot stall another.
106
*
117
* The controller runs in exactly ONE process per database. In standalone mode that is
128
* the server itself; when `STANDALONE=false` it is the dedicated single-replica
13-
* process in `src/controller.ts`. Loops are written assuming no peer runs alongside
14-
* them — see the concurrency note in `scheduleDispatch.ts`.
9+
* process in `src/controller.ts`. Loops are written assuming this.
1510
*/
1611
import type { Logger } from 'winston';
1712

0 commit comments

Comments
 (0)