Skip to content

fix(agents): OpenCode keeps its sessions on the env's clock at any date - #870

Merged
earakely-scale merged 8 commits into
mainfrom
edgararakelyan/fd-6473-opencode-env-clock
Nov 23, 2026
Merged

earakely-scale merged 8 commits into
mainfrom
edgararakelyan/fd-6473-opencode-env-clock

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Why

On a run that opts OpenCode into the env's clock (sync_env_clock with agent_clock), OpenCode broke whenever the clock went behind the session's earlier messages. This is FD-6473.

  • Backward jump between turns: the history came back out of order (CHARLIE, ALPHA, BRAVO), and the next turn failed with "opencode exited with code 0 without completing".
  • Clock before 1970: every turn failed with "Session not found".

OpenCode 1.17.18's source shows two causes. It orders a session's messages by time_created (MessageV2.page), and its ids embed the time. Its schemas also take no time before 1970 (time.created: NonNegativeInt).

What

  • OpenCode's own process runs on real time. Where the image follows the clock (follows()), the bridge starts the CLI with the real-time marker (AGENTENV_REAL_TIME=1). libfaketime keeps the marked process on real time and removes the marker from its environment, so everything it starts, including the model's commands, git and LSP servers, still reads the env's time. A session's records now sort in the order they were written, whatever date the clock shows.
  • The model still sees the env's date. Every model call the CLI makes already goes through the turn's model relay. Everything that depends on where OpenCode writes its date lives in opencode_date.py, so a CLI upgrade touches one file. While this agent follows an armed clock, the relay sets two things to the env's date, read from date the same way the model's commands read it:
    • the <env> block's "Today's date" line in the system prompt;
    • the websearch tool's "current year".
  • If the relay can't read the date, the call is refused. That covers a date that fails, prints garbage, or hangs (killed after 5 s, with anything it left behind). The same goes for an <env> block with no date line the relay recognises, a call that isn't JSON, or any other error in the rewrite. The refusal is a 400 that the CLI doesn't retry, so the turn fails instead of showing the real date.
  • The relay re-encodes rewritten calls with ASCII escapes, so a lone UTF-16 surrogate from an MCP tool goes through as it came.
  • With the clock off, calls go upstream byte for byte as the CLI wrote them, and no date is read.
  • On an armed clock, the inline config enables only the litellm provider, so no provider added by a project's opencode.json can route a call around the relay. A subagent model named on another provider fails with "Model not found" while the clock is armed.
  • The shared model relay gains an optional rewrite hook and a Refusal, and logs each call it refuses. Letta Code, which also uses the relay, passes no rewrite.
  • Why not a plugin: an earlier version set the date with an OpenCode plugin. Chaos testing showed two problems. Configuring any plugin makes OpenCode install its plugin package from npm before the first model call, which hangs every turn where npm is unreachable and writes the package into a workspace's .opencode folder. And a plugin that fails to load silently shows the real date.

Known limits

  • OpenCode upgrades are guarded by a test. The relay depends on where OpenCode 1.17.18 writes its date. The integration test above runs against whatever version the lockfile pins, so an upgrade that breaks this fails CI. At runtime, an <env> block whose date line the relay doesn't recognise is refused, never passed through.

  • Older snapshots may not restore. A snapshot saved by an older OpenCode image while it followed the env's clock may fail to restore into this image. OpenCode's message ids embed a millisecond timestamp that wraps every 2^36 ms (795 days), so those messages can sort after new ones. This affects roughly half of env dates. Only sessions saved while OpenCode followed the clock before this release are affected.

  • The provider restriction comes one turn late if the clock is armed mid-turn. The CLI's config is set when its turn starts.

  • Code the model writes into .opencode/tool reads real time. It runs inside the CLI, which stays on real time. Commands the CLI starts read the env's time.

Tests

  • Unit:

    • The marker is set only where the image follows the clock.
    • Every turn's relay gets the rewrite.
    • On an armed clock, the rewrite gives 2027, 2200, 1950 and half a second before 1950, in system messages written as a string or as parts, and in the task's time zone.
    • It changes the websearch year, and leaves other tools and the user's messages alone.
    • Off the clock, or with nothing to rewrite, it forwards the call byte for byte and reads no date.
    • A date that fails, is garbled or hangs refuses the call, and so does an <env> block with no date line.
    • The relay forwards the rewritten body; a refused call gets a 400 with the reason, goes no further, and counts as refused.
  • Integration, against the pinned CLI (tst/integration/agent/opencode_env_date_test.py). The test installs the opencode-ai version the agent's package-lock.json pins and runs a real turn through the bridge's relay on an armed clock, against the fake OpenAI endpoint. It checks four things:

    • the model is sent the env's date on the <env> line and in websearch's year;
    • no copy of the machine's date is left in the system prompt or tool descriptions;
    • the CLI's bin is its native binary, which the real-time marker reaches;
    • the turn runs in that one process.

    Bumping OpenCode runs this test against the new version, so a version that moves or rewrites any of these fails CI. I checked the test by breaking the rewrite and the websearch pattern; each made it fail.

  • E2E and chaos on dedicated VMs, three rounds driven by workflows, each with an independent review and a completeness critic:

    • Round 1 tested the plugin version. It confirmed that running the CLI on real time keeps history and dates right, and it found the plugin's problems.
    • Round 2 tested this design and found the fixes above. Nothing failed because of the change.
    • Final round, on the final code: 36 of 36 cases passed, each repeated (mostly three times).
  • Every date line sent upstream matched the env's date. That is 1,554 of 1,554 stand-in requests and 249 of 249 under fuzz, plus every captured real-model request. History stayed in order in every run. This held:

    • at 1900, 1950, 2200, real time, 75 days ahead and behind real time;
    • in the Kiritimati and St John's time zones;
    • across jumps between turns, and across seven kinds of clock move during a tool call (set back, advance, 3600× and 0.001× rates, freeze, clear);
    • under seeded 12-turn fuzz and a burst of 20 clock moves in 2 s;
    • in subagents, the websearch year and snapshot restores.
  • Clock off: request bodies matched main byte for byte (27 pairs), and the median turn time was 3.38 s against main's 3.34 s. Offline (no npm route), a workspace .opencode folder and OPENCODE_PURE all matched main.

  • Refusals: a failing, garbled, hanging or missing date was refused before any upstream request, and the next turn recovered. With the clock off, the same breakage changed nothing.

  • Other checks:

    • real models: anthropic/claude-haiku-4-5 and openai/gpt-5.4-mini, with non-ASCII text passing through the armed relay;
    • the HTTPS MCP case on Modal, armed before the tunnel certificate's start date, and the agent itself on Modal at 1950 with a backward jump;
    • require_agent_clock, a late-deployed agent, and two agents in one run;
    • the container exiting when the A2A server dies;
    • Letta Code against main;
    • an install into a plain container.
  • Bugs main also has, found along the way and filed: FD-6834 (CLI output over 64 KiB is cut, breaking large snapshot exports and large replies), FD-6835 (changelog apply "Cannot rename"), FD-6836 (a rewound deploy loses the conversation), FD-6837 (a lone surrogate hangs a run).

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking findings remain.

Summary

OpenCode keeps its session records on real time while the relay gives the model the env's date.

  • OpenCode uses real time for sessions when its image follows the clock.
  • OpenCode sessions stay ordered while models see the environment date.

Reviews (8) · Last reviewed commit: "refactor(agents): name the limit on what..." · Reviewed by Greptile

earakely-scale and others added 5 commits November 21, 2026 16:21
OpenCode orders a session's messages by when they were written and takes no time before 1970, so on the env's clock a
set behind earlier messages scrambled the history and broke the next turn, and a clock in 1950 broke every turn. Its
bridge now starts the CLI on real time, so the commands it runs still read the env's time, and an OpenCode plugin puts
the env's date, read from a command, in the prompt's date line and the websearch tool's year.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l the env's date

An OpenCode plugin makes every CLI start wait on OpenCode installing its plugin package from npm, which hangs every
turn where npm can't be reached and writes the package into a workspace's .opencode folder, and a plugin that doesn't
load tells the model the real date without a word. The turn's model relay, which every model call the CLI makes goes
through, now puts the env's date in each call while the clock is armed, refusing a call whose date it can't read or
whose env block it doesn't know. The CLI starts on real time only where the image follows the clock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an hang or tell the real date

- A rewrite error other than a Refusal (a lone surrogate an MCP tool sent, which
  json.dumps(ensure_ascii=False) couldn't encode) became an aiohttp 500, which
  OpenCode retries without limit until the turn's timeout. The relay now answers
  any rewrite error with a 400 naming it, and the rewrite writes escaped JSON.
- Every <env> block's date line takes the env's date, not only the first: a
  config's agent prompt with a block of its own came first and left OpenCode's
  with the real date.
- An armed call that isn't JSON is refused rather than sent as written.
- The date read checks date's exit code before its output, names the timeout,
  and kills what date started with it (a new session and _kill_cli), so a child
  holding its pipes no longer stretches the 5 s bound to the child's lifetime.
- The CLI version test asks a bump to recheck where the CLI writes its date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… relay dates

A project's opencode.json can add a provider and name a model on it, as for the
general subagent; that call skipped the turn's relay and, with the CLI on real
time, told the model the real date (a second stand-in received "Sun Nov 22 2026"
while the env was at 2031; main's faked CLI sent 2031). While the clock is armed
at the turn's start the config sets enabled_providers to the endpoint alone, so
such a model isn't found (the task tool answers "Model not found") instead.
Unarmed, the config is as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…output

A date that exits but leaves a process holding its stdout outlived the 5 s bound's kill, which skips the process
group once its leader has exited, and the process and two pipes stayed open in the A2A server after each refused call.
The read now kills the command's session group either way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale marked this pull request as ready for review November 22, 2026 21:14
@earakely-scale
earakely-scale requested a review from a team as a code owner November 22, 2026 21:14
earakely-scale and others added 2 commits November 22, 2026 21:19
… relay rewrites it

An integration test installs the CLI the agent's package-lock.json pins and runs a turn through the bridge's relay on
an armed clock: the model is sent the env's date on the <env> line and in websearch's year, with no copy of the
machine's date in the system prompt or a tool; the CLI's bin is its native binary, which the real-time marker
reaches; and the turn runs in that one process. Bumping the CLI runs it against the new version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e relay logs what it refuses

Everything that depends on where the pinned OpenCode writes its date, the patterns, the date read and the rewrite,
moves from the bridge into opencode_date.py with its own tests, so a CLI upgrade touches one file. The date read keeps
its process-group kill to itself, leaving the bridge's _communicate as it was. The relay logs each call it refuses,
the bridge's config parameter no longer shares the clock_armed function's name, and the docstrings and README say it
plainly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale merged commit a137127 into main Nov 23, 2026
61 of 63 checks passed
@earakely-scale
earakely-scale deleted the edgararakelyan/fd-6473-opencode-env-clock branch November 23, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant