[PR #6234] feat: add file persistence for large tool outputs #11791

Closed
opened 2026-02-16 18:16:44 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6234

State: closed
Merged: No


Edited 2026-01-07: Refactored to align to centralized truncation changes upstream; retain file persistence.

Summary

Enhances the centralized truncation system with file persistence for large tool outputs.

Background: Upstream recently added centralized truncation (Truncate.output() in tool.ts and registry.ts) which addresses "prompt is too long" errors by capping outputs at 50KB/2000 lines. However, this truncation permanently loses data - the model has no way to access content beyond the cutoff.

This PR retains the benefits of file persistence from the original approach: when outputs exceed the threshold, the full output is saved to disk and the model receives a preview with instructions for exploring the data using Read, Grep, or jq tools. The model can then make targeted requests to examine specific portions of large outputs.

Closes #4560
Related: #4826, #4845, #5360

Changes

truncation.ts - Extends the centralized truncation namespace with:

  • outputWithPersistence() - saves large outputs to ~/.local/share/opencode/storage/tool_results/{sessionID}/
  • Head (67%) + tail (33%) preview respecting caller's maxLines/maxBytes
  • JSON detection (.json extension + jq hint for structured queries)
  • 10MB hard cap on persisted files with proper multibyte handling
  • Path traversal protection (sessionID validation + resolve containment check)
  • cleanupSessionFiles() for session deletion lifecycle

tool.ts - Updates Tool.define() wrapper to use outputWithPersistence

registry.ts - Updates fromPlugin() to use outputWithPersistence

session/index.ts - Calls cleanup on session delete

Benefits over pure truncation

Aspect Upstream Truncation This PR
Prompt size Controlled Controlled
Data access Lost permanently Preserved on disk
Model exploration Cannot access Via Read/Grep/jq

Coverage

All tools automatically get file persistence:

  • Built-in tools via Tool.define() wrapper
  • Plugin tools via fromPlugin()
  • MCP tools (via registry)
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6234 **State:** closed **Merged:** No --- > Edited 2026-01-07: Refactored to align to centralized truncation changes upstream; retain file persistence. ## Summary Enhances the centralized truncation system with file persistence for large tool outputs. **Background**: Upstream recently added centralized truncation (`Truncate.output()` in `tool.ts` and `registry.ts`) which addresses "prompt is too long" errors by capping outputs at 50KB/2000 lines. However, this truncation **permanently loses data** - the model has no way to access content beyond the cutoff. This PR retains the benefits of file persistence from the original approach: when outputs exceed the threshold, the full output is saved to disk and the model receives a preview with instructions for exploring the data using Read, Grep, or jq tools. The model can then make targeted requests to examine specific portions of large outputs. **Closes #4560** **Related: #4826, #4845, #5360** ## Changes **`truncation.ts`** - Extends the centralized truncation namespace with: - `outputWithPersistence()` - saves large outputs to `~/.local/share/opencode/storage/tool_results/{sessionID}/` - Head (67%) + tail (33%) preview respecting caller's maxLines/maxBytes - JSON detection (`.json` extension + jq hint for structured queries) - 10MB hard cap on persisted files with proper multibyte handling - Path traversal protection (sessionID validation + resolve containment check) - `cleanupSessionFiles()` for session deletion lifecycle **`tool.ts`** - Updates `Tool.define()` wrapper to use `outputWithPersistence` **`registry.ts`** - Updates `fromPlugin()` to use `outputWithPersistence` **`session/index.ts`** - Calls cleanup on session delete ## Benefits over pure truncation | Aspect | Upstream Truncation | This PR | |--------|---------------------|---------| | Prompt size | ✅ Controlled | ✅ Controlled | | Data access | ❌ Lost permanently | ✅ Preserved on disk | | Model exploration | ❌ Cannot access | ✅ Via Read/Grep/jq | ## Coverage All tools automatically get file persistence: - ✅ Built-in tools via `Tool.define()` wrapper - ✅ Plugin tools via `fromPlugin()` - ✅ MCP tools (via registry)
yindo added the pull-request label 2026-02-16 18:16:44 -05:00
yindo closed this issue 2026-02-16 18:16:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11791