Skip to content

Commit a1019be

Browse files
committed
feat: pass last review findings to rescue automatically
After a successful /opencode:review or /opencode:adversarial-review, save the rendered output to ~/.opencode-companion/last-review-<hash>.md (per repo, SHA-256 of workspace path). Add a new 'last-review' subcommand that reports availability or streams the content. rescue.md now checks for a saved review when invoked without task text and asks via AskUserQuestion whether to fix the prior findings or describe a new task. The save is best-effort — a failed persistence never fails the review itself. Closes #44. Port of openai/codex-plugin-cc#129 (simplified: logic lives in the companion script rather than an inline node -e one-liner).
1 parent 3535f56 commit a1019be

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎plugins/opencode/commands/rescue.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,23 @@ Operating rules:
4646
- Leave the model unset unless the user explicitly asks for one.
4747
- Leave `--resume` and `--fresh` in the forwarded request. The subagent handles that routing when it builds the `task` command.
4848
- If the helper reports that OpenCode is missing or unauthenticated, stop and tell the user to run `/opencode:setup`.
49-
- If the user did not supply a request, ask what OpenCode should investigate or fix.
49+
- If the user did not supply a request, check for a saved review from `/opencode:review` or `/opencode:adversarial-review`:
50+
51+
```bash
52+
node "${CLAUDE_PLUGIN_ROOT}/scripts/opencode-companion.mjs" last-review
53+
```
54+
55+
- If stdout is `LAST_REVIEW_AVAILABLE`, use `AskUserQuestion` exactly once with two options:
56+
- `Fix issues from last review (Recommended)` — prepend the saved review content as context for the rescue task
57+
- `Describe a new task` — ask what OpenCode should investigate or fix
58+
- If the user chooses to fix from last review, read the saved review via:
59+
60+
```bash
61+
node "${CLAUDE_PLUGIN_ROOT}/scripts/opencode-companion.mjs" last-review --content
62+
```
63+
64+
and include its stdout verbatim in the forwarded task text, prefixed with:
65+
66+
`The following issues were found in a prior OpenCode review. Please fix them:\n\n`
67+
68+
- If stdout is `NO_LAST_REVIEW`, ask what OpenCode should investigate or fix.

‎plugins/opencode/scripts/opencode-companion.mjs‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
// wired the argument through but never adapted the shape.
3636
// (Apache License 2.0 §4(b) modification notice.)
3737

38+
import crypto from "node:crypto";
39+
import os from "node:os";
3840
import path from "node:path";
3941
import process from "node:process";
4042
import fs from "node:fs";
@@ -75,6 +77,7 @@ const handlers = {
7577
task: handleTask,
7678
"task-worker": handleTaskWorker,
7779
"task-resume-candidate": handleTaskResumeCandidate,
80+
"last-review": handleLastReview,
7881
status: handleStatus,
7982
result: handleResult,
8083
cancel: handleCancel,
@@ -214,6 +217,7 @@ async function handleReview(argv) {
214217
};
215218
});
216219

220+
saveLastReview(workspace, result.rendered);
217221
console.log(result.rendered);
218222
} catch (err) {
219223
console.error(`Review failed: ${err.message}`);
@@ -297,6 +301,7 @@ async function handleAdversarialReview(argv) {
297301
};
298302
});
299303

304+
saveLastReview(workspace, result.rendered);
300305
console.log(result.rendered);
301306
} catch (err) {
302307
console.error(`Adversarial review failed: ${err.message}`);
@@ -602,6 +607,69 @@ async function handleCancel(argv) {
602607
console.log(`Canceled job: ${job.id}`);
603608
}
604609

610+
// ------------------------------------------------------------------
611+
// Last-review persistence
612+
// ------------------------------------------------------------------
613+
614+
/**
615+
* Per-repo path where the most recent successful review is saved so the
616+
* rescue command can pick it up without the user copy-pasting findings.
617+
* @param {string} workspace
618+
* @returns {{ dir: string, file: string }}
619+
*/
620+
function lastReviewPath(workspace) {
621+
const hash = crypto.createHash("sha256").update(workspace).digest("hex").slice(0, 16);
622+
const dir = path.join(os.homedir(), ".opencode-companion");
623+
return { dir, file: path.join(dir, `last-review-${hash}.md`) };
624+
}
625+
626+
/**
627+
* Best-effort persistence of a rendered review so the rescue flow can read
628+
* it later. Never throws — a failed write must not fail the review itself.
629+
* @param {string} workspace
630+
* @param {string} rendered
631+
*/
632+
function saveLastReview(workspace, rendered) {
633+
if (!rendered) return;
634+
try {
635+
const { dir, file } = lastReviewPath(workspace);
636+
fs.mkdirSync(dir, { recursive: true });
637+
fs.writeFileSync(file, rendered, "utf8");
638+
} catch {
639+
// best-effort
640+
}
641+
}
642+
643+
async function handleLastReview(argv) {
644+
const { options } = parseArgs(argv, { booleanOptions: ["json", "content"] });
645+
const workspace = await resolveWorkspace();
646+
const { file } = lastReviewPath(workspace);
647+
648+
if (!fs.existsSync(file)) {
649+
if (options.json) console.log(JSON.stringify({ available: false }));
650+
else console.log("NO_LAST_REVIEW");
651+
return;
652+
}
653+
654+
if (options.content) {
655+
process.stdout.write(fs.readFileSync(file, "utf8"));
656+
return;
657+
}
658+
659+
if (options.json) {
660+
const stat = fs.statSync(file);
661+
console.log(
662+
JSON.stringify({
663+
available: true,
664+
updatedAt: stat.mtime.toISOString(),
665+
path: file,
666+
})
667+
);
668+
} else {
669+
console.log("LAST_REVIEW_AVAILABLE");
670+
}
671+
}
672+
605673
// ------------------------------------------------------------------
606674
// Helpers
607675
// ------------------------------------------------------------------

0 commit comments

Comments
 (0)