@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.
Files changed (54) hide show
  1. package/dist/channels/digest.js +4 -4
  2. package/dist/channels/format.d.ts +30 -4
  3. package/dist/channels/format.js +125 -22
  4. package/dist/channels/payload.js +8 -6
  5. package/dist/channels/plan.js +3 -2
  6. package/dist/channels/types.d.ts +17 -1
  7. package/dist/energy.d.ts +7 -0
  8. package/dist/energy.js +7 -0
  9. package/dist/grid.d.ts +10 -2
  10. package/dist/grid.js +14 -5
  11. package/dist/handoff.d.ts +88 -6
  12. package/dist/handoff.js +177 -20
  13. package/dist/history.d.ts +19 -0
  14. package/dist/history.js +24 -1
  15. package/dist/index.d.ts +24 -18
  16. package/dist/index.js +16 -12
  17. package/dist/job-cadence.d.ts +10 -0
  18. package/dist/job-cadence.js +34 -0
  19. package/dist/job-wake.d.ts +55 -0
  20. package/dist/job-wake.js +91 -0
  21. package/dist/jobs.d.ts +74 -17
  22. package/dist/jobs.js +420 -75
  23. package/dist/notify.d.ts +9 -0
  24. package/dist/notify.js +17 -3
  25. package/dist/providers/anthropic.js +8 -6
  26. package/dist/providers/base.d.ts +47 -9
  27. package/dist/providers/base.js +29 -4
  28. package/dist/providers/gemini.d.ts +3 -0
  29. package/dist/providers/gemini.js +9 -6
  30. package/dist/providers/http.d.ts +40 -0
  31. package/dist/providers/http.js +82 -0
  32. package/dist/providers/index.d.ts +1 -1
  33. package/dist/providers/index.js +1 -1
  34. package/dist/providers/ollama.d.ts +3 -0
  35. package/dist/providers/ollama.js +8 -5
  36. package/dist/providers/openai.js +7 -5
  37. package/dist/providers/timeout.d.ts +26 -8
  38. package/dist/providers/timeout.js +53 -15
  39. package/dist/readiness.d.ts +39 -3
  40. package/dist/readiness.js +73 -1
  41. package/dist/run-limits.d.ts +268 -0
  42. package/dist/run-limits.js +572 -0
  43. package/dist/schedule-job.d.ts +41 -2
  44. package/dist/schedule-job.js +70 -2
  45. package/dist/scheduler.d.ts +161 -17
  46. package/dist/scheduler.js +485 -128
  47. package/dist/storage/sqlite.d.ts +52 -5
  48. package/dist/storage/sqlite.js +200 -33
  49. package/dist/task-events.d.ts +19 -4
  50. package/dist/task-events.js +51 -5
  51. package/dist/tool-surface.js +62 -7
  52. package/dist/types.d.ts +72 -11
  53. package/dist/types.js +1 -3
  54. package/package.json +5 -2
@@ -37,8 +37,14 @@
37
37
  * modelled with `optionalPerHost` rather than dropped.
38
38
  */
39
39
  import { DEFAULT_MODEL_BY_PROVIDER } from "./providers/defaults.js";
40
+ import { formatDuration, RUN_LIMITS } from "./run-limits.js";
40
41
  /** Every host, in a stable order (used by the divergence-canary snapshot). */
41
42
  export const ALL_TOOL_HOSTS = ["mcp", "openclaw"];
43
+ /** The built-in LLM time limits per runner, as the tool text prints them (`30m`). */
44
+ const LLM_DEFAULTS = Object.fromEntries(Object.entries(RUN_LIMITS.find((d) => d.name === "llm_timeout").defaults).map(([runner, ms]) => [runner, formatDuration(ms)]));
45
+ /** The built-in retries and retry delay, the same for every runner, as the tool text prints them. */
46
+ const RETRIES_DEFAULT = String(RUN_LIMITS.find((d) => d.name === "retries").defaults.command);
47
+ const RETRY_DELAY_DEFAULT = formatDuration(RUN_LIMITS.find((d) => d.name === "retry_delay").defaults.command);
42
48
  // ── Reusable parameter descriptors ──────────────────────────────────────────
43
49
  // Defined once so a description or shape can never drift between the two
44
50
  // tools/hosts that share it.
@@ -317,7 +323,14 @@ export const TOOL_SURFACE = [
317
323
  kind: "boolean",
318
324
  optional: true,
319
325
  hosts: ["mcp"],
320
- description: "true wakes this Claude Code session when the task finishes, if it sits idle, within 12 hours after its last turn ended: the session starts a turn by itself with the notice. A wake spends a model turn while nobody may be watching, so set it only when the user wants the result acted on at once. Rejected outside a Claude Code session. Omit (default false) to get the notice with the next prompt.",
326
+ description: "true wakes this Claude Code session when the task finishes, if it sits idle, within 12 hours after its last turn ended: the session starts a turn by itself with the notice. A wake spends a model turn while nobody may be watching, so set it only when the user wants the result acted on at once. Rejected outside a Claude Code session, and when this server's ledger is not the default ~/.tidegap/queue.db (TIDEGAP_DB_PATH set elsewhere): the plugin's hooks read only that one. Omit (default false) to get the notice with the next prompt. With a job repeat, true binds the job to this session and wakes it on a run that failed for good (after its retries); only those runs and the job's end reach the session.",
327
+ },
328
+ {
329
+ name: "wake_match",
330
+ kind: "string",
331
+ optional: true,
332
+ hosts: ["mcp"],
333
+ description: "With a job repeat only: a JavaScript regular expression, e.g. '^ALERT' (flag m, so ^ and $ match at each line; at most 200 characters; one that matches an empty output is rejected). A run whose output matches wakes this session; a run that failed for good comes with the next prompt; other runs never reach the session. Tidegap tests the pattern after the run: the notice names the run id, its status and 'matched', never the output. Omit wake_session or set it true.",
321
334
  },
322
335
  {
323
336
  name: "dry_run",
@@ -415,6 +428,48 @@ export const TOOL_SURFACE = [
415
428
  optional: true,
416
429
  description: "IANA zone that window and days are read in, e.g. 'Europe/Berlin'; take it from the user's location. Defaults to the host's system zone. Only with a job repeat.",
417
430
  },
431
+ {
432
+ name: "until",
433
+ kind: "string",
434
+ optional: true,
435
+ hosts: ["mcp"],
436
+ description: "When a job ends: a day 'YYYY-MM-DD' (the last local day in time_zone a run may start, e.g. '2026-11-17' for \"for 5 days\") or an ISO instant with Z or an offset (a run must start before it). The job then ends by itself and sends one final notice. Only with a job repeat; omit for a job that runs until it is removed.",
437
+ },
438
+ {
439
+ name: "count",
440
+ kind: "number",
441
+ integer: true,
442
+ min: 1,
443
+ max: 1000,
444
+ optional: true,
445
+ hosts: ["mcp"],
446
+ description: "How many runs a job makes before it ends by itself, 1 to 1000, e.g. 5 for \"five times\". A retry is part of its run; a skipped run does not count. Only with a job repeat; with until too, the job ends at whichever comes first. Omit for a job that runs until it is removed.",
447
+ },
448
+ {
449
+ name: "llm_timeout",
450
+ kind: "string",
451
+ optional: true,
452
+ description: "Time limit of the run, or of each job run: a duration from 1s to 7d, e.g. '45m' or '2h', or 'none' for no limit; '0' is rejected. A run stopped at the limit fails. " +
453
+ `Omit to use the user's setting (\`tidegap limits\`), else ${LLM_DEFAULTS["claude-code"]} for claude-code and ${LLM_DEFAULTS.anthropic} for an API provider. ` +
454
+ "Raise it for a long task; 'none' holds other due tasks while it runs.",
455
+ },
456
+ {
457
+ name: "retries",
458
+ kind: "number",
459
+ integer: true,
460
+ min: 0,
461
+ max: 10,
462
+ optional: true,
463
+ description: "Automatic retries of a failed run, or of each job run: 0 to 10; 0 runs it once, e.g. for a task that must not run twice. " +
464
+ `Omit to use the user's setting (\`tidegap limits\`), else ${RETRIES_DEFAULT}. ` +
465
+ "A retry starts no later than the deadline; a permanent error (a rejected request, missing setup) never retries, and only the final outcome sends a notice.",
466
+ },
467
+ {
468
+ name: "retry_delay",
469
+ kind: "string",
470
+ optional: true,
471
+ description: `Wait before the first retry: a duration from 1s to 7d, e.g. '10m'; each later retry waits twice as long. '0' and 'none' are rejected. Omit to use the user's setting, else ${RETRY_DELAY_DEFAULT}.`,
472
+ },
418
473
  {
419
474
  name: "origin_session_id",
420
475
  kind: "string",
@@ -447,9 +502,9 @@ export const TOOL_SURFACE = [
447
502
  {
448
503
  name: "check_queue_status",
449
504
  hosts: ["mcp", "openclaw"],
450
- description: "Show the local task queue and jobs: every task in brief or one in full. Use when: the user asks what is queued or repeats, whether a task finished, or for its result. Do not use to change a task (use cancel_task, postpone_task, update_deadline or retry_task) or for limits (use tidegap_usage). Behavior: no network or login; marks a shown completed or failed task read; never changes a status. Returns: without task_id, status counts, last_tick_at, a line per task and job (guarantee, next run and id, last result); with task_id, its prompt, result, receipt and events. Rejects an unknown task_id.",
505
+ description: "Show the local queue and jobs: every task in brief or one in full. Use when: the user asks what is queued or repeats, whether a task finished, or its result. Do not use to change a task (use cancel_task, postpone_task, update_deadline or retry_task) or for usage limits (tidegap_usage). Behavior: no network or login; marks a shown completed or failed task read; changes no status. Returns: without task_id, status counts, last_tick_at, a line per task and job (guarantee, next run and id, last result); with task_id, its prompt, time limits, result, receipt and events. Rejects an unknown task_id.",
451
506
  descriptionPerHost: {
452
- openclaw: "Show the local task queue and jobs: every task in brief, or one task in full. Use when: the user asks what is queued or repeats, whether a task finished, or for its result. Do not use to change a task (use cancel_task, update_deadline or retry_task) or to set delivery (use set_delivery). Behavior: no network or login; marks a completed or failed task read when shown; never changes a status. Returns: without task_id, total, last_tick_at, tasks (task_id, title, status, scheduled_for, job_id) and jobs (guarantee, next_run, last_outcome); with task_id, its record. Throws on an unknown task_id.",
507
+ openclaw: "Show the local task queue and jobs: every task in brief or one in full. Use when: the user asks what is queued or repeats, whether a task finished, or its result. Do not use to change a task (use cancel_task, update_deadline or retry_task) or to set delivery (use set_delivery). Behavior: no network or login; marks a shown completed or failed task read; never changes a status. Returns: without task_id, total, last_tick_at, tasks (task_id, title, status, scheduled_for, job_id) and jobs (guarantee, next_run, last_outcome); with task_id, its record and time limits. Throws on an unknown task_id.",
453
508
  },
454
509
  annotations: {
455
510
  title: "Show task queue",
@@ -490,9 +545,9 @@ export const TOOL_SURFACE = [
490
545
  {
491
546
  name: "cancel_task",
492
547
  hosts: ["mcp", "openclaw"],
493
- description: "Cancel one queued or scheduled task by task_id; for all pending, use cancel_all. Use when: the user drops one task. Do not use to delay it (use postpone_task) or rerun a failed one (use retry_task). Behavior: marks it cancelled locally (no network) and redacts its prompt. Also cancels running and submitted tasks; a Batch call may still bill. A job or series skips this run; series=true ends it, even from a finished run. Else a finished task or repeat call changes nothing. Returns: \"Task <id> status: <status>\", terminated_at, and a job or series line when ended. Rejects an unknown task_id.",
548
+ description: "Cancel one task by task_id; for all pending, use cancel_all. Use when: the user drops one task. Do not use to delay it (use postpone_task) or rerun a failed one (use retry_task). Behavior: marks it cancelled (no network) and redacts its prompt; a running command, Claude Code run or API call then stops, and a submitted Batch call may still bill. A job or series skips this run; series=true ends it, even from a finished run. A finished task or repeat call is unchanged. Returns: \"Task <id> status: <status>\", terminated_at, and a job or series line when ended. Rejects an unknown task_id.",
494
549
  descriptionPerHost: {
495
- openclaw: "Cancel one queued or scheduled task by task_id; for all pending, use cancel_all. Use when: the user drops a task. Do not use to delay it (use update_deadline) or rerun a failed one (use retry_task). Behavior: marks it cancelled locally (no network) and redacts its prompt. Also cancels running and submitted tasks; a Batch call may still bill. A job or series skips this run; series=true ends it, even from a finished run. Else a finished task or repeat call changes nothing. Returns: task_id, status, completed_at, job_id and job_ended or series_id and series_ended. Throws on an unknown task_id.",
550
+ openclaw: "Cancel one task by task_id; for all pending, use cancel_all. Use when: the user drops a task. Do not use to delay it (use update_deadline) or rerun a failed one (use retry_task). Behavior: marks it cancelled (no network) and redacts its prompt; a running command, Claude Code run or API call then stops, and a submitted Batch call may still bill. A job or series skips this run; series=true ends it, even from a finished run. A finished task or repeat call is unchanged. Returns: task_id, status, completed_at, job_id and job_ended or series_id and series_ended. Throws on an unknown task_id.",
496
551
  },
497
552
  annotations: {
498
553
  title: "Cancel one task",
@@ -620,9 +675,9 @@ export const TOOL_SURFACE = [
620
675
  {
621
676
  name: "retry_task",
622
677
  hosts: ["mcp", "openclaw"],
623
- description: "Run a failed task again now, with the same prompt, provider and model. Use when: a task failed on a transient error (rate limit, 5xx). Do not use for a task that did not fail (use expedite_task to run it now, update_deadline to move it), or for a claude-code task (run `tidegap queue run-now <id>`). Behavior: one provider call now; needs the task provider's key in the server env (and ANTHROPIC_API_KEY or OPENAI_API_KEY); the new result and signed receipt replace the old ones. Returns: status (completed or failed) and error on failure. Rejects non-failed and unknown tasks.",
678
+ description: "Run a failed task again, a command or claude-code task included, as attempt 1 of a new count. Use when: a task failed and should run again. Do not use for a task that did not fail (use expedite_task or update_deadline). Behavior: with the provider's key in the server env, one call now; its result and signed receipt replace the old ones. A command or claude-code task, or one with no key here, is due now and the next tidegap tick runs it. The deadline does not block it. Returns: status (completed, failed, retrying or scheduled), error on failure. Rejects non-failed, unknown and job-run tasks.",
624
679
  descriptionPerHost: {
625
- openclaw: "Run a failed task again now, with the same prompt, provider and model. Use when: a task failed on a transient error (rate limit, 5xx) and the user wants it redone. Do not use for a task that did not fail (use expedite_task to run it now, update_deadline to move it). Behavior: one provider call now, through the OpenClaw runtime or a provider key in the plugin config; the new result and signed receipt replace the old ones, then the result is delivered. Returns: status, error on failure, result, receipt and delivery. Throws on tasks that are not failed and unknown task_ids.",
680
+ openclaw: "Run a failed task again, as attempt 1 of a new count. Use when: a task failed and the user wants it redone. Do not use for a task that did not fail (use expedite_task to run it now, update_deadline to move it). Behavior: one provider call now, through the OpenClaw runtime or a provider key in the plugin config; the new result and signed receipt replace the old ones, then the result is delivered. A command or claude-code task is due now and the next tidegap tick runs it. Returns: status, error on failure, result, receipt and delivery. Throws on tasks that are not failed and unknown task_ids.",
626
681
  },
627
682
  annotations: {
628
683
  title: "Retry a failed task",
package/dist/types.d.ts CHANGED
@@ -469,8 +469,19 @@ export interface ProviderCallSpec {
469
469
  provider: ProviderName;
470
470
  model?: string;
471
471
  };
472
- /** Per-run caps of a job occurrence. */
473
- caps?: JobCaps;
472
+ /**
473
+ * On a run of a job bound to a session: its wake rule and the session.
474
+ * Never `origin`, the adapter's fork input: a job run never forks a session.
475
+ */
476
+ jobWake?: {
477
+ on: "failures" | "match";
478
+ session: string;
479
+ };
480
+ /**
481
+ * Per-run caps: a job occurrence's, or the time limits a one-off task
482
+ * was queued with (only the keys the caller set, with `capsVersion`).
483
+ */
484
+ caps?: Partial<JobCaps>;
474
485
  }
475
486
  /** {@link ProviderCallSpec.origin}. */
476
487
  export interface ProviderCallOrigin {
@@ -493,10 +504,12 @@ export interface TickResultEntry {
493
504
  * "completed" or "failed". "completed"/"failed" are terminal.
494
505
  * "deferred": the provider asked to wait (a usage limit); the task is
495
506
  * `scheduled` again with `notBefore = retryAt`.
507
+ * "retrying": the run failed and the retry policy queued another attempt;
508
+ * the task is `scheduled` again with `notBefore = retryAt`. Not terminal.
496
509
  */
497
- status: "completed" | "failed" | "submitted" | "deferred";
510
+ status: "completed" | "failed" | "submitted" | "deferred" | "retrying";
498
511
  error?: string;
499
- /** ISO-8601 earliest retry of a `deferred` entry. */
512
+ /** ISO-8601 earliest start of the next attempt of a `deferred` or `retrying` entry. */
500
513
  retryAt?: string;
501
514
  }
502
515
  /** Aggregate result of one call to `Scheduler.tick`. */
@@ -516,6 +529,8 @@ export interface TickResult {
516
529
  batchPolled: number;
517
530
  /** Tasks a provider deferred this tick (back to `scheduled` with a `notBefore`). */
518
531
  deferred: number;
532
+ /** Tasks whose run failed and that the retry policy queued again this tick (back to `scheduled` with a `notBefore`). */
533
+ retrying: number;
519
534
  }
520
535
  /** Function the user hands to `defer`. */
521
536
  export type DeferrableTask<T> = () => Promise<T> | T;
@@ -670,19 +685,49 @@ export type JobTemplate = {
670
685
  model?: string;
671
686
  };
672
687
  };
673
- /** Per-run caps of a job. */
688
+ /**
689
+ * Per-run caps of a job. The two time limits, retries and the retry delay
690
+ * are stored only when the caller set them (ms, or `"none"` for no time
691
+ * limit; retries 0 to 10); unset, the run gets the setting or the built-in
692
+ * default when it starts (`run-limits.ts`).
693
+ */
674
694
  export interface JobCaps {
675
- commandTimeoutMs: number;
676
- llmTimeoutMs: number;
695
+ commandTimeoutMs?: number | "none";
696
+ llmTimeoutMs?: number | "none";
697
+ /** Automatic retries of a failed run, 0 to 10. */
698
+ retries?: number;
699
+ /** The wait before the first retry, in ms; each later retry waits twice the one before. */
700
+ retryDelayMs?: number;
677
701
  maxBudgetUsd: number;
678
702
  outputCapBytes: number;
679
703
  summarizeInputCapBytes: number;
704
+ /**
705
+ * `2` on caps that hold only the time limits a caller set. On caps without
706
+ * it, a stored time limit equal to the old default counts as unset.
707
+ */
708
+ capsVersion?: number;
680
709
  }
681
- /** The caps a job gets when it names none. */
710
+ /** The budget and output caps a job gets when it names none; the time limits come from the run-limits registry. */
682
711
  export declare const DEFAULT_JOB_CAPS: Readonly<JobCaps>;
683
712
  /** Which runs of a job notify: every run, failures only, or the digest only. */
684
713
  export type JobNotify = "every_run" | "failures" | "summary";
685
- export type JobStatus = "active" | "paused" | "removed";
714
+ /** `ended`: the job reached its end date or ran its runs; it keeps its history and only `remove` applies. */
715
+ export type JobStatus = "active" | "paused" | "removed" | "ended";
716
+ /** Why a job ended: its `until` bound passed, or its runs are done. */
717
+ export type JobEndReason = "until" | "count";
718
+ /**
719
+ * The wake rule of a job bound to a Claude Code session: wake it on a run
720
+ * whose slot failed for good (`failures`), or on a completed run whose
721
+ * result text matches `pattern` (`match`; a JavaScript regular expression
722
+ * with the `m` flag). `boundAt` is when the session bound the job.
723
+ */
724
+ export interface JobWake {
725
+ on: "failures" | "match";
726
+ pattern?: string;
727
+ boundAt: string;
728
+ }
729
+ /** Why a run of a bound job is a session notice. */
730
+ export type JobNoticeReason = "failed" | "matched" | "ended";
686
731
  /** Outcome of a job's last finished occurrence. */
687
732
  export type JobOutcome = "ok" | "failed" | "skipped";
688
733
  /** One row of the `jobs` table. */
@@ -714,12 +759,28 @@ export interface JobRecord {
714
759
  lastRunStartedAt?: string;
715
760
  lastOutcome?: JobOutcome;
716
761
  consecutiveFailures: number;
717
- /** True once the current slot used its one retry. */
718
- retryUsed: boolean;
762
+ /** The retries the current slot used, 0 to the job's retries (`retry_used`; a row from before held 0 or 1). */
763
+ retryUsed: number;
719
764
  /** `manual`, `auto: <first error line>` or `corrupt template`. */
720
765
  pauseReason?: string;
721
766
  pausedAt?: string;
722
767
  pauseNotifiedAt?: string;
768
+ /**
769
+ * The end date as given: `YYYY-MM-DD` (the last local day in the job's
770
+ * zone on which a slot may start) or an ISO instant (a slot must start
771
+ * before it).
772
+ */
773
+ until?: string;
774
+ /** Runs left before the job ends; a slot that ends for good takes one off. */
775
+ runsLeft?: number;
776
+ endedAt?: string;
777
+ endReason?: JobEndReason;
778
+ /** When the final notice of an ended job was claimed. */
779
+ endNotifiedAt?: string;
780
+ /** The wake rule; the job is bound only when this and {@link originSessionId} are both set. */
781
+ wake?: JobWake;
782
+ /** The Claude Code session the job wakes. */
783
+ originSessionId?: string;
723
784
  }
724
785
  /** Next occurrence of a job: run no earlier than `earliest`, finish by `deadline`. */
725
786
  export interface Occurrence {
package/dist/types.js CHANGED
@@ -1,10 +1,8 @@
1
1
  /**
2
2
  * Public + internal types for @tidegap/core.
3
3
  */
4
- /** The caps a job gets when it names none. */
4
+ /** The budget and output caps a job gets when it names none; the time limits come from the run-limits registry. */
5
5
  export const DEFAULT_JOB_CAPS = Object.freeze({
6
- commandTimeoutMs: 300_000,
7
- llmTimeoutMs: 600_000,
8
6
  maxBudgetUsd: 1,
9
7
  outputCapBytes: 10 * 1024 * 1024,
10
8
  summarizeInputCapBytes: 200 * 1024,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidegap/core",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "Carbon-aware scheduling for agentic AI workflows — core TypeScript library.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Vitalii Borovyk",
@@ -55,7 +55,7 @@
55
55
  "@anthropic-ai/sdk": "^0.137.0",
56
56
  "@types/better-sqlite3": "^7.6.13",
57
57
  "@types/node": "^22.20.5",
58
- "openai": "^7.43.0",
58
+ "openai": "^7.53.0",
59
59
  "typescript": "^5.6.3",
60
60
  "vitest": "^4.1.12"
61
61
  },
@@ -67,6 +67,9 @@
67
67
  "publishConfig": {
68
68
  "access": "public"
69
69
  },
70
+ "dependencies": {
71
+ "undici": "7.30.2"
72
+ },
70
73
  "scripts": {
71
74
  "build": "tsc -p tsconfig.json",
72
75
  "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json",