kibi-opencode 0.12.1 → 0.14.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.
@@ -0,0 +1,254 @@
1
+ // implements REQ-opencode-worktree-hard-enforcement-v1
2
+ import { DEFAULTS } from "./config.js";
3
+ import { buildEnforcementScopeKey } from "./enforcement-scope.js";
4
+ import { computeEnforcementPolicy, } from "./enforcement-policy.js";
5
+ import { createSyncScheduler, } from "./scheduler.js";
6
+ const HARD_CHECKPOINT_TIMEOUT_MS = 30_000;
7
+ const CHECKPOINT_REASON = "smart-enforcement.checkpoint";
8
+ const NO_E2E_SIGNAL = {
9
+ level: "none",
10
+ evidence: [],
11
+ reminderText: null,
12
+ };
13
+ // implements REQ-opencode-worktree-hard-enforcement-v1
14
+ export class KibiCheckpointRunner {
15
+ config;
16
+ runSync;
17
+ runCheck;
18
+ onRunComplete;
19
+ timeoutMs;
20
+ setTimeoutFn;
21
+ clearTimeoutFn;
22
+ requested = new Map();
23
+ passed = new Map();
24
+ passedScopeKeysByFingerprint = new Map();
25
+ constructor(options = {}) {
26
+ this.config = options.config;
27
+ this.runSync = options.runSync;
28
+ this.runCheck = options.runCheck;
29
+ this.onRunComplete = options.onRunComplete;
30
+ this.timeoutMs = options.timeoutMs ?? HARD_CHECKPOINT_TIMEOUT_MS;
31
+ this.setTimeoutFn = options.setTimeoutFn ?? setTimeout;
32
+ this.clearTimeoutFn = options.clearTimeoutFn ?? clearTimeout;
33
+ }
34
+ // implements REQ-opencode-worktree-hard-enforcement-v1
35
+ requestCheckpoint(context, fingerprint) {
36
+ const normalizedFingerprint = normalizeFingerprint(fingerprint);
37
+ const metadata = this.baseMetadata(context, normalizedFingerprint);
38
+ if (!context.workContext.isAuthoritative) {
39
+ return {
40
+ kind: "skip",
41
+ metadata: { ...metadata, reason: "non_authoritative" },
42
+ };
43
+ }
44
+ const guidanceText = this.renderHardGuidance(context);
45
+ if (!guidanceText) {
46
+ return {
47
+ kind: "hard_block",
48
+ metadata: {
49
+ ...metadata,
50
+ guidanceRendered: false,
51
+ reason: "hard_guidance_not_rendered",
52
+ },
53
+ };
54
+ }
55
+ const requested = {
56
+ guidanceRendered: true,
57
+ metadata: {
58
+ ...metadata,
59
+ guidanceRendered: true,
60
+ guidanceText,
61
+ },
62
+ };
63
+ this.requested.set(metadata.scopeKey, requested);
64
+ return { kind: "requested", metadata: requested.metadata };
65
+ }
66
+ // implements REQ-opencode-worktree-hard-enforcement-v1
67
+ isCheckpointPassed(fingerprint, context) {
68
+ const normalizedFingerprint = normalizeFingerprint(fingerprint);
69
+ if (context) {
70
+ return this.passed.has(this.scopeKey(context, normalizedFingerprint));
71
+ }
72
+ return (this.passedScopeKeysByFingerprint.get(normalizedFingerprint)?.size ?? 0) > 0;
73
+ }
74
+ // implements REQ-opencode-worktree-hard-enforcement-v1
75
+ async runCheckpoint(context, fingerprint) {
76
+ const normalizedFingerprint = normalizeFingerprint(fingerprint);
77
+ const metadata = this.baseMetadata(context, normalizedFingerprint);
78
+ if (!context.workContext.isAuthoritative) {
79
+ return {
80
+ kind: "skip",
81
+ metadata: { ...metadata, reason: "non_authoritative" },
82
+ };
83
+ }
84
+ if (context.maintenanceDegraded === true) {
85
+ return {
86
+ kind: "hard_block",
87
+ metadata: {
88
+ ...metadata,
89
+ reason: "maintenance_degraded",
90
+ restoreInstructions: "Restore Kibi maintenance for this authoritative root before continuing: ensure sync is enabled and configured KB targets resolve, then retry the hard checkpoint.",
91
+ },
92
+ };
93
+ }
94
+ const request = this.requested.get(metadata.scopeKey);
95
+ if (!request?.guidanceRendered) {
96
+ return {
97
+ kind: "hard_block",
98
+ metadata: {
99
+ ...metadata,
100
+ guidanceRendered: false,
101
+ reason: "checkpoint_not_requested",
102
+ },
103
+ };
104
+ }
105
+ const completions = [];
106
+ const scheduler = createSyncScheduler({
107
+ worktree: context.workContext.worktreeRoot,
108
+ config: context.config ?? this.config ?? DEFAULTS,
109
+ ...(this.runSync ? { runSync: this.runSync } : {}),
110
+ ...(this.runCheck ? { runCheck: this.runCheck } : {}),
111
+ onRunComplete: (meta) => {
112
+ completions.push(meta);
113
+ this.onRunComplete?.(meta);
114
+ },
115
+ });
116
+ try {
117
+ scheduler.scheduleSync(CHECKPOINT_REASON, context.filePath, normalizedCheckRules(context));
118
+ const outcome = await this.withTimeout(scheduler.flush());
119
+ if (outcome === "timeout") {
120
+ scheduler.dispose();
121
+ return {
122
+ kind: "hard_block",
123
+ metadata: {
124
+ ...request.metadata,
125
+ reason: "timeout",
126
+ timeoutMs: this.timeoutMs,
127
+ checkRules: normalizedCheckRules(context),
128
+ },
129
+ };
130
+ }
131
+ const sync = completions[completions.length - 1];
132
+ if (!sync) {
133
+ return {
134
+ kind: "hard_block",
135
+ metadata: {
136
+ ...request.metadata,
137
+ reason: "sync_not_run",
138
+ checkRules: normalizedCheckRules(context),
139
+ },
140
+ };
141
+ }
142
+ const failureReason = checkpointFailureReason(sync, normalizedCheckRules(context));
143
+ if (failureReason) {
144
+ return {
145
+ kind: "hard_block",
146
+ metadata: {
147
+ ...request.metadata,
148
+ sync,
149
+ reason: failureReason,
150
+ checkRules: normalizedCheckRules(context),
151
+ },
152
+ };
153
+ }
154
+ const passedMetadata = {
155
+ ...request.metadata,
156
+ sync,
157
+ checkRules: normalizedCheckRules(context),
158
+ };
159
+ this.recordPassed(passedMetadata);
160
+ return { kind: "passed", metadata: passedMetadata };
161
+ }
162
+ finally {
163
+ scheduler.dispose();
164
+ }
165
+ }
166
+ baseMetadata(context, fingerprint) {
167
+ const workContext = context.workContext;
168
+ return {
169
+ fingerprint,
170
+ scopeKey: this.scopeKey(context, fingerprint),
171
+ worktree: workContext.worktreeRoot,
172
+ branch: workContext.branch,
173
+ ...(workContext.sessionId !== undefined
174
+ ? { sessionId: workContext.sessionId }
175
+ : {}),
176
+ agentIdentity: workContext.agentIdentity,
177
+ };
178
+ }
179
+ scopeKey(context, fingerprint) {
180
+ return buildEnforcementScopeKey({
181
+ sessionId: context.workContext.sessionId,
182
+ agentIdentity: context.workContext.agentIdentity,
183
+ worktreeRoot: context.workContext.worktreeRoot,
184
+ branch: context.workContext.branch,
185
+ dirtyRelevantFingerprint: fingerprint,
186
+ });
187
+ }
188
+ renderHardGuidance(context) {
189
+ if (typeof context.hardGuidanceText === "string") {
190
+ const trimmed = context.hardGuidanceText.trim();
191
+ return trimmed.length > 0 ? context.hardGuidanceText : null;
192
+ }
193
+ const policy = computeEnforcementPolicy({
194
+ resolvedContext: context.workContext,
195
+ effectiveMode: "hard",
196
+ lifecycleEvents: context.lifecycleEvents ?? [defaultLifecycleEvent(context)],
197
+ pathKinds: context.pathKinds ?? ["code"],
198
+ linkedEntityResults: context.linkedEntityResults ?? [
199
+ { ids: [], source: "none" },
200
+ ],
201
+ e2eSignals: context.e2eSignals ?? [NO_E2E_SIGNAL],
202
+ checkpointEvidence: false,
203
+ posture: context.workContext.posture,
204
+ });
205
+ return policy.kind === "hard_block" ? policy.text : null;
206
+ }
207
+ async withTimeout(promise) {
208
+ let timeoutHandle;
209
+ const timeout = new Promise((resolve) => {
210
+ timeoutHandle = this.setTimeoutFn(() => resolve("timeout"), this.timeoutMs);
211
+ });
212
+ const result = await Promise.race([
213
+ promise.then(() => "done"),
214
+ timeout,
215
+ ]);
216
+ if (timeoutHandle !== undefined) {
217
+ this.clearTimeoutFn(timeoutHandle);
218
+ }
219
+ return result;
220
+ }
221
+ recordPassed(metadata) {
222
+ this.passed.set(metadata.scopeKey, metadata);
223
+ const existing = this.passedScopeKeysByFingerprint.get(metadata.fingerprint);
224
+ if (existing) {
225
+ existing.add(metadata.scopeKey);
226
+ return;
227
+ }
228
+ this.passedScopeKeysByFingerprint.set(metadata.fingerprint, new Set([metadata.scopeKey]));
229
+ }
230
+ }
231
+ function normalizeFingerprint(fingerprint) {
232
+ const trimmed = fingerprint.trim();
233
+ return trimmed.length > 0 ? trimmed : "clean";
234
+ }
235
+ function normalizedCheckRules(context) {
236
+ return context.checkRules && context.checkRules.length > 0
237
+ ? [...context.checkRules]
238
+ : undefined;
239
+ }
240
+ function defaultLifecycleEvent(context) {
241
+ return {
242
+ normalizedPath: context.filePath ?? context.workContext.repoRelativePath ?? "<unknown>",
243
+ lifecycle: "edited",
244
+ };
245
+ }
246
+ function checkpointFailureReason(sync, checkRules) {
247
+ if (sync.exitCode !== 0) {
248
+ return "sync_failed";
249
+ }
250
+ if (checkRules && checkRules.length > 0 && sync.checkExitCode !== 0) {
251
+ return "check_failed";
252
+ }
253
+ return null;
254
+ }
package/dist/plugin.d.ts CHANGED
@@ -3,6 +3,7 @@ export interface PluginInput {
3
3
  worktree: string;
4
4
  directory: string;
5
5
  sessionId?: string;
6
+ agentIdentity?: string;
6
7
  serverUrl?: unknown;
7
8
  workspace?: string;
8
9
  project?: unknown;