@letra-ai/cli 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-4QDEU6KP.js → chunk-DMOLJWWW.js} +595 -39
- package/dist/{chunk-ELBTIPYN.js → chunk-H64RC3YS.js} +2 -4
- package/dist/{chunk-BTPFUKHG.js → chunk-L6YKDTAQ.js} +1 -1
- package/dist/{chunk-IEFSLCJL.js → chunk-OIHMDOIN.js} +491 -2
- package/dist/{chunk-KGQLHMGZ.js → chunk-URUC6ZZP.js} +300 -13
- package/dist/client/assets/index-EyRsq1SH.css +1 -0
- package/dist/client/assets/index-WfKR7Xp6.js +232 -0
- package/dist/client/index.html +2 -2
- package/dist/{fallback-UXSZ4LNR.js → fallback-MQLGS4KL.js} +1 -2
- package/dist/{flow-serve-MTB6KZMR.js → flow-serve-G5MSSB3G.js} +4 -5
- package/dist/{generate-N22QOXUF.js → generate-UPFERQMD.js} +1 -1
- package/dist/harness/default/v0.2.0/flows/flow-main.yaml +3 -1
- package/dist/harness/default/v0.2.0/gates/human-approved.yaml +3 -3
- package/dist/harness/default/v0.2.0/gates/spec-approved.yaml +3 -3
- package/dist/index.js +34 -17
- package/dist/{push-5JEYSXBY.js → push-VJRSQGUD.js} +1 -1
- package/dist/{server-FUP3APZA.js → server-P6GKRWDX.js} +17 -9
- package/dist/service-2JN2KI4D.js +29 -0
- package/dist/{sitrep-DJXTP3JO.js → sitrep-OKNZJICV.js} +2 -2
- package/package.json +1 -1
- package/dist/chunk-MSF44WWE.js +0 -443
- package/dist/chunk-QIVKP3EY.js +0 -362
- package/dist/client/assets/index-BuABgOyz.js +0 -232
- package/dist/client/assets/index-CpseCZh9.css +0 -1
- package/dist/service-OGF7BMCQ.js +0 -21
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
resolveActiveFlowFor
|
|
4
|
-
} from "./chunk-MSF44WWE.js";
|
|
5
2
|
import {
|
|
6
3
|
DEFAULT_HARNESS_VERSION,
|
|
7
4
|
parseSimpleYaml,
|
|
5
|
+
resolveActiveFlowFor,
|
|
8
6
|
resolveHarnessRoot
|
|
9
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-DMOLJWWW.js";
|
|
10
8
|
import {
|
|
11
9
|
queryLog
|
|
12
10
|
} from "./chunk-BKAUN5QH.js";
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
GateChecker,
|
|
4
|
+
writeFocusWithRecommendations
|
|
5
|
+
} from "./chunk-H64RC3YS.js";
|
|
6
|
+
import {
|
|
7
|
+
createWorkspaceBoundary,
|
|
8
|
+
generateAdapters,
|
|
9
|
+
loadWorkflow,
|
|
10
|
+
resolveActiveFlow,
|
|
11
|
+
resolveAgentDirection,
|
|
12
|
+
writeWorkflow
|
|
13
|
+
} from "./chunk-DMOLJWWW.js";
|
|
2
14
|
import {
|
|
3
15
|
logEntry
|
|
4
16
|
} from "./chunk-BKAUN5QH.js";
|
|
5
17
|
import {
|
|
6
|
-
getLetraDir2 as getLetraDir
|
|
18
|
+
getLetraDir2 as getLetraDir,
|
|
19
|
+
resolveWorkspaceRoot
|
|
7
20
|
} from "./chunk-6HSUVCTR.js";
|
|
8
21
|
|
|
22
|
+
// src/domain-operations/service.ts
|
|
23
|
+
import { existsSync as existsSync4, readFileSync as readFileSync4, renameSync, writeFileSync, statSync as statSync2 } from "fs";
|
|
24
|
+
import { createHash } from "crypto";
|
|
25
|
+
import { join as join4, resolve as resolve2 } from "path";
|
|
26
|
+
|
|
9
27
|
// src/commands/validate.ts
|
|
10
28
|
import { existsSync as existsSync3, readFileSync as readFileSync3, readdirSync as readdirSync2, watch } from "fs";
|
|
11
29
|
import { join as join3, resolve, dirname } from "path";
|
|
@@ -832,6 +850,477 @@ async function validate(targetPath, options) {
|
|
|
832
850
|
return summary;
|
|
833
851
|
}
|
|
834
852
|
|
|
853
|
+
// src/domain-operations/service.ts
|
|
854
|
+
function activityOperation(root, itemId) {
|
|
855
|
+
const direction = resolveAgentDirection(createWorkspaceBoundary(resolve2(root)).root);
|
|
856
|
+
return direction.item && (!itemId || direction.item.id === itemId) ? direction.item : null;
|
|
857
|
+
}
|
|
858
|
+
async function submitEvidenceOperation(root, input) {
|
|
859
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
860
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
861
|
+
const subject = { itemId: input.itemId, operation: "submit_evidence" };
|
|
862
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
863
|
+
if (stale) return stale;
|
|
864
|
+
const item = loadWorkflow(workspaceRoot)?.items.find((candidate) => candidate.id === input.itemId);
|
|
865
|
+
if (!item || item.claimedBy !== input.actor || item.claimExecutorId && item.claimExecutorId !== input.executorId) return rejected(workspaceRoot, before, "CLAIM_REQUIRED", "A evid\xEAncia exige claim vigente.", input, subject);
|
|
866
|
+
const boundary = createWorkspaceBoundary(resolveWorkspaceRoot(root).workspaceDir);
|
|
867
|
+
for (const evidence of input.evidence) {
|
|
868
|
+
if (!evidence.source?.trim() || !evidence.value?.trim()) return rejected(workspaceRoot, before, "EVIDENCE_INVALID", "Evid\xEAncia exige origem e valor.", input, subject);
|
|
869
|
+
if (evidence.kind === "file" || evidence.kind === "diff" || evidence.kind === "artifact") {
|
|
870
|
+
try {
|
|
871
|
+
const path = boundary.assertPath(evidence.value);
|
|
872
|
+
if (existsSync4(path) && statSync2(path).isSymbolicLink()) throw new Error("symlink");
|
|
873
|
+
} catch {
|
|
874
|
+
return rejected(workspaceRoot, before, "EVIDENCE_PATH_OUTSIDE_WORKSPACE", "Path da evid\xEAncia fora do workspace autorizado.", input, subject);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
const entry = audit(workspaceRoot, "agent_execution_event", before, { outcome: "accepted", reasonCode: "EVIDENCE_ACCEPTED", reason: input.reason, actor: input.actor, itemId: input.itemId, details: { evidence: input.evidence.map((e) => ({ ...e, observedAt: e.observedAt ?? (/* @__PURE__ */ new Date()).toISOString(), sha256: e.sha256 ?? createHash("sha256").update(e.value).digest("hex") })) } });
|
|
879
|
+
return result(before, entry.id, "accepted", "EVIDENCE_ACCEPTED", input.reason, resolveAgentDirection(workspaceRoot));
|
|
880
|
+
}
|
|
881
|
+
async function requestHandoffOperation(root, input) {
|
|
882
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
883
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
884
|
+
const subject = { itemId: input.itemId, operation: "request_handoff" };
|
|
885
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
886
|
+
if (stale) return stale;
|
|
887
|
+
const workflow = loadWorkflow(workspaceRoot);
|
|
888
|
+
const item = workflow?.items.find((candidate) => candidate.id === input.itemId);
|
|
889
|
+
if (!workflow || !item || item.claimedBy !== input.actor || item.claimExecutorId && item.claimExecutorId !== input.executorId) return rejected(workspaceRoot, before, "CLAIM_REQUIRED", "Handoff exige claim vigente do executor.", input, subject);
|
|
890
|
+
if (item.handoff) return rejected(workspaceRoot, before, "HANDOFF_CONFLICT", "J\xE1 existe handoff pendente.", input, subject);
|
|
891
|
+
const now = /* @__PURE__ */ new Date();
|
|
892
|
+
item.handoff = { from: input.actor ?? "unknown", to: input.to, summary: input.summary, evidence: input.evidence, timestamp: now.toISOString(), expiresAt: new Date(now.getTime() + (input.ttlMinutes ?? 30) * 6e4).toISOString(), executorId: input.executorId };
|
|
893
|
+
item.claimedBy = void 0;
|
|
894
|
+
item.claimedAt = void 0;
|
|
895
|
+
item.claimExecutorId = void 0;
|
|
896
|
+
item.claimCapability = void 0;
|
|
897
|
+
item.claimRevision = void 0;
|
|
898
|
+
item.claimExpiresAt = void 0;
|
|
899
|
+
item.claimTtlMinutes = void 0;
|
|
900
|
+
workflow.updatedAt = now.toISOString();
|
|
901
|
+
const write = await writeWorkflow(workspaceRoot, { workflow, source: "flow-handoff", primaryItemId: item.id, skipSitrep: true, skipLog: true, quiet: true, confineAdapterWrites: true });
|
|
902
|
+
if (!write.ok) return rejected(workspaceRoot, before, "HANDOFF_WRITE_FAILED", write.error ?? "Falha ao persistir handoff.", input, subject);
|
|
903
|
+
const entry = audit(workspaceRoot, "agent_execution_event", before, { outcome: "accepted", reasonCode: "HANDOFF_ACCEPTED", reason: input.reason, actor: input.actor, itemId: item.id, details: { to: input.to, executorId: input.executorId } });
|
|
904
|
+
return result(before, entry.id, "accepted", "HANDOFF_ACCEPTED", input.reason, resolveAgentDirection(workspaceRoot));
|
|
905
|
+
}
|
|
906
|
+
function audit(root, action, direction, input) {
|
|
907
|
+
return logEntry(root, action, input.reason, {
|
|
908
|
+
itemId: input.itemId,
|
|
909
|
+
acId: input.acId,
|
|
910
|
+
details: {
|
|
911
|
+
adapter: "codex",
|
|
912
|
+
by: input.actor ?? "agent:codex",
|
|
913
|
+
revision: direction.revision,
|
|
914
|
+
outcome: input.outcome,
|
|
915
|
+
reasonCode: input.reasonCode,
|
|
916
|
+
...input.details
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
function result(direction, entryId, outcome, reasonCode, reason, nextDirection = direction, extra = {}) {
|
|
921
|
+
return {
|
|
922
|
+
outcome,
|
|
923
|
+
auditId: entryId,
|
|
924
|
+
beforeRevision: direction.revision,
|
|
925
|
+
afterRevision: nextDirection.revision,
|
|
926
|
+
reasonCode,
|
|
927
|
+
reason,
|
|
928
|
+
nextDirection,
|
|
929
|
+
...extra
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
function rejected(root, direction, reasonCode, reason, context, subject) {
|
|
933
|
+
const entry = audit(root, "agent_operation_rejected", direction, {
|
|
934
|
+
outcome: "rejected",
|
|
935
|
+
reasonCode,
|
|
936
|
+
reason,
|
|
937
|
+
actor: context.actor,
|
|
938
|
+
itemId: subject.itemId,
|
|
939
|
+
acId: subject.acId,
|
|
940
|
+
details: { operation: subject.operation }
|
|
941
|
+
});
|
|
942
|
+
return result(direction, entry.id, "rejected", reasonCode, reason);
|
|
943
|
+
}
|
|
944
|
+
function checkRevision(root, direction, context, subject) {
|
|
945
|
+
if (context.expectedRevision === direction.revision) return null;
|
|
946
|
+
return rejected(
|
|
947
|
+
root,
|
|
948
|
+
direction,
|
|
949
|
+
"DIRECTION_STALE",
|
|
950
|
+
"A dire\xE7\xE3o mudou desde a \xFAltima consulta. Consulte get_direction e tente novamente.",
|
|
951
|
+
context,
|
|
952
|
+
subject
|
|
953
|
+
);
|
|
954
|
+
}
|
|
955
|
+
function normalizeAcId(value) {
|
|
956
|
+
const match = value.trim().match(/^AC[\s-]?(\d+(?:\.\d+)*)$/i);
|
|
957
|
+
return match ? `AC${match[1]}` : value.trim().toUpperCase();
|
|
958
|
+
}
|
|
959
|
+
async function claimOperation(root, input) {
|
|
960
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
961
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
962
|
+
const subject = { itemId: input.itemId, operation: "claim" };
|
|
963
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
964
|
+
if (stale) return stale;
|
|
965
|
+
if (!input.actor?.trim())
|
|
966
|
+
return rejected(workspaceRoot, before, "ACTOR_REQUIRED", "Claim exige identidade do actor.", input, subject);
|
|
967
|
+
if (!before.item || before.item.id !== input.itemId)
|
|
968
|
+
return rejected(workspaceRoot, before, "ITEM_NOT_CURRENT", "O claim exige o item vigente.", input, subject);
|
|
969
|
+
const flow = resolveActiveFlow(workspaceRoot).flow;
|
|
970
|
+
const stage = flow?.stages.find((candidate) => candidate.id === before.item?.stage);
|
|
971
|
+
const capabilities = stage?.roles.flatMap((role) => role.capabilities) ?? [];
|
|
972
|
+
if (capabilities.length > 0 && !capabilities.includes(input.capability))
|
|
973
|
+
return rejected(workspaceRoot, before, "CAPABILITY_INVALID", `Capability n\xE3o permitida: ${input.capability}.`, input, subject);
|
|
974
|
+
const workflow = loadWorkflow(workspaceRoot);
|
|
975
|
+
const item = workflow?.items.find((candidate) => candidate.id === input.itemId);
|
|
976
|
+
if (!workflow || !item) return rejected(workspaceRoot, before, "ITEM_NOT_FOUND", "Item n\xE3o encontrado.", input, subject);
|
|
977
|
+
const claimExpired = item.claimExpiresAt ? Date.now() >= Date.parse(item.claimExpiresAt) : false;
|
|
978
|
+
if (item.claimedBy && !claimExpired && (item.claimedBy !== input.actor || item.claimExecutorId !== input.executorId))
|
|
979
|
+
return rejected(workspaceRoot, before, "CLAIM_CONFLICT", `Item j\xE1 est\xE1 sob responsabilidade de ${item.claimedBy}.`, input, subject);
|
|
980
|
+
const ttl = Math.max(1, Math.min(1440, input.ttlMinutes ?? 30));
|
|
981
|
+
const now = /* @__PURE__ */ new Date();
|
|
982
|
+
item.claimedBy = input.actor.trim();
|
|
983
|
+
item.claimedAt = now.toISOString();
|
|
984
|
+
item.claimExecutorId = input.executorId.trim();
|
|
985
|
+
item.claimCapability = input.capability.trim();
|
|
986
|
+
item.claimRevision = before.revision;
|
|
987
|
+
item.claimTtlMinutes = ttl;
|
|
988
|
+
item.claimExpiresAt = new Date(now.getTime() + ttl * 6e4).toISOString();
|
|
989
|
+
workflow.updatedAt = now.toISOString();
|
|
990
|
+
const writeResult = await writeWorkflow(workspaceRoot, {
|
|
991
|
+
workflow,
|
|
992
|
+
source: "flow-claim",
|
|
993
|
+
primaryItemId: item.id,
|
|
994
|
+
skipSitrep: true,
|
|
995
|
+
skipLog: true,
|
|
996
|
+
quiet: true,
|
|
997
|
+
confineAdapterWrites: true
|
|
998
|
+
});
|
|
999
|
+
if (!writeResult.ok)
|
|
1000
|
+
return rejected(workspaceRoot, before, "CLAIM_WRITE_FAILED", writeResult.error ?? "Falha ao persistir claim.", input, subject);
|
|
1001
|
+
const after = resolveAgentDirection(workspaceRoot);
|
|
1002
|
+
const entry = audit(workspaceRoot, "agent_claim_requested", before, {
|
|
1003
|
+
outcome: "accepted",
|
|
1004
|
+
reasonCode: "CLAIM_ACCEPTED",
|
|
1005
|
+
reason: input.reason,
|
|
1006
|
+
actor: input.actor,
|
|
1007
|
+
itemId: item.id,
|
|
1008
|
+
details: { executorId: input.executorId, capability: input.capability, ttlMinutes: ttl, expiresAt: item.claimExpiresAt }
|
|
1009
|
+
});
|
|
1010
|
+
return result(before, entry.id, "accepted", "CLAIM_ACCEPTED", input.reason, after);
|
|
1011
|
+
}
|
|
1012
|
+
async function recordExecutionEvent(root, input) {
|
|
1013
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
1014
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
1015
|
+
const subject = { itemId: input.itemId, operation: input.status };
|
|
1016
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
1017
|
+
if (stale) return stale;
|
|
1018
|
+
if (!input.actor?.trim()) return rejected(workspaceRoot, before, "ACTOR_REQUIRED", "Evento exige identidade do actor.", input, subject);
|
|
1019
|
+
if (!before.item || before.item.id !== input.itemId) return rejected(workspaceRoot, before, "ITEM_NOT_CURRENT", "Evento exige o item vigente.", input, subject);
|
|
1020
|
+
const workflow = loadWorkflow(workspaceRoot);
|
|
1021
|
+
const item = workflow?.items.find((candidate) => candidate.id === input.itemId);
|
|
1022
|
+
if (!workflow || !item) return rejected(workspaceRoot, before, "ITEM_NOT_FOUND", "Item n\xE3o encontrado.", input, subject);
|
|
1023
|
+
const expired = !item.claimExpiresAt || Date.now() >= Date.parse(item.claimExpiresAt);
|
|
1024
|
+
if (item.claimedBy !== input.actor || item.claimExecutorId !== input.executorId) return rejected(workspaceRoot, before, "CLAIM_REQUIRED", "Actor e executor precisam possuir o claim vigente.", input, subject);
|
|
1025
|
+
if (expired) return rejected(workspaceRoot, before, "CLAIM_EXPIRED", "O lease do claim expirou; fa\xE7a um novo claim.", input, subject);
|
|
1026
|
+
const nowDate = /* @__PURE__ */ new Date();
|
|
1027
|
+
const now = nowDate.toISOString();
|
|
1028
|
+
item.activityStatus = input.status;
|
|
1029
|
+
if (input.status === "started") item.activityStartedAt = now;
|
|
1030
|
+
if (input.status === "heartbeat" || input.status === "started") item.lastHeartbeatAt = now;
|
|
1031
|
+
if (input.status === "heartbeat") item.claimExpiresAt = new Date(nowDate.getTime() + (item.claimTtlMinutes ?? 30) * 6e4).toISOString();
|
|
1032
|
+
if (input.status === "failed") item.lastFailure = { code: input.errorCode ?? "EXECUTION_FAILED", message: input.message ?? "Execu\xE7\xE3o falhou.", recovery: input.recovery ?? "human", at: now };
|
|
1033
|
+
workflow.updatedAt = now;
|
|
1034
|
+
const writeResult = await writeWorkflow(workspaceRoot, { workflow, source: "flow-claim", primaryItemId: item.id, skipSitrep: true, skipLog: true, quiet: true, confineAdapterWrites: true });
|
|
1035
|
+
if (!writeResult.ok) return rejected(workspaceRoot, before, "EVENT_WRITE_FAILED", writeResult.error ?? "Falha ao persistir evento.", input, subject);
|
|
1036
|
+
const after = resolveAgentDirection(workspaceRoot);
|
|
1037
|
+
const entry = audit(workspaceRoot, "agent_execution_event", before, { outcome: "accepted", reasonCode: "EVENT_RECORDED", reason: input.reason, actor: input.actor, itemId: item.id, details: { status: input.status, executorId: input.executorId, message: input.message, recovery: input.recovery, errorCode: input.errorCode } });
|
|
1038
|
+
return result(before, entry.id, "accepted", "EVENT_RECORDED", input.reason, after);
|
|
1039
|
+
}
|
|
1040
|
+
function markPendingAc(content, acId) {
|
|
1041
|
+
const lines = content.split("\n");
|
|
1042
|
+
const expected = normalizeAcId(acId);
|
|
1043
|
+
const index = lines.findIndex((line) => {
|
|
1044
|
+
if (!/^\s*-\s*\[ \]\s*\*\*/.test(line)) return false;
|
|
1045
|
+
const label = line.match(/\*\*([^*]+)\*\*/)?.[1] ?? "";
|
|
1046
|
+
const id = label.match(/\bAC[\s-]?(\d+(?:\.\d+)*)\b/i);
|
|
1047
|
+
return id ? `AC${id[1]}` === expected : false;
|
|
1048
|
+
});
|
|
1049
|
+
if (index < 0) return null;
|
|
1050
|
+
lines[index] = lines[index].replace(/\[ \]/, "[x]");
|
|
1051
|
+
return lines.join("\n");
|
|
1052
|
+
}
|
|
1053
|
+
async function runValidationOperation(root, context) {
|
|
1054
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
1055
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
1056
|
+
const stale = checkRevision(workspaceRoot, before, context, { operation: "validate" });
|
|
1057
|
+
if (stale) return stale;
|
|
1058
|
+
const validation = await validate(workspaceRoot, {
|
|
1059
|
+
format: "silent",
|
|
1060
|
+
exit: false,
|
|
1061
|
+
log: false
|
|
1062
|
+
});
|
|
1063
|
+
const after = resolveAgentDirection(workspaceRoot);
|
|
1064
|
+
const entry = audit(workspaceRoot, "agent_validation_run", before, {
|
|
1065
|
+
outcome: validation.failed === 0 ? "accepted" : "rejected",
|
|
1066
|
+
reasonCode: validation.failed === 0 ? "VALIDATION_COMPLETED" : "VALIDATION_FAILED",
|
|
1067
|
+
reason: context.reason,
|
|
1068
|
+
actor: context.actor,
|
|
1069
|
+
itemId: before.item?.id,
|
|
1070
|
+
details: { validation }
|
|
1071
|
+
});
|
|
1072
|
+
return result(
|
|
1073
|
+
before,
|
|
1074
|
+
entry.id,
|
|
1075
|
+
validation.failed === 0 ? "accepted" : "rejected",
|
|
1076
|
+
validation.failed === 0 ? "VALIDATION_COMPLETED" : "VALIDATION_FAILED",
|
|
1077
|
+
context.reason,
|
|
1078
|
+
after,
|
|
1079
|
+
{ validation }
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
function completeAcOperation(root, input) {
|
|
1083
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
1084
|
+
const boundary = createWorkspaceBoundary(resolveWorkspaceRoot(root).workspaceDir);
|
|
1085
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
1086
|
+
const subject = { itemId: before.item?.id, acId: input.acId, operation: "complete_ac" };
|
|
1087
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
1088
|
+
if (stale) return stale;
|
|
1089
|
+
const evidence = input.evidence.map((item) => item.trim()).filter(Boolean);
|
|
1090
|
+
if (evidence.length === 0) {
|
|
1091
|
+
return rejected(
|
|
1092
|
+
workspaceRoot,
|
|
1093
|
+
before,
|
|
1094
|
+
"REGRESSION_EVIDENCE_REQUIRED",
|
|
1095
|
+
"\xC9 obrigat\xF3ria ao menos uma evid\xEAncia verific\xE1vel de regress\xE3o.",
|
|
1096
|
+
input,
|
|
1097
|
+
subject
|
|
1098
|
+
);
|
|
1099
|
+
}
|
|
1100
|
+
const requestedAc = normalizeAcId(input.acId);
|
|
1101
|
+
if (!before.pendingAC || normalizeAcId(before.pendingAC.id) !== requestedAc) {
|
|
1102
|
+
return rejected(
|
|
1103
|
+
workspaceRoot,
|
|
1104
|
+
before,
|
|
1105
|
+
"AC_NOT_CURRENT",
|
|
1106
|
+
`O AC solicitado n\xE3o \xE9 o crit\xE9rio pendente vigente (${before.pendingAC?.id ?? "nenhum"}).`,
|
|
1107
|
+
input,
|
|
1108
|
+
subject
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
const spec = before.item?.spec;
|
|
1112
|
+
if (!spec || !/^[a-zA-Z0-9._-]+$/.test(spec)) {
|
|
1113
|
+
return rejected(
|
|
1114
|
+
workspaceRoot,
|
|
1115
|
+
before,
|
|
1116
|
+
"ACTIVE_SPEC_REQUIRED",
|
|
1117
|
+
"O item vigente n\xE3o possui uma spec v\xE1lida vinculada.",
|
|
1118
|
+
input,
|
|
1119
|
+
subject
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
const specDir = join4(getLetraDir(workspaceRoot), "specs", spec);
|
|
1123
|
+
const specPath = boundary.assertPath(join4(specDir, "spec.md"));
|
|
1124
|
+
if (!existsSync4(specPath)) {
|
|
1125
|
+
return rejected(
|
|
1126
|
+
workspaceRoot,
|
|
1127
|
+
before,
|
|
1128
|
+
"ACTIVE_SPEC_REQUIRED",
|
|
1129
|
+
"A spec vigente n\xE3o foi encontrada no workspace.",
|
|
1130
|
+
input,
|
|
1131
|
+
subject
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
const updatedSpec = markPendingAc(readFileSync4(specPath, "utf-8"), requestedAc);
|
|
1135
|
+
if (!updatedSpec) {
|
|
1136
|
+
return rejected(
|
|
1137
|
+
workspaceRoot,
|
|
1138
|
+
before,
|
|
1139
|
+
"AC_NOT_PENDING",
|
|
1140
|
+
"O AC solicitado n\xE3o est\xE1 pendente na spec vigente.",
|
|
1141
|
+
input,
|
|
1142
|
+
subject
|
|
1143
|
+
);
|
|
1144
|
+
}
|
|
1145
|
+
const acceptancePath = boundary.assertPath(join4(specDir, "acceptance.md"));
|
|
1146
|
+
const updatedAcceptance = existsSync4(acceptancePath) ? markPendingAc(readFileSync4(acceptancePath, "utf-8"), requestedAc) : null;
|
|
1147
|
+
const specTmp = `${specPath}.tmp`;
|
|
1148
|
+
writeFileSync(specTmp, updatedSpec, "utf-8");
|
|
1149
|
+
renameSync(specTmp, specPath);
|
|
1150
|
+
if (updatedAcceptance) {
|
|
1151
|
+
const acceptanceTmp = `${acceptancePath}.tmp`;
|
|
1152
|
+
writeFileSync(acceptanceTmp, updatedAcceptance, "utf-8");
|
|
1153
|
+
renameSync(acceptanceTmp, acceptancePath);
|
|
1154
|
+
}
|
|
1155
|
+
const workflow = loadWorkflow(workspaceRoot);
|
|
1156
|
+
if (workflow?.tools?.length) {
|
|
1157
|
+
generateAdapters(workspaceRoot, workflow.tools, {
|
|
1158
|
+
source: "flow-ac",
|
|
1159
|
+
quiet: true,
|
|
1160
|
+
confineWrites: true
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
const after = resolveAgentDirection(workspaceRoot);
|
|
1164
|
+
const entry = audit(workspaceRoot, "agent_ac_completion_requested", before, {
|
|
1165
|
+
outcome: "accepted",
|
|
1166
|
+
reasonCode: "AC_COMPLETED",
|
|
1167
|
+
reason: input.reason,
|
|
1168
|
+
actor: input.actor,
|
|
1169
|
+
itemId: before.item?.id,
|
|
1170
|
+
acId: requestedAc,
|
|
1171
|
+
details: { spec, evidence, afterRevision: after.revision }
|
|
1172
|
+
});
|
|
1173
|
+
return result(before, entry.id, "accepted", "AC_COMPLETED", input.reason, after);
|
|
1174
|
+
}
|
|
1175
|
+
async function requestTransitionOperation(root, input) {
|
|
1176
|
+
const workspaceRoot = createWorkspaceBoundary(resolve2(root)).root;
|
|
1177
|
+
const before = resolveAgentDirection(workspaceRoot);
|
|
1178
|
+
const subject = { itemId: input.itemId, operation: "request_transition" };
|
|
1179
|
+
const stale = checkRevision(workspaceRoot, before, input, subject);
|
|
1180
|
+
if (stale) return stale;
|
|
1181
|
+
if (!before.item || before.item.id !== input.itemId) {
|
|
1182
|
+
return rejected(
|
|
1183
|
+
workspaceRoot,
|
|
1184
|
+
before,
|
|
1185
|
+
"ITEM_NOT_CURRENT",
|
|
1186
|
+
"A transi\xE7\xE3o somente pode ser solicitada para o item vigente.",
|
|
1187
|
+
input,
|
|
1188
|
+
subject
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
if (before.pendingAC) {
|
|
1192
|
+
return rejected(
|
|
1193
|
+
workspaceRoot,
|
|
1194
|
+
before,
|
|
1195
|
+
"PENDING_ACCEPTANCE_CRITERIA",
|
|
1196
|
+
`O item ainda possui o crit\xE9rio pendente ${before.pendingAC.id}.`,
|
|
1197
|
+
input,
|
|
1198
|
+
subject
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
const workflow = loadWorkflow(workspaceRoot);
|
|
1202
|
+
const item = workflow?.items.find((candidate) => candidate.id === input.itemId);
|
|
1203
|
+
const target = workflow?.stages.find((stage) => stage.id === input.targetStageId);
|
|
1204
|
+
if (!workflow || !item || !target) {
|
|
1205
|
+
return rejected(
|
|
1206
|
+
workspaceRoot,
|
|
1207
|
+
before,
|
|
1208
|
+
"INVALID_TRANSITION",
|
|
1209
|
+
"O item ou est\xE1gio de destino n\xE3o existe no workflow vigente.",
|
|
1210
|
+
input,
|
|
1211
|
+
subject
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
if (item.stage === target.id) {
|
|
1215
|
+
return rejected(
|
|
1216
|
+
workspaceRoot,
|
|
1217
|
+
before,
|
|
1218
|
+
"INVALID_TRANSITION",
|
|
1219
|
+
"O item j\xE1 est\xE1 no est\xE1gio solicitado.",
|
|
1220
|
+
input,
|
|
1221
|
+
subject
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
if (before.allowedStageIds.length > 0 && !before.allowedStageIds.includes(target.id)) {
|
|
1225
|
+
return rejected(
|
|
1226
|
+
workspaceRoot,
|
|
1227
|
+
before,
|
|
1228
|
+
"STAGE_NOT_ALLOWED",
|
|
1229
|
+
"O papel vigente n\xE3o permite transi\xE7\xE3o para o est\xE1gio solicitado.",
|
|
1230
|
+
input,
|
|
1231
|
+
subject
|
|
1232
|
+
);
|
|
1233
|
+
}
|
|
1234
|
+
const activeFlow = resolveActiveFlow(workspaceRoot).flow;
|
|
1235
|
+
const sourceDefinition = activeFlow?.stages.find((stage) => stage.id === item.stage);
|
|
1236
|
+
const targetDefinition = activeFlow?.stages.find((stage) => stage.id === target.id);
|
|
1237
|
+
const blockingGate = [sourceDefinition?.gate, targetDefinition?.gate].find(
|
|
1238
|
+
(gate) => gate?.blocking
|
|
1239
|
+
);
|
|
1240
|
+
if (blockingGate) {
|
|
1241
|
+
const checker = new GateChecker(workspaceRoot);
|
|
1242
|
+
const gateResult = checker.check(blockingGate.id, item);
|
|
1243
|
+
if (blockingGate.type === "human") {
|
|
1244
|
+
const gateHint = targetDefinition?.activity?.gate;
|
|
1245
|
+
const reviewHint = targetDefinition?.activity?.review;
|
|
1246
|
+
const enriched = {
|
|
1247
|
+
...before,
|
|
1248
|
+
allowedStageIds: targetDefinition ? [...new Set(targetDefinition.roles.flatMap((role) => role.allowedStages))] : before.allowedStageIds,
|
|
1249
|
+
prohibitions: [...gateHint?.mustNotDo ?? reviewHint?.mustNotDo ?? []],
|
|
1250
|
+
requiredEvidence: gateHint?.evidence ? [gateHint.evidence] : []
|
|
1251
|
+
};
|
|
1252
|
+
enriched.pendingAC = void 0;
|
|
1253
|
+
const entry2 = audit(workspaceRoot, "agent_transition_requested", before, {
|
|
1254
|
+
outcome: "approval-required",
|
|
1255
|
+
reasonCode: "HUMAN_APPROVAL_REQUIRED",
|
|
1256
|
+
reason: `O gate "${blockingGate.name}" exige decis\xE3o humana expl\xEDcita.`,
|
|
1257
|
+
actor: input.actor,
|
|
1258
|
+
itemId: item.id,
|
|
1259
|
+
details: { from: item.stage, to: target.id, gateId: blockingGate.id }
|
|
1260
|
+
});
|
|
1261
|
+
return result(
|
|
1262
|
+
before,
|
|
1263
|
+
entry2.id,
|
|
1264
|
+
"approval-required",
|
|
1265
|
+
"HUMAN_APPROVAL_REQUIRED",
|
|
1266
|
+
`O gate "${blockingGate.name}" exige decis\xE3o humana expl\xEDcita.`,
|
|
1267
|
+
enriched
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
if (!gateResult.allowed) {
|
|
1271
|
+
return rejected(
|
|
1272
|
+
workspaceRoot,
|
|
1273
|
+
before,
|
|
1274
|
+
"BLOCKING_GATE",
|
|
1275
|
+
gateResult.reason ?? `O gate "${blockingGate.name}" deve ser satisfeito antes da transi\xE7\xE3o.`,
|
|
1276
|
+
input,
|
|
1277
|
+
subject
|
|
1278
|
+
);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
const from = item.stage;
|
|
1282
|
+
item.stage = target.id;
|
|
1283
|
+
workflow.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1284
|
+
const writeResult = await writeWorkflow(workspaceRoot, {
|
|
1285
|
+
workflow,
|
|
1286
|
+
source: "flow-move",
|
|
1287
|
+
primaryItemId: item.id,
|
|
1288
|
+
skipSitrep: true,
|
|
1289
|
+
skipLog: true,
|
|
1290
|
+
quiet: true,
|
|
1291
|
+
confineAdapterWrites: true
|
|
1292
|
+
});
|
|
1293
|
+
if (!writeResult.ok) {
|
|
1294
|
+
return rejected(
|
|
1295
|
+
workspaceRoot,
|
|
1296
|
+
before,
|
|
1297
|
+
"TRANSITION_WRITE_FAILED",
|
|
1298
|
+
writeResult.error ?? "A persist\xEAncia da transi\xE7\xE3o falhou.",
|
|
1299
|
+
input,
|
|
1300
|
+
subject
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
if (item.spec) writeFocusWithRecommendations(workspaceRoot, item.spec, item.id);
|
|
1304
|
+
const after = resolveAgentDirection(workspaceRoot);
|
|
1305
|
+
const entry = audit(workspaceRoot, "agent_transition_requested", before, {
|
|
1306
|
+
outcome: "accepted",
|
|
1307
|
+
reasonCode: "TRANSITION_COMPLETED",
|
|
1308
|
+
reason: input.reason,
|
|
1309
|
+
actor: input.actor,
|
|
1310
|
+
itemId: item.id,
|
|
1311
|
+
details: { from, to: target.id, afterRevision: after.revision }
|
|
1312
|
+
});
|
|
1313
|
+
return result(before, entry.id, "accepted", "TRANSITION_COMPLETED", input.reason, after);
|
|
1314
|
+
}
|
|
1315
|
+
|
|
835
1316
|
export {
|
|
836
|
-
validate
|
|
1317
|
+
validate,
|
|
1318
|
+
activityOperation,
|
|
1319
|
+
submitEvidenceOperation,
|
|
1320
|
+
requestHandoffOperation,
|
|
1321
|
+
claimOperation,
|
|
1322
|
+
recordExecutionEvent,
|
|
1323
|
+
runValidationOperation,
|
|
1324
|
+
completeAcOperation,
|
|
1325
|
+
requestTransitionOperation
|
|
837
1326
|
};
|