opencode-dev-framework 0.1.5 → 0.1.8
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 +71 -8
- package/bin/df +94 -0
- package/dist/gate.d.ts +6 -4
- package/dist/gate.js +15 -4
- package/dist/index.d.ts +20 -5
- package/dist/index.js +142 -69
- package/dist/installer.d.ts +35 -0
- package/dist/installer.js +155 -0
- package/dist/logger.d.ts +4 -2
- package/dist/logger.js +11 -4
- package/dist/registry.d.ts +23 -0
- package/dist/registry.js +40 -0
- package/dist/rules.d.ts +13 -6
- package/dist/rules.js +45 -10
- package/dist/tools.d.ts +3 -0
- package/dist/tools.js +97 -0
- package/package.json +7 -3
- package/rules/00-activation.md +36 -0
- package/rules/10-quality-bar.md +34 -0
- package/rules/20-match-existing-patterns.md +41 -0
- package/rules/30-testing-discipline.md +41 -0
- package/rules/40-delegation.md +50 -0
- package/templates/.opencode/agents/pattern-guardian.md +78 -0
- package/templates/.opencode/agents/style-enforcer.md +75 -0
- package/templates/.opencode/agents/test-grounder.md +71 -0
- package/templates/.opencode/commands/df-profile.md +7 -0
- package/templates/.opencode/commands/df-verify.md +8 -0
- package/templates/.opencode/skills/ground-in-tests/SKILL.md +53 -0
- package/templates/.opencode/skills/match-patterns/SKILL.md +60 -0
- package/templates/.opencode/skills/peer-review/SKILL.md +56 -0
- package/templates/.opencode-dev-framework.yml +35 -0
- package/commands/df-verify.md +0 -5
- package/rules/constitution.md +0 -23
package/README.md
CHANGED
|
@@ -9,16 +9,24 @@ reimplemented as an OpenCode-native plugin.
|
|
|
9
9
|
|
|
10
10
|
- **Constitution injection.** Adds a project constitution (quality bar, test
|
|
11
11
|
discipline, focus rules) to the system prompt at session start. Ships with a
|
|
12
|
-
bundled default; point `constitution` at your
|
|
12
|
+
bundled default split into numbered rule files; point `constitution` at your
|
|
13
|
+
own Markdown file to override.
|
|
13
14
|
- **Guardrails.** Blocks or warns on edits to protected paths (`.env` files,
|
|
14
15
|
`node_modules`, vendored code, your own globs) and on dangerous shell
|
|
15
16
|
commands (`git push`, `rm -rf`, `git reset --hard`, ...).
|
|
16
17
|
- **Per-edit lint.** Runs your configured linter on each file the agent edits
|
|
17
18
|
and reports failures loudly.
|
|
18
19
|
- **Completion gate.** When the session goes idle, runs typecheck, tests, and
|
|
19
|
-
(optionally) lint on changed files
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
(optionally) lint on changed files. In OpenCode versions with the
|
|
21
|
+
`experimental.session.stopping` hook, gate failures keep the session running
|
|
22
|
+
(up to `gate.max_blocks` times) so the agent must fix the failures. In older
|
|
23
|
+
versions the gate reports loudly via `session.idle`.
|
|
24
|
+
- **Custom tools.** `dev_framework_init` scaffolds project-level agents,
|
|
25
|
+
skills, commands, and config; `dev_framework_set_profile` changes the profile
|
|
26
|
+
in-session without restarting.
|
|
27
|
+
- **Slash-command templates.** `/df-verify` and `/df-profile` are installed
|
|
28
|
+
into `.opencode/commands/` by `df init` or the `dev_framework_init` tool.
|
|
29
|
+
(OpenCode does not load slash commands from plugin packages automatically.)
|
|
22
30
|
|
|
23
31
|
## Profiles
|
|
24
32
|
|
|
@@ -43,6 +51,53 @@ Then add it to your project's `opencode.json`:
|
|
|
43
51
|
}
|
|
44
52
|
```
|
|
45
53
|
|
|
54
|
+
Finally, scaffold the project-level files (agents, skills, commands, default
|
|
55
|
+
config) into your repo with the bundled `df` CLI:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npx df init # if installed locally as a project dependency
|
|
59
|
+
# or, after npm i -g opencode-dev-framework:
|
|
60
|
+
df init
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`df init` is interactive by default: it asks before overwriting existing files.
|
|
64
|
+
Use `--skip-existing` or `--overwrite-existing` for non-interactive runs.
|
|
65
|
+
`df status` shows what is and isn't scaffolded; `df version` prints the plugin
|
|
66
|
+
version.
|
|
67
|
+
|
|
68
|
+
## Local development and testing
|
|
69
|
+
|
|
70
|
+
To test the plugin from source without publishing to npm, point OpenCode at the
|
|
71
|
+
local repository path in your project's `opencode.json`:
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"plugin": ["/home/jerome/opencode-dev-framework"]
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The path must be the **repository root** (where `package.json` lives). OpenCode
|
|
80
|
+
loads `dist/index.js`, so rebuild after every source change:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npm run build
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Important caveats
|
|
87
|
+
|
|
88
|
+
- The `opencode plugin <module>` command creates a `.opencode/opencode.json`
|
|
89
|
+
file in the project directory that takes precedence over the project-level
|
|
90
|
+
`opencode.json`. If you used that command while testing, either delete
|
|
91
|
+
`.opencode/opencode.json` or make sure it also points to the local path.
|
|
92
|
+
- OpenCode caches downloaded plugins in
|
|
93
|
+
`~/.cache/opencode/packages/opencode-dev-framework*/`. If you previously
|
|
94
|
+
loaded a published version and then switch to a local source, clear that
|
|
95
|
+
cache so OpenCode does not reuse the old build:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
rm -rf ~/.cache/opencode/packages/opencode-dev-framework*
|
|
99
|
+
```
|
|
100
|
+
|
|
46
101
|
## Configuration
|
|
47
102
|
|
|
48
103
|
Create `.opencode-dev-framework.yml` in your project root:
|
|
@@ -77,12 +132,20 @@ compatibility, but the native format above is preferred. See
|
|
|
77
132
|
config reference and [`examples/go-service/`](examples/go-service/) for a
|
|
78
133
|
complete example.
|
|
79
134
|
|
|
135
|
+
## Slash command
|
|
136
|
+
|
|
137
|
+
The `/df-verify` and `/df-profile` commands are installed by `df init` (or the
|
|
138
|
+
`dev_framework_init` tool) into `.opencode/commands/`. Inside an OpenCode
|
|
139
|
+
session, type `/df-verify` to ask the agent to run the configured verification
|
|
140
|
+
suite manually, or `/df-profile strict` to change the profile.
|
|
141
|
+
|
|
80
142
|
## Limitations
|
|
81
143
|
|
|
82
|
-
- **The completion gate
|
|
83
|
-
`
|
|
84
|
-
|
|
85
|
-
|
|
144
|
+
- **The completion gate blocks only in newer OpenCode versions.** The
|
|
145
|
+
`experimental.session.stopping` hook (PR #41811) lets the plugin keep the
|
|
146
|
+
session running until checks pass. In older OpenCode versions the gate runs
|
|
147
|
+
on `session.idle` and reports failures loudly but cannot force the agent to
|
|
148
|
+
keep working.
|
|
86
149
|
- **Guardrails run inside OpenCode.** The `tool.execute.before` hook runs
|
|
87
150
|
after OpenCode's own permission system; it adds project rules on top, it
|
|
88
151
|
does not replace OpenCode permissions.
|
package/bin/df
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// df — opencode-dev-framework CLI installer.
|
|
3
|
+
// Scaffold project-level agents, skills, commands, and config into a repo.
|
|
4
|
+
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { installTemplates, resolveTargetDir, statusTemplates } from "../dist/installer.js";
|
|
9
|
+
|
|
10
|
+
async function getVersion() {
|
|
11
|
+
const packagePath = join(dirname(fileURLToPath(import.meta.url)), "..", "package.json");
|
|
12
|
+
const pkg = JSON.parse(await readFile(packagePath, "utf8"));
|
|
13
|
+
return pkg.version ?? "unknown";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function printHelp() {
|
|
17
|
+
console.log(`df — opencode-dev-framework project installer
|
|
18
|
+
|
|
19
|
+
Usage:
|
|
20
|
+
df init [dir] Scaffold templates into a project (default: cwd)
|
|
21
|
+
df status [dir] Show which template files are missing/present/different
|
|
22
|
+
df version Print the plugin version
|
|
23
|
+
df help Show this help
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--overwrite-existing Overwrite files that already exist (non-interactive)
|
|
27
|
+
--skip-existing Skip files that already exist (non-interactive)
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formatList(label, files) {
|
|
32
|
+
if (files.length === 0) {
|
|
33
|
+
return [`${label}: (none)`];
|
|
34
|
+
}
|
|
35
|
+
return [`${label}:`, ...files.map((file) => ` - ${file}`)];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function main() {
|
|
39
|
+
const args = process.argv.slice(2);
|
|
40
|
+
const command = args[0];
|
|
41
|
+
|
|
42
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
43
|
+
printHelp();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (command === "version" || command === "--version" || command === "-v") {
|
|
48
|
+
console.log(await getVersion());
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (command !== "init" && command !== "status") {
|
|
53
|
+
console.error(`Unknown command: ${command}`);
|
|
54
|
+
printHelp();
|
|
55
|
+
process.exitCode = 1;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const flags = new Set(args.slice(1).filter((arg) => arg.startsWith("--")));
|
|
60
|
+
const positional = args.slice(1).filter((arg) => !arg.startsWith("--"));
|
|
61
|
+
const targetDir = resolveTargetDir(positional[0]);
|
|
62
|
+
|
|
63
|
+
if (command === "status") {
|
|
64
|
+
const status = await statusTemplates(targetDir);
|
|
65
|
+
console.log(`Template status for ${targetDir}:`);
|
|
66
|
+
for (const line of [
|
|
67
|
+
...formatList("Missing", status.missing),
|
|
68
|
+
...formatList("Present", status.present),
|
|
69
|
+
...formatList("Different", status.different),
|
|
70
|
+
]) {
|
|
71
|
+
console.log(line);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const result = await installTemplates(targetDir, {
|
|
77
|
+
overwriteExisting: flags.has("--overwrite-existing"),
|
|
78
|
+
skipExisting: flags.has("--skip-existing"),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
console.log(`Installed templates into ${targetDir}:`);
|
|
82
|
+
for (const line of [
|
|
83
|
+
...formatList("Created", result.created),
|
|
84
|
+
...formatList("Overwritten", result.overwritten),
|
|
85
|
+
...formatList("Skipped", result.skipped),
|
|
86
|
+
]) {
|
|
87
|
+
console.log(line);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
main().catch((error) => {
|
|
92
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
93
|
+
process.exitCode = 1;
|
|
94
|
+
});
|
package/dist/gate.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Completion gate: run typecheck / test / lint-changed commands when the
|
|
3
|
-
* session
|
|
3
|
+
* session tries to stop, plus the changed-file tracking that feeds it.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* `session.
|
|
7
|
-
*
|
|
5
|
+
* The gate runs from two call sites:
|
|
6
|
+
* - `experimental.session.stopping` (OpenCode PR #41811): on failure, a
|
|
7
|
+
* synthetic user message keeps the session running, up to `gate.max_blocks`.
|
|
8
|
+
* - `session.idle` (fallback on older OpenCode): advisory logging only — by
|
|
9
|
+
* then the agent's turn has already finished, so failures cannot block.
|
|
8
10
|
*/
|
|
9
11
|
import type { CommandResult, RunCommand } from "./host.js";
|
|
10
12
|
import type { ResolvedConfig } from "./types.js";
|
package/dist/gate.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Completion gate: run typecheck / test / lint-changed commands when the
|
|
3
|
-
* session
|
|
3
|
+
* session tries to stop, plus the changed-file tracking that feeds it.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* `session.
|
|
7
|
-
*
|
|
5
|
+
* The gate runs from two call sites:
|
|
6
|
+
* - `experimental.session.stopping` (OpenCode PR #41811): on failure, a
|
|
7
|
+
* synthetic user message keeps the session running, up to `gate.max_blocks`.
|
|
8
|
+
* - `session.idle` (fallback on older OpenCode): advisory logging only — by
|
|
9
|
+
* then the agent's turn has already finished, so failures cannot block.
|
|
8
10
|
*/
|
|
9
11
|
import { isAbsolute, relative } from "node:path";
|
|
10
12
|
import { splitCommand } from "./config-to-opencode.js";
|
|
@@ -49,6 +51,15 @@ export function substituteFiles(template, files) {
|
|
|
49
51
|
* aggregated — a failing step does not abort the remaining steps.
|
|
50
52
|
*/
|
|
51
53
|
export async function runGate(run, config, changedFiles, options) {
|
|
54
|
+
if (!config.gate) {
|
|
55
|
+
return {
|
|
56
|
+
ran: false,
|
|
57
|
+
reason: "incomplete plugin config: gate section missing",
|
|
58
|
+
ok: true,
|
|
59
|
+
steps: [],
|
|
60
|
+
failedSteps: [],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
52
63
|
if (config.gate.skip_unchanged && changedFiles.length === 0) {
|
|
53
64
|
return { ran: false, reason: "no changed files", steps: [], ok: true, failedSteps: [] };
|
|
54
65
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* Wired so far:
|
|
5
5
|
* - tool.execute.before -> protected-path / dangerous-command guardrails
|
|
6
6
|
* - event (file.edited) -> per-edit lint + changed-file tracking
|
|
7
|
-
* - event (session.idle) -> completion gate (advisory
|
|
8
|
-
* - experimental.chat.system.transform -> constitution injection
|
|
7
|
+
* - event (session.idle) -> completion gate fallback (advisory logging)
|
|
8
|
+
* - experimental.chat.system.transform -> constitution injection + session mapping
|
|
9
|
+
* - experimental.session.stopping -> blocking completion gate (OpenCode >= PR #41811)
|
|
10
|
+
* - tool -> dev_framework_init / dev_framework_set_profile custom tools
|
|
9
11
|
*/
|
|
10
12
|
import type { Hooks, Plugin, PluginInput } from "@opencode-ai/plugin";
|
|
11
13
|
import { type ChangedFileTracker } from "./gate.js";
|
|
@@ -13,9 +15,22 @@ import { type RunCommand } from "./host.js";
|
|
|
13
15
|
import { type LogFn } from "./logger.js";
|
|
14
16
|
import type { ResolvedConfig } from "./types.js";
|
|
15
17
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
+
* The `experimental.session.stopping` hook was added in OpenCode PR #41811
|
|
19
|
+
* and is not yet in the published `@opencode-ai/plugin` types. We define it
|
|
20
|
+
* locally so the plugin compiles and automatically uses the hook when the
|
|
21
|
+
* runtime supports it.
|
|
18
22
|
*/
|
|
19
|
-
export
|
|
23
|
+
export interface HooksWithStopping extends Hooks {
|
|
24
|
+
"experimental.session.stopping"?: (input: {
|
|
25
|
+
sessionID: string;
|
|
26
|
+
}, output: {
|
|
27
|
+
context: string[];
|
|
28
|
+
}) => Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Build the plugin's hooks and custom tools. Exported (rather than inlined in
|
|
32
|
+
* the plugin) so tests can inject a stubbed command runner and tracker.
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildHooks(ctx: PluginInput, config: ResolvedConfig, log: LogFn, run?: RunCommand, tracker?: ChangedFileTracker, constitution?: string | null): HooksWithStopping;
|
|
20
35
|
export declare const devFramework: Plugin;
|
|
21
36
|
export default devFramework;
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* Wired so far:
|
|
5
5
|
* - tool.execute.before -> protected-path / dangerous-command guardrails
|
|
6
6
|
* - event (file.edited) -> per-edit lint + changed-file tracking
|
|
7
|
-
* - event (session.idle) -> completion gate (advisory
|
|
8
|
-
* - experimental.chat.system.transform -> constitution injection
|
|
7
|
+
* - event (session.idle) -> completion gate fallback (advisory logging)
|
|
8
|
+
* - experimental.chat.system.transform -> constitution injection + session mapping
|
|
9
|
+
* - experimental.session.stopping -> blocking completion gate (OpenCode >= PR #41811)
|
|
10
|
+
* - tool -> dev_framework_init / dev_framework_set_profile custom tools
|
|
9
11
|
*/
|
|
10
12
|
import { loadConfig } from "./config.js";
|
|
11
13
|
import { createChangedFileTracker, runGate, summarizeGate, } from "./gate.js";
|
|
@@ -13,79 +15,49 @@ import { runCommand } from "./host.js";
|
|
|
13
15
|
import { isLintFailure, lintFile, summarizeLint } from "./lint.js";
|
|
14
16
|
import { createLogger } from "./logger.js";
|
|
15
17
|
import { checkToolCall } from "./protect.js";
|
|
18
|
+
import { clearSessionDirectory, getDirectoryForSession, getHookState, setHookState, setSessionDirectory, } from "./registry.js";
|
|
16
19
|
import { injectConstitution, loadConstitution } from "./rules.js";
|
|
20
|
+
import { buildTools } from "./tools.js";
|
|
17
21
|
/**
|
|
18
|
-
* Build the plugin's hooks. Exported (rather than inlined in
|
|
19
|
-
* tests can inject a stubbed command runner and tracker.
|
|
22
|
+
* Build the plugin's hooks and custom tools. Exported (rather than inlined in
|
|
23
|
+
* the plugin) so tests can inject a stubbed command runner and tracker.
|
|
20
24
|
*/
|
|
21
25
|
export function buildHooks(ctx, config, log, run = runCommand, tracker = createChangedFileTracker(), constitution = null) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (!isLintFailure(outcome)) {
|
|
36
|
-
await log("info", summarizeLint(outcome), { filePath });
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const summary = summarizeLint(outcome);
|
|
40
|
-
await log("error", summary, {
|
|
41
|
-
filePath,
|
|
42
|
-
command: outcome.command?.join(" "),
|
|
43
|
-
stdout: outcome.result?.stdout,
|
|
44
|
-
stderr: outcome.result?.stderr,
|
|
45
|
-
});
|
|
46
|
-
// In strict mode lint failures throw. Note: file.edited is an event
|
|
47
|
-
// notification (the edit already happened), so this cannot undo the
|
|
48
|
-
// edit — it surfaces the failure loudly to the session.
|
|
49
|
-
if (config.profile === "strict") {
|
|
50
|
-
throw new Error(`[opencode-dev-framework] ${summary}`);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const onSessionIdle = async () => {
|
|
54
|
-
const changedFiles = tracker.getChangedFiles();
|
|
55
|
-
const report = await runGate(run, config, changedFiles, { cwd: ctx.directory });
|
|
56
|
-
tracker.clearChangedFiles();
|
|
57
|
-
const summary = summarizeGate(report);
|
|
58
|
-
if (!report.ran) {
|
|
59
|
-
await log("debug", summary);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (report.ok) {
|
|
63
|
-
await log("info", summary);
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
// The gate cannot physically block (session.idle fires after the turn
|
|
67
|
-
// ends), so failure visibility is the enforcement mechanism.
|
|
68
|
-
const level = config.gate.block_on_failure ? "error" : "warn";
|
|
69
|
-
await log(level, summary, {
|
|
70
|
-
failedSteps: report.failedSteps.map((step) => ({
|
|
71
|
-
name: step.name,
|
|
72
|
-
command: step.command?.join(" "),
|
|
73
|
-
exitCode: step.result?.exitCode,
|
|
74
|
-
timedOut: step.result?.timedOut,
|
|
75
|
-
stderr: step.result?.stderr,
|
|
76
|
-
})),
|
|
77
|
-
});
|
|
78
|
-
};
|
|
26
|
+
// All hook state — including the project directory — lives in the registry
|
|
27
|
+
// and is looked up at call time. OpenCode's Effect runtime stripped closure
|
|
28
|
+
// captures in production (config/log/run came back undefined), so hooks must
|
|
29
|
+
// not rely on variables from this scope.
|
|
30
|
+
setHookState(ctx.directory, {
|
|
31
|
+
directory: ctx.directory,
|
|
32
|
+
config,
|
|
33
|
+
log,
|
|
34
|
+
run,
|
|
35
|
+
tracker,
|
|
36
|
+
constitution,
|
|
37
|
+
blockCounts: new Map(),
|
|
38
|
+
});
|
|
79
39
|
return {
|
|
80
|
-
|
|
81
|
-
|
|
40
|
+
tool: buildTools(ctx),
|
|
41
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
42
|
+
const state = getHookState();
|
|
43
|
+
if (!state) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (input.sessionID) {
|
|
47
|
+
setSessionDirectory(input.sessionID, state.directory);
|
|
48
|
+
}
|
|
49
|
+
const next = injectConstitution(output.system, state.constitution);
|
|
82
50
|
if (next !== output.system) {
|
|
83
|
-
await log("info", "constitution injected into system prompt");
|
|
51
|
+
await state.log("info", "constitution injected into system prompt");
|
|
84
52
|
output.system = next;
|
|
85
53
|
}
|
|
86
54
|
},
|
|
87
55
|
"tool.execute.before": async (input, output) => {
|
|
88
|
-
const
|
|
56
|
+
const state = getHookState();
|
|
57
|
+
if (!state) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const result = checkToolCall(state.config, input.tool, output.args, state.directory);
|
|
89
61
|
if (result.decision === "allow") {
|
|
90
62
|
return;
|
|
91
63
|
}
|
|
@@ -96,19 +68,120 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
|
|
|
96
68
|
pattern: result.matchedPattern,
|
|
97
69
|
};
|
|
98
70
|
if (result.decision === "warn") {
|
|
99
|
-
await log("warn", message, extra);
|
|
71
|
+
await state.log("warn", message, extra);
|
|
100
72
|
return;
|
|
101
73
|
}
|
|
102
|
-
await log("error", message, extra);
|
|
74
|
+
await state.log("error", message, extra);
|
|
103
75
|
throw new Error(`[opencode-dev-framework] ${message}`);
|
|
104
76
|
},
|
|
77
|
+
"experimental.session.stopping": async (input, output) => {
|
|
78
|
+
const state = getHookState(getDirectoryForSession(input.sessionID) ?? undefined);
|
|
79
|
+
if (!state?.config.gate || state.config.profile === "off") {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const changedFiles = state.tracker.getChangedFiles();
|
|
83
|
+
const report = await runGate(state.run, state.config, changedFiles, { cwd: state.directory });
|
|
84
|
+
if (!report.ran || report.ok) {
|
|
85
|
+
state.tracker.clearChangedFiles();
|
|
86
|
+
state.blockCounts.delete(input.sessionID);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const maxBlocks = state.config.gate.max_blocks ?? 3;
|
|
90
|
+
const blockCount = (state.blockCounts.get(input.sessionID) ?? 0) + 1;
|
|
91
|
+
state.blockCounts.set(input.sessionID, blockCount);
|
|
92
|
+
if (blockCount > maxBlocks) {
|
|
93
|
+
// Standing down: clear the tracker so the session.idle fallback does
|
|
94
|
+
// not re-run the same failing gate commands a second time.
|
|
95
|
+
state.tracker.clearChangedFiles();
|
|
96
|
+
await state.log("warn", `completion gate has blocked ${maxBlocks} times; standing down but checks are still failing`, { failedSteps: report.failedSteps.map((step) => step.name) });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const summary = summarizeGate(report);
|
|
100
|
+
await state.log("error", summary, {
|
|
101
|
+
failedSteps: report.failedSteps.map((step) => step.name),
|
|
102
|
+
});
|
|
103
|
+
output.context.push(`opencode-dev-framework completion gate blocked you from finishing (block ${blockCount}/${maxBlocks}). ` +
|
|
104
|
+
`The following checks failed:\n\n${summary}\n\n` +
|
|
105
|
+
`Fix the underlying cause and continue. Do NOT disable, skip, or weaken these checks to make them pass.`);
|
|
106
|
+
},
|
|
105
107
|
event: async ({ event }) => {
|
|
108
|
+
// session.deleted must run even without hook state so the session map
|
|
109
|
+
// does not grow unboundedly.
|
|
110
|
+
if (event.type === "session.deleted") {
|
|
111
|
+
clearSessionDirectory(event.properties.info.id);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const state = getHookState();
|
|
115
|
+
if (!state) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
106
118
|
if (event.type === "file.edited") {
|
|
107
|
-
|
|
119
|
+
const filePath = event.properties.file;
|
|
120
|
+
state.tracker.add(filePath, state.directory);
|
|
121
|
+
if (!state.config.on_edit.lint) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const outcome = await lintFile(state.run, state.config, filePath, {
|
|
125
|
+
cwd: state.directory,
|
|
126
|
+
timeout: state.config.gate?.timeout,
|
|
127
|
+
});
|
|
128
|
+
if (outcome.skipped) {
|
|
129
|
+
await state.log("debug", summarizeLint(outcome), { filePath, reason: outcome.reason });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (!isLintFailure(outcome)) {
|
|
133
|
+
await state.log("info", summarizeLint(outcome), { filePath });
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const summary = summarizeLint(outcome);
|
|
137
|
+
await state.log("error", summary, {
|
|
138
|
+
filePath,
|
|
139
|
+
command: outcome.command?.join(" "),
|
|
140
|
+
stdout: outcome.result?.stdout,
|
|
141
|
+
stderr: outcome.result?.stderr,
|
|
142
|
+
});
|
|
143
|
+
// In strict mode lint failures throw. Note: file.edited is an event
|
|
144
|
+
// notification (the edit already happened), so this cannot undo the
|
|
145
|
+
// edit — it surfaces the failure loudly to the session.
|
|
146
|
+
if (state.config.profile === "strict") {
|
|
147
|
+
throw new Error(`[opencode-dev-framework] ${summary}`);
|
|
148
|
+
}
|
|
108
149
|
return;
|
|
109
150
|
}
|
|
110
151
|
if (event.type === "session.idle") {
|
|
111
|
-
|
|
152
|
+
if (state.config.profile === "off") {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!state.config.gate) {
|
|
156
|
+
await state.log("warn", "plugin config is incomplete (missing gate section), skipping completion gate", { directory: state.directory });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const changedFiles = state.tracker.getChangedFiles();
|
|
160
|
+
const report = await runGate(state.run, state.config, changedFiles, {
|
|
161
|
+
cwd: state.directory,
|
|
162
|
+
});
|
|
163
|
+
state.tracker.clearChangedFiles();
|
|
164
|
+
const summary = summarizeGate(report);
|
|
165
|
+
if (!report.ran) {
|
|
166
|
+
await state.log("debug", summary);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (report.ok) {
|
|
170
|
+
await state.log("info", summary);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
// The gate cannot physically block on session.idle (the turn already
|
|
174
|
+
// ended), so failure visibility is the enforcement mechanism.
|
|
175
|
+
const level = state.config.gate.block_on_failure ? "error" : "warn";
|
|
176
|
+
await state.log(level, summary, {
|
|
177
|
+
failedSteps: report.failedSteps.map((step) => ({
|
|
178
|
+
name: step.name,
|
|
179
|
+
command: step.command?.join(" "),
|
|
180
|
+
exitCode: step.result?.exitCode,
|
|
181
|
+
timedOut: step.result?.timedOut,
|
|
182
|
+
stderr: step.result?.stderr,
|
|
183
|
+
})),
|
|
184
|
+
});
|
|
112
185
|
}
|
|
113
186
|
},
|
|
114
187
|
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Directory containing the project templates shipped with the package. */
|
|
2
|
+
export declare const TEMPLATES_DIR: string;
|
|
3
|
+
export type OverwriteDecision = "overwrite" | "skip" | "overwrite-all" | "skip-all";
|
|
4
|
+
export type PromptFn = (relativePath: string) => Promise<OverwriteDecision>;
|
|
5
|
+
export interface InstallOptions {
|
|
6
|
+
/** How to handle files that already exist with different content. */
|
|
7
|
+
overwriteExisting?: boolean;
|
|
8
|
+
/** Non-interactive mode: never prompt. Defaults to skip when false. */
|
|
9
|
+
skipExisting?: boolean;
|
|
10
|
+
/** Custom prompt function for interactive installs. */
|
|
11
|
+
prompt?: PromptFn;
|
|
12
|
+
}
|
|
13
|
+
export interface InstallResult {
|
|
14
|
+
created: string[];
|
|
15
|
+
skipped: string[];
|
|
16
|
+
overwritten: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface TemplateStatus {
|
|
19
|
+
missing: string[];
|
|
20
|
+
present: string[];
|
|
21
|
+
different: string[];
|
|
22
|
+
}
|
|
23
|
+
/** List all template files relative to TEMPLATES_DIR. */
|
|
24
|
+
export declare function listTemplateFiles(): Promise<string[]>;
|
|
25
|
+
/**
|
|
26
|
+
* Install all bundled templates into a target project directory.
|
|
27
|
+
* Returns lists of created, skipped, and overwritten files (relative paths).
|
|
28
|
+
*/
|
|
29
|
+
export declare function installTemplates(targetDir: string, options?: InstallOptions): Promise<InstallResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Compare the target project against the bundled templates.
|
|
32
|
+
*/
|
|
33
|
+
export declare function statusTemplates(targetDir: string): Promise<TemplateStatus>;
|
|
34
|
+
/** Resolve a user-supplied directory (default cwd) to an absolute path. */
|
|
35
|
+
export declare function resolveTargetDir(dir?: string): string;
|