@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,198 @@
1
+ function uniqueById(items) {
2
+ const seen = new Set();
3
+ const result = [];
4
+ for (const item of items) {
5
+ if (seen.has(item.id))
6
+ continue;
7
+ seen.add(item.id);
8
+ result.push(item);
9
+ }
10
+ return result;
11
+ }
12
+ function selectTasks(goal, taskState) {
13
+ const selected = [];
14
+ for (const id of goal.scope.taskIds ?? []) {
15
+ const task = taskState.tasksById[id];
16
+ if (task)
17
+ selected.push(task);
18
+ }
19
+ for (const prefix of goal.scope.subjectPrefixes ?? []) {
20
+ for (const task of Object.values(taskState.tasksById)) {
21
+ if (task.subject.startsWith(prefix))
22
+ selected.push(task);
23
+ }
24
+ }
25
+ return uniqueById(selected);
26
+ }
27
+ function selectLoops(goal, loopState) {
28
+ const selected = [];
29
+ for (const id of goal.scope.loopIds ?? []) {
30
+ const loop = loopState.loopsById[id];
31
+ if (loop)
32
+ selected.push(loop);
33
+ }
34
+ return uniqueById(selected);
35
+ }
36
+ function selectMonitors(goal, monitorState) {
37
+ const selected = [];
38
+ for (const id of goal.scope.monitorIds ?? []) {
39
+ const monitor = monitorState.monitorsById[id];
40
+ if (monitor)
41
+ selected.push(monitor);
42
+ }
43
+ return uniqueById(selected);
44
+ }
45
+ export function projectGoalProgress(goal, taskState, loopState, monitorState) {
46
+ const tasks = selectTasks(goal, taskState);
47
+ const loops = selectLoops(goal, loopState);
48
+ const monitors = selectMonitors(goal, monitorState);
49
+ const timestamps = [];
50
+ for (const task of tasks)
51
+ timestamps.push(task.updatedAt);
52
+ for (const loop of loops)
53
+ timestamps.push(loop.updatedAt);
54
+ for (const monitor of monitors)
55
+ timestamps.push(monitor.completedAt ?? monitor.startedAt);
56
+ return {
57
+ totalTasks: tasks.length,
58
+ pendingTasks: tasks.filter(task => task.status === "pending").length,
59
+ inProgressTasks: tasks.filter(task => task.status === "in_progress").length,
60
+ completedTasks: tasks.filter(task => task.status === "completed").length,
61
+ activeLoops: loops.filter(loop => loop.status === "active").length,
62
+ pausedLoops: loops.filter(loop => loop.status === "paused").length,
63
+ runningMonitors: monitors.filter(monitor => monitor.status === "running").length,
64
+ completedMonitors: monitors.filter(monitor => monitor.status === "completed").length,
65
+ erroredMonitors: monitors.filter(monitor => monitor.status === "error").length,
66
+ stoppedMonitors: monitors.filter(monitor => monitor.status === "stopped").length,
67
+ lastProgressAt: timestamps.length > 0 ? Math.max(...timestamps) : undefined,
68
+ };
69
+ }
70
+ function dispatchEffect(event) {
71
+ return {
72
+ type: "DISPATCH_EVENT",
73
+ entityType: "goal",
74
+ entityId: event.entityId,
75
+ payload: { event },
76
+ };
77
+ }
78
+ function buildEffects(goal, at, progress, resultType, reason) {
79
+ return [
80
+ dispatchEffect({
81
+ type: "GOAL_VERIFICATION_STARTED",
82
+ at,
83
+ source: "coordinator",
84
+ entityType: "goal",
85
+ entityId: goal.id,
86
+ payload: { id: goal.id },
87
+ }),
88
+ dispatchEffect({
89
+ type: "GOAL_PROGRESS_RECORDED",
90
+ at,
91
+ source: "coordinator",
92
+ entityType: "goal",
93
+ entityId: goal.id,
94
+ payload: { id: goal.id, progress },
95
+ }),
96
+ dispatchEffect({
97
+ type: resultType,
98
+ at,
99
+ source: "coordinator",
100
+ entityType: "goal",
101
+ entityId: goal.id,
102
+ payload: { id: goal.id, reason, progress },
103
+ }),
104
+ ];
105
+ }
106
+ export function verifyGoal(input) {
107
+ const { goal, taskState, loopState, monitorState, at } = input;
108
+ const progress = projectGoalProgress(goal, taskState, loopState, monitorState);
109
+ if (goal.criteria.failure?.maxVerificationFailures !== undefined
110
+ && goal.verification.failures >= goal.criteria.failure.maxVerificationFailures) {
111
+ return {
112
+ progress,
113
+ verdict: "failed",
114
+ reason: "maximum verification failures reached",
115
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", "maximum verification failures reached"),
116
+ };
117
+ }
118
+ for (const monitorId of goal.criteria.failure?.anyMonitorIdsErrored ?? []) {
119
+ if (monitorState.monitorsById[monitorId]?.status === "error") {
120
+ return {
121
+ progress,
122
+ verdict: "failed",
123
+ reason: `monitor #${monitorId} errored`,
124
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", `monitor #${monitorId} errored`),
125
+ };
126
+ }
127
+ }
128
+ for (const taskId of goal.criteria.failure?.failIfTaskIdsDeleted ?? []) {
129
+ if (!taskState.tasksById[taskId]) {
130
+ return {
131
+ progress,
132
+ verdict: "failed",
133
+ reason: `task #${taskId} missing`,
134
+ effects: buildEffects(goal, at, progress, "GOAL_FAILED", `task #${taskId} missing`),
135
+ };
136
+ }
137
+ }
138
+ const requiredTasksDone = (goal.criteria.success.requiredTaskIds ?? [])
139
+ .every(taskId => taskState.tasksById[taskId]?.status === "completed");
140
+ const requiredMonitorsDone = (goal.criteria.success.requiredMonitorIdsCompleted ?? [])
141
+ .every(monitorId => monitorState.monitorsById[monitorId]?.status === "completed");
142
+ const requiredLoopsPresent = (goal.criteria.success.requiredLoopIdsPresent ?? [])
143
+ .every(loopId => Boolean(loopState.loopsById[loopId]));
144
+ const minCompletedTasksMet = progress.completedTasks >= (goal.criteria.success.minCompletedTasks ?? 0);
145
+ const noPendingWork = !goal.criteria.success.requireNoPendingTasksInScope
146
+ || (progress.pendingTasks === 0 && progress.inProgressTasks === 0);
147
+ const latestVerificationPass = !goal.criteria.success.requireLatestVerificationPass
148
+ || goal.verificationStatus === "verified";
149
+ const success = requiredTasksDone
150
+ && requiredMonitorsDone
151
+ && requiredLoopsPresent
152
+ && minCompletedTasksMet
153
+ && noPendingWork
154
+ && latestVerificationPass;
155
+ if (success) {
156
+ return {
157
+ progress,
158
+ verdict: "passed",
159
+ reason: "success criteria satisfied",
160
+ effects: buildEffects(goal, at, progress, "GOAL_VERIFICATION_PASSED", "success criteria satisfied"),
161
+ };
162
+ }
163
+ if (goal.criteria.blocked?.blockedIfRequiredLoopMissing && !requiredLoopsPresent) {
164
+ return {
165
+ progress,
166
+ verdict: "blocked",
167
+ reason: "required loop missing",
168
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "required loop missing"),
169
+ };
170
+ }
171
+ if (goal.criteria.blocked?.blockedIfNoScopedProgressSinceMs !== undefined
172
+ && progress.lastProgressAt !== undefined
173
+ && at - progress.lastProgressAt >= goal.criteria.blocked.blockedIfNoScopedProgressSinceMs) {
174
+ return {
175
+ progress,
176
+ verdict: "blocked",
177
+ reason: "no scoped progress within configured interval",
178
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "no scoped progress within configured interval"),
179
+ };
180
+ }
181
+ if (goal.criteria.blocked?.blockedIfAllTasksCompletedButVerificationFails
182
+ && progress.totalTasks > 0
183
+ && progress.pendingTasks === 0
184
+ && progress.inProgressTasks === 0) {
185
+ return {
186
+ progress,
187
+ verdict: "blocked",
188
+ reason: "all scoped tasks completed but verification has not passed",
189
+ effects: buildEffects(goal, at, progress, "GOAL_BLOCKED", "all scoped tasks completed but verification has not passed"),
190
+ };
191
+ }
192
+ return {
193
+ progress,
194
+ verdict: "failed",
195
+ reason: "success criteria not yet satisfied",
196
+ effects: buildEffects(goal, at, progress, "GOAL_VERIFICATION_FAILED", "success criteria not yet satisfied"),
197
+ };
198
+ }