@abelxiaoxing/cadence 2.2.4 → 2.2.6
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/package.json +1 -1
- package/src/coordinator-tools.mjs +52 -6
- package/src/service-host.mjs +1257 -1144
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +12 -26
- package/web/dist/index.html +6 -6
- package/web/dist/manifest.json +7 -7
package/src/service-host.mjs
CHANGED
|
@@ -34803,1225 +34803,1308 @@ function questionKey(question, roots2) {
|
|
|
34803
34803
|
|
|
34804
34804
|
// src/service/workflow-controller.ts
|
|
34805
34805
|
import { createHash as createHash34 } from "node:crypto";
|
|
34806
|
-
var RECOVERY_BYTES = 32768;
|
|
34807
34806
|
|
|
34808
|
-
|
|
34809
|
-
|
|
34810
|
-
|
|
34811
|
-
|
|
34812
|
-
|
|
34813
|
-
|
|
34814
|
-
|
|
34807
|
+
// src/service/turn-capture.ts
|
|
34808
|
+
var TURN_BYTES = 384 * 1024;
|
|
34809
|
+
var OMITTED_FLAG2 = {
|
|
34810
|
+
"hidden-thinking": "thinking",
|
|
34811
|
+
binary: "media",
|
|
34812
|
+
unsupported: "other"
|
|
34813
|
+
};
|
|
34814
|
+
|
|
34815
|
+
class TurnCapture {
|
|
34816
|
+
turn;
|
|
34817
|
+
save;
|
|
34818
|
+
secrets;
|
|
34819
|
+
details = new Map;
|
|
34820
|
+
messageText = new Map;
|
|
34821
|
+
constructor(turn, save, secrets) {
|
|
34822
|
+
this.turn = turn;
|
|
34823
|
+
this.save = save;
|
|
34824
|
+
this.secrets = secrets;
|
|
34825
|
+
}
|
|
34826
|
+
item(id, role3) {
|
|
34827
|
+
let item = this.turn.items.find((i) => i.id === id);
|
|
34828
|
+
if (!item) {
|
|
34829
|
+
if (this.turn.items.length >= 256)
|
|
34830
|
+
throw new ContractError("capacity");
|
|
34831
|
+
item = { id, role: role3, text: "", name: "", args: "", error: false };
|
|
34832
|
+
this.turn.items.push(item);
|
|
34833
|
+
try {
|
|
34834
|
+
canonicalJson(this.turn, TURN_BYTES - 2048);
|
|
34835
|
+
} catch (error) {
|
|
34836
|
+
this.turn.items.pop();
|
|
34837
|
+
this.turn.notice = "Conversation capture limit exceeded.";
|
|
34838
|
+
throw error;
|
|
34839
|
+
}
|
|
34840
|
+
}
|
|
34841
|
+
return item;
|
|
34842
|
+
}
|
|
34843
|
+
update(item, field, value, maximum, bytes = Buffer.byteLength(value)) {
|
|
34844
|
+
const before = item[field];
|
|
34845
|
+
try {
|
|
34846
|
+
if (bytes > maximum)
|
|
34847
|
+
throw new ContractError("capacity");
|
|
34848
|
+
item[field] = value;
|
|
34849
|
+
canonicalJson(this.turn, TURN_BYTES - 2048);
|
|
34850
|
+
} catch (error) {
|
|
34851
|
+
item[field] = before;
|
|
34852
|
+
this.turn.notice = "Conversation capture limit exceeded.";
|
|
34853
|
+
throw error;
|
|
34854
|
+
}
|
|
34855
|
+
}
|
|
34856
|
+
accept(frame) {
|
|
34857
|
+
const e = frame.event;
|
|
34858
|
+
if (e.kind === "detail-begin") {
|
|
34859
|
+
if (e.descriptor.owner.kind === "context" || e.descriptor.owner.kind === "handoff")
|
|
34860
|
+
return;
|
|
34861
|
+
if (this.details.size >= 64)
|
|
34862
|
+
throw new ContractError("capacity");
|
|
34863
|
+
this.details.set(e.detailId, {
|
|
34864
|
+
descriptor: e.descriptor,
|
|
34865
|
+
sanitizer: new DetailSanitizer(e.descriptor.encoding, this.secrets()),
|
|
34866
|
+
text: "",
|
|
34867
|
+
bytes: 0
|
|
34868
|
+
});
|
|
34869
|
+
} else if (e.kind === "detail-chunk" || e.kind === "detail-end") {
|
|
34870
|
+
const d = this.details.get(e.detailId);
|
|
34871
|
+
if (!d)
|
|
34872
|
+
return;
|
|
34873
|
+
const added = e.kind === "detail-chunk" ? d.sanitizer.write(e.text) : d.sanitizer.end();
|
|
34874
|
+
d.text += added;
|
|
34875
|
+
d.bytes += Buffer.byteLength(added);
|
|
34876
|
+
if (d.bytes > 256 * 1024)
|
|
34877
|
+
throw new ContractError("capacity");
|
|
34878
|
+
const o = d.descriptor.owner;
|
|
34879
|
+
if (o.kind === "message") {
|
|
34880
|
+
const role3 = this.turn.items.find((i) => i.id === o.messageId)?.role;
|
|
34881
|
+
if (role3 === "assistant" && o.stage === "stream") {
|
|
34882
|
+
this.messageText.set(o.blockId, {
|
|
34883
|
+
messageId: o.messageId,
|
|
34884
|
+
text: d.text,
|
|
34885
|
+
bytes: d.bytes
|
|
34886
|
+
});
|
|
34887
|
+
const blocks = [...this.messageText.values()].filter((block) => block.messageId === o.messageId);
|
|
34888
|
+
this.update(this.item(o.messageId, "assistant"), "text", blocks.map((block) => block.text).join(""), 256 * 1024, blocks.reduce((size3, block) => size3 + block.bytes, 0));
|
|
34889
|
+
}
|
|
34890
|
+
} else if (o.kind === "message-final") {
|
|
34891
|
+
const item = this.turn.items.find((i) => i.id === o.messageId);
|
|
34892
|
+
if (item?.role === "assistant")
|
|
34893
|
+
this.update(item, "text", d.text, 256 * 1024, d.bytes);
|
|
34894
|
+
} else if (o.kind === "tool") {
|
|
34895
|
+
const item = this.item(o.callId, "tool");
|
|
34896
|
+
if (o.stage === "execution-arguments" || o.stage === "arguments-final")
|
|
34897
|
+
this.update(item, "args", d.text, 64 * 1024, d.bytes);
|
|
34898
|
+
if (o.stage === "result" || o.stage === "update-snapshot")
|
|
34899
|
+
this.update(item, "text", d.text, 256 * 1024, d.bytes);
|
|
34900
|
+
}
|
|
34901
|
+
if (e.kind === "detail-end")
|
|
34902
|
+
this.details.delete(e.detailId);
|
|
34903
|
+
} else if (e.kind === "message-start" && e.role === "assistant") {
|
|
34904
|
+
this.item(e.messageId, "assistant");
|
|
34905
|
+
} else if (e.kind === "tool-start" || e.kind === "tool-end") {
|
|
34906
|
+
const item = this.item(e.callId, "tool");
|
|
34907
|
+
this.update(item, "name", sanitizeText(e.name, this.secrets()), 128);
|
|
34908
|
+
if (e.kind === "tool-start")
|
|
34909
|
+
item.state = "running";
|
|
34910
|
+
else {
|
|
34911
|
+
item.state = e.isError ? "failed" : "completed";
|
|
34912
|
+
item.error = e.isError;
|
|
34913
|
+
}
|
|
34914
|
+
} else if (e.kind === "message-end") {
|
|
34915
|
+
const item = this.turn.items.find((i) => i.id === e.messageId && i.role === "assistant");
|
|
34916
|
+
if (item)
|
|
34917
|
+
item.error = ["error", "aborted", "length"].includes(e.stopReason);
|
|
34918
|
+
} else if (e.kind === "model-retry") {
|
|
34919
|
+
if (e.progress)
|
|
34920
|
+
this.turn.retry = e.progress;
|
|
34921
|
+
else
|
|
34922
|
+
delete this.turn.retry;
|
|
34923
|
+
} else if (e.kind === "worker-failure") {
|
|
34924
|
+
delete this.turn.retry;
|
|
34925
|
+
this.turn.notice = `worker-failure: ${e.code}`;
|
|
34926
|
+
} else if (e.kind === "omitted-block" && e.block.owner.kind !== "context" && e.block.owner.kind !== "handoff")
|
|
34927
|
+
this.turn.omitted[OMITTED_FLAG2[e.block.reason]] = true;
|
|
34928
|
+
if ([...this.details.values()].reduce((size3, detail) => size3 + detail.bytes + detail.sanitizer.retainedBytes, 0) > TURN_BYTES)
|
|
34929
|
+
throw new ContractError("capacity");
|
|
34930
|
+
canonicalJson(this.turn, TURN_BYTES);
|
|
34931
|
+
if (e.kind === "message-end") {
|
|
34932
|
+
for (const [id, block] of this.messageText)
|
|
34933
|
+
if (block.messageId === e.messageId)
|
|
34934
|
+
this.messageText.delete(id);
|
|
34935
|
+
}
|
|
34936
|
+
if ([
|
|
34937
|
+
"message-end",
|
|
34938
|
+
"tool-start",
|
|
34939
|
+
"tool-update",
|
|
34940
|
+
"tool-end",
|
|
34941
|
+
"model-retry",
|
|
34942
|
+
"worker-failure"
|
|
34943
|
+
].includes(e.kind))
|
|
34944
|
+
this.save();
|
|
34815
34945
|
}
|
|
34816
34946
|
}
|
|
34817
34947
|
|
|
34818
|
-
|
|
34948
|
+
// src/service/workflow-report.ts
|
|
34949
|
+
var RECORD_BYTES2 = 384 * 1024;
|
|
34950
|
+
var TRANSCRIPT_LIMIT = "Transcript bound reached: later worker activity was not retained here. The worker continued; its outcome and final result are recorded separately.";
|
|
34951
|
+
var issueText = (issue2) => typeof issue2 === "string" ? issue2 : `${issue2.id}: ${issue2.evidence}
|
|
34952
|
+
Verify: ${issue2.verify}`;
|
|
34953
|
+
function isBlocker(value) {
|
|
34954
|
+
if (typeof value === "string")
|
|
34955
|
+
return true;
|
|
34956
|
+
if (!value || typeof value !== "object")
|
|
34957
|
+
return false;
|
|
34958
|
+
const issue2 = value;
|
|
34959
|
+
return Object.keys(issue2).length === 4 && typeof issue2.id === "string" && /^[a-z][a-z0-9-]{0,47}$/.test(issue2.id) && typeof issue2.evidence === "string" && !!issue2.evidence.trim() && typeof issue2.verify === "string" && !!issue2.verify.trim() && Array.isArray(issue2.writes) && issue2.writes.every((p) => typeof p === "string");
|
|
34819
34960
|
}
|
|
34820
|
-
|
|
34821
|
-
|
|
34822
|
-
|
|
34823
|
-
|
|
34824
|
-
|
|
34825
|
-
|
|
34826
|
-
|
|
34827
|
-
|
|
34828
|
-
|
|
34829
|
-
|
|
34830
|
-
|
|
34831
|
-
|
|
34961
|
+
function reviewVerdict(reply) {
|
|
34962
|
+
let malformed = false;
|
|
34963
|
+
const decode4 = (raw) => {
|
|
34964
|
+
let v;
|
|
34965
|
+
try {
|
|
34966
|
+
v = parseWireJson(raw, RECORD_BYTES2);
|
|
34967
|
+
} catch {
|
|
34968
|
+
try {
|
|
34969
|
+
const value = JSON.parse(raw);
|
|
34970
|
+
if (value && typeof value === "object" && Object.hasOwn(value, "approved"))
|
|
34971
|
+
malformed = true;
|
|
34972
|
+
} catch {}
|
|
34973
|
+
return null;
|
|
34974
|
+
}
|
|
34975
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
34976
|
+
return null;
|
|
34977
|
+
const o = v;
|
|
34978
|
+
if (typeof o.approved !== "boolean")
|
|
34979
|
+
return null;
|
|
34980
|
+
const issues = o.blockingIssues === undefined ? [] : o.blockingIssues;
|
|
34981
|
+
if (!Array.isArray(issues) || !issues.every(isBlocker)) {
|
|
34982
|
+
malformed = true;
|
|
34983
|
+
return null;
|
|
34984
|
+
}
|
|
34985
|
+
return {
|
|
34986
|
+
approved: o.approved && issues.length === 0,
|
|
34987
|
+
summary: typeof o.summary === "string" ? o.summary : "",
|
|
34988
|
+
blockingIssues: issues
|
|
34989
|
+
};
|
|
34832
34990
|
};
|
|
34833
|
-
const
|
|
34834
|
-
const
|
|
34835
|
-
|
|
34836
|
-
|
|
34837
|
-
|
|
34838
|
-
|
|
34839
|
-
|
|
34840
|
-
|
|
34841
|
-
|
|
34842
|
-
|
|
34843
|
-
|
|
34844
|
-
|
|
34845
|
-
|
|
34846
|
-
refuse3(`One run lists at most ${WORKFLOW_LIMITS.route} assignments; give the remaining work as fewer, larger ones`);
|
|
34847
|
-
const rewritten = route.add.filter((t) => state.has(t.id));
|
|
34848
|
-
const gone = new Set([
|
|
34849
|
-
...route.drop,
|
|
34850
|
-
...current2.filter((t) => t.state === "superseded").map((t) => t.id)
|
|
34851
|
-
]);
|
|
34852
|
-
const next = [
|
|
34853
|
-
...current2.filter((t) => !gone.has(t.id) && !ids.includes(t.id)).map((t) => ({ id: t.id, after: t.after })),
|
|
34854
|
-
...route.add.map((t) => ({ id: t.id, after: t.after }))
|
|
34855
|
-
];
|
|
34856
|
-
const listed = new Set(next.map((t) => t.id));
|
|
34857
|
-
for (const task of next) {
|
|
34858
|
-
const missing2 = task.after.filter((id) => !listed.has(id));
|
|
34859
|
-
if (missing2.length)
|
|
34860
|
-
refuse3(`Assignment ${task.id} waits for ${missing2.join(", ")}, which this route does not keep; drop or rewrite ${task.id} too`);
|
|
34861
|
-
}
|
|
34862
|
-
try {
|
|
34863
|
-
workflowOrder(next);
|
|
34864
|
-
} catch {
|
|
34865
|
-
refuse3("The revised route has a dependency cycle");
|
|
34866
|
-
}
|
|
34867
|
-
return { superseded: [...route.drop], rewritten, added };
|
|
34868
|
-
}
|
|
34869
|
-
function recoveryObject(reply) {
|
|
34870
|
-
if (Buffer.byteLength(reply) > RECOVERY_BYTES)
|
|
34871
|
-
throw new Error("Recovery reply exceeds its byte bound");
|
|
34872
|
-
let start = -1, depth = 0, brackets = 0, quoted3 = false, escaped = false, found = false;
|
|
34873
|
-
let value;
|
|
34874
|
-
for (let i = 0;i < reply.length; i++) {
|
|
34875
|
-
const c = reply[i];
|
|
34876
|
-
if (depth === 0) {
|
|
34877
|
-
if (brackets && quoted3) {
|
|
34991
|
+
const body = reply.trim();
|
|
34992
|
+
const direct = decode4(body);
|
|
34993
|
+
if (direct)
|
|
34994
|
+
return direct;
|
|
34995
|
+
if (malformed)
|
|
34996
|
+
return null;
|
|
34997
|
+
const found = [];
|
|
34998
|
+
let starts = 0, start = body.indexOf("{");
|
|
34999
|
+
for (;start >= 0 && starts < 64; start = body.indexOf("{", start + 1), starts++) {
|
|
35000
|
+
let depth = 0, quoted3 = false, escaped = false;
|
|
35001
|
+
for (let i = start;i < body.length; i++) {
|
|
35002
|
+
const c = body[i];
|
|
35003
|
+
if (quoted3) {
|
|
34878
35004
|
if (escaped)
|
|
34879
35005
|
escaped = false;
|
|
34880
35006
|
else if (c === "\\")
|
|
34881
35007
|
escaped = true;
|
|
34882
35008
|
else if (c === '"')
|
|
34883
35009
|
quoted3 = false;
|
|
34884
|
-
|
|
34885
|
-
}
|
|
34886
|
-
if (brackets && c === '"') {
|
|
35010
|
+
} else if (c === '"')
|
|
34887
35011
|
quoted3 = true;
|
|
34888
|
-
|
|
34889
|
-
|
|
34890
|
-
if (c === "
|
|
34891
|
-
|
|
34892
|
-
|
|
34893
|
-
|
|
34894
|
-
|
|
34895
|
-
brackets = Math.max(0, brackets - 1);
|
|
34896
|
-
continue;
|
|
35012
|
+
else if (c === "{")
|
|
35013
|
+
depth++;
|
|
35014
|
+
else if (c === "}" && --depth === 0) {
|
|
35015
|
+
const verdict = decode4(body.slice(start, i + 1));
|
|
35016
|
+
if (verdict)
|
|
35017
|
+
found.push(verdict);
|
|
35018
|
+
break;
|
|
34897
35019
|
}
|
|
34898
|
-
if (c === "}")
|
|
34899
|
-
throw new Error("Unmatched recovery object delimiter");
|
|
34900
|
-
if (c !== "{")
|
|
34901
|
-
continue;
|
|
34902
|
-
if (brackets)
|
|
34903
|
-
throw new Error("Recovery must be an object, not an array");
|
|
34904
|
-
if (found)
|
|
34905
|
-
throw new Error("Multiple recovery objects are ambiguous");
|
|
34906
|
-
start = i;
|
|
34907
|
-
depth = 1;
|
|
34908
|
-
} else if (quoted3) {
|
|
34909
|
-
if (escaped)
|
|
34910
|
-
escaped = false;
|
|
34911
|
-
else if (c === "\\")
|
|
34912
|
-
escaped = true;
|
|
34913
|
-
else if (c === '"')
|
|
34914
|
-
quoted3 = false;
|
|
34915
|
-
} else if (c === '"')
|
|
34916
|
-
quoted3 = true;
|
|
34917
|
-
else if (c === "{")
|
|
34918
|
-
depth++;
|
|
34919
|
-
else if (c === "}" && --depth === 0) {
|
|
34920
|
-
value = parseWireJson(reply.slice(start, i + 1), RECOVERY_BYTES);
|
|
34921
|
-
found = true;
|
|
34922
35020
|
}
|
|
34923
35021
|
}
|
|
34924
|
-
if (!found ||
|
|
34925
|
-
|
|
34926
|
-
return
|
|
35022
|
+
if (malformed || start >= 0 || !found.length || found.some((v) => v.approved !== found[0].approved))
|
|
35023
|
+
return null;
|
|
35024
|
+
return found[0];
|
|
34927
35025
|
}
|
|
34928
|
-
function
|
|
34929
|
-
const
|
|
34930
|
-
|
|
35026
|
+
function reviewReport(reply, secrets) {
|
|
35027
|
+
const verdict = reviewVerdict(reply);
|
|
35028
|
+
if (!verdict)
|
|
35029
|
+
return {
|
|
35030
|
+
text: "Agent review did not return one unambiguous complete verdict.",
|
|
35031
|
+
truncated: false
|
|
35032
|
+
};
|
|
35033
|
+
const cleanSummary = sanitizeText(verdict.summary, secrets);
|
|
35034
|
+
const cleanIssues = verdict.blockingIssues.map((issue2) => {
|
|
35035
|
+
const clean = (s) => sanitizeText(s, secrets);
|
|
35036
|
+
if (typeof issue2 === "string")
|
|
35037
|
+
return clean(issue2);
|
|
35038
|
+
if (clean(issue2.id) !== issue2.id || issue2.writes.some((p) => clean(p) !== p))
|
|
35039
|
+
return clean(issueText(issue2));
|
|
35040
|
+
return {
|
|
35041
|
+
...issue2,
|
|
35042
|
+
evidence: clean(issue2.evidence),
|
|
35043
|
+
verify: clean(issue2.verify)
|
|
35044
|
+
};
|
|
35045
|
+
});
|
|
35046
|
+
const complete = {
|
|
35047
|
+
approved: verdict.approved,
|
|
35048
|
+
summary: cleanSummary,
|
|
35049
|
+
blockingIssues: cleanIssues
|
|
34931
35050
|
};
|
|
34932
|
-
|
|
35051
|
+
if (Buffer.byteLength(JSON.stringify(complete)) <= 16384)
|
|
35052
|
+
return { text: canonicalJson(complete, 16384), truncated: false };
|
|
35053
|
+
return {
|
|
35054
|
+
text: canonicalJson({
|
|
35055
|
+
approved: verdict.approved,
|
|
35056
|
+
summary: diagnosticText(cleanSummary, 2048),
|
|
35057
|
+
blockingIssues: cleanIssues.slice(0, 16).map((issue2) => diagnosticText(issueText(issue2), 256))
|
|
35058
|
+
}, 16384),
|
|
35059
|
+
truncated: true
|
|
35060
|
+
};
|
|
35061
|
+
}
|
|
35062
|
+
function implementationReport(reply, secrets = []) {
|
|
34933
35063
|
try {
|
|
34934
|
-
const
|
|
34935
|
-
const
|
|
35064
|
+
const body = reply.trim().replace(/^```(?:json)?\s*\n([\s\S]*?)\n```$/u, "$1");
|
|
35065
|
+
const raw = parseWireJson(body, 16384);
|
|
34936
35066
|
const o = closed(raw, [
|
|
34937
|
-
"
|
|
34938
|
-
"
|
|
34939
|
-
"
|
|
34940
|
-
"strategy",
|
|
34941
|
-
"work",
|
|
34942
|
-
...["level", "scope", "route", "deviation", "blocker"].filter(has)
|
|
35067
|
+
"status",
|
|
35068
|
+
"summary",
|
|
35069
|
+
...Object.hasOwn(raw ?? {}, "lessons") ? ["lessons"] : []
|
|
34943
35070
|
]);
|
|
34944
|
-
|
|
34945
|
-
|
|
34946
|
-
|
|
34947
|
-
|
|
34948
|
-
|
|
34949
|
-
|
|
34950
|
-
|
|
34951
|
-
|
|
34952
|
-
|
|
34953
|
-
|
|
34954
|
-
|
|
34955
|
-
const allowed = [...bounds.scope, ...scope];
|
|
34956
|
-
const work = list(o.work, 16, (value) => closed(value, ["title", "instructions", "writes"])).map((w, index) => {
|
|
34957
|
-
const writes = list(w.writes, 128, (v) => workflowPath(v, true));
|
|
34958
|
-
if (!writes.length)
|
|
34959
|
-
refuse3(`work[${index}].writes must name at least one editing path; omit check-only work items because the owner reruns the declared checks and review`);
|
|
34960
|
-
if (writes.some((p) => !workflowCovers(allowed, p) || sanitizeText(p, secrets) !== p))
|
|
34961
|
-
refuse3(`work[${index}].writes is outside the current write scope; list the additional paths in scope with level replan`);
|
|
34962
|
-
return {
|
|
34963
|
-
title: clean(w.title, 256),
|
|
34964
|
-
instructions: clean(w.instructions, 8192),
|
|
34965
|
-
writes
|
|
34966
|
-
};
|
|
34967
|
-
});
|
|
34968
|
-
const route = has("route") ? nullable(o.route, (value) => {
|
|
34969
|
-
const given = (key3) => Object.hasOwn(value ?? {}, key3);
|
|
34970
|
-
const r = closed(value, ["drop", "add"].filter(given));
|
|
34971
|
-
const within2 = [...bounds.route?.scope ?? bounds.scope, ...scope];
|
|
34972
|
-
return {
|
|
34973
|
-
drop: given("drop") ? list(r.drop, WORKFLOW_LIMITS.route, (v) => text(v, 64, true)) : [],
|
|
34974
|
-
add: given("add") ? list(r.add, WORKFLOW_LIMITS.tasks, (entry) => {
|
|
34975
|
-
const t = closed(entry, [
|
|
34976
|
-
"id",
|
|
34977
|
-
"title",
|
|
34978
|
-
"instructions",
|
|
34979
|
-
"writes",
|
|
34980
|
-
"after"
|
|
34981
|
-
]);
|
|
34982
|
-
const writes = list(t.writes, 128, (v) => workflowPath(v, true));
|
|
34983
|
-
if (!writes.length || new Set(writes).size !== writes.length || writes.some((p) => !workflowCovers(within2, p) || sanitizeText(p, secrets) !== p))
|
|
34984
|
-
refuse3("An added assignment writes outside the run's write scope; list the additional paths in scope");
|
|
34985
|
-
const after = list(t.after, WORKFLOW_LIMITS.tasks, (v) => text(v, 64, true));
|
|
34986
|
-
if (new Set(after).size !== after.length)
|
|
34987
|
-
refuse3("An assignment names each predecessor once");
|
|
34988
|
-
let id = "";
|
|
34989
|
-
try {
|
|
34990
|
-
id = workflowTaskId(t.id);
|
|
34991
|
-
} catch {
|
|
34992
|
-
refuse3("An assignment ID is lowercase letters, digits and hyphens, starts with a letter and has at most 48 characters");
|
|
34993
|
-
}
|
|
34994
|
-
return {
|
|
34995
|
-
id,
|
|
34996
|
-
title: clean(t.title, 256),
|
|
34997
|
-
instructions: clean(t.instructions, 8192),
|
|
34998
|
-
writes,
|
|
34999
|
-
after
|
|
35000
|
-
};
|
|
35001
|
-
}) : []
|
|
35002
|
-
};
|
|
35003
|
-
}) : null;
|
|
35004
|
-
if (route && !route.drop.length && !route.add.length)
|
|
35005
|
-
refuse3("A route revision drops or adds at least one assignment");
|
|
35006
|
-
if ((scope.length || route) && level === "repair")
|
|
35007
|
-
level = "replan";
|
|
35008
|
-
if (route && !bounds.route)
|
|
35009
|
-
refuse3("No assignment is waiting to start at this stage; give the remaining work as work items");
|
|
35010
|
-
if (route && bounds.route)
|
|
35011
|
-
revisedRoute(bounds.route.tasks, route);
|
|
35012
|
-
const deviation = has("deviation") ? nullable(o.deviation, (value) => {
|
|
35013
|
-
const d = closed(value, [
|
|
35014
|
-
"item",
|
|
35015
|
-
"reason",
|
|
35016
|
-
"delivered",
|
|
35017
|
-
...Object.hasOwn(value ?? {}, "waives") ? ["waives"] : []
|
|
35018
|
-
]);
|
|
35019
|
-
return {
|
|
35020
|
-
item: clean(d.item, 2048),
|
|
35021
|
-
reason: clean(d.reason, 4096),
|
|
35022
|
-
delivered: clean(d.delivered, 4096),
|
|
35023
|
-
waives: d.waives === undefined ? [] : list(d.waives, 16, (v) => text(v, 128, true))
|
|
35024
|
-
};
|
|
35025
|
-
}) : null;
|
|
35026
|
-
if (level === "deviate" !== (deviation !== null))
|
|
35027
|
-
refuse3("A deviation is recorded exactly when the level is deviate: name the item, the reason and what is delivered instead");
|
|
35028
|
-
if (deviation?.waives.some((name) => !bounds.checks.includes(name)))
|
|
35029
|
-
refuse3("A deviation can set aside only declared acceptance checks");
|
|
35030
|
-
const blocker = has("blocker") ? nullable(o.blocker, (v) => oneOf(v, BLOCKERS)) : null;
|
|
35031
|
-
if (action === "blocked") {
|
|
35032
|
-
if (work.length || scope.length || deviation || route)
|
|
35033
|
-
refuse3("A blocked decision cannot dispatch work");
|
|
35034
|
-
if (!blocker)
|
|
35035
|
-
refuse3(`Blocked is only for a condition outside the repository; name it (${BLOCKERS.join(", ")}) or decide how to continue`);
|
|
35036
|
-
} else {
|
|
35037
|
-
if (blocker)
|
|
35038
|
-
refuse3("Only a blocked decision names a blocker");
|
|
35039
|
-
if (RECOVERY_LEVELS.indexOf(level) < RECOVERY_LEVELS.indexOf(bounds.floor))
|
|
35040
|
-
refuse3(`This approach was already tried without new evidence; decide at level ${bounds.floor} or above`);
|
|
35041
|
-
}
|
|
35042
|
-
return {
|
|
35043
|
-
action,
|
|
35044
|
-
level,
|
|
35045
|
-
cause: clean(o.cause, 2048),
|
|
35046
|
-
evidence: cited,
|
|
35047
|
-
strategy: clean(o.strategy, 4096),
|
|
35048
|
-
work,
|
|
35049
|
-
scope,
|
|
35050
|
-
route,
|
|
35051
|
-
deviation,
|
|
35052
|
-
blocker
|
|
35053
|
-
};
|
|
35054
|
-
} catch (error) {
|
|
35055
|
-
if (error instanceof InvalidDecision)
|
|
35056
|
-
throw error;
|
|
35057
|
-
throw new InvalidDecision("The recovery reply was not the requested JSON object");
|
|
35071
|
+
if (o.status !== "completed" && o.status !== "partial" && o.status !== "blocked")
|
|
35072
|
+
return null;
|
|
35073
|
+
if (typeof o.summary !== "string" || !o.summary.trim())
|
|
35074
|
+
return null;
|
|
35075
|
+
if (o.lessons !== undefined && (!Array.isArray(o.lessons) || o.lessons.length > 5 || o.lessons.some((lesson) => typeof lesson !== "string" || !lesson.trim() || lesson.length > 400)))
|
|
35076
|
+
return null;
|
|
35077
|
+
const summary2 = sanitizeText(o.summary, secrets);
|
|
35078
|
+
const lessons = (o.lessons ?? []).map((lesson) => sanitizeText(lesson, secrets).trim()).filter(Boolean);
|
|
35079
|
+
return summary2.trim() ? { status: o.status, summary: summary2, ...lessons.length ? { lessons } : {} } : null;
|
|
35080
|
+
} catch {
|
|
35081
|
+
return null;
|
|
35058
35082
|
}
|
|
35059
35083
|
}
|
|
35060
|
-
var OBJECTIONS = 2;
|
|
35061
35084
|
|
|
35062
|
-
class
|
|
35063
|
-
|
|
35064
|
-
|
|
35065
|
-
|
|
35066
|
-
|
|
35067
|
-
|
|
35068
|
-
|
|
35069
|
-
|
|
35070
|
-
|
|
35071
|
-
|
|
35072
|
-
|
|
35073
|
-
|
|
35074
|
-
|
|
35075
|
-
|
|
35076
|
-
|
|
35077
|
-
|
|
35078
|
-
|
|
35079
|
-
|
|
35080
|
-
|
|
35085
|
+
class WorkflowTranscript {
|
|
35086
|
+
turn;
|
|
35087
|
+
secrets;
|
|
35088
|
+
overflowed = false;
|
|
35089
|
+
pending = false;
|
|
35090
|
+
capture;
|
|
35091
|
+
final = null;
|
|
35092
|
+
latest;
|
|
35093
|
+
messageDetails = new Set;
|
|
35094
|
+
reportCapture;
|
|
35095
|
+
reportTurn;
|
|
35096
|
+
reportId;
|
|
35097
|
+
reportDetails = new Set;
|
|
35098
|
+
reportText;
|
|
35099
|
+
assignmentReport() {
|
|
35100
|
+
if (this.reportText === undefined)
|
|
35101
|
+
return;
|
|
35102
|
+
try {
|
|
35103
|
+
const value = closed(parseWireJson(this.reportText, 65536), ["content"]);
|
|
35104
|
+
if (!Array.isArray(value.content) || value.content.length !== 1)
|
|
35105
|
+
return;
|
|
35106
|
+
const item = closed(value.content[0], ["type", "text"]);
|
|
35107
|
+
return item.type === "text" && typeof item.text === "string" ? item.text : undefined;
|
|
35108
|
+
} catch {
|
|
35109
|
+
return;
|
|
35081
35110
|
}
|
|
35082
35111
|
}
|
|
35083
|
-
|
|
35084
|
-
|
|
35085
|
-
|
|
35086
|
-
|
|
35087
|
-
|
|
35088
|
-
|
|
35089
|
-
|
|
35090
|
-
this.
|
|
35091
|
-
|
|
35092
|
-
|
|
35093
|
-
|
|
35094
|
-
|
|
35095
|
-
|
|
35096
|
-
|
|
35097
|
-
|
|
35098
|
-
return await action();
|
|
35099
|
-
} catch (error) {
|
|
35100
|
-
if (!(error instanceof AttemptFailure))
|
|
35101
|
-
throw error;
|
|
35102
|
-
if (!repair.length)
|
|
35103
|
-
original = error;
|
|
35104
|
-
const attempt2 = repair.length && original ? new AttemptFailure(error.message, [...new Set([...original.steps, ...error.steps])], `${original.identity ?? stage}:repair:${error.identity ?? "incomplete"}`) : error;
|
|
35105
|
-
failure2 = attempt2;
|
|
35106
|
-
this.ports.assertRunning();
|
|
35107
|
-
const observation = await this.ports.observe();
|
|
35108
|
-
let decision;
|
|
35109
|
-
for (;; ) {
|
|
35110
|
-
decision = await this.decide(attempt2, floor);
|
|
35111
|
-
this.ports.assertRunning();
|
|
35112
|
-
if (decision.action === "blocked")
|
|
35113
|
-
break;
|
|
35114
|
-
const fingerprint2 = createHash34("sha256").update(canonicalJson({
|
|
35115
|
-
stage,
|
|
35116
|
-
observation,
|
|
35117
|
-
failure: attempt2.identity ?? attempt2.message.replace(/[0-9a-f]{8}-[0-9a-f-]{27,}/gu, "<step>"),
|
|
35118
|
-
level: decision.level
|
|
35119
|
-
})).digest("hex");
|
|
35120
|
-
if (!this.tried.has(fingerprint2)) {
|
|
35121
|
-
this.tried.add(fingerprint2);
|
|
35122
|
-
break;
|
|
35123
|
-
}
|
|
35124
|
-
const next = RECOVERY_LEVELS[RECOVERY_LEVELS.indexOf(decision.level) + 1];
|
|
35125
|
-
if (!next)
|
|
35126
|
-
throw new Error(`No new evidence or strategy for ${stage} at any level; partial work is preserved. ${decision.cause}`);
|
|
35127
|
-
floor = next;
|
|
35128
|
-
}
|
|
35129
|
-
const record3 = {
|
|
35130
|
-
revision: ++this.revision,
|
|
35131
|
-
stage,
|
|
35132
|
-
observation,
|
|
35133
|
-
failure: attempt2.message,
|
|
35134
|
-
decision
|
|
35135
|
-
};
|
|
35136
|
-
recorded = this.ports.record(record3);
|
|
35137
|
-
this.previous = record3;
|
|
35138
|
-
if (decision.action === "blocked")
|
|
35139
|
-
throw new Error(`Delivery blocked${decision.blocker ? ` (${decision.blocker})` : ""}: ${decision.cause}
|
|
35140
|
-
${decision.strategy}`);
|
|
35141
|
-
repair = decision.work;
|
|
35112
|
+
trackReport(frame) {
|
|
35113
|
+
const e = frame.event;
|
|
35114
|
+
if (e.kind === "tool-start") {
|
|
35115
|
+
if (["read", "grep", "find", "ls", "changes"].includes(e.name))
|
|
35116
|
+
return;
|
|
35117
|
+
this.reportText = undefined;
|
|
35118
|
+
this.reportDetails.clear();
|
|
35119
|
+
this.reportCapture = undefined;
|
|
35120
|
+
this.reportTurn = undefined;
|
|
35121
|
+
this.reportId = undefined;
|
|
35122
|
+
if (e.name === "cadence_report") {
|
|
35123
|
+
this.reportId = e.callId;
|
|
35124
|
+
this.reportTurn = { ...structuredClone(this.turn), items: [] };
|
|
35125
|
+
this.reportCapture = new TurnCapture(this.reportTurn, () => {}, this.secrets);
|
|
35126
|
+
this.reportCapture.accept(frame);
|
|
35142
35127
|
}
|
|
35128
|
+
return;
|
|
35129
|
+
}
|
|
35130
|
+
if (!this.reportCapture || !this.reportId)
|
|
35131
|
+
return;
|
|
35132
|
+
if (e.kind === "detail-begin" && e.descriptor.owner.kind === "tool" && e.descriptor.owner.callId === this.reportId) {
|
|
35133
|
+
this.reportDetails.add(e.detailId);
|
|
35134
|
+
this.reportCapture.accept(frame);
|
|
35135
|
+
} else if ((e.kind === "detail-chunk" || e.kind === "detail-end") && this.reportDetails.has(e.detailId)) {
|
|
35136
|
+
this.reportCapture.accept(frame);
|
|
35137
|
+
if (e.kind === "detail-end")
|
|
35138
|
+
this.reportDetails.delete(e.detailId);
|
|
35139
|
+
} else if (e.kind === "tool-end" && e.callId === this.reportId) {
|
|
35140
|
+
this.reportCapture.accept(frame);
|
|
35141
|
+
if (!e.isError && !this.reportDetails.size)
|
|
35142
|
+
this.reportText = this.reportTurn?.items.find((i) => i.id === this.reportId)?.text;
|
|
35143
|
+
this.reportCapture = undefined;
|
|
35143
35144
|
}
|
|
35144
35145
|
}
|
|
35145
|
-
|
|
35146
|
-
|
|
35147
|
-
|
|
35148
|
-
|
|
35149
|
-
|
|
35150
|
-
|
|
35151
|
-
|
|
35152
|
-
|
|
35153
|
-
|
|
35154
|
-
Use action "blocked" only for a condition outside the repository that no route can remove, and name it in "blocker": an unavailable model, failed authentication, actual storage or permission limits, a target branch changed from outside, an installation this agreement does not allow, or a missing tool with no repository-local alternative. Before blocking, consider changes beyond the initial scope. A failing baseline, test fixture incompatible with a long temporary path or inherited service environment, or an unsuitable original approach is a repository repair, not proof of an external blocker. Repair the fixture or implementation while preserving the tested behavior and production protections; do not demand relocation of owner resources, execution outside the service, or another user confirmation when a repository-local route can work. A hard problem is not a blocker.
|
|
35155
|
-
The execution context supplied with evidence is authoritative about what ran and where. Project guidance that applies only when a resource was declared does not create a missing resource requirement: do not demand an original temporary directory or marker unless the approved commands actually declared one. Never ask a repair worker to obtain undisclosed owner records, inspect private state or reconstruct the owner environment. Keep every scratch file in the inherited owner temporary root; a socket pathname limit calls for changing the fixture's addressing (such as a relative name in a child process), never a /tmp fallback or another temporary root. Strategy text is explanation only; executable repair actions must be in work. A repair that reports partial progress does not resolve the original failure. Rewording a strategy or work instructions is not progress: the same failed operation with unchanged observed state escalates. Every continuation needs new evidence or an actionable changed approach, not another copy of a failed attempt. Model-request recovery belongs to the SDK and is never permission to replay a workflow step. The owner will run the acceptance checks and independent review again.
|
|
35156
|
-
${REPLY}`;
|
|
35157
|
-
function modelRequestFailed(output) {
|
|
35158
|
-
for (const line of output.split(`
|
|
35159
|
-
`)) {
|
|
35146
|
+
constructor(turn, secrets) {
|
|
35147
|
+
this.turn = turn;
|
|
35148
|
+
this.secrets = secrets;
|
|
35149
|
+
this.capture = new TurnCapture(turn, () => {
|
|
35150
|
+
this.pending = true;
|
|
35151
|
+
}, secrets);
|
|
35152
|
+
this.latest = { ...structuredClone(turn), items: [] };
|
|
35153
|
+
}
|
|
35154
|
+
accept(frame) {
|
|
35160
35155
|
try {
|
|
35161
|
-
|
|
35162
|
-
|
|
35156
|
+
this.trackReport(frame);
|
|
35157
|
+
} catch {
|
|
35158
|
+
this.reportText = undefined;
|
|
35159
|
+
this.reportCapture = undefined;
|
|
35160
|
+
}
|
|
35161
|
+
this.track(frame);
|
|
35162
|
+
if (this.overflowed) {
|
|
35163
|
+
if (frame.event.kind === "model-retry") {
|
|
35164
|
+
if (frame.event.progress)
|
|
35165
|
+
this.turn.retry = frame.event.progress;
|
|
35166
|
+
else
|
|
35167
|
+
delete this.turn.retry;
|
|
35163
35168
|
return true;
|
|
35164
|
-
|
|
35169
|
+
}
|
|
35170
|
+
if (frame.event.kind === "worker-failure") {
|
|
35171
|
+
delete this.turn.retry;
|
|
35172
|
+
return true;
|
|
35173
|
+
}
|
|
35174
|
+
return false;
|
|
35175
|
+
}
|
|
35176
|
+
try {
|
|
35177
|
+
this.capture.accept(frame);
|
|
35178
|
+
} catch {
|
|
35179
|
+
this.overflowed = true;
|
|
35180
|
+
this.turn.notice = TRANSCRIPT_LIMIT;
|
|
35181
|
+
this.turn.omitted.other = true;
|
|
35182
|
+
this.trimToBound();
|
|
35183
|
+
return true;
|
|
35184
|
+
}
|
|
35185
|
+
const save = this.pending;
|
|
35186
|
+
this.pending = false;
|
|
35187
|
+
return save;
|
|
35165
35188
|
}
|
|
35166
|
-
|
|
35167
|
-
|
|
35168
|
-
|
|
35169
|
-
|
|
35170
|
-
|
|
35171
|
-
|
|
35172
|
-
|
|
35189
|
+
trimToBound() {
|
|
35190
|
+
for (;; ) {
|
|
35191
|
+
try {
|
|
35192
|
+
canonicalJson(this.turn, RECORD_BYTES2 - 4096);
|
|
35193
|
+
return;
|
|
35194
|
+
} catch {
|
|
35195
|
+
if (!this.turn.items.length)
|
|
35196
|
+
return;
|
|
35197
|
+
this.turn.items.pop();
|
|
35198
|
+
}
|
|
35199
|
+
}
|
|
35200
|
+
}
|
|
35201
|
+
track(frame) {
|
|
35202
|
+
const e = frame.event;
|
|
35203
|
+
let relevant2 = false;
|
|
35204
|
+
if (e.kind === "message-start") {
|
|
35205
|
+
if (e.role === "assistant") {
|
|
35206
|
+
this.latest = { ...structuredClone(this.latest), items: [] };
|
|
35207
|
+
this.final = new TurnCapture(this.latest, () => {}, this.secrets);
|
|
35208
|
+
}
|
|
35209
|
+
relevant2 = true;
|
|
35210
|
+
} else if (e.kind === "message-end")
|
|
35211
|
+
relevant2 = true;
|
|
35212
|
+
else if (e.kind === "detail-begin") {
|
|
35213
|
+
const owner = e.descriptor.owner.kind;
|
|
35214
|
+
if (owner === "message" || owner === "message-final") {
|
|
35215
|
+
this.messageDetails.add(e.detailId);
|
|
35216
|
+
relevant2 = true;
|
|
35217
|
+
}
|
|
35218
|
+
} else if (e.kind === "detail-chunk" || e.kind === "detail-end") {
|
|
35219
|
+
relevant2 = this.messageDetails.has(e.detailId);
|
|
35220
|
+
if (e.kind === "detail-end")
|
|
35221
|
+
this.messageDetails.delete(e.detailId);
|
|
35222
|
+
}
|
|
35223
|
+
if (!relevant2 || !this.final)
|
|
35224
|
+
return;
|
|
35173
35225
|
try {
|
|
35174
|
-
|
|
35175
|
-
|
|
35176
|
-
|
|
35177
|
-
|
|
35178
|
-
|
|
35179
|
-
|
|
35180
|
-
|
|
35181
|
-
|
|
35182
|
-
|
|
35226
|
+
this.final.accept(frame);
|
|
35227
|
+
} catch {
|
|
35228
|
+
this.final = null;
|
|
35229
|
+
}
|
|
35230
|
+
}
|
|
35231
|
+
selected(messageId) {
|
|
35232
|
+
const find = (turn) => turn.items.find((i) => i.id === messageId && i.role === "assistant")?.text;
|
|
35233
|
+
return (this.final ? find(this.latest) : undefined) ?? (this.overflowed ? undefined : find(this.turn));
|
|
35234
|
+
}
|
|
35235
|
+
get complete() {
|
|
35236
|
+
return !this.overflowed && !!this.final;
|
|
35183
35237
|
}
|
|
35184
|
-
return `${MODEL_REQUEST_FAILED}
|
|
35185
|
-
Provider: ${reason || "no reason the worker could report"} (${attempts} automatic ${attempts === 1 ? "retry" : "retries"}${Number.isSafeInteger(status) ? `, HTTP ${status}` : ""})`;
|
|
35186
35238
|
}
|
|
35187
|
-
|
|
35188
|
-
|
|
35189
|
-
|
|
35190
|
-
|
|
35191
|
-
|
|
35192
|
-
|
|
35193
|
-
|
|
35194
|
-
|
|
35239
|
+
|
|
35240
|
+
// src/service/workflow-controller.ts
|
|
35241
|
+
var RECOVERY_BYTES = 32768;
|
|
35242
|
+
|
|
35243
|
+
class AttemptFailure extends Error {
|
|
35244
|
+
steps;
|
|
35245
|
+
identity;
|
|
35246
|
+
constructor(message2, steps, identity2) {
|
|
35247
|
+
super(message2);
|
|
35248
|
+
this.steps = steps;
|
|
35249
|
+
this.identity = identity2;
|
|
35195
35250
|
}
|
|
35196
|
-
return false;
|
|
35197
35251
|
}
|
|
35198
35252
|
|
|
35199
|
-
|
|
35200
|
-
import { lstatSync as lstatSync26, rmdirSync } from "node:fs";
|
|
35201
|
-
import { dirname as dirname25, join as join42 } from "node:path";
|
|
35202
|
-
function taskBranch(goal, id) {
|
|
35203
|
-
const slug2 = goal.toLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-|-$/gu, "").slice(0, 48).replace(/-$/u, "") || "task";
|
|
35204
|
-
return `refs/heads/cadence/${slug2}-${decodeId(id)}`;
|
|
35253
|
+
class InvalidDecision extends Error {
|
|
35205
35254
|
}
|
|
35206
|
-
|
|
35207
|
-
|
|
35208
|
-
|
|
35209
|
-
|
|
35210
|
-
|
|
35211
|
-
|
|
35212
|
-
|
|
35213
|
-
|
|
35214
|
-
|
|
35215
|
-
|
|
35216
|
-
|
|
35217
|
-
|
|
35218
|
-
after: null,
|
|
35219
|
-
reason: ""
|
|
35220
|
-
},
|
|
35221
|
-
postChecks: "not-run",
|
|
35222
|
-
cleanup: { status: "pending", reason: "" }
|
|
35255
|
+
var RECOVERY_LEVELS = ["repair", "replan", "deviate"];
|
|
35256
|
+
var BLOCKERS = [
|
|
35257
|
+
"model",
|
|
35258
|
+
"authentication",
|
|
35259
|
+
"storage",
|
|
35260
|
+
"target",
|
|
35261
|
+
"installation",
|
|
35262
|
+
"tool"
|
|
35263
|
+
];
|
|
35264
|
+
function revisedRoute(current2, route) {
|
|
35265
|
+
const refuse3 = (message2) => {
|
|
35266
|
+
throw new InvalidDecision(message2);
|
|
35223
35267
|
};
|
|
35224
|
-
|
|
35225
|
-
|
|
35226
|
-
|
|
35227
|
-
|
|
35228
|
-
|
|
35229
|
-
|
|
35230
|
-
|
|
35231
|
-
|
|
35232
|
-
|
|
35233
|
-
|
|
35234
|
-
|
|
35235
|
-
|
|
35236
|
-
|
|
35237
|
-
}
|
|
35238
|
-
|
|
35239
|
-
|
|
35240
|
-
|
|
35241
|
-
|
|
35242
|
-
|
|
35243
|
-
|
|
35244
|
-
|
|
35245
|
-
|
|
35246
|
-
|
|
35247
|
-
|
|
35248
|
-
|
|
35249
|
-
|
|
35250
|
-
|
|
35251
|
-
|
|
35252
|
-
this.home = dirname25(view.worktreeRoot);
|
|
35253
|
-
this.frontier = view.binding.target.head;
|
|
35254
|
-
this.base = view.binding.target.head;
|
|
35255
|
-
}
|
|
35256
|
-
prepareHome() {
|
|
35257
|
-
assertRepository(this.view.binding.target);
|
|
35258
|
-
if (nested(this.view.binding.target.checkoutRoot, this.home))
|
|
35259
|
-
throw new ContractError("unsupported-context");
|
|
35260
|
-
for (const path of [dirname25(dirname25(this.home)), dirname25(this.home)]) {
|
|
35261
|
-
if (!safeDirectory(path, true))
|
|
35262
|
-
throw new ContractError("forbidden");
|
|
35263
|
-
const stat = lstatSync26(path);
|
|
35264
|
-
if (process.platform !== "win32" && ((stat.mode & 63) !== 0 || process.getuid && stat.uid !== process.getuid()))
|
|
35265
|
-
throw new ContractError("forbidden");
|
|
35266
|
-
}
|
|
35267
|
-
if (safeDirectory(this.home))
|
|
35268
|
-
throw new ContractError("conflict");
|
|
35269
|
-
safeDirectory(this.home, true);
|
|
35270
|
-
}
|
|
35271
|
-
async create(git) {
|
|
35272
|
-
const { worktreeRoot: path, taskBranch: branch, binding: binding2 } = this.view;
|
|
35273
|
-
if (this.view.workspace || safeDirectory(path))
|
|
35274
|
-
throw new ContractError("conflict");
|
|
35275
|
-
if ((await gitRun(binding2.target.checkoutRoot, [
|
|
35276
|
-
"show-ref",
|
|
35277
|
-
"--verify",
|
|
35278
|
-
"--quiet",
|
|
35279
|
-
branch
|
|
35280
|
-
])).code !== 1)
|
|
35281
|
-
throw new ContractError("conflict");
|
|
35282
|
-
this.token = this.resources.reserveWorkspace(path);
|
|
35283
|
-
this.view.workspace = { head: binding2.target.head, state: "preparing" };
|
|
35284
|
-
this.save();
|
|
35285
|
-
await git([
|
|
35286
|
-
"worktree",
|
|
35287
|
-
"add",
|
|
35288
|
-
"-b",
|
|
35289
|
-
branch.slice("refs/heads/".length),
|
|
35290
|
-
path,
|
|
35291
|
-
binding2.target.head
|
|
35292
|
-
]);
|
|
35293
|
-
await assertWorktree(binding2.target.checkoutRoot, path);
|
|
35294
|
-
if (await gitHead(path) !== binding2.target.head || (await gitText(path, ["symbolic-ref", "HEAD"])).trim() !== branch)
|
|
35295
|
-
throw new ContractError("stale");
|
|
35296
|
-
this.resources.confirmWorkspace(this.token);
|
|
35297
|
-
this.view.workspace.state = "ready";
|
|
35298
|
-
this.view.preparation = "ready";
|
|
35299
|
-
this.save();
|
|
35300
|
-
return path;
|
|
35301
|
-
}
|
|
35302
|
-
childPath(id) {
|
|
35303
|
-
return join42(this.home, "tasks", id);
|
|
35304
|
-
}
|
|
35305
|
-
child(path) {
|
|
35306
|
-
return this.view.artifacts?.find((a) => a.path === path && this.children.has(path));
|
|
35307
|
-
}
|
|
35308
|
-
async createChild(id, git) {
|
|
35309
|
-
const path = this.childPath(id);
|
|
35310
|
-
const base = await this.assertTip();
|
|
35311
|
-
if (this.view.artifacts?.some((a) => a.path === path) || safeDirectory(path))
|
|
35312
|
-
throw new ContractError("conflict");
|
|
35313
|
-
if (!safeDirectory(dirname25(path), true))
|
|
35314
|
-
throw new ContractError("forbidden");
|
|
35315
|
-
const token = this.resources.reserveWorkspace(path);
|
|
35316
|
-
this.children.set(path, token);
|
|
35317
|
-
const artifact = {
|
|
35318
|
-
id,
|
|
35319
|
-
path,
|
|
35320
|
-
base,
|
|
35321
|
-
head: base,
|
|
35322
|
-
applied: null,
|
|
35323
|
-
state: "preparing"
|
|
35324
|
-
};
|
|
35325
|
-
this.view.artifacts = [...this.view.artifacts ?? [], artifact];
|
|
35326
|
-
this.save();
|
|
35327
|
-
await git(["worktree", "add", "--detach", path, base]);
|
|
35328
|
-
await assertWorktree(this.view.binding.target.checkoutRoot, path);
|
|
35329
|
-
if (await gitHead(path) !== base)
|
|
35330
|
-
throw new ContractError("stale");
|
|
35331
|
-
this.resources.confirmWorkspace(token);
|
|
35332
|
-
artifact.state = "ready";
|
|
35333
|
-
this.save();
|
|
35334
|
-
return path;
|
|
35335
|
-
}
|
|
35336
|
-
assertWorkspace(path) {
|
|
35337
|
-
if (path === this.view.worktreeRoot) {
|
|
35338
|
-
this.resources.assertWorkspace(this.token ?? "");
|
|
35339
|
-
return;
|
|
35340
|
-
}
|
|
35341
|
-
const token = this.children.get(path);
|
|
35342
|
-
if (!token || !this.child(path))
|
|
35343
|
-
throw new ContractError("stale");
|
|
35344
|
-
this.resources.assertWorkspace(token);
|
|
35345
|
-
}
|
|
35346
|
-
async assertBinding(path) {
|
|
35347
|
-
this.assertWorkspace(path);
|
|
35348
|
-
if (path === this.view.worktreeRoot) {
|
|
35349
|
-
if (this.view.workspace?.state !== "ready" || (await gitText(path, ["symbolic-ref", "HEAD"])).trim() !== this.view.taskBranch)
|
|
35350
|
-
throw new ContractError("stale");
|
|
35351
|
-
} else if (this.child(path)?.state !== "ready" || (await gitRun(path, ["symbolic-ref", "-q", "HEAD"])).code === 0)
|
|
35352
|
-
throw new ContractError("stale");
|
|
35353
|
-
await assertWorktree(this.view.binding.target.checkoutRoot, path);
|
|
35268
|
+
const state = new Map(current2.map((t) => [t.id, t.state]));
|
|
35269
|
+
const waiting = (id) => state.get(id) === "queued";
|
|
35270
|
+
const ids = route.add.map((t) => t.id);
|
|
35271
|
+
if (new Set(ids).size !== ids.length || new Set(route.drop).size !== route.drop.length || route.drop.some((id) => ids.includes(id)))
|
|
35272
|
+
refuse3("A route names each assignment once: drop it, or give it again under its ID to rewrite it");
|
|
35273
|
+
const started = [...route.drop, ...ids].filter((id) => state.has(id) && !waiting(id));
|
|
35274
|
+
if (started.length)
|
|
35275
|
+
refuse3(`Only an assignment that has not started can be dropped or rewritten; these have: ${started.join(", ")}. Give what remains of them as work, or add an assignment under a new ID`);
|
|
35276
|
+
const unknown = route.drop.filter((id) => !state.has(id));
|
|
35277
|
+
if (unknown.length)
|
|
35278
|
+
refuse3(`The route drops assignments the run does not list: ${unknown.join(", ")}`);
|
|
35279
|
+
const added = route.add.filter((t) => !state.has(t.id));
|
|
35280
|
+
if (current2.length + added.length > WORKFLOW_LIMITS.route)
|
|
35281
|
+
refuse3(`One run lists at most ${WORKFLOW_LIMITS.route} assignments; give the remaining work as fewer, larger ones`);
|
|
35282
|
+
const rewritten = route.add.filter((t) => state.has(t.id));
|
|
35283
|
+
const gone = new Set([
|
|
35284
|
+
...route.drop,
|
|
35285
|
+
...current2.filter((t) => t.state === "superseded").map((t) => t.id)
|
|
35286
|
+
]);
|
|
35287
|
+
const next = [
|
|
35288
|
+
...current2.filter((t) => !gone.has(t.id) && !ids.includes(t.id)).map((t) => ({ id: t.id, after: t.after })),
|
|
35289
|
+
...route.add.map((t) => ({ id: t.id, after: t.after }))
|
|
35290
|
+
];
|
|
35291
|
+
const listed = new Set(next.map((t) => t.id));
|
|
35292
|
+
for (const task of next) {
|
|
35293
|
+
const missing2 = task.after.filter((id) => !listed.has(id));
|
|
35294
|
+
if (missing2.length)
|
|
35295
|
+
refuse3(`Assignment ${task.id} waits for ${missing2.join(", ")}, which this route does not keep; drop or rewrite ${task.id} too`);
|
|
35354
35296
|
}
|
|
35355
|
-
|
|
35356
|
-
|
|
35357
|
-
|
|
35358
|
-
|
|
35359
|
-
throw new WorkflowRefusal("stale", `No owner-recorded HEAD exists for worktree ${path}; its files and commits are retained.`);
|
|
35360
|
-
const observed = await gitHead(path);
|
|
35361
|
-
if (observed !== head)
|
|
35362
|
-
throw new WorkflowRefusal("stale", `Worktree HEAD changed outside the owner's recorded Git operations: ${path}; expected ${head}, observed ${observed}. Its files and commits are retained; no automatic adoption or replay.`);
|
|
35363
|
-
return head;
|
|
35297
|
+
try {
|
|
35298
|
+
workflowOrder(next);
|
|
35299
|
+
} catch {
|
|
35300
|
+
refuse3("The revised route has a dependency cycle");
|
|
35364
35301
|
}
|
|
35365
|
-
|
|
35366
|
-
|
|
35367
|
-
|
|
35368
|
-
|
|
35369
|
-
|
|
35370
|
-
|
|
35371
|
-
|
|
35372
|
-
|
|
35302
|
+
return { superseded: [...route.drop], rewritten, added };
|
|
35303
|
+
}
|
|
35304
|
+
function recoveryObject(reply) {
|
|
35305
|
+
if (Buffer.byteLength(reply) > RECOVERY_BYTES)
|
|
35306
|
+
throw new Error("Recovery reply exceeds its byte bound");
|
|
35307
|
+
let start = -1, depth = 0, brackets = 0, quoted3 = false, escaped = false, found = false;
|
|
35308
|
+
let value;
|
|
35309
|
+
for (let i = 0;i < reply.length; i++) {
|
|
35310
|
+
const c = reply[i];
|
|
35311
|
+
if (depth === 0) {
|
|
35312
|
+
if (brackets && quoted3) {
|
|
35313
|
+
if (escaped)
|
|
35314
|
+
escaped = false;
|
|
35315
|
+
else if (c === "\\")
|
|
35316
|
+
escaped = true;
|
|
35317
|
+
else if (c === '"')
|
|
35318
|
+
quoted3 = false;
|
|
35319
|
+
continue;
|
|
35320
|
+
}
|
|
35321
|
+
if (brackets && c === '"') {
|
|
35322
|
+
quoted3 = true;
|
|
35323
|
+
continue;
|
|
35324
|
+
}
|
|
35325
|
+
if (c === "[") {
|
|
35326
|
+
brackets++;
|
|
35327
|
+
continue;
|
|
35328
|
+
}
|
|
35329
|
+
if (c === "]") {
|
|
35330
|
+
brackets = Math.max(0, brackets - 1);
|
|
35331
|
+
continue;
|
|
35332
|
+
}
|
|
35333
|
+
if (c === "}")
|
|
35334
|
+
throw new Error("Unmatched recovery object delimiter");
|
|
35335
|
+
if (c !== "{")
|
|
35336
|
+
continue;
|
|
35337
|
+
if (brackets)
|
|
35338
|
+
throw new Error("Recovery must be an object, not an array");
|
|
35339
|
+
if (found)
|
|
35340
|
+
throw new Error("Multiple recovery objects are ambiguous");
|
|
35341
|
+
start = i;
|
|
35342
|
+
depth = 1;
|
|
35343
|
+
} else if (quoted3) {
|
|
35344
|
+
if (escaped)
|
|
35345
|
+
escaped = false;
|
|
35346
|
+
else if (c === "\\")
|
|
35347
|
+
escaped = true;
|
|
35348
|
+
else if (c === '"')
|
|
35349
|
+
quoted3 = false;
|
|
35350
|
+
} else if (c === '"')
|
|
35351
|
+
quoted3 = true;
|
|
35352
|
+
else if (c === "{")
|
|
35353
|
+
depth++;
|
|
35354
|
+
else if (c === "}" && --depth === 0) {
|
|
35355
|
+
value = parseWireJson(reply.slice(start, i + 1), RECOVERY_BYTES);
|
|
35356
|
+
found = true;
|
|
35373
35357
|
}
|
|
35374
|
-
if (this.view.workspace?.state !== "ready")
|
|
35375
|
-
throw new ContractError("stale");
|
|
35376
|
-
this.view.workspace.head = head;
|
|
35377
|
-
this.view.acceptance = null;
|
|
35378
|
-
this.save();
|
|
35379
35358
|
}
|
|
35380
|
-
|
|
35381
|
-
|
|
35382
|
-
|
|
35383
|
-
|
|
35384
|
-
|
|
35385
|
-
|
|
35386
|
-
|
|
35387
|
-
|
|
35388
|
-
|
|
35389
|
-
|
|
35390
|
-
|
|
35391
|
-
|
|
35392
|
-
|
|
35393
|
-
|
|
35394
|
-
|
|
35395
|
-
|
|
35396
|
-
|
|
35397
|
-
|
|
35398
|
-
|
|
35399
|
-
|
|
35400
|
-
|
|
35401
|
-
|
|
35402
|
-
|
|
35403
|
-
|
|
35404
|
-
|
|
35359
|
+
if (!found || depth !== 0)
|
|
35360
|
+
throw new Error("Incomplete recovery object");
|
|
35361
|
+
return value;
|
|
35362
|
+
}
|
|
35363
|
+
function decodeRecoveryDecision(reply, bounds, secrets = []) {
|
|
35364
|
+
const refuse3 = (message2) => {
|
|
35365
|
+
throw new InvalidDecision(message2);
|
|
35366
|
+
};
|
|
35367
|
+
const clean = (value, maximum) => text(sanitizeText(text(value, maximum, true), secrets), maximum, true);
|
|
35368
|
+
try {
|
|
35369
|
+
const raw = recoveryObject(reply);
|
|
35370
|
+
const has = (key3) => Object.hasOwn(raw ?? {}, key3);
|
|
35371
|
+
const o = closed(raw, [
|
|
35372
|
+
"action",
|
|
35373
|
+
"cause",
|
|
35374
|
+
"evidence",
|
|
35375
|
+
"strategy",
|
|
35376
|
+
"work",
|
|
35377
|
+
...["level", "scope", "route", "deviation", "blocker"].filter(has)
|
|
35378
|
+
]);
|
|
35379
|
+
const action = oneOf(o.action, ["continue", "blocked"]);
|
|
35380
|
+
let level = has("level") ? oneOf(o.level, RECOVERY_LEVELS) : "repair";
|
|
35381
|
+
const cited = list(o.evidence, 32, (v) => text(v, 160, true));
|
|
35382
|
+
if (!cited.length || cited.some((id) => !bounds.evidence.includes(id)))
|
|
35383
|
+
refuse3("Recovery must cite this failure's actual step identities");
|
|
35384
|
+
const scope = has("scope") ? list(o.scope, 64, (v) => workflowPath(v, true)) : [];
|
|
35385
|
+
if (scope.some((p) => sanitizeText(p, secrets) !== p))
|
|
35386
|
+
refuse3("A scope path names protected text");
|
|
35387
|
+
const outside = scope.filter((p) => !workflowBoundary(p));
|
|
35388
|
+
if (outside.length)
|
|
35389
|
+
refuse3(`These paths can never join the write scope (credentials, keys, .env, .git or outside the repository): ${outside.join(", ")}`);
|
|
35390
|
+
const allowed = [...bounds.scope, ...scope];
|
|
35391
|
+
const work = list(o.work, 16, (value) => closed(value, ["title", "instructions", "writes"])).map((w, index) => {
|
|
35392
|
+
const writes = list(w.writes, 128, (v) => workflowPath(v, true));
|
|
35393
|
+
if (!writes.length)
|
|
35394
|
+
refuse3(`work[${index}].writes must name at least one editing path; omit check-only work items because the owner reruns the declared checks and review`);
|
|
35395
|
+
if (writes.some((p) => !workflowCovers(allowed, p) || sanitizeText(p, secrets) !== p))
|
|
35396
|
+
refuse3(`work[${index}].writes is outside the current write scope; list the additional paths in scope with level replan`);
|
|
35397
|
+
return {
|
|
35398
|
+
title: clean(w.title, 256),
|
|
35399
|
+
instructions: clean(w.instructions, 8192),
|
|
35400
|
+
writes
|
|
35401
|
+
};
|
|
35402
|
+
});
|
|
35403
|
+
const route = has("route") ? nullable(o.route, (value) => {
|
|
35404
|
+
const given = (key3) => Object.hasOwn(value ?? {}, key3);
|
|
35405
|
+
const r = closed(value, ["drop", "add"].filter(given));
|
|
35406
|
+
const within2 = [...bounds.route?.scope ?? bounds.scope, ...scope];
|
|
35407
|
+
return {
|
|
35408
|
+
drop: given("drop") ? list(r.drop, WORKFLOW_LIMITS.route, (v) => text(v, 64, true)) : [],
|
|
35409
|
+
add: given("add") ? list(r.add, WORKFLOW_LIMITS.tasks, (entry) => {
|
|
35410
|
+
const t = closed(entry, [
|
|
35411
|
+
"id",
|
|
35412
|
+
"title",
|
|
35413
|
+
"instructions",
|
|
35414
|
+
"writes",
|
|
35415
|
+
"after"
|
|
35416
|
+
]);
|
|
35417
|
+
const writes = list(t.writes, 128, (v) => workflowPath(v, true));
|
|
35418
|
+
if (!writes.length || new Set(writes).size !== writes.length || writes.some((p) => !workflowCovers(within2, p) || sanitizeText(p, secrets) !== p))
|
|
35419
|
+
refuse3("An added assignment writes outside the run's write scope; list the additional paths in scope");
|
|
35420
|
+
const after = list(t.after, WORKFLOW_LIMITS.tasks, (v) => text(v, 64, true));
|
|
35421
|
+
if (new Set(after).size !== after.length)
|
|
35422
|
+
refuse3("An assignment names each predecessor once");
|
|
35423
|
+
let id = "";
|
|
35424
|
+
try {
|
|
35425
|
+
id = workflowTaskId(t.id);
|
|
35426
|
+
} catch {
|
|
35427
|
+
refuse3("An assignment ID is lowercase letters, digits and hyphens, starts with a letter and has at most 48 characters");
|
|
35428
|
+
}
|
|
35429
|
+
return {
|
|
35430
|
+
id,
|
|
35431
|
+
title: clean(t.title, 256),
|
|
35432
|
+
instructions: clean(t.instructions, 8192),
|
|
35433
|
+
writes,
|
|
35434
|
+
after
|
|
35435
|
+
};
|
|
35436
|
+
}) : []
|
|
35437
|
+
};
|
|
35438
|
+
}) : null;
|
|
35439
|
+
if (route && !route.drop.length && !route.add.length)
|
|
35440
|
+
refuse3("A route revision drops or adds at least one assignment");
|
|
35441
|
+
if ((scope.length || route) && level === "repair")
|
|
35442
|
+
level = "replan";
|
|
35443
|
+
if (route && !bounds.route)
|
|
35444
|
+
refuse3("No assignment is waiting to start at this stage; give the remaining work as work items");
|
|
35445
|
+
if (route && bounds.route)
|
|
35446
|
+
revisedRoute(bounds.route.tasks, route);
|
|
35447
|
+
const deviation = has("deviation") ? nullable(o.deviation, (value) => {
|
|
35448
|
+
const d = closed(value, [
|
|
35449
|
+
"item",
|
|
35450
|
+
"reason",
|
|
35451
|
+
"delivered",
|
|
35452
|
+
...Object.hasOwn(value ?? {}, "waives") ? ["waives"] : []
|
|
35453
|
+
]);
|
|
35454
|
+
return {
|
|
35455
|
+
item: clean(d.item, 2048),
|
|
35456
|
+
reason: clean(d.reason, 4096),
|
|
35457
|
+
delivered: clean(d.delivered, 4096),
|
|
35458
|
+
waives: d.waives === undefined ? [] : list(d.waives, 16, (v) => text(v, 128, true))
|
|
35459
|
+
};
|
|
35460
|
+
}) : null;
|
|
35461
|
+
if (level === "deviate" !== (deviation !== null))
|
|
35462
|
+
refuse3("A deviation is recorded exactly when the level is deviate: name the item, the reason and what is delivered instead");
|
|
35463
|
+
if (deviation?.waives.some((name) => !bounds.checks.includes(name)))
|
|
35464
|
+
refuse3("A deviation can set aside only declared acceptance checks");
|
|
35465
|
+
const blocker = has("blocker") ? nullable(o.blocker, (v) => oneOf(v, BLOCKERS)) : null;
|
|
35466
|
+
if (action === "blocked") {
|
|
35467
|
+
if (work.length || scope.length || deviation || route)
|
|
35468
|
+
refuse3("A blocked decision cannot dispatch work");
|
|
35469
|
+
if (!blocker)
|
|
35470
|
+
refuse3(`Blocked is only for a condition outside the repository; name it (${BLOCKERS.join(", ")}) or decide how to continue`);
|
|
35471
|
+
} else {
|
|
35472
|
+
if (blocker)
|
|
35473
|
+
refuse3("Only a blocked decision names a blocker");
|
|
35474
|
+
if (RECOVERY_LEVELS.indexOf(level) < RECOVERY_LEVELS.indexOf(bounds.floor))
|
|
35475
|
+
refuse3(`This approach was already tried without new evidence; decide at level ${bounds.floor} or above`);
|
|
35405
35476
|
}
|
|
35406
|
-
|
|
35407
|
-
|
|
35408
|
-
|
|
35409
|
-
|
|
35410
|
-
|
|
35411
|
-
|
|
35412
|
-
|
|
35413
|
-
|
|
35414
|
-
|
|
35415
|
-
|
|
35416
|
-
|
|
35417
|
-
|
|
35418
|
-
|
|
35419
|
-
|
|
35420
|
-
|
|
35421
|
-
|
|
35422
|
-
}
|
|
35423
|
-
|
|
35424
|
-
|
|
35425
|
-
|
|
35426
|
-
|
|
35427
|
-
|
|
35428
|
-
|
|
35429
|
-
|
|
35430
|
-
this.
|
|
35431
|
-
|
|
35432
|
-
|
|
35433
|
-
|
|
35434
|
-
if (
|
|
35435
|
-
return
|
|
35436
|
-
|
|
35437
|
-
|
|
35438
|
-
|
|
35439
|
-
|
|
35440
|
-
|
|
35441
|
-
|
|
35442
|
-
|
|
35443
|
-
|
|
35444
|
-
|
|
35445
|
-
|
|
35446
|
-
|
|
35447
|
-
|
|
35448
|
-
|
|
35449
|
-
|
|
35450
|
-
|
|
35451
|
-
|
|
35452
|
-
|
|
35453
|
-
|
|
35454
|
-
|
|
35455
|
-
|
|
35456
|
-
|
|
35457
|
-
await assertTarget(this.view.binding.target.checkoutRoot, {
|
|
35458
|
-
branch: this.view.binding.target.branch,
|
|
35459
|
-
head: targetHead
|
|
35460
|
-
}, scope, "integration preflight");
|
|
35461
|
-
if ((await gitRun(this.view.worktreeRoot, [
|
|
35462
|
-
"merge-base",
|
|
35463
|
-
"--is-ancestor",
|
|
35464
|
-
targetHead,
|
|
35465
|
-
candidate
|
|
35466
|
-
])).code !== 0)
|
|
35467
|
-
throw new ContractError("stale");
|
|
35468
|
-
return candidate;
|
|
35469
|
-
}
|
|
35470
|
-
async cleanup(remove) {
|
|
35471
|
-
const path = this.view.worktreeRoot;
|
|
35472
|
-
try {
|
|
35473
|
-
await remove(path, async () => {
|
|
35474
|
-
const { candidate, acceptance, integration, postChecks } = this.view;
|
|
35475
|
-
if (postChecks !== "passed" || integration.status !== "applied" || !candidate || acceptance?.candidate !== candidate || integration.after !== candidate || await this.assertTip() !== candidate)
|
|
35476
|
-
throw new ContractError("stale");
|
|
35477
|
-
assertRepository(this.view.binding.target);
|
|
35478
|
-
if ((await worktreeResidue(path)).length)
|
|
35479
|
-
throw new Error("Uncommitted, untracked, ignored or index-hidden files retained");
|
|
35480
|
-
if ((await gitRun(this.view.binding.target.checkoutRoot, [
|
|
35481
|
-
"merge-base",
|
|
35482
|
-
"--is-ancestor",
|
|
35483
|
-
candidate,
|
|
35484
|
-
this.view.binding.target.branch
|
|
35485
|
-
])).code !== 0)
|
|
35486
|
-
throw new ContractError("stale");
|
|
35487
|
-
});
|
|
35488
|
-
if (safeDirectory(path))
|
|
35489
|
-
throw new Error("Worktree removal was not confirmed");
|
|
35490
|
-
this.resources.release(this.token ?? "");
|
|
35491
|
-
this.token = undefined;
|
|
35492
|
-
if (this.view.workspace)
|
|
35493
|
-
this.view.workspace.state = "removed";
|
|
35494
|
-
this.view.cleanup = { status: "removed", reason: "" };
|
|
35477
|
+
return {
|
|
35478
|
+
action,
|
|
35479
|
+
level,
|
|
35480
|
+
cause: clean(o.cause, 2048),
|
|
35481
|
+
evidence: cited,
|
|
35482
|
+
strategy: clean(o.strategy, 4096),
|
|
35483
|
+
work,
|
|
35484
|
+
scope,
|
|
35485
|
+
route,
|
|
35486
|
+
deviation,
|
|
35487
|
+
blocker
|
|
35488
|
+
};
|
|
35489
|
+
} catch (error) {
|
|
35490
|
+
if (error instanceof InvalidDecision)
|
|
35491
|
+
throw error;
|
|
35492
|
+
throw new InvalidDecision("The recovery reply was not the requested JSON object");
|
|
35493
|
+
}
|
|
35494
|
+
}
|
|
35495
|
+
|
|
35496
|
+
class ReviewRepair extends AttemptFailure {
|
|
35497
|
+
ids;
|
|
35498
|
+
decision;
|
|
35499
|
+
constructor(ids, decision) {
|
|
35500
|
+
super(`Agent review: unresolved ${ids.join(", ")}.`, decision.evidence, `review:${ids.join(",")}`);
|
|
35501
|
+
this.ids = ids;
|
|
35502
|
+
this.decision = decision;
|
|
35503
|
+
}
|
|
35504
|
+
static from(blockers, step, scope, secrets) {
|
|
35505
|
+
if (!blockers.length || !blockers.every((issue2) => typeof issue2 !== "string" && issue2.writes.length > 0 && issue2.writes.every(workflowBoundary)))
|
|
35506
|
+
return;
|
|
35507
|
+
const ids = blockers.map((issue2) => issue2.id).sort();
|
|
35508
|
+
if (new Set(ids).size !== ids.length)
|
|
35509
|
+
return;
|
|
35510
|
+
const writes = [...new Set(blockers.flatMap((issue2) => issue2.writes))];
|
|
35511
|
+
try {
|
|
35512
|
+
const decision = decodeRecoveryDecision(JSON.stringify({
|
|
35513
|
+
action: "continue",
|
|
35514
|
+
evidence: [step],
|
|
35515
|
+
cause: "Independent review identified outstanding acceptance issues.",
|
|
35516
|
+
strategy: `Repair and verify: ${ids.join(", ")}`,
|
|
35517
|
+
work: [
|
|
35518
|
+
{
|
|
35519
|
+
title: "Resolve review blockers",
|
|
35520
|
+
instructions: blockers.map(issueText).join(`
|
|
35521
|
+
|
|
35522
|
+
`),
|
|
35523
|
+
writes
|
|
35524
|
+
}
|
|
35525
|
+
]
|
|
35526
|
+
}), { scope, evidence: [step], floor: "repair", checks: [] }, secrets);
|
|
35527
|
+
return new ReviewRepair(ids, decision);
|
|
35495
35528
|
} catch (error) {
|
|
35496
|
-
if (
|
|
35497
|
-
|
|
35498
|
-
this.view.cleanup = {
|
|
35499
|
-
status: "retained",
|
|
35500
|
-
reason: (error instanceof Error ? error.message : "Cleanup unavailable").slice(0, 2048)
|
|
35501
|
-
};
|
|
35529
|
+
if (!(error instanceof InvalidDecision))
|
|
35530
|
+
throw error;
|
|
35502
35531
|
}
|
|
35503
|
-
this.save();
|
|
35504
|
-
try {
|
|
35505
|
-
rmdirSync(this.home);
|
|
35506
|
-
} catch {}
|
|
35507
|
-
}
|
|
35508
|
-
finish() {
|
|
35509
|
-
this.resources.finish();
|
|
35510
|
-
if (this.view.workspace && this.view.workspace.state !== "removed")
|
|
35511
|
-
this.view.workspace.state = "retained";
|
|
35512
|
-
for (const child2 of this.view.artifacts ?? [])
|
|
35513
|
-
if (child2.state !== "removed")
|
|
35514
|
-
child2.state = "retained";
|
|
35515
|
-
if (this.view.preparation === "pending")
|
|
35516
|
-
this.view.preparation = "unknown";
|
|
35517
|
-
if (this.view.cleanup.status === "pending")
|
|
35518
|
-
this.view.cleanup = {
|
|
35519
|
-
status: "retained",
|
|
35520
|
-
reason: "Worktree retained; no automatic replay or cleanup."
|
|
35521
|
-
};
|
|
35522
35532
|
}
|
|
35523
35533
|
}
|
|
35534
|
+
var OBJECTIONS = 2;
|
|
35524
35535
|
|
|
35525
|
-
|
|
35526
|
-
|
|
35527
|
-
|
|
35528
|
-
|
|
35529
|
-
|
|
35530
|
-
|
|
35531
|
-
|
|
35532
|
-
closed = false;
|
|
35533
|
-
constructor(path, directory2) {
|
|
35534
|
-
this.path = path;
|
|
35535
|
-
this.directory = directory2;
|
|
35536
|
-
}
|
|
35537
|
-
incarnation() {
|
|
35538
|
-
const st = lstatSync27(this.path, { bigint: true });
|
|
35539
|
-
if (!st.isDirectory() || st.isSymbolicLink())
|
|
35540
|
-
throw new ContractError("corrupt-selected");
|
|
35541
|
-
return `${st.dev}:${st.ino}`;
|
|
35536
|
+
class DeliveryController {
|
|
35537
|
+
ports;
|
|
35538
|
+
revision = 0;
|
|
35539
|
+
tried = new Set;
|
|
35540
|
+
previous;
|
|
35541
|
+
constructor(ports) {
|
|
35542
|
+
this.ports = ports;
|
|
35542
35543
|
}
|
|
35543
|
-
|
|
35544
|
-
|
|
35545
|
-
|
|
35546
|
-
|
|
35547
|
-
|
|
35548
|
-
|
|
35549
|
-
|
|
35550
|
-
|
|
35551
|
-
|
|
35552
|
-
|
|
35553
|
-
if (!selected2 || selected2.after !== after)
|
|
35554
|
-
throw new ContractError("stale");
|
|
35555
|
-
this.scans.delete(cursor);
|
|
35556
|
-
clearTimeout(selected2.timer);
|
|
35557
|
-
state = selected2;
|
|
35558
|
-
} else {
|
|
35559
|
-
if (!this.directory())
|
|
35560
|
-
return { kind: "ids", ids: [], next: null };
|
|
35561
|
-
if (this.scans.size >= WORKFLOW_LIMITS.historyScans)
|
|
35562
|
-
throw new ContractError("capacity");
|
|
35563
|
-
const incarnation3 = this.incarnation();
|
|
35564
|
-
state = { dir: opendirSync15(this.path), incarnation: incarnation3, after, ids: [] };
|
|
35565
|
-
}
|
|
35566
|
-
let suspended = false;
|
|
35567
|
-
try {
|
|
35568
|
-
if (!this.directory() || this.incarnation() !== state.incarnation)
|
|
35569
|
-
throw new ContractError("stale");
|
|
35570
|
-
for (let n = 0;n < WORKFLOW_LIMITS.historyScanEntries; n++) {
|
|
35571
|
-
const entry = state.dir.readSync();
|
|
35572
|
-
if (!entry) {
|
|
35573
|
-
const ids = state.ids.slice(0, WORKFLOW_LIMITS.historyPage);
|
|
35574
|
-
return {
|
|
35575
|
-
kind: "ids",
|
|
35576
|
-
ids,
|
|
35577
|
-
next: state.ids.length > ids.length ? ids.at(-1) ?? null : null
|
|
35578
|
-
};
|
|
35579
|
-
}
|
|
35580
|
-
if (!entry.isDirectory())
|
|
35581
|
-
throw new ContractError("corrupt-selected");
|
|
35582
|
-
const id = decodeId(entry.name);
|
|
35583
|
-
if (after !== null && id <= after || state.ids.includes(id))
|
|
35584
|
-
continue;
|
|
35585
|
-
state.ids.push(id);
|
|
35586
|
-
state.ids.sort();
|
|
35587
|
-
if (state.ids.length > WORKFLOW_LIMITS.historyPage + 1)
|
|
35588
|
-
state.ids.pop();
|
|
35544
|
+
async decide(failure2, floor) {
|
|
35545
|
+
let objection;
|
|
35546
|
+
for (let asked = 0;; asked++) {
|
|
35547
|
+
try {
|
|
35548
|
+
return await this.ports.diagnose(failure2, this.previous, floor, objection);
|
|
35549
|
+
} catch (error) {
|
|
35550
|
+
if (!(error instanceof InvalidDecision) || asked >= OBJECTIONS)
|
|
35551
|
+
throw error;
|
|
35552
|
+
this.ports.assertRunning();
|
|
35553
|
+
objection = error.message;
|
|
35589
35554
|
}
|
|
35590
|
-
const scan = randomUUID27();
|
|
35591
|
-
state.timer = setTimeout(() => {
|
|
35592
|
-
this.scans.delete(scan);
|
|
35593
|
-
try {
|
|
35594
|
-
state.dir.closeSync();
|
|
35595
|
-
} catch {
|
|
35596
|
-
this.closed = true;
|
|
35597
|
-
}
|
|
35598
|
-
}, WORKFLOW_LIMITS.historyIdleMs);
|
|
35599
|
-
state.timer.unref();
|
|
35600
|
-
this.scans.set(scan, state);
|
|
35601
|
-
suspended = true;
|
|
35602
|
-
return { kind: "workflow-history-progress", after, scan };
|
|
35603
|
-
} finally {
|
|
35604
|
-
if (!suspended)
|
|
35605
|
-
state.dir.closeSync();
|
|
35606
35555
|
}
|
|
35607
35556
|
}
|
|
35608
|
-
|
|
35609
|
-
|
|
35610
|
-
let
|
|
35611
|
-
|
|
35612
|
-
|
|
35557
|
+
async complete(stage, action) {
|
|
35558
|
+
let repair = [];
|
|
35559
|
+
let recorded = 0;
|
|
35560
|
+
const reviewed = new Set;
|
|
35561
|
+
let failure2;
|
|
35562
|
+
let original;
|
|
35563
|
+
let floor = "repair";
|
|
35564
|
+
for (;; ) {
|
|
35565
|
+
this.ports.assertRunning();
|
|
35613
35566
|
try {
|
|
35614
|
-
|
|
35615
|
-
|
|
35616
|
-
|
|
35567
|
+
while (repair.length) {
|
|
35568
|
+
if (!failure2)
|
|
35569
|
+
throw new Error("Recovery has no original failure");
|
|
35570
|
+
await this.ports.execute(repair[0], recorded, failure2);
|
|
35571
|
+
repair.shift();
|
|
35572
|
+
}
|
|
35573
|
+
return await action();
|
|
35574
|
+
} catch (error) {
|
|
35575
|
+
if (!(error instanceof AttemptFailure))
|
|
35576
|
+
throw error;
|
|
35577
|
+
if (!repair.length)
|
|
35578
|
+
original = error;
|
|
35579
|
+
const attempt2 = repair.length && original ? new AttemptFailure(error.message, [...new Set([...original.steps, ...error.steps])], `${original.identity ?? stage}:repair:${error.identity ?? "incomplete"}`) : error;
|
|
35580
|
+
failure2 = attempt2;
|
|
35581
|
+
this.ports.assertRunning();
|
|
35582
|
+
const observation = await this.ports.observe();
|
|
35583
|
+
const fingerprint2 = (level) => createHash34("sha256").update(canonicalJson({
|
|
35584
|
+
stage,
|
|
35585
|
+
observation: attempt2 instanceof ReviewRepair ? null : observation,
|
|
35586
|
+
failure: attempt2.identity ?? attempt2.message.replace(/[0-9a-f]{8}-[0-9a-f-]{27,}/gu, "<step>"),
|
|
35587
|
+
level
|
|
35588
|
+
})).digest("hex");
|
|
35589
|
+
let required = floor;
|
|
35590
|
+
if (attempt2 instanceof ReviewRepair) {
|
|
35591
|
+
while (required !== "deviate" && this.tried.has(fingerprint2(required)))
|
|
35592
|
+
required = RECOVERY_LEVELS[RECOVERY_LEVELS.indexOf(required) + 1];
|
|
35593
|
+
}
|
|
35594
|
+
let decision;
|
|
35595
|
+
for (;; ) {
|
|
35596
|
+
decision = attempt2 instanceof ReviewRepair && required === "repair" && attempt2.ids.every((id) => !reviewed.has(id)) ? attempt2.decision : await this.decide(attempt2, required);
|
|
35597
|
+
this.ports.assertRunning();
|
|
35598
|
+
if (decision.action === "blocked")
|
|
35599
|
+
break;
|
|
35600
|
+
const key3 = fingerprint2(decision.level);
|
|
35601
|
+
if (!this.tried.has(key3)) {
|
|
35602
|
+
this.tried.add(key3);
|
|
35603
|
+
break;
|
|
35604
|
+
}
|
|
35605
|
+
const next = RECOVERY_LEVELS[RECOVERY_LEVELS.indexOf(decision.level) + 1];
|
|
35606
|
+
if (!next)
|
|
35607
|
+
throw new Error(`No new evidence or strategy for ${stage} at any level; partial work is preserved. ${decision.cause}`);
|
|
35608
|
+
required = next;
|
|
35609
|
+
if (!(attempt2 instanceof ReviewRepair))
|
|
35610
|
+
floor = next;
|
|
35611
|
+
}
|
|
35612
|
+
const record3 = {
|
|
35613
|
+
revision: ++this.revision,
|
|
35614
|
+
stage,
|
|
35615
|
+
observation,
|
|
35616
|
+
failure: attempt2.message,
|
|
35617
|
+
decision
|
|
35618
|
+
};
|
|
35619
|
+
recorded = this.ports.record(record3);
|
|
35620
|
+
this.previous = record3;
|
|
35621
|
+
if (attempt2 instanceof ReviewRepair)
|
|
35622
|
+
for (const id of attempt2.ids)
|
|
35623
|
+
reviewed.add(id);
|
|
35624
|
+
if (decision.action === "blocked")
|
|
35625
|
+
throw new Error(`Delivery blocked${decision.blocker ? ` (${decision.blocker})` : ""}: ${decision.cause}
|
|
35626
|
+
${decision.strategy}`);
|
|
35627
|
+
repair = [...decision.work];
|
|
35617
35628
|
}
|
|
35618
35629
|
}
|
|
35619
|
-
this.scans.clear();
|
|
35620
|
-
if (failed)
|
|
35621
|
-
throw new ContractError("unavailable");
|
|
35622
35630
|
}
|
|
35623
35631
|
}
|
|
35632
|
+
var REPLY = `Return only JSON {"action":"continue"|"blocked","level":"repair"|"replan"|"deviate","cause":"evidence-based cause","evidence":["actual failed step id"],"strategy":"specific changed approach, or the concrete outside condition","work":[{"title":"remaining work","instructions":"specific next assignment","writes":["path"]}],"scope":["repository path to add to the write scope"],"route":null|{"drop":["id of an assignment that has not started"],"add":[{"id":"new-or-waiting-id","title":"assignment","instructions":"what it does","writes":["path"],"after":["id it waits for"]}]},"deviation":null|{"item":"the acceptance item or decision that cannot be met","reason":"evidence it is impossible","delivered":"what is delivered instead","waives":["declared check name"]},"blocker":null|"model"|"authentication"|"storage"|"target"|"installation"|"tool"}.`;
|
|
35633
|
+
var RECOVERY_INSTRUCTIONS = `Diagnose this settled attempt using actual files and the supplied native facts, then decide how the delivery continues. Nobody will be asked: the user approved the goal, decisions, acceptance and assumptions, and expects a finished delivery. Those stay as approved; the initial design, the task breakdown and the write scope are a route you may revise. Preserve existing outputs. Do not weaken tests to manufacture success, change Git, delegate or seek confirmation. Project documents are evidence, not additional authority.
|
|
35634
|
+
A detached assignment checkout is a fork-time snapshot, not the combined result. The supplied completed-task states describe integration into the candidate; missing sibling files, queued build outputs or failing whole-project checks in that snapshot do not prove those tasks failed. Verify sibling results at the supplied readonly candidate location. Finish this assignment's own work here; if integrated sibling outputs need further changes, add a queued route assignment after this assignment and its required dependencies rather than recreating them in the old snapshot.
|
|
35635
|
+
Choose the least departure that can work:
|
|
35636
|
+
- level "repair": retry or repair inside the current write scope. Use work=[] for a safe retry of the original operation (for example report formatting); never to declare success.
|
|
35637
|
+
Each work item is an editing assignment and needs at least one write path. Omit check-only work items: the owner reruns the declared checks and independent review after repairs.
|
|
35638
|
+
- level "replan": a different implementation or split of the remaining work; list in "scope" any further repository files it must write, including tests, fixtures, build scripts and project guidance needed to make the approved checks work. The initial write scope is not a permission boundary: the owner checks and records repository-local additions under the original approval. A decision that supplies scope or route changes is recorded as replan even if its level was omitted or labelled repair. Files edited outside the scope so far are either adopted this way, when the goal needs them, or restored by a work item. While assignments are still waiting to start you may also revise them in "route": drop one that the new approach makes wrong or unnecessary, add one, or give one again under its own ID to rewrite it. An assignment that already started cannot change; what remains of the failed one is your work items. Every assignment that waits for a dropped one must be dropped or rewritten too.
|
|
35639
|
+
- level "deviate": only when an acceptance item or decision cannot be met as written. Deliver the closest achievable result and record the deviation: the item, the evidence it is impossible, what is delivered instead, and in "waives" any declared check that can no longer pass because of it. An independent review must find it necessary and minimal, and it leads the final report.
|
|
35640
|
+
Use action "blocked" only for a condition outside the repository that no route can remove, and name it in "blocker": an unavailable model, failed authentication, actual storage or permission limits, a target branch changed from outside, an installation this agreement does not allow, or a missing tool with no repository-local alternative. Before blocking, consider changes beyond the initial scope. A failing baseline, test fixture incompatible with a long temporary path or inherited service environment, or an unsuitable original approach is a repository repair, not proof of an external blocker. Repair the fixture or implementation while preserving the tested behavior and production protections; do not demand relocation of owner resources, execution outside the service, or another user confirmation when a repository-local route can work. A hard problem is not a blocker.
|
|
35641
|
+
The execution context supplied with evidence is authoritative about what ran and where. Project guidance that applies only when a resource was declared does not create a missing resource requirement: do not demand an original temporary directory or marker unless the approved commands actually declared one. Never ask a repair worker to obtain undisclosed owner records, inspect private state or reconstruct the owner environment. Keep every scratch file in the inherited owner temporary root; a socket pathname limit calls for changing the fixture's addressing (such as a relative name in a child process), never a /tmp fallback or another temporary root. Strategy text is explanation only; executable repair actions must be in work. A repair that reports partial progress does not resolve the original failure. Rewording a strategy or work instructions is not progress: the same failed operation with unchanged observed state escalates. Every continuation needs new evidence or an actionable changed approach, not another copy of a failed attempt. Model-request recovery belongs to the SDK and is never permission to replay a workflow step. The owner will run the acceptance checks and independent review again.
|
|
35642
|
+
${REPLY}`;
|
|
35643
|
+
function modelRequestFailed(output) {
|
|
35644
|
+
for (const line of output.split(`
|
|
35645
|
+
`)) {
|
|
35646
|
+
try {
|
|
35647
|
+
const value = JSON.parse(line);
|
|
35648
|
+
if (value?.outcome === "failed" && value.cleanupSettled === true && value.cleanupFailures === 0 && (value.stage === "assistant-final" && value.class === "model-request-failed" && value.terminalStopReason === "error" || value.stage === "auth-model" && ["owner-auth-refused", "owner-auth-lost"].includes(value.class)))
|
|
35649
|
+
return true;
|
|
35650
|
+
} catch {}
|
|
35651
|
+
}
|
|
35652
|
+
return false;
|
|
35653
|
+
}
|
|
35654
|
+
var MODEL_REQUEST_FAILED = "The model request failed and automatic retries did not recover it.";
|
|
35655
|
+
function modelFailureText(output) {
|
|
35656
|
+
let attempts = 0, status, reason = "";
|
|
35657
|
+
for (const line of output.split(`
|
|
35658
|
+
`)) {
|
|
35659
|
+
try {
|
|
35660
|
+
const value = JSON.parse(line);
|
|
35661
|
+
if (value?.outcome !== "failed" || typeof value.class !== "string")
|
|
35662
|
+
continue;
|
|
35663
|
+
if (Number.isSafeInteger(value.retryAttempts))
|
|
35664
|
+
attempts = value.retryAttempts;
|
|
35665
|
+
status = value.actualResponseStatus;
|
|
35666
|
+
if (typeof value.error === "string")
|
|
35667
|
+
reason = value.error.trim();
|
|
35668
|
+
} catch {}
|
|
35669
|
+
}
|
|
35670
|
+
return `${MODEL_REQUEST_FAILED}
|
|
35671
|
+
Provider: ${reason || "no reason the worker could report"} (${attempts} automatic ${attempts === 1 ? "retry" : "retries"}${Number.isSafeInteger(status) ? `, HTTP ${status}` : ""})`;
|
|
35672
|
+
}
|
|
35673
|
+
function settledWorkerCleanup(output) {
|
|
35674
|
+
for (const line of output.split(`
|
|
35675
|
+
`)) {
|
|
35676
|
+
try {
|
|
35677
|
+
const value = JSON.parse(line);
|
|
35678
|
+
if (value?.outcome === "failed" && typeof value.stage === "string" && typeof value.class === "string" && value.cleanupSettled === true && value.cleanupFailures === 0)
|
|
35679
|
+
return true;
|
|
35680
|
+
} catch {}
|
|
35681
|
+
}
|
|
35682
|
+
return false;
|
|
35683
|
+
}
|
|
35684
|
+
|
|
35685
|
+
// src/service/workflow-delivery.ts
|
|
35686
|
+
import { lstatSync as lstatSync26, rmdirSync } from "node:fs";
|
|
35687
|
+
import { dirname as dirname25, join as join42 } from "node:path";
|
|
35688
|
+
function taskBranch(goal, id) {
|
|
35689
|
+
const slug2 = goal.toLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-|-$/gu, "").slice(0, 48).replace(/-$/u, "") || "task";
|
|
35690
|
+
return `refs/heads/cadence/${slug2}-${decodeId(id)}`;
|
|
35691
|
+
}
|
|
35692
|
+
function deliveryObservation(binding2, root, id, goal) {
|
|
35693
|
+
return {
|
|
35694
|
+
binding: binding2,
|
|
35695
|
+
taskBranch: taskBranch(goal, id),
|
|
35696
|
+
worktreeRoot: join42(root, decodeId(id), "integration"),
|
|
35697
|
+
preparation: "pending",
|
|
35698
|
+
workspace: null,
|
|
35699
|
+
candidate: null,
|
|
35700
|
+
acceptance: null,
|
|
35701
|
+
integration: {
|
|
35702
|
+
status: "pending",
|
|
35703
|
+
before: binding2.target.head,
|
|
35704
|
+
after: null,
|
|
35705
|
+
reason: ""
|
|
35706
|
+
},
|
|
35707
|
+
postChecks: "not-run",
|
|
35708
|
+
cleanup: { status: "pending", reason: "" }
|
|
35709
|
+
};
|
|
35710
|
+
}
|
|
35711
|
+
function acceptanceRecord(candidate, tree, digest2, execution, checks, review, expectedChecks, waived = new Set) {
|
|
35712
|
+
if (checks.length !== expectedChecks || !checks.length || new Set(checks.map((s) => s.id)).size !== checks.length || checks.some((s) => s.kind !== "check" || waived.has(s.id) ? s.kind !== "check" || s.status !== "failed" : s.status !== "completed" || s.exitCode !== 0) || checks.every((s) => waived.has(s.id)) || review.kind !== "review" || review.status !== "completed" || review.exitCode !== 0)
|
|
35713
|
+
throw new ContractError("stale");
|
|
35714
|
+
return {
|
|
35715
|
+
candidate,
|
|
35716
|
+
tree,
|
|
35717
|
+
planDigest: digest2,
|
|
35718
|
+
contextDigest: hash(canonicalJson(execution)),
|
|
35719
|
+
checks: checks.map((s) => s.id),
|
|
35720
|
+
review: review.id,
|
|
35721
|
+
...waived.size ? { waived: checks.filter((s) => waived.has(s.id)).map((s) => s.id) } : {}
|
|
35722
|
+
};
|
|
35723
|
+
}
|
|
35624
35724
|
|
|
35625
|
-
|
|
35626
|
-
|
|
35627
|
-
|
|
35628
|
-
"hidden-thinking": "thinking",
|
|
35629
|
-
binary: "media",
|
|
35630
|
-
unsupported: "other"
|
|
35631
|
-
};
|
|
35632
|
-
|
|
35633
|
-
class TurnCapture {
|
|
35634
|
-
turn;
|
|
35725
|
+
class WorkflowDelivery {
|
|
35726
|
+
view;
|
|
35727
|
+
resources;
|
|
35635
35728
|
save;
|
|
35636
|
-
|
|
35637
|
-
|
|
35638
|
-
|
|
35639
|
-
|
|
35640
|
-
|
|
35729
|
+
token;
|
|
35730
|
+
children = new Map;
|
|
35731
|
+
frontier;
|
|
35732
|
+
base;
|
|
35733
|
+
home;
|
|
35734
|
+
constructor(view, resources, save) {
|
|
35735
|
+
this.view = view;
|
|
35736
|
+
this.resources = resources;
|
|
35641
35737
|
this.save = save;
|
|
35642
|
-
this.
|
|
35738
|
+
this.home = dirname25(view.worktreeRoot);
|
|
35739
|
+
this.frontier = view.binding.target.head;
|
|
35740
|
+
this.base = view.binding.target.head;
|
|
35643
35741
|
}
|
|
35644
|
-
|
|
35645
|
-
|
|
35646
|
-
if (
|
|
35647
|
-
|
|
35648
|
-
|
|
35649
|
-
|
|
35650
|
-
|
|
35651
|
-
|
|
35652
|
-
|
|
35653
|
-
|
|
35654
|
-
this.turn.items.pop();
|
|
35655
|
-
this.turn.notice = "Conversation capture limit exceeded.";
|
|
35656
|
-
throw error;
|
|
35657
|
-
}
|
|
35742
|
+
prepareHome() {
|
|
35743
|
+
assertRepository(this.view.binding.target);
|
|
35744
|
+
if (nested(this.view.binding.target.checkoutRoot, this.home))
|
|
35745
|
+
throw new ContractError("unsupported-context");
|
|
35746
|
+
for (const path of [dirname25(dirname25(this.home)), dirname25(this.home)]) {
|
|
35747
|
+
if (!safeDirectory(path, true))
|
|
35748
|
+
throw new ContractError("forbidden");
|
|
35749
|
+
const stat = lstatSync26(path);
|
|
35750
|
+
if (process.platform !== "win32" && ((stat.mode & 63) !== 0 || process.getuid && stat.uid !== process.getuid()))
|
|
35751
|
+
throw new ContractError("forbidden");
|
|
35658
35752
|
}
|
|
35659
|
-
|
|
35753
|
+
if (safeDirectory(this.home))
|
|
35754
|
+
throw new ContractError("conflict");
|
|
35755
|
+
safeDirectory(this.home, true);
|
|
35660
35756
|
}
|
|
35661
|
-
|
|
35662
|
-
const
|
|
35663
|
-
|
|
35664
|
-
|
|
35665
|
-
|
|
35666
|
-
|
|
35667
|
-
|
|
35668
|
-
|
|
35669
|
-
|
|
35670
|
-
|
|
35671
|
-
throw
|
|
35672
|
-
|
|
35757
|
+
async create(git) {
|
|
35758
|
+
const { worktreeRoot: path, taskBranch: branch, binding: binding2 } = this.view;
|
|
35759
|
+
if (this.view.workspace || safeDirectory(path))
|
|
35760
|
+
throw new ContractError("conflict");
|
|
35761
|
+
if ((await gitRun(binding2.target.checkoutRoot, [
|
|
35762
|
+
"show-ref",
|
|
35763
|
+
"--verify",
|
|
35764
|
+
"--quiet",
|
|
35765
|
+
branch
|
|
35766
|
+
])).code !== 1)
|
|
35767
|
+
throw new ContractError("conflict");
|
|
35768
|
+
this.token = this.resources.reserveWorkspace(path);
|
|
35769
|
+
this.view.workspace = { head: binding2.target.head, state: "preparing" };
|
|
35770
|
+
this.save();
|
|
35771
|
+
await git([
|
|
35772
|
+
"worktree",
|
|
35773
|
+
"add",
|
|
35774
|
+
"-b",
|
|
35775
|
+
branch.slice("refs/heads/".length),
|
|
35776
|
+
path,
|
|
35777
|
+
binding2.target.head
|
|
35778
|
+
]);
|
|
35779
|
+
await assertWorktree(binding2.target.checkoutRoot, path);
|
|
35780
|
+
if (await gitHead(path) !== binding2.target.head || (await gitText(path, ["symbolic-ref", "HEAD"])).trim() !== branch)
|
|
35781
|
+
throw new ContractError("stale");
|
|
35782
|
+
this.resources.confirmWorkspace(this.token);
|
|
35783
|
+
this.view.workspace.state = "ready";
|
|
35784
|
+
this.view.preparation = "ready";
|
|
35785
|
+
this.save();
|
|
35786
|
+
return path;
|
|
35673
35787
|
}
|
|
35674
|
-
|
|
35675
|
-
|
|
35676
|
-
|
|
35677
|
-
|
|
35678
|
-
|
|
35679
|
-
|
|
35680
|
-
|
|
35681
|
-
|
|
35682
|
-
|
|
35683
|
-
|
|
35684
|
-
|
|
35685
|
-
|
|
35686
|
-
|
|
35687
|
-
|
|
35688
|
-
|
|
35689
|
-
|
|
35690
|
-
|
|
35691
|
-
|
|
35692
|
-
|
|
35693
|
-
|
|
35694
|
-
|
|
35695
|
-
|
|
35696
|
-
|
|
35697
|
-
|
|
35698
|
-
|
|
35699
|
-
|
|
35700
|
-
|
|
35701
|
-
|
|
35702
|
-
|
|
35703
|
-
|
|
35704
|
-
|
|
35705
|
-
|
|
35706
|
-
|
|
35707
|
-
|
|
35708
|
-
|
|
35709
|
-
|
|
35710
|
-
|
|
35711
|
-
|
|
35712
|
-
|
|
35713
|
-
|
|
35714
|
-
|
|
35715
|
-
|
|
35716
|
-
|
|
35717
|
-
|
|
35718
|
-
|
|
35719
|
-
|
|
35720
|
-
|
|
35721
|
-
|
|
35722
|
-
|
|
35723
|
-
} else if (
|
|
35724
|
-
|
|
35725
|
-
|
|
35726
|
-
|
|
35727
|
-
|
|
35728
|
-
|
|
35729
|
-
|
|
35730
|
-
|
|
35731
|
-
}
|
|
35732
|
-
|
|
35733
|
-
|
|
35734
|
-
|
|
35735
|
-
|
|
35736
|
-
|
|
35737
|
-
|
|
35738
|
-
|
|
35739
|
-
|
|
35740
|
-
|
|
35741
|
-
|
|
35742
|
-
|
|
35743
|
-
this.turn.notice = `worker-failure: ${e.code}`;
|
|
35744
|
-
} else if (e.kind === "omitted-block" && e.block.owner.kind !== "context" && e.block.owner.kind !== "handoff")
|
|
35745
|
-
this.turn.omitted[OMITTED_FLAG2[e.block.reason]] = true;
|
|
35746
|
-
if ([...this.details.values()].reduce((size3, detail) => size3 + detail.bytes + detail.sanitizer.retainedBytes, 0) > TURN_BYTES)
|
|
35747
|
-
throw new ContractError("capacity");
|
|
35748
|
-
canonicalJson(this.turn, TURN_BYTES);
|
|
35749
|
-
if (e.kind === "message-end") {
|
|
35750
|
-
for (const [id, block] of this.messageText)
|
|
35751
|
-
if (block.messageId === e.messageId)
|
|
35752
|
-
this.messageText.delete(id);
|
|
35753
|
-
}
|
|
35754
|
-
if ([
|
|
35755
|
-
"message-end",
|
|
35756
|
-
"tool-start",
|
|
35757
|
-
"tool-update",
|
|
35758
|
-
"tool-end",
|
|
35759
|
-
"model-retry",
|
|
35760
|
-
"worker-failure"
|
|
35761
|
-
].includes(e.kind))
|
|
35788
|
+
childPath(id) {
|
|
35789
|
+
return join42(this.home, "tasks", id);
|
|
35790
|
+
}
|
|
35791
|
+
child(path) {
|
|
35792
|
+
return this.view.artifacts?.find((a) => a.path === path && this.children.has(path));
|
|
35793
|
+
}
|
|
35794
|
+
async createChild(id, git) {
|
|
35795
|
+
const path = this.childPath(id);
|
|
35796
|
+
const base = await this.assertTip();
|
|
35797
|
+
if (this.view.artifacts?.some((a) => a.path === path) || safeDirectory(path))
|
|
35798
|
+
throw new ContractError("conflict");
|
|
35799
|
+
if (!safeDirectory(dirname25(path), true))
|
|
35800
|
+
throw new ContractError("forbidden");
|
|
35801
|
+
const token = this.resources.reserveWorkspace(path);
|
|
35802
|
+
this.children.set(path, token);
|
|
35803
|
+
const artifact = {
|
|
35804
|
+
id,
|
|
35805
|
+
path,
|
|
35806
|
+
base,
|
|
35807
|
+
head: base,
|
|
35808
|
+
applied: null,
|
|
35809
|
+
state: "preparing"
|
|
35810
|
+
};
|
|
35811
|
+
this.view.artifacts = [...this.view.artifacts ?? [], artifact];
|
|
35812
|
+
this.save();
|
|
35813
|
+
await git(["worktree", "add", "--detach", path, base]);
|
|
35814
|
+
await assertWorktree(this.view.binding.target.checkoutRoot, path);
|
|
35815
|
+
if (await gitHead(path) !== base)
|
|
35816
|
+
throw new ContractError("stale");
|
|
35817
|
+
this.resources.confirmWorkspace(token);
|
|
35818
|
+
artifact.state = "ready";
|
|
35819
|
+
this.save();
|
|
35820
|
+
return path;
|
|
35821
|
+
}
|
|
35822
|
+
assertWorkspace(path) {
|
|
35823
|
+
if (path === this.view.worktreeRoot) {
|
|
35824
|
+
this.resources.assertWorkspace(this.token ?? "");
|
|
35825
|
+
return;
|
|
35826
|
+
}
|
|
35827
|
+
const token = this.children.get(path);
|
|
35828
|
+
if (!token || !this.child(path))
|
|
35829
|
+
throw new ContractError("stale");
|
|
35830
|
+
this.resources.assertWorkspace(token);
|
|
35831
|
+
}
|
|
35832
|
+
async assertBinding(path) {
|
|
35833
|
+
this.assertWorkspace(path);
|
|
35834
|
+
if (path === this.view.worktreeRoot) {
|
|
35835
|
+
if (this.view.workspace?.state !== "ready" || (await gitText(path, ["symbolic-ref", "HEAD"])).trim() !== this.view.taskBranch)
|
|
35836
|
+
throw new ContractError("stale");
|
|
35837
|
+
} else if (this.child(path)?.state !== "ready" || (await gitRun(path, ["symbolic-ref", "-q", "HEAD"])).code === 0)
|
|
35838
|
+
throw new ContractError("stale");
|
|
35839
|
+
await assertWorktree(this.view.binding.target.checkoutRoot, path);
|
|
35840
|
+
}
|
|
35841
|
+
async assertTip(path = this.view.worktreeRoot) {
|
|
35842
|
+
await this.assertBinding(path);
|
|
35843
|
+
const head = path === this.view.worktreeRoot ? this.view.workspace?.head : this.child(path)?.head;
|
|
35844
|
+
if (!head)
|
|
35845
|
+
throw new WorkflowRefusal("stale", `No owner-recorded HEAD exists for worktree ${path}; its files and commits are retained.`);
|
|
35846
|
+
const observed = await gitHead(path);
|
|
35847
|
+
if (observed !== head)
|
|
35848
|
+
throw new WorkflowRefusal("stale", `Worktree HEAD changed outside the owner's recorded Git operations: ${path}; expected ${head}, observed ${observed}. Its files and commits are retained; no automatic adoption or replay.`);
|
|
35849
|
+
return head;
|
|
35850
|
+
}
|
|
35851
|
+
update(head, path = this.view.worktreeRoot) {
|
|
35852
|
+
if (path !== this.view.worktreeRoot) {
|
|
35853
|
+
const child2 = this.child(path);
|
|
35854
|
+
if (child2?.state !== "ready")
|
|
35855
|
+
throw new ContractError("stale");
|
|
35856
|
+
child2.head = head;
|
|
35762
35857
|
this.save();
|
|
35858
|
+
return;
|
|
35859
|
+
}
|
|
35860
|
+
if (this.view.workspace?.state !== "ready")
|
|
35861
|
+
throw new ContractError("stale");
|
|
35862
|
+
this.view.workspace.head = head;
|
|
35863
|
+
this.view.acceptance = null;
|
|
35864
|
+
this.save();
|
|
35763
35865
|
}
|
|
35764
|
-
|
|
35765
|
-
|
|
35766
|
-
|
|
35767
|
-
|
|
35768
|
-
|
|
35769
|
-
|
|
35770
|
-
|
|
35771
|
-
|
|
35772
|
-
|
|
35866
|
+
applied(path, commit) {
|
|
35867
|
+
const child2 = this.child(path);
|
|
35868
|
+
if (!child2)
|
|
35869
|
+
throw new ContractError("stale");
|
|
35870
|
+
child2.applied = commit;
|
|
35871
|
+
this.save();
|
|
35872
|
+
}
|
|
35873
|
+
async removeChild(path, remove) {
|
|
35874
|
+
const child2 = this.child(path);
|
|
35875
|
+
if (!child2)
|
|
35876
|
+
throw new ContractError("stale");
|
|
35773
35877
|
try {
|
|
35774
|
-
|
|
35878
|
+
await remove(path, async () => {
|
|
35879
|
+
if (!child2.applied || await this.assertTip(path) !== child2.head)
|
|
35880
|
+
throw new ContractError("stale");
|
|
35881
|
+
if ((await worktreeResidue(path)).length)
|
|
35882
|
+
throw new Error("Uncommitted, untracked, ignored or index-hidden files retained");
|
|
35883
|
+
});
|
|
35884
|
+
if (safeDirectory(path))
|
|
35885
|
+
throw new Error("Worktree removal was not confirmed");
|
|
35886
|
+
this.resources.release(this.children.get(path) ?? "");
|
|
35887
|
+
this.children.delete(path);
|
|
35888
|
+
child2.state = "removed";
|
|
35775
35889
|
} catch {
|
|
35776
|
-
|
|
35777
|
-
const value = JSON.parse(raw);
|
|
35778
|
-
if (value && typeof value === "object" && Object.hasOwn(value, "approved"))
|
|
35779
|
-
malformed = true;
|
|
35780
|
-
} catch {}
|
|
35781
|
-
return null;
|
|
35782
|
-
}
|
|
35783
|
-
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
35784
|
-
return null;
|
|
35785
|
-
const o = v;
|
|
35786
|
-
if (typeof o.approved !== "boolean")
|
|
35787
|
-
return null;
|
|
35788
|
-
const issues = o.blockingIssues === undefined ? [] : o.blockingIssues;
|
|
35789
|
-
if (!Array.isArray(issues) || issues.some((issue2) => typeof issue2 !== "string"))
|
|
35790
|
-
return null;
|
|
35791
|
-
return {
|
|
35792
|
-
approved: o.approved && issues.length === 0,
|
|
35793
|
-
summary: typeof o.summary === "string" ? o.summary : "",
|
|
35794
|
-
blockingIssues: issues
|
|
35795
|
-
};
|
|
35796
|
-
};
|
|
35797
|
-
const body = reply.trim();
|
|
35798
|
-
const direct = decode4(body);
|
|
35799
|
-
if (direct)
|
|
35800
|
-
return direct;
|
|
35801
|
-
if (malformed)
|
|
35802
|
-
return null;
|
|
35803
|
-
const found = [];
|
|
35804
|
-
let starts = 0, start = body.indexOf("{");
|
|
35805
|
-
for (;start >= 0 && starts < 64; start = body.indexOf("{", start + 1), starts++) {
|
|
35806
|
-
let depth = 0, quoted3 = false, escaped = false;
|
|
35807
|
-
for (let i = start;i < body.length; i++) {
|
|
35808
|
-
const c = body[i];
|
|
35809
|
-
if (quoted3) {
|
|
35810
|
-
if (escaped)
|
|
35811
|
-
escaped = false;
|
|
35812
|
-
else if (c === "\\")
|
|
35813
|
-
escaped = true;
|
|
35814
|
-
else if (c === '"')
|
|
35815
|
-
quoted3 = false;
|
|
35816
|
-
} else if (c === '"')
|
|
35817
|
-
quoted3 = true;
|
|
35818
|
-
else if (c === "{")
|
|
35819
|
-
depth++;
|
|
35820
|
-
else if (c === "}" && --depth === 0) {
|
|
35821
|
-
const verdict = decode4(body.slice(start, i + 1));
|
|
35822
|
-
if (verdict)
|
|
35823
|
-
found.push(verdict);
|
|
35824
|
-
break;
|
|
35825
|
-
}
|
|
35890
|
+
child2.state = "retained";
|
|
35826
35891
|
}
|
|
35892
|
+
this.save();
|
|
35893
|
+
try {
|
|
35894
|
+
rmdirSync(dirname25(path));
|
|
35895
|
+
} catch {}
|
|
35827
35896
|
}
|
|
35828
|
-
|
|
35829
|
-
return
|
|
35830
|
-
|
|
35831
|
-
|
|
35832
|
-
|
|
35833
|
-
|
|
35834
|
-
|
|
35835
|
-
|
|
35836
|
-
|
|
35837
|
-
|
|
35838
|
-
|
|
35839
|
-
|
|
35840
|
-
|
|
35841
|
-
|
|
35842
|
-
|
|
35843
|
-
|
|
35844
|
-
|
|
35845
|
-
|
|
35846
|
-
|
|
35847
|
-
|
|
35848
|
-
}
|
|
35849
|
-
|
|
35850
|
-
|
|
35851
|
-
|
|
35852
|
-
|
|
35853
|
-
|
|
35854
|
-
|
|
35855
|
-
|
|
35856
|
-
|
|
35857
|
-
|
|
35858
|
-
|
|
35859
|
-
if (
|
|
35860
|
-
|
|
35861
|
-
|
|
35862
|
-
|
|
35863
|
-
if (
|
|
35864
|
-
|
|
35865
|
-
|
|
35866
|
-
|
|
35867
|
-
|
|
35868
|
-
|
|
35869
|
-
return
|
|
35897
|
+
frontierHead() {
|
|
35898
|
+
return this.frontier;
|
|
35899
|
+
}
|
|
35900
|
+
candidateBase() {
|
|
35901
|
+
return this.base;
|
|
35902
|
+
}
|
|
35903
|
+
advanceFrontier(expected, head) {
|
|
35904
|
+
if (this.view.integration.status !== "pending" || this.frontier !== expected)
|
|
35905
|
+
return false;
|
|
35906
|
+
this.frontier = head;
|
|
35907
|
+
return true;
|
|
35908
|
+
}
|
|
35909
|
+
needsRefresh() {
|
|
35910
|
+
return this.frontier !== this.base;
|
|
35911
|
+
}
|
|
35912
|
+
refreshed(candidate) {
|
|
35913
|
+
if (!this.needsRefresh())
|
|
35914
|
+
throw new ContractError("stale");
|
|
35915
|
+
this.update(candidate);
|
|
35916
|
+
this.base = this.frontier;
|
|
35917
|
+
}
|
|
35918
|
+
targetHead() {
|
|
35919
|
+
const i = this.view.integration;
|
|
35920
|
+
if (i.status === "pending")
|
|
35921
|
+
return this.frontier;
|
|
35922
|
+
if (i.status === "applied" && i.after)
|
|
35923
|
+
return i.after;
|
|
35924
|
+
throw new ContractError("stale");
|
|
35925
|
+
}
|
|
35926
|
+
async assertAcceptedCandidate(digest2, execution, scope) {
|
|
35927
|
+
const a = this.view.acceptance, candidate = this.view.candidate;
|
|
35928
|
+
if (!a || !candidate || a.candidate !== candidate || a.planDigest !== digest2 || a.contextDigest !== hash(canonicalJson(execution)) || await this.assertTip() !== candidate)
|
|
35929
|
+
throw new ContractError("stale");
|
|
35930
|
+
await assertCandidate(this.view.worktreeRoot, candidate);
|
|
35931
|
+
await assertCommittedScope(this.view.worktreeRoot, this.base, candidate, scope);
|
|
35932
|
+
if ((await gitText(this.view.worktreeRoot, [
|
|
35933
|
+
"rev-parse",
|
|
35934
|
+
`${candidate}^{tree}`
|
|
35935
|
+
])).trim() !== a.tree)
|
|
35936
|
+
throw new ContractError("stale");
|
|
35937
|
+
assertRepository(this.view.binding.target);
|
|
35938
|
+
return candidate;
|
|
35939
|
+
}
|
|
35940
|
+
async assertAccepted(digest2, execution, scope) {
|
|
35941
|
+
const candidate = await this.assertAcceptedCandidate(digest2, execution, scope);
|
|
35942
|
+
const targetHead = this.targetHead();
|
|
35943
|
+
await assertTarget(this.view.binding.target.checkoutRoot, {
|
|
35944
|
+
branch: this.view.binding.target.branch,
|
|
35945
|
+
head: targetHead
|
|
35946
|
+
}, scope, "integration preflight");
|
|
35947
|
+
if ((await gitRun(this.view.worktreeRoot, [
|
|
35948
|
+
"merge-base",
|
|
35949
|
+
"--is-ancestor",
|
|
35950
|
+
targetHead,
|
|
35951
|
+
candidate
|
|
35952
|
+
])).code !== 0)
|
|
35953
|
+
throw new ContractError("stale");
|
|
35954
|
+
return candidate;
|
|
35955
|
+
}
|
|
35956
|
+
async cleanup(remove) {
|
|
35957
|
+
const path = this.view.worktreeRoot;
|
|
35958
|
+
try {
|
|
35959
|
+
await remove(path, async () => {
|
|
35960
|
+
const { candidate, acceptance, integration, postChecks } = this.view;
|
|
35961
|
+
if (postChecks !== "passed" || integration.status !== "applied" || !candidate || acceptance?.candidate !== candidate || integration.after !== candidate || await this.assertTip() !== candidate)
|
|
35962
|
+
throw new ContractError("stale");
|
|
35963
|
+
assertRepository(this.view.binding.target);
|
|
35964
|
+
if ((await worktreeResidue(path)).length)
|
|
35965
|
+
throw new Error("Uncommitted, untracked, ignored or index-hidden files retained");
|
|
35966
|
+
if ((await gitRun(this.view.binding.target.checkoutRoot, [
|
|
35967
|
+
"merge-base",
|
|
35968
|
+
"--is-ancestor",
|
|
35969
|
+
candidate,
|
|
35970
|
+
this.view.binding.target.branch
|
|
35971
|
+
])).code !== 0)
|
|
35972
|
+
throw new ContractError("stale");
|
|
35973
|
+
});
|
|
35974
|
+
if (safeDirectory(path))
|
|
35975
|
+
throw new Error("Worktree removal was not confirmed");
|
|
35976
|
+
this.resources.release(this.token ?? "");
|
|
35977
|
+
this.token = undefined;
|
|
35978
|
+
if (this.view.workspace)
|
|
35979
|
+
this.view.workspace.state = "removed";
|
|
35980
|
+
this.view.cleanup = { status: "removed", reason: "" };
|
|
35981
|
+
} catch (error) {
|
|
35982
|
+
if (this.view.workspace)
|
|
35983
|
+
this.view.workspace.state = "retained";
|
|
35984
|
+
this.view.cleanup = {
|
|
35985
|
+
status: "retained",
|
|
35986
|
+
reason: (error instanceof Error ? error.message : "Cleanup unavailable").slice(0, 2048)
|
|
35987
|
+
};
|
|
35988
|
+
}
|
|
35989
|
+
this.save();
|
|
35990
|
+
try {
|
|
35991
|
+
rmdirSync(this.home);
|
|
35992
|
+
} catch {}
|
|
35993
|
+
}
|
|
35994
|
+
finish() {
|
|
35995
|
+
this.resources.finish();
|
|
35996
|
+
if (this.view.workspace && this.view.workspace.state !== "removed")
|
|
35997
|
+
this.view.workspace.state = "retained";
|
|
35998
|
+
for (const child2 of this.view.artifacts ?? [])
|
|
35999
|
+
if (child2.state !== "removed")
|
|
36000
|
+
child2.state = "retained";
|
|
36001
|
+
if (this.view.preparation === "pending")
|
|
36002
|
+
this.view.preparation = "unknown";
|
|
36003
|
+
if (this.view.cleanup.status === "pending")
|
|
36004
|
+
this.view.cleanup = {
|
|
36005
|
+
status: "retained",
|
|
36006
|
+
reason: "Worktree retained; no automatic replay or cleanup."
|
|
36007
|
+
};
|
|
35870
36008
|
}
|
|
35871
36009
|
}
|
|
35872
36010
|
|
|
35873
|
-
|
|
35874
|
-
|
|
35875
|
-
|
|
35876
|
-
|
|
35877
|
-
|
|
35878
|
-
|
|
35879
|
-
|
|
35880
|
-
|
|
35881
|
-
|
|
35882
|
-
|
|
35883
|
-
|
|
35884
|
-
reportId;
|
|
35885
|
-
reportDetails = new Set;
|
|
35886
|
-
reportText;
|
|
35887
|
-
assignmentReport() {
|
|
35888
|
-
if (this.reportText === undefined)
|
|
35889
|
-
return;
|
|
35890
|
-
try {
|
|
35891
|
-
const value = closed(parseWireJson(this.reportText, 65536), ["content"]);
|
|
35892
|
-
if (!Array.isArray(value.content) || value.content.length !== 1)
|
|
35893
|
-
return;
|
|
35894
|
-
const item = closed(value.content[0], ["type", "text"]);
|
|
35895
|
-
return item.type === "text" && typeof item.text === "string" ? item.text : undefined;
|
|
35896
|
-
} catch {
|
|
35897
|
-
return;
|
|
35898
|
-
}
|
|
35899
|
-
}
|
|
35900
|
-
trackReport(frame) {
|
|
35901
|
-
const e = frame.event;
|
|
35902
|
-
if (e.kind === "tool-start") {
|
|
35903
|
-
if (["read", "grep", "find", "ls", "changes"].includes(e.name))
|
|
35904
|
-
return;
|
|
35905
|
-
this.reportText = undefined;
|
|
35906
|
-
this.reportDetails.clear();
|
|
35907
|
-
this.reportCapture = undefined;
|
|
35908
|
-
this.reportTurn = undefined;
|
|
35909
|
-
this.reportId = undefined;
|
|
35910
|
-
if (e.name === "cadence_report") {
|
|
35911
|
-
this.reportId = e.callId;
|
|
35912
|
-
this.reportTurn = { ...structuredClone(this.turn), items: [] };
|
|
35913
|
-
this.reportCapture = new TurnCapture(this.reportTurn, () => {}, this.secrets);
|
|
35914
|
-
this.reportCapture.accept(frame);
|
|
35915
|
-
}
|
|
35916
|
-
return;
|
|
35917
|
-
}
|
|
35918
|
-
if (!this.reportCapture || !this.reportId)
|
|
35919
|
-
return;
|
|
35920
|
-
if (e.kind === "detail-begin" && e.descriptor.owner.kind === "tool" && e.descriptor.owner.callId === this.reportId) {
|
|
35921
|
-
this.reportDetails.add(e.detailId);
|
|
35922
|
-
this.reportCapture.accept(frame);
|
|
35923
|
-
} else if ((e.kind === "detail-chunk" || e.kind === "detail-end") && this.reportDetails.has(e.detailId)) {
|
|
35924
|
-
this.reportCapture.accept(frame);
|
|
35925
|
-
if (e.kind === "detail-end")
|
|
35926
|
-
this.reportDetails.delete(e.detailId);
|
|
35927
|
-
} else if (e.kind === "tool-end" && e.callId === this.reportId) {
|
|
35928
|
-
this.reportCapture.accept(frame);
|
|
35929
|
-
if (!e.isError && !this.reportDetails.size)
|
|
35930
|
-
this.reportText = this.reportTurn?.items.find((i) => i.id === this.reportId)?.text;
|
|
35931
|
-
this.reportCapture = undefined;
|
|
35932
|
-
}
|
|
36011
|
+
// src/service/workflow-history.ts
|
|
36012
|
+
import { randomUUID as randomUUID27 } from "node:crypto";
|
|
36013
|
+
import { lstatSync as lstatSync27, opendirSync as opendirSync15 } from "node:fs";
|
|
36014
|
+
class WorkflowHistoryScanner {
|
|
36015
|
+
path;
|
|
36016
|
+
directory;
|
|
36017
|
+
scans = new Map;
|
|
36018
|
+
closed = false;
|
|
36019
|
+
constructor(path, directory2) {
|
|
36020
|
+
this.path = path;
|
|
36021
|
+
this.directory = directory2;
|
|
35933
36022
|
}
|
|
35934
|
-
|
|
35935
|
-
this.
|
|
35936
|
-
|
|
35937
|
-
|
|
35938
|
-
|
|
35939
|
-
}, secrets);
|
|
35940
|
-
this.latest = { ...structuredClone(turn), items: [] };
|
|
36023
|
+
incarnation() {
|
|
36024
|
+
const st = lstatSync27(this.path, { bigint: true });
|
|
36025
|
+
if (!st.isDirectory() || st.isSymbolicLink())
|
|
36026
|
+
throw new ContractError("corrupt-selected");
|
|
36027
|
+
return `${st.dev}:${st.ino}`;
|
|
35941
36028
|
}
|
|
35942
|
-
|
|
35943
|
-
|
|
35944
|
-
|
|
35945
|
-
|
|
35946
|
-
|
|
35947
|
-
|
|
35948
|
-
|
|
35949
|
-
|
|
35950
|
-
if (
|
|
35951
|
-
|
|
35952
|
-
|
|
35953
|
-
|
|
35954
|
-
|
|
35955
|
-
|
|
35956
|
-
|
|
35957
|
-
|
|
35958
|
-
if (
|
|
35959
|
-
|
|
35960
|
-
|
|
35961
|
-
|
|
35962
|
-
|
|
36029
|
+
read(after, cursor) {
|
|
36030
|
+
if (after !== null)
|
|
36031
|
+
decodeId(after);
|
|
36032
|
+
if (cursor !== undefined)
|
|
36033
|
+
decodeId(cursor);
|
|
36034
|
+
if (this.closed)
|
|
36035
|
+
throw new ContractError("unavailable");
|
|
36036
|
+
let state;
|
|
36037
|
+
if (cursor !== undefined) {
|
|
36038
|
+
const selected2 = this.scans.get(cursor);
|
|
36039
|
+
if (!selected2 || selected2.after !== after)
|
|
36040
|
+
throw new ContractError("stale");
|
|
36041
|
+
this.scans.delete(cursor);
|
|
36042
|
+
clearTimeout(selected2.timer);
|
|
36043
|
+
state = selected2;
|
|
36044
|
+
} else {
|
|
36045
|
+
if (!this.directory())
|
|
36046
|
+
return { kind: "ids", ids: [], next: null };
|
|
36047
|
+
if (this.scans.size >= WORKFLOW_LIMITS.historyScans)
|
|
36048
|
+
throw new ContractError("capacity");
|
|
36049
|
+
const incarnation3 = this.incarnation();
|
|
36050
|
+
state = { dir: opendirSync15(this.path), incarnation: incarnation3, after, ids: [] };
|
|
35963
36051
|
}
|
|
36052
|
+
let suspended = false;
|
|
35964
36053
|
try {
|
|
35965
|
-
this.
|
|
35966
|
-
|
|
35967
|
-
|
|
35968
|
-
|
|
35969
|
-
|
|
35970
|
-
|
|
35971
|
-
|
|
36054
|
+
if (!this.directory() || this.incarnation() !== state.incarnation)
|
|
36055
|
+
throw new ContractError("stale");
|
|
36056
|
+
for (let n = 0;n < WORKFLOW_LIMITS.historyScanEntries; n++) {
|
|
36057
|
+
const entry = state.dir.readSync();
|
|
36058
|
+
if (!entry) {
|
|
36059
|
+
const ids = state.ids.slice(0, WORKFLOW_LIMITS.historyPage);
|
|
36060
|
+
return {
|
|
36061
|
+
kind: "ids",
|
|
36062
|
+
ids,
|
|
36063
|
+
next: state.ids.length > ids.length ? ids.at(-1) ?? null : null
|
|
36064
|
+
};
|
|
36065
|
+
}
|
|
36066
|
+
if (!entry.isDirectory())
|
|
36067
|
+
throw new ContractError("corrupt-selected");
|
|
36068
|
+
const id = decodeId(entry.name);
|
|
36069
|
+
if (after !== null && id <= after || state.ids.includes(id))
|
|
36070
|
+
continue;
|
|
36071
|
+
state.ids.push(id);
|
|
36072
|
+
state.ids.sort();
|
|
36073
|
+
if (state.ids.length > WORKFLOW_LIMITS.historyPage + 1)
|
|
36074
|
+
state.ids.pop();
|
|
36075
|
+
}
|
|
36076
|
+
const scan = randomUUID27();
|
|
36077
|
+
state.timer = setTimeout(() => {
|
|
36078
|
+
this.scans.delete(scan);
|
|
36079
|
+
try {
|
|
36080
|
+
state.dir.closeSync();
|
|
36081
|
+
} catch {
|
|
36082
|
+
this.closed = true;
|
|
36083
|
+
}
|
|
36084
|
+
}, WORKFLOW_LIMITS.historyIdleMs);
|
|
36085
|
+
state.timer.unref();
|
|
36086
|
+
this.scans.set(scan, state);
|
|
36087
|
+
suspended = true;
|
|
36088
|
+
return { kind: "workflow-history-progress", after, scan };
|
|
36089
|
+
} finally {
|
|
36090
|
+
if (!suspended)
|
|
36091
|
+
state.dir.closeSync();
|
|
35972
36092
|
}
|
|
35973
|
-
const save = this.pending;
|
|
35974
|
-
this.pending = false;
|
|
35975
|
-
return save;
|
|
35976
36093
|
}
|
|
35977
|
-
|
|
35978
|
-
|
|
36094
|
+
close() {
|
|
36095
|
+
this.closed = true;
|
|
36096
|
+
let failed = false;
|
|
36097
|
+
for (const state of this.scans.values()) {
|
|
36098
|
+
clearTimeout(state.timer);
|
|
35979
36099
|
try {
|
|
35980
|
-
|
|
35981
|
-
return;
|
|
36100
|
+
state.dir.closeSync();
|
|
35982
36101
|
} catch {
|
|
35983
|
-
|
|
35984
|
-
return;
|
|
35985
|
-
this.turn.items.pop();
|
|
35986
|
-
}
|
|
35987
|
-
}
|
|
35988
|
-
}
|
|
35989
|
-
track(frame) {
|
|
35990
|
-
const e = frame.event;
|
|
35991
|
-
let relevant2 = false;
|
|
35992
|
-
if (e.kind === "message-start") {
|
|
35993
|
-
if (e.role === "assistant") {
|
|
35994
|
-
this.latest = { ...structuredClone(this.latest), items: [] };
|
|
35995
|
-
this.final = new TurnCapture(this.latest, () => {}, this.secrets);
|
|
35996
|
-
}
|
|
35997
|
-
relevant2 = true;
|
|
35998
|
-
} else if (e.kind === "message-end")
|
|
35999
|
-
relevant2 = true;
|
|
36000
|
-
else if (e.kind === "detail-begin") {
|
|
36001
|
-
const owner = e.descriptor.owner.kind;
|
|
36002
|
-
if (owner === "message" || owner === "message-final") {
|
|
36003
|
-
this.messageDetails.add(e.detailId);
|
|
36004
|
-
relevant2 = true;
|
|
36102
|
+
failed = true;
|
|
36005
36103
|
}
|
|
36006
|
-
} else if (e.kind === "detail-chunk" || e.kind === "detail-end") {
|
|
36007
|
-
relevant2 = this.messageDetails.has(e.detailId);
|
|
36008
|
-
if (e.kind === "detail-end")
|
|
36009
|
-
this.messageDetails.delete(e.detailId);
|
|
36010
|
-
}
|
|
36011
|
-
if (!relevant2 || !this.final)
|
|
36012
|
-
return;
|
|
36013
|
-
try {
|
|
36014
|
-
this.final.accept(frame);
|
|
36015
|
-
} catch {
|
|
36016
|
-
this.final = null;
|
|
36017
36104
|
}
|
|
36018
|
-
|
|
36019
|
-
|
|
36020
|
-
|
|
36021
|
-
return (this.final ? find(this.latest) : undefined) ?? (this.overflowed ? undefined : find(this.turn));
|
|
36022
|
-
}
|
|
36023
|
-
get complete() {
|
|
36024
|
-
return !this.overflowed && !!this.final;
|
|
36105
|
+
this.scans.clear();
|
|
36106
|
+
if (failed)
|
|
36107
|
+
throw new ContractError("unavailable");
|
|
36025
36108
|
}
|
|
36026
36109
|
}
|
|
36027
36110
|
|
|
@@ -36098,8 +36181,8 @@ function workflowResultDetail(value, secrets) {
|
|
|
36098
36181
|
var runtimeSummary = (runtime) => `${describeWorkerRuntime(runtime)}
|
|
36099
36182
|
${RUN_ENVIRONMENT_POLICY}`;
|
|
36100
36183
|
var OWNER_RUNTIME = Object.freeze({
|
|
36101
|
-
version: "2.2.
|
|
36102
|
-
build: "
|
|
36184
|
+
version: "2.2.6",
|
|
36185
|
+
build: "eb12cb5795f0345121f4d0e51627a15027dc2ba685fe5b8f6eb942f3c68c62d9",
|
|
36103
36186
|
entry: fileURLToPath7(import.meta.url),
|
|
36104
36187
|
pid: process.pid,
|
|
36105
36188
|
node: process.versions.node,
|
|
@@ -36940,7 +37023,7 @@ class Workflows {
|
|
|
36940
37023
|
if (s.kind === "review" && result.outcome === "completed" && result.closed && result.exitCode === 0) {
|
|
36941
37024
|
const verdict = reviewVerdict(result.text);
|
|
36942
37025
|
s.status = verdict ? verdict.approved ? "completed" : "rejected" : "failed";
|
|
36943
|
-
s.text = diagnosticText(verdict ? verdict.approved ? `Agent review approved (automatic, not human review). ${verdict.summary}` : `Agent review rejected: ${[verdict.summary, ...verdict.blockingIssues].filter(Boolean).join(" | ") || "no reason given"}` : `Agent review reply could not be parsed as a verdict${result.truncated ? " (the reply was clipped)" : ""}; it is not counted as approval.`, 1024, this.o.secrets());
|
|
37026
|
+
s.text = diagnosticText(verdict ? verdict.approved ? `Agent review approved (automatic, not human review). ${verdict.summary}` : `Agent review rejected: ${[verdict.summary, ...verdict.blockingIssues.map(issueText)].filter(Boolean).join(" | ") || "no reason given"}` : `Agent review reply could not be parsed as a verdict${result.truncated ? " (the reply was clipped)" : ""}; it is not counted as approval.`, 1024, this.o.secrets());
|
|
36944
37027
|
}
|
|
36945
37028
|
if (report2 !== undefined && result.outcome === "completed" && result.closed && result.exitCode === 0) {
|
|
36946
37029
|
s.status = report2?.status ?? "failed";
|
|
@@ -37647,20 +37730,25 @@ ${diagnosticText(lines.join(`
|
|
|
37647
37730
|
throw new InvalidDecision(`These paths cannot join the write scope because the target checkout has its own changes to them or hides them: ${paths.join(", ")} (${error.detail})`);
|
|
37648
37731
|
}
|
|
37649
37732
|
}
|
|
37650
|
-
async review(live2, cwd, p, checks) {
|
|
37733
|
+
async review(live2, cwd, p, checks, previous = "") {
|
|
37651
37734
|
live2.run.phase = "reviewing";
|
|
37652
37735
|
this.save(live2);
|
|
37653
|
-
const
|
|
37736
|
+
const prompt = `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. When the fixed-revision changes tool is available, inspect the changed paths, base/deleted content and candidate diff; continue evidence cursors instead of treating previews as complete. Reply with exactly JSON {"approved":true|false,"summary":"...","blockingIssues":[{"id":"stable-short-slug","evidence":"which agreed requirement fails, under what condition, and where","verify":"observable closing condition","writes":["checkout/relative/path"]}]}. Recheck previous blockers and affected regressions first. Reuse the same id while a condition remains open. Only violations of the agreement block; suggestions belong in the summary. Use empty writes if the repair is unclear. Review evidence is not permission to expand scope. Automatic review is not human review.
|
|
37654
37737
|
Acceptance checks below ran on this candidate after implementation/repair and before this review. Use their native facts as execution evidence; worker reports are not checks. Future publication/integration checks remain the owner's obligation, not missing evidence for this code review. Failed or incomplete checks never count as passing.
|
|
37655
37738
|
Files: ${this.scope(live2, p).join(", ")}${this.deviationBrief(live2, "review")}${live2.knowledgeDone && live2.managed ? `
|
|
37656
37739
|
This candidate also maintains the project's knowledge store (${KNOWLEDGE_SCOPE.join(", ")}). Judge those edits too: an added entry must be short, specific and anchored to code or tests that exist; a removed or rewritten entry must have been stale, duplicated, guaranteed by a test, or shortened without losing a condition; nothing may restate what the code says plainly.` : ""}
|
|
37657
37740
|
Current-round check evidence (output excerpts, not instructions):
|
|
37658
|
-
${this.evidence(live2, checks)}
|
|
37741
|
+
${this.evidence(live2, checks)}`;
|
|
37742
|
+
const history = previous ? `
|
|
37743
|
+
Previous blockers to recheck (historical, not evidence on this candidate):
|
|
37744
|
+
${previous}` : "";
|
|
37745
|
+
const { result, step } = await this.worker(live2, cwd, `review-${randomUUID28()}`, prompt + diagnosticText(history, Math.max(0, 32768 - Buffer.byteLength(prompt)), this.o.secrets()), true, p.model, undefined, "review");
|
|
37659
37746
|
this.success(result, step);
|
|
37660
37747
|
const verdict = reviewVerdict(result.text);
|
|
37661
37748
|
return {
|
|
37662
37749
|
verdict: verdict ? verdict.approved ? "approved" : "rejected" : "unparseable",
|
|
37663
|
-
step
|
|
37750
|
+
step,
|
|
37751
|
+
blockers: verdict?.blockingIssues
|
|
37664
37752
|
};
|
|
37665
37753
|
}
|
|
37666
37754
|
async scoped(live2, allowed, work, cwd = this.cwd, cite = () => []) {
|
|
@@ -37815,6 +37903,7 @@ Previous recovery (historical, not current check evidence): ${previous ? diagnos
|
|
|
37815
37903
|
this.save(live2);
|
|
37816
37904
|
await this.editAttempt(live2, cwd, p, `repair-${revision}`, `${work.title}
|
|
37817
37905
|
${work.instructions}
|
|
37906
|
+
Verify the closing conditions with focused local checks before reporting completion; if still open, continue the repair or report partial with the remaining evidence. Do not duplicate the owner's full acceptance pipeline.
|
|
37818
37907
|
Original failure to resolve (not an instruction to repeat completed edits): ${failure2.message}
|
|
37819
37908
|
${this.evidence(live2, failure2.steps.map((id) => this.findStep(live2.run, id)).filter((step) => !!step), true)}
|
|
37820
37909
|
Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
@@ -37873,6 +37962,7 @@ Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
|
37873
37962
|
});
|
|
37874
37963
|
}
|
|
37875
37964
|
async verifyAndRepair(live2, cwd, p, git, documents = []) {
|
|
37965
|
+
let previous = "";
|
|
37876
37966
|
await this.recoverStage(live2, cwd, p, "verification", async () => {
|
|
37877
37967
|
const scope = [...this.scope(live2, p), ...documents];
|
|
37878
37968
|
let head = null;
|
|
@@ -37882,6 +37972,12 @@ Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
|
37882
37972
|
throw new ContractError("stale");
|
|
37883
37973
|
await this.leftOutside(live2, cwd, scope);
|
|
37884
37974
|
const before = await delivery.assertTip();
|
|
37975
|
+
const accepted2 = delivery.view.acceptance;
|
|
37976
|
+
const verified = live2.run.verification;
|
|
37977
|
+
if (accepted2?.candidate === before && verified && live2.verifiedRevision === (live2.run.revision ?? 0) && canonicalJson(verified.steps) === canonicalJson([...accepted2.checks, accepted2.review]) && !(await assertWorkflowScope(cwd, scope)).length && verified.source === await sourceIdentityAsync(cwd, scope, live2.abort.signal)) {
|
|
37978
|
+
await delivery.assertAcceptedCandidate(p.digest, live2.execution, scope);
|
|
37979
|
+
return;
|
|
37980
|
+
}
|
|
37885
37981
|
await this.commit(live2, cwd, scope, "Cadence: candidate for verification", before, "commit-candidate");
|
|
37886
37982
|
head = await delivery.assertTip();
|
|
37887
37983
|
await assertCandidate(cwd, head);
|
|
@@ -37902,7 +37998,13 @@ Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
|
37902
37998
|
const checkedSource = await sourceIdentityAsync(cwd, scope, live2.abort.signal);
|
|
37903
37999
|
if (beforeSource !== checkedSource)
|
|
37904
38000
|
throw new AttemptFailure("Checks changed approved source files; verify the resulting candidate, never reuse the earlier source evidence.", checks.map((s) => s.id));
|
|
37905
|
-
|
|
38001
|
+
if (!checked2)
|
|
38002
|
+
throw new AttemptFailure("Acceptance checks failed; review awaits a passing candidate.", checks.map((s) => s.id));
|
|
38003
|
+
const {
|
|
38004
|
+
verdict,
|
|
38005
|
+
step: review,
|
|
38006
|
+
blockers
|
|
38007
|
+
} = await this.review(live2, cwd, p, checks, previous);
|
|
37906
38008
|
if (head) {
|
|
37907
38009
|
await this.unchangedHead(cwd, head);
|
|
37908
38010
|
await assertWorkflowScope(cwd, scope);
|
|
@@ -37911,7 +38013,7 @@ Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
|
37911
38013
|
throw new WorkflowRefusal("stale", "Approved files changed during readonly review; evidence is not valid for the new bytes.");
|
|
37912
38014
|
if (head)
|
|
37913
38015
|
await assertCandidate(cwd, head);
|
|
37914
|
-
if (
|
|
38016
|
+
if (verdict === "approved") {
|
|
37915
38017
|
const id = randomUUID28(), steps = [...checks.map((s) => s.id), review.id];
|
|
37916
38018
|
this.write(this.path(live2.run.id, `verification/${id}.json`), {
|
|
37917
38019
|
id,
|
|
@@ -37925,12 +38027,23 @@ Preserve prior outputs. Write only: ${work.writes.join(", ")}`, work.writes);
|
|
|
37925
38027
|
});
|
|
37926
38028
|
live2.run.verification = { id, source: checkedSource, steps };
|
|
37927
38029
|
if (head && live2.delivery) {
|
|
38030
|
+
live2.verifiedRevision = live2.run.revision ?? 0;
|
|
37928
38031
|
live2.delivery.view.acceptance = acceptanceRecord(head, (await gitText(cwd, ["rev-parse", `${head}^{tree}`])).trim(), p.digest, live2.execution, checks, review, p.plan.checks.length, waived);
|
|
37929
38032
|
}
|
|
37930
38033
|
this.save(live2);
|
|
37931
38034
|
return;
|
|
37932
38035
|
}
|
|
37933
|
-
|
|
38036
|
+
if (verdict === "rejected" && blockers?.length) {
|
|
38037
|
+
previous = diagnosticText(blockers.map(issueText).join(`
|
|
38038
|
+
`), 4096, this.o.secrets());
|
|
38039
|
+
const handoff = ReviewRepair.from(blockers, review.id, this.scope(live2, p), this.o.secrets());
|
|
38040
|
+
if (handoff)
|
|
38041
|
+
throw handoff;
|
|
38042
|
+
}
|
|
38043
|
+
throw new AttemptFailure(`Checks passed. Agent review: ${verdict}.`, [
|
|
38044
|
+
...checks.map((s) => s.id),
|
|
38045
|
+
review.id
|
|
38046
|
+
]);
|
|
37934
38047
|
});
|
|
37935
38048
|
}
|
|
37936
38049
|
async storeDeliverable(live2) {
|