@tidegap/core 0.20.0 → 0.22.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/dist/channels/digest.js +4 -4
- package/dist/channels/format.d.ts +30 -4
- package/dist/channels/format.js +125 -22
- package/dist/channels/payload.js +8 -6
- package/dist/channels/plan.js +3 -2
- package/dist/channels/types.d.ts +17 -1
- package/dist/energy.d.ts +7 -0
- package/dist/energy.js +7 -0
- package/dist/grid.d.ts +10 -2
- package/dist/grid.js +14 -5
- package/dist/handoff.d.ts +88 -6
- package/dist/handoff.js +177 -20
- package/dist/history.d.ts +19 -0
- package/dist/history.js +24 -1
- package/dist/index.d.ts +24 -18
- package/dist/index.js +16 -12
- package/dist/job-cadence.d.ts +10 -0
- package/dist/job-cadence.js +34 -0
- package/dist/job-wake.d.ts +55 -0
- package/dist/job-wake.js +91 -0
- package/dist/jobs.d.ts +74 -17
- package/dist/jobs.js +420 -75
- package/dist/notify.d.ts +9 -0
- package/dist/notify.js +17 -3
- package/dist/providers/anthropic.js +8 -6
- package/dist/providers/base.d.ts +47 -9
- package/dist/providers/base.js +29 -4
- package/dist/providers/gemini.d.ts +3 -0
- package/dist/providers/gemini.js +9 -6
- package/dist/providers/http.d.ts +40 -0
- package/dist/providers/http.js +82 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/ollama.d.ts +3 -0
- package/dist/providers/ollama.js +8 -5
- package/dist/providers/openai.js +7 -5
- package/dist/providers/timeout.d.ts +26 -8
- package/dist/providers/timeout.js +53 -15
- package/dist/readiness.d.ts +39 -3
- package/dist/readiness.js +73 -1
- package/dist/run-limits.d.ts +268 -0
- package/dist/run-limits.js +572 -0
- package/dist/schedule-job.d.ts +41 -2
- package/dist/schedule-job.js +70 -2
- package/dist/scheduler.d.ts +161 -17
- package/dist/scheduler.js +485 -128
- package/dist/storage/sqlite.d.ts +52 -5
- package/dist/storage/sqlite.js +200 -33
- package/dist/task-events.d.ts +19 -4
- package/dist/task-events.js +51 -5
- package/dist/tool-surface.js +62 -7
- package/dist/types.d.ts +72 -11
- package/dist/types.js +1 -3
- package/package.json +5 -2
|
@@ -1,33 +1,71 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
3
|
-
* reaches the adapter as `timeoutMs`. The call gets an
|
|
4
|
-
* dispatch rejects when the limit passes, even when
|
|
5
|
-
* signal. An SDK client also gets the limit as its
|
|
6
|
-
* its default (10 minutes) cannot end a longer call
|
|
2
|
+
* The LLM time limit of an API adapter: the limit the scheduler resolved
|
|
3
|
+
* when the run started reaches the adapter as `timeoutMs`. The call gets an
|
|
4
|
+
* abort signal, and the dispatch rejects when the limit passes, even when
|
|
5
|
+
* the client ignores the signal. An SDK client also gets the limit as its
|
|
6
|
+
* own request timeout, so its default (10 minutes) cannot end a longer call
|
|
7
|
+
* first, unlabelled; with `"none"` it gets the longest timer it takes and
|
|
8
|
+
* Tidegap sets none. The error names the cap in its details (`stopped_by:
|
|
7
9
|
* "llm_timeout"`), so no caller reads the message text for it, and it is
|
|
8
10
|
* not retried: the limit is spent.
|
|
11
|
+
*
|
|
12
|
+
* The caller's `signal` (a cancel of the task while it runs) aborts the call
|
|
13
|
+
* the same way, at once, with a `stopped: the task was cancelled` error.
|
|
14
|
+
*
|
|
15
|
+
* Under a limit, `none` included, the request also carries an undici
|
|
16
|
+
* dispatcher whose header and body timeouts follow the limit
|
|
17
|
+
* (`fetchOptions.dispatcher`, see `http.ts`), so Node's 300-second header
|
|
18
|
+
* timeout cannot end the call first: no hidden ceiling remains.
|
|
9
19
|
*/
|
|
10
20
|
import { NonRetryableError } from "./base.js";
|
|
21
|
+
import { limitDispatcher } from "./http.js";
|
|
11
22
|
/** The longest delay `setTimeout` honours; a larger one fires at once. Every timer that takes a user's limit clamps to it. */
|
|
12
23
|
export const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
/** The error of a call stopped because its task was cancelled. */
|
|
25
|
+
function cancelledError(provider) {
|
|
26
|
+
return new NonRetryableError(`${provider}: stopped: the task was cancelled`);
|
|
27
|
+
}
|
|
28
|
+
export async function withLlmTimeout(provider, timeoutMs, call, signal) {
|
|
29
|
+
const unlimited = timeoutMs === "none";
|
|
30
|
+
const limited = typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0;
|
|
31
|
+
if (!limited && !unlimited && !signal)
|
|
15
32
|
return call(undefined);
|
|
33
|
+
if (signal?.aborted)
|
|
34
|
+
throw cancelledError(provider);
|
|
16
35
|
const controller = new AbortController();
|
|
17
|
-
const limit = Math.min(timeoutMs, MAX_TIMER_MS);
|
|
36
|
+
const limit = limited ? Math.min(timeoutMs, MAX_TIMER_MS) : undefined;
|
|
37
|
+
const sdkTimeout = unlimited ? MAX_TIMER_MS : limit;
|
|
18
38
|
let timer;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
39
|
+
let onAbort;
|
|
40
|
+
const stopped = new Promise((_, reject) => {
|
|
41
|
+
// Each rejects before the abort, so a client that rejects on abort at once cannot win the race.
|
|
42
|
+
if (limit !== undefined) {
|
|
43
|
+
timer = setTimeout(() => {
|
|
44
|
+
reject(new NonRetryableError(`${provider}: timed out after ${timeoutMs / 1000}s`, { stopped_by: "llm_timeout" }));
|
|
45
|
+
controller.abort();
|
|
46
|
+
}, limit);
|
|
47
|
+
}
|
|
48
|
+
if (signal) {
|
|
49
|
+
onAbort = () => {
|
|
50
|
+
reject(cancelledError(provider));
|
|
51
|
+
controller.abort();
|
|
52
|
+
};
|
|
53
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
54
|
+
}
|
|
25
55
|
});
|
|
26
56
|
try {
|
|
27
|
-
|
|
57
|
+
const dispatcher = limitDispatcher(timeoutMs);
|
|
58
|
+
const request = {
|
|
59
|
+
signal: controller.signal,
|
|
60
|
+
...(sdkTimeout !== undefined ? { timeout: sdkTimeout } : {}),
|
|
61
|
+
...(dispatcher ? { fetchOptions: { dispatcher } } : {}),
|
|
62
|
+
};
|
|
63
|
+
return await Promise.race([call(request), stopped]);
|
|
28
64
|
}
|
|
29
65
|
finally {
|
|
30
66
|
clearTimeout(timer);
|
|
67
|
+
if (onAbort)
|
|
68
|
+
signal.removeEventListener("abort", onAbort);
|
|
31
69
|
}
|
|
32
70
|
}
|
|
33
71
|
//# sourceMappingURL=timeout.js.map
|
package/dist/readiness.d.ts
CHANGED
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
* A fact nobody can observe is "unknown" and raises no problem (R2).
|
|
6
6
|
*/
|
|
7
7
|
import type { MetaStore } from "./postpone.js";
|
|
8
|
-
import type
|
|
8
|
+
import { type DurationValue } from "./run-limits.js";
|
|
9
|
+
import type { TickDetails } from "./scheduler.js";
|
|
10
|
+
import type { TaskEvent } from "./task-events.js";
|
|
11
|
+
import type { TaskRecord, TaskStatus } from "./types.js";
|
|
9
12
|
/**
|
|
10
13
|
* The tick daemon as the host sees it. `installed` = the unit file exists
|
|
11
14
|
* and whether it is loaded was not probed; `unknown` = nothing on this
|
|
@@ -95,9 +98,22 @@ export interface ReadinessFacts {
|
|
|
95
98
|
runners: RunnerFact | null;
|
|
96
99
|
/** Pending provider-call tasks by provider. */
|
|
97
100
|
pendingByProvider: Record<string, number>;
|
|
101
|
+
/**
|
|
102
|
+
* The oldest `running` row a locked Node tick dispatched ({@link oldestTickRun}).
|
|
103
|
+
* The tick runs due rows one at a time, so this run holds every other due
|
|
104
|
+
* task while it lasts.
|
|
105
|
+
*/
|
|
106
|
+
oldestRun?: OldestRun | null;
|
|
98
107
|
now?: Date;
|
|
99
108
|
}
|
|
100
|
-
|
|
109
|
+
/** A run that holds the tick: its task, when its dispatch began, and its time limit when known. */
|
|
110
|
+
export interface OldestRun {
|
|
111
|
+
taskId: string;
|
|
112
|
+
/** ISO-8601 time of its `dispatched` event. */
|
|
113
|
+
startedAt: string;
|
|
114
|
+
timeLimit?: DurationValue;
|
|
115
|
+
}
|
|
116
|
+
export type ReadinessCode = "no_daemon" | "daemon_not_loaded" | "daemon_stale" | "long_run" | "no_runner";
|
|
101
117
|
/** Problems of the ticker, not of the runners: only a process outside the tick can see them (KTD11). */
|
|
102
118
|
export declare const TICKER_CODES: readonly ReadinessCode[];
|
|
103
119
|
export interface ReadinessProblem {
|
|
@@ -110,11 +126,13 @@ export interface ReadinessProblem {
|
|
|
110
126
|
provider?: string;
|
|
111
127
|
/** `no_runner` only: pending tasks of that provider. */
|
|
112
128
|
tasks?: number;
|
|
129
|
+
/** `long_run` only: the task that holds the tick. */
|
|
130
|
+
task_id?: string;
|
|
113
131
|
}
|
|
114
132
|
/** The readiness result every surface shows; JSON as-is in the API and MCP. */
|
|
115
133
|
export interface Readiness {
|
|
116
134
|
ready: boolean;
|
|
117
|
-
/** Ordered `no_daemon`, `daemon_not_loaded`, `daemon_stale`, `no_runner`. */
|
|
135
|
+
/** Ordered `no_daemon`, `daemon_not_loaded`, `daemon_stale`, `long_run`, `no_runner`. */
|
|
118
136
|
problems: ReadinessProblem[];
|
|
119
137
|
/** The first problem's sentence plus its fix; null when ready. */
|
|
120
138
|
line: string | null;
|
|
@@ -166,4 +184,22 @@ export declare function pendingProvidersFromStore(store: {
|
|
|
166
184
|
bodyJson?: string;
|
|
167
185
|
}>;
|
|
168
186
|
}): Record<string, number>;
|
|
187
|
+
/** A row's newest event, progress lines aside, when it is a dispatch by a locked tick: its tick and time. */
|
|
188
|
+
export declare function tickDispatch(events: readonly TaskEvent[]): {
|
|
189
|
+
tick: TickDetails;
|
|
190
|
+
at: string;
|
|
191
|
+
} | undefined;
|
|
192
|
+
/**
|
|
193
|
+
* The oldest `running` row a locked Node tick dispatched (the readiness
|
|
194
|
+
* `long_run` fact), or null. A row another writer runs (the Python tick, the
|
|
195
|
+
* MCP server) holds no Node tick and is left out. So is a Batch row: it is
|
|
196
|
+
* `running` only while a poll checks its batch, and its dispatch is the
|
|
197
|
+
* submit, hours before.
|
|
198
|
+
*/
|
|
199
|
+
export declare function oldestTickRun(store: {
|
|
200
|
+
list(filter: {
|
|
201
|
+
status: TaskStatus;
|
|
202
|
+
}): Array<Pick<TaskRecord<unknown>, "taskId" | "bodyJson" | "batchId">>;
|
|
203
|
+
listTaskEvents(taskId: string): TaskEvent[];
|
|
204
|
+
}): OldestRun | null;
|
|
169
205
|
//# sourceMappingURL=readiness.d.ts.map
|
package/dist/readiness.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* a runner. Pure over facts the host gathers; core spawns nothing.
|
|
5
5
|
* A fact nobody can observe is "unknown" and raises no problem (R2).
|
|
6
6
|
*/
|
|
7
|
+
import { formatDuration, runLimitsFromDetails, versionedCaps } from "./run-limits.js";
|
|
7
8
|
/** Ledger meta key of the {@link SleepReading} the last CLI tick took. */
|
|
8
9
|
export const TICK_SLEEP_META_KEY = "last_tick_sleep";
|
|
9
10
|
/**
|
|
@@ -69,7 +70,7 @@ const NO_DAEMON_INTERVAL_S = 60;
|
|
|
69
70
|
const ENV_FILE = "~/.config/tidegap/env";
|
|
70
71
|
const INSTALL = "tidegap install";
|
|
71
72
|
const TICK_ONCE = "tidegap tick --once";
|
|
72
|
-
const ORDER = ["no_daemon", "daemon_not_loaded", "daemon_stale", "no_runner"];
|
|
73
|
+
const ORDER = ["no_daemon", "daemon_not_loaded", "daemon_stale", "long_run", "no_runner"];
|
|
73
74
|
const PENDING = ["queued", "scheduled", "submitted"];
|
|
74
75
|
/** The fix for a provider with no runner. */
|
|
75
76
|
const RUNNER_FIX = {
|
|
@@ -158,6 +159,17 @@ export function assessReadiness(facts) {
|
|
|
158
159
|
problems.push({ code: "no_daemon", message: "Tasks will not run: no tick daemon.", fix: INSTALL });
|
|
159
160
|
}
|
|
160
161
|
}
|
|
162
|
+
const run = facts.oldestRun;
|
|
163
|
+
const runMs = run ? now.getTime() - Date.parse(run.startedAt) : NaN;
|
|
164
|
+
if (run && runMs > 2 * intervalS * 1000) {
|
|
165
|
+
const limit = run.timeLimit === undefined ? "" : run.timeLimit === "none" ? " (no time limit)" : ` (time limit ${formatDuration(run.timeLimit)})`;
|
|
166
|
+
problems.push({
|
|
167
|
+
code: "long_run",
|
|
168
|
+
message: `Task ${run.taskId} has run for ${Math.floor(runMs / 60_000)} minutes${limit}; due tasks wait until it ends.`,
|
|
169
|
+
fix: `tidegap queue cancel ${run.taskId}`,
|
|
170
|
+
task_id: run.taskId,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
161
173
|
const runners = facts.runners;
|
|
162
174
|
if (runners?.notes)
|
|
163
175
|
notes.push(...runners.notes);
|
|
@@ -251,4 +263,64 @@ export function pendingProvidersFromStore(store) {
|
|
|
251
263
|
}
|
|
252
264
|
return out;
|
|
253
265
|
}
|
|
266
|
+
/** A row's newest event, progress lines aside, when it is a dispatch by a locked tick: its tick and time. */
|
|
267
|
+
export function tickDispatch(events) {
|
|
268
|
+
let last;
|
|
269
|
+
for (let i = events.length - 1; i >= 0 && !last; i--)
|
|
270
|
+
if (events[i].kind !== "progress")
|
|
271
|
+
last = events[i];
|
|
272
|
+
if (last?.kind !== "dispatched")
|
|
273
|
+
return undefined;
|
|
274
|
+
const t = last.details.tick;
|
|
275
|
+
if (!t || typeof t.id !== "string" || !Number.isInteger(t.pid))
|
|
276
|
+
return undefined;
|
|
277
|
+
return { tick: { id: t.id, pid: t.pid, ...(typeof t.started_at === "string" ? { started_at: t.started_at } : {}) }, at: last.at };
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The time limit of a run (the command limit of a command row, else the LLM
|
|
281
|
+
* limit): the one its `dispatched` event recorded, else, for a run dispatched
|
|
282
|
+
* before limits were recorded, the one its row names itself (an old stored
|
|
283
|
+
* default counts as unset). Undefined when neither is known.
|
|
284
|
+
*/
|
|
285
|
+
function runTimeLimit(bodyJson, details) {
|
|
286
|
+
let spec;
|
|
287
|
+
try {
|
|
288
|
+
spec = JSON.parse(bodyJson ?? "");
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
spec = undefined;
|
|
292
|
+
}
|
|
293
|
+
const command = spec?.provider === "command";
|
|
294
|
+
const recorded = runLimitsFromDetails(details);
|
|
295
|
+
if (recorded)
|
|
296
|
+
return (command ? recorded.command_timeout : recorded.llm_timeout)?.value;
|
|
297
|
+
if (!spec?.caps || typeof spec.caps !== "object")
|
|
298
|
+
return undefined;
|
|
299
|
+
const caps = versionedCaps(spec.caps);
|
|
300
|
+
const value = command ? caps.commandTimeoutMs : caps.llmTimeoutMs;
|
|
301
|
+
return value === "none" || (typeof value === "number" && value > 0) ? value : undefined;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* The oldest `running` row a locked Node tick dispatched (the readiness
|
|
305
|
+
* `long_run` fact), or null. A row another writer runs (the Python tick, the
|
|
306
|
+
* MCP server) holds no Node tick and is left out. So is a Batch row: it is
|
|
307
|
+
* `running` only while a poll checks its batch, and its dispatch is the
|
|
308
|
+
* submit, hours before.
|
|
309
|
+
*/
|
|
310
|
+
export function oldestTickRun(store) {
|
|
311
|
+
let oldest = null;
|
|
312
|
+
for (const row of store.list({ status: "running" })) {
|
|
313
|
+
if (row.batchId)
|
|
314
|
+
continue;
|
|
315
|
+
const events = store.listTaskEvents(row.taskId);
|
|
316
|
+
const dispatch = tickDispatch(events);
|
|
317
|
+
if (!dispatch || (oldest && Date.parse(oldest.startedAt) <= Date.parse(dispatch.at)))
|
|
318
|
+
continue;
|
|
319
|
+
// tickDispatch found it: the newest event apart from progress lines.
|
|
320
|
+
const dispatched = [...events].reverse().find((e) => e.kind !== "progress");
|
|
321
|
+
const timeLimit = runTimeLimit(row.bodyJson, dispatched?.details);
|
|
322
|
+
oldest = { taskId: row.taskId, startedAt: dispatch.at, ...(timeLimit !== undefined ? { timeLimit } : {}) };
|
|
323
|
+
}
|
|
324
|
+
return oldest;
|
|
325
|
+
}
|
|
254
326
|
//# sourceMappingURL=readiness.js.map
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import type { ProviderName } from "./types.js";
|
|
2
|
+
/** A time limit in milliseconds, or `none` for no limit. */
|
|
3
|
+
export type DurationValue = number | "none";
|
|
4
|
+
export type RunLimitName = "command_timeout" | "llm_timeout" | "retries" | "retry_delay";
|
|
5
|
+
/** Where an effective limit came from. */
|
|
6
|
+
export type RunLimitSource = "task" | `setting:${ProviderName}` | "setting" | "default";
|
|
7
|
+
export interface RunLimitDefinition {
|
|
8
|
+
name: RunLimitName;
|
|
9
|
+
/**
|
|
10
|
+
* `time_limit`: a duration in ms or `none`; `delay`: a duration in ms,
|
|
11
|
+
* `none` refused; `count`: a whole number from 0 to 10.
|
|
12
|
+
*/
|
|
13
|
+
kind: "time_limit" | "delay" | "count";
|
|
14
|
+
/** The all-runners key in `~/.tidegap/config`. */
|
|
15
|
+
key: string;
|
|
16
|
+
/** The CLI flag and setting name. */
|
|
17
|
+
cliName: string;
|
|
18
|
+
/** The key of the row's or job's caps that holds a per-task value. */
|
|
19
|
+
capsKey: "commandTimeoutMs" | "llmTimeoutMs" | "retries" | "retryDelayMs";
|
|
20
|
+
/** Built-in default per runner, in ms (a count for `count`). The runners listed are the ones the setting applies to. */
|
|
21
|
+
defaults: Readonly<Partial<Record<ProviderName, number>>>;
|
|
22
|
+
/** Whether `<KEY>_<RUNNER>` keys are read. */
|
|
23
|
+
perRunner: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The default every writer stored before caps carried `capsVersion`: on
|
|
26
|
+
* caps without the marker, a stored value equal to it counts as unset.
|
|
27
|
+
* Only the time limits had one.
|
|
28
|
+
*/
|
|
29
|
+
legacyDefaultMs?: number;
|
|
30
|
+
}
|
|
31
|
+
/** The caps marker of writers that store only the values a caller set. */
|
|
32
|
+
export declare const CAPS_VERSION = 2;
|
|
33
|
+
/** The most automatic retries a setting or a task can ask for. */
|
|
34
|
+
export declare const MAX_RETRIES = 10;
|
|
35
|
+
export declare const RUN_LIMITS: readonly Readonly<RunLimitDefinition>[];
|
|
36
|
+
/** The registry entry of `name`. */
|
|
37
|
+
export declare function runLimitDefinition(name: RunLimitName): Readonly<RunLimitDefinition>;
|
|
38
|
+
/** A value the duration grammar refuses; the message is a fixed sentence. */
|
|
39
|
+
export declare class DurationError extends Error {
|
|
40
|
+
name: string;
|
|
41
|
+
}
|
|
42
|
+
export declare const DURATION_FORM_ERROR = "Use a duration from 1s to 7d, such as 90s, 30m, 1.5h or 2d, or none for no time limit.";
|
|
43
|
+
export declare const DURATION_ZERO_ERROR = "0 is not a time limit; use none for no time limit.";
|
|
44
|
+
export declare const DURATION_RANGE_ERROR = "A time limit must be from 1s to 7d; use none for no time limit.";
|
|
45
|
+
/**
|
|
46
|
+
* Parse a time limit: a number with an optional unit `s`, `m` (or `min`),
|
|
47
|
+
* `h` or `d`, case-insensitive, a bare number being seconds; from 1 second
|
|
48
|
+
* to 7 days; or `none`. Returns milliseconds or `"none"`; throws a
|
|
49
|
+
* {@link DurationError} with a fixed sentence that names `none`.
|
|
50
|
+
*/
|
|
51
|
+
export declare function parseDuration(input: string): DurationValue;
|
|
52
|
+
/** The canonical form of a time limit: the largest whole unit, else seconds; `none` as is. */
|
|
53
|
+
export declare function formatDuration(value: DurationValue): string;
|
|
54
|
+
export declare const RETRIES_ERROR = "Retries must be a whole number from 0 to 10; 0 turns automatic retries off.";
|
|
55
|
+
export declare const RETRY_DELAY_ERROR = "A retry delay must be a duration from 1s to 7d, such as 90s, 30m or 2h; 0 and none are refused.";
|
|
56
|
+
/** Parse a retry count: a whole number from 0 to 10. Throws a {@link DurationError} with {@link RETRIES_ERROR}. */
|
|
57
|
+
export declare function parseRetries(input: string): number;
|
|
58
|
+
/** Parse a retry delay: the duration grammar without `none`. Throws a {@link DurationError} with {@link RETRY_DELAY_ERROR}. */
|
|
59
|
+
export declare function parseRetryDelay(input: string): number;
|
|
60
|
+
/** Parse a value of setting `def` by its kind; throws a {@link DurationError} with the setting's sentence. */
|
|
61
|
+
export declare function parseRunLimitValue(def: Pick<RunLimitDefinition, "kind">, input: string): DurationValue;
|
|
62
|
+
/** A value of setting `def` as a person reads and writes it: `30m`, `none`, `2`. */
|
|
63
|
+
export declare function formatRunLimitValue(def: Pick<RunLimitDefinition, "kind">, value: DurationValue): string;
|
|
64
|
+
/** One `~/.tidegap/config` line the read skipped, and why. */
|
|
65
|
+
export interface IgnoredRunLimitLine {
|
|
66
|
+
key: string;
|
|
67
|
+
value: string;
|
|
68
|
+
reason: string;
|
|
69
|
+
}
|
|
70
|
+
export interface RunLimitSettings {
|
|
71
|
+
/** The valid values by config key, in ms or `"none"`. */
|
|
72
|
+
values: Readonly<Record<string, DurationValue>>;
|
|
73
|
+
/** Lines under a run-limit key that were skipped, in file order. */
|
|
74
|
+
ignored: readonly IgnoredRunLimitLine[];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The run-limit settings out of already-read KEY=VALUE pairs. Keys outside
|
|
78
|
+
* the registry are left alone; an empty value counts as unset; any other
|
|
79
|
+
* line under a run-limit key that cannot be used is listed in `ignored`.
|
|
80
|
+
*/
|
|
81
|
+
export declare function runLimitSettingsFromValues(pairs: Readonly<Record<string, string>>): RunLimitSettings;
|
|
82
|
+
export interface LoadRunLimitSettingsOptions {
|
|
83
|
+
/** Override the config file path (tests). Defaults to `~/.tidegap/config`. */
|
|
84
|
+
path?: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Read the run-limit settings from `~/.tidegap/config` (or `opts.path`). A
|
|
88
|
+
* missing or unreadable file gives no settings and never throws. Each
|
|
89
|
+
* ignored line is reported on stderr once per process.
|
|
90
|
+
*/
|
|
91
|
+
export declare function loadRunLimitSettings(opts?: LoadRunLimitSettingsOptions): RunLimitSettings;
|
|
92
|
+
export interface ResolvedRunLimit {
|
|
93
|
+
value: DurationValue;
|
|
94
|
+
source: RunLimitSource;
|
|
95
|
+
}
|
|
96
|
+
export type ResolvedRunLimits = Partial<Record<RunLimitName, ResolvedRunLimit>>;
|
|
97
|
+
/**
|
|
98
|
+
* Each limit that applies to `runner`, with its value and source. `caps` is
|
|
99
|
+
* the row's spec caps or the job's caps as stored; only the keys the caller
|
|
100
|
+
* set count, and on caps without `capsVersion: 2` a value equal to the old
|
|
101
|
+
* stored default counts as unset.
|
|
102
|
+
*/
|
|
103
|
+
export declare function resolveRunLimits(caps: Readonly<Record<string, unknown>> | undefined, settings: RunLimitSettings, runner: ProviderName): ResolvedRunLimits;
|
|
104
|
+
/**
|
|
105
|
+
* The limits of one run: a model call's for its runner; a command's for
|
|
106
|
+
* the `command` runner, plus, when it has a summarize step, that step's LLM
|
|
107
|
+
* limit for the summarize provider. `spec` is the row's body or a job
|
|
108
|
+
* occurrence's.
|
|
109
|
+
*/
|
|
110
|
+
export declare function resolveTaskRunLimits(spec: {
|
|
111
|
+
provider: ProviderName;
|
|
112
|
+
caps?: Readonly<Record<string, unknown>>;
|
|
113
|
+
summarize?: {
|
|
114
|
+
provider: ProviderName;
|
|
115
|
+
};
|
|
116
|
+
}, settings: RunLimitSettings): ResolvedRunLimits;
|
|
117
|
+
/** An effective time limit as a person reads it: `30m (default)`, `none (setting:claude-code)`. */
|
|
118
|
+
export declare function describeRunLimit(limit: ResolvedRunLimit): string;
|
|
119
|
+
/**
|
|
120
|
+
* The fixed sentence a writer refuses a stored time limit with, or
|
|
121
|
+
* undefined for a valid one: milliseconds from 1 second to 7 days, or
|
|
122
|
+
* `"none"`.
|
|
123
|
+
*/
|
|
124
|
+
export declare function timeLimitError(value: unknown): string | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* True for a caps key that holds a run-limit setting (`commandTimeoutMs`,
|
|
127
|
+
* `llmTimeoutMs`, `retries`, `retryDelayMs`): a writer drops it on `null`,
|
|
128
|
+
* so the setting or the default applies at each run.
|
|
129
|
+
*/
|
|
130
|
+
export declare function isRunLimitCap(key: string): boolean;
|
|
131
|
+
/**
|
|
132
|
+
* The fixed sentence a writer refuses a stored run-limit setting with
|
|
133
|
+
* (caps key `commandTimeoutMs`, `llmTimeoutMs`, `retries` or
|
|
134
|
+
* `retryDelayMs`), or undefined for a valid value or another key: a time
|
|
135
|
+
* limit per {@link timeLimitError}, retries a whole number from 0 to 10, a
|
|
136
|
+
* retry delay from 1 second to 7 days.
|
|
137
|
+
*/
|
|
138
|
+
export declare function runLimitValueError(key: string, value: unknown): string | undefined;
|
|
139
|
+
/** Why a writer refuses a cap value, or undefined: a run-limit setting per {@link runLimitValueError}, any other cap a positive number. */
|
|
140
|
+
export declare function capValueError(key: string, value: unknown): string | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* Caps as a writer stores them from now on: on caps without `capsVersion`,
|
|
143
|
+
* a time limit equal to the old stored default is dropped (it counted as
|
|
144
|
+
* unset), then `capsVersion: 2` is added, so each time limit left is one a
|
|
145
|
+
* caller set.
|
|
146
|
+
*/
|
|
147
|
+
export declare function versionedCaps<T extends object>(caps: T): T & {
|
|
148
|
+
capsVersion: number;
|
|
149
|
+
};
|
|
150
|
+
/** A time limit as event details and HTTP JSON carry it: seconds, or `"none"`. */
|
|
151
|
+
export declare function durationSeconds(value: DurationValue): number | "none";
|
|
152
|
+
/** One setting in event details and HTTP JSON: a duration in seconds (`value_s`, or `"none"`), retries as a count (`value`), and its source. */
|
|
153
|
+
export interface RunLimitDetail {
|
|
154
|
+
value_s?: number | "none";
|
|
155
|
+
value?: number;
|
|
156
|
+
source: RunLimitSource;
|
|
157
|
+
}
|
|
158
|
+
/** The `limits` details of a `dispatched` event: each effective setting and its source. */
|
|
159
|
+
export type RunLimitsDetails = Partial<Record<RunLimitName, RunLimitDetail>>;
|
|
160
|
+
export declare function runLimitsDetails(limits: ResolvedRunLimits): RunLimitsDetails;
|
|
161
|
+
/** The limits a `dispatched` event's details record, or undefined when it records none (a run from before they were recorded). */
|
|
162
|
+
export declare function runLimitsFromDetails(details: unknown): ResolvedRunLimits | undefined;
|
|
163
|
+
/** A task's limits as a view shows them, and whether its newest run recorded them or they apply if it ran now. */
|
|
164
|
+
export interface TaskRunLimitsView {
|
|
165
|
+
limits: RunLimitsDetails;
|
|
166
|
+
from: "dispatch" | "now";
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The limits of a task: the ones its newest `dispatched` event recorded,
|
|
170
|
+
* or, before a run recorded them, the ones a run that starts now gets.
|
|
171
|
+
* `events` are the task's events, oldest first.
|
|
172
|
+
*/
|
|
173
|
+
export declare function taskRunLimitsView(bodyJson: string | undefined, events: readonly {
|
|
174
|
+
kind: string;
|
|
175
|
+
details?: unknown;
|
|
176
|
+
}[], settings: RunLimitSettings): TaskRunLimitsView;
|
|
177
|
+
/** `command 30m (default), LLM 10m (setting)`: each time limit of a view, or undefined when it has none. */
|
|
178
|
+
export declare function runLimitsText(limits: RunLimitsDetails): string | undefined;
|
|
179
|
+
/** `1 after 30m (default)`: the retry policy of a view, or undefined when it has none. The delay's source is named when it differs. */
|
|
180
|
+
export declare function retryPolicyText(limits: RunLimitsDetails): string | undefined;
|
|
181
|
+
/**
|
|
182
|
+
* Why a run failed, as the retry policy reads it. `timeout`, `exit`,
|
|
183
|
+
* `transient` and `interrupted` may retry; `permanent` (a retry cannot fix
|
|
184
|
+
* it), `after_command` (a job's LLM step failed after its command ran) and
|
|
185
|
+
* `batch_submit_interrupted` (the provider may hold the batch) never do.
|
|
186
|
+
*/
|
|
187
|
+
export type FailureClass = "timeout" | "exit" | "transient" | "interrupted" | "permanent" | "after_command" | "batch_submit_interrupted";
|
|
188
|
+
export declare const FAILURE_CLASSES: readonly FailureClass[];
|
|
189
|
+
/** True for a class whose failure may retry. */
|
|
190
|
+
export declare function isRetryableFailure(cls: FailureClass): boolean;
|
|
191
|
+
/** A failure as both cores describe it to {@link classifyFailure}; every key is optional. */
|
|
192
|
+
export interface FailureFacts {
|
|
193
|
+
/** The HTTP status the provider answered with. */
|
|
194
|
+
status?: number;
|
|
195
|
+
/** Set by an adapter or a refusal: a retry cannot fix it (a refusal, missing setup, a rejected request). */
|
|
196
|
+
permanent?: boolean;
|
|
197
|
+
/** The cap that stopped the run: `command_timeout`, `llm_timeout`, `budget`. */
|
|
198
|
+
stopped_by?: string;
|
|
199
|
+
/** The steps of the run (`command`, `prompt`); a failed run with `prompt` failed after its command ran. */
|
|
200
|
+
steps?: readonly string[];
|
|
201
|
+
/** The run's process exited non-zero or was killed (a command or Claude Code run). */
|
|
202
|
+
exit?: boolean;
|
|
203
|
+
/** A tick stopped the run (`run`: a forced stop or a dead tick), or its Batch submit (`batch_submit`). */
|
|
204
|
+
interrupted?: "run" | "batch_submit";
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The facts of a thrown error: its HTTP status (`status` or `statusCode`),
|
|
208
|
+
* the adapter's `permanent`, `exit` and `interrupted` marks, and the `steps`
|
|
209
|
+
* and `stopped_by` of its run details.
|
|
210
|
+
*/
|
|
211
|
+
export declare function failureFactsOf(err: unknown): FailureFacts;
|
|
212
|
+
/**
|
|
213
|
+
* True for an HTTP status a retry of the task cannot fix: a 4xx other than
|
|
214
|
+
* 408 (the server stopped waiting), 425 (the server asks for the request
|
|
215
|
+
* again) and 429 (rate limit). Every other status may retry.
|
|
216
|
+
*/
|
|
217
|
+
export declare function isPermanentStatus(status: number): boolean;
|
|
218
|
+
/** The class of a failure; an unmarked unknown error is `transient`: one more attempt costs less than a lost task. */
|
|
219
|
+
export declare function classifyFailure(facts: FailureFacts): FailureClass;
|
|
220
|
+
/** The retry policy of resolved limits: retries and the first delay in ms (the registry gives every runner both). */
|
|
221
|
+
export declare function retryPolicyOf(limits: ResolvedRunLimits): {
|
|
222
|
+
retries: number;
|
|
223
|
+
delayMs: number;
|
|
224
|
+
};
|
|
225
|
+
/** Why a failure is final when its class allows a retry. */
|
|
226
|
+
export type FinalReason = "no_retries_left" | "no_time_left";
|
|
227
|
+
/** The sentence a final failure's error ends with, for a reason that has one. */
|
|
228
|
+
export declare const NO_TIME_LEFT_TEXT = "no time left before the deadline for a retry";
|
|
229
|
+
/** What {@link planRetry} decides: the next attempt's earliest start, or why there is none. */
|
|
230
|
+
export type RetryPlan = {
|
|
231
|
+
retryAt: number;
|
|
232
|
+
} | {
|
|
233
|
+
final: FinalReason;
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* The next attempt after attempt `attempt` (1-based) failed at `failedAt`:
|
|
237
|
+
* retry k waits `delayMs × 2^(k−1)`, a later `notBefore` wins, and the start
|
|
238
|
+
* must not pass `deadline`. All times in ms since the epoch; no deadline
|
|
239
|
+
* means no bound.
|
|
240
|
+
*/
|
|
241
|
+
export declare function planRetry(input: {
|
|
242
|
+
attempt: number;
|
|
243
|
+
retries: number;
|
|
244
|
+
delayMs: number;
|
|
245
|
+
failedAt: number;
|
|
246
|
+
deadline?: number;
|
|
247
|
+
notBefore?: number;
|
|
248
|
+
}): RetryPlan;
|
|
249
|
+
/**
|
|
250
|
+
* The number of the attempt a run of a task is: one plus the
|
|
251
|
+
* `attempt_failed` events since the newest `created` event or manual retry
|
|
252
|
+
* (a `rescheduled` event with reason `retry`). `events` are oldest first.
|
|
253
|
+
*/
|
|
254
|
+
export declare function attemptNumber(events: readonly {
|
|
255
|
+
kind: string;
|
|
256
|
+
details?: unknown;
|
|
257
|
+
}[]): number;
|
|
258
|
+
/** The error a final failure stores: the run's error, then the attempt count when more than one ran, then the no-time reason. */
|
|
259
|
+
export declare function finalFailureError(error: string, attempts: number, reason?: FinalReason): string;
|
|
260
|
+
/**
|
|
261
|
+
* The notice for a task's own LLM time limit that a Batch API run does not
|
|
262
|
+
* apply: the provider owns a Batch run's hour and its 24-hour window, so the
|
|
263
|
+
* limit is kept for a regular call and ignored for a Batch one.
|
|
264
|
+
*/
|
|
265
|
+
export declare const BATCH_LLM_TIMEOUT_NOTE = "not applied if the task runs through the provider Batch API (a deadline over 24 h away); a Batch run has no time limit";
|
|
266
|
+
/** True when a task of `provider` with this deadline may go to a provider Batch API, where no LLM time limit applies. */
|
|
267
|
+
export declare function mayRunAsBatch(provider: string, deadlineMs: number, nowMs: number): boolean;
|
|
268
|
+
//# sourceMappingURL=run-limits.d.ts.map
|