@kealthas-dev/opencode-verify-session 0.0.0-stage → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -2
- package/package.json +20 -4
- package/verify-session.ts +137 -0
package/README.md
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# verify-session
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`/verify <what the change should do>` starts a brand-new top-level opencode session that independently reviews the working tree, then switches the TUI to it. It is a plugin and not a `subtask: true` command because a subtask is a child session with restricted permissions; a top-level session runs as the normal agent with full tool permissions and can run tests itself. It answers `VERDICT: PASS | FAIL | UNVERIFIABLE` with evidence per requirement.
|
|
4
|
+
|
|
5
|
+
- Package: `@kealthas-dev/opencode-verify-session`, source `verify-session.ts`. Published; enabled in `deploy/opencode.json.example`.
|
|
6
|
+
|
|
7
|
+
## What the new session is given
|
|
8
|
+
|
|
9
|
+
The reviewer must not inherit the implementer's blind spots, so it gets only:
|
|
10
|
+
- the claim (`$ARGUMENTS`), treated as a hypothesis to falsify;
|
|
11
|
+
- the calling session's user messages as the spec (within a ~16k-character budget: the first two plus the newest that fit, gap marked), and only the assistant's last text message as its completion report, labelled UNVERIFIED — never its tool calls or reasoning;
|
|
12
|
+
- `git status --short`, a diff against the fork point from master/main (capped) and the branch commits, collected by the plugin itself so nothing is relayed second-hand.
|
|
13
|
+
|
|
14
|
+
## Mechanism
|
|
15
|
+
|
|
16
|
+
The `config` hook registers the `verify` command. `command.execute.before` creates the session, fires `session.promptAsync` without awaiting, and POSTs `/tui/select-session` (the v1 client the plugin receives has no `tui.selectSession`). The originating session still spends one trivial model turn on the command's replaced template, since the hook cannot suppress it.
|
|
17
|
+
|
|
18
|
+
## Tests and limits
|
|
19
|
+
|
|
20
|
+
`tests/unit/verify-session.test.mjs` covers command registration, a top-level session with no `parentID`, prompt content (no assistant-message leak), the `select-session` call and other commands being ignored. A real `opencode serve` run produced a separate session that ran its own check. The TUI actually switching to the new session needs an interactive TUI and is unverified.
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kealthas-dev/opencode-verify-session",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.1.5",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "verify-session.ts opencode plugin: adds a /verify command that starts a brand-new, full-permission opencode session to independently review the current changes, and switches the TUI to it. Written in TypeScript against @opencode-ai/plugin's Plugin type (type-only). Independently installable - not bundled with the other plugins.",
|
|
6
|
+
"main": "verify-session.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"verify-session.ts"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/Keal-thas/opencode-toolkit.git",
|
|
16
|
+
"directory": "plugins/verify-session"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@opencode-ai/plugin": "^1.0.0",
|
|
20
|
+
"@types/node": "^22.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
|
|
3
|
+
// /verify <claim> starts a separate top-level session (not a subtask, so it
|
|
4
|
+
// keeps the normal agent's full permissions) that reviews the current changes
|
|
5
|
+
// with a fresh context. The new session is seeded with only what an outside
|
|
6
|
+
// reviewer could observe: the claim, the user's own requests from the calling
|
|
7
|
+
// session (the spec), and git evidence. The calling session's assistant
|
|
8
|
+
// messages and reasoning are deliberately left out, so the reviewer can't
|
|
9
|
+
// inherit the implementer's blind spots.
|
|
10
|
+
|
|
11
|
+
const COMMAND_NAME = "verify";
|
|
12
|
+
// Char budget for the user's requests as a whole, not a message count: the opening messages usually define the task and the latest ones are often just "continue".
|
|
13
|
+
const MAX_REQUESTS_CHARS = 16_000;
|
|
14
|
+
const MAX_MESSAGE_CHARS = 3000;
|
|
15
|
+
const KEEP_FIRST = 2;
|
|
16
|
+
const MAX_DIFF_CHARS = 60_000;
|
|
17
|
+
const MAX_REPORT_CHARS = 6000;
|
|
18
|
+
|
|
19
|
+
const REVIEW_INSTRUCTIONS = `You are an independent verifier. You did not write this change and have no memory of how it was made. Trust only what you can observe yourself: the diff, the files, and the output of commands you run.
|
|
20
|
+
|
|
21
|
+
- The user does not trust the agent's completion report and is asking you to check it. Treat every statement in it as a hypothesis to falsify: "done", "tests pass", "fixed" mean nothing until you have observed them yourself.
|
|
22
|
+
- Compare three things: what the user asked for, what the report claims, and what the code actually does. Look for requirements the report silently skipped, results it overstated, and changes it did not mention.
|
|
23
|
+
- Check each requirement separately against the code, reading the surrounding code and not just the diff hunk.
|
|
24
|
+
- Run the project's tests, or the narrowest command that exercises the change, and report the real output.
|
|
25
|
+
- Do not modify files; describe fixes instead.
|
|
26
|
+
|
|
27
|
+
Finish with:
|
|
28
|
+
VERDICT: PASS | FAIL | UNVERIFIABLE
|
|
29
|
+
- one line per requirement: met / not met / not checked, with file:line or command evidence
|
|
30
|
+
- problems found, most severe first
|
|
31
|
+
- anything you could not check, and why`;
|
|
32
|
+
|
|
33
|
+
const truncate = (s: string, max: number) => (s.length > max ? `${s.slice(0, max)}\n...[truncated ${s.length - max} chars]` : s);
|
|
34
|
+
|
|
35
|
+
export const VerifySession: Plugin = async ({ client, serverUrl, $ }) => {
|
|
36
|
+
async function git(args: string[]): Promise<string> {
|
|
37
|
+
try {
|
|
38
|
+
return (await $`git ${args}`.quiet().nothrow().text()).trim();
|
|
39
|
+
} catch {
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const textOf = (parts: { type: string; synthetic?: boolean }[]) =>
|
|
45
|
+
parts.filter((p) => p.type === "text" && !p.synthetic).map((p) => (p as unknown as { text: string }).text).join("\n").trim();
|
|
46
|
+
|
|
47
|
+
// The user's own messages are the spec. From the assistant we take only its last
|
|
48
|
+
// text message - its completion report, which is the thing under suspicion - and
|
|
49
|
+
// never its tool calls or reasoning.
|
|
50
|
+
async function readSession(sessionID: string): Promise<{ requests: string[]; report: string }> {
|
|
51
|
+
try {
|
|
52
|
+
const res = await client.session.messages({ path: { id: sessionID }, throwOnError: true });
|
|
53
|
+
const msgs = res.data ?? [];
|
|
54
|
+
const requests = msgs.filter((m) => m.info.role === "user").map((m) => textOf(m.parts)).filter(Boolean).map((t) => truncate(t, MAX_MESSAGE_CHARS));
|
|
55
|
+
const report = msgs.filter((m) => m.info.role === "assistant").map((m) => textOf(m.parts)).filter(Boolean).at(-1) ?? "";
|
|
56
|
+
return { requests, report: truncate(report, MAX_REPORT_CHARS) };
|
|
57
|
+
} catch {
|
|
58
|
+
return { requests: [], report: "" };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Diff against the fork point from the main branch, so work the agent already committed is visible too (a plain `git diff HEAD` is empty then).
|
|
63
|
+
async function baseRef(): Promise<string | undefined> {
|
|
64
|
+
for (const b of ["origin/master", "origin/main", "master", "main"]) {
|
|
65
|
+
const mb = await git(["merge-base", "HEAD", b]);
|
|
66
|
+
if (mb) return mb;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Keep everything if it fits; otherwise keep the first KEEP_FIRST plus as many of the newest as fit, marking the gap.
|
|
71
|
+
function fitRequests(all: string[]): string[] {
|
|
72
|
+
const total = all.reduce((n, t) => n + t.length, 0);
|
|
73
|
+
if (total <= MAX_REQUESTS_CHARS) return all;
|
|
74
|
+
const head = all.slice(0, KEEP_FIRST);
|
|
75
|
+
let budget = MAX_REQUESTS_CHARS - head.reduce((n, t) => n + t.length, 0);
|
|
76
|
+
const tail: string[] = [];
|
|
77
|
+
for (let i = all.length - 1; i >= KEEP_FIRST && budget > 0; i--) {
|
|
78
|
+
if (all[i].length > budget) break;
|
|
79
|
+
tail.unshift(all[i]);
|
|
80
|
+
budget -= all[i].length;
|
|
81
|
+
}
|
|
82
|
+
const omitted = all.length - head.length - tail.length;
|
|
83
|
+
return [...head, `[... ${omitted} earlier messages omitted ...]`, ...tail];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function buildPrompt(claim: string, sourceSessionID: string): Promise<string> {
|
|
87
|
+
const base = await baseRef();
|
|
88
|
+
const [status, diff, log, session] = await Promise.all([
|
|
89
|
+
git(["status", "--short"]),
|
|
90
|
+
git(base ? ["diff", base] : ["diff", "HEAD"]),
|
|
91
|
+
git(base ? ["log", "--oneline", `${base}..HEAD`] : ["log", "--oneline", "-10"]),
|
|
92
|
+
readSession(sourceSessionID),
|
|
93
|
+
]);
|
|
94
|
+
const requests = fitRequests(session.requests);
|
|
95
|
+
return [
|
|
96
|
+
`## What the user asked for in the originating session\n${requests.length ? requests.map((r, i) => `${i + 1}. ${r}`).join("\n") : "(unavailable)"}`,
|
|
97
|
+
`## The agent's completion report - UNVERIFIED, the user doubts it\n${session.report || "(unavailable)"}`,
|
|
98
|
+
claim ? `## What the user wants checked specifically\n${claim}` : "",
|
|
99
|
+
`## git status --short\n${status || "(clean)"}`,
|
|
100
|
+
`## git diff ${base ? "<fork point from main branch>" : "HEAD"} (tracked files only, committed + uncommitted - untracked files appear in status above, read them yourself)\n${truncate(diff, MAX_DIFF_CHARS) || "(empty)"}`,
|
|
101
|
+
`## Commits on this branch\n${log || "(none)"}`,
|
|
102
|
+
].filter(Boolean).join("\n\n");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function selectSession(sessionID: string) {
|
|
106
|
+
// The v1 client the plugin receives has no tui.selectSession, so call the endpoint directly.
|
|
107
|
+
await fetch(new URL("/tui/select-session", serverUrl), {
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: { "content-type": "application/json" },
|
|
110
|
+
body: JSON.stringify({ sessionID }),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
config: async (config) => {
|
|
116
|
+
config.command ??= {};
|
|
117
|
+
config.command[COMMAND_NAME] ??= {
|
|
118
|
+
description: "Review the current changes in a brand-new session (usage: /verify [what to check in particular])",
|
|
119
|
+
template: "Reply with exactly: verify-session handled this command.",
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
"command.execute.before": async (input, output) => {
|
|
123
|
+
if (input.command !== COMMAND_NAME) return;
|
|
124
|
+
try {
|
|
125
|
+
const prompt = await buildPrompt(input.arguments.trim(), input.sessionID);
|
|
126
|
+
const created = await client.session.create({ body: { title: `verify: ${input.arguments.trim().slice(0, 60) || "current changes"}` }, throwOnError: true });
|
|
127
|
+
const sessionID = created.data.id;
|
|
128
|
+
// Fire and forget: the review runs for minutes, and the user should see it stream live in the new session.
|
|
129
|
+
void client.session.promptAsync({ path: { id: sessionID }, body: { system: REVIEW_INSTRUCTIONS, parts: [{ type: "text", text: prompt }] } }).catch(() => {});
|
|
130
|
+
await selectSession(sessionID).catch(() => {});
|
|
131
|
+
output.parts = [{ type: "text", text: `A new review session (${sessionID}) was started. Reply with exactly: verify started.` } as (typeof output.parts)[number]];
|
|
132
|
+
} catch (e) {
|
|
133
|
+
output.parts = [{ type: "text", text: `/verify failed to start a review session: ${e instanceof Error ? e.message : String(e)}. Tell the user.` } as (typeof output.parts)[number]];
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
};
|