@plainconceptsplatform/agent-harness 2.9.0 → 3.0.2
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 +5 -5
- package/cli/commands/join.js +19 -3
- package/cli/presets/quota.json +6 -2
- package/cli/steps/copy/opencode-json.js +260 -135
- package/cli/steps/optimization/quota.js +36 -20
- package/cli/utils/copy.js +0 -1
- package/harness/.opencode/_gitignore +9 -9
- package/harness/.opencode/package.json +1 -5
- package/harness/.opencode/plugins/pc-subagent-monitor.js +144 -179
- package/harness/.opencode/plugins/pc-subagent-tiers.js +225 -248
- package/harness/.opencode/plugins/pc-system-reminders.js +386 -434
- package/harness/opencode.jsonc +21 -32
- package/package.json +1 -1
- package/skills/pc-guardrails-generic/SKILL.md +15 -10
- package/skills/pc-make-engineer/SKILL.md +59 -59
- package/skills/pc-make-engineer/template.md +36 -42
- package/skills/pc-make-guardrails/category-reference.md +2 -2
- package/skills/pc-plan-goal/branching.md +39 -30
- package/harness/.opencode/tui/pc-subagents.tsx +0 -98
- package/harness/.opencode/tui.json +0 -6
|
@@ -1,434 +1,386 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"`git commit -a` stages every tracked change, including work that is not yours.",
|
|
79
|
-
"Stage named paths first, then commit without `-a`.",
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
if (/^git\s+clean\b/.test(segment) && !/\s--\s+\S/.test(segment)) {
|
|
83
|
-
deny(
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
return
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
async function
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
if (input.tool === "webfetch" || /^agent-browser_/.test(input.tool)) {
|
|
388
|
-
backlogPlatform ??= await readBacklogPlatform(root)
|
|
389
|
-
checkHost(input.tool, args, backlogPlatform)
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (input.tool === "bash") {
|
|
393
|
-
const command = typeof args.command === "string" ? args.command : ""
|
|
394
|
-
if (!command) return
|
|
395
|
-
|
|
396
|
-
if (state?.agent?.replace(TIER_SUFFIX, "") === "plan") checkPlanReadOnly(command)
|
|
397
|
-
checkGit(command)
|
|
398
|
-
checkScratch(command)
|
|
399
|
-
if (/\bgit\s+push\b/.test(command)) {
|
|
400
|
-
defaultBranch ??= await detectDefaultBranch(root)
|
|
401
|
-
await checkPush(command, root, defaultBranch)
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
} catch (error) {
|
|
405
|
-
if (error?.[DENIED]) throw error
|
|
406
|
-
// A bug in this guard must never break the session it is guarding.
|
|
407
|
-
console.error(`[harness] guard failed open on ${input.tool}: ${error?.message}`)
|
|
408
|
-
}
|
|
409
|
-
},
|
|
410
|
-
"tool.execute.after": async (input) => {
|
|
411
|
-
if (input.tool !== "skill") return
|
|
412
|
-
const state = sessions.get(input.sessionID)
|
|
413
|
-
const name = skillName(input.args)
|
|
414
|
-
if (state && name) state.loaded.add(name)
|
|
415
|
-
},
|
|
416
|
-
event: async ({ event }) => {
|
|
417
|
-
if (event.type !== "session.compacted") return
|
|
418
|
-
const sessionID = event.properties?.sessionID ?? event.properties?.info?.id
|
|
419
|
-
const state = sessionID && sessions.get(sessionID)
|
|
420
|
-
if (state) state.loaded.clear()
|
|
421
|
-
},
|
|
422
|
-
"experimental.chat.messages.transform": async (_input, output) => {
|
|
423
|
-
const userMessage = [...output.messages].reverse().find(message => message.info.role === "user")
|
|
424
|
-
if (!userMessage) return
|
|
425
|
-
|
|
426
|
-
const state = await stateFor(userMessage.info.sessionID, userMessage.info.agent)
|
|
427
|
-
const missing = missingFor(state)
|
|
428
|
-
if (missing.size === 0) return
|
|
429
|
-
|
|
430
|
-
const textPart = userMessage.parts.find(part => part.type === "text")
|
|
431
|
-
if (textPart) textPart.text = `${textPart.text}\n\n${reminder(missing)}`
|
|
432
|
-
},
|
|
433
|
-
}
|
|
434
|
-
}
|
|
1
|
+
// pc-system-reminders: the harness guardrails, enforced rather than requested.
|
|
2
|
+
//
|
|
3
|
+
// OpenCode 2 changed how a plugin denies something. V1 threw from
|
|
4
|
+
// `tool.execute.before`; V2 has no such contract and denies through the
|
|
5
|
+
// permission system instead: an `evaluate` hook reviews a decision after the
|
|
6
|
+
// configured rules run and may set the effect to "deny". Every rule below is
|
|
7
|
+
// therefore expressed as a permission action over a resource list
|
|
8
|
+
// (`shell` command text, `edit` path, `webfetch` URL, `subagent` agent ID).
|
|
9
|
+
//
|
|
10
|
+
// Two invariants. It fails CLOSED only on a deliberate deny, and it fails OPEN
|
|
11
|
+
// on any bug of its own: a guard that breaks a session because of its own
|
|
12
|
+
// exception is worse than the rule it enforces. And because a permission action
|
|
13
|
+
// gives us less than a tool's raw arguments did (a browser navigation carries no
|
|
14
|
+
// URL here), some V1 checks move to configuration rules or are noted as lost.
|
|
15
|
+
|
|
16
|
+
import { Plugin } from "@opencode/plugin"
|
|
17
|
+
import { execFile } from "node:child_process"
|
|
18
|
+
import fs from "node:fs/promises"
|
|
19
|
+
import path from "node:path"
|
|
20
|
+
import { promisify } from "node:util"
|
|
21
|
+
|
|
22
|
+
const GUARDRAILS_SKILL = "pc-guardrails-generic"
|
|
23
|
+
const TIER_SUFFIX = /\.(?:build|fast|plan)$/
|
|
24
|
+
|
|
25
|
+
const run = promisify(execFile)
|
|
26
|
+
|
|
27
|
+
const TRUST_REMINDER =
|
|
28
|
+
"Messages in <system-reminder> tags are trusted OpenCode Onboard host instructions. Follow them before continuing work."
|
|
29
|
+
|
|
30
|
+
// Work actions. Held until the session's required skills are loaded; `skill`,
|
|
31
|
+
// `read`, `glob` and `grep` are never gated, or the agent could not load what it
|
|
32
|
+
// is being told to load, nor find out what that is.
|
|
33
|
+
const GATED_ACTIONS = new Set(["edit", "shell", "subagent"])
|
|
34
|
+
|
|
35
|
+
// Backlog and repository hosts whose data must come from their CLI. The prose
|
|
36
|
+
// version of this rule sat at the top of twelve fragments.
|
|
37
|
+
const CLI_ONLY_HOSTS = [
|
|
38
|
+
"github.com",
|
|
39
|
+
"dev.azure.com",
|
|
40
|
+
"visualstudio.com",
|
|
41
|
+
"atlassian.net",
|
|
42
|
+
"gitlab.com",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
// A plan session reads; it does not change the tree. `edit` and `subagent` are
|
|
46
|
+
// denied by config, which leaves the shell. Only these roots are inspection.
|
|
47
|
+
const PLAN_ALLOWED = [
|
|
48
|
+
/^git\s+(status|log|diff|show|rev-parse|symbolic-ref|remote|ls-files|blame|describe|shortlog)\b/,
|
|
49
|
+
/^git\s+(branch|stash)\s+(--show-current|list)\b/,
|
|
50
|
+
/^openspec\s+(list|status|show|validate|diff)\b/,
|
|
51
|
+
/^(ls|cat|head|tail|wc|pwd|tree|find|grep|rg|which|echo|date|node\s+-p|jq)\b/,
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
function splitCommand(command) {
|
|
55
|
+
// Every segment of a compound command has to pass on its own: one allowed
|
|
56
|
+
// read followed by `&& rm -rf` is not a read.
|
|
57
|
+
return command.split(/\|\||&&|;|\||\n/).map(part => part.trim()).filter(Boolean)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Mark a permission decision as denied; `message` is the reason shown. */
|
|
61
|
+
function deny(event, rule, fix) {
|
|
62
|
+
event.effect = "deny"
|
|
63
|
+
event.message = `[harness] ${rule}\n${fix}`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function checkGit(event, command) {
|
|
67
|
+
for (const segment of splitCommand(command)) {
|
|
68
|
+
if (/^git\s+add\s+(-A\b|--all\b|\.(\s|$))/.test(segment)) {
|
|
69
|
+
return deny(
|
|
70
|
+
event,
|
|
71
|
+
"`git add -A` and `git add .` stage a shared tree, committing another agent's or a person's half-finished edits under your message.",
|
|
72
|
+
"Stage the paths you changed: `git add <path> <path>`.",
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
if (/^git\s+commit\b.*\s-a\b/.test(segment) || /^git\s+commit\s+-[a-z]*a[a-z]*\b/.test(segment)) {
|
|
76
|
+
return deny(
|
|
77
|
+
event,
|
|
78
|
+
"`git commit -a` stages every tracked change, including work that is not yours.",
|
|
79
|
+
"Stage named paths first, then commit without `-a`.",
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
if (/^git\s+clean\b/.test(segment) && !/\s--\s+\S/.test(segment)) {
|
|
83
|
+
return deny(
|
|
84
|
+
event,
|
|
85
|
+
"`git clean` without `-- <paths>` deletes untracked files anywhere in the tree, including work nobody has committed yet.",
|
|
86
|
+
"Scope it: `git clean -f -- <path> <path>`.",
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
if (/^git\s+reset\s+--hard\b/.test(segment)) {
|
|
90
|
+
return deny(
|
|
91
|
+
event,
|
|
92
|
+
"`git reset --hard` discards uncommitted work in a tree you may be sharing.",
|
|
93
|
+
"Revert the paths you touched: `git checkout -- <path>`.",
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
if (/^git\s+stash\s+(drop|clear)\b/.test(segment)) {
|
|
97
|
+
return deny(
|
|
98
|
+
event,
|
|
99
|
+
"Dropping a stash destroys work that was set aside, possibly not yours.",
|
|
100
|
+
"Leave the stash and report its `git stash list` reference.",
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
if (/^git\s+(checkout|restore)\s+\.(\s|$)/.test(segment)) {
|
|
104
|
+
return deny(
|
|
105
|
+
event,
|
|
106
|
+
"`git checkout .` and `git restore .` discard every uncommitted change in the tree.",
|
|
107
|
+
"Name the paths to revert: `git checkout -- <path>`.",
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
if (/^git\s+push\b/.test(segment) && /--force\b/.test(segment) && !/--force-with-lease\b/.test(segment)) {
|
|
111
|
+
return deny(
|
|
112
|
+
event,
|
|
113
|
+
"`git push --force` can overwrite commits someone else pushed.",
|
|
114
|
+
"Use `--force-with-lease`, which refuses when the remote moved.",
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function checkPush(event, command, root, defaultBranch) {
|
|
121
|
+
if (!defaultBranch) return
|
|
122
|
+
for (const segment of splitCommand(command)) {
|
|
123
|
+
if (!/^git\s+push\b/.test(segment)) continue
|
|
124
|
+
// Split on delimiters rather than interpolating the branch name into a
|
|
125
|
+
// pattern: a branch name is not a pattern, and a dynamic RegExp was both a
|
|
126
|
+
// Semgrep finding and a hole in the guard.
|
|
127
|
+
const named = segment.split(/[\s:]+/).includes(defaultBranch)
|
|
128
|
+
if (named) {
|
|
129
|
+
return deny(
|
|
130
|
+
event,
|
|
131
|
+
`This pushes \`${defaultBranch}\`, the default branch. The harness ships work on a branch and lets a human merge it.`,
|
|
132
|
+
"Push your work branch instead, or open a pull request.",
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
if (!/\s(origin|upstream)\b/.test(segment) || /^git\s+push\s*$/.test(segment)) {
|
|
136
|
+
const current = await run("git", ["branch", "--show-current"], { cwd: root })
|
|
137
|
+
.then(result => result.stdout.trim())
|
|
138
|
+
.catch(() => "")
|
|
139
|
+
if (current && current === defaultBranch) {
|
|
140
|
+
return deny(
|
|
141
|
+
event,
|
|
142
|
+
`You are on \`${defaultBranch}\`, the default branch, and this pushes it.`,
|
|
143
|
+
"Move the work to a branch first: `git switch -c feature/<slug>`.",
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// `/tmp/gh-aw/` is the agent workflow runtime's own working directory, not agent
|
|
151
|
+
// scratch. GitHub Agentic Workflows stages the issue context there and the
|
|
152
|
+
// worker prompts name those paths, so writes inside it are expected.
|
|
153
|
+
function checkScratch(event, command) {
|
|
154
|
+
const writesSomewhere = /(>>?|\btee\b|\bcp\b|\bmv\b|\bmkdir\b|\btouch\b|\bdd\b)/.test(command)
|
|
155
|
+
const outsideRepo = /(\/tmp\/(?!gh-aw\/)|\$TMPDIR|\$TEMP\b|%TEMP%|\bmktemp\b)/.test(command)
|
|
156
|
+
if (writesSomewhere && outsideRepo) {
|
|
157
|
+
return deny(
|
|
158
|
+
event,
|
|
159
|
+
"Scratch files belong inside the repository, where the next step and the next agent can still find them.",
|
|
160
|
+
"Write under `$REPO_ROOT/.opencode/.tmp/`, or the workflow runtime's own `/tmp/gh-aw/`.",
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function checkWritePath(event, target, root) {
|
|
166
|
+
if (typeof target !== "string" || !target) return
|
|
167
|
+
if (!path.isAbsolute(target)) return
|
|
168
|
+
// Windows hands back mixed drive-letter case, so compare case-insensitively
|
|
169
|
+
// there and exactly everywhere else.
|
|
170
|
+
const normalize = value => (process.platform === "win32" ? path.resolve(value).toLowerCase() : path.resolve(value))
|
|
171
|
+
const resolved = normalize(target)
|
|
172
|
+
const base = normalize(root)
|
|
173
|
+
if (resolved === base || resolved.startsWith(base + path.sep)) return
|
|
174
|
+
return deny(
|
|
175
|
+
event,
|
|
176
|
+
`\`${target}\` is outside the repository, so nothing else in the run can see it and nobody will clean it up.`,
|
|
177
|
+
"Write under `$REPO_ROOT/.opencode/.tmp/` instead.",
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function checkHost(event, url) {
|
|
182
|
+
if (typeof url !== "string" || !url) return
|
|
183
|
+
let host = ""
|
|
184
|
+
try {
|
|
185
|
+
host = new URL(url).hostname.toLowerCase()
|
|
186
|
+
} catch {
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
const cliOnly = CLI_ONLY_HOSTS.find(candidate => host === candidate || host.endsWith(`.${candidate}`))
|
|
190
|
+
if (cliOnly) {
|
|
191
|
+
return deny(
|
|
192
|
+
event,
|
|
193
|
+
`${cliOnly} data must come from its CLI (\`gh\`, \`az\`, \`glab\`, \`acli\`), which is authenticated and returns structured output. A page fetch returns whatever HTML the browser would see.`,
|
|
194
|
+
"Use the platform CLI. If it is unavailable, report that as a blocker.",
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function checkPlanReadOnly(event, command) {
|
|
200
|
+
for (const segment of splitCommand(command)) {
|
|
201
|
+
// `echo` and `cat` are inspection right up to the point a redirect turns
|
|
202
|
+
// them into a write, so the redirect is checked before the allowlist.
|
|
203
|
+
if (/(^|\s)>>?\s*\S/.test(segment) || /\btee\b/.test(segment)) {
|
|
204
|
+
return deny(
|
|
205
|
+
event,
|
|
206
|
+
"The plan agent reads; a redirect writes to the tree.",
|
|
207
|
+
"Switch to the build agent to make changes.",
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
if (PLAN_ALLOWED.some(pattern => pattern.test(segment))) continue
|
|
211
|
+
return deny(
|
|
212
|
+
event,
|
|
213
|
+
`The plan agent reads; it does not change the tree, and \`${segment.split(/\s+/)[0]}\` is not one of its inspection commands.`,
|
|
214
|
+
"Switch to the build agent to make changes.",
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function skillNames(content) {
|
|
220
|
+
const abilities = content.match(/^## Abilities\s*\n([\s\S]*?)(?=^## |\s*$)/m)?.[1] ?? ""
|
|
221
|
+
return [...abilities.matchAll(/@([a-z0-9][a-z0-9-]*)/gi)].map(match => match[1])
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function transitiveSkillNames(content) {
|
|
225
|
+
return [...content.matchAll(/skill\(["`]([a-z0-9][a-z0-9-]*)["`]\)/gi)].map(match => match[1])
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function readFile(filePath) {
|
|
229
|
+
try {
|
|
230
|
+
return await fs.readFile(filePath, "utf-8")
|
|
231
|
+
} catch {
|
|
232
|
+
return ""
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function requiredSkills(directory, agent) {
|
|
237
|
+
const baseAgent = (agent ?? "").replace(TIER_SUFFIX, "")
|
|
238
|
+
const agentPath = path.join(directory, ".opencode", "agents", `${baseAgent}.md`)
|
|
239
|
+
const guardrailsPath = path.join(directory, ".agents", "skills", GUARDRAILS_SKILL, "SKILL.md")
|
|
240
|
+
const [agentContent, guardrailsContent] = await Promise.all([readFile(agentPath), readFile(guardrailsPath)])
|
|
241
|
+
return new Set([GUARDRAILS_SKILL, ...skillNames(agentContent), ...transitiveSkillNames(guardrailsContent)])
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Only skills that exist on disk can gate work. An `@skill` reference to
|
|
245
|
+
// something uninstalled would otherwise be unloadable and deadlock the worker,
|
|
246
|
+
// which is a worse failure than the dangling reference itself.
|
|
247
|
+
async function installedSkills(directory, names) {
|
|
248
|
+
const present = new Set()
|
|
249
|
+
await Promise.all([...names].map(async name => {
|
|
250
|
+
try {
|
|
251
|
+
await fs.access(path.join(directory, ".agents", "skills", name, "SKILL.md"))
|
|
252
|
+
present.add(name)
|
|
253
|
+
} catch {
|
|
254
|
+
// not installed; the reminder still names it, the gate ignores it
|
|
255
|
+
}
|
|
256
|
+
}))
|
|
257
|
+
return present
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async function detectDefaultBranch(root) {
|
|
261
|
+
const fromRemote = await run("git", ["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], { cwd: root })
|
|
262
|
+
.then(result => result.stdout.trim().replace(/^origin\//, ""))
|
|
263
|
+
.catch(() => "")
|
|
264
|
+
if (fromRemote) return fromRemote
|
|
265
|
+
return run("git", ["config", "--get", "init.defaultBranch"], { cwd: root })
|
|
266
|
+
.then(result => result.stdout.trim() || "main")
|
|
267
|
+
.catch(() => "main")
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function readBacklogPlatform(root) {
|
|
271
|
+
const raw = await readFile(path.join(root, ".opencode", "harness.json"))
|
|
272
|
+
try {
|
|
273
|
+
return JSON.parse(raw)?.platform?.backlog ?? "none"
|
|
274
|
+
} catch {
|
|
275
|
+
return "none"
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function reminder(missing) {
|
|
280
|
+
const skills = [...missing].map(name => `\`${name}\``).join(", ")
|
|
281
|
+
return `<system-reminder>Load these required skills before continuing: ${skills}. Guardrails first. A loaded skill can require further loads; follow those too. Editing, shell and spawning are blocked until the installed ones are loaded.</system-reminder>`
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export default Plugin.define({
|
|
285
|
+
id: "pc.system-reminders",
|
|
286
|
+
async setup(ctx) {
|
|
287
|
+
const root = ctx.location.directory || process.cwd()
|
|
288
|
+
const sessions = new Map()
|
|
289
|
+
let defaultBranch
|
|
290
|
+
let backlogPlatform
|
|
291
|
+
|
|
292
|
+
async function stateFor(sessionID, agent) {
|
|
293
|
+
const state = sessions.get(sessionID)
|
|
294
|
+
if (state?.agent === agent) return state
|
|
295
|
+
const required = await requiredSkills(root, agent)
|
|
296
|
+
const next = {
|
|
297
|
+
agent,
|
|
298
|
+
required,
|
|
299
|
+
present: await installedSkills(root, required),
|
|
300
|
+
// A session can switch agent (plan <-> build). What has been loaded is a
|
|
301
|
+
// property of the session, not the agent, so it carries across.
|
|
302
|
+
loaded: state?.loaded ?? new Set(),
|
|
303
|
+
}
|
|
304
|
+
sessions.set(sessionID, next)
|
|
305
|
+
return next
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function missingFor(state) {
|
|
309
|
+
return [...state.required].filter(name => !state.loaded.has(name) && state.present.has(name))
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// System instructions that name the trust boundary and, until the session's
|
|
313
|
+
// required skills are loaded, that work is blocked. V2 applies this to every
|
|
314
|
+
// request through the agent loop, which is at least as strong as the V1
|
|
315
|
+
// message-transcript injection it replaces.
|
|
316
|
+
await ctx.session.hook("context", async event => {
|
|
317
|
+
try {
|
|
318
|
+
event.system.push({ type: "text", text: TRUST_REMINDER })
|
|
319
|
+
const state = await stateFor(event.sessionID, event.agent)
|
|
320
|
+
const missing = missingFor(state)
|
|
321
|
+
if (missing.length > 0) event.system.push({ type: "text", text: reminder(missing) })
|
|
322
|
+
} catch (error) {
|
|
323
|
+
console.error(`[harness] reminder hook failed open: ${error?.message}`)
|
|
324
|
+
}
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
await ctx.permission.hook("evaluate", async event => {
|
|
328
|
+
try {
|
|
329
|
+
const state = await stateFor(event.sessionID, event.agent)
|
|
330
|
+
|
|
331
|
+
// Skill activation is how the gate learns a skill was loaded. V2 routes
|
|
332
|
+
// skill use through the `skill` action (resource = skill ID); an allow
|
|
333
|
+
// decision still runs this hook, so it is the observation point.
|
|
334
|
+
if (event.action === "skill") {
|
|
335
|
+
for (const resource of event.resources) state.loaded.add(resource)
|
|
336
|
+
return
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (GATED_ACTIONS.has(event.action)) {
|
|
340
|
+
const missing = missingFor(state)
|
|
341
|
+
if (missing.length > 0) {
|
|
342
|
+
deny(
|
|
343
|
+
event,
|
|
344
|
+
`Required skills are not loaded yet: ${missing.map(name => `\`${name}\``).join(", ")}.`,
|
|
345
|
+
`Call skill(${JSON.stringify(missing[0])}) first, guardrails first. Editing, shell and spawning stay blocked until then.`,
|
|
346
|
+
)
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if (event.action === "edit") {
|
|
352
|
+
for (const resource of event.resources) {
|
|
353
|
+
if (checkWritePath(event, resource, root)) return
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (event.action === "webfetch") {
|
|
358
|
+
for (const resource of event.resources) {
|
|
359
|
+
backlogPlatform ??= await readBacklogPlatform(root)
|
|
360
|
+
if (checkHost(event, resource)) return
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (event.action === "shell") {
|
|
365
|
+
const command = event.resources.join(" && ")
|
|
366
|
+
if (!command) return
|
|
367
|
+
|
|
368
|
+
if ((state.agent ?? "").replace(TIER_SUFFIX, "") === "plan") {
|
|
369
|
+
if (checkPlanReadOnly(event, command)) return
|
|
370
|
+
}
|
|
371
|
+
if (checkGit(event, command)) return
|
|
372
|
+
if (checkScratch(event, command)) return
|
|
373
|
+
if (/\bgit\s+push\b/.test(command)) {
|
|
374
|
+
defaultBranch ??= await detectDefaultBranch(root)
|
|
375
|
+
if (await checkPush(event, command, root, defaultBranch)) return
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
} catch (error) {
|
|
379
|
+
// A bug in this guard must never break the session it is guarding.
|
|
380
|
+
console.error(`[harness] guard failed open on ${event.action}: ${error?.message}`)
|
|
381
|
+
}
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
console.error("[pc-system-reminders] guardrails active (V2 permission hooks registered)")
|
|
385
|
+
},
|
|
386
|
+
})
|