opencode-dev-framework 0.5.1 → 1.1.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/README.md +76 -5
- package/dist/command-utils.d.ts +5 -0
- package/dist/command-utils.js +21 -0
- package/dist/commands.d.ts +23 -7
- package/dist/commands.js +24 -7
- package/dist/config.d.ts +13 -0
- package/dist/config.js +75 -0
- package/dist/format-status.js +1 -0
- package/dist/gate.d.ts +13 -5
- package/dist/gate.js +72 -14
- package/dist/goal.d.ts +101 -0
- package/dist/goal.js +177 -0
- package/dist/harness.d.ts +72 -1
- package/dist/harness.js +169 -15
- package/dist/index.d.ts +16 -0
- package/dist/index.js +86 -8
- package/dist/lint.d.ts +15 -1
- package/dist/lint.js +39 -7
- package/dist/protect.d.ts +2 -1
- package/dist/protect.js +6 -6
- package/dist/rules.d.ts +15 -0
- package/dist/rules.js +20 -1
- package/dist/types.d.ts +18 -0
- package/package.json +1 -1
- package/rules/10-quality-bar.md +6 -0
- package/rules/30-testing-discipline.md +6 -1
- package/rules/40-delegation.md +16 -0
package/dist/goal.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/df-goal`: verified-goal wrapper around native `/goal`.
|
|
3
|
+
*
|
|
4
|
+
* A thin adapter, not a second goal engine: it validates that verified-goal
|
|
5
|
+
* mode is meaningful and available (§3.3), appends the framework verification
|
|
6
|
+
* addendum to the user's objective verbatim (§3.2), and delegates exactly
|
|
7
|
+
* once to native `/goal` via `session.command`. Native `/goal` keeps owning
|
|
8
|
+
* kickoff, state, reviewer, pause/resume/clear, and continuations; the
|
|
9
|
+
* framework's idle gate stays the independent deterministic backstop.
|
|
10
|
+
*
|
|
11
|
+
* All policy here is pure (input -> data, no session I/O): `src/index.ts`
|
|
12
|
+
* owns the `ctx.command.transform` registration, the `command.list()`
|
|
13
|
+
* call, and the delegation call; the shape-tolerant listing predicate
|
|
14
|
+
* (`isNativeGoalListed`) lives here. Unit tests drive these functions
|
|
15
|
+
* with a stub delegate.
|
|
16
|
+
*/
|
|
17
|
+
import type { ResolvedConfig } from "./types.js";
|
|
18
|
+
/**
|
|
19
|
+
* Native `/goal` objective limit. Counts JS `text.length` (UTF-16 code
|
|
20
|
+
* units) — confirmed by the Phase 0 spike against core `goal.ts`
|
|
21
|
+
* (`MAX_OBJECTIVE = 4_000`), not bytes or grapheme clusters.
|
|
22
|
+
*/
|
|
23
|
+
export declare const NATIVE_GOAL_LIMIT = 4000;
|
|
24
|
+
/**
|
|
25
|
+
* Verification addendum appended to the objective (plan §3.2). The addendum
|
|
26
|
+
* length in limit accounting is computed from this exact string at
|
|
27
|
+
* delegation time, never from a hardcoded constant.
|
|
28
|
+
*/
|
|
29
|
+
export declare const VERIFICATION_ADDENDUM = "Verification contract added by opencode-dev-framework:\n\nBefore declaring this goal complete, run dev_framework_verify after your final changes. A FAIL result means the goal is incomplete: fix the underlying cause and run verification again. Any later edit invalidates an earlier verification result. PASS is required but does not replace the original objective or its acceptance criteria. Do not disable, skip, or weaken the configured checks to obtain PASS.";
|
|
30
|
+
export type GoalBuildResult = {
|
|
31
|
+
ok: true;
|
|
32
|
+
text: string;
|
|
33
|
+
alreadyWrapped: boolean;
|
|
34
|
+
} | {
|
|
35
|
+
ok: false;
|
|
36
|
+
error: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Append the verification addendum to the objective without rewriting the
|
|
40
|
+
* user's text. An objective that already contains the addendum is used
|
|
41
|
+
* verbatim (never double-wrapped on retry). The complete string counts
|
|
42
|
+
* toward the native limit; overflow is rejected with lengths, never
|
|
43
|
+
* silently truncated.
|
|
44
|
+
*/
|
|
45
|
+
export declare function buildVerifiedObjective(objective: string): GoalBuildResult;
|
|
46
|
+
export type GoalAvailability = {
|
|
47
|
+
ok: true;
|
|
48
|
+
} | {
|
|
49
|
+
ok: false;
|
|
50
|
+
error: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Availability/profile/meaningful-check rules (plan §3.3). `/df-goal` means
|
|
54
|
+
* verified goal, not an alias for `/goal`: every failure names the exact
|
|
55
|
+
* cause and points at the alternatives (enable a blocking profile or use
|
|
56
|
+
* native `/goal` directly for an unverified objective).
|
|
57
|
+
*/
|
|
58
|
+
export declare function checkGoalAvailability(config: ResolvedConfig, nativeGoalAvailable: boolean): GoalAvailability;
|
|
59
|
+
/** Single delegation call to native `/goal` (flat core protocol shape). */
|
|
60
|
+
export type GoalDelegate = (args: {
|
|
61
|
+
sessionID: string;
|
|
62
|
+
name: string;
|
|
63
|
+
text: string;
|
|
64
|
+
}) => Promise<unknown>;
|
|
65
|
+
export interface RunGoalDeps {
|
|
66
|
+
config: ResolvedConfig;
|
|
67
|
+
/** Raw objective text from the command invocation. */
|
|
68
|
+
objective: string;
|
|
69
|
+
sessionID: string;
|
|
70
|
+
nativeGoalAvailable: boolean;
|
|
71
|
+
delegate: GoalDelegate;
|
|
72
|
+
}
|
|
73
|
+
export type RunGoalResult = {
|
|
74
|
+
delegated: true;
|
|
75
|
+
text: string;
|
|
76
|
+
alreadyWrapped: boolean;
|
|
77
|
+
} | {
|
|
78
|
+
delegated: false;
|
|
79
|
+
error: string;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Validate, wrap, and delegate exactly once. Native delegation errors
|
|
83
|
+
* propagate untouched — never an unverified fallback. The wrapper itself
|
|
84
|
+
* admits no kickoff synthetic message.
|
|
85
|
+
*/
|
|
86
|
+
export declare function runGoalCommand(deps: RunGoalDeps): Promise<RunGoalResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Extract the raw objective from a command invocation prompt. The
|
|
89
|
+
* invocation prompt is a `Prompt` struct (`{ text, ... }`); accept a bare
|
|
90
|
+
* string defensively and fall back to empty (rejected downstream as an
|
|
91
|
+
* empty objective) for anything else.
|
|
92
|
+
*/
|
|
93
|
+
export declare function extractObjectiveText(prompt: unknown): string;
|
|
94
|
+
/**
|
|
95
|
+
* Capability probe over `command.list()` output: true when an entry named
|
|
96
|
+
* `goal` is present. The real output shape is `{ location, data: [...] }`
|
|
97
|
+
* (`CommandListOutput`); a bare array is tolerated for forward
|
|
98
|
+
* compatibility. Defensive throughout (SDK codegen drifts; core is truth) —
|
|
99
|
+
* anything unrecognized means unavailable, never an assumed capability.
|
|
100
|
+
*/
|
|
101
|
+
export declare function isNativeGoalListed(listed: unknown): boolean;
|
package/dist/goal.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/df-goal`: verified-goal wrapper around native `/goal`.
|
|
3
|
+
*
|
|
4
|
+
* A thin adapter, not a second goal engine: it validates that verified-goal
|
|
5
|
+
* mode is meaningful and available (§3.3), appends the framework verification
|
|
6
|
+
* addendum to the user's objective verbatim (§3.2), and delegates exactly
|
|
7
|
+
* once to native `/goal` via `session.command`. Native `/goal` keeps owning
|
|
8
|
+
* kickoff, state, reviewer, pause/resume/clear, and continuations; the
|
|
9
|
+
* framework's idle gate stays the independent deterministic backstop.
|
|
10
|
+
*
|
|
11
|
+
* All policy here is pure (input -> data, no session I/O): `src/index.ts`
|
|
12
|
+
* owns the `ctx.command.transform` registration, the `command.list()`
|
|
13
|
+
* call, and the delegation call; the shape-tolerant listing predicate
|
|
14
|
+
* (`isNativeGoalListed`) lives here. Unit tests drive these functions
|
|
15
|
+
* with a stub delegate.
|
|
16
|
+
*/
|
|
17
|
+
import { hasMeaningfulChecks } from "./config.js";
|
|
18
|
+
/**
|
|
19
|
+
* Native `/goal` objective limit. Counts JS `text.length` (UTF-16 code
|
|
20
|
+
* units) — confirmed by the Phase 0 spike against core `goal.ts`
|
|
21
|
+
* (`MAX_OBJECTIVE = 4_000`), not bytes or grapheme clusters.
|
|
22
|
+
*/
|
|
23
|
+
export const NATIVE_GOAL_LIMIT = 4000;
|
|
24
|
+
/**
|
|
25
|
+
* Verification addendum appended to the objective (plan §3.2). The addendum
|
|
26
|
+
* length in limit accounting is computed from this exact string at
|
|
27
|
+
* delegation time, never from a hardcoded constant.
|
|
28
|
+
*/
|
|
29
|
+
export const VERIFICATION_ADDENDUM = `Verification contract added by opencode-dev-framework:
|
|
30
|
+
|
|
31
|
+
Before declaring this goal complete, run dev_framework_verify after your final changes. A FAIL result means the goal is incomplete: fix the underlying cause and run verification again. Any later edit invalidates an earlier verification result. PASS is required but does not replace the original objective or its acceptance criteria. Do not disable, skip, or weaken the configured checks to obtain PASS.`;
|
|
32
|
+
/** Separator between the verbatim objective and the addendum. */
|
|
33
|
+
const OBJECTIVE_SEPARATOR = "\n\n";
|
|
34
|
+
/**
|
|
35
|
+
* Append the verification addendum to the objective without rewriting the
|
|
36
|
+
* user's text. An objective that already contains the addendum is used
|
|
37
|
+
* verbatim (never double-wrapped on retry). The complete string counts
|
|
38
|
+
* toward the native limit; overflow is rejected with lengths, never
|
|
39
|
+
* silently truncated.
|
|
40
|
+
*/
|
|
41
|
+
export function buildVerifiedObjective(objective) {
|
|
42
|
+
if (objective.trim().length === 0) {
|
|
43
|
+
return {
|
|
44
|
+
ok: false,
|
|
45
|
+
error: "Cannot start a verified goal: the objective is empty. " +
|
|
46
|
+
"Provide an objective, e.g. /df-goal <objective>.",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (objective.includes(VERIFICATION_ADDENDUM)) {
|
|
50
|
+
// Already wrapped (e.g. a retry of wrapper output): idempotent, but the
|
|
51
|
+
// combined text still counts toward the native limit. Report the single
|
|
52
|
+
// combined length — the addendum is inside, not additive.
|
|
53
|
+
if (objective.length > NATIVE_GOAL_LIMIT) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
error: `Cannot start a verified goal: objective including the previously appended ` +
|
|
57
|
+
`verification addendum is ${objective.length} characters, exceeding the native ` +
|
|
58
|
+
`/goal limit of ${NATIVE_GOAL_LIMIT} characters. Shorten the objective by at ` +
|
|
59
|
+
`least ${objective.length - NATIVE_GOAL_LIMIT} characters; the addendum is never truncated.`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return { ok: true, text: objective, alreadyWrapped: true };
|
|
63
|
+
}
|
|
64
|
+
const text = `${objective}${OBJECTIVE_SEPARATOR}${VERIFICATION_ADDENDUM}`;
|
|
65
|
+
// Same unit native uses: JS string length (UTF-16 code units).
|
|
66
|
+
if (text.length > NATIVE_GOAL_LIMIT) {
|
|
67
|
+
return {
|
|
68
|
+
ok: false,
|
|
69
|
+
error: `Cannot start a verified goal: objective is ${objective.length} characters, ` +
|
|
70
|
+
`the verification addendum adds ${VERIFICATION_ADDENDUM.length} characters plus a ` +
|
|
71
|
+
`blank-line separator, total ${text.length} exceeds the native /goal limit of ` +
|
|
72
|
+
`${NATIVE_GOAL_LIMIT} characters. ` +
|
|
73
|
+
`Shorten the objective; the addendum is never truncated.`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return { ok: true, text, alreadyWrapped: false };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Availability/profile/meaningful-check rules (plan §3.3). `/df-goal` means
|
|
80
|
+
* verified goal, not an alias for `/goal`: every failure names the exact
|
|
81
|
+
* cause and points at the alternatives (enable a blocking profile or use
|
|
82
|
+
* native `/goal` directly for an unverified objective).
|
|
83
|
+
*/
|
|
84
|
+
export function checkGoalAvailability(config, nativeGoalAvailable) {
|
|
85
|
+
if (!nativeGoalAvailable) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
error: "Cannot start a verified goal: native /goal is not available on this core. " +
|
|
89
|
+
"Upgrade OpenCode to a build containing native /goal, or run the objective " +
|
|
90
|
+
"without verification where native /goal exists.",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (config.profile === "off") {
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
error: 'Cannot start a verified goal: profile is "off", so verification would be vacuous. ' +
|
|
97
|
+
"Enable a blocking profile (standard or strict), or use native /goal directly " +
|
|
98
|
+
"for an unverified objective.",
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (!config.gate?.block_on_failure) {
|
|
102
|
+
const found = config.gate?.block_on_failure;
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
error: `Cannot start a verified goal: gate.block_on_failure is ${String(found)}, ` +
|
|
106
|
+
"but /df-goal requires it to be true. Advisory mode must not gain a blocking " +
|
|
107
|
+
"side channel through /df-goal. Enable a blocking profile (standard or strict), " +
|
|
108
|
+
"or use native /goal directly.",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (!hasMeaningfulChecks(config)) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
error: "Cannot start a verified goal: the resolved configuration provides no meaningful " +
|
|
115
|
+
"completion check (no typecheck/test command, no changed-file lint, no acceptance " +
|
|
116
|
+
"commands, and no review requirement). Configure at least one check, or use native " +
|
|
117
|
+
"/goal directly for a purely semantic objective.",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return { ok: true };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Validate, wrap, and delegate exactly once. Native delegation errors
|
|
124
|
+
* propagate untouched — never an unverified fallback. The wrapper itself
|
|
125
|
+
* admits no kickoff synthetic message.
|
|
126
|
+
*/
|
|
127
|
+
export async function runGoalCommand(deps) {
|
|
128
|
+
const availability = checkGoalAvailability(deps.config, deps.nativeGoalAvailable);
|
|
129
|
+
if (!availability.ok) {
|
|
130
|
+
return { delegated: false, error: availability.error };
|
|
131
|
+
}
|
|
132
|
+
const built = buildVerifiedObjective(deps.objective);
|
|
133
|
+
if (!built.ok) {
|
|
134
|
+
return { delegated: false, error: built.error };
|
|
135
|
+
}
|
|
136
|
+
await deps.delegate({ sessionID: deps.sessionID, name: "goal", text: built.text });
|
|
137
|
+
return { delegated: true, text: built.text, alreadyWrapped: built.alreadyWrapped };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Extract the raw objective from a command invocation prompt. The
|
|
141
|
+
* invocation prompt is a `Prompt` struct (`{ text, ... }`); accept a bare
|
|
142
|
+
* string defensively and fall back to empty (rejected downstream as an
|
|
143
|
+
* empty objective) for anything else.
|
|
144
|
+
*/
|
|
145
|
+
export function extractObjectiveText(prompt) {
|
|
146
|
+
if (typeof prompt === "string")
|
|
147
|
+
return prompt;
|
|
148
|
+
if (prompt !== null && typeof prompt === "object") {
|
|
149
|
+
const text = prompt.text;
|
|
150
|
+
if (typeof text === "string")
|
|
151
|
+
return text;
|
|
152
|
+
}
|
|
153
|
+
return "";
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Capability probe over `command.list()` output: true when an entry named
|
|
157
|
+
* `goal` is present. The real output shape is `{ location, data: [...] }`
|
|
158
|
+
* (`CommandListOutput`); a bare array is tolerated for forward
|
|
159
|
+
* compatibility. Defensive throughout (SDK codegen drifts; core is truth) —
|
|
160
|
+
* anything unrecognized means unavailable, never an assumed capability.
|
|
161
|
+
*/
|
|
162
|
+
export function isNativeGoalListed(listed) {
|
|
163
|
+
const entries = unwrapCommandEntries(listed);
|
|
164
|
+
if (entries === null)
|
|
165
|
+
return false;
|
|
166
|
+
return entries.some((entry) => entry !== null && typeof entry === "object" && entry.name === "goal");
|
|
167
|
+
}
|
|
168
|
+
function unwrapCommandEntries(listed) {
|
|
169
|
+
if (Array.isArray(listed))
|
|
170
|
+
return listed;
|
|
171
|
+
if (listed !== null && typeof listed === "object") {
|
|
172
|
+
const data = listed.data;
|
|
173
|
+
if (Array.isArray(data))
|
|
174
|
+
return data;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
package/dist/harness.d.ts
CHANGED
|
@@ -17,8 +17,9 @@
|
|
|
17
17
|
* synthetic continuation) and the `session.idle` event net (re-prompt).
|
|
18
18
|
*/
|
|
19
19
|
import type { BlockStore } from "./block-store.js";
|
|
20
|
-
import { type ChangedFileTracker } from "./gate.js";
|
|
20
|
+
import { type ChangedFileTracker, type GateReport } from "./gate.js";
|
|
21
21
|
import type { RunCommand } from "./host.js";
|
|
22
|
+
import type { LintLimiter } from "./lint.js";
|
|
22
23
|
import type { LogFn } from "./logger.js";
|
|
23
24
|
import type { ResolvedConfig } from "./types.js";
|
|
24
25
|
/** Project-relative path of the peer-review artifact the agent must produce. */
|
|
@@ -79,7 +80,77 @@ export interface CompletionDeps {
|
|
|
79
80
|
tracker: ChangedFileTracker;
|
|
80
81
|
blockCounts: BlockStore;
|
|
81
82
|
log: LogFn;
|
|
83
|
+
/** Shared limiter bounding concurrent linter subprocesses. */
|
|
84
|
+
lintLimiter?: LintLimiter;
|
|
85
|
+
/** Session pass cache; skips redundant gate suites across idle boundaries. */
|
|
86
|
+
passCache?: PassCache;
|
|
82
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Side-effect-free inputs for a completion inspection, with one deliberate
|
|
90
|
+
* exception: the session pass-cache write-through (`passCache.key/summary`
|
|
91
|
+
* store on clean runs, clear on failures) when the caller opts in by
|
|
92
|
+
* passing `passCache`. The tracker file list is a read-only snapshot:
|
|
93
|
+
* inspection never clears it, never touches block counts, never logs, and
|
|
94
|
+
* never admits input — those are policy decisions owned by
|
|
95
|
+
* `evaluateCompletion`.
|
|
96
|
+
*/
|
|
97
|
+
export interface InspectionDeps {
|
|
98
|
+
run: RunCommand;
|
|
99
|
+
config: ResolvedConfig;
|
|
100
|
+
directory: string;
|
|
101
|
+
/** Read-only snapshot of tracker files; never mutated. */
|
|
102
|
+
trackerFiles: string[];
|
|
103
|
+
/** Shared limiter bounding concurrent linter subprocesses. */
|
|
104
|
+
lintLimiter?: LintLimiter;
|
|
105
|
+
/**
|
|
106
|
+
* Pass cache owned by the session state. When set and the working tree
|
|
107
|
+
* still matches the last clean pass (same files, same mtimes/sizes,
|
|
108
|
+
* same gate-relevant config, review artifact untouched — modulo the
|
|
109
|
+
* mtime/size same-tick rewrite window), inspection returns the cached
|
|
110
|
+
* pass without spawning any gate command. Any content change, config
|
|
111
|
+
* change, or review removal misses the cache and re-runs. Only
|
|
112
|
+
* genuinely clean passes are stored — advisory problems and failures
|
|
113
|
+
* never are, so loud reporting is preserved.
|
|
114
|
+
*/
|
|
115
|
+
passCache?: PassCache;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Fingerprint of the last clean pass. `key` is null when nothing is cached;
|
|
119
|
+
* a non-null key with its summary replays the pass verbatim.
|
|
120
|
+
*/
|
|
121
|
+
export interface PassCache {
|
|
122
|
+
key: string | null;
|
|
123
|
+
summary: string | null;
|
|
124
|
+
}
|
|
125
|
+
/** Create an empty pass cache (one per session state). */
|
|
126
|
+
export declare function createPassCache(): PassCache;
|
|
127
|
+
/**
|
|
128
|
+
* Raw completion report. `ok` is the unadjudicated outcome (gate passed and
|
|
129
|
+
* review satisfied); profiles, block counting, and stand-down are applied by
|
|
130
|
+
* `evaluateCompletion`, and `verifyGate` reports `ok` verbatim as its
|
|
131
|
+
* PASS/FAIL verdict.
|
|
132
|
+
*/
|
|
133
|
+
export interface CompletionInspection {
|
|
134
|
+
changedFiles: string[];
|
|
135
|
+
report: GateReport;
|
|
136
|
+
reviewRequired: boolean;
|
|
137
|
+
reviewState: ArtifactState;
|
|
138
|
+
reviewMissing: boolean;
|
|
139
|
+
/** True when the gate short-circuited on `skip_unchanged` with no files. */
|
|
140
|
+
skipped: boolean;
|
|
141
|
+
/** Raw outcome: gate ok AND review satisfied. No profile/policy applied. */
|
|
142
|
+
ok: boolean;
|
|
143
|
+
/** Human- and transcript-readable summary (gate detail or review notice). */
|
|
144
|
+
summary: string;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Inspect the current completion state without mutating anything except the
|
|
148
|
+
* opted-in session pass-cache write-through: no tracker clearing, no
|
|
149
|
+
* block-count changes, no logging, no input admitted. Both the idle policy
|
|
150
|
+
* (`evaluateCompletion`) and manual verification (`verifyGate`) read from
|
|
151
|
+
* here so they evaluate the same full contract.
|
|
152
|
+
*/
|
|
153
|
+
export declare function inspectCompletion(deps: InspectionDeps): Promise<CompletionInspection>;
|
|
83
154
|
/**
|
|
84
155
|
* Evaluate the completion gate + review requirement and update block state.
|
|
85
156
|
* Pure orchestration: both the `experimental.idle` hook and the
|
package/dist/harness.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* The verdict is consumed by the `experimental.idle` hook (same-execution
|
|
17
17
|
* synthetic continuation) and the `session.idle` event net (re-prompt).
|
|
18
18
|
*/
|
|
19
|
-
import { readFile } from "node:fs/promises";
|
|
19
|
+
import { readFile, stat } from "node:fs/promises";
|
|
20
20
|
import { join } from "node:path";
|
|
21
21
|
import { runGate, summarizeGate } from "./gate.js";
|
|
22
22
|
/** Project-relative path of the peer-review artifact the agent must produce. */
|
|
@@ -122,42 +122,196 @@ function reviewMissingMessage(changedFiles) {
|
|
|
122
122
|
`review findings and their resolution to ${REVIEW_ARTIFACT_REL}.\n\n` +
|
|
123
123
|
`Investigation-only reviewers: do not ask them to edit product code.`);
|
|
124
124
|
}
|
|
125
|
+
/** Create an empty pass cache (one per session state). */
|
|
126
|
+
export function createPassCache() {
|
|
127
|
+
return { key: null, summary: null };
|
|
128
|
+
}
|
|
125
129
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
+
* Fingerprint the current tree state for cache comparison. Returns null
|
|
131
|
+
* when any input is unreadable — an unreadable file can never prove the
|
|
132
|
+
* tree unchanged, so null forces a re-run (the safe direction). Covers
|
|
133
|
+
* every live input to `runGate` (commands, gate options, `exclude`) plus
|
|
134
|
+
* the review artifact, so any behavior-changing config edit misses.
|
|
130
135
|
*/
|
|
131
|
-
|
|
132
|
-
const
|
|
136
|
+
async function passCacheKey(config, directory, changedFiles, reviewRequired) {
|
|
137
|
+
const gateConfig = JSON.stringify({
|
|
138
|
+
commands: config.commands,
|
|
139
|
+
gate: config.gate,
|
|
140
|
+
exclude: config.exclude,
|
|
141
|
+
});
|
|
142
|
+
const parts = [];
|
|
143
|
+
for (const file of [...changedFiles].sort()) {
|
|
144
|
+
try {
|
|
145
|
+
const s = await stat(join(directory, file));
|
|
146
|
+
parts.push(`${file}:${s.mtimeMs}:${s.size}`);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let review = "not-required";
|
|
153
|
+
if (reviewRequired) {
|
|
154
|
+
try {
|
|
155
|
+
const s = await stat(join(directory, REVIEW_ARTIFACT_REL));
|
|
156
|
+
review = `${s.mtimeMs}:${s.size}`;
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
// Review was required at pass time but is now unreadable: the
|
|
160
|
+
// requirement may have become unmet, so force a re-run.
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return `${gateConfig}\n${parts.join("\n")}\nreview:${review}`;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Inspect the current completion state without mutating anything except the
|
|
168
|
+
* opted-in session pass-cache write-through: no tracker clearing, no
|
|
169
|
+
* block-count changes, no logging, no input admitted. Both the idle policy
|
|
170
|
+
* (`evaluateCompletion`) and manual verification (`verifyGate`) read from
|
|
171
|
+
* here so they evaluate the same full contract.
|
|
172
|
+
*/
|
|
173
|
+
export async function inspectCompletion(deps) {
|
|
174
|
+
const { run, config, directory, trackerFiles } = deps;
|
|
175
|
+
const disabled = {
|
|
176
|
+
changedFiles: [],
|
|
177
|
+
report: {
|
|
178
|
+
ran: false,
|
|
179
|
+
reason: "completion gate disabled",
|
|
180
|
+
steps: [],
|
|
181
|
+
ok: true,
|
|
182
|
+
failedSteps: [],
|
|
183
|
+
},
|
|
184
|
+
reviewRequired: false,
|
|
185
|
+
reviewState: "present",
|
|
186
|
+
reviewMissing: false,
|
|
187
|
+
skipped: false,
|
|
188
|
+
ok: true,
|
|
189
|
+
summary: "completion gate disabled (profile off or no gate section)",
|
|
190
|
+
};
|
|
133
191
|
if (config.profile === "off" || !config.gate) {
|
|
134
|
-
return
|
|
192
|
+
return disabled;
|
|
135
193
|
}
|
|
136
|
-
const trackerFiles = tracker.getChangedFiles();
|
|
137
194
|
const gitFiles = await gitChangedFiles(run, directory);
|
|
138
195
|
const changedFiles = mergeChangedFiles(trackerFiles, gitFiles);
|
|
139
196
|
if (config.gate.skip_unchanged && changedFiles.length === 0) {
|
|
140
|
-
return {
|
|
197
|
+
return {
|
|
198
|
+
...disabled,
|
|
199
|
+
changedFiles,
|
|
200
|
+
report: { ran: false, reason: "no changed files", steps: [], ok: true, failedSteps: [] },
|
|
201
|
+
skipped: true,
|
|
202
|
+
summary: "completion gate skipped: no changed files",
|
|
203
|
+
};
|
|
141
204
|
}
|
|
142
|
-
const report = await runGate(run, config, changedFiles, { cwd: directory });
|
|
143
|
-
const gateFailed = report.ran && !report.ok;
|
|
144
205
|
const reviewRequired = config.gate.require_review && changedFiles.length > 0;
|
|
206
|
+
// Fingerprint the tree the gate is about to validate, once. A hit replays
|
|
207
|
+
// the cached pass; a clean run stores this same pre-run key. Storing the
|
|
208
|
+
// pre-run (not post-run) state matters: an edit landing mid-gate-run
|
|
209
|
+
// changes the stats, so the next idle misses and gates the edit instead
|
|
210
|
+
// of replaying a pass over content the gate never saw.
|
|
211
|
+
const fingerprint = deps.passCache !== undefined
|
|
212
|
+
? await passCacheKey(config, directory, changedFiles, reviewRequired)
|
|
213
|
+
: null;
|
|
214
|
+
// Fast path: the tree still matches the last clean pass — replay it
|
|
215
|
+
// without spawning gate commands. The key covers file identity, content
|
|
216
|
+
// freshness (mtime+size), gate-relevant config, and the review artifact,
|
|
217
|
+
// so any real change misses and re-runs.
|
|
218
|
+
const cachedKey = deps.passCache?.key;
|
|
219
|
+
const cachedSummary = deps.passCache?.summary;
|
|
220
|
+
if (cachedKey !== undefined &&
|
|
221
|
+
cachedKey !== null &&
|
|
222
|
+
cachedSummary !== undefined &&
|
|
223
|
+
cachedSummary !== null) {
|
|
224
|
+
if (fingerprint !== null && fingerprint === cachedKey) {
|
|
225
|
+
return {
|
|
226
|
+
changedFiles,
|
|
227
|
+
report: { ran: true, steps: [], ok: true, failedSteps: [] },
|
|
228
|
+
reviewRequired,
|
|
229
|
+
reviewState: "present",
|
|
230
|
+
reviewMissing: false,
|
|
231
|
+
skipped: false,
|
|
232
|
+
ok: true,
|
|
233
|
+
summary: cachedSummary,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const report = await runGate(run, config, changedFiles, {
|
|
238
|
+
cwd: directory,
|
|
239
|
+
lintLimiter: deps.lintLimiter,
|
|
240
|
+
});
|
|
241
|
+
const gateFailed = report.ran && !report.ok;
|
|
145
242
|
const reviewState = reviewRequired
|
|
146
243
|
? await artifactState(directory, REVIEW_ARTIFACT_REL)
|
|
147
244
|
: "present";
|
|
148
245
|
const reviewMissing = reviewRequired && reviewState !== "present";
|
|
246
|
+
const ok = !gateFailed && !reviewMissing;
|
|
247
|
+
const summary = gateFailed
|
|
248
|
+
? summarizeGate(report)
|
|
249
|
+
: reviewMissing
|
|
250
|
+
? reviewMissingMessage(changedFiles)
|
|
251
|
+
: report.ran
|
|
252
|
+
? `completion gate passed\n${summarizeGate(report)}`
|
|
253
|
+
: `completion gate passed (${report.reason ?? "nothing to run"})`;
|
|
254
|
+
if (deps.passCache !== undefined) {
|
|
255
|
+
if (!gateFailed && !reviewMissing && report.ran && fingerprint !== null) {
|
|
256
|
+
// Genuinely clean pass that actually ran — store the pre-run
|
|
257
|
+
// fingerprint for the next idle. Advisory problems (gateFailed) and
|
|
258
|
+
// missing reviews never store; vacuous passes (nothing ran) neither
|
|
259
|
+
// store nor clear.
|
|
260
|
+
deps.passCache.key = fingerprint;
|
|
261
|
+
deps.passCache.summary = summary;
|
|
262
|
+
}
|
|
263
|
+
else if (gateFailed || reviewMissing) {
|
|
264
|
+
deps.passCache.key = null;
|
|
265
|
+
deps.passCache.summary = null;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return {
|
|
269
|
+
changedFiles,
|
|
270
|
+
report,
|
|
271
|
+
reviewRequired,
|
|
272
|
+
reviewState,
|
|
273
|
+
reviewMissing,
|
|
274
|
+
skipped: false,
|
|
275
|
+
ok,
|
|
276
|
+
summary,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Evaluate the completion gate + review requirement and update block state.
|
|
281
|
+
* Pure orchestration: both the `experimental.idle` hook and the
|
|
282
|
+
* `session.idle` event net feed their outcome from here so one session's
|
|
283
|
+
* block count applies to either lever.
|
|
284
|
+
*/
|
|
285
|
+
export async function evaluateCompletion(deps) {
|
|
286
|
+
const { run, config, directory, sessionID, tracker, blockCounts } = deps;
|
|
287
|
+
if (config.profile === "off" || !config.gate) {
|
|
288
|
+
return { decision: "pass", changedFiles: [] };
|
|
289
|
+
}
|
|
290
|
+
const inspection = await inspectCompletion({
|
|
291
|
+
run,
|
|
292
|
+
config,
|
|
293
|
+
directory,
|
|
294
|
+
trackerFiles: tracker.getChangedFiles(),
|
|
295
|
+
lintLimiter: deps.lintLimiter,
|
|
296
|
+
passCache: deps.passCache,
|
|
297
|
+
});
|
|
298
|
+
const { changedFiles, report } = inspection;
|
|
299
|
+
const gateFailed = report.ran && !report.ok;
|
|
300
|
+
const reviewMissing = inspection.reviewMissing;
|
|
149
301
|
if (!gateFailed && !reviewMissing) {
|
|
150
302
|
tracker.clearChangedFiles();
|
|
151
303
|
await blockCounts.clear(sessionID);
|
|
152
304
|
return {
|
|
153
305
|
decision: "pass",
|
|
154
306
|
changedFiles,
|
|
155
|
-
note:
|
|
307
|
+
// Preserve the historical note shape: detailed on run, skip note on
|
|
308
|
+
// skip, absent when the gate never ran for any other reason.
|
|
309
|
+
note: report.ran || inspection.skipped ? inspection.summary : undefined,
|
|
156
310
|
};
|
|
157
311
|
}
|
|
158
312
|
// Advisory: report loudly, never block.
|
|
159
313
|
if (!config.gate.block_on_failure) {
|
|
160
|
-
const summary =
|
|
314
|
+
const summary = inspection.summary;
|
|
161
315
|
await deps.log("error", `completion gate (advisory — not blocking) saw problems:\n${summary}`);
|
|
162
316
|
return {
|
|
163
317
|
decision: "pass",
|
|
@@ -166,7 +320,7 @@ export async function evaluateCompletion(deps) {
|
|
|
166
320
|
};
|
|
167
321
|
}
|
|
168
322
|
const reason = gateFailed ? "gate" : "review";
|
|
169
|
-
const summary =
|
|
323
|
+
const summary = inspection.summary;
|
|
170
324
|
const maxBlocks = config.gate.max_blocks ?? 3;
|
|
171
325
|
const blockCount = await blockCounts.increment(sessionID);
|
|
172
326
|
if (blockCount > maxBlocks) {
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* (`filesystem.changed` stays as a fallback this release; see Phase 24)
|
|
11
11
|
* - ctx.event.subscribe() -> file tracking + gate enforcement via re-prompt
|
|
12
12
|
* - ctx.tool.transform(...) -> custom tools
|
|
13
|
+
* - ctx.command.transform(...) -> server-side `/df-goal` verified-goal
|
|
14
|
+
* wrapper (thin adapter delegating to native `/goal`; see src/goal.ts)
|
|
13
15
|
*
|
|
14
16
|
* Gate enforcement: no synchronous pre-stop veto. On `experimental.idle`
|
|
15
17
|
* (same execution) we run the gate and if it fails inside budget we admit
|
|
@@ -21,7 +23,9 @@
|
|
|
21
23
|
import { Plugin } from "@opencode/plugin";
|
|
22
24
|
import { type BlockStore } from "./block-store.js";
|
|
23
25
|
import { type ChangedFileTracker } from "./gate.js";
|
|
26
|
+
import { type PassCache } from "./harness.js";
|
|
24
27
|
import { type RunCommand } from "./host.js";
|
|
28
|
+
import { type LintLimiter } from "./lint.js";
|
|
25
29
|
import { type LogFn } from "./logger.js";
|
|
26
30
|
import type { ResolvedConfig } from "./types.js";
|
|
27
31
|
export interface HookState {
|
|
@@ -44,6 +48,18 @@ export interface HookState {
|
|
|
44
48
|
blocks: BlockStore;
|
|
45
49
|
precommitAvailable?: boolean;
|
|
46
50
|
configMtime?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Shared FIFO limiter bounding concurrent linter subprocesses across
|
|
53
|
+
* per-edit lints and gate lint steps.
|
|
54
|
+
*/
|
|
55
|
+
lintLimiter: LintLimiter;
|
|
56
|
+
/** Fingerprint of the last clean gate pass; skips redundant suites. */
|
|
57
|
+
passCache: PassCache;
|
|
58
|
+
/**
|
|
59
|
+
* `Date.now()` of the last config-mtime check. Hook fires are far more
|
|
60
|
+
* frequent than config edits, so mtime stats are throttled to a 1s window.
|
|
61
|
+
*/
|
|
62
|
+
lastConfigCheck: number;
|
|
47
63
|
/**
|
|
48
64
|
* Which lever completed a gate evaluation most recently in this process:
|
|
49
65
|
* `"idle"` once the `experimental.idle` hook has (same-execution
|