opencode-shell-safety 0.2.3 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +18 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-shell-safety",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "OpenCode plugin that classifies shell permission requests against agent policy.",
5
5
  "license": "MIT",
6
6
  "type": "module",
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
- ): PermissionEffect => {
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: ${effect} (` +
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
- event.effect = permissionFromAssessment(result.assessment, policy.thresholds ?? options)
476
- event.message = summary(result.assessment, event.effect)
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
  })