@treeseed/core 0.4.10 → 0.4.11

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 (55) hide show
  1. package/dist/api/auth/rbac.d.ts +2 -2
  2. package/dist/api/auth/rbac.js +2 -1
  3. package/dist/components/site/RouteNotFound.astro +25 -0
  4. package/dist/content-config.d.ts +1 -0
  5. package/dist/content.d.ts +1 -0
  6. package/dist/content.js +177 -1
  7. package/dist/dev.d.ts +7 -2
  8. package/dist/dev.js +59 -1
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.js +9 -1
  11. package/dist/middleware/editorial-preview.d.ts +26 -0
  12. package/dist/middleware/editorial-preview.js +37 -0
  13. package/dist/middleware/starlightRouteData.js +15 -4
  14. package/dist/pages/[slug].astro +12 -10
  15. package/dist/pages/agents/[slug].astro +28 -21
  16. package/dist/pages/books/[slug].astro +19 -12
  17. package/dist/pages/feed.xml.js +6 -4
  18. package/dist/pages/index.astro +43 -14
  19. package/dist/pages/notes/[slug].astro +19 -12
  20. package/dist/pages/objectives/[slug].astro +30 -23
  21. package/dist/pages/people/[slug].astro +28 -21
  22. package/dist/pages/questions/[slug].astro +30 -23
  23. package/dist/scripts/build-dist.js +6 -1
  24. package/dist/scripts/dev-platform.js +9 -1
  25. package/dist/services/agents.d.ts +22 -0
  26. package/dist/services/agents.js +29 -0
  27. package/dist/services/index.d.ts +3 -0
  28. package/dist/services/index.js +11 -0
  29. package/dist/services/manager.d.ts +247 -0
  30. package/dist/services/manager.js +1129 -0
  31. package/dist/services/remote-runner.d.ts +7 -0
  32. package/dist/services/remote-runner.js +6 -0
  33. package/dist/services/workday-content.d.ts +53 -0
  34. package/dist/services/workday-content.js +190 -0
  35. package/dist/services/workday-report.d.ts +160 -2
  36. package/dist/services/workday-report.js +3 -26
  37. package/dist/services/workday-start.d.ts +170 -1
  38. package/dist/services/workday-start.js +3 -7
  39. package/dist/services/worker-pool-scaler.d.ts +27 -0
  40. package/dist/services/worker-pool-scaler.js +109 -0
  41. package/dist/services/worker.d.ts +7 -0
  42. package/dist/services/worker.js +3 -0
  43. package/dist/site.js +43 -27
  44. package/dist/templates.d.ts +98 -0
  45. package/dist/templates.js +170 -0
  46. package/dist/tenant/runtime-config.d.ts +4 -0
  47. package/dist/tenant/runtime-config.js +34 -1
  48. package/dist/utils/hub-content.js +35 -0
  49. package/dist/utils/published-content.js +60 -0
  50. package/dist/utils/site-models.d.ts +6 -0
  51. package/dist/utils/site-models.js +16 -0
  52. package/dist/utils/starlight-nav.js +50 -0
  53. package/package.json +20 -2
  54. package/templates/github/deploy.workflow.yml +404 -9
  55. package/templates/github/hosted-project.workflow.yml +77 -0
@@ -0,0 +1,1129 @@
1
+ #!/usr/bin/env node
2
+ import { fileURLToPath } from "node:url";
3
+ import {
4
+ createControlPlaneReporter
5
+ } from "@treeseed/sdk";
6
+ import { createQueuePushClient, createServiceSdk, queueEnvelopeForTask, resolveManagerConfig } from "./common.js";
7
+ import { writeWorkdayContentSnapshot } from "./workday-content.js";
8
+ import { createWorkerPoolScaler } from "./worker-pool-scaler.js";
9
+ const DEFAULT_WORK_DAYS = [1, 2, 3, 4, 5];
10
+ const DEFAULT_PRIORITY_MODELS = ["objective", "question", "note", "page", "book", "knowledge"];
11
+ function integerFromEnv(name, fallback) {
12
+ const value = process.env[name];
13
+ if (!value) return fallback;
14
+ const parsed = Number.parseInt(value, 10);
15
+ return Number.isFinite(parsed) ? parsed : fallback;
16
+ }
17
+ function envValue(name) {
18
+ const value = process.env[name]?.trim();
19
+ return value ? value : "";
20
+ }
21
+ function booleanFromEnv(name, fallback = false) {
22
+ const value = envValue(name).toLowerCase();
23
+ if (!value) {
24
+ return fallback;
25
+ }
26
+ return ["1", "true", "yes", "on"].includes(value);
27
+ }
28
+ function parseDays(value) {
29
+ const days = value.split(",").map((entry) => Number.parseInt(entry.trim(), 10)).filter((entry) => Number.isInteger(entry) && entry >= 0 && entry <= 6);
30
+ return days.length > 0 ? [...new Set(days)] : [...DEFAULT_WORK_DAYS];
31
+ }
32
+ function parseWindowsFromEnv() {
33
+ const jsonValue = envValue("TREESEED_WORKDAY_WINDOWS_JSON");
34
+ if (jsonValue) {
35
+ try {
36
+ const parsed = JSON.parse(jsonValue);
37
+ if (Array.isArray(parsed) && parsed.length > 0) {
38
+ return parsed;
39
+ }
40
+ } catch {
41
+ }
42
+ }
43
+ return [{
44
+ days: parseDays(envValue("TREESEED_WORKDAY_DAYS") || DEFAULT_WORK_DAYS.join(",")),
45
+ startTime: envValue("TREESEED_WORKDAY_START_TIME") || "09:00",
46
+ endTime: envValue("TREESEED_WORKDAY_END_TIME") || "17:00"
47
+ }];
48
+ }
49
+ function resolveScheduleFromEnv() {
50
+ return {
51
+ timezone: envValue("TREESEED_WORKDAY_TIMEZONE") || process.env.TZ || "UTC",
52
+ windows: parseWindowsFromEnv()
53
+ };
54
+ }
55
+ function parsePriorityModels() {
56
+ const raw = envValue("TREESEED_MANAGER_PRIORITY_MODELS");
57
+ if (!raw) {
58
+ return [...DEFAULT_PRIORITY_MODELS];
59
+ }
60
+ return raw.split(",").map((entry) => entry.trim()).filter(Boolean);
61
+ }
62
+ function parseMinutes(value) {
63
+ const [hours, minutes] = value.split(":", 2).map((entry) => Number.parseInt(entry, 10));
64
+ if (!Number.isInteger(hours) || !Number.isInteger(minutes)) {
65
+ return 0;
66
+ }
67
+ return hours * 60 + minutes;
68
+ }
69
+ function zonedNowParts(date, timezone) {
70
+ const parts = new Intl.DateTimeFormat("en-US", {
71
+ timeZone: timezone,
72
+ weekday: "short",
73
+ hour: "2-digit",
74
+ minute: "2-digit",
75
+ hour12: false
76
+ }).formatToParts(date);
77
+ const weekdayMap = {
78
+ Sun: 0,
79
+ Mon: 1,
80
+ Tue: 2,
81
+ Wed: 3,
82
+ Thu: 4,
83
+ Fri: 5,
84
+ Sat: 6
85
+ };
86
+ const weekday = weekdayMap[parts.find((part) => part.type === "weekday")?.value ?? "Sun"] ?? 0;
87
+ const hour = Number.parseInt(parts.find((part) => part.type === "hour")?.value ?? "0", 10);
88
+ const minute = Number.parseInt(parts.find((part) => part.type === "minute")?.value ?? "0", 10);
89
+ return {
90
+ weekday,
91
+ minutes: hour * 60 + minute
92
+ };
93
+ }
94
+ function isWithinWorkWindow(date, schedule) {
95
+ const now = zonedNowParts(date, schedule.timezone);
96
+ for (const window of schedule.windows) {
97
+ const startMinutes = parseMinutes(window.startTime);
98
+ const endMinutes = parseMinutes(window.endTime);
99
+ const todayIncluded = window.days.includes(now.weekday);
100
+ if (startMinutes <= endMinutes) {
101
+ if (todayIncluded && now.minutes >= startMinutes && now.minutes <= endMinutes) {
102
+ return true;
103
+ }
104
+ continue;
105
+ }
106
+ const previousDay = (now.weekday + 6) % 7;
107
+ if (todayIncluded && now.minutes >= startMinutes) {
108
+ return true;
109
+ }
110
+ if (window.days.includes(previousDay) && now.minutes <= endMinutes) {
111
+ return true;
112
+ }
113
+ }
114
+ return false;
115
+ }
116
+ function parseJson(value, fallback) {
117
+ if (typeof value !== "string" || !value.trim()) {
118
+ return fallback;
119
+ }
120
+ try {
121
+ return JSON.parse(value);
122
+ } catch {
123
+ return fallback;
124
+ }
125
+ }
126
+ function asRecords(value) {
127
+ return Array.isArray(value) ? value : [];
128
+ }
129
+ function readString(record, ...keys) {
130
+ for (const key of keys) {
131
+ const value = record[key];
132
+ if (typeof value === "string" && value.trim()) {
133
+ return value.trim();
134
+ }
135
+ }
136
+ return "";
137
+ }
138
+ function readArray(record, ...keys) {
139
+ for (const key of keys) {
140
+ const value = record[key];
141
+ if (Array.isArray(value)) {
142
+ return value.filter((entry) => typeof entry === "string" && entry.trim().length > 0);
143
+ }
144
+ }
145
+ return [];
146
+ }
147
+ function readNumber(record, ...keys) {
148
+ for (const key of keys) {
149
+ const value = record[key];
150
+ if (typeof value === "number" && Number.isFinite(value)) {
151
+ return value;
152
+ }
153
+ if (typeof value === "string" && value.trim()) {
154
+ const parsed = Number.parseFloat(value);
155
+ if (Number.isFinite(parsed)) {
156
+ return parsed;
157
+ }
158
+ }
159
+ }
160
+ return null;
161
+ }
162
+ function readDate(record, ...keys) {
163
+ const raw = readString(record, ...keys);
164
+ if (!raw) {
165
+ return null;
166
+ }
167
+ const parsed = new Date(raw);
168
+ return Number.isFinite(parsed.valueOf()) ? parsed : null;
169
+ }
170
+ function parseJsonString(value, fallback = {}) {
171
+ if (typeof value !== "string" || !value.trim()) {
172
+ return fallback;
173
+ }
174
+ try {
175
+ return JSON.parse(value);
176
+ } catch {
177
+ return fallback;
178
+ }
179
+ }
180
+ function normalizeChangedFilesFromValue(value, changedFiles = /* @__PURE__ */ new Set()) {
181
+ if (Array.isArray(value)) {
182
+ for (const entry of value) {
183
+ if (typeof entry === "string" && entry.trim()) {
184
+ changedFiles.add(entry.trim());
185
+ } else if (entry && typeof entry === "object") {
186
+ normalizeChangedFilesFromValue(entry, changedFiles);
187
+ }
188
+ }
189
+ return changedFiles;
190
+ }
191
+ if (!value || typeof value !== "object") {
192
+ return changedFiles;
193
+ }
194
+ for (const [key, nested] of Object.entries(value)) {
195
+ if (["changedFiles", "changed_files", "files", "paths"].includes(key)) {
196
+ normalizeChangedFilesFromValue(nested, changedFiles);
197
+ continue;
198
+ }
199
+ if (nested && typeof nested === "object") {
200
+ normalizeChangedFilesFromValue(nested, changedFiles);
201
+ }
202
+ }
203
+ return changedFiles;
204
+ }
205
+ function isoDateOrNull(value) {
206
+ if (!value) {
207
+ return null;
208
+ }
209
+ const parsed = new Date(value);
210
+ return Number.isFinite(parsed.valueOf()) ? parsed.toISOString() : null;
211
+ }
212
+ function filterDeploymentsForWorkday(deployments, workDay, generatedAt) {
213
+ const startedAt = readDate(workDay, "startedAt", "started_at");
214
+ const endedAt = readDate(workDay, "endedAt", "ended_at") ?? new Date(generatedAt);
215
+ if (!startedAt || !endedAt) {
216
+ return deployments;
217
+ }
218
+ return deployments.filter((deployment) => {
219
+ const relevant = readDate(deployment, "finishedAt", "finished_at") ?? readDate(deployment, "startedAt", "started_at") ?? readDate(deployment, "createdAt", "created_at");
220
+ if (!relevant) {
221
+ return false;
222
+ }
223
+ return relevant.valueOf() >= startedAt.valueOf() && relevant.valueOf() <= endedAt.valueOf();
224
+ });
225
+ }
226
+ async function fetchRunnerDeployments(config) {
227
+ if (!config.marketBaseUrl || !config.runnerToken) {
228
+ return [];
229
+ }
230
+ const url = new URL(`/v1/projects/${encodeURIComponent(config.projectId)}/runner/deployments`, config.marketBaseUrl);
231
+ url.searchParams.set("environment", config.environment);
232
+ const response = await fetch(url, {
233
+ headers: {
234
+ accept: "application/json",
235
+ authorization: `Bearer ${config.runnerToken}`
236
+ }
237
+ });
238
+ if (!response.ok) {
239
+ return [];
240
+ }
241
+ const payload = await response.json().catch(() => ({}));
242
+ return asRecords(payload.payload);
243
+ }
244
+ function defaultCreditsForModel(model) {
245
+ switch (model) {
246
+ case "objective":
247
+ return 5;
248
+ case "question":
249
+ return 4;
250
+ case "note":
251
+ case "page":
252
+ return 3;
253
+ case "book":
254
+ case "knowledge":
255
+ return 2;
256
+ default:
257
+ return 1;
258
+ }
259
+ }
260
+ function statusWeight(status) {
261
+ switch (status.toLowerCase()) {
262
+ case "urgent":
263
+ return 50;
264
+ case "blocked":
265
+ return 45;
266
+ case "active":
267
+ case "in_progress":
268
+ case "open":
269
+ return 35;
270
+ case "ready":
271
+ return 30;
272
+ case "draft":
273
+ return 20;
274
+ case "live":
275
+ return 15;
276
+ case "done":
277
+ case "completed":
278
+ return -25;
279
+ case "archived":
280
+ return -40;
281
+ default:
282
+ return 0;
283
+ }
284
+ }
285
+ function modelWeight(model) {
286
+ switch (model) {
287
+ case "objective":
288
+ return 45;
289
+ case "question":
290
+ return 40;
291
+ case "note":
292
+ return 25;
293
+ case "page":
294
+ return 20;
295
+ case "book":
296
+ return 15;
297
+ case "knowledge":
298
+ return 10;
299
+ default:
300
+ return 5;
301
+ }
302
+ }
303
+ function relationWeight(record) {
304
+ const relatedCount = readArray(record, "related_objectives", "relatedObjectives").length + readArray(record, "related_questions", "relatedQuestions").length + readArray(record, "related_books", "relatedBooks").length;
305
+ return relatedCount * 4;
306
+ }
307
+ function stalenessWeight(updatedAt, now) {
308
+ if (!updatedAt) {
309
+ return 8;
310
+ }
311
+ const ageDays = Math.max(0, Math.floor((now.valueOf() - updatedAt.valueOf()) / (24 * 60 * 60 * 1e3)));
312
+ if (ageDays >= 90) return 18;
313
+ if (ageDays >= 30) return 12;
314
+ if (ageDays >= 7) return 6;
315
+ return 0;
316
+ }
317
+ function resolveEstimatedCredits(model, policy, override) {
318
+ const overrideCredits = readNumber(override ?? {}, "estimatedCredits", "estimated_credits");
319
+ if (overrideCredits && overrideCredits > 0) {
320
+ return overrideCredits;
321
+ }
322
+ const weighted = policy.creditWeights.find((weight) => weight.taskType === `${model}_review`);
323
+ return weighted?.credits ?? defaultCreditsForModel(model);
324
+ }
325
+ function summarizeWorkWindow(schedule) {
326
+ return schedule.windows.map((window) => ({
327
+ days: window.days,
328
+ startTime: window.startTime,
329
+ endTime: window.endTime
330
+ }));
331
+ }
332
+ function normalizePolicyRecord(projectId, environment, config) {
333
+ return {
334
+ projectId,
335
+ environment,
336
+ schedule: config.defaultSchedule,
337
+ dailyTaskCreditBudget: config.dailyTaskCreditBudget,
338
+ maxQueuedTasks: config.maxQueuedTasks,
339
+ maxQueuedCredits: config.maxQueuedCredits,
340
+ autoscale: config.autoscale,
341
+ creditWeights: config.creditWeights,
342
+ metadata: {
343
+ managedBy: "manager",
344
+ mode: config.mode
345
+ }
346
+ };
347
+ }
348
+ function resolveManagerServiceConfig() {
349
+ const shared = resolveManagerConfig();
350
+ const environment = envValue("TREESEED_DEPLOY_ENVIRONMENT") || (process.env.NODE_ENV === "production" ? "prod" : "local");
351
+ const projectId = envValue("TREESEED_PROJECT_ID") || shared.projectId;
352
+ const teamId = envValue("TREESEED_HOSTING_TEAM_ID") || envValue("TREESEED_CONTENT_DEFAULT_TEAM_ID") || projectId;
353
+ const dailyTaskCreditBudget = integerFromEnv(
354
+ "TREESEED_WORKDAY_TASK_CREDIT_BUDGET",
355
+ integerFromEnv("TREESEED_WORKDAY_CAPACITY_BUDGET", shared.defaultCapacityBudget)
356
+ );
357
+ const maxQueuedTasks = integerFromEnv("TREESEED_MANAGER_MAX_QUEUED_TASKS", Math.max(1, Math.min(20, dailyTaskCreditBudget)));
358
+ const maxQueuedCredits = integerFromEnv("TREESEED_MANAGER_MAX_QUEUED_CREDITS", Math.max(1, Math.min(dailyTaskCreditBudget, maxQueuedTasks * 4)));
359
+ return {
360
+ ...shared,
361
+ mode: envValue("TREESEED_MANAGER_MODE") || (process.env.CI ? "reconcile" : "loop"),
362
+ managerId: envValue("TREESEED_MANAGER_ID") || `manager-${process.pid}`,
363
+ marketBaseUrl: envValue("TREESEED_MARKET_API_BASE_URL") || envValue("TREESEED_API_BASE_URL"),
364
+ runnerToken: envValue("TREESEED_PROJECT_RUNNER_TOKEN"),
365
+ projectId,
366
+ teamId,
367
+ environment,
368
+ poolName: envValue("TREESEED_AGENT_POOL_NAME") || `${projectId}-${environment}`,
369
+ serviceBaseUrl: envValue("TREESEED_MANAGER_BASE_URL") || null,
370
+ pollIntervalMs: integerFromEnv("TREESEED_MANAGER_POLL_INTERVAL_MS", 15e3),
371
+ dailyTaskCreditBudget,
372
+ maxQueuedTasks,
373
+ maxQueuedCredits,
374
+ priorityModels: parsePriorityModels(),
375
+ priorityLimitPerModel: integerFromEnv("TREESEED_MANAGER_PRIORITY_LIMIT_PER_MODEL", 50),
376
+ graphInvalidated: booleanFromEnv("TREESEED_MANAGER_GRAPH_INVALIDATED"),
377
+ defaultSchedule: resolveScheduleFromEnv(),
378
+ scalerKind: envValue("TREESEED_WORKER_POOL_SCALER") || "" || null,
379
+ creditWeights: parseJson(envValue("TREESEED_TASK_CREDIT_WEIGHTS_JSON"), []),
380
+ autoscale: {
381
+ minWorkers: integerFromEnv("TREESEED_AGENT_POOL_MIN_WORKERS", 0),
382
+ maxWorkers: integerFromEnv("TREESEED_AGENT_POOL_MAX_WORKERS", 1),
383
+ targetQueueDepth: Math.max(1, integerFromEnv("TREESEED_AGENT_POOL_TARGET_QUEUE_DEPTH", 1)),
384
+ cooldownSeconds: Math.max(0, integerFromEnv("TREESEED_AGENT_POOL_COOLDOWN_SECONDS", 60))
385
+ }
386
+ };
387
+ }
388
+ async function resolveReporter(reporter) {
389
+ return reporter ?? createControlPlaneReporter();
390
+ }
391
+ function resolveScaler(config, scaler) {
392
+ return scaler ?? createWorkerPoolScaler(config.scalerKind);
393
+ }
394
+ async function getActiveWorkDay(sdk, projectId) {
395
+ const workDays = await sdk.search({
396
+ model: "work_day",
397
+ limit: 10,
398
+ filters: [
399
+ { field: "project_id", op: "eq", value: projectId },
400
+ { field: "state", op: "eq", value: "active" }
401
+ ],
402
+ sort: [{ field: "updated_at", direction: "desc" }]
403
+ });
404
+ return asRecords(workDays.payload)[0] ?? null;
405
+ }
406
+ async function ensureWorkPolicy(sdk, config) {
407
+ const existing = await sdk.getWorkPolicy(config.projectId, config.environment);
408
+ if (existing.payload) {
409
+ return existing.payload;
410
+ }
411
+ const created = await sdk.upsertWorkPolicy(normalizePolicyRecord(config.projectId, config.environment, config));
412
+ return created.payload;
413
+ }
414
+ async function loadPriorityInputs(sdk, config) {
415
+ const [overridesEnvelope, ...contentEnvelopes] = await Promise.all([
416
+ sdk.listPriorityOverrides(config.projectId),
417
+ ...config.priorityModels.map((model) => sdk.search({
418
+ model,
419
+ limit: config.priorityLimitPerModel,
420
+ sort: [{ field: "updated_at", direction: "desc" }]
421
+ }).catch(() => ({ payload: [] })))
422
+ ]);
423
+ const overrides = asRecords(overridesEnvelope.payload).reduce((map, entry) => {
424
+ const model = readString(entry, "model");
425
+ const subjectId = readString(entry, "subjectId", "subject_id");
426
+ if (model && subjectId) {
427
+ map.set(`${model}:${subjectId}`, entry);
428
+ }
429
+ return map;
430
+ }, /* @__PURE__ */ new Map());
431
+ const records = contentEnvelopes.flatMap((envelope, index) => {
432
+ const model = config.priorityModels[index];
433
+ return asRecords(envelope.payload).map((entry) => ({ model, entry }));
434
+ });
435
+ return { overrides, records };
436
+ }
437
+ async function buildPrioritySnapshot(sdk, config, policy, now, workDayId) {
438
+ const { overrides, records } = await loadPriorityInputs(sdk, config);
439
+ const items = records.map(({ model, entry }) => {
440
+ const id = readString(entry, "id", "slug");
441
+ const slug = readString(entry, "slug") || null;
442
+ const title = readString(entry, "title", "name") || null;
443
+ const status = readString(entry, "status", "runtime_status", "runtimeStatus");
444
+ const updatedAt = readDate(entry, "updated_at", "updatedAt", "updated", "date");
445
+ const override = overrides.get(`${model}:${id}`);
446
+ const overridePriority = readNumber(override ?? {}, "priority") ?? 0;
447
+ const reasons = [
448
+ overridePriority > 0 ? `override:${overridePriority}` : null,
449
+ status ? `status:${status}` : null,
450
+ relationWeight(entry) > 0 ? "linked_work" : null,
451
+ updatedAt ? `updated:${updatedAt.toISOString()}` : "updated:unknown"
452
+ ].filter((value) => Boolean(value));
453
+ return {
454
+ model,
455
+ id,
456
+ slug,
457
+ title,
458
+ priority: modelWeight(model) + statusWeight(status) + relationWeight(entry) + stalenessWeight(updatedAt, now) + overridePriority,
459
+ estimatedCredits: resolveEstimatedCredits(model, policy, override),
460
+ reasons,
461
+ metadata: {
462
+ status: status || null,
463
+ updatedAt: updatedAt?.toISOString() ?? null,
464
+ overrideId: override ? readString(override, "id") : null
465
+ }
466
+ };
467
+ }).filter((item) => item.id).sort((left, right) => right.priority - left.priority || left.model.localeCompare(right.model) || left.id.localeCompare(right.id));
468
+ const snapshot = await sdk.createPrioritySnapshot({
469
+ projectId: config.projectId,
470
+ workDayId: workDayId ?? null,
471
+ items,
472
+ metadata: {
473
+ models: config.priorityModels,
474
+ schedule: summarizeWorkWindow(policy.schedule)
475
+ }
476
+ });
477
+ return snapshot.payload;
478
+ }
479
+ async function openWorkday(sdk, config, policy, now) {
480
+ const graphRefresh = await sdk.refreshGraph();
481
+ const created = await sdk.startWorkDay({
482
+ projectId: config.projectId,
483
+ capacityBudget: policy.dailyTaskCreditBudget,
484
+ graphVersion: graphRefresh.snapshotRoot,
485
+ summary: {
486
+ openedAt: now.toISOString(),
487
+ environment: config.environment,
488
+ graphVersion: graphRefresh.snapshotRoot
489
+ },
490
+ actor: "manager"
491
+ });
492
+ return created.payload;
493
+ }
494
+ async function collectTaskMetrics(sdk, workDayId) {
495
+ const [queuedEnvelope, activeEnvelope] = await Promise.all([
496
+ sdk.searchTasks({
497
+ workDayId: workDayId ?? void 0,
498
+ limit: 500,
499
+ state: ["pending", "queued"]
500
+ }),
501
+ sdk.searchTasks({
502
+ workDayId: workDayId ?? void 0,
503
+ limit: 500,
504
+ state: ["claimed", "running"]
505
+ })
506
+ ]);
507
+ const queuedTasks = asRecords(queuedEnvelope.payload);
508
+ const activeTasks = asRecords(activeEnvelope.payload);
509
+ const queuedCredits = queuedTasks.reduce((total, task) => {
510
+ const payload = parseJson(String(task.payloadJson ?? "{}"), {});
511
+ const credits = readNumber(payload, "estimatedCredits") ?? 1;
512
+ return total + credits;
513
+ }, 0);
514
+ return {
515
+ queuedTasks,
516
+ activeTasks,
517
+ queuedCount: queuedTasks.length,
518
+ activeLeases: activeTasks.length,
519
+ queuedCredits
520
+ };
521
+ }
522
+ function remainingCredits(workDay, policy) {
523
+ if (!workDay) {
524
+ return policy.dailyTaskCreditBudget;
525
+ }
526
+ const budget = Number(workDay.capacityBudget ?? policy.dailyTaskCreditBudget ?? 0);
527
+ const used = Number(workDay.capacityUsed ?? 0);
528
+ return Math.max(0, budget - used);
529
+ }
530
+ function chooseAgentId(agentSpecs) {
531
+ const preferred = agentSpecs.find((spec) => {
532
+ const triggers = Array.isArray(spec.triggers) ? spec.triggers : [];
533
+ return triggers.some((trigger) => {
534
+ const type = typeof trigger === "string" ? trigger : readString(trigger, "type");
535
+ return type === "startup" || type === "schedule";
536
+ });
537
+ });
538
+ return readString(preferred ?? agentSpecs[0] ?? {}, "slug");
539
+ }
540
+ async function maybeEnqueueTask(sdk, task) {
541
+ const queue = createQueuePushClient();
542
+ if (!queue) {
543
+ return { queued: false, queueName: null };
544
+ }
545
+ await queue.enqueue({
546
+ message: queueEnvelopeForTask(task),
547
+ delaySeconds: 0
548
+ });
549
+ await sdk.recordTaskProgress({
550
+ id: String(task.id ?? ""),
551
+ state: "queued",
552
+ appendEvent: {
553
+ kind: "queued",
554
+ data: {
555
+ queueName: envValue("TREESEED_QUEUE_ID") || null
556
+ }
557
+ },
558
+ actor: "manager"
559
+ });
560
+ return { queued: true, queueName: envValue("TREESEED_QUEUE_ID") || null };
561
+ }
562
+ async function topUpQueuedTasks(sdk, config, policy, workDay, snapshot, now) {
563
+ const agentSpecs = await sdk.listAgentSpecs({ enabled: true });
564
+ const agentId = chooseAgentId(asRecords(agentSpecs));
565
+ if (!agentId || !snapshot?.items.length) {
566
+ return {
567
+ createdTasks: [],
568
+ remainingCandidates: 0,
569
+ remainingCredits: remainingCredits(workDay, policy)
570
+ };
571
+ }
572
+ const [allTasksEnvelope, queuedMetrics] = await Promise.all([
573
+ sdk.searchTasks({ workDayId: String(workDay.id ?? ""), limit: 1e3 }),
574
+ collectTaskMetrics(sdk, String(workDay.id ?? ""))
575
+ ]);
576
+ const existingTasks = asRecords(allTasksEnvelope.payload);
577
+ const existingKeys = new Set(existingTasks.map((task) => readString(task, "idempotencyKey", "idempotency_key")));
578
+ let availableCredits = remainingCredits(workDay, policy);
579
+ let remainingQueuedSlots = Math.max(0, policy.maxQueuedTasks - queuedMetrics.queuedCount);
580
+ let remainingQueuedCredits = Math.max(0, policy.maxQueuedCredits - queuedMetrics.queuedCredits);
581
+ const createdTasks = [];
582
+ for (const item of snapshot.items) {
583
+ if (remainingQueuedSlots <= 0 || availableCredits <= 0 || remainingQueuedCredits <= 0) {
584
+ break;
585
+ }
586
+ const idempotencyKey = `${String(workDay.id ?? "")}:${item.model}:${item.id}`;
587
+ if (existingKeys.has(idempotencyKey)) {
588
+ continue;
589
+ }
590
+ const estimatedCredits = Math.max(1, Math.ceil(item.estimatedCredits));
591
+ if (estimatedCredits > availableCredits || estimatedCredits > remainingQueuedCredits) {
592
+ continue;
593
+ }
594
+ const created = await sdk.createTask({
595
+ workDayId: String(workDay.id ?? ""),
596
+ agentId,
597
+ type: `${item.model}_review`,
598
+ priority: Math.max(1, Math.round(item.priority)),
599
+ idempotencyKey,
600
+ payload: {
601
+ subject: {
602
+ model: item.model,
603
+ id: item.id,
604
+ slug: item.slug ?? null,
605
+ title: item.title ?? null
606
+ },
607
+ estimatedCredits,
608
+ priority: item.priority,
609
+ reasons: item.reasons,
610
+ createdAt: now.toISOString()
611
+ },
612
+ graphVersion: typeof workDay.graphVersion === "string" ? workDay.graphVersion : null,
613
+ actor: "manager"
614
+ });
615
+ if (!created.payload) {
616
+ continue;
617
+ }
618
+ await sdk.recordTaskCredits({
619
+ projectId: config.projectId,
620
+ workDayId: String(workDay.id ?? ""),
621
+ taskId: String(created.payload.id ?? ""),
622
+ phase: "seed",
623
+ credits: estimatedCredits,
624
+ metadata: {
625
+ model: item.model,
626
+ subjectId: item.id
627
+ }
628
+ });
629
+ await maybeEnqueueTask(sdk, created.payload);
630
+ createdTasks.push(created.payload);
631
+ existingKeys.add(idempotencyKey);
632
+ availableCredits -= estimatedCredits;
633
+ remainingQueuedSlots -= 1;
634
+ remainingQueuedCredits -= estimatedCredits;
635
+ }
636
+ const remainingCandidates = snapshot.items.filter((item) => !existingKeys.has(`${String(workDay.id ?? "")}:${item.model}:${item.id}`)).length;
637
+ return {
638
+ createdTasks,
639
+ remainingCandidates,
640
+ remainingCredits: availableCredits
641
+ };
642
+ }
643
+ function desiredWorkersForSnapshot(policy, metrics) {
644
+ const { minWorkers, maxWorkers, targetQueueDepth } = policy.autoscale;
645
+ if (metrics.queuedCount <= 0 && metrics.activeLeases <= 0) {
646
+ return minWorkers;
647
+ }
648
+ const requiredByQueue = Math.ceil(metrics.queuedCount / Math.max(1, targetQueueDepth));
649
+ const minimumActive = metrics.activeLeases > 0 ? 1 : 0;
650
+ return Math.max(minWorkers, Math.min(maxWorkers, Math.max(requiredByQueue, minimumActive)));
651
+ }
652
+ function applyScaleCooldown(policy, latestDecision, nextDesired, now) {
653
+ if (!latestDecision) {
654
+ return nextDesired;
655
+ }
656
+ if (nextDesired >= latestDecision.desiredWorkers) {
657
+ return nextDesired;
658
+ }
659
+ const cooldownMs = Math.max(0, policy.autoscale.cooldownSeconds) * 1e3;
660
+ if (cooldownMs === 0) {
661
+ return nextDesired;
662
+ }
663
+ const lastChangedAt = new Date(latestDecision.createdAt);
664
+ if (!Number.isFinite(lastChangedAt.valueOf())) {
665
+ return nextDesired;
666
+ }
667
+ return now.valueOf() - lastChangedAt.valueOf() < cooldownMs ? latestDecision.desiredWorkers : nextDesired;
668
+ }
669
+ async function registerHeartbeat(reporter, config, policy, desiredWorkers, metrics) {
670
+ await reporter.registerAgentPoolHeartbeat({
671
+ teamId: config.teamId,
672
+ environment: config.environment,
673
+ poolName: config.poolName,
674
+ managerId: config.managerId,
675
+ serviceName: "manager",
676
+ registrationIdentity: config.managerId,
677
+ serviceBaseUrl: config.serviceBaseUrl,
678
+ autoscale: policy.autoscale,
679
+ desiredWorkers,
680
+ observedQueueDepth: metrics.queuedCount,
681
+ observedActiveLeases: metrics.activeLeases,
682
+ metadata: {
683
+ projectId: config.projectId,
684
+ managerPort: config.port
685
+ }
686
+ });
687
+ }
688
+ async function buildWorkdaySummary(sdk, config, workDay, policy, currentSnapshot, scaleDecision, scaleResult) {
689
+ const generatedAt = (/* @__PURE__ */ new Date()).toISOString();
690
+ const [tasksEnvelope, creditsEnvelope, deployments] = await Promise.all([
691
+ sdk.searchTasks({ workDayId: String(workDay.id ?? ""), limit: 1e3 }),
692
+ sdk.listTaskCredits(String(workDay.id ?? "")),
693
+ fetchRunnerDeployments(config)
694
+ ]);
695
+ const tasks = asRecords(tasksEnvelope.payload);
696
+ const credits = Array.isArray(creditsEnvelope.payload) ? creditsEnvelope.payload : [];
697
+ const taskDetails = await Promise.all(tasks.map(async (task) => {
698
+ const taskId = readString(task, "id");
699
+ const [eventsEnvelope, outputsEnvelope] = await Promise.all([
700
+ sdk.search({
701
+ model: "task_event",
702
+ filters: [{ field: "taskId", op: "eq", value: taskId }],
703
+ limit: 200
704
+ }),
705
+ sdk.search({
706
+ model: "task_output",
707
+ filters: [{ field: "taskId", op: "eq", value: taskId }],
708
+ limit: 200
709
+ })
710
+ ]);
711
+ const taskEvents = asRecords(eventsEnvelope.payload);
712
+ const taskOutputs = asRecords(outputsEnvelope.payload);
713
+ const changedFiles2 = /* @__PURE__ */ new Set();
714
+ for (const output of taskOutputs) {
715
+ normalizeChangedFilesFromValue(parseJsonString(output.outputJson ?? output.output_json), changedFiles2);
716
+ }
717
+ const latestEvent = [...taskEvents].sort((left, right) => Number(readNumber(right, "seq") ?? 0) - Number(readNumber(left, "seq") ?? 0))[0];
718
+ return {
719
+ task: {
720
+ id: taskId,
721
+ agentId: readString(task, "agentId", "agent_id") || void 0,
722
+ type: readString(task, "type") || void 0,
723
+ state: readString(task, "state") || void 0,
724
+ priority: readNumber(task, "priority") ?? void 0,
725
+ idempotencyKey: readString(task, "idempotencyKey", "idempotency_key") || void 0,
726
+ createdAt: isoDateOrNull(readString(task, "createdAt", "created_at")),
727
+ startedAt: isoDateOrNull(readString(task, "startedAt", "started_at")),
728
+ completedAt: isoDateOrNull(readString(task, "completedAt", "completed_at")),
729
+ lastErrorCode: readString(task, "lastErrorCode", "last_error_code") || null,
730
+ lastErrorMessage: readString(task, "lastErrorMessage", "last_error_message") || null,
731
+ lastEventKind: latestEvent ? readString(latestEvent, "kind") || null : null,
732
+ outputCount: taskOutputs.length,
733
+ changedFiles: [...changedFiles2]
734
+ },
735
+ changedFiles: changedFiles2
736
+ };
737
+ }));
738
+ const changedFiles = [...taskDetails.reduce((set, detail) => {
739
+ for (const filePath of detail.changedFiles) {
740
+ set.add(filePath);
741
+ }
742
+ return set;
743
+ }, /* @__PURE__ */ new Set())].sort((left, right) => left.localeCompare(right));
744
+ const releases = filterDeploymentsForWorkday(deployments, workDay, generatedAt).map((deployment) => ({
745
+ id: readString(deployment, "id") || void 0,
746
+ deploymentKind: readString(deployment, "deploymentKind", "deployment_kind") || "code",
747
+ status: readString(deployment, "status") || "unknown",
748
+ releaseTag: readString(deployment, "releaseTag", "release_tag") || null,
749
+ commitSha: readString(deployment, "commitSha", "commit_sha") || null,
750
+ sourceRef: readString(deployment, "sourceRef", "source_ref") || null,
751
+ startedAt: isoDateOrNull(readString(deployment, "startedAt", "started_at")),
752
+ finishedAt: isoDateOrNull(readString(deployment, "finishedAt", "finished_at")),
753
+ createdAt: isoDateOrNull(readString(deployment, "createdAt", "created_at"))
754
+ }));
755
+ const budget = Number(workDay.capacityBudget ?? policy.dailyTaskCreditBudget ?? 0);
756
+ const used = Number(workDay.capacityUsed ?? 0);
757
+ return {
758
+ projectId: config.projectId,
759
+ environment: config.environment,
760
+ workDayId: String(workDay.id ?? ""),
761
+ state: String(workDay.state ?? "active"),
762
+ totalTasks: tasks.length,
763
+ completedTasks: tasks.filter((task) => task.state === "completed").length,
764
+ failedTasks: tasks.filter((task) => task.state === "failed").length,
765
+ queuedTasks: tasks.filter((task) => task.state === "queued" || task.state === "pending").length,
766
+ activeTasks: tasks.filter((task) => task.state === "claimed" || task.state === "running").length,
767
+ dailyTaskCreditBudget: budget,
768
+ usedTaskCredits: used,
769
+ remainingTaskCredits: Math.max(0, budget - used),
770
+ creditLedgerEntries: credits.length,
771
+ prioritySnapshotId: currentSnapshot?.id ?? null,
772
+ priorityItemCount: currentSnapshot?.items.length ?? 0,
773
+ priorityItems: currentSnapshot?.items ?? [],
774
+ taskItems: taskDetails.map((detail) => detail.task),
775
+ changedFiles,
776
+ releases,
777
+ scaleDecision,
778
+ scaleResult,
779
+ generatedAt
780
+ };
781
+ }
782
+ async function reportWorkdaySummary(sdk, reporter, config, workDay, policy, currentSnapshot, scaleDecision, scaleResult) {
783
+ const summary = await buildWorkdaySummary(sdk, config, workDay, policy, currentSnapshot, scaleDecision, scaleResult);
784
+ const snapshot = writeWorkdayContentSnapshot({
785
+ repoRoot: process.env.TREESEED_AGENT_REPO_ROOT?.trim() || process.cwd(),
786
+ projectId: config.projectId,
787
+ teamId: config.teamId,
788
+ environment: config.environment,
789
+ workDay,
790
+ summary,
791
+ prioritySnapshot: currentSnapshot,
792
+ scaleDecision,
793
+ scaleResult,
794
+ tasks: Array.isArray(summary.taskItems) ? summary.taskItems : [],
795
+ changedFiles: Array.isArray(summary.changedFiles) ? summary.changedFiles.filter((entry) => typeof entry === "string") : [],
796
+ releases: Array.isArray(summary.releases) ? summary.releases : [],
797
+ generatedAt: String(summary.generatedAt ?? (/* @__PURE__ */ new Date()).toISOString())
798
+ });
799
+ const report = await sdk.createReport({
800
+ workDayId: String(workDay.id ?? ""),
801
+ kind: "workday_summary",
802
+ body: {
803
+ ...summary,
804
+ contentSnapshot: {
805
+ relativePath: snapshot.relativePath,
806
+ slug: snapshot.slug,
807
+ reportVersion: snapshot.reportVersion,
808
+ title: snapshot.title
809
+ }
810
+ },
811
+ renderedRef: snapshot.relativePath,
812
+ sentAt: String(summary.generatedAt ?? (/* @__PURE__ */ new Date()).toISOString()),
813
+ actor: "manager"
814
+ });
815
+ await reporter.reportWorkdaySummary({
816
+ environment: config.environment,
817
+ workDayId: String(workDay.id ?? ""),
818
+ kind: "workday_summary",
819
+ state: String(workDay.state ?? "active"),
820
+ startedAt: readString(workDay, "startedAt", "started_at") || null,
821
+ endedAt: readString(workDay, "endedAt", "ended_at") || null,
822
+ summary,
823
+ metadata: {
824
+ projectId: config.projectId,
825
+ contentSnapshot: {
826
+ relativePath: snapshot.relativePath,
827
+ slug: snapshot.slug,
828
+ reportVersion: snapshot.reportVersion
829
+ },
830
+ reportId: report.payload ? readString(report.payload, "id") || null : null
831
+ }
832
+ });
833
+ return {
834
+ ...summary,
835
+ contentSnapshot: {
836
+ relativePath: snapshot.relativePath,
837
+ slug: snapshot.slug,
838
+ reportVersion: snapshot.reportVersion,
839
+ title: snapshot.title
840
+ }
841
+ };
842
+ }
843
+ function shouldCloseWorkday(options) {
844
+ if (!options.workDay) {
845
+ return false;
846
+ }
847
+ const drained = options.queuedCount === 0 && options.activeLeases === 0;
848
+ if (!drained) {
849
+ return false;
850
+ }
851
+ return !options.insideWorkWindow || options.remainingCredits <= 0 || options.remainingCandidates <= 0;
852
+ }
853
+ async function reconcileManager(options) {
854
+ const config = options.config ?? resolveManagerServiceConfig();
855
+ const sdk = options.sdk ?? createServiceSdk();
856
+ const reporter = await resolveReporter(options.reporter);
857
+ const scaler = resolveScaler(config, options.scaler);
858
+ const now = options.now ?? /* @__PURE__ */ new Date();
859
+ const policy = await ensureWorkPolicy(sdk, config);
860
+ const insideWorkWindow = isWithinWorkWindow(now, policy.schedule);
861
+ let activeWorkDay = await getActiveWorkDay(sdk, config.projectId);
862
+ let currentSnapshot = null;
863
+ if (!activeWorkDay && insideWorkWindow && policy.dailyTaskCreditBudget > 0) {
864
+ const previewSnapshot = await buildPrioritySnapshot(sdk, config, policy, now, null);
865
+ if ((previewSnapshot?.items.length ?? 0) > 0) {
866
+ activeWorkDay = await openWorkday(sdk, config, policy, now);
867
+ currentSnapshot = activeWorkDay ? await buildPrioritySnapshot(sdk, config, policy, now, String(activeWorkDay.id ?? "")) : previewSnapshot;
868
+ }
869
+ }
870
+ if (activeWorkDay && !currentSnapshot) {
871
+ currentSnapshot = await buildPrioritySnapshot(sdk, config, policy, now, String(activeWorkDay.id ?? ""));
872
+ }
873
+ let seedResult = {
874
+ createdTasks: [],
875
+ remainingCandidates: currentSnapshot?.items.length ?? 0,
876
+ remainingCredits: remainingCredits(activeWorkDay, policy)
877
+ };
878
+ if (activeWorkDay && insideWorkWindow && seedResult.remainingCredits > 0) {
879
+ seedResult = await topUpQueuedTasks(sdk, config, policy, activeWorkDay, currentSnapshot, now);
880
+ }
881
+ const metrics = await collectTaskMetrics(sdk, activeWorkDay ? String(activeWorkDay.id ?? "") : null);
882
+ const rawDesiredWorkers = activeWorkDay ? desiredWorkersForSnapshot(policy, metrics) : 0;
883
+ const latestScaleDecision = await sdk.getLatestScaleDecision(config.projectId, config.environment, config.poolName);
884
+ const desiredWorkers = applyScaleCooldown(policy, latestScaleDecision.payload, rawDesiredWorkers, now);
885
+ const scaleDecision = {
886
+ projectId: config.projectId,
887
+ environment: config.environment,
888
+ poolName: config.poolName,
889
+ workDayId: activeWorkDay ? String(activeWorkDay.id ?? "") : null,
890
+ desiredWorkers,
891
+ observedQueueDepth: metrics.queuedCount,
892
+ observedActiveLeases: metrics.activeLeases,
893
+ reason: desiredWorkers !== rawDesiredWorkers ? "cooldown_hold" : "reconcile",
894
+ metadata: {
895
+ insideWorkWindow,
896
+ remainingCredits: seedResult.remainingCredits,
897
+ seededTaskCount: seedResult.createdTasks.length
898
+ }
899
+ };
900
+ const recordedScaleDecision = await sdk.recordScaleDecision(scaleDecision);
901
+ const appliedScaleDecision = recordedScaleDecision.payload ?? scaleDecision;
902
+ const scaleResult = await scaler.scale(appliedScaleDecision);
903
+ await registerHeartbeat(reporter, config, policy, desiredWorkers, metrics);
904
+ await reporter.reportScaleDecision({
905
+ environment: config.environment,
906
+ poolName: config.poolName,
907
+ workDayId: activeWorkDay ? String(activeWorkDay.id ?? "") : null,
908
+ desiredWorkers,
909
+ observedQueueDepth: metrics.queuedCount,
910
+ observedActiveLeases: metrics.activeLeases,
911
+ reason: appliedScaleDecision.reason,
912
+ metadata: {
913
+ ...appliedScaleDecision.metadata,
914
+ scaleResult
915
+ }
916
+ });
917
+ let closedWorkDay = null;
918
+ let workdaySummary = null;
919
+ if (shouldCloseWorkday({
920
+ insideWorkWindow,
921
+ workDay: activeWorkDay,
922
+ remainingCredits: seedResult.remainingCredits,
923
+ queuedCount: metrics.queuedCount,
924
+ activeLeases: metrics.activeLeases,
925
+ remainingCandidates: seedResult.remainingCandidates
926
+ })) {
927
+ if (activeWorkDay) {
928
+ workdaySummary = await reportWorkdaySummary(
929
+ sdk,
930
+ reporter,
931
+ config,
932
+ activeWorkDay,
933
+ policy,
934
+ currentSnapshot,
935
+ appliedScaleDecision,
936
+ scaleResult
937
+ );
938
+ const closed = await sdk.closeWorkDay({
939
+ id: String(activeWorkDay.id ?? ""),
940
+ state: "completed",
941
+ summary: workdaySummary,
942
+ actor: "manager"
943
+ });
944
+ closedWorkDay = closed.payload ?? activeWorkDay;
945
+ }
946
+ }
947
+ return {
948
+ ok: true,
949
+ mode: "reconcile",
950
+ managerId: config.managerId,
951
+ projectId: config.projectId,
952
+ environment: config.environment,
953
+ insideWorkWindow,
954
+ workPolicy: policy,
955
+ workDay: closedWorkDay ?? activeWorkDay,
956
+ prioritySnapshot: currentSnapshot,
957
+ seededTasks: seedResult.createdTasks,
958
+ queuedCount: metrics.queuedCount,
959
+ activeLeases: metrics.activeLeases,
960
+ desiredWorkers,
961
+ scaleResult,
962
+ workdaySummary
963
+ };
964
+ }
965
+ async function runOpenWorkday(options) {
966
+ const config = options.config ?? resolveManagerServiceConfig();
967
+ const sdk = options.sdk ?? createServiceSdk();
968
+ const now = options.now ?? /* @__PURE__ */ new Date();
969
+ const policy = await ensureWorkPolicy(sdk, config);
970
+ const active = await getActiveWorkDay(sdk, config.projectId);
971
+ if (active) {
972
+ return { ok: true, created: false, workDay: active };
973
+ }
974
+ if (!isWithinWorkWindow(now, policy.schedule)) {
975
+ return { ok: true, created: false, skipped: true, reason: "outside_work_window" };
976
+ }
977
+ const workDay = await openWorkday(sdk, config, policy, now);
978
+ const prioritySnapshot = workDay ? await buildPrioritySnapshot(sdk, config, policy, now, String(workDay.id ?? "")) : null;
979
+ return { ok: true, created: Boolean(workDay), workDay, prioritySnapshot };
980
+ }
981
+ async function runCloseWorkday(options) {
982
+ const config = options.config ?? resolveManagerServiceConfig();
983
+ const sdk = options.sdk ?? createServiceSdk();
984
+ const reporter = await resolveReporter(options.reporter);
985
+ const scaler = resolveScaler(config, options.scaler);
986
+ const policy = await ensureWorkPolicy(sdk, config);
987
+ const activeWorkDay = await getActiveWorkDay(sdk, config.projectId);
988
+ if (!activeWorkDay) {
989
+ return { ok: true, skipped: true, reason: "no_active_workday" };
990
+ }
991
+ const decision = {
992
+ projectId: config.projectId,
993
+ environment: config.environment,
994
+ poolName: config.poolName,
995
+ workDayId: String(activeWorkDay.id ?? ""),
996
+ desiredWorkers: 0,
997
+ observedQueueDepth: 0,
998
+ observedActiveLeases: 0,
999
+ reason: "close_workday",
1000
+ metadata: {
1001
+ requestedBy: "manager"
1002
+ }
1003
+ };
1004
+ const recorded = await sdk.recordScaleDecision(decision);
1005
+ const scale = await scaler.scale(recorded.payload ?? decision);
1006
+ const latestSnapshot = await sdk.getLatestPrioritySnapshot(config.projectId, String(activeWorkDay.id ?? ""));
1007
+ const summary = await reportWorkdaySummary(
1008
+ sdk,
1009
+ reporter,
1010
+ config,
1011
+ activeWorkDay,
1012
+ policy,
1013
+ latestSnapshot.payload,
1014
+ recorded.payload ?? decision,
1015
+ scale
1016
+ );
1017
+ const closed = await sdk.closeWorkDay({
1018
+ id: String(activeWorkDay.id ?? ""),
1019
+ state: "completed",
1020
+ summary,
1021
+ actor: "manager"
1022
+ });
1023
+ return { ok: true, workDay: closed.payload, summary, scale };
1024
+ }
1025
+ async function runReportWorkday(options) {
1026
+ const config = options.config ?? resolveManagerServiceConfig();
1027
+ const sdk = options.sdk ?? createServiceSdk();
1028
+ const reporter = await resolveReporter(options.reporter);
1029
+ const policy = await ensureWorkPolicy(sdk, config);
1030
+ const activeWorkDay = await getActiveWorkDay(sdk, config.projectId);
1031
+ if (!activeWorkDay) {
1032
+ return { ok: true, skipped: true, reason: "no_active_workday" };
1033
+ }
1034
+ const latestScaleDecision = await sdk.getLatestScaleDecision(config.projectId, config.environment, config.poolName);
1035
+ const latestSnapshot = await sdk.getLatestPrioritySnapshot(config.projectId, String(activeWorkDay.id ?? ""));
1036
+ const summary = await reportWorkdaySummary(
1037
+ sdk,
1038
+ reporter,
1039
+ config,
1040
+ activeWorkDay,
1041
+ policy,
1042
+ latestSnapshot.payload,
1043
+ latestScaleDecision.payload ?? {
1044
+ projectId: config.projectId,
1045
+ environment: config.environment,
1046
+ poolName: config.poolName,
1047
+ workDayId: String(activeWorkDay.id ?? ""),
1048
+ desiredWorkers: 0,
1049
+ observedQueueDepth: 0,
1050
+ observedActiveLeases: 0,
1051
+ reason: "report_workday",
1052
+ metadata: {},
1053
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
1054
+ },
1055
+ {
1056
+ applied: false,
1057
+ provider: "noop",
1058
+ desiredWorkers: Number(latestScaleDecision.payload?.desiredWorkers ?? 0),
1059
+ metadata: {
1060
+ reason: "report_only"
1061
+ }
1062
+ }
1063
+ );
1064
+ return { ok: true, workDayId: activeWorkDay.id, summary };
1065
+ }
1066
+ async function runManagerAction(options = {}) {
1067
+ const mode = options.mode ?? options.config?.mode ?? resolveManagerServiceConfig().mode;
1068
+ switch (mode) {
1069
+ case "open-workday":
1070
+ return runOpenWorkday(options);
1071
+ case "close-workday":
1072
+ return runCloseWorkday(options);
1073
+ case "report-workday":
1074
+ return runReportWorkday(options);
1075
+ case "reconcile":
1076
+ return reconcileManager(options);
1077
+ case "loop":
1078
+ default:
1079
+ return reconcileManager(options);
1080
+ }
1081
+ }
1082
+ async function runManagerCycle(options = {}) {
1083
+ return reconcileManager(options);
1084
+ }
1085
+ async function startManagerLoop(options = {}) {
1086
+ const config = options.config ?? resolveManagerServiceConfig();
1087
+ for (; ; ) {
1088
+ try {
1089
+ await reconcileManager({
1090
+ ...options,
1091
+ config
1092
+ });
1093
+ } catch (error) {
1094
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}
1095
+ `);
1096
+ }
1097
+ await new Promise((resolvePromise) => setTimeout(resolvePromise, config.pollIntervalMs));
1098
+ }
1099
+ }
1100
+ function readCliMode() {
1101
+ const args = process.argv.slice(2);
1102
+ const index = args.indexOf("--mode");
1103
+ if (index >= 0) {
1104
+ return args[index + 1];
1105
+ }
1106
+ return void 0;
1107
+ }
1108
+ const currentFile = fileURLToPath(import.meta.url);
1109
+ const entryFile = process.argv[1] ?? "";
1110
+ if (entryFile === currentFile) {
1111
+ const mode = readCliMode() ?? resolveManagerServiceConfig().mode;
1112
+ if (mode === "loop") {
1113
+ await startManagerLoop({
1114
+ config: {
1115
+ ...resolveManagerServiceConfig(),
1116
+ mode
1117
+ }
1118
+ });
1119
+ } else {
1120
+ process.stdout.write(`${JSON.stringify(await runManagerAction({ mode }), null, 2)}
1121
+ `);
1122
+ }
1123
+ }
1124
+ export {
1125
+ resolveManagerServiceConfig,
1126
+ runManagerAction,
1127
+ runManagerCycle,
1128
+ startManagerLoop
1129
+ };