@ngockhoale/ukit 3.4.29 → 3.4.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/manifests/documentation.yaml +100 -0
- package/package.json +1 -1
- package/src/core/executionContracts.js +32 -0
- package/src/core/permissionPolicy.js +1 -1
- package/src/decision/client.js +15 -0
- package/src/decision/protocol.js +12 -14
- package/src/index/buildIndex.js +244 -4
- package/src/index/impactContext.js +91 -11
- package/src/index/languageTools.js +126 -0
- package/src/index/paths.js +5 -1
- package/src/index/playbookRegistry.js +108 -9
- package/src/index/queryIndex.js +415 -1
- package/src/index/routeCatalog.js +1 -1
- package/src/index/routeResolver.js +2 -2
- package/src/index/taskRouting.js +28 -4
- package/template_project/.claude/commands/ukit/handoff-create.md +5 -0
- package/template_project/.claude/commands/ukit/handoff-fullstack.md +43 -4
- package/template_project/.claude/hooks/block-dangerous.mjs +7 -1
- package/template_project/.claude/hooks/handoff-model-guard.sh +235 -21
- package/template_project/.claude/hooks/record-execution.mjs +21 -0
- package/template_project/.claude/hooks/skill-router.sh +187 -16
- package/template_project/.claude/settings.json +43 -1
- package/template_project/.claude/ukit/index/cleanup-task-worktree.mjs +1 -1
- package/template_project/.claude/ukit/index/fix-loop-escalation.mjs +10 -4
- package/template_project/.claude/ukit/index/impact-context.mjs +70 -15
- package/template_project/.claude/ukit/index/land-worktree.mjs +2 -2
- package/template_project/.claude/ukit/index/lib/index-core.mjs +883 -14
- package/template_project/.claude/ukit/index/playbook-registry.mjs +108 -9
- package/template_project/.claude/ukit/index/query-index.mjs +100 -7
- package/template_project/.claude/ukit/index/review-verdict.mjs +14 -4
- package/template_project/.claude/ukit/index/route-catalog.mjs +1 -1
- package/template_project/.claude/ukit/index/route-resolver.mjs +34 -2
- package/template_project/.claude/ukit/index/route-task.mjs +243 -38
- package/template_project/.claude/ukit/index/sidecar-decision.mjs +3 -59
- package/template_project/.claude/ukit/index/unic-decision.mjs +27 -14
- package/template_project/.claude/ukit/runtime/cron-operation-adapter.mjs +43 -0
- package/template_project/.claude/ukit/runtime/execution-ledger.mjs +612 -56
- package/template_project/.claude/ukit/runtime/handoff-intent.mjs +122 -6
- package/template_project/.claude/ukit/runtime/loop-state.mjs +1 -1
- package/template_project/.claude/ukit/runtime/operation-evidence.mjs +87 -0
- package/template_project/.claude/ukit/runtime/resumable-run.mjs +417 -0
- package/template_project/.claude/ukit/runtime/stop-coordinator.mjs +635 -28
- package/template_project/.omp/hooks/pre/ukit-bridge.js +40 -5
- package/template_project/AGENTS.md +1 -1
- package/template_project/CLAUDE.md +1 -1
- package/template_project/instructions/core.md +1 -1
- package/template_user/playbooks/feature-implementation.md +9 -0
- package/template_user/playbooks/migration.md +4 -0
|
@@ -697,6 +697,393 @@ export async function resumableRunSourceFingerprint(projectRoot, { indexGenerate
|
|
|
697
697
|
};
|
|
698
698
|
}
|
|
699
699
|
|
|
700
|
+
// --- per-task execution claims (goal-loop duplicate-execution guard) -----------
|
|
701
|
+
//
|
|
702
|
+
// A goal-level run-all loop resumes by re-reading persisted state; without a
|
|
703
|
+
// claim book, a stale resume (or two parallel resumes after a compact) can
|
|
704
|
+
// execute the same task twice. A claim is `{ owner, heartbeat, ttlMs }` recorded
|
|
705
|
+
// per safeTaskId in `.ukit/storage/runs/claims.json` — a SEPARATE file, so
|
|
706
|
+
// previously written C10 records stay byte-compatible and their validation and
|
|
707
|
+
// journaling semantics are untouched. Heartbeats keep a claim alive; a claim
|
|
708
|
+
// whose heartbeat is older than its TTL may be taken over. Every mutation is
|
|
709
|
+
// lock-scoped through the same withAsyncLock discipline as the record itself
|
|
710
|
+
// and published with the atomic tmp+rename write. A lock-busy acquisition fails
|
|
711
|
+
// CLOSED (no claim = no execution right): two claimers must never both proceed
|
|
712
|
+
// on a contended lock.
|
|
713
|
+
|
|
714
|
+
export const TASK_CLAIMS_SCHEMA_VERSION = 1;
|
|
715
|
+
export const DEFAULT_TASK_CLAIM_TTL_MS = 15 * 60 * 1000;
|
|
716
|
+
const CLAIM_OWNER_MAX = 128;
|
|
717
|
+
|
|
718
|
+
export function taskClaimsPath(projectRoot) {
|
|
719
|
+
return path.join(buildRuntimePaths(projectRoot).storageRoot, 'runs', 'claims.json');
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function normalizeClaimOwner(owner) {
|
|
723
|
+
const token = typeof owner === 'string' && owner.trim()
|
|
724
|
+
? owner.trim().slice(0, CLAIM_OWNER_MAX)
|
|
725
|
+
: `pid-${process.pid}-${crypto.randomUUID().slice(0, 8)}`;
|
|
726
|
+
return token;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
function claimActive(claim, now) {
|
|
730
|
+
const heartbeat = Number(claim?.heartbeat);
|
|
731
|
+
const ttl = Number(claim?.ttlMs);
|
|
732
|
+
return Number.isFinite(heartbeat) && Number.isFinite(ttl) && ttl >= 0
|
|
733
|
+
? now - heartbeat <= ttl
|
|
734
|
+
: false;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// Read the claims map. Missing file → empty map; corrupt file → `ok:false`
|
|
738
|
+
// (callers fail closed on mutations, report honestly on reads).
|
|
739
|
+
async function readClaimsMap(projectRoot) {
|
|
740
|
+
try {
|
|
741
|
+
const parsed = JSON.parse(await fs.readFile(taskClaimsPath(projectRoot), 'utf8'));
|
|
742
|
+
const claims = isPlainObject(parsed?.claims) ? parsed.claims : {};
|
|
743
|
+
return { ok: true, claims };
|
|
744
|
+
} catch (error) {
|
|
745
|
+
if (error?.code === 'ENOENT') return { ok: true, claims: {} };
|
|
746
|
+
return { ok: false, reason: 'claims-unreadable' };
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
async function writeClaimsMap(projectRoot, claims) {
|
|
751
|
+
const target = taskClaimsPath(projectRoot);
|
|
752
|
+
await fs.mkdir(path.dirname(target), { recursive: true });
|
|
753
|
+
await writeJsonAtomic(target, { schemaVersion: TASK_CLAIMS_SCHEMA_VERSION, claims });
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Claim exclusive execution of `taskId`. Idempotent for the SAME owner (the
|
|
758
|
+
* heartbeat is refreshed); a second owner inside the TTL gets
|
|
759
|
+
* `{ ok: false, reason: 'active-claim' }`; an expired claim can be taken over.
|
|
760
|
+
*
|
|
761
|
+
* @returns {Promise<{ok:true, claim:{owner,heartbeat,ttlMs}, refreshed?:boolean, tookOver?:boolean}
|
|
762
|
+
* | {ok:false, reason:string, claim?:object}>}
|
|
763
|
+
*/
|
|
764
|
+
export async function acquireTaskClaim(projectRoot, taskId, {
|
|
765
|
+
owner = null,
|
|
766
|
+
sessionId = null,
|
|
767
|
+
ttlMs = DEFAULT_TASK_CLAIM_TTL_MS,
|
|
768
|
+
now = Date.now(),
|
|
769
|
+
deadlineMs = RUN_JOURNAL_LOCK_BUDGET_MS,
|
|
770
|
+
} = {}) {
|
|
771
|
+
const key = safeTaskId(taskId);
|
|
772
|
+
const ownerToken = normalizeClaimOwner(owner);
|
|
773
|
+
const ttl = Number.isFinite(ttlMs) && ttlMs >= 0 ? ttlMs : DEFAULT_TASK_CLAIM_TTL_MS;
|
|
774
|
+
try {
|
|
775
|
+
const outcome = await withAsyncLock(taskClaimsPath(projectRoot), { deadlineMs }, async () => {
|
|
776
|
+
const read = await readClaimsMap(projectRoot);
|
|
777
|
+
if (!read.ok) return { ok: false, reason: read.reason };
|
|
778
|
+
const claims = read.claims;
|
|
779
|
+
const existing = claims[key];
|
|
780
|
+
if (isPlainObject(existing) && claimActive(existing, now)) {
|
|
781
|
+
if (existing.owner === ownerToken) {
|
|
782
|
+
claims[key] = { ...existing, heartbeat: now, ttlMs: existing.ttlMs ?? ttl };
|
|
783
|
+
await writeClaimsMap(projectRoot, claims);
|
|
784
|
+
return { ok: true, claim: { ...claims[key] }, refreshed: true };
|
|
785
|
+
}
|
|
786
|
+
return { ok: false, reason: 'active-claim', claim: { ...existing } };
|
|
787
|
+
}
|
|
788
|
+
// `generation` is the fencing token: every fresh grant or takeover bumps it, so a result
|
|
789
|
+
// committed by an owner whose claim lapsed can be told apart from the current holder's.
|
|
790
|
+
const generation = (Number(existing?.generation) || 0) + 1;
|
|
791
|
+
const claim = {
|
|
792
|
+
owner: ownerToken,
|
|
793
|
+
heartbeat: now,
|
|
794
|
+
ttlMs: ttl,
|
|
795
|
+
generation,
|
|
796
|
+
...(sessionId ? { sessionId: String(sessionId) } : {}),
|
|
797
|
+
};
|
|
798
|
+
claims[key] = claim;
|
|
799
|
+
await writeClaimsMap(projectRoot, claims);
|
|
800
|
+
return {
|
|
801
|
+
ok: true,
|
|
802
|
+
claim: { ...claim },
|
|
803
|
+
...(isPlainObject(existing) ? { tookOver: true } : {}),
|
|
804
|
+
};
|
|
805
|
+
});
|
|
806
|
+
// withAsyncLock wraps the callback result as { ok, value } — unwrap it; a
|
|
807
|
+
// lock failure (ok:false, no value) fails closed below.
|
|
808
|
+
if (outcome?.ok === true && isPlainObject(outcome.value)) return outcome.value;
|
|
809
|
+
return { ok: false, reason: outcome?.value?.reason ?? outcome?.reason ?? 'busy' };
|
|
810
|
+
} catch {
|
|
811
|
+
return { ok: false, reason: 'busy' }; // fail closed: no claim, no execution right
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** Refresh the heartbeat of a claim this owner still holds. Never creates one. */
|
|
816
|
+
export async function heartbeatTaskClaim(projectRoot, taskId, {
|
|
817
|
+
owner = null,
|
|
818
|
+
now = Date.now(),
|
|
819
|
+
deadlineMs = RUN_JOURNAL_LOCK_BUDGET_MS,
|
|
820
|
+
} = {}) {
|
|
821
|
+
const key = safeTaskId(taskId);
|
|
822
|
+
const ownerToken = normalizeClaimOwner(owner);
|
|
823
|
+
try {
|
|
824
|
+
const outcome = await withAsyncLock(taskClaimsPath(projectRoot), { deadlineMs }, async () => {
|
|
825
|
+
const read = await readClaimsMap(projectRoot);
|
|
826
|
+
if (!read.ok) return { ok: false, reason: read.reason };
|
|
827
|
+
const existing = read.claims[key];
|
|
828
|
+
if (!isPlainObject(existing)) return { ok: false, reason: 'no-claim' };
|
|
829
|
+
if (existing.owner !== ownerToken) return { ok: false, reason: 'owner-mismatch' };
|
|
830
|
+
read.claims[key] = { ...existing, heartbeat: now };
|
|
831
|
+
await writeClaimsMap(projectRoot, read.claims);
|
|
832
|
+
return { ok: true, claim: { ...read.claims[key] } };
|
|
833
|
+
});
|
|
834
|
+
if (outcome?.ok === true && isPlainObject(outcome.value)) return outcome.value;
|
|
835
|
+
return { ok: false, reason: outcome?.value?.reason ?? outcome?.reason ?? 'busy' };
|
|
836
|
+
} catch {
|
|
837
|
+
return { ok: false, reason: 'busy' };
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** Release a claim. Only the owner can remove it; absent claims release clean. */
|
|
842
|
+
export async function releaseTaskClaim(projectRoot, taskId, {
|
|
843
|
+
owner = null,
|
|
844
|
+
deadlineMs = RUN_JOURNAL_LOCK_BUDGET_MS,
|
|
845
|
+
} = {}) {
|
|
846
|
+
const key = safeTaskId(taskId);
|
|
847
|
+
const ownerToken = normalizeClaimOwner(owner);
|
|
848
|
+
try {
|
|
849
|
+
const outcome = await withAsyncLock(taskClaimsPath(projectRoot), { deadlineMs }, async () => {
|
|
850
|
+
const read = await readClaimsMap(projectRoot);
|
|
851
|
+
if (!read.ok) return { ok: false, reason: read.reason };
|
|
852
|
+
const existing = read.claims[key];
|
|
853
|
+
if (!isPlainObject(existing)) return { ok: true, released: false };
|
|
854
|
+
if (existing.owner !== ownerToken) return { ok: false, reason: 'owner-mismatch' };
|
|
855
|
+
delete read.claims[key];
|
|
856
|
+
await writeClaimsMap(projectRoot, read.claims);
|
|
857
|
+
return { ok: true, released: true };
|
|
858
|
+
});
|
|
859
|
+
if (outcome?.ok === true && isPlainObject(outcome.value)) return outcome.value;
|
|
860
|
+
return { ok: false, reason: outcome?.value?.reason ?? outcome?.reason ?? 'busy' };
|
|
861
|
+
} catch {
|
|
862
|
+
return { ok: false, reason: 'busy' };
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* Read one claim: `{ claimed: true, active, claim }` when present, else
|
|
868
|
+
* `{ claimed: false }`. Lock-free read — the atomic publish means a reader sees
|
|
869
|
+
* the old or the new map, never a torn one.
|
|
870
|
+
*/
|
|
871
|
+
export async function readTaskClaim(projectRoot, taskId, { now = Date.now() } = {}) {
|
|
872
|
+
const read = await readClaimsMap(projectRoot);
|
|
873
|
+
if (!read.ok) return { claimed: false, unreadable: true };
|
|
874
|
+
const existing = read.claims[safeTaskId(taskId)];
|
|
875
|
+
if (!isPlainObject(existing)) return { claimed: false };
|
|
876
|
+
return { claimed: true, active: claimActive(existing, now), claim: { ...existing } };
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// ─── delegation → claim wiring ──────────────────────────────────────────────
|
|
880
|
+
// The PreToolUse delegation guard acquires, the PostToolUse receipt hook releases. Only
|
|
881
|
+
// write-capable executor agents claim (a reviewer/planner touching the same task must not
|
|
882
|
+
// be blocked by — or block — the executor), and only a task id that names ONE task: the
|
|
883
|
+
// FIRST `TASK-…` in the delegation text is the primary task (later ids are dependencies).
|
|
884
|
+
export const CLAIMING_AGENT_TYPES = new Set(['feature-implementer', 'bug-debugger']);
|
|
885
|
+
export const DELEGATED_CLAIM_TTL_MS = 30 * 60 * 1000;
|
|
886
|
+
const DELEGATED_TASK_ID_RE = /\bTASK-(?:C\d+-)?\d+\b/gi;
|
|
887
|
+
// A TASK id directly after an execution verb is the delegation's primary task; ids elsewhere in
|
|
888
|
+
// the text are dependencies or context ("after TASK-001", "see TASK-004").
|
|
889
|
+
const DELEGATED_PRIMARY_RE = /\b(?:execute|implement|run|do|fix|complete|finish|work\s+on|resume|retry|start)\s+(?:the\s+)?(?:task\s+)?(TASK-(?:C\d+-)?\d+)\b/i;
|
|
890
|
+
|
|
891
|
+
function structuredTaskId(source) {
|
|
892
|
+
const meta = isPlainObject(source?.metadata) ? source.metadata : {};
|
|
893
|
+
const raw = source?.task_id ?? source?.taskId ?? meta.task_id ?? meta.taskId;
|
|
894
|
+
return typeof raw === 'string' && /^TASK-(?:C\d+-)?\d+$/i.test(raw.trim()) ? raw.trim().toUpperCase() : null;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* Resolve the ONE task a delegation executes. Authority order: structured id field > id after an
|
|
899
|
+
* execution verb > the sole distinct id > (several ids, no verb) the first id the caller can
|
|
900
|
+
* validate against the task store. Anything unresolvable claims nothing rather than guessing.
|
|
901
|
+
*/
|
|
902
|
+
function resolveDelegatedTaskId(entry, isKnownTask) {
|
|
903
|
+
if (entry.structured) return entry.structured;
|
|
904
|
+
const verbLed = DELEGATED_PRIMARY_RE.exec(entry.text);
|
|
905
|
+
if (verbLed) return verbLed[1].toUpperCase();
|
|
906
|
+
const ids = [...new Set((entry.text.match(DELEGATED_TASK_ID_RE) || []).map((id) => id.toUpperCase()))];
|
|
907
|
+
if (ids.length === 1) return ids[0];
|
|
908
|
+
if (ids.length > 1 && typeof isKnownTask === 'function') return ids.find((id) => isKnownTask(id)) ?? null;
|
|
909
|
+
return null;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/** @returns {Array<{taskId:string, agent:string}>} */
|
|
913
|
+
export function delegatedClaimTargets(toolInput, { isKnownTask = null } = {}) {
|
|
914
|
+
const input = isPlainObject(toolInput) ? toolInput : {};
|
|
915
|
+
const entries = Array.isArray(input.tasks) && input.tasks.length
|
|
916
|
+
? input.tasks.map((t) => ({
|
|
917
|
+
agent: t?.agent || t?.subagent_type,
|
|
918
|
+
structured: structuredTaskId(t) || structuredTaskId(input),
|
|
919
|
+
text: [t?.task, t?.name, t?.prompt, t?.description].filter((v) => typeof v === 'string').join('\n'),
|
|
920
|
+
}))
|
|
921
|
+
: [{
|
|
922
|
+
agent: input.subagent_type || input.agent || input.agent_type,
|
|
923
|
+
structured: structuredTaskId(input),
|
|
924
|
+
text: [input.prompt, input.description, input.task].filter((v) => typeof v === 'string').join('\n'),
|
|
925
|
+
}];
|
|
926
|
+
const targets = [];
|
|
927
|
+
const seen = new Set();
|
|
928
|
+
for (const entry of entries) {
|
|
929
|
+
const agent = String(entry.agent ?? '').toLowerCase();
|
|
930
|
+
if (!CLAIMING_AGENT_TYPES.has(agent)) continue;
|
|
931
|
+
const taskId = resolveDelegatedTaskId(entry, isKnownTask);
|
|
932
|
+
if (taskId && !seen.has(taskId)) {
|
|
933
|
+
seen.add(taskId);
|
|
934
|
+
targets.push({ taskId, agent });
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
return targets;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
/** Stable owner token for one delegation: the tool-call id, else the session id. */
|
|
941
|
+
export function delegatedClaimOwner(payload) {
|
|
942
|
+
const id = payload?.tool_use_id || payload?.session_id;
|
|
943
|
+
return typeof id === 'string' && id ? `deleg:${id}` : null;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Lease renewal: a delegated agent that runs longer than the claim TTL keeps its claim alive
|
|
948
|
+
* through its own session activity. Any tool event of the owning session (the parent's hooks
|
|
949
|
+
* fire for subagent tool calls too) refreshes that session's active claims, so a LIVE task is
|
|
950
|
+
* never handed to a second agent by TTL alone; only a session that went silent for a full TTL
|
|
951
|
+
* loses its claim. Lock-free pre-check keeps the common call (nothing due) free of I/O writes.
|
|
952
|
+
*/
|
|
953
|
+
export async function renewSessionClaims(projectRoot, sessionId, {
|
|
954
|
+
now = Date.now(),
|
|
955
|
+
deadlineMs = RUN_JOURNAL_LOCK_BUDGET_MS,
|
|
956
|
+
} = {}) {
|
|
957
|
+
const sid = typeof sessionId === 'string' ? sessionId : '';
|
|
958
|
+
if (!sid) return { renewed: 0 };
|
|
959
|
+
const due = (claim) => isPlainObject(claim) && claim.sessionId === sid && claimActive(claim, now)
|
|
960
|
+
&& now - Number(claim.heartbeat) > Number(claim.ttlMs) / 10;
|
|
961
|
+
const peek = await readClaimsMap(projectRoot);
|
|
962
|
+
if (!peek.ok || !Object.values(peek.claims).some(due)) return { renewed: 0 };
|
|
963
|
+
try {
|
|
964
|
+
const outcome = await withAsyncLock(taskClaimsPath(projectRoot), { deadlineMs }, async () => {
|
|
965
|
+
const read = await readClaimsMap(projectRoot);
|
|
966
|
+
if (!read.ok) return { renewed: 0 };
|
|
967
|
+
let renewed = 0;
|
|
968
|
+
for (const [key, claim] of Object.entries(read.claims)) {
|
|
969
|
+
if (due(claim)) {
|
|
970
|
+
read.claims[key] = { ...claim, heartbeat: now };
|
|
971
|
+
renewed += 1;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
if (renewed) await writeClaimsMap(projectRoot, read.claims);
|
|
975
|
+
return { renewed };
|
|
976
|
+
});
|
|
977
|
+
return outcome?.ok === true && isPlainObject(outcome.value) ? outcome.value : { renewed: 0 };
|
|
978
|
+
} catch {
|
|
979
|
+
return { renewed: 0 };
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* Fence check for result commits: true only while `owner` still holds an ACTIVE claim on the
|
|
985
|
+
* task (and, when given, at the same generation). An owner whose lease lapsed and was taken over
|
|
986
|
+
* must not commit — the new holder's generation differs.
|
|
987
|
+
*/
|
|
988
|
+
export async function verifyTaskClaimFence(projectRoot, taskId, { owner, generation = null, now = Date.now() } = {}) {
|
|
989
|
+
const read = await readTaskClaim(projectRoot, taskId, { now });
|
|
990
|
+
if (!read.claimed || !read.active) return false;
|
|
991
|
+
if (read.claim.owner !== owner) return false;
|
|
992
|
+
return generation === null || read.claim.generation === generation;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Count the ACTIVE claims in the registry — the `agents:none-running` exit
|
|
997
|
+
* predicate's input. Returns null only when the registry exists but cannot be
|
|
998
|
+
* read (unknown state must read as unmet, never as "none running").
|
|
999
|
+
*/
|
|
1000
|
+
export async function countActiveTaskClaims(projectRoot, { now = Date.now() } = {}) {
|
|
1001
|
+
const read = await readClaimsMap(projectRoot);
|
|
1002
|
+
if (!read.ok) return null;
|
|
1003
|
+
return Object.values(read.claims).filter((claim) => claimActive(claim, now)).length;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
// --- Archive-time status snapshot ---------------------------------------------------
|
|
1007
|
+
// INDEX.md owns the task lifecycle; a task file's own `- Status:` header is executor-written
|
|
1008
|
+
// and goes stale (`ready`) once the task is archived. `archiveCycleSnapshot` persists the
|
|
1009
|
+
// verified terminal INDEX rows beside the archived task files (`archive/cycle-NN/INDEX.md`),
|
|
1010
|
+
// which is what the Stop predicate (stop-coordinator `evalFinalizedCycles`) reads. Idempotent,
|
|
1011
|
+
// crash-safe (run it BEFORE the INDEX reset; a retry after a partial archive just re-derives),
|
|
1012
|
+
// and fail-closed: a non-terminal live row is recorded as-is, never promoted.
|
|
1013
|
+
|
|
1014
|
+
const TERMINAL_TASK_STATUSES = new Set(['done', 'cancelled_superseded']);
|
|
1015
|
+
|
|
1016
|
+
function indexRows(text) {
|
|
1017
|
+
const rows = new Map();
|
|
1018
|
+
for (const line of String(text).split('\n')) {
|
|
1019
|
+
const row = line.trim();
|
|
1020
|
+
if (!/^\|\s*TASK-/i.test(row)) continue;
|
|
1021
|
+
const cells = row.split('|').slice(1, -1).map((c) => c.trim()); // keep empty cells: columns must not shift
|
|
1022
|
+
rows.set(String(cells[0]).toUpperCase(), { cells, status: String(cells[4] || '').toLowerCase() });
|
|
1023
|
+
}
|
|
1024
|
+
return rows;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Snapshot the live INDEX rows of the tasks that belong to `archive/cycle-<NN>/` (task files
|
|
1029
|
+
* already moved there, or still in `tasks/` and about to move) and stamp each archived task
|
|
1030
|
+
* header with its terminal INDEX status. Returns `{ ok, cycle, snapshotted, stamped, unchanged }`.
|
|
1031
|
+
*/
|
|
1032
|
+
export async function archiveCycleSnapshot(projectRoot, cycle) {
|
|
1033
|
+
const nn = String(cycle).padStart(2, '0');
|
|
1034
|
+
if (!/^\d{2,}$/.test(nn)) return { ok: false, reason: 'bad-cycle' };
|
|
1035
|
+
const ai = path.join(projectRoot, 'docs', 'AI_HANDOFF');
|
|
1036
|
+
const dir = path.join(ai, 'archive', `cycle-${nn}`);
|
|
1037
|
+
const snapPath = path.join(dir, 'INDEX.md');
|
|
1038
|
+
let live = new Map();
|
|
1039
|
+
try { live = indexRows(await fs.readFile(path.join(ai, 'INDEX.md'), 'utf8')); } catch { /* header-only / missing → rely on prior snapshot */ }
|
|
1040
|
+
let prior = new Map();
|
|
1041
|
+
try { prior = indexRows(await fs.readFile(snapPath, 'utf8')); } catch { /* first run */ }
|
|
1042
|
+
// Only task files already inside THIS cycle's archive folder are snapshotted (F3 moves them
|
|
1043
|
+
// first). Task ids restart per cycle, so the live `tasks/` and live INDEX rows of a LATER
|
|
1044
|
+
// cycle must never be attributed here: an existing snapshot row is immutable (prior wins);
|
|
1045
|
+
// a live row only fills an id that has no snapshot row yet.
|
|
1046
|
+
const ids = new Set();
|
|
1047
|
+
try {
|
|
1048
|
+
for (const f of await fs.readdir(dir)) {
|
|
1049
|
+
if (/^TASK-[\w.-]+\.md$/i.test(f)) ids.add(f.replace(/\.md$/i, '').toUpperCase());
|
|
1050
|
+
}
|
|
1051
|
+
} catch { /* absent dir */ }
|
|
1052
|
+
if (ids.size === 0) return { ok: false, reason: 'no-task-files' };
|
|
1053
|
+
const merged = new Map();
|
|
1054
|
+
for (const id of [...ids].sort()) {
|
|
1055
|
+
const row = prior.get(id) || live.get(id);
|
|
1056
|
+
if (row) merged.set(id, row);
|
|
1057
|
+
}
|
|
1058
|
+
const header = '# Archived INDEX snapshot (cycle ' + nn + ')\n\n'
|
|
1059
|
+
+ '| ID | Title | Priority | Size | Status | Owner | Reviewer | File |\n'
|
|
1060
|
+
+ '|----|-------|----------|------|--------|-------|----------|------|\n';
|
|
1061
|
+
const body = [...merged.values()].map((r) => `| ${r.cells.join(' | ')} |`).join('\n');
|
|
1062
|
+
const next = `${header}${body}${body ? '\n' : ''}`;
|
|
1063
|
+
let unchanged = true;
|
|
1064
|
+
let current = null;
|
|
1065
|
+
try { current = await fs.readFile(snapPath, 'utf8'); } catch { /* none */ }
|
|
1066
|
+
if (merged.size > 0 && current !== next) {
|
|
1067
|
+
await fs.mkdir(dir, { recursive: true });
|
|
1068
|
+
const tmp = `${snapPath}.${process.pid}.tmp`;
|
|
1069
|
+
await fs.writeFile(tmp, next, 'utf8');
|
|
1070
|
+
await fs.rename(tmp, snapPath);
|
|
1071
|
+
unchanged = false;
|
|
1072
|
+
}
|
|
1073
|
+
let stamped = 0;
|
|
1074
|
+
for (const [id, row] of merged) {
|
|
1075
|
+
if (!TERMINAL_TASK_STATUSES.has(row.status)) continue;
|
|
1076
|
+
const file = path.join(dir, `${id}.md`);
|
|
1077
|
+
let text;
|
|
1078
|
+
try { text = await fs.readFile(file, 'utf8'); } catch { continue; }
|
|
1079
|
+
const m = text.match(/^(-\s*Status:\s*`?)([\w-]+)(`?)/im);
|
|
1080
|
+
if (!m || m[2].toLowerCase() === row.status) continue;
|
|
1081
|
+
await fs.writeFile(file, text.replace(m[0], `${m[1]}${row.status}${m[3]}`), 'utf8');
|
|
1082
|
+
stamped += 1;
|
|
1083
|
+
}
|
|
1084
|
+
return { ok: true, cycle: nn, snapshotted: merged.size, stamped, unchanged: unchanged && stamped === 0 };
|
|
1085
|
+
}
|
|
1086
|
+
|
|
700
1087
|
// --- CLI (diagnostics) ---------------------------------------------------------
|
|
701
1088
|
|
|
702
1089
|
function parseCliFlags(argv) {
|
|
@@ -762,8 +1149,38 @@ async function main() {
|
|
|
762
1149
|
if (!result.ok && !result.journaled) process.exitCode = 3;
|
|
763
1150
|
return;
|
|
764
1151
|
}
|
|
1152
|
+
if (command === 'archive-cycle' && taskId) {
|
|
1153
|
+
const result = await archiveCycleSnapshot(projectRoot, taskId);
|
|
1154
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
1155
|
+
if (!result.ok) process.exitCode = 3;
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
if ((command === 'claim' || command === 'release') && taskId) {
|
|
1159
|
+
// Main-agent participation in the existing claim registry (no second scheduler): the
|
|
1160
|
+
// orchestrator that must run a task itself claims it first; the guard honours `main:<session>`.
|
|
1161
|
+
const owner = typeof flags.owner === 'string' && flags.owner ? flags.owner : null;
|
|
1162
|
+
if (!owner) {
|
|
1163
|
+
// A random owner token would never match the guard's `main:<session>` check and could
|
|
1164
|
+
// not be released — refuse instead of minting an unusable claim.
|
|
1165
|
+
process.stderr.write('claim|release requires --owner <main:session>\n');
|
|
1166
|
+
process.exitCode = 2;
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
const result = command === 'claim'
|
|
1170
|
+
? await acquireTaskClaim(projectRoot, taskId.toUpperCase(), {
|
|
1171
|
+
owner,
|
|
1172
|
+
sessionId: owner.includes(':') ? owner.slice(owner.indexOf(':') + 1) : null,
|
|
1173
|
+
ttlMs: DELEGATED_CLAIM_TTL_MS,
|
|
1174
|
+
})
|
|
1175
|
+
: await releaseTaskClaim(projectRoot, taskId.toUpperCase(), { owner });
|
|
1176
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
1177
|
+
if (result.ok === false) process.exitCode = 3;
|
|
1178
|
+
return;
|
|
1179
|
+
}
|
|
765
1180
|
process.stderr.write(
|
|
766
1181
|
'usage: resumable-run.mjs read <projectRoot> <taskId> [--boundary <id>]\n'
|
|
1182
|
+
+ ' resumable-run.mjs archive-cycle <projectRoot> <NN>\n'
|
|
1183
|
+
+ ' resumable-run.mjs claim|release <projectRoot> <taskId> --owner <main:session>\n'
|
|
767
1184
|
+ ' resumable-run.mjs suspend <projectRoot> <taskId> [--boundary <id>] '
|
|
768
1185
|
+ '[--phase <phase>] [--next <action>] [--completed <a,b,...>]\n',
|
|
769
1186
|
);
|