@abelxiaoxing/cadence 2.0.26 → 2.0.28
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/docs/conversation-mode.md +4 -3
- package/docs/service-management.md +1 -1
- package/package.json +1 -1
- package/src/conversation-context.mjs +84 -19
- package/src/native-build.ts +1 -1
- package/src/sdk-worker.mjs +5 -4
- package/src/service/contracts/models.ts +21 -1
- package/src/service/contracts/workbench.ts +89 -6
- package/src/service-contract.ts +1 -0
- package/src/service-host.mjs +650 -191
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +16 -16
- package/web/dist/manifest.json +7 -7
package/src/service-host.mjs
CHANGED
|
@@ -3611,6 +3611,9 @@ function decodeModelCandidate(value) {
|
|
|
3611
3611
|
invalid();
|
|
3612
3612
|
return candidate;
|
|
3613
3613
|
}
|
|
3614
|
+
function catalogOffers(entries, selector) {
|
|
3615
|
+
return entries.some((e) => e.provider === selector.provider && e.model === selector.model && e.api === selector.api && e.thinking.some((t) => t.selectable && t.level === selector.thinking));
|
|
3616
|
+
}
|
|
3614
3617
|
function decodeModelCatalog(value) {
|
|
3615
3618
|
canonicalJson({ kind: "ok", value }, LIMITS.pageBytes);
|
|
3616
3619
|
const o = closed(value, [
|
|
@@ -3618,19 +3621,22 @@ function decodeModelCatalog(value) {
|
|
|
3618
3621
|
"selected",
|
|
3619
3622
|
"status",
|
|
3620
3623
|
"entries",
|
|
3621
|
-
"withheld"
|
|
3624
|
+
"withheld",
|
|
3625
|
+
"remembered"
|
|
3622
3626
|
]);
|
|
3623
3627
|
const entries = list(o.entries, LIMITS.maxPageEntries, decodeModelCandidate);
|
|
3628
|
+
const remembered = nullable(o.remembered, decodeModelSelector);
|
|
3624
3629
|
const status = oneOf(o.status, ["complete", "capacity"]);
|
|
3625
3630
|
const withheld = integer(o.withheld, 0, LIMITS.maxPageEntries);
|
|
3626
|
-
if (status === "capacity" && (entries.length || withheld) || entries.length + withheld > LIMITS.maxPageEntries || new Set(entries.map((e) => canonicalJson([e.provider, e.model]))).size !== entries.length)
|
|
3631
|
+
if (status === "capacity" && (entries.length || withheld) || entries.length + withheld > LIMITS.maxPageEntries || new Set(entries.map((e) => canonicalJson([e.provider, e.model]))).size !== entries.length || remembered && !catalogOffers(entries, remembered))
|
|
3627
3632
|
invalid();
|
|
3628
3633
|
return {
|
|
3629
3634
|
version: decodeChoiceVersion(o.version),
|
|
3630
3635
|
selected: nullable(o.selected, decodeModelSelection),
|
|
3631
3636
|
status,
|
|
3632
3637
|
entries,
|
|
3633
|
-
withheld
|
|
3638
|
+
withheld,
|
|
3639
|
+
remembered
|
|
3634
3640
|
};
|
|
3635
3641
|
}
|
|
3636
3642
|
// src/service/contracts/workflow.ts
|
|
@@ -4064,6 +4070,10 @@ function decodeCadenceConversation(v) {
|
|
|
4064
4070
|
...degraded ? { notice: text(o.notice, 1024, true) } : {}
|
|
4065
4071
|
};
|
|
4066
4072
|
}
|
|
4073
|
+
function decodeChatCursor(value) {
|
|
4074
|
+
const o = closed(value, ["id", "updatedAt"]);
|
|
4075
|
+
return { id: decodeId(o.id), updatedAt: integer(o.updatedAt, 0) };
|
|
4076
|
+
}
|
|
4067
4077
|
var statuses = [
|
|
4068
4078
|
"queued",
|
|
4069
4079
|
"running",
|
|
@@ -4076,6 +4086,15 @@ var statuses = [
|
|
|
4076
4086
|
function decodeWorkbenchRead(v) {
|
|
4077
4087
|
const kind = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4078
4088
|
switch (kind) {
|
|
4089
|
+
case "conversation-transcript": {
|
|
4090
|
+
const o = closed(v, ["kind", "id", "turnId", "page"]);
|
|
4091
|
+
return {
|
|
4092
|
+
kind,
|
|
4093
|
+
id: decodeId(o.id),
|
|
4094
|
+
turnId: decodeId(o.turnId),
|
|
4095
|
+
page: integer(o.page, 0)
|
|
4096
|
+
};
|
|
4097
|
+
}
|
|
4079
4098
|
case "cadence-conversation": {
|
|
4080
4099
|
const o = closed(v, ["kind", "id"]);
|
|
4081
4100
|
return { kind, id: decodeId(o.id) };
|
|
@@ -4101,7 +4120,20 @@ function decodeWorkbenchRead(v) {
|
|
|
4101
4120
|
const o = closed(v, ["kind", "id"]);
|
|
4102
4121
|
return { kind, id: decodeId(o.id) };
|
|
4103
4122
|
}
|
|
4104
|
-
case "conversations":
|
|
4123
|
+
case "conversations": {
|
|
4124
|
+
const after = Object.hasOwn(v, "after"), scan = Object.hasOwn(v, "scan");
|
|
4125
|
+
const o = closed(v, [
|
|
4126
|
+
"kind",
|
|
4127
|
+
...after ? ["after"] : [],
|
|
4128
|
+
...scan ? ["scan"] : []
|
|
4129
|
+
]);
|
|
4130
|
+
return {
|
|
4131
|
+
kind,
|
|
4132
|
+
...after ? { after: nullable(o.after, decodeChatCursor) } : {},
|
|
4133
|
+
...scan ? { scan: decodeId(o.scan) } : {}
|
|
4134
|
+
};
|
|
4135
|
+
}
|
|
4136
|
+
case "conversation-active":
|
|
4105
4137
|
case "projects":
|
|
4106
4138
|
closed(v, ["kind"]);
|
|
4107
4139
|
return { kind };
|
|
@@ -4240,13 +4272,20 @@ function decodeWorkbenchCommand(v) {
|
|
|
4240
4272
|
function decodeChatTurn(v) {
|
|
4241
4273
|
const keys2 = ["id", "epoch", "status", "items", "notice"];
|
|
4242
4274
|
const current = v !== null && typeof v === "object" && Object.hasOwn(v, "omitted");
|
|
4243
|
-
const
|
|
4275
|
+
const paged = v !== null && typeof v === "object" && Object.hasOwn(v, "transcript");
|
|
4276
|
+
const o = closed(v, [
|
|
4277
|
+
...keys2,
|
|
4278
|
+
...current ? ["omitted"] : [],
|
|
4279
|
+
...paged ? ["transcript"] : []
|
|
4280
|
+
]);
|
|
4281
|
+
const transcript = paged ? closed(o.transcript, ["pages"]) : null;
|
|
4244
4282
|
const omitted = current ? closed(o.omitted, ["thinking", "media", "other"]) : null;
|
|
4245
4283
|
return {
|
|
4246
4284
|
id: decodeId(o.id),
|
|
4247
4285
|
epoch: decodeId(o.epoch),
|
|
4248
4286
|
status: oneOf(o.status, statuses),
|
|
4249
4287
|
notice: text(o.notice, 1024),
|
|
4288
|
+
...transcript ? { transcript: { pages: integer(transcript.pages, 0) } } : {},
|
|
4250
4289
|
omitted: {
|
|
4251
4290
|
thinking: omitted ? bool(omitted.thinking) : false,
|
|
4252
4291
|
media: omitted ? bool(omitted.media) : false,
|
|
@@ -4287,6 +4326,13 @@ function decodeChatSummary(v) {
|
|
|
4287
4326
|
function decodeWorkbenchResult(v) {
|
|
4288
4327
|
canonicalJson(v, 512 * 1024);
|
|
4289
4328
|
const k = Object.getOwnPropertyDescriptor(v ?? {}, "kind")?.value;
|
|
4329
|
+
if (k === "conversation-transcript") {
|
|
4330
|
+
const o2 = closed(v, ["kind", "conversationId", "page", "turn"]);
|
|
4331
|
+
const turn = decodeChatTurn(o2.turn), page = integer(o2.page, 0);
|
|
4332
|
+
if (!turn.transcript || page >= turn.transcript.pages)
|
|
4333
|
+
throw new ContractError("invalid");
|
|
4334
|
+
return { kind: k, conversationId: decodeId(o2.conversationId), page, turn };
|
|
4335
|
+
}
|
|
4290
4336
|
if (k === "workflow-transcript") {
|
|
4291
4337
|
const o2 = closed(v, ["kind", "runId", "stepId", "turn"]);
|
|
4292
4338
|
return {
|
|
@@ -4372,9 +4418,26 @@ function decodeWorkbenchResult(v) {
|
|
|
4372
4418
|
conversationId: nullable(o2.conversationId, decodeId)
|
|
4373
4419
|
};
|
|
4374
4420
|
}
|
|
4375
|
-
if (kind === "conversations") {
|
|
4421
|
+
if (kind === "conversations-progress") {
|
|
4422
|
+
const o2 = closed(v, ["kind", "after", "scan"]);
|
|
4423
|
+
return {
|
|
4424
|
+
kind,
|
|
4425
|
+
after: nullable(o2.after, decodeChatCursor),
|
|
4426
|
+
scan: decodeId(o2.scan)
|
|
4427
|
+
};
|
|
4428
|
+
}
|
|
4429
|
+
if (kind === "conversation-active") {
|
|
4376
4430
|
const o2 = closed(v, ["kind", "entries"]);
|
|
4377
|
-
return { kind, entries: list(o2.entries,
|
|
4431
|
+
return { kind, entries: list(o2.entries, 8, decodeChatSummary) };
|
|
4432
|
+
}
|
|
4433
|
+
if (kind === "conversations") {
|
|
4434
|
+
const paged = Object.hasOwn(v, "next");
|
|
4435
|
+
const o2 = closed(v, ["kind", "entries", ...paged ? ["next"] : []]);
|
|
4436
|
+
return {
|
|
4437
|
+
kind,
|
|
4438
|
+
entries: list(o2.entries, 200, decodeChatSummary),
|
|
4439
|
+
...paged ? { next: nullable(o2.next, decodeChatCursor) } : {}
|
|
4440
|
+
};
|
|
4378
4441
|
}
|
|
4379
4442
|
if (kind === "conversation") {
|
|
4380
4443
|
const o2 = closed(v, ["kind", "summary", "turns", "before"]);
|
|
@@ -9884,7 +9947,7 @@ async function createGateway(value, hostUrl, source) {
|
|
|
9884
9947
|
const input = decodeGatewayInput(value), store = new GatewayStore(input.serverConfig, input.stateHome);
|
|
9885
9948
|
const web = loadWebAssets(source?.webDirectory);
|
|
9886
9949
|
const build2 = {
|
|
9887
|
-
contractHash: "
|
|
9950
|
+
contractHash: "45f3666b0fc0d853e95e618b50156819f6282ce648e9703b876d9658ae15b78f",
|
|
9888
9951
|
assets: {
|
|
9889
9952
|
sourceHash: web.manifest.sourceHash,
|
|
9890
9953
|
contractHash: web.manifest.contractHash,
|
|
@@ -10469,7 +10532,7 @@ Content-Length: 0\r
|
|
|
10469
10532
|
|
|
10470
10533
|
// src/service/host.ts
|
|
10471
10534
|
import { spawn as spawn4 } from "node:child_process";
|
|
10472
|
-
import { lstatSync as
|
|
10535
|
+
import { lstatSync as lstatSync21, opendirSync as opendirSync15 } from "node:fs";
|
|
10473
10536
|
import { join as join32 } from "node:path";
|
|
10474
10537
|
import { fileURLToPath as fileURLToPath6 } from "node:url";
|
|
10475
10538
|
|
|
@@ -21173,13 +21236,14 @@ import { fileURLToPath as fileURLToPath4 } from "node:url";
|
|
|
21173
21236
|
|
|
21174
21237
|
// src/conversation-context.mjs
|
|
21175
21238
|
var CONTEXT_BYTES = 2 * 1024 * 1024;
|
|
21176
|
-
var
|
|
21239
|
+
var CONTEXT_RECORD_BYTES = LIMITS.detailBytes;
|
|
21240
|
+
var clean = (s, secrets, max = CONTEXT_RECORD_BYTES) => sanitizeText(text(s, max), secrets);
|
|
21177
21241
|
function argumentsData(value, secrets) {
|
|
21178
21242
|
const sanitizer = new DetailSanitizer("canonical-json", secrets);
|
|
21179
|
-
const source = sanitizer.write(canonicalJson(value,
|
|
21243
|
+
const source = sanitizer.write(canonicalJson(value, CONTEXT_RECORD_BYTES)) + sanitizer.end();
|
|
21180
21244
|
if (sanitizer.limited)
|
|
21181
21245
|
throw new Error("Unsupported context arguments");
|
|
21182
|
-
return parseWireJson(source,
|
|
21246
|
+
return parseWireJson(source, CONTEXT_RECORD_BYTES);
|
|
21183
21247
|
}
|
|
21184
21248
|
function own2(value, key2) {
|
|
21185
21249
|
const d = value && typeof value === "object" ? Object.getOwnPropertyDescriptor(value, key2) : undefined;
|
|
@@ -22159,12 +22223,26 @@ function runProcess(spec) {
|
|
|
22159
22223
|
contextStream?.on("data", (chunk) => {
|
|
22160
22224
|
if (settled || stopping)
|
|
22161
22225
|
return;
|
|
22162
|
-
|
|
22163
|
-
|
|
22226
|
+
try {
|
|
22227
|
+
let offset = 0;
|
|
22228
|
+
while (offset < chunk.length) {
|
|
22229
|
+
const end = chunk.indexOf(10, offset);
|
|
22230
|
+
const part = chunk.subarray(offset, end < 0 ? chunk.length : end);
|
|
22231
|
+
contextBytes += part.length;
|
|
22232
|
+
if (contextBytes > CONTEXT_RECORD_BYTES)
|
|
22233
|
+
throw new Error("Checkpoint record too large");
|
|
22234
|
+
contextChunks.push(part);
|
|
22235
|
+
if (end < 0)
|
|
22236
|
+
break;
|
|
22237
|
+
const source = new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(contextChunks, contextBytes));
|
|
22238
|
+
contextChunks.length = 0;
|
|
22239
|
+
contextBytes = 0;
|
|
22240
|
+
spec.onContext?.(source);
|
|
22241
|
+
offset = end + 1;
|
|
22242
|
+
}
|
|
22243
|
+
} catch {
|
|
22164
22244
|
stop("failed");
|
|
22165
|
-
return;
|
|
22166
22245
|
}
|
|
22167
|
-
contextChunks.push(chunk);
|
|
22168
22246
|
});
|
|
22169
22247
|
contextStream?.once("end", () => {
|
|
22170
22248
|
contextEnded = true;
|
|
@@ -22224,9 +22302,9 @@ function runProcess(spec) {
|
|
|
22224
22302
|
const close = () => {
|
|
22225
22303
|
if (spec.onContext && !stopping && code2 === 0) {
|
|
22226
22304
|
try {
|
|
22227
|
-
if (!contextEnded)
|
|
22305
|
+
if (!contextEnded || contextBytes)
|
|
22228
22306
|
throw new Error("Context EOF missing");
|
|
22229
|
-
spec.
|
|
22307
|
+
spec.onContextEnd?.();
|
|
22230
22308
|
} catch {
|
|
22231
22309
|
stopping = "failed";
|
|
22232
22310
|
}
|
|
@@ -22329,22 +22407,33 @@ function launchConversation(prepared, conversation, signal, options) {
|
|
|
22329
22407
|
if (!encoded2 || encoded2.input !== input)
|
|
22330
22408
|
throw new Error("Invalid chat launch");
|
|
22331
22409
|
const secrets = encoded2.secrets;
|
|
22410
|
+
let contextCount = 0, contextComplete = false;
|
|
22332
22411
|
return launchEncoded(encoded2, signal, options.timeoutMs, {
|
|
22333
22412
|
maySignal: options.maySignal,
|
|
22334
22413
|
onNative: options.onNative,
|
|
22335
22414
|
onCoordinator: options.onCoordinator,
|
|
22336
22415
|
onFrame: (frame) => options.onFrame(frame, secrets),
|
|
22337
22416
|
onContext: (source) => {
|
|
22338
|
-
|
|
22339
|
-
"
|
|
22340
|
-
|
|
22341
|
-
|
|
22342
|
-
|
|
22417
|
+
if (contextComplete)
|
|
22418
|
+
throw new Error("Checkpoint after end");
|
|
22419
|
+
const value = parseWireJson(source, CONTEXT_RECORD_BYTES);
|
|
22420
|
+
if (value.schema === "cadence-conversation-stream-end/1") {
|
|
22421
|
+
const record3 = closed(value, ["schema", "count"]);
|
|
22422
|
+
if (record3.count !== contextCount)
|
|
22423
|
+
throw new Error("Invalid checkpoint count");
|
|
22424
|
+
contextComplete = true;
|
|
22425
|
+
return;
|
|
22426
|
+
}
|
|
22427
|
+
const record2 = closed(value, ["schema", "message"]);
|
|
22428
|
+
if (record2.schema !== "cadence-conversation-stream/1")
|
|
22343
22429
|
throw new Error("Invalid checkpoint");
|
|
22344
|
-
options.onContext(conversationMessages(
|
|
22345
|
-
|
|
22346
|
-
|
|
22347
|
-
|
|
22430
|
+
options.onContext(conversationMessages([record2.message], [...options.ownerSecrets(), ...secrets])[0]);
|
|
22431
|
+
contextCount++;
|
|
22432
|
+
},
|
|
22433
|
+
onContextEnd: () => {
|
|
22434
|
+
if (!contextComplete)
|
|
22435
|
+
throw new Error("Incomplete checkpoint");
|
|
22436
|
+
options.onContextEnd();
|
|
22348
22437
|
}
|
|
22349
22438
|
});
|
|
22350
22439
|
});
|
|
@@ -22569,7 +22658,15 @@ class OwnerModelChoice {
|
|
|
22569
22658
|
this.options.models.assertCurrent(before.selected);
|
|
22570
22659
|
if (canonicalJson(before) !== canonicalJson(this.snapshot()))
|
|
22571
22660
|
throw new ContractError("stale");
|
|
22572
|
-
|
|
22661
|
+
let remembered = null;
|
|
22662
|
+
try {
|
|
22663
|
+
const saved = this.options.recall?.() ?? null;
|
|
22664
|
+
if (saved && catalogOffers(catalog.entries, saved))
|
|
22665
|
+
remembered = saved;
|
|
22666
|
+
} catch {
|
|
22667
|
+
remembered = null;
|
|
22668
|
+
}
|
|
22669
|
+
return decodeModelCatalog({ ...before, ...catalog, remembered });
|
|
22573
22670
|
}
|
|
22574
22671
|
choose(value) {
|
|
22575
22672
|
const request3 = decodeChooseModelRequest(value);
|
|
@@ -24129,12 +24226,12 @@ class ServiceOwner {
|
|
|
24129
24226
|
}
|
|
24130
24227
|
|
|
24131
24228
|
// src/service/workbench.ts
|
|
24132
|
-
import { opendirSync as
|
|
24229
|
+
import { opendirSync as opendirSync14 } from "node:fs";
|
|
24133
24230
|
import { join as join31 } from "node:path";
|
|
24134
24231
|
|
|
24135
24232
|
// src/service/conversation-workflow.ts
|
|
24136
|
-
import { randomUUID as
|
|
24137
|
-
import { opendirSync as
|
|
24233
|
+
import { randomUUID as randomUUID21 } from "node:crypto";
|
|
24234
|
+
import { opendirSync as opendirSync11 } from "node:fs";
|
|
24138
24235
|
import { dirname as dirname17, join as join27 } from "node:path";
|
|
24139
24236
|
|
|
24140
24237
|
// src/cadence-instructions.ts
|
|
@@ -24146,12 +24243,246 @@ Present the returned complete plan and invite a single clear execution approval
|
|
|
24146
24243
|
During accepted work answer questions, report actual retained progress, and let the service execute. cadence_cancel is explicit cancellation, not navigation/exit. Never claim a worker's prose is a passing check or automatic review is human review. Failed/unknown/blocked outcomes are not success. A completed goal leaves Cadence ON and ready for the next goal; do not require /cadence again. Unrelated discussion is still ordinary conversation within this mode, not implicit permission to start another implementation. Tools operate on the host; this is not a sandbox.`;
|
|
24147
24244
|
|
|
24148
24245
|
// src/service/conversations.ts
|
|
24149
|
-
import { randomUUID as
|
|
24150
|
-
import { lstatSync as
|
|
24246
|
+
import { randomUUID as randomUUID20 } from "node:crypto";
|
|
24247
|
+
import { lstatSync as lstatSync16, opendirSync as opendirSync10, realpathSync as realpathSync8, statfsSync as statfsSync2 } from "node:fs";
|
|
24151
24248
|
import { dirname as dirname16, join as join26 } from "node:path";
|
|
24249
|
+
|
|
24250
|
+
// src/service/conversation-history.ts
|
|
24251
|
+
import { randomUUID as randomUUID19 } from "node:crypto";
|
|
24252
|
+
import { lstatSync as lstatSync15, opendirSync as opendirSync9 } from "node:fs";
|
|
24253
|
+
var CONVERSATION_PAGE = 50;
|
|
24254
|
+
var SCAN_ENTRIES = 8;
|
|
24255
|
+
var SCANS = 16;
|
|
24256
|
+
var IDLE_MS = 120000;
|
|
24257
|
+
var compareChats = (a, b) => b.updatedAt - a.updatedAt || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
24258
|
+
|
|
24259
|
+
class ConversationHistory {
|
|
24260
|
+
path;
|
|
24261
|
+
directory;
|
|
24262
|
+
summary;
|
|
24263
|
+
scans = new Map;
|
|
24264
|
+
closed = false;
|
|
24265
|
+
constructor(path, directory, summary2) {
|
|
24266
|
+
this.path = path;
|
|
24267
|
+
this.directory = directory;
|
|
24268
|
+
this.summary = summary2;
|
|
24269
|
+
}
|
|
24270
|
+
incarnation() {
|
|
24271
|
+
const st = lstatSync15(this.path, { bigint: true });
|
|
24272
|
+
if (!st.isDirectory() || st.isSymbolicLink())
|
|
24273
|
+
throw new ContractError("corrupt-selected");
|
|
24274
|
+
return `${st.dev}:${st.ino}`;
|
|
24275
|
+
}
|
|
24276
|
+
read(after, cursor) {
|
|
24277
|
+
if (this.closed)
|
|
24278
|
+
throw new ContractError("unavailable");
|
|
24279
|
+
let state;
|
|
24280
|
+
if (cursor) {
|
|
24281
|
+
const selected2 = this.scans.get(cursor);
|
|
24282
|
+
if (!selected2 || selected2.after?.id !== after?.id || selected2.after?.updatedAt !== after?.updatedAt)
|
|
24283
|
+
throw new ContractError("stale");
|
|
24284
|
+
this.scans.delete(cursor);
|
|
24285
|
+
clearTimeout(selected2.timer);
|
|
24286
|
+
state = selected2;
|
|
24287
|
+
} else {
|
|
24288
|
+
if (!this.directory())
|
|
24289
|
+
return { kind: "conversations", entries: [], next: null };
|
|
24290
|
+
if (this.scans.size >= SCANS)
|
|
24291
|
+
throw new ContractError("capacity");
|
|
24292
|
+
const incarnation3 = this.incarnation();
|
|
24293
|
+
state = {
|
|
24294
|
+
dir: opendirSync9(this.path),
|
|
24295
|
+
incarnation: incarnation3,
|
|
24296
|
+
after,
|
|
24297
|
+
entries: []
|
|
24298
|
+
};
|
|
24299
|
+
}
|
|
24300
|
+
let suspended = false;
|
|
24301
|
+
try {
|
|
24302
|
+
if (!this.directory() || this.incarnation() !== state.incarnation)
|
|
24303
|
+
throw new ContractError("stale");
|
|
24304
|
+
const started = performance.now();
|
|
24305
|
+
for (let n = 0;n < SCAN_ENTRIES; n++) {
|
|
24306
|
+
if (n && performance.now() - started >= LIMITS.searchMs)
|
|
24307
|
+
break;
|
|
24308
|
+
const e = state.dir.readSync();
|
|
24309
|
+
if (!e) {
|
|
24310
|
+
const entries2 = state.entries.slice(0, CONVERSATION_PAGE);
|
|
24311
|
+
const last = entries2.at(-1);
|
|
24312
|
+
return {
|
|
24313
|
+
kind: "conversations",
|
|
24314
|
+
entries: entries2,
|
|
24315
|
+
next: state.entries.length > entries2.length && last ? { id: last.id, updatedAt: last.updatedAt } : null
|
|
24316
|
+
};
|
|
24317
|
+
}
|
|
24318
|
+
if (!e.isFile() || !e.name.endsWith(".json"))
|
|
24319
|
+
throw new ContractError("corrupt-selected");
|
|
24320
|
+
const summary2 = this.summary(decodeId(e.name.slice(0, -5)));
|
|
24321
|
+
if (after && compareChats(summary2, after) <= 0)
|
|
24322
|
+
continue;
|
|
24323
|
+
if (state.entries.some((row) => row.id === summary2.id))
|
|
24324
|
+
continue;
|
|
24325
|
+
state.entries.push(summary2);
|
|
24326
|
+
state.entries.sort(compareChats);
|
|
24327
|
+
if (state.entries.length > CONVERSATION_PAGE + 1)
|
|
24328
|
+
state.entries.pop();
|
|
24329
|
+
}
|
|
24330
|
+
const scan = randomUUID19();
|
|
24331
|
+
state.timer = setTimeout(() => {
|
|
24332
|
+
this.scans.delete(scan);
|
|
24333
|
+
try {
|
|
24334
|
+
state.dir.closeSync();
|
|
24335
|
+
} catch {
|
|
24336
|
+
this.closed = true;
|
|
24337
|
+
}
|
|
24338
|
+
}, IDLE_MS);
|
|
24339
|
+
state.timer.unref();
|
|
24340
|
+
this.scans.set(scan, state);
|
|
24341
|
+
suspended = true;
|
|
24342
|
+
return { kind: "conversations-progress", after, scan };
|
|
24343
|
+
} finally {
|
|
24344
|
+
if (!suspended)
|
|
24345
|
+
state.dir.closeSync();
|
|
24346
|
+
}
|
|
24347
|
+
}
|
|
24348
|
+
close() {
|
|
24349
|
+
this.closed = true;
|
|
24350
|
+
let failed = false;
|
|
24351
|
+
for (const state of this.scans.values()) {
|
|
24352
|
+
clearTimeout(state.timer);
|
|
24353
|
+
try {
|
|
24354
|
+
state.dir.closeSync();
|
|
24355
|
+
} catch {
|
|
24356
|
+
failed = true;
|
|
24357
|
+
}
|
|
24358
|
+
}
|
|
24359
|
+
this.scans.clear();
|
|
24360
|
+
if (failed)
|
|
24361
|
+
throw new ContractError("unavailable");
|
|
24362
|
+
}
|
|
24363
|
+
}
|
|
24364
|
+
|
|
24365
|
+
// src/service/conversation-transcript.ts
|
|
24366
|
+
var TRANSCRIPT_PAGE_BYTES = 384 * 1024;
|
|
24367
|
+
var PREVIEW_BYTES = 300 * 1024;
|
|
24368
|
+
var FIELD_CHARS = 16 * 1024;
|
|
24369
|
+
var PREVIEW_MARK = `
|
|
24370
|
+
[Preview only; open the full transcript.]`;
|
|
24371
|
+
function part(value, offset, chars = FIELD_CHARS) {
|
|
24372
|
+
let end = Math.min(value.length, offset + chars);
|
|
24373
|
+
if (end < value.length && /[\uD800-\uDBFF]/u.test(value[end - 1]))
|
|
24374
|
+
end--;
|
|
24375
|
+
return value.slice(offset, end);
|
|
24376
|
+
}
|
|
24377
|
+
|
|
24378
|
+
class ConversationTranscript {
|
|
24379
|
+
turn;
|
|
24380
|
+
write;
|
|
24381
|
+
state = { pages: 0 };
|
|
24382
|
+
workingSizes = new Map;
|
|
24383
|
+
workingBytes = 0;
|
|
24384
|
+
sizes = new Map;
|
|
24385
|
+
bytes = 4096;
|
|
24386
|
+
pending = new Map;
|
|
24387
|
+
constructor(turn, write2) {
|
|
24388
|
+
this.turn = turn;
|
|
24389
|
+
this.write = write2;
|
|
24390
|
+
turn.transcript = this.state;
|
|
24391
|
+
for (const item of [...turn.items]) {
|
|
24392
|
+
this.archive(item);
|
|
24393
|
+
this.preview(item);
|
|
24394
|
+
}
|
|
24395
|
+
}
|
|
24396
|
+
item(id, role2) {
|
|
24397
|
+
let item = this.pending.get(id);
|
|
24398
|
+
if (!item) {
|
|
24399
|
+
if (this.pending.size >= 256)
|
|
24400
|
+
throw new ContractError("capacity");
|
|
24401
|
+
item = { id, role: role2, text: "", name: "", args: "", error: false };
|
|
24402
|
+
this.pending.set(id, item);
|
|
24403
|
+
this.preview(item);
|
|
24404
|
+
}
|
|
24405
|
+
return item;
|
|
24406
|
+
}
|
|
24407
|
+
find(id) {
|
|
24408
|
+
return this.pending.get(id);
|
|
24409
|
+
}
|
|
24410
|
+
preview(item) {
|
|
24411
|
+
if (this.pending.has(item.id)) {
|
|
24412
|
+
const size3 = Buffer.byteLength(item.text) + Buffer.byteLength(item.args) + Buffer.byteLength(item.name);
|
|
24413
|
+
this.workingBytes += size3 - (this.workingSizes.get(item.id) ?? 0);
|
|
24414
|
+
this.workingSizes.set(item.id, size3);
|
|
24415
|
+
if (this.workingBytes > 2 * LIMITS.detailBytes)
|
|
24416
|
+
throw new ContractError("capacity");
|
|
24417
|
+
}
|
|
24418
|
+
const clip = (value) => {
|
|
24419
|
+
const prefix = part(value, 0);
|
|
24420
|
+
return prefix.length < value.length ? prefix + PREVIEW_MARK : value;
|
|
24421
|
+
};
|
|
24422
|
+
const preview = { ...item, text: clip(item.text), args: clip(item.args) };
|
|
24423
|
+
const size2 = Buffer.byteLength(canonicalJson(preview, PREVIEW_BYTES));
|
|
24424
|
+
this.bytes += size2 - (this.sizes.get(item.id) ?? 0);
|
|
24425
|
+
this.sizes.set(item.id, size2);
|
|
24426
|
+
const at = this.turn.items.findIndex((i) => i.id === item.id);
|
|
24427
|
+
if (at < 0)
|
|
24428
|
+
this.turn.items.push(preview);
|
|
24429
|
+
else
|
|
24430
|
+
this.turn.items[at] = preview;
|
|
24431
|
+
while (this.bytes > PREVIEW_BYTES || this.turn.items.length > 128) {
|
|
24432
|
+
if (this.turn.items.length <= 2)
|
|
24433
|
+
throw new ContractError("oversize");
|
|
24434
|
+
const [removed] = this.turn.items.splice(this.turn.items[0].role === "user" ? 1 : 0, 1);
|
|
24435
|
+
this.bytes -= this.sizes.get(removed.id) ?? 0;
|
|
24436
|
+
this.sizes.delete(removed.id);
|
|
24437
|
+
}
|
|
24438
|
+
}
|
|
24439
|
+
complete(id) {
|
|
24440
|
+
const item = this.pending.get(id);
|
|
24441
|
+
if (!item)
|
|
24442
|
+
return;
|
|
24443
|
+
this.preview(item);
|
|
24444
|
+
this.archive(item);
|
|
24445
|
+
this.pending.delete(id);
|
|
24446
|
+
this.workingBytes -= this.workingSizes.get(id) ?? 0;
|
|
24447
|
+
this.workingSizes.delete(id);
|
|
24448
|
+
}
|
|
24449
|
+
finish() {
|
|
24450
|
+
for (const id of this.pending.keys())
|
|
24451
|
+
this.complete(id);
|
|
24452
|
+
}
|
|
24453
|
+
archive(item) {
|
|
24454
|
+
let text2 = 0, args = 0;
|
|
24455
|
+
do {
|
|
24456
|
+
let chars = 128 * 1024;
|
|
24457
|
+
const fragment = { ...item, text: "", args: part(item.args, args) };
|
|
24458
|
+
for (;; ) {
|
|
24459
|
+
fragment.text = part(item.text, text2, chars);
|
|
24460
|
+
try {
|
|
24461
|
+
if (Buffer.byteLength(fragment.text) > 256 * 1024)
|
|
24462
|
+
throw new ContractError("oversize");
|
|
24463
|
+
canonicalJson(fragment, TRANSCRIPT_PAGE_BYTES - 4096);
|
|
24464
|
+
break;
|
|
24465
|
+
} catch (error) {
|
|
24466
|
+
if (!(error instanceof ContractError) || error.code !== "oversize" || chars <= FIELD_CHARS)
|
|
24467
|
+
throw error;
|
|
24468
|
+
chars /= 2;
|
|
24469
|
+
}
|
|
24470
|
+
}
|
|
24471
|
+
const page = this.state.pages;
|
|
24472
|
+
if (!Number.isSafeInteger(page + 1))
|
|
24473
|
+
throw new ContractError("capacity");
|
|
24474
|
+
this.write({ turnId: this.turn.id, page, items: [fragment] });
|
|
24475
|
+
this.state.pages = page + 1;
|
|
24476
|
+
text2 += fragment.text.length;
|
|
24477
|
+
args += fragment.args.length;
|
|
24478
|
+
} while (text2 < item.text.length || args < item.args.length);
|
|
24479
|
+
}
|
|
24480
|
+
}
|
|
24481
|
+
|
|
24482
|
+
// src/service/conversations.ts
|
|
24152
24483
|
var TURN_BYTES = 384 * 1024;
|
|
24153
24484
|
var META_BYTES = 64 * 1024;
|
|
24154
|
-
var
|
|
24485
|
+
var SUMMARY_CACHE = 256;
|
|
24155
24486
|
var OMITTED_FLAG = {
|
|
24156
24487
|
"hidden-thinking": "thinking",
|
|
24157
24488
|
binary: "media",
|
|
@@ -24159,7 +24490,7 @@ var OMITTED_FLAG = {
|
|
|
24159
24490
|
};
|
|
24160
24491
|
function observedFile(path) {
|
|
24161
24492
|
try {
|
|
24162
|
-
const s =
|
|
24493
|
+
const s = lstatSync16(path);
|
|
24163
24494
|
return [s.dev, s.ino, s.size, s.mode, s.nlink, s.ctimeMs, s.mtimeMs].join(":");
|
|
24164
24495
|
} catch (error) {
|
|
24165
24496
|
if (error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"))
|
|
@@ -24172,14 +24503,18 @@ class ConversationCapture {
|
|
|
24172
24503
|
turn;
|
|
24173
24504
|
save;
|
|
24174
24505
|
secrets;
|
|
24506
|
+
transcript;
|
|
24175
24507
|
details = new Map;
|
|
24176
24508
|
messageText = new Map;
|
|
24177
|
-
constructor(turn, save, secrets) {
|
|
24509
|
+
constructor(turn, save, secrets, transcript) {
|
|
24178
24510
|
this.turn = turn;
|
|
24179
24511
|
this.save = save;
|
|
24180
24512
|
this.secrets = secrets;
|
|
24513
|
+
this.transcript = transcript;
|
|
24181
24514
|
}
|
|
24182
24515
|
item(id, role2) {
|
|
24516
|
+
if (this.transcript)
|
|
24517
|
+
return this.transcript.item(id, role2);
|
|
24183
24518
|
let item = this.turn.items.find((i) => i.id === id);
|
|
24184
24519
|
if (!item) {
|
|
24185
24520
|
if (this.turn.items.length >= 256)
|
|
@@ -24197,6 +24532,13 @@ class ConversationCapture {
|
|
|
24197
24532
|
return item;
|
|
24198
24533
|
}
|
|
24199
24534
|
update(item, field, value, maximum) {
|
|
24535
|
+
if (this.transcript) {
|
|
24536
|
+
if (Buffer.byteLength(value) > LIMITS.detailBytes)
|
|
24537
|
+
throw new ContractError("capacity");
|
|
24538
|
+
item[field] = value;
|
|
24539
|
+
this.transcript.preview(item);
|
|
24540
|
+
return;
|
|
24541
|
+
}
|
|
24200
24542
|
const before = item[field];
|
|
24201
24543
|
try {
|
|
24202
24544
|
if (Buffer.byteLength(value) > maximum)
|
|
@@ -24226,11 +24568,11 @@ class ConversationCapture {
|
|
|
24226
24568
|
if (!d)
|
|
24227
24569
|
return;
|
|
24228
24570
|
d.text += e.kind === "detail-chunk" ? d.sanitizer.write(e.text) : d.sanitizer.end();
|
|
24229
|
-
if (Buffer.byteLength(d.text) > 256 * 1024)
|
|
24571
|
+
if (Buffer.byteLength(d.text) > (this.transcript ? LIMITS.detailBytes : 256 * 1024))
|
|
24230
24572
|
throw new ContractError("capacity");
|
|
24231
24573
|
const o = d.descriptor.owner;
|
|
24232
24574
|
if (o.kind === "message") {
|
|
24233
|
-
const role2 = this.turn.items.find((i) => i.id === o.messageId)?.role;
|
|
24575
|
+
const role2 = (this.transcript?.find(o.messageId) ?? this.turn.items.find((i) => i.id === o.messageId))?.role;
|
|
24234
24576
|
if (role2 === "assistant" && o.stage === "stream") {
|
|
24235
24577
|
this.messageText.set(o.blockId, {
|
|
24236
24578
|
messageId: o.messageId,
|
|
@@ -24239,7 +24581,7 @@ class ConversationCapture {
|
|
|
24239
24581
|
this.update(this.item(o.messageId, "assistant"), "text", [...this.messageText.values()].filter((block) => block.messageId === o.messageId).map((block) => block.text).join(""), 256 * 1024);
|
|
24240
24582
|
}
|
|
24241
24583
|
} else if (o.kind === "message-final") {
|
|
24242
|
-
const item = this.turn.items.find((i) => i.id === o.messageId);
|
|
24584
|
+
const item = this.transcript?.find(o.messageId) ?? this.turn.items.find((i) => i.id === o.messageId);
|
|
24243
24585
|
if (item?.role === "assistant")
|
|
24244
24586
|
this.update(item, "text", d.text, 256 * 1024);
|
|
24245
24587
|
} else if (o.kind === "tool") {
|
|
@@ -24262,22 +24604,26 @@ class ConversationCapture {
|
|
|
24262
24604
|
this.turn.notice = `worker-failure: ${e.code}`;
|
|
24263
24605
|
else if (e.kind === "omitted-block" && e.block.owner.kind !== "context" && e.block.owner.kind !== "handoff")
|
|
24264
24606
|
this.turn.omitted[OMITTED_FLAG[e.block.reason]] = true;
|
|
24265
|
-
if ([...this.details.values()].reduce((size2, detail) => size2 + Buffer.byteLength(detail.text) + detail.sanitizer.retainedBytes, 0) > TURN_BYTES)
|
|
24607
|
+
if ([...this.details.values()].reduce((size2, detail) => size2 + Buffer.byteLength(detail.text) + detail.sanitizer.retainedBytes, 0) > (this.transcript ? 2 * LIMITS.detailBytes : TURN_BYTES))
|
|
24266
24608
|
throw new ContractError("capacity");
|
|
24267
|
-
|
|
24609
|
+
if (!this.transcript)
|
|
24610
|
+
canonicalJson(this.turn, TURN_BYTES);
|
|
24268
24611
|
if (e.kind === "message-end") {
|
|
24269
24612
|
for (const [id, block] of this.messageText)
|
|
24270
24613
|
if (block.messageId === e.messageId)
|
|
24271
24614
|
this.messageText.delete(id);
|
|
24272
24615
|
}
|
|
24273
|
-
if (e.kind === "message-end" || e.kind === "tool-end")
|
|
24616
|
+
if (e.kind === "message-end" || e.kind === "tool-end") {
|
|
24617
|
+
this.transcript?.complete(e.kind === "message-end" ? e.messageId : e.callId);
|
|
24274
24618
|
this.save();
|
|
24619
|
+
}
|
|
24275
24620
|
}
|
|
24276
24621
|
}
|
|
24277
24622
|
|
|
24278
24623
|
class Conversations {
|
|
24279
24624
|
o;
|
|
24280
24625
|
root;
|
|
24626
|
+
history;
|
|
24281
24627
|
cwd;
|
|
24282
24628
|
active = new Map;
|
|
24283
24629
|
summaries = new Map;
|
|
@@ -24285,6 +24631,11 @@ class Conversations {
|
|
|
24285
24631
|
this.o = o;
|
|
24286
24632
|
this.root = join26(o.coordination.directory, "conversations-v1");
|
|
24287
24633
|
this.cwd = realpathSync8(o.cwd);
|
|
24634
|
+
const sessions = join26(this.root, "sessions");
|
|
24635
|
+
this.history = new ConversationHistory(sessions, () => {
|
|
24636
|
+
this.o.owner.lease.assert();
|
|
24637
|
+
return this.files.directory(sessions);
|
|
24638
|
+
}, (id) => this.retainedSummary(id));
|
|
24288
24639
|
}
|
|
24289
24640
|
currentDirectory() {
|
|
24290
24641
|
const current2 = resolveWorkspace(this.o.cwd);
|
|
@@ -24327,16 +24678,65 @@ class Conversations {
|
|
|
24327
24678
|
]);
|
|
24328
24679
|
if (identity.schema !== "cadence-conversation/1" || identity.id !== id || identity.cwd !== this.cwd)
|
|
24329
24680
|
throw new ContractError("corrupt-selected");
|
|
24330
|
-
const
|
|
24681
|
+
const indexed = value !== null && typeof value === "object" && Object.hasOwn(value, "count");
|
|
24682
|
+
const m = closed(value, [
|
|
24683
|
+
"id",
|
|
24684
|
+
"title",
|
|
24685
|
+
"turns",
|
|
24686
|
+
"checkpoint",
|
|
24687
|
+
...indexed ? ["head", "count"] : []
|
|
24688
|
+
]);
|
|
24331
24689
|
const turns = list(m.turns, 1000, decodeId);
|
|
24332
|
-
|
|
24690
|
+
const count2 = indexed ? integer(m.count, Math.max(1, turns.length)) : turns.length;
|
|
24691
|
+
const head = indexed ? decodeId(m.head) : turns.at(-1);
|
|
24692
|
+
if (!head || !count2 || new Set(turns).size !== turns.length || m.id !== id)
|
|
24333
24693
|
throw new ContractError("corrupt-selected");
|
|
24334
|
-
|
|
24694
|
+
const meta = {
|
|
24335
24695
|
id,
|
|
24336
24696
|
title: text(m.title, 512, true),
|
|
24337
24697
|
turns,
|
|
24698
|
+
head,
|
|
24699
|
+
count: count2,
|
|
24338
24700
|
checkpoint: m.checkpoint === null ? null : decodeId(m.checkpoint)
|
|
24339
24701
|
};
|
|
24702
|
+
if (this.turnAt(meta, count2 - 1) !== head || indexed && this.position(meta, head) !== count2 - 1 || meta.checkpoint && !this.contains(meta, meta.checkpoint))
|
|
24703
|
+
throw new ContractError("corrupt-selected");
|
|
24704
|
+
return meta;
|
|
24705
|
+
}
|
|
24706
|
+
slotPath(id, ordinal) {
|
|
24707
|
+
return join26(this.root, "indices", decodeId(id), `${integer(ordinal, 0)}.json`);
|
|
24708
|
+
}
|
|
24709
|
+
turnAt(meta, ordinal) {
|
|
24710
|
+
if (ordinal < 0 || ordinal >= meta.count)
|
|
24711
|
+
throw new ContractError("stale");
|
|
24712
|
+
if (ordinal < meta.turns.length)
|
|
24713
|
+
return meta.turns[ordinal];
|
|
24714
|
+
const row = closed(this.read(this.slotPath(meta.id, ordinal), META_BYTES), [
|
|
24715
|
+
"conversationId",
|
|
24716
|
+
"ordinal",
|
|
24717
|
+
"turnId"
|
|
24718
|
+
]);
|
|
24719
|
+
if (row.conversationId !== meta.id || row.ordinal !== ordinal)
|
|
24720
|
+
throw new ContractError("corrupt-selected");
|
|
24721
|
+
return decodeId(row.turnId);
|
|
24722
|
+
}
|
|
24723
|
+
position(meta, turnId) {
|
|
24724
|
+
const legacy = meta.turns.indexOf(turnId);
|
|
24725
|
+
if (legacy >= 0)
|
|
24726
|
+
return legacy;
|
|
24727
|
+
const value = this.read(this.path(turnId, "position.json"), META_BYTES);
|
|
24728
|
+
if (value === undefined)
|
|
24729
|
+
return -1;
|
|
24730
|
+
const row = closed(value, ["conversationId", "ordinal", "turnId"]);
|
|
24731
|
+
if (row.turnId !== turnId)
|
|
24732
|
+
throw new ContractError("corrupt-selected");
|
|
24733
|
+
const ordinal = integer(row.ordinal, 0);
|
|
24734
|
+
if (row.conversationId !== meta.id || ordinal >= meta.count)
|
|
24735
|
+
return -1;
|
|
24736
|
+
return this.turnAt(meta, ordinal) === turnId ? ordinal : -1;
|
|
24737
|
+
}
|
|
24738
|
+
contains(meta, turnId) {
|
|
24739
|
+
return this.position(meta, turnId) >= 0;
|
|
24340
24740
|
}
|
|
24341
24741
|
turn(id) {
|
|
24342
24742
|
const live2 = this.active.get(id);
|
|
@@ -24367,10 +24767,11 @@ class Conversations {
|
|
|
24367
24767
|
this.write(join26(this.root, "control-links", `${decodeId(id)}.json`), { schema: "cadence-conversation-control/1", conversationId: id }, META_BYTES);
|
|
24368
24768
|
}
|
|
24369
24769
|
hasTurn(id, turnId) {
|
|
24370
|
-
|
|
24770
|
+
const meta = this.metadata(id);
|
|
24771
|
+
return !!meta && this.contains(meta, turnId);
|
|
24371
24772
|
}
|
|
24372
24773
|
summary(meta) {
|
|
24373
|
-
const head = meta.
|
|
24774
|
+
const head = meta.head, turn = this.turn(head);
|
|
24374
24775
|
const accepted2 = closed(this.read(this.path(head, "accepted.json"), META_BYTES), ["command", "at"]);
|
|
24375
24776
|
const command = decodeWorkbenchCommand(accepted2.command);
|
|
24376
24777
|
if (command.kind !== "send" || command.commandId !== head || command.conversationId !== meta.id || command.model.epoch !== turn.epoch)
|
|
@@ -24383,25 +24784,34 @@ class Conversations {
|
|
|
24383
24784
|
updatedAt: integer(accepted2.at, 0)
|
|
24384
24785
|
};
|
|
24385
24786
|
}
|
|
24386
|
-
headKey(head) {
|
|
24387
|
-
return [
|
|
24787
|
+
headKey(head, id, count2) {
|
|
24788
|
+
return [
|
|
24789
|
+
this.path(head, "turn.json"),
|
|
24790
|
+
this.path(head, "accepted.json"),
|
|
24791
|
+
this.path(head, "position.json"),
|
|
24792
|
+
this.slotPath(id, count2 - 1)
|
|
24793
|
+
].map(observedFile).join("|");
|
|
24388
24794
|
}
|
|
24389
24795
|
retainedSummary(id) {
|
|
24390
24796
|
const base = [this.metadataPath(id), this.identityPath(id)].map(observedFile).join("|");
|
|
24391
24797
|
const hit = this.summaries.get(id);
|
|
24392
|
-
if (hit && !this.active.has(hit.summary.head) && hit.key === `${base}|${this.headKey(hit.summary.head)}`)
|
|
24798
|
+
if (hit && !this.active.has(hit.summary.head) && hit.key === `${base}|${this.headKey(hit.summary.head, id, hit.count)}`)
|
|
24393
24799
|
return { ...hit.summary };
|
|
24394
24800
|
const meta = this.metadata(id);
|
|
24395
24801
|
if (!meta)
|
|
24396
24802
|
throw new ContractError("corrupt-selected");
|
|
24397
|
-
const head = meta.
|
|
24398
|
-
const key2 = `${base}|${this.headKey(head)}`;
|
|
24803
|
+
const head = meta.head;
|
|
24804
|
+
const key2 = `${base}|${this.headKey(head, id, meta.count)}`;
|
|
24399
24805
|
const summary2 = this.summary(meta);
|
|
24400
24806
|
this.summaries.delete(id);
|
|
24401
24807
|
if (!this.active.has(head) && !["queued", "running"].includes(summary2.status)) {
|
|
24402
|
-
if (this.summaries.size >=
|
|
24403
|
-
this.summaries.
|
|
24404
|
-
this.summaries.set(id, {
|
|
24808
|
+
if (this.summaries.size >= SUMMARY_CACHE)
|
|
24809
|
+
this.summaries.delete(this.summaries.keys().next().value);
|
|
24810
|
+
this.summaries.set(id, {
|
|
24811
|
+
key: key2,
|
|
24812
|
+
summary: { ...summary2 },
|
|
24813
|
+
count: meta.count
|
|
24814
|
+
});
|
|
24405
24815
|
}
|
|
24406
24816
|
return summary2;
|
|
24407
24817
|
}
|
|
@@ -24414,35 +24824,36 @@ class Conversations {
|
|
|
24414
24824
|
updatedAt: live2.acceptedAt
|
|
24415
24825
|
};
|
|
24416
24826
|
}
|
|
24417
|
-
list() {
|
|
24827
|
+
list(after = null, scan) {
|
|
24418
24828
|
this.o.owner.lease.assert();
|
|
24419
|
-
|
|
24420
|
-
|
|
24421
|
-
|
|
24422
|
-
|
|
24423
|
-
|
|
24424
|
-
|
|
24425
|
-
|
|
24829
|
+
return this.history.read(after, scan);
|
|
24830
|
+
}
|
|
24831
|
+
activeConversations() {
|
|
24832
|
+
this.o.owner.lease.assert();
|
|
24833
|
+
return {
|
|
24834
|
+
kind: "conversation-active",
|
|
24835
|
+
entries: [...this.active.values()].map((live2) => this.liveSummary(live2))
|
|
24836
|
+
};
|
|
24837
|
+
}
|
|
24838
|
+
closeReads() {
|
|
24839
|
+
this.history.close();
|
|
24840
|
+
}
|
|
24841
|
+
assertSettled() {
|
|
24842
|
+
this.o.owner.lease.assert();
|
|
24843
|
+
const path = join26(this.root, "sessions");
|
|
24844
|
+
if (!this.files.directory(path))
|
|
24845
|
+
return;
|
|
24846
|
+
const dir = opendirSync10(path);
|
|
24426
24847
|
try {
|
|
24427
|
-
for (let e =
|
|
24428
|
-
if (!e.isFile() || !e.name.endsWith(".json")
|
|
24429
|
-
throw new ContractError("
|
|
24430
|
-
|
|
24848
|
+
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
24849
|
+
if (!e.isFile() || !e.name.endsWith(".json"))
|
|
24850
|
+
throw new ContractError("corrupt-selected");
|
|
24851
|
+
if (this.retainedSummary(decodeId(e.name.slice(0, -5))).status === "unknown")
|
|
24852
|
+
throw new ContractError("quarantine");
|
|
24431
24853
|
}
|
|
24432
24854
|
} finally {
|
|
24433
|
-
|
|
24434
|
-
}
|
|
24435
|
-
for (const live2 of this.active.values()) {
|
|
24436
|
-
const at = entries2.findIndex((entry) => entry.id === live2.conversationId);
|
|
24437
|
-
if (at < 0)
|
|
24438
|
-
entries2.push(this.liveSummary(live2));
|
|
24439
|
-
else
|
|
24440
|
-
entries2[at] = this.liveSummary(live2);
|
|
24855
|
+
dir.closeSync();
|
|
24441
24856
|
}
|
|
24442
|
-
return {
|
|
24443
|
-
kind: "conversations",
|
|
24444
|
-
entries: entries2.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
24445
|
-
};
|
|
24446
24857
|
}
|
|
24447
24858
|
view(id, before) {
|
|
24448
24859
|
this.o.owner.lease.assert();
|
|
@@ -24465,13 +24876,13 @@ class Conversations {
|
|
|
24465
24876
|
before: null
|
|
24466
24877
|
};
|
|
24467
24878
|
}
|
|
24468
|
-
let end = before === null ? meta.
|
|
24879
|
+
let end = before === null ? meta.count : this.position(meta, before);
|
|
24469
24880
|
if (end < 0)
|
|
24470
24881
|
throw new ContractError("stale");
|
|
24471
24882
|
const turns = [];
|
|
24472
24883
|
let bytes = 0;
|
|
24473
24884
|
while (end > 0 && turns.length < 20) {
|
|
24474
|
-
const turn = this.turn(meta
|
|
24885
|
+
const turn = this.turn(this.turnAt(meta, end - 1)), size2 = Buffer.byteLength(canonicalJson(turn, TURN_BYTES));
|
|
24475
24886
|
if (bytes + size2 > 400 * 1024)
|
|
24476
24887
|
break;
|
|
24477
24888
|
bytes += size2;
|
|
@@ -24485,36 +24896,27 @@ class Conversations {
|
|
|
24485
24896
|
before: end > 0 ? turns[0].id : null
|
|
24486
24897
|
};
|
|
24487
24898
|
}
|
|
24488
|
-
|
|
24489
|
-
|
|
24490
|
-
|
|
24491
|
-
|
|
24492
|
-
|
|
24493
|
-
|
|
24494
|
-
|
|
24495
|
-
|
|
24496
|
-
|
|
24497
|
-
|
|
24498
|
-
|
|
24499
|
-
|
|
24500
|
-
|
|
24501
|
-
|
|
24502
|
-
|
|
24503
|
-
else
|
|
24504
|
-
throw new ContractError("corrupt-selected");
|
|
24505
|
-
if (bytes + (this.active.size + 1) * (CONTEXT_BYTES + TURN_BYTES + 128 * 1024) > 512 * 1024 * 1024)
|
|
24506
|
-
throw new ContractError("capacity");
|
|
24507
|
-
}
|
|
24508
|
-
} finally {
|
|
24509
|
-
dir.closeSync();
|
|
24510
|
-
}
|
|
24899
|
+
transcript(id, turnId, page) {
|
|
24900
|
+
this.o.owner.lease.assert();
|
|
24901
|
+
if (!this.hasTurn(id, turnId))
|
|
24902
|
+
throw new ContractError("invalid");
|
|
24903
|
+
const turn = this.turn(turnId);
|
|
24904
|
+
if (!turn.transcript || page >= turn.transcript.pages)
|
|
24905
|
+
throw new ContractError("stale");
|
|
24906
|
+
const record2 = closed(this.read(this.path(turnId, `transcript/${page}.json`), TRANSCRIPT_PAGE_BYTES), ["turnId", "page", "items"]);
|
|
24907
|
+
if (record2.turnId !== turnId || record2.page !== page)
|
|
24908
|
+
throw new ContractError("corrupt-selected");
|
|
24909
|
+
return {
|
|
24910
|
+
kind: "conversation-transcript",
|
|
24911
|
+
conversationId: id,
|
|
24912
|
+
page,
|
|
24913
|
+
turn: decodeChatTurn({ ...turn, items: record2.items })
|
|
24511
24914
|
};
|
|
24512
|
-
visit(this.root, 0);
|
|
24513
24915
|
}
|
|
24514
24916
|
attempted(conversationId, commandId) {
|
|
24515
24917
|
this.o.owner.lease.assert();
|
|
24516
24918
|
const meta = this.metadata(conversationId);
|
|
24517
|
-
return this.active.has(commandId) || this.files.directory(dirname16(this.path(commandId, "accepted.json"))) || !!meta
|
|
24919
|
+
return this.active.has(commandId) || this.files.directory(dirname16(this.path(commandId, "accepted.json"))) || !!meta && this.contains(meta, commandId) || !meta && !!this.files.read(this.identityPath(conversationId), META_BYTES);
|
|
24518
24920
|
}
|
|
24519
24921
|
command(input, reply) {
|
|
24520
24922
|
return {
|
|
@@ -24554,17 +24956,14 @@ class Conversations {
|
|
|
24554
24956
|
this.files.assert(this.metadataPath(input.conversationId), beforeMeta, META_BYTES);
|
|
24555
24957
|
if (!meta && this.files.read(this.identityPath(input.conversationId), META_BYTES))
|
|
24556
24958
|
throw new ContractError("corrupt-selected");
|
|
24557
|
-
if ((meta?.
|
|
24959
|
+
if ((meta?.head ?? null) !== input.head)
|
|
24558
24960
|
throw new ContractError("stale");
|
|
24559
24961
|
if (meta && this.turn(input.head).status !== "completed" && (!control || ["queued", "running", "unknown"].includes(this.turn(input.head).status)))
|
|
24560
24962
|
throw new ContractError("conflict");
|
|
24561
|
-
if (this.active.size >= 8 ||
|
|
24963
|
+
if (this.active.size >= 8 || meta && !Number.isSafeInteger(meta.count + 1))
|
|
24562
24964
|
throw new ContractError("capacity");
|
|
24563
24965
|
if (!input.text.trim())
|
|
24564
24966
|
throw new ContractError("invalid");
|
|
24565
|
-
if (this.list().entries.some((entry) => entry.status === "unknown"))
|
|
24566
|
-
throw new ContractError("quarantine");
|
|
24567
|
-
this.capacity();
|
|
24568
24967
|
const space = statfsSync2(this.o.coordination.directory);
|
|
24569
24968
|
if (space.bavail * space.bsize < LIMITS.freeSpaceFloorBytes)
|
|
24570
24969
|
throw new ContractError("capacity");
|
|
@@ -24589,6 +24988,8 @@ class Conversations {
|
|
|
24589
24988
|
id: input.conversationId,
|
|
24590
24989
|
title: [...command.text.trim()].slice(0, 100).join("") || "Conversation",
|
|
24591
24990
|
turns: [],
|
|
24991
|
+
head: turn.id,
|
|
24992
|
+
count: 0,
|
|
24592
24993
|
checkpoint: null
|
|
24593
24994
|
};
|
|
24594
24995
|
const previous = next.checkpoint ? this.path(next.checkpoint, "context.json") : null;
|
|
@@ -24619,7 +25020,15 @@ class Conversations {
|
|
|
24619
25020
|
this.write(this.identityPath(next.id), { schema: "cadence-conversation/1", id: next.id, cwd: this.cwd }, META_BYTES);
|
|
24620
25021
|
this.write(this.path(turn.id, "accepted.json"), { command, at: live2.acceptedAt }, META_BYTES);
|
|
24621
25022
|
live2.turnFile = this.write(this.path(turn.id, "turn.json"), turn, TURN_BYTES);
|
|
24622
|
-
|
|
25023
|
+
const position2 = {
|
|
25024
|
+
conversationId: next.id,
|
|
25025
|
+
ordinal: next.count,
|
|
25026
|
+
turnId: turn.id
|
|
25027
|
+
};
|
|
25028
|
+
this.write(this.slotPath(next.id, next.count), position2, META_BYTES);
|
|
25029
|
+
this.write(this.path(turn.id, "position.json"), position2, META_BYTES);
|
|
25030
|
+
next.head = turn.id;
|
|
25031
|
+
next.count++;
|
|
24623
25032
|
live2.metaFile = this.write(this.metadataPath(next.id), next, META_BYTES, beforeMeta);
|
|
24624
25033
|
control?.accept();
|
|
24625
25034
|
} catch (error) {
|
|
@@ -24652,13 +25061,14 @@ class Conversations {
|
|
|
24652
25061
|
return;
|
|
24653
25062
|
}
|
|
24654
25063
|
const meta = this.metadata(id);
|
|
24655
|
-
if (!meta
|
|
25064
|
+
if (!meta || !this.contains(meta, turnId))
|
|
24656
25065
|
throw new ContractError("invalid");
|
|
24657
25066
|
if (this.turn(turnId).epoch !== epoch)
|
|
24658
25067
|
throw new ContractError("stale");
|
|
24659
25068
|
this.active.get(turnId)?.abort.abort();
|
|
24660
25069
|
}
|
|
24661
25070
|
async run(command, turn, previous, previousFile, meta, abort, control) {
|
|
25071
|
+
let transcript;
|
|
24662
25072
|
let barrier;
|
|
24663
25073
|
let authSettled;
|
|
24664
25074
|
let uncertain = false;
|
|
@@ -24713,7 +25123,7 @@ class Conversations {
|
|
|
24713
25123
|
taskId: meta.id,
|
|
24714
25124
|
taskKind: control ? "research" : "code",
|
|
24715
25125
|
runId: turn.id,
|
|
24716
|
-
childRunId:
|
|
25126
|
+
childRunId: randomUUID20(),
|
|
24717
25127
|
assignmentId: "conversation",
|
|
24718
25128
|
purpose: control ? "research" : "edit",
|
|
24719
25129
|
model: selected2,
|
|
@@ -24728,7 +25138,7 @@ class Conversations {
|
|
|
24728
25138
|
});
|
|
24729
25139
|
const auth = this.o.models.prepareTracked({
|
|
24730
25140
|
request: request3,
|
|
24731
|
-
operationId:
|
|
25141
|
+
operationId: randomUUID20(),
|
|
24732
25142
|
epoch: turn.epoch,
|
|
24733
25143
|
timeoutMs: this.o.timeoutMs ?? LIMITS.workerMs,
|
|
24734
25144
|
signal: abort.signal,
|
|
@@ -24746,11 +25156,15 @@ class Conversations {
|
|
|
24746
25156
|
save();
|
|
24747
25157
|
this.write(this.path(turn.id, "launch.json"), { epoch: turn.epoch, childRunId: request3.childRunId }, META_BYTES);
|
|
24748
25158
|
let credentials = [];
|
|
24749
|
-
let checkpoint;
|
|
24750
|
-
|
|
24751
|
-
|
|
24752
|
-
|
|
24753
|
-
|
|
25159
|
+
let checkpoint = false, contextCount = 0;
|
|
25160
|
+
let contextHead = null;
|
|
25161
|
+
transcript = new ConversationTranscript(turn, (page) => {
|
|
25162
|
+
const space = statfsSync2(this.o.coordination.directory);
|
|
25163
|
+
if (space.bavail * space.bsize - TRANSCRIPT_PAGE_BYTES < LIMITS.freeSpaceFloorBytes)
|
|
25164
|
+
throw new ContractError("capacity");
|
|
25165
|
+
this.write(this.path(turn.id, `transcript/${page.page}.json`), page, TRANSCRIPT_PAGE_BYTES);
|
|
25166
|
+
});
|
|
25167
|
+
const capture = new ConversationCapture(turn, save, () => [...this.o.secrets(), ...credentials], transcript);
|
|
24754
25168
|
const running = Promise.resolve().then(() => launchConversation(prepared.prepared, {
|
|
24755
25169
|
previous,
|
|
24756
25170
|
agentDir: this.o.agentDir,
|
|
@@ -24759,8 +25173,21 @@ class Conversations {
|
|
|
24759
25173
|
timeoutMs: this.o.timeoutMs ?? LIMITS.workerMs,
|
|
24760
25174
|
ownerSecrets: this.o.secrets,
|
|
24761
25175
|
onCoordinator: control?.onTool,
|
|
24762
|
-
onContext: (
|
|
24763
|
-
|
|
25176
|
+
onContext: (message) => {
|
|
25177
|
+
const space = statfsSync2(this.o.coordination.directory);
|
|
25178
|
+
if (space.bavail * space.bsize - CONTEXT_RECORD_BYTES < LIMITS.freeSpaceFloorBytes)
|
|
25179
|
+
throw new ContractError("capacity");
|
|
25180
|
+
const file = this.write(this.path(turn.id, `context-${contextCount}.json`), {
|
|
25181
|
+
schema: "cadence-conversation-message/1",
|
|
25182
|
+
index: contextCount,
|
|
25183
|
+
previous: contextHead,
|
|
25184
|
+
message
|
|
25185
|
+
}, CONTEXT_RECORD_BYTES);
|
|
25186
|
+
contextHead = file.identity;
|
|
25187
|
+
contextCount++;
|
|
25188
|
+
},
|
|
25189
|
+
onContextEnd: () => {
|
|
25190
|
+
checkpoint = true;
|
|
24764
25191
|
},
|
|
24765
25192
|
maySignal: () => {
|
|
24766
25193
|
try {
|
|
@@ -24786,7 +25213,11 @@ class Conversations {
|
|
|
24786
25213
|
if (result.outcome === "completed") {
|
|
24787
25214
|
if (!checkpoint)
|
|
24788
25215
|
throw new ContractError("corrupt-selected");
|
|
24789
|
-
this.write(this.path(turn.id, "context.json"), {
|
|
25216
|
+
this.write(this.path(turn.id, "context.json"), {
|
|
25217
|
+
schema: "cadence-conversation-context/2",
|
|
25218
|
+
messages: contextCount,
|
|
25219
|
+
head: contextHead
|
|
25220
|
+
}, CONTEXT_BYTES);
|
|
24790
25221
|
meta.checkpoint = turn.id;
|
|
24791
25222
|
if (!live2?.metaFile)
|
|
24792
25223
|
throw new ContractError("corrupt-selected");
|
|
@@ -24804,6 +25235,7 @@ class Conversations {
|
|
|
24804
25235
|
turn.status = "unknown";
|
|
24805
25236
|
try {
|
|
24806
25237
|
this.write(this.path(turn.id, "native.json"), native, META_BYTES);
|
|
25238
|
+
transcript?.finish();
|
|
24807
25239
|
if (turn.status !== "completed")
|
|
24808
25240
|
turn.notice = [
|
|
24809
25241
|
turn.notice,
|
|
@@ -24829,8 +25261,7 @@ var BYTES = 384 * 1024;
|
|
|
24829
25261
|
var EVIDENCE_BYTES = 16384;
|
|
24830
25262
|
var SCHEMA = "cadence-conversation-workflow/1";
|
|
24831
25263
|
var COORDINATORS = 8;
|
|
24832
|
-
var
|
|
24833
|
-
var RUNS = 200;
|
|
25264
|
+
var RECENT_RUNS = 200;
|
|
24834
25265
|
var SCAN = 256;
|
|
24835
25266
|
var INCOMPLETE = "Cadence control state for this conversation is missing, incomplete or unreadable. No mode, plan or run is inferred and nothing was repaired. Start a new conversation; preserve this state for local diagnosis.";
|
|
24836
25267
|
var UNRECORDED = "The last Cadence message of this conversation was not durably recorded. Its plan is not approvable and nothing was replayed or repaired. Start a new conversation; preserve this state for local diagnosis.";
|
|
@@ -24839,14 +25270,14 @@ var PLAN_REFUSALS = {
|
|
|
24839
25270
|
stale: "The project, model selection or owner changed while publishing. No plan card was published; ask the user before trying again.",
|
|
24840
25271
|
conflict: "The current project state conflicts with this plan (for example uncommitted changes in a declared target or its project context). No plan card was published; explain this to the user.",
|
|
24841
25272
|
forbidden: "The plan contains a protected value or an unsafe path and cannot be shown for approval. No plan card was published.",
|
|
24842
|
-
capacity: "
|
|
25273
|
+
capacity: "Workflow storage is unavailable. No plan card was published.",
|
|
24843
25274
|
invalid: "The plan cannot use this delivery policy here (for example local-merge outside an existing Git branch). No plan card was published.",
|
|
24844
25275
|
oversize: "The plan exceeds its size bounds. No plan card was published.",
|
|
24845
25276
|
unavailable: "Git or the project state could not be observed. No plan card was published."
|
|
24846
25277
|
};
|
|
24847
25278
|
var RESEARCH_REFUSALS = {
|
|
24848
25279
|
stale: "The project, model selection or owner changed. No investigation started.",
|
|
24849
|
-
capacity: "The owner
|
|
25280
|
+
capacity: "The owner has no available workflow slot or storage. No investigation started.",
|
|
24850
25281
|
forbidden: "The request contains a protected value. No investigation started."
|
|
24851
25282
|
};
|
|
24852
25283
|
var refusal3 = (refused2, notice) => ({ refused: refused2, notice });
|
|
@@ -24927,9 +25358,28 @@ class ConversationWorkflow {
|
|
|
24927
25358
|
this.o.owner.lease.assert();
|
|
24928
25359
|
if (!Number.isSafeInteger(state.revision))
|
|
24929
25360
|
throw new ContractError("capacity");
|
|
24930
|
-
const { notice: _projectionOnly, ...persisted } =
|
|
25361
|
+
const { notice: _projectionOnly, ...persisted } = {
|
|
25362
|
+
...state,
|
|
25363
|
+
runs: state.runs.slice(-RECENT_RUNS)
|
|
25364
|
+
};
|
|
24931
25365
|
const body = canonicalJson({ schema: SCHEMA, state: decodeCadenceConversation(persisted) }, BYTES);
|
|
24932
25366
|
try {
|
|
25367
|
+
const prior = file ? decodeCadenceConversation(closed(parseWireJson(file.bytes.toString("utf8"), BYTES), [
|
|
25368
|
+
"schema",
|
|
25369
|
+
"state"
|
|
25370
|
+
]).state).runs : [];
|
|
25371
|
+
for (const [index, runId] of state.runs.entries()) {
|
|
25372
|
+
if (index >= state.runs.length - RECENT_RUNS && prior.includes(runId))
|
|
25373
|
+
continue;
|
|
25374
|
+
const path = this.runLink(runId);
|
|
25375
|
+
const old = this.o.coordination.files.read(path);
|
|
25376
|
+
const value = canonicalJson({ runId, conversationId: state.id });
|
|
25377
|
+
if (old) {
|
|
25378
|
+
if (old.bytes.toString("utf8") !== value)
|
|
25379
|
+
throw new ContractError("conflict");
|
|
25380
|
+
} else
|
|
25381
|
+
this.o.coordination.files.create(path, value);
|
|
25382
|
+
}
|
|
24933
25383
|
if (file)
|
|
24934
25384
|
this.o.coordination.files.replace(this.path(state.id), file, body, BYTES);
|
|
24935
25385
|
else
|
|
@@ -24939,23 +25389,6 @@ class ConversationWorkflow {
|
|
|
24939
25389
|
throw e;
|
|
24940
25390
|
}
|
|
24941
25391
|
}
|
|
24942
|
-
records() {
|
|
24943
|
-
if (!this.o.coordination.files.directory(this.root))
|
|
24944
|
-
return 0;
|
|
24945
|
-
const dir = opendirSync10(this.root);
|
|
24946
|
-
let entries2 = 0, records = 0;
|
|
24947
|
-
try {
|
|
24948
|
-
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
24949
|
-
if (++entries2 > SCAN)
|
|
24950
|
-
return RECORDS;
|
|
24951
|
-
if (e.isDirectory())
|
|
24952
|
-
++records;
|
|
24953
|
-
}
|
|
24954
|
-
} finally {
|
|
24955
|
-
dir.closeSync();
|
|
24956
|
-
}
|
|
24957
|
-
return records;
|
|
24958
|
-
}
|
|
24959
25392
|
state(id) {
|
|
24960
25393
|
const loaded = this.read(id);
|
|
24961
25394
|
if (loaded.transient)
|
|
@@ -24983,15 +25416,38 @@ class ConversationWorkflow {
|
|
|
24983
25416
|
this.cache.set(id, { key: key2, state: loaded.state });
|
|
24984
25417
|
return loaded.state;
|
|
24985
25418
|
}
|
|
25419
|
+
runLink(runId) {
|
|
25420
|
+
return join27(this.o.coordination.directory, "conversations-v1", "workflow-links", `${decodeId(runId)}.json`);
|
|
25421
|
+
}
|
|
24986
25422
|
links(runIds) {
|
|
24987
25423
|
this.o.owner.lease.assert();
|
|
24988
25424
|
const wanted = new Set(runIds), links = [];
|
|
25425
|
+
for (const runId of runIds) {
|
|
25426
|
+
try {
|
|
25427
|
+
const file = this.o.coordination.files.read(this.runLink(runId));
|
|
25428
|
+
if (!file)
|
|
25429
|
+
continue;
|
|
25430
|
+
const row = closed(parseWireJson(file.bytes.toString("utf8")), [
|
|
25431
|
+
"runId",
|
|
25432
|
+
"conversationId"
|
|
25433
|
+
]);
|
|
25434
|
+
if (row.runId !== runId)
|
|
25435
|
+
continue;
|
|
25436
|
+
const conversationId = decodeId(row.conversationId);
|
|
25437
|
+
if (this.observe(conversationId).notice)
|
|
25438
|
+
continue;
|
|
25439
|
+
links.push({ runId, conversationId });
|
|
25440
|
+
wanted.delete(runId);
|
|
25441
|
+
} catch {
|
|
25442
|
+
this.o.owner.lease.assert();
|
|
25443
|
+
}
|
|
25444
|
+
}
|
|
24989
25445
|
if (!wanted.size)
|
|
24990
25446
|
return links;
|
|
24991
25447
|
try {
|
|
24992
25448
|
if (!this.o.coordination.files.directory(this.root))
|
|
24993
25449
|
return links;
|
|
24994
|
-
const dir =
|
|
25450
|
+
const dir = opendirSync11(this.root);
|
|
24995
25451
|
try {
|
|
24996
25452
|
let seen = 0;
|
|
24997
25453
|
for (let e = dir.readSync();e && seen < SCAN && wanted.size; e = dir.readSync()) {
|
|
@@ -25076,8 +25532,6 @@ class ConversationWorkflow {
|
|
|
25076
25532
|
throw new ContractError("conflict");
|
|
25077
25533
|
if (enabled && this.active.size >= COORDINATORS)
|
|
25078
25534
|
throw new ContractError("capacity");
|
|
25079
|
-
if (!file && this.records() >= RECORDS)
|
|
25080
|
-
throw new ContractError("capacity");
|
|
25081
25535
|
const next = {
|
|
25082
25536
|
id: state.id,
|
|
25083
25537
|
revision: state.revision + 1,
|
|
@@ -25162,7 +25616,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25162
25616
|
}
|
|
25163
25617
|
let published;
|
|
25164
25618
|
try {
|
|
25165
|
-
published = this.o.workflows.command(
|
|
25619
|
+
published = this.o.workflows.command(randomUUID21(), epoch, {
|
|
25166
25620
|
action: "propose",
|
|
25167
25621
|
plan,
|
|
25168
25622
|
model
|
|
@@ -25203,12 +25657,10 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25203
25657
|
} catch {
|
|
25204
25658
|
return refusal3("invalid", "An investigation needs a goal and one or two non-empty questions. Nothing was started.");
|
|
25205
25659
|
}
|
|
25206
|
-
if (current2.state.runs.length >= RUNS)
|
|
25207
|
-
return refusal3("capacity", "This conversation reached its retained workflow limit. Nothing was started; continue in a new conversation.");
|
|
25208
25660
|
const busy = this.o.workflows.activeRuns().runs.filter((r) => r.phase !== "research").map((r) => r.id);
|
|
25209
25661
|
if (busy.length)
|
|
25210
25662
|
return refusal3("checkout-busy", `An approved workflow (${busy.join(", ")}) is using this checkout. New investigations start only after it settles; meanwhile discuss recorded evidence or use cadence_status. Nothing was started.`);
|
|
25211
|
-
const id =
|
|
25663
|
+
const id = randomUUID21(), conversationId = current2.state.id;
|
|
25212
25664
|
try {
|
|
25213
25665
|
this.o.workflows.command(id, epoch, {
|
|
25214
25666
|
action: "research",
|
|
@@ -25232,7 +25684,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25232
25684
|
}, current2.file);
|
|
25233
25685
|
const cancel = () => {
|
|
25234
25686
|
try {
|
|
25235
|
-
this.o.workflows.command(
|
|
25687
|
+
this.o.workflows.command(randomUUID21(), epoch, {
|
|
25236
25688
|
action: "cancel",
|
|
25237
25689
|
runId: id
|
|
25238
25690
|
});
|
|
@@ -25310,7 +25762,7 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25310
25762
|
});
|
|
25311
25763
|
if (prepared.kind !== "workflow" || prepared.response.type !== "grant")
|
|
25312
25764
|
throw new ContractError("unavailable");
|
|
25313
|
-
const g = prepared.response, grant2 =
|
|
25765
|
+
const g = prepared.response, grant2 = randomUUID21();
|
|
25314
25766
|
this.grants.set(grant2, {
|
|
25315
25767
|
caller: canonicalJson(caller),
|
|
25316
25768
|
revision: c.revision,
|
|
@@ -25329,8 +25781,6 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25329
25781
|
proposal: g.proposal
|
|
25330
25782
|
};
|
|
25331
25783
|
}
|
|
25332
|
-
if (state.runs.length >= RUNS)
|
|
25333
|
-
throw new ContractError("capacity");
|
|
25334
25784
|
const grant = this.grants.get(c.grant);
|
|
25335
25785
|
if (grant && grant.expiresAt < Date.now())
|
|
25336
25786
|
this.grants.delete(c.grant);
|
|
@@ -25377,10 +25827,10 @@ BROWSER COORDINATOR: You have only cadence_research, cadence_plan and cadence_st
|
|
|
25377
25827
|
}
|
|
25378
25828
|
|
|
25379
25829
|
// src/service/workflows.ts
|
|
25380
|
-
import { randomUUID as
|
|
25830
|
+
import { randomUUID as randomUUID23 } from "node:crypto";
|
|
25381
25831
|
import {
|
|
25382
|
-
lstatSync as
|
|
25383
|
-
opendirSync as
|
|
25832
|
+
lstatSync as lstatSync20,
|
|
25833
|
+
opendirSync as opendirSync13,
|
|
25384
25834
|
realpathSync as realpathSync10,
|
|
25385
25835
|
rmdirSync,
|
|
25386
25836
|
statfsSync as statfsSync3,
|
|
@@ -25392,7 +25842,7 @@ import { fileURLToPath as fileURLToPath5 } from "node:url";
|
|
|
25392
25842
|
// src/task-diff.ts
|
|
25393
25843
|
import { spawnSync as spawnSync3 } from "node:child_process";
|
|
25394
25844
|
import { createHash as createHash24 } from "node:crypto";
|
|
25395
|
-
import { lstatSync as
|
|
25845
|
+
import { lstatSync as lstatSync17, readdirSync as readdirSync3, readlinkSync } from "node:fs";
|
|
25396
25846
|
import { join as join28, relative as relative8 } from "node:path";
|
|
25397
25847
|
var admin = (path) => path === ".cadence/assistant/tasks" || path.startsWith(".cadence/assistant/tasks/");
|
|
25398
25848
|
var GIT_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
@@ -25437,7 +25887,7 @@ function captureChanges(root, outputBytes = GIT_OUTPUT_BYTES) {
|
|
|
25437
25887
|
}
|
|
25438
25888
|
try {
|
|
25439
25889
|
const path = join28(root, name);
|
|
25440
|
-
const st =
|
|
25890
|
+
const st = lstatSync17(path);
|
|
25441
25891
|
add(name, `${st.mode}:${st.size}`);
|
|
25442
25892
|
if (st.isSymbolicLink()) {
|
|
25443
25893
|
add(name, readlinkSync(path));
|
|
@@ -25586,7 +26036,7 @@ ${data2.toString("utf8")}`);
|
|
|
25586
26036
|
break;
|
|
25587
26037
|
}
|
|
25588
26038
|
try {
|
|
25589
|
-
const st =
|
|
26039
|
+
const st = lstatSync17(path);
|
|
25590
26040
|
if (st.isDirectory())
|
|
25591
26041
|
walk(path, depth + 1);
|
|
25592
26042
|
else
|
|
@@ -25618,7 +26068,7 @@ import {
|
|
|
25618
26068
|
closeSync as closeSync9,
|
|
25619
26069
|
constants as constants7,
|
|
25620
26070
|
fstatSync as fstatSync6,
|
|
25621
|
-
lstatSync as
|
|
26071
|
+
lstatSync as lstatSync18,
|
|
25622
26072
|
openSync as openSync9,
|
|
25623
26073
|
readdirSync as readdirSync4,
|
|
25624
26074
|
readlinkSync as readlinkSync2,
|
|
@@ -25724,7 +26174,7 @@ function safeWorkflowFile(cwd, name) {
|
|
|
25724
26174
|
throw new ContractError("forbidden");
|
|
25725
26175
|
safeDirectory(dirname18(p));
|
|
25726
26176
|
try {
|
|
25727
|
-
const st =
|
|
26177
|
+
const st = lstatSync18(p);
|
|
25728
26178
|
if (!st.isFile() || st.isSymbolicLink() || st.nlink !== 1)
|
|
25729
26179
|
throw new ContractError("forbidden");
|
|
25730
26180
|
} catch (e) {
|
|
@@ -25740,7 +26190,7 @@ function declaredTarget(cwd, name) {
|
|
|
25740
26190
|
throw new ContractError("forbidden");
|
|
25741
26191
|
safeDirectory(dirname18(p));
|
|
25742
26192
|
try {
|
|
25743
|
-
const st =
|
|
26193
|
+
const st = lstatSync18(p);
|
|
25744
26194
|
if (st.isSymbolicLink() || !st.isFile() && !st.isDirectory() || st.isFile() && st.nlink !== 1)
|
|
25745
26195
|
throw new ContractError("forbidden");
|
|
25746
26196
|
} catch (e) {
|
|
@@ -25821,7 +26271,7 @@ function directoryIdentity(root, name) {
|
|
|
25821
26271
|
if (++count2 > DIRECTORY_ENTRIES)
|
|
25822
26272
|
throw tooLarge(name);
|
|
25823
26273
|
const full = join29(directory, entry), relative10 = `${prefix}${entry}`;
|
|
25824
|
-
const st =
|
|
26274
|
+
const st = lstatSync18(full);
|
|
25825
26275
|
if (st.isDirectory()) {
|
|
25826
26276
|
rows.push(`${relative10}/`);
|
|
25827
26277
|
visit(full, `${relative10}/`);
|
|
@@ -25861,7 +26311,7 @@ function pathIdentity(cwd, name, walk = true) {
|
|
|
25861
26311
|
throw new ContractError("forbidden");
|
|
25862
26312
|
let st;
|
|
25863
26313
|
try {
|
|
25864
|
-
st =
|
|
26314
|
+
st = lstatSync18(p);
|
|
25865
26315
|
} catch (e) {
|
|
25866
26316
|
if (absent2(e))
|
|
25867
26317
|
return null;
|
|
@@ -26083,13 +26533,13 @@ async function assertWorktree(root, cwd) {
|
|
|
26083
26533
|
if (await common(root) !== await common(cwd))
|
|
26084
26534
|
throw new ContractError("stale");
|
|
26085
26535
|
const marker2 = resolve9(cwd, ".git");
|
|
26086
|
-
if (!
|
|
26536
|
+
if (!lstatSync18(marker2).isFile() || !(readBounded(marker2, 8192)?.toString("utf8") ?? "").startsWith("gitdir: "))
|
|
26087
26537
|
throw new ContractError("stale");
|
|
26088
26538
|
}
|
|
26089
26539
|
|
|
26090
26540
|
// src/service/workflow-history.ts
|
|
26091
|
-
import { randomUUID as
|
|
26092
|
-
import { lstatSync as
|
|
26541
|
+
import { randomUUID as randomUUID22 } from "node:crypto";
|
|
26542
|
+
import { lstatSync as lstatSync19, opendirSync as opendirSync12 } from "node:fs";
|
|
26093
26543
|
class WorkflowHistoryScanner {
|
|
26094
26544
|
path;
|
|
26095
26545
|
directory;
|
|
@@ -26100,7 +26550,7 @@ class WorkflowHistoryScanner {
|
|
|
26100
26550
|
this.directory = directory;
|
|
26101
26551
|
}
|
|
26102
26552
|
incarnation() {
|
|
26103
|
-
const st =
|
|
26553
|
+
const st = lstatSync19(this.path, { bigint: true });
|
|
26104
26554
|
if (!st.isDirectory() || st.isSymbolicLink())
|
|
26105
26555
|
throw new ContractError("corrupt-selected");
|
|
26106
26556
|
return `${st.dev}:${st.ino}`;
|
|
@@ -26126,7 +26576,7 @@ class WorkflowHistoryScanner {
|
|
|
26126
26576
|
if (this.scans.size >= WORKFLOW_LIMITS.historyScans)
|
|
26127
26577
|
throw new ContractError("capacity");
|
|
26128
26578
|
const incarnation3 = this.incarnation();
|
|
26129
|
-
state = { dir:
|
|
26579
|
+
state = { dir: opendirSync12(this.path), incarnation: incarnation3, after, ids: [] };
|
|
26130
26580
|
}
|
|
26131
26581
|
let suspended = false;
|
|
26132
26582
|
try {
|
|
@@ -26152,7 +26602,7 @@ class WorkflowHistoryScanner {
|
|
|
26152
26602
|
if (state.ids.length > WORKFLOW_LIMITS.historyPage + 1)
|
|
26153
26603
|
state.ids.pop();
|
|
26154
26604
|
}
|
|
26155
|
-
const scan =
|
|
26605
|
+
const scan = randomUUID22();
|
|
26156
26606
|
state.timer = setTimeout(() => {
|
|
26157
26607
|
this.scans.delete(scan);
|
|
26158
26608
|
try {
|
|
@@ -26189,8 +26639,8 @@ class WorkflowHistoryScanner {
|
|
|
26189
26639
|
|
|
26190
26640
|
// src/service/workflows.ts
|
|
26191
26641
|
var OWNER_RUNTIME = Object.freeze({
|
|
26192
|
-
version: "2.0.
|
|
26193
|
-
build: "
|
|
26642
|
+
version: "2.0.28",
|
|
26643
|
+
build: "87bcf7aa64bfe005d9861522dc15aa94185c48c56a4e41b1a1e18645b2d075d5",
|
|
26194
26644
|
entry: fileURLToPath5(import.meta.url),
|
|
26195
26645
|
pid: process.pid,
|
|
26196
26646
|
node: process.versions.node,
|
|
@@ -26411,7 +26861,7 @@ class Workflows {
|
|
|
26411
26861
|
if (!this.files.directory(runs))
|
|
26412
26862
|
return;
|
|
26413
26863
|
const ids = [];
|
|
26414
|
-
const dir =
|
|
26864
|
+
const dir = opendirSync13(runs);
|
|
26415
26865
|
try {
|
|
26416
26866
|
for (let e = dir.readSync();e; e = dir.readSync()) {
|
|
26417
26867
|
if (!e.isDirectory())
|
|
@@ -26725,7 +27175,7 @@ class Workflows {
|
|
|
26725
27175
|
this.grants.delete(key2);
|
|
26726
27176
|
if (this.grants.size >= 16)
|
|
26727
27177
|
throw new ContractError("capacity");
|
|
26728
|
-
const grant =
|
|
27178
|
+
const grant = randomUUID23(), expiresAt = Date.now() + 120000;
|
|
26729
27179
|
effect();
|
|
26730
27180
|
this.grants.set(grant, {
|
|
26731
27181
|
commandId,
|
|
@@ -26867,7 +27317,7 @@ class Workflows {
|
|
|
26867
27317
|
if (live2.run.steps.length >= MAX_STEPS)
|
|
26868
27318
|
throw new ContractError("capacity");
|
|
26869
27319
|
const s = {
|
|
26870
|
-
id: `${clipUnits(id, STEP_UNITS)}-${
|
|
27320
|
+
id: `${clipUnits(id, STEP_UNITS)}-${randomUUID23()}`,
|
|
26871
27321
|
...childRunId ? { childRunId } : {},
|
|
26872
27322
|
kind,
|
|
26873
27323
|
...childRunId ? {
|
|
@@ -26936,7 +27386,7 @@ class Workflows {
|
|
|
26936
27386
|
throw new ContractError("unavailable");
|
|
26937
27387
|
if (cwd !== this.cwd)
|
|
26938
27388
|
await assertWorktree(this.cwd, cwd);
|
|
26939
|
-
const childRunId =
|
|
27389
|
+
const childRunId = randomUUID23(), op = randomUUID23();
|
|
26940
27390
|
const plan = live2.proposal?.plan;
|
|
26941
27391
|
const request3 = decodeWorkerPublicRequest({
|
|
26942
27392
|
protocol: 2,
|
|
@@ -27094,7 +27544,7 @@ Read applicable AGENTS.md, existing project conventions and relevant tests befor
|
|
|
27094
27544
|
const shell = "check" in operation;
|
|
27095
27545
|
if (cwd !== this.cwd && shell)
|
|
27096
27546
|
await assertWorktree(this.cwd, cwd);
|
|
27097
|
-
const id =
|
|
27547
|
+
const id = randomUUID23();
|
|
27098
27548
|
this.write(this.path(live2.run.id, `operations/${id}-intent.json`), {
|
|
27099
27549
|
cwd,
|
|
27100
27550
|
args: shell ? operation.check : operation.git
|
|
@@ -27250,7 +27700,7 @@ Read applicable AGENTS.md, existing project conventions and relevant tests befor
|
|
|
27250
27700
|
async review(live2, cwd, p) {
|
|
27251
27701
|
live2.run.phase = "reviewing";
|
|
27252
27702
|
this.save(live2);
|
|
27253
|
-
const { result, step } = await this.worker(live2, cwd, `review-${
|
|
27703
|
+
const { result, step } = await this.worker(live2, cwd, `review-${randomUUID23()}`, `Independently inspect the implemented files against the approved plan and acceptance. Look for missing behavior, regressions, weakened tests and scope violations. Do not edit or run commands. Reply with exactly JSON {"approved":true|false,"summary":"...","blockingIssues":["..."]}. Automatic review is not human review.
|
|
27254
27704
|
Files: ${p.plan.tasks.flatMap((t) => t.writes).join(", ")}`, true, p.model);
|
|
27255
27705
|
this.success(result);
|
|
27256
27706
|
const verdict = reviewVerdict(result.text);
|
|
@@ -27358,7 +27808,7 @@ ${changed.map((p) => `- ${p}`).join(`
|
|
|
27358
27808
|
for (const directory of [dirname19(this.worktreeRoot), this.worktreeRoot]) {
|
|
27359
27809
|
if (!safeDirectory(directory, true))
|
|
27360
27810
|
throw new ContractError("forbidden");
|
|
27361
|
-
const st =
|
|
27811
|
+
const st = lstatSync20(directory);
|
|
27362
27812
|
if (process.platform !== "win32" && ((st.mode & 63) !== 0 || process.getuid && st.uid !== process.getuid()))
|
|
27363
27813
|
throw new ContractError("forbidden");
|
|
27364
27814
|
}
|
|
@@ -27661,13 +28111,16 @@ class Workbench {
|
|
|
27661
28111
|
conversations: this.conversations,
|
|
27662
28112
|
workflows: this.workflows
|
|
27663
28113
|
});
|
|
27664
|
-
|
|
27665
|
-
throw new ContractError("quarantine");
|
|
28114
|
+
this.conversations.assertSettled();
|
|
27666
28115
|
this.files = new WorkbenchFiles(o.cwd, [o.coordination.files.namespace, o.agentDir], o.secrets);
|
|
27667
28116
|
this.projectRoot = join31(o.coordination.files.namespace, "workbench-projects-v1");
|
|
27668
28117
|
}
|
|
27669
28118
|
closeReads() {
|
|
27670
|
-
|
|
28119
|
+
try {
|
|
28120
|
+
this.workflows.closeHistory();
|
|
28121
|
+
} finally {
|
|
28122
|
+
this.conversations.closeReads();
|
|
28123
|
+
}
|
|
27671
28124
|
}
|
|
27672
28125
|
read(value) {
|
|
27673
28126
|
const request3 = decodeWorkbenchRead(value);
|
|
@@ -27692,8 +28145,11 @@ class Workbench {
|
|
|
27692
28145
|
return this.workflows.view(request3.id);
|
|
27693
28146
|
case "task-origin":
|
|
27694
28147
|
return this.conversations.origin(request3.taskId);
|
|
28148
|
+
case "conversation-active":
|
|
27695
28149
|
case "conversations": {
|
|
27696
|
-
const value2 = this.conversations.list();
|
|
28150
|
+
const value2 = request3.kind === "conversation-active" ? this.conversations.activeConversations() : this.conversations.list(request3.after ?? null, request3.scan);
|
|
28151
|
+
if (value2.kind !== "conversations" && value2.kind !== "conversation-active")
|
|
28152
|
+
return value2;
|
|
27697
28153
|
return {
|
|
27698
28154
|
...value2,
|
|
27699
28155
|
entries: value2.entries.map((entry) => ({
|
|
@@ -27702,6 +28158,8 @@ class Workbench {
|
|
|
27702
28158
|
}))
|
|
27703
28159
|
};
|
|
27704
28160
|
}
|
|
28161
|
+
case "conversation-transcript":
|
|
28162
|
+
return this.conversations.transcript(request3.id, request3.turnId, request3.page);
|
|
27705
28163
|
case "conversation":
|
|
27706
28164
|
return this.conversations.view(request3.id, request3.before);
|
|
27707
28165
|
case "files":
|
|
@@ -27754,7 +28212,7 @@ class Workbench {
|
|
|
27754
28212
|
const entries2 = [{ path: this.o.cwd, current: true }], files = this.o.coordination.files;
|
|
27755
28213
|
if (!files.directory(this.projectRoot))
|
|
27756
28214
|
return { kind: "projects", entries: entries2 };
|
|
27757
|
-
const directory =
|
|
28215
|
+
const directory = opendirSync14(this.projectRoot);
|
|
27758
28216
|
try {
|
|
27759
28217
|
for (let e = directory.readSync();e; e = directory.readSync()) {
|
|
27760
28218
|
if (!e.isFile() || !/^[a-f0-9]{64}\.json$/.test(e.name) || entries2.length > 128)
|
|
@@ -27905,7 +28363,7 @@ function legacy(store, budget, reader) {
|
|
|
27905
28363
|
throw new ContractError("capacity");
|
|
27906
28364
|
}
|
|
27907
28365
|
}
|
|
27908
|
-
const dir =
|
|
28366
|
+
const dir = opendirSync15(store.tasks), records = [];
|
|
27909
28367
|
let entries2 = 0;
|
|
27910
28368
|
try {
|
|
27911
28369
|
for (let entry = dir.readSync();entry; entry = dir.readSync()) {
|
|
@@ -27915,7 +28373,7 @@ function legacy(store, budget, reader) {
|
|
|
27915
28373
|
if (created.has(entry.name))
|
|
27916
28374
|
continue;
|
|
27917
28375
|
const path = join32(store.tasks, entry.name, "task.json");
|
|
27918
|
-
const size2 =
|
|
28376
|
+
const size2 = lstatSync21(path).size;
|
|
27919
28377
|
if (size2 > 128 * 1024)
|
|
27920
28378
|
throw new ContractError("corrupt-selected");
|
|
27921
28379
|
budget.reserve(size2);
|
|
@@ -27968,7 +28426,7 @@ var processLifetime = {
|
|
|
27968
28426
|
}
|
|
27969
28427
|
};
|
|
27970
28428
|
async function createServiceHost(value, sourceBuild, lifetime) {
|
|
27971
|
-
const embedded = "
|
|
28429
|
+
const embedded = "45f3666b0fc0d853e95e618b50156819f6282ce648e9703b876d9658ae15b78f";
|
|
27972
28430
|
if (embedded && sourceBuild)
|
|
27973
28431
|
throw new ContractError("unsupported-context");
|
|
27974
28432
|
const contractHash = decodeHash(embedded ?? sourceBuild?.contractHash);
|
|
@@ -28126,7 +28584,8 @@ async function createServiceHost(value, sourceBuild, lifetime) {
|
|
|
28126
28584
|
owner,
|
|
28127
28585
|
models,
|
|
28128
28586
|
access,
|
|
28129
|
-
remember: (value2) => preferences.save(value2)
|
|
28587
|
+
remember: (value2) => preferences.save(value2),
|
|
28588
|
+
recall: () => preferences.read()
|
|
28130
28589
|
});
|
|
28131
28590
|
const { PI_OFFLINE: _offline, ...ordinary } = launch;
|
|
28132
28591
|
const checkEnvironment = Object.freeze(ordinary);
|