perf(opencode): resolve instruction files once per turn

Closed
PI
2mo ago
+6-2

Issue for this PR

Issues are disabled on this fork; motivation below. Stacked on #27 (top of the per-step-prep stack #23 → #25 → #26 → #27); re-target after it merges.

Type of change

  • Refactor / code improvement

What does this PR do?

instruction.system() ran inside the step loop: a findUp walk for AGENTS.md/CLAUDE.md/CONTEXT.md plus reads of every match — and an HTTP GET with a 5s timeout for any URL instruction — repeated on every step of every turn. Instruction files cannot change mid-turn except by the turn's own edits.

The resolve now happens once, before the loop. Deliberate behavior change, documented in-line with a one-line comment: an instruction file edited by the turn itself applies on the next turn (matching how skills and config already behave). Its cost stays visible in context_ms (#25).

How did you verify your code works?

prompt.test.ts 60/60; full packages/opencode suite over the whole stack (#23+#25+#26+#27+this): 3129 pass, failures limited to the pre-existing Truncate date bug (#24) and one pty load flake that passes in isolation. bun run typecheck clean, bun run lint 0 errors. No new test: pinning "mid-turn instruction edits are deferred" would freeze a deliberate simplification as if it were a contract.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code


Summary by cubic

Resolves instruction files once per turn instead of once per step to remove redundant I/O and network calls while keeping their cost visible in context_ms. Previously, instructions were re-resolved on every step; now they are precomputed before the loop, so instruction edits during a turn apply on the next turn.

  • Behavior change: Instruction edits made within a turn take effect on the next turn (aligned with skills and config).
  • Reviewer notes: instruction.system() is called once before the loop with timing and removed from the per-step Effect.all; verify contextMs accumulation and that precomputed instructions are used throughout packages/opencode/src/session/prompt.ts. No migration needed.