opencode-dev-framework 0.2.3 → 0.2.4
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/dist/index.js +95 -72
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -214,40 +214,50 @@ const plugin = Plugin.define({
|
|
|
214
214
|
registrations.push(value);
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
|
+
// Stale-hook guard: every closure below captures its own `state` and
|
|
218
|
+
// no-ops when it is no longer the live singleton. This covers three
|
|
219
|
+
// cases disposal alone cannot: (a) teardown with no fresh setup
|
|
220
|
+
// (activeState is null), (b) teardown + reload where an old
|
|
221
|
+
// registration outlives disposal (activeState is a newer state —
|
|
222
|
+
// the stale hook must NOT double-enforce), and (c) teardown racing
|
|
223
|
+
// an in-flight handler between awaits (captured `state` stays valid
|
|
224
|
+
// while the global may be cleared). See issue #1 follow-up.
|
|
217
225
|
// 1) Constitution injection — per model request (V1: experimental.chat.system.transform -> V2: session.hook("context", ...))
|
|
218
226
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
219
227
|
trackRegistration(await ctx.session.hook("context", async (event) => {
|
|
220
|
-
if (
|
|
228
|
+
if (activeState !== state)
|
|
221
229
|
return;
|
|
222
|
-
await reloadConfigIfChanged(
|
|
223
|
-
if (
|
|
230
|
+
await reloadConfigIfChanged(state);
|
|
231
|
+
if (state.config.profile === "off")
|
|
224
232
|
return;
|
|
225
|
-
if (!
|
|
233
|
+
if (!state.constitution)
|
|
226
234
|
return;
|
|
227
235
|
// V2 system is Array<SystemPart> {type:"text", text:string}
|
|
228
|
-
const already = event.system.some((part) => part.text.includes(
|
|
236
|
+
const already = event.system.some((part) => part.text.includes(state.constitution));
|
|
229
237
|
if (already)
|
|
230
238
|
return;
|
|
231
239
|
event.system.push({
|
|
232
240
|
type: "text",
|
|
233
|
-
text:
|
|
241
|
+
text: state.constitution,
|
|
234
242
|
});
|
|
235
|
-
await safeLog(
|
|
243
|
+
await safeLog(state, "info", "constitution injected into system prompt");
|
|
236
244
|
}));
|
|
237
245
|
// Also hook other request kinds so the reminder is not missed on title/generate
|
|
238
246
|
// (no-op if model doesn't need it; cheap)
|
|
239
247
|
for (const kind of ["generate", "compaction"]) {
|
|
240
248
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
241
249
|
trackRegistration(await ctx.session.hook(kind, async (event) => {
|
|
242
|
-
if (
|
|
250
|
+
if (activeState !== state)
|
|
251
|
+
return;
|
|
252
|
+
if (state.config.profile === "off" || !state.constitution)
|
|
243
253
|
return;
|
|
244
254
|
// generate/compaction also have system — inject similarly if present
|
|
245
255
|
const ev = event;
|
|
246
256
|
if (!ev.system)
|
|
247
257
|
return;
|
|
248
|
-
const already = ev.system.some((p) => p.text.includes(
|
|
258
|
+
const already = ev.system.some((p) => p.text.includes(state.constitution));
|
|
249
259
|
if (!already)
|
|
250
|
-
ev.system.push({ type: "text", text:
|
|
260
|
+
ev.system.push({ type: "text", text: state.constitution });
|
|
251
261
|
}));
|
|
252
262
|
}
|
|
253
263
|
// 1b) Hard-stop completion gate — `session.stopping` (OpenCode PR #44712).
|
|
@@ -264,19 +274,19 @@ const plugin = Plugin.define({
|
|
|
264
274
|
await ctx.session.hook("stopping",
|
|
265
275
|
// biome-ignore lint/suspicious/noExplicitAny: core may dispatch (input, output) or a single mutable event
|
|
266
276
|
async (input, output) => {
|
|
267
|
-
if (
|
|
277
|
+
if (activeState !== state)
|
|
268
278
|
return;
|
|
269
279
|
const out = output ?? input ?? {};
|
|
270
280
|
const sessionID = input?.sessionID ??
|
|
271
281
|
out.sessionID ??
|
|
272
282
|
getEventSessionID(input) ??
|
|
273
283
|
"unknown";
|
|
274
|
-
|
|
275
|
-
await reloadConfigIfChanged(
|
|
276
|
-
if (
|
|
284
|
+
state.stopHookSupported = true;
|
|
285
|
+
await reloadConfigIfChanged(state);
|
|
286
|
+
if (state.config.profile === "off")
|
|
277
287
|
return;
|
|
278
|
-
const verdict = await runCompletion(
|
|
279
|
-
const message = await actOnVerdict(
|
|
288
|
+
const verdict = await runCompletion(state, sessionID);
|
|
289
|
+
const message = await actOnVerdict(state, verdict);
|
|
280
290
|
if (message !== null) {
|
|
281
291
|
out.stop = false;
|
|
282
292
|
out.message = message;
|
|
@@ -288,17 +298,22 @@ const plugin = Plugin.define({
|
|
|
288
298
|
await safeLog(state, "warn", "could not register session.stopping hook; using session.idle re-prompt fallback only");
|
|
289
299
|
}
|
|
290
300
|
// 2) Guardrails — tool execution blocking (V1: tool.execute.before -> V2: ctx.tool.hook("execute.before", ...))
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
//
|
|
301
|
+
// Stale-hook fail-open: the closure captures its own `state` and allows
|
|
302
|
+
// the call whenever it is no longer live — torn down (activeState null)
|
|
303
|
+
// or superseded by a newer setup (activeState is a different instance,
|
|
304
|
+
// whose own hook enforces; this one must not double-enforce). Using the
|
|
305
|
+
// captured `state` throughout also closes the teardown race where the
|
|
306
|
+
// global is cleared between awaits. See issue #1.
|
|
294
307
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
295
308
|
trackRegistration(await ctx.tool.hook("execute.before", async (event) => {
|
|
296
|
-
if (
|
|
297
|
-
|
|
309
|
+
if (activeState !== state) {
|
|
310
|
+
if (activeState == null) {
|
|
311
|
+
await safeLog(null, "warn", "[opencode-dev-framework] plugin state is not available; allowing tool call (fail-open after teardown)", { tool: event.tool });
|
|
312
|
+
}
|
|
298
313
|
return;
|
|
299
314
|
}
|
|
300
|
-
await reloadConfigIfChanged(
|
|
301
|
-
if (
|
|
315
|
+
await reloadConfigIfChanged(state);
|
|
316
|
+
if (state.config.profile === "off")
|
|
302
317
|
return;
|
|
303
318
|
// V2 event shapes: { tool, sessionID, agent, messageID, id, input: unknown }
|
|
304
319
|
const toolName = event.tool;
|
|
@@ -307,31 +322,31 @@ const plugin = Plugin.define({
|
|
|
307
322
|
// Pre-flight harness: when `preflight:` tasks are configured, file
|
|
308
323
|
// edits are denied (standard/strict) until the pre-flight artifact
|
|
309
324
|
// exists. This is a true hard gate — it runs before the edit lands.
|
|
310
|
-
if (
|
|
311
|
-
const artifact = await artifactState(
|
|
312
|
-
const reason = preflightBlockReason(
|
|
325
|
+
if (state.config.preflight.length > 0) {
|
|
326
|
+
const artifact = await artifactState(state.directory, PREFLIGHT_ARTIFACT_REL);
|
|
327
|
+
const reason = preflightBlockReason(state.config, toolName, extractFilePath(input), artifact);
|
|
313
328
|
if (reason !== null) {
|
|
314
329
|
const extra = { tool: toolName, sessionID, artifact: PREFLIGHT_ARTIFACT_REL };
|
|
315
|
-
if (
|
|
316
|
-
await safeLog(
|
|
330
|
+
if (state.config.profile === "advisory") {
|
|
331
|
+
await safeLog(state, "warn", reason, extra);
|
|
317
332
|
}
|
|
318
333
|
else {
|
|
319
|
-
await safeLog(
|
|
334
|
+
await safeLog(state, "error", reason, extra);
|
|
320
335
|
throw new Error(`[opencode-dev-framework] ${reason}`);
|
|
321
336
|
}
|
|
322
337
|
}
|
|
323
338
|
}
|
|
324
339
|
// hostPermissions: not available in V2 permission model; pass undefined (conservative — still guard)
|
|
325
|
-
const result = checkToolCall(
|
|
340
|
+
const result = checkToolCall(state.config, toolName, input, state.directory, undefined);
|
|
326
341
|
if (result.decision === "allow")
|
|
327
342
|
return;
|
|
328
343
|
const message = result.reason ?? "blocked by guardrails";
|
|
329
344
|
const extra = { tool: toolName, sessionID, pattern: result.matchedPattern };
|
|
330
345
|
if (result.decision === "warn") {
|
|
331
|
-
await safeLog(
|
|
346
|
+
await safeLog(state, "warn", message, extra);
|
|
332
347
|
return;
|
|
333
348
|
}
|
|
334
|
-
await safeLog(
|
|
349
|
+
await safeLog(state, "error", message, extra);
|
|
335
350
|
throw new Error(`[opencode-dev-framework] ${message}`);
|
|
336
351
|
}));
|
|
337
352
|
// 3) Custom tools — V2: ctx.tool.transform(editor => editor.add(...))
|
|
@@ -415,7 +430,11 @@ const plugin = Plugin.define({
|
|
|
415
430
|
ccc();
|
|
416
431
|
const newConfig = lc(targetDir);
|
|
417
432
|
const { constitution: newConstitution } = await lc2(newConfig, targetDir);
|
|
418
|
-
if (
|
|
433
|
+
if (state.directory === targetDir) {
|
|
434
|
+
state.config = newConfig;
|
|
435
|
+
state.constitution = newConstitution;
|
|
436
|
+
}
|
|
437
|
+
if (activeState && activeState !== state && activeState.directory === targetDir) {
|
|
419
438
|
activeState.config = newConfig;
|
|
420
439
|
activeState.constitution = newConstitution;
|
|
421
440
|
}
|
|
@@ -439,12 +458,12 @@ const plugin = Plugin.define({
|
|
|
439
458
|
const { renderStatus } = await import("./format-status.js");
|
|
440
459
|
const args = input;
|
|
441
460
|
const targetDir = args.directory ?? directory;
|
|
442
|
-
const
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
// renderStatus expects HookState? pass
|
|
461
|
+
const live = activeState && activeState.directory === targetDir ? activeState : null;
|
|
462
|
+
const own = state.directory === targetDir ? state : null;
|
|
463
|
+
const cfg = (live ?? own)?.config ?? loadConfig(targetDir);
|
|
464
|
+
// renderStatus expects HookState? pass live ?? own (may be null for other dirs)
|
|
446
465
|
return {
|
|
447
|
-
content: renderStatus(cfg,
|
|
466
|
+
content: renderStatus(cfg, (live ?? own)),
|
|
448
467
|
};
|
|
449
468
|
},
|
|
450
469
|
});
|
|
@@ -456,78 +475,81 @@ const plugin = Plugin.define({
|
|
|
456
475
|
const eventLoop = (async () => {
|
|
457
476
|
try {
|
|
458
477
|
for await (const rawEvent of ctx.event.subscribe({ signal: controller.signal })) {
|
|
478
|
+
// Stale-loop guard: a previous setup's loop must exit rather than
|
|
479
|
+
// hijack a newer setup's state (duplicate gate re-prompts / lint).
|
|
480
|
+
if (activeState !== state)
|
|
481
|
+
return;
|
|
459
482
|
const event = rawEvent;
|
|
460
483
|
const type = event.type;
|
|
461
484
|
if (isSessionDeletedEvent(type)) {
|
|
462
485
|
// session.deleted: cleanup blockCounts (and tracker if needed)
|
|
463
486
|
const sid = getEventSessionID(event);
|
|
464
|
-
if (sid
|
|
465
|
-
|
|
487
|
+
if (sid)
|
|
488
|
+
state.blockCounts.delete(sid);
|
|
466
489
|
continue;
|
|
467
490
|
}
|
|
468
491
|
if (isFilesystemChangedEvent(type)) {
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
await reloadConfigIfChanged(
|
|
472
|
-
if (
|
|
492
|
+
if (activeState !== state)
|
|
493
|
+
return;
|
|
494
|
+
await reloadConfigIfChanged(state);
|
|
495
|
+
if (state.config.profile === "off")
|
|
473
496
|
continue;
|
|
474
497
|
const filePath = getEventFilePath(event);
|
|
475
498
|
if (!filePath)
|
|
476
499
|
continue;
|
|
477
|
-
|
|
500
|
+
state.tracker.add(filePath, state.directory);
|
|
478
501
|
// per-edit lint (V1: event file.edited -> lintFile)
|
|
479
|
-
if (!
|
|
502
|
+
if (!state.config.on_edit.lint)
|
|
480
503
|
continue;
|
|
481
|
-
if (
|
|
482
|
-
|
|
483
|
-
activeState.precommitAvailable = await detectPreCommitAvailability(activeState.run, activeState.directory);
|
|
504
|
+
if (state.config.precommit === "auto" && state.precommitAvailable === undefined) {
|
|
505
|
+
state.precommitAvailable = await detectPreCommitAvailability(state.run, state.directory);
|
|
484
506
|
}
|
|
485
|
-
const outcome = await lintFile(
|
|
486
|
-
cwd:
|
|
487
|
-
timeout:
|
|
488
|
-
precommitAvailable:
|
|
507
|
+
const outcome = await lintFile(state.run, state.config, filePath, {
|
|
508
|
+
cwd: state.directory,
|
|
509
|
+
timeout: state.config.gate?.timeout,
|
|
510
|
+
precommitAvailable: state.precommitAvailable,
|
|
489
511
|
});
|
|
490
512
|
if (outcome.skipped) {
|
|
491
|
-
await safeLog(
|
|
513
|
+
await safeLog(state, "debug", summarizeLint(outcome), {
|
|
492
514
|
filePath,
|
|
493
515
|
reason: outcome.reason,
|
|
494
516
|
});
|
|
495
517
|
continue;
|
|
496
518
|
}
|
|
497
519
|
if (!isLintFailure(outcome)) {
|
|
498
|
-
await safeLog(
|
|
520
|
+
await safeLog(state, "info", summarizeLint(outcome), { filePath });
|
|
499
521
|
continue;
|
|
500
522
|
}
|
|
501
523
|
const summary = summarizeLint(outcome);
|
|
502
|
-
await safeLog(
|
|
524
|
+
await safeLog(state, "error", summary, {
|
|
503
525
|
filePath,
|
|
504
526
|
command: outcome.command?.join(" "),
|
|
505
527
|
stdout: outcome.result?.stdout,
|
|
506
528
|
stderr: outcome.result?.stderr,
|
|
507
529
|
});
|
|
508
|
-
if (
|
|
530
|
+
if (state.config.profile === "strict") {
|
|
509
531
|
// Event handlers cannot throw to block the edit (edit already happened), but we log loudly.
|
|
510
532
|
// In V2 we also have the tool hook to block before; this is advisory.
|
|
511
533
|
}
|
|
512
534
|
continue;
|
|
513
535
|
}
|
|
514
536
|
if (isIdleEvent(event)) {
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
await reloadConfigIfChanged(
|
|
518
|
-
if (
|
|
537
|
+
if (activeState !== state)
|
|
538
|
+
return;
|
|
539
|
+
await reloadConfigIfChanged(state);
|
|
540
|
+
if (state.config.profile === "off")
|
|
519
541
|
continue;
|
|
520
|
-
if (!
|
|
521
|
-
await safeLog(
|
|
522
|
-
directory:
|
|
542
|
+
if (!state.config.gate) {
|
|
543
|
+
await safeLog(state, "warn", "plugin config is incomplete (missing gate section), skipping completion gate", {
|
|
544
|
+
directory: state.directory,
|
|
523
545
|
});
|
|
524
546
|
continue;
|
|
525
547
|
}
|
|
526
548
|
const sessionID = getEventSessionID(event);
|
|
527
549
|
if (!sessionID)
|
|
528
550
|
continue;
|
|
529
|
-
const verdict = await runCompletion(
|
|
530
|
-
const message = await actOnVerdict(
|
|
551
|
+
const verdict = await runCompletion(state, sessionID);
|
|
552
|
+
const message = await actOnVerdict(state, verdict);
|
|
531
553
|
if (message === null)
|
|
532
554
|
continue;
|
|
533
555
|
const blockInfo = verdict.decision === "blocked"
|
|
@@ -544,12 +566,12 @@ const plugin = Plugin.define({
|
|
|
544
566
|
await ctx.session.synthetic({ sessionID, text: message });
|
|
545
567
|
}
|
|
546
568
|
else {
|
|
547
|
-
await safeLog(
|
|
569
|
+
await safeLog(state, "error", `gate blocked but no session prompt API available: ${message}`);
|
|
548
570
|
}
|
|
549
|
-
await safeLog(
|
|
571
|
+
await safeLog(state, "info", `completion gate re-prompted session ${sessionID}${blockInfo}`);
|
|
550
572
|
}
|
|
551
573
|
catch (err) {
|
|
552
|
-
await safeLog(
|
|
574
|
+
await safeLog(state, "error", `failed to re-prompt session ${sessionID} after gate block: ${String(err)}`, {
|
|
553
575
|
sessionID,
|
|
554
576
|
});
|
|
555
577
|
}
|
|
@@ -561,7 +583,7 @@ const plugin = Plugin.define({
|
|
|
561
583
|
// normal on dispose
|
|
562
584
|
return;
|
|
563
585
|
}
|
|
564
|
-
await safeLog(
|
|
586
|
+
await safeLog(state, "error", `event loop error: ${String(err)}`);
|
|
565
587
|
}
|
|
566
588
|
})();
|
|
567
589
|
// Ensure the loop doesn't block setup
|
|
@@ -571,13 +593,14 @@ const plugin = Plugin.define({
|
|
|
571
593
|
// Unregister every hook/transform registered by this setup() so a
|
|
572
594
|
// teardown/reload (e.g. session_move into a worktree) does not leave
|
|
573
595
|
// stale closures alive. Disposal failures are ignored — the
|
|
574
|
-
//
|
|
596
|
+
// stale-hook guard (`activeState !== state` → no-op / fail-open) keeps
|
|
597
|
+
// the session usable regardless.
|
|
575
598
|
for (const registration of registrations) {
|
|
576
599
|
try {
|
|
577
600
|
await registration.dispose();
|
|
578
601
|
}
|
|
579
602
|
catch {
|
|
580
|
-
// ignore —
|
|
603
|
+
// ignore — stale-hook guard is the safety net
|
|
581
604
|
}
|
|
582
605
|
}
|
|
583
606
|
// Only clear the singleton if it is still ours; a newer setup() may
|