@trevonistrevon/pi-loop 0.5.9 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/README.md +64 -5
- package/dist/api.d.ts +11 -0
- package/dist/api.js +10 -0
- package/dist/commands/loop-command.d.ts +9 -2
- package/dist/commands/loop-command.js +81 -51
- package/dist/commands/tasks-command.js +3 -3
- package/dist/coordinator.d.ts +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +89 -19
- package/dist/loop-format.d.ts +3 -0
- package/dist/loop-format.js +20 -0
- package/dist/loop-parse.d.ts +1 -0
- package/dist/loop-parse.js +59 -6
- package/dist/loop-reducer.d.ts +13 -1
- package/dist/loop-reducer.js +26 -0
- package/dist/monitor-manager.js +1 -1
- package/dist/notification-reducer.d.ts +2 -0
- package/dist/reducer-backed-store.d.ts +1 -1
- package/dist/reducer-backed-store.js +1 -1
- package/dist/rpc/channels.d.ts +73 -0
- package/dist/rpc/channels.js +30 -0
- package/dist/rpc/cross-extension-rpc.d.ts +59 -0
- package/dist/rpc/cross-extension-rpc.js +117 -0
- package/dist/runtime/loop-events.d.ts +26 -0
- package/dist/runtime/loop-events.js +26 -0
- package/dist/runtime/monitor-ondone-runtime.js +1 -1
- package/dist/runtime/native-task-rpc.d.ts +22 -0
- package/dist/runtime/native-task-rpc.js +50 -0
- package/dist/runtime/notification-runtime.d.ts +2 -1
- package/dist/runtime/notification-runtime.js +19 -7
- package/dist/runtime/task-backlog-runtime.d.ts +5 -1
- package/dist/runtime/task-backlog-runtime.js +14 -7
- package/dist/runtime/task-mutations.d.ts +39 -0
- package/dist/runtime/task-mutations.js +68 -0
- package/dist/runtime/task-rpc.d.ts +4 -0
- package/dist/runtime/task-rpc.js +40 -58
- package/dist/scheduler.js +25 -7
- package/dist/store.d.ts +9 -1
- package/dist/store.js +57 -0
- package/dist/tools/loop-tools.d.ts +8 -0
- package/dist/tools/loop-tools.js +105 -15
- package/dist/tools/monitor-tools.js +1 -3
- package/dist/tools/native-task-tools.d.ts +2 -6
- package/dist/tools/native-task-tools.js +25 -55
- package/dist/tools/tool-result.d.ts +8 -0
- package/dist/tools/tool-result.js +4 -0
- package/dist/trigger-system.js +1 -1
- package/dist/types.d.ts +24 -1
- package/package.json +15 -2
- package/src/api.ts +36 -0
- package/src/commands/loop-command.ts +100 -55
- package/src/commands/tasks-command.ts +3 -3
- package/src/coordinator.ts +1 -1
- package/src/index.ts +92 -19
- package/src/loop-format.ts +22 -0
- package/src/loop-parse.ts +62 -8
- package/src/loop-reducer.ts +41 -1
- package/src/monitor-manager.ts +1 -1
- package/src/notification-reducer.ts +3 -0
- package/src/reducer-backed-store.ts +3 -3
- package/src/rpc/channels.ts +97 -0
- package/src/rpc/cross-extension-rpc.ts +152 -0
- package/src/runtime/loop-events.ts +60 -0
- package/src/runtime/monitor-ondone-runtime.ts +1 -1
- package/src/runtime/native-task-rpc.ts +139 -0
- package/src/runtime/notification-runtime.ts +21 -8
- package/src/runtime/task-backlog-runtime.ts +25 -7
- package/src/runtime/task-mutations.ts +103 -0
- package/src/runtime/task-rpc.ts +48 -54
- package/src/scheduler.ts +25 -8
- package/src/store.ts +59 -2
- package/src/tools/loop-tools.ts +129 -16
- package/src/tools/monitor-tools.ts +1 -4
- package/src/tools/native-task-tools.ts +31 -52
- package/src/tools/tool-result.ts +4 -0
- package/src/trigger-system.ts +5 -5
- package/src/types.ts +29 -1
- package/.release-please-manifest.json +0 -3
- package/AGENTS.md +0 -83
- package/CONTRIBUTING.md +0 -69
- package/Dockerfile +0 -17
- package/benchmarks/experiment-sim.js +0 -30
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/coverage-final.json +0 -33
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -176
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -210
- package/coverage/src/commands/index.html +0 -131
- package/coverage/src/commands/loop-command.ts.html +0 -634
- package/coverage/src/commands/tasks-command.ts.html +0 -490
- package/coverage/src/coordinator.ts.html +0 -430
- package/coverage/src/goal-coordinator.ts.html +0 -259
- package/coverage/src/goal-reducer.ts.html +0 -982
- package/coverage/src/goal-store.ts.html +0 -742
- package/coverage/src/goal-verifier.ts.html +0 -808
- package/coverage/src/index.html +0 -386
- package/coverage/src/index.ts.html +0 -1111
- package/coverage/src/loop-parse.ts.html +0 -574
- package/coverage/src/loop-reducer.ts.html +0 -559
- package/coverage/src/monitor-completion-coordinator.ts.html +0 -157
- package/coverage/src/monitor-manager.ts.html +0 -940
- package/coverage/src/monitor-reducer.ts.html +0 -583
- package/coverage/src/notification-reducer.ts.html +0 -550
- package/coverage/src/reducer-backed-store.ts.html +0 -589
- package/coverage/src/runtime/index.html +0 -206
- package/coverage/src/runtime/monitor-ondone-runtime.ts.html +0 -310
- package/coverage/src/runtime/notification-runtime.ts.html +0 -721
- package/coverage/src/runtime/scope.ts.html +0 -196
- package/coverage/src/runtime/session-runtime.ts.html +0 -679
- package/coverage/src/runtime/task-backlog-runtime.ts.html +0 -574
- package/coverage/src/runtime/task-events.ts.html +0 -208
- package/coverage/src/runtime/task-rpc.ts.html +0 -541
- package/coverage/src/scheduler.ts.html +0 -400
- package/coverage/src/store.ts.html +0 -667
- package/coverage/src/task-backlog-coordinator.ts.html +0 -181
- package/coverage/src/task-reducer.ts.html +0 -550
- package/coverage/src/task-store.ts.html +0 -526
- package/coverage/src/tools/index.html +0 -146
- package/coverage/src/tools/loop-tools.ts.html +0 -1000
- package/coverage/src/tools/monitor-tools.ts.html +0 -547
- package/coverage/src/tools/native-task-tools.ts.html +0 -556
- package/coverage/src/trigger-system.ts.html +0 -547
- package/coverage/src/ui/index.html +0 -116
- package/coverage/src/ui/widget.ts.html +0 -292
- package/dist/goal-coordinator.d.ts +0 -22
- package/dist/goal-coordinator.js +0 -28
- package/dist/goal-reducer.d.ts +0 -98
- package/dist/goal-reducer.js +0 -190
- package/dist/goal-store.d.ts +0 -22
- package/dist/goal-store.js +0 -188
- package/dist/goal-types.d.ts +0 -82
- package/dist/goal-types.js +0 -1
- package/dist/goal-verifier.d.ts +0 -20
- package/dist/goal-verifier.js +0 -198
- package/docker-compose.yml +0 -12
- package/docs/architecture/goal-state-schema.md +0 -505
- package/docs/architecture/state-machine-migration.md +0 -546
- package/docs/architecture/state-machine-reducer-event-model.md +0 -823
- package/docs/architecture/state-machine-test-matrix.md +0 -249
- package/docs/architecture/state-machine-transition-map.md +0 -436
- package/release-please-config.json +0 -9
- package/src/goal-coordinator.ts +0 -58
- package/src/goal-reducer.ts +0 -299
- package/src/goal-store.ts +0 -219
- package/src/goal-types.ts +0 -104
- package/src/goal-verifier.ts +0 -241
- package/vitest.config.ts +0 -29
|
@@ -8,7 +8,13 @@ import {
|
|
|
8
8
|
type TaskBacklogEvent,
|
|
9
9
|
} from "../task-backlog-coordinator.js";
|
|
10
10
|
import type { TaskStore } from "../task-store.js";
|
|
11
|
-
import type { LoopEntry, Trigger } from "../types.js";
|
|
11
|
+
import type { LoopDeletionTombstoneInput, LoopEntry, Trigger } from "../types.js";
|
|
12
|
+
import {
|
|
13
|
+
buildLoopAutodeletedPayload,
|
|
14
|
+
buildTaskBacklogEmptyPayload,
|
|
15
|
+
type LoopAutodeletedPayload,
|
|
16
|
+
type TaskBacklogEmptyPayload,
|
|
17
|
+
} from "./loop-events.js";
|
|
12
18
|
|
|
13
19
|
export const AUTO_TASK_WORKER_THRESHOLD = 5;
|
|
14
20
|
export const AUTO_TASK_WORKER_PROMPT = "Run TaskList, pick next pending task, mark it in_progress, implement it, run validation, complete it. If no pending tasks remain, call LoopDelete on your own loop ID.";
|
|
@@ -21,12 +27,15 @@ export interface TaskBacklogRuntimeOptions {
|
|
|
21
27
|
maxFires?: number;
|
|
22
28
|
}) => LoopEntry;
|
|
23
29
|
deleteLoop: (id: string) => void;
|
|
30
|
+
recordDeletionTombstone?: (id: string, tombstone: LoopDeletionTombstoneInput) => void;
|
|
24
31
|
addTrigger: (entry: LoopEntry) => void;
|
|
25
32
|
removeTrigger: (id: string) => void;
|
|
26
33
|
updateWidget: () => void;
|
|
27
34
|
hasPendingTasks: () => Promise<number>;
|
|
28
35
|
bootstrapTaskLoop: (entry: LoopEntry) => Promise<boolean>;
|
|
29
36
|
triggerHasEventSource: (trigger: Trigger | string, source: string) => boolean;
|
|
37
|
+
emitLoopAutodeleted?: (payload: LoopAutodeletedPayload) => void;
|
|
38
|
+
emitTaskBacklogEmpty?: (payload: TaskBacklogEmptyPayload) => void;
|
|
30
39
|
debug?: (...args: unknown[]) => void;
|
|
31
40
|
}
|
|
32
41
|
|
|
@@ -44,12 +53,15 @@ export function createTaskBacklogRuntime(options: TaskBacklogRuntimeOptions): Ta
|
|
|
44
53
|
getLoops,
|
|
45
54
|
createLoop,
|
|
46
55
|
deleteLoop,
|
|
56
|
+
recordDeletionTombstone,
|
|
47
57
|
addTrigger,
|
|
48
58
|
removeTrigger,
|
|
49
59
|
updateWidget,
|
|
50
60
|
hasPendingTasks,
|
|
51
61
|
bootstrapTaskLoop,
|
|
52
62
|
triggerHasEventSource,
|
|
63
|
+
emitLoopAutodeleted,
|
|
64
|
+
emitTaskBacklogEmpty,
|
|
53
65
|
debug,
|
|
54
66
|
} = options;
|
|
55
67
|
|
|
@@ -69,6 +81,14 @@ export function createTaskBacklogRuntime(options: TaskBacklogRuntimeOptions): Ta
|
|
|
69
81
|
return getLoops().find(isAutoTaskWorkerLoop);
|
|
70
82
|
}
|
|
71
83
|
|
|
84
|
+
function deleteTaskBacklogLoop(entry: LoopEntry, pendingCount: number): void {
|
|
85
|
+
debug?.(`task backlog loop #${entry.id} — no pending tasks remain, deleting`);
|
|
86
|
+
removeTrigger(entry.id);
|
|
87
|
+
recordDeletionTombstone?.(entry.id, { reason: "task_backlog_empty", pendingCount });
|
|
88
|
+
deleteLoop(entry.id);
|
|
89
|
+
emitLoopAutodeleted?.(buildLoopAutodeletedPayload(entry, pendingCount));
|
|
90
|
+
}
|
|
91
|
+
|
|
72
92
|
async function cleanupTaskBacklogLoops(): Promise<number> {
|
|
73
93
|
const backlogLoops = getLoops().filter(isTaskBacklogLoop);
|
|
74
94
|
if (backlogLoops.length === 0) return 0;
|
|
@@ -76,11 +96,9 @@ export function createTaskBacklogRuntime(options: TaskBacklogRuntimeOptions): Ta
|
|
|
76
96
|
const pending = await hasPendingTasks();
|
|
77
97
|
if (pending < 0 || pending > 0) return 0;
|
|
78
98
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
deleteLoop(entry.id);
|
|
83
|
-
}
|
|
99
|
+
const deletedLoopIds = backlogLoops.map((entry) => entry.id);
|
|
100
|
+
emitTaskBacklogEmpty?.(buildTaskBacklogEmptyPayload(deletedLoopIds));
|
|
101
|
+
for (const entry of backlogLoops) deleteTaskBacklogLoop(entry, pending);
|
|
84
102
|
updateWidget();
|
|
85
103
|
return backlogLoops.length;
|
|
86
104
|
}
|
|
@@ -93,7 +111,7 @@ export function createTaskBacklogRuntime(options: TaskBacklogRuntimeOptions): Ta
|
|
|
93
111
|
|
|
94
112
|
const trigger: Trigger = {
|
|
95
113
|
type: "hybrid",
|
|
96
|
-
cron: "*/
|
|
114
|
+
cron: "*/3 * * * *",
|
|
97
115
|
event: { source: "tasks:created" },
|
|
98
116
|
debounceMs: 30000,
|
|
99
117
|
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { TaskStore } from "../task-store.js";
|
|
3
|
+
import type { TaskEntry, TaskStatus } from "../task-types.js";
|
|
4
|
+
import { emitNativeTaskEvent } from "./task-events.js";
|
|
5
|
+
|
|
6
|
+
export interface TaskBacklogResult {
|
|
7
|
+
created: boolean;
|
|
8
|
+
entry?: { id: string };
|
|
9
|
+
cleaned?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TaskMutationContext {
|
|
13
|
+
pi: ExtensionAPI;
|
|
14
|
+
taskStore: TaskStore;
|
|
15
|
+
evaluateTaskBacklog: (
|
|
16
|
+
taskStore: TaskStore,
|
|
17
|
+
pendingCount: number,
|
|
18
|
+
) => Promise<TaskBacklogResult>;
|
|
19
|
+
updateWidget: () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface UpdateTaskFields {
|
|
23
|
+
id: string;
|
|
24
|
+
status?: TaskStatus;
|
|
25
|
+
subject?: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Single source of truth for native task mutations, shared by the TaskCreate/
|
|
31
|
+
* TaskUpdate/TaskDelete tools and the tasks:rpc:* server so both surfaces emit
|
|
32
|
+
* identical events in one canonical order: mutate → emit → widget → backlog.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
async function settle(ctx: TaskMutationContext): Promise<TaskBacklogResult> {
|
|
36
|
+
ctx.updateWidget();
|
|
37
|
+
return await ctx.evaluateTaskBacklog(ctx.taskStore, ctx.taskStore.pendingCount());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function createTask(
|
|
41
|
+
ctx: TaskMutationContext,
|
|
42
|
+
params: { subject: string; description: string; metadata?: Record<string, unknown> },
|
|
43
|
+
): Promise<{ entry: TaskEntry; backlog: TaskBacklogResult }> {
|
|
44
|
+
const entry = ctx.taskStore.create(params.subject, params.description, params.metadata);
|
|
45
|
+
emitNativeTaskEvent(ctx.pi, "tasks:created", entry);
|
|
46
|
+
const backlog = await settle(ctx);
|
|
47
|
+
return { entry, backlog };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function updateTask(
|
|
51
|
+
ctx: TaskMutationContext,
|
|
52
|
+
params: UpdateTaskFields,
|
|
53
|
+
): Promise<{ entry: TaskEntry; backlog: TaskBacklogResult } | undefined> {
|
|
54
|
+
const { id, status, subject, description } = params;
|
|
55
|
+
let entry = ctx.taskStore.get(id);
|
|
56
|
+
if (!entry) return undefined;
|
|
57
|
+
|
|
58
|
+
const previousStatus = entry.status;
|
|
59
|
+
if (status === "in_progress") {
|
|
60
|
+
entry = ctx.taskStore.start(id);
|
|
61
|
+
if (entry) emitNativeTaskEvent(ctx.pi, "tasks:started", entry, previousStatus);
|
|
62
|
+
} else if (status === "completed") {
|
|
63
|
+
entry = ctx.taskStore.complete(id);
|
|
64
|
+
if (entry) emitNativeTaskEvent(ctx.pi, "tasks:completed", entry, previousStatus);
|
|
65
|
+
} else if (status === "pending") {
|
|
66
|
+
entry = ctx.taskStore.reopen(id);
|
|
67
|
+
if (entry) emitNativeTaskEvent(ctx.pi, "tasks:reopened", entry, previousStatus);
|
|
68
|
+
}
|
|
69
|
+
if (!entry) return undefined;
|
|
70
|
+
|
|
71
|
+
if (subject !== undefined || description !== undefined) {
|
|
72
|
+
// A details edit is not a transition: its previousStatus is the status
|
|
73
|
+
// current at edit time (i.e. after any transition above), so consumers
|
|
74
|
+
// never see a fabricated second transition.
|
|
75
|
+
const statusAtEdit = entry.status;
|
|
76
|
+
entry = ctx.taskStore.updateDetails(id, { subject, description });
|
|
77
|
+
if (entry) emitNativeTaskEvent(ctx.pi, "tasks:updated", entry, statusAtEdit);
|
|
78
|
+
}
|
|
79
|
+
if (!entry) return undefined;
|
|
80
|
+
|
|
81
|
+
const backlog = await settle(ctx);
|
|
82
|
+
return { entry, backlog };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function deleteTask(
|
|
86
|
+
ctx: TaskMutationContext,
|
|
87
|
+
id: string,
|
|
88
|
+
): Promise<{ entry: TaskEntry; backlog: TaskBacklogResult } | undefined> {
|
|
89
|
+
const existing = ctx.taskStore.get(id);
|
|
90
|
+
if (!existing || !ctx.taskStore.delete(id)) {
|
|
91
|
+
ctx.updateWidget();
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
emitNativeTaskEvent(ctx.pi, "tasks:deleted", existing, existing.status);
|
|
95
|
+
const backlog = await settle(ctx);
|
|
96
|
+
return { entry: existing, backlog };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function cleanTasks(ctx: TaskMutationContext): Promise<number> {
|
|
100
|
+
const pruned = ctx.taskStore.pruneCompleted();
|
|
101
|
+
await settle(ctx);
|
|
102
|
+
return pruned;
|
|
103
|
+
}
|
package/src/runtime/task-rpc.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import {
|
|
4
|
+
type CleanReply,
|
|
5
|
+
type CreateTaskReply,
|
|
6
|
+
type PendingReply,
|
|
7
|
+
type PingReply,
|
|
8
|
+
replyChannel,
|
|
9
|
+
TASKS_RPC,
|
|
10
|
+
} from "../rpc/channels.js";
|
|
11
|
+
import { type RpcReply, rpcCall } from "../rpc/cross-extension-rpc.js";
|
|
3
12
|
import type { TaskStore } from "../task-store.js";
|
|
4
13
|
import type { LoopEntry } from "../types.js";
|
|
14
|
+
import { NATIVE_TASKS_PROVIDER } from "./native-task-rpc.js";
|
|
5
15
|
import { emitNativeTaskEvent } from "./task-events.js";
|
|
6
16
|
|
|
7
17
|
export interface TaskRuntimeBridgeOptions {
|
|
@@ -11,6 +21,10 @@ export interface TaskRuntimeBridgeOptions {
|
|
|
11
21
|
getNativeTaskStore: () => TaskStore | undefined;
|
|
12
22
|
onNativeTaskCreated?: (taskStore: TaskStore) => void;
|
|
13
23
|
onNativeTasksPruned?: (taskStore: TaskStore) => Promise<void> | void;
|
|
24
|
+
/** Called when a detection window opens. */
|
|
25
|
+
onDetectionStarted?: () => void;
|
|
26
|
+
/** Called when a detection window closes (provider found or probe timed out). */
|
|
27
|
+
onDetectionSettled?: () => void;
|
|
14
28
|
debug?: (...args: unknown[]) => void;
|
|
15
29
|
}
|
|
16
30
|
|
|
@@ -29,48 +43,51 @@ export function createTaskRuntimeBridge(options: TaskRuntimeBridgeOptions): Task
|
|
|
29
43
|
getNativeTaskStore,
|
|
30
44
|
onNativeTaskCreated,
|
|
31
45
|
onNativeTasksPruned,
|
|
46
|
+
onDetectionStarted,
|
|
47
|
+
onDetectionSettled,
|
|
32
48
|
debug,
|
|
33
49
|
} = options;
|
|
50
|
+
let detectionEpoch = 0;
|
|
34
51
|
|
|
35
52
|
function checkTasksVersion() {
|
|
53
|
+
// Not rpcProbe: pi-loop's own native server also answers this ping, and a
|
|
54
|
+
// first-reply-wins probe would always settle on that self-reply. Keep the
|
|
55
|
+
// listener open for the whole window and skip self-replies so a slower
|
|
56
|
+
// external provider (pi-tasks) is still detected.
|
|
57
|
+
const epoch = ++detectionEpoch;
|
|
58
|
+
onDetectionStarted?.();
|
|
59
|
+
const settleCurrentDetection = () => {
|
|
60
|
+
if (epoch !== detectionEpoch) return;
|
|
61
|
+
onDetectionSettled?.();
|
|
62
|
+
};
|
|
36
63
|
const requestId = randomUUID();
|
|
37
64
|
const timer = setTimeout(() => {
|
|
38
65
|
unsub();
|
|
66
|
+
settleCurrentDetection();
|
|
39
67
|
}, 5000);
|
|
40
|
-
const unsub = pi.events.on(
|
|
68
|
+
const unsub = pi.events.on(replyChannel(TASKS_RPC.ping, requestId), (raw: unknown) => {
|
|
69
|
+
const reply = raw as RpcReply<PingReply> | undefined;
|
|
70
|
+
if (!reply?.success || !reply.data) return;
|
|
71
|
+
if (reply.data.provider === NATIVE_TASKS_PROVIDER) return;
|
|
72
|
+
if (reply.data.version === undefined) return;
|
|
41
73
|
unsub();
|
|
42
74
|
clearTimeout(timer);
|
|
43
|
-
|
|
44
|
-
|
|
75
|
+
setTasksAvailable(true);
|
|
76
|
+
settleCurrentDetection();
|
|
45
77
|
});
|
|
46
|
-
pi.events.emit(
|
|
78
|
+
pi.events.emit(TASKS_RPC.ping, { requestId });
|
|
47
79
|
}
|
|
48
80
|
|
|
49
81
|
async function autoCreateTask(entry: LoopEntry): Promise<string | undefined> {
|
|
50
82
|
if (!entry.autoTask) return undefined;
|
|
51
83
|
if (isTasksAvailable()) {
|
|
52
84
|
try {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const unsub = pi.events.on(`tasks:rpc:create:reply:${requestId}`, (raw: unknown) => {
|
|
60
|
-
unsub();
|
|
61
|
-
clearTimeout(timer);
|
|
62
|
-
const reply = raw as { success: boolean; data?: { id: string } };
|
|
63
|
-
if (reply.success && reply.data) resolve(reply.data.id);
|
|
64
|
-
else resolve(undefined);
|
|
65
|
-
});
|
|
66
|
-
pi.events.emit("tasks:rpc:create", {
|
|
67
|
-
requestId,
|
|
68
|
-
subject: entry.prompt.slice(0, 80),
|
|
69
|
-
description: `Auto-created from loop #${entry.id}`,
|
|
70
|
-
metadata: { loopId: entry.id, trigger: entry.trigger },
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
return taskId;
|
|
85
|
+
const reply = await rpcCall<CreateTaskReply>(pi.events, TASKS_RPC.create, {
|
|
86
|
+
subject: entry.prompt.slice(0, 80),
|
|
87
|
+
description: `Auto-created from loop #${entry.id}`,
|
|
88
|
+
metadata: { loopId: entry.id, trigger: entry.trigger },
|
|
89
|
+
}, 5000);
|
|
90
|
+
return reply.id;
|
|
74
91
|
} catch {
|
|
75
92
|
return undefined;
|
|
76
93
|
}
|
|
@@ -90,22 +107,11 @@ export function createTaskRuntimeBridge(options: TaskRuntimeBridgeOptions): Task
|
|
|
90
107
|
|
|
91
108
|
async function hasPendingTasks(): Promise<number> {
|
|
92
109
|
if (isTasksAvailable()) {
|
|
110
|
+
// -1 is this bridge's "unknown" sentinel, consumed by notification-runtime;
|
|
111
|
+
// the RPC layer itself rejects on failure.
|
|
93
112
|
try {
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
const timer = setTimeout(() => {
|
|
97
|
-
unsub();
|
|
98
|
-
resolve(-1);
|
|
99
|
-
}, 3000);
|
|
100
|
-
const unsub = pi.events.on(`tasks:rpc:pending:reply:${requestId}`, (raw: unknown) => {
|
|
101
|
-
unsub();
|
|
102
|
-
clearTimeout(timer);
|
|
103
|
-
const reply = raw as { success: boolean; data?: { pending: number } };
|
|
104
|
-
resolve(reply.success && reply.data ? reply.data.pending : -1);
|
|
105
|
-
});
|
|
106
|
-
pi.events.emit("tasks:rpc:pending", { requestId });
|
|
107
|
-
});
|
|
108
|
-
return count;
|
|
113
|
+
const reply = await rpcCall<PendingReply>(pi.events, TASKS_RPC.pending, {}, 3000);
|
|
114
|
+
return reply.pending;
|
|
109
115
|
} catch {
|
|
110
116
|
return -1;
|
|
111
117
|
}
|
|
@@ -117,20 +123,8 @@ export function createTaskRuntimeBridge(options: TaskRuntimeBridgeOptions): Task
|
|
|
117
123
|
async function cleanDoneTasks(): Promise<void> {
|
|
118
124
|
if (isTasksAvailable()) {
|
|
119
125
|
try {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const timer = setTimeout(() => {
|
|
123
|
-
unsub();
|
|
124
|
-
resolve();
|
|
125
|
-
}, 3000);
|
|
126
|
-
const unsub = pi.events.on(`tasks:rpc:clean:reply:${requestId}`, () => {
|
|
127
|
-
unsub();
|
|
128
|
-
clearTimeout(timer);
|
|
129
|
-
debug?.("tasks:rpc:clean — done tasks swept");
|
|
130
|
-
resolve();
|
|
131
|
-
});
|
|
132
|
-
pi.events.emit("tasks:rpc:clean", { requestId });
|
|
133
|
-
});
|
|
126
|
+
await rpcCall<CleanReply>(pi.events, TASKS_RPC.clean, {}, 3000);
|
|
127
|
+
debug?.(`${TASKS_RPC.clean} — done tasks swept`);
|
|
134
128
|
} catch {
|
|
135
129
|
// timeout or error, ignore
|
|
136
130
|
}
|
package/src/scheduler.ts
CHANGED
|
@@ -8,6 +8,9 @@ function computeNextFire(entry: LoopEntry): Date {
|
|
|
8
8
|
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid") {
|
|
9
9
|
return cronToNextFire(entry.trigger.type === "hybrid" ? entry.trigger.cron : entry.trigger.schedule);
|
|
10
10
|
}
|
|
11
|
+
if (entry.trigger.type === "dynamic") {
|
|
12
|
+
return new Date(entry.dynamic?.nextWakeAt ?? Date.now());
|
|
13
|
+
}
|
|
11
14
|
return new Date(Date.now() + 60000);
|
|
12
15
|
}
|
|
13
16
|
|
|
@@ -20,9 +23,19 @@ export class CronScheduler {
|
|
|
20
23
|
) {}
|
|
21
24
|
|
|
22
25
|
start(): void {
|
|
23
|
-
for (const
|
|
26
|
+
for (const storedEntry of this.store.list()) {
|
|
27
|
+
let entry = storedEntry;
|
|
24
28
|
if (entry.status !== "active") continue;
|
|
25
|
-
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid") {
|
|
29
|
+
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid" || entry.trigger.type === "dynamic") {
|
|
30
|
+
if (entry.trigger.type === "dynamic" && entry.dynamic?.awaitingUpdate && !this.fireTimes.has(entry.id)) {
|
|
31
|
+
entry = this.store.updateDynamic(entry.id, {
|
|
32
|
+
dynamic: {
|
|
33
|
+
awaitingUpdate: false,
|
|
34
|
+
nextWakeAt: undefined,
|
|
35
|
+
lastUpdatedAt: Date.now(),
|
|
36
|
+
},
|
|
37
|
+
}) ?? entry;
|
|
38
|
+
}
|
|
26
39
|
this.armTimer(entry);
|
|
27
40
|
}
|
|
28
41
|
}
|
|
@@ -33,7 +46,7 @@ export class CronScheduler {
|
|
|
33
46
|
}
|
|
34
47
|
|
|
35
48
|
add(entry: LoopEntry): void {
|
|
36
|
-
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid") {
|
|
49
|
+
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid" || entry.trigger.type === "dynamic") {
|
|
37
50
|
this.armTimer(entry);
|
|
38
51
|
}
|
|
39
52
|
}
|
|
@@ -47,12 +60,14 @@ export class CronScheduler {
|
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
private armTimer(entry: LoopEntry): void {
|
|
50
|
-
const _scheduleExpr = entry.trigger.type === "hybrid" ? entry.trigger.cron : (entry.trigger as { schedule: string }).schedule;
|
|
51
|
-
|
|
52
63
|
const nextFire = computeNextFire(entry);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
let jitter = 0;
|
|
65
|
+
if (entry.trigger.type === "cron" || entry.trigger.type === "hybrid") {
|
|
66
|
+
const scheduleExpr = entry.trigger.type === "hybrid" ? entry.trigger.cron : entry.trigger.schedule;
|
|
67
|
+
const minuteField = scheduleExpr.trim().split(/\s+/)[0] ?? "";
|
|
68
|
+
const minuteStep = minuteField.startsWith("*/") ? parseInt(minuteField.slice(2), 10) || 30 : 30;
|
|
69
|
+
jitter = computeJitter(entry.id, entry.recurring, minuteStep);
|
|
70
|
+
}
|
|
56
71
|
const fireTime = nextFire.getTime() + jitter;
|
|
57
72
|
|
|
58
73
|
if (fireTime > entry.expiresAt) {
|
|
@@ -73,6 +88,8 @@ export class CronScheduler {
|
|
|
73
88
|
continue;
|
|
74
89
|
}
|
|
75
90
|
|
|
91
|
+
if (entry.trigger.type === "dynamic" && entry.dynamic?.awaitingUpdate) continue;
|
|
92
|
+
|
|
76
93
|
if (filter && !filter(entry)) continue;
|
|
77
94
|
|
|
78
95
|
if (now >= entry.expiresAt) {
|
package/src/store.ts
CHANGED
|
@@ -2,12 +2,15 @@ import { homedir } from "node:os";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { type LoopReducerEvent, type LoopReducerState, reduceLoopState } from "./loop-reducer.js";
|
|
4
4
|
import { ReducerBackedStore } from "./reducer-backed-store.js";
|
|
5
|
-
import type { LoopEntry, LoopStoreData, Trigger } from "./types.js";
|
|
5
|
+
import type { DynamicLoopState, LoopDeletionTombstone, LoopDeletionTombstoneInput, LoopEntry, LoopStoreData, Trigger } from "./types.js";
|
|
6
6
|
|
|
7
7
|
const LOOPS_DIR = join(homedir(), ".pi", "loops");
|
|
8
8
|
const MAX_LOOPS = 25;
|
|
9
|
+
const TOMBSTONE_TTL_MS = 10 * 60 * 1000;
|
|
9
10
|
|
|
10
11
|
export class LoopStore extends ReducerBackedStore<LoopEntry, LoopReducerState, LoopReducerEvent, LoopStoreData> {
|
|
12
|
+
private tombstones = new Map<string, LoopDeletionTombstone>();
|
|
13
|
+
|
|
11
14
|
constructor(listIdOrPath?: string) {
|
|
12
15
|
super(
|
|
13
16
|
{
|
|
@@ -22,7 +25,7 @@ export class LoopStore extends ReducerBackedStore<LoopEntry, LoopReducerState, L
|
|
|
22
25
|
);
|
|
23
26
|
}
|
|
24
27
|
|
|
25
|
-
create(trigger: Trigger, prompt: string, opts: { recurring: boolean; autoTask?: boolean; taskBacklog?: boolean; readOnly?: boolean; maxFires?: number }): LoopEntry {
|
|
28
|
+
create(trigger: Trigger, prompt: string, opts: { recurring: boolean; autoTask?: boolean; taskBacklog?: boolean; readOnly?: boolean; maxFires?: number; dynamic?: Partial<DynamicLoopState> }): LoopEntry {
|
|
26
29
|
return this.withLock(() => {
|
|
27
30
|
if (this.entries.size >= MAX_LOOPS) {
|
|
28
31
|
throw new Error(`Maximum of ${MAX_LOOPS} loops reached. Delete some before creating new ones.`);
|
|
@@ -41,6 +44,7 @@ export class LoopStore extends ReducerBackedStore<LoopEntry, LoopReducerState, L
|
|
|
41
44
|
taskBacklog: opts.taskBacklog,
|
|
42
45
|
readOnly: opts.readOnly,
|
|
43
46
|
maxFires: opts.maxFires,
|
|
47
|
+
dynamic: opts.dynamic,
|
|
44
48
|
},
|
|
45
49
|
});
|
|
46
50
|
return this.entries.get(String(this.nextId - 1))!;
|
|
@@ -75,6 +79,22 @@ export class LoopStore extends ReducerBackedStore<LoopEntry, LoopReducerState, L
|
|
|
75
79
|
entityId: id,
|
|
76
80
|
payload: { id },
|
|
77
81
|
});
|
|
82
|
+
if (entry.trigger.type === "dynamic" && entry.dynamic?.awaitingUpdate) {
|
|
83
|
+
this.applyReducerEvent({
|
|
84
|
+
type: "LOOP_DYNAMIC_UPDATED",
|
|
85
|
+
at: Date.now(),
|
|
86
|
+
source: "tool",
|
|
87
|
+
entityType: "loop",
|
|
88
|
+
entityId: id,
|
|
89
|
+
payload: {
|
|
90
|
+
id,
|
|
91
|
+
dynamic: {
|
|
92
|
+
awaitingUpdate: false,
|
|
93
|
+
lastUpdatedAt: Date.now(),
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
78
98
|
return this.entries.get(id);
|
|
79
99
|
});
|
|
80
100
|
}
|
|
@@ -119,6 +139,43 @@ export class LoopStore extends ReducerBackedStore<LoopEntry, LoopReducerState, L
|
|
|
119
139
|
});
|
|
120
140
|
}
|
|
121
141
|
|
|
142
|
+
updateDynamic(id: string, fields: { prompt?: string; dynamic: Partial<DynamicLoopState> }): LoopEntry | undefined {
|
|
143
|
+
return this.withLock(() => {
|
|
144
|
+
if (!this.entries.has(id)) return undefined;
|
|
145
|
+
this.applyReducerEvent({
|
|
146
|
+
type: "LOOP_DYNAMIC_UPDATED",
|
|
147
|
+
at: Date.now(),
|
|
148
|
+
source: "tool",
|
|
149
|
+
entityType: "loop",
|
|
150
|
+
entityId: id,
|
|
151
|
+
payload: { id, prompt: fields.prompt, dynamic: fields.dynamic },
|
|
152
|
+
});
|
|
153
|
+
return this.entries.get(id);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getDeletionTombstone(id: string): LoopDeletionTombstone | undefined {
|
|
158
|
+
const tombstone = this.tombstones.get(id);
|
|
159
|
+
if (!tombstone) return undefined;
|
|
160
|
+
if (Date.now() - tombstone.deletedAt <= TOMBSTONE_TTL_MS) return tombstone;
|
|
161
|
+
this.tombstones.delete(id);
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
recordDeletionTombstone(id: string, input: LoopDeletionTombstoneInput): LoopDeletionTombstone | undefined {
|
|
166
|
+
const entry = this.entries.get(id);
|
|
167
|
+
if (!entry) return undefined;
|
|
168
|
+
const tombstone: LoopDeletionTombstone = {
|
|
169
|
+
id,
|
|
170
|
+
reason: input.reason,
|
|
171
|
+
pendingCount: input.pendingCount,
|
|
172
|
+
deletedAt: Date.now(),
|
|
173
|
+
prompt: entry.prompt,
|
|
174
|
+
};
|
|
175
|
+
this.tombstones.set(id, tombstone);
|
|
176
|
+
return tombstone;
|
|
177
|
+
}
|
|
178
|
+
|
|
122
179
|
delete(id: string): boolean {
|
|
123
180
|
return this.withLock(() => {
|
|
124
181
|
if (!this.entries.has(id)) return false;
|