opencode-dev-framework 0.1.27 → 0.1.28

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.
Files changed (2) hide show
  1. package/dist/index.js +43 -19
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -21,6 +21,18 @@ import { checkToolCall } from "./protect.js";
21
21
  import { clearSessionDirectory, getHookState, getStateForSession, setBaseDirectory, setHookState, setSessionDirectory, } from "./registry.js";
22
22
  import { injectConstitution, loadConstitution } from "./rules.js";
23
23
  import { buildTools } from "./tools.js";
24
+ /**
25
+ * Module-level fallback logger, captured at plugin init. `safeLog` uses it when
26
+ * a hook's `HookState.log` is missing or was stripped, so a logging failure can
27
+ * never crash a hook. The original `state.log is not a function` error (seen when
28
+ * switching `off` -> `standard` at runtime) happened because a hook fired against
29
+ * a `HookState` whose `log` was never populated.
30
+ */
31
+ let fallbackLog = async () => { };
32
+ function safeLog(state, level, message, extra) {
33
+ const fn = typeof state.log === "function" ? state.log : fallbackLog;
34
+ return fn(level, message, extra);
35
+ }
24
36
  /**
25
37
  * Reload the in-memory config if the config file on disk changed since we last
26
38
  * loaded it. This makes out-of-process edits (e.g. a `/df-profile` change made
@@ -104,9 +116,12 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
104
116
  if (input.sessionID) {
105
117
  setSessionDirectory(input.sessionID, state.directory);
106
118
  }
119
+ if (state.config.profile === "off") {
120
+ return;
121
+ }
107
122
  const next = injectConstitution(output.system, state.constitution);
108
123
  if (next !== output.system) {
109
- await state.log("info", "constitution injected into system prompt");
124
+ await safeLog(state, "info", "constitution injected into system prompt");
110
125
  output.system = next;
111
126
  }
112
127
  },
@@ -116,6 +131,9 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
116
131
  throw new Error("[opencode-dev-framework] plugin state is not available; guardrail cannot evaluate this tool call");
117
132
  }
118
133
  await reloadConfigIfChanged(state);
134
+ if (state.config.profile === "off") {
135
+ return;
136
+ }
119
137
  const result = checkToolCall(state.config, input.tool, output.args, state.directory, state.hostPermissions);
120
138
  if (result.decision === "allow") {
121
139
  return;
@@ -127,10 +145,10 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
127
145
  pattern: result.matchedPattern,
128
146
  };
129
147
  if (result.decision === "warn") {
130
- await state.log("warn", message, extra);
148
+ await safeLog(state, "warn", message, extra);
131
149
  return;
132
150
  }
133
- await state.log("error", message, extra);
151
+ await safeLog(state, "error", message, extra);
134
152
  throw new Error(`[opencode-dev-framework] ${message}`);
135
153
  },
136
154
  "experimental.session.stopping": async (input, output) => {
@@ -156,11 +174,11 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
156
174
  // Standing down: clear the tracker so the session.idle fallback does
157
175
  // not re-run the same failing gate commands a second time.
158
176
  state.tracker.clearChangedFiles();
159
- await state.log("warn", `completion gate has blocked ${maxBlocks} times; standing down but checks are still failing`, { failedSteps: report.failedSteps.map((step) => step.name) });
177
+ await safeLog(state, "warn", `completion gate has blocked ${maxBlocks} times; standing down but checks are still failing`, { failedSteps: report.failedSteps.map((step) => step.name) });
160
178
  return;
161
179
  }
162
180
  const summary = summarizeGate(report);
163
- await state.log("error", summary, {
181
+ await safeLog(state, "error", summary, {
164
182
  failedSteps: report.failedSteps.map((step) => step.name),
165
183
  });
166
184
  output.context.push(`opencode-dev-framework completion gate blocked you from finishing (block ${blockCount}/${maxBlocks}). ` +
@@ -182,6 +200,9 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
182
200
  return;
183
201
  }
184
202
  await reloadConfigIfChanged(state);
203
+ if (state.config.profile === "off") {
204
+ return;
205
+ }
185
206
  if (event.type === "file.edited") {
186
207
  const filePath = event.properties.file;
187
208
  state.tracker.add(filePath, state.directory);
@@ -197,15 +218,18 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
197
218
  precommitAvailable: state.precommitAvailable,
198
219
  });
199
220
  if (outcome.skipped) {
200
- await state.log("debug", summarizeLint(outcome), { filePath, reason: outcome.reason });
221
+ await safeLog(state, "debug", summarizeLint(outcome), {
222
+ filePath,
223
+ reason: outcome.reason,
224
+ });
201
225
  return;
202
226
  }
203
227
  if (!isLintFailure(outcome)) {
204
- await state.log("info", summarizeLint(outcome), { filePath });
228
+ await safeLog(state, "info", summarizeLint(outcome), { filePath });
205
229
  return;
206
230
  }
207
231
  const summary = summarizeLint(outcome);
208
- await state.log("error", summary, {
232
+ await safeLog(state, "error", summary, {
209
233
  filePath,
210
234
  command: outcome.command?.join(" "),
211
235
  stdout: outcome.result?.stdout,
@@ -220,11 +244,8 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
220
244
  return;
221
245
  }
222
246
  if (event.type === "session.idle") {
223
- if (state.config.profile === "off") {
224
- return;
225
- }
226
247
  if (!state.config.gate) {
227
- await state.log("warn", "plugin config is incomplete (missing gate section), skipping completion gate", { directory: state.directory });
248
+ await safeLog(state, "warn", "plugin config is incomplete (missing gate section), skipping completion gate", { directory: state.directory });
228
249
  return;
229
250
  }
230
251
  const changedFiles = state.tracker.getChangedFiles();
@@ -234,17 +255,17 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
234
255
  state.tracker.clearChangedFiles();
235
256
  const summary = summarizeGate(report);
236
257
  if (!report.ran) {
237
- await state.log("debug", summary);
258
+ await safeLog(state, "debug", summary);
238
259
  return;
239
260
  }
240
261
  if (report.ok) {
241
- await state.log("info", summary);
262
+ await safeLog(state, "info", summary);
242
263
  return;
243
264
  }
244
265
  // The gate cannot physically block on session.idle (the turn already
245
266
  // ended), so failure visibility is the enforcement mechanism.
246
267
  const level = state.config.gate.block_on_failure ? "error" : "warn";
247
- await state.log(level, summary, {
268
+ await safeLog(state, level, summary, {
248
269
  failedSteps: report.failedSteps.map((step) => ({
249
270
  name: step.name,
250
271
  command: step.command?.join(" "),
@@ -259,6 +280,7 @@ export function buildHooks(ctx, config, log, run = runCommand, tracker = createC
259
280
  }
260
281
  export const devFramework = async (ctx) => {
261
282
  const log = createLogger(ctx.client);
283
+ fallbackLog = log;
262
284
  let config;
263
285
  try {
264
286
  config = loadConfig(ctx.directory);
@@ -278,10 +300,12 @@ export const devFramework = async (ctx) => {
278
300
  }
279
301
  return {};
280
302
  }
281
- // The off profile registers no hooks at all.
282
- if (config.profile === "off") {
283
- return {};
284
- }
303
+ // Hooks are now registered for every profile; each enforcement hook gates its
304
+ // own behavior by `state.config.profile` (off => no-op). Registering always
305
+ // guarantees HookState.log is populated at init, so a runtime `off` -> `standard`
306
+ // switch (via /df-profile) takes effect on the next hook call via
307
+ // reloadConfigIfChanged — no restart required — and a hook can never crash on a
308
+ // missing `log` (see safeLog).
285
309
  setBaseDirectory(ctx.directory);
286
310
  const { constitution, warning } = await loadConstitution(config, ctx.directory);
287
311
  if (warning) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-dev-framework",
3
- "version": "0.1.27",
3
+ "version": "0.1.28",
4
4
  "description": "OpenCode plugin that enforces project-specific quality gates during AI coding sessions",
5
5
  "license": "MIT",
6
6
  "type": "module",