kibi-opencode 0.13.0 → 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.
- package/README.md +4 -4
- package/dist/config.d.ts +1 -1
- package/dist/config.js +2 -2
- package/dist/enforcement-policy.d.ts +71 -0
- package/dist/enforcement-policy.js +269 -0
- package/dist/enforcement-scope.d.ts +15 -0
- package/dist/enforcement-scope.js +36 -0
- package/dist/file-operation-reminders.d.ts +13 -0
- package/dist/file-operation-reminders.js +24 -37
- package/dist/guidance-cache.d.ts +3 -0
- package/dist/guidance-cache.js +1 -1
- package/dist/kibi-checkpoint-runner.d.ts +83 -0
- package/dist/kibi-checkpoint-runner.js +254 -0
- package/dist/plugin.d.ts +1 -0
- package/dist/plugin.js +407 -164
- package/dist/prompt.d.ts +6 -0
- package/dist/prompt.js +25 -0
- package/dist/smart-enforcement.d.ts +6 -2
- package/dist/smart-enforcement.js +7 -1
- package/dist/work-context-resolver.d.ts +21 -0
- package/dist/work-context-resolver.js +197 -0
- package/package.json +1 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { type KibiConfig } from "./config.js";
|
|
2
|
+
import { type EnforcementLifecycleEvent, type PolicyLinkedEntityResult } from "./enforcement-policy.js";
|
|
3
|
+
import type { E2eCoverageSignal } from "./e2e-coverage-signals.js";
|
|
4
|
+
import type { PathKind } from "./path-kind.js";
|
|
5
|
+
import { type CheckRunner, type SyncRunMetadata, type SyncRunner, type TimeoutHandle } from "./scheduler.js";
|
|
6
|
+
import type { WorkContext } from "./work-context-resolver.js";
|
|
7
|
+
export interface KibiCheckpointContext {
|
|
8
|
+
workContext: WorkContext;
|
|
9
|
+
config?: KibiConfig;
|
|
10
|
+
filePath?: string;
|
|
11
|
+
checkRules?: string[] | undefined;
|
|
12
|
+
maintenanceDegraded?: boolean;
|
|
13
|
+
hardGuidanceText?: string | null;
|
|
14
|
+
lifecycleEvents?: EnforcementLifecycleEvent[];
|
|
15
|
+
pathKinds?: PathKind[];
|
|
16
|
+
linkedEntityResults?: PolicyLinkedEntityResult[];
|
|
17
|
+
e2eSignals?: E2eCoverageSignal[];
|
|
18
|
+
}
|
|
19
|
+
export interface KibiCheckpointRunnerOptions {
|
|
20
|
+
config?: KibiConfig;
|
|
21
|
+
runSync?: SyncRunner;
|
|
22
|
+
runCheck?: CheckRunner;
|
|
23
|
+
onRunComplete?: (meta: SyncRunMetadata) => void;
|
|
24
|
+
timeoutMs?: number;
|
|
25
|
+
setTimeoutFn?: (fn: () => void, ms: number) => TimeoutHandle;
|
|
26
|
+
clearTimeoutFn?: (handle: TimeoutHandle) => void;
|
|
27
|
+
}
|
|
28
|
+
export interface KibiCheckpointMetadata {
|
|
29
|
+
fingerprint: string;
|
|
30
|
+
scopeKey: string;
|
|
31
|
+
worktree: string;
|
|
32
|
+
branch: string;
|
|
33
|
+
sessionId?: string;
|
|
34
|
+
agentIdentity: string;
|
|
35
|
+
guidanceRendered?: boolean;
|
|
36
|
+
guidanceText?: string;
|
|
37
|
+
sync?: SyncRunMetadata;
|
|
38
|
+
reason?: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
checkRules?: string[] | undefined;
|
|
41
|
+
restoreInstructions?: string;
|
|
42
|
+
}
|
|
43
|
+
export type KibiCheckpointRequestResult = {
|
|
44
|
+
kind: "requested";
|
|
45
|
+
metadata: KibiCheckpointMetadata;
|
|
46
|
+
} | {
|
|
47
|
+
kind: "skip";
|
|
48
|
+
metadata: KibiCheckpointMetadata;
|
|
49
|
+
} | {
|
|
50
|
+
kind: "hard_block";
|
|
51
|
+
metadata: KibiCheckpointMetadata;
|
|
52
|
+
};
|
|
53
|
+
export type KibiCheckpointRunResult = {
|
|
54
|
+
kind: "passed";
|
|
55
|
+
metadata: KibiCheckpointMetadata;
|
|
56
|
+
} | {
|
|
57
|
+
kind: "hard_block";
|
|
58
|
+
metadata: KibiCheckpointMetadata;
|
|
59
|
+
} | {
|
|
60
|
+
kind: "skip";
|
|
61
|
+
metadata: KibiCheckpointMetadata;
|
|
62
|
+
};
|
|
63
|
+
export declare class KibiCheckpointRunner {
|
|
64
|
+
private readonly config;
|
|
65
|
+
private readonly runSync;
|
|
66
|
+
private readonly runCheck;
|
|
67
|
+
private readonly onRunComplete;
|
|
68
|
+
private readonly timeoutMs;
|
|
69
|
+
private readonly setTimeoutFn;
|
|
70
|
+
private readonly clearTimeoutFn;
|
|
71
|
+
private readonly requested;
|
|
72
|
+
private readonly passed;
|
|
73
|
+
private readonly passedScopeKeysByFingerprint;
|
|
74
|
+
constructor(options?: KibiCheckpointRunnerOptions);
|
|
75
|
+
requestCheckpoint(context: KibiCheckpointContext, fingerprint: string): KibiCheckpointRequestResult;
|
|
76
|
+
isCheckpointPassed(fingerprint: string, context?: KibiCheckpointContext): boolean;
|
|
77
|
+
runCheckpoint(context: KibiCheckpointContext, fingerprint: string): Promise<KibiCheckpointRunResult>;
|
|
78
|
+
private baseMetadata;
|
|
79
|
+
private scopeKey;
|
|
80
|
+
private renderHardGuidance;
|
|
81
|
+
private withTimeout;
|
|
82
|
+
private recordPassed;
|
|
83
|
+
}
|
|
@@ -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
|
+
}
|