@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
package/dist/job-wake.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wake rules of a job bound to a Claude Code session (KTD7, KTD9): the rule
|
|
3
|
+
* text the hosts take, the pattern checks on add and edit, and the bounded
|
|
4
|
+
* match the job sweep runs on a completed run's result text.
|
|
5
|
+
*
|
|
6
|
+
* The pattern is a JavaScript regular expression with the `m` flag (`^`
|
|
7
|
+
* and `$` match at each line). The match reads at most
|
|
8
|
+
* {@link JOB_WAKE_TEXT_MAX_CHARS} of the text and runs inside `vm` with a
|
|
9
|
+
* {@link JOB_WAKE_MATCH_TIMEOUT_MS} limit, so a pattern that backtracks
|
|
10
|
+
* without end cannot hold the tick. A match that times out counts as
|
|
11
|
+
* matched: a broken pattern wakes once rather than missing an alert.
|
|
12
|
+
*/
|
|
13
|
+
import { createContext, runInContext } from "node:vm";
|
|
14
|
+
/** The longest wake pattern. */
|
|
15
|
+
export const JOB_WAKE_PATTERN_MAX_CHARS = 200;
|
|
16
|
+
/** How much of a run's result text the pattern reads (64 KiB of characters). */
|
|
17
|
+
export const JOB_WAKE_TEXT_MAX_CHARS = 64 * 1024;
|
|
18
|
+
/** How long one match may run. */
|
|
19
|
+
export const JOB_WAKE_MATCH_TIMEOUT_MS = 50;
|
|
20
|
+
/** Why a wake rule or pattern was refused. */
|
|
21
|
+
export class JobWakeError extends Error {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "JobWakeError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** What `--wake` takes. */
|
|
28
|
+
export const JOB_WAKE_FORMS = "never, failures or match=<pattern>";
|
|
29
|
+
/**
|
|
30
|
+
* Why a wake pattern is refused, or undefined when it is fine: it must be
|
|
31
|
+
* a string of 1 to {@link JOB_WAKE_PATTERN_MAX_CHARS} characters that
|
|
32
|
+
* compiles with the `m` flag and does not match the empty string (it would
|
|
33
|
+
* wake on every run).
|
|
34
|
+
*/
|
|
35
|
+
export function wakePatternError(pattern) {
|
|
36
|
+
if (typeof pattern !== "string" || pattern === "")
|
|
37
|
+
return "the wake pattern must be a non-empty string";
|
|
38
|
+
if (pattern.length > JOB_WAKE_PATTERN_MAX_CHARS)
|
|
39
|
+
return `the wake pattern is longer than ${JOB_WAKE_PATTERN_MAX_CHARS} characters`;
|
|
40
|
+
// The pattern is the user's own text: it compiles and runs only inside the bounded vm match.
|
|
41
|
+
const empty = boundedTest(pattern, "", JOB_WAKE_MATCH_TIMEOUT_MS);
|
|
42
|
+
if (empty.error !== undefined)
|
|
43
|
+
return `the wake pattern is not a valid regular expression: ${empty.error}`;
|
|
44
|
+
if (empty.timedOut)
|
|
45
|
+
return `the wake pattern takes longer than ${JOB_WAKE_MATCH_TIMEOUT_MS} ms on an empty output; make it simpler`;
|
|
46
|
+
if (empty.matched)
|
|
47
|
+
return "the wake pattern matches an empty output, so it would wake on every run; make it name what to look for";
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
/** Compile `pattern` (flag `m`) and test it on `text` in a fresh `vm` context with a time limit. Never throws. */
|
|
51
|
+
function boundedTest(pattern, text, timeoutMs) {
|
|
52
|
+
const context = createContext({ pattern, text });
|
|
53
|
+
try {
|
|
54
|
+
return { matched: runInContext(`new RegExp(pattern, "m").test(text)`, context, { timeout: timeoutMs }) === true };
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
if (err?.code === "ERR_SCRIPT_EXECUTION_TIMEOUT")
|
|
58
|
+
return { matched: false, timedOut: true };
|
|
59
|
+
// An error from inside the context is not an instance of this realm's Error.
|
|
60
|
+
const message = err?.message;
|
|
61
|
+
return { matched: false, error: typeof message === "string" ? message : String(err) };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** The rule `--wake never|failures|match=<pattern>` names. Throws {@link JobWakeError}. */
|
|
65
|
+
export function parseWakeRule(text) {
|
|
66
|
+
const value = text.trim();
|
|
67
|
+
if (value === "never" || value === "failures")
|
|
68
|
+
return { on: value };
|
|
69
|
+
if (value.startsWith("match=")) {
|
|
70
|
+
const pattern = value.slice("match=".length);
|
|
71
|
+
const error = wakePatternError(pattern);
|
|
72
|
+
if (error)
|
|
73
|
+
throw new JobWakeError(error);
|
|
74
|
+
return { on: "match", pattern };
|
|
75
|
+
}
|
|
76
|
+
throw new JobWakeError(`--wake must be ${JOB_WAKE_FORMS}`);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Test `pattern` (flag `m`) against the first {@link JOB_WAKE_TEXT_MAX_CHARS}
|
|
80
|
+
* of `text`, in a fresh `vm` context with a {@link JOB_WAKE_MATCH_TIMEOUT_MS}
|
|
81
|
+
* limit. Never throws.
|
|
82
|
+
*/
|
|
83
|
+
export function matchWake(pattern, text, timeoutMs = JOB_WAKE_MATCH_TIMEOUT_MS) {
|
|
84
|
+
const r = boundedTest(pattern, text.slice(0, JOB_WAKE_TEXT_MAX_CHARS), timeoutMs);
|
|
85
|
+
if (r.timedOut)
|
|
86
|
+
return { matched: true, timedOut: true };
|
|
87
|
+
if (r.error !== undefined)
|
|
88
|
+
return { matched: true, broken: true };
|
|
89
|
+
return { matched: r.matched };
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=job-wake.js.map
|
package/dist/jobs.d.ts
CHANGED
|
@@ -10,12 +10,14 @@
|
|
|
10
10
|
* inside `prepareTick`, so every ticker gets it. For each job it reads the
|
|
11
11
|
* tracked occurrence and:
|
|
12
12
|
*
|
|
13
|
-
* - live occurrence: nothing (R17), except a `running` one whose
|
|
14
|
-
* is older than its
|
|
15
|
-
* with a batch id goes back to
|
|
13
|
+
* - live occurrence: nothing (R17), except a `running` one whose tick is
|
|
14
|
+
* gone or whose dispatch is older than its time limits allow, which
|
|
15
|
+
* fails as interrupted (a Batch row with a batch id goes back to
|
|
16
|
+
* `submitted` instead);
|
|
16
17
|
* - finished occurrence: records the outcome (KTD9: ok, a failure with
|
|
17
|
-
*
|
|
18
|
-
* skip), then materializes the next
|
|
18
|
+
* the retries of the job's retry policy per slot and auto-pause after
|
|
19
|
+
* three failed slots, or a skip), then materializes the next
|
|
20
|
+
* occurrence;
|
|
19
21
|
* - no occurrence on an active job: materializes one.
|
|
20
22
|
*
|
|
21
23
|
* Every job write is a compare-and-set on `revision`. Pause, edit and
|
|
@@ -24,9 +26,12 @@
|
|
|
24
26
|
* adopts it on an active job and cancels it otherwise (the stop-flag-first
|
|
25
27
|
* rule). The sweep never throws.
|
|
26
28
|
*/
|
|
29
|
+
import { BATCH_SUBMIT_INTERRUPTED_ERROR } from "./history.js";
|
|
30
|
+
import { type JobWakeRule } from "./job-wake.js";
|
|
27
31
|
import { type SleepReading } from "./readiness.js";
|
|
28
32
|
import type { Scheduler } from "./scheduler.js";
|
|
29
|
-
import { type
|
|
33
|
+
import { type ResolvedRunLimits } from "./run-limits.js";
|
|
34
|
+
import { type JobCadence, type JobCaps, type JobEndReason, type JobNotify, type JobRecord, type JobTemplate, type JobWindow, type TaskRecord, type TaskStatus } from "./types.js";
|
|
30
35
|
/** Why a job action was refused; `code` maps to an HTTP status in the hosts. */
|
|
31
36
|
export declare class JobActionError extends Error {
|
|
32
37
|
/** `invalid` = bad input (400), `not_found` (404), `conflict` = the state forbids it (409). */
|
|
@@ -53,7 +58,7 @@ export interface JobInput {
|
|
|
53
58
|
/** IANA zone; default the system zone. */
|
|
54
59
|
timeZone?: string;
|
|
55
60
|
template: JobTemplate;
|
|
56
|
-
caps?:
|
|
61
|
+
caps?: JobCapsPatch;
|
|
57
62
|
/** Default `every_run`. */
|
|
58
63
|
notify?: JobNotify;
|
|
59
64
|
/** `page`, `cli`, `mcp` or `openclaw`. */
|
|
@@ -64,7 +69,26 @@ export interface JobInput {
|
|
|
64
69
|
* AS9): it may run from now until then. Later ones follow the cadence.
|
|
65
70
|
*/
|
|
66
71
|
deadline?: string | Date;
|
|
72
|
+
/**
|
|
73
|
+
* The end date: `YYYY-MM-DD` (the last local day a slot may start,
|
|
74
|
+
* inclusive) or an ISO instant (a slot must start before it).
|
|
75
|
+
*/
|
|
76
|
+
until?: string | null;
|
|
77
|
+
/** Runs from now before the job ends, 1 to {@link JOB_MAX_COUNT}. */
|
|
78
|
+
count?: number | null;
|
|
79
|
+
/**
|
|
80
|
+
* Bind the job to the Claude Code session that sets it (KTD8). Never read
|
|
81
|
+
* from the job JSON: only a host that runs in the session sets it.
|
|
82
|
+
*/
|
|
83
|
+
wake?: JobWakeInput;
|
|
67
84
|
}
|
|
85
|
+
/** A wake rule with the session that sets it; `never` unbinds the job and needs no session. */
|
|
86
|
+
export type JobWakeInput = JobWakeRule & {
|
|
87
|
+
/** The session to wake. */
|
|
88
|
+
sessionId?: string;
|
|
89
|
+
/** The origin's agent: a wake needs `claude-code`. */
|
|
90
|
+
agent?: string;
|
|
91
|
+
};
|
|
68
92
|
/** The fields {@link updateJob} can change; `window: null` removes the window. */
|
|
69
93
|
export interface JobPatch {
|
|
70
94
|
title?: string;
|
|
@@ -72,9 +96,25 @@ export interface JobPatch {
|
|
|
72
96
|
window?: JobWindow | null;
|
|
73
97
|
timeZone?: string;
|
|
74
98
|
template?: JobTemplate;
|
|
75
|
-
caps?:
|
|
99
|
+
caps?: JobCapsPatch;
|
|
76
100
|
notify?: JobNotify;
|
|
101
|
+
/** A new end date; `null` clears it. */
|
|
102
|
+
until?: string | null;
|
|
103
|
+
/** Runs left from now; `null` clears the bound. */
|
|
104
|
+
count?: number | null;
|
|
105
|
+
/** A new wake binding (to the session that sets it), or `never` to unbind. */
|
|
106
|
+
wake?: JobWakeInput;
|
|
77
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Caps a caller sets: a value sets the key. `null` removes a time limit, so
|
|
110
|
+
* the setting or the default applies at each run, and puts any other cap
|
|
111
|
+
* back to its default.
|
|
112
|
+
*/
|
|
113
|
+
export type JobCapsPatch = {
|
|
114
|
+
[K in Exclude<keyof JobCaps, "capsVersion">]?: JobCaps[K] | null;
|
|
115
|
+
};
|
|
116
|
+
/** Every caps key a caller may set. */
|
|
117
|
+
export declare const JOB_CAPS_KEYS: ReadonlySet<string>;
|
|
78
118
|
/** Per-call options of the job actions. */
|
|
79
119
|
export interface JobActionOptions {
|
|
80
120
|
/** Who the change is recorded as on occurrence events; default `system`. */
|
|
@@ -115,11 +155,21 @@ export interface JobSweepResult {
|
|
|
115
155
|
autoPaused: string[];
|
|
116
156
|
/** Running occurrences failed as interrupted this pass. */
|
|
117
157
|
interrupted: string[];
|
|
158
|
+
/** Jobs that reached their end date or ran their runs this pass. */
|
|
159
|
+
ended: string[];
|
|
160
|
+
/** Runs of bound jobs that became session notices this pass. */
|
|
161
|
+
sessionNotices: string[];
|
|
118
162
|
}
|
|
119
163
|
/** A job secret name: what the command sees as an environment variable. */
|
|
120
164
|
export declare const JOB_SECRET_NAME: RegExp;
|
|
121
|
-
/**
|
|
122
|
-
|
|
165
|
+
/**
|
|
166
|
+
* The prompt `tidegap jobs add --summarize` stores when the user gives none.
|
|
167
|
+
* It is stored as text on the job, so a later change here leaves existing
|
|
168
|
+
* jobs as they are.
|
|
169
|
+
*/
|
|
170
|
+
export declare const DEFAULT_SUMMARIZE_PROMPT: string;
|
|
171
|
+
/** The most runs a job's `count` may ask for. */
|
|
172
|
+
export declare const JOB_MAX_COUNT = 1000;
|
|
123
173
|
/** Failed slots in a row that pause a job. */
|
|
124
174
|
export declare const JOB_AUTO_PAUSE_FAILURES = 3;
|
|
125
175
|
/** How `pauseReason` starts when the third failed slot paused the job: `auto: <first error line>`. */
|
|
@@ -127,14 +177,21 @@ export declare const JOB_AUTO_PAUSE_PREFIX = "auto: ";
|
|
|
127
177
|
/** The error line of an auto-pause reason, or undefined when the job was not paused by its failures. */
|
|
128
178
|
export declare function jobAutoPauseReason(pauseReason: string | undefined): string | undefined;
|
|
129
179
|
export declare const JOB_INTERRUPTED_ERROR = "interrupted: the tick stopped during the run";
|
|
180
|
+
export { BATCH_SUBMIT_INTERRUPTED_ERROR };
|
|
181
|
+
/** What an end reason says: `its end date (<until>) passed` or `its runs are done`. */
|
|
182
|
+
export declare function jobEndText(reason: JobEndReason | undefined, until?: string): string;
|
|
183
|
+
/**
|
|
184
|
+
* A job's end condition in words, or undefined for a job with none:
|
|
185
|
+
* `ends after 2026-11-17 (Europe/Berlin)`, `3 runs left`, or both joined by `; `.
|
|
186
|
+
*/
|
|
187
|
+
export declare function jobEndsText(job: Pick<JobRecord, "until" | "runsLeft" | "timeZone">): string | undefined;
|
|
188
|
+
/** The session a job wakes, or undefined when it is not bound. */
|
|
189
|
+
export declare function jobWakeSession(job: Pick<JobRecord, "wake" | "originSessionId">): string | undefined;
|
|
130
190
|
/**
|
|
131
|
-
* The
|
|
132
|
-
*
|
|
133
|
-
* `retryTask` and the Python `retry_task` refuse it, and the sweep does not
|
|
134
|
-
* retry it. The text is the marker; `test/fixtures/interrupted-batch-submit.json`
|
|
135
|
-
* shares it with the Python core.
|
|
191
|
+
* The time limits a run of `job` gets if it starts now, with their sources:
|
|
192
|
+
* the scheduler's settings reader, or without one the settings file.
|
|
136
193
|
*/
|
|
137
|
-
export declare
|
|
194
|
+
export declare function jobRunLimits(job: JobRecord, scheduler?: Scheduler): ResolvedRunLimits;
|
|
138
195
|
/**
|
|
139
196
|
* The job sweep (KTD2): for every job, record the outcome of its finished
|
|
140
197
|
* occurrence and keep one live occurrence per active job. Never throws.
|
|
@@ -164,7 +221,7 @@ export declare function runJobNow(scheduler: Scheduler, jobId: string, opts?: Jo
|
|
|
164
221
|
export declare function removeJob(scheduler: Scheduler, jobId: string, opts?: JobActionOptions): Promise<JobRecord>;
|
|
165
222
|
/** One job, removed ones included; undefined when the id is unknown. */
|
|
166
223
|
export declare function getJob(scheduler: Scheduler, jobId: string): JobRecord | undefined;
|
|
167
|
-
/** Jobs, oldest first; removed jobs are hidden unless asked for. */
|
|
224
|
+
/** Jobs, oldest first; removed and ended jobs are hidden unless asked for. */
|
|
168
225
|
export declare function listJobs(scheduler: Scheduler, opts?: {
|
|
169
226
|
includeRemoved?: boolean;
|
|
170
227
|
}): JobRecord[];
|