@noppu-labs/opencode-permission-reviewer 2.4.1 → 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.
- package/CHANGELOG.md +64 -1
- package/LICENSE +21 -201
- package/LICENSE-APACHE +201 -0
- package/NOTICE +14 -6
- package/README.md +100 -750
- package/SECURITY.md +41 -22
- package/dist/explain.js +4932 -3522
- package/dist/index.d.ts +520 -513
- package/dist/index.js +12595 -9115
- package/dist/rpc.js +4 -1
- package/dist/tui/config/jsonc.ts +71 -63
- package/dist/tui/config/layer-read.ts +120 -0
- package/dist/tui/config/loader.ts +61 -513
- package/dist/tui/config/risk-policy-clamp.ts +80 -0
- package/dist/tui/config/trust-boundary-keys.ts +100 -0
- package/dist/tui/config/trust-boundary.ts +135 -0
- package/dist/tui/config/trusted-degradation.ts +97 -0
- package/dist/tui/config-fields.ts +55 -0
- package/dist/tui/config-knobs.ts +119 -0
- package/dist/tui/config-policy-rules.ts +86 -0
- package/dist/tui/config-risk-policy.ts +59 -0
- package/dist/tui/config-rule-condition.ts +150 -0
- package/dist/tui/config.ts +61 -430
- package/dist/tui/descriptor-io.ts +42 -0
- package/dist/tui/is-record.ts +13 -0
- package/dist/tui/opencode/event-normalizer.ts +41 -31
- package/dist/tui/reviewer-model.ts +87 -0
- package/dist/tui/tui.tsx +87 -99
- package/dist/tui/types.ts +171 -538
- package/dist/tui/ui/components.tsx +32 -136
- package/dist/tui/ui/review-connection.ts +32 -0
- package/dist/tui/ui/review-feed.ts +131 -0
- package/dist/tui/ui/review-overlay.tsx +83 -0
- package/dist/tui/ui/review-result.tsx +56 -0
- package/dist/tui/ui/review-store.ts +88 -0
- package/dist/tui/ui/review-theme.ts +63 -0
- package/dist/tui/ui/rpc.ts +5 -2
- package/dist/tui/ui/v1-host.ts +39 -0
- package/dist/tui/ui/v2.tsx +66 -220
- package/dist/tui/ui-protocol.ts +63 -118
- package/dist/tui/ui-state.ts +71 -64
- package/dist/tui/ui-status-parse.ts +74 -0
- package/package.json +18 -17
- package/rpc.js +2 -1
- package/server.js +2 -2
- package/tui.tsx +2 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { DEFAULT_RISK_POLICY } from "../config-risk-policy.ts";
|
|
2
|
+
import { asObjectLike } from "../is-record.ts";
|
|
3
|
+
|
|
4
|
+
type FailureKnob = "onInvalidDecision" | "onReviewerFailure";
|
|
5
|
+
|
|
6
|
+
/** Ensure project riskPolicy can only tighten the trusted baseline. */
|
|
7
|
+
export function clampRiskPolicy(
|
|
8
|
+
project: Record<string, unknown>,
|
|
9
|
+
trusted: Record<string, unknown>,
|
|
10
|
+
): Record<string, unknown> {
|
|
11
|
+
// Start from trusted so partial project objects cannot wipe failure knobs.
|
|
12
|
+
const out: Record<string, unknown> = {
|
|
13
|
+
...trusted,
|
|
14
|
+
allow: clampAllowMatrix(project.allow, trusted.allow),
|
|
15
|
+
};
|
|
16
|
+
hardenFailureKnob(out, project, trusted, "onInvalidDecision");
|
|
17
|
+
hardenFailureKnob(out, project, trusted, "onReviewerFailure");
|
|
18
|
+
out.minimumConfidence = raisedMinimumConfidence(
|
|
19
|
+
project.minimumConfidence,
|
|
20
|
+
trusted.minimumConfidence,
|
|
21
|
+
);
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function asArray(value: unknown): unknown[] {
|
|
26
|
+
return Array.isArray(value) ? (value as unknown[]) : [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function clampAllowMatrix(
|
|
30
|
+
projectValue: unknown,
|
|
31
|
+
trustedValue: unknown,
|
|
32
|
+
): Record<string, unknown> {
|
|
33
|
+
const projectAllow = asObjectLike(projectValue);
|
|
34
|
+
const trustedAllow = asObjectLike(trustedValue) ?? {};
|
|
35
|
+
const clampedAllow: Record<string, unknown> = {};
|
|
36
|
+
for (const risk of ["low", "medium", "high", "critical"] as const) {
|
|
37
|
+
const trustedCell = asArray(trustedAllow[risk]);
|
|
38
|
+
if (projectAllow === undefined) {
|
|
39
|
+
// Project omitted allow entirely: keep the trusted cells.
|
|
40
|
+
clampedAllow[risk] = trustedCell;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const projectCell = asArray(projectAllow[risk]);
|
|
44
|
+
// Intersection: project can only remove, never add. Missing project cell →
|
|
45
|
+
// empty intersection (most restrictive) when the project provided an allow
|
|
46
|
+
// object at all.
|
|
47
|
+
clampedAllow[risk] = trustedCell.filter((auth) =>
|
|
48
|
+
projectCell.includes(auth),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return clampedAllow;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Failure knobs: project may harden manual → deny only. */
|
|
55
|
+
function hardenFailureKnob(
|
|
56
|
+
out: Record<string, unknown>,
|
|
57
|
+
project: Record<string, unknown>,
|
|
58
|
+
trusted: Record<string, unknown>,
|
|
59
|
+
knob: FailureKnob,
|
|
60
|
+
): void {
|
|
61
|
+
if (project[knob] === "deny" || trusted[knob] === "deny") {
|
|
62
|
+
out[knob] = "deny";
|
|
63
|
+
} else if (project[knob] === "manual" || project[knob] === undefined) {
|
|
64
|
+
out[knob] = trusted[knob] ?? "manual";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** minimumConfidence: project can raise but not lower. */
|
|
69
|
+
function raisedMinimumConfidence(
|
|
70
|
+
projectValue: unknown,
|
|
71
|
+
trustedValue: unknown,
|
|
72
|
+
): number {
|
|
73
|
+
const trustedMin =
|
|
74
|
+
typeof trustedValue === "number" && Number.isFinite(trustedValue)
|
|
75
|
+
? trustedValue
|
|
76
|
+
: DEFAULT_RISK_POLICY.minimumConfidence;
|
|
77
|
+
if (typeof projectValue === "number" && Number.isFinite(projectValue))
|
|
78
|
+
return Math.max(trustedMin, projectValue);
|
|
79
|
+
return trustedMin;
|
|
80
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/** Fields managed by the trust boundary. For these, the boundary's output is
|
|
2
|
+
* final: the project layer may only tighten them against the trusted
|
|
3
|
+
* baseline (which already includes inline), and a later inline value must not
|
|
4
|
+
* undo that tightening. For every other field the documented precedence
|
|
5
|
+
* applies and inline (trusted, most specific) wins over the project. The
|
|
6
|
+
* reviewer resource knobs (timeouts, context budgets) are the strictest
|
|
7
|
+
* case: the project cannot set them at all, in either direction. */
|
|
8
|
+
export const TRUST_BOUNDARY_KEYS: ReadonlySet<string> = new Set([
|
|
9
|
+
"confidenceThreshold",
|
|
10
|
+
"systemOneConfidenceThreshold",
|
|
11
|
+
"systemOneReasoningThreshold",
|
|
12
|
+
"audit",
|
|
13
|
+
"auditPath",
|
|
14
|
+
"model",
|
|
15
|
+
"escalationReviewer",
|
|
16
|
+
"policy",
|
|
17
|
+
"repositoryTrust",
|
|
18
|
+
"actorProfiles",
|
|
19
|
+
"enforcementMode",
|
|
20
|
+
"riskPolicy",
|
|
21
|
+
"escalationMode",
|
|
22
|
+
"policyRules",
|
|
23
|
+
"variant",
|
|
24
|
+
"outputFormat",
|
|
25
|
+
"retainReviewSessions",
|
|
26
|
+
"timeoutMs",
|
|
27
|
+
"reviewBudgetMs",
|
|
28
|
+
"maxContextChars",
|
|
29
|
+
"maxPartChars",
|
|
30
|
+
"maxEnrichmentChars",
|
|
31
|
+
"maxIntentChars",
|
|
32
|
+
"transcriptMessages",
|
|
33
|
+
"intentMessages",
|
|
34
|
+
"historyMessages",
|
|
35
|
+
"maxSessionDepth",
|
|
36
|
+
"maxParentSessions",
|
|
37
|
+
"askDecisions",
|
|
38
|
+
"debug",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/** Keys the project layer can never set, in either direction.
|
|
42
|
+
*
|
|
43
|
+
* - configDegraded describes the state of TRUSTED sources; the project layer
|
|
44
|
+
* has no say in it (injecting fake degradation would only tighten, but the
|
|
45
|
+
* field must stay authoritative for the loader that computes it).
|
|
46
|
+
* - Reviewer resource knobs (timeouts and context budgets): how long a review
|
|
47
|
+
* may run and how much context (transcript, evidence, session chains) it may
|
|
48
|
+
* consume. They are not monotonic security fields: raising them means more
|
|
49
|
+
* conversation sent to the provider, more cost, and longer processing, so a
|
|
50
|
+
* repository may not move them in either direction. Wrong-typed values are
|
|
51
|
+
* dropped with them.
|
|
52
|
+
* - askDecisions: prior user answers are scoped-authorization evidence for the
|
|
53
|
+
* reviewer. Only trusted layers may decide whether the reviewer sees them; a
|
|
54
|
+
* repository can neither strip them nor inject them.
|
|
55
|
+
* - auditPath: only trusted global/inline config may choose the audit
|
|
56
|
+
* destination. A repository must never be able to redirect or silence the
|
|
57
|
+
* audit trail by pointing it at /dev/null or a path it controls.
|
|
58
|
+
* - model, escalationReviewer and policy: the reviewer destinations and the
|
|
59
|
+
* tenant policy text are trusted decisions. A repository must not choose where code/context is sent
|
|
60
|
+
* for review, nor rewrite the policy the reviewer enforces. The reasoning
|
|
61
|
+
* variant, the decision output format, and review-session retention are the
|
|
62
|
+
* same class of decision: a weaker variant, a looser output format, or
|
|
63
|
+
* silenced retention must not be selectable from a repository.
|
|
64
|
+
* - actorProfiles: name→profile mappings are a trust delegation (which agent
|
|
65
|
+
* gets which capability profile). Only trusted global/inline config may
|
|
66
|
+
* grant them; otherwise a repository could promote its own agent to a
|
|
67
|
+
* higher-privilege profile ("build" → "operator"). */
|
|
68
|
+
export const TRUSTED_ONLY_KEYS = [
|
|
69
|
+
"configDegraded",
|
|
70
|
+
"timeoutMs",
|
|
71
|
+
"reviewBudgetMs",
|
|
72
|
+
"maxContextChars",
|
|
73
|
+
"maxPartChars",
|
|
74
|
+
"maxEnrichmentChars",
|
|
75
|
+
"maxIntentChars",
|
|
76
|
+
"transcriptMessages",
|
|
77
|
+
"intentMessages",
|
|
78
|
+
"historyMessages",
|
|
79
|
+
"maxSessionDepth",
|
|
80
|
+
"maxParentSessions",
|
|
81
|
+
"askDecisions",
|
|
82
|
+
"auditPath",
|
|
83
|
+
"model",
|
|
84
|
+
"escalationReviewer",
|
|
85
|
+
"policy",
|
|
86
|
+
"variant",
|
|
87
|
+
"outputFormat",
|
|
88
|
+
"retainReviewSessions",
|
|
89
|
+
"debug",
|
|
90
|
+
"actorProfiles",
|
|
91
|
+
] as const;
|
|
92
|
+
|
|
93
|
+
/** Numeric floors: project config can raise but not lower them. Confidence
|
|
94
|
+
* floors guard the decision gate and are monotonic hardening: a stricter
|
|
95
|
+
* threshold is always safer, so the project may add one. */
|
|
96
|
+
export const CONFIDENCE_FLOOR_KEYS = [
|
|
97
|
+
"confidenceThreshold",
|
|
98
|
+
"systemOneConfidenceThreshold",
|
|
99
|
+
"systemOneReasoningThreshold",
|
|
100
|
+
] as const;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { DEFAULT_CONFIG } from "../config.ts";
|
|
2
|
+
import { DEFAULT_RISK_POLICY } from "../config-risk-policy.ts";
|
|
3
|
+
import { isRecord } from "../is-record.ts";
|
|
4
|
+
import type { PolicyRule } from "../types.ts";
|
|
5
|
+
import { clampRiskPolicy } from "./risk-policy-clamp.ts";
|
|
6
|
+
import {
|
|
7
|
+
CONFIDENCE_FLOOR_KEYS,
|
|
8
|
+
TRUSTED_ONLY_KEYS,
|
|
9
|
+
} from "./trust-boundary-keys.ts";
|
|
10
|
+
|
|
11
|
+
/** Whether the key is present in the layer (a `null`/wrong-type value must be
|
|
12
|
+
* handled as a present-but-invalid override, never silently forwarded). */
|
|
13
|
+
function hasKey(object: Record<string, unknown>, key: string): boolean {
|
|
14
|
+
return Object.hasOwn(object, key);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Merge the trusted baseline with the untrusted project layer. Project config
|
|
18
|
+
* can only TIGHTEN security-sensitive fields, never weaken them. A project
|
|
19
|
+
* value of the wrong type (including `null`) is dropped so it can never fall
|
|
20
|
+
* through to `resolveConfig`'s defaults and reset a trusted restriction. */
|
|
21
|
+
export function mergeWithTrustBoundary(
|
|
22
|
+
trusted: Record<string, unknown>,
|
|
23
|
+
project: Record<string, unknown>,
|
|
24
|
+
): Record<string, unknown> {
|
|
25
|
+
const clamped = { ...project };
|
|
26
|
+
for (const key of TRUSTED_ONLY_KEYS) delete clamped[key];
|
|
27
|
+
clampConfidenceFloors(clamped, trusted);
|
|
28
|
+
// audit: project can enable but not disable.
|
|
29
|
+
if (clamped.audit === false && trusted.audit !== false) delete clamped.audit;
|
|
30
|
+
// repositoryTrust: the project layer may only declare its own repository
|
|
31
|
+
// untrusted; it cannot grant "trusted" or reset a trusted "untrusted".
|
|
32
|
+
if (clamped.repositoryTrust !== "untrusted") delete clamped.repositoryTrust;
|
|
33
|
+
clampEnforcementMode(clamped, trusted);
|
|
34
|
+
clampProjectRiskPolicy(clamped, trusted);
|
|
35
|
+
clampEscalationMode(clamped, trusted);
|
|
36
|
+
combinePolicyRules(clamped, trusted);
|
|
37
|
+
return { ...trusted, ...clamped };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Non-numeric values (including null) are dropped so they cannot reset a
|
|
41
|
+
* trusted floor. */
|
|
42
|
+
function clampConfidenceFloors(
|
|
43
|
+
clamped: Record<string, unknown>,
|
|
44
|
+
trusted: Record<string, unknown>,
|
|
45
|
+
): void {
|
|
46
|
+
for (const key of CONFIDENCE_FLOOR_KEYS) {
|
|
47
|
+
if (!hasKey(clamped, key)) continue;
|
|
48
|
+
const value = clamped[key];
|
|
49
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
50
|
+
delete clamped[key];
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const floor =
|
|
54
|
+
typeof trusted[key] === "number"
|
|
55
|
+
? (trusted[key] as number)
|
|
56
|
+
: DEFAULT_CONFIG[key];
|
|
57
|
+
if (value < floor) clamped[key] = floor;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** enforcementMode: project cannot enable OR disable enforcement — only
|
|
62
|
+
* global/inline can. A project "enforce" is deleted (can't enable), and a
|
|
63
|
+
* project "observe" when the trusted baseline is "enforce" is also deleted
|
|
64
|
+
* (can't downgrade from a global enforcement setting). */
|
|
65
|
+
function clampEnforcementMode(
|
|
66
|
+
clamped: Record<string, unknown>,
|
|
67
|
+
trusted: Record<string, unknown>,
|
|
68
|
+
): void {
|
|
69
|
+
if (clamped.enforcementMode === undefined) return;
|
|
70
|
+
if (
|
|
71
|
+
clamped.enforcementMode === "enforce" ||
|
|
72
|
+
trusted.enforcementMode === "enforce"
|
|
73
|
+
) {
|
|
74
|
+
delete clamped.enforcementMode;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** riskPolicy: project can narrow allow cells and harden failure knobs, never
|
|
79
|
+
* relax a trusted deny or widen an allow cell. A non-object value (including
|
|
80
|
+
* null) is ignored entirely so it cannot wipe the trusted matrix. */
|
|
81
|
+
function clampProjectRiskPolicy(
|
|
82
|
+
clamped: Record<string, unknown>,
|
|
83
|
+
trusted: Record<string, unknown>,
|
|
84
|
+
): void {
|
|
85
|
+
if (!hasKey(clamped, "riskPolicy")) return;
|
|
86
|
+
if (!isRecord(clamped.riskPolicy)) {
|
|
87
|
+
delete clamped.riskPolicy;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const trustedPolicy = isRecord(trusted.riskPolicy)
|
|
91
|
+
? trusted.riskPolicy
|
|
92
|
+
: (DEFAULT_RISK_POLICY as unknown as Record<string, unknown>);
|
|
93
|
+
clamped.riskPolicy = clampRiskPolicy(clamped.riskPolicy, trustedPolicy);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** escalationMode: project can only harden manual → deny, never relax deny →
|
|
97
|
+
* manual. Invalid values are dropped so they cannot override a trusted deny
|
|
98
|
+
* through resolveConfig's "anything but deny → manual" fallback. */
|
|
99
|
+
function clampEscalationMode(
|
|
100
|
+
clamped: Record<string, unknown>,
|
|
101
|
+
trusted: Record<string, unknown>,
|
|
102
|
+
): void {
|
|
103
|
+
const mode = clamped.escalationMode;
|
|
104
|
+
if (mode === undefined) return;
|
|
105
|
+
const invalid = mode !== "manual" && mode !== "deny";
|
|
106
|
+
const relaxes = mode === "manual" && trusted.escalationMode === "deny";
|
|
107
|
+
if (invalid || relaxes) delete clamped.escalationMode;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** policyRules: the project layer ADDS rules (which can only tighten — its
|
|
111
|
+
* allow rules are filtered by the engine), it never erases trusted
|
|
112
|
+
* global/inline deny/manual rules. Combine instead of replace so a repo
|
|
113
|
+
* cannot weaken policy by declaring an empty or narrower rule set. Project
|
|
114
|
+
* rules are also re-tagged source:"project" so they cannot spoof
|
|
115
|
+
* source:"inline" to bypass the project-allow filter. */
|
|
116
|
+
function combinePolicyRules(
|
|
117
|
+
clamped: Record<string, unknown>,
|
|
118
|
+
trusted: Record<string, unknown>,
|
|
119
|
+
): void {
|
|
120
|
+
if (Array.isArray(clamped.policyRules)) {
|
|
121
|
+
const projectRules = (
|
|
122
|
+
clamped.policyRules as Array<Record<string, unknown>>
|
|
123
|
+
).map((rule) => ({
|
|
124
|
+
...rule,
|
|
125
|
+
source: "project" as PolicyRule["source"],
|
|
126
|
+
}));
|
|
127
|
+
const trustedRules = Array.isArray(trusted.policyRules)
|
|
128
|
+
? trusted.policyRules
|
|
129
|
+
: [];
|
|
130
|
+
clamped.policyRules = [...trustedRules, ...projectRules];
|
|
131
|
+
} else if (Array.isArray(trusted.policyRules)) {
|
|
132
|
+
// Project omitted policyRules entirely: preserve the trusted rules.
|
|
133
|
+
clamped.policyRules = trusted.policyRules;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { countInvalidPolicyRules } from "../config-policy-rules.ts";
|
|
2
|
+
import { isValidEscalationReviewer } from "../reviewer-model.ts";
|
|
3
|
+
import type { ConfigLayer } from "./layer-read.ts";
|
|
4
|
+
import type { InlineOptionsTrust } from "./sources.ts";
|
|
5
|
+
|
|
6
|
+
/** Trusted mode fields and the values resolveConfig honors. A mistyped mode
|
|
7
|
+
* silently falls back to the less restrictive default in resolveConfig, so it
|
|
8
|
+
* gets the same fail-closed treatment as a dropped rule: visible degradation
|
|
9
|
+
* instead of a quiet downgrade. */
|
|
10
|
+
const MODE_FIELDS = [
|
|
11
|
+
["enforcementMode", ["enforce", "observe"]],
|
|
12
|
+
["escalationMode", ["manual", "deny"]],
|
|
13
|
+
] as const;
|
|
14
|
+
|
|
15
|
+
function invalidEscalationReviewer(
|
|
16
|
+
raw: Record<string, unknown>,
|
|
17
|
+
layer: "global" | "inline",
|
|
18
|
+
): string[] {
|
|
19
|
+
return raw.escalationReviewer !== undefined &&
|
|
20
|
+
!isValidEscalationReviewer(raw.escalationReviewer)
|
|
21
|
+
? [`${layer} config escalationReviewer is invalid and was ignored`]
|
|
22
|
+
: [];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function invalidModes(
|
|
26
|
+
raw: Record<string, unknown>,
|
|
27
|
+
layer: "global" | "inline",
|
|
28
|
+
): string[] {
|
|
29
|
+
const invalid: string[] = [];
|
|
30
|
+
for (const [key, valid] of MODE_FIELDS) {
|
|
31
|
+
const value = raw[key];
|
|
32
|
+
if (value !== undefined && !(valid as readonly unknown[]).includes(value)) {
|
|
33
|
+
invalid.push(
|
|
34
|
+
`${layer} config ${key} ${JSON.stringify(value)} is invalid and was ignored`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return invalid;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function warnUntilFixed(entries: string[]): string[] {
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
console.warn(
|
|
44
|
+
`permission-reviewer: ${entry}; automatic approval stays disabled until it is fixed`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return entries;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A TRUSTED layer that exists but cannot be honored may have lost the very
|
|
51
|
+
* restrictions it was supposed to carry (deny rules, enforce mode, stricter
|
|
52
|
+
* thresholds). That must degrade the config — automatic approval stays off
|
|
53
|
+
* until the file is fixed — instead of quietly reactivating defaults. Each
|
|
54
|
+
* reason found in a readable global file is also warned about. */
|
|
55
|
+
export function globalLayerDegradation(layer: ConfigLayer): string[] {
|
|
56
|
+
if (layer.status === "malformed")
|
|
57
|
+
return ["global config file is malformed and was ignored"];
|
|
58
|
+
if (layer.status === "read-error")
|
|
59
|
+
return ["global config file exists but could not be read"];
|
|
60
|
+
if (layer.status !== "ok") return [];
|
|
61
|
+
const degraded = warnUntilFixed(
|
|
62
|
+
invalidEscalationReviewer(layer.raw, "global"),
|
|
63
|
+
);
|
|
64
|
+
const invalidRules = countInvalidPolicyRules(layer.raw.policyRules);
|
|
65
|
+
if (invalidRules > 0) {
|
|
66
|
+
degraded.push(
|
|
67
|
+
`${invalidRules} policy rule(s) from the global config were dropped by validation`,
|
|
68
|
+
);
|
|
69
|
+
console.warn(
|
|
70
|
+
`permission-reviewer: ${invalidRules} policy rule(s) in the global config are invalid and were dropped; automatic approval stays disabled until they are fixed`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return [...degraded, ...warnUntilFixed(invalidModes(layer.raw, "global"))];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Same visibility for trusted inline options, without console.warn.
|
|
77
|
+
* resolveConfig still clamps each value to the safe default; this only
|
|
78
|
+
* records that the trusted input was lost. */
|
|
79
|
+
export function inlineDegradation(
|
|
80
|
+
inlineOptions: Record<string, unknown> | undefined,
|
|
81
|
+
inlineTrust: InlineOptionsTrust,
|
|
82
|
+
): string[] {
|
|
83
|
+
if (inlineTrust !== "trusted") return [];
|
|
84
|
+
const invalidRules = countInvalidPolicyRules(inlineOptions?.policyRules);
|
|
85
|
+
const degraded =
|
|
86
|
+
invalidRules > 0
|
|
87
|
+
? [
|
|
88
|
+
`${invalidRules} policy rule(s) from inline config were dropped by validation`,
|
|
89
|
+
]
|
|
90
|
+
: [];
|
|
91
|
+
if (inlineOptions === undefined) return degraded;
|
|
92
|
+
return [
|
|
93
|
+
...degraded,
|
|
94
|
+
...invalidEscalationReviewer(inlineOptions, "inline"),
|
|
95
|
+
...invalidModes(inlineOptions, "inline"),
|
|
96
|
+
];
|
|
97
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ACTOR_PROFILES } from "./config-rule-condition.ts";
|
|
2
|
+
import type { ActorProfile, EscalationMode, RepositoryTrust } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
/** Parse a trusted name→profile mapping. Invalid entries are dropped. */
|
|
5
|
+
export function resolveActorProfiles(
|
|
6
|
+
value: unknown,
|
|
7
|
+
): Record<string, ActorProfile> {
|
|
8
|
+
if (typeof value !== "object" || value === null) return {};
|
|
9
|
+
const out: Record<string, ActorProfile> = {};
|
|
10
|
+
for (const [name, profile] of Object.entries(
|
|
11
|
+
value as Record<string, unknown>,
|
|
12
|
+
)) {
|
|
13
|
+
if (
|
|
14
|
+
typeof profile === "string" &&
|
|
15
|
+
ACTOR_PROFILES.has(profile as ActorProfile)
|
|
16
|
+
) {
|
|
17
|
+
out[name] = profile as ActorProfile;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolveEscalationMode(value: unknown): EscalationMode {
|
|
24
|
+
return value === "deny" ? "deny" : "manual";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function resolveRepositoryTrust(value: unknown): RepositoryTrust {
|
|
28
|
+
if (value === "trusted") return "trusted";
|
|
29
|
+
if (value === "untrusted") return "untrusted";
|
|
30
|
+
return "unknown";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function nonEmptyStringOr(value: unknown, fallback: string): string {
|
|
34
|
+
return typeof value === "string" && value.length > 0 ? value : fallback;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The trimmed string, or undefined when it is not a string or is blank. */
|
|
38
|
+
export function trimmedText(value: unknown): string | undefined {
|
|
39
|
+
return typeof value === "string" && value.trim().length > 0
|
|
40
|
+
? value.trim()
|
|
41
|
+
: undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isStringArray(value: unknown): value is string[] {
|
|
45
|
+
return Array.isArray(value) && value.every((v) => typeof v === "string");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A single-key object for a present value and an empty one for an absent
|
|
49
|
+
* value, so an optional field keeps its place in the output key order. */
|
|
50
|
+
export function optional<K extends string, V>(
|
|
51
|
+
key: K,
|
|
52
|
+
value: V | undefined,
|
|
53
|
+
): Partial<Record<K, V>> {
|
|
54
|
+
return value === undefined ? {} : ({ [key]: value } as Record<K, V>);
|
|
55
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { ReviewerConfig } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
export function boundedInteger(
|
|
4
|
+
value: unknown,
|
|
5
|
+
fallback: number,
|
|
6
|
+
min: number,
|
|
7
|
+
max: number,
|
|
8
|
+
): number {
|
|
9
|
+
if (typeof value !== "number" || !Number.isInteger(value)) return fallback;
|
|
10
|
+
return Math.min(max, Math.max(min, value));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function boundedNumber(
|
|
14
|
+
value: unknown,
|
|
15
|
+
fallback: number,
|
|
16
|
+
min: number,
|
|
17
|
+
max: number,
|
|
18
|
+
): number {
|
|
19
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
|
|
20
|
+
return Math.min(max, Math.max(min, value));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type NumericKey = {
|
|
24
|
+
[K in keyof ReviewerConfig]-?: ReviewerConfig[K] extends number ? K : never;
|
|
25
|
+
}[keyof ReviewerConfig];
|
|
26
|
+
|
|
27
|
+
type KnobBounds<K extends NumericKey> = Readonly<
|
|
28
|
+
Record<K, readonly [min: number, max: number]>
|
|
29
|
+
>;
|
|
30
|
+
|
|
31
|
+
/** The total review deadline: absent when the source omits it, otherwise an
|
|
32
|
+
* integer clamped to [5 s, 15 min], or 3 min when the value is not one. */
|
|
33
|
+
export function reviewBudgetKnob(
|
|
34
|
+
source: Record<string, unknown>,
|
|
35
|
+
): Pick<ReviewerConfig, "reviewBudgetMs"> {
|
|
36
|
+
return source.reviewBudgetMs === undefined
|
|
37
|
+
? {}
|
|
38
|
+
: {
|
|
39
|
+
reviewBudgetMs: boundedInteger(
|
|
40
|
+
source.reviewBudgetMs,
|
|
41
|
+
180_000,
|
|
42
|
+
5_000,
|
|
43
|
+
900_000,
|
|
44
|
+
),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const REVIEW_TIMEOUT: KnobBounds<"timeoutMs"> = {
|
|
49
|
+
timeoutMs: [5_000, 600_000],
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const CONTEXT_BUDGETS: KnobBounds<
|
|
53
|
+
| "maxContextChars"
|
|
54
|
+
| "maxPartChars"
|
|
55
|
+
| "maxEnrichmentChars"
|
|
56
|
+
| "maxIntentChars"
|
|
57
|
+
| "transcriptMessages"
|
|
58
|
+
| "intentMessages"
|
|
59
|
+
| "historyMessages"
|
|
60
|
+
> = {
|
|
61
|
+
maxContextChars: [4_000, 200_000],
|
|
62
|
+
maxPartChars: [500, 50_000],
|
|
63
|
+
maxEnrichmentChars: [1_000, 100_000],
|
|
64
|
+
maxIntentChars: [1_000, 50_000],
|
|
65
|
+
transcriptMessages: [1, 100],
|
|
66
|
+
intentMessages: [1, 50],
|
|
67
|
+
historyMessages: [20, 500],
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const CONFIDENCE_FLOORS: KnobBounds<
|
|
71
|
+
| "confidenceThreshold"
|
|
72
|
+
| "systemOneConfidenceThreshold"
|
|
73
|
+
| "systemOneReasoningThreshold"
|
|
74
|
+
> = {
|
|
75
|
+
confidenceThreshold: [0.5, 1],
|
|
76
|
+
systemOneConfidenceThreshold: [0.3, 1],
|
|
77
|
+
systemOneReasoningThreshold: [0, 1],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const LINEAGE_LIMITS: KnobBounds<
|
|
81
|
+
"maxSessionDepth" | "maxParentSessions"
|
|
82
|
+
> = {
|
|
83
|
+
maxSessionDepth: [1, 32],
|
|
84
|
+
maxParentSessions: [0, 32],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
function clampKnobs<K extends NumericKey>(
|
|
88
|
+
source: Record<string, unknown>,
|
|
89
|
+
bounds: KnobBounds<K>,
|
|
90
|
+
defaults: ReviewerConfig,
|
|
91
|
+
clamp: typeof boundedInteger,
|
|
92
|
+
): Record<K, number> {
|
|
93
|
+
const out = {} as Record<K, number>;
|
|
94
|
+
for (const key of Object.keys(bounds) as K[]) {
|
|
95
|
+
const [min, max] = bounds[key];
|
|
96
|
+
out[key] = clamp(source[key], defaults[key], min, max);
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Each knob in table order, clamped to an integer in its bounds; a missing
|
|
102
|
+
* or non-integer value falls back to its default. */
|
|
103
|
+
export function integerKnobs<K extends NumericKey>(
|
|
104
|
+
source: Record<string, unknown>,
|
|
105
|
+
bounds: KnobBounds<K>,
|
|
106
|
+
defaults: ReviewerConfig,
|
|
107
|
+
): Record<K, number> {
|
|
108
|
+
return clampKnobs(source, bounds, defaults, boundedInteger);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Each knob in table order, clamped to a finite number in its bounds; a
|
|
112
|
+
* missing or non-finite value falls back to its default. */
|
|
113
|
+
export function numberKnobs<K extends NumericKey>(
|
|
114
|
+
source: Record<string, unknown>,
|
|
115
|
+
bounds: KnobBounds<K>,
|
|
116
|
+
defaults: ReviewerConfig,
|
|
117
|
+
): Record<K, number> {
|
|
118
|
+
return clampKnobs(source, bounds, defaults, boundedNumber);
|
|
119
|
+
}
|
|
@@ -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
|
+
}
|