@moinulmoin/eden-runtime-cloudflare 0.1.4 → 0.2.0
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.
- package/NOTICE +0 -31
- package/dist/eden-eve-host-worker.mjs +1 -9
- package/dist/eve-host-runtime.d.ts +0 -1
- package/dist/eve-host-runtime.js +0 -1
- package/dist/eve-host.d.ts +2 -1
- package/dist/eve-host.js +2 -8
- package/dist/index.d.ts +0 -18
- package/dist/index.js +0 -5
- package/package.json +2 -6
- package/dist/artifact-runtime.d.ts +0 -27
- package/dist/artifact-runtime.js +0 -14
- package/dist/http-host.d.ts +0 -6
- package/dist/http-host.js +0 -419
- package/dist/model-adapter-internal.d.ts +0 -12
- package/dist/model-adapter-internal.js +0 -172
- package/dist/model-adapter.d.ts +0 -9
- package/dist/model-adapter.js +0 -56
- package/dist/model-contracts.d.ts +0 -92
- package/dist/model-contracts.js +0 -2
- package/dist/model-normalizers.d.ts +0 -7
- package/dist/model-normalizers.js +0 -453
- package/dist/session-checkpoint.d.ts +0 -48
- package/dist/session-checkpoint.js +0 -300
- package/dist/session-identity.d.ts +0 -9
- package/dist/session-identity.js +0 -45
- package/dist/session-jobs.d.ts +0 -69
- package/dist/session-jobs.js +0 -427
- package/dist/session-journal.d.ts +0 -154
- package/dist/session-journal.js +0 -391
- package/dist/session-schema.d.ts +0 -21
- package/dist/session-schema.js +0 -247
- package/dist/session-state.d.ts +0 -165
- package/dist/session-state.js +0 -168
- package/dist/session.d.ts +0 -40
- package/dist/session.js +0 -792
- package/dist/test-execution.d.ts +0 -8
- package/dist/test-execution.js +0 -12
- package/dist/test-worker.d.ts +0 -12
- package/dist/test-worker.js +0 -214
- package/dist/tool-harness.d.ts +0 -31
- package/dist/tool-harness.js +0 -226
- package/dist/turn-runner-storage.d.ts +0 -28
- package/dist/turn-runner-storage.js +0 -583
- package/dist/turn-runner-types.d.ts +0 -112
- package/dist/turn-runner-types.js +0 -21
- package/dist/turn-runner.d.ts +0 -6
- package/dist/turn-runner.js +0 -370
package/dist/session-jobs.js
DELETED
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
export const MAX_RECOVERY_JOBS_PER_ALARM = 4;
|
|
2
|
-
export const MAX_RECOVERY_JOBS_PER_INSPECTION_PAGE = 32;
|
|
3
|
-
export const MAX_RECOVERY_JOB_ATTEMPTS = 3;
|
|
4
|
-
export const RECOVERY_RETRY_DELAY_MS = 1_000;
|
|
5
|
-
const MAX_RECOVERY_JOB_ID_BYTES = 256;
|
|
6
|
-
const MAX_RECOVERY_JOB_KIND_BYTES = 128;
|
|
7
|
-
const MAX_RECOVERY_ACTION_BYTES = 256;
|
|
8
|
-
const MAX_RECOVERY_ERROR_BYTES = 512;
|
|
9
|
-
const MAX_RECOVERY_TIMESTAMP_BYTES = 64;
|
|
10
|
-
const MAX_RECOVERY_INSPECTION_CURSOR_BYTES = 1_024;
|
|
11
|
-
export const REGISTERED_RECOVERY_ACTIONS = [
|
|
12
|
-
"mark-complete",
|
|
13
|
-
"always-fail",
|
|
14
|
-
"fail",
|
|
15
|
-
"run-bounded-turn",
|
|
16
|
-
];
|
|
17
|
-
function bytes(value) {
|
|
18
|
-
return new TextEncoder().encode(value).byteLength;
|
|
19
|
-
}
|
|
20
|
-
function assertBoundedText(value, label, maxBytes) {
|
|
21
|
-
if (value.length === 0 || bytes(value) > maxBytes) {
|
|
22
|
-
throw new Error(`${label} is invalid`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function assertJobId(jobId) {
|
|
26
|
-
assertBoundedText(jobId, "Recovery job identity", MAX_RECOVERY_JOB_ID_BYTES);
|
|
27
|
-
}
|
|
28
|
-
export function isRegisteredRecoveryAction(action) {
|
|
29
|
-
return (typeof action === "string" &&
|
|
30
|
-
REGISTERED_RECOVERY_ACTIONS.includes(action));
|
|
31
|
-
}
|
|
32
|
-
function assertRecoveryAction(action) {
|
|
33
|
-
if (!isRegisteredRecoveryAction(action)) {
|
|
34
|
-
throw new Error("Unknown recovery action");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function assertJobInput(input) {
|
|
38
|
-
assertJobId(input.jobId);
|
|
39
|
-
assertBoundedText(input.kind, "Recovery job kind", MAX_RECOVERY_JOB_KIND_BYTES);
|
|
40
|
-
assertBoundedText(input.recoveryAction, "Recovery action", MAX_RECOVERY_ACTION_BYTES);
|
|
41
|
-
if (!Number.isSafeInteger(input.dueAt) || input.dueAt < 0) {
|
|
42
|
-
throw new Error("Recovery job due time is invalid");
|
|
43
|
-
}
|
|
44
|
-
const maxAttempts = input.maxAttempts ?? MAX_RECOVERY_JOB_ATTEMPTS;
|
|
45
|
-
if (!Number.isSafeInteger(maxAttempts) ||
|
|
46
|
-
maxAttempts < 1 ||
|
|
47
|
-
maxAttempts > MAX_RECOVERY_JOB_ATTEMPTS) {
|
|
48
|
-
throw new Error("Recovery job attempt bound is invalid");
|
|
49
|
-
}
|
|
50
|
-
if (input.createdAt !== undefined) {
|
|
51
|
-
assertBoundedText(input.createdAt, "Recovery job creation time", MAX_RECOVERY_TIMESTAMP_BYTES);
|
|
52
|
-
}
|
|
53
|
-
assertRecoveryAction(input.recoveryAction);
|
|
54
|
-
}
|
|
55
|
-
function nowIso(now = Date.now()) {
|
|
56
|
-
return new Date(now).toISOString();
|
|
57
|
-
}
|
|
58
|
-
function readJob(sql, sessionId, jobId) {
|
|
59
|
-
const row = sql
|
|
60
|
-
.exec(`SELECT job_id, session_id, kind, status, due_at, attempts,
|
|
61
|
-
max_attempts, last_error, recovery_action, created_at, updated_at,
|
|
62
|
-
completed_at
|
|
63
|
-
FROM jobs
|
|
64
|
-
WHERE session_id = ? AND job_id = ?`, sessionId, jobId)
|
|
65
|
-
.toArray()[0];
|
|
66
|
-
return row === undefined ? null : mapJob(row);
|
|
67
|
-
}
|
|
68
|
-
function mapJob(row) {
|
|
69
|
-
return {
|
|
70
|
-
jobId: row.job_id,
|
|
71
|
-
kind: row.kind,
|
|
72
|
-
status: row.status,
|
|
73
|
-
dueAt: row.due_at,
|
|
74
|
-
attempts: row.attempts,
|
|
75
|
-
maxAttempts: row.max_attempts,
|
|
76
|
-
lastError: row.last_error,
|
|
77
|
-
recoveryAction: row.recovery_action,
|
|
78
|
-
createdAt: row.created_at,
|
|
79
|
-
updatedAt: row.updated_at,
|
|
80
|
-
completedAt: row.completed_at,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function readDueJob(sql, sessionId, now, excludedJobIds) {
|
|
84
|
-
const exclusion = Array.from(excludedJobIds);
|
|
85
|
-
const exclusionClause = exclusion.length === 0
|
|
86
|
-
? ""
|
|
87
|
-
: ` AND job_id NOT IN (${exclusion.map(() => "?").join(", ")})`;
|
|
88
|
-
const row = sql
|
|
89
|
-
.exec(`SELECT job_id, session_id, kind, status, due_at, attempts,
|
|
90
|
-
max_attempts, last_error, recovery_action, created_at, updated_at,
|
|
91
|
-
completed_at
|
|
92
|
-
FROM jobs
|
|
93
|
-
WHERE session_id = ?
|
|
94
|
-
AND status IN ('pending', 'retryable', 'running')
|
|
95
|
-
AND due_at <= ?${exclusionClause}
|
|
96
|
-
ORDER BY due_at ASC, job_id ASC
|
|
97
|
-
LIMIT 1`, sessionId, now, ...exclusion)
|
|
98
|
-
.toArray()[0];
|
|
99
|
-
return row === undefined ? null : mapJob(row);
|
|
100
|
-
}
|
|
101
|
-
function readNextDueAt(sql, sessionId) {
|
|
102
|
-
const row = sql
|
|
103
|
-
.exec(`SELECT MIN(due_at) AS due_at
|
|
104
|
-
FROM jobs
|
|
105
|
-
WHERE session_id = ?
|
|
106
|
-
AND status IN ('pending', 'retryable', 'running')`, sessionId)
|
|
107
|
-
.toArray()[0];
|
|
108
|
-
return row?.due_at ?? null;
|
|
109
|
-
}
|
|
110
|
-
function encodeInspectionCursor(job) {
|
|
111
|
-
const payload = {
|
|
112
|
-
version: 2,
|
|
113
|
-
createdAt: job.createdAt,
|
|
114
|
-
jobId: job.jobId,
|
|
115
|
-
};
|
|
116
|
-
const encoded = new TextEncoder().encode(JSON.stringify(payload));
|
|
117
|
-
let binary = "";
|
|
118
|
-
for (const byte of encoded)
|
|
119
|
-
binary += String.fromCharCode(byte);
|
|
120
|
-
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/u, "");
|
|
121
|
-
}
|
|
122
|
-
function decodeInspectionCursor(cursor) {
|
|
123
|
-
if (cursor.length === 0 || bytes(cursor) > MAX_RECOVERY_INSPECTION_CURSOR_BYTES) {
|
|
124
|
-
throw new Error("Recovery inspection cursor is invalid");
|
|
125
|
-
}
|
|
126
|
-
try {
|
|
127
|
-
const padded = cursor.replace(/-/g, "+").replace(/_/g, "/");
|
|
128
|
-
const padding = padded.length % 4;
|
|
129
|
-
const binary = atob(padded + (padding === 0 ? "" : "=".repeat(4 - padding)));
|
|
130
|
-
const encoded = Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
131
|
-
const parsed = JSON.parse(new TextDecoder().decode(encoded));
|
|
132
|
-
if (typeof parsed !== "object" ||
|
|
133
|
-
parsed === null ||
|
|
134
|
-
Array.isArray(parsed) ||
|
|
135
|
-
parsed.version !== 2 ||
|
|
136
|
-
typeof parsed.createdAt !== "string" ||
|
|
137
|
-
typeof parsed.jobId !== "string") {
|
|
138
|
-
throw new Error("Recovery inspection cursor is invalid");
|
|
139
|
-
}
|
|
140
|
-
assertBoundedText(parsed.createdAt, "Recovery inspection cursor creation time", MAX_RECOVERY_TIMESTAMP_BYTES);
|
|
141
|
-
assertJobId(parsed.jobId);
|
|
142
|
-
return parsed;
|
|
143
|
-
}
|
|
144
|
-
catch {
|
|
145
|
-
throw new Error("Recovery inspection cursor is invalid");
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
function readInspectionPage(sql, sessionId, options) {
|
|
149
|
-
const requestedLimit = options.limit ?? MAX_RECOVERY_JOBS_PER_INSPECTION_PAGE;
|
|
150
|
-
if (!Number.isSafeInteger(requestedLimit) || requestedLimit < 1) {
|
|
151
|
-
throw new Error("Recovery inspection page limit is invalid");
|
|
152
|
-
}
|
|
153
|
-
const limit = Math.min(requestedLimit, MAX_RECOVERY_JOBS_PER_INSPECTION_PAGE);
|
|
154
|
-
const cursor = options.cursor === undefined
|
|
155
|
-
? null
|
|
156
|
-
: decodeInspectionCursor(options.cursor);
|
|
157
|
-
const rows = cursor === null
|
|
158
|
-
? sql.exec(`SELECT job_id, session_id, kind, status, due_at, attempts,
|
|
159
|
-
max_attempts, last_error, recovery_action, created_at, updated_at,
|
|
160
|
-
completed_at
|
|
161
|
-
FROM jobs
|
|
162
|
-
WHERE session_id = ?
|
|
163
|
-
ORDER BY created_at ASC, job_id ASC
|
|
164
|
-
LIMIT ?`, sessionId, limit + 1).toArray()
|
|
165
|
-
: sql.exec(`SELECT job_id, session_id, kind, status, due_at, attempts,
|
|
166
|
-
max_attempts, last_error, recovery_action, created_at, updated_at,
|
|
167
|
-
completed_at
|
|
168
|
-
FROM jobs
|
|
169
|
-
WHERE session_id = ?
|
|
170
|
-
AND (created_at > ? OR (created_at = ? AND job_id > ?))
|
|
171
|
-
ORDER BY created_at ASC, job_id ASC
|
|
172
|
-
LIMIT ?`, sessionId, cursor.createdAt, cursor.createdAt, cursor.jobId, limit + 1).toArray();
|
|
173
|
-
const jobs = rows.slice(0, limit).map(mapJob);
|
|
174
|
-
const lastJob = jobs[jobs.length - 1];
|
|
175
|
-
return {
|
|
176
|
-
jobs,
|
|
177
|
-
nextCursor: rows.length > limit && lastJob !== undefined
|
|
178
|
-
? encodeInspectionCursor(lastJob)
|
|
179
|
-
: null,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
export function quarantineLegacyCompletedUnknownActions(storage, sessionId) {
|
|
183
|
-
storage.transactionSync(() => {
|
|
184
|
-
const placeholders = REGISTERED_RECOVERY_ACTIONS.map(() => "?").join(", ");
|
|
185
|
-
const legacyRows = storage.sql
|
|
186
|
-
.exec(`SELECT job_id, session_id, kind, status, due_at, attempts,
|
|
187
|
-
max_attempts, last_error, recovery_action, created_at, updated_at,
|
|
188
|
-
completed_at
|
|
189
|
-
FROM jobs
|
|
190
|
-
WHERE session_id = ?
|
|
191
|
-
AND status = 'completed'
|
|
192
|
-
AND (
|
|
193
|
-
recovery_action IS NULL
|
|
194
|
-
OR recovery_action NOT IN (${placeholders})
|
|
195
|
-
)`, sessionId, ...REGISTERED_RECOVERY_ACTIONS)
|
|
196
|
-
.toArray();
|
|
197
|
-
if (legacyRows.length === 0)
|
|
198
|
-
return;
|
|
199
|
-
const timestamp = nowIso();
|
|
200
|
-
for (const row of legacyRows) {
|
|
201
|
-
const summary = "Unknown recovery action";
|
|
202
|
-
storage.sql.exec(`UPDATE jobs
|
|
203
|
-
SET status = 'dead', last_error = ?, updated_at = ?,
|
|
204
|
-
completed_at = NULL
|
|
205
|
-
WHERE session_id = ? AND job_id = ? AND status = 'completed'`, summary, timestamp, sessionId, row.job_id);
|
|
206
|
-
const job = readJob(storage.sql, sessionId, row.job_id);
|
|
207
|
-
if (job !== null) {
|
|
208
|
-
upsertJobError(storage.sql, sessionId, job, summary, timestamp);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
function requireSession(sql, sessionId) {
|
|
214
|
-
const row = sql
|
|
215
|
-
.exec("SELECT EXISTS (SELECT 1 FROM session_meta WHERE session_id = ?) AS present", sessionId)
|
|
216
|
-
.toArray()[0];
|
|
217
|
-
if (row?.present !== 1) {
|
|
218
|
-
throw new Error("Recovery job session is not initialized");
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
function sanitizeText(value) {
|
|
222
|
-
const compact = value
|
|
223
|
-
.replace(/\p{Cc}/gu, " ")
|
|
224
|
-
.replace(/\s+/gu, " ")
|
|
225
|
-
.trim();
|
|
226
|
-
if (/authorization|bearer|binding|cookie|credential|password|secret|token|api[-_ ]?key/iu.test(compact)) {
|
|
227
|
-
return "Recovery job failed with a redacted error";
|
|
228
|
-
}
|
|
229
|
-
const encoded = new TextEncoder().encode(compact);
|
|
230
|
-
if (encoded.byteLength <= MAX_RECOVERY_ERROR_BYTES)
|
|
231
|
-
return compact;
|
|
232
|
-
return new TextDecoder().decode(encoded.slice(0, MAX_RECOVERY_ERROR_BYTES));
|
|
233
|
-
}
|
|
234
|
-
export function sanitizeRecoveryError(error) {
|
|
235
|
-
const candidate = error instanceof Error
|
|
236
|
-
? error.message
|
|
237
|
-
: typeof error === "string"
|
|
238
|
-
? error
|
|
239
|
-
: "Recovery job failed";
|
|
240
|
-
const sanitized = sanitizeText(candidate);
|
|
241
|
-
return sanitized.length > 0 ? sanitized : "Recovery job failed";
|
|
242
|
-
}
|
|
243
|
-
function errorId(jobId) {
|
|
244
|
-
return `err_job_${jobId}`;
|
|
245
|
-
}
|
|
246
|
-
function upsertJobError(sql, sessionId, job, summary, timestamp) {
|
|
247
|
-
sql.exec(`INSERT INTO errors (
|
|
248
|
-
error_id, session_id, turn_id, step_id, code, message, retryable,
|
|
249
|
-
status, created_at, resolved_at
|
|
250
|
-
) VALUES (?, ?, NULL, NULL, ?, ?, ?, 'open', ?, NULL)
|
|
251
|
-
ON CONFLICT(error_id) DO UPDATE SET
|
|
252
|
-
code = excluded.code,
|
|
253
|
-
message = excluded.message,
|
|
254
|
-
retryable = excluded.retryable,
|
|
255
|
-
status = 'open',
|
|
256
|
-
resolved_at = NULL`, errorId(job.jobId), sessionId, "recovery_job_failed", summary, job.status !== "dead" && job.attempts < job.maxAttempts ? 1 : 0, timestamp);
|
|
257
|
-
}
|
|
258
|
-
function resolveJobError(sql, sessionId, jobId, timestamp) {
|
|
259
|
-
sql.exec(`UPDATE errors
|
|
260
|
-
SET status = 'resolved', resolved_at = ?
|
|
261
|
-
WHERE session_id = ? AND error_id = ?`, timestamp, sessionId, errorId(jobId));
|
|
262
|
-
}
|
|
263
|
-
function claimJob(storage, sessionId, now, excludedJobIds) {
|
|
264
|
-
return storage.transactionSync(() => {
|
|
265
|
-
const candidate = readDueJob(storage.sql, sessionId, now, excludedJobIds);
|
|
266
|
-
if (candidate === null)
|
|
267
|
-
return null;
|
|
268
|
-
const timestamp = nowIso(now);
|
|
269
|
-
if (candidate.attempts >= candidate.maxAttempts) {
|
|
270
|
-
const summary = isRegisteredRecoveryAction(candidate.recoveryAction)
|
|
271
|
-
? sanitizeRecoveryError(candidate.lastError ?? "Recovery job retry limit reached")
|
|
272
|
-
: "Unknown recovery action";
|
|
273
|
-
storage.sql.exec(`UPDATE jobs
|
|
274
|
-
SET status = 'dead', last_error = ?,
|
|
275
|
-
updated_at = ?
|
|
276
|
-
WHERE session_id = ? AND job_id = ?
|
|
277
|
-
AND status IN ('pending', 'retryable', 'running')`, summary, timestamp, sessionId, candidate.jobId);
|
|
278
|
-
upsertJobError(storage.sql, sessionId, { ...candidate, status: "dead", lastError: summary }, summary, timestamp);
|
|
279
|
-
return readJob(storage.sql, sessionId, candidate.jobId);
|
|
280
|
-
}
|
|
281
|
-
storage.sql.exec(`UPDATE jobs
|
|
282
|
-
SET status = 'running', attempts = attempts + 1, updated_at = ?
|
|
283
|
-
WHERE session_id = ? AND job_id = ?
|
|
284
|
-
AND status IN ('pending', 'retryable', 'running')
|
|
285
|
-
AND due_at <= ?`, timestamp, sessionId, candidate.jobId, now);
|
|
286
|
-
return readJob(storage.sql, sessionId, candidate.jobId);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
function completeJob(storage, sessionId, job, now) {
|
|
290
|
-
storage.transactionSync(() => {
|
|
291
|
-
const timestamp = nowIso(now);
|
|
292
|
-
storage.sql.exec(`UPDATE jobs
|
|
293
|
-
SET status = 'completed', last_error = NULL, updated_at = ?,
|
|
294
|
-
completed_at = ?
|
|
295
|
-
WHERE session_id = ? AND job_id = ? AND status = 'running'
|
|
296
|
-
AND attempts = ?`, timestamp, timestamp, sessionId, job.jobId, job.attempts);
|
|
297
|
-
resolveJobError(storage.sql, sessionId, job.jobId, timestamp);
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
function failJob(storage, sessionId, job, error, now) {
|
|
301
|
-
return storage.transactionSync(() => {
|
|
302
|
-
const timestamp = nowIso(now);
|
|
303
|
-
const summary = sanitizeRecoveryError(error);
|
|
304
|
-
const status = job.attempts >= job.maxAttempts ? "dead" : "retryable";
|
|
305
|
-
storage.sql.exec(`UPDATE jobs
|
|
306
|
-
SET status = ?, due_at = ?, last_error = ?, updated_at = ?,
|
|
307
|
-
completed_at = NULL
|
|
308
|
-
WHERE session_id = ? AND job_id = ? AND status = 'running'
|
|
309
|
-
AND attempts = ?`, status, now + RECOVERY_RETRY_DELAY_MS, summary, timestamp, sessionId, job.jobId, job.attempts);
|
|
310
|
-
upsertJobError(storage.sql, sessionId, { ...job, status, lastError: summary }, summary, timestamp);
|
|
311
|
-
return status;
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
export function inspectRecoveryJobs(storage, sessionId, options = {}) {
|
|
315
|
-
quarantineLegacyCompletedUnknownActions(storage, sessionId);
|
|
316
|
-
requireSession(storage.sql, sessionId);
|
|
317
|
-
const page = readInspectionPage(storage.sql, sessionId, options);
|
|
318
|
-
return {
|
|
319
|
-
jobs: page.jobs,
|
|
320
|
-
nextDueAt: readNextDueAt(storage.sql, sessionId),
|
|
321
|
-
nextCursor: page.nextCursor,
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
export function enqueueRecoveryJob(storage, sessionId, input) {
|
|
325
|
-
assertJobInput(input);
|
|
326
|
-
requireSession(storage.sql, sessionId);
|
|
327
|
-
return storage.transactionSync(() => {
|
|
328
|
-
const existing = readJob(storage.sql, sessionId, input.jobId);
|
|
329
|
-
const maxAttempts = input.maxAttempts ?? MAX_RECOVERY_JOB_ATTEMPTS;
|
|
330
|
-
if (existing !== null) {
|
|
331
|
-
if (existing.kind !== input.kind ||
|
|
332
|
-
existing.maxAttempts !== maxAttempts ||
|
|
333
|
-
existing.recoveryAction !== input.recoveryAction) {
|
|
334
|
-
throw new Error("Recovery job identity conflicts with durable state");
|
|
335
|
-
}
|
|
336
|
-
return { status: "deduplicated", job: existing };
|
|
337
|
-
}
|
|
338
|
-
const timestamp = input.createdAt ?? nowIso();
|
|
339
|
-
storage.sql.exec(`INSERT INTO jobs (
|
|
340
|
-
job_id, session_id, kind, status, due_at, attempts, max_attempts,
|
|
341
|
-
last_error, recovery_action, created_at, updated_at, completed_at
|
|
342
|
-
) VALUES (?, ?, ?, 'pending', ?, 0, ?, NULL, ?, ?, ?, NULL)`, input.jobId, sessionId, input.kind, input.dueAt, maxAttempts, input.recoveryAction, timestamp, timestamp);
|
|
343
|
-
const job = readJob(storage.sql, sessionId, input.jobId);
|
|
344
|
-
if (job === null)
|
|
345
|
-
throw new Error("Recovery job was not persisted");
|
|
346
|
-
return { status: "scheduled", job };
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
export function recoverRecoveryJob(storage, sessionId, jobId, recoveryAction, dueAt = Date.now() + RECOVERY_RETRY_DELAY_MS) {
|
|
350
|
-
assertJobId(jobId);
|
|
351
|
-
if (!Number.isSafeInteger(dueAt) ||
|
|
352
|
-
dueAt < 0 ||
|
|
353
|
-
(recoveryAction !== undefined &&
|
|
354
|
-
(recoveryAction.length === 0 ||
|
|
355
|
-
bytes(recoveryAction) > MAX_RECOVERY_ACTION_BYTES))) {
|
|
356
|
-
throw new Error("Recovery operation is invalid");
|
|
357
|
-
}
|
|
358
|
-
quarantineLegacyCompletedUnknownActions(storage, sessionId);
|
|
359
|
-
requireSession(storage.sql, sessionId);
|
|
360
|
-
return storage.transactionSync(() => {
|
|
361
|
-
const existing = readJob(storage.sql, sessionId, jobId);
|
|
362
|
-
if (existing === null)
|
|
363
|
-
throw new Error("Recovery job was not found");
|
|
364
|
-
const action = recoveryAction ?? existing.recoveryAction;
|
|
365
|
-
if (action === null)
|
|
366
|
-
throw new Error("Recovery action is required");
|
|
367
|
-
assertRecoveryAction(action);
|
|
368
|
-
storage.sql.exec(`UPDATE jobs
|
|
369
|
-
SET status = 'pending', due_at = ?, attempts = 0, last_error = NULL,
|
|
370
|
-
recovery_action = ?, updated_at = ?, completed_at = NULL
|
|
371
|
-
WHERE session_id = ? AND job_id = ? AND status = 'dead'`, dueAt, action, nowIso(), sessionId, jobId);
|
|
372
|
-
const job = readJob(storage.sql, sessionId, jobId);
|
|
373
|
-
if (job === null || job.status !== "pending") {
|
|
374
|
-
throw new Error("Recovery job is not eligible for recovery");
|
|
375
|
-
}
|
|
376
|
-
return { status: "scheduled", job };
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
export async function processRecoveryJobs(storage, sessionId, execute, options = {}) {
|
|
380
|
-
const now = options.now ?? Date.now();
|
|
381
|
-
const requestedLimit = options.limit ?? MAX_RECOVERY_JOBS_PER_ALARM;
|
|
382
|
-
if (!Number.isSafeInteger(now) || now < 0) {
|
|
383
|
-
throw new Error("Recovery job clock value is invalid");
|
|
384
|
-
}
|
|
385
|
-
if (!Number.isSafeInteger(requestedLimit) || requestedLimit < 1) {
|
|
386
|
-
throw new Error("Recovery job batch limit is invalid");
|
|
387
|
-
}
|
|
388
|
-
const limit = Math.min(requestedLimit, MAX_RECOVERY_JOBS_PER_ALARM);
|
|
389
|
-
quarantineLegacyCompletedUnknownActions(storage, sessionId);
|
|
390
|
-
requireSession(storage.sql, sessionId);
|
|
391
|
-
let processed = 0;
|
|
392
|
-
let completed = 0;
|
|
393
|
-
let retryable = 0;
|
|
394
|
-
let dead = 0;
|
|
395
|
-
const processedJobIds = new Set();
|
|
396
|
-
while (processed < limit) {
|
|
397
|
-
const job = claimJob(storage, sessionId, now, processedJobIds);
|
|
398
|
-
if (job === null)
|
|
399
|
-
break;
|
|
400
|
-
processedJobIds.add(job.jobId);
|
|
401
|
-
if (job.status === "dead") {
|
|
402
|
-
dead += 1;
|
|
403
|
-
processed += 1;
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
processed += 1;
|
|
407
|
-
try {
|
|
408
|
-
assertRecoveryAction(job.recoveryAction);
|
|
409
|
-
await execute(job);
|
|
410
|
-
completeJob(storage, sessionId, job, now);
|
|
411
|
-
completed += 1;
|
|
412
|
-
}
|
|
413
|
-
catch (error) {
|
|
414
|
-
const status = failJob(storage, sessionId, job, error, now);
|
|
415
|
-
if (status === "dead")
|
|
416
|
-
dead += 1;
|
|
417
|
-
else
|
|
418
|
-
retryable += 1;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return { processed, completed, retryable, dead };
|
|
422
|
-
}
|
|
423
|
-
export function nextRecoveryJobDueAt(sql, sessionId) {
|
|
424
|
-
requireSession(sql, sessionId);
|
|
425
|
-
return readNextDueAt(sql, sessionId);
|
|
426
|
-
}
|
|
427
|
-
//# sourceMappingURL=session-jobs.js.map
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import type { EdenEvent, EdenEventDataByType, EdenEventType, EdenJsonValue, EdenStepPhase, EdenTurnStatus } from "@moinulmoin/eden-definitions";
|
|
2
|
-
export declare const MAX_JOURNAL_EVENTS_PER_PAGE = 256;
|
|
3
|
-
export type EdenSqlValue = ArrayBuffer | string | number | null;
|
|
4
|
-
export interface EdenSqlCursor<TRow extends Record<string, EdenSqlValue>> {
|
|
5
|
-
toArray(): TRow[];
|
|
6
|
-
}
|
|
7
|
-
export interface EdenSqlStorage {
|
|
8
|
-
exec<TRow extends Record<string, EdenSqlValue>>(query: string, ...bindings: EdenSqlValue[]): EdenSqlCursor<TRow>;
|
|
9
|
-
}
|
|
10
|
-
export interface EdenSessionStorage {
|
|
11
|
-
readonly sql: EdenSqlStorage;
|
|
12
|
-
transactionSync<T>(closure: () => T): T;
|
|
13
|
-
}
|
|
14
|
-
export interface AppendJournalEventInput<TType extends EdenEventType> {
|
|
15
|
-
readonly type: TType;
|
|
16
|
-
readonly data: EdenEventDataByType[TType];
|
|
17
|
-
readonly turnId?: string;
|
|
18
|
-
readonly stepId?: string;
|
|
19
|
-
readonly committedAt?: string;
|
|
20
|
-
}
|
|
21
|
-
export interface InsertTurnInput {
|
|
22
|
-
readonly turnId: string;
|
|
23
|
-
readonly status: EdenTurnStatus;
|
|
24
|
-
readonly acceptedAt: string;
|
|
25
|
-
readonly startedAt?: string;
|
|
26
|
-
}
|
|
27
|
-
export interface UpdateTurnInput {
|
|
28
|
-
readonly turnId: string;
|
|
29
|
-
readonly status: EdenTurnStatus;
|
|
30
|
-
readonly startedAt?: string | null;
|
|
31
|
-
readonly completedAt?: string | null;
|
|
32
|
-
readonly failedAt?: string | null;
|
|
33
|
-
readonly errorId?: string | null;
|
|
34
|
-
}
|
|
35
|
-
export interface InsertStepInput {
|
|
36
|
-
readonly stepId: string;
|
|
37
|
-
readonly turnId: string;
|
|
38
|
-
readonly logicalKey: string;
|
|
39
|
-
readonly phase: EdenStepPhase;
|
|
40
|
-
readonly status: "pending" | "running" | "completed" | "retryable" | "failed";
|
|
41
|
-
readonly attemptCount?: number;
|
|
42
|
-
readonly resultRef?: string;
|
|
43
|
-
readonly errorId?: string;
|
|
44
|
-
readonly createdAt: string;
|
|
45
|
-
readonly updatedAt: string;
|
|
46
|
-
readonly completedAt?: string;
|
|
47
|
-
}
|
|
48
|
-
export interface UpdateStepInput {
|
|
49
|
-
readonly stepId: string;
|
|
50
|
-
readonly status: "pending" | "running" | "completed" | "retryable" | "failed";
|
|
51
|
-
readonly attemptCount?: number;
|
|
52
|
-
readonly resultRef?: string | null;
|
|
53
|
-
readonly errorId?: string | null;
|
|
54
|
-
readonly updatedAt: string;
|
|
55
|
-
readonly completedAt?: string | null;
|
|
56
|
-
}
|
|
57
|
-
export interface InsertMessageInput {
|
|
58
|
-
readonly messageId: string;
|
|
59
|
-
readonly turnId?: string;
|
|
60
|
-
readonly role: "user" | "assistant" | "tool";
|
|
61
|
-
readonly content: string;
|
|
62
|
-
readonly createdAt: string;
|
|
63
|
-
readonly completedAt?: string;
|
|
64
|
-
}
|
|
65
|
-
export interface UpdateMessageInput {
|
|
66
|
-
readonly messageId: string;
|
|
67
|
-
readonly content?: string;
|
|
68
|
-
readonly completedAt?: string | null;
|
|
69
|
-
}
|
|
70
|
-
export interface RequestEffectInput {
|
|
71
|
-
readonly effectId: string;
|
|
72
|
-
readonly turnId: string;
|
|
73
|
-
readonly stepId: string;
|
|
74
|
-
readonly callId: string;
|
|
75
|
-
readonly toolName: string;
|
|
76
|
-
readonly idempotencyKey: string;
|
|
77
|
-
readonly input: EdenJsonValue;
|
|
78
|
-
readonly createdAt: string;
|
|
79
|
-
}
|
|
80
|
-
export interface CompleteEffectInput {
|
|
81
|
-
readonly effectId: string;
|
|
82
|
-
readonly output: EdenJsonValue;
|
|
83
|
-
readonly completedAt: string;
|
|
84
|
-
}
|
|
85
|
-
export interface StartEffectInput {
|
|
86
|
-
readonly effectId: string;
|
|
87
|
-
readonly updatedAt: string;
|
|
88
|
-
}
|
|
89
|
-
export interface FailEffectInput {
|
|
90
|
-
readonly effectId: string;
|
|
91
|
-
readonly error: EdenJsonValue;
|
|
92
|
-
readonly updatedAt: string;
|
|
93
|
-
}
|
|
94
|
-
export interface RecordErrorInput {
|
|
95
|
-
readonly errorId: string;
|
|
96
|
-
readonly turnId?: string;
|
|
97
|
-
readonly stepId?: string;
|
|
98
|
-
readonly code: string;
|
|
99
|
-
readonly message: string;
|
|
100
|
-
readonly retryable: boolean;
|
|
101
|
-
readonly createdAt: string;
|
|
102
|
-
}
|
|
103
|
-
export type EdenJobStatus = "pending" | "running" | "completed" | "retryable" | "dead";
|
|
104
|
-
export interface InsertJobInput {
|
|
105
|
-
readonly jobId: string;
|
|
106
|
-
readonly kind: string;
|
|
107
|
-
readonly status?: EdenJobStatus;
|
|
108
|
-
readonly dueAt: number;
|
|
109
|
-
readonly attempts?: number;
|
|
110
|
-
readonly maxAttempts?: number;
|
|
111
|
-
readonly lastError?: string;
|
|
112
|
-
readonly recoveryAction?: string;
|
|
113
|
-
readonly createdAt: string;
|
|
114
|
-
readonly updatedAt?: string;
|
|
115
|
-
readonly completedAt?: string;
|
|
116
|
-
}
|
|
117
|
-
export interface UpdateJobInput {
|
|
118
|
-
readonly jobId: string;
|
|
119
|
-
readonly status: EdenJobStatus;
|
|
120
|
-
readonly dueAt?: number;
|
|
121
|
-
readonly attempts?: number;
|
|
122
|
-
readonly lastError?: string | null;
|
|
123
|
-
readonly recoveryAction?: string | null;
|
|
124
|
-
readonly updatedAt: string;
|
|
125
|
-
readonly completedAt?: string | null;
|
|
126
|
-
}
|
|
127
|
-
export interface JournalTransaction {
|
|
128
|
-
appendEvent<TType extends EdenEventType>(input: AppendJournalEventInput<TType>): EdenEvent<TType>;
|
|
129
|
-
setSessionStatus(status: "new" | "running" | "waiting" | "failed" | "completed", updatedAt?: string): void;
|
|
130
|
-
insertTurn(input: InsertTurnInput): void;
|
|
131
|
-
updateTurn(input: UpdateTurnInput): void;
|
|
132
|
-
insertStep(input: InsertStepInput): void;
|
|
133
|
-
updateStep(input: UpdateStepInput): void;
|
|
134
|
-
insertMessage(input: InsertMessageInput): void;
|
|
135
|
-
updateMessage(input: UpdateMessageInput): void;
|
|
136
|
-
upsertProjection(projectionKey: string, value: EdenJsonValue, updatedAt?: string): void;
|
|
137
|
-
requestEffect(input: RequestEffectInput): void;
|
|
138
|
-
startEffect(input: StartEffectInput): void;
|
|
139
|
-
completeEffect(input: CompleteEffectInput): void;
|
|
140
|
-
failEffect(input: FailEffectInput): void;
|
|
141
|
-
recordError(input: RecordErrorInput): void;
|
|
142
|
-
insertJob(input: InsertJobInput): void;
|
|
143
|
-
updateJob(input: UpdateJobInput): void;
|
|
144
|
-
}
|
|
145
|
-
export declare function createOpaqueEventId(): string;
|
|
146
|
-
export declare function commitSessionTransaction<T>(storage: EdenSessionStorage, sessionId: string, transition: (journal: JournalTransaction) => T): T;
|
|
147
|
-
export interface JournalEventPageOptions {
|
|
148
|
-
readonly endIndex?: number;
|
|
149
|
-
readonly limit?: number;
|
|
150
|
-
}
|
|
151
|
-
export declare function readJournalEventsPage(sql: EdenSqlStorage, sessionId: string, startIndex?: number, options?: JournalEventPageOptions): readonly EdenEvent[];
|
|
152
|
-
export declare function readJournalEvents(sql: EdenSqlStorage, sessionId: string, startIndex?: number): readonly EdenEvent[];
|
|
153
|
-
export declare function readLatestJournalCursor(sql: EdenSqlStorage, sessionId: string): number;
|
|
154
|
-
//# sourceMappingURL=session-journal.d.ts.map
|