opencode-shell-safety 0.2.2 → 0.3.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/LICENSE +21 -0
- package/README.md +35 -33
- package/package.json +3 -1
- package/src/index.ts +18 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexandru Nedelcu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
# opencode-shell-safety
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
its filesystem policy.
|
|
3
|
+
[](https://www.npmjs.com/package/opencode-shell-safety)
|
|
4
|
+
|
|
5
|
+
An OpenCode v2 plugin that uses [Jev](<https://en.wikipedia.org/wiki/Jev_(AI_model)>) to classify shell permission requests against the active agent definition and its filesystem policy.
|
|
6
|
+
|
|
7
|
+
This project is part of [alexandru/agents-config](https://github.com/alexandru/agents-config). See it in use in my [OpenCode configuration](https://github.com/alexandru/opencode-config).
|
|
6
8
|
|
|
7
9
|
## Configuration
|
|
8
10
|
|
|
@@ -21,12 +23,12 @@ its filesystem policy.
|
|
|
21
23
|
"integration": "opencode",
|
|
22
24
|
"agents": {
|
|
23
25
|
"Explorer": {
|
|
24
|
-
"enabled": true
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
]
|
|
26
|
+
"enabled": true,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
],
|
|
30
32
|
}
|
|
31
33
|
```
|
|
32
34
|
|
|
@@ -51,35 +53,35 @@ Get an API key from [TypeSafe AI](https://docs.typesafe.ai/introduction/quicksta
|
|
|
51
53
|
"apiKeyEnv": "TYPESAFE_API_KEY",
|
|
52
54
|
"agents": {
|
|
53
55
|
"Explorer": {
|
|
54
|
-
"enabled": true
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
]
|
|
56
|
+
"enabled": true,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
],
|
|
60
62
|
}
|
|
61
63
|
```
|
|
62
64
|
|
|
63
65
|
## Options
|
|
64
66
|
|
|
65
|
-
| Option
|
|
66
|
-
|
|
|
67
|
-
| `endpoint`
|
|
68
|
-
| `model`
|
|
69
|
-
| `integration`
|
|
70
|
-
| `apiKeyEnv`
|
|
71
|
-
| `allowProbability`
|
|
72
|
-
| `violationProbability`
|
|
73
|
-
| `timeoutMs`
|
|
74
|
-
| `maxAttempts`
|
|
75
|
-
| `retryDelayMs`
|
|
76
|
-
| `cache.capacity`
|
|
77
|
-
| `cache.ttlMs`
|
|
78
|
-
| `agents.<name>.enabled`
|
|
79
|
-
| `agents.<name>.thresholds`
|
|
80
|
-
| `agents.<name>.http`
|
|
81
|
-
| `agents.<name>.http.methods`
|
|
82
|
-
| `agents.<name>.http.credentials` | Environment credential names mapped to allowed HTTPS hosts.
|
|
67
|
+
| Option | Description |
|
|
68
|
+
| -------------------------------- | ------------------------------------------------------------------------------------------------------ |
|
|
69
|
+
| `endpoint` | SystemOne HTTP endpoint. |
|
|
70
|
+
| `model` | Model sent in each classification request. |
|
|
71
|
+
| `integration` | OpenCode integration whose credential takes precedence over `apiKeyEnv`. |
|
|
72
|
+
| `apiKeyEnv` | Environment variable to read if no integration credential is available. |
|
|
73
|
+
| `allowProbability` | Minimum `withinPolicy` probability required to allow a command. |
|
|
74
|
+
| `violationProbability` | Probability at or above which a filesystem, remote-mutation, or credential violation denies a command. |
|
|
75
|
+
| `timeoutMs` | Timeout for one request attempt. |
|
|
76
|
+
| `maxAttempts` | Maximum attempts for network, timeout, rate-limit, and server failures. |
|
|
77
|
+
| `retryDelayMs` | Delay between retry attempts. |
|
|
78
|
+
| `cache.capacity` | Maximum number of cached classification results. |
|
|
79
|
+
| `cache.ttlMs` | Successful classification cache lifetime in milliseconds. |
|
|
80
|
+
| `agents.<name>.enabled` | Enables classification for an agent. |
|
|
81
|
+
| `agents.<name>.thresholds` | Optional per-agent probability thresholds. |
|
|
82
|
+
| `agents.<name>.http` | Optional. Omission permits no HTTP methods or credential hosts. |
|
|
83
|
+
| `agents.<name>.http.methods` | HTTP methods the policy permits. |
|
|
84
|
+
| `agents.<name>.http.credentials` | Environment credential names mapped to allowed HTTPS hosts. |
|
|
83
85
|
|
|
84
86
|
Set at least one of `integration` or `apiKeyEnv`.
|
|
85
87
|
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-shell-safety",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "OpenCode plugin that classifies shell permission requests against agent policy.",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": "./src/index.ts"
|
|
8
9
|
},
|
|
9
10
|
"files": [
|
|
11
|
+
"LICENSE",
|
|
10
12
|
"src",
|
|
11
13
|
"README.md"
|
|
12
14
|
],
|
package/src/index.ts
CHANGED
|
@@ -161,10 +161,12 @@ export const resolveAgentDefinition = (agent: Agent.Info): ResolvedAgentDefiniti
|
|
|
161
161
|
.map((rule) => ({ resource: rule.resource, effect: rule.effect })),
|
|
162
162
|
})
|
|
163
163
|
|
|
164
|
+
export type ClassificationVerdict = "allow" | "deny"
|
|
165
|
+
|
|
164
166
|
export const permissionFromAssessment = (
|
|
165
167
|
assessment: SafetyAssessment,
|
|
166
168
|
thresholds: Thresholds,
|
|
167
|
-
):
|
|
169
|
+
): ClassificationVerdict => {
|
|
168
170
|
const hasViolation =
|
|
169
171
|
assessment.filesystemViolation.noul >= thresholds.violationProbability ||
|
|
170
172
|
assessment.remoteMutation.noul >= thresholds.violationProbability ||
|
|
@@ -172,6 +174,9 @@ export const permissionFromAssessment = (
|
|
|
172
174
|
return assessment.withinPolicy.noul >= thresholds.allowProbability && !hasViolation ? "allow" : "deny"
|
|
173
175
|
}
|
|
174
176
|
|
|
177
|
+
export const fallbackEffect = (configured: PermissionEffect): PermissionEffect =>
|
|
178
|
+
configured === "allow" ? "deny" : configured
|
|
179
|
+
|
|
175
180
|
const noulQuestion = (instructions: string, yes: string, no: string): NoulQuestion => ({
|
|
176
181
|
type: "noul",
|
|
177
182
|
instructions,
|
|
@@ -221,7 +226,6 @@ export const buildRequest = (
|
|
|
221
226
|
|
|
|
222
227
|
|Rules:
|
|
223
228
|
|- Decide whether "shellCommand" complies with those instructions and with "state.policy".
|
|
224
|
-
|- OpenCode invokes this classifier only after permission resolution returns "ask".
|
|
225
229
|
|- An "ask" rule means this classifier must decide from the command's actual effects; it is not a violation.
|
|
226
230
|
|- Shell deny rules remain final.
|
|
227
231
|
|- Apply ordered permission rules with the last matching rule winning.
|
|
@@ -387,16 +391,17 @@ const findPolicy = (options: Options, agent: string): AgentPolicy | undefined =>
|
|
|
387
391
|
const hasShellControlSyntax = (resources: ReadonlyArray<string>): boolean =>
|
|
388
392
|
resources.some((resource) => /[;&|<>`$]/.test(resource))
|
|
389
393
|
|
|
390
|
-
const summary = (assessment: SafetyAssessment, effect: PermissionEffect): string => {
|
|
394
|
+
const summary = (assessment: SafetyAssessment, verdict: ClassificationVerdict, effect: PermissionEffect): string => {
|
|
391
395
|
const classification =
|
|
392
|
-
`Jev shell classification: ${
|
|
396
|
+
`Jev shell classification: ${verdict} (` +
|
|
393
397
|
`withinPolicy=${assessment.withinPolicy.noul.toFixed(3)}, ` +
|
|
394
398
|
`filesystemViolation=${assessment.filesystemViolation.noul.toFixed(3)}, ` +
|
|
395
399
|
`remoteMutation=${assessment.remoteMutation.noul.toFixed(3)}, ` +
|
|
396
400
|
`credentialViolation=${assessment.credentialViolation.noul.toFixed(3)})`
|
|
401
|
+
if (verdict === "allow") return classification
|
|
397
402
|
return effect === "deny"
|
|
398
403
|
? `${classification}. Rewrite the command to comply with your system prompt and permissions, or report the limitation if no compliant form exists.`
|
|
399
|
-
: classification
|
|
404
|
+
: `${classification}. Jev rejected this command.`
|
|
400
405
|
}
|
|
401
406
|
|
|
402
407
|
const classificationSucceeded = (assessment: SafetyAssessment): ClassificationResult => ({ ok: true, assessment })
|
|
@@ -443,19 +448,22 @@ export const createPermissionEvaluator = (
|
|
|
443
448
|
const policy = findPolicy(options, agent)
|
|
444
449
|
if (!policy?.enabled) return Effect.void
|
|
445
450
|
const classifiedEvent = { ...event, agent }
|
|
451
|
+
const fallback = fallbackEffect(event.effect)
|
|
446
452
|
|
|
447
453
|
return Effect.gen(function* () {
|
|
448
|
-
event.effect = "deny"
|
|
449
454
|
if (!projectDirectory) {
|
|
455
|
+
event.effect = fallback
|
|
450
456
|
event.message = "Jev could not classify this command because the session directory is unavailable."
|
|
451
457
|
return
|
|
452
458
|
}
|
|
453
459
|
if (!assessmentCache) {
|
|
460
|
+
event.effect = fallback
|
|
454
461
|
event.message = "Jev could not classify this command because no OpenCode Zen credential is available."
|
|
455
462
|
return
|
|
456
463
|
}
|
|
457
464
|
const agentDefinition = yield* resolveAgentDefinition(agent)
|
|
458
465
|
if (!agentDefinition) {
|
|
466
|
+
event.effect = fallback
|
|
459
467
|
event.message = `Jev could not classify this command because the resolved ${agent} definition is unavailable.`
|
|
460
468
|
return
|
|
461
469
|
}
|
|
@@ -468,12 +476,14 @@ export const createPermissionEvaluator = (
|
|
|
468
476
|
Effect.catch((error) => Effect.succeed(classificationFailed(error))),
|
|
469
477
|
)
|
|
470
478
|
if (!result.ok) {
|
|
479
|
+
event.effect = fallback
|
|
471
480
|
event.message = `Jev could not classify this command (${requestFailure(result.error, options)}).`
|
|
472
481
|
return
|
|
473
482
|
}
|
|
474
483
|
|
|
475
|
-
|
|
476
|
-
event.
|
|
484
|
+
const verdict = permissionFromAssessment(result.assessment, policy.thresholds ?? options)
|
|
485
|
+
event.effect = verdict === "allow" ? "allow" : fallback
|
|
486
|
+
event.message = summary(result.assessment, verdict, event.effect)
|
|
477
487
|
})
|
|
478
488
|
}
|
|
479
489
|
})
|