@mortz.prk/opencode-auto-mode 1.0.2 → 1.1.1
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/CONTRIBUTING.md +234 -0
- package/README.llm.md +314 -0
- package/README.md +174 -295
- package/dist/gui-experimental.cjs +1 -1
- package/package.json +9 -8
- package/src/audit.ts +130 -49
- package/src/classifier.ts +44 -47
- package/src/code-mode.ts +55 -0
- package/src/config.ts +102 -105
- package/src/debug-events.ts +223 -0
- package/src/decision.ts +72 -0
- package/src/gates.ts +16 -0
- package/src/global-options.ts +85 -0
- package/src/gui-state.ts +230 -121
- package/src/gui.tsx +56 -107
- package/src/index.ts +482 -373
- package/src/mode.ts +237 -0
- package/src/reviewer.ts +70 -94
- package/src/rpc.ts +48 -38
- package/src/score/files.ts +49 -0
- package/src/score/history.ts +141 -0
- package/src/score/index.ts +37 -0
- package/src/score/limits.ts +24 -0
- package/src/score/shell-argv.ts +74 -0
- package/src/score/shell-baseline.ts +113 -0
- package/src/score/shell-safety.ts +54 -0
- package/src/score/shell.ts +30 -0
- package/src/score/types.ts +14 -0
- package/src/score.ts +9 -0
- package/src/shell-policy.ts +672 -30
- package/src/shell.ts +272 -52
- package/src/status.ts +49 -36
- package/src/tiers.ts +285 -19
- package/src/tui.tsx +44 -34
- package/src/user-files.ts +75 -0
- package/src/worktree.ts +30 -0
- package/src/decisions.ts +0 -236
- package/src/logger.ts +0 -26
- package/src/otlp.ts +0 -77
- package/src/policy.ts +0 -36
- package/src/state.ts +0 -135
- package/src/storage.ts +0 -26
- package/src/traces.ts +0 -90
- package/src/transcript.ts +0 -204
- package/src/tui-state.ts +0 -13
package/src/transcript.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
const MAX_USERS = 10;
|
|
2
|
-
const MAX_TOOLS = 20;
|
|
3
|
-
const MAX_USER_CHARS = 8_000;
|
|
4
|
-
const MAX_ENTRY_BYTES = 500;
|
|
5
|
-
const MAX_TRANSCRIPT_BYTES = 12_000;
|
|
6
|
-
|
|
7
|
-
interface ToolEntry {
|
|
8
|
-
name: string;
|
|
9
|
-
input: unknown;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface Entry {
|
|
13
|
-
role: "user" | "tool";
|
|
14
|
-
text: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
18
|
-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
19
|
-
return value as Record<string, unknown>;
|
|
20
|
-
}
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function userText(message: unknown): string | undefined {
|
|
25
|
-
const record = asRecord(message);
|
|
26
|
-
if (record?.["type"] !== "user") return undefined;
|
|
27
|
-
const text = record["text"];
|
|
28
|
-
return typeof text === "string" && text.trim() ? text.trim() : undefined;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function assistantTools(message: unknown): ToolEntry[] {
|
|
32
|
-
const record = asRecord(message);
|
|
33
|
-
if (record?.["type"] !== "assistant") return [];
|
|
34
|
-
const content = record["content"];
|
|
35
|
-
if (!Array.isArray(content)) return [];
|
|
36
|
-
const tools: ToolEntry[] = [];
|
|
37
|
-
for (const part of content) {
|
|
38
|
-
const entry = asRecord(part);
|
|
39
|
-
if (entry?.["type"] !== "tool") continue;
|
|
40
|
-
const name = entry["name"];
|
|
41
|
-
if (typeof name !== "string" || !name) continue;
|
|
42
|
-
tools.push({ name, input: entry["state"] });
|
|
43
|
-
}
|
|
44
|
-
return tools;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function formatTool(tool: ToolEntry): { text: string; truncated: boolean } {
|
|
48
|
-
let detail = "";
|
|
49
|
-
let truncated = false;
|
|
50
|
-
try {
|
|
51
|
-
const state = asRecord(tool.input);
|
|
52
|
-
const input = state?.["input"] ?? tool.input;
|
|
53
|
-
const raw = typeof input === "string" ? input : JSON.stringify(input);
|
|
54
|
-
if (raw) {
|
|
55
|
-
// Keep the tail as well as the head: written payloads and patches often
|
|
56
|
-
// carry their constraints at the end. Clip once, on escaped bytes, so a
|
|
57
|
-
// later UTF-16 length check cannot cut the preserved tail back off.
|
|
58
|
-
detail = clipEscaped(raw, MAX_ENTRY_BYTES);
|
|
59
|
-
truncated = detail !== raw;
|
|
60
|
-
}
|
|
61
|
-
} catch {
|
|
62
|
-
detail = "";
|
|
63
|
-
}
|
|
64
|
-
return { text: detail ? `${tool.name} ${detail}` : tool.name, truncated };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function escapedSize(value: string): number {
|
|
68
|
-
return Buffer.byteLength(JSON.stringify(value));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function prefixByEscapedBytes(characters: string[], maxBytes: number): string {
|
|
72
|
-
let low = 0;
|
|
73
|
-
let high = characters.length;
|
|
74
|
-
while (low < high) {
|
|
75
|
-
const middle = Math.ceil((low + high) / 2);
|
|
76
|
-
if (escapedSize(characters.slice(0, middle).join("")) <= maxBytes) low = middle;
|
|
77
|
-
else high = middle - 1;
|
|
78
|
-
}
|
|
79
|
-
return characters.slice(0, low).join("");
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function suffixByEscapedBytes(characters: string[], maxBytes: number): string {
|
|
83
|
-
let low = 0;
|
|
84
|
-
let high = characters.length;
|
|
85
|
-
while (low < high) {
|
|
86
|
-
const middle = Math.ceil((low + high) / 2);
|
|
87
|
-
if (escapedSize(characters.slice(characters.length - middle).join("")) <= maxBytes) low = middle;
|
|
88
|
-
else high = middle - 1;
|
|
89
|
-
}
|
|
90
|
-
return characters.slice(characters.length - low).join("");
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** Clips the head and tail so escaping cannot hide a trailing constraint. */
|
|
94
|
-
function clipEscaped(value: string, maxBytes: number): string {
|
|
95
|
-
if (escapedSize(value) <= maxBytes) return value;
|
|
96
|
-
const characters = Array.from(value);
|
|
97
|
-
const marker = "…";
|
|
98
|
-
const budget = Math.max(0, maxBytes - escapedSize(marker));
|
|
99
|
-
const headBudget = Math.floor(budget * 0.6);
|
|
100
|
-
return `${prefixByEscapedBytes(characters, headBudget)}${marker}${suffixByEscapedBytes(characters, budget - headBudget)}`;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Clips text to `max` code points, keeping both ends for review evidence. */
|
|
104
|
-
export function clipChars(value: string, max: number): string {
|
|
105
|
-
const characters = Array.from(value);
|
|
106
|
-
// Compare code points, not UTF-16 units, so astral characters cannot make
|
|
107
|
-
// this return a longer string than it received.
|
|
108
|
-
if (characters.length <= max) return value;
|
|
109
|
-
const head = Math.floor(max * 0.6);
|
|
110
|
-
return `${characters.slice(0, head).join("")}…${characters.slice(characters.length - (max - head)).join("")}`;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface TranscriptData {
|
|
114
|
-
transcript: string;
|
|
115
|
-
userEvidence: boolean;
|
|
116
|
-
notes: string[];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function renderTranscript(entries: Entry[], toolTruncated: boolean, userClipped: boolean): { transcript: string; notes: string[] } {
|
|
120
|
-
if (!entries.some((entry) => entry.role === "user")) return { transcript: "(no prior user messages in this session)", notes: [] };
|
|
121
|
-
let users = 0;
|
|
122
|
-
let tools = 0;
|
|
123
|
-
const recent = entries.slice().reverse().filter((entry) => entry.role === "user" ? ++users <= MAX_USERS : ++tools <= MAX_TOOLS).reverse();
|
|
124
|
-
const notes: string[] = [];
|
|
125
|
-
if (recent.length < entries.length) notes.push("older messages omitted");
|
|
126
|
-
if (toolTruncated) notes.push("some tool details truncated");
|
|
127
|
-
if (userClipped) notes.push("latest user message truncated");
|
|
128
|
-
const render = () => `User messages and prior tool calls (chronological):\n${JSON.stringify(recent)}` +
|
|
129
|
-
(notes.length ? `\n… partial session history (${notes.join("; ")})` : "");
|
|
130
|
-
|
|
131
|
-
while (Buffer.byteLength(render()) > MAX_TRANSCRIPT_BYTES && recent.length > 1) {
|
|
132
|
-
// Keep at least the latest user message.
|
|
133
|
-
const onlyOneUser = recent.filter((entry) => entry.role === "user").length <= 1;
|
|
134
|
-
const index = onlyOneUser && recent[0].role === "user" ? recent.findIndex((entry) => entry.role === "tool") : 0;
|
|
135
|
-
if (index < 0) break;
|
|
136
|
-
recent.splice(index, 1);
|
|
137
|
-
if (!notes.includes("older messages omitted")) notes.push("older messages omitted");
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const latestUser = [...recent].reverse().find((entry) => entry.role === "user");
|
|
141
|
-
for (let attempt = 0; latestUser !== undefined && Buffer.byteLength(render()) > MAX_TRANSCRIPT_BYTES && attempt < 10; attempt++) {
|
|
142
|
-
if (!notes.includes("latest user message truncated")) notes.push("latest user message truncated");
|
|
143
|
-
const excess = Buffer.byteLength(render()) - MAX_TRANSCRIPT_BYTES;
|
|
144
|
-
latestUser.text = clipEscaped(latestUser.text, Math.max(128, escapedSize(latestUser.text) - excess - 64));
|
|
145
|
-
}
|
|
146
|
-
return { transcript: render(), notes };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function buildTranscript(messages: unknown[]): TranscriptData {
|
|
150
|
-
const entries: Entry[] = [];
|
|
151
|
-
let userEvidence = false;
|
|
152
|
-
let userClipped = false;
|
|
153
|
-
let toolTruncated = false;
|
|
154
|
-
for (const message of messages) {
|
|
155
|
-
const record = asRecord(message);
|
|
156
|
-
if (record?.type === "user") {
|
|
157
|
-
const text = userText(message);
|
|
158
|
-
// An empty turn (attachments only, synthetic input) says nothing about
|
|
159
|
-
// authorization; it must not invalidate the rest of the session.
|
|
160
|
-
if (text === undefined) continue;
|
|
161
|
-
userEvidence = true;
|
|
162
|
-
const clipped = clipChars(text, MAX_USER_CHARS);
|
|
163
|
-
if (clipped !== text) userClipped = true;
|
|
164
|
-
entries.push({ role: "user", text: clipped });
|
|
165
|
-
}
|
|
166
|
-
for (const tool of assistantTools(message)) {
|
|
167
|
-
const formatted = formatTool(tool);
|
|
168
|
-
if (formatted.truncated) toolTruncated = true;
|
|
169
|
-
entries.push({ role: "tool", text: formatted.text });
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
const rendered = renderTranscript(entries, toolTruncated, userClipped);
|
|
173
|
-
return { transcript: rendered.transcript, userEvidence, notes: rendered.notes };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export interface ReviewContext { transcript: string; complete: boolean; reason?: string; notes?: string[] }
|
|
177
|
-
|
|
178
|
-
export function extractReviewContext(messages: unknown[]): ReviewContext {
|
|
179
|
-
const attachment = messages.some((message) => {
|
|
180
|
-
const record = asRecord(message);
|
|
181
|
-
// Skill messages carry model-facing instructions the text-only reviewer cannot see.
|
|
182
|
-
if (record?.type === "skill") return true;
|
|
183
|
-
if (record?.type !== "user") return false;
|
|
184
|
-
// These attachments carry evidence that the text-only reviewer cannot see.
|
|
185
|
-
return ["files", "skills", "agents"].some((key) =>
|
|
186
|
-
Array.isArray(record[key]) && record[key].length > 0);
|
|
187
|
-
});
|
|
188
|
-
const data = buildTranscript(messages);
|
|
189
|
-
if (attachment)
|
|
190
|
-
return {
|
|
191
|
-
transcript: data.transcript, complete: false, notes: data.notes,
|
|
192
|
-
reason: "User authorization includes attachments that cannot be reviewed automatically",
|
|
193
|
-
};
|
|
194
|
-
if (!data.userEvidence)
|
|
195
|
-
return {
|
|
196
|
-
transcript: data.transcript, complete: false, notes: data.notes,
|
|
197
|
-
reason: "No user authorization message is available",
|
|
198
|
-
};
|
|
199
|
-
return { transcript: data.transcript, complete: true, notes: data.notes };
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function extractTranscript(messages: unknown[]): string {
|
|
203
|
-
return buildTranscript(messages).transcript;
|
|
204
|
-
}
|
package/src/tui-state.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { DecisionRecord } from "./state.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* How long a "reviewing" record keeps the TUI indicator alive. It must exceed
|
|
5
|
-
* the largest configurable review deadline so a slow review stays visible.
|
|
6
|
-
*/
|
|
7
|
-
export const REVIEW_DISPLAY_WINDOW_MS = 180_000;
|
|
8
|
-
|
|
9
|
-
export function isReviewing(records: Record<string, DecisionRecord>, sessionID: string | undefined, now = Date.now()): boolean {
|
|
10
|
-
if (!sessionID) return false;
|
|
11
|
-
return Object.values(records).some((record) => record.sessionID === sessionID &&
|
|
12
|
-
record.decision === "reviewing" && now - record.at >= 0 && now - record.at < REVIEW_DISPLAY_WINDOW_MS);
|
|
13
|
-
}
|