@rvdbreemen/adr-kit-opencode 0.59.1 → 0.61.0
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/.claude-plugin/plugin.json +1 -1
- package/README.md +115 -39
- package/bin/adr-context +2 -1
- package/bin/adr-doctor +1 -1
- package/bin/adr-guardian +10 -1
- package/bin/adr-judge +12 -57
- package/bin/adr-settings +59 -8
- package/bin/adr-substance +15 -0
- package/bin/adr-suggest +8 -3
- package/bin/adr_config.py +33 -0
- package/bin/adr_doctor_checks.py +9 -3
- package/bin/adr_doctor_core.py +4 -4
- package/bin/adr_doctor_probes.py +100 -13
- package/bin/adr_llm.py +186 -3
- package/bin/adr_readiness.py +17 -1
- package/clients/workflows.json +10 -7
- package/hooks/adr_hook_core.py +176 -28
- package/instructions/ADR-guide.md +1 -1
- package/omp/extension.ts +365 -0
- package/opencode/plugin.ts +95 -14
- package/package.json +7 -1
- package/schemas/adr-kit-config.schema.json +7 -2
- package/scripts/client_generation.py +2 -5
- package/scripts/client_generation_artifacts.py +16 -2
- package/scripts/client_generation_model.py +9 -0
- package/scripts/release_phases.py +7 -7
- package/skills/adr/SKILL.md +2 -0
- package/skills/audit/SKILL.md +2 -0
- package/skills/context/SKILL.md +2 -0
- package/skills/grill/SKILL.md +2 -0
- package/skills/guardian/SKILL.md +8 -4
- package/skills/init/SKILL.md +34 -11
- package/skills/install-hooks/SKILL.md +12 -9
- package/skills/judge/SKILL.md +8 -3
- package/skills/lint/SKILL.md +4 -1
- package/skills/migrate/SKILL.md +2 -0
- package/skills/related/SKILL.md +4 -1
- package/skills/retire/SKILL.md +2 -0
- package/skills/review/SKILL.md +4 -1
- package/skills/settings/SKILL.md +2 -0
- package/skills/setup/SKILL.md +4 -1
- package/skills/supersede/SKILL.md +4 -1
- package/skills/upgrade/SKILL.md +24 -3
- package/templates/adr-kit-guide.md +3 -3
- package/templates/cc-settings/guardian-hook-entry.json +2 -2
- package/templates/githooks/pre-commit +37 -9
- package/templates/github-workflows/adr-index-check.yml +1 -1
- package/templates/github-workflows/adr-judge.yml +1 -1
- package/templates/github-workflows/adr-readiness.yml +1 -1
package/omp/extension.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// Native OMP (oh-my-pi) extension for ADR Kit (ADR-050).
|
|
2
|
+
//
|
|
3
|
+
// OMP installs this repository as a Claude-format marketplace plugin and reads
|
|
4
|
+
// its skills, agent and `.mcp.json` itself. What it does not do is run the
|
|
5
|
+
// command hooks in `hooks/hooks.json` or provide `${CLAUDE_PLUGIN_ROOT}`. This
|
|
6
|
+
// module, declared in `package.json` under `omp.extensions`, fills those two
|
|
7
|
+
// gaps and nothing else: every ADR decision is made by `hooks/adr-hook.py`.
|
|
8
|
+
import { existsSync } from "node:fs"
|
|
9
|
+
import { dirname, join, resolve } from "node:path"
|
|
10
|
+
import { fileURLToPath } from "node:url"
|
|
11
|
+
|
|
12
|
+
type AnyRecord = Record<string, any>
|
|
13
|
+
|
|
14
|
+
type HookResult = {
|
|
15
|
+
context: string
|
|
16
|
+
denied: boolean
|
|
17
|
+
reason: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type AgentIdentity = { kind?: string }
|
|
21
|
+
|
|
22
|
+
type HandlerContext = {
|
|
23
|
+
cwd: string
|
|
24
|
+
agent?: AgentIdentity
|
|
25
|
+
sessionManager?: { getSessionId?(): string }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type Handler = (event: AnyRecord, ctx: HandlerContext) => Promise<AnyRecord | void>
|
|
29
|
+
|
|
30
|
+
type ExtensionAPI = {
|
|
31
|
+
on(event: string, handler: Handler): void
|
|
32
|
+
setLabel?(label: string): void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type HookProcess = {
|
|
36
|
+
stdin: { write(value: string): void; end(): void }
|
|
37
|
+
stdout: ReadableStream<Uint8Array>
|
|
38
|
+
exited: Promise<number>
|
|
39
|
+
kill(): void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type HookOutcome = { timedOut: boolean; value: string; exitCode: number }
|
|
43
|
+
|
|
44
|
+
declare const Bun: {
|
|
45
|
+
spawn(
|
|
46
|
+
command: string[],
|
|
47
|
+
options: {
|
|
48
|
+
cwd: string
|
|
49
|
+
stdin: "pipe"
|
|
50
|
+
stdout: "pipe"
|
|
51
|
+
stderr: "ignore"
|
|
52
|
+
env: Record<string, string | undefined>
|
|
53
|
+
},
|
|
54
|
+
): HookProcess
|
|
55
|
+
spawnSync(
|
|
56
|
+
command: string[],
|
|
57
|
+
options: { stdout: "ignore"; stderr: "ignore"; timeout: number },
|
|
58
|
+
): { exitCode: number | null }
|
|
59
|
+
which(name: string): string | null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const PACKAGE_ROOT = dirname(dirname(fileURLToPath(import.meta.url)))
|
|
63
|
+
const MAX_CONTEXT_CHARS = 4096
|
|
64
|
+
const MAX_INPUT_CHARS = 8192
|
|
65
|
+
const MAX_OUTPUT_CHARS = 16384
|
|
66
|
+
const MAX_EDIT_PATHS = 32
|
|
67
|
+
const DEFAULT_HOOK_TIMEOUT_MS = 2000
|
|
68
|
+
const PR_HOOK_TIMEOUT_MS = 5000
|
|
69
|
+
const CONTEXT_MESSAGE_TYPE = "adr-kit-context"
|
|
70
|
+
const SYSTEM_MARKER = "<adr-kit-instructions>"
|
|
71
|
+
// The OMP tools that write files. `ast_edit` rewrites code in place.
|
|
72
|
+
const WRITE_TOOLS: Record<string, string> = { edit: "Edit", write: "Write", ast_edit: "Edit" }
|
|
73
|
+
const SHELL_TOOL = "bash"
|
|
74
|
+
const NO_RESULT: HookResult = { context: "", denied: false, reason: "" }
|
|
75
|
+
|
|
76
|
+
const STATIC_INSTRUCTIONS = `${SYSTEM_MARKER}
|
|
77
|
+
ADR Kit is active in this OMP session. Use the adr-kit MCP tools to find the
|
|
78
|
+
governing Accepted ADRs before implementation and to judge diffs when asked;
|
|
79
|
+
read the linked Markdown ADR before treating a result as binding. ADR Kit
|
|
80
|
+
workflows are skills: invoke them as /skill:<name>, for example /skill:grill.
|
|
81
|
+
Hook context is advisory and fail-open; the pre-commit and CI judge are the
|
|
82
|
+
enforcement floor.
|
|
83
|
+
</adr-kit-instructions>`
|
|
84
|
+
|
|
85
|
+
// Child sessions (task subagents) rebind this factory but share module state,
|
|
86
|
+
// so the latest context of a workspace's main session is kept here for the
|
|
87
|
+
// subagent and compaction hooks, which only forward what they are given.
|
|
88
|
+
const lastContext = new Map<string, string>()
|
|
89
|
+
|
|
90
|
+
// ADR-044: an explicit ADR_KIT_ROOT wins; otherwise the plugin this module
|
|
91
|
+
// was installed with.
|
|
92
|
+
function findRuntimeRoot(): string | undefined {
|
|
93
|
+
for (const candidate of [process.env.ADR_KIT_ROOT, PACKAGE_ROOT]) {
|
|
94
|
+
if (!candidate) continue
|
|
95
|
+
const root = resolve(candidate)
|
|
96
|
+
if (
|
|
97
|
+
existsSync(join(root, "bin", "adr-mcp")) &&
|
|
98
|
+
existsSync(join(root, "hooks", "adr-hook.py")) &&
|
|
99
|
+
existsSync(join(root, "skills"))
|
|
100
|
+
) return root
|
|
101
|
+
}
|
|
102
|
+
return undefined
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ADR-044's interpreter order, as in the OpenCode plugin: ADR_KIT_PYTHON, then
|
|
106
|
+
// the first of python3, python and, on Windows, py -3 that runs Python 3.10+.
|
|
107
|
+
function resolvePython(): string[] {
|
|
108
|
+
if (process.env.ADR_KIT_PYTHON) return [process.env.ADR_KIT_PYTHON]
|
|
109
|
+
const candidates = [["python3"], ["python"]]
|
|
110
|
+
if (process.platform === "win32") candidates.push(["py", "-3"])
|
|
111
|
+
for (const [name, ...args] of candidates) {
|
|
112
|
+
const executable = Bun.which(name)
|
|
113
|
+
if (!executable) continue
|
|
114
|
+
try {
|
|
115
|
+
const probe = Bun.spawnSync(
|
|
116
|
+
[executable, ...args, "-c", "import sys; sys.exit(sys.version_info < (3, 10))"],
|
|
117
|
+
{ stdout: "ignore", stderr: "ignore", timeout: 5000 },
|
|
118
|
+
)
|
|
119
|
+
if (probe.exitCode === 0) return [executable, ...args]
|
|
120
|
+
} catch {
|
|
121
|
+
// Not runnable here; try the next candidate.
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return []
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function bounded(value: unknown, limit: number): string {
|
|
128
|
+
return typeof value === "string" ? value.slice(0, limit) : ""
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function extractHookResult(raw: string): HookResult {
|
|
132
|
+
for (const line of raw.split(/\r?\n/).filter(Boolean)) {
|
|
133
|
+
try {
|
|
134
|
+
const value = JSON.parse(line) as AnyRecord
|
|
135
|
+
const specific = value.hookSpecificOutput && typeof value.hookSpecificOutput === "object"
|
|
136
|
+
? value.hookSpecificOutput
|
|
137
|
+
: {}
|
|
138
|
+
const context = bounded(specific.additionalContext, MAX_CONTEXT_CHARS)
|
|
139
|
+
const denied = specific.permissionDecision === "deny"
|
|
140
|
+
const reason = bounded(specific.permissionDecisionReason || context, MAX_CONTEXT_CHARS)
|
|
141
|
+
return { context, denied, reason }
|
|
142
|
+
} catch {
|
|
143
|
+
// The shared hook is fail-open. Ignore malformed output and continue.
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return NO_RESULT
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// The hook renders Claude Code's grill command; OMP exposes it as a skill.
|
|
150
|
+
function ompCommands(context: string): string {
|
|
151
|
+
return context
|
|
152
|
+
.replaceAll("/adr-kit:grill", "/skill:grill")
|
|
153
|
+
.replaceAll("$adr-kit:grill", "/skill:grill")
|
|
154
|
+
.replaceAll("adr-kit:grill", "/skill:grill")
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Only the paths an edit touches go to the hook: a written file's content can
|
|
158
|
+
// exceed the hook's 64 KiB input bound, which would drop the whole event. One
|
|
159
|
+
// path is sent as `file_path`; several as apply_patch headers, the shape the
|
|
160
|
+
// hook core already reads for multi-file edits.
|
|
161
|
+
function editInput(input: AnyRecord): AnyRecord | undefined {
|
|
162
|
+
const paths: string[] = []
|
|
163
|
+
for (const value of [input.path, input.file_path, ...(Array.isArray(input.paths) ? input.paths : [])]) {
|
|
164
|
+
if (typeof value === "string" && value && !paths.includes(value)) paths.push(value)
|
|
165
|
+
}
|
|
166
|
+
if (!paths.length) return undefined
|
|
167
|
+
if (paths.length === 1) return { file_path: paths[0] }
|
|
168
|
+
return {
|
|
169
|
+
patchText: paths
|
|
170
|
+
.slice(0, MAX_EDIT_PATHS)
|
|
171
|
+
.map((path) => `*** Update File: ${path}`)
|
|
172
|
+
.join("\n"),
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function sessionId(ctx: HandlerContext): string | undefined {
|
|
177
|
+
try {
|
|
178
|
+
return ctx.sessionManager?.getSessionId?.() || undefined
|
|
179
|
+
} catch {
|
|
180
|
+
return undefined
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isSubagent(ctx: HandlerContext): boolean {
|
|
185
|
+
return ctx.agent?.kind === "sub"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function wrap(context: string): string {
|
|
189
|
+
return `<adr-kit-context>\n${context}\n</adr-kit-context>`
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export default function adrKitOmpExtension(pi: ExtensionAPI): void {
|
|
193
|
+
const runtimeRoot = findRuntimeRoot()
|
|
194
|
+
if (!runtimeRoot) return
|
|
195
|
+
// Skills resolve the engine as ${ADR_KIT_ROOT:-${CLAUDE_PLUGIN_ROOT}}; OMP
|
|
196
|
+
// provides neither, and its shell tools inherit this process's environment.
|
|
197
|
+
if (!process.env.ADR_KIT_ROOT) process.env.ADR_KIT_ROOT = runtimeRoot
|
|
198
|
+
const hook = join(runtimeRoot, "hooks", "adr-hook.py")
|
|
199
|
+
let python: string[] | undefined
|
|
200
|
+
pi.setLabel?.("ADR Kit")
|
|
201
|
+
|
|
202
|
+
async function runHook(
|
|
203
|
+
cwd: string,
|
|
204
|
+
event: string,
|
|
205
|
+
session: string | undefined,
|
|
206
|
+
payload: AnyRecord,
|
|
207
|
+
timeoutMs: number,
|
|
208
|
+
): Promise<HookResult> {
|
|
209
|
+
python ??= resolvePython()
|
|
210
|
+
if (!python.length) return NO_RESULT
|
|
211
|
+
let child: HookProcess
|
|
212
|
+
try {
|
|
213
|
+
child = Bun.spawn([...python, hook, "--client", "claude-code-cli", "--event", event], {
|
|
214
|
+
cwd,
|
|
215
|
+
stdin: "pipe",
|
|
216
|
+
stdout: "pipe",
|
|
217
|
+
stderr: "ignore",
|
|
218
|
+
// The hook renders Claude-shaped output for this adapter, but the LLM
|
|
219
|
+
// layers must run on OMP's own model (ADR-049, ADR-050).
|
|
220
|
+
env: { ...process.env, ADR_KIT_HOST_CLIENT: "omp-cli" },
|
|
221
|
+
})
|
|
222
|
+
child.stdin.write(JSON.stringify({ cwd, hook_event_name: event, session_id: session, ...payload }))
|
|
223
|
+
child.stdin.end()
|
|
224
|
+
} catch {
|
|
225
|
+
return NO_RESULT
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const deadline = Promise.withResolvers<HookOutcome>()
|
|
229
|
+
const timer = setTimeout(() => deadline.resolve({ timedOut: true, value: "", exitCode: -1 }), timeoutMs)
|
|
230
|
+
const completed = Promise.all([
|
|
231
|
+
new Response(child.stdout).text().catch(() => ""),
|
|
232
|
+
child.exited.catch(() => -1),
|
|
233
|
+
]).then(([value, exitCode]): HookOutcome => ({ timedOut: false, value, exitCode }))
|
|
234
|
+
const result = await Promise.race([completed, deadline.promise])
|
|
235
|
+
clearTimeout(timer)
|
|
236
|
+
if (result.timedOut) {
|
|
237
|
+
try { child.kill() } catch { /* fail open */ }
|
|
238
|
+
return NO_RESULT
|
|
239
|
+
}
|
|
240
|
+
if (result.exitCode !== 0) return NO_RESULT
|
|
241
|
+
const parsed = extractHookResult(result.value.slice(0, MAX_OUTPUT_CHARS))
|
|
242
|
+
return { ...parsed, context: ompCommands(parsed.context) }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// A fault in a handler must never surface: OMP treats a throwing tool_call
|
|
246
|
+
// handler as a block, and every other moment here is advisory.
|
|
247
|
+
function on(event: string, handler: Handler): void {
|
|
248
|
+
pi.on(event, async (payload, ctx) => {
|
|
249
|
+
try {
|
|
250
|
+
return await handler(payload, ctx)
|
|
251
|
+
} catch {
|
|
252
|
+
return undefined
|
|
253
|
+
}
|
|
254
|
+
})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Session context waits here until the first prompt, the first moment OMP
|
|
258
|
+
// lets an extension add a message to the conversation.
|
|
259
|
+
let pending = ""
|
|
260
|
+
let subagentStarted = false
|
|
261
|
+
|
|
262
|
+
on("session_start", async (_event, ctx) => {
|
|
263
|
+
if (isSubagent(ctx)) return
|
|
264
|
+
const result = await runHook(ctx.cwd, "SessionStart", sessionId(ctx), {}, DEFAULT_HOOK_TIMEOUT_MS)
|
|
265
|
+
pending = result.context
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
on("before_agent_start", async (event, ctx) => {
|
|
269
|
+
const parts: string[] = []
|
|
270
|
+
if (isSubagent(ctx)) {
|
|
271
|
+
const parent = lastContext.get(ctx.cwd)
|
|
272
|
+
if (!subagentStarted && parent) {
|
|
273
|
+
subagentStarted = true
|
|
274
|
+
const result = await runHook(
|
|
275
|
+
ctx.cwd,
|
|
276
|
+
"SubagentStart",
|
|
277
|
+
sessionId(ctx),
|
|
278
|
+
{ parent_context: parent },
|
|
279
|
+
DEFAULT_HOOK_TIMEOUT_MS,
|
|
280
|
+
)
|
|
281
|
+
if (result.context) parts.push(result.context)
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
if (pending) parts.push(pending)
|
|
285
|
+
pending = ""
|
|
286
|
+
const prompt = bounded(event.prompt, MAX_INPUT_CHARS)
|
|
287
|
+
if (prompt.trim()) {
|
|
288
|
+
const result = await runHook(
|
|
289
|
+
ctx.cwd,
|
|
290
|
+
"UserPromptSubmit",
|
|
291
|
+
sessionId(ctx),
|
|
292
|
+
{ prompt },
|
|
293
|
+
DEFAULT_HOOK_TIMEOUT_MS,
|
|
294
|
+
)
|
|
295
|
+
if (result.context) parts.push(result.context)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const systemPrompt = Array.isArray(event.systemPrompt) ? event.systemPrompt : []
|
|
299
|
+
const output: AnyRecord = {}
|
|
300
|
+
if (!systemPrompt.some((item: unknown) => typeof item === "string" && item.includes(SYSTEM_MARKER))) {
|
|
301
|
+
output.systemPrompt = [...systemPrompt, STATIC_INSTRUCTIONS]
|
|
302
|
+
}
|
|
303
|
+
const context = parts.join("\n\n").slice(0, MAX_CONTEXT_CHARS)
|
|
304
|
+
if (context) {
|
|
305
|
+
if (!isSubagent(ctx)) lastContext.set(ctx.cwd, context)
|
|
306
|
+
output.message = { customType: CONTEXT_MESSAGE_TYPE, content: wrap(context), display: false }
|
|
307
|
+
}
|
|
308
|
+
return output
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
on("tool_call", async (event, ctx) => {
|
|
312
|
+
const tool = String(event.toolName || "").toLowerCase()
|
|
313
|
+
const input = event.input && typeof event.input === "object" ? event.input : {}
|
|
314
|
+
if (tool === SHELL_TOOL && typeof input.command === "string") {
|
|
315
|
+
const result = await runHook(
|
|
316
|
+
ctx.cwd,
|
|
317
|
+
"PreToolUse",
|
|
318
|
+
sessionId(ctx),
|
|
319
|
+
{ tool_name: "Bash", tool_input: { command: bounded(input.command, MAX_INPUT_CHARS) } },
|
|
320
|
+
PR_HOOK_TIMEOUT_MS,
|
|
321
|
+
)
|
|
322
|
+
if (result.denied) return { block: true, reason: result.reason || "ADR Kit blocked this pull-request command." }
|
|
323
|
+
return result.context ? { additionalContext: wrap(result.context) } : undefined
|
|
324
|
+
}
|
|
325
|
+
const toolName = WRITE_TOOLS[tool]
|
|
326
|
+
const toolInput = toolName ? editInput(input) : undefined
|
|
327
|
+
if (!toolName || !toolInput) return undefined
|
|
328
|
+
const result = await runHook(
|
|
329
|
+
ctx.cwd,
|
|
330
|
+
"PreToolUse",
|
|
331
|
+
sessionId(ctx),
|
|
332
|
+
{ tool_name: toolName, tool_input: toolInput },
|
|
333
|
+
DEFAULT_HOOK_TIMEOUT_MS,
|
|
334
|
+
)
|
|
335
|
+
return result.context ? { additionalContext: wrap(result.context) } : undefined
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
on("tool_result", async (event, ctx) => {
|
|
339
|
+
if (event.isError) return undefined
|
|
340
|
+
const toolName = WRITE_TOOLS[String(event.toolName || "").toLowerCase()]
|
|
341
|
+
const toolInput = toolName && event.input && typeof event.input === "object" ? editInput(event.input) : undefined
|
|
342
|
+
if (!toolName || !toolInput) return undefined
|
|
343
|
+
const result = await runHook(
|
|
344
|
+
ctx.cwd,
|
|
345
|
+
"PostToolUse",
|
|
346
|
+
sessionId(ctx),
|
|
347
|
+
{ tool_name: toolName, tool_input: toolInput },
|
|
348
|
+
DEFAULT_HOOK_TIMEOUT_MS,
|
|
349
|
+
)
|
|
350
|
+
return result.context ? { additionalContext: wrap(result.context) } : undefined
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
on("session.compacting", async (_event, ctx) => {
|
|
354
|
+
const parent = lastContext.get(ctx.cwd)
|
|
355
|
+
if (!parent) return undefined
|
|
356
|
+
const result = await runHook(
|
|
357
|
+
ctx.cwd,
|
|
358
|
+
"PreCompact",
|
|
359
|
+
sessionId(ctx),
|
|
360
|
+
{ parent_context: parent },
|
|
361
|
+
DEFAULT_HOOK_TIMEOUT_MS,
|
|
362
|
+
)
|
|
363
|
+
return result.context ? { context: [wrap(result.context)] } : undefined
|
|
364
|
+
})
|
|
365
|
+
}
|
package/opencode/plugin.ts
CHANGED
|
@@ -107,6 +107,60 @@ function unique(values: string[]): string[] {
|
|
|
107
107
|
return Array.from(new Set(values.filter(Boolean)))
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
// OpenCode dedupes plugins by npm package name only, so a global npm entry and
|
|
111
|
+
// a project's file:// or path entry both load (TASK-235). Each copy would add
|
|
112
|
+
// its own skills path and guide, spawn its own hook per event, and the first
|
|
113
|
+
// one would own the MCP server and commands. One registry per process, shared
|
|
114
|
+
// across module copies through Symbol.for, keeps one instance per project
|
|
115
|
+
// active: the last loaded, as OpenCode's own dedupe lets project config beat
|
|
116
|
+
// global config. The others no-op until the active one is disposed.
|
|
117
|
+
const INSTANCES_KEY = Symbol.for("adr-kit.opencode.instances")
|
|
118
|
+
|
|
119
|
+
function instanceRegistry(): Map<string, object[]> {
|
|
120
|
+
const scope = globalThis as unknown as Record<symbol, Map<string, object[]> | undefined>
|
|
121
|
+
return (scope[INSTANCES_KEY] ||= new Map())
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function registerInstance(directory: string): { active(): boolean; release(): void } {
|
|
125
|
+
const key = resolve(directory)
|
|
126
|
+
const token = {}
|
|
127
|
+
const registry = instanceRegistry()
|
|
128
|
+
registry.set(key, [...(registry.get(key) || []), token])
|
|
129
|
+
return {
|
|
130
|
+
active: () => registry.get(key)?.at(-1) === token,
|
|
131
|
+
release: () => {
|
|
132
|
+
const remaining = (registry.get(key) || []).filter((entry) => entry !== token)
|
|
133
|
+
if (remaining.length) registry.set(key, remaining)
|
|
134
|
+
else registry.delete(key)
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ADR-044's order: ADR_KIT_PYTHON, then the interpreter this plugin was
|
|
140
|
+
// configured with (an installer's pin), then the first of python3, python and,
|
|
141
|
+
// on Windows, py -3 that runs Python 3.10 or newer. A bare python is missing on
|
|
142
|
+
// macOS and most Linux distributions, and macOS's own python3 is 3.9.
|
|
143
|
+
function resolvePython(options: PluginOptions): string[] {
|
|
144
|
+
const pinned = process.env.ADR_KIT_PYTHON || options.python
|
|
145
|
+
if (pinned) return [pinned]
|
|
146
|
+
const candidates = [["python3"], ["python"]]
|
|
147
|
+
if (process.platform === "win32") candidates.push(["py", "-3"])
|
|
148
|
+
for (const [name, ...args] of candidates) {
|
|
149
|
+
const executable = Bun.which(name)
|
|
150
|
+
if (!executable) continue
|
|
151
|
+
try {
|
|
152
|
+
const probe = Bun.spawnSync(
|
|
153
|
+
[executable, ...args, "-c", "import sys; sys.exit(sys.version_info < (3, 10))"],
|
|
154
|
+
{ stdout: "ignore", stderr: "ignore", timeout: 5000 },
|
|
155
|
+
)
|
|
156
|
+
if (probe.exitCode === 0) return [executable, ...args]
|
|
157
|
+
} catch {
|
|
158
|
+
// Not runnable here; try the next candidate.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return [candidates[0][0]]
|
|
162
|
+
}
|
|
163
|
+
|
|
110
164
|
function bounded(value: unknown, limit: number): string {
|
|
111
165
|
return typeof value === "string" ? value.slice(0, limit) : ""
|
|
112
166
|
}
|
|
@@ -179,7 +233,7 @@ function normalizeAutoGrillCommand(context: string): string {
|
|
|
179
233
|
|
|
180
234
|
async function runHook(
|
|
181
235
|
runtimeRoot: string | undefined,
|
|
182
|
-
python: string,
|
|
236
|
+
python: string[],
|
|
183
237
|
cwd: string,
|
|
184
238
|
event: string,
|
|
185
239
|
sessionID: string | undefined,
|
|
@@ -193,8 +247,16 @@ async function runHook(
|
|
|
193
247
|
let child: ReturnType<typeof Bun.spawn>
|
|
194
248
|
try {
|
|
195
249
|
child = Bun.spawn(
|
|
196
|
-
[python, hook, "--client", "claude-code-cli", "--event", event],
|
|
197
|
-
{
|
|
250
|
+
[...python, hook, "--client", "claude-code-cli", "--event", event],
|
|
251
|
+
{
|
|
252
|
+
cwd,
|
|
253
|
+
stdin: "pipe",
|
|
254
|
+
stdout: "pipe",
|
|
255
|
+
stderr: "ignore",
|
|
256
|
+
// The hook renders Claude-shaped output for this plugin, but the LLM
|
|
257
|
+
// layers must run on OpenCode's own model (ADR-049, TASK-233).
|
|
258
|
+
env: { ...process.env, ADR_KIT_HOST_CLIENT: "opencode-cli" },
|
|
259
|
+
},
|
|
198
260
|
)
|
|
199
261
|
child.stdin.write(JSON.stringify({
|
|
200
262
|
cwd,
|
|
@@ -244,6 +306,12 @@ function toolPath(args: AnyRecord): string {
|
|
|
244
306
|
return ""
|
|
245
307
|
}
|
|
246
308
|
|
|
309
|
+
// apply_patch names no file path: its files are in the patch's header lines,
|
|
310
|
+
// which the hook core reads from patchText.
|
|
311
|
+
function editsFiles(args: AnyRecord): boolean {
|
|
312
|
+
return Boolean(toolPath(args)) || typeof args.patchText === "string"
|
|
313
|
+
}
|
|
314
|
+
|
|
247
315
|
function hookTimeout(options: PluginOptions, fallback: number): number {
|
|
248
316
|
const configured = options.hookTimeoutMs
|
|
249
317
|
if (!Number.isInteger(configured)) return fallback
|
|
@@ -257,9 +325,12 @@ function addContext(sessions: Map<string, SessionState>, sessionID: string | und
|
|
|
257
325
|
state.contextKind = kind
|
|
258
326
|
}
|
|
259
327
|
|
|
260
|
-
function commandTemplate(root: string, workflow: AnyRecord): string {
|
|
328
|
+
function commandTemplate(root: string, python: string[], workflow: AnyRecord): string {
|
|
329
|
+
const interpreter = python.map((part) => (/\s/.test(part) ? `"${part}"` : part)).join(" ")
|
|
261
330
|
const procedure = Array.isArray(workflow.procedure)
|
|
262
|
-
? workflow.procedure
|
|
331
|
+
? workflow.procedure
|
|
332
|
+
.map((item) => `- ${String(item).replaceAll("<plugin-root>", root).replaceAll("<python>", interpreter)}`)
|
|
333
|
+
.join("\n")
|
|
263
334
|
: "- Follow the ADR Kit documentation and use the adr-kit MCP tools."
|
|
264
335
|
return [
|
|
265
336
|
`Run the ADR Kit '${workflow.id}' workflow in OpenCode.`,
|
|
@@ -280,10 +351,11 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
280
351
|
) {
|
|
281
352
|
const options = rawOptions as PluginOptions
|
|
282
353
|
const runtimeRoot = findRuntimeRoot(options.root, input.directory, input.worktree)
|
|
283
|
-
const python = options
|
|
354
|
+
const python = resolvePython(options)
|
|
284
355
|
const sessions = new Map<string, SessionState>()
|
|
356
|
+
const instance = registerInstance(input.directory)
|
|
285
357
|
|
|
286
|
-
|
|
358
|
+
const hooks: Record<string, (...args: any[]) => Promise<void>> = {
|
|
287
359
|
config: async (rawConfig: AnyRecord) => {
|
|
288
360
|
if (!runtimeRoot) return
|
|
289
361
|
const config = rawConfig as AnyRecord
|
|
@@ -316,7 +388,7 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
316
388
|
if (!config.mcp["adr-kit"]) {
|
|
317
389
|
config.mcp["adr-kit"] = {
|
|
318
390
|
type: "local",
|
|
319
|
-
command: [python, join(runtimeRoot, "bin", "adr-mcp"), "--root", input.directory],
|
|
391
|
+
command: [...python, join(runtimeRoot, "bin", "adr-mcp"), "--root", input.directory],
|
|
320
392
|
cwd: input.directory,
|
|
321
393
|
enabled: true,
|
|
322
394
|
timeout: 60000,
|
|
@@ -335,7 +407,7 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
335
407
|
if (config.command[name]) continue
|
|
336
408
|
config.command[name] = {
|
|
337
409
|
description: `ADR Kit: ${workflow.description || workflow.title || workflow.id}`,
|
|
338
|
-
template: commandTemplate(runtimeRoot, workflow),
|
|
410
|
+
template: commandTemplate(runtimeRoot, python, workflow),
|
|
339
411
|
}
|
|
340
412
|
}
|
|
341
413
|
}
|
|
@@ -417,7 +489,7 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
417
489
|
if (result.denied) throw new Error(result.reason || "ADR Kit blocked this pull-request command.")
|
|
418
490
|
return
|
|
419
491
|
}
|
|
420
|
-
if (!WRITE_TOOLS.has(normalized) || !
|
|
492
|
+
if (!WRITE_TOOLS.has(normalized) || !editsFiles(args)) return
|
|
421
493
|
const result = await runHook(
|
|
422
494
|
runtimeRoot,
|
|
423
495
|
python,
|
|
@@ -435,7 +507,7 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
435
507
|
) => {
|
|
436
508
|
if (options.hooks === false) return
|
|
437
509
|
const normalized = toolName(hookInput.tool)
|
|
438
|
-
if (!WRITE_TOOLS.has(normalized) || !
|
|
510
|
+
if (!WRITE_TOOLS.has(normalized) || !editsFiles(hookInput.args || {})) return
|
|
439
511
|
const result = await runHook(
|
|
440
512
|
runtimeRoot,
|
|
441
513
|
python,
|
|
@@ -455,8 +527,17 @@ export default async function AdrKitOpenCodePlugin(
|
|
|
455
527
|
if (event.type === "session.deleted" && typeof id === "string") sessions.delete(id)
|
|
456
528
|
},
|
|
457
529
|
|
|
458
|
-
dispose: async () => {
|
|
459
|
-
sessions.clear()
|
|
460
|
-
},
|
|
461
530
|
}
|
|
531
|
+
|
|
532
|
+
const guarded: Record<string, (...args: any[]) => Promise<void>> = {}
|
|
533
|
+
for (const [name, hook] of Object.entries(hooks)) {
|
|
534
|
+
guarded[name] = async (...args: any[]) => {
|
|
535
|
+
if (instance.active()) await hook(...args)
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
guarded.dispose = async () => {
|
|
539
|
+
instance.release()
|
|
540
|
+
sessions.clear()
|
|
541
|
+
}
|
|
542
|
+
return guarded
|
|
462
543
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rvdbreemen/adr-kit-opencode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.61.0",
|
|
4
4
|
"description": "Native OpenCode plugin for ADR Kit, backed by the shared deterministic Python engines.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode/plugin.ts",
|
|
@@ -20,8 +20,14 @@
|
|
|
20
20
|
"engines": {
|
|
21
21
|
"opencode": ">=1.18.0"
|
|
22
22
|
},
|
|
23
|
+
"omp": {
|
|
24
|
+
"extensions": [
|
|
25
|
+
"./omp/extension.ts"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
23
28
|
"files": [
|
|
24
29
|
"opencode/plugin.ts",
|
|
30
|
+
"omp/extension.ts",
|
|
25
31
|
"bin/adr",
|
|
26
32
|
"bin/adr-*",
|
|
27
33
|
"bin/*.py",
|
|
@@ -197,9 +197,9 @@
|
|
|
197
197
|
},
|
|
198
198
|
"llm_timeout_seconds": {
|
|
199
199
|
"type": "integer",
|
|
200
|
-
"description": "Per-call timeout for the suggestion LLM pass in seconds.
|
|
200
|
+
"description": "Per-call timeout for the suggestion LLM pass in seconds. When unset: judge.llm_timeout_seconds if set, otherwise 30 (TASK-122: a nudge must not outlast what a person will wait for).",
|
|
201
201
|
"minimum": 1,
|
|
202
|
-
"default":
|
|
202
|
+
"default": 30
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
},
|
|
@@ -366,6 +366,11 @@
|
|
|
366
366
|
"minimum": 0,
|
|
367
367
|
"default": 24
|
|
368
368
|
},
|
|
369
|
+
"llm_enabled": {
|
|
370
|
+
"type": "boolean",
|
|
371
|
+
"description": "When false, the guardian's LLM tier (adr-substance and the full audit) is never due and calls no model. Defaults to true. An init or upgrade answer sets it for this machine only in .adr-kit.local.json (TASK-233); the cheap tier is unaffected.",
|
|
372
|
+
"default": true
|
|
373
|
+
},
|
|
369
374
|
"llm_autorun": {
|
|
370
375
|
"type": "boolean",
|
|
371
376
|
"description": "When false (default), the /adr-kit:guardian skill always asks the user to confirm cost before running the LLM tier (adr-suggest + full audit). Set true only if you want the LLM tier to run without an interactive confirmation \u2014 consistent with ADR-001's opt-in posture, this defaults to false. The cheap tier (drift + retire + lint) is always free and unaffected by this flag.",
|
|
@@ -69,8 +69,7 @@ def _same_content(actual: bytes | None, expected: bytes) -> bool:
|
|
|
69
69
|
Only the EOL dimension is relaxed. Content is still compared byte for byte,
|
|
70
70
|
so a single changed character is still drift.
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
byte-exactly and never normalised: a 0x0D 0x0A pair inside a binary is data,
|
|
72
|
+
A binary output would be compared byte-exactly and never normalised: a 0x0D 0x0A pair inside a binary is data,
|
|
74
73
|
not a line ending, and collapsing it would make two genuinely different
|
|
75
74
|
binaries compare equal. A NUL byte on either side is the binary signal --
|
|
76
75
|
the same heuristic git itself uses.
|
|
@@ -140,9 +139,7 @@ def generate(
|
|
|
140
139
|
|
|
141
140
|
for relative in HOOK_RUNTIME_FILES:
|
|
142
141
|
source = source_root / relative
|
|
143
|
-
content = read(source, stats)
|
|
144
|
-
if source.suffix.casefold() not in {".exe", ".dll"}:
|
|
145
|
-
content = content.replace(b"\r\n", b"\n")
|
|
142
|
+
content = read(source, stats).replace(b"\r\n", b"\n")
|
|
146
143
|
suffix = relative.removeprefix("hooks/")
|
|
147
144
|
mode = stat.S_IMODE(source.stat().st_mode)
|
|
148
145
|
for client_dir in GENERATED_CLIENTS.values():
|
|
@@ -11,6 +11,7 @@ from client_generation_model import (
|
|
|
11
11
|
COPY_ROOTS,
|
|
12
12
|
HOOK_RUNTIME_FILES,
|
|
13
13
|
PROVENANCE,
|
|
14
|
+
REPLY_RULE,
|
|
14
15
|
RUNTIME_SUPPORT_FILES,
|
|
15
16
|
WORKFLOW_IDS,
|
|
16
17
|
GenerationError,
|
|
@@ -243,9 +244,22 @@ def render_skill(workflow: dict, client_id: str) -> bytes:
|
|
|
243
244
|
f"<!-- {PROVENANCE}; schema v1; client {client_id}. -->",
|
|
244
245
|
f"# {workflow['title']}",
|
|
245
246
|
"",
|
|
247
|
+
REPLY_RULE,
|
|
248
|
+
"",
|
|
246
249
|
invocation,
|
|
247
|
-
|
|
248
|
-
|
|
250
|
+
*(
|
|
251
|
+
[
|
|
252
|
+
"Resolve `<plugin-root>` from this installed skill, and `<python>` to the",
|
|
253
|
+
"first of `python3`, `python` or, on Windows, `py -3` that runs Python 3.10",
|
|
254
|
+
"or newer. Use only bundled, local ADR Kit tools and follow this canonical",
|
|
255
|
+
"workflow:",
|
|
256
|
+
]
|
|
257
|
+
if any("<python>" in step for step in workflow["procedure"])
|
|
258
|
+
else [
|
|
259
|
+
"Resolve `<plugin-root>` from this installed skill. Use only bundled,",
|
|
260
|
+
"local ADR Kit tools and follow this canonical workflow:",
|
|
261
|
+
]
|
|
262
|
+
),
|
|
249
263
|
"",
|
|
250
264
|
]
|
|
251
265
|
lines.extend(f"{index}. {step}" for index, step in enumerate(workflow["procedure"], 1))
|
|
@@ -80,6 +80,10 @@ RUNTIME_SUPPORT_FILES = (
|
|
|
80
80
|
"hooks/hook_benchmark.py",
|
|
81
81
|
"scripts/adr_settings.py",
|
|
82
82
|
"scripts/project_setup.py",
|
|
83
|
+
# The setup workflow runs <plugin-root>/scripts/setup-project.py, and an
|
|
84
|
+
# installed skill's <plugin-root> is its own tree. It imports only the two
|
|
85
|
+
# modules above and reads instructions/ and templates/, both mirrored.
|
|
86
|
+
"scripts/setup-project.py",
|
|
83
87
|
)
|
|
84
88
|
SOURCE_FILES = (
|
|
85
89
|
"clients/capabilities.json",
|
|
@@ -100,6 +104,11 @@ SOURCE_FILES = (
|
|
|
100
104
|
"copilot/.mcp.json",
|
|
101
105
|
)
|
|
102
106
|
PROVENANCE = "Generated by scripts/build-client-adapters.py from clients/workflows.json"
|
|
107
|
+
# Same sentence as under the H1 of every canonical skills/*/SKILL.md (TASK-238).
|
|
108
|
+
REPLY_RULE = (
|
|
109
|
+
"Reply in the language the user writes in; default to English. Keep command "
|
|
110
|
+
"output, identifiers, paths, and ADR file content unchanged."
|
|
111
|
+
)
|
|
103
112
|
CACHE_VERSION = 1
|
|
104
113
|
|
|
105
114
|
|