-
Notifications
You must be signed in to change notification settings - Fork 0
feat(guardrail): add git freshness, merge gate, test falsifiability, doc reminder hooks #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -150,6 +150,19 @@ function str(data: unknown) { | |||||
| return typeof data === "string" ? data : "" | ||||||
| } | ||||||
|
|
||||||
| async function git(dir: string, args: string[]) { | ||||||
| const proc = Bun.spawn(["git", "-C", dir, ...args], { | ||||||
| stdout: "pipe", | ||||||
| stderr: "pipe", | ||||||
| }) | ||||||
| const [stdout, stderr] = await Promise.all([ | ||||||
| new Response(proc.stdout).text(), | ||||||
| new Response(proc.stderr).text(), | ||||||
| proc.exited, | ||||||
| ]) | ||||||
| return { stdout, stderr } | ||||||
| } | ||||||
|
|
||||||
| function free(data: { | ||||||
| id?: unknown | ||||||
| providerID?: unknown | ||||||
|
|
@@ -399,6 +412,8 @@ export default async function guardrail(input: { | |||||
| edits_since_review: 0, | ||||||
| last_block: "", | ||||||
| last_reason: "", | ||||||
| git_freshness_checked: false, | ||||||
| review_state: "", | ||||||
| }) | ||||||
| } | ||||||
| if (event.type === "permission.asked") { | ||||||
|
|
@@ -415,6 +430,44 @@ export default async function guardrail(input: { | |||||
| }) | ||||||
| } | ||||||
| }, | ||||||
| "chat.message": async ( | ||||||
| item: { | ||||||
| sessionID: string | ||||||
| }, | ||||||
| out: { | ||||||
| message: { | ||||||
| id: string | ||||||
| sessionID: string | ||||||
| role: string | ||||||
| } | ||||||
| parts: { | ||||||
| id: string | ||||||
| sessionID: string | ||||||
| messageID?: string | ||||||
| type: string | ||||||
| text: string | ||||||
| }[] | ||||||
| }, | ||||||
| ) => { | ||||||
| if (out.message.role !== "user") return | ||||||
| const data = await stash(state) | ||||||
| if (flag(data.git_freshness_checked)) return | ||||||
| await mark({ git_freshness_checked: true }) | ||||||
| try { | ||||||
| const fetchCheck = await git(input.worktree, ["fetch", "--dry-run"]) | ||||||
| if (fetchCheck.stdout.trim() || fetchCheck.stderr.includes("From")) { | ||||||
| out.parts.push({ | ||||||
| id: crypto.randomUUID(), | ||||||
| sessionID: out.message.sessionID, | ||||||
| messageID: out.message.id, | ||||||
| type: "text", | ||||||
| text: "⚠️ Your branch may be behind origin. Consider running `git pull` before making changes.", | ||||||
| }) | ||||||
| } | ||||||
| } catch { | ||||||
| // git fetch may fail in offline or no-remote scenarios; skip silently | ||||||
| } | ||||||
|
Comment on lines
+456
to
+469
|
||||||
| }, | ||||||
| "tool.execute.before": async ( | ||||||
| item: { tool: string; args?: unknown }, | ||||||
| out: { args: Record<string, unknown> }, | ||||||
|
|
@@ -450,6 +503,13 @@ export default async function guardrail(input: { | |||||
| await mark({ last_block: "bash", last_command: cmd, last_reason: "shell access to protected files" }) | ||||||
| throw new Error(text("shell access to protected files")) | ||||||
| } | ||||||
| if (/\b(git\s+merge|gh\s+pr\s+merge)\b/i.test(cmd)) { | ||||||
| const data = await stash(state) | ||||||
| if (str(data.review_state) !== "done") { | ||||||
| await mark({ last_block: "bash", last_command: cmd, last_reason: "merge blocked: review not done" }) | ||||||
| throw new Error(text("merge blocked: run /review before merging")) | ||||||
| } | ||||||
|
Comment on lines
+506
to
+511
|
||||||
| } | ||||||
| if (!bash(cmd)) return | ||||||
| if (!cfg.some((rule) => rule.test(file)) && !file.includes(".opencode/guardrails/")) return | ||||||
| await mark({ last_block: "bash", last_command: cmd, last_reason: "protected runtime or config mutation" }) | ||||||
|
|
@@ -458,7 +518,7 @@ export default async function guardrail(input: { | |||||
| }, | ||||||
| "tool.execute.after": async ( | ||||||
| item: { tool: string; args?: Record<string, unknown> }, | ||||||
| _out: { title: string; output: string; metadata: Record<string, unknown> }, | ||||||
| out: { title: string; output: string; metadata: Record<string, unknown> }, | ||||||
| ) => { | ||||||
| const now = new Date().toISOString() | ||||||
| const file = pick(item.args) | ||||||
|
|
@@ -508,13 +568,23 @@ export default async function guardrail(input: { | |||||
| if ((item.tool === "edit" || item.tool === "write") && file) { | ||||||
| const seen = list(data.edited_files) | ||||||
| const next = seen.includes(rel(input.worktree, file)) ? seen : [...seen, rel(input.worktree, file)] | ||||||
| const nextEditCount = num(data.edit_count) + 1 | ||||||
| await mark({ | ||||||
| edited_files: next, | ||||||
| edit_count: num(data.edit_count) + 1, | ||||||
| edit_count: nextEditCount, | ||||||
| edit_count_since_check: num(data.edit_count_since_check) + 1, | ||||||
| edits_since_review: num(data.edits_since_review) + 1, | ||||||
| last_edit: rel(input.worktree, file), | ||||||
| review_state: "", | ||||||
| }) | ||||||
|
Comment on lines
568
to
579
|
||||||
|
|
||||||
| if (/\.(test|spec)\.(ts|tsx|js|jsx)$|^test_.*\.py$|_test\.go$/.test(rel(input.worktree, file))) { | ||||||
|
||||||
| if (/\.(test|spec)\.(ts|tsx|js|jsx)$|^test_.*\.py$|_test\.go$/.test(rel(input.worktree, file))) { | |
| if (/\.(test|spec)\.(ts|tsx|js|jsx)$|(^|\/)test_.*\.py$|_test\.go$/.test(rel(input.worktree, file))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
git()helper drops the process exit code, so callers can’t distinguish a successful command from a failure (Bun.spawn won’t throw on non‑zero exit). Consider returningcode(like the existing helper inteam.ts) and using it in the freshness check to avoid warning on failed fetches.