Skip to content

Agent hook for src file modification - #844

Merged
GiovanniCanali merged 2 commits into
devfrom
agent-hook
Oct 28, 2026
Merged

GiovanniCanali merged 2 commits into
devfrom
agent-hook

Conversation

@dario-coscia

@dario-coscia dario-coscia commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Description

Summary of Changes

Goal

Relax the old “never modify source code” rule and add automatic approval prompts when an agent edits files under pina/, tests/, or docs/.

Shared instruction

.opencode/skills/RULES.md, added a single shared rule:

Modify files - If the request will modify source files (pina, tests),
announce which files and wait for the user's explicit approval before editing..

opencode

  • .opencode/commands/pina-agentic.md

    • Replaced “never write files” with:
      • confirm target paths before writing;
      • require explicit approval for pina/, tests/, docs/.
    • Updated the delivery guidance so saved scripts require path confirmation.
  • .opencode/opencode.json (new)

    • Added edit permissions:
      "pina/*": "ask",
      "tests/*": "ask",
      "docs/*": "ask",
      "*": "allow"
  • .opencode/plugin/source-edit-guard.ts (new)

    • permission.ask re-asserts ask for protected source paths.
    • tool.execute.before shows a warning toast before an edit/write/patch.
    • Covers edit, write, and patch tools.

Claude Code

  • .claude/settings.json (new)

    • Added a PreToolUse hook for:
      Edit|Write|MultiEdit|NotebookEdit.
  • .claude/hooks/source_edit_guard.py (new)

    • Reads hook JSON from stdin.
    • Extracts file_path / notebook_path.
    • Returns:
      {
        "hookSpecificOutput": {
          "hookEventName": "PreToolUse",
          "permissionDecision": "ask",
          "permissionDecisionReason": "Modifying source file ..."
        }
      }
    • Stays silent for files outside the protected directories.

Tool support

Tool Enforcement
opencode Permission config + TS plugin
Claude Code PreToolUse hook
Codex Instruction-level only via RULES.md / AGENTS.md

Verification

  • opencode plugin logic tested with Node type stripping.
  • Claude hook tested with protected/relative/absolute/out-of-project paths.
  • Both JSON config files validated.
  • Claude Code should pick up the hook automatically; restart if not.

This PR fixes #842.

Checklist

  • Code follows the project’s Code Style Guidelines
  • Tests have been added or updated
  • Documentation has been updated if necessary
  • Pull request is linked to an open issue

@dario-coscia
dario-coscia requested a review from a team as a code owner October 21, 2026 18:38
@dario-coscia dario-coscia changed the title update Agent hook for src file modification Oct 21, 2026

@GiovanniCanali GiovanniCanali left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks clean. Both OpenCode and Claude Code provide native permission mechanisms that replicate the behavior previously enforced through custom hooks. I therefore removed the hooks and updated the corresponding JSON configuration files.

Additionally, I configured both tools to require explicit approval before executing shell commands. If you prefer a less restrictive setup, feel free to adjust the permissions in .claude/settings.json and .opencode/opencode.json.

I tested the updated configuration in OpenCode and confirmed that it behaves as expected.

@GiovanniCanali GiovanniCanali added pr-to-review Label for PR that are ready to been reviewed agents Agent-related features, issues, and improvements labels Oct 26, 2026

@dario-coscia dario-coscia left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Ready to merge for me

@GiovanniCanali
GiovanniCanali merged commit 6aa6931 into dev Oct 28, 2026
17 of 18 checks passed
@GiovanniCanali
GiovanniCanali deleted the agent-hook branch October 28, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent-related features, issues, and improvements pr-to-review Label for PR that are ready to been reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants