@noppu-labs/opencode-permission-reviewer 2.4.2 → 3.0.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 (46) hide show
  1. package/CHANGELOG.md +44 -1
  2. package/LICENSE +21 -201
  3. package/LICENSE-APACHE +201 -0
  4. package/NOTICE +14 -6
  5. package/README.md +100 -787
  6. package/SECURITY.md +41 -22
  7. package/dist/explain.js +4932 -3522
  8. package/dist/index.d.ts +508 -547
  9. package/dist/index.js +12454 -9030
  10. package/dist/rpc.js +4 -1
  11. package/dist/tui/config/jsonc.ts +71 -63
  12. package/dist/tui/config/layer-read.ts +120 -0
  13. package/dist/tui/config/loader.ts +61 -513
  14. package/dist/tui/config/risk-policy-clamp.ts +80 -0
  15. package/dist/tui/config/trust-boundary-keys.ts +100 -0
  16. package/dist/tui/config/trust-boundary.ts +135 -0
  17. package/dist/tui/config/trusted-degradation.ts +97 -0
  18. package/dist/tui/config-fields.ts +55 -0
  19. package/dist/tui/config-knobs.ts +119 -0
  20. package/dist/tui/config-policy-rules.ts +86 -0
  21. package/dist/tui/config-risk-policy.ts +59 -0
  22. package/dist/tui/config-rule-condition.ts +150 -0
  23. package/dist/tui/config.ts +61 -430
  24. package/dist/tui/descriptor-io.ts +42 -0
  25. package/dist/tui/is-record.ts +13 -0
  26. package/dist/tui/opencode/event-normalizer.ts +41 -31
  27. package/dist/tui/reviewer-model.ts +87 -0
  28. package/dist/tui/tui.tsx +87 -99
  29. package/dist/tui/types.ts +170 -597
  30. package/dist/tui/ui/components.tsx +32 -136
  31. package/dist/tui/ui/review-connection.ts +32 -0
  32. package/dist/tui/ui/review-feed.ts +131 -0
  33. package/dist/tui/ui/review-overlay.tsx +83 -0
  34. package/dist/tui/ui/review-result.tsx +56 -0
  35. package/dist/tui/ui/review-store.ts +88 -0
  36. package/dist/tui/ui/review-theme.ts +63 -0
  37. package/dist/tui/ui/rpc.ts +5 -2
  38. package/dist/tui/ui/v1-host.ts +39 -0
  39. package/dist/tui/ui/v2.tsx +66 -220
  40. package/dist/tui/ui-protocol.ts +63 -118
  41. package/dist/tui/ui-state.ts +71 -64
  42. package/dist/tui/ui-status-parse.ts +74 -0
  43. package/package.json +16 -15
  44. package/rpc.js +2 -1
  45. package/server.js +2 -2
  46. package/tui.tsx +2 -1
@@ -0,0 +1,59 @@
1
+ import type { RiskPolicy, UserAuthorization } from "./types.ts";
2
+
3
+ /** Default risk×authorization matrix. `critical` stays empty: critical risk
4
+ * never auto-allows. */
5
+ export const DEFAULT_RISK_POLICY: RiskPolicy = {
6
+ allow: {
7
+ low: ["high", "medium", "low", "unknown"],
8
+ medium: ["high", "medium", "low"],
9
+ high: ["high", "medium"],
10
+ critical: [],
11
+ },
12
+ minimumConfidence: 0.7,
13
+ onInvalidDecision: "manual",
14
+ onReviewerFailure: "manual",
15
+ };
16
+
17
+ const VALID_AUTH = new Set(["high", "medium", "low", "unknown"]);
18
+
19
+ /** Parse a risk×authorization matrix. Invalid or partial entries fall back to
20
+ * the conservative default. Failure-mode knobs only accept the stricter
21
+ * `"deny"` override; anything else keeps the default `"manual"`. */
22
+ export function resolveRiskPolicy(value: unknown): RiskPolicy {
23
+ if (typeof value !== "object" || value === null)
24
+ return { ...DEFAULT_RISK_POLICY, allow: { ...DEFAULT_RISK_POLICY.allow } };
25
+ const src = value as Record<string, unknown>;
26
+ return {
27
+ allow: resolveAllowMatrix(src.allow),
28
+ minimumConfidence: resolveMinimumConfidence(src.minimumConfidence),
29
+ // Prefer an explicit deny from either the field or a pre-clamped trusted
30
+ // baseline (loader may have already hardened the knob).
31
+ onInvalidDecision:
32
+ src.onInvalidDecision === "deny"
33
+ ? "deny"
34
+ : DEFAULT_RISK_POLICY.onInvalidDecision,
35
+ onReviewerFailure:
36
+ src.onReviewerFailure === "deny"
37
+ ? "deny"
38
+ : DEFAULT_RISK_POLICY.onReviewerFailure,
39
+ };
40
+ }
41
+
42
+ function resolveAllowMatrix(value: unknown): RiskPolicy["allow"] {
43
+ const allowSrc = typeof value === "object" && value !== null ? value : {};
44
+ const allow = { ...DEFAULT_RISK_POLICY.allow };
45
+ for (const risk of ["low", "medium", "high", "critical"] as const) {
46
+ const cell = (allowSrc as Record<string, unknown>)[risk];
47
+ if (!Array.isArray(cell)) continue;
48
+ allow[risk] = cell.filter(
49
+ (a) => typeof a === "string" && VALID_AUTH.has(a),
50
+ ) as UserAuthorization[];
51
+ }
52
+ return allow;
53
+ }
54
+
55
+ function resolveMinimumConfidence(value: unknown): number {
56
+ if (typeof value !== "number" || !Number.isFinite(value))
57
+ return DEFAULT_RISK_POLICY.minimumConfidence;
58
+ return Math.min(1, Math.max(0.5, value));
59
+ }
@@ -0,0 +1,150 @@
1
+ import type {
2
+ ActorProfile,
3
+ CapabilityActionClass,
4
+ PolicyCondition,
5
+ } from "./types.ts";
6
+
7
+ export const ACTOR_PROFILES: ReadonlySet<ActorProfile> = new Set([
8
+ "read-only",
9
+ "validation",
10
+ "workspace",
11
+ "operator",
12
+ "reviewer",
13
+ "unknown",
14
+ ]);
15
+
16
+ /** Every action class the capability analyzer can produce. The `Record`
17
+ * annotation keeps this list in sync with the union type: adding a member to
18
+ * `CapabilityActionClass` without listing it here is a compile error. */
19
+ const ACTION_CLASS_MEMBERS: Record<CapabilityActionClass, true> = {
20
+ "read-only": true,
21
+ "workspace-write": true,
22
+ "temporary-write": true,
23
+ "external-write": true,
24
+ destruction: true,
25
+ "code-execution": true,
26
+ "package-management": true,
27
+ "git-mutation": true,
28
+ network: true,
29
+ "remote-operation": true,
30
+ "service-management": true,
31
+ persistence: true,
32
+ "privilege-escalation": true,
33
+ unknown: true,
34
+ };
35
+
36
+ /** List conditions and the closed set each member must belong to, in the
37
+ * order they are copied into the validated condition. */
38
+ const CONDITION_LISTS: ReadonlyArray<
39
+ readonly [key: string, valid: ReadonlySet<string>]
40
+ > = [
41
+ ["actionClass", new Set(Object.keys(ACTION_CLASS_MEMBERS))],
42
+ ["actorProfile", ACTOR_PROFILES],
43
+ ["repositoryTrust", new Set(["trusted", "untrusted", "unknown"])],
44
+ ];
45
+
46
+ /** The boolean conditions, each backed by one capability fact. */
47
+ export type ConditionFlag = Exclude<
48
+ keyof PolicyCondition,
49
+ "always" | "actionClass" | "actorProfile" | "repositoryTrust"
50
+ >;
51
+
52
+ /** Every flag condition, in the order flags are validated and evaluated. As
53
+ * with `ACTION_CLASS_MEMBERS`, the `Record` annotation makes a flag missing
54
+ * from this list a compile error. */
55
+ const CONDITION_FLAG_MEMBERS: Record<ConditionFlag, true> = {
56
+ writesWorkspace: true,
57
+ writesExternal: true,
58
+ writesTemporary: true,
59
+ deletion: true,
60
+ executesCode: true,
61
+ createsAdHocCode: true,
62
+ packageManagement: true,
63
+ gitMutation: true,
64
+ networkObserved: true,
65
+ credentialRead: true,
66
+ privilegeEscalation: true,
67
+ remoteEnabled: true,
68
+ persistence: true,
69
+ };
70
+
71
+ export const CONDITION_FLAG_KEYS = Object.keys(
72
+ CONDITION_FLAG_MEMBERS,
73
+ ) as readonly ConditionFlag[];
74
+
75
+ const KNOWN_CONDITION_KEYS: ReadonlySet<string> = new Set([
76
+ "always",
77
+ ...CONDITION_LISTS.map(([key]) => key),
78
+ ...CONDITION_FLAG_KEYS,
79
+ ]);
80
+
81
+ /** Validate a policy condition's sub-fields; return null if malformed (so a bad
82
+ * rule is dropped rather than crashing the engine at match time). */
83
+ export function validateCondition(
84
+ value: Record<string, unknown>,
85
+ ): PolicyCondition | null {
86
+ // An unknown key (usually a typo) must drop the rule: silently discarding it
87
+ // could strip the rule's only condition and turn a narrow rule into a
88
+ // universal match.
89
+ if (Object.keys(value).some((key) => !KNOWN_CONDITION_KEYS.has(key)))
90
+ return null;
91
+ // `always` is the explicit catch-all spelling; it makes sense only alone.
92
+ if (value.always !== undefined) {
93
+ if (value.always !== true || Object.keys(value).length !== 1) return null;
94
+ return { always: true };
95
+ }
96
+ const out: Record<string, unknown> = {};
97
+ if (!copyListConditions(value, out) || !copyFlagConditions(value, out))
98
+ return null;
99
+ // A condition that validated to nothing would match every request while
100
+ // LOOKING conditioned; catch-alls must be spelled unambiguously (omit `when`
101
+ // entirely, or `{ always: true }`).
102
+ if (Object.keys(out).length === 0) return null;
103
+ return out as PolicyCondition;
104
+ }
105
+
106
+ /** List conditions are matched with `includes()` at match time, so a member
107
+ * outside the closed set (usually a typo) would never match and would
108
+ * silently disable the rule. Rejecting it drops the rule through the normal
109
+ * path, which degrades trusted configs fail-closed instead of losing a
110
+ * restriction quietly. Empty lists never match either, so they are rejected
111
+ * for the same reason. */
112
+ function copyListConditions(
113
+ value: Record<string, unknown>,
114
+ out: Record<string, unknown>,
115
+ ): boolean {
116
+ for (const [key, valid] of CONDITION_LISTS) {
117
+ if (value[key] === undefined) continue;
118
+ if (!isClosedSetMemberArray(value[key], valid)) return false;
119
+ out[key] = value[key];
120
+ }
121
+ return true;
122
+ }
123
+
124
+ function copyFlagConditions(
125
+ value: Record<string, unknown>,
126
+ out: Record<string, unknown>,
127
+ ): boolean {
128
+ for (const flag of CONDITION_FLAG_KEYS) {
129
+ if (value[flag] === undefined) continue;
130
+ if (typeof value[flag] !== "boolean") return false;
131
+ // Capability facts are `true | "unknown"` — never false — so a `false`
132
+ // condition could never match. Reject it instead of ignoring the filter:
133
+ // an ignored condition widens the rule.
134
+ if (value[flag] === false) return false;
135
+ out[flag] = value[flag];
136
+ }
137
+ return true;
138
+ }
139
+
140
+ /** A non-empty string array whose members all belong to the closed set. */
141
+ function isClosedSetMemberArray(
142
+ value: unknown,
143
+ valid: ReadonlySet<string>,
144
+ ): value is string[] {
145
+ return (
146
+ Array.isArray(value) &&
147
+ value.length > 0 &&
148
+ value.every((v) => typeof v === "string" && valid.has(v))
149
+ );
150
+ }