@treeseed/core 0.8.3 → 0.8.5

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 (133) hide show
  1. package/README.md +7 -11
  2. package/dist/dev-watch.js +1 -1
  3. package/dist/dev.d.ts +2 -4
  4. package/dist/dev.js +4 -124
  5. package/dist/env.yaml +23 -175
  6. package/dist/index.d.ts +0 -4
  7. package/dist/index.js +0 -6
  8. package/dist/scripts/build-dist.js +3 -3
  9. package/dist/scripts/dev-platform.js +1 -7
  10. package/dist/scripts/run-fixture-astro-command.js +25 -51
  11. package/dist/scripts/test-smoke.js +50 -7
  12. package/package.json +5 -78
  13. package/templates/github/deploy-web.workflow.yml +111 -0
  14. package/templates/github/hosted-project.workflow.yml +4 -4
  15. package/dist/agent-runtime.d.ts +0 -17
  16. package/dist/agent-runtime.js +0 -117
  17. package/dist/agent.d.ts +0 -11
  18. package/dist/agent.js +0 -25
  19. package/dist/agents/adapters/execution.d.ts +0 -41
  20. package/dist/agents/adapters/execution.js +0 -73
  21. package/dist/agents/adapters/mutations.d.ts +0 -22
  22. package/dist/agents/adapters/mutations.js +0 -30
  23. package/dist/agents/adapters/notification.d.ts +0 -26
  24. package/dist/agents/adapters/notification.js +0 -46
  25. package/dist/agents/adapters/repository.d.ts +0 -23
  26. package/dist/agents/adapters/repository.js +0 -61
  27. package/dist/agents/adapters/research.d.ts +0 -26
  28. package/dist/agents/adapters/research.js +0 -59
  29. package/dist/agents/adapters/verification.d.ts +0 -36
  30. package/dist/agents/adapters/verification.js +0 -62
  31. package/dist/agents/cli-tools.d.ts +0 -1
  32. package/dist/agents/cli-tools.js +0 -5
  33. package/dist/agents/cli.d.ts +0 -15
  34. package/dist/agents/cli.js +0 -109
  35. package/dist/agents/contracts/messages.d.ts +0 -88
  36. package/dist/agents/contracts/messages.js +0 -138
  37. package/dist/agents/contracts/run.d.ts +0 -21
  38. package/dist/agents/contracts/run.js +0 -0
  39. package/dist/agents/index.d.ts +0 -1
  40. package/dist/agents/index.js +0 -5
  41. package/dist/agents/kernel/agent-kernel.d.ts +0 -51
  42. package/dist/agents/kernel/agent-kernel.js +0 -292
  43. package/dist/agents/kernel/trigger-resolver.d.ts +0 -19
  44. package/dist/agents/kernel/trigger-resolver.js +0 -157
  45. package/dist/agents/registry-helper.d.ts +0 -4
  46. package/dist/agents/registry-helper.js +0 -14
  47. package/dist/agents/registry.d.ts +0 -6
  48. package/dist/agents/registry.js +0 -98
  49. package/dist/agents/runtime-types.d.ts +0 -118
  50. package/dist/agents/runtime-types.js +0 -0
  51. package/dist/agents/spec-loader.d.ts +0 -18
  52. package/dist/agents/spec-loader.js +0 -55
  53. package/dist/agents/spec-normalizer.d.ts +0 -2
  54. package/dist/agents/spec-normalizer.js +0 -327
  55. package/dist/agents/spec-types.d.ts +0 -64
  56. package/dist/agents/spec-types.js +0 -0
  57. package/dist/agents/testing/agents-smoke.d.ts +0 -1
  58. package/dist/agents/testing/agents-smoke.js +0 -32
  59. package/dist/agents/testing/e2e-harness.d.ts +0 -44
  60. package/dist/agents/testing/e2e-harness.js +0 -504
  61. package/dist/api/agent-routes.d.ts +0 -13
  62. package/dist/api/agent-routes.js +0 -327
  63. package/dist/api/app.d.ts +0 -5
  64. package/dist/api/app.js +0 -361
  65. package/dist/api/auth/d1-database.d.ts +0 -3
  66. package/dist/api/auth/d1-database.js +0 -20
  67. package/dist/api/auth/d1-provider.d.ts +0 -79
  68. package/dist/api/auth/d1-provider.js +0 -92
  69. package/dist/api/auth/d1-store.d.ts +0 -114
  70. package/dist/api/auth/d1-store.js +0 -895
  71. package/dist/api/auth/memory-provider.d.ts +0 -77
  72. package/dist/api/auth/memory-provider.js +0 -249
  73. package/dist/api/auth/rbac.d.ts +0 -22
  74. package/dist/api/auth/rbac.js +0 -162
  75. package/dist/api/auth/tokens.d.ts +0 -18
  76. package/dist/api/auth/tokens.js +0 -56
  77. package/dist/api/capabilities.d.ts +0 -9
  78. package/dist/api/capabilities.js +0 -33
  79. package/dist/api/config.d.ts +0 -2
  80. package/dist/api/config.js +0 -77
  81. package/dist/api/http.d.ts +0 -28
  82. package/dist/api/http.js +0 -51
  83. package/dist/api/index.d.ts +0 -9
  84. package/dist/api/index.js +0 -18
  85. package/dist/api/operations-routes.d.ts +0 -11
  86. package/dist/api/operations-routes.js +0 -87
  87. package/dist/api/operations.d.ts +0 -3
  88. package/dist/api/operations.js +0 -26
  89. package/dist/api/project-routes.d.ts +0 -8
  90. package/dist/api/project-routes.js +0 -586
  91. package/dist/api/providers.d.ts +0 -2
  92. package/dist/api/providers.js +0 -62
  93. package/dist/api/railway.d.ts +0 -50
  94. package/dist/api/railway.js +0 -69
  95. package/dist/api/sdk-dispatch.d.ts +0 -5
  96. package/dist/api/sdk-dispatch.js +0 -13
  97. package/dist/api/sdk-routes.d.ts +0 -11
  98. package/dist/api/sdk-routes.js +0 -29
  99. package/dist/api/server.d.ts +0 -2
  100. package/dist/api/server.js +0 -10
  101. package/dist/api/templates.d.ts +0 -3
  102. package/dist/api/templates.js +0 -31
  103. package/dist/api/types.d.ts +0 -231
  104. package/dist/api/types.js +0 -0
  105. package/dist/api.d.ts +0 -1
  106. package/dist/api.js +0 -1
  107. package/dist/railway.d.ts +0 -1
  108. package/dist/railway.js +0 -4
  109. package/dist/services/agents.d.ts +0 -11
  110. package/dist/services/agents.js +0 -48
  111. package/dist/services/common.d.ts +0 -66
  112. package/dist/services/common.js +0 -212
  113. package/dist/services/index.d.ts +0 -6
  114. package/dist/services/index.js +0 -19
  115. package/dist/services/manager.d.ts +0 -267
  116. package/dist/services/manager.js +0 -1368
  117. package/dist/services/remote-runner.d.ts +0 -30
  118. package/dist/services/remote-runner.js +0 -230
  119. package/dist/services/workday-content.d.ts +0 -53
  120. package/dist/services/workday-content.js +0 -190
  121. package/dist/services/workday-manager.d.ts +0 -279
  122. package/dist/services/workday-manager.js +0 -163
  123. package/dist/services/workday-report.d.ts +0 -195
  124. package/dist/services/workday-report.js +0 -17
  125. package/dist/services/workday-start.d.ts +0 -195
  126. package/dist/services/workday-start.js +0 -17
  127. package/dist/services/worker-capacity.d.ts +0 -58
  128. package/dist/services/worker-capacity.js +0 -208
  129. package/dist/services/worker-pool-scaler.d.ts +0 -27
  130. package/dist/services/worker-pool-scaler.js +0 -127
  131. package/dist/services/worker.d.ts +0 -19
  132. package/dist/services/worker.js +0 -436
  133. package/templates/github/deploy.workflow.yml +0 -577
@@ -1,195 +0,0 @@
1
- #!/usr/bin/env node
2
- export declare function runWorkdayStart(): Promise<{
3
- ok: boolean;
4
- mode: "reconcile";
5
- managerId: string;
6
- projectId: string;
7
- environment: "local" | "prod" | "staging";
8
- insideWorkWindow: boolean;
9
- workPolicy: import("@treeseed/sdk").WorkdayPolicy;
10
- workDay: Record<string, unknown>;
11
- prioritySnapshot: import("@treeseed/sdk").PrioritySnapshot;
12
- seededTasks: {
13
- [x: string]: unknown;
14
- }[];
15
- queuedCount: number;
16
- activeLeases: number;
17
- desiredWorkers: number;
18
- scaleResult: import("@treeseed/sdk").WorkerPoolScaleResult;
19
- workdaySummary: Record<string, unknown>;
20
- } | {
21
- ok: boolean;
22
- created: boolean;
23
- workDay: Record<string, unknown>;
24
- skipped?: undefined;
25
- reason?: undefined;
26
- prioritySnapshot?: undefined;
27
- } | {
28
- ok: boolean;
29
- created: boolean;
30
- skipped: boolean;
31
- reason: string;
32
- workDay?: undefined;
33
- prioritySnapshot?: undefined;
34
- } | {
35
- ok: boolean;
36
- created: boolean;
37
- workDay: import("@treeseed/sdk").SdkWorkDayEntity;
38
- prioritySnapshot: import("@treeseed/sdk").PrioritySnapshot;
39
- skipped?: undefined;
40
- reason?: undefined;
41
- } | {
42
- ok: boolean;
43
- skipped: boolean;
44
- reason: string;
45
- workDay?: undefined;
46
- summary?: undefined;
47
- scale?: undefined;
48
- } | {
49
- ok: boolean;
50
- workDay: import("@treeseed/sdk").SdkWorkDayEntity;
51
- summary: {
52
- contentSnapshot: {
53
- relativePath: string;
54
- slug: string;
55
- reportVersion: string;
56
- title: string;
57
- };
58
- capacity: {
59
- providerSplit: any;
60
- grantedDailyCredits: number;
61
- reservedCredits: number;
62
- consumedCredits: number;
63
- remainingDailyCredits: number | null;
64
- providerCount: number;
65
- laneCount: number;
66
- grantCount: number;
67
- };
68
- projectId: string;
69
- environment: "local" | "prod" | "staging";
70
- workDayId: string;
71
- state: string;
72
- totalTasks: number;
73
- completedTasks: number;
74
- failedTasks: number;
75
- queuedTasks: number;
76
- activeTasks: number;
77
- dailyTaskCreditBudget: number;
78
- usedTaskCredits: number;
79
- remainingTaskCredits: number;
80
- creditLedgerEntries: number;
81
- prioritySnapshotId: string;
82
- priorityItemCount: number;
83
- priorityItems: import("@treeseed/sdk").PrioritySnapshotItem[];
84
- taskItems: {
85
- id: string;
86
- agentId: string;
87
- type: string;
88
- state: string;
89
- priority: number;
90
- idempotencyKey: string;
91
- createdAt: string;
92
- startedAt: string;
93
- completedAt: string;
94
- lastErrorCode: string;
95
- lastErrorMessage: string;
96
- lastEventKind: string;
97
- outputCount: number;
98
- changedFiles: string[];
99
- }[];
100
- changedFiles: string[];
101
- releases: {
102
- id: string;
103
- deploymentKind: string;
104
- status: string;
105
- releaseTag: string;
106
- commitSha: string;
107
- sourceRef: string;
108
- startedAt: string;
109
- finishedAt: string;
110
- createdAt: string;
111
- }[];
112
- scaleDecision: import("@treeseed/sdk").ScaleDecision;
113
- scaleResult: import("@treeseed/sdk").WorkerPoolScaleResult;
114
- generatedAt: string;
115
- };
116
- scale: import("@treeseed/sdk").WorkerPoolScaleResult;
117
- skipped?: undefined;
118
- reason?: undefined;
119
- } | {
120
- ok: boolean;
121
- skipped: boolean;
122
- reason: string;
123
- workDayId?: undefined;
124
- summary?: undefined;
125
- } | {
126
- ok: boolean;
127
- workDayId: unknown;
128
- summary: {
129
- contentSnapshot: {
130
- relativePath: string;
131
- slug: string;
132
- reportVersion: string;
133
- title: string;
134
- };
135
- capacity: {
136
- providerSplit: any;
137
- grantedDailyCredits: number;
138
- reservedCredits: number;
139
- consumedCredits: number;
140
- remainingDailyCredits: number | null;
141
- providerCount: number;
142
- laneCount: number;
143
- grantCount: number;
144
- };
145
- projectId: string;
146
- environment: "local" | "prod" | "staging";
147
- workDayId: string;
148
- state: string;
149
- totalTasks: number;
150
- completedTasks: number;
151
- failedTasks: number;
152
- queuedTasks: number;
153
- activeTasks: number;
154
- dailyTaskCreditBudget: number;
155
- usedTaskCredits: number;
156
- remainingTaskCredits: number;
157
- creditLedgerEntries: number;
158
- prioritySnapshotId: string;
159
- priorityItemCount: number;
160
- priorityItems: import("@treeseed/sdk").PrioritySnapshotItem[];
161
- taskItems: {
162
- id: string;
163
- agentId: string;
164
- type: string;
165
- state: string;
166
- priority: number;
167
- idempotencyKey: string;
168
- createdAt: string;
169
- startedAt: string;
170
- completedAt: string;
171
- lastErrorCode: string;
172
- lastErrorMessage: string;
173
- lastEventKind: string;
174
- outputCount: number;
175
- changedFiles: string[];
176
- }[];
177
- changedFiles: string[];
178
- releases: {
179
- id: string;
180
- deploymentKind: string;
181
- status: string;
182
- releaseTag: string;
183
- commitSha: string;
184
- sourceRef: string;
185
- startedAt: string;
186
- finishedAt: string;
187
- createdAt: string;
188
- }[];
189
- scaleDecision: import("@treeseed/sdk").ScaleDecision;
190
- scaleResult: import("@treeseed/sdk").WorkerPoolScaleResult;
191
- generatedAt: string;
192
- };
193
- skipped?: undefined;
194
- reason?: undefined;
195
- }>;
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env node
2
- import { fileURLToPath } from "node:url";
3
- import { runManagerAction } from "./manager.js";
4
- async function runWorkdayStart() {
5
- return runManagerAction({
6
- mode: "open-workday"
7
- });
8
- }
9
- const currentFile = fileURLToPath(import.meta.url);
10
- const entryFile = process.argv[1] ?? "";
11
- if (entryFile === currentFile) {
12
- process.stdout.write(`${JSON.stringify(await runWorkdayStart(), null, 2)}
13
- `);
14
- }
15
- export {
16
- runWorkdayStart
17
- };
@@ -1,58 +0,0 @@
1
- import type { AgentPoolAutoscalePolicy, AgentSdk, ProjectEnvironmentName, ScaleDecision, WorkerPoolScaleResult, WorkerPoolScaler } from '@treeseed/sdk';
2
- export interface TaskMetricsSnapshot {
3
- queuedTasks: Array<Record<string, unknown>>;
4
- activeTasks: Array<Record<string, unknown>>;
5
- queuedCount: number;
6
- activeLeases: number;
7
- queuedCredits: number;
8
- }
9
- export interface WorkerPoolIdentity {
10
- projectId: string;
11
- environment: ProjectEnvironmentName | 'local';
12
- poolName: string;
13
- }
14
- export interface CapacityAssuranceResult {
15
- ok: true;
16
- taskId: string;
17
- queued: true;
18
- workerState: 'warm' | 'cold_starting';
19
- desiredWorkers: number;
20
- scaleApplied: boolean;
21
- scaleReason: string;
22
- scaleDecision: ScaleDecision;
23
- scaleResult: WorkerPoolScaleResult;
24
- metrics: TaskMetricsSnapshot;
25
- }
26
- export declare function resolveAutoscalePolicyFromEnv(): AgentPoolAutoscalePolicy;
27
- export declare function resolveWorkerPoolIdentityFromEnv(projectId?: string): WorkerPoolIdentity;
28
- export declare function computeDesiredWorkerCount(autoscale: AgentPoolAutoscalePolicy, metrics: Pick<TaskMetricsSnapshot, 'queuedCount' | 'activeLeases'>): number;
29
- export declare function applyScaleCooldown(autoscale: AgentPoolAutoscalePolicy, latestDecision: ScaleDecision | null, nextDesired: number, now: Date): number;
30
- export declare function applyInteractiveWakeUpOverride(options: {
31
- priorityClass?: 'interactive' | 'background';
32
- queuedCount: number;
33
- currentWorkers: number;
34
- desiredWorkers: number;
35
- }): number;
36
- export declare function collectTaskMetrics(sdk: AgentSdk, workDayId?: string | null): Promise<TaskMetricsSnapshot>;
37
- export declare function enqueueTaskAndEnsureCapacity(sdk: AgentSdk, request: {
38
- taskId: string;
39
- actor?: string;
40
- queueName?: string;
41
- deliveryDelaySeconds?: number;
42
- priorityClass?: 'interactive' | 'background';
43
- projectId?: string;
44
- identity?: WorkerPoolIdentity;
45
- autoscale?: AgentPoolAutoscalePolicy;
46
- scaler?: WorkerPoolScaler;
47
- now?: Date;
48
- enqueueTask: (sdk: AgentSdk, request: {
49
- taskId: string;
50
- queueName?: string;
51
- deliveryDelaySeconds?: number;
52
- actor?: string;
53
- }) => Promise<{
54
- ok: boolean;
55
- taskId: string;
56
- queued: boolean;
57
- }>;
58
- }): Promise<CapacityAssuranceResult>;
@@ -1,208 +0,0 @@
1
- import { createWorkerPoolScaler } from "./worker-pool-scaler.js";
2
- function integerFromEnv(name, fallback) {
3
- const value = process.env[name];
4
- if (!value) return fallback;
5
- const parsed = Number.parseInt(value, 10);
6
- return Number.isFinite(parsed) ? parsed : fallback;
7
- }
8
- function envValue(name) {
9
- const value = process.env[name]?.trim();
10
- return value ? value : "";
11
- }
12
- function parseJson(value, fallback) {
13
- if (typeof value !== "string" || !value.trim()) {
14
- return fallback;
15
- }
16
- try {
17
- return JSON.parse(value);
18
- } catch {
19
- return fallback;
20
- }
21
- }
22
- function readNumber(record, ...keys) {
23
- for (const key of keys) {
24
- const value = record[key];
25
- if (typeof value === "number" && Number.isFinite(value)) {
26
- return value;
27
- }
28
- if (typeof value === "string" && value.trim()) {
29
- const parsed = Number.parseFloat(value);
30
- if (Number.isFinite(parsed)) {
31
- return parsed;
32
- }
33
- }
34
- }
35
- return null;
36
- }
37
- function asRecords(value) {
38
- return Array.isArray(value) ? value : [];
39
- }
40
- function resolveAutoscalePolicyFromEnv() {
41
- return {
42
- minWorkers: integerFromEnv("TREESEED_AGENT_POOL_MIN_WORKERS", 0),
43
- maxWorkers: integerFromEnv("TREESEED_AGENT_POOL_MAX_WORKERS", 1),
44
- targetQueueDepth: Math.max(1, integerFromEnv("TREESEED_AGENT_POOL_TARGET_QUEUE_DEPTH", 1)),
45
- cooldownSeconds: Math.max(0, integerFromEnv("TREESEED_AGENT_POOL_COOLDOWN_SECONDS", 60))
46
- };
47
- }
48
- function resolveWorkerPoolIdentityFromEnv(projectId) {
49
- const environment = envValue("TREESEED_DEPLOY_ENVIRONMENT") || (process.env.NODE_ENV === "production" ? "prod" : "local");
50
- const resolvedProjectId = projectId?.trim() || envValue("TREESEED_PROJECT_ID") || "treeseed-market";
51
- return {
52
- projectId: resolvedProjectId,
53
- environment,
54
- poolName: envValue("TREESEED_AGENT_POOL_NAME") || `${resolvedProjectId}-${environment}`
55
- };
56
- }
57
- function computeDesiredWorkerCount(autoscale, metrics) {
58
- const { minWorkers, maxWorkers, targetQueueDepth } = autoscale;
59
- if (metrics.queuedCount <= 0 && metrics.activeLeases <= 0) {
60
- return minWorkers;
61
- }
62
- const requiredByQueue = Math.ceil(metrics.queuedCount / Math.max(1, targetQueueDepth));
63
- const minimumActive = metrics.activeLeases > 0 ? 1 : 0;
64
- return Math.max(minWorkers, Math.min(maxWorkers, Math.max(requiredByQueue, minimumActive)));
65
- }
66
- function applyScaleCooldown(autoscale, latestDecision, nextDesired, now) {
67
- if (!latestDecision) {
68
- return nextDesired;
69
- }
70
- if (nextDesired >= latestDecision.desiredWorkers) {
71
- return nextDesired;
72
- }
73
- const cooldownMs = Math.max(0, autoscale.cooldownSeconds) * 1e3;
74
- if (cooldownMs === 0) {
75
- return nextDesired;
76
- }
77
- const lastChangedAt = new Date(latestDecision.createdAt);
78
- if (!Number.isFinite(lastChangedAt.valueOf())) {
79
- return nextDesired;
80
- }
81
- return now.valueOf() - lastChangedAt.valueOf() < cooldownMs ? latestDecision.desiredWorkers : nextDesired;
82
- }
83
- function applyInteractiveWakeUpOverride(options) {
84
- if (options.priorityClass !== "interactive") {
85
- return options.desiredWorkers;
86
- }
87
- if (options.queuedCount <= 0 || options.currentWorkers > 0 || options.desiredWorkers > 0) {
88
- return options.desiredWorkers;
89
- }
90
- return 1;
91
- }
92
- async function collectTaskMetrics(sdk, workDayId) {
93
- const [queuedEnvelope, activeEnvelope] = await Promise.all([
94
- sdk.searchTasks({
95
- workDayId: workDayId ?? void 0,
96
- limit: 500,
97
- state: ["pending", "queued"]
98
- }),
99
- sdk.searchTasks({
100
- workDayId: workDayId ?? void 0,
101
- limit: 500,
102
- state: ["claimed", "running"]
103
- })
104
- ]);
105
- const queuedTasks = asRecords(queuedEnvelope.payload);
106
- const activeTasks = asRecords(activeEnvelope.payload);
107
- const queuedCredits = queuedTasks.reduce((total, task) => {
108
- const payload = parseJson(String(task.payloadJson ?? "{}"), {});
109
- const credits = readNumber(payload, "estimatedCredits") ?? 1;
110
- return total + credits;
111
- }, 0);
112
- return {
113
- queuedTasks,
114
- activeTasks,
115
- queuedCount: queuedTasks.length,
116
- activeLeases: activeTasks.length,
117
- queuedCredits
118
- };
119
- }
120
- async function enqueueTaskAndEnsureCapacity(sdk, request) {
121
- await request.enqueueTask(sdk, {
122
- taskId: request.taskId,
123
- queueName: request.queueName,
124
- deliveryDelaySeconds: request.deliveryDelaySeconds,
125
- actor: request.actor
126
- });
127
- const now = request.now ?? /* @__PURE__ */ new Date();
128
- const identity = request.identity ?? resolveWorkerPoolIdentityFromEnv(request.projectId);
129
- const autoscale = request.autoscale ?? resolveAutoscalePolicyFromEnv();
130
- const scaler = request.scaler ?? createWorkerPoolScaler();
131
- const metrics = await collectTaskMetrics(sdk);
132
- const latestScaleDecision = await sdk.getLatestScaleDecision(identity.projectId, identity.environment, identity.poolName);
133
- const currentWorkers = Math.max(0, Number(latestScaleDecision.payload?.desiredWorkers ?? 0));
134
- const rawDesiredWorkers = computeDesiredWorkerCount(autoscale, metrics);
135
- const desiredAfterCooldown = applyScaleCooldown(autoscale, latestScaleDecision.payload, rawDesiredWorkers, now);
136
- const desiredWorkers = applyInteractiveWakeUpOverride({
137
- priorityClass: request.priorityClass ?? "background",
138
- queuedCount: metrics.queuedCount,
139
- currentWorkers,
140
- desiredWorkers: desiredAfterCooldown
141
- });
142
- const coldStartTriggered = (request.priorityClass ?? "background") === "interactive" && currentWorkers <= 0 && desiredWorkers > 0;
143
- const scaleReason = desiredWorkers !== desiredAfterCooldown ? "interactive_cold_start" : coldStartTriggered ? "interactive_cold_start" : desiredWorkers !== rawDesiredWorkers ? "cooldown_hold" : request.priorityClass === "interactive" ? "interactive_enqueue" : "enqueue";
144
- const recordedScaleDecision = await sdk.recordScaleDecision({
145
- projectId: identity.projectId,
146
- environment: identity.environment,
147
- poolName: identity.poolName,
148
- workDayId: null,
149
- desiredWorkers,
150
- observedQueueDepth: metrics.queuedCount,
151
- observedActiveLeases: metrics.activeLeases,
152
- reason: scaleReason,
153
- metadata: {
154
- priorityClass: request.priorityClass ?? "background",
155
- taskId: request.taskId
156
- }
157
- });
158
- const scaleDecision = recordedScaleDecision.payload ?? {
159
- projectId: identity.projectId,
160
- environment: identity.environment,
161
- poolName: identity.poolName,
162
- workDayId: null,
163
- desiredWorkers,
164
- observedQueueDepth: metrics.queuedCount,
165
- observedActiveLeases: metrics.activeLeases,
166
- reason: scaleReason,
167
- metadata: {
168
- priorityClass: request.priorityClass ?? "background",
169
- taskId: request.taskId
170
- },
171
- createdAt: now.toISOString()
172
- };
173
- let scaleResult;
174
- try {
175
- scaleResult = await scaler.scale(scaleDecision);
176
- } catch (error) {
177
- scaleResult = {
178
- applied: false,
179
- provider: "error",
180
- desiredWorkers,
181
- metadata: {
182
- reason: "scale_failed",
183
- error: error instanceof Error ? error.message : String(error)
184
- }
185
- };
186
- }
187
- return {
188
- ok: true,
189
- taskId: request.taskId,
190
- queued: true,
191
- workerState: currentWorkers > 0 ? "warm" : desiredWorkers > 0 ? "cold_starting" : "warm",
192
- desiredWorkers,
193
- scaleApplied: scaleResult.applied,
194
- scaleReason,
195
- scaleDecision,
196
- scaleResult,
197
- metrics
198
- };
199
- }
200
- export {
201
- applyInteractiveWakeUpOverride,
202
- applyScaleCooldown,
203
- collectTaskMetrics,
204
- computeDesiredWorkerCount,
205
- enqueueTaskAndEnsureCapacity,
206
- resolveAutoscalePolicyFromEnv,
207
- resolveWorkerPoolIdentityFromEnv
208
- };
@@ -1,27 +0,0 @@
1
- import type { ScaleDecision, WorkerPoolScaleResult, WorkerPoolScaler } from '@treeseed/sdk';
2
- export type WorkerPoolScalerKind = 'noop' | 'railway';
3
- export interface RailwayWorkerPoolScalerOptions {
4
- apiToken?: string | null;
5
- apiUrl?: string | null;
6
- serviceId?: string | null;
7
- environmentId?: string | null;
8
- projectId?: string | null;
9
- fetchImpl?: typeof fetch;
10
- }
11
- export declare class NoopWorkerPoolScaler implements WorkerPoolScaler {
12
- scale(decision: ScaleDecision): Promise<WorkerPoolScaleResult>;
13
- }
14
- export declare class RailwayWorkerPoolScaler implements WorkerPoolScaler {
15
- private readonly apiToken;
16
- private readonly apiUrl;
17
- private readonly serviceId;
18
- private readonly environmentId;
19
- private readonly projectId;
20
- private readonly fetchImpl;
21
- constructor(options?: RailwayWorkerPoolScalerOptions);
22
- private railwayMutation;
23
- private wakeRunner;
24
- private sleepRunner;
25
- scale(decision: ScaleDecision): Promise<WorkerPoolScaleResult>;
26
- }
27
- export declare function createWorkerPoolScaler(kind?: WorkerPoolScalerKind | null, options?: RailwayWorkerPoolScalerOptions): WorkerPoolScaler;
@@ -1,127 +0,0 @@
1
- function envValue(name) {
2
- const value = process.env[name]?.trim();
3
- return value ? value : "";
4
- }
5
- class NoopWorkerPoolScaler {
6
- async scale(decision) {
7
- return {
8
- applied: false,
9
- provider: "noop",
10
- desiredWorkers: decision.desiredWorkers,
11
- metadata: {
12
- reason: "scaler_unconfigured"
13
- }
14
- };
15
- }
16
- }
17
- class RailwayWorkerPoolScaler {
18
- apiToken;
19
- apiUrl;
20
- serviceId;
21
- environmentId;
22
- projectId;
23
- fetchImpl;
24
- constructor(options = {}) {
25
- this.apiToken = options.apiToken?.trim() || envValue("RAILWAY_API_TOKEN") || null;
26
- this.apiUrl = options.apiUrl?.trim() || envValue("TREESEED_RAILWAY_API_URL") || "https://backboard.railway.com/graphql/v2";
27
- this.serviceId = options.serviceId?.trim() || envValue("TREESEED_RAILWAY_WORKER_SERVICE_ID") || envValue("TREESEED_WORKER_SERVICE_ID") || null;
28
- this.environmentId = options.environmentId?.trim() || envValue("TREESEED_RAILWAY_ENVIRONMENT_ID") || null;
29
- this.projectId = options.projectId?.trim() || envValue("TREESEED_RAILWAY_PROJECT_ID") || null;
30
- this.fetchImpl = options.fetchImpl ?? fetch;
31
- }
32
- async railwayMutation(query, variables) {
33
- if (!this.apiToken) {
34
- throw new Error("Configure RAILWAY_API_TOKEN before waking Railway worker runners.");
35
- }
36
- const response = await this.fetchImpl(this.apiUrl, {
37
- method: "POST",
38
- headers: {
39
- authorization: `Bearer ${this.apiToken}`,
40
- "content-type": "application/json"
41
- },
42
- body: JSON.stringify({ query, variables })
43
- });
44
- const payload = await response.json().catch(() => ({}));
45
- if (!response.ok || Array.isArray(payload.errors) && payload.errors.length > 0) {
46
- throw new Error(payload.errors?.[0]?.message ?? `Railway runner request failed with ${response.status}.`);
47
- }
48
- return payload.data;
49
- }
50
- async wakeRunner() {
51
- if (!this.serviceId || !this.environmentId) {
52
- throw new Error("Railway runner wake requires serviceId and environmentId.");
53
- }
54
- const mutation = envValue("TREESEED_RAILWAY_RUNNER_WAKE_MUTATION") || `
55
- mutation TreeseedRailwayRunnerWake($serviceId: String!, $environmentId: String!) {
56
- serviceInstanceRedeploy(serviceId: $serviceId, environmentId: $environmentId)
57
- }
58
- `.trim();
59
- return await this.railwayMutation(mutation, {
60
- serviceId: this.serviceId,
61
- environmentId: this.environmentId,
62
- projectId: this.projectId
63
- });
64
- }
65
- async sleepRunner() {
66
- if (!this.serviceId || !this.environmentId) {
67
- throw new Error("Railway runner sleep requires serviceId and environmentId.");
68
- }
69
- const mutation = envValue("TREESEED_RAILWAY_RUNNER_SLEEP_MUTATION") || `
70
- mutation TreeseedRailwayRunnerSleep($serviceId: String!, $environmentId: String!) {
71
- deploymentRemove(serviceId: $serviceId, environmentId: $environmentId)
72
- }
73
- `.trim();
74
- return await this.railwayMutation(mutation, {
75
- serviceId: this.serviceId,
76
- environmentId: this.environmentId,
77
- projectId: this.projectId
78
- });
79
- }
80
- async scale(decision) {
81
- const desiredWorkers = Math.max(0, Number(decision.desiredWorkers ?? 0));
82
- const action = desiredWorkers > 0 ? "wake" : "sleep";
83
- try {
84
- const result = action === "wake" ? await this.wakeRunner() : await this.sleepRunner();
85
- return {
86
- applied: true,
87
- provider: "railway",
88
- desiredWorkers,
89
- metadata: {
90
- action,
91
- projectId: this.projectId,
92
- serviceId: this.serviceId,
93
- environmentId: this.environmentId,
94
- result
95
- }
96
- };
97
- } catch (error) {
98
- return {
99
- applied: false,
100
- provider: "railway",
101
- desiredWorkers,
102
- metadata: {
103
- action,
104
- reason: "named_runner_action_failed",
105
- projectId: this.projectId,
106
- serviceId: this.serviceId,
107
- environmentId: this.environmentId,
108
- error: error instanceof Error ? error.message : String(error)
109
- }
110
- };
111
- }
112
- }
113
- }
114
- function createWorkerPoolScaler(kind, options = {}) {
115
- const configuredKind = envValue("TREESEED_WORKER_POOL_SCALER") || null;
116
- const inferredKind = envValue("RAILWAY_API_TOKEN") && (envValue("TREESEED_RAILWAY_WORKER_SERVICE_ID") || envValue("TREESEED_WORKER_SERVICE_ID")) && envValue("TREESEED_RAILWAY_ENVIRONMENT_ID") ? "railway" : "noop";
117
- const resolvedKind = kind ?? configuredKind ?? inferredKind;
118
- if (resolvedKind === "railway") {
119
- return new RailwayWorkerPoolScaler(options);
120
- }
121
- return new NoopWorkerPoolScaler();
122
- }
123
- export {
124
- NoopWorkerPoolScaler,
125
- RailwayWorkerPoolScaler,
126
- createWorkerPoolScaler
127
- };
@@ -1,19 +0,0 @@
1
- #!/usr/bin/env node
2
- export declare function runWorkerCycle(): Promise<{
3
- ok: boolean;
4
- processed: number;
5
- idle: boolean;
6
- reason: string;
7
- } | {
8
- ok: boolean;
9
- processed: number;
10
- idle?: undefined;
11
- reason?: undefined;
12
- }>;
13
- export declare function shouldExitWorkerLoopAfterIdle(options: {
14
- idleExitMs?: number | null;
15
- idleSince: number | null;
16
- now: number;
17
- processed: number;
18
- }): boolean;
19
- export declare function startWorkerLoop(): Promise<void>;