Repository navigation
fix(agents): OpenCode keeps its sessions on the env's clock at any date - #870
Merged
Merged
Conversation
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>
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
On a run that opts OpenCode into the env's clock (
sync_env_clockwithagent_clock), OpenCode broke whenever the clock went behind the session's earlier messages. This is FD-6473.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
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.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 fromdatethe same way the model's commands read it:<env>block's "Today's date" line in the system prompt;datethat 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.litellmprovider, so no provider added by a project'sopencode.jsoncan route a call around the relay. A subagent model named on another provider fails with "Model not found" while the clock is armed.rewritehook and aRefusal, and logs each call it refuses. Letta Code, which also uses the relay, passes no rewrite..opencodefolder. 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/toolreads real time. It runs inside the CLI, which stays on real time. Commands the CLI starts read the env's time.Tests
Unit:
<env>block with no date line.Integration, against the pinned CLI (
tst/integration/agent/opencode_env_date_test.py). The test installs theopencode-aiversion the agent'spackage-lock.jsonpins and runs a real turn through the bridge's relay on an armed clock, against the fake OpenAI endpoint. It checks four things:<env>line and in websearch's year;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:
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:
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
.opencodefolder andOPENCODE_PUREall matched main.Refusals: a failing, garbled, hanging or missing
datewas refused before any upstream request, and the next turn recovered. With the clock off, the same breakage changed nothing.Other checks:
anthropic/claude-haiku-4-5andopenai/gpt-5.4-mini, with non-ASCII text passing through the armed relay;require_agent_clock, a late-deployed agent, and two agents in one run;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


Confidence 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.
Reviews (8) · Last reviewed commit: "refactor(agents): name the limit on what..." · Reviewed by Greptile