cool-workflow 0.1.78

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 (193) hide show
  1. package/.claude-plugin/plugin.json +20 -0
  2. package/.codex-plugin/mcp.json +10 -0
  3. package/.codex-plugin/plugin.json +38 -0
  4. package/.mcp.json +10 -0
  5. package/LICENSE +24 -0
  6. package/README.md +638 -0
  7. package/apps/architecture-review/app.json +51 -0
  8. package/apps/architecture-review/workflow.js +116 -0
  9. package/apps/end-to-end-golden-path/app.json +30 -0
  10. package/apps/end-to-end-golden-path/workflow.js +33 -0
  11. package/apps/pr-review-fix-ci/app.json +59 -0
  12. package/apps/pr-review-fix-ci/workflow.js +90 -0
  13. package/apps/release-cut/app.json +54 -0
  14. package/apps/release-cut/workflow.js +82 -0
  15. package/apps/research-synthesis/app.json +50 -0
  16. package/apps/research-synthesis/workflow.js +76 -0
  17. package/apps/workflow-app-framework-demo/app.json +29 -0
  18. package/apps/workflow-app-framework-demo/workflow.js +44 -0
  19. package/dist/agent-config.js +223 -0
  20. package/dist/candidate-scoring.js +715 -0
  21. package/dist/capability-core.js +630 -0
  22. package/dist/capability-dispatcher.js +86 -0
  23. package/dist/capability-registry.js +523 -0
  24. package/dist/cli.js +1276 -0
  25. package/dist/collaboration.js +727 -0
  26. package/dist/commit.js +570 -0
  27. package/dist/contract-migration.js +234 -0
  28. package/dist/coordinator.js +1163 -0
  29. package/dist/daemon.js +44 -0
  30. package/dist/dispatch.js +201 -0
  31. package/dist/drive.js +503 -0
  32. package/dist/error-feedback.js +415 -0
  33. package/dist/evidence-grounding.js +179 -0
  34. package/dist/evidence-reasoning.js +733 -0
  35. package/dist/execution-backend.js +1279 -0
  36. package/dist/harness.js +61 -0
  37. package/dist/mcp-server.js +1615 -0
  38. package/dist/multi-agent-eval.js +857 -0
  39. package/dist/multi-agent-host.js +764 -0
  40. package/dist/multi-agent-operator-ux.js +537 -0
  41. package/dist/multi-agent-trust.js +366 -0
  42. package/dist/multi-agent.js +1173 -0
  43. package/dist/node-snapshot.js +270 -0
  44. package/dist/observability.js +922 -0
  45. package/dist/operator-ux.js +971 -0
  46. package/dist/orchestrator/audit-operations.js +182 -0
  47. package/dist/orchestrator/candidate-operations.js +117 -0
  48. package/dist/orchestrator/cli-options.js +288 -0
  49. package/dist/orchestrator/collaboration-operations.js +86 -0
  50. package/dist/orchestrator/feedback-operations.js +81 -0
  51. package/dist/orchestrator/host-operations.js +78 -0
  52. package/dist/orchestrator/lifecycle-operations.js +462 -0
  53. package/dist/orchestrator/migration-operations.js +44 -0
  54. package/dist/orchestrator/multi-agent-operations.js +362 -0
  55. package/dist/orchestrator/report.js +369 -0
  56. package/dist/orchestrator/topology-operations.js +84 -0
  57. package/dist/orchestrator.js +874 -0
  58. package/dist/pipeline-contract.js +92 -0
  59. package/dist/pipeline-runner.js +285 -0
  60. package/dist/reclamation.js +882 -0
  61. package/dist/result-normalize.js +194 -0
  62. package/dist/run-export.js +64 -0
  63. package/dist/run-registry.js +1347 -0
  64. package/dist/run-state-schema.js +67 -0
  65. package/dist/sandbox-profile.js +471 -0
  66. package/dist/scheduler.js +266 -0
  67. package/dist/scheduling.js +184 -0
  68. package/dist/schema-validate.js +98 -0
  69. package/dist/state-explosion.js +1213 -0
  70. package/dist/state-migrations.js +463 -0
  71. package/dist/state-node.js +301 -0
  72. package/dist/state.js +308 -0
  73. package/dist/telemetry-attestation.js +156 -0
  74. package/dist/telemetry-ledger.js +145 -0
  75. package/dist/topology.js +527 -0
  76. package/dist/triggers.js +159 -0
  77. package/dist/trust-audit.js +475 -0
  78. package/dist/types/blackboard.js +2 -0
  79. package/dist/types/boundary.js +29 -0
  80. package/dist/types/candidate.js +2 -0
  81. package/dist/types/collaboration.js +2 -0
  82. package/dist/types/core.js +2 -0
  83. package/dist/types/drive.js +10 -0
  84. package/dist/types/error-feedback.js +2 -0
  85. package/dist/types/evidence-reasoning.js +2 -0
  86. package/dist/types/execution-backend.js +2 -0
  87. package/dist/types/multi-agent.js +2 -0
  88. package/dist/types/observability.js +2 -0
  89. package/dist/types/pipeline.js +2 -0
  90. package/dist/types/reclamation.js +8 -0
  91. package/dist/types/result.js +2 -0
  92. package/dist/types/run-registry.js +2 -0
  93. package/dist/types/run.js +2 -0
  94. package/dist/types/sandbox.js +2 -0
  95. package/dist/types/schedule.js +2 -0
  96. package/dist/types/state-node.js +2 -0
  97. package/dist/types/topology.js +2 -0
  98. package/dist/types/trust.js +2 -0
  99. package/dist/types/workbench.js +2 -0
  100. package/dist/types/worker.js +2 -0
  101. package/dist/types/workflow-app.js +2 -0
  102. package/dist/types.js +43 -0
  103. package/dist/verifier-registry.js +46 -0
  104. package/dist/verifier.js +78 -0
  105. package/dist/version.js +8 -0
  106. package/dist/workbench-host.js +172 -0
  107. package/dist/workbench.js +190 -0
  108. package/dist/worker-isolation.js +1028 -0
  109. package/dist/workflow-api.js +98 -0
  110. package/dist/workflow-app-framework.js +626 -0
  111. package/docs/agent-delegation-drive.7.md +190 -0
  112. package/docs/agent-framework.md +176 -0
  113. package/docs/candidate-scoring.7.md +106 -0
  114. package/docs/canonical-workflow-apps.7.md +137 -0
  115. package/docs/capability-topology-registry.7.md +168 -0
  116. package/docs/cli-mcp-parity.7.md +373 -0
  117. package/docs/contract-migration-tooling.7.md +123 -0
  118. package/docs/control-plane-scheduling.7.md +110 -0
  119. package/docs/coordinator-blackboard.7.md +183 -0
  120. package/docs/dogfood/architecture-review-cool-workflow.md +16 -0
  121. package/docs/dogfood-one-real-repo.7.md +168 -0
  122. package/docs/durable-state-and-locking.7.md +107 -0
  123. package/docs/end-to-end-golden-path.7.md +117 -0
  124. package/docs/error-feedback.7.md +153 -0
  125. package/docs/evidence-adoption-reasoning-chain.7.md +270 -0
  126. package/docs/execution-backends.7.md +300 -0
  127. package/docs/getting-started.md +99 -0
  128. package/docs/index.md +41 -0
  129. package/docs/mcp-app-surface.7.md +235 -0
  130. package/docs/multi-agent-cli-mcp-surface.7.md +265 -0
  131. package/docs/multi-agent-eval-replay-harness.7.md +302 -0
  132. package/docs/multi-agent-operator-ux.7.md +314 -0
  133. package/docs/multi-agent-runtime-core.7.md +231 -0
  134. package/docs/multi-agent-topologies.7.md +103 -0
  135. package/docs/multi-agent-trust-policy-audit.7.md +154 -0
  136. package/docs/node-snapshot-diff-replay.7.md +135 -0
  137. package/docs/observability-cost-accounting.7.md +194 -0
  138. package/docs/operator-ux.7.md +180 -0
  139. package/docs/pipeline-runner.7.md +136 -0
  140. package/docs/project-index.md +261 -0
  141. package/docs/real-execution-backends.7.md +142 -0
  142. package/docs/release-and-migration.7.md +280 -0
  143. package/docs/release-tooling.7.md +159 -0
  144. package/docs/routines.md +48 -0
  145. package/docs/run-registry-control-plane.7.md +312 -0
  146. package/docs/run-retention-reclamation.7.md +191 -0
  147. package/docs/sandbox-profiles.7.md +137 -0
  148. package/docs/scheduled-tasks.md +80 -0
  149. package/docs/security-trust-hardening.7.md +117 -0
  150. package/docs/state-explosion-management.7.md +264 -0
  151. package/docs/state-node.7.md +96 -0
  152. package/docs/team-collaboration.7.md +207 -0
  153. package/docs/unix-principles.md +192 -0
  154. package/docs/verifier-gated-commit.7.md +140 -0
  155. package/docs/web-desktop-workbench.7.md +215 -0
  156. package/docs/worker-isolation.7.md +167 -0
  157. package/docs/workflow-app-framework.7.md +274 -0
  158. package/manifest/README.md +43 -0
  159. package/manifest/plugin.manifest.json +316 -0
  160. package/manifest/pricing.policy.json +14 -0
  161. package/package.json +79 -0
  162. package/scripts/agents/claude-p-agent.js +104 -0
  163. package/scripts/agents/claude-p-agent.sh +9 -0
  164. package/scripts/agents/cw-attest-keygen.js +55 -0
  165. package/scripts/agents/cw-attest-wrap.js +143 -0
  166. package/scripts/block-unapproved-tag.sh +39 -0
  167. package/scripts/bump-version.js +249 -0
  168. package/scripts/canonical-apps.js +171 -0
  169. package/scripts/cw.js +4 -0
  170. package/scripts/dist-drift-check.js +79 -0
  171. package/scripts/dogfood-architecture-review.js +237 -0
  172. package/scripts/dogfood-release.js +624 -0
  173. package/scripts/forward-ref-docs.js +73 -0
  174. package/scripts/gen-manifests.js +232 -0
  175. package/scripts/golden-path.js +300 -0
  176. package/scripts/mcp-server.js +4 -0
  177. package/scripts/new-feature.js +121 -0
  178. package/scripts/parity-check.js +213 -0
  179. package/scripts/release-check.js +118 -0
  180. package/scripts/release-flow.js +272 -0
  181. package/scripts/release-gate.sh +85 -0
  182. package/scripts/sync-project-index.js +387 -0
  183. package/scripts/validate-run-state-schema.js +126 -0
  184. package/scripts/verify-container-selfref.js +64 -0
  185. package/scripts/version-sync-check.js +237 -0
  186. package/skills/cool-workflow/SKILL.md +162 -0
  187. package/skills/cool-workflow/references/commands.md +282 -0
  188. package/tsconfig.json +16 -0
  189. package/ui/workbench/app.css +76 -0
  190. package/ui/workbench/app.js +159 -0
  191. package/ui/workbench/index.html +32 -0
  192. package/workflows/architecture-review.workflow.js +84 -0
  193. package/workflows/research-synthesis.workflow.js +47 -0
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Scheduler = void 0;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const state_1 = require("./state");
10
+ const DEFAULT_TTL_DAYS = 7;
11
+ class Scheduler {
12
+ cwd;
13
+ storePath;
14
+ constructor(cwd = process.cwd()) {
15
+ this.cwd = node_path_1.default.resolve(cwd);
16
+ this.storePath = node_path_1.default.join(this.cwd, ".cw", "schedules", "tasks.json");
17
+ }
18
+ create(options) {
19
+ const kind = normalizeKind(options.kind);
20
+ const now = new Date();
21
+ const intervalMinutes = numberOption(options.intervalMinutes || options.interval);
22
+ const cron = stringOption(options.cron);
23
+ const delayMinutes = numberOption(options.delayMinutes || options.delay);
24
+ const jitterSeconds = numberOption(options.jitterSeconds) ?? 0;
25
+ const nextRunAt = computeInitialNextRunAt({ kind, now, intervalMinutes, cron, delayMinutes, jitterSeconds });
26
+ const ttlDays = numberOption(options.ttlDays) ?? DEFAULT_TTL_DAYS;
27
+ const task = {
28
+ id: createScheduleId(kind),
29
+ kind,
30
+ status: "active",
31
+ createdAt: now.toISOString(),
32
+ updatedAt: now.toISOString(),
33
+ nextRunAt: nextRunAt.toISOString(),
34
+ expiresAt: new Date(now.getTime() + ttlDays * 24 * 60 * 60 * 1000).toISOString(),
35
+ prompt: requiredString(options.prompt, "prompt"),
36
+ workflowId: stringOption(options.workflowId),
37
+ runId: stringOption(options.runId),
38
+ sessionId: stringOption(options.sessionId),
39
+ intervalMinutes,
40
+ cron,
41
+ jitterSeconds,
42
+ maxRuns: numberOption(options.maxRuns),
43
+ runCount: 0
44
+ };
45
+ const store = this.load();
46
+ store.tasks.push(task);
47
+ this.save(store);
48
+ return task;
49
+ }
50
+ list(status) {
51
+ const store = this.load();
52
+ return status ? store.tasks.filter((task) => task.status === status) : store.tasks;
53
+ }
54
+ delete(id) {
55
+ const store = this.load();
56
+ const before = store.tasks.length;
57
+ store.tasks = store.tasks.filter((task) => task.id !== id);
58
+ this.save(store);
59
+ return { deleted: store.tasks.length !== before, id };
60
+ }
61
+ due(now = new Date()) {
62
+ const store = this.load();
63
+ let changed = false;
64
+ for (const task of store.tasks) {
65
+ if (task.status === "active" && new Date(task.expiresAt).getTime() <= now.getTime()) {
66
+ task.status = "expired";
67
+ task.updatedAt = now.toISOString();
68
+ changed = true;
69
+ }
70
+ }
71
+ if (changed)
72
+ this.save(store);
73
+ const dueTasks = store.tasks.filter((task) => task.status === "active" && new Date(task.nextRunAt).getTime() <= now.getTime());
74
+ if (dueTasks.length) {
75
+ for (const task of dueTasks) {
76
+ const alreadyRecorded = task.lastDueAt && new Date(task.lastDueAt).getTime() >= new Date(task.nextRunAt).getTime();
77
+ if (alreadyRecorded)
78
+ continue;
79
+ task.lastDueAt = now.toISOString();
80
+ store.history.push(createHistoryRecord(task, "due", this.cwd, now));
81
+ changed = true;
82
+ }
83
+ }
84
+ if (changed)
85
+ this.save(store);
86
+ return dueTasks;
87
+ }
88
+ complete(id, options = {}) {
89
+ const store = this.load();
90
+ const task = store.tasks.find((candidate) => candidate.id === id);
91
+ if (!task)
92
+ throw new Error(`Scheduled task not found: ${id}`);
93
+ const now = new Date();
94
+ task.runCount += 1;
95
+ task.lastRunAt = now.toISOString();
96
+ task.updatedAt = now.toISOString();
97
+ const maxRuns = numberOption(options.maxRuns) ?? task.maxRuns;
98
+ if (maxRuns !== undefined)
99
+ task.maxRuns = maxRuns;
100
+ if (task.kind === "reminder" || (task.maxRuns !== undefined && task.runCount >= task.maxRuns)) {
101
+ task.status = "completed";
102
+ }
103
+ else {
104
+ task.nextRunAt = computeNextRunAt(task, now).toISOString();
105
+ }
106
+ this.save(store);
107
+ return task;
108
+ }
109
+ pause(id) {
110
+ return this.setStatus(id, "paused");
111
+ }
112
+ resume(id) {
113
+ const store = this.load();
114
+ const task = findTask(store, id);
115
+ const now = new Date();
116
+ task.status = "active";
117
+ task.updatedAt = now.toISOString();
118
+ if (new Date(task.nextRunAt).getTime() <= now.getTime()) {
119
+ task.nextRunAt = computeNextRunAt(task, now).toISOString();
120
+ }
121
+ this.save(store);
122
+ return task;
123
+ }
124
+ runNow(id) {
125
+ const store = this.load();
126
+ const task = findTask(store, id);
127
+ const now = new Date();
128
+ task.lastDueAt = now.toISOString();
129
+ task.updatedAt = now.toISOString();
130
+ const record = createHistoryRecord(task, "started", this.cwd, now);
131
+ store.history.push(record);
132
+ this.save(store);
133
+ return record;
134
+ }
135
+ history(id) {
136
+ const store = this.load();
137
+ return id ? store.history.filter((record) => record.scheduleId === id) : store.history;
138
+ }
139
+ setStatus(id, status) {
140
+ const store = this.load();
141
+ const task = findTask(store, id);
142
+ task.status = status;
143
+ task.updatedAt = new Date().toISOString();
144
+ this.save(store);
145
+ return task;
146
+ }
147
+ load() {
148
+ if (!node_fs_1.default.existsSync(this.storePath))
149
+ return { schemaVersion: 1, tasks: [], history: [] };
150
+ const value = (0, state_1.readJson)(this.storePath);
151
+ return {
152
+ schemaVersion: 1,
153
+ tasks: Array.isArray(value.tasks) ? value.tasks : [],
154
+ history: Array.isArray(value.history) ? value.history : []
155
+ };
156
+ }
157
+ save(store) {
158
+ // Authoritative scheduler store — atomic + durable (v0.1.40). writeJson is now
159
+ // always atomic (temp → rename), so a crash mid-write can never truncate it.
160
+ (0, state_1.writeJson)(this.storePath, store, { durable: true });
161
+ }
162
+ }
163
+ exports.Scheduler = Scheduler;
164
+ function findTask(store, id) {
165
+ const task = store.tasks.find((candidate) => candidate.id === id);
166
+ if (!task)
167
+ throw new Error(`Scheduled task not found: ${id}`);
168
+ return task;
169
+ }
170
+ function createHistoryRecord(task, status, cwd, now) {
171
+ return {
172
+ id: createScheduleRunId(task.kind),
173
+ scheduleId: task.id,
174
+ status,
175
+ dueAt: now.toISOString(),
176
+ startedAt: status === "started" ? now.toISOString() : undefined,
177
+ prompt: task.prompt,
178
+ cwd,
179
+ workflowId: task.workflowId,
180
+ runId: task.runId
181
+ };
182
+ }
183
+ function normalizeKind(value) {
184
+ const kind = String(value || "loop");
185
+ if (["loop", "cron", "reminder"].includes(kind))
186
+ return kind;
187
+ throw new Error(`Unsupported schedule kind: ${kind}`);
188
+ }
189
+ function computeInitialNextRunAt(options) {
190
+ if (options.kind === "reminder") {
191
+ return addJitter(new Date(options.now.getTime() + (options.delayMinutes ?? options.intervalMinutes ?? 1) * 60 * 1000), options.jitterSeconds);
192
+ }
193
+ if (options.kind === "cron") {
194
+ if (!options.cron)
195
+ throw new Error("cron schedule requires --cron");
196
+ return addJitter(nextFromCron(options.cron, options.now), options.jitterSeconds);
197
+ }
198
+ return addJitter(new Date(options.now.getTime() + (options.intervalMinutes ?? 1) * 60 * 1000), options.jitterSeconds);
199
+ }
200
+ function computeNextRunAt(task, now) {
201
+ if (task.kind === "cron" && task.cron)
202
+ return addJitter(nextFromCron(task.cron, now), task.jitterSeconds);
203
+ return addJitter(new Date(now.getTime() + (task.intervalMinutes ?? 1) * 60 * 1000), task.jitterSeconds);
204
+ }
205
+ function nextFromCron(cron, now) {
206
+ const parts = cron.trim().split(/\s+/);
207
+ if (parts.length !== 5)
208
+ throw new Error("Only 5-field cron expressions are supported");
209
+ const [minuteExpr, hourExpr, dayExpr, monthExpr, weekdayExpr] = parts;
210
+ const cursor = new Date(now.getTime() + 60 * 1000);
211
+ cursor.setSeconds(0, 0);
212
+ for (let attempt = 0; attempt < 8 * 24 * 60; attempt += 1) {
213
+ if (matchesCron(cursor.getMinutes(), minuteExpr, 0, 59) &&
214
+ matchesCron(cursor.getHours(), hourExpr, 0, 23) &&
215
+ matchesCron(cursor.getDate(), dayExpr, 1, 31) &&
216
+ matchesCron(cursor.getMonth() + 1, monthExpr, 1, 12) &&
217
+ matchesCron(cursor.getDay(), weekdayExpr, 0, 6)) {
218
+ return cursor;
219
+ }
220
+ cursor.setMinutes(cursor.getMinutes() + 1);
221
+ }
222
+ throw new Error("Unable to resolve next cron run within 8 days");
223
+ }
224
+ function matchesCron(value, expr, min, max) {
225
+ if (expr === "*")
226
+ return true;
227
+ if (expr.startsWith("*/")) {
228
+ const step = Number(expr.slice(2));
229
+ return Number.isFinite(step) && step > 0 && value % step === 0;
230
+ }
231
+ return expr.split(",").some((part) => {
232
+ const parsed = Number(part);
233
+ return Number.isInteger(parsed) && parsed >= min && parsed <= max && parsed === value;
234
+ });
235
+ }
236
+ function addJitter(date, jitterSeconds) {
237
+ if (!jitterSeconds)
238
+ return date;
239
+ const offset = Math.floor(Math.random() * (jitterSeconds + 1)) * 1000;
240
+ return new Date(date.getTime() + offset);
241
+ }
242
+ function createScheduleId(kind) {
243
+ const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
244
+ return `${kind}-${stamp}-${Math.random().toString(36).slice(2, 8)}`;
245
+ }
246
+ function createScheduleRunId(kind) {
247
+ const stamp = new Date().toISOString().replace(/[-:]/g, "").replace(/\..+/, "Z");
248
+ return `run-${kind}-${stamp}-${Math.random().toString(36).slice(2, 8)}`;
249
+ }
250
+ function requiredString(value, name) {
251
+ const text = stringOption(value);
252
+ if (!text)
253
+ throw new Error(`Missing required ${name}`);
254
+ return text;
255
+ }
256
+ function stringOption(value) {
257
+ if (value === undefined || value === null || value === true)
258
+ return undefined;
259
+ return String(value);
260
+ }
261
+ function numberOption(value) {
262
+ if (value === undefined || value === null || value === true)
263
+ return undefined;
264
+ const parsed = Number(value);
265
+ return Number.isFinite(parsed) ? parsed : undefined;
266
+ }
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ // Control-Plane Scheduling (v0.1.37) — policy-as-data over the v0.1.28 Run
3
+ // Registry queue. The queue has ORDER; this PURE core adds the scheduling policy:
4
+ // priority+readiness selection, a hard concurrency ceiling, leases, retry with
5
+ // computed backoff, and a fail-closed park state.
6
+ //
7
+ // BSD discipline:
8
+ // - MECHANISM vs POLICY: these functions are mechanism; the SchedulingPolicy is
9
+ // data (concurrency, retry budget, backoff curve, lease TTL), kept out of the
10
+ // kernel and defaulting to conservative fail-closed values.
11
+ // - FAIL CLOSED [load-bearing]: a concurrency ceiling is NEVER exceeded; an entry
12
+ // at maxAttempts is PARKED and never re-selected (only `reset` recovers it) —
13
+ // the queue can never re-hand a failing entry forever.
14
+ // - DETERMINISTIC: every function takes an injected `now`; selection reuses the
15
+ // registry's compareQueue; backoff is a pure curve with NO randomness. The
16
+ // `plan` is read-only and replayable.
17
+ // - REUSE, don't fork: operates on the existing RunQueueEntry[] from the registry
18
+ // queue store; does not duplicate the queue file.
19
+ //
20
+ // See docs/control-plane-scheduling.7.md.
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.DEFAULT_SCHEDULING_POLICY = exports.SCHEDULING_SCHEMA_VERSION = void 0;
23
+ exports.normalizeSchedulingPolicy = normalizeSchedulingPolicy;
24
+ exports.backoffMs = backoffMs;
25
+ exports.planSchedule = planSchedule;
26
+ exports.applyLease = applyLease;
27
+ exports.retryOrPark = retryOrPark;
28
+ exports.reclaimExpired = reclaimExpired;
29
+ exports.leaseComplete = leaseComplete;
30
+ exports.leaseRelease = leaseRelease;
31
+ exports.resetEntry = resetEntry;
32
+ const run_registry_1 = require("./run-registry");
33
+ exports.SCHEDULING_SCHEMA_VERSION = 1;
34
+ /** Conservative fail-closed defaults: serial, bounded retries, exponential backoff. */
35
+ exports.DEFAULT_SCHEDULING_POLICY = {
36
+ schemaVersion: 1,
37
+ maxConcurrent: 1,
38
+ maxAttempts: 3,
39
+ leaseTtlMs: 300_000,
40
+ backoffBaseMs: 1_000,
41
+ backoffFactor: 2,
42
+ backoffCapMs: 60_000
43
+ };
44
+ function normalizeSchedulingPolicy(input) {
45
+ const base = exports.DEFAULT_SCHEDULING_POLICY;
46
+ const num = (value, fallback, min) => typeof value === "number" && Number.isFinite(value) && value >= min ? value : fallback;
47
+ return {
48
+ schemaVersion: 1,
49
+ maxConcurrent: num(input?.maxConcurrent, base.maxConcurrent, 1),
50
+ maxAttempts: num(input?.maxAttempts, base.maxAttempts, 1),
51
+ leaseTtlMs: num(input?.leaseTtlMs, base.leaseTtlMs, 1),
52
+ backoffBaseMs: num(input?.backoffBaseMs, base.backoffBaseMs, 0),
53
+ backoffFactor: num(input?.backoffFactor, base.backoffFactor, 1),
54
+ backoffCapMs: num(input?.backoffCapMs, base.backoffCapMs, 0)
55
+ };
56
+ }
57
+ /** Deterministic backoff: baseMs * factor^(attempts-1), capped. No jitter. */
58
+ function backoffMs(policy, attempts) {
59
+ const raw = policy.backoffBaseMs * Math.pow(policy.backoffFactor, Math.max(0, attempts - 1));
60
+ return Math.min(Math.round(raw), policy.backoffCapMs);
61
+ }
62
+ function addMs(now, ms) {
63
+ return new Date(new Date(now).getTime() + ms).toISOString();
64
+ }
65
+ function leaseActive(entry, now) {
66
+ return entry.status === "leased" && Boolean(entry.leaseExpiresAt) && entry.leaseExpiresAt > now;
67
+ }
68
+ function leaseExpired(entry, now) {
69
+ return entry.status === "leased" && (!entry.leaseExpiresAt || entry.leaseExpiresAt <= now);
70
+ }
71
+ /** Eligible = pending/ready, not parked/terminal/leased, and past any backoff. */
72
+ function eligible(entry, now) {
73
+ if (entry.status !== "pending" && entry.status !== "ready")
74
+ return false;
75
+ if (entry.nextEligibleAt && entry.nextEligibleAt > now)
76
+ return false;
77
+ return true;
78
+ }
79
+ /** Read-only lease plan for the current queue + policy + now. Pure: no mutation. */
80
+ function planSchedule(entries, policy, now) {
81
+ const sorted = [...entries].sort(run_registry_1.compareQueue);
82
+ const inFlight = sorted.filter((entry) => leaseActive(entry, now)).length;
83
+ const available = Math.max(0, policy.maxConcurrent - inFlight);
84
+ const leases = [];
85
+ const skipped = [];
86
+ for (const entry of sorted) {
87
+ if (leaseActive(entry, now))
88
+ continue; // counted in inFlight
89
+ if (entry.status === "parked") {
90
+ skipped.push({ id: entry.id, reason: "parked" });
91
+ continue;
92
+ }
93
+ if (entry.status === "drained" || entry.status === "cancelled") {
94
+ skipped.push({ id: entry.id, reason: "terminal" });
95
+ continue;
96
+ }
97
+ if (!eligible(entry, now)) {
98
+ skipped.push({ id: entry.id, reason: leaseExpired(entry, now) ? "leased" : "backoff" });
99
+ continue;
100
+ }
101
+ if (leases.length >= available) {
102
+ skipped.push({ id: entry.id, reason: "concurrency-ceiling" });
103
+ continue;
104
+ }
105
+ leases.push({
106
+ id: entry.id,
107
+ leaseId: `lease-${entry.id}-${(entry.attempts || 0) + 1}-${now.replace(/[^0-9]/g, "")}`,
108
+ leaseExpiresAt: addMs(now, policy.leaseTtlMs),
109
+ attempts: entry.attempts || 0,
110
+ priority: entry.priority
111
+ });
112
+ }
113
+ return { schemaVersion: 1, now, maxConcurrent: policy.maxConcurrent, inFlight, available, leases, skipped };
114
+ }
115
+ /** Apply the plan: mark the selected entries leased. Never exceeds the ceiling.
116
+ * Returns the new entries + the granted leases. */
117
+ function applyLease(entries, policy, now, limit) {
118
+ const plan = planSchedule(entries, policy, now);
119
+ const granted = typeof limit === "number" ? plan.leases.slice(0, Math.max(0, limit)) : plan.leases;
120
+ const byId = new Map(granted.map((lease) => [lease.id, lease]));
121
+ const next = entries.map((entry) => {
122
+ const lease = byId.get(entry.id);
123
+ if (!lease)
124
+ return entry;
125
+ return { ...entry, status: "leased", leaseId: lease.leaseId, leaseExpiresAt: lease.leaseExpiresAt };
126
+ });
127
+ return { entries: next, leases: granted };
128
+ }
129
+ /** A failed/expired attempt: increment attempts, then park (at budget) or set
130
+ * ready with backoff. Fail closed — parked is terminal until reset. */
131
+ function retryOrPark(entry, policy, now, reason) {
132
+ const attempts = (entry.attempts || 0) + 1;
133
+ const cleared = { ...entry, attempts, leaseId: undefined, leaseExpiresAt: undefined };
134
+ if (attempts >= policy.maxAttempts) {
135
+ return { ...cleared, status: "parked", parkedReason: `${reason} (attempt ${attempts}/${policy.maxAttempts})` };
136
+ }
137
+ return { ...cleared, status: "ready", nextEligibleAt: addMs(now, backoffMs(policy, attempts)) };
138
+ }
139
+ /** Reclaim expired leases (host died): each counts as one failed attempt. */
140
+ function reclaimExpired(entries, policy, now) {
141
+ const reclaimed = [];
142
+ const next = entries.map((entry) => {
143
+ if (!leaseExpired(entry, now))
144
+ return entry;
145
+ reclaimed.push(entry.id);
146
+ return retryOrPark(entry, policy, now, "lease expired (host did not complete)");
147
+ });
148
+ return { entries: next, reclaimed };
149
+ }
150
+ /** Complete a lease: terminal success. */
151
+ function leaseComplete(entries, leaseId, now) {
152
+ let matched = false;
153
+ const next = entries.map((entry) => {
154
+ if (entry.leaseId !== leaseId || entry.status !== "leased")
155
+ return entry;
156
+ matched = true;
157
+ return { ...entry, status: "drained", drainedAt: now, leaseId: undefined, leaseExpiresAt: undefined };
158
+ });
159
+ return { entries: next, matched };
160
+ }
161
+ /** Release a lease: failed -> attempt+backoff/park; otherwise back to ready. */
162
+ function leaseRelease(entries, leaseId, policy, now, options = {}) {
163
+ let matched = false;
164
+ const next = entries.map((entry) => {
165
+ if (entry.leaseId !== leaseId || entry.status !== "leased")
166
+ return entry;
167
+ matched = true;
168
+ if (options.failed)
169
+ return retryOrPark(entry, policy, now, options.reason || "released as failed");
170
+ return { ...entry, status: "ready", leaseId: undefined, leaseExpiresAt: undefined };
171
+ });
172
+ return { entries: next, matched };
173
+ }
174
+ /** Operator recovery: park -> ready, clearing attempts/backoff. The ONLY way back. */
175
+ function resetEntry(entries, id) {
176
+ let matched = false;
177
+ const next = entries.map((entry) => {
178
+ if (entry.id !== id || entry.status !== "parked")
179
+ return entry;
180
+ matched = true;
181
+ return { ...entry, status: "ready", attempts: 0, nextEligibleAt: undefined, parkedReason: undefined };
182
+ });
183
+ return { entries: next, matched };
184
+ }
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ // Minimal, dependency-free structural schema validation (Track 3).
3
+ //
4
+ // CW declares an optional output `schema` per task (WorkflowTaskDefinition.schema);
5
+ // this enforces it at result-acceptance time so CW only records structured output
6
+ // it has actually validated — the other half of "auditable" (Track 1 proves the
7
+ // telemetry is real; this proves the result SHAPE is what was declared).
8
+ //
9
+ // DELIBERATELY a subset of JSON Schema, NOT a full implementation: it adds NO
10
+ // runtime dependency (the portability red line — CI runs on node/npm/git only).
11
+ // Supported keywords cover structured-agent-output contracts:
12
+ // type (object|array|string|number|integer|boolean|null, or an array of them),
13
+ // enum, const, required, properties, additionalProperties (false), items.
14
+ // Unsupported keywords ($ref, allOf/anyOf/oneOf, pattern, formats, numeric
15
+ // bounds) are IGNORED — never silently "passed" as a constraint that wasn't
16
+ // checked; a schema relying on them simply isn't enforced for those keywords.
17
+ // (If full JSON Schema is needed later, swap this module's impl for ajv behind
18
+ // the same signature.)
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.validateAgainstSchema = validateAgainstSchema;
21
+ function typeOf(value) {
22
+ if (value === null)
23
+ return "null";
24
+ if (Array.isArray(value))
25
+ return "array";
26
+ return typeof value; // object | string | number | boolean | undefined | function
27
+ }
28
+ function matchesType(value, type) {
29
+ switch (type) {
30
+ case "object":
31
+ return value !== null && typeof value === "object" && !Array.isArray(value);
32
+ case "array":
33
+ return Array.isArray(value);
34
+ case "string":
35
+ return typeof value === "string";
36
+ case "number":
37
+ return typeof value === "number" && Number.isFinite(value);
38
+ case "integer":
39
+ return typeof value === "number" && Number.isInteger(value);
40
+ case "boolean":
41
+ return typeof value === "boolean";
42
+ case "null":
43
+ return value === null;
44
+ default:
45
+ return true; // unknown declared type ⇒ not enforced (never a false fail)
46
+ }
47
+ }
48
+ /** Validate `value` against `schema`. Returns a list of human-readable errors;
49
+ * empty ⇒ valid. Pure + deterministic; never throws on a malformed schema (an
50
+ * unparseable schema constraint is skipped, not treated as a failure). */
51
+ function validateAgainstSchema(value, schema, path = "$") {
52
+ const errors = [];
53
+ if (!schema || typeof schema !== "object")
54
+ return errors;
55
+ // type
56
+ if (schema.type !== undefined) {
57
+ const types = Array.isArray(schema.type) ? schema.type.map(String) : [String(schema.type)];
58
+ if (!types.some((t) => matchesType(value, t))) {
59
+ errors.push(`${path}: expected type ${types.join("|")}, got ${typeOf(value)}`);
60
+ return errors; // type mismatch ⇒ downstream keyword checks would be noise
61
+ }
62
+ }
63
+ // const / enum
64
+ if ("const" in schema && JSON.stringify(value) !== JSON.stringify(schema.const)) {
65
+ errors.push(`${path}: expected const ${JSON.stringify(schema.const)}`);
66
+ }
67
+ if (Array.isArray(schema.enum) && !schema.enum.some((option) => JSON.stringify(option) === JSON.stringify(value))) {
68
+ errors.push(`${path}: ${JSON.stringify(value)} is not one of ${JSON.stringify(schema.enum)}`);
69
+ }
70
+ // object: required, properties, additionalProperties:false
71
+ if (value !== null && typeof value === "object" && !Array.isArray(value)) {
72
+ const obj = value;
73
+ if (Array.isArray(schema.required)) {
74
+ for (const key of schema.required) {
75
+ if (!(String(key) in obj))
76
+ errors.push(`${path}: missing required property "${String(key)}"`);
77
+ }
78
+ }
79
+ const properties = (schema.properties && typeof schema.properties === "object" ? schema.properties : {});
80
+ for (const [key, subSchema] of Object.entries(properties)) {
81
+ if (key in obj)
82
+ errors.push(...validateAgainstSchema(obj[key], subSchema, `${path}.${key}`));
83
+ }
84
+ if (schema.additionalProperties === false) {
85
+ for (const key of Object.keys(obj)) {
86
+ if (!(key in properties))
87
+ errors.push(`${path}: additional property "${key}" is not allowed`);
88
+ }
89
+ }
90
+ }
91
+ // array: items (single schema applied to every element)
92
+ if (Array.isArray(value) && schema.items && typeof schema.items === "object" && !Array.isArray(schema.items)) {
93
+ value.forEach((element, index) => {
94
+ errors.push(...validateAgainstSchema(element, schema.items, `${path}[${index}]`));
95
+ });
96
+ }
97
+ return errors;
98
+ }