Skip to content

Commit 7aa2295

Browse files
authored
refactor(tasks): rename registry hooks to observers (#59829)
1 parent 676b748 commit 7aa2295

6 files changed

Lines changed: 54 additions & 51 deletions

src/tasks/task-flow-registry.store.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type TaskFlowRegistryStore = {
1919
close?: () => void;
2020
};
2121

22-
export type TaskFlowRegistryHookEvent =
22+
export type TaskFlowRegistryObserverEvent =
2323
| {
2424
kind: "restored";
2525
flows: TaskFlowRecord[];
@@ -35,9 +35,9 @@ export type TaskFlowRegistryHookEvent =
3535
previous: TaskFlowRecord;
3636
};
3737

38-
export type TaskFlowRegistryHooks = {
39-
// Hooks are incremental/observational. Snapshot persistence belongs to TaskFlowRegistryStore.
40-
onEvent?: (event: TaskFlowRegistryHookEvent) => void;
38+
export type TaskFlowRegistryObservers = {
39+
// Observers are incremental/best-effort only. Snapshot persistence belongs to TaskFlowRegistryStore.
40+
onEvent?: (event: TaskFlowRegistryObserverEvent) => void;
4141
};
4242

4343
const defaultFlowRegistryStore: TaskFlowRegistryStore = {
@@ -49,30 +49,30 @@ const defaultFlowRegistryStore: TaskFlowRegistryStore = {
4949
};
5050

5151
let configuredFlowRegistryStore: TaskFlowRegistryStore = defaultFlowRegistryStore;
52-
let configuredFlowRegistryHooks: TaskFlowRegistryHooks | null = null;
52+
let configuredFlowRegistryObservers: TaskFlowRegistryObservers | null = null;
5353

5454
export function getTaskFlowRegistryStore(): TaskFlowRegistryStore {
5555
return configuredFlowRegistryStore;
5656
}
5757

58-
export function getTaskFlowRegistryHooks(): TaskFlowRegistryHooks | null {
59-
return configuredFlowRegistryHooks;
58+
export function getTaskFlowRegistryObservers(): TaskFlowRegistryObservers | null {
59+
return configuredFlowRegistryObservers;
6060
}
6161

6262
export function configureTaskFlowRegistryRuntime(params: {
6363
store?: TaskFlowRegistryStore;
64-
hooks?: TaskFlowRegistryHooks | null;
64+
observers?: TaskFlowRegistryObservers | null;
6565
}) {
6666
if (params.store) {
6767
configuredFlowRegistryStore = params.store;
6868
}
69-
if ("hooks" in params) {
70-
configuredFlowRegistryHooks = params.hooks ?? null;
69+
if ("observers" in params) {
70+
configuredFlowRegistryObservers = params.observers ?? null;
7171
}
7272
}
7373

7474
export function resetTaskFlowRegistryRuntimeForTests() {
7575
configuredFlowRegistryStore.close?.();
7676
configuredFlowRegistryStore = defaultFlowRegistryStore;
77-
configuredFlowRegistryHooks = null;
77+
configuredFlowRegistryObservers = null;
7878
}

src/tasks/task-flow-registry.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe("task-flow-registry", () => {
194194
});
195195
});
196196

197-
it("emits restored, upserted, and deleted flow hook events", () => {
197+
it("emits restored, upserted, and deleted flow observer events", () => {
198198
const onEvent = vi.fn();
199199
configureTaskFlowRegistryRuntime({
200200
store: {
@@ -203,15 +203,15 @@ describe("task-flow-registry", () => {
203203
}),
204204
saveSnapshot: () => {},
205205
},
206-
hooks: {
206+
observers: {
207207
onEvent,
208208
},
209209
});
210210

211211
const created = createManagedTaskFlow({
212212
ownerKey: "agent:main:main",
213-
controllerId: "tests/hooks",
214-
goal: "Observe hooks",
213+
controllerId: "tests/observers",
214+
goal: "Observe observers",
215215
});
216216

217217
deleteTaskFlowRecordById(created.flowId);

src/tasks/task-flow-registry.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import crypto from "node:crypto";
22
import { createSubsystemLogger } from "../logging/subsystem.js";
33
import {
4-
getTaskFlowRegistryHooks,
4+
getTaskFlowRegistryObservers,
55
getTaskFlowRegistryStore,
66
resetTaskFlowRegistryRuntimeForTests,
7-
type TaskFlowRegistryHookEvent,
7+
type TaskFlowRegistryObserverEvent,
88
} from "./task-flow-registry.store.js";
99
import type {
1010
TaskFlowRecord,
@@ -118,15 +118,15 @@ function snapshotFlowRecords(source: ReadonlyMap<string, TaskFlowRecord>): TaskF
118118
return [...source.values()].map((record) => cloneFlowRecord(record));
119119
}
120120

121-
function emitFlowRegistryHookEvent(createEvent: () => TaskFlowRegistryHookEvent): void {
122-
const hooks = getTaskFlowRegistryHooks();
123-
if (!hooks?.onEvent) {
121+
function emitFlowRegistryObserverEvent(createEvent: () => TaskFlowRegistryObserverEvent): void {
122+
const observers = getTaskFlowRegistryObservers();
123+
if (!observers?.onEvent) {
124124
return;
125125
}
126126
try {
127-
hooks.onEvent(createEvent());
127+
observers.onEvent(createEvent());
128128
} catch {
129-
// Flow hooks are observational. They must not break registry writes.
129+
// Flow observers are best-effort only. They must not break registry writes.
130130
}
131131
}
132132

@@ -216,7 +216,7 @@ function ensureFlowRegistryReady() {
216216
log.warn("Failed to restore task-flow registry", { error });
217217
return;
218218
}
219-
emitFlowRegistryHookEvent(() => ({
219+
emitFlowRegistryObserverEvent(() => ({
220220
kind: "restored",
221221
flows: snapshotFlowRecords(flows),
222222
}));
@@ -339,7 +339,7 @@ function applyFlowPatch(current: TaskFlowRecord, patch: FlowRecordPatch): TaskFl
339339
function writeFlowRecord(next: TaskFlowRecord, previous?: TaskFlowRecord): TaskFlowRecord {
340340
flows.set(next.flowId, next);
341341
persistFlowUpsert(next);
342-
emitFlowRegistryHookEvent(() => ({
342+
emitFlowRegistryObserverEvent(() => ({
343343
kind: "upserted",
344344
flow: cloneFlowRecord(next),
345345
...(previous ? { previous: cloneFlowRecord(previous) } : {}),
@@ -695,7 +695,7 @@ export function deleteTaskFlowRecordById(flowId: string): boolean {
695695
}
696696
flows.delete(flowId);
697697
persistFlowDelete(flowId);
698-
emitFlowRegistryHookEvent(() => ({
698+
emitFlowRegistryObserverEvent(() => ({
699699
kind: "deleted",
700700
flowId,
701701
previous: cloneFlowRecord(current),

src/tasks/task-registry.store.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
resetTaskRegistryForTests,
1313
} from "./task-registry.js";
1414
import { resolveTaskRegistryDir, resolveTaskRegistrySqlitePath } from "./task-registry.paths.js";
15-
import { configureTaskRegistryRuntime, type TaskRegistryHookEvent } from "./task-registry.store.js";
15+
import {
16+
configureTaskRegistryRuntime,
17+
type TaskRegistryObserverEvent,
18+
} from "./task-registry.store.js";
1619
import type { TaskRecord } from "./task-registry.types.js";
1720

1821
function createStoredTask(): TaskRecord {
@@ -80,8 +83,8 @@ describe("task-registry store runtime", () => {
8083
expect(latestSnapshot.tasks.get("task-restored")?.task).toBe("Restored task");
8184
});
8285

83-
it("emits incremental hook events for restore, mutation, and delete", () => {
84-
const events: TaskRegistryHookEvent[] = [];
86+
it("emits incremental observer events for restore, mutation, and delete", () => {
87+
const events: TaskRegistryObserverEvent[] = [];
8588
configureTaskRegistryRuntime({
8689
store: {
8790
loadSnapshot: () => ({
@@ -90,7 +93,7 @@ describe("task-registry store runtime", () => {
9093
}),
9194
saveSnapshot: () => {},
9295
},
93-
hooks: {
96+
observers: {
9497
onEvent: (event) => {
9598
events.push(event);
9699
},

src/tasks/task-registry.store.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type TaskRegistryStore = {
3131
close?: () => void;
3232
};
3333

34-
export type TaskRegistryHookEvent =
34+
export type TaskRegistryObserverEvent =
3535
| {
3636
kind: "restored";
3737
tasks: TaskRecord[];
@@ -47,9 +47,9 @@ export type TaskRegistryHookEvent =
4747
previous: TaskRecord;
4848
};
4949

50-
export type TaskRegistryHooks = {
51-
// Hooks are incremental/observational. Snapshot persistence belongs to TaskRegistryStore.
52-
onEvent?: (event: TaskRegistryHookEvent) => void;
50+
export type TaskRegistryObservers = {
51+
// Observers are incremental/best-effort only. Snapshot persistence belongs to TaskRegistryStore.
52+
onEvent?: (event: TaskRegistryObserverEvent) => void;
5353
};
5454

5555
const defaultTaskRegistryStore: TaskRegistryStore = {
@@ -65,30 +65,30 @@ const defaultTaskRegistryStore: TaskRegistryStore = {
6565
};
6666

6767
let configuredTaskRegistryStore: TaskRegistryStore = defaultTaskRegistryStore;
68-
let configuredTaskRegistryHooks: TaskRegistryHooks | null = null;
68+
let configuredTaskRegistryObservers: TaskRegistryObservers | null = null;
6969

7070
export function getTaskRegistryStore(): TaskRegistryStore {
7171
return configuredTaskRegistryStore;
7272
}
7373

74-
export function getTaskRegistryHooks(): TaskRegistryHooks | null {
75-
return configuredTaskRegistryHooks;
74+
export function getTaskRegistryObservers(): TaskRegistryObservers | null {
75+
return configuredTaskRegistryObservers;
7676
}
7777

7878
export function configureTaskRegistryRuntime(params: {
7979
store?: TaskRegistryStore;
80-
hooks?: TaskRegistryHooks | null;
80+
observers?: TaskRegistryObservers | null;
8181
}) {
8282
if (params.store) {
8383
configuredTaskRegistryStore = params.store;
8484
}
85-
if ("hooks" in params) {
86-
configuredTaskRegistryHooks = params.hooks ?? null;
85+
if ("observers" in params) {
86+
configuredTaskRegistryObservers = params.observers ?? null;
8787
}
8888
}
8989

9090
export function resetTaskRegistryRuntimeForTests() {
9191
configuredTaskRegistryStore.close?.();
9292
configuredTaskRegistryStore = defaultTaskRegistryStore;
93-
configuredTaskRegistryHooks = null;
93+
configuredTaskRegistryObservers = null;
9494
}

src/tasks/task-registry.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import {
2525
updateFlowRecordByIdExpectedRevision,
2626
} from "./task-flow-runtime-internal.js";
2727
import {
28-
getTaskRegistryHooks,
28+
getTaskRegistryObservers,
2929
getTaskRegistryStore,
3030
resetTaskRegistryRuntimeForTests,
31-
type TaskRegistryHookEvent,
31+
type TaskRegistryObserverEvent,
3232
} from "./task-registry.store.js";
3333
import { summarizeTaskRecords } from "./task-registry.summary.js";
3434
import type {
@@ -174,15 +174,15 @@ function snapshotTaskRecords(source: ReadonlyMap<string, TaskRecord>): TaskRecor
174174
return [...source.values()].map((record) => cloneTaskRecord(record));
175175
}
176176

177-
function emitTaskRegistryHookEvent(createEvent: () => TaskRegistryHookEvent): void {
178-
const hooks = getTaskRegistryHooks();
179-
if (!hooks?.onEvent) {
177+
function emitTaskRegistryObserverEvent(createEvent: () => TaskRegistryObserverEvent): void {
178+
const observers = getTaskRegistryObservers();
179+
if (!observers?.onEvent) {
180180
return;
181181
}
182182
try {
183-
hooks.onEvent(createEvent());
183+
observers.onEvent(createEvent());
184184
} catch (error) {
185-
log.warn("Task registry hook failed", {
185+
log.warn("Task registry observer failed", {
186186
event: "task-registry",
187187
error,
188188
});
@@ -826,7 +826,7 @@ function restoreTaskRegistryOnce() {
826826
rebuildOwnerKeyIndex();
827827
rebuildParentFlowIdIndex();
828828
rebuildRelatedSessionKeyIndex();
829-
emitTaskRegistryHookEvent(() => ({
829+
emitTaskRegistryObserverEvent(() => ({
830830
kind: "restored",
831831
tasks: snapshotTaskRecords(tasks),
832832
}));
@@ -888,7 +888,7 @@ function updateTask(taskId: string, patch: Partial<TaskRecord>): TaskRecord | nu
888888
error,
889889
});
890890
}
891-
emitTaskRegistryHookEvent(() => ({
891+
emitTaskRegistryObserverEvent(() => ({
892892
kind: "upserted",
893893
task: cloneTaskRecord(next),
894894
previous: cloneTaskRecord(current),
@@ -1456,7 +1456,7 @@ export function createTaskRecord(params: {
14561456
error,
14571457
});
14581458
}
1459-
emitTaskRegistryHookEvent(() => ({
1459+
emitTaskRegistryObserverEvent(() => ({
14601460
kind: "upserted",
14611461
task: cloneTaskRecord(record),
14621462
}));
@@ -1902,7 +1902,7 @@ export function deleteTaskRecordById(taskId: string): boolean {
19021902
rebuildRunIdIndex();
19031903
persistTaskDelete(taskId);
19041904
persistTaskDeliveryStateDelete(taskId);
1905-
emitTaskRegistryHookEvent(() => ({
1905+
emitTaskRegistryObserverEvent(() => ({
19061906
kind: "deleted",
19071907
taskId: current.taskId,
19081908
previous: cloneTaskRecord(current),

0 commit comments

Comments
 (0)