Repository navigation
Description
Description
Instruction files are not read per session. Their contents live in a per-location, in-memory cache (InstructionDiscovery), and a brand-new session only snapshots that cache into its durable instruction state. The cache is invalidated exclusively by a filesystem watcher: there is no read-on-session-start and no mtime/hash check when a session is initialized. So whenever the watcher misses an edit, every new session in that location keeps the previous contents of AGENTS.md until the location is rebuilt (explicit config reload, restart, or location TTL expiry).
This is not limited to symlinked files; the symlink case is simply a deterministic reproduction, because the watcher subscribes to the unresolved path while the reader resolves it with fs.resolve (realpath).
Where in the code
-
packages/core/src/config/plugin/instruction.tsThe only invalidation path is the watcher, subscribed once when the location activates, with a debounce before the rescan:
const updates = yield* PubSub.subscribe(changes) yield* Stream.fromSubscription(updates).pipe( Stream.debounce("100 millis"), Stream.runForEach((file) => refresh(file).pipe(Effect.andThen(discovery.reload()))), Effect.forkScoped({ startImmediately: true }), ) yield* refresh()
Nothing else re-reads the files afterwards. The watcher candidates for project files are not realpath-resolved:
...(project ? ancestorDirectories(start, stop) .map((directory) => join(directory, "AGENTS.md")) .filter((file) => discovery.global || file !== globalFile) : []),
while
projectSource()/read()read throughfs.resolve/fs.readFileStringSafe. With a symlinkedAGENTS.md,Watcher.subscribe({ path, type: "file" })watches the symlink's directory instead of the target's directory, so edits to the real file produce no event andrefresh()never runs. (The global candidate is resolved viafs.resolveand is watched correctly; only project candidates are affected.) -
packages/core/src/instruction-discovery.tsload()returns a snapshot of the in-memory state:load: Effect.fn("InstructionDiscovery.load")(function* () { const files = yield* list() if (!Array.isArray(files)) return source(files) return source(files.length === 0 ? Instructions.removed : files) }),
with
source(...)usingread: Effect.succeed(value). The cached value only changes throughtransform/reload, i.e. only when the watcher fires. -
packages/core/src/session/instruction-state.tsA new session's baseline is taken from that snapshot:
observe()→Instructions.read(instructions)→commit()persistsinitial_values, andinitial()renders the per-request epoch baseline frominitial_values. There is no re-read of the files at session creation, so the session inherits whatever the location cached — including a value that is no longer on disk.
Steps to reproduce (deterministic)
- At the project root, make the instruction file a symlink:
ln -s real/AGENTS.md AGENTS.md. - Start OpenCode in that project (the location activates and discovers
AGENTS.md). - Edit the real file
real/AGENTS.md, not the symlink. - Create a brand-new session in the same project.
- Ask the model to repeat the instructions it was given from that file.
General case (no symlink): any edit the watcher does not deliver — made before the subscription is ready, or on a filesystem where events are unreliable — leaves new sessions with the old contents. There is no fallback re-read that corrects it.
Expected Behavior
A new session uses the current contents of AGENTS.md.
Actual Behavior
The new session keeps the previous contents. The change only appears after the location is rebuilt (config reload or restart), because the per-location cache is stale and nothing re-reads the file when a new session starts.
Workaround
Force the watcher to deliver an event for the instruction file by bumping the metadata of the watched (unresolved) path itself:
touch -h AGENTS.mdWhy this works:
touch -histouch --no-dereference: it updates the timestamps of the symlink itself instead of its target (thelutimes(2)syscall). Plaintouch AGENTS.mdfollows the link and touches the real file — precisely the write that the watcher never observes here, so it changes nothing about the cache.- The project candidates are registered through the unresolved path (
join(directory, "AGENTS.md")), whileprojectSource()/read()resolve the path withfs.resolve(realpath). As a result the watcher is attached to the symlink's own directory and only reports events for the symlink inode. Rewriting the target changes neither the link nor its directory entry, so no event is produced andrefresh()/discovery.reload()never run (see "Where in the code"). touch -hproduces a metadata change on the link inode that the watcher does report. That drives the existing invalidation path —Stream.debounce("100 millis")→refresh(file)→discovery.reload()— standing in for the missing event, so the location cache is refilled with the current contents of the target.- For a regular (non-symlinked)
AGENTS.mdthe same trick applies with a plaintouch AGENTS.md: on filesystems where events are unreliable, an explicit metadata event for the watched file forces the rescan that a missed notification would otherwise skip.
In other words, the manual touch is a deterministic substitute for the watcher event the stale cache depends on. It is a workaround, not a fix: it has to be repeated after every edit until instruction files are re-read at session start (or the cache is invalidated by some other means).
Environment
- opencode version: 2.0.18
- OS: Linux (Ubuntu, x64)
- Terminal: unavailable
- Shell: /usr/bin/bash
Activity
github-actions commentedon Sep 28, 2026
This issue might be a duplicate of or overlap with existing issues. Please check:
AGENTS.mdnot being present in a newly created/newsession; this issue identifies a symlink-specific cause for stale instructions.AGENTS.mdfiles are re-observed and delivered through durable Instructions, which may overlap with the stale-update behavior here.[-]core: new sessions keep stale AGENTS.md when the project instruction file is a symlink[/-][+]core: new sessions reuse stale AGENTS.md from the per-location instruction cache[/+]opencodejr commentedon Oct 28, 2026
Thanks for the detailed report. I can confirm this on 2.0.18, and it still happens on the latest
dev(86ce19d).Setup:
AGENTS.mdat the project root is a symlink toreal/AGENTS.md, and a simulated model reports which instruction text it received in its request.real/AGENTS.md(the target, not the symlink), waited a few seconds and started a new session with/new. That session still got the old contents.touch -h AGENTS.mddoes, and started another new session. That one got the new contents, so your workaround works.The recording and screenshots below show this, and the script below reproduces it.
The code matches your analysis: project
AGENTS.mdpaths are watched without resolving the symlink, but they are read through the resolved path. The globalAGENTS.mdand files listed ininstructionsare already resolved before they are watched.recording.mp4
Reproduction script (drive.ts)
Run it with opencode-drive:
opencode-drive run ./drive.ts. It drivesopencodefrom yourPATH; setOC_DEVto the path of an OpenCode checkout to run that from source instead.rafaetri commentedon Nov 13, 2026
I would like to take this one. I have a fix with a test: OpenCode now also watches the file a symlinked AGENTS.md points to, so edits to it reach new sessions. I put it up in #63754.