Skip to content

workflow next and the dispatcher disagree on when an interval trigger fires #2788

Closed

Description

@cristbc

Version

LifeOS 8.761.0 · attestation: not run

What is broken

For an interval trigger, workflow next and the dispatcher compute different fire times, so next can name a time at which the lane never runs.

  • workflow next prints the last run's start plus the period (nextFire, interval case).
  • The dispatcher finds due occurrences with occurrencesIn, which places interval occurrences at multiples of the period counted from the Unix epoch, and ignores when the last run happened.

The two agree only when the last run started exactly on that epoch grid. A run the dispatcher itself starts has a recorded start at or after its tick, so next is off by that latency even in the normal case (the second test below shows it with 4 seconds), and a run started by hand or after a pause is off by whatever phase it had.

Seen on 2026-11-12: workflow next reported an interval lane as next due at 23:45Z, and the dispatcher ran it at 00:00Z.

Expected: workflow next names the time the dispatcher will fire (the next epoch-grid multiple after now).
Actual: workflow next names last run plus period. In the test below: expected 2026-11-13T00:00:00.000Z, received 2026-11-12T23:45:00.000Z.

The grid is deliberate: the header of clock.ts says the Mac dispatcher and the cloud Worker run this same pure clock and compute the same occurrence keys so the ledger can dedupe. An anchor on the last run could not give two clocks the same keys. So nextFire is the side that is out of line.

Where (file:line)

LIFEOS/WORKFLOWS/engine/schedule.ts:244-249 (nextFire, interval case) vs LIFEOS/WORKFLOWS/engine/clock.ts:41-44 (occurrencesIn, interval case); caller LIFEOS/WORKFLOWS/workflow.ts:525-529

Repro on a clean tree

# From a LifeOS 8.761.0 checkout. Fixed times only; nothing is written except this test file.
cat > LIFEOS/WORKFLOWS/engine/next-vs-dispatcher.test.ts <<'EOF'
/**
 * An interval trigger has two clocks that disagree.
 *
 *   `workflow next` -> nextFire()          engine/schedule.ts:244  last run + period
 *   dispatcher fire -> occurrencesBetween() engine/schedule.ts:122 -> occurrencesIn()
 *                                           engine/clock.ts:43     multiples of the period counted from the epoch
 *
 * Fixed times only, no ledger, no state dir, nothing written. Imports the real engine read-only.
 * In a checkout, put this file in LIFEOS/WORKFLOWS/engine/ and run `bun test` on it.
 */
import { existsSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "bun:test";

const engineDir = existsSync(join(import.meta.dir, "schedule.ts"))
  ? import.meta.dir
  : join(process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude"), "LIFEOS", "WORKFLOWS", "engine");
const { nextFire, occurrencesBetween } = await import(join(engineDir, "schedule.ts"));

const interval = (minutes: number) => ({ type: "interval" as const, minutes, missed_run: "once" as const });
const at = (iso: string) => new Date(iso);

/** What `workflow next` prints: the code behind it, given the last run's start time. */
const printedNext = (minutes: number, lastRunIso: string, nowIso: string): string =>
  nextFire(interval(minutes), "any-lane", at(nowIso), Date.parse(lastRunIso), "/nonexistent")!.toISOString();

/** What the dispatcher fires: the first occurrence its clock finds after `now` (dispatcher.ts:570 calls this). */
const dispatcherNext = (minutes: number, nowIso: string): string => {
  const now = at(nowIso);
  const found = occurrencesBetween(interval(minutes), now, new Date(now.getTime() + minutes * 2 * 60_000));
  return found[0].at.toISOString();
};

describe("interval trigger: `workflow next` vs the dispatcher's clock", () => {
  test("a 60-minute lane whose last run was off the grid: next prints 23:45Z, the dispatcher fires 00:00Z", () => {
    const now = "2026-11-12T23:20:00.000Z";
    expect(printedNext(60, "2026-11-12T22:45:00.000Z", now)).toBe(dispatcherNext(60, now));
  });

  test("a run that starts a few seconds after its tick (normal latency) already makes next print the wrong second", () => {
    const now = "2026-11-12T10:01:00.000Z";
    expect(printedNext(15, "2026-11-12T10:00:04.000Z", now)).toBe(dispatcherNext(15, now));
  });

  test("control: they agree only when the last run started exactly on the grid", () => {
    const now = "2026-11-12T10:01:00.000Z";
    expect(printedNext(15, "2026-11-12T10:00:00.000Z", now)).toBe(dispatcherNext(15, now));
  });
});
EOF
bun test LIFEOS/WORKFLOWS/engine/next-vs-dispatcher.test.ts
# Two tests fail (the 60-minute grid-phase case and the few-seconds-late case); the on-grid control passes.

Negative control

Probe: mkdir -p /tmp/lifeos-repro && cd /tmp/lifeos-repro && cp "$TEST_FILE" ./ && bun test ./next-vs-dispatcher.test.ts · exit 1 · captured 2026-11-13T06:12:49.502Z

Red looks like: Received: "2026-11-12T23:45:00.000Z"

bun test v1.4.4 (38afee6c8)

--- stderr ---

next-vs-dispatcher.test.ts:
33 | };
34 | 
35 | describe("interval trigger: `workflow next` vs the dispatcher's clock", () => {
36 |   test("a 60-minute lane whose last run was off the grid: next prints 23:45Z, the dispatcher fires 00:00Z", () => {
37 |     const now = "2026-11-12T23:20:00.000Z";
38 |     expect(printedNext(60, "2026-11-12T22:45:00.000Z", now)).toBe(dispatcherNext(60, now));
                                                                  ^
error: expect(received).toBe(expected)

Expected: "2026-11-13T00:00:00.000Z"
Received: "2026-11-12T23:45:00.000Z"

      at <anonymous> (/private/tmp/lifeos-repro/next-vs-dispatcher.test.ts:38:62)
(fail) interval trigger: `workflow next` vs the dispatcher's clock > a 60-minute lane whose last run was off the grid: next prints 23:45Z, the dispatcher fires 00:00Z [3.77ms]
38 |     expect(printedNext(60, "2026-11-12T22:45:00.000Z", now)).toBe(dispatcherNext(60, now));
39 |   });
40 | 
41 |   test("a run that starts a few seconds after its tick (normal latency) already makes next print the wrong second", () => {
42 |     const now = "2026-11-12T10:01:00.000Z";
43 |     expect(printedNext(15, "2026-11-12T10:00:04.000Z", now)).toBe(dispatcherNext(15, now));
                                                                  ^
error: expect(received).toBe(expected)

Expected: "2026-11-12T10:15:00.000Z"
Received: "2026-11-12T10:15:04.000Z"

      at <anonymous> (/private/tmp/lifeos-repro/next-vs-dispatcher.test.ts:43:62)
(fail) interval trigger: `workflow next` vs the dispatcher's clock > a run that starts a few seconds after its tick (normal latency) already makes next print the wrong second [0.09ms]

 1 pass
 2 fail
 3 expect() calls
Ran 3 tests across 1 file. [44.00ms]

Suggested fix

Sketch only. I have not applied this to the engine or run the engine's own test suite against it. Make the interval case of nextFire ask the same clock the dispatcher asks, so the two cannot drift:

case "interval": {
  const period = trigger.minutes * 60_000;
  const found = occurrencesBetween(trigger, now, new Date(now.getTime() + period));
  return found[0]?.at ?? null;
}

lastStart is then unused for interval triggers. If a last-run anchor is wanted for display, next could show both the grid time and the last run.

Environment

  • OS: macOS 27.2.1 (arm64)
  • Harness: claude-code
  • bun: 1.4.4 · git: 2.54.0
  • Doctor broken: none

Activity

  1. danielmiessler commented on Nov 18, 2026

    @danielmiessler
    Owner

    Note

    🤖 Kai (AI assistant) responding on behalf of Daniel Miessler

    Thanks @cristbc, you found that workflow next computed interval triggers from the last start while the dispatcher fires on the epoch grid. next now asks the same clock the dispatcher uses, and your three cases are engine tests. Thanks also to @isihussain, whose PR #2799 we used as a reference.

    This fix lands in the next LifeOS release, so I'm closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions