plugins: file.edited event missing before text, hard to extract edit locations #3360

Open
opened 2026-02-16 17:39:46 -05:00 by yindo · 1 comment
Owner

Originally created by @Profpatsch on GitHub (Dec 6, 2025).

Originally assigned to: @rekram1-node on GitHub.

Description

file.edited event should include line number information

We're building a plugin to track file edit positions using the file.edited event, but the event doesn't provide enough information to determine which line(s) were edited.

Currently, file.edited only provides:

{
  "type": "file.edited",
  "properties": {
    "file": ".opencode/plugin/README.md"
  }
}

This tells us which file was edited, but not where in the file the edit occurred.

Use Case

We want to track the line numbers of edits made by OpenCode agents so users can navigate through their edit history using commands like "jump to previous/next edit location". This is similar to how IDEs track code changes.

Our plugin integrates with agent-last-position, a service that maintains a history of (file_path, line_number) pairs and allows navigation between them.

Attempted Workarounds

1. Using session.diff event

We tried using session.diff which provides before and after file contents:

{
  "type": "session.diff",
  "properties": {
    "diff": [
      {
        "file": "path/to/file",
        "before": "",  // Often empty!
        "after": "full file contents...",
        "additions": 42,
        "deletions": 0
      }
    ]
  }
}

Problems:

  • The before field is empty ("") for files created during the session, even after they've been edited multiple times within the same session
  • When before is empty, we can't determine which lines were actually changed - the diff just shows the entire file as new
  • The diff is cumulative (shows all changes since session start), not incremental (changes since last event)
  • We have to run diff(1) on before and after to extract line numbers, which is inefficient
  • For files where before IS populated (files that existed before the session), we do get proper diffs, but this inconsistency makes it unreliable

Proposed Solution

Enhance the file.edited event to include edit location information:

{
  "type": "file.edited",
  "properties": {
    "file": ".opencode/plugin/README.md",
    "edits": [
      {
        "range": {
          "start": { "line": 32, "character": 0 },
          "end": { "line": 33, "character": 15 }
        },
        "newText": "..."
      }
    ]
  }
}

This would provide:

  • The file path (already present)
  • An array of edits with line/character positions (like LSP TextEdit format)
  • The new text content (optional, but useful)

Alternative Solutions

  1. Make tool.execute.before / tool.execute.after available as event types so plugins can capture file state before/after edits

  2. Fix session.diff to include proper before/after states for all files, not just files that existed before the session started

  3. Add a separate file.line.edited event that fires with just the line number for simpler use cases

Related

This is similar to how LSP provides textDocument/didChange with contentChanges that include range information.

OpenCode version

1.0.132

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

NixOS 25.05

Terminal

alacritty

Originally created by @Profpatsch on GitHub (Dec 6, 2025). Originally assigned to: @rekram1-node on GitHub. ### Description # `file.edited` event should include line number information We're building a plugin to track file edit positions using the `file.edited` event, but the event doesn't provide enough information to determine which line(s) were edited. Currently, `file.edited` only provides: ```json { "type": "file.edited", "properties": { "file": ".opencode/plugin/README.md" } } ``` This tells us *which* file was edited, but not *where* in the file the edit occurred. ## Use Case We want to track the line numbers of edits made by OpenCode agents so users can navigate through their edit history using commands like "jump to previous/next edit location". This is similar to how IDEs track code changes. Our plugin integrates with `agent-last-position`, a service that maintains a history of `(file_path, line_number)` pairs and allows navigation between them. ## Attempted Workarounds ### 1. Using `session.diff` event We tried using `session.diff` which provides `before` and `after` file contents: ```json { "type": "session.diff", "properties": { "diff": [ { "file": "path/to/file", "before": "", // Often empty! "after": "full file contents...", "additions": 42, "deletions": 0 } ] } } ``` **Problems:** - The `before` field is empty (`""`) for files created during the session, even after they've been edited multiple times within the same session - When `before` is empty, we can't determine which lines were actually changed - the diff just shows the entire file as new - The diff is cumulative (shows all changes since session start), not incremental (changes since last event) - We have to run `diff(1)` on `before` and `after` to extract line numbers, which is inefficient - For files where `before` IS populated (files that existed before the session), we do get proper diffs, but this inconsistency makes it unreliable - ## Proposed Solution Enhance the `file.edited` event to include edit location information: ```json { "type": "file.edited", "properties": { "file": ".opencode/plugin/README.md", "edits": [ { "range": { "start": { "line": 32, "character": 0 }, "end": { "line": 33, "character": 15 } }, "newText": "..." } ] } } ``` This would provide: - The file path (already present) - An array of edits with line/character positions (like LSP TextEdit format) - The new text content (optional, but useful) ## Alternative Solutions 1. **Make `tool.execute.before` / `tool.execute.after` available as event types** so plugins can capture file state before/after edits 2. **Fix `session.diff` to include proper before/after states** for all files, not just files that existed before the session started 3. **Add a separate `file.line.edited` event** that fires with just the line number for simpler use cases ## Related This is similar to how LSP provides `textDocument/didChange` with `contentChanges` that include range information. ### OpenCode version 1.0.132 ### Steps to reproduce _No response_ ### Screenshot and/or share link _No response_ ### Operating System NixOS 25.05 ### Terminal alacritty
yindo added the bug label 2026-02-16 17:39:46 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Dec 6, 2025):

This issue might be a duplicate of existing issues. Please check:

  • #5102: [FEATURE]: Session or Per-chat File Diff Viewer - Discusses the need to track and navigate to specific lines/blocks of code changes and understand which lines were edited

Both issues are related to understanding and tracking file edits at the line level, though #5102 focuses on a diff viewer UI while this issue focuses on plugin event data structure for tracking.

@github-actions[bot] commented on GitHub (Dec 6, 2025): This issue might be a duplicate of existing issues. Please check: - #5102: [FEATURE]: Session or Per-chat File Diff Viewer - Discusses the need to track and navigate to specific lines/blocks of code changes and understand which lines were edited Both issues are related to understanding and tracking file edits at the line level, though #5102 focuses on a diff viewer UI while this issue focuses on plugin event data structure for tracking.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#3360