@attalabs/vinaya 0.43.1 → 0.44.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/README.md +1 -1
- package/dist/checks/bin/check-token-collection-wired.js +0 -1
- package/dist/checks/bin/verify-dispatch.js +0 -1
- package/dist/index.js +105 -42
- package/dist/programs/dev-review-loop.js +3 -2
- package/dist/programs/run-engine.js +59 -33
- package/dist/programs/task-brief.js +29 -4
- package/dist/programs/task-tools-serve.js +37 -8
- package/package.json +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/BUILD_ID +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/build-manifest.json +2 -2
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error.html +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error.rsc +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/milestones/page_client-reference-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/page.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/page.js.nft.json +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/page_client-reference-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/app/tasks/page_client-reference-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/chunks/209.js +2 -2
- package/studio-standalone/apps/vinaya-studio/.next/server/chunks/951.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/middleware-build-manifest.js +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/server/pages/500.html +1 -1
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/151-23bea4cac9687151.js +1 -0
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/layout-dfa2186095e797bd.js +1 -0
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/milestones/page-2fffb49104860a15.js +1 -0
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/page-6ae258f663623b2a.js +1 -0
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/tasks/page-2fffb49104860a15.js +1 -0
- package/studio-standalone/apps/vinaya-studio/log-follow.mjs +5601 -0
- package/studio-standalone/apps/vinaya-studio/log-sync-folder-source.mjs +5089 -0
- package/studio-standalone/apps/vinaya-studio/package.json +2 -0
- package/studio-standalone/apps/vinaya-studio/studio-server.mjs +2574 -2
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/layout-57b790abf8793795.js +0 -1
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/milestones/page-9fbd5991bc7a1eed.js +0 -1
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/page-9fbd5991bc7a1eed.js +0 -1
- package/studio-standalone/apps/vinaya-studio/.next/static/chunks/app/tasks/page-9fbd5991bc7a1eed.js +0 -1
- /package/studio-standalone/apps/vinaya-studio/.next/static/{64XwAXRg-jkgQPC54BkEN → _r9iZDxlkoek85d_3oaxL}/_buildManifest.js +0 -0
- /package/studio-standalone/apps/vinaya-studio/.next/static/{64XwAXRg-jkgQPC54BkEN → _r9iZDxlkoek85d_3oaxL}/_ssgManifest.js +0 -0
|
@@ -5128,8 +5128,36 @@ function resolveLogsSetting(config) {
|
|
|
5128
5128
|
return { folder: raw.folder };
|
|
5129
5129
|
return null;
|
|
5130
5130
|
}
|
|
5131
|
+
function resolveLogsHeaderValues(headers, env) {
|
|
5132
|
+
if (!headers)
|
|
5133
|
+
return;
|
|
5134
|
+
const resolved = {};
|
|
5135
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
5136
|
+
resolved[key] = value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_match, name) => env[name] ?? "");
|
|
5137
|
+
}
|
|
5138
|
+
return resolved;
|
|
5139
|
+
}
|
|
5140
|
+
|
|
5141
|
+
// ../../harness/kernel/log/sync/measured.ts
|
|
5142
|
+
function known(value) {
|
|
5143
|
+
return { known: true, value };
|
|
5144
|
+
}
|
|
5145
|
+
function unknownBecause(reason) {
|
|
5146
|
+
return { known: false, reason };
|
|
5147
|
+
}
|
|
5131
5148
|
|
|
5132
5149
|
// ../../harness/kernel/log/client/log-sync-server-source.ts
|
|
5150
|
+
var SERVER_SOURCE_PAGE_MAX = 1000;
|
|
5151
|
+
var SERVER_SOURCE_TIMEOUT_MS = 1e4;
|
|
5152
|
+
function swapEventsTail(eventsUrl, tail) {
|
|
5153
|
+
return eventsUrl.replace(/\/events(\?|$)/, `/${tail}$1`);
|
|
5154
|
+
}
|
|
5155
|
+
function statsUrlFrom(eventsUrl) {
|
|
5156
|
+
return swapEventsTail(eventsUrl, "stats");
|
|
5157
|
+
}
|
|
5158
|
+
function rejectedUrlFrom(eventsUrl) {
|
|
5159
|
+
return swapEventsTail(eventsUrl, "rejected");
|
|
5160
|
+
}
|
|
5133
5161
|
function credentialVarNames(headers) {
|
|
5134
5162
|
const names = new Set;
|
|
5135
5163
|
for (const value of Object.values(headers ?? {})) {
|
|
@@ -5150,6 +5178,141 @@ function credentialMissing(headers, env) {
|
|
|
5150
5178
|
return false;
|
|
5151
5179
|
}
|
|
5152
5180
|
|
|
5181
|
+
class ServerSourceError extends Error {
|
|
5182
|
+
kind;
|
|
5183
|
+
constructor(kind, message) {
|
|
5184
|
+
super(message);
|
|
5185
|
+
this.kind = kind;
|
|
5186
|
+
this.name = "ServerSourceError";
|
|
5187
|
+
}
|
|
5188
|
+
}
|
|
5189
|
+
async function discard(response) {
|
|
5190
|
+
await response.body?.cancel().catch(() => {});
|
|
5191
|
+
}
|
|
5192
|
+
function createServerLogSource(deps) {
|
|
5193
|
+
let totalRowsRead = 0;
|
|
5194
|
+
function credentialRefusedReason() {
|
|
5195
|
+
const vars = credentialVarNames(deps.readHeaders);
|
|
5196
|
+
return vars.length > 0 ? `the read credential was refused — set ${vars.join(", ")} to the server's read token (the value is never printed).` : "the read credential was refused by the log server.";
|
|
5197
|
+
}
|
|
5198
|
+
async function get(url) {
|
|
5199
|
+
if (credentialMissing(deps.readHeaders, deps.env)) {
|
|
5200
|
+
throw new ServerSourceError("credential", credentialRefusedReason());
|
|
5201
|
+
}
|
|
5202
|
+
try {
|
|
5203
|
+
return await deps.fetchImpl(url, {
|
|
5204
|
+
method: "GET",
|
|
5205
|
+
headers: { ...resolveLogsHeaderValues(deps.readHeaders, deps.env) ?? {} },
|
|
5206
|
+
signal: AbortSignal.timeout(SERVER_SOURCE_TIMEOUT_MS)
|
|
5207
|
+
});
|
|
5208
|
+
} catch (err) {
|
|
5209
|
+
if (err instanceof Error && err.name === "TimeoutError") {
|
|
5210
|
+
throw new ServerSourceError("timeout", `the log server timed out after ${SERVER_SOURCE_TIMEOUT_MS}ms reading ${url}.`);
|
|
5211
|
+
}
|
|
5212
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
5213
|
+
throw new ServerSourceError("unreachable", `the log server could not be reached reading ${url}: ${detail}`);
|
|
5214
|
+
}
|
|
5215
|
+
}
|
|
5216
|
+
async function readFrom(after, limit) {
|
|
5217
|
+
const boundedLimit = Math.max(1, Math.min(limit, SERVER_SOURCE_PAGE_MAX));
|
|
5218
|
+
const url = new URL(deps.eventsUrl);
|
|
5219
|
+
url.searchParams.set("after", String(after));
|
|
5220
|
+
url.searchParams.set("limit", String(boundedLimit));
|
|
5221
|
+
const response = await get(url.toString());
|
|
5222
|
+
if (response.status === 401) {
|
|
5223
|
+
await discard(response);
|
|
5224
|
+
throw new ServerSourceError("credential", credentialRefusedReason());
|
|
5225
|
+
}
|
|
5226
|
+
if (!response.ok) {
|
|
5227
|
+
await discard(response);
|
|
5228
|
+
throw new ServerSourceError("server-error", `the log server returned ${response.status} reading ${url.toString()}.`);
|
|
5229
|
+
}
|
|
5230
|
+
const nextHeader = response.headers.get("vinaya-log-next-after");
|
|
5231
|
+
const text = await response.text();
|
|
5232
|
+
const lines = text.split(`
|
|
5233
|
+
`).filter((line) => line.length > 0).map((line) => {
|
|
5234
|
+
const parsed = JSON.parse(line);
|
|
5235
|
+
return { raw: JSON.stringify(parsed.event), position: String(parsed.seq) };
|
|
5236
|
+
});
|
|
5237
|
+
totalRowsRead += lines.length;
|
|
5238
|
+
const next = lines.length === 0 ? null : nextHeader ?? String(after);
|
|
5239
|
+
const gaps = [];
|
|
5240
|
+
return { lines, next, gaps };
|
|
5241
|
+
}
|
|
5242
|
+
return {
|
|
5243
|
+
id: `server:${deps.eventsUrl}`,
|
|
5244
|
+
async readPage(cursor, limit) {
|
|
5245
|
+
const after = cursor !== null ? Number(cursor) : 0;
|
|
5246
|
+
return readFrom(after, limit);
|
|
5247
|
+
},
|
|
5248
|
+
async lookback(cursor, span) {
|
|
5249
|
+
const cursorPos = cursor !== null ? Number(cursor) : 0;
|
|
5250
|
+
const start = Math.max(0, cursorPos - span);
|
|
5251
|
+
return readFrom(start, Math.min(span, SERVER_SOURCE_PAGE_MAX));
|
|
5252
|
+
},
|
|
5253
|
+
async retention() {
|
|
5254
|
+
let response;
|
|
5255
|
+
try {
|
|
5256
|
+
response = await get(statsUrlFrom(deps.eventsUrl));
|
|
5257
|
+
} catch (err) {
|
|
5258
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
5259
|
+
return { head: unknownBecause(reason), storedCount: unknownBecause(reason), gap: unknownBecause(reason) };
|
|
5260
|
+
}
|
|
5261
|
+
if (response.status === 401) {
|
|
5262
|
+
await discard(response);
|
|
5263
|
+
const reason = credentialRefusedReason();
|
|
5264
|
+
return { head: unknownBecause(reason), storedCount: unknownBecause(reason), gap: unknownBecause(reason) };
|
|
5265
|
+
}
|
|
5266
|
+
if (!response.ok) {
|
|
5267
|
+
await discard(response);
|
|
5268
|
+
const reason = `the log server returned ${response.status} reading its stats route.`;
|
|
5269
|
+
return { head: unknownBecause(reason), storedCount: unknownBecause(reason), gap: unknownBecause(reason) };
|
|
5270
|
+
}
|
|
5271
|
+
const body = await response.json();
|
|
5272
|
+
const head = typeof body.last_seq === "number" ? known(body.last_seq) : unknownBecause("the stats route did not report last_seq.");
|
|
5273
|
+
const storedCount = typeof body.events === "number" ? known(body.events) : unknownBecause("the stats route did not report events.");
|
|
5274
|
+
const gap = head.known && storedCount.known ? storedCount.value < head.value ? known(head.value - storedCount.value) : known(0) : unknownBecause("the stats route did not report a usable head and stored count.");
|
|
5275
|
+
return { head, storedCount, gap };
|
|
5276
|
+
},
|
|
5277
|
+
async rejected() {
|
|
5278
|
+
let response;
|
|
5279
|
+
try {
|
|
5280
|
+
response = await get(rejectedUrlFrom(deps.eventsUrl));
|
|
5281
|
+
} catch (err) {
|
|
5282
|
+
return { available: false, reason: err instanceof Error ? err.message : String(err) };
|
|
5283
|
+
}
|
|
5284
|
+
if (response.status === 401) {
|
|
5285
|
+
await discard(response);
|
|
5286
|
+
return { available: false, reason: credentialRefusedReason() };
|
|
5287
|
+
}
|
|
5288
|
+
if (response.status === 404) {
|
|
5289
|
+
await discard(response);
|
|
5290
|
+
return {
|
|
5291
|
+
available: false,
|
|
5292
|
+
reason: "the log server has no rejected route — redeploy it to get a lost-event diagnostic (an older server predates this route)."
|
|
5293
|
+
};
|
|
5294
|
+
}
|
|
5295
|
+
if (!response.ok) {
|
|
5296
|
+
await discard(response);
|
|
5297
|
+
return { available: false, reason: `the log server returned ${response.status} reading its rejected route.` };
|
|
5298
|
+
}
|
|
5299
|
+
const body = await response.json();
|
|
5300
|
+
const window = typeof body.window === "number" ? body.window : 0;
|
|
5301
|
+
const reasons = Array.isArray(body.reasons) ? body.reasons : [];
|
|
5302
|
+
const recent = Array.isArray(body.recent) ? body.recent.map((r) => ({
|
|
5303
|
+
id: r.id,
|
|
5304
|
+
receivedAt: r.received_at,
|
|
5305
|
+
reason: r.reason,
|
|
5306
|
+
line: r.line
|
|
5307
|
+
})) : [];
|
|
5308
|
+
return { available: true, window, reasons, recent };
|
|
5309
|
+
},
|
|
5310
|
+
rowsRead() {
|
|
5311
|
+
return totalRowsRead;
|
|
5312
|
+
}
|
|
5313
|
+
};
|
|
5314
|
+
}
|
|
5315
|
+
|
|
5153
5316
|
// ../../harness/kernel/run-paths.ts
|
|
5154
5317
|
import { dirname as dirname2, join as join2, resolve as resolvePath } from "node:path";
|
|
5155
5318
|
|
|
@@ -5359,6 +5522,7 @@ var CUSTOM_EVENT_RESERVED_NAMESPACE = "vinaya";
|
|
|
5359
5522
|
var CUSTOM_EVENT_MAX_FIELDS = 20;
|
|
5360
5523
|
var CUSTOM_FIELD_NAME_PATTERN = /^[a-z][a-z0-9_]*$/;
|
|
5361
5524
|
var CUSTOM_FIELD_NAME_MAX_LENGTH = 64;
|
|
5525
|
+
var CUSTOM_TEXT_MAX_LENGTH = 500;
|
|
5362
5526
|
var CUSTOM_WORD_PATTERN = /^[a-z0-9][a-z0-9_-]*$/;
|
|
5363
5527
|
var CUSTOM_WORD_MAX_LENGTH = 64;
|
|
5364
5528
|
var CUSTOM_FIELD_MAX_WORDS = 50;
|
|
@@ -7447,6 +7611,2398 @@ Connection: close\r
|
|
|
7447
7611
|
};
|
|
7448
7612
|
}
|
|
7449
7613
|
|
|
7614
|
+
// src/lib/mission-control-protocol.ts
|
|
7615
|
+
var PROTOCOL_VERSION = 1;
|
|
7616
|
+
var KEEPALIVE_MS = 20000;
|
|
7617
|
+
var SILENCE_LIMIT_MS = 2 * KEEPALIVE_MS + 1e4;
|
|
7618
|
+
var CLIENT_MESSAGE_MAX_BYTES = 256;
|
|
7619
|
+
var TEXT_MAX = 500;
|
|
7620
|
+
var TASKS_PER_REPOSITORY_MAX = 1e4;
|
|
7621
|
+
function isRecord(value) {
|
|
7622
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7623
|
+
}
|
|
7624
|
+
function parseClientMessage(text) {
|
|
7625
|
+
if (text.length > CLIENT_MESSAGE_MAX_BYTES)
|
|
7626
|
+
return null;
|
|
7627
|
+
let raw;
|
|
7628
|
+
try {
|
|
7629
|
+
raw = JSON.parse(text);
|
|
7630
|
+
} catch {
|
|
7631
|
+
return null;
|
|
7632
|
+
}
|
|
7633
|
+
if (!isRecord(raw) || raw.v !== PROTOCOL_VERSION || raw.type !== "resync")
|
|
7634
|
+
return null;
|
|
7635
|
+
if (Object.keys(raw).length !== 2)
|
|
7636
|
+
return null;
|
|
7637
|
+
return { v: 1, type: "resync" };
|
|
7638
|
+
}
|
|
7639
|
+
|
|
7640
|
+
// src/server/projection.ts
|
|
7641
|
+
function text(value) {
|
|
7642
|
+
return value.length > TEXT_MAX ? `${value.slice(0, TEXT_MAX - 3)}...` : value;
|
|
7643
|
+
}
|
|
7644
|
+
var nullableText = (value) => value === null ? null : text(value);
|
|
7645
|
+
function counts(value) {
|
|
7646
|
+
return value === null ? null : { blocking: value.blocking, nonBlocking: value.nonBlocking, unclassified: value.unclassified };
|
|
7647
|
+
}
|
|
7648
|
+
function roundView(round) {
|
|
7649
|
+
return {
|
|
7650
|
+
n: round.n,
|
|
7651
|
+
startedAt: round.startedAt,
|
|
7652
|
+
endedAt: round.endedAt,
|
|
7653
|
+
wallMs: round.wallMs,
|
|
7654
|
+
outcome: round.outcome,
|
|
7655
|
+
checksGreen: round.checksGreen,
|
|
7656
|
+
agents: { ...round.agents },
|
|
7657
|
+
confidence: round.confidence?.value ?? null,
|
|
7658
|
+
reviewers: round.reviewers.map((r) => ({ role: text(r.role), outcome: r.outcome, blocking: r.blocking })),
|
|
7659
|
+
findings: counts(round.findings)
|
|
7660
|
+
};
|
|
7661
|
+
}
|
|
7662
|
+
function taskView(task) {
|
|
7663
|
+
const tokens = {};
|
|
7664
|
+
for (const [role, t] of Object.entries(task.tokens)) {
|
|
7665
|
+
tokens[role] = { input: t.input, output: t.output, unknownTurns: t.unknownTurns };
|
|
7666
|
+
}
|
|
7667
|
+
return {
|
|
7668
|
+
task: task.task,
|
|
7669
|
+
title: nullableText(task.title),
|
|
7670
|
+
state: task.state,
|
|
7671
|
+
amber: task.amber,
|
|
7672
|
+
reason: nullableText(task.reason),
|
|
7673
|
+
decision: nullableText(task.decision),
|
|
7674
|
+
pr: task.pr,
|
|
7675
|
+
nowLine: text(task.nowLine),
|
|
7676
|
+
lastSign: task.lastSign,
|
|
7677
|
+
whoActs: text(task.whoActs),
|
|
7678
|
+
command: nullableText(task.command),
|
|
7679
|
+
model: nullableText(task.model),
|
|
7680
|
+
rounds: task.rounds.map(roundView),
|
|
7681
|
+
tokens
|
|
7682
|
+
};
|
|
7683
|
+
}
|
|
7684
|
+
function statusView(snapshot) {
|
|
7685
|
+
const { live, connection, reason } = snapshot.status;
|
|
7686
|
+
return { live, connection, reason: nullableText(reason) };
|
|
7687
|
+
}
|
|
7688
|
+
function coverageView(snapshot) {
|
|
7689
|
+
return {
|
|
7690
|
+
complete: snapshot.coverage.complete,
|
|
7691
|
+
limits: snapshot.coverage.limits.map((l) => ({ kind: l.kind, detail: text(l.detail) }))
|
|
7692
|
+
};
|
|
7693
|
+
}
|
|
7694
|
+
function repositoryView(id, snapshot) {
|
|
7695
|
+
return {
|
|
7696
|
+
id,
|
|
7697
|
+
remote: snapshot.remote,
|
|
7698
|
+
status: statusView(snapshot),
|
|
7699
|
+
coverage: coverageView(snapshot),
|
|
7700
|
+
tasks: snapshot.picture.tasks.slice(0, TASKS_PER_REPOSITORY_MAX).map(taskView)
|
|
7701
|
+
};
|
|
7702
|
+
}
|
|
7703
|
+
|
|
7704
|
+
// src/server/live-socket.ts
|
|
7705
|
+
var SOFT_BUFFER_BYTES = 1024 * 1024;
|
|
7706
|
+
var HARD_BUFFER_BYTES = 4 * 1024 * 1024;
|
|
7707
|
+
var CLOSE_PROTOCOL = 1008;
|
|
7708
|
+
var CLOSE_TOO_BIG = 1009;
|
|
7709
|
+
var CLOSE_TRY_LATER = 1013;
|
|
7710
|
+
var CLOSE_GOING_AWAY = 1001;
|
|
7711
|
+
var OPEN = 1;
|
|
7712
|
+
var realTimers = {
|
|
7713
|
+
setInterval: (handler, ms) => setInterval(handler, ms),
|
|
7714
|
+
clearInterval: (handle) => clearInterval(handle)
|
|
7715
|
+
};
|
|
7716
|
+
function frameText(data) {
|
|
7717
|
+
if (typeof data === "string")
|
|
7718
|
+
return data;
|
|
7719
|
+
if (data instanceof Uint8Array)
|
|
7720
|
+
return Buffer.from(data).toString("utf8");
|
|
7721
|
+
if (Array.isArray(data) && data.every((d) => d instanceof Uint8Array))
|
|
7722
|
+
return Buffer.concat(data).toString("utf8");
|
|
7723
|
+
return null;
|
|
7724
|
+
}
|
|
7725
|
+
function createLiveSocket(options) {
|
|
7726
|
+
const timers = options.timers ?? realTimers;
|
|
7727
|
+
const keepAliveMs = options.keepAliveMs ?? KEEPALIVE_MS;
|
|
7728
|
+
const soft = options.softBufferBytes ?? SOFT_BUFFER_BYTES;
|
|
7729
|
+
const hard = options.hardBufferBytes ?? HARD_BUFFER_BYTES;
|
|
7730
|
+
const ids = new Map(options.repositories.map((r, i) => [r.root, `r${i + 1}`]));
|
|
7731
|
+
const clients = new Set;
|
|
7732
|
+
let closed = false;
|
|
7733
|
+
const idOf = (snapshot) => ids.get(snapshot.repository) ?? null;
|
|
7734
|
+
function drop(client, code, reason) {
|
|
7735
|
+
if (!clients.delete(client))
|
|
7736
|
+
return;
|
|
7737
|
+
timers.clearInterval(client.keepAlive);
|
|
7738
|
+
try {
|
|
7739
|
+
client.socket.close(code, reason);
|
|
7740
|
+
} catch {}
|
|
7741
|
+
}
|
|
7742
|
+
function writable(client) {
|
|
7743
|
+
if (client.socket.readyState !== OPEN)
|
|
7744
|
+
return false;
|
|
7745
|
+
const buffered = client.socket.bufferedAmount;
|
|
7746
|
+
if (buffered > hard) {
|
|
7747
|
+
drop(client, CLOSE_TRY_LATER, "too slow");
|
|
7748
|
+
return false;
|
|
7749
|
+
}
|
|
7750
|
+
if (buffered > soft) {
|
|
7751
|
+
client.stale = true;
|
|
7752
|
+
return false;
|
|
7753
|
+
}
|
|
7754
|
+
return true;
|
|
7755
|
+
}
|
|
7756
|
+
function write(client, message) {
|
|
7757
|
+
try {
|
|
7758
|
+
client.socket.send(JSON.stringify(message));
|
|
7759
|
+
} catch {
|
|
7760
|
+
drop(client, CLOSE_TRY_LATER, "send failed");
|
|
7761
|
+
}
|
|
7762
|
+
}
|
|
7763
|
+
function sendSnapshot(client) {
|
|
7764
|
+
client.generation += 1;
|
|
7765
|
+
client.stale = false;
|
|
7766
|
+
const repositories = options.service.snapshots().flatMap((s) => {
|
|
7767
|
+
const id = idOf(s);
|
|
7768
|
+
return id === null ? [] : [repositoryView(id, s)];
|
|
7769
|
+
});
|
|
7770
|
+
write(client, { v: PROTOCOL_VERSION, type: "snapshot", generation: client.generation, repositories });
|
|
7771
|
+
}
|
|
7772
|
+
function deliver(client, build) {
|
|
7773
|
+
if (!writable(client))
|
|
7774
|
+
return;
|
|
7775
|
+
if (client.stale) {
|
|
7776
|
+
sendSnapshot(client);
|
|
7777
|
+
return;
|
|
7778
|
+
}
|
|
7779
|
+
const message = build();
|
|
7780
|
+
if (message)
|
|
7781
|
+
write(client, message);
|
|
7782
|
+
}
|
|
7783
|
+
const leave = options.service.subscribe((event) => {
|
|
7784
|
+
const id = idOf(event.snapshot);
|
|
7785
|
+
if (id === null)
|
|
7786
|
+
return;
|
|
7787
|
+
for (const client of [...clients]) {
|
|
7788
|
+
if (event.type === "snapshot") {
|
|
7789
|
+
if (writable(client))
|
|
7790
|
+
sendSnapshot(client);
|
|
7791
|
+
} else if (event.type === "status") {
|
|
7792
|
+
deliver(client, () => ({
|
|
7793
|
+
v: PROTOCOL_VERSION,
|
|
7794
|
+
type: "status",
|
|
7795
|
+
generation: client.generation,
|
|
7796
|
+
repository: id,
|
|
7797
|
+
status: statusView(event.snapshot),
|
|
7798
|
+
coverage: coverageView(event.snapshot)
|
|
7799
|
+
}));
|
|
7800
|
+
} else {
|
|
7801
|
+
if (!writable(client))
|
|
7802
|
+
continue;
|
|
7803
|
+
if (client.stale) {
|
|
7804
|
+
sendSnapshot(client);
|
|
7805
|
+
continue;
|
|
7806
|
+
}
|
|
7807
|
+
for (const number of event.tasks) {
|
|
7808
|
+
const task = event.snapshot.picture.tasks.find((t) => t.task === number);
|
|
7809
|
+
if (!task || !writable(client))
|
|
7810
|
+
continue;
|
|
7811
|
+
write(client, {
|
|
7812
|
+
v: PROTOCOL_VERSION,
|
|
7813
|
+
type: "task",
|
|
7814
|
+
generation: client.generation,
|
|
7815
|
+
repository: id,
|
|
7816
|
+
task: taskView(task)
|
|
7817
|
+
});
|
|
7818
|
+
}
|
|
7819
|
+
deliver(client, () => ({
|
|
7820
|
+
v: PROTOCOL_VERSION,
|
|
7821
|
+
type: "status",
|
|
7822
|
+
generation: client.generation,
|
|
7823
|
+
repository: id,
|
|
7824
|
+
status: statusView(event.snapshot),
|
|
7825
|
+
coverage: coverageView(event.snapshot)
|
|
7826
|
+
}));
|
|
7827
|
+
}
|
|
7828
|
+
}
|
|
7829
|
+
});
|
|
7830
|
+
return {
|
|
7831
|
+
get connections() {
|
|
7832
|
+
return clients.size;
|
|
7833
|
+
},
|
|
7834
|
+
handler(socket) {
|
|
7835
|
+
if (closed) {
|
|
7836
|
+
socket.close(CLOSE_GOING_AWAY, "closing");
|
|
7837
|
+
return;
|
|
7838
|
+
}
|
|
7839
|
+
const client = { socket, generation: 0, stale: false, keepAlive: null };
|
|
7840
|
+
clients.add(client);
|
|
7841
|
+
client.keepAlive = timers.setInterval(() => {
|
|
7842
|
+
if (!writable(client))
|
|
7843
|
+
return;
|
|
7844
|
+
if (client.stale)
|
|
7845
|
+
return sendSnapshot(client);
|
|
7846
|
+
write(client, { v: PROTOCOL_VERSION, type: "keepalive" });
|
|
7847
|
+
}, keepAliveMs);
|
|
7848
|
+
socket.on("message", (data) => {
|
|
7849
|
+
const textual = frameText(data);
|
|
7850
|
+
if (textual === null || textual.length > CLIENT_MESSAGE_MAX_BYTES) {
|
|
7851
|
+
return drop(client, CLOSE_TOO_BIG, "message too large");
|
|
7852
|
+
}
|
|
7853
|
+
if (parseClientMessage(textual) === null)
|
|
7854
|
+
return drop(client, CLOSE_PROTOCOL, "unknown message");
|
|
7855
|
+
if (writable(client))
|
|
7856
|
+
sendSnapshot(client);
|
|
7857
|
+
});
|
|
7858
|
+
socket.on("close", () => {
|
|
7859
|
+
if (clients.delete(client))
|
|
7860
|
+
timers.clearInterval(client.keepAlive);
|
|
7861
|
+
});
|
|
7862
|
+
socket.on("error", () => drop(client, CLOSE_PROTOCOL, "socket error"));
|
|
7863
|
+
sendSnapshot(client);
|
|
7864
|
+
},
|
|
7865
|
+
close() {
|
|
7866
|
+
if (closed)
|
|
7867
|
+
return;
|
|
7868
|
+
closed = true;
|
|
7869
|
+
leave();
|
|
7870
|
+
for (const client of [...clients])
|
|
7871
|
+
drop(client, CLOSE_GOING_AWAY, "closing");
|
|
7872
|
+
}
|
|
7873
|
+
};
|
|
7874
|
+
}
|
|
7875
|
+
|
|
7876
|
+
// ../../harness/kernel/log/sync/normalize.ts
|
|
7877
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
7878
|
+
|
|
7879
|
+
// ../../harness/kernel/log/redact.ts
|
|
7880
|
+
var GITHUB_TOKEN_PATTERN = /\b(?:gho_|ghp_|ghs_|ghu_|ghr_|github_pat_)[A-Za-z0-9_]+\b/g;
|
|
7881
|
+
var CLASSIC_GITHUB_PAT_PATTERN = /\b((?:token|GITHUB_TOKEN|PAT|api[_-]?key|api[_-]?token)\s*[:=]\s*)[a-f0-9]{40}\b/gi;
|
|
7882
|
+
var BEARER_TOKEN_PATTERN = /\bBearer\s+\S+/g;
|
|
7883
|
+
var AWS_ACCESS_KEY_PATTERN = /\bAKIA[0-9A-Z]{16}\b/g;
|
|
7884
|
+
var SLACK_TOKEN_PATTERN = /\bxox[baprs]-[0-9A-Za-z-]+\b/g;
|
|
7885
|
+
var STRIPE_KEY_PATTERN = /\b(?:sk|pk|rk)_(?:live|test)_[0-9A-Za-z]{10,}\b/g;
|
|
7886
|
+
var ANTHROPIC_KEY_PATTERN = /\bsk-ant-[A-Za-z0-9_-]{20,}\b/g;
|
|
7887
|
+
var OPENAI_KEY_PATTERN = /\bsk-(?!ant-)[A-Za-z0-9]{20,}\b/g;
|
|
7888
|
+
var NPM_TOKEN_PATTERN = /\bnpm_[A-Za-z0-9]{36}\b/g;
|
|
7889
|
+
var JWT_PATTERN = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g;
|
|
7890
|
+
var PEM_BLOCK_PATTERN = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g;
|
|
7891
|
+
var BASIC_AUTH_URL_PATTERN = /(https?:\/\/)[^/\s:@]+:[^/\s@]+@/g;
|
|
7892
|
+
var GENERIC_SECRET_ASSIGNMENT_PATTERN = /\b((?:secret|password|passwd|api[_-]?key|access[_-]?key|client[_-]?secret|private[_-]?key)\s*[:=]\s*)[A-Za-z0-9+/_=-]{16,}\b/gi;
|
|
7893
|
+
function redactString(value, home) {
|
|
7894
|
+
let out = value.replace(GITHUB_TOKEN_PATTERN, "<redacted>").replace(CLASSIC_GITHUB_PAT_PATTERN, "$1<redacted>").replace(BEARER_TOKEN_PATTERN, "<redacted>").replace(AWS_ACCESS_KEY_PATTERN, "<redacted>").replace(SLACK_TOKEN_PATTERN, "<redacted>").replace(STRIPE_KEY_PATTERN, "<redacted>").replace(ANTHROPIC_KEY_PATTERN, "<redacted>").replace(OPENAI_KEY_PATTERN, "<redacted>").replace(NPM_TOKEN_PATTERN, "<redacted>").replace(JWT_PATTERN, "<redacted>").replace(PEM_BLOCK_PATTERN, "<redacted>").replace(BASIC_AUTH_URL_PATTERN, "$1<redacted>@").replace(GENERIC_SECRET_ASSIGNMENT_PATTERN, "$1<redacted>");
|
|
7895
|
+
if (home.length > 0 && out.startsWith(home)) {
|
|
7896
|
+
const rest = out.slice(home.length);
|
|
7897
|
+
if (rest === "" || rest.startsWith("/"))
|
|
7898
|
+
out = `~${rest}`;
|
|
7899
|
+
}
|
|
7900
|
+
return out;
|
|
7901
|
+
}
|
|
7902
|
+
function redact(value, home) {
|
|
7903
|
+
if (typeof value === "string")
|
|
7904
|
+
return redactString(value, home);
|
|
7905
|
+
if (Array.isArray(value))
|
|
7906
|
+
return value.map((v) => redact(v, home));
|
|
7907
|
+
if (value !== null && typeof value === "object") {
|
|
7908
|
+
const out = {};
|
|
7909
|
+
for (const [k, v] of Object.entries(value)) {
|
|
7910
|
+
out[k] = redact(v, home);
|
|
7911
|
+
}
|
|
7912
|
+
return out;
|
|
7913
|
+
}
|
|
7914
|
+
return value;
|
|
7915
|
+
}
|
|
7916
|
+
|
|
7917
|
+
// ../../harness/kernel/log/schema.ts
|
|
7918
|
+
var CONFIDENCE_REASON_MAX_LENGTH = 280;
|
|
7919
|
+
var REVIEW_FINDING_DESCRIPTION_MAX_LENGTH = 1000;
|
|
7920
|
+
var ROLE_NAME_PATTERN = /^[a-z0-9-]+$/;
|
|
7921
|
+
var RoleSchema = stringType().regex(ROLE_NAME_PATTERN);
|
|
7922
|
+
var HOST_VALUES = ["hook", "ci", "cli", "loop"];
|
|
7923
|
+
var HostSchema = enumType(HOST_VALUES);
|
|
7924
|
+
var RUN_ID_PATTERN = /^[A-Za-z0-9_.-]{1,128}$/;
|
|
7925
|
+
var headerMetaCore = {
|
|
7926
|
+
ts: stringType(),
|
|
7927
|
+
run_id: stringType().regex(RUN_ID_PATTERN),
|
|
7928
|
+
seq: numberType().int().nonnegative(),
|
|
7929
|
+
repo: stringType().nullable(),
|
|
7930
|
+
vinaya: stringType(),
|
|
7931
|
+
doctrine: stringType(),
|
|
7932
|
+
host: HostSchema,
|
|
7933
|
+
machine: stringType()
|
|
7934
|
+
};
|
|
7935
|
+
var HeaderMetaV1Schema = objectType({
|
|
7936
|
+
schema: literalType(1),
|
|
7937
|
+
...headerMetaCore
|
|
7938
|
+
}).strict();
|
|
7939
|
+
var LineageSchema = objectType({
|
|
7940
|
+
run: stringType().nullable(),
|
|
7941
|
+
attempt: numberType().int().nullable(),
|
|
7942
|
+
parent: stringType().nullable()
|
|
7943
|
+
}).strict();
|
|
7944
|
+
var InputVersionsSchema = objectType({
|
|
7945
|
+
manifest_version: literalType(2).nullable().optional(),
|
|
7946
|
+
objectives_version: stringType().nullable(),
|
|
7947
|
+
brief_hash: stringType().nullable(),
|
|
7948
|
+
ruling_ordinal: numberType().int().nullable(),
|
|
7949
|
+
pause_bound_ruling: objectType({ rulingId: stringType().min(1), pauseId: stringType().min(1), contentDigest: stringType().min(1) }).strict().nullable().optional(),
|
|
7950
|
+
policy_digest: stringType().nullable()
|
|
7951
|
+
}).strict();
|
|
7952
|
+
var ProvenanceSchema = enumType(["parent_attributed", "env_correlated", "self_reported", "unavailable"]);
|
|
7953
|
+
var HeaderMetaV2Schema = objectType({
|
|
7954
|
+
schema: literalType(2),
|
|
7955
|
+
...headerMetaCore,
|
|
7956
|
+
event_id: stringType().min(1),
|
|
7957
|
+
process_id: stringType().min(1),
|
|
7958
|
+
actor_id: stringType().nullable(),
|
|
7959
|
+
lineage: LineageSchema,
|
|
7960
|
+
input_versions: InputVersionsSchema,
|
|
7961
|
+
provenance: ProvenanceSchema
|
|
7962
|
+
}).strict();
|
|
7963
|
+
var WorkSchema = objectType({
|
|
7964
|
+
ref: stringType().nullable(),
|
|
7965
|
+
repo: stringType().nullable(),
|
|
7966
|
+
change: stringType().nullable(),
|
|
7967
|
+
revision: stringType().nullable()
|
|
7968
|
+
}).strict();
|
|
7969
|
+
var FlowSchema = objectType({
|
|
7970
|
+
id: stringType().nullable(),
|
|
7971
|
+
version: stringType().nullable()
|
|
7972
|
+
}).strict();
|
|
7973
|
+
var HeaderMetaV3Schema = objectType({
|
|
7974
|
+
schema: literalType(3),
|
|
7975
|
+
...headerMetaCore,
|
|
7976
|
+
event_id: stringType().min(1),
|
|
7977
|
+
process_id: stringType().min(1),
|
|
7978
|
+
actor_id: stringType().nullable(),
|
|
7979
|
+
lineage: LineageSchema,
|
|
7980
|
+
input_versions: InputVersionsSchema,
|
|
7981
|
+
provenance: ProvenanceSchema,
|
|
7982
|
+
work: WorkSchema,
|
|
7983
|
+
flow: FlowSchema,
|
|
7984
|
+
runtime: stringType().nullable(),
|
|
7985
|
+
source: stringType().nullable()
|
|
7986
|
+
}).strict();
|
|
7987
|
+
var HeaderMetaV4Schema = HeaderMetaV3Schema.extend({
|
|
7988
|
+
schema: literalType(4),
|
|
7989
|
+
process: objectType({ id: stringType(), version: stringType().nullable(), digest: stringType().nullable() }).strict(),
|
|
7990
|
+
runner: objectType({ id: stringType(), version: stringType().nullable() }).strict()
|
|
7991
|
+
}).strict();
|
|
7992
|
+
var HeaderMetaSchema = discriminatedUnionType("schema", [
|
|
7993
|
+
HeaderMetaV1Schema,
|
|
7994
|
+
HeaderMetaV2Schema,
|
|
7995
|
+
HeaderMetaV3Schema,
|
|
7996
|
+
HeaderMetaV4Schema
|
|
7997
|
+
]);
|
|
7998
|
+
var SubjectSchema = objectType({
|
|
7999
|
+
issue: numberType().int().nullable(),
|
|
8000
|
+
pr: numberType().int().optional(),
|
|
8001
|
+
sha: stringType().optional(),
|
|
8002
|
+
role: unionType([RoleSchema, literalType("unattributed")]),
|
|
8003
|
+
round: numberType().int().optional(),
|
|
8004
|
+
objectives_version: stringType().optional()
|
|
8005
|
+
}).strict();
|
|
8006
|
+
var HeaderSchema = objectType({
|
|
8007
|
+
meta: HeaderMetaSchema,
|
|
8008
|
+
subject: SubjectSchema
|
|
8009
|
+
}).strict();
|
|
8010
|
+
var envelopeTail = {
|
|
8011
|
+
duration_ms: numberType().nonnegative().optional(),
|
|
8012
|
+
payload: objectType({}).strict()
|
|
8013
|
+
};
|
|
8014
|
+
var ReviewFindingSchema = objectType({
|
|
8015
|
+
id: stringType(),
|
|
8016
|
+
severity: stringType(),
|
|
8017
|
+
location: stringType().optional(),
|
|
8018
|
+
deferral_reason: enumType(["outside-surface", "unchanged-line"]).optional(),
|
|
8019
|
+
description: stringType().max(REVIEW_FINDING_DESCRIPTION_MAX_LENGTH).optional(),
|
|
8020
|
+
state: stringType().optional(),
|
|
8021
|
+
severity_scale: stringType().optional(),
|
|
8022
|
+
policy_treatment: enumType(["blocking", "non_blocking", "unavailable"]).optional(),
|
|
8023
|
+
confidence: numberType().min(0).max(1).optional(),
|
|
8024
|
+
confidence_scale: stringType().optional(),
|
|
8025
|
+
confidence_source: stringType().optional()
|
|
8026
|
+
}).strict();
|
|
8027
|
+
var DispatchOutcomeSchema = discriminatedUnionType("type", [
|
|
8028
|
+
objectType({ type: literalType("pr_opened"), pr: numberType().int(), head: stringType() }).strict(),
|
|
8029
|
+
objectType({
|
|
8030
|
+
type: literalType("round_pushed"),
|
|
8031
|
+
pr: numberType().int(),
|
|
8032
|
+
head: stringType(),
|
|
8033
|
+
comment_id: numberType().int()
|
|
8034
|
+
}).strict(),
|
|
8035
|
+
objectType({
|
|
8036
|
+
type: literalType("verdict"),
|
|
8037
|
+
verdict: enumType(["APPROVE", "REQUEST CHANGES", "PASS", "FAIL"]),
|
|
8038
|
+
head: stringType(),
|
|
8039
|
+
comment_id: numberType().int(),
|
|
8040
|
+
objectives: arrayType(objectType({ id: stringType(), met: booleanType() }).strict()),
|
|
8041
|
+
findings: arrayType(ReviewFindingSchema)
|
|
8042
|
+
}).strict(),
|
|
8043
|
+
objectType({
|
|
8044
|
+
type: literalType("escalation"),
|
|
8045
|
+
class: enumType(["authority", "strategy", "product"]),
|
|
8046
|
+
comment_id: numberType().int()
|
|
8047
|
+
}).strict(),
|
|
8048
|
+
objectType({ type: literalType("brief"), comment_id: numberType().int(), hash: stringType() }).strict(),
|
|
8049
|
+
objectType({ type: literalType("plan"), issues: arrayType(numberType().int()) }).strict(),
|
|
8050
|
+
objectType({ type: literalType("archive"), provenance_comment_id: numberType().int() }).strict(),
|
|
8051
|
+
objectType({ type: literalType("completed") }).strict()
|
|
8052
|
+
]);
|
|
8053
|
+
var dispatchShared = {
|
|
8054
|
+
meta: HeaderMetaSchema,
|
|
8055
|
+
subject: SubjectSchema,
|
|
8056
|
+
kind: literalType("dispatch"),
|
|
8057
|
+
...envelopeTail,
|
|
8058
|
+
target_role: RoleSchema,
|
|
8059
|
+
model: stringType(),
|
|
8060
|
+
round: numberType().int().optional(),
|
|
8061
|
+
effect_id: stringType()
|
|
8062
|
+
};
|
|
8063
|
+
var dispatchUsageField = objectType({ input: numberType().nonnegative(), output: numberType().nonnegative() }).strict().nullable();
|
|
8064
|
+
var DispatchEventSchema = discriminatedUnionType("event", [
|
|
8065
|
+
objectType({ ...dispatchShared, event: literalType("dispatched"), prompt_hash: stringType() }).strict(),
|
|
8066
|
+
objectType({
|
|
8067
|
+
...dispatchShared,
|
|
8068
|
+
event: literalType("outcome_received"),
|
|
8069
|
+
outcome: DispatchOutcomeSchema,
|
|
8070
|
+
usage: dispatchUsageField
|
|
8071
|
+
}).strict(),
|
|
8072
|
+
objectType({
|
|
8073
|
+
...dispatchShared,
|
|
8074
|
+
event: literalType("dispatch_failed"),
|
|
8075
|
+
reason: enumType(["timeout", "crash", "refused", "unattributed_write", "usage_limit"]),
|
|
8076
|
+
usage_limit: objectType({ agent: stringType().min(1), reset_at: stringType().nullable() }).strict().optional(),
|
|
8077
|
+
usage: dispatchUsageField
|
|
8078
|
+
}).strict()
|
|
8079
|
+
]);
|
|
8080
|
+
var CODE_TOKEN_PATTERN = /^[A-Za-z0-9._-]{1,64}$/;
|
|
8081
|
+
var CodeTokenSchema = stringType().regex(CODE_TOKEN_PATTERN);
|
|
8082
|
+
var ReviewerVerdictSchema = objectType({
|
|
8083
|
+
role: RoleSchema,
|
|
8084
|
+
outcome: enumType(["approve", "changes_requested", "not_reviewed"]),
|
|
8085
|
+
blockers: numberType().int().nonnegative()
|
|
8086
|
+
}).strict();
|
|
8087
|
+
var loopShared = {
|
|
8088
|
+
meta: HeaderMetaSchema,
|
|
8089
|
+
subject: SubjectSchema,
|
|
8090
|
+
kind: literalType("dev_review_loop"),
|
|
8091
|
+
...envelopeTail,
|
|
8092
|
+
loop_id: stringType()
|
|
8093
|
+
};
|
|
8094
|
+
var DevReviewLoopEventSchema = discriminatedUnionType("event", [
|
|
8095
|
+
objectType({
|
|
8096
|
+
...loopShared,
|
|
8097
|
+
event: literalType("loop_started"),
|
|
8098
|
+
task: numberType().int(),
|
|
8099
|
+
title: stringType().optional(),
|
|
8100
|
+
policy: objectType({
|
|
8101
|
+
max_rounds: numberType().int().nonnegative(),
|
|
8102
|
+
reviewers: arrayType(RoleSchema),
|
|
8103
|
+
models: recordType(RoleSchema, stringType())
|
|
8104
|
+
}).strict()
|
|
8105
|
+
}).strict(),
|
|
8106
|
+
objectType({ ...loopShared, event: literalType("round_started"), round: numberType().int(), base_head: stringType() }).strict(),
|
|
8107
|
+
objectType({
|
|
8108
|
+
...loopShared,
|
|
8109
|
+
event: literalType("gate_result_read"),
|
|
8110
|
+
round: numberType().int(),
|
|
8111
|
+
head: stringType(),
|
|
8112
|
+
green: booleanType(),
|
|
8113
|
+
confidence_value: numberType().int().min(0).max(100).optional(),
|
|
8114
|
+
confidence_reason: stringType().max(CONFIDENCE_REASON_MAX_LENGTH).optional(),
|
|
8115
|
+
confidence_unavailable: booleanType().optional(),
|
|
8116
|
+
extra_turn_spent: booleanType().optional()
|
|
8117
|
+
}).strict(),
|
|
8118
|
+
objectType({
|
|
8119
|
+
...loopShared,
|
|
8120
|
+
event: literalType("verdicts_read"),
|
|
8121
|
+
round: numberType().int(),
|
|
8122
|
+
head: stringType(),
|
|
8123
|
+
all_approve: booleanType(),
|
|
8124
|
+
blockers: numberType().int().nonnegative(),
|
|
8125
|
+
findings: arrayType(ReviewFindingSchema).optional(),
|
|
8126
|
+
reviewers: arrayType(ReviewerVerdictSchema).optional()
|
|
8127
|
+
}).strict(),
|
|
8128
|
+
objectType({
|
|
8129
|
+
...loopShared,
|
|
8130
|
+
event: literalType("findings_compared"),
|
|
8131
|
+
round: numberType().int(),
|
|
8132
|
+
open: arrayType(stringType()),
|
|
8133
|
+
resolved: arrayType(stringType()),
|
|
8134
|
+
new: arrayType(stringType()),
|
|
8135
|
+
recurring: arrayType(stringType())
|
|
8136
|
+
}).strict(),
|
|
8137
|
+
objectType({
|
|
8138
|
+
...loopShared,
|
|
8139
|
+
event: literalType("stop_condition_met"),
|
|
8140
|
+
round: numberType().int(),
|
|
8141
|
+
condition: enumType([
|
|
8142
|
+
"green",
|
|
8143
|
+
"max_rounds",
|
|
8144
|
+
"no_progress",
|
|
8145
|
+
"escalated",
|
|
8146
|
+
"principal_stop",
|
|
8147
|
+
"confidence",
|
|
8148
|
+
"reappearance",
|
|
8149
|
+
"repeat_finding",
|
|
8150
|
+
"repeat_failure",
|
|
8151
|
+
"time_budget"
|
|
8152
|
+
])
|
|
8153
|
+
}).strict(),
|
|
8154
|
+
objectType({
|
|
8155
|
+
...loopShared,
|
|
8156
|
+
event: literalType("paused"),
|
|
8157
|
+
round: numberType().int(),
|
|
8158
|
+
reason: enumType(["escalation", "principal_item", "refreeze_needed"]),
|
|
8159
|
+
reason_code: CodeTokenSchema.optional()
|
|
8160
|
+
}).strict(),
|
|
8161
|
+
objectType({
|
|
8162
|
+
...loopShared,
|
|
8163
|
+
event: literalType("resumed"),
|
|
8164
|
+
round: numberType().int(),
|
|
8165
|
+
by: enumType(["principal", "driver"])
|
|
8166
|
+
}).strict(),
|
|
8167
|
+
objectType({
|
|
8168
|
+
...loopShared,
|
|
8169
|
+
event: literalType("cancelled"),
|
|
8170
|
+
round: numberType().int(),
|
|
8171
|
+
by: literalType("principal")
|
|
8172
|
+
}).strict(),
|
|
8173
|
+
objectType({
|
|
8174
|
+
...loopShared,
|
|
8175
|
+
event: literalType("unpushed_work_resume"),
|
|
8176
|
+
round: numberType().int(),
|
|
8177
|
+
branch: stringType(),
|
|
8178
|
+
detail: stringType()
|
|
8179
|
+
}).strict(),
|
|
8180
|
+
objectType({
|
|
8181
|
+
...loopShared,
|
|
8182
|
+
event: literalType("round_ended"),
|
|
8183
|
+
round: numberType().int(),
|
|
8184
|
+
base_head: stringType(),
|
|
8185
|
+
head: stringType(),
|
|
8186
|
+
files_changed: numberType().int().nonnegative(),
|
|
8187
|
+
insertions: numberType().int().nonnegative(),
|
|
8188
|
+
deletions: numberType().int().nonnegative(),
|
|
8189
|
+
wall_ms: numberType().nonnegative(),
|
|
8190
|
+
outcome: enumType(["green", "changes_requested", "escalated"])
|
|
8191
|
+
}).strict(),
|
|
8192
|
+
objectType({
|
|
8193
|
+
...loopShared,
|
|
8194
|
+
event: literalType("journal_finalized"),
|
|
8195
|
+
rounds: numberType().int().nonnegative(),
|
|
8196
|
+
total_wall_ms: numberType().nonnegative(),
|
|
8197
|
+
time_to_green_ms: numberType().nonnegative().nullable(),
|
|
8198
|
+
files_changed_total: numberType().int().nonnegative(),
|
|
8199
|
+
final_head: stringType(),
|
|
8200
|
+
result: enumType(["merged_ready", "stopped"])
|
|
8201
|
+
}).strict(),
|
|
8202
|
+
objectType({
|
|
8203
|
+
...loopShared,
|
|
8204
|
+
event: literalType("infrastructure_retry"),
|
|
8205
|
+
round: numberType().int(),
|
|
8206
|
+
failure_kind: enumType(["pause_comment_post", "developer_connection"]),
|
|
8207
|
+
attempts: numberType().int().positive(),
|
|
8208
|
+
outcome: enumType(["recovered", "exhausted"])
|
|
8209
|
+
}).strict(),
|
|
8210
|
+
objectType({
|
|
8211
|
+
...loopShared,
|
|
8212
|
+
event: literalType("driver_heartbeat"),
|
|
8213
|
+
task: numberType().int(),
|
|
8214
|
+
pr: numberType().int().positive().optional(),
|
|
8215
|
+
round: numberType().int(),
|
|
8216
|
+
phase: stringType()
|
|
8217
|
+
}).strict(),
|
|
8218
|
+
objectType({
|
|
8219
|
+
...loopShared,
|
|
8220
|
+
event: literalType("conflict_reentry"),
|
|
8221
|
+
task: numberType().int(),
|
|
8222
|
+
pr: numberType().int().positive(),
|
|
8223
|
+
attempt: numberType().int().positive(),
|
|
8224
|
+
bound: numberType().int().positive()
|
|
8225
|
+
}).strict(),
|
|
8226
|
+
objectType({
|
|
8227
|
+
...loopShared,
|
|
8228
|
+
event: literalType("storage_push"),
|
|
8229
|
+
round: numberType().int(),
|
|
8230
|
+
branch: stringType(),
|
|
8231
|
+
head: stringType()
|
|
8232
|
+
}).strict(),
|
|
8233
|
+
objectType({
|
|
8234
|
+
...loopShared,
|
|
8235
|
+
event: literalType("local_checks_run"),
|
|
8236
|
+
round: numberType().int(),
|
|
8237
|
+
head: stringType().nullable(),
|
|
8238
|
+
passed: booleanType(),
|
|
8239
|
+
wall_ms: numberType().nonnegative()
|
|
8240
|
+
}).strict(),
|
|
8241
|
+
objectType({
|
|
8242
|
+
...loopShared,
|
|
8243
|
+
event: literalType("ready_push"),
|
|
8244
|
+
round: numberType().int(),
|
|
8245
|
+
head: stringType(),
|
|
8246
|
+
outcome: enumType(["passed", "refused"]),
|
|
8247
|
+
selected: numberType().int().nonnegative(),
|
|
8248
|
+
failed: numberType().int().nonnegative(),
|
|
8249
|
+
rerun: booleanType(),
|
|
8250
|
+
wall_ms: numberType().nonnegative()
|
|
8251
|
+
}).strict(),
|
|
8252
|
+
objectType({
|
|
8253
|
+
...loopShared,
|
|
8254
|
+
event: literalType("failed_tests_rerun"),
|
|
8255
|
+
round: numberType().int(),
|
|
8256
|
+
head: stringType(),
|
|
8257
|
+
rerun_of: stringType(),
|
|
8258
|
+
failed_files: numberType().int().nonnegative(),
|
|
8259
|
+
affected_files: numberType().int().nonnegative(),
|
|
8260
|
+
full_count: numberType().int().nonnegative()
|
|
8261
|
+
}).strict(),
|
|
8262
|
+
objectType({
|
|
8263
|
+
...loopShared,
|
|
8264
|
+
event: literalType("pr_opened"),
|
|
8265
|
+
round: numberType().int(),
|
|
8266
|
+
pr: numberType().int().positive(),
|
|
8267
|
+
head: stringType()
|
|
8268
|
+
}).strict(),
|
|
8269
|
+
objectType({
|
|
8270
|
+
...loopShared,
|
|
8271
|
+
event: literalType("ci_result_read"),
|
|
8272
|
+
round: numberType().int(),
|
|
8273
|
+
pr: numberType().int().positive(),
|
|
8274
|
+
head: stringType(),
|
|
8275
|
+
conclusion: enumType(["green", "red", "pending", "skipped"]),
|
|
8276
|
+
failing_checks: arrayType(stringType()),
|
|
8277
|
+
wait_ms: numberType().nonnegative()
|
|
8278
|
+
}).strict(),
|
|
8279
|
+
objectType({
|
|
8280
|
+
...loopShared,
|
|
8281
|
+
event: literalType("phase_time"),
|
|
8282
|
+
round: numberType().int(),
|
|
8283
|
+
phase: enumType(["developer", "local_checks", "review", "push", "ci", "other"]),
|
|
8284
|
+
step: stringType(),
|
|
8285
|
+
wall_ms: numberType().nonnegative()
|
|
8286
|
+
}).strict(),
|
|
8287
|
+
objectType({
|
|
8288
|
+
...loopShared,
|
|
8289
|
+
event: literalType("driver_exited"),
|
|
8290
|
+
task: numberType().int(),
|
|
8291
|
+
reason: enumType(["finished", "paused", "reexec", "error", "signal"]),
|
|
8292
|
+
last_decision: stringType(),
|
|
8293
|
+
exit_code: numberType().int().optional(),
|
|
8294
|
+
error_class: CodeTokenSchema.optional()
|
|
8295
|
+
}).strict()
|
|
8296
|
+
]);
|
|
8297
|
+
var ForgeOpSchema = enumType([
|
|
8298
|
+
"pr.create",
|
|
8299
|
+
"pr.comment",
|
|
8300
|
+
"pr.body.replace",
|
|
8301
|
+
"pr.refreeze",
|
|
8302
|
+
"issue.create",
|
|
8303
|
+
"issue.edit",
|
|
8304
|
+
"issue.comment",
|
|
8305
|
+
"milestone.create",
|
|
8306
|
+
"milestone.edit",
|
|
8307
|
+
"milestone.close",
|
|
8308
|
+
"label.add",
|
|
8309
|
+
"label.remove"
|
|
8310
|
+
]);
|
|
8311
|
+
var ForgeWriteTargetSchema = objectType({
|
|
8312
|
+
issue: numberType().int().optional(),
|
|
8313
|
+
pr: numberType().int().optional()
|
|
8314
|
+
}).strict();
|
|
8315
|
+
var forgeWriteShared = {
|
|
8316
|
+
meta: HeaderMetaSchema,
|
|
8317
|
+
subject: SubjectSchema,
|
|
8318
|
+
kind: literalType("forge_write"),
|
|
8319
|
+
...envelopeTail,
|
|
8320
|
+
op: ForgeOpSchema,
|
|
8321
|
+
target: ForgeWriteTargetSchema
|
|
8322
|
+
};
|
|
8323
|
+
var ForgeWriteEventSchema = discriminatedUnionType("event", [
|
|
8324
|
+
objectType({ ...forgeWriteShared, event: literalType("validated") }).strict(),
|
|
8325
|
+
objectType({ ...forgeWriteShared, event: literalType("refused"), reason: stringType() }).strict(),
|
|
8326
|
+
objectType({ ...forgeWriteShared, event: literalType("written"), comment_ids: arrayType(stringType()) }).strict()
|
|
8327
|
+
]);
|
|
8328
|
+
var GateOutcomeSchema = enumType([
|
|
8329
|
+
"pass",
|
|
8330
|
+
"fail",
|
|
8331
|
+
"wait",
|
|
8332
|
+
"skip",
|
|
8333
|
+
"invalid_input",
|
|
8334
|
+
"unavailable_dependency",
|
|
8335
|
+
"timeout",
|
|
8336
|
+
"cancelled"
|
|
8337
|
+
]);
|
|
8338
|
+
var gateShared = {
|
|
8339
|
+
meta: HeaderMetaSchema,
|
|
8340
|
+
subject: SubjectSchema,
|
|
8341
|
+
kind: literalType("gate"),
|
|
8342
|
+
...envelopeTail,
|
|
8343
|
+
check: stringType(),
|
|
8344
|
+
check_version: stringType().nullable(),
|
|
8345
|
+
policy_version: stringType().nullable(),
|
|
8346
|
+
input_fingerprint: stringType().nullable()
|
|
8347
|
+
};
|
|
8348
|
+
var GateEventSchema = discriminatedUnionType("event", [
|
|
8349
|
+
objectType({
|
|
8350
|
+
...gateShared,
|
|
8351
|
+
event: literalType("checked"),
|
|
8352
|
+
outcome: GateOutcomeSchema,
|
|
8353
|
+
reason: stringType().optional()
|
|
8354
|
+
}).strict(),
|
|
8355
|
+
objectType({
|
|
8356
|
+
meta: HeaderMetaSchema,
|
|
8357
|
+
subject: SubjectSchema,
|
|
8358
|
+
kind: literalType("gate"),
|
|
8359
|
+
...envelopeTail,
|
|
8360
|
+
event: literalType("summary"),
|
|
8361
|
+
ran: numberType().int().nonnegative(),
|
|
8362
|
+
passed: numberType().int().nonnegative(),
|
|
8363
|
+
failed: numberType().int().nonnegative(),
|
|
8364
|
+
skipped: numberType().int().nonnegative(),
|
|
8365
|
+
failed_checks: arrayType(stringType())
|
|
8366
|
+
}).strict()
|
|
8367
|
+
]);
|
|
8368
|
+
var OperationResultSchema = enumType(["ok", "error", "refused", "timeout", "cancelled", "unavailable"]);
|
|
8369
|
+
var operationShared = {
|
|
8370
|
+
meta: HeaderMetaSchema,
|
|
8371
|
+
subject: SubjectSchema,
|
|
8372
|
+
kind: literalType("operation"),
|
|
8373
|
+
...envelopeTail,
|
|
8374
|
+
operation: stringType(),
|
|
8375
|
+
target: stringType().nullable()
|
|
8376
|
+
};
|
|
8377
|
+
var OperationEventSchema = discriminatedUnionType("event", [
|
|
8378
|
+
objectType({
|
|
8379
|
+
...operationShared,
|
|
8380
|
+
event: literalType("completed"),
|
|
8381
|
+
result: OperationResultSchema,
|
|
8382
|
+
error_class: stringType().nullable(),
|
|
8383
|
+
field_names: arrayType(stringType().max(CUSTOM_FIELD_NAME_MAX_LENGTH)).max(CUSTOM_EVENT_MAX_FIELDS).optional()
|
|
8384
|
+
}).strict()
|
|
8385
|
+
]);
|
|
8386
|
+
var UsageUnitsSchema = objectType({
|
|
8387
|
+
input: numberType().nonnegative().nullable(),
|
|
8388
|
+
output: numberType().nonnegative().nullable(),
|
|
8389
|
+
cache: numberType().nonnegative().nullable()
|
|
8390
|
+
}).strict();
|
|
8391
|
+
var usageShared = {
|
|
8392
|
+
meta: HeaderMetaSchema,
|
|
8393
|
+
subject: SubjectSchema,
|
|
8394
|
+
kind: literalType("usage"),
|
|
8395
|
+
...envelopeTail,
|
|
8396
|
+
model: stringType().nullable(),
|
|
8397
|
+
source: stringType(),
|
|
8398
|
+
semantics: enumType(["cumulative", "delta"])
|
|
8399
|
+
};
|
|
8400
|
+
var UsageEventSchema = discriminatedUnionType("event", [
|
|
8401
|
+
objectType({
|
|
8402
|
+
...usageShared,
|
|
8403
|
+
event: literalType("observed"),
|
|
8404
|
+
units: UsageUnitsSchema,
|
|
8405
|
+
unknown_reason: stringType().nullable()
|
|
8406
|
+
}).strict()
|
|
8407
|
+
]);
|
|
8408
|
+
var RoleAttemptOutcomeSchema = enumType([
|
|
8409
|
+
"completed",
|
|
8410
|
+
"incomplete",
|
|
8411
|
+
"infrastructure_failed",
|
|
8412
|
+
"cancelled",
|
|
8413
|
+
"timed_out",
|
|
8414
|
+
"capability_refused"
|
|
8415
|
+
]);
|
|
8416
|
+
var roleAttemptShared = {
|
|
8417
|
+
meta: HeaderMetaSchema,
|
|
8418
|
+
subject: SubjectSchema,
|
|
8419
|
+
kind: literalType("role_attempt"),
|
|
8420
|
+
...envelopeTail,
|
|
8421
|
+
actor: stringType().nullable(),
|
|
8422
|
+
attempt: numberType().int().nullable(),
|
|
8423
|
+
effect_id: stringType(),
|
|
8424
|
+
model: stringType().nullable()
|
|
8425
|
+
};
|
|
8426
|
+
var RoleAttemptEventSchema = discriminatedUnionType("event", [
|
|
8427
|
+
objectType({
|
|
8428
|
+
...roleAttemptShared,
|
|
8429
|
+
event: literalType("attempted"),
|
|
8430
|
+
outcome: RoleAttemptOutcomeSchema,
|
|
8431
|
+
usage: dispatchUsageField
|
|
8432
|
+
}).strict()
|
|
8433
|
+
]);
|
|
8434
|
+
var handoffShared = {
|
|
8435
|
+
meta: HeaderMetaSchema,
|
|
8436
|
+
subject: SubjectSchema,
|
|
8437
|
+
kind: literalType("handoff"),
|
|
8438
|
+
...envelopeTail,
|
|
8439
|
+
class: enumType(["authority", "strategy", "product"]),
|
|
8440
|
+
reason: stringType()
|
|
8441
|
+
};
|
|
8442
|
+
var HandoffEventSchema = discriminatedUnionType("event", [
|
|
8443
|
+
objectType({
|
|
8444
|
+
...handoffShared,
|
|
8445
|
+
event: literalType("raised"),
|
|
8446
|
+
requested_decision: stringType().nullable()
|
|
8447
|
+
}).strict(),
|
|
8448
|
+
objectType({
|
|
8449
|
+
...handoffShared,
|
|
8450
|
+
event: literalType("resolved"),
|
|
8451
|
+
resolution: stringType().nullable(),
|
|
8452
|
+
resolved_by: stringType().nullable()
|
|
8453
|
+
}).strict()
|
|
8454
|
+
]);
|
|
8455
|
+
var EffectTargetSchema = objectType({
|
|
8456
|
+
kind: stringType(),
|
|
8457
|
+
ref: stringType()
|
|
8458
|
+
}).strict();
|
|
8459
|
+
var effectShared = {
|
|
8460
|
+
meta: HeaderMetaSchema,
|
|
8461
|
+
subject: SubjectSchema,
|
|
8462
|
+
kind: literalType("effect"),
|
|
8463
|
+
...envelopeTail,
|
|
8464
|
+
effect_id: stringType(),
|
|
8465
|
+
target: EffectTargetSchema
|
|
8466
|
+
};
|
|
8467
|
+
var EffectEventSchema = discriminatedUnionType("event", [
|
|
8468
|
+
objectType({ ...effectShared, event: literalType("attempted") }).strict(),
|
|
8469
|
+
objectType({ ...effectShared, event: literalType("observed"), outcome: enumType(["success", "failure", "uncertain"]) }).strict(),
|
|
8470
|
+
objectType({ ...effectShared, event: literalType("verified"), outcome: enumType(["success", "failure", "uncertain"]) }).strict()
|
|
8471
|
+
]);
|
|
8472
|
+
var CustomFieldValueSchema = unionType([stringType().max(CUSTOM_TEXT_MAX_LENGTH), numberType().finite(), booleanType()]);
|
|
8473
|
+
var customShared = {
|
|
8474
|
+
meta: HeaderMetaSchema,
|
|
8475
|
+
subject: SubjectSchema,
|
|
8476
|
+
kind: literalType("custom"),
|
|
8477
|
+
...envelopeTail
|
|
8478
|
+
};
|
|
8479
|
+
var CustomEventSchema = discriminatedUnionType("event", [
|
|
8480
|
+
objectType({
|
|
8481
|
+
...customShared,
|
|
8482
|
+
event: literalType("recorded"),
|
|
8483
|
+
name: stringType().max(CUSTOM_EVENT_NAME_MAX_LENGTH).regex(CUSTOM_EVENT_NAME_PATTERN),
|
|
8484
|
+
fields: recordType(stringType().max(CUSTOM_FIELD_NAME_MAX_LENGTH).regex(CUSTOM_FIELD_NAME_PATTERN), CustomFieldValueSchema).refine((f) => Object.keys(f).length <= CUSTOM_EVENT_MAX_FIELDS, {
|
|
8485
|
+
message: `custom: at most ${CUSTOM_EVENT_MAX_FIELDS} fields`
|
|
8486
|
+
})
|
|
8487
|
+
}).strict()
|
|
8488
|
+
]);
|
|
8489
|
+
var LogEventSchema = unionType([
|
|
8490
|
+
DispatchEventSchema,
|
|
8491
|
+
DevReviewLoopEventSchema,
|
|
8492
|
+
ForgeWriteEventSchema,
|
|
8493
|
+
GateEventSchema,
|
|
8494
|
+
OperationEventSchema,
|
|
8495
|
+
UsageEventSchema,
|
|
8496
|
+
RoleAttemptEventSchema,
|
|
8497
|
+
HandoffEventSchema,
|
|
8498
|
+
EffectEventSchema,
|
|
8499
|
+
CustomEventSchema
|
|
8500
|
+
]);
|
|
8501
|
+
|
|
8502
|
+
// ../../harness/kernel/log/store.ts
|
|
8503
|
+
var KNOWN_SCHEMA_VERSIONS = [1, 2, 3, 4];
|
|
8504
|
+
function metaOf(obj) {
|
|
8505
|
+
if (obj === null || typeof obj !== "object")
|
|
8506
|
+
return null;
|
|
8507
|
+
const meta = obj.meta;
|
|
8508
|
+
if (meta === null || typeof meta !== "object")
|
|
8509
|
+
return null;
|
|
8510
|
+
return meta;
|
|
8511
|
+
}
|
|
8512
|
+
function recordIdentity(obj) {
|
|
8513
|
+
const meta = metaOf(obj);
|
|
8514
|
+
if (meta === null)
|
|
8515
|
+
return null;
|
|
8516
|
+
const eventId = meta.event_id;
|
|
8517
|
+
if (typeof eventId === "string" && eventId.length > 0)
|
|
8518
|
+
return eventId;
|
|
8519
|
+
const runId = meta.run_id;
|
|
8520
|
+
const seq = meta.seq;
|
|
8521
|
+
if (typeof runId === "string" && runId.length > 0 && typeof seq === "number" && Number.isInteger(seq)) {
|
|
8522
|
+
return `${runId}:${seq}`;
|
|
8523
|
+
}
|
|
8524
|
+
return null;
|
|
8525
|
+
}
|
|
8526
|
+
function schemaVersionOf(obj) {
|
|
8527
|
+
const meta = metaOf(obj);
|
|
8528
|
+
const schema = meta?.schema;
|
|
8529
|
+
return typeof schema === "number" ? schema : null;
|
|
8530
|
+
}
|
|
8531
|
+
function isKnownSchema(version) {
|
|
8532
|
+
return version !== null && KNOWN_SCHEMA_VERSIONS.includes(version);
|
|
8533
|
+
}
|
|
8534
|
+
function classifyStoredLine(raw, home) {
|
|
8535
|
+
let obj;
|
|
8536
|
+
try {
|
|
8537
|
+
obj = JSON.parse(raw);
|
|
8538
|
+
} catch {
|
|
8539
|
+
return { status: "invalid", identity: null, raw, reason: "not valid JSON" };
|
|
8540
|
+
}
|
|
8541
|
+
const version = schemaVersionOf(obj);
|
|
8542
|
+
if (version === null) {
|
|
8543
|
+
return { status: "invalid", identity: recordIdentity(obj), raw, reason: "no readable meta.schema" };
|
|
8544
|
+
}
|
|
8545
|
+
if (!isKnownSchema(version)) {
|
|
8546
|
+
return {
|
|
8547
|
+
status: "unknown_version",
|
|
8548
|
+
identity: recordIdentity(obj),
|
|
8549
|
+
schema: version,
|
|
8550
|
+
raw,
|
|
8551
|
+
reason: `schema version ${version} is not one this build validates (${KNOWN_SCHEMA_VERSIONS.join(", ")}) — kept for diagnosis`
|
|
8552
|
+
};
|
|
8553
|
+
}
|
|
8554
|
+
const result = LogEventSchema.safeParse(obj);
|
|
8555
|
+
if (!result.success) {
|
|
8556
|
+
return {
|
|
8557
|
+
status: "invalid",
|
|
8558
|
+
identity: recordIdentity(obj),
|
|
8559
|
+
raw,
|
|
8560
|
+
reason: result.error.issues[0]?.message ?? "schema violation"
|
|
8561
|
+
};
|
|
8562
|
+
}
|
|
8563
|
+
const event = result.data;
|
|
8564
|
+
const identity = recordIdentity(event);
|
|
8565
|
+
if (identity === null) {
|
|
8566
|
+
return { status: "invalid", identity: null, raw, reason: "valid event with no derivable identity" };
|
|
8567
|
+
}
|
|
8568
|
+
const redacted = redact(event, home);
|
|
8569
|
+
const provenance = version >= 2 ? event.meta.provenance ?? null : null;
|
|
8570
|
+
return {
|
|
8571
|
+
status: "ok",
|
|
8572
|
+
identity,
|
|
8573
|
+
runId: event.meta.run_id,
|
|
8574
|
+
seq: event.meta.seq,
|
|
8575
|
+
schema: version,
|
|
8576
|
+
provenance,
|
|
8577
|
+
event,
|
|
8578
|
+
postLine: JSON.stringify(redacted)
|
|
8579
|
+
};
|
|
8580
|
+
}
|
|
8581
|
+
|
|
8582
|
+
// ../../harness/kernel/log/sync/row.ts
|
|
8583
|
+
var LOW_TRUST_BELOW_VERSION = "0.33.0";
|
|
8584
|
+
|
|
8585
|
+
// ../../harness/kernel/log/sync/normalize.ts
|
|
8586
|
+
function sha256(text) {
|
|
8587
|
+
return createHash2("sha256").update(text).digest("hex");
|
|
8588
|
+
}
|
|
8589
|
+
function asObject(value) {
|
|
8590
|
+
return value !== null && typeof value === "object" ? value : {};
|
|
8591
|
+
}
|
|
8592
|
+
function has(obj, key) {
|
|
8593
|
+
return Object.hasOwn(obj, key);
|
|
8594
|
+
}
|
|
8595
|
+
function stringOrNull(value) {
|
|
8596
|
+
return typeof value === "string" ? value : null;
|
|
8597
|
+
}
|
|
8598
|
+
function integerOrNull(value) {
|
|
8599
|
+
return typeof value === "number" && Number.isInteger(value) ? value : null;
|
|
8600
|
+
}
|
|
8601
|
+
var VERSION_PATTERN = /^v?(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
|
|
8602
|
+
function parseVersion(text) {
|
|
8603
|
+
const match = VERSION_PATTERN.exec(text.trim());
|
|
8604
|
+
if (match === null)
|
|
8605
|
+
return null;
|
|
8606
|
+
return { core: [Number(match[1]), Number(match[2]), Number(match[3])], prerelease: match[4] !== undefined };
|
|
8607
|
+
}
|
|
8608
|
+
function isLowTrustVersion(version) {
|
|
8609
|
+
const parsed = parseVersion(version);
|
|
8610
|
+
const floor = parseVersion(LOW_TRUST_BELOW_VERSION);
|
|
8611
|
+
if (parsed === null)
|
|
8612
|
+
return true;
|
|
8613
|
+
for (let i = 0;i < 3; i++) {
|
|
8614
|
+
const a = parsed.core[i];
|
|
8615
|
+
const b = floor.core[i];
|
|
8616
|
+
if (a !== b)
|
|
8617
|
+
return a < b;
|
|
8618
|
+
}
|
|
8619
|
+
return parsed.prerelease;
|
|
8620
|
+
}
|
|
8621
|
+
function normalizeStoredLine(raw, origin = null) {
|
|
8622
|
+
const classified = classifyStoredLine(raw, "");
|
|
8623
|
+
if (classified.status !== "ok") {
|
|
8624
|
+
const raw = redact(classified.raw, "");
|
|
8625
|
+
return {
|
|
8626
|
+
type: "quarantine",
|
|
8627
|
+
record: {
|
|
8628
|
+
status: classified.status,
|
|
8629
|
+
identity: classified.identity,
|
|
8630
|
+
schema: classified.status === "unknown_version" ? classified.schema : null,
|
|
8631
|
+
reason: classified.reason,
|
|
8632
|
+
raw,
|
|
8633
|
+
contentHash: sha256(raw),
|
|
8634
|
+
origin
|
|
8635
|
+
}
|
|
8636
|
+
};
|
|
8637
|
+
}
|
|
8638
|
+
const stored = asObject(JSON.parse(classified.postLine));
|
|
8639
|
+
const meta = asObject(stored.meta);
|
|
8640
|
+
const subject = asObject(stored.subject);
|
|
8641
|
+
const schema = classified.schema;
|
|
8642
|
+
const unknown = {};
|
|
8643
|
+
const missing = (field, headerField) => {
|
|
8644
|
+
unknown[field] = `a schema ${schema} header has no ${headerField}`;
|
|
8645
|
+
return null;
|
|
8646
|
+
};
|
|
8647
|
+
const work = asObject(meta.work);
|
|
8648
|
+
const flow = asObject(meta.flow);
|
|
8649
|
+
const workRef = has(meta, "work") ? stringOrNull(work.ref) : missing("workRef", "work.ref");
|
|
8650
|
+
const actor = has(meta, "actor_id") ? stringOrNull(meta.actor_id) : missing("actor", "actor_id");
|
|
8651
|
+
const flowId = has(meta, "flow") ? stringOrNull(flow.id) : missing("flowId", "flow.id");
|
|
8652
|
+
const flowVersion = has(meta, "flow") ? stringOrNull(flow.version) : missing("flowVersion", "flow.version");
|
|
8653
|
+
const provenance = has(meta, "provenance") ? stringOrNull(meta.provenance) : missing("provenance", "provenance");
|
|
8654
|
+
const cliVersion = typeof meta.vinaya === "string" ? meta.vinaya : "";
|
|
8655
|
+
let trust;
|
|
8656
|
+
if (isLowTrustVersion(cliVersion))
|
|
8657
|
+
trust = "low";
|
|
8658
|
+
else if (provenance !== null)
|
|
8659
|
+
trust = provenance;
|
|
8660
|
+
else {
|
|
8661
|
+
trust = null;
|
|
8662
|
+
unknown.trust = `a schema ${schema} header declares no provenance`;
|
|
8663
|
+
}
|
|
8664
|
+
const payload = {};
|
|
8665
|
+
for (const [key, value] of Object.entries(stored)) {
|
|
8666
|
+
if (key !== "meta" && key !== "subject" && key !== "kind" && key !== "event")
|
|
8667
|
+
payload[key] = value;
|
|
8668
|
+
}
|
|
8669
|
+
const row = {
|
|
8670
|
+
identity: classified.identity,
|
|
8671
|
+
schema,
|
|
8672
|
+
kind: String(stored.kind),
|
|
8673
|
+
event: String(stored.event),
|
|
8674
|
+
time: String(meta.ts),
|
|
8675
|
+
runId: classified.runId,
|
|
8676
|
+
seq: classified.seq,
|
|
8677
|
+
workRef,
|
|
8678
|
+
actor,
|
|
8679
|
+
cliVersion,
|
|
8680
|
+
doctrine: String(meta.doctrine),
|
|
8681
|
+
flowId,
|
|
8682
|
+
flowVersion,
|
|
8683
|
+
host: String(meta.host),
|
|
8684
|
+
repo: stringOrNull(meta.repo),
|
|
8685
|
+
provenance,
|
|
8686
|
+
trust,
|
|
8687
|
+
issue: integerOrNull(subject.issue),
|
|
8688
|
+
pr: integerOrNull(subject.pr),
|
|
8689
|
+
round: integerOrNull(subject.round),
|
|
8690
|
+
commit: stringOrNull(subject.sha),
|
|
8691
|
+
role: String(subject.role),
|
|
8692
|
+
objectivesVersion: stringOrNull(subject.objectives_version),
|
|
8693
|
+
header: { meta, subject },
|
|
8694
|
+
payload,
|
|
8695
|
+
contentHash: sha256(classified.postLine),
|
|
8696
|
+
origin,
|
|
8697
|
+
unknown
|
|
8698
|
+
};
|
|
8699
|
+
return { type: "row", row };
|
|
8700
|
+
}
|
|
8701
|
+
|
|
8702
|
+
// ../../harness/processes/vinaya-strict/tools/mission-control/picture.ts
|
|
8703
|
+
var TASK_STATES = [
|
|
8704
|
+
"needs_you",
|
|
8705
|
+
"presumed_dead",
|
|
8706
|
+
"stopped_itself",
|
|
8707
|
+
"needs_a_look",
|
|
8708
|
+
"running",
|
|
8709
|
+
"ready_to_merge"
|
|
8710
|
+
];
|
|
8711
|
+
var AMBER_AFTER_MS = 5 * 60000;
|
|
8712
|
+
var PRESUMED_DEAD_AFTER_MS = 10 * 60000;
|
|
8713
|
+
var PERSON_NEEDED_PAUSE_CODES = [
|
|
8714
|
+
"developer_turn_blocked",
|
|
8715
|
+
"developer_turn_needs_ruling",
|
|
8716
|
+
"developer_turn_rejected",
|
|
8717
|
+
"developer_turn_stale",
|
|
8718
|
+
"developer_turn_no_adapter"
|
|
8719
|
+
];
|
|
8720
|
+
function emptyPicture() {
|
|
8721
|
+
return { now: null, readThrough: null, skipped: 0, repeated: 0, tasks: [], loops: {} };
|
|
8722
|
+
}
|
|
8723
|
+
|
|
8724
|
+
// ../../harness/kernel/log/roles.ts
|
|
8725
|
+
var roles = new Map;
|
|
8726
|
+
var titles = new Map;
|
|
8727
|
+
function registerRole(name, role) {
|
|
8728
|
+
roles.set(name, role);
|
|
8729
|
+
}
|
|
8730
|
+
function registerTitle(name, title) {
|
|
8731
|
+
titles.set(name, title);
|
|
8732
|
+
}
|
|
8733
|
+
function lookupRole(name) {
|
|
8734
|
+
return roles.get(name);
|
|
8735
|
+
}
|
|
8736
|
+
|
|
8737
|
+
// ../../harness/kernel/log/client/narration.ts
|
|
8738
|
+
var REASONS = {
|
|
8739
|
+
escalation: { words: "a role escalated a question", person: true },
|
|
8740
|
+
approved_repairs_spent: { words: "the approved repair turns ran out without a green CI", person: true },
|
|
8741
|
+
ready_push_failed: { words: "the ready push failed three consecutive times", person: true },
|
|
8742
|
+
max_rounds: { words: "the round limit was reached", person: true },
|
|
8743
|
+
no_progress: { words: "the rounds made no progress", person: true },
|
|
8744
|
+
confidence: { words: "the stated confidence collapsed", person: true },
|
|
8745
|
+
reappearance: { words: "a finding marked resolved came back", person: true },
|
|
8746
|
+
repeat_finding: { words: "the same blocking finding stayed open two rounds in a row", person: true },
|
|
8747
|
+
repeat_failure: { words: "the same mechanical failure happened twice in a row", person: true },
|
|
8748
|
+
time_budget: { words: "the time budget was passed", person: true },
|
|
8749
|
+
infrastructure: { words: "a role or file the round needed was missing", person: true },
|
|
8750
|
+
no_push: { words: "work was left unpushed", person: true },
|
|
8751
|
+
objectives_changed: { words: "the objectives changed during the round", person: false },
|
|
8752
|
+
ruling_posted: { words: "a ruling was posted during the round", person: false },
|
|
8753
|
+
stale_driver: { words: "the base branch moved past the driver code", person: false },
|
|
8754
|
+
brief_superseded: { words: "the brief was replaced during the round", person: false },
|
|
8755
|
+
policy_changed: { words: "the review policy changed during the round", person: false },
|
|
8756
|
+
sandbox_refused: { words: "the agent's sandbox could not run a command on this host", person: true },
|
|
8757
|
+
missing_source: { words: "a required source could not be prepared", person: true },
|
|
8758
|
+
principal_stop: { words: "an item needs a person to decide", person: true },
|
|
8759
|
+
escalated: { words: "a role escalated a question", person: true }
|
|
8760
|
+
};
|
|
8761
|
+
var PAUSE_WITHOUT_CODE = {
|
|
8762
|
+
escalation: "escalation",
|
|
8763
|
+
principal_item: "principal_stop"
|
|
8764
|
+
};
|
|
8765
|
+
function text2(value) {
|
|
8766
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
8767
|
+
}
|
|
8768
|
+
function whole(value) {
|
|
8769
|
+
return typeof value === "number" && Number.isInteger(value) ? value : null;
|
|
8770
|
+
}
|
|
8771
|
+
function code(value) {
|
|
8772
|
+
return typeof value === "string" && CODE_TOKEN_PATTERN.test(value) ? value : null;
|
|
8773
|
+
}
|
|
8774
|
+
function roleSubject(value, sentenceStart = true) {
|
|
8775
|
+
const known = typeof value === "string" ? lookupRole(value) : undefined;
|
|
8776
|
+
const name = known ? `the ${known.reading}` : "a role";
|
|
8777
|
+
return sentenceStart ? `${name[0]?.toUpperCase()}${name.slice(1)}` : name;
|
|
8778
|
+
}
|
|
8779
|
+
function roundLabel(record) {
|
|
8780
|
+
const round = whole(record.round);
|
|
8781
|
+
return round === null ? "The round" : `Round ${round}`;
|
|
8782
|
+
}
|
|
8783
|
+
function duration(ms) {
|
|
8784
|
+
if (typeof ms !== "number" || !Number.isFinite(ms) || ms < 0)
|
|
8785
|
+
return null;
|
|
8786
|
+
const seconds = Math.round(ms / 1000);
|
|
8787
|
+
return `${Math.floor(seconds / 60)}m ${seconds % 60}s`;
|
|
8788
|
+
}
|
|
8789
|
+
function reasonKey(reasonCode, fallbackReason) {
|
|
8790
|
+
return reasonCode ?? (fallbackReason ? PAUSE_WITHOUT_CODE[fallbackReason] ?? null : null);
|
|
8791
|
+
}
|
|
8792
|
+
function reasonWaitsOn(reasonCode, fallbackReason = null) {
|
|
8793
|
+
const key = reasonKey(reasonCode, fallbackReason);
|
|
8794
|
+
const known = key ? REASONS[key] : undefined;
|
|
8795
|
+
if (!known)
|
|
8796
|
+
return null;
|
|
8797
|
+
return known.person ? "person" : "loop";
|
|
8798
|
+
}
|
|
8799
|
+
function paused(reasonCode, fallbackReason) {
|
|
8800
|
+
const key = reasonKey(reasonCode, fallbackReason);
|
|
8801
|
+
const known = key ? REASONS[key] : undefined;
|
|
8802
|
+
if (known) {
|
|
8803
|
+
return known.person ? { kind: "blocked", text: `The loop paused for a person: ${known.words}.` } : { kind: "paused", text: `The loop paused itself: ${known.words}.` };
|
|
8804
|
+
}
|
|
8805
|
+
const named = reasonCode ?? code(fallbackReason);
|
|
8806
|
+
return { kind: "paused", text: named ? `Paused: ${named}.` : "Paused." };
|
|
8807
|
+
}
|
|
8808
|
+
function stopped(condition) {
|
|
8809
|
+
const name = code(condition);
|
|
8810
|
+
if (!name)
|
|
8811
|
+
return null;
|
|
8812
|
+
if (name === "green")
|
|
8813
|
+
return { kind: "done", text: "The loop stopped: every check and review passed." };
|
|
8814
|
+
const known = REASONS[name];
|
|
8815
|
+
if (!known)
|
|
8816
|
+
return { kind: "information", text: `Stopped: ${name}.` };
|
|
8817
|
+
return known.person ? { kind: "blocked", text: `The loop stopped itself: ${known.words}. A person has to act.` } : { kind: "paused", text: `The loop stopped itself: ${known.words}.` };
|
|
8818
|
+
}
|
|
8819
|
+
function confidenceClause(record, earlier) {
|
|
8820
|
+
let read = null;
|
|
8821
|
+
for (const candidate of earlier) {
|
|
8822
|
+
if (typeof candidate !== "object" || candidate === null)
|
|
8823
|
+
continue;
|
|
8824
|
+
const fields = candidate;
|
|
8825
|
+
if (fields.event === "gate_result_read" && fields.loop_id === record.loop_id && whole(fields.round) !== null && fields.round === record.round)
|
|
8826
|
+
read = fields;
|
|
8827
|
+
}
|
|
8828
|
+
if (!read)
|
|
8829
|
+
return null;
|
|
8830
|
+
if (read.confidence_unavailable === true)
|
|
8831
|
+
return "Confidence: not given.";
|
|
8832
|
+
const value = read.confidence_value;
|
|
8833
|
+
if (whole(value) !== null && value >= 0 && value <= 100)
|
|
8834
|
+
return `Confidence: ${value}${read.extra_turn_spent === true ? " (after the extra turn)" : ""}.`;
|
|
8835
|
+
return "Confidence: —.";
|
|
8836
|
+
}
|
|
8837
|
+
function verdicts(record) {
|
|
8838
|
+
const label = roundLabel(record);
|
|
8839
|
+
const blockers = whole(record.blockers) ?? 0;
|
|
8840
|
+
const reviewers = Array.isArray(record.reviewers) ? record.reviewers : [];
|
|
8841
|
+
const parts = [];
|
|
8842
|
+
for (const entry of reviewers) {
|
|
8843
|
+
if (typeof entry !== "object" || entry === null)
|
|
8844
|
+
continue;
|
|
8845
|
+
const { role, outcome, blockers: count } = entry;
|
|
8846
|
+
const who = roleSubject(role, parts.length === 0);
|
|
8847
|
+
if (outcome === "approve")
|
|
8848
|
+
parts.push(`${who} approved`);
|
|
8849
|
+
else if (outcome === "changes_requested")
|
|
8850
|
+
parts.push(`${who} asked for changes (${whole(count) ?? 0} blocking)`);
|
|
8851
|
+
else if (outcome === "not_reviewed")
|
|
8852
|
+
parts.push(`${who} did not review`);
|
|
8853
|
+
}
|
|
8854
|
+
const approved = record.all_approve === true;
|
|
8855
|
+
const detail = parts.length > 0 ? parts.join("; ") : approved ? "approved" : `${blockers} blocking`;
|
|
8856
|
+
return { kind: approved ? "done" : "information", text: `${label} verdicts: ${detail}.` };
|
|
8857
|
+
}
|
|
8858
|
+
function roundEnded(record, earlier) {
|
|
8859
|
+
const label = roundLabel(record);
|
|
8860
|
+
const took = duration(record.wall_ms);
|
|
8861
|
+
const outcome = record.outcome;
|
|
8862
|
+
const [kind, words] = outcome === "green" ? ["done", "approved"] : outcome === "changes_requested" ? ["information", "changes requested"] : outcome === "escalated" ? ["blocked", "a role escalated, a person has to decide"] : ["information", "ended"];
|
|
8863
|
+
const confidence = confidenceClause(record, earlier);
|
|
8864
|
+
return {
|
|
8865
|
+
kind,
|
|
8866
|
+
text: `${label} ended${took ? ` after ${took}` : ""}: ${words}.${confidence ? ` ${confidence}` : ""}`
|
|
8867
|
+
};
|
|
8868
|
+
}
|
|
8869
|
+
function driverExited(record) {
|
|
8870
|
+
const exit = whole(record.exit_code);
|
|
8871
|
+
const exitWords = exit === null ? "" : ` (exit code ${exit})`;
|
|
8872
|
+
switch (record.reason) {
|
|
8873
|
+
case "finished":
|
|
8874
|
+
return { kind: "done", text: `The driver finished${exitWords}.` };
|
|
8875
|
+
case "paused":
|
|
8876
|
+
return { kind: "paused", text: `The driver stopped because the loop paused${exitWords}.` };
|
|
8877
|
+
case "reexec":
|
|
8878
|
+
return { kind: "information", text: "The driver restarted itself on updated code." };
|
|
8879
|
+
case "error": {
|
|
8880
|
+
const kind = code(record.error_class);
|
|
8881
|
+
return { kind: "failed", text: `The driver stopped on an error${kind ? ` (${kind})` : ""}${exitWords}.` };
|
|
8882
|
+
}
|
|
8883
|
+
case "signal":
|
|
8884
|
+
return { kind: "failed", text: `The driver was stopped by a signal${exitWords}.` };
|
|
8885
|
+
default:
|
|
8886
|
+
return { kind: "information", text: `The driver exited${exitWords}.` };
|
|
8887
|
+
}
|
|
8888
|
+
}
|
|
8889
|
+
function conflictReentry(record) {
|
|
8890
|
+
const pr = whole(record.pr);
|
|
8891
|
+
const attempt = whole(record.attempt);
|
|
8892
|
+
const bound = whole(record.bound);
|
|
8893
|
+
const count = attempt !== null && bound !== null ? ` (re-entry ${attempt} of ${bound})` : "";
|
|
8894
|
+
return {
|
|
8895
|
+
kind: "working",
|
|
8896
|
+
text: `${pr === null ? "The pull request" : `Pull request #${pr}`} fell into conflict with the default branch after it was published: the loop re-entered by itself to send it back to the Developer${count}.`
|
|
8897
|
+
};
|
|
8898
|
+
}
|
|
8899
|
+
function dispatched(record) {
|
|
8900
|
+
const role = text2(record.target_role);
|
|
8901
|
+
const who = roleSubject(role);
|
|
8902
|
+
const reviewing = role !== null && (lookupRole(role)?.reviews ?? false);
|
|
8903
|
+
const round = whole(record.round);
|
|
8904
|
+
return {
|
|
8905
|
+
kind: "working",
|
|
8906
|
+
text: `${who} is ${reviewing ? "reviewing" : "working"}${round === null ? "" : `${reviewing ? "" : " on"} round ${round}`}.`
|
|
8907
|
+
};
|
|
8908
|
+
}
|
|
8909
|
+
function outcomeReceived(record) {
|
|
8910
|
+
const who = roleSubject(record.target_role);
|
|
8911
|
+
const outcome = record.outcome;
|
|
8912
|
+
if (typeof outcome === "object" && outcome !== null && outcome.type === "escalation") {
|
|
8913
|
+
const kind = text2(outcome.class);
|
|
8914
|
+
return {
|
|
8915
|
+
kind: "blocked",
|
|
8916
|
+
text: `${who} raised an escalation${kind ? ` (${kind})` : ""}. A person has to decide.`
|
|
8917
|
+
};
|
|
8918
|
+
}
|
|
8919
|
+
return { kind: "done", text: `${who} finished its turn.` };
|
|
8920
|
+
}
|
|
8921
|
+
function narrate(record, earlier = []) {
|
|
8922
|
+
try {
|
|
8923
|
+
if (typeof record !== "object" || record === null)
|
|
8924
|
+
return null;
|
|
8925
|
+
const fields = record;
|
|
8926
|
+
if (fields.kind === "dispatch") {
|
|
8927
|
+
if (fields.event === "dispatched")
|
|
8928
|
+
return dispatched(fields);
|
|
8929
|
+
if (fields.event === "outcome_received")
|
|
8930
|
+
return outcomeReceived(fields);
|
|
8931
|
+
return null;
|
|
8932
|
+
}
|
|
8933
|
+
if (fields.kind !== "dev_review_loop")
|
|
8934
|
+
return null;
|
|
8935
|
+
switch (fields.event) {
|
|
8936
|
+
case "round_started":
|
|
8937
|
+
return { kind: "starting", text: `${roundLabel(fields)} started.` };
|
|
8938
|
+
case "gate_result_read":
|
|
8939
|
+
return typeof fields.green === "boolean" ? fields.green ? { kind: "done", text: `${roundLabel(fields)}: the checks passed.` } : { kind: "failed", text: `${roundLabel(fields)}: the checks failed.` } : null;
|
|
8940
|
+
case "verdicts_read":
|
|
8941
|
+
return verdicts(fields);
|
|
8942
|
+
case "round_ended":
|
|
8943
|
+
return roundEnded(fields, Array.isArray(earlier) ? earlier : []);
|
|
8944
|
+
case "paused":
|
|
8945
|
+
return paused(code(fields.reason_code), text2(fields.reason));
|
|
8946
|
+
case "stop_condition_met":
|
|
8947
|
+
return stopped(fields.condition);
|
|
8948
|
+
case "driver_exited":
|
|
8949
|
+
return driverExited(fields);
|
|
8950
|
+
case "conflict_reentry":
|
|
8951
|
+
return conflictReentry(fields);
|
|
8952
|
+
default:
|
|
8953
|
+
return null;
|
|
8954
|
+
}
|
|
8955
|
+
} catch {
|
|
8956
|
+
return null;
|
|
8957
|
+
}
|
|
8958
|
+
}
|
|
8959
|
+
|
|
8960
|
+
// ../../harness/processes/vinaya-strict/integration/log/log-roles.ts
|
|
8961
|
+
var VINAYA_LOG_ROLES = [
|
|
8962
|
+
{ name: "planner", reading: "planner", title: "Planner", reviews: false },
|
|
8963
|
+
{ name: "developer", reading: "developer", title: "Developer", reviews: false },
|
|
8964
|
+
{ name: "code-reviewer", reading: "code reviewer", title: "Code review", reviews: true },
|
|
8965
|
+
{ name: "security", reading: "security reviewer", title: "Security", reviews: true },
|
|
8966
|
+
{ name: "principal", reading: "principal", title: "Principal", reviews: false },
|
|
8967
|
+
{ name: "archivist", reading: "archivist", title: "Archivist", reviews: false },
|
|
8968
|
+
{ name: "architect", reading: "architect", title: "Architect", reviews: false }
|
|
8969
|
+
];
|
|
8970
|
+
var VINAYA_LOG_ROLE_NAMES = VINAYA_LOG_ROLES.map((role) => role.name);
|
|
8971
|
+
function registerVinayaLogRoles() {
|
|
8972
|
+
for (const { name, reading, title, reviews } of VINAYA_LOG_ROLES)
|
|
8973
|
+
registerRole(name, { reading, title, reviews });
|
|
8974
|
+
registerTitle("dev-review-loop", "Loop");
|
|
8975
|
+
}
|
|
8976
|
+
registerVinayaLogRoles();
|
|
8977
|
+
|
|
8978
|
+
// ../../harness/processes/vinaya-strict/tools/mission-control/reading.ts
|
|
8979
|
+
var PRINCIPAL = "the Principal";
|
|
8980
|
+
var LOOP = "the loop";
|
|
8981
|
+
function whole2(value) {
|
|
8982
|
+
return typeof value === "number" && Number.isInteger(value) ? value : null;
|
|
8983
|
+
}
|
|
8984
|
+
function text3(value) {
|
|
8985
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
8986
|
+
}
|
|
8987
|
+
function fieldsOf(value) {
|
|
8988
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
8989
|
+
}
|
|
8990
|
+
function recordOf(row) {
|
|
8991
|
+
return { meta: row.header.meta, subject: row.header.subject, kind: row.kind, event: row.event, ...row.payload };
|
|
8992
|
+
}
|
|
8993
|
+
function standingOf(task, now) {
|
|
8994
|
+
const { pause, handoff, stop, endedReady, exit } = task.signals;
|
|
8995
|
+
if (handoff)
|
|
8996
|
+
return { state: "needs_you", amber: false, reason: handoff.class, decision: null };
|
|
8997
|
+
if (pause) {
|
|
8998
|
+
const waitsOn = pause.code !== null && PERSON_NEEDED_PAUSE_CODES.includes(pause.code) ? "person" : reasonWaitsOn(pause.code, pause.reason);
|
|
8999
|
+
const recorded = pause.code ?? pause.reason;
|
|
9000
|
+
if (waitsOn === "person")
|
|
9001
|
+
return { state: "needs_you", amber: false, reason: recorded, decision: null };
|
|
9002
|
+
if (waitsOn === null) {
|
|
9003
|
+
return {
|
|
9004
|
+
state: "needs_a_look",
|
|
9005
|
+
amber: false,
|
|
9006
|
+
reason: recorded,
|
|
9007
|
+
decision: exit?.lastDecision ?? recorded ?? "paused"
|
|
9008
|
+
};
|
|
9009
|
+
}
|
|
9010
|
+
}
|
|
9011
|
+
if (stop)
|
|
9012
|
+
return { state: "stopped_itself", amber: false, reason: stop.condition, decision: null };
|
|
9013
|
+
if (endedReady)
|
|
9014
|
+
return { state: "ready_to_merge", amber: false, reason: null, decision: null };
|
|
9015
|
+
if (exit && exit.reason !== "reexec") {
|
|
9016
|
+
return { state: "needs_a_look", amber: false, reason: exit.reason, decision: exit.lastDecision };
|
|
9017
|
+
}
|
|
9018
|
+
const silence = now === null ? 0 : Math.max(0, now - task.lastSign);
|
|
9019
|
+
if (silence >= PRESUMED_DEAD_AFTER_MS)
|
|
9020
|
+
return { state: "presumed_dead", amber: false, reason: null, decision: null };
|
|
9021
|
+
return { state: "running", amber: silence >= AMBER_AFTER_MS, reason: null, decision: null };
|
|
9022
|
+
}
|
|
9023
|
+
function whoAndCommand(task, state) {
|
|
9024
|
+
const pr = task.pr;
|
|
9025
|
+
switch (state) {
|
|
9026
|
+
case "running":
|
|
9027
|
+
return { whoActs: LOOP, command: null };
|
|
9028
|
+
case "ready_to_merge":
|
|
9029
|
+
return { whoActs: PRINCIPAL, command: pr === null ? null : `gh pr merge ${pr} --squash` };
|
|
9030
|
+
case "presumed_dead":
|
|
9031
|
+
return { whoActs: PRINCIPAL, command: pr === null ? null : `vinaya dev-review-loop --resume ${pr}` };
|
|
9032
|
+
default:
|
|
9033
|
+
return { whoActs: PRINCIPAL, command: pr === null ? null : `vinaya pr rule ${pr} --file <ruling.md>` };
|
|
9034
|
+
}
|
|
9035
|
+
}
|
|
9036
|
+
function nowLineOf(task, standing) {
|
|
9037
|
+
const latest = task.feed.at(-1)?.text ?? null;
|
|
9038
|
+
switch (standing.state) {
|
|
9039
|
+
case "presumed_dead":
|
|
9040
|
+
return "No sign of life for ten minutes or more, and no exit recorded.";
|
|
9041
|
+
case "needs_a_look":
|
|
9042
|
+
return `Stopped for a reason this reading does not know: ${standing.decision}.`;
|
|
9043
|
+
case "running":
|
|
9044
|
+
return standing.amber ? "No sign of life for five minutes or more." : latest ?? "Running.";
|
|
9045
|
+
case "needs_you":
|
|
9046
|
+
return latest ?? "Waiting for a person.";
|
|
9047
|
+
case "stopped_itself":
|
|
9048
|
+
return latest ?? `Stopped: ${standing.reason}.`;
|
|
9049
|
+
case "ready_to_merge":
|
|
9050
|
+
return latest ?? "Ready to merge.";
|
|
9051
|
+
}
|
|
9052
|
+
}
|
|
9053
|
+
function settle(task, now) {
|
|
9054
|
+
const standing = standingOf(task, now);
|
|
9055
|
+
return { ...task, ...standing, ...whoAndCommand(task, standing.state), nowLine: nowLineOf(task, standing) };
|
|
9056
|
+
}
|
|
9057
|
+
function ordered(tasks) {
|
|
9058
|
+
return [...tasks].sort((a, b) => TASK_STATES.indexOf(a.state) - TASK_STATES.indexOf(b.state) || a.task - b.task);
|
|
9059
|
+
}
|
|
9060
|
+
function newTask(task, at) {
|
|
9061
|
+
return {
|
|
9062
|
+
task,
|
|
9063
|
+
title: null,
|
|
9064
|
+
state: "running",
|
|
9065
|
+
amber: false,
|
|
9066
|
+
reason: null,
|
|
9067
|
+
decision: null,
|
|
9068
|
+
rounds: [],
|
|
9069
|
+
pr: null,
|
|
9070
|
+
nowLine: "Running.",
|
|
9071
|
+
lastSign: at,
|
|
9072
|
+
whoActs: LOOP,
|
|
9073
|
+
command: null,
|
|
9074
|
+
feed: [],
|
|
9075
|
+
tokens: {},
|
|
9076
|
+
model: null,
|
|
9077
|
+
models: {},
|
|
9078
|
+
machine: null,
|
|
9079
|
+
reviewerRoles: [],
|
|
9080
|
+
loopIds: [],
|
|
9081
|
+
signals: { pause: null, handoff: null, stop: null, endedReady: false, exit: null },
|
|
9082
|
+
gateReads: {}
|
|
9083
|
+
};
|
|
9084
|
+
}
|
|
9085
|
+
function emptyRound(n) {
|
|
9086
|
+
return {
|
|
9087
|
+
n,
|
|
9088
|
+
startedAt: null,
|
|
9089
|
+
endedAt: null,
|
|
9090
|
+
wallMs: null,
|
|
9091
|
+
outcome: null,
|
|
9092
|
+
checksGreen: null,
|
|
9093
|
+
agents: {},
|
|
9094
|
+
confidence: null,
|
|
9095
|
+
reviewers: [],
|
|
9096
|
+
findings: null
|
|
9097
|
+
};
|
|
9098
|
+
}
|
|
9099
|
+
function withRound(task, n, change) {
|
|
9100
|
+
const existing = task.rounds.find((r) => r.n === n);
|
|
9101
|
+
const next = change(existing ?? emptyRound(n));
|
|
9102
|
+
const rounds = existing ? task.rounds.map((r) => r.n === n ? next : r) : [...task.rounds, next];
|
|
9103
|
+
return { ...task, rounds: rounds.sort((a, b) => a.n - b.n) };
|
|
9104
|
+
}
|
|
9105
|
+
function countFindings(findings) {
|
|
9106
|
+
const counts = { blocking: 0, nonBlocking: 0, unclassified: 0, severities: {} };
|
|
9107
|
+
if (!Array.isArray(findings))
|
|
9108
|
+
return counts;
|
|
9109
|
+
for (const entry of findings) {
|
|
9110
|
+
const finding = fieldsOf(entry);
|
|
9111
|
+
if (!finding)
|
|
9112
|
+
continue;
|
|
9113
|
+
if (finding.policy_treatment === "blocking")
|
|
9114
|
+
counts.blocking += 1;
|
|
9115
|
+
else if (finding.policy_treatment === "non_blocking")
|
|
9116
|
+
counts.nonBlocking += 1;
|
|
9117
|
+
else
|
|
9118
|
+
counts.unclassified += 1;
|
|
9119
|
+
const severity = text3(finding.severity);
|
|
9120
|
+
if (severity)
|
|
9121
|
+
counts.severities[severity] = (counts.severities[severity] ?? 0) + 1;
|
|
9122
|
+
}
|
|
9123
|
+
return counts;
|
|
9124
|
+
}
|
|
9125
|
+
function upsertReviewer(reviewers, verdict) {
|
|
9126
|
+
const rest = reviewers.filter((r) => r.role !== verdict.role);
|
|
9127
|
+
return [...rest, verdict].sort((a, b) => a.role.localeCompare(b.role));
|
|
9128
|
+
}
|
|
9129
|
+
function reviewerFromDispatch(round, role, outcome) {
|
|
9130
|
+
const word = outcome.verdict;
|
|
9131
|
+
const verdict = word === "APPROVE" || word === "PASS" ? "approve" : "changes_requested";
|
|
9132
|
+
const findings = countFindings(outcome.findings);
|
|
9133
|
+
return {
|
|
9134
|
+
...round,
|
|
9135
|
+
reviewers: upsertReviewer(round.reviewers, {
|
|
9136
|
+
role,
|
|
9137
|
+
outcome: verdict,
|
|
9138
|
+
blocking: findings.blocking,
|
|
9139
|
+
findings,
|
|
9140
|
+
shared: false
|
|
9141
|
+
})
|
|
9142
|
+
};
|
|
9143
|
+
}
|
|
9144
|
+
function verdictsRead(round, record, policyReviewers) {
|
|
9145
|
+
const findings = Array.isArray(record.findings) ? countFindings(record.findings) : null;
|
|
9146
|
+
const listed = Array.isArray(record.reviewers) ? record.reviewers.map(fieldsOf).filter((r) => r !== null && typeof r.role === "string").map((r) => ({
|
|
9147
|
+
role: r.role,
|
|
9148
|
+
outcome: r.outcome === "approve" || r.outcome === "not_reviewed" ? r.outcome : "changes_requested",
|
|
9149
|
+
blocking: whole2(r.blockers)
|
|
9150
|
+
})) : null;
|
|
9151
|
+
const mark = record.all_approve === true ? "approve" : "changes_requested";
|
|
9152
|
+
const entries = listed ?? policyReviewers.map((role) => ({ role, outcome: mark, blocking: null }));
|
|
9153
|
+
const alone = entries.length === 1;
|
|
9154
|
+
let reviewers = round.reviewers;
|
|
9155
|
+
for (const entry of entries) {
|
|
9156
|
+
const own = reviewers.find((r) => r.role === entry.role && !r.shared);
|
|
9157
|
+
if (own) {
|
|
9158
|
+
reviewers = upsertReviewer(reviewers, {
|
|
9159
|
+
...own,
|
|
9160
|
+
outcome: listed ? entry.outcome : own.outcome,
|
|
9161
|
+
blocking: own.blocking ?? entry.blocking
|
|
9162
|
+
});
|
|
9163
|
+
continue;
|
|
9164
|
+
}
|
|
9165
|
+
reviewers = upsertReviewer(reviewers, {
|
|
9166
|
+
role: entry.role,
|
|
9167
|
+
outcome: entry.outcome,
|
|
9168
|
+
blocking: entry.blocking ?? (alone && findings ? findings.blocking : null),
|
|
9169
|
+
findings: alone ? findings : null,
|
|
9170
|
+
shared: !alone
|
|
9171
|
+
});
|
|
9172
|
+
}
|
|
9173
|
+
return { ...round, reviewers, findings };
|
|
9174
|
+
}
|
|
9175
|
+
function roundOf(record, row) {
|
|
9176
|
+
return whole2(record.round) ?? row.round;
|
|
9177
|
+
}
|
|
9178
|
+
function addFeed(task, record, event) {
|
|
9179
|
+
const round = whole2(record.round);
|
|
9180
|
+
const read = round === null ? undefined : task.gateReads[String(round)];
|
|
9181
|
+
const line = narrate(record, read ? [read] : []);
|
|
9182
|
+
if (!line)
|
|
9183
|
+
return task;
|
|
9184
|
+
const entry = { position: event.position, at: event.receivedAt, kind: line.kind, text: line.text };
|
|
9185
|
+
return { ...task, feed: [...task.feed, entry] };
|
|
9186
|
+
}
|
|
9187
|
+
function addTokens(task, role, usage) {
|
|
9188
|
+
const before = task.tokens[role] ?? { input: null, output: null, unknownTurns: 0 };
|
|
9189
|
+
const known = fieldsOf(usage);
|
|
9190
|
+
const input = typeof known?.input === "number" ? known.input : null;
|
|
9191
|
+
const output = typeof known?.output === "number" ? known.output : null;
|
|
9192
|
+
const after = input === null || output === null ? { ...before, unknownTurns: before.unknownTurns + 1 } : { input: (before.input ?? 0) + input, output: (before.output ?? 0) + output, unknownTurns: before.unknownTurns };
|
|
9193
|
+
return { ...task, tokens: { ...task.tokens, [role]: after } };
|
|
9194
|
+
}
|
|
9195
|
+
function withPr(task, pr) {
|
|
9196
|
+
const n = whole2(pr);
|
|
9197
|
+
return n !== null && n > 0 ? { ...task, pr: n } : task;
|
|
9198
|
+
}
|
|
9199
|
+
function readLoopRecord(task, record, row, event) {
|
|
9200
|
+
let next = { ...task, signals: { ...task.signals } };
|
|
9201
|
+
const signals = next.signals;
|
|
9202
|
+
const loopId = text3(record.loop_id);
|
|
9203
|
+
if (loopId && !next.loopIds.includes(loopId))
|
|
9204
|
+
next.loopIds = [...next.loopIds, loopId];
|
|
9205
|
+
const machine = text3(row.header.meta.machine);
|
|
9206
|
+
if (machine)
|
|
9207
|
+
next.machine = machine;
|
|
9208
|
+
if (record.event !== "driver_exited")
|
|
9209
|
+
signals.exit = null;
|
|
9210
|
+
const round = roundOf(record, row);
|
|
9211
|
+
switch (record.event) {
|
|
9212
|
+
case "loop_started": {
|
|
9213
|
+
const title = text3(record.title);
|
|
9214
|
+
if (title)
|
|
9215
|
+
next.title = title;
|
|
9216
|
+
const policy = fieldsOf(record.policy);
|
|
9217
|
+
if (policy) {
|
|
9218
|
+
if (Array.isArray(policy.reviewers)) {
|
|
9219
|
+
next.reviewerRoles = policy.reviewers.filter((r) => typeof r === "string");
|
|
9220
|
+
}
|
|
9221
|
+
const models = fieldsOf(policy.models);
|
|
9222
|
+
if (models) {
|
|
9223
|
+
const named = {};
|
|
9224
|
+
for (const [role, model] of Object.entries(models))
|
|
9225
|
+
if (typeof model === "string")
|
|
9226
|
+
named[role] = model;
|
|
9227
|
+
next.models = { ...named, ...next.models };
|
|
9228
|
+
next.model = next.models.developer ?? next.model;
|
|
9229
|
+
}
|
|
9230
|
+
}
|
|
9231
|
+
break;
|
|
9232
|
+
}
|
|
9233
|
+
case "round_started":
|
|
9234
|
+
signals.pause = null;
|
|
9235
|
+
signals.stop = null;
|
|
9236
|
+
signals.endedReady = false;
|
|
9237
|
+
if (round !== null)
|
|
9238
|
+
next = withRound(next, round, (r) => ({ ...r, startedAt: r.startedAt ?? event.receivedAt }));
|
|
9239
|
+
break;
|
|
9240
|
+
case "gate_result_read":
|
|
9241
|
+
if (round !== null) {
|
|
9242
|
+
next = {
|
|
9243
|
+
...next,
|
|
9244
|
+
gateReads: {
|
|
9245
|
+
...next.gateReads,
|
|
9246
|
+
[String(round)]: {
|
|
9247
|
+
event: "gate_result_read",
|
|
9248
|
+
loop_id: record.loop_id,
|
|
9249
|
+
round,
|
|
9250
|
+
confidence_value: record.confidence_value,
|
|
9251
|
+
confidence_unavailable: record.confidence_unavailable,
|
|
9252
|
+
extra_turn_spent: record.extra_turn_spent
|
|
9253
|
+
}
|
|
9254
|
+
}
|
|
9255
|
+
};
|
|
9256
|
+
const value = whole2(record.confidence_value);
|
|
9257
|
+
const unavailable = record.confidence_unavailable === true;
|
|
9258
|
+
next = withRound(next, round, (r) => ({
|
|
9259
|
+
...r,
|
|
9260
|
+
checksGreen: typeof record.green === "boolean" ? record.green : r.checksGreen,
|
|
9261
|
+
confidence: value !== null || unavailable ? { value, unavailable, extraTurnSpent: record.extra_turn_spent === true } : r.confidence
|
|
9262
|
+
}));
|
|
9263
|
+
}
|
|
9264
|
+
break;
|
|
9265
|
+
case "verdicts_read":
|
|
9266
|
+
if (round !== null)
|
|
9267
|
+
next = withRound(next, round, (r) => verdictsRead(r, record, next.reviewerRoles));
|
|
9268
|
+
break;
|
|
9269
|
+
case "round_ended":
|
|
9270
|
+
if (round !== null) {
|
|
9271
|
+
next = withRound(next, round, (r) => ({
|
|
9272
|
+
...r,
|
|
9273
|
+
endedAt: event.receivedAt,
|
|
9274
|
+
wallMs: typeof record.wall_ms === "number" ? record.wall_ms : r.wallMs,
|
|
9275
|
+
outcome: record.outcome === "green" || record.outcome === "changes_requested" || record.outcome === "escalated" ? record.outcome : r.outcome
|
|
9276
|
+
}));
|
|
9277
|
+
}
|
|
9278
|
+
break;
|
|
9279
|
+
case "stop_condition_met": {
|
|
9280
|
+
const condition = text3(record.condition);
|
|
9281
|
+
if (condition === "green")
|
|
9282
|
+
signals.endedReady = true;
|
|
9283
|
+
else if (condition)
|
|
9284
|
+
signals.stop = { condition };
|
|
9285
|
+
break;
|
|
9286
|
+
}
|
|
9287
|
+
case "journal_finalized":
|
|
9288
|
+
if (record.result === "merged_ready")
|
|
9289
|
+
signals.endedReady = true;
|
|
9290
|
+
break;
|
|
9291
|
+
case "paused":
|
|
9292
|
+
signals.pause = { code: text3(record.reason_code), reason: text3(record.reason) };
|
|
9293
|
+
break;
|
|
9294
|
+
case "resumed":
|
|
9295
|
+
signals.pause = null;
|
|
9296
|
+
signals.stop = null;
|
|
9297
|
+
break;
|
|
9298
|
+
case "cancelled":
|
|
9299
|
+
signals.pause = null;
|
|
9300
|
+
signals.stop = { condition: "cancelled" };
|
|
9301
|
+
break;
|
|
9302
|
+
case "conflict_reentry":
|
|
9303
|
+
signals.endedReady = false;
|
|
9304
|
+
next = withPr(next, record.pr);
|
|
9305
|
+
break;
|
|
9306
|
+
case "driver_heartbeat":
|
|
9307
|
+
case "pr_opened":
|
|
9308
|
+
case "ci_result_read":
|
|
9309
|
+
next = withPr(next, record.pr);
|
|
9310
|
+
break;
|
|
9311
|
+
case "driver_exited": {
|
|
9312
|
+
const reason = text3(record.reason) ?? "unknown";
|
|
9313
|
+
signals.exit = { reason, lastDecision: typeof record.last_decision === "string" ? record.last_decision : "" };
|
|
9314
|
+
if (reason === "finished")
|
|
9315
|
+
signals.endedReady = true;
|
|
9316
|
+
break;
|
|
9317
|
+
}
|
|
9318
|
+
default:
|
|
9319
|
+
break;
|
|
9320
|
+
}
|
|
9321
|
+
return next;
|
|
9322
|
+
}
|
|
9323
|
+
function readDispatchRecord(task, record, row) {
|
|
9324
|
+
const role = text3(record.target_role);
|
|
9325
|
+
if (!role)
|
|
9326
|
+
return task;
|
|
9327
|
+
let next = task;
|
|
9328
|
+
const model = text3(record.model);
|
|
9329
|
+
if (model) {
|
|
9330
|
+
next = { ...next, models: { ...next.models, [role]: model } };
|
|
9331
|
+
if (role === "developer")
|
|
9332
|
+
next = { ...next, model };
|
|
9333
|
+
}
|
|
9334
|
+
const round = roundOf(record, row);
|
|
9335
|
+
const setAgent = (state) => {
|
|
9336
|
+
if (round !== null)
|
|
9337
|
+
next = withRound(next, round, (r) => ({ ...r, agents: { ...r.agents, [role]: state } }));
|
|
9338
|
+
};
|
|
9339
|
+
if (record.event === "dispatched")
|
|
9340
|
+
setAgent("working");
|
|
9341
|
+
else if (record.event === "dispatch_failed") {
|
|
9342
|
+
setAgent("failed");
|
|
9343
|
+
next = addTokens(next, role, record.usage);
|
|
9344
|
+
} else if (record.event === "outcome_received") {
|
|
9345
|
+
const outcome = fieldsOf(record.outcome);
|
|
9346
|
+
setAgent(outcome?.type === "escalation" ? "escalated" : "done");
|
|
9347
|
+
next = addTokens(next, role, record.usage);
|
|
9348
|
+
if (outcome && (outcome.type === "pr_opened" || outcome.type === "round_pushed"))
|
|
9349
|
+
next = withPr(next, outcome.pr);
|
|
9350
|
+
if (outcome?.type === "verdict" && round !== null) {
|
|
9351
|
+
next = withRound(next, round, (r) => reviewerFromDispatch(r, role, outcome));
|
|
9352
|
+
}
|
|
9353
|
+
}
|
|
9354
|
+
return next;
|
|
9355
|
+
}
|
|
9356
|
+
function readHandoffRecord(task, record) {
|
|
9357
|
+
const signals = { ...task.signals };
|
|
9358
|
+
if (record.event === "raised")
|
|
9359
|
+
signals.handoff = { class: text3(record.class) ?? "handoff" };
|
|
9360
|
+
else if (record.event === "resolved")
|
|
9361
|
+
signals.handoff = null;
|
|
9362
|
+
return { ...task, signals };
|
|
9363
|
+
}
|
|
9364
|
+
function taskOf(picture, row, record) {
|
|
9365
|
+
if (row.issue !== null)
|
|
9366
|
+
return row.issue;
|
|
9367
|
+
const named = whole2(record.task);
|
|
9368
|
+
if (named !== null)
|
|
9369
|
+
return named;
|
|
9370
|
+
const loopId = text3(record.loop_id);
|
|
9371
|
+
return loopId ? picture.loops[loopId] ?? null : null;
|
|
9372
|
+
}
|
|
9373
|
+
function reduce(picture, event) {
|
|
9374
|
+
if (picture.readThrough !== null && event.position <= picture.readThrough) {
|
|
9375
|
+
return { ...picture, repeated: picture.repeated + 1 };
|
|
9376
|
+
}
|
|
9377
|
+
const read = { ...picture, readThrough: event.position };
|
|
9378
|
+
if (event.line.type !== "row")
|
|
9379
|
+
return { ...read, skipped: read.skipped + 1 };
|
|
9380
|
+
const row = event.line.row;
|
|
9381
|
+
const family = row.kind;
|
|
9382
|
+
if (family !== "dev_review_loop" && family !== "dispatch" && family !== "handoff")
|
|
9383
|
+
return read;
|
|
9384
|
+
const record = recordOf(row);
|
|
9385
|
+
const id = taskOf(read, row, record);
|
|
9386
|
+
if (id === null)
|
|
9387
|
+
return read;
|
|
9388
|
+
const existing = read.tasks.find((t) => t.task === id);
|
|
9389
|
+
if (!existing && family !== "dev_review_loop")
|
|
9390
|
+
return read;
|
|
9391
|
+
let task = existing ?? newTask(id, event.receivedAt);
|
|
9392
|
+
task = { ...task, lastSign: Math.max(task.lastSign, event.receivedAt) };
|
|
9393
|
+
task = withPr(task, row.pr);
|
|
9394
|
+
if (family === "dev_review_loop")
|
|
9395
|
+
task = readLoopRecord(task, record, row, event);
|
|
9396
|
+
else if (family === "dispatch")
|
|
9397
|
+
task = readDispatchRecord(task, record, row);
|
|
9398
|
+
else
|
|
9399
|
+
task = readHandoffRecord(task, record);
|
|
9400
|
+
task = addFeed(task, record, event);
|
|
9401
|
+
const loopId = text3(record.loop_id);
|
|
9402
|
+
const loops = loopId && read.loops[loopId] !== id ? { ...read.loops, [loopId]: id } : read.loops;
|
|
9403
|
+
const others = read.tasks.filter((t) => t.task !== id);
|
|
9404
|
+
return { ...read, loops, tasks: ordered([...others, settle(task, read.now)]) };
|
|
9405
|
+
}
|
|
9406
|
+
function tick(picture, now) {
|
|
9407
|
+
if (picture.now !== null && now < picture.now)
|
|
9408
|
+
return { picture, changed: [] };
|
|
9409
|
+
const changed = [];
|
|
9410
|
+
const tasks = picture.tasks.map((task) => {
|
|
9411
|
+
const next = settle(task, now);
|
|
9412
|
+
if (next.state !== task.state || next.amber !== task.amber)
|
|
9413
|
+
changed.push(task.task);
|
|
9414
|
+
return next;
|
|
9415
|
+
});
|
|
9416
|
+
return { picture: { ...picture, now, tasks: ordered(tasks) }, changed: changed.sort((a, b) => a - b) };
|
|
9417
|
+
}
|
|
9418
|
+
|
|
9419
|
+
// src/server/picture-service.ts
|
|
9420
|
+
var TICK_INTERVAL_MS = 30000;
|
|
9421
|
+
var DEFAULT_STRETCH = 5000;
|
|
9422
|
+
var FEED_CAP = 200;
|
|
9423
|
+
var DEFAULT_BUDGETS = {
|
|
9424
|
+
identities: 1e5,
|
|
9425
|
+
tasks: 1e4,
|
|
9426
|
+
roundsPerTask: 1000,
|
|
9427
|
+
bootstrapBytes: 64 * 1024 * 1024
|
|
9428
|
+
};
|
|
9429
|
+
var realTimers2 = {
|
|
9430
|
+
setInterval: (handler, ms) => setInterval(handler, ms),
|
|
9431
|
+
clearInterval: (handle) => clearInterval(handle)
|
|
9432
|
+
};
|
|
9433
|
+
var LOG_FOLLOW_FILENAME = "log-follow.mjs";
|
|
9434
|
+
var FOLDER_SOURCE_FILENAME = "log-sync-folder-source.mjs";
|
|
9435
|
+
function createRealSourceFrom(runtimeDir) {
|
|
9436
|
+
const load = (filename) => {
|
|
9437
|
+
const specifier = new URL(filename, runtimeDir).href;
|
|
9438
|
+
return import(specifier);
|
|
9439
|
+
};
|
|
9440
|
+
return async (spec, repository) => {
|
|
9441
|
+
const follow = await load(LOG_FOLLOW_FILENAME);
|
|
9442
|
+
if (spec.kind === "folder") {
|
|
9443
|
+
const folder = await load(FOLDER_SOURCE_FILENAME);
|
|
9444
|
+
const deps = { folderRoot: spec.folderRoot, repo: repository.remote };
|
|
9445
|
+
return { source: folder.createFolderLogSource(deps), follower: follow.createFolderLogFollower(deps) };
|
|
9446
|
+
}
|
|
9447
|
+
const deps = {
|
|
9448
|
+
eventsUrl: spec.url,
|
|
9449
|
+
readHeaders: spec.readHeaders,
|
|
9450
|
+
env: process.env,
|
|
9451
|
+
fetchImpl: (input, init) => fetch(input, init),
|
|
9452
|
+
WebSocketImpl: globalThis.WebSocket
|
|
9453
|
+
};
|
|
9454
|
+
return { source: createServerLogSource(deps), follower: follow.createServerLogFollower(deps) };
|
|
9455
|
+
};
|
|
9456
|
+
}
|
|
9457
|
+
var createRealSource = (spec, repository) => createRealSourceFrom(new URL("./", import.meta.url))(spec, repository);
|
|
9458
|
+
function validTime(text) {
|
|
9459
|
+
const ms = Date.parse(text);
|
|
9460
|
+
return Number.isFinite(ms) ? ms : null;
|
|
9461
|
+
}
|
|
9462
|
+
function whole3(value) {
|
|
9463
|
+
return typeof value === "number" && Number.isInteger(value) ? value : null;
|
|
9464
|
+
}
|
|
9465
|
+
function namedTask(line) {
|
|
9466
|
+
if (line.type !== "row")
|
|
9467
|
+
return null;
|
|
9468
|
+
return line.row.issue ?? whole3(line.row.payload.task);
|
|
9469
|
+
}
|
|
9470
|
+
function isLoopStart(line) {
|
|
9471
|
+
return line.type === "row" && line.row.kind === "dev_review_loop" && line.row.event === "loop_started";
|
|
9472
|
+
}
|
|
9473
|
+
function recordedTime(line) {
|
|
9474
|
+
return line.type === "row" ? validTime(line.row.time) : null;
|
|
9475
|
+
}
|
|
9476
|
+
function normalize(source, line) {
|
|
9477
|
+
try {
|
|
9478
|
+
return normalizeStoredLine(line.raw, { source, position: line.position });
|
|
9479
|
+
} catch {
|
|
9480
|
+
return null;
|
|
9481
|
+
}
|
|
9482
|
+
}
|
|
9483
|
+
function streamOf(position) {
|
|
9484
|
+
return position.split(":")[0] ?? position;
|
|
9485
|
+
}
|
|
9486
|
+
function createPictureService(options) {
|
|
9487
|
+
const clock = options.clock ?? Date.now;
|
|
9488
|
+
const timers = options.timers ?? realTimers2;
|
|
9489
|
+
const createSource = options.createSource ?? createRealSource;
|
|
9490
|
+
const stretch = Math.max(0, options.stretch ?? DEFAULT_STRETCH);
|
|
9491
|
+
const pageSize = Math.max(1, Math.min(options.pageSize ?? SERVER_SOURCE_PAGE_MAX, SERVER_SOURCE_PAGE_MAX));
|
|
9492
|
+
const budgets = { ...DEFAULT_BUDGETS, ...options.budgets };
|
|
9493
|
+
const tickMs = options.tickMs ?? TICK_INTERVAL_MS;
|
|
9494
|
+
const repos = new Map;
|
|
9495
|
+
for (const repository of options.repositories) {
|
|
9496
|
+
repos.set(repository.root, {
|
|
9497
|
+
repository,
|
|
9498
|
+
kind: repository.source.ok ? repository.source.spec.kind : null,
|
|
9499
|
+
handle: null,
|
|
9500
|
+
follow: null,
|
|
9501
|
+
picture: emptyPicture(),
|
|
9502
|
+
sequence: 0,
|
|
9503
|
+
seen: new Set,
|
|
9504
|
+
bytes: 0,
|
|
9505
|
+
connection: "opening",
|
|
9506
|
+
reason: null,
|
|
9507
|
+
limits: new Map,
|
|
9508
|
+
startPosition: null,
|
|
9509
|
+
startSeen: new Set,
|
|
9510
|
+
tasksSeen: new Set,
|
|
9511
|
+
lastChrono: new Map,
|
|
9512
|
+
chronologyLimited: new Set,
|
|
9513
|
+
exhausted: null,
|
|
9514
|
+
followerLive: false,
|
|
9515
|
+
everLive: false,
|
|
9516
|
+
reconnected: false,
|
|
9517
|
+
applied: 0,
|
|
9518
|
+
duplicates: 0,
|
|
9519
|
+
unreadable: 0,
|
|
9520
|
+
heldBack: 0,
|
|
9521
|
+
lastApplied: null
|
|
9522
|
+
});
|
|
9523
|
+
}
|
|
9524
|
+
const listeners = new Set;
|
|
9525
|
+
let closed = false;
|
|
9526
|
+
let started = null;
|
|
9527
|
+
let timer = null;
|
|
9528
|
+
function snapshotOf(repo) {
|
|
9529
|
+
const limits = [...repo.limits.values()];
|
|
9530
|
+
if (repo.chronologyLimited.size > 0) {
|
|
9531
|
+
limits.push({
|
|
9532
|
+
kind: "chronology-limited",
|
|
9533
|
+
detail: `records older than the newest applied one were held back for task(s) ${[...repo.chronologyLimited].sort((a, b) => a - b).join(", ")}.`
|
|
9534
|
+
});
|
|
9535
|
+
}
|
|
9536
|
+
const live = repo.connection === "live" && repo.exhausted === null;
|
|
9537
|
+
return {
|
|
9538
|
+
repository: repo.repository.root,
|
|
9539
|
+
remote: repo.repository.remote ? `${repo.repository.remote.owner}/${repo.repository.remote.repo}` : null,
|
|
9540
|
+
sourceKind: repo.kind,
|
|
9541
|
+
status: { live, connection: repo.connection, reason: repo.reason },
|
|
9542
|
+
coverage: { startPosition: repo.startPosition, complete: limits.length === 0, limits },
|
|
9543
|
+
picture: repo.picture,
|
|
9544
|
+
partialTasks: repo.picture.tasks.map((t) => t.task).filter((t) => !repo.startSeen.has(t)).sort((a, b) => a - b),
|
|
9545
|
+
chronologyLimitedTasks: [...repo.chronologyLimited].sort((a, b) => a - b),
|
|
9546
|
+
diagnostics: {
|
|
9547
|
+
applied: repo.applied,
|
|
9548
|
+
duplicates: repo.duplicates,
|
|
9549
|
+
unreadable: repo.unreadable,
|
|
9550
|
+
heldBack: repo.heldBack,
|
|
9551
|
+
identities: repo.seen.size,
|
|
9552
|
+
bootstrapBytes: repo.bytes,
|
|
9553
|
+
sequence: repo.sequence,
|
|
9554
|
+
lastAppliedPosition: repo.lastApplied
|
|
9555
|
+
}
|
|
9556
|
+
};
|
|
9557
|
+
}
|
|
9558
|
+
function emit(event) {
|
|
9559
|
+
if (closed)
|
|
9560
|
+
return;
|
|
9561
|
+
for (const listener of [...listeners]) {
|
|
9562
|
+
try {
|
|
9563
|
+
listener(event);
|
|
9564
|
+
} catch {}
|
|
9565
|
+
}
|
|
9566
|
+
}
|
|
9567
|
+
const emitStatus = (repo) => emit({ type: "status", repository: repo.repository.root, snapshot: snapshotOf(repo) });
|
|
9568
|
+
function setConnection(repo, connection, reason = null) {
|
|
9569
|
+
repo.connection = connection;
|
|
9570
|
+
repo.reason = reason;
|
|
9571
|
+
emitStatus(repo);
|
|
9572
|
+
}
|
|
9573
|
+
function exhaust(repo, what) {
|
|
9574
|
+
if (repo.exhausted !== null)
|
|
9575
|
+
return;
|
|
9576
|
+
repo.exhausted = what;
|
|
9577
|
+
repo.limits.set("capacity-limited", {
|
|
9578
|
+
kind: "capacity-limited",
|
|
9579
|
+
detail: `${what} budget exhausted: the source stopped advancing at ${repo.lastApplied ?? "its start"}; the picture so far is kept and is not live or complete.`
|
|
9580
|
+
});
|
|
9581
|
+
const follow = repo.follow;
|
|
9582
|
+
repo.follow = null;
|
|
9583
|
+
repo.connection = "stopped";
|
|
9584
|
+
repo.reason = `${what} budget exhausted.`;
|
|
9585
|
+
follow?.stop();
|
|
9586
|
+
emitStatus(repo);
|
|
9587
|
+
}
|
|
9588
|
+
function receivedAtFor(repo, line, phase) {
|
|
9589
|
+
const recorded = recordedTime(line);
|
|
9590
|
+
if (phase === "bootstrap")
|
|
9591
|
+
return recorded ?? 0;
|
|
9592
|
+
if (repo.followerLive && !repo.reconnected)
|
|
9593
|
+
return clock();
|
|
9594
|
+
return recorded === null ? 0 : Math.min(recorded, clock());
|
|
9595
|
+
}
|
|
9596
|
+
function ingest(repo, position, line, phase) {
|
|
9597
|
+
if (repo.exhausted !== null)
|
|
9598
|
+
return null;
|
|
9599
|
+
if (line.type === "quarantine") {
|
|
9600
|
+
repo.unreadable += 1;
|
|
9601
|
+
repo.limits.set("unreadable-records", {
|
|
9602
|
+
kind: "unreadable-records",
|
|
9603
|
+
detail: `${repo.unreadable} stored line(s) could not be read and were skipped, never guessed.`
|
|
9604
|
+
});
|
|
9605
|
+
}
|
|
9606
|
+
const key = line.type === "row" ? line.row.identity : `quarantine:${line.record.contentHash}`;
|
|
9607
|
+
if (repo.seen.has(key)) {
|
|
9608
|
+
repo.duplicates += 1;
|
|
9609
|
+
return null;
|
|
9610
|
+
}
|
|
9611
|
+
if (repo.seen.size >= budgets.identities) {
|
|
9612
|
+
exhaust(repo, "event identity");
|
|
9613
|
+
return null;
|
|
9614
|
+
}
|
|
9615
|
+
const task = namedTask(line);
|
|
9616
|
+
const recorded = recordedTime(line);
|
|
9617
|
+
if (repo.kind === "folder" && phase === "follow" && task !== null && recorded !== null) {
|
|
9618
|
+
const newest = repo.lastChrono.get(task);
|
|
9619
|
+
if (newest !== undefined && recorded < newest) {
|
|
9620
|
+
repo.seen.add(key);
|
|
9621
|
+
repo.heldBack += 1;
|
|
9622
|
+
repo.chronologyLimited.add(task);
|
|
9623
|
+
return task;
|
|
9624
|
+
}
|
|
9625
|
+
}
|
|
9626
|
+
const before = repo.picture;
|
|
9627
|
+
const sequence = repo.sequence + 1;
|
|
9628
|
+
const read = reduce(before, { position: sequence, receivedAt: receivedAtFor(repo, line, phase), line });
|
|
9629
|
+
let after = read;
|
|
9630
|
+
const id = task ?? (line.type === "row" ? resolveLoopTask(read, line) : null);
|
|
9631
|
+
if (id !== null) {
|
|
9632
|
+
const changed = after.tasks.find((t) => t.task === id);
|
|
9633
|
+
if (changed) {
|
|
9634
|
+
if (changed.rounds.length > budgets.roundsPerTask) {
|
|
9635
|
+
exhaust(repo, "rounds per task");
|
|
9636
|
+
return null;
|
|
9637
|
+
}
|
|
9638
|
+
if (changed.feed.length > FEED_CAP) {
|
|
9639
|
+
const trimmed = { ...changed, feed: changed.feed.slice(-FEED_CAP) };
|
|
9640
|
+
after = { ...after, tasks: after.tasks.map((t) => t.task === id ? trimmed : t) };
|
|
9641
|
+
}
|
|
9642
|
+
}
|
|
9643
|
+
}
|
|
9644
|
+
if (after.tasks.length > budgets.tasks) {
|
|
9645
|
+
exhaust(repo, "task");
|
|
9646
|
+
return null;
|
|
9647
|
+
}
|
|
9648
|
+
repo.picture = after;
|
|
9649
|
+
repo.sequence = sequence;
|
|
9650
|
+
repo.seen.add(key);
|
|
9651
|
+
repo.applied += 1;
|
|
9652
|
+
repo.lastApplied = position;
|
|
9653
|
+
if (id !== null) {
|
|
9654
|
+
repo.tasksSeen.add(id);
|
|
9655
|
+
if (isLoopStart(line))
|
|
9656
|
+
repo.startSeen.add(id);
|
|
9657
|
+
if (recorded !== null)
|
|
9658
|
+
repo.lastChrono.set(id, Math.max(repo.lastChrono.get(id) ?? recorded, recorded));
|
|
9659
|
+
}
|
|
9660
|
+
return id;
|
|
9661
|
+
}
|
|
9662
|
+
function resolveLoopTask(picture, line) {
|
|
9663
|
+
if (line.type !== "row")
|
|
9664
|
+
return null;
|
|
9665
|
+
const loopId = line.row.payload.loop_id;
|
|
9666
|
+
return typeof loopId === "string" ? picture.loops[loopId] ?? null : null;
|
|
9667
|
+
}
|
|
9668
|
+
function reasonOf(err) {
|
|
9669
|
+
return err instanceof Error ? err.message : String(err);
|
|
9670
|
+
}
|
|
9671
|
+
function unavailable(repo, reason) {
|
|
9672
|
+
setConnection(repo, "unavailable", reason);
|
|
9673
|
+
}
|
|
9674
|
+
function present(repo) {
|
|
9675
|
+
repo.picture = tick(repo.picture, clock()).picture;
|
|
9676
|
+
emit({ type: "snapshot", repository: repo.repository.root, snapshot: snapshotOf(repo) });
|
|
9677
|
+
}
|
|
9678
|
+
function startFollow(repo, from) {
|
|
9679
|
+
const handle = repo.handle;
|
|
9680
|
+
if (handle === null || closed || repo.exhausted !== null)
|
|
9681
|
+
return;
|
|
9682
|
+
repo.followerLive = false;
|
|
9683
|
+
const follow = handle.follower.follow(from, {
|
|
9684
|
+
line(line) {
|
|
9685
|
+
if (closed || repo.exhausted !== null)
|
|
9686
|
+
return;
|
|
9687
|
+
const normalized = normalize(handle.source.id, line);
|
|
9688
|
+
if (normalized === null)
|
|
9689
|
+
return;
|
|
9690
|
+
const changed = ingest(repo, line.position, normalized, "follow");
|
|
9691
|
+
if (changed !== null && !closed) {
|
|
9692
|
+
emit({
|
|
9693
|
+
type: "changed",
|
|
9694
|
+
repository: repo.repository.root,
|
|
9695
|
+
tasks: [changed],
|
|
9696
|
+
snapshot: snapshotOf(repo)
|
|
9697
|
+
});
|
|
9698
|
+
}
|
|
9699
|
+
},
|
|
9700
|
+
state(change) {
|
|
9701
|
+
onFollowState(repo, change);
|
|
9702
|
+
},
|
|
9703
|
+
gap(gap) {
|
|
9704
|
+
repo.limits.set(`source-gap:${gap.from ?? ""}`, {
|
|
9705
|
+
kind: "source-gap",
|
|
9706
|
+
detail: `${gap.reason} (from ${gap.from ?? "an unknown position"}).`
|
|
9707
|
+
});
|
|
9708
|
+
emitStatus(repo);
|
|
9709
|
+
}
|
|
9710
|
+
});
|
|
9711
|
+
repo.follow = follow;
|
|
9712
|
+
follow.done.then(() => {
|
|
9713
|
+
if (repo.follow === follow && !closed && repo.exhausted === null) {
|
|
9714
|
+
repo.follow = null;
|
|
9715
|
+
setConnection(repo, "unavailable", repo.reason ?? "the follow ended.");
|
|
9716
|
+
}
|
|
9717
|
+
});
|
|
9718
|
+
}
|
|
9719
|
+
function onFollowState(repo, change) {
|
|
9720
|
+
if (closed || repo.exhausted !== null)
|
|
9721
|
+
return;
|
|
9722
|
+
switch (change.state) {
|
|
9723
|
+
case "live":
|
|
9724
|
+
repo.followerLive = true;
|
|
9725
|
+
repo.everLive = true;
|
|
9726
|
+
setConnection(repo, "live");
|
|
9727
|
+
return;
|
|
9728
|
+
case "catching-up":
|
|
9729
|
+
repo.followerLive = false;
|
|
9730
|
+
if (repo.everLive)
|
|
9731
|
+
repo.reconnected = true;
|
|
9732
|
+
setConnection(repo, "catching-up");
|
|
9733
|
+
return;
|
|
9734
|
+
case "connecting":
|
|
9735
|
+
repo.followerLive = false;
|
|
9736
|
+
if (repo.everLive)
|
|
9737
|
+
repo.reconnected = true;
|
|
9738
|
+
setConnection(repo, repo.everLive ? "reconnecting" : "catching-up");
|
|
9739
|
+
return;
|
|
9740
|
+
case "waiting-to-reconnect":
|
|
9741
|
+
repo.followerLive = false;
|
|
9742
|
+
repo.reconnected = true;
|
|
9743
|
+
setConnection(repo, "reconnecting", change.reason ?? "the source dropped.");
|
|
9744
|
+
return;
|
|
9745
|
+
case "stopped":
|
|
9746
|
+
repo.followerLive = false;
|
|
9747
|
+
setConnection(repo, "unavailable", change.reason ?? "the follow stopped.");
|
|
9748
|
+
return;
|
|
9749
|
+
}
|
|
9750
|
+
}
|
|
9751
|
+
async function openServer(repo) {
|
|
9752
|
+
const handle = repo.handle;
|
|
9753
|
+
if (!handle.source.retention)
|
|
9754
|
+
return unavailable(repo, "the source cannot state what it retains.");
|
|
9755
|
+
const retention = await handle.source.retention();
|
|
9756
|
+
if (closed)
|
|
9757
|
+
return;
|
|
9758
|
+
if (!retention.head.known)
|
|
9759
|
+
return unavailable(repo, retention.head.reason);
|
|
9760
|
+
const head = retention.head.value;
|
|
9761
|
+
const start = Math.max(0, head - stretch);
|
|
9762
|
+
repo.startPosition = String(start);
|
|
9763
|
+
if (start > 0) {
|
|
9764
|
+
repo.limits.set("opening-limited", {
|
|
9765
|
+
kind: "opening-limited",
|
|
9766
|
+
detail: `opened on the ${stretch} newest events, from position ${start} of ${head}; earlier start records and rounds are unknown.`
|
|
9767
|
+
});
|
|
9768
|
+
}
|
|
9769
|
+
if (retention.gap.known && retention.gap.value > 0) {
|
|
9770
|
+
repo.limits.set("retention-gap", {
|
|
9771
|
+
kind: "retention-gap",
|
|
9772
|
+
detail: `the server retains ${retention.gap.value} fewer events than its head states.`
|
|
9773
|
+
});
|
|
9774
|
+
} else if (!retention.gap.known) {
|
|
9775
|
+
repo.limits.set("retention-unknown", { kind: "retention-unknown", detail: retention.gap.reason });
|
|
9776
|
+
}
|
|
9777
|
+
let cursor = String(start);
|
|
9778
|
+
try {
|
|
9779
|
+
for (;; ) {
|
|
9780
|
+
const page = await handle.source.readPage(cursor, pageSize);
|
|
9781
|
+
if (closed)
|
|
9782
|
+
return;
|
|
9783
|
+
if (page.lines.length === 0 || page.next === null)
|
|
9784
|
+
break;
|
|
9785
|
+
for (const line of page.lines) {
|
|
9786
|
+
const normalized = normalize(handle.source.id, line);
|
|
9787
|
+
if (normalized === null)
|
|
9788
|
+
continue;
|
|
9789
|
+
repo.bytes += Buffer.byteLength(line.raw);
|
|
9790
|
+
if (repo.bytes > budgets.bootstrapBytes) {
|
|
9791
|
+
exhaust(repo, "bootstrap data");
|
|
9792
|
+
break;
|
|
9793
|
+
}
|
|
9794
|
+
ingest(repo, line.position, normalized, "bootstrap");
|
|
9795
|
+
if (repo.exhausted !== null)
|
|
9796
|
+
break;
|
|
9797
|
+
cursor = line.position;
|
|
9798
|
+
}
|
|
9799
|
+
if (repo.exhausted !== null)
|
|
9800
|
+
break;
|
|
9801
|
+
}
|
|
9802
|
+
} catch (err) {
|
|
9803
|
+
return unavailable(repo, reasonOf(err));
|
|
9804
|
+
}
|
|
9805
|
+
if (repo.exhausted !== null) {
|
|
9806
|
+
present(repo);
|
|
9807
|
+
return;
|
|
9808
|
+
}
|
|
9809
|
+
repo.connection = "catching-up";
|
|
9810
|
+
present(repo);
|
|
9811
|
+
startFollow(repo, cursor);
|
|
9812
|
+
}
|
|
9813
|
+
async function openFolder(repo) {
|
|
9814
|
+
const handle = repo.handle;
|
|
9815
|
+
repo.limits.set("rotation-unknown", {
|
|
9816
|
+
kind: "rotation-unknown",
|
|
9817
|
+
detail: "a fresh read covers the folder’s live files; rotated slots are not read, so an early record of a task may be missing."
|
|
9818
|
+
});
|
|
9819
|
+
const buffered = [];
|
|
9820
|
+
let cursor = null;
|
|
9821
|
+
let overflow = false;
|
|
9822
|
+
try {
|
|
9823
|
+
for (;; ) {
|
|
9824
|
+
const page = await handle.source.readPage(cursor, pageSize);
|
|
9825
|
+
if (closed)
|
|
9826
|
+
return;
|
|
9827
|
+
for (const gap of page.gaps) {
|
|
9828
|
+
repo.limits.set(`source-gap:${gap.from ?? ""}`, {
|
|
9829
|
+
kind: "source-gap",
|
|
9830
|
+
detail: `${gap.reason} (from ${gap.from ?? "an unknown position"}).`
|
|
9831
|
+
});
|
|
9832
|
+
}
|
|
9833
|
+
for (const line of page.lines) {
|
|
9834
|
+
repo.bytes += Buffer.byteLength(line.raw);
|
|
9835
|
+
if (repo.bytes > budgets.bootstrapBytes) {
|
|
9836
|
+
overflow = true;
|
|
9837
|
+
break;
|
|
9838
|
+
}
|
|
9839
|
+
buffered.push(line);
|
|
9840
|
+
}
|
|
9841
|
+
if (overflow)
|
|
9842
|
+
break;
|
|
9843
|
+
if (page.next !== null)
|
|
9844
|
+
cursor = page.next;
|
|
9845
|
+
if (page.lines.length === 0)
|
|
9846
|
+
break;
|
|
9847
|
+
}
|
|
9848
|
+
} catch (err) {
|
|
9849
|
+
return unavailable(repo, reasonOf(err));
|
|
9850
|
+
}
|
|
9851
|
+
const streams = new Map;
|
|
9852
|
+
let missing = false;
|
|
9853
|
+
let contradictory = false;
|
|
9854
|
+
for (const line of buffered) {
|
|
9855
|
+
const normalized = normalize(handle.source.id, line);
|
|
9856
|
+
if (normalized === null)
|
|
9857
|
+
continue;
|
|
9858
|
+
const stream = streamOf(line.position);
|
|
9859
|
+
const queue = streams.get(stream) ?? [];
|
|
9860
|
+
const time = recordedTime(normalized);
|
|
9861
|
+
const previous = queue.at(-1)?.effective ?? Number.NEGATIVE_INFINITY;
|
|
9862
|
+
if (time === null)
|
|
9863
|
+
missing = true;
|
|
9864
|
+
else if (time < previous)
|
|
9865
|
+
contradictory = true;
|
|
9866
|
+
queue.push({ stream, line, normalized, time, effective: time === null ? previous : Math.max(previous, time) });
|
|
9867
|
+
streams.set(stream, queue);
|
|
9868
|
+
}
|
|
9869
|
+
if (missing || contradictory) {
|
|
9870
|
+
repo.limits.set("source-limited", {
|
|
9871
|
+
kind: "source-limited",
|
|
9872
|
+
detail: `${missing ? "a record has no readable time" : "a stream’s recorded times run backwards"}: the order across machines is not authoritative.`
|
|
9873
|
+
});
|
|
9874
|
+
}
|
|
9875
|
+
const heads = new Map;
|
|
9876
|
+
const names = [...streams.keys()].sort();
|
|
9877
|
+
for (const name of names)
|
|
9878
|
+
heads.set(name, 0);
|
|
9879
|
+
let ambiguous = false;
|
|
9880
|
+
for (;; ) {
|
|
9881
|
+
let best = null;
|
|
9882
|
+
let tied = false;
|
|
9883
|
+
for (const name of names) {
|
|
9884
|
+
const queue = streams.get(name);
|
|
9885
|
+
const entry = queue[heads.get(name)];
|
|
9886
|
+
if (!entry)
|
|
9887
|
+
continue;
|
|
9888
|
+
if (best === null) {
|
|
9889
|
+
best = entry;
|
|
9890
|
+
continue;
|
|
9891
|
+
}
|
|
9892
|
+
const byTime = entry.effective - best.effective;
|
|
9893
|
+
const byStart = Number(!isLoopStart(entry.normalized)) - Number(!isLoopStart(best.normalized));
|
|
9894
|
+
if (byTime < 0 || byTime === 0 && byStart < 0) {
|
|
9895
|
+
best = entry;
|
|
9896
|
+
tied = false;
|
|
9897
|
+
} else if (byTime === 0 && byStart === 0) {
|
|
9898
|
+
const a = namedTask(entry.normalized);
|
|
9899
|
+
if (a !== null && a === namedTask(best.normalized))
|
|
9900
|
+
tied = true;
|
|
9901
|
+
}
|
|
9902
|
+
}
|
|
9903
|
+
if (best === null)
|
|
9904
|
+
break;
|
|
9905
|
+
if (tied)
|
|
9906
|
+
ambiguous = true;
|
|
9907
|
+
heads.set(best.stream, heads.get(best.stream) + 1);
|
|
9908
|
+
ingest(repo, best.line.position, best.normalized, "bootstrap");
|
|
9909
|
+
if (repo.exhausted !== null)
|
|
9910
|
+
break;
|
|
9911
|
+
}
|
|
9912
|
+
if (ambiguous) {
|
|
9913
|
+
repo.limits.set("source-limited", {
|
|
9914
|
+
kind: "source-limited",
|
|
9915
|
+
detail: "two machines recorded the same task at the same instant: their order is not authoritative."
|
|
9916
|
+
});
|
|
9917
|
+
}
|
|
9918
|
+
if (overflow)
|
|
9919
|
+
exhaust(repo, "bootstrap data");
|
|
9920
|
+
if (repo.exhausted === null)
|
|
9921
|
+
repo.connection = "catching-up";
|
|
9922
|
+
present(repo);
|
|
9923
|
+
if (repo.exhausted !== null)
|
|
9924
|
+
return;
|
|
9925
|
+
startFollow(repo, cursor);
|
|
9926
|
+
}
|
|
9927
|
+
async function open(repo) {
|
|
9928
|
+
const source = repo.repository.source;
|
|
9929
|
+
if (!source.ok)
|
|
9930
|
+
return unavailable(repo, source.message);
|
|
9931
|
+
try {
|
|
9932
|
+
repo.handle = await createSource(source.spec, repo.repository);
|
|
9933
|
+
if (closed)
|
|
9934
|
+
return;
|
|
9935
|
+
if (source.spec.kind === "server")
|
|
9936
|
+
await openServer(repo);
|
|
9937
|
+
else
|
|
9938
|
+
await openFolder(repo);
|
|
9939
|
+
} catch (err) {
|
|
9940
|
+
if (!closed)
|
|
9941
|
+
unavailable(repo, reasonOf(err));
|
|
9942
|
+
}
|
|
9943
|
+
}
|
|
9944
|
+
function onTick() {
|
|
9945
|
+
if (closed)
|
|
9946
|
+
return;
|
|
9947
|
+
const now = clock();
|
|
9948
|
+
for (const repo of repos.values()) {
|
|
9949
|
+
const result = tick(repo.picture, now);
|
|
9950
|
+
repo.picture = result.picture;
|
|
9951
|
+
if (result.changed.length > 0) {
|
|
9952
|
+
emit({
|
|
9953
|
+
type: "changed",
|
|
9954
|
+
repository: repo.repository.root,
|
|
9955
|
+
tasks: result.changed,
|
|
9956
|
+
snapshot: snapshotOf(repo)
|
|
9957
|
+
});
|
|
9958
|
+
}
|
|
9959
|
+
}
|
|
9960
|
+
}
|
|
9961
|
+
return {
|
|
9962
|
+
start() {
|
|
9963
|
+
if (closed)
|
|
9964
|
+
return Promise.resolve();
|
|
9965
|
+
if (started === null) {
|
|
9966
|
+
timer = timers.setInterval(onTick, tickMs);
|
|
9967
|
+
started = Promise.all([...repos.values()].map(open)).then(() => {
|
|
9968
|
+
return;
|
|
9969
|
+
});
|
|
9970
|
+
}
|
|
9971
|
+
return started;
|
|
9972
|
+
},
|
|
9973
|
+
snapshot(repository) {
|
|
9974
|
+
const repo = repos.get(repository);
|
|
9975
|
+
return repo ? snapshotOf(repo) : null;
|
|
9976
|
+
},
|
|
9977
|
+
snapshots() {
|
|
9978
|
+
return [...repos.values()].map(snapshotOf);
|
|
9979
|
+
},
|
|
9980
|
+
subscribe(listener) {
|
|
9981
|
+
listeners.add(listener);
|
|
9982
|
+
return () => {
|
|
9983
|
+
listeners.delete(listener);
|
|
9984
|
+
};
|
|
9985
|
+
},
|
|
9986
|
+
async close() {
|
|
9987
|
+
if (closed)
|
|
9988
|
+
return;
|
|
9989
|
+
closed = true;
|
|
9990
|
+
if (timer !== null)
|
|
9991
|
+
timers.clearInterval(timer);
|
|
9992
|
+
timer = null;
|
|
9993
|
+
listeners.clear();
|
|
9994
|
+
const stops = [];
|
|
9995
|
+
for (const repo of repos.values()) {
|
|
9996
|
+
const follow = repo.follow;
|
|
9997
|
+
repo.follow = null;
|
|
9998
|
+
if (follow)
|
|
9999
|
+
stops.push(follow.stop());
|
|
10000
|
+
}
|
|
10001
|
+
await Promise.all(stops);
|
|
10002
|
+
}
|
|
10003
|
+
};
|
|
10004
|
+
}
|
|
10005
|
+
|
|
7450
10006
|
// src/server/entry.ts
|
|
7451
10007
|
var FORCE_EXIT_MS = 5000;
|
|
7452
10008
|
function flag(name) {
|
|
@@ -7482,7 +10038,22 @@ for (const repository of repositories) {
|
|
|
7482
10038
|
if (!repository.source.ok)
|
|
7483
10039
|
console.error(`Studio: ${repository.root}: ${repository.source.message}`);
|
|
7484
10040
|
}
|
|
7485
|
-
var
|
|
10041
|
+
var pictures = null;
|
|
10042
|
+
var live = null;
|
|
10043
|
+
var host = createStudioHost({
|
|
10044
|
+
dir,
|
|
10045
|
+
dev,
|
|
10046
|
+
hostname,
|
|
10047
|
+
port,
|
|
10048
|
+
repositories,
|
|
10049
|
+
session: { secret: context.secret },
|
|
10050
|
+
compose: (composition) => {
|
|
10051
|
+
pictures = createPictureService({ repositories: composition.repositories, clock: composition.clock });
|
|
10052
|
+
live = createLiveSocket({ service: pictures, repositories: composition.repositories });
|
|
10053
|
+
composition.socket(LIVE_ROUTE, (socket) => live?.handler(socket));
|
|
10054
|
+
pictures.start();
|
|
10055
|
+
}
|
|
10056
|
+
});
|
|
7486
10057
|
var stopping = false;
|
|
7487
10058
|
function stop() {
|
|
7488
10059
|
if (stopping)
|
|
@@ -7490,7 +10061,8 @@ function stop() {
|
|
|
7490
10061
|
stopping = true;
|
|
7491
10062
|
const force = setTimeout(() => process.exit(1), FORCE_EXIT_MS);
|
|
7492
10063
|
force.unref();
|
|
7493
|
-
|
|
10064
|
+
live?.close();
|
|
10065
|
+
Promise.all([pictures?.close(), host.close()]).then(() => process.exit(0), () => process.exit(1));
|
|
7494
10066
|
}
|
|
7495
10067
|
process.once("SIGINT", stop);
|
|
7496
10068
|
process.once("SIGTERM", stop);
|