opencode-commit-guard 1.0.5 → 1.0.7
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 +33 -5
- package/dist/index.js +552 -152
- package/package.json +7 -7
- package/src/capture.ts +44 -3
- package/src/config.ts +5 -0
- package/src/plugin.ts +156 -54
- package/src/scope.ts +1 -0
- package/src/shell.ts +443 -80
- package/src/types.ts +3 -0
- package/src/validator.ts +23 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-commit-guard",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "OpenCode V2 plugin enforcing git commit format rules",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
"prepack": "bun run build"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@opencode/plugin": "2.
|
|
25
|
-
"@opencode/schema": "2.
|
|
26
|
-
"effect": "4.0.
|
|
24
|
+
"@opencode/plugin": "2.2.1",
|
|
25
|
+
"@opencode/schema": "2.2.1",
|
|
26
|
+
"effect": "4.0.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@
|
|
30
|
-
"
|
|
31
|
-
"oxlint": "
|
|
29
|
+
"@types/bun": "1.4.4",
|
|
30
|
+
"oxlint": "1.90.0",
|
|
31
|
+
"oxlint-plugin-anti-slop": "github:dmmulroy/anti-slop#c44ef22ca116d0ba62a3ff663a0bd13a3f3fa40b",
|
|
32
32
|
"typescript": "7.0.2"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
package/src/capture.ts
CHANGED
|
@@ -53,7 +53,10 @@ function digest(text: string): string {
|
|
|
53
53
|
export function scopeFor(ctx: Context, sessionID: string, config: CommitGuardConfig, targetDir?: string): string {
|
|
54
54
|
const location = ctx.location
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
const directory = targetDir ?? location.directory
|
|
57
|
+
const root = findGitRoot(directory) ?? realpathSync(directory)
|
|
58
|
+
|
|
59
|
+
return digest(JSON.stringify([version, location.project.id, sessionID, root, location.workspaceID ?? null,
|
|
57
60
|
policy, config.requireScope, config.allowedScopes ?? null, config.maxLineLength]))
|
|
58
61
|
}
|
|
59
62
|
|
|
@@ -195,14 +198,52 @@ export function counter(ctx: Context, scope: string): Effect.Effect<number> {
|
|
|
195
198
|
})
|
|
196
199
|
}
|
|
197
200
|
|
|
198
|
-
export function invalidate(ctx: Context, scope: string): Effect.Effect<
|
|
201
|
+
export function invalidate(ctx: Context, scope: string): Effect.Effect<number> {
|
|
199
202
|
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
200
203
|
const previous = yield* counter(ctx, scope)
|
|
201
204
|
|
|
202
|
-
|
|
205
|
+
const next = previous < 0 ? 1 : previous + 1
|
|
206
|
+
|
|
207
|
+
yield* ctx.storage.set(counterKey(scope), next)
|
|
208
|
+
|
|
209
|
+
return next
|
|
210
|
+
}))
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function invalidateOwned(ctx: Context, attempt: Pending): Effect.Effect<void> {
|
|
214
|
+
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
215
|
+
if ((yield* counter(ctx, attempt.scope)) === attempt.counter) {
|
|
216
|
+
yield* ctx.storage.set(counterKey(attempt.scope), attempt.counter + 1)
|
|
217
|
+
}
|
|
218
|
+
}))
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export type CaptureState = "prepared" | "claimed" | "published" | "expired" | "failed"
|
|
222
|
+
|
|
223
|
+
export function recordState(ctx: Context, attempt: Pending, state: CaptureState, reason = ""): Effect.Effect<void> {
|
|
224
|
+
return storageLock.withPermits(1)(Effect.gen(function*() {
|
|
225
|
+
if ((yield* counter(ctx, attempt.scope)) !== attempt.counter) return
|
|
226
|
+
yield* ctx.storage.set(`state/${attempt.scope}`, { state, reason: reason.slice(0, 256) })
|
|
203
227
|
}))
|
|
204
228
|
}
|
|
205
229
|
|
|
230
|
+
export function captureStatus(ctx: Context, scope: string): Effect.Effect<string> {
|
|
231
|
+
return Effect.gen(function*() {
|
|
232
|
+
// SAFETY: Plugin storage contains JSON; validate the record before reading its fields.
|
|
233
|
+
const value = (yield* ctx.storage.get(`state/${scope}`)) as JsonValue | undefined
|
|
234
|
+
|
|
235
|
+
if (!isRecord(value)) return ""
|
|
236
|
+
|
|
237
|
+
if (value["state"] === "expired") return "The previous capture expired before its command was claimed; no matching shell result was observed. "
|
|
238
|
+
|
|
239
|
+
if (value["state"] === "failed" && isJSONString(value["reason"])) return `The previous capture failed: ${value["reason"]}. `
|
|
240
|
+
|
|
241
|
+
if (value["state"] === "prepared" || value["state"] === "claimed") return "The previous capture was interrupted before publication. "
|
|
242
|
+
|
|
243
|
+
return ""
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
|
|
206
247
|
function validBaseline(value: JsonValue | undefined): value is Baseline & Record<string, JsonValue> {
|
|
207
248
|
if (!isRecord(value)) return false
|
|
208
249
|
|
package/src/config.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
isRecord,
|
|
7
7
|
} from "./types.js"
|
|
8
8
|
import type { CommitGuardConfig, JsonValue } from "./types.js"
|
|
9
|
+
import { scopePattern } from "./scope.js"
|
|
9
10
|
|
|
10
11
|
export function parseConfig(options: JsonValue | undefined): CommitGuardConfig {
|
|
11
12
|
if (options === undefined) {
|
|
@@ -42,6 +43,10 @@ export function parseConfig(options: JsonValue | undefined): CommitGuardConfig {
|
|
|
42
43
|
throw new Error("Invalid plugin option allowedScopes; expected an array of non-empty strings.")
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
if (scopePattern.exec(`${item.trim()}: Subject`)?.[1] !== item.trim()) {
|
|
47
|
+
throw new Error(`Invalid plugin option allowedScopes; scope ${JSON.stringify(item)} cannot match the commit scope syntax.`)
|
|
48
|
+
}
|
|
49
|
+
|
|
45
50
|
scopes.push(item.trim())
|
|
46
51
|
}
|
|
47
52
|
|
package/src/plugin.ts
CHANGED
|
@@ -5,23 +5,25 @@ import { randomUUID } from "node:crypto"
|
|
|
5
5
|
import { existsSync, realpathSync } from "node:fs"
|
|
6
6
|
import { relative, resolve } from "node:path"
|
|
7
7
|
import {
|
|
8
|
-
|
|
8
|
+
captureStatus,
|
|
9
9
|
cleanup,
|
|
10
10
|
counter,
|
|
11
11
|
createPending,
|
|
12
12
|
findGitRoot,
|
|
13
13
|
importCapture,
|
|
14
14
|
invalidate,
|
|
15
|
+
invalidateOwned,
|
|
15
16
|
load,
|
|
16
17
|
publish,
|
|
18
|
+
recordState,
|
|
17
19
|
resolveTargetWorkdir,
|
|
18
20
|
scopeFor,
|
|
19
21
|
} from "./capture.js"
|
|
20
|
-
import type { Pending } from "./capture.js"
|
|
22
|
+
import type { Baseline, Pending } from "./capture.js"
|
|
21
23
|
import { parseConfig } from "./config.js"
|
|
22
24
|
import { extractGitCommits } from "./shell.js"
|
|
23
25
|
import { isJSONString, isRecord } from "./types.js"
|
|
24
|
-
import type { JsonValue } from "./types.js"
|
|
26
|
+
import type { CommitGuardConfig, JsonValue } from "./types.js"
|
|
25
27
|
import { validateGitCommits } from "./validator.js"
|
|
26
28
|
|
|
27
29
|
export const plugin = Plugin.define({
|
|
@@ -36,11 +38,32 @@ export const plugin = Plugin.define({
|
|
|
36
38
|
const pending = new Map<string, Pending>()
|
|
37
39
|
const preparing = new Set<string>()
|
|
38
40
|
const lastSessionWorkdir = new Map<string, string>()
|
|
41
|
+
const failures = new Map<string, string>()
|
|
42
|
+
|
|
43
|
+
const rememberFailure = (scope: string, reason: string) => {
|
|
44
|
+
failures.delete(scope)
|
|
45
|
+
failures.set(scope, `The previous capture failed: ${reason.slice(0, 256)}. `)
|
|
46
|
+
|
|
47
|
+
if (failures.size > 128) failures.delete(failures.keys().next().value!)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const sweep = () => Effect.gen(function*() {
|
|
51
|
+
for (const attempt of [...pending.values()].slice(0, 32)) {
|
|
52
|
+
pending.delete(attempt.scope)
|
|
53
|
+
|
|
54
|
+
if (attempt.claimed === undefined && Date.now() >= attempt.expires) {
|
|
55
|
+
cleanup(attempt)
|
|
56
|
+
yield* recordState(ctx, attempt, "expired")
|
|
57
|
+
yield* invalidateOwned(ctx, attempt)
|
|
58
|
+
} else pending.set(attempt.scope, attempt)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
39
61
|
|
|
40
62
|
yield* Effect.addFinalizer(() => Effect.gen(function*() {
|
|
41
63
|
for (const attempt of pending.values()) {
|
|
42
64
|
cleanup(attempt)
|
|
43
|
-
yield*
|
|
65
|
+
yield* recordState(ctx, attempt, "failed", "plugin unloaded before publication")
|
|
66
|
+
yield* invalidateOwned(ctx, attempt)
|
|
44
67
|
}
|
|
45
68
|
|
|
46
69
|
pending.clear()
|
|
@@ -48,7 +71,20 @@ export const plugin = Plugin.define({
|
|
|
48
71
|
|
|
49
72
|
yield* ctx.tool.transform((editor) => editor.add({
|
|
50
73
|
name: "commit_context",
|
|
51
|
-
description:
|
|
74
|
+
description: String.raw`Prepare a Git baseline. Run the returned command unchanged in the foreground; require "Frozen commit baseline".
|
|
75
|
+
Call before your first commit in each session and checkout.
|
|
76
|
+
Set workdir when targeting another repository.
|
|
77
|
+
Run the returned command immediately, unchanged, with the indicated workdir and background:false.
|
|
78
|
+
Do not wrap it or chain another command onto it.
|
|
79
|
+
Commit only after the shell result or this tool reports "Frozen commit baseline".
|
|
80
|
+
RECEIPT_OK alone is insufficient.
|
|
81
|
+
The shell result includes commit rules; a second call is optional.
|
|
82
|
+
Repeat an explicit workdir when checking that checkout.
|
|
83
|
+
Set refresh:true after switching branches, changing signing settings, or changing commit instructions.
|
|
84
|
+
Use literal message text and real body newlines.
|
|
85
|
+
Example: git commit -m $'scope: Subject\n\nBody line one.\nBody line two.'
|
|
86
|
+
Replace scope and follow the returned line-length and signoff rules.
|
|
87
|
+
Add -s only when the returned policy or repository instructions require it.`,
|
|
52
88
|
input: {
|
|
53
89
|
type: "object",
|
|
54
90
|
properties: {
|
|
@@ -59,6 +95,8 @@ export const plugin = Plugin.define({
|
|
|
59
95
|
additionalProperties: false,
|
|
60
96
|
},
|
|
61
97
|
execute: (input, toolContext) => Effect.gen(function*() {
|
|
98
|
+
yield* sweep()
|
|
99
|
+
|
|
62
100
|
if (ctx.location.workspaceID !== undefined) return { content: "Commit context capture is unavailable for remote workspaces." }
|
|
63
101
|
|
|
64
102
|
const session = yield* ctx.session.get({ sessionID: toolContext.sessionID })
|
|
@@ -94,7 +132,7 @@ export const plugin = Plugin.define({
|
|
|
94
132
|
|
|
95
133
|
const scope = scopeFor(ctx, toolContext.sessionID, config, targetWorkdir)
|
|
96
134
|
|
|
97
|
-
if (preparing.has(scope)) return { content: "A commit context capture is preparing for this session and checkout.
|
|
135
|
+
if (preparing.has(scope)) return { content: "A commit context capture is preparing for this session and checkout. Wait for its command, then follow its instructions." }
|
|
98
136
|
|
|
99
137
|
preparing.add(scope)
|
|
100
138
|
|
|
@@ -102,57 +140,47 @@ export const plugin = Plugin.define({
|
|
|
102
140
|
const previous = pending.get(scope)
|
|
103
141
|
|
|
104
142
|
if (previous !== undefined) {
|
|
105
|
-
if (
|
|
106
|
-
return { content:
|
|
143
|
+
if (previous.claimed !== undefined && request.refresh !== true) {
|
|
144
|
+
return { content: 'Capture claimed; permission approval or execution is pending. Wait for its foreground shell result. Commit after it reports "Frozen commit baseline"; otherwise call commit_context again with the same workdir before committing. RECEIPT_OK confirms only that the artifact was written, not that the baseline was published.' }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (request.refresh !== true) {
|
|
148
|
+
return { content: captureInstructions(previous, "Capture still pending and unclaimed. ") }
|
|
107
149
|
}
|
|
108
150
|
|
|
109
151
|
pending.delete(scope)
|
|
110
152
|
cleanup(previous)
|
|
111
|
-
yield*
|
|
153
|
+
yield* recordState(ctx, previous, "failed", "capture replaced by refresh")
|
|
154
|
+
yield* invalidateOwned(ctx, previous)
|
|
112
155
|
}
|
|
113
156
|
|
|
114
157
|
if (request.refresh !== true) {
|
|
115
158
|
const baseline = yield* load(ctx, scope)
|
|
116
159
|
|
|
117
|
-
if (baseline !== undefined &&
|
|
118
|
-
|
|
119
|
-
const examples = observed.length > 0 ? `Observed scope examples (not an allowlist): ${observed.join(", ")}.\n` : ""
|
|
120
|
-
|
|
121
|
-
const scopes = config.allowedScopes === undefined ? "No configured scope allowlist." :
|
|
122
|
-
`Configured scope allowlist: ${config.allowedScopes.join(", ").slice(0, 512)}.`
|
|
123
|
-
|
|
124
|
-
return { content: `Frozen commit baseline for ${targetWorkdir}.\n` +
|
|
125
|
-
`Use <scope>: <subject>${config.requireScope ? " (required)" : " (optional)"}; maximum line length: ${config.maxLineLength || "unlimited"}.\n` +
|
|
126
|
-
`${scopes}\n${examples}` +
|
|
127
|
-
`Effective commit.gpgsign: ${baseline.gpgsign}. Signoff ${baseline.gpgsign === "true" ? "required" : "not required"}; ` +
|
|
128
|
-
`signoff is not a cryptographic signature. user.signingkey: ${baseline.signingkey === null ? "unset" : "configured"}.\n` +
|
|
129
|
-
"Set refresh:true after a branch switch, changed signing settings, or changed commit instructions." }
|
|
160
|
+
if (baseline !== undefined && baseline.directory === findGitRoot(targetWorkdir)) {
|
|
161
|
+
return { content: baselineGuidance(baseline, config) }
|
|
130
162
|
}
|
|
131
163
|
}
|
|
132
164
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const count = yield* counter(ctx, scope)
|
|
165
|
+
const count = yield* invalidate(ctx, scope)
|
|
136
166
|
const attempt = yield* Effect.sync(() => createPending(scope, toolContext.sessionID, toolContext.agent, count, targetWorkdir))
|
|
137
167
|
|
|
138
168
|
pending.set(scope, attempt)
|
|
139
169
|
|
|
140
|
-
|
|
170
|
+
const status = failures.get(scope) ?? (yield* captureStatus(ctx, scope))
|
|
171
|
+
|
|
172
|
+
yield* recordState(ctx, attempt, "prepared")
|
|
173
|
+
|
|
174
|
+
return { content: captureInstructions(attempt, status) }
|
|
141
175
|
}).pipe(Effect.ensuring(Effect.sync(() => { preparing.delete(scope) })))
|
|
142
176
|
}).pipe(Effect.mapError(() => new Tool.Error({
|
|
143
177
|
message: "[commit-guard] Could not access the session or commit context. Call commit_context again from this checkout.",
|
|
144
178
|
}))),
|
|
145
179
|
}))
|
|
146
180
|
|
|
147
|
-
yield* ctx.shell.hook("create.before", (event) => Effect.sync(() => {
|
|
148
|
-
for (const attempt of pending.values()) {
|
|
149
|
-
if (attempt.claimed !== undefined && event.command === attempt.command && event.cwd === attempt.workdir) {
|
|
150
|
-
attempt.expires = Date.now() + captureTimeout
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}))
|
|
154
|
-
|
|
155
181
|
yield* ctx.tool.hook("execute.before", (event) => Effect.gen(function*() {
|
|
182
|
+
yield* sweep()
|
|
183
|
+
|
|
156
184
|
const captureSession = event.tool === "shell" || event.tool === "bash"
|
|
157
185
|
? yield* ctx.session.get({ sessionID: event.sessionID }) : undefined
|
|
158
186
|
|
|
@@ -193,7 +221,6 @@ export const plugin = Plugin.define({
|
|
|
193
221
|
}
|
|
194
222
|
|
|
195
223
|
attempt.claimed = { messageID: event.messageID, id: event.id }
|
|
196
|
-
attempt.expires = Date.now() + captureTimeout
|
|
197
224
|
|
|
198
225
|
return
|
|
199
226
|
}
|
|
@@ -204,8 +231,6 @@ export const plugin = Plugin.define({
|
|
|
204
231
|
return
|
|
205
232
|
}
|
|
206
233
|
|
|
207
|
-
validateGitCommits(invocations, { ...config, enforceSignoff: false }, command)
|
|
208
|
-
|
|
209
234
|
return { invocations, command, rawInput }
|
|
210
235
|
},
|
|
211
236
|
catch: (error) => new Tool.Error({
|
|
@@ -213,7 +238,12 @@ export const plugin = Plugin.define({
|
|
|
213
238
|
}),
|
|
214
239
|
})
|
|
215
240
|
|
|
216
|
-
|
|
241
|
+
for (const attempt of pending.values()) {
|
|
242
|
+
if (attempt.claimed?.messageID === event.messageID && attempt.claimed.id === event.id &&
|
|
243
|
+
attempt.sessionID === event.sessionID && attempt.agent === event.agent) yield* recordState(ctx, attempt, "claimed")
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (parsed === undefined || parsed.invocations.every((invocation) => invocation.isHelp)) return
|
|
217
247
|
|
|
218
248
|
if (ctx.location.workspaceID !== undefined) return yield* Effect.fail(new Tool.Error({
|
|
219
249
|
message: "[commit-guard] Remote workspace commits cannot use local commit context. Capture from a verified local checkout.",
|
|
@@ -236,30 +266,28 @@ export const plugin = Plugin.define({
|
|
|
236
266
|
))
|
|
237
267
|
|
|
238
268
|
const targetGitRoot = findGitRoot(effectiveWorkdir)
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (baseline === undefined && targetGitRoot !== undefined && targetGitRoot !== effectiveWorkdir) {
|
|
243
|
-
scope = scopeFor(ctx, event.sessionID, config, targetGitRoot)
|
|
244
|
-
baseline = yield* load(ctx, scope)
|
|
245
|
-
}
|
|
269
|
+
const scope = scopeFor(ctx, event.sessionID, config, effectiveWorkdir)
|
|
270
|
+
const baseline = yield* load(ctx, scope)
|
|
246
271
|
|
|
247
272
|
const expectedDir = targetGitRoot ?? effectiveWorkdir
|
|
248
273
|
|
|
249
274
|
const baselineValid = baseline !== undefined &&
|
|
250
275
|
targetGitRoot !== undefined && baseline.directory === targetGitRoot &&
|
|
251
|
-
|
|
252
|
-
baseline.gpgsign !== "error" && baseline.signingkey !== "error"
|
|
276
|
+
baseline.directory === expectedDir
|
|
253
277
|
|
|
254
278
|
if (!baselineValid || baseline === undefined) {
|
|
255
279
|
const relWorkdir = relative(sessionDirectory, expectedDir)
|
|
256
|
-
const workdirHint = relWorkdir.length > 0
|
|
280
|
+
const workdirHint = JSON.stringify(relWorkdir.length > 0 ? relWorkdir : ".")
|
|
257
281
|
|
|
258
282
|
return yield* Effect.fail(new Tool.Error({
|
|
259
|
-
message: `[commit-guard] No
|
|
283
|
+
message: `[commit-guard] No published baseline for ${expectedDir}. Call commit_context({ workdir: ${workdirHint} }), then run the returned command immediately, unchanged, in the foreground with that workdir and background:false. Do not wrap it or chain another command onto it. Commit after the shell result reports "Frozen commit baseline"; otherwise call commit_context again with the same workdir before committing. RECEIPT_OK alone is insufficient.`,
|
|
260
284
|
}))
|
|
261
285
|
}
|
|
262
286
|
|
|
287
|
+
if (baseline.gpgsign === "error" || baseline.signingkey === "error") return yield* Effect.fail(new Tool.Error({
|
|
288
|
+
message: signingError(baseline.gpgsign === "error" ? "commit.gpgsign" : "user.signingkey"),
|
|
289
|
+
}))
|
|
290
|
+
|
|
263
291
|
yield* Effect.try({
|
|
264
292
|
try: () => validateGitCommits(parsed.invocations, { ...config, enforceSignoff: baseline.gpgsign === "true" }, parsed.command),
|
|
265
293
|
catch: (error) => new Tool.Error({
|
|
@@ -279,13 +307,21 @@ export const plugin = Plugin.define({
|
|
|
279
307
|
if (attempt === undefined) return
|
|
280
308
|
|
|
281
309
|
let published = false
|
|
310
|
+
let captured: Baseline | undefined
|
|
311
|
+
let reason = "capture import or storage failed"
|
|
282
312
|
|
|
283
313
|
yield* Effect.gen(function*() {
|
|
284
|
-
|
|
285
|
-
|
|
314
|
+
reason = "shell result did not complete successfully"
|
|
315
|
+
|
|
316
|
+
if (event.status !== "completed") return
|
|
317
|
+
reason = "capture was superseded by a refresh"
|
|
318
|
+
|
|
319
|
+
if ((yield* counter(ctx, attempt.scope)) !== attempt.counter) return
|
|
286
320
|
// SAFETY: Tool result metadata is untrusted JSON; verify every required field before import.
|
|
287
321
|
const metadata = event.result.metadata as JsonValue | undefined
|
|
288
322
|
|
|
323
|
+
reason = "shell result lacks verified foreground completion metadata"
|
|
324
|
+
|
|
289
325
|
if (!isRecord(metadata)) return
|
|
290
326
|
|
|
291
327
|
// OpenCode's shell tool reports completed/exit:0 after a foreground process exits.
|
|
@@ -296,21 +332,87 @@ export const plugin = Plugin.define({
|
|
|
296
332
|
|
|
297
333
|
const session = yield* Effect.orElseSucceed(ctx.session.get({ sessionID: event.sessionID }), () => undefined)
|
|
298
334
|
|
|
335
|
+
reason = "capture session or workspace no longer matches"
|
|
336
|
+
|
|
299
337
|
if (session === undefined || session.location.workspaceID !== ctx.location.workspaceID) return
|
|
300
338
|
|
|
339
|
+
reason = "private artifact is invalid or incomplete (END required)"
|
|
301
340
|
const baseline = yield* Effect.try({ try: () => importCapture(attempt), catch: () => new Error("Invalid capture") })
|
|
302
341
|
|
|
303
342
|
if (baseline.workdir !== attempt.workdir || ctx.location.workspaceID !== undefined) return
|
|
343
|
+
reason = "capture storage failed or its counter changed"
|
|
304
344
|
published = yield* publish(ctx, attempt, generation, baseline)
|
|
345
|
+
|
|
346
|
+
if (published) {
|
|
347
|
+
captured = baseline
|
|
348
|
+
failures.delete(attempt.scope)
|
|
349
|
+
yield* recordState(ctx, attempt, "published")
|
|
350
|
+
}
|
|
305
351
|
}).pipe(Effect.onExit(() => Effect.gen(function*() {
|
|
306
352
|
if (pending.get(attempt.scope) === attempt) pending.delete(attempt.scope)
|
|
307
353
|
|
|
308
|
-
if (!published)
|
|
354
|
+
if (!published) {
|
|
355
|
+
rememberFailure(attempt.scope, reason)
|
|
356
|
+
yield* recordState(ctx, attempt, "failed", reason).pipe(Effect.catchCause(() => Effect.void))
|
|
357
|
+
yield* invalidateOwned(ctx, attempt)
|
|
358
|
+
}
|
|
359
|
+
}).pipe(Effect.ensuring(Effect.sync(() => cleanup(attempt))))), Effect.catchCause(() => Effect.void))
|
|
360
|
+
|
|
361
|
+
const note = published && captured !== undefined ? baselineGuidance(captured, config) :
|
|
362
|
+
`[commit-guard] This capture did not publish a usable baseline: ${reason.slice(0, 256)}.\n` +
|
|
363
|
+
"RECEIPT_OK alone does not authorize a commit.\n" +
|
|
364
|
+
"Call commit_context with the same workdir for status and next steps."
|
|
309
365
|
|
|
310
|
-
|
|
311
|
-
|
|
366
|
+
if (event.status === "error") {
|
|
367
|
+
event.error = new Tool.Error({ message: `${event.error.message}\n\n${note}` })
|
|
368
|
+
} else event.result = appendCaptureNote(event.result, note)
|
|
312
369
|
}))
|
|
313
370
|
}),
|
|
314
371
|
})
|
|
315
372
|
|
|
316
373
|
export default plugin
|
|
374
|
+
|
|
375
|
+
function captureInstructions(attempt: Pending, status: string): string {
|
|
376
|
+
return `${status}Run this shell command immediately, unchanged, in the foreground with workdir ${attempt.workdir} and background:false. ` +
|
|
377
|
+
'Do not wrap it or chain another command onto it. Commit after the shell result reports "Frozen commit baseline"; ' +
|
|
378
|
+
"otherwise call commit_context again with the same workdir before committing. " +
|
|
379
|
+
`RECEIPT_OK confirms only that the artifact was written, not that the baseline was published:\n${attempt.command}`
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function baselineGuidance(baseline: Baseline, config: CommitGuardConfig): string {
|
|
383
|
+
if (baseline.gpgsign === "error" || baseline.signingkey === "error") return signingError(baseline.gpgsign === "error" ? "commit.gpgsign" : "user.signingkey")
|
|
384
|
+
const observed = [...new Set(baseline.messages.map((message) => message.split("\n")[0]?.match(/^([a-zA-Z0-9_./-]{1,40}): /)?.[1]).filter((value) => value !== undefined))].slice(0, 3)
|
|
385
|
+
const examples = observed.length > 0 ? `Observed scope examples (not an allowlist): ${observed.join(", ")}.\n` : ""
|
|
386
|
+
|
|
387
|
+
const scopes = config.allowedScopes === undefined ? "No configured scope allowlist." :
|
|
388
|
+
`Configured scope allowlist: ${config.allowedScopes.join(", ").slice(0, 512)}.`
|
|
389
|
+
|
|
390
|
+
const signoff = baseline.gpgsign === "true" ? "Add -s or --signoff." :
|
|
391
|
+
"Signoff is not required; add it only if repository instructions require it."
|
|
392
|
+
|
|
393
|
+
return `[commit-guard] Frozen commit baseline for ${baseline.directory.slice(0, 4096)}.\n` +
|
|
394
|
+
`Use <scope>: <subject>${config.requireScope ? " (required)" : " (optional)"}; maximum line length: ${config.maxLineLength || "unlimited"}.\n` +
|
|
395
|
+
`${scopes}\n${examples}Effective commit.gpgsign: ${baseline.gpgsign}. ${signoff}\n` +
|
|
396
|
+
`user.signingkey: ${baseline.signingkey === null ? "unset" : "configured"}. Signoff is not a cryptographic signature.\n` +
|
|
397
|
+
"A second commit_context call is optional.\n" +
|
|
398
|
+
"Set refresh:true after a branch switch, changed signing settings, or changed commit instructions."
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function appendCaptureNote(result: Tool.Result, note: string): Tool.Result {
|
|
402
|
+
const content: readonly Tool.Content[] = Array.isArray(result.content) ? result.content :
|
|
403
|
+
result.content === undefined ? [] : [{ type: "text", text: String(result.content) }]
|
|
404
|
+
|
|
405
|
+
// SAFETY: Structured tool output is untrusted JSON; check the record and output field before copying it.
|
|
406
|
+
const output = result.output as JsonValue | undefined
|
|
407
|
+
const annotated: Tool.Result = { ...result, content: [...content, { type: "text", text: note }] }
|
|
408
|
+
|
|
409
|
+
if (isRecord(output) && isJSONString(output["output"])) {
|
|
410
|
+
return { ...annotated, output: { ...output, output: `${output["output"]}\n\n${note}` } }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return annotated
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function signingError(setting: string): string {
|
|
417
|
+
return `[commit-guard] Git could not read ${setting} as valid. Repeating the capture will not fix it. Inspect with git config --show-origin --get-all ${setting}; fix the value without changing the intended signing policy, then call commit_context with refresh:true.`
|
|
418
|
+
}
|
package/src/scope.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const scopePattern = /^([a-zA-Z0-9_./-]+(?:\([a-zA-Z0-9_./-]+\))?):\s+(.+)$/
|