@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.
- package/CHANGELOG.md +174 -1
- package/LICENSE +21 -201
- package/LICENSE-APACHE +201 -0
- package/MIGRATION.md +2 -1
- package/NOTICE +14 -6
- package/README.md +109 -787
- package/SECURITY.md +41 -22
- package/dist/explain.d.ts +3 -4
- package/dist/explain.js +9708 -4298
- package/dist/index.d.ts +1096 -987
- package/dist/index.js +18646 -10575
- package/dist/rpc.d.ts +38 -38
- package/dist/rpc.js +30 -18
- package/dist/tui/config/jsonc.ts +71 -63
- package/dist/tui/config/layer-read.ts +120 -0
- package/dist/tui/config/loader.ts +73 -512
- package/dist/tui/config/risk-policy-clamp.ts +80 -0
- package/dist/tui/config/trust-boundary-keys.ts +102 -0
- package/dist/tui/config/trust-boundary.ts +148 -0
- package/dist/tui/config/trusted-degradation.ts +97 -0
- package/dist/tui/config-fields.ts +75 -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 +72 -431
- package/dist/tui/descriptor-io.ts +42 -0
- package/dist/tui/failure-kind.ts +28 -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/system-one-routing.ts +110 -0
- package/dist/tui/tui.tsx +97 -99
- package/dist/tui/types.ts +206 -596
- package/dist/tui/ui/action-fit.ts +161 -0
- package/dist/tui/ui/action-spans.tsx +60 -0
- package/dist/tui/ui/attribution.ts +113 -0
- package/dist/tui/ui/components.tsx +69 -137
- package/dist/tui/ui/config-notices.ts +137 -0
- package/dist/tui/ui/fit-parts.ts +76 -0
- package/dist/tui/ui/manual-toast.ts +87 -0
- package/dist/tui/ui/review-connection.ts +32 -0
- package/dist/tui/ui/review-feed.ts +156 -0
- package/dist/tui/ui/review-overlay.tsx +111 -0
- package/dist/tui/ui/review-result.tsx +74 -0
- package/dist/tui/ui/review-store.ts +88 -0
- package/dist/tui/ui/review-theme.ts +70 -0
- package/dist/tui/ui/rpc.ts +5 -2
- package/dist/tui/ui/v1-host.ts +66 -0
- package/dist/tui/ui/v2.tsx +82 -220
- package/dist/tui/ui-protocol.ts +112 -124
- package/dist/tui/ui-state.ts +86 -64
- package/dist/tui/ui-status-extras.ts +165 -0
- package/dist/tui/ui-status-parse.ts +132 -0
- package/dist/tui/ui-text-elide.ts +89 -0
- package/package.json +24 -23
- package/rpc.js +2 -1
- package/server.js +2 -2
- package/tui.tsx +2 -1
package/dist/tui/ui-protocol.ts
CHANGED
|
@@ -1,51 +1,89 @@
|
|
|
1
|
+
import type { HandoffInfo } from "./system-one/backend.ts";
|
|
1
2
|
import type {
|
|
2
3
|
ActorProfile,
|
|
3
4
|
EscalationDisposition,
|
|
5
|
+
ManualResolution,
|
|
4
6
|
PermissionRequest,
|
|
5
7
|
ReviewDecision,
|
|
6
|
-
} from "./types.ts"
|
|
8
|
+
} from "./types.ts";
|
|
9
|
+
import {
|
|
10
|
+
boundedText,
|
|
11
|
+
fitStatus,
|
|
12
|
+
type StatusExtrasOptions,
|
|
13
|
+
statusExtras,
|
|
14
|
+
} from "./ui-status-extras.ts";
|
|
15
|
+
import { parseUiStatus } from "./ui-status-parse.ts";
|
|
16
|
+
import { boundedHeadTail } from "./ui-text-elide.ts";
|
|
7
17
|
|
|
8
|
-
export const UI_COMMAND_PREFIX = "opencode-permission-reviewer.status."
|
|
18
|
+
export const UI_COMMAND_PREFIX = "opencode-permission-reviewer.status.";
|
|
9
19
|
// Time to wait for the server's first "reviewing" ack after permission.asked.
|
|
10
20
|
// Short enough to surface a broken publish path, long enough to survive a busy
|
|
11
21
|
// event bus without flipping the panel to "manual" before the review starts.
|
|
12
|
-
export const UI_START_GRACE_MS = 15_000
|
|
13
|
-
export const UI_WATCHDOG_GRACE_MS = 5_000
|
|
22
|
+
export const UI_START_GRACE_MS = 15_000;
|
|
23
|
+
export const UI_WATCHDOG_GRACE_MS = 5_000;
|
|
14
24
|
|
|
15
|
-
export type ReviewUiPhase =
|
|
25
|
+
export type ReviewUiPhase =
|
|
26
|
+
| "reviewing"
|
|
27
|
+
| "approved"
|
|
28
|
+
| "denied"
|
|
29
|
+
| "manual"
|
|
30
|
+
| "unknown";
|
|
31
|
+
|
|
32
|
+
/** One command of a shell action as the server split it, for the TUI to fit
|
|
33
|
+
* to its width. Built by `src/ui-action-segments.ts`. */
|
|
34
|
+
export interface ActionSegment {
|
|
35
|
+
/** The segment as typed in the command, heredoc bodies left out,
|
|
36
|
+
* whitespace collapsed, at most 200 code points. */
|
|
37
|
+
text: string;
|
|
38
|
+
/** The operator that ends this segment: "|", "||", "&&", ";", "&". */
|
|
39
|
+
separator?: string;
|
|
40
|
+
/** Total body lines of the segment's heredocs, whose bodies are not
|
|
41
|
+
* shown. */
|
|
42
|
+
heredocLines?: number;
|
|
43
|
+
}
|
|
16
44
|
|
|
17
45
|
export interface ReviewUiStatus {
|
|
18
|
-
version: 1
|
|
19
|
-
requestID: string
|
|
20
|
-
sessionID: string
|
|
21
|
-
phase: ReviewUiPhase
|
|
22
|
-
permission: string
|
|
23
|
-
action: string
|
|
24
|
-
model: string
|
|
25
|
-
variant: string
|
|
26
|
-
emittedAt: number
|
|
27
|
-
timeoutMs: number
|
|
28
|
-
reason?: string
|
|
29
|
-
decision?: ReviewDecision
|
|
46
|
+
version: 1;
|
|
47
|
+
requestID: string;
|
|
48
|
+
sessionID: string;
|
|
49
|
+
phase: ReviewUiPhase;
|
|
50
|
+
permission: string;
|
|
51
|
+
action: string;
|
|
52
|
+
model: string;
|
|
53
|
+
variant: string;
|
|
54
|
+
emittedAt: number;
|
|
55
|
+
timeoutMs: number;
|
|
56
|
+
reason?: string;
|
|
57
|
+
decision?: ReviewDecision;
|
|
30
58
|
/**
|
|
31
59
|
* When phase is `denied` and this is `"deny"`, the block came from fail-closed
|
|
32
60
|
* escalation disposition rather than an explicit reviewer/policy deny.
|
|
33
61
|
*/
|
|
34
|
-
escalationDisposition?: EscalationDisposition
|
|
62
|
+
escalationDisposition?: EscalationDisposition;
|
|
35
63
|
/** Resolved actor (absent on the initial "reviewing" phase, before context
|
|
36
64
|
* collection completes). */
|
|
37
|
-
actorName?: string
|
|
38
|
-
actorProfile?: ActorProfile
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
actorName?: string;
|
|
66
|
+
actorProfile?: ActorProfile;
|
|
67
|
+
/** Jev's hand-off to the reasoning reviewer, on the second "reviewing"
|
|
68
|
+
* status and on the result of a handed-off review. */
|
|
69
|
+
handoff?: HandoffInfo;
|
|
70
|
+
/** How a manual outcome came about; chooses the manual-review toast. */
|
|
71
|
+
resolution?: ManualResolution;
|
|
72
|
+
/** The model that made the decision, on a final status a reviewer model
|
|
73
|
+
* decided; absent on a brake, policy or failure-safe result. */
|
|
74
|
+
decidedBy?: string;
|
|
75
|
+
/** The server's config warnings and degraded-config reasons. V1 carries them
|
|
76
|
+
* on every status; V2 reads them from the `status` RPC. */
|
|
77
|
+
configWarnings?: string[];
|
|
78
|
+
configDegraded?: string[];
|
|
79
|
+
/** The action split at its top-level shell operators. Absent when the
|
|
80
|
+
* server did not segment it or left the segments off to fit the status
|
|
81
|
+
* cap, and from a server that predates segments. */
|
|
82
|
+
actionSegments?: ActionSegment[];
|
|
45
83
|
}
|
|
46
84
|
|
|
47
85
|
export function permissionAction(request: PermissionRequest): string {
|
|
48
|
-
const metadata = request.metadata
|
|
86
|
+
const metadata = request.metadata;
|
|
49
87
|
const candidates = [
|
|
50
88
|
metadata.command,
|
|
51
89
|
metadata.filepath,
|
|
@@ -53,30 +91,38 @@ export function permissionAction(request: PermissionRequest): string {
|
|
|
53
91
|
metadata.query,
|
|
54
92
|
metadata.path,
|
|
55
93
|
request.patterns.join(", "),
|
|
56
|
-
]
|
|
94
|
+
];
|
|
57
95
|
const detail = candidates.find(
|
|
58
|
-
(value): value is string =>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
96
|
+
(value): value is string =>
|
|
97
|
+
typeof value === "string" && value.trim().length > 0,
|
|
98
|
+
);
|
|
99
|
+
const prefix = request.permission === "bash" && detail ? "$ " : "";
|
|
100
|
+
const text = `${prefix}${detail ?? request.permission}`;
|
|
101
|
+
// 500 UTF-16 units at most, as the status size budget assumes: 250 code
|
|
102
|
+
// points always fit.
|
|
103
|
+
const bounded = boundedHeadTail(text, 500);
|
|
104
|
+
return bounded.length <= 500 ? bounded : boundedHeadTail(text, 250);
|
|
62
105
|
}
|
|
63
106
|
|
|
107
|
+
export type UiStatusOptions = {
|
|
108
|
+
model: string;
|
|
109
|
+
variant: string;
|
|
110
|
+
timeoutMs: number;
|
|
111
|
+
reason?: string;
|
|
112
|
+
decision?: ReviewDecision;
|
|
113
|
+
escalationDisposition?: EscalationDisposition;
|
|
114
|
+
emittedAt?: number;
|
|
115
|
+
actorName?: string;
|
|
116
|
+
actorProfile?: ActorProfile;
|
|
117
|
+
actionSegments?: ActionSegment[];
|
|
118
|
+
} & StatusExtrasOptions;
|
|
119
|
+
|
|
64
120
|
export function createUiStatus(
|
|
65
121
|
request: PermissionRequest,
|
|
66
122
|
phase: ReviewUiPhase,
|
|
67
|
-
options:
|
|
68
|
-
model: string
|
|
69
|
-
variant: string
|
|
70
|
-
timeoutMs: number
|
|
71
|
-
reason?: string
|
|
72
|
-
decision?: ReviewDecision
|
|
73
|
-
escalationDisposition?: EscalationDisposition
|
|
74
|
-
emittedAt?: number
|
|
75
|
-
actorName?: string
|
|
76
|
-
actorProfile?: ActorProfile
|
|
77
|
-
},
|
|
123
|
+
options: UiStatusOptions,
|
|
78
124
|
): ReviewUiStatus {
|
|
79
|
-
return {
|
|
125
|
+
return fitStatus({
|
|
80
126
|
version: 1,
|
|
81
127
|
requestID: request.id,
|
|
82
128
|
sessionID: request.sessionID,
|
|
@@ -87,97 +133,39 @@ export function createUiStatus(
|
|
|
87
133
|
variant: boundedText(options.variant, 100),
|
|
88
134
|
emittedAt: options.emittedAt ?? Date.now(),
|
|
89
135
|
timeoutMs: options.timeoutMs,
|
|
90
|
-
...(options.reason === undefined
|
|
136
|
+
...(options.reason === undefined
|
|
137
|
+
? {}
|
|
138
|
+
: { reason: boundedText(options.reason, 2_000) }),
|
|
91
139
|
...(options.decision === undefined ? {} : { decision: options.decision }),
|
|
92
140
|
...(options.escalationDisposition === undefined
|
|
93
141
|
? {}
|
|
94
142
|
: { escalationDisposition: options.escalationDisposition }),
|
|
95
|
-
...(options.actorName === undefined
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return (
|
|
107
|
-
(value.outcome === "allow" || value.outcome === "deny" || value.outcome === "escalate") &&
|
|
108
|
-
(value.risk_level === "low" ||
|
|
109
|
-
value.risk_level === "medium" ||
|
|
110
|
-
value.risk_level === "high" ||
|
|
111
|
-
value.risk_level === "critical") &&
|
|
112
|
-
(value.user_authorization === "high" ||
|
|
113
|
-
value.user_authorization === "medium" ||
|
|
114
|
-
value.user_authorization === "low" ||
|
|
115
|
-
value.user_authorization === "unknown") &&
|
|
116
|
-
typeof value.rationale === "string" &&
|
|
117
|
-
typeof value.confidence === "number" &&
|
|
118
|
-
Number.isFinite(value.confidence) &&
|
|
119
|
-
value.confidence >= 0 &&
|
|
120
|
-
value.confidence <= 1
|
|
121
|
-
)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function parseUiStatus(value: unknown): ReviewUiStatus | undefined {
|
|
125
|
-
if (!isRecord(value) || value.version !== 1) return
|
|
126
|
-
if (typeof value.requestID !== "string" || typeof value.sessionID !== "string") return
|
|
127
|
-
if (
|
|
128
|
-
value.phase !== "reviewing" &&
|
|
129
|
-
value.phase !== "approved" &&
|
|
130
|
-
value.phase !== "denied" &&
|
|
131
|
-
value.phase !== "manual" &&
|
|
132
|
-
value.phase !== "unknown"
|
|
133
|
-
) {
|
|
134
|
-
return
|
|
135
|
-
}
|
|
136
|
-
if (typeof value.permission !== "string" || typeof value.action !== "string") return
|
|
137
|
-
if (typeof value.model !== "string" || typeof value.variant !== "string") return
|
|
138
|
-
if (typeof value.emittedAt !== "number" || !Number.isFinite(value.emittedAt)) return
|
|
139
|
-
if (
|
|
140
|
-
typeof value.timeoutMs !== "number" ||
|
|
141
|
-
!Number.isFinite(value.timeoutMs) ||
|
|
142
|
-
value.timeoutMs < 0
|
|
143
|
-
)
|
|
144
|
-
return
|
|
145
|
-
if (value.reason !== undefined && typeof value.reason !== "string") return
|
|
146
|
-
if (value.decision !== undefined && !isDecision(value.decision)) return
|
|
147
|
-
if (
|
|
148
|
-
value.escalationDisposition !== undefined &&
|
|
149
|
-
value.escalationDisposition !== "manual" &&
|
|
150
|
-
value.escalationDisposition !== "deny"
|
|
151
|
-
) {
|
|
152
|
-
return
|
|
153
|
-
}
|
|
154
|
-
if (value.actorName !== undefined && typeof value.actorName !== "string") return
|
|
155
|
-
if (
|
|
156
|
-
value.actorProfile !== undefined &&
|
|
157
|
-
value.actorProfile !== "read-only" &&
|
|
158
|
-
value.actorProfile !== "validation" &&
|
|
159
|
-
value.actorProfile !== "workspace" &&
|
|
160
|
-
value.actorProfile !== "operator" &&
|
|
161
|
-
value.actorProfile !== "reviewer" &&
|
|
162
|
-
value.actorProfile !== "unknown"
|
|
163
|
-
) {
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return value as unknown as ReviewUiStatus
|
|
143
|
+
...(options.actorName === undefined
|
|
144
|
+
? {}
|
|
145
|
+
: { actorName: boundedText(options.actorName, 100) }),
|
|
146
|
+
...(options.actorProfile === undefined
|
|
147
|
+
? {}
|
|
148
|
+
: { actorProfile: options.actorProfile }),
|
|
149
|
+
...(options.actionSegments === undefined
|
|
150
|
+
? {}
|
|
151
|
+
: { actionSegments: options.actionSegments }),
|
|
152
|
+
...statusExtras(options),
|
|
153
|
+
});
|
|
168
154
|
}
|
|
169
155
|
|
|
170
156
|
export function encodeUiStatus(status: ReviewUiStatus): string {
|
|
171
|
-
return `${UI_COMMAND_PREFIX}${Buffer.from(JSON.stringify(status), "utf8").toString("base64url")}
|
|
157
|
+
return `${UI_COMMAND_PREFIX}${Buffer.from(JSON.stringify(status), "utf8").toString("base64url")}`;
|
|
172
158
|
}
|
|
173
159
|
|
|
174
160
|
export function decodeUiStatus(command: string): ReviewUiStatus | undefined {
|
|
175
|
-
if (!command.startsWith(UI_COMMAND_PREFIX)) return
|
|
176
|
-
const encoded = command.slice(UI_COMMAND_PREFIX.length)
|
|
177
|
-
if (!encoded || encoded.length > 16_000) return
|
|
161
|
+
if (!command.startsWith(UI_COMMAND_PREFIX)) return;
|
|
162
|
+
const encoded = command.slice(UI_COMMAND_PREFIX.length);
|
|
163
|
+
if (!encoded || encoded.length > 16_000) return;
|
|
178
164
|
try {
|
|
179
|
-
return parseUiStatus(
|
|
165
|
+
return parseUiStatus(
|
|
166
|
+
JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")),
|
|
167
|
+
);
|
|
180
168
|
} catch {
|
|
181
|
-
return
|
|
169
|
+
return;
|
|
182
170
|
}
|
|
183
171
|
}
|
package/dist/tui/ui-state.ts
CHANGED
|
@@ -1,73 +1,95 @@
|
|
|
1
|
-
import type { PermissionRequest } from "./types.ts"
|
|
1
|
+
import type { PermissionRequest } from "./types.ts";
|
|
2
2
|
import {
|
|
3
|
-
UI_START_GRACE_MS,
|
|
4
|
-
UI_WATCHDOG_GRACE_MS,
|
|
5
3
|
createUiStatus,
|
|
6
4
|
type ReviewUiStatus,
|
|
7
|
-
|
|
5
|
+
UI_START_GRACE_MS,
|
|
6
|
+
UI_WATCHDOG_GRACE_MS,
|
|
7
|
+
} from "./ui-protocol.ts";
|
|
8
8
|
|
|
9
9
|
export interface UiStateOptions {
|
|
10
|
-
model: string
|
|
11
|
-
variant: string
|
|
12
|
-
timeoutMs: number
|
|
10
|
+
model: string;
|
|
11
|
+
variant: string;
|
|
12
|
+
timeoutMs: number;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
const RESULT_DISPLAY_MS = {
|
|
16
16
|
approved: 5_000,
|
|
17
17
|
denied: 5_000,
|
|
18
|
-
} as const
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
/** A "reviewing" status after another keeps the first `emittedAt`, so elapsed
|
|
21
|
+
* time is the total across a hand-off; a plain one after a hand-off, or any
|
|
22
|
+
* "reviewing" status after a final one, is stale and dropped. */
|
|
23
|
+
function successor(
|
|
24
|
+
current: ReviewUiStatus,
|
|
25
|
+
status: ReviewUiStatus,
|
|
26
|
+
): ReviewUiStatus | undefined {
|
|
27
|
+
if (status.phase !== "reviewing") return status;
|
|
28
|
+
if (current.phase !== "reviewing") return;
|
|
29
|
+
if (current.handoff !== undefined && status.handoff === undefined) return;
|
|
30
|
+
return { ...status, emittedAt: current.emittedAt };
|
|
31
|
+
}
|
|
19
32
|
|
|
20
33
|
export class ReviewUiState {
|
|
21
|
-
private readonly requests = new Map<string, ReviewUiStatus>()
|
|
22
|
-
private readonly acknowledged = new Set<string>()
|
|
34
|
+
private readonly requests = new Map<string, ReviewUiStatus>();
|
|
35
|
+
private readonly acknowledged = new Set<string>();
|
|
23
36
|
|
|
24
37
|
constructor(private readonly options: UiStateOptions) {}
|
|
25
38
|
|
|
26
39
|
asked(request: PermissionRequest, now = Date.now()): ReviewUiStatus {
|
|
27
|
-
const existing = this.requests.get(request.id)
|
|
28
|
-
if (existing) return existing
|
|
29
|
-
const status = createUiStatus(request, "reviewing", {
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
const existing = this.requests.get(request.id);
|
|
41
|
+
if (existing) return existing;
|
|
42
|
+
const status = createUiStatus(request, "reviewing", {
|
|
43
|
+
...this.options,
|
|
44
|
+
emittedAt: now,
|
|
45
|
+
});
|
|
46
|
+
this.requests.set(request.id, status);
|
|
47
|
+
return status;
|
|
32
48
|
}
|
|
33
49
|
|
|
34
50
|
apply(status: ReviewUiStatus): boolean {
|
|
35
|
-
const current = this.requests.get(status.requestID)
|
|
51
|
+
const current = this.requests.get(status.requestID);
|
|
36
52
|
if (current && !this.acknowledged.has(status.requestID)) {
|
|
37
53
|
this.requests.set(
|
|
38
54
|
status.requestID,
|
|
39
|
-
status.phase === "reviewing"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
status.phase === "reviewing"
|
|
56
|
+
? { ...status, emittedAt: current.emittedAt }
|
|
57
|
+
: status,
|
|
58
|
+
);
|
|
59
|
+
this.acknowledged.add(status.requestID);
|
|
60
|
+
return true;
|
|
43
61
|
}
|
|
44
|
-
if (current && status.emittedAt < current.emittedAt) return false
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
62
|
+
if (current && status.emittedAt < current.emittedAt) return false;
|
|
63
|
+
const next = current ? successor(current, status) : status;
|
|
64
|
+
if (!next) return false;
|
|
65
|
+
this.requests.set(status.requestID, next);
|
|
66
|
+
this.acknowledged.add(status.requestID);
|
|
67
|
+
return true;
|
|
48
68
|
}
|
|
49
69
|
|
|
50
70
|
replied(requestID: string): void {
|
|
51
|
-
const current = this.requests.get(requestID)
|
|
52
|
-
if (!current || current.phase === "approved" || current.phase === "denied")
|
|
53
|
-
|
|
54
|
-
this.
|
|
71
|
+
const current = this.requests.get(requestID);
|
|
72
|
+
if (!current || current.phase === "approved" || current.phase === "denied")
|
|
73
|
+
return;
|
|
74
|
+
this.requests.delete(requestID);
|
|
75
|
+
this.acknowledged.delete(requestID);
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
remove(requestID: string): void {
|
|
58
|
-
this.requests.delete(requestID)
|
|
59
|
-
this.acknowledged.delete(requestID)
|
|
79
|
+
this.requests.delete(requestID);
|
|
80
|
+
this.acknowledged.delete(requestID);
|
|
60
81
|
}
|
|
61
82
|
|
|
62
83
|
get(requestID: string): ReviewUiStatus | undefined {
|
|
63
|
-
return this.requests.get(requestID)
|
|
84
|
+
return this.requests.get(requestID);
|
|
64
85
|
}
|
|
65
86
|
|
|
66
87
|
all(): ReviewUiStatus[] {
|
|
67
88
|
return [...this.requests.values()].sort(
|
|
68
89
|
(left, right) =>
|
|
69
|
-
left.emittedAt - right.emittedAt ||
|
|
70
|
-
|
|
90
|
+
left.emittedAt - right.emittedAt ||
|
|
91
|
+
left.requestID.localeCompare(right.requestID),
|
|
92
|
+
);
|
|
71
93
|
}
|
|
72
94
|
|
|
73
95
|
activeFor(
|
|
@@ -78,40 +100,40 @@ export class ReviewUiState {
|
|
|
78
100
|
// they hide later reviews. Prefer an in-flight review over a terminal
|
|
79
101
|
// result, and within each class pick the newest request.
|
|
80
102
|
const matches = this.all().filter((status) => {
|
|
81
|
-
if (status.phase === "manual") return false
|
|
82
|
-
if (status.sessionID === sessionID) return true
|
|
83
|
-
let current = parentOf(status.sessionID)
|
|
84
|
-
const visited = new Set<string>()
|
|
103
|
+
if (status.phase === "manual") return false;
|
|
104
|
+
if (status.sessionID === sessionID) return true;
|
|
105
|
+
let current = parentOf(status.sessionID);
|
|
106
|
+
const visited = new Set<string>();
|
|
85
107
|
while (current && !visited.has(current)) {
|
|
86
|
-
if (current === sessionID) return true
|
|
87
|
-
visited.add(current)
|
|
88
|
-
current = parentOf(current)
|
|
108
|
+
if (current === sessionID) return true;
|
|
109
|
+
visited.add(current);
|
|
110
|
+
current = parentOf(current);
|
|
89
111
|
}
|
|
90
|
-
return false
|
|
91
|
-
})
|
|
92
|
-
if (matches.length === 0) return undefined
|
|
112
|
+
return false;
|
|
113
|
+
});
|
|
114
|
+
if (matches.length === 0) return undefined;
|
|
93
115
|
const rank = (phase: ReviewUiStatus["phase"]): number => {
|
|
94
|
-
if (phase === "reviewing") return 2
|
|
95
|
-
if (phase === "approved" || phase === "denied") return 1
|
|
96
|
-
return 0
|
|
97
|
-
}
|
|
116
|
+
if (phase === "reviewing") return 2;
|
|
117
|
+
if (phase === "approved" || phase === "denied") return 1;
|
|
118
|
+
return 0;
|
|
119
|
+
};
|
|
98
120
|
return matches.sort(
|
|
99
121
|
(left, right) =>
|
|
100
122
|
rank(right.phase) - rank(left.phase) ||
|
|
101
123
|
right.emittedAt - left.emittedAt ||
|
|
102
124
|
right.requestID.localeCompare(left.requestID),
|
|
103
|
-
)[0]
|
|
125
|
+
)[0];
|
|
104
126
|
}
|
|
105
127
|
|
|
106
128
|
expire(now = Date.now()): ReviewUiStatus[] {
|
|
107
|
-
const expired: ReviewUiStatus[] = []
|
|
129
|
+
const expired: ReviewUiStatus[] = [];
|
|
108
130
|
for (const [requestID, status] of this.requests) {
|
|
109
|
-
if (status.phase !== "reviewing") continue
|
|
110
|
-
const acknowledged = this.acknowledged.has(requestID)
|
|
131
|
+
if (status.phase !== "reviewing") continue;
|
|
132
|
+
const acknowledged = this.acknowledged.has(requestID);
|
|
111
133
|
const deadline = acknowledged
|
|
112
134
|
? status.emittedAt + status.timeoutMs + UI_WATCHDOG_GRACE_MS
|
|
113
|
-
: status.emittedAt + UI_START_GRACE_MS
|
|
114
|
-
if (now < deadline) continue
|
|
135
|
+
: status.emittedAt + UI_START_GRACE_MS;
|
|
136
|
+
if (now < deadline) continue;
|
|
115
137
|
const unknown: ReviewUiStatus = {
|
|
116
138
|
...status,
|
|
117
139
|
phase: "unknown",
|
|
@@ -119,21 +141,21 @@ export class ReviewUiState {
|
|
|
119
141
|
reason: acknowledged
|
|
120
142
|
? "The reviewer did not return a result within the expected time."
|
|
121
143
|
: "The reviewer did not acknowledge the start of the review.",
|
|
122
|
-
}
|
|
123
|
-
this.requests.set(requestID, unknown)
|
|
124
|
-
expired.push(unknown)
|
|
144
|
+
};
|
|
145
|
+
this.requests.set(requestID, unknown);
|
|
146
|
+
expired.push(unknown);
|
|
125
147
|
}
|
|
126
|
-
return expired
|
|
148
|
+
return expired;
|
|
127
149
|
}
|
|
128
150
|
|
|
129
151
|
dismissResults(now = Date.now()): string[] {
|
|
130
|
-
const removed: string[] = []
|
|
152
|
+
const removed: string[] = [];
|
|
131
153
|
for (const [requestID, status] of this.requests) {
|
|
132
|
-
if (status.phase !== "approved" && status.phase !== "denied") continue
|
|
133
|
-
if (now < status.emittedAt + RESULT_DISPLAY_MS[status.phase]) continue
|
|
134
|
-
this.remove(requestID)
|
|
135
|
-
removed.push(requestID)
|
|
154
|
+
if (status.phase !== "approved" && status.phase !== "denied") continue;
|
|
155
|
+
if (now < status.emittedAt + RESULT_DISPLAY_MS[status.phase]) continue;
|
|
156
|
+
this.remove(requestID);
|
|
157
|
+
removed.push(requestID);
|
|
136
158
|
}
|
|
137
|
-
return removed
|
|
159
|
+
return removed;
|
|
138
160
|
}
|
|
139
161
|
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Bounds the routing and config fields, then fits the whole status to the TUI
|
|
2
|
+
// command cap. Oversized identifiers are not cut, so they can still overflow it.
|
|
3
|
+
import type { HandoffInfo } from "./system-one/backend.ts";
|
|
4
|
+
import type { ManualResolution, ReviewDecision } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
const MAX_LIST_ENTRIES = 5;
|
|
7
|
+
const MAX_ENTRY_CHARS = 200;
|
|
8
|
+
const MAX_MODEL_CHARS = 200;
|
|
9
|
+
const MAX_FAILURE_KIND_CHARS = 40;
|
|
10
|
+
|
|
11
|
+
/** Collapses whitespace and cuts `value` to `max` characters, ending in "…"
|
|
12
|
+
* when it was longer. */
|
|
13
|
+
export function boundedText(value: string, max: number): string {
|
|
14
|
+
const compact = value.replace(/\s+/g, " ").trim();
|
|
15
|
+
if (compact.length <= max) return compact;
|
|
16
|
+
return `${compact.slice(0, Math.max(0, max - 1))}…`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface StatusExtrasOptions {
|
|
20
|
+
handoff?: HandoffInfo;
|
|
21
|
+
resolution?: ManualResolution;
|
|
22
|
+
decidedBy?: string;
|
|
23
|
+
configWarnings?: readonly string[];
|
|
24
|
+
configDegraded?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface StatusExtras {
|
|
28
|
+
handoff?: HandoffInfo;
|
|
29
|
+
resolution?: ManualResolution;
|
|
30
|
+
decidedBy?: string;
|
|
31
|
+
configWarnings?: string[];
|
|
32
|
+
configDegraded?: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function boundedList(values: readonly string[] | undefined): string[] {
|
|
36
|
+
return (values ?? [])
|
|
37
|
+
.slice(0, MAX_LIST_ENTRIES)
|
|
38
|
+
.map((entry) => boundedText(entry, MAX_ENTRY_CHARS))
|
|
39
|
+
.filter((entry) => entry.length > 0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function boundedHandoff(handoff: HandoffInfo): HandoffInfo {
|
|
43
|
+
const { failureKind } = handoff;
|
|
44
|
+
return {
|
|
45
|
+
from: boundedText(handoff.from, MAX_MODEL_CHARS),
|
|
46
|
+
to: boundedText(handoff.to, MAX_MODEL_CHARS),
|
|
47
|
+
trigger: handoff.trigger,
|
|
48
|
+
...(failureKind === undefined
|
|
49
|
+
? {}
|
|
50
|
+
: { failureKind: boundedText(failureKind, MAX_FAILURE_KIND_CHARS) }),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The hand-off, resolution, deciding model and config lists a status
|
|
55
|
+
* carries; an empty list is left out. */
|
|
56
|
+
export function statusExtras(options: StatusExtrasOptions): StatusExtras {
|
|
57
|
+
const configWarnings = boundedList(options.configWarnings);
|
|
58
|
+
const configDegraded = boundedList(options.configDegraded);
|
|
59
|
+
return {
|
|
60
|
+
...(options.handoff === undefined
|
|
61
|
+
? {}
|
|
62
|
+
: { handoff: boundedHandoff(options.handoff) }),
|
|
63
|
+
...(options.resolution === undefined
|
|
64
|
+
? {}
|
|
65
|
+
: { resolution: options.resolution }),
|
|
66
|
+
...(options.decidedBy === undefined
|
|
67
|
+
? {}
|
|
68
|
+
: { decidedBy: boundedText(options.decidedBy, MAX_MODEL_CHARS) }),
|
|
69
|
+
...(configWarnings.length === 0 ? {} : { configWarnings }),
|
|
70
|
+
...(configDegraded.length === 0 ? {} : { configDegraded }),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The JSON byte size whose base64url encoding fits the 16000-character cap
|
|
75
|
+
* `decodeUiStatus` enforces. */
|
|
76
|
+
export const MAX_STATUS_BYTES = 12_000;
|
|
77
|
+
|
|
78
|
+
function withoutLastEntry<T extends StatusExtras>(status: T): T | undefined {
|
|
79
|
+
const key =
|
|
80
|
+
(status.configWarnings?.length ?? 0) > 0
|
|
81
|
+
? "configWarnings"
|
|
82
|
+
: (status.configDegraded?.length ?? 0) > 0
|
|
83
|
+
? "configDegraded"
|
|
84
|
+
: undefined;
|
|
85
|
+
if (key === undefined) return;
|
|
86
|
+
const { [key]: list, ...rest } = status;
|
|
87
|
+
const kept = (list ?? []).slice(0, -1);
|
|
88
|
+
return (kept.length === 0 ? rest : { ...rest, [key]: kept }) as T;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The config lists the fit drops and the texts it shortens. */
|
|
92
|
+
interface FittableStatus extends StatusExtras {
|
|
93
|
+
reason?: string;
|
|
94
|
+
decision?: Pick<ReviewDecision, "rationale" | "script_analysis">;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const FITTABLE_FIELDS = ["rationale", "script_analysis", "reason"] as const;
|
|
98
|
+
type FittableField = (typeof FITTABLE_FIELDS)[number];
|
|
99
|
+
|
|
100
|
+
const ELLIPSIS = "…";
|
|
101
|
+
|
|
102
|
+
function jsonBytes(value: unknown): number {
|
|
103
|
+
return Buffer.byteLength(JSON.stringify(value), "utf8");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Drops config list entries, warnings before degraded reasons and the last
|
|
107
|
+
* first, until the encoded status fits the cap. */
|
|
108
|
+
function fitConfigLists<T extends StatusExtras>(status: T): T {
|
|
109
|
+
let fitted = status;
|
|
110
|
+
while (jsonBytes(fitted) > MAX_STATUS_BYTES) {
|
|
111
|
+
const next = withoutLastEntry(fitted);
|
|
112
|
+
if (next === undefined) break;
|
|
113
|
+
fitted = next;
|
|
114
|
+
}
|
|
115
|
+
return fitted;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Cuts on a code-point boundary so the JSON form, "…" included, is at least
|
|
119
|
+
* `excess` bytes smaller; "…" alone when even the whole text is not enough. */
|
|
120
|
+
function shortenedBy(text: string, excess: number): string {
|
|
121
|
+
if (jsonBytes(text) <= jsonBytes(ELLIPSIS)) return text;
|
|
122
|
+
let room = jsonBytes(text) - excess - jsonBytes(ELLIPSIS);
|
|
123
|
+
let end = 0;
|
|
124
|
+
for (const char of text) {
|
|
125
|
+
room -= jsonBytes(char) - 2;
|
|
126
|
+
if (room < 0) break;
|
|
127
|
+
end += char.length;
|
|
128
|
+
}
|
|
129
|
+
return `${text.slice(0, end)}${ELLIPSIS}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function withShorter<T extends FittableStatus>(
|
|
133
|
+
status: T,
|
|
134
|
+
field: FittableField,
|
|
135
|
+
excess: number,
|
|
136
|
+
): T {
|
|
137
|
+
if (field === "reason") {
|
|
138
|
+
const { reason } = status;
|
|
139
|
+
return reason === undefined
|
|
140
|
+
? status
|
|
141
|
+
: { ...status, reason: shortenedBy(reason, excess) };
|
|
142
|
+
}
|
|
143
|
+
const { decision } = status;
|
|
144
|
+
const text = decision?.[field];
|
|
145
|
+
return decision === undefined || text === undefined
|
|
146
|
+
? status
|
|
147
|
+
: {
|
|
148
|
+
...status,
|
|
149
|
+
decision: { ...decision, [field]: shortenedBy(text, excess) },
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Drops config entries, then shortens fields in FITTABLE_FIELDS order. The
|
|
154
|
+
* TUI shows only reason, and the gate paths copy the rationale into it, so the
|
|
155
|
+
* hidden fields go first. A status under the cap comes back as the same
|
|
156
|
+
* object. */
|
|
157
|
+
export function fitStatus<T extends FittableStatus>(status: T): T {
|
|
158
|
+
let fitted = fitConfigLists(status);
|
|
159
|
+
for (const field of FITTABLE_FIELDS) {
|
|
160
|
+
const excess = jsonBytes(fitted) - MAX_STATUS_BYTES;
|
|
161
|
+
if (excess <= 0) break;
|
|
162
|
+
fitted = withShorter(fitted, field, excess);
|
|
163
|
+
}
|
|
164
|
+
return fitted;
|
|
165
|
+
}
|