@noppu-labs/opencode-permission-reviewer 2.4.2 → 3.1.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 (59) hide show
  1. package/CHANGELOG.md +174 -1
  2. package/LICENSE +21 -201
  3. package/LICENSE-APACHE +201 -0
  4. package/MIGRATION.md +2 -1
  5. package/NOTICE +14 -6
  6. package/README.md +109 -787
  7. package/SECURITY.md +41 -22
  8. package/dist/explain.d.ts +3 -4
  9. package/dist/explain.js +9708 -4298
  10. package/dist/index.d.ts +1096 -987
  11. package/dist/index.js +18646 -10575
  12. package/dist/rpc.d.ts +38 -38
  13. package/dist/rpc.js +30 -18
  14. package/dist/tui/config/jsonc.ts +71 -63
  15. package/dist/tui/config/layer-read.ts +120 -0
  16. package/dist/tui/config/loader.ts +73 -512
  17. package/dist/tui/config/risk-policy-clamp.ts +80 -0
  18. package/dist/tui/config/trust-boundary-keys.ts +102 -0
  19. package/dist/tui/config/trust-boundary.ts +148 -0
  20. package/dist/tui/config/trusted-degradation.ts +97 -0
  21. package/dist/tui/config-fields.ts +75 -0
  22. package/dist/tui/config-knobs.ts +119 -0
  23. package/dist/tui/config-policy-rules.ts +86 -0
  24. package/dist/tui/config-risk-policy.ts +59 -0
  25. package/dist/tui/config-rule-condition.ts +150 -0
  26. package/dist/tui/config.ts +72 -431
  27. package/dist/tui/descriptor-io.ts +42 -0
  28. package/dist/tui/failure-kind.ts +28 -0
  29. package/dist/tui/is-record.ts +13 -0
  30. package/dist/tui/opencode/event-normalizer.ts +41 -31
  31. package/dist/tui/reviewer-model.ts +87 -0
  32. package/dist/tui/system-one-routing.ts +110 -0
  33. package/dist/tui/tui.tsx +97 -99
  34. package/dist/tui/types.ts +206 -596
  35. package/dist/tui/ui/action-fit.ts +161 -0
  36. package/dist/tui/ui/action-spans.tsx +60 -0
  37. package/dist/tui/ui/attribution.ts +113 -0
  38. package/dist/tui/ui/components.tsx +69 -137
  39. package/dist/tui/ui/config-notices.ts +137 -0
  40. package/dist/tui/ui/fit-parts.ts +76 -0
  41. package/dist/tui/ui/manual-toast.ts +87 -0
  42. package/dist/tui/ui/review-connection.ts +32 -0
  43. package/dist/tui/ui/review-feed.ts +156 -0
  44. package/dist/tui/ui/review-overlay.tsx +111 -0
  45. package/dist/tui/ui/review-result.tsx +74 -0
  46. package/dist/tui/ui/review-store.ts +88 -0
  47. package/dist/tui/ui/review-theme.ts +70 -0
  48. package/dist/tui/ui/rpc.ts +5 -2
  49. package/dist/tui/ui/v1-host.ts +66 -0
  50. package/dist/tui/ui/v2.tsx +82 -220
  51. package/dist/tui/ui-protocol.ts +112 -124
  52. package/dist/tui/ui-state.ts +86 -64
  53. package/dist/tui/ui-status-extras.ts +165 -0
  54. package/dist/tui/ui-status-parse.ts +132 -0
  55. package/dist/tui/ui-text-elide.ts +89 -0
  56. package/package.json +24 -23
  57. package/rpc.js +2 -1
  58. package/server.js +2 -2
  59. package/tui.tsx +2 -1
@@ -0,0 +1,86 @@
1
+ import { validateCondition } from "./config-rule-condition.ts";
2
+ import type { PolicyRule } from "./types.ts";
3
+
4
+ const VALID_EFFECTS: ReadonlySet<string> = new Set([
5
+ "review",
6
+ "manual",
7
+ "deny",
8
+ "allow",
9
+ ]);
10
+ const VALID_SOURCES: ReadonlySet<string> = new Set([
11
+ "builtin",
12
+ "global",
13
+ "project",
14
+ "inline",
15
+ ]);
16
+
17
+ /** The fields every rule needs, already checked against their closed sets. */
18
+ interface RuleFields {
19
+ id: string;
20
+ source: PolicyRule["source"];
21
+ effect: PolicyRule["effect"];
22
+ reason: string;
23
+ when?: unknown;
24
+ }
25
+
26
+ function isNonEmptyString(value: unknown): value is string {
27
+ return typeof value === "string" && value.length > 0;
28
+ }
29
+
30
+ function hasRuleFields(r: Record<string, unknown>): r is RuleFields & {
31
+ [key: string]: unknown;
32
+ } {
33
+ return (
34
+ isNonEmptyString(r.id) &&
35
+ typeof r.source === "string" &&
36
+ VALID_SOURCES.has(r.source) &&
37
+ typeof r.effect === "string" &&
38
+ VALID_EFFECTS.has(r.effect) &&
39
+ isNonEmptyString(r.reason)
40
+ );
41
+ }
42
+
43
+ /** Parse one declarative policy rule. Malformed entries return null so the
44
+ * caller decides whether dropping them is safe (untrusted layer) or must
45
+ * degrade the config (trusted layer — a dropped restriction may be the whole
46
+ * point of the rule). A missing `when` is a valid universal rule. */
47
+ function parsePolicyRule(raw: unknown): PolicyRule | null {
48
+ if (typeof raw !== "object" || raw === null) return null;
49
+ const r = raw as Record<string, unknown>;
50
+ if (!hasRuleFields(r)) return null;
51
+ // No `when` at all: a universal rule. `{ always: true }` is the explicit
52
+ // spelling of the same thing for admins who prefer it.
53
+ if (r.when === undefined)
54
+ return { id: r.id, source: r.source, effect: r.effect, reason: r.reason };
55
+ if (typeof r.when !== "object" || r.when === null) return null;
56
+ const when = validateCondition(r.when as Record<string, unknown>);
57
+ if (when === null) return null;
58
+ return {
59
+ id: r.id,
60
+ source: r.source,
61
+ when,
62
+ effect: r.effect,
63
+ reason: r.reason,
64
+ };
65
+ }
66
+
67
+ /** Parse declarative policy rules. Malformed entries are dropped. */
68
+ export function resolvePolicyRules(value: unknown): PolicyRule[] {
69
+ if (!Array.isArray(value)) return [];
70
+ const out: PolicyRule[] = [];
71
+ for (const raw of value) {
72
+ const rule = parsePolicyRule(raw);
73
+ if (rule !== null) out.push(rule);
74
+ }
75
+ return out;
76
+ }
77
+
78
+ /** How many entries a policyRules array would silently drop under
79
+ * `resolvePolicyRules`. Trusted layers use this to refuse silent degradation:
80
+ * an admin rule that vanishes on a typo must block auto-approval, not just
81
+ * disappear. */
82
+ export function countInvalidPolicyRules(value: unknown): number {
83
+ if (value === undefined) return 0;
84
+ if (!Array.isArray(value)) return 1;
85
+ return value.filter((raw) => parsePolicyRule(raw) === null).length;
86
+ }
@@ -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
+ }