robisie 0.1.0 → 0.1.2
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 +79 -0
- package/README.md +109 -14
- package/dist/active-recovery.d.ts +58 -0
- package/dist/active-recovery.js +143 -0
- package/dist/agents.d.ts +34 -0
- package/dist/agents.js +77 -16
- package/dist/attempt-identity.d.ts +21 -0
- package/dist/attempt-identity.js +39 -0
- package/dist/ci-failure-result.d.ts +23 -0
- package/dist/ci-failure-result.js +90 -0
- package/dist/claude-headless.d.ts +7 -0
- package/dist/claude-headless.js +28 -0
- package/dist/cli.d.ts +17 -0
- package/dist/cli.js +54 -14
- package/dist/commands.d.ts +18 -0
- package/dist/commands.js +195 -88
- package/dist/constants.d.ts +32 -0
- package/dist/constants.js +3 -3
- package/dist/context.d.ts +24 -0
- package/dist/context.js +22 -6
- package/dist/dependency-readiness.d.ts +13 -0
- package/dist/dependency-readiness.js +80 -0
- package/dist/dispatch-scope.d.ts +18 -0
- package/dist/dispatch-scope.js +46 -0
- package/dist/done-evidence.d.ts +31 -0
- package/dist/done-evidence.js +216 -0
- package/dist/engine-config.d.ts +65 -0
- package/dist/engine-config.js +139 -0
- package/dist/fsops.d.ts +87 -0
- package/dist/fsops.js +117 -24
- package/dist/helpers.d.ts +15 -0
- package/dist/helpers.js +268 -45
- package/dist/intent-rules/admission.d.ts +10 -0
- package/dist/intent-rules/admission.js +35 -0
- package/dist/intent-rules/done-when-roles.d.ts +21 -0
- package/dist/intent-rules/done-when-roles.js +101 -0
- package/dist/intent-rules/hooks.d.ts +29 -0
- package/dist/intent-rules/hooks.js +23 -0
- package/dist/k3-codex-role.d.ts +20 -0
- package/dist/k3-codex-role.js +68 -0
- package/dist/manifest.d.ts +41 -0
- package/dist/manifest.js +59 -14
- package/dist/mcp.d.ts +18 -0
- package/dist/mcp.js +117 -40
- package/dist/opencode-result.d.ts +19 -0
- package/dist/opencode-result.js +136 -0
- package/dist/opencode-review-guard.d.ts +1 -0
- package/dist/opencode-review-guard.js +19 -0
- package/dist/opencode-session-export.d.ts +5 -0
- package/dist/opencode-session-export.js +28 -0
- package/dist/outcome-moves.d.ts +15 -0
- package/dist/outcome-moves.js +39 -0
- package/dist/planer-client.d.ts +214 -0
- package/dist/planer-client.js +351 -0
- package/dist/restart-recovery.d.ts +47 -0
- package/dist/restart-recovery.js +70 -0
- package/dist/review-source-packet.d.ts +17 -0
- package/dist/review-source-packet.js +44 -0
- package/dist/role-registry.d.ts +25 -0
- package/dist/role-registry.js +91 -0
- package/dist/role-results.d.ts +15 -0
- package/dist/role-results.js +42 -0
- package/dist/roles.d.ts +30 -0
- package/dist/roles.js +20 -16
- package/dist/seed-validation.d.ts +28 -0
- package/dist/seed-validation.js +294 -0
- package/dist/selection-result.d.ts +44 -0
- package/dist/selection-result.js +144 -0
- package/dist/sparse-policy.d.ts +8 -0
- package/dist/sparse-policy.js +49 -0
- package/dist/tester-identity.d.ts +11 -0
- package/dist/tester-identity.js +53 -0
- package/dist/worker-env.d.ts +5 -0
- package/dist/worker-env.js +34 -0
- package/docs/ENGINE-API.md +88 -0
- package/docs/ENGINE-CONFIG.md +113 -0
- package/docs/README.en.md +142 -20
- package/docs/README.md +144 -20
- package/docs/RELEASE-0.1.0.md +81 -8
- package/docs/RELEASE-0.1.1.md +110 -0
- package/helpers/ceo-client-launcher-mcp.mjs +36 -8
- package/helpers/client-spawn-session.sh +181 -0
- package/helpers/client-spawn.sh +825 -0
- package/package.json +9 -4
- package/roles/ceo-client.md +3 -2
- package/roles/manager-builder-client.md +18 -4
- package/roles/quality-officer-client.md +20 -6
package/dist/mcp.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { CONTRACT_VERSION_HEADER, PACKAGE_NAME, PACKAGE_VERSION, SUPPORTED_CONTRACT_MAJOR, mcpUrl, wellKnownUrl } from "./constants.js";
|
|
2
|
-
import { RobisieError
|
|
2
|
+
import { RobisieError } from "./context.js";
|
|
3
3
|
const TIMEOUT_MS = 15_000;
|
|
4
|
-
const
|
|
4
|
+
const RESPONSE_LIMIT_BYTES = 4_194_304;
|
|
5
|
+
const responseLimitErrors = new WeakSet();
|
|
6
|
+
const UPDATE_STEP = "Zainstaluj z npm aktualną paczkę zgodną z kontraktem serwera; w katalogu instalacji wykonaj `npm i robisie@latest`, potem lokalne `npx robisie init` i `npx robisie doctor`.";
|
|
5
7
|
/** Major of a semver string, or null when it is not one. */
|
|
6
8
|
export function majorOf(version) {
|
|
7
9
|
const match = /^(\d+)\.\d+\.\d+/.exec(version.trim());
|
|
@@ -15,15 +17,15 @@ export function assertCompatible(serverVersion) {
|
|
|
15
17
|
const major = majorOf(serverVersion);
|
|
16
18
|
if (major === SUPPORTED_CONTRACT_MAJOR)
|
|
17
19
|
return;
|
|
18
|
-
const
|
|
19
|
-
throw new RobisieError(`Wersja kontraktu serwera (${serverVersion}) nie pasuje do paczki ${PACKAGE_NAME} ${PACKAGE_VERSION} (obsługuje ${SUPPORTED_CONTRACT_MAJOR}.x).`,
|
|
20
|
+
const older = major !== null && major < SUPPORTED_CONTRACT_MAJOR;
|
|
21
|
+
throw new RobisieError(`Wersja kontraktu serwera (${serverVersion}) nie pasuje do paczki ${PACKAGE_NAME} ${PACKAGE_VERSION} (obsługuje ${SUPPORTED_CONTRACT_MAJOR}.x).`, older ? "Serwer jest starszy niż paczka. Zainstaluj z npm wydanie zgodne z kontraktem serwera: `npm i robisie@<wersja-zgodna-z-kontraktem-serwera>` (zastąp placeholder numerem zgodnego wydania), potem lokalne `npx robisie init` i `npx robisie doctor`." : UPDATE_STEP);
|
|
20
22
|
}
|
|
21
23
|
async function request(ctx, url, init) {
|
|
22
24
|
try {
|
|
23
|
-
return await ctx.fetch(url, { ...init, signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
25
|
+
return await ctx.fetch(url, { ...init, redirect: "error", signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
24
26
|
}
|
|
25
|
-
catch
|
|
26
|
-
throw new RobisieError(`Brak połączenia z ${new URL(url).host}
|
|
27
|
+
catch {
|
|
28
|
+
throw new RobisieError(`Brak połączenia z ${new URL(url).host}.`, "Sprawdź internet (albo proxy/firewall) i spróbuj ponownie.");
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
/** Public, unauthenticated: the contract version the server announces in /.well-known/mcp.json. */
|
|
@@ -33,45 +35,101 @@ export async function fetchContractVersion(ctx) {
|
|
|
33
35
|
throw new RobisieError(`Serwer robisie odpowiedział błędem ${res.status} przy sprawdzaniu wersji.`, "Spróbuj ponownie za kilka minut; jeśli błąd zostaje, napisz do robisie.");
|
|
34
36
|
}
|
|
35
37
|
try {
|
|
36
|
-
const body = await res
|
|
38
|
+
const body = JSON.parse(await readText(res));
|
|
37
39
|
const version = typeof body === "object" && body !== null ? body.contractVersion : undefined;
|
|
38
40
|
return typeof version === "string" ? version : null;
|
|
39
41
|
}
|
|
40
|
-
catch {
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error instanceof RobisieError && responseLimitErrors.has(error))
|
|
44
|
+
throw error;
|
|
41
45
|
return null;
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
48
|
+
function isRecord(value) {
|
|
49
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
function nonEmptyString(value) {
|
|
52
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
53
|
+
}
|
|
54
|
+
function formatError() {
|
|
55
|
+
return new RobisieError("Serwer robisie odpowiedział w nieznanym formacie.", UPDATE_STEP);
|
|
56
|
+
}
|
|
57
|
+
/** Count actual bytes before decoding, including SSE frames already discarded by the parser. */
|
|
58
|
+
async function* readChunks(res) {
|
|
59
|
+
if (!res.body)
|
|
60
|
+
return;
|
|
61
|
+
const reader = res.body.getReader();
|
|
62
|
+
const decoder = new TextDecoder();
|
|
63
|
+
let bytes = 0;
|
|
64
|
+
try {
|
|
65
|
+
while (true) {
|
|
66
|
+
const { done, value } = await reader.read();
|
|
67
|
+
if (done) {
|
|
68
|
+
yield decoder.decode();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
bytes += value.byteLength;
|
|
72
|
+
if (bytes > RESPONSE_LIMIT_BYTES) {
|
|
73
|
+
const error = new RobisieError("Odpowiedź serwera robisie jest za duża (limit 4 MiB).", "Poproś administratora serwera o zmniejszenie odpowiedzi MCP do 4 MiB, potem uruchom ponownie `npx robisie doctor`.");
|
|
74
|
+
responseLimitErrors.add(error);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
yield decoder.decode(value, { stream: true });
|
|
57
78
|
}
|
|
58
79
|
}
|
|
59
|
-
|
|
60
|
-
|
|
80
|
+
finally {
|
|
81
|
+
// Cancellation may never settle; release ownership without delaying the result.
|
|
82
|
+
void reader.cancel().catch(() => { });
|
|
83
|
+
reader.releaseLock();
|
|
61
84
|
}
|
|
62
|
-
|
|
85
|
+
}
|
|
86
|
+
async function readText(res) {
|
|
87
|
+
let text = "";
|
|
88
|
+
for await (const chunk of readChunks(res))
|
|
89
|
+
text += chunk;
|
|
90
|
+
return text;
|
|
91
|
+
}
|
|
92
|
+
/** Streamable HTTP answers either plain JSON or an SSE stream; pick our response out of either. */
|
|
93
|
+
async function readRpc(res, id) {
|
|
94
|
+
const matching = (candidate) => {
|
|
63
95
|
try {
|
|
64
96
|
const parsed = JSON.parse(candidate);
|
|
65
97
|
for (const message of Array.isArray(parsed) ? parsed : [parsed]) {
|
|
66
|
-
if (
|
|
98
|
+
if (isRecord(message) && message.id === id)
|
|
67
99
|
return message;
|
|
68
100
|
}
|
|
69
101
|
}
|
|
70
102
|
catch {
|
|
71
103
|
// not JSON — skip this event
|
|
72
104
|
}
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
if (!(res.headers.get("content-type") ?? "").includes("text/event-stream"))
|
|
109
|
+
return matching(await readText(res));
|
|
110
|
+
let buffer = "";
|
|
111
|
+
for await (const chunk of readChunks(res)) {
|
|
112
|
+
buffer += chunk;
|
|
113
|
+
let boundary;
|
|
114
|
+
while ((boundary = /\r?\n\r?\n/.exec(buffer)) !== null) {
|
|
115
|
+
const event = buffer.slice(0, boundary.index);
|
|
116
|
+
buffer = buffer.slice(boundary.index + boundary[0].length);
|
|
117
|
+
const data = event.split(/\r?\n/)
|
|
118
|
+
.filter((line) => line.startsWith("data:"))
|
|
119
|
+
.map((line) => line.slice(5).trimStart())
|
|
120
|
+
.join("\n");
|
|
121
|
+
const message = matching(data);
|
|
122
|
+
if (message)
|
|
123
|
+
return message;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return null; // EOF never completes an unterminated SSE event.
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (error instanceof RobisieError && responseLimitErrors.has(error))
|
|
130
|
+
throw error;
|
|
131
|
+
throw new RobisieError("Nie mogę odczytać odpowiedzi serwera robisie.", "Sprawdź połączenie i uruchom ponownie `npx robisie doctor`.");
|
|
73
132
|
}
|
|
74
|
-
return null;
|
|
75
133
|
}
|
|
76
134
|
async function rpc(ctx, key, id, method, params) {
|
|
77
135
|
const res = await request(ctx, mcpUrl(ctx.origin), {
|
|
@@ -93,10 +151,13 @@ async function rpc(ctx, key, id, method, params) {
|
|
|
93
151
|
if (!res.ok)
|
|
94
152
|
throw new RobisieError(`Serwer robisie odpowiedział błędem ${res.status}.`, "Spróbuj ponownie za kilka minut; jeśli błąd zostaje, napisz do robisie.");
|
|
95
153
|
const message = await readRpc(res, id);
|
|
96
|
-
if (!message)
|
|
97
|
-
throw
|
|
98
|
-
if (message
|
|
99
|
-
|
|
154
|
+
if (!message || message.jsonrpc !== "2.0" || ("result" in message) === ("error" in message))
|
|
155
|
+
throw formatError();
|
|
156
|
+
if ("error" in message) {
|
|
157
|
+
if (!isRecord(message.error) || !Number.isInteger(message.error.code) || typeof message.error.message !== "string")
|
|
158
|
+
throw formatError();
|
|
159
|
+
throw new RobisieError("Serwer robisie zwrócił błąd RPC.", "Spróbuj ponownie; jeśli błąd zostaje, uruchom lokalne `npx robisie doctor` w katalogu instalacji.");
|
|
160
|
+
}
|
|
100
161
|
return { result: message.result, contractVersion };
|
|
101
162
|
}
|
|
102
163
|
/** initialize + list_projects: proves the key works end-to-end and reads the board. */
|
|
@@ -106,22 +167,38 @@ export async function checkConnection(ctx, key) {
|
|
|
106
167
|
capabilities: {},
|
|
107
168
|
clientInfo: { name: PACKAGE_NAME, version: PACKAGE_VERSION },
|
|
108
169
|
});
|
|
109
|
-
|
|
170
|
+
if (!isRecord(init.result) || !isRecord(init.result.serverInfo) || !nonEmptyString(init.result.serverInfo.name)
|
|
171
|
+
|| !nonEmptyString(init.result.protocolVersion) || !isRecord(init.result.capabilities))
|
|
172
|
+
throw formatError();
|
|
173
|
+
const serverName = init.result.serverInfo.name;
|
|
110
174
|
const list = await rpc(ctx, key, 2, "tools/call", { name: "list_projects", arguments: {} });
|
|
111
175
|
const result = list.result;
|
|
112
|
-
if (result
|
|
113
|
-
throw
|
|
114
|
-
|
|
176
|
+
if (!isRecord(result) || (result.isError !== undefined && typeof result.isError !== "boolean"))
|
|
177
|
+
throw formatError();
|
|
178
|
+
if (result.isError)
|
|
179
|
+
throw new RobisieError("Narzędzie list_projects zwróciło błąd.", "Spróbuj ponownie; jeśli błąd zostaje, napisz do robisie.");
|
|
180
|
+
if (!Array.isArray(result.content))
|
|
181
|
+
throw formatError();
|
|
182
|
+
const content = result.content.find((item) => isRecord(item) && item.type === "text");
|
|
183
|
+
if (!isRecord(content) || typeof content.text !== "string")
|
|
184
|
+
throw formatError();
|
|
185
|
+
let parsed;
|
|
115
186
|
try {
|
|
116
|
-
|
|
117
|
-
projectCount = Array.isArray(parsed) ? parsed.length : 0;
|
|
187
|
+
parsed = JSON.parse(content.text);
|
|
118
188
|
}
|
|
119
189
|
catch {
|
|
120
|
-
|
|
190
|
+
throw formatError();
|
|
121
191
|
}
|
|
192
|
+
if (!Array.isArray(parsed) || !parsed.every((project) => isRecord(project)
|
|
193
|
+
&& nonEmptyString(project.id) && nonEmptyString(project.name)))
|
|
194
|
+
throw formatError();
|
|
195
|
+
if (init.contractVersion)
|
|
196
|
+
assertCompatible(init.contractVersion);
|
|
197
|
+
if (list.contractVersion)
|
|
198
|
+
assertCompatible(list.contractVersion);
|
|
122
199
|
return {
|
|
123
|
-
serverName
|
|
200
|
+
serverName,
|
|
124
201
|
contractVersion: init.contractVersion ?? list.contractVersion,
|
|
125
|
-
projectCount,
|
|
202
|
+
projectCount: parsed.length,
|
|
126
203
|
};
|
|
127
204
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type OpenCodeTokenUsage = Readonly<{
|
|
2
|
+
input?: number;
|
|
3
|
+
output?: number;
|
|
4
|
+
reasoning?: number;
|
|
5
|
+
cacheRead?: number;
|
|
6
|
+
cacheWrite?: number;
|
|
7
|
+
}>;
|
|
8
|
+
export declare function parseOpenCodeStream(raw: string, model: string): {
|
|
9
|
+
sessionId: string;
|
|
10
|
+
messageId: string;
|
|
11
|
+
report: string;
|
|
12
|
+
costUsd?: number;
|
|
13
|
+
usage?: OpenCodeTokenUsage;
|
|
14
|
+
};
|
|
15
|
+
export declare function bindOpenCodeExport(raw: string, stream: {
|
|
16
|
+
sessionId: string;
|
|
17
|
+
messageId: string;
|
|
18
|
+
report: string;
|
|
19
|
+
}, model: string): void;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
const PROVIDER = "opencode-go";
|
|
2
|
+
const REPORT_MARKER = "### RAPORT MASZYNISTY";
|
|
3
|
+
const SAFE_STREAM_ID = /^(?:ses|msg)_[A-Za-z0-9]+$/;
|
|
4
|
+
function record(value) {
|
|
5
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
6
|
+
? value
|
|
7
|
+
: null;
|
|
8
|
+
}
|
|
9
|
+
function countMarker(text) {
|
|
10
|
+
return text.split(REPORT_MARKER).length - 1;
|
|
11
|
+
}
|
|
12
|
+
export function parseOpenCodeStream(raw, model) {
|
|
13
|
+
const lines = raw.split(/\r?\n/).filter((line) => line.trim());
|
|
14
|
+
if (lines.length === 0)
|
|
15
|
+
throw new Error("OpenCode stream is empty");
|
|
16
|
+
let sessionId = null;
|
|
17
|
+
const textByMessage = new Map();
|
|
18
|
+
const seenModels = new Set(), seenProviders = new Set();
|
|
19
|
+
const costs = [];
|
|
20
|
+
const usage = {};
|
|
21
|
+
let usageObserved = false;
|
|
22
|
+
let lastTextMessageId = null;
|
|
23
|
+
for (const line of lines) {
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = JSON.parse(line);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new Error("OpenCode stream contains malformed JSON");
|
|
30
|
+
}
|
|
31
|
+
const event = record(parsed), part = record(event?.part);
|
|
32
|
+
if (!event || !part || typeof event.timestamp !== "number" || !Number.isFinite(event.timestamp)
|
|
33
|
+
|| typeof event.sessionID !== "string" || !SAFE_STREAM_ID.test(event.sessionID))
|
|
34
|
+
throw new Error("OpenCode stream envelope is malformed");
|
|
35
|
+
if (sessionId !== null && event.sessionID !== sessionId)
|
|
36
|
+
throw new Error("OpenCode stream mixes sessions");
|
|
37
|
+
sessionId = event.sessionID;
|
|
38
|
+
const type = event.type, partType = part.type;
|
|
39
|
+
if (!["step_start", "reasoning", "tool_use", "text", "step_finish"].includes(type)) {
|
|
40
|
+
throw new Error("OpenCode stream event is unsupported");
|
|
41
|
+
}
|
|
42
|
+
const messageId = part.messageID;
|
|
43
|
+
if (messageId !== undefined && (typeof messageId !== "string" || !SAFE_STREAM_ID.test(messageId)))
|
|
44
|
+
throw new Error("OpenCode stream message identity is malformed");
|
|
45
|
+
if (type === "step_start") {
|
|
46
|
+
if (partType !== "step-start")
|
|
47
|
+
throw new Error("OpenCode step_start is malformed");
|
|
48
|
+
if (typeof part.model === "string" && part.model.trim())
|
|
49
|
+
seenModels.add(part.model);
|
|
50
|
+
if (typeof part.provider === "string" && part.provider.trim())
|
|
51
|
+
seenProviders.add(part.provider);
|
|
52
|
+
}
|
|
53
|
+
else if (type === "text") {
|
|
54
|
+
if (partType !== "text" || typeof messageId !== "string" || typeof part.text !== "string")
|
|
55
|
+
throw new Error("OpenCode text event is malformed");
|
|
56
|
+
const chunks = textByMessage.get(messageId) ?? [];
|
|
57
|
+
chunks.push(part.text);
|
|
58
|
+
textByMessage.set(messageId, chunks);
|
|
59
|
+
lastTextMessageId = messageId;
|
|
60
|
+
}
|
|
61
|
+
else if (type === "reasoning" && partType !== "reasoning")
|
|
62
|
+
throw new Error("OpenCode reasoning event is malformed");
|
|
63
|
+
else if (type === "step_finish") {
|
|
64
|
+
if (partType !== "step-finish")
|
|
65
|
+
throw new Error("OpenCode step_finish is malformed");
|
|
66
|
+
if (part.cost !== undefined) {
|
|
67
|
+
if (typeof part.cost !== "number" || !Number.isFinite(part.cost) || part.cost < 0)
|
|
68
|
+
throw new Error("OpenCode stream cost is malformed");
|
|
69
|
+
costs.push(part.cost);
|
|
70
|
+
}
|
|
71
|
+
if (part.tokens !== undefined) {
|
|
72
|
+
const tokens = record(part.tokens), cache = record(tokens?.cache);
|
|
73
|
+
if (!tokens)
|
|
74
|
+
throw new Error("OpenCode stream usage is malformed");
|
|
75
|
+
const values = [
|
|
76
|
+
["input", tokens.input], ["output", tokens.output], ["reasoning", tokens.reasoning],
|
|
77
|
+
["cacheRead", cache?.read], ["cacheWrite", cache?.write],
|
|
78
|
+
];
|
|
79
|
+
for (const [key, value] of values) {
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
continue;
|
|
82
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
|
|
83
|
+
throw new Error("OpenCode stream usage is malformed");
|
|
84
|
+
usage[key] = (usage[key] ?? 0) + value;
|
|
85
|
+
usageObserved = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const reports = [...textByMessage].map(([messageId, chunks]) => ({ messageId, report: chunks.join("") }))
|
|
91
|
+
.filter(({ report }) => countMarker(report) === 1);
|
|
92
|
+
if (!sessionId || reports.length !== 1 || reports[0]?.messageId !== lastTextMessageId) {
|
|
93
|
+
throw new Error("OpenCode stream does not contain exactly one final report");
|
|
94
|
+
}
|
|
95
|
+
for (const observed of seenModels) {
|
|
96
|
+
if (observed !== model && observed !== `${PROVIDER}/${model}`)
|
|
97
|
+
throw new Error("OpenCode stream model does not match the selected model");
|
|
98
|
+
}
|
|
99
|
+
for (const observed of seenProviders)
|
|
100
|
+
if (observed !== PROVIDER)
|
|
101
|
+
throw new Error("OpenCode stream provider does not match the Go plan");
|
|
102
|
+
return { sessionId, ...reports[0], ...(costs.length ? { costUsd: costs.reduce((sum, cost) => sum + cost, 0) } : {}),
|
|
103
|
+
...(usageObserved ? { usage } : {}) };
|
|
104
|
+
}
|
|
105
|
+
export function bindOpenCodeExport(raw, stream, model) {
|
|
106
|
+
let parsed;
|
|
107
|
+
try {
|
|
108
|
+
parsed = JSON.parse(raw);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new Error("OpenCode export is malformed JSON");
|
|
112
|
+
}
|
|
113
|
+
const root = record(parsed), info = record(root?.info);
|
|
114
|
+
if (!root || info?.id !== stream.sessionId || !Array.isArray(root.messages))
|
|
115
|
+
throw new Error("OpenCode export session does not match stream");
|
|
116
|
+
const matches = root.messages.map(record).filter((message) => message?.type === "assistant" && message.id === stream.messageId);
|
|
117
|
+
if (matches.length !== 1)
|
|
118
|
+
throw new Error("OpenCode export message does not match stream");
|
|
119
|
+
const assistant = matches[0], identity = record(assistant.model);
|
|
120
|
+
if (identity?.id !== model || identity.providerID !== PROVIDER || assistant.finish !== "stop" || assistant.rawFinish !== "stop") {
|
|
121
|
+
throw new Error("OpenCode export terminal identity is malformed");
|
|
122
|
+
}
|
|
123
|
+
if (assistant.error !== undefined
|
|
124
|
+
|| (assistant.modelID !== undefined && assistant.modelID !== identity.id)
|
|
125
|
+
|| (assistant.providerID !== undefined && assistant.providerID !== identity.providerID)) {
|
|
126
|
+
throw new Error("OpenCode export contains conflicting terminal identity");
|
|
127
|
+
}
|
|
128
|
+
if (!Array.isArray(assistant.content))
|
|
129
|
+
throw new Error("OpenCode export content is malformed");
|
|
130
|
+
const textParts = assistant.content.map(record).filter((part) => part?.type === "text");
|
|
131
|
+
if (textParts.length !== 1 || textParts.some((part) => typeof part?.text !== "string"))
|
|
132
|
+
throw new Error("OpenCode export report is malformed");
|
|
133
|
+
const report = textParts.map((part) => String(part?.text)).join("");
|
|
134
|
+
if (countMarker(report) !== 1 || report !== stream.report)
|
|
135
|
+
throw new Error("OpenCode export report does not match stream");
|
|
136
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const OPENCODE_REVIEW_GUARD: string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FILE_MARK } from "./constants.js";
|
|
2
|
+
// Keep this function self-contained: its compiled JS is the installed plugin.
|
|
3
|
+
async function robisieReviewGuard() {
|
|
4
|
+
return {
|
|
5
|
+
"tool.execute.before": async (input, output) => {
|
|
6
|
+
if (typeof input !== "object" || input === null || !("tool" in input))
|
|
7
|
+
return;
|
|
8
|
+
if (input.tool !== "robisie_create_task" && input.tool !== "robisie_update_task")
|
|
9
|
+
return;
|
|
10
|
+
if (typeof output !== "object" || output === null || !("args" in output))
|
|
11
|
+
return;
|
|
12
|
+
const args = output.args;
|
|
13
|
+
if (typeof args === "object" && args !== null && "status" in args && args.status === "delivered") {
|
|
14
|
+
throw new Error("Brak wspieranego protokołu recenzji w OpenCode. Ta próba nie zmieniła karty.");
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export const OPENCODE_REVIEW_GUARD = `// ${FILE_MARK}\nexport const RobisieReviewGuard = ${robisieReviewGuard.toString()};\n`;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type SpawnSyncOptionsWithStringEncoding, type SpawnSyncReturns } from "node:child_process";
|
|
2
|
+
/** OpenCode may exit before pipe stdout drains; a regular file preserves the export. */
|
|
3
|
+
export declare function exportOpenCodeSessionFile(binary: string, session: string, options: SpawnSyncOptionsWithStringEncoding & {
|
|
4
|
+
maxBuffer: number;
|
|
5
|
+
}): SpawnSyncReturns<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { closeSync, mkdtempSync, openSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
/** OpenCode may exit before pipe stdout drains; a regular file preserves the export. */
|
|
6
|
+
export function exportOpenCodeSessionFile(binary, session, options) {
|
|
7
|
+
const dir = mkdtempSync(join(tmpdir(), "opencode-export-"));
|
|
8
|
+
let fd;
|
|
9
|
+
try {
|
|
10
|
+
const file = join(dir, "session.json");
|
|
11
|
+
fd = openSync(file, "w", 0o600);
|
|
12
|
+
const result = spawnSync(binary, ["session", "export", session], { ...options, shell: false, stdio: ["ignore", fd, "pipe"] });
|
|
13
|
+
if (statSync(file).size > options.maxBuffer) {
|
|
14
|
+
return { ...result, status: null, stdout: "", error: result.error ?? Object.assign(new Error(`spawnSync ${binary} ENOBUFS: session export exceeds ${options.maxBuffer} bytes`), { code: "ENOBUFS" }) };
|
|
15
|
+
}
|
|
16
|
+
return { ...result, stdout: readFileSync(file, "utf8") };
|
|
17
|
+
}
|
|
18
|
+
finally {
|
|
19
|
+
// IO errors propagate to the caller's existing process-error handling.
|
|
20
|
+
try {
|
|
21
|
+
if (fd !== undefined)
|
|
22
|
+
closeSync(fd);
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
rmSync(dir, { recursive: true, force: true });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type WorkerOutcome, type ReviewVerdict } from "./role-results.js";
|
|
2
|
+
/** Mapuje wynik Workera na ruch na tablicy. */
|
|
3
|
+
export declare function decideBoardMove(outcome: WorkerOutcome, attempts: number, maxAttempts?: number): {
|
|
4
|
+
status: "in_review";
|
|
5
|
+
note: string;
|
|
6
|
+
} | {
|
|
7
|
+
status: "blocked";
|
|
8
|
+
note: string;
|
|
9
|
+
} | {
|
|
10
|
+
status: "retry";
|
|
11
|
+
note: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const MAX_REVIEW_ROUNDS = 3;
|
|
14
|
+
/** reviewRound = indeks od zera; unavailable obejmuje bieżącą próbę. Grant R3 weryfikuje i trwale konsumuje caller. */
|
|
15
|
+
export declare function decideReviewMove(verdict: ReviewVerdict, reviewRound: number, unavailable?: number, verifiedR3Grant?: boolean): "land" | "repair" | "blocked" | "retry-review";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { plainMarkers } from "./role-results.js";
|
|
2
|
+
/** Mapuje wynik Workera na ruch na tablicy. */
|
|
3
|
+
export function decideBoardMove(outcome, attempts, maxAttempts = 3) {
|
|
4
|
+
/** Powód: tekst po „CZEGO NIE ZROBIONO I DLACZEGO:” w tej samej linii, inaczej pierwsza niepusta linia niżej. */
|
|
5
|
+
const reasonOf = (status) => {
|
|
6
|
+
const lines = plainMarkers(outcome.report).split(/\r?\n/).map((line) => line.trim());
|
|
7
|
+
const statusLine = lines.findIndex((line) => new RegExp(`^[-*]?\\s*STATUS:\\s*${status}\\b`).test(line));
|
|
8
|
+
const header = /^[-*]?\s*CZEGO NIE ZROBIONO I DLACZEGO:\s*(.*)$/;
|
|
9
|
+
const field = lines.findIndex((line, index) => index > statusLine && header.test(line));
|
|
10
|
+
const reason = (field >= 0 ? header.exec(lines[field])?.[1] : "")
|
|
11
|
+
|| lines.slice((field >= 0 ? field : statusLine) + 1).find(Boolean)?.replace(/^[-*]\s*/, "") || "brak podanego powodu";
|
|
12
|
+
return Array.from(reason).slice(0, 200).join("");
|
|
13
|
+
};
|
|
14
|
+
switch (outcome.status) {
|
|
15
|
+
case "WORKER_FINISHED":
|
|
16
|
+
case "ALREADY_DONE":
|
|
17
|
+
return { status: "in_review", note: `${outcome.status} — dowód w details.maszynista` };
|
|
18
|
+
case "NEEDS_HUMAN":
|
|
19
|
+
return { status: "blocked", note: `worker: WYMAGA CZŁOWIEKA — ${reasonOf("NEEDS_HUMAN")}` };
|
|
20
|
+
case "ACTIONABLE_BLOCKER":
|
|
21
|
+
return { status: "blocked", note: `worker: ACTIONABLE_BLOCKER — ${reasonOf("ACTIONABLE_BLOCKER")}` };
|
|
22
|
+
case "FAILED":
|
|
23
|
+
case "UNKNOWN":
|
|
24
|
+
default:
|
|
25
|
+
if (attempts < maxAttempts)
|
|
26
|
+
return { status: "retry", note: `próba ${attempts}/${maxAttempts} nieudana (${outcome.status})` };
|
|
27
|
+
return { status: "blocked", note: `${maxAttempts} próby nieudane (${outcome.status}) — blokada techniczna, do CR/Reksia` };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export const MAX_REVIEW_ROUNDS = 3;
|
|
31
|
+
/** reviewRound = indeks od zera; unavailable obejmuje bieżącą próbę. Grant R3 weryfikuje i trwale konsumuje caller. */
|
|
32
|
+
export function decideReviewMove(verdict, reviewRound, unavailable = 0, verifiedR3Grant = false) {
|
|
33
|
+
if (verdict === "PASS")
|
|
34
|
+
return "land";
|
|
35
|
+
if (verdict === "FAIL")
|
|
36
|
+
return reviewRound < MAX_REVIEW_ROUNDS - 1
|
|
37
|
+
|| verifiedR3Grant === true && reviewRound === MAX_REVIEW_ROUNDS - 1 ? "repair" : "blocked";
|
|
38
|
+
return unavailable >= 3 ? "blocked" : "retry-review";
|
|
39
|
+
}
|