opencode-dev-framework 0.1.2
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/LICENSE +21 -0
- package/README.md +116 -0
- package/commands/df-verify.md +5 -0
- package/dist/config-to-opencode.d.ts +51 -0
- package/dist/config-to-opencode.js +124 -0
- package/dist/config.d.ts +33 -0
- package/dist/config.js +325 -0
- package/dist/gate.d.ts +55 -0
- package/dist/gate.js +129 -0
- package/dist/host.d.ts +27 -0
- package/dist/host.js +58 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +129 -0
- package/dist/lint.d.ts +44 -0
- package/dist/lint.js +90 -0
- package/dist/logger.d.ts +12 -0
- package/dist/logger.js +18 -0
- package/dist/protect.d.ts +43 -0
- package/dist/protect.js +121 -0
- package/dist/rules.d.ts +30 -0
- package/dist/rules.js +77 -0
- package/dist/types.d.ts +77 -0
- package/dist/types.js +6 -0
- package/package.json +49 -0
- package/rules/constitution.md +23 -0
package/dist/protect.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Guardrail logic: match tool calls against protected paths and dangerous
|
|
3
|
+
* shell commands. Pure functions — the OpenCode hook wiring lives in
|
|
4
|
+
* src/index.ts.
|
|
5
|
+
*/
|
|
6
|
+
import { basename, isAbsolute, relative } from "node:path";
|
|
7
|
+
import picomatch from "picomatch";
|
|
8
|
+
/** Tools whose args carry a target file path. */
|
|
9
|
+
export const FILE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
10
|
+
/** Tools whose args carry a shell command. */
|
|
11
|
+
export const SHELL_TOOLS = new Set(["bash", "shell"]);
|
|
12
|
+
const PATH_ARG_KEYS = ["filePath", "path", "file"];
|
|
13
|
+
const COMMAND_ARG_KEYS = ["command", "cmd", "script"];
|
|
14
|
+
/** Dangerous shell commands that guardrails flag. */
|
|
15
|
+
export const DANGEROUS_COMMANDS = [
|
|
16
|
+
{ pattern: /\bgit\s+push\b/, reason: "git push is blocked: pushing is a deliberate user action" },
|
|
17
|
+
{
|
|
18
|
+
pattern: /\brm\s+(-\w*r\w*f\w*|-\w*f\w*r\w*)\b/,
|
|
19
|
+
reason: "recursive force delete (rm -rf) is blocked",
|
|
20
|
+
},
|
|
21
|
+
{ pattern: /\bgit\s+reset\s+--hard\b/, reason: "git reset --hard is blocked: it discards work" },
|
|
22
|
+
{
|
|
23
|
+
pattern: /\bgit\s+clean\s+-\w*f\w*/,
|
|
24
|
+
reason: "git clean -f is blocked: it deletes untracked files",
|
|
25
|
+
},
|
|
26
|
+
{ pattern: /\bmkfs\b/, reason: "mkfs is blocked: it formats filesystems" },
|
|
27
|
+
{ pattern: /\bdd\s[^|]*\bof=/, reason: "dd with of= is blocked: it can overwrite devices/files" },
|
|
28
|
+
];
|
|
29
|
+
/** Extract the target file path from tool args, if present. */
|
|
30
|
+
export function extractFilePath(args) {
|
|
31
|
+
if (typeof args !== "object" || args === null) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
for (const key of PATH_ARG_KEYS) {
|
|
35
|
+
const value = args[key];
|
|
36
|
+
if (typeof value === "string" && value !== "") {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
/** Extract the shell command from tool args, if present. */
|
|
43
|
+
export function extractCommand(args) {
|
|
44
|
+
if (typeof args !== "object" || args === null) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
for (const key of COMMAND_ARG_KEYS) {
|
|
48
|
+
const value = args[key];
|
|
49
|
+
if (typeof value === "string" && value !== "") {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check a file path against the protected globs. Returns the matching
|
|
57
|
+
* pattern, or undefined. Patterns without a slash also match basenames at
|
|
58
|
+
* any depth (so `.env*` protects `config/.env.local` too).
|
|
59
|
+
*/
|
|
60
|
+
export function matchProtectedPath(config, filePath, directory) {
|
|
61
|
+
let candidate = filePath;
|
|
62
|
+
if (directory !== undefined && isAbsolute(filePath)) {
|
|
63
|
+
candidate = relative(directory, filePath);
|
|
64
|
+
}
|
|
65
|
+
candidate = candidate.replace(/^\.\//, "");
|
|
66
|
+
const name = basename(candidate);
|
|
67
|
+
for (const pattern of config.protect) {
|
|
68
|
+
const isMatch = picomatch(pattern, { dot: true });
|
|
69
|
+
if (isMatch(candidate)) {
|
|
70
|
+
return pattern;
|
|
71
|
+
}
|
|
72
|
+
if (!pattern.includes("/") && isMatch(name)) {
|
|
73
|
+
return pattern;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
/** Check a shell command against the dangerous-command list. */
|
|
79
|
+
export function matchDangerousCommand(command) {
|
|
80
|
+
return DANGEROUS_COMMANDS.find(({ pattern }) => pattern.test(command));
|
|
81
|
+
}
|
|
82
|
+
function enforce(config, reason, matchedPattern) {
|
|
83
|
+
if (config.protect_mode === "deny") {
|
|
84
|
+
return { decision: "deny", reason, matchedPattern };
|
|
85
|
+
}
|
|
86
|
+
return { decision: "warn", reason, matchedPattern };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Evaluate a tool call against the guardrail config.
|
|
90
|
+
*
|
|
91
|
+
* Returns `allow` when protection is disabled (`off` profile or
|
|
92
|
+
* `protect_off: true`), when the tool is not guarded, or when nothing
|
|
93
|
+
* matched. Otherwise returns `deny` or `warn` per `protect_mode`.
|
|
94
|
+
*/
|
|
95
|
+
export function checkToolCall(config, tool, args, directory) {
|
|
96
|
+
if (config.profile === "off" || config.protect_off) {
|
|
97
|
+
return { decision: "allow" };
|
|
98
|
+
}
|
|
99
|
+
if (FILE_TOOLS.has(tool)) {
|
|
100
|
+
const filePath = extractFilePath(args);
|
|
101
|
+
if (filePath === undefined) {
|
|
102
|
+
return { decision: "allow" };
|
|
103
|
+
}
|
|
104
|
+
const pattern = matchProtectedPath(config, filePath, directory);
|
|
105
|
+
if (pattern !== undefined) {
|
|
106
|
+
return enforce(config, `${tool} on protected path "${filePath}" is blocked (matched "${pattern}")`, pattern);
|
|
107
|
+
}
|
|
108
|
+
return { decision: "allow" };
|
|
109
|
+
}
|
|
110
|
+
if (SHELL_TOOLS.has(tool)) {
|
|
111
|
+
const command = extractCommand(args);
|
|
112
|
+
if (command === undefined) {
|
|
113
|
+
return { decision: "allow" };
|
|
114
|
+
}
|
|
115
|
+
const dangerous = matchDangerousCommand(command);
|
|
116
|
+
if (dangerous !== undefined) {
|
|
117
|
+
return enforce(config, `bash command blocked: ${dangerous.reason}`, dangerous.pattern.source);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { decision: "allow" };
|
|
121
|
+
}
|
package/dist/rules.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ResolvedConfig } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Location of the constitution bundled with the plugin package.
|
|
4
|
+
* Built code lives in `dist/`, so `../rules/constitution.md` resolves to the
|
|
5
|
+
* package root at runtime; during tests (ts source) it resolves the same way
|
|
6
|
+
* relative to `src/`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BUNDLED_CONSTITUTION_PATH: string;
|
|
9
|
+
export interface ConstitutionResult {
|
|
10
|
+
/** Constitution text to inject, or null when nothing should be injected. */
|
|
11
|
+
constitution: string | null;
|
|
12
|
+
source: "custom" | "bundled" | null;
|
|
13
|
+
/** Human-readable warning when a configured constitution could not be read. */
|
|
14
|
+
warning?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Loads the constitution to inject for a session.
|
|
18
|
+
*
|
|
19
|
+
* - `off` profile never injects anything.
|
|
20
|
+
* - A configured `constitution` path wins; if it cannot be read, a warning is
|
|
21
|
+
* returned and the bundled constitution is used as a fallback.
|
|
22
|
+
* - When the bundled constitution itself cannot be read, nothing is injected
|
|
23
|
+
* and a warning is returned.
|
|
24
|
+
*/
|
|
25
|
+
export declare function loadConstitution(config: ResolvedConfig, directory?: string): Promise<ConstitutionResult>;
|
|
26
|
+
/**
|
|
27
|
+
* Appends the constitution to a system-prompt array, unless it is already
|
|
28
|
+
* present (exact entry match or contained in an existing entry).
|
|
29
|
+
*/
|
|
30
|
+
export declare function injectConstitution(system: string[], constitution: string | null): string[];
|
package/dist/rules.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
/**
|
|
5
|
+
* Location of the constitution bundled with the plugin package.
|
|
6
|
+
* Built code lives in `dist/`, so `../rules/constitution.md` resolves to the
|
|
7
|
+
* package root at runtime; during tests (ts source) it resolves the same way
|
|
8
|
+
* relative to `src/`.
|
|
9
|
+
*/
|
|
10
|
+
export const BUNDLED_CONSTITUTION_PATH = join(dirname(fileURLToPath(import.meta.url)), "..", "rules", "constitution.md");
|
|
11
|
+
async function readConstitution(path) {
|
|
12
|
+
try {
|
|
13
|
+
const content = await readFile(path, "utf8");
|
|
14
|
+
return content.trim() === "" ? null : content;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Loads the constitution to inject for a session.
|
|
22
|
+
*
|
|
23
|
+
* - `off` profile never injects anything.
|
|
24
|
+
* - A configured `constitution` path wins; if it cannot be read, a warning is
|
|
25
|
+
* returned and the bundled constitution is used as a fallback.
|
|
26
|
+
* - When the bundled constitution itself cannot be read, nothing is injected
|
|
27
|
+
* and a warning is returned.
|
|
28
|
+
*/
|
|
29
|
+
export async function loadConstitution(config, directory) {
|
|
30
|
+
if (config.profile === "off") {
|
|
31
|
+
return { constitution: null, source: null };
|
|
32
|
+
}
|
|
33
|
+
if (config.constitution) {
|
|
34
|
+
const customPath = isAbsolute(config.constitution)
|
|
35
|
+
? config.constitution
|
|
36
|
+
: resolve(directory ?? process.cwd(), config.constitution);
|
|
37
|
+
const custom = await readConstitution(customPath);
|
|
38
|
+
if (custom !== null) {
|
|
39
|
+
return { constitution: custom, source: "custom" };
|
|
40
|
+
}
|
|
41
|
+
const bundled = await readConstitution(BUNDLED_CONSTITUTION_PATH);
|
|
42
|
+
if (bundled !== null) {
|
|
43
|
+
return {
|
|
44
|
+
constitution: bundled,
|
|
45
|
+
source: "bundled",
|
|
46
|
+
warning: `Could not read configured constitution at ${customPath}; using bundled constitution.`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
constitution: null,
|
|
51
|
+
source: null,
|
|
52
|
+
warning: `Could not read configured constitution at ${customPath} or the bundled constitution; no constitution injected.`,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const bundled = await readConstitution(BUNDLED_CONSTITUTION_PATH);
|
|
56
|
+
if (bundled !== null) {
|
|
57
|
+
return { constitution: bundled, source: "bundled" };
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
constitution: null,
|
|
61
|
+
source: null,
|
|
62
|
+
warning: "Could not read the bundled constitution; no constitution injected.",
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Appends the constitution to a system-prompt array, unless it is already
|
|
67
|
+
* present (exact entry match or contained in an existing entry).
|
|
68
|
+
*/
|
|
69
|
+
export function injectConstitution(system, constitution) {
|
|
70
|
+
if (constitution === null) {
|
|
71
|
+
return system;
|
|
72
|
+
}
|
|
73
|
+
if (system.some((entry) => entry.includes(constitution))) {
|
|
74
|
+
return system;
|
|
75
|
+
}
|
|
76
|
+
return [...system, constitution];
|
|
77
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared types for opencode-dev-framework.
|
|
3
|
+
*
|
|
4
|
+
* The config vocabulary follows docs/plans/03-config-spec.md.
|
|
5
|
+
*/
|
|
6
|
+
export type Profile = "off" | "advisory" | "standard" | "strict";
|
|
7
|
+
export type ProtectMode = "warn" | "deny";
|
|
8
|
+
export type GateScope = "all" | "changed";
|
|
9
|
+
/**
|
|
10
|
+
* A command specification: either a single default command string, or a map
|
|
11
|
+
* of per-extension overrides keyed by extension (e.g. `.go`).
|
|
12
|
+
*/
|
|
13
|
+
export type CommandMap = string | Record<string, string>;
|
|
14
|
+
export interface CommandsConfig {
|
|
15
|
+
test?: string;
|
|
16
|
+
typecheck?: string;
|
|
17
|
+
format?: CommandMap;
|
|
18
|
+
lint?: CommandMap;
|
|
19
|
+
test_changed?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface GateConfig {
|
|
22
|
+
run_typecheck: boolean;
|
|
23
|
+
run_tests: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* If true, gate failures are reported loudly. Note: OpenCode cannot
|
|
26
|
+
* physically block session completion, so this is advisory by nature.
|
|
27
|
+
*/
|
|
28
|
+
block_on_failure: boolean;
|
|
29
|
+
skip_unchanged: boolean;
|
|
30
|
+
scope: GateScope;
|
|
31
|
+
lint_changed: boolean;
|
|
32
|
+
/** Per-command timeout in seconds. */
|
|
33
|
+
timeout?: number;
|
|
34
|
+
/** Reserved for future use; advisory mode currently ignores this. */
|
|
35
|
+
max_blocks: number;
|
|
36
|
+
}
|
|
37
|
+
export interface OnEditConfig {
|
|
38
|
+
format: boolean;
|
|
39
|
+
lint: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Raw config as parsed from a config file. Every field is optional; defaults
|
|
43
|
+
* are applied during resolution (see resolveConfig in config.ts).
|
|
44
|
+
*/
|
|
45
|
+
export interface Config {
|
|
46
|
+
profile?: Profile;
|
|
47
|
+
commands?: CommandsConfig;
|
|
48
|
+
protect?: string[];
|
|
49
|
+
protect_mode?: ProtectMode;
|
|
50
|
+
protect_off?: boolean;
|
|
51
|
+
gate?: Partial<GateConfig>;
|
|
52
|
+
on_edit?: Partial<OnEditConfig>;
|
|
53
|
+
exclude?: string[];
|
|
54
|
+
/** Path to a custom constitution file (absolute or relative to project root). */
|
|
55
|
+
constitution?: string;
|
|
56
|
+
rules?: string[];
|
|
57
|
+
style_guide?: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Fully resolved config with profile defaults applied. All consumers of the
|
|
61
|
+
* config should use this type, never the raw Config.
|
|
62
|
+
*/
|
|
63
|
+
export interface ResolvedConfig {
|
|
64
|
+
profile: Profile;
|
|
65
|
+
/** Absolute path of the config file that was loaded, if any. */
|
|
66
|
+
configPath?: string;
|
|
67
|
+
commands: CommandsConfig;
|
|
68
|
+
protect: string[];
|
|
69
|
+
protect_mode: ProtectMode;
|
|
70
|
+
protect_off: boolean;
|
|
71
|
+
gate: GateConfig;
|
|
72
|
+
on_edit: OnEditConfig;
|
|
73
|
+
exclude: string[];
|
|
74
|
+
constitution?: string;
|
|
75
|
+
rules?: string[];
|
|
76
|
+
style_guide?: string;
|
|
77
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-dev-framework",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "OpenCode plugin that enforces project-specific quality gates during AI coding sessions",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/jwitmann/opencode-dev-framework.git"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"commands",
|
|
16
|
+
"rules"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.json",
|
|
20
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"lint": "biome lint .",
|
|
23
|
+
"lint:fix": "biome lint --write .",
|
|
24
|
+
"format:check": "biome format .",
|
|
25
|
+
"format:fix": "biome format --write .",
|
|
26
|
+
"lint:md": "markdownlint-cli2 \"**/*.md\""
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"opencode",
|
|
30
|
+
"plugin",
|
|
31
|
+
"ai",
|
|
32
|
+
"quality-gates",
|
|
33
|
+
"guardrails"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"picomatch": "^4.0.5",
|
|
37
|
+
"yaml": "^2.9.0",
|
|
38
|
+
"zod": "^4.4.3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@biomejs/biome": "^2.5.8",
|
|
42
|
+
"@opencode-ai/plugin": "^1.18.18",
|
|
43
|
+
"@types/node": "^26.2.0",
|
|
44
|
+
"@types/picomatch": "^4.0.3",
|
|
45
|
+
"markdownlint-cli2": "^0.23.2",
|
|
46
|
+
"typescript": "^7.0.2",
|
|
47
|
+
"vitest": "^4.1.10"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Project Constitution
|
|
2
|
+
|
|
3
|
+
You are working in a project that uses a development framework with quality
|
|
4
|
+
gates. Follow these rules for every change you make:
|
|
5
|
+
|
|
6
|
+
1. **Verify before declaring completion.** Before you tell the user a task is
|
|
7
|
+
done, run the project's configured checks: typecheck, tests, and lint.
|
|
8
|
+
Report their actual results. Do not claim success without evidence.
|
|
9
|
+
|
|
10
|
+
2. **Respect protected paths.** Never edit or delete protected files (such as
|
|
11
|
+
`.env` files, vendored dependencies, or project-specific protected paths)
|
|
12
|
+
unless the user explicitly asks and confirms.
|
|
13
|
+
|
|
14
|
+
3. **Keep changes focused.** Modify only what the task requires. Do not
|
|
15
|
+
refactor unrelated code, reformat untouched files, or add unrequested
|
|
16
|
+
features.
|
|
17
|
+
|
|
18
|
+
4. **Follow project conventions.** Match the existing code style, directory
|
|
19
|
+
layout, and tooling of the project instead of introducing new patterns.
|
|
20
|
+
|
|
21
|
+
5. **Run dangerous commands only with approval.** Do not run destructive or
|
|
22
|
+
hard-to-reverse commands (force pushes, history rewrites, recursive
|
|
23
|
+
deletes, dependency downgrades) without explicit user approval.
|