@trevonistrevon/pi-loop 0.4.11 → 0.5.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.
Files changed (87) hide show
  1. package/README.md +20 -9
  2. package/dist/commands/loop-command.d.ts +22 -0
  3. package/dist/commands/loop-command.js +148 -0
  4. package/dist/commands/tasks-command.d.ts +15 -0
  5. package/dist/commands/tasks-command.js +117 -0
  6. package/dist/coordinator.d.ts +35 -0
  7. package/dist/coordinator.js +56 -0
  8. package/dist/goal-coordinator.d.ts +22 -0
  9. package/dist/goal-coordinator.js +28 -0
  10. package/dist/goal-reducer.d.ts +89 -0
  11. package/dist/goal-reducer.js +181 -0
  12. package/dist/goal-store.d.ts +31 -0
  13. package/dist/goal-store.js +298 -0
  14. package/dist/goal-types.d.ts +82 -0
  15. package/dist/goal-types.js +1 -0
  16. package/dist/goal-verifier.d.ts +20 -0
  17. package/dist/goal-verifier.js +198 -0
  18. package/dist/index.js +130 -1087
  19. package/dist/loop-reducer.d.ts +63 -0
  20. package/dist/loop-reducer.js +67 -0
  21. package/dist/monitor-completion-coordinator.d.ts +10 -0
  22. package/dist/monitor-completion-coordinator.js +13 -0
  23. package/dist/monitor-manager.d.ts +2 -0
  24. package/dist/monitor-manager.js +107 -29
  25. package/dist/monitor-reducer.d.ts +82 -0
  26. package/dist/monitor-reducer.js +69 -0
  27. package/dist/notification-reducer.d.ts +81 -0
  28. package/dist/notification-reducer.js +65 -0
  29. package/dist/runtime/monitor-ondone-runtime.d.ts +13 -0
  30. package/dist/runtime/monitor-ondone-runtime.js +49 -0
  31. package/dist/runtime/notification-runtime.d.ts +34 -0
  32. package/dist/runtime/notification-runtime.js +152 -0
  33. package/dist/runtime/scope.d.ts +8 -0
  34. package/dist/runtime/scope.js +33 -0
  35. package/dist/runtime/session-runtime.d.ts +39 -0
  36. package/dist/runtime/session-runtime.js +110 -0
  37. package/dist/runtime/task-backlog-runtime.d.ts +36 -0
  38. package/dist/runtime/task-backlog-runtime.js +105 -0
  39. package/dist/runtime/task-rpc.d.ts +19 -0
  40. package/dist/runtime/task-rpc.js +118 -0
  41. package/dist/store.d.ts +7 -4
  42. package/dist/store.js +129 -49
  43. package/dist/task-backlog-coordinator.d.ts +12 -0
  44. package/dist/task-backlog-coordinator.js +22 -0
  45. package/dist/task-reducer.d.ts +66 -0
  46. package/dist/task-reducer.js +76 -0
  47. package/dist/task-store.d.ts +8 -4
  48. package/dist/task-store.js +102 -33
  49. package/dist/tools/loop-tools.d.ts +41 -0
  50. package/dist/tools/loop-tools.js +241 -0
  51. package/dist/tools/monitor-tools.d.ts +25 -0
  52. package/dist/tools/monitor-tools.js +110 -0
  53. package/dist/tools/native-task-tools.d.ts +15 -0
  54. package/dist/tools/native-task-tools.js +127 -0
  55. package/docs/architecture/goal-state-schema.md +505 -0
  56. package/docs/architecture/state-machine-migration.md +546 -0
  57. package/docs/architecture/state-machine-reducer-event-model.md +823 -0
  58. package/docs/architecture/state-machine-test-matrix.md +249 -0
  59. package/docs/architecture/state-machine-transition-map.md +436 -0
  60. package/package.json +1 -1
  61. package/src/commands/loop-command.ts +184 -0
  62. package/src/commands/tasks-command.ts +135 -0
  63. package/src/coordinator.ts +115 -0
  64. package/src/goal-coordinator.ts +58 -0
  65. package/src/goal-reducer.ts +280 -0
  66. package/src/goal-store.ts +315 -0
  67. package/src/goal-types.ts +104 -0
  68. package/src/goal-verifier.ts +241 -0
  69. package/src/index.ts +134 -1147
  70. package/src/loop-reducer.ts +148 -0
  71. package/src/monitor-completion-coordinator.ts +24 -0
  72. package/src/monitor-manager.ts +115 -27
  73. package/src/monitor-reducer.ts +166 -0
  74. package/src/notification-reducer.ts +155 -0
  75. package/src/runtime/monitor-ondone-runtime.ts +75 -0
  76. package/src/runtime/notification-runtime.ts +212 -0
  77. package/src/runtime/scope.ts +37 -0
  78. package/src/runtime/session-runtime.ts +168 -0
  79. package/src/runtime/task-backlog-runtime.ts +163 -0
  80. package/src/runtime/task-rpc.ts +150 -0
  81. package/src/store.ts +132 -50
  82. package/src/task-backlog-coordinator.ts +32 -0
  83. package/src/task-reducer.ts +152 -0
  84. package/src/task-store.ts +103 -31
  85. package/src/tools/loop-tools.ts +304 -0
  86. package/src/tools/monitor-tools.ts +145 -0
  87. package/src/tools/native-task-tools.ts +144 -0
@@ -0,0 +1,241 @@
1
+ import type { ReducerEffect, ReducerEvent } from "./coordinator.js";
2
+ import type { GoalEntry, GoalProgressSnapshot } from "./goal-types.js";
3
+ import type { LoopReducerState } from "./loop-reducer.js";
4
+ import type { MonitorReducerState } from "./monitor-reducer.js";
5
+ import type { TaskReducerState } from "./task-reducer.js";
6
+ import type { TaskEntry } from "./task-types.js";
7
+ import type { LoopEntry, MonitorEntry } from "./types.js";
8
+
9
+ export interface GoalVerifierInput {
10
+ goal: GoalEntry;
11
+ taskState: TaskReducerState;
12
+ loopState: LoopReducerState;
13
+ monitorState: MonitorReducerState;
14
+ at: number;
15
+ }
16
+
17
+ export interface GoalVerifierResult {
18
+ progress: GoalProgressSnapshot;
19
+ verdict: "passed" | "failed" | "blocked";
20
+ reason: string;
21
+ effects: ReducerEffect[];
22
+ }
23
+
24
+ function uniqueById<T extends { id: string }>(items: T[]): T[] {
25
+ const seen = new Set<string>();
26
+ const result: T[] = [];
27
+ for (const item of items) {
28
+ if (seen.has(item.id)) continue;
29
+ seen.add(item.id);
30
+ result.push(item);
31
+ }
32
+ return result;
33
+ }
34
+
35
+ function selectTasks(goal: GoalEntry, taskState: TaskReducerState): TaskEntry[] {
36
+ const selected: TaskEntry[] = [];
37
+ for (const id of goal.scope.taskIds ?? []) {
38
+ const task = taskState.tasksById[id];
39
+ if (task) selected.push(task);
40
+ }
41
+ for (const prefix of goal.scope.subjectPrefixes ?? []) {
42
+ for (const task of Object.values(taskState.tasksById)) {
43
+ if (task.subject.startsWith(prefix)) selected.push(task);
44
+ }
45
+ }
46
+ return uniqueById(selected);
47
+ }
48
+
49
+ function selectLoops(goal: GoalEntry, loopState: LoopReducerState): LoopEntry[] {
50
+ const selected: LoopEntry[] = [];
51
+ for (const id of goal.scope.loopIds ?? []) {
52
+ const loop = loopState.loopsById[id];
53
+ if (loop) selected.push(loop);
54
+ }
55
+ return uniqueById(selected);
56
+ }
57
+
58
+ function selectMonitors(goal: GoalEntry, monitorState: MonitorReducerState): MonitorEntry[] {
59
+ const selected: MonitorEntry[] = [];
60
+ for (const id of goal.scope.monitorIds ?? []) {
61
+ const monitor = monitorState.monitorsById[id];
62
+ if (monitor) selected.push(monitor);
63
+ }
64
+ return uniqueById(selected);
65
+ }
66
+
67
+ export function projectGoalProgress(
68
+ goal: GoalEntry,
69
+ taskState: TaskReducerState,
70
+ loopState: LoopReducerState,
71
+ monitorState: MonitorReducerState,
72
+ ): GoalProgressSnapshot {
73
+ const tasks = selectTasks(goal, taskState);
74
+ const loops = selectLoops(goal, loopState);
75
+ const monitors = selectMonitors(goal, monitorState);
76
+
77
+ const timestamps: number[] = [];
78
+ for (const task of tasks) timestamps.push(task.updatedAt);
79
+ for (const loop of loops) timestamps.push(loop.updatedAt);
80
+ for (const monitor of monitors) timestamps.push(monitor.completedAt ?? monitor.startedAt);
81
+
82
+ return {
83
+ totalTasks: tasks.length,
84
+ pendingTasks: tasks.filter(task => task.status === "pending").length,
85
+ inProgressTasks: tasks.filter(task => task.status === "in_progress").length,
86
+ completedTasks: tasks.filter(task => task.status === "completed").length,
87
+ activeLoops: loops.filter(loop => loop.status === "active").length,
88
+ pausedLoops: loops.filter(loop => loop.status === "paused").length,
89
+ runningMonitors: monitors.filter(monitor => monitor.status === "running").length,
90
+ completedMonitors: monitors.filter(monitor => monitor.status === "completed").length,
91
+ erroredMonitors: monitors.filter(monitor => monitor.status === "error").length,
92
+ stoppedMonitors: monitors.filter(monitor => monitor.status === "stopped").length,
93
+ lastProgressAt: timestamps.length > 0 ? Math.max(...timestamps) : undefined,
94
+ };
95
+ }
96
+
97
+ function dispatchEffect(event: ReducerEvent): ReducerEffect<"DISPATCH_EVENT", { event: ReducerEvent }> {
98
+ return {
99
+ type: "DISPATCH_EVENT",
100
+ entityType: "goal",
101
+ entityId: event.entityId,
102
+ payload: { event },
103
+ };
104
+ }
105
+
106
+ function buildEffects(goal: GoalEntry, at: number, progress: GoalProgressSnapshot, resultType: string, reason: string) {
107
+ return [
108
+ dispatchEffect({
109
+ type: "GOAL_VERIFICATION_STARTED",
110
+ at,
111
+ source: "coordinator",
112
+ entityType: "goal",
113
+ entityId: goal.id,
114
+ payload: { id: goal.id },
115
+ }),
116
+ dispatchEffect({
117
+ type: "GOAL_PROGRESS_RECORDED",
118
+ at,
119
+ source: "coordinator",
120
+ entityType: "goal",
121
+ entityId: goal.id,
122
+ payload: { id: goal.id, progress },
123
+ }),
124
+ dispatchEffect({
125
+ type: resultType,
126
+ at,
127
+ source: "coordinator",
128
+ entityType: "goal",
129
+ entityId: goal.id,
130
+ payload: { id: goal.id, reason, progress },
131
+ }),
132
+ ];
133
+ }
134
+
135
+ export function verifyGoal(input: GoalVerifierInput): GoalVerifierResult {
136
+ const { goal, taskState, loopState, monitorState, at } = input;
137
+ const progress = projectGoalProgress(goal, taskState, loopState, monitorState);
138
+
139
+ if (goal.criteria.failure?.maxVerificationFailures !== undefined
140
+ && goal.verification.failures >= goal.criteria.failure.maxVerificationFailures) {
141
+ return {
142
+ progress,
143
+ verdict: "failed",
144
+ reason: "maximum verification failures reached",
145
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", "maximum verification failures reached"),
146
+ };
147
+ }
148
+
149
+ for (const monitorId of goal.criteria.failure?.anyMonitorIdsErrored ?? []) {
150
+ if (monitorState.monitorsById[monitorId]?.status === "error") {
151
+ return {
152
+ progress,
153
+ verdict: "failed",
154
+ reason: `monitor #${monitorId} errored`,
155
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", `monitor #${monitorId} errored`),
156
+ };
157
+ }
158
+ }
159
+
160
+ for (const taskId of goal.criteria.failure?.failIfTaskIdsDeleted ?? []) {
161
+ if (!taskState.tasksById[taskId]) {
162
+ return {
163
+ progress,
164
+ verdict: "failed",
165
+ reason: `task #${taskId} missing`,
166
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", `task #${taskId} missing`),
167
+ };
168
+ }
169
+ }
170
+
171
+ const requiredTasksDone = (goal.criteria.success.requiredTaskIds ?? [])
172
+ .every(taskId => taskState.tasksById[taskId]?.status === "completed");
173
+ const requiredMonitorsDone = (goal.criteria.success.requiredMonitorIdsCompleted ?? [])
174
+ .every(monitorId => monitorState.monitorsById[monitorId]?.status === "completed");
175
+ const requiredLoopsPresent = (goal.criteria.success.requiredLoopIdsPresent ?? [])
176
+ .every(loopId => Boolean(loopState.loopsById[loopId]));
177
+ const minCompletedTasksMet = progress.completedTasks >= (goal.criteria.success.minCompletedTasks ?? 0);
178
+ const noPendingWork = !goal.criteria.success.requireNoPendingTasksInScope
179
+ || (progress.pendingTasks === 0 && progress.inProgressTasks === 0);
180
+ const latestVerificationPass = !goal.criteria.success.requireLatestVerificationPass
181
+ || goal.verificationStatus === "verified";
182
+
183
+ const success = requiredTasksDone
184
+ && requiredMonitorsDone
185
+ && requiredLoopsPresent
186
+ && minCompletedTasksMet
187
+ && noPendingWork
188
+ && latestVerificationPass;
189
+
190
+ if (success) {
191
+ return {
192
+ progress,
193
+ verdict: "passed",
194
+ reason: "success criteria satisfied",
195
+ effects: buildEffects(goal, at, progress, "GOAL_VERIFICATION_PASSED", "success criteria satisfied"),
196
+ };
197
+ }
198
+
199
+ if (goal.criteria.blocked?.blockedIfRequiredLoopMissing && !requiredLoopsPresent) {
200
+ return {
201
+ progress,
202
+ verdict: "blocked",
203
+ reason: "required loop missing",
204
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "required loop missing"),
205
+ };
206
+ }
207
+
208
+ if (
209
+ goal.criteria.blocked?.blockedIfNoScopedProgressSinceMs !== undefined
210
+ && progress.lastProgressAt !== undefined
211
+ && at - progress.lastProgressAt >= goal.criteria.blocked.blockedIfNoScopedProgressSinceMs
212
+ ) {
213
+ return {
214
+ progress,
215
+ verdict: "blocked",
216
+ reason: "no scoped progress within configured interval",
217
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "no scoped progress within configured interval"),
218
+ };
219
+ }
220
+
221
+ if (
222
+ goal.criteria.blocked?.blockedIfAllTasksCompletedButVerificationFails
223
+ && progress.totalTasks > 0
224
+ && progress.pendingTasks === 0
225
+ && progress.inProgressTasks === 0
226
+ ) {
227
+ return {
228
+ progress,
229
+ verdict: "blocked",
230
+ reason: "all scoped tasks completed but verification has not passed",
231
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "all scoped tasks completed but verification has not passed"),
232
+ };
233
+ }
234
+
235
+ return {
236
+ progress,
237
+ verdict: "failed",
238
+ reason: "success criteria not yet satisfied",
239
+ effects: buildEffects(goal, at, progress, "GOAL_VERIFICATION_FAILED", "success criteria not yet satisfied"),
240
+ };
241
+ }