Skip to content
Open
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
15 changes: 13 additions & 2 deletions .github/workflows/closura-arc-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ jobs:
vp run --filter t3 typecheck
vp run --filter @t3tools/mobile typecheck

- name: Test AssemblyAI dictation, native inbox and thread placement behavior
- name: Test AssemblyAI dictation, native inbox, thread placement and scheduled-task behavior
shell: bash
run: |
set -euo pipefail
Expand All @@ -161,7 +161,18 @@ jobs:
apps/web/src/components/chat/draftAttachmentUploads.test.ts \
apps/web/src/components/chat/draftUploadReconcile.test.ts \
apps/web/src/lib/attachmentUploadQueue.test.ts \
apps/server/src/ws.test.ts
apps/server/src/ws.test.ts \
packages/contracts/src/scheduledTask.test.ts \
apps/server/src/scheduledTasks/storedSchedule.test.ts \
apps/server/src/scheduledTasks/Schedule.test.ts \
apps/server/src/scheduledTasks/ScheduledTaskService.test.ts \
apps/server/src/scheduledTasks/ScheduledTaskService.interval.test.ts \
apps/server/src/scheduledTasks/ScheduledTaskService.schedule.test.ts \
apps/server/src/scheduledTasks/ScheduledTaskService.webhook.test.ts \
apps/server/src/scheduledTasks/ScheduledTaskService.placement.test.ts \
apps/server/src/scheduledTasks/webhookRoute.test.ts \
apps/server/src/persistence/Migrations.test.ts \
apps/server/src/persistence/Migrations/097_ScheduledTaskOccurrences.test.ts

- name: Test mobile new-task placement, outbox and uploads
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import Migration0093 from "./Migrations/093_ProviderTurnAttemptIndex.ts";
import Migration0094 from "./Migrations/094_TurnItemMessageOrdinalIndex.ts";
import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts";
import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts";
import Migration0097 from "./Migrations/097_ScheduledTaskOccurrences.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -203,6 +204,7 @@ const migrationEntries = [
[94, "TurnItemMessageOrdinalIndex", Migration0094],
[95, "TurnItemInlineWorkOutput", Migration0095],
[96, "RepairStaleTitleRegeneration", Migration0096],
[97, "ScheduledTaskOccurrences", Migration0097],
] as const;

export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { assert, it } from "@effect/vitest";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as SqlClient from "effect/sql/SqlClient";

import { runMigrations } from "../Migrations.ts";

const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
const at = "2026-11-18T12:00:00.000Z";

layer("097_ScheduledTaskOccurrences", (it) => {
it.effect("keeps one occurrence per trigger identity, independent of the task row", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runMigrations({ toMigrationInclusive: 97 });
const occurrence = (id: string, overrides: Record<string, unknown> = {}) =>
sql`INSERT INTO scheduled_task_occurrences ${sql.insert({
occurrence_id: id,
origin_environment_id: "environment-origin",
// No task row exists: an operation outlives its task's deletion or replacement.
task_id: "task:gone",
task_incarnation: "incarnation-1",
trigger_kind: "timer",
trigger_key: at,
owner_ref: null,
command_id: `command:${id}`,
message_id: `message:${id}`,
request_key: `request:${id}`,
policy_revision: 1,
snapshot_json: "{}",
state: "pending",
created_at: at,
updated_at: at,
...overrides,
})}`;
yield* occurrence("first");
// The same trigger of the same incarnation is the same operation.
assert.isTrue(Exit.isFailure(yield* Effect.exit(occurrence("again"))));
// A request key names exactly one operation.
assert.isTrue(
Exit.isFailure(
yield* Effect.exit(
occurrence("other-trigger", { trigger_key: "x", request_key: "request:first" }),
),
),
);
assert.isTrue(
Exit.isFailure(yield* Effect.exit(occurrence("bad-kind", { trigger_kind: "cron" }))),
);
// A recreated task is a new incarnation, with its own occurrences.
yield* occurrence("recreated", { task_incarnation: "incarnation-2" });
const rows = yield* sql<{ occurrence_id: string; revision: number }>`
SELECT occurrence_id, revision FROM scheduled_task_occurrences ORDER BY occurrence_id`;
assert.deepStrictEqual(rows, [
{ occurrence_id: "first", revision: 1 },
{ occurrence_id: "recreated", revision: 1 },
]);
}),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

/**
* One durable record per occurrence of a task an accepted placement owns: its trigger identity is
* unique per origin, task incarnation and trigger, so a retry or restart resumes the same operation.
* No foreign key to scheduled_tasks: deleting or recreating a task must not erase an operation
* whose effect may already have happened.
*/
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`
CREATE TABLE scheduled_task_occurrences (
occurrence_id TEXT PRIMARY KEY,
origin_environment_id TEXT NOT NULL,
task_id TEXT NOT NULL,
task_incarnation TEXT NOT NULL,
trigger_kind TEXT NOT NULL CHECK (trigger_kind IN ('timer', 'manual', 'webhook')),
trigger_key TEXT NOT NULL,
owner_ref TEXT,
command_id TEXT NOT NULL,
message_id TEXT NOT NULL,
request_key TEXT NOT NULL UNIQUE,
policy_revision INTEGER NOT NULL,
snapshot_json TEXT NOT NULL,
state TEXT NOT NULL,
destination_environment_id TEXT,
destination_project_id TEXT,
destination_link_incarnation TEXT,
destination_thread_id TEXT,
destination_message_id TEXT,
destination_run_id TEXT,
outcome_json TEXT,
revision INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
UNIQUE (origin_environment_id, task_id, task_incarnation, trigger_kind, trigger_key)
)
`;
yield* sql`
CREATE INDEX idx_scheduled_task_occurrences_task
ON scheduled_task_occurrences (task_id, task_incarnation, created_at)
`;
});
Loading
Loading