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
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
/** Directory containing the project templates shipped with the package. */
|
|
6
|
+
export const TEMPLATES_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "templates");
|
|
7
|
+
/** List all template files relative to TEMPLATES_DIR. */
|
|
8
|
+
export async function listTemplateFiles() {
|
|
9
|
+
const files = [];
|
|
10
|
+
async function walk(dir, prefix) {
|
|
11
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
12
|
+
for (const entry of entries) {
|
|
13
|
+
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
14
|
+
if (entry.isDirectory()) {
|
|
15
|
+
await walk(join(dir, entry.name), relative);
|
|
16
|
+
}
|
|
17
|
+
else if (entry.isFile()) {
|
|
18
|
+
files.push(relative);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
await walk(TEMPLATES_DIR, "");
|
|
23
|
+
return files.sort();
|
|
24
|
+
}
|
|
25
|
+
async function fileExists(path) {
|
|
26
|
+
try {
|
|
27
|
+
await fs.access(path);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function readFileSafe(path) {
|
|
35
|
+
try {
|
|
36
|
+
return await fs.readFile(path, "utf8");
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function defaultPrompt(relativePath) {
|
|
43
|
+
if (!process.stdin.isTTY) {
|
|
44
|
+
return "skip";
|
|
45
|
+
}
|
|
46
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
47
|
+
try {
|
|
48
|
+
const answer = await new Promise((resolve) => {
|
|
49
|
+
rl.question(`File exists with different content: ${relativePath}\nOverwrite? [o]verwrite / [s]kip / [a]ll / [n]one: `, resolve);
|
|
50
|
+
});
|
|
51
|
+
const normalized = answer.trim().toLowerCase();
|
|
52
|
+
if (normalized === "o" || normalized === "overwrite") {
|
|
53
|
+
return "overwrite";
|
|
54
|
+
}
|
|
55
|
+
if (normalized === "s" || normalized === "skip") {
|
|
56
|
+
return "skip";
|
|
57
|
+
}
|
|
58
|
+
if (normalized === "a" || normalized === "all") {
|
|
59
|
+
return "overwrite-all";
|
|
60
|
+
}
|
|
61
|
+
if (normalized === "n" || normalized === "none") {
|
|
62
|
+
return "skip-all";
|
|
63
|
+
}
|
|
64
|
+
return "skip";
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
rl.close();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Install all bundled templates into a target project directory.
|
|
72
|
+
* Returns lists of created, skipped, and overwritten files (relative paths).
|
|
73
|
+
*/
|
|
74
|
+
export async function installTemplates(targetDir, options = {}) {
|
|
75
|
+
const result = { created: [], skipped: [], overwritten: [] };
|
|
76
|
+
const files = await listTemplateFiles();
|
|
77
|
+
const prompt = options.prompt ?? defaultPrompt;
|
|
78
|
+
/** Sticky decision set when the user answers "all" or "none". */
|
|
79
|
+
let stickyDecision = null;
|
|
80
|
+
for (const relative of files) {
|
|
81
|
+
const source = join(TEMPLATES_DIR, relative);
|
|
82
|
+
const destination = join(targetDir, relative);
|
|
83
|
+
const sourceContent = await readFileSafe(source);
|
|
84
|
+
if (sourceContent === null) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (!(await fileExists(destination))) {
|
|
88
|
+
await fs.mkdir(dirname(destination), { recursive: true });
|
|
89
|
+
await fs.writeFile(destination, sourceContent, "utf8");
|
|
90
|
+
result.created.push(relative);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const existing = await readFileSafe(destination);
|
|
94
|
+
if (existing === sourceContent) {
|
|
95
|
+
result.skipped.push(relative);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
let decision;
|
|
99
|
+
if (stickyDecision !== null) {
|
|
100
|
+
decision = stickyDecision;
|
|
101
|
+
}
|
|
102
|
+
else if (options.skipExisting) {
|
|
103
|
+
decision = "skip";
|
|
104
|
+
}
|
|
105
|
+
else if (options.overwriteExisting) {
|
|
106
|
+
decision = "overwrite";
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
decision = await prompt(relative);
|
|
110
|
+
}
|
|
111
|
+
if (decision === "overwrite-all") {
|
|
112
|
+
stickyDecision = "overwrite";
|
|
113
|
+
decision = "overwrite";
|
|
114
|
+
}
|
|
115
|
+
else if (decision === "skip-all") {
|
|
116
|
+
stickyDecision = "skip";
|
|
117
|
+
decision = "skip";
|
|
118
|
+
}
|
|
119
|
+
if (decision === "overwrite") {
|
|
120
|
+
await fs.writeFile(destination, sourceContent, "utf8");
|
|
121
|
+
result.overwritten.push(relative);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
result.skipped.push(relative);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Compare the target project against the bundled templates.
|
|
131
|
+
*/
|
|
132
|
+
export async function statusTemplates(targetDir) {
|
|
133
|
+
const files = await listTemplateFiles();
|
|
134
|
+
const status = { missing: [], present: [], different: [] };
|
|
135
|
+
for (const relative of files) {
|
|
136
|
+
const source = join(TEMPLATES_DIR, relative);
|
|
137
|
+
const destination = join(targetDir, relative);
|
|
138
|
+
const sourceContent = await readFileSafe(source);
|
|
139
|
+
const existing = await readFileSafe(destination);
|
|
140
|
+
if (existing === null) {
|
|
141
|
+
status.missing.push(relative);
|
|
142
|
+
}
|
|
143
|
+
else if (existing === sourceContent) {
|
|
144
|
+
status.present.push(relative);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
status.different.push(relative);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return status;
|
|
151
|
+
}
|
|
152
|
+
/** Resolve a user-supplied directory (default cwd) to an absolute path. */
|
|
153
|
+
export function resolveTargetDir(dir) {
|
|
154
|
+
return resolve(dir ?? process.cwd());
|
|
155
|
+
}
|
package/dist/logger.d.ts
CHANGED
|
@@ -6,7 +6,9 @@ export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
|
6
6
|
export declare const LOG_SERVICE = "opencode-dev-framework";
|
|
7
7
|
export type LogFn = (level: LogLevel, message: string, extra?: Record<string, unknown>) => Promise<void>;
|
|
8
8
|
/**
|
|
9
|
-
* Create a logger bound to the OpenCode client. Logging
|
|
10
|
-
*
|
|
9
|
+
* Create a logger bound to the OpenCode client. Logging never throws:
|
|
10
|
+
* `app.log` failures fall back to stderr so the failure stays visible
|
|
11
|
+
* (a total swallow made production debugging painful during development),
|
|
12
|
+
* and even stderr failures are ignored as a last resort.
|
|
11
13
|
*/
|
|
12
14
|
export declare function createLogger(client: PluginInput["client"]): LogFn;
|
package/dist/logger.js
CHANGED
|
@@ -3,16 +3,23 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export const LOG_SERVICE = "opencode-dev-framework";
|
|
5
5
|
/**
|
|
6
|
-
* Create a logger bound to the OpenCode client. Logging
|
|
7
|
-
*
|
|
6
|
+
* Create a logger bound to the OpenCode client. Logging never throws:
|
|
7
|
+
* `app.log` failures fall back to stderr so the failure stays visible
|
|
8
|
+
* (a total swallow made production debugging painful during development),
|
|
9
|
+
* and even stderr failures are ignored as a last resort.
|
|
8
10
|
*/
|
|
9
11
|
export function createLogger(client) {
|
|
10
12
|
return async (level, message, extra) => {
|
|
11
13
|
try {
|
|
12
14
|
await client.app.log({ body: { service: LOG_SERVICE, level, message, extra } });
|
|
13
15
|
}
|
|
14
|
-
catch {
|
|
15
|
-
|
|
16
|
+
catch (error) {
|
|
17
|
+
try {
|
|
18
|
+
process.stderr.write(`[${LOG_SERVICE}] ${level} ${message} (app.log failed: ${String(error)})\n`);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// Even stderr may be unavailable; truly ignore.
|
|
22
|
+
}
|
|
16
23
|
}
|
|
17
24
|
};
|
|
18
25
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ChangedFileTracker } from "./gate.js";
|
|
2
|
+
import type { RunCommand } from "./host.js";
|
|
3
|
+
import type { LogFn } from "./logger.js";
|
|
4
|
+
import type { ResolvedConfig } from "./types.js";
|
|
5
|
+
export interface HookState {
|
|
6
|
+
/** Project directory this state belongs to. Stored here (rather than
|
|
7
|
+
* closure-captured by hooks) so every hook invocation is self-contained. */
|
|
8
|
+
directory: string;
|
|
9
|
+
config: ResolvedConfig;
|
|
10
|
+
log: LogFn;
|
|
11
|
+
run: RunCommand;
|
|
12
|
+
tracker: ChangedFileTracker;
|
|
13
|
+
constitution: string | null;
|
|
14
|
+
/** Per-session count of completion-gate blocks (keyed by sessionID). */
|
|
15
|
+
blockCounts: Map<string, number>;
|
|
16
|
+
}
|
|
17
|
+
export declare function setHookState(directory: string, state: HookState): void;
|
|
18
|
+
export declare function getHookState(directoryHint?: string): HookState | null;
|
|
19
|
+
export declare function updateHookState(directory: string, updates: Partial<HookState>): HookState | null;
|
|
20
|
+
export declare function setSessionDirectory(sessionID: string, directory: string): void;
|
|
21
|
+
export declare function getDirectoryForSession(sessionID: string): string | null;
|
|
22
|
+
/** Forget a session's directory mapping (called on `session.deleted`). */
|
|
23
|
+
export declare function clearSessionDirectory(sessionID: string): void;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-directory hook state registry. OpenCode's effect runtime can strip
|
|
3
|
+
* closure variables when invoking hooks asynchronously, so we store the state
|
|
4
|
+
* in module-level maps and look it up at call time.
|
|
5
|
+
*/
|
|
6
|
+
const hookRegistry = new Map();
|
|
7
|
+
let activeDirectory = null;
|
|
8
|
+
export function setHookState(directory, state) {
|
|
9
|
+
hookRegistry.set(directory, state);
|
|
10
|
+
activeDirectory = directory;
|
|
11
|
+
}
|
|
12
|
+
export function getHookState(directoryHint) {
|
|
13
|
+
const directory = directoryHint ?? activeDirectory;
|
|
14
|
+
if (!directory) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return hookRegistry.get(directory) ?? null;
|
|
18
|
+
}
|
|
19
|
+
export function updateHookState(directory, updates) {
|
|
20
|
+
const state = hookRegistry.get(directory);
|
|
21
|
+
if (!state) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const next = { ...state, ...updates };
|
|
25
|
+
hookRegistry.set(directory, next);
|
|
26
|
+
return next;
|
|
27
|
+
}
|
|
28
|
+
/** Map session IDs to their project directory so hooks that only receive a
|
|
29
|
+
* session ID can still find their state. */
|
|
30
|
+
const sessionToDirectory = new Map();
|
|
31
|
+
export function setSessionDirectory(sessionID, directory) {
|
|
32
|
+
sessionToDirectory.set(sessionID, directory);
|
|
33
|
+
}
|
|
34
|
+
export function getDirectoryForSession(sessionID) {
|
|
35
|
+
return sessionToDirectory.get(sessionID) ?? null;
|
|
36
|
+
}
|
|
37
|
+
/** Forget a session's directory mapping (called on `session.deleted`). */
|
|
38
|
+
export function clearSessionDirectory(sessionID) {
|
|
39
|
+
sessionToDirectory.delete(sessionID);
|
|
40
|
+
}
|
package/dist/rules.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import type { ResolvedConfig } from "./types.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* Built code lives in `dist/`, so `../rules
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* Directory containing the constitution bundled with the plugin package.
|
|
4
|
+
* Built code lives in `dist/`, so `../rules` resolves to the package root at
|
|
5
|
+
* runtime; during tests (ts source) it resolves the same way relative to
|
|
6
|
+
* `src/`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BUNDLED_CONSTITUTION_DIR: string;
|
|
9
|
+
/**
|
|
10
|
+
* Kept for backward compatibility with tests and docs. The bundled
|
|
11
|
+
* constitution is now loaded from all `.md` files in `BUNDLED_CONSTITUTION_DIR`.
|
|
7
12
|
*/
|
|
8
13
|
export declare const BUNDLED_CONSTITUTION_PATH: string;
|
|
9
14
|
export interface ConstitutionResult {
|
|
@@ -19,8 +24,10 @@ export interface ConstitutionResult {
|
|
|
19
24
|
* - `off` profile never injects anything.
|
|
20
25
|
* - A configured `constitution` path wins; if it cannot be read, a warning is
|
|
21
26
|
* returned and the bundled constitution is used as a fallback.
|
|
22
|
-
* - When the bundled constitution itself cannot be read, nothing is
|
|
23
|
-
* and a warning is returned.
|
|
27
|
+
* - When the bundled constitution directory itself cannot be read, nothing is
|
|
28
|
+
* injected and a warning is returned.
|
|
29
|
+
* - The bundled constitution is built from all `.md` files in `rules/`, in
|
|
30
|
+
* sorted order.
|
|
24
31
|
*/
|
|
25
32
|
export declare function loadConstitution(config: ResolvedConfig, directory?: string): Promise<ConstitutionResult>;
|
|
26
33
|
/**
|
package/dist/rules.js
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Built code lives in `dist/`, so `../rules
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Directory containing the constitution bundled with the plugin package.
|
|
6
|
+
* Built code lives in `dist/`, so `../rules` resolves to the package root at
|
|
7
|
+
* runtime; during tests (ts source) it resolves the same way relative to
|
|
8
|
+
* `src/`.
|
|
9
9
|
*/
|
|
10
|
-
export const
|
|
10
|
+
export const BUNDLED_CONSTITUTION_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "rules");
|
|
11
|
+
/**
|
|
12
|
+
* Kept for backward compatibility with tests and docs. The bundled
|
|
13
|
+
* constitution is now loaded from all `.md` files in `BUNDLED_CONSTITUTION_DIR`.
|
|
14
|
+
*/
|
|
15
|
+
export const BUNDLED_CONSTITUTION_PATH = join(BUNDLED_CONSTITUTION_DIR, "constitution.md");
|
|
11
16
|
async function readConstitution(path) {
|
|
12
17
|
try {
|
|
13
18
|
const content = await readFile(path, "utf8");
|
|
@@ -17,14 +22,44 @@ async function readConstitution(path) {
|
|
|
17
22
|
return null;
|
|
18
23
|
}
|
|
19
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Reads all `.md` files in a directory and concatenates them in sorted order,
|
|
27
|
+
* separated by blank lines. Returns null when the directory is missing or
|
|
28
|
+
* contains no non-empty markdown files.
|
|
29
|
+
*/
|
|
30
|
+
async function readConstitutionDir(dir) {
|
|
31
|
+
try {
|
|
32
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
33
|
+
const files = entries
|
|
34
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
|
|
35
|
+
.map((entry) => entry.name)
|
|
36
|
+
.sort();
|
|
37
|
+
if (files.length === 0) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const parts = [];
|
|
41
|
+
for (const name of files) {
|
|
42
|
+
const content = await readFile(join(dir, name), "utf8");
|
|
43
|
+
if (content.trim() !== "") {
|
|
44
|
+
parts.push(content.trim());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return parts.length === 0 ? null : parts.join("\n\n");
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
20
53
|
/**
|
|
21
54
|
* Loads the constitution to inject for a session.
|
|
22
55
|
*
|
|
23
56
|
* - `off` profile never injects anything.
|
|
24
57
|
* - A configured `constitution` path wins; if it cannot be read, a warning is
|
|
25
58
|
* returned and the bundled constitution is used as a fallback.
|
|
26
|
-
* - When the bundled constitution itself cannot be read, nothing is
|
|
27
|
-
* and a warning is returned.
|
|
59
|
+
* - When the bundled constitution directory itself cannot be read, nothing is
|
|
60
|
+
* injected and a warning is returned.
|
|
61
|
+
* - The bundled constitution is built from all `.md` files in `rules/`, in
|
|
62
|
+
* sorted order.
|
|
28
63
|
*/
|
|
29
64
|
export async function loadConstitution(config, directory) {
|
|
30
65
|
if (config.profile === "off") {
|
|
@@ -38,7 +73,7 @@ export async function loadConstitution(config, directory) {
|
|
|
38
73
|
if (custom !== null) {
|
|
39
74
|
return { constitution: custom, source: "custom" };
|
|
40
75
|
}
|
|
41
|
-
const bundled = await
|
|
76
|
+
const bundled = await readConstitutionDir(BUNDLED_CONSTITUTION_DIR);
|
|
42
77
|
if (bundled !== null) {
|
|
43
78
|
return {
|
|
44
79
|
constitution: bundled,
|
|
@@ -52,7 +87,7 @@ export async function loadConstitution(config, directory) {
|
|
|
52
87
|
warning: `Could not read configured constitution at ${customPath} or the bundled constitution; no constitution injected.`,
|
|
53
88
|
};
|
|
54
89
|
}
|
|
55
|
-
const bundled = await
|
|
90
|
+
const bundled = await readConstitutionDir(BUNDLED_CONSTITUTION_DIR);
|
|
56
91
|
if (bundled !== null) {
|
|
57
92
|
return { constitution: bundled, source: "bundled" };
|
|
58
93
|
}
|
package/dist/tools.d.ts
ADDED
package/dist/tools.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { tool } from "@opencode-ai/plugin";
|
|
4
|
+
import { loadConfig, clearConfigCache } from "./config.js";
|
|
5
|
+
import { installTemplates } from "./installer.js";
|
|
6
|
+
import { getHookState, updateHookState } from "./registry.js";
|
|
7
|
+
import { loadConstitution } from "./rules.js";
|
|
8
|
+
const PROFILES = ["off", "advisory", "standard", "strict"];
|
|
9
|
+
function isProfile(value) {
|
|
10
|
+
return PROFILES.includes(value);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Set the top-level `profile:` key in the config file. Edits the raw text
|
|
14
|
+
* instead of re-serializing the YAML so user comments and formatting survive.
|
|
15
|
+
*/
|
|
16
|
+
async function setProfileInFile(configPath, profile) {
|
|
17
|
+
let content = "";
|
|
18
|
+
try {
|
|
19
|
+
content = await readFile(configPath, "utf8");
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// File does not exist yet; it will be created below.
|
|
23
|
+
}
|
|
24
|
+
const line = `profile: ${profile}`;
|
|
25
|
+
if (/^profile\s*:/m.test(content)) {
|
|
26
|
+
content = content.replace(/^profile\s*:.*$/m, line);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
content = content.trim() === "" ? `${line}\n` : `${content.trimEnd()}\n${line}\n`;
|
|
30
|
+
}
|
|
31
|
+
await writeFile(configPath, content, "utf8");
|
|
32
|
+
}
|
|
33
|
+
export function buildTools(_ctx) {
|
|
34
|
+
return {
|
|
35
|
+
dev_framework_init: tool({
|
|
36
|
+
description: "Scaffold opencode-dev-framework project files (agents, skills, commands, default config) into the current project. Missing files are created; existing files are skipped unless overwrite is true.",
|
|
37
|
+
args: {
|
|
38
|
+
directory: tool.schema
|
|
39
|
+
.string()
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("Target project directory (defaults to current project)"),
|
|
42
|
+
overwrite: tool.schema
|
|
43
|
+
.boolean()
|
|
44
|
+
.optional()
|
|
45
|
+
.describe("Overwrite existing files that differ from templates"),
|
|
46
|
+
},
|
|
47
|
+
async execute(args, context) {
|
|
48
|
+
const targetDir = args.directory ?? context.directory;
|
|
49
|
+
const result = await installTemplates(targetDir, {
|
|
50
|
+
overwriteExisting: args.overwrite ?? false,
|
|
51
|
+
skipExisting: !(args.overwrite ?? false),
|
|
52
|
+
});
|
|
53
|
+
const lines = [
|
|
54
|
+
`Installed opencode-dev-framework templates into ${targetDir}.`,
|
|
55
|
+
`Created: ${result.created.length} file(s)`,
|
|
56
|
+
`Overwritten: ${result.overwritten.length} file(s)`,
|
|
57
|
+
`Skipped: ${result.skipped.length} file(s)`,
|
|
58
|
+
];
|
|
59
|
+
if (result.created.length > 0) {
|
|
60
|
+
lines.push("", "Created files:", ...result.created.map((file) => `- ${file}`));
|
|
61
|
+
}
|
|
62
|
+
if (result.overwritten.length > 0) {
|
|
63
|
+
lines.push("", "Overwritten files:", ...result.overwritten.map((file) => `- ${file}`));
|
|
64
|
+
}
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
dev_framework_set_profile: tool({
|
|
69
|
+
description: "Change the opencode-dev-framework profile (off, advisory, standard, strict) for the current project and apply it immediately without restarting OpenCode.",
|
|
70
|
+
args: {
|
|
71
|
+
profile: tool.schema.string().describe("New profile: off, advisory, standard, or strict"),
|
|
72
|
+
directory: tool.schema
|
|
73
|
+
.string()
|
|
74
|
+
.optional()
|
|
75
|
+
.describe("Project directory (defaults to current project)"),
|
|
76
|
+
},
|
|
77
|
+
async execute(args, context) {
|
|
78
|
+
const targetDir = args.directory ?? context.directory;
|
|
79
|
+
const profile = args.profile.trim().toLowerCase();
|
|
80
|
+
if (!isProfile(profile)) {
|
|
81
|
+
return `Invalid profile "${args.profile}". Valid values: ${PROFILES.join(", ")}.`;
|
|
82
|
+
}
|
|
83
|
+
const configPath = join(targetDir, ".opencode-dev-framework.yml");
|
|
84
|
+
await setProfileInFile(configPath, profile);
|
|
85
|
+
// Reload config and constitution so the change takes effect immediately.
|
|
86
|
+
clearConfigCache();
|
|
87
|
+
const config = loadConfig(targetDir);
|
|
88
|
+
const { constitution } = await loadConstitution(config, targetDir);
|
|
89
|
+
const state = getHookState(targetDir);
|
|
90
|
+
if (state) {
|
|
91
|
+
updateHookState(targetDir, { config, constitution });
|
|
92
|
+
}
|
|
93
|
+
return `opencode-dev-framework profile set to "${profile}" in ${configPath}. Change applied immediately.`;
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
97
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-dev-framework",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "OpenCode plugin that enforces project-specific quality gates during AI coding sessions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -12,9 +12,13 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
15
|
-
"
|
|
16
|
-
"rules"
|
|
15
|
+
"bin",
|
|
16
|
+
"rules",
|
|
17
|
+
"templates"
|
|
17
18
|
],
|
|
19
|
+
"bin": {
|
|
20
|
+
"df": "bin/df"
|
|
21
|
+
},
|
|
18
22
|
"scripts": {
|
|
19
23
|
"build": "tsc -p tsconfig.json",
|
|
20
24
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# OpenCode Dev Framework — Activation Gate
|
|
2
|
+
|
|
3
|
+
You are running with the **opencode-dev-framework** plugin installed. This framework
|
|
4
|
+
enforces disciplined software engineering: a quality bar, close style-guide adherence,
|
|
5
|
+
continuous peer review to prevent code fragmentation and drift, and test-grounded
|
|
6
|
+
completion.
|
|
7
|
+
|
|
8
|
+
## When these rules apply
|
|
9
|
+
|
|
10
|
+
The framework is **ACTIVE** for this session only if **either** of the following is true:
|
|
11
|
+
|
|
12
|
+
- The environment variable `DEV_FRAMEWORK` is set to a non-empty value, **or**
|
|
13
|
+
- A file named `.opencode-dev-framework.yml` exists at the root of the working repository.
|
|
14
|
+
|
|
15
|
+
When the framework is active, the plugin injects a `DEV-FRAMEWORK: ACTIVE` banner at
|
|
16
|
+
the start of the session stating the **profile** in effect — `advisory`, `standard`, or
|
|
17
|
+
`strict`:
|
|
18
|
+
|
|
19
|
+
- **advisory** — you receive formatting, lint, and review feedback, but nothing blocks.
|
|
20
|
+
Treat it as strong guidance.
|
|
21
|
+
- **standard** — feedback as above, plus a completion gate that runs type-check and
|
|
22
|
+
tests and blocks finishing while they are red; protected paths cannot be edited.
|
|
23
|
+
- **strict** — as standard, plus lint of changed files must also pass at the gate.
|
|
24
|
+
|
|
25
|
+
Apply the rules with intensity matching the profile, but the disciplines themselves
|
|
26
|
+
(quality bar, matching patterns, grounding in tests) always hold when active.
|
|
27
|
+
|
|
28
|
+
- **If the session is active**, follow every rule in this set as a hard requirement,
|
|
29
|
+
and use the specialist agents and skills described in `40-delegation` as your default
|
|
30
|
+
working loop.
|
|
31
|
+
- **If the session is NOT active** (no banner, no env var, no config file), treat these
|
|
32
|
+
rules as dormant. Do not change your behavior and do not mention the framework.
|
|
33
|
+
Proceed exactly as you normally would.
|
|
34
|
+
|
|
35
|
+
Never disable, bypass, or talk the user out of an active framework gate. If a hook
|
|
36
|
+
blocks completion, fix the underlying problem rather than working around the gate.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Quality Bar
|
|
2
|
+
|
|
3
|
+
When the framework is active, every change you propose must clear this bar before you
|
|
4
|
+
consider a task done. The goal is output a careful senior engineer on this codebase
|
|
5
|
+
would approve without rework.
|
|
6
|
+
|
|
7
|
+
## Non-negotiables
|
|
8
|
+
|
|
9
|
+
1. **Correctness over completion.** A change that compiles but is unverified is not
|
|
10
|
+
done. Prefer a smaller, fully-correct change to a larger speculative one.
|
|
11
|
+
2. **No broken windows.** Do not leave the tree in a worse state than you found it:
|
|
12
|
+
no failing builds, no failing tests you caused, no dead code you introduced, no
|
|
13
|
+
commented-out experiments, no debug prints.
|
|
14
|
+
3. **Scope discipline.** Make precise, surgical changes that fully address the request.
|
|
15
|
+
Do not refactor unrelated code. If you discover an unrelated bug, note it; do not
|
|
16
|
+
silently expand scope.
|
|
17
|
+
4. **Complete, not minimal.** Within the requested scope, deliver the complete correct
|
|
18
|
+
solution — handle the edge cases and error paths, don't stub them.
|
|
19
|
+
5. **Evidence, not assertion.** Do not claim something works, passes, or is fixed
|
|
20
|
+
unless you have run it and observed the result. See `30-testing-discipline`.
|
|
21
|
+
|
|
22
|
+
## Definition of done (active sessions)
|
|
23
|
+
|
|
24
|
+
A task is done only when:
|
|
25
|
+
|
|
26
|
+
- The change builds / type-checks clean (or the project had no such step).
|
|
27
|
+
- Relevant tests pass, and new behavior has a test that fails without your change.
|
|
28
|
+
- Files you touched are formatted and lint-clean per project tooling.
|
|
29
|
+
- The change matches existing patterns in the surrounding code (see
|
|
30
|
+
`20-match-existing-patterns`).
|
|
31
|
+
- A peer-review pass found no blocking issues (see `40-delegation`).
|
|
32
|
+
|
|
33
|
+
If you cannot meet the bar, stop and tell the user what is blocking you rather than
|
|
34
|
+
declaring success.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Match Existing Patterns (Anti-Fragmentation)
|
|
2
|
+
|
|
3
|
+
The single biggest cause of codebase drift is agents writing *plausible* code instead
|
|
4
|
+
of *consistent* code — re-inventing helpers, introducing a second way to do something
|
|
5
|
+
that already has an established way, and diverging from local conventions. When the
|
|
6
|
+
framework is active, you actively fight this.
|
|
7
|
+
|
|
8
|
+
## Look before you write
|
|
9
|
+
|
|
10
|
+
Before introducing **any** new function, type, abstraction, dependency, config key,
|
|
11
|
+
error pattern, logging call, or file, search the codebase for how this is already done:
|
|
12
|
+
|
|
13
|
+
- Is there an existing utility/helper/service that does this? **Reuse it.**
|
|
14
|
+
- Is there an established pattern for this concern (error handling, validation, data
|
|
15
|
+
access, DI, naming, file layout)? **Follow it**, even if you'd personally prefer
|
|
16
|
+
another style.
|
|
17
|
+
- Is there already a dependency that solves this? **Don't add a new one.**
|
|
18
|
+
|
|
19
|
+
Prefer the conventions of the **immediately surrounding code and the nearest sibling
|
|
20
|
+
files** over global or generic "best practices." Consistency with the local module
|
|
21
|
+
beats theoretical purity.
|
|
22
|
+
|
|
23
|
+
## Concrete rules
|
|
24
|
+
|
|
25
|
+
1. **Reuse before create.** Adding a new helper that duplicates existing behavior is a
|
|
26
|
+
defect. If an existing helper is *almost* right, extend it rather than fork it.
|
|
27
|
+
2. **One way to do a thing.** Do not introduce a second pattern for a concern that
|
|
28
|
+
already has one. If the existing pattern is genuinely inadequate, raise it with the
|
|
29
|
+
user instead of quietly diverging.
|
|
30
|
+
3. **Match names and shapes.** Mirror the naming conventions, file/folder structure,
|
|
31
|
+
import ordering, parameter ordering, and return/error conventions already in use.
|
|
32
|
+
4. **No new dependencies without cause.** Adding a library is a scope and drift
|
|
33
|
+
decision — call it out and justify it; never slip one in.
|
|
34
|
+
5. **When in doubt, copy the neighbor.** Find the closest existing example of the thing
|
|
35
|
+
you're building and follow its structure.
|
|
36
|
+
|
|
37
|
+
## How to verify
|
|
38
|
+
|
|
39
|
+
For non-trivial additions, delegate a check to the **`pattern-guardian`** agent (see
|
|
40
|
+
`40-delegation`). It exists to catch duplication, divergence, and "second way to do it"
|
|
41
|
+
drift that is easy to miss while focused on the immediate task.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Testing Discipline (Ground in Reality)
|
|
2
|
+
|
|
3
|
+
When the framework is active, you ground your claims in observed reality, not in
|
|
4
|
+
plausible-sounding assertions. Running code is the source of truth.
|
|
5
|
+
|
|
6
|
+
## Principles
|
|
7
|
+
|
|
8
|
+
1. **Verify, don't assume.** Never say "this works", "tests pass", "this fixes it", or
|
|
9
|
+
"this should be fine" unless you have just run the relevant command and seen it
|
|
10
|
+
succeed. If you have not run it, say so explicitly.
|
|
11
|
+
2. **New behavior needs a test.** Any behavioral change should come with a test that
|
|
12
|
+
**fails before** your change and **passes after**. If you can't write such a test,
|
|
13
|
+
explain why.
|
|
14
|
+
3. **Reproduce before you fix.** For a bug, first reproduce it (a failing test or a
|
|
15
|
+
concrete command), then fix it, then show the same check now passes.
|
|
16
|
+
4. **Run the narrowest useful check often, the full suite before done.** Iterate with
|
|
17
|
+
targeted tests for speed; the completion gate will run the broader suite.
|
|
18
|
+
5. **Read the actual output.** Don't pattern-match on exit codes — read failures and
|
|
19
|
+
address the real cause. Treat a flaky or skipped test as a finding, not a pass.
|
|
20
|
+
|
|
21
|
+
## Use existing tooling only
|
|
22
|
+
|
|
23
|
+
Use the project's existing test/build/lint commands. Do not introduce a new test
|
|
24
|
+
framework, runner, or CI tooling unless the user asks. If the project has no tests at
|
|
25
|
+
all, say so and propose the smallest reasonable way to verify, rather than claiming
|
|
26
|
+
success blind.
|
|
27
|
+
|
|
28
|
+
## The completion gate
|
|
29
|
+
|
|
30
|
+
When the framework is active, a completion gate runs the project's type-check and test
|
|
31
|
+
commands before you finish. If it reports failures:
|
|
32
|
+
|
|
33
|
+
- **Do not** try to disable the gate, weaken the assertion, or mark the test skipped to
|
|
34
|
+
get past it.
|
|
35
|
+
- Read the failure, fix the underlying cause, and let the gate re-run.
|
|
36
|
+
|
|
37
|
+
If you believe a failure is pre-existing and unrelated to your change, prove it (e.g.
|
|
38
|
+
show it fails on a clean tree) and tell the user — don't silently ignore it.
|
|
39
|
+
|
|
40
|
+
For an explicit, thorough verification pass, use the **`ground-in-tests`** skill or the
|
|
41
|
+
**`test-grounder`** agent (see `40-delegation`).
|