@shapeshift-labs/frontier-swarm 0.5.25 → 0.5.26
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/index.d.ts +73 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +165 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRunDashboardSnapshot, createRunEdgeEvent, createRunEvent, createRunNodeEvent, defineRunArtifact, defineRunAttempt, defineRunDecision, defineRunEvidence, defineRunLane, defineRunPatch, defineRunTask, defineRunVerification, linkRunNodes, replayRunEvents } from '@shapeshift-labs/frontier-run';
|
|
2
|
-
import { acquireSemanticLease, createSemanticLeaseState, defineSemanticLeaseScope, validateSemanticLeaseFence } from '@shapeshift-labs/frontier-lease';
|
|
2
|
+
import { acquireSemanticLease, createSemanticLeaseFence, createSemanticLeaseState, defineSemanticLeaseScope, validateSemanticLeaseFence } from '@shapeshift-labs/frontier-lease';
|
|
3
3
|
export const FRONTIER_SWARM_MANIFEST_KIND = 'frontier.swarm.manifest';
|
|
4
4
|
export const FRONTIER_SWARM_MANIFEST_VERSION = 1;
|
|
5
5
|
export const FRONTIER_SWARM_TASK_KIND = 'frontier.swarm.task';
|
|
@@ -4481,6 +4481,160 @@ export function validateSwarmCoordinatorSemanticLeaseFence(input) {
|
|
|
4481
4481
|
scopes: requiredScopes.length ? requiredScopes : undefined
|
|
4482
4482
|
});
|
|
4483
4483
|
}
|
|
4484
|
+
export function claimSwarmCoordinatorQueueItem(input) {
|
|
4485
|
+
const generatedAt = input.now ?? Date.now();
|
|
4486
|
+
const coordinatorId = input.coordinatorId ?? 'frontier-swarm-coordinator';
|
|
4487
|
+
const assignment = input.assignment ?? findCoordinatorQueueAssignment(input.queue, input);
|
|
4488
|
+
const reasons = assignment
|
|
4489
|
+
? uniqueStrings(['queue-item-claimed', ...(assignment.reasons ?? [])])
|
|
4490
|
+
: uniqueStrings(['missing-queue-assignment']);
|
|
4491
|
+
const requiredSemanticLeaseScopes = assignment
|
|
4492
|
+
? semanticLeaseScopesForCoordinatorAssignment(input.queue, assignment, { metadata: input.metadata }).map(cloneSemanticLeaseScope)
|
|
4493
|
+
: [];
|
|
4494
|
+
const requiredLeaseScopeIds = assignment ? coordinatorAssignmentRequiredLeaseScopeIds(assignment) : [];
|
|
4495
|
+
const requiredLeaseKeys = assignment ? coordinatorAssignmentRequiredLeaseKeys(assignment, requiredSemanticLeaseScopes) : [];
|
|
4496
|
+
return {
|
|
4497
|
+
id: 'swarm-coordinator-queue-item-claim:' + stableHash([input.queue.id, assignment?.jobId, input.queueItemId, input.jobId, coordinatorId, generatedAt]),
|
|
4498
|
+
queueId: input.queue.id,
|
|
4499
|
+
mergeIndexId: input.queue.mergeIndexId,
|
|
4500
|
+
generatedAt,
|
|
4501
|
+
coordinatorId,
|
|
4502
|
+
claimed: Boolean(assignment),
|
|
4503
|
+
...(assignment?.jobId ? { jobId: assignment.jobId } : {}),
|
|
4504
|
+
...(assignment?.taskId ? { taskId: assignment.taskId } : {}),
|
|
4505
|
+
...(assignment?.lane ? { lane: assignment.lane } : {}),
|
|
4506
|
+
queueItemIds: assignment ? [...assignment.queueItemIds] : uniqueStrings([input.queueItemId, input.jobId]),
|
|
4507
|
+
...(assignment ? { queueIdForAssignment: assignment.scopeId, assignedAction: assignment.action } : {}),
|
|
4508
|
+
requiredLeaseScopeIds,
|
|
4509
|
+
requiredLeaseKeys,
|
|
4510
|
+
requiredSemanticLeaseScopes,
|
|
4511
|
+
reasons,
|
|
4512
|
+
...(assignment ? { assignment: cloneJsonValue(assignment) } : {}),
|
|
4513
|
+
...(toJsonObject(input.metadata) ? { metadata: toJsonObject(input.metadata) } : {})
|
|
4514
|
+
};
|
|
4515
|
+
}
|
|
4516
|
+
export function claimSwarmCoordinatorQueueLease(input) {
|
|
4517
|
+
const generatedAt = input.now ?? Date.now();
|
|
4518
|
+
const assignment = input.claim.assignment;
|
|
4519
|
+
if (!assignment) {
|
|
4520
|
+
const state = input.state ?? createSemanticLeaseState({ id: `frontier-swarm-merge-queue:${input.queue.id}` });
|
|
4521
|
+
const mutation = {
|
|
4522
|
+
state,
|
|
4523
|
+
outcome: 'denied',
|
|
4524
|
+
granted: false,
|
|
4525
|
+
conflicts: [],
|
|
4526
|
+
events: [],
|
|
4527
|
+
evidence: {
|
|
4528
|
+
kind: 'frontier.semantic-lease.evidence',
|
|
4529
|
+
version: 1,
|
|
4530
|
+
stateId: state.id,
|
|
4531
|
+
outcome: 'denied',
|
|
4532
|
+
beforeHash: stableHash(state),
|
|
4533
|
+
afterHash: stableHash(state),
|
|
4534
|
+
eventCount: 0,
|
|
4535
|
+
activeLeaseCount: 0,
|
|
4536
|
+
conflictCount: 0,
|
|
4537
|
+
replayVerified: true
|
|
4538
|
+
}
|
|
4539
|
+
};
|
|
4540
|
+
return {
|
|
4541
|
+
id: 'swarm-coordinator-queue-lease-claim:' + stableHash([input.queue.id, input.claim.id, generatedAt, 'missing-assignment']),
|
|
4542
|
+
queueId: input.queue.id,
|
|
4543
|
+
claimId: input.claim.id,
|
|
4544
|
+
generatedAt,
|
|
4545
|
+
granted: false,
|
|
4546
|
+
state,
|
|
4547
|
+
mutation,
|
|
4548
|
+
scopes: [],
|
|
4549
|
+
requiredLeaseScopeIds: [],
|
|
4550
|
+
requiredLeaseKeys: [],
|
|
4551
|
+
reasons: ['missing-queue-assignment']
|
|
4552
|
+
};
|
|
4553
|
+
}
|
|
4554
|
+
const acquired = acquireSwarmCoordinatorSemanticLease({
|
|
4555
|
+
...input,
|
|
4556
|
+
assignment,
|
|
4557
|
+
ownerId: input.ownerId,
|
|
4558
|
+
holderId: input.holderId,
|
|
4559
|
+
now: generatedAt,
|
|
4560
|
+
metadata: mergeSwarmMetadata([
|
|
4561
|
+
toJsonObject(input.metadata),
|
|
4562
|
+
{
|
|
4563
|
+
claimId: input.claim.id,
|
|
4564
|
+
coordinatorId: input.claim.coordinatorId,
|
|
4565
|
+
queueItemIds: input.claim.queueItemIds
|
|
4566
|
+
}
|
|
4567
|
+
])
|
|
4568
|
+
});
|
|
4569
|
+
return {
|
|
4570
|
+
id: 'swarm-coordinator-queue-lease-claim:' + stableHash([input.queue.id, input.claim.id, acquired.lease?.id, generatedAt]),
|
|
4571
|
+
queueId: input.queue.id,
|
|
4572
|
+
claimId: input.claim.id,
|
|
4573
|
+
generatedAt,
|
|
4574
|
+
granted: acquired.mutation.granted,
|
|
4575
|
+
state: acquired.state,
|
|
4576
|
+
mutation: acquired.mutation,
|
|
4577
|
+
scopes: acquired.scopes,
|
|
4578
|
+
requiredLeaseScopeIds: acquired.requiredLeaseScopeIds,
|
|
4579
|
+
requiredLeaseKeys: acquired.requiredLeaseKeys,
|
|
4580
|
+
...(acquired.lease ? { lease: acquired.lease, fence: createSemanticLeaseFence(acquired.lease) } : {}),
|
|
4581
|
+
reasons: acquired.mutation.granted ? ['semantic-lease-granted'] : uniqueStrings(['semantic-lease-denied', ...acquired.mutation.conflicts.map((conflict) => conflict.reason)])
|
|
4582
|
+
};
|
|
4583
|
+
}
|
|
4584
|
+
export function decideSwarmCoordinatorClaim(input) {
|
|
4585
|
+
const generatedAt = input.generatedAt ?? Date.now();
|
|
4586
|
+
const assignment = input.claim.assignment;
|
|
4587
|
+
const leaseBlocked = input.leaseClaim && !input.leaseClaim.granted;
|
|
4588
|
+
const decision = createSwarmQueueOutcomeDecision({
|
|
4589
|
+
subjectId: input.claim.queueItemIds[0] ?? input.claim.jobId ?? input.claim.id,
|
|
4590
|
+
subjectAliases: uniqueStrings([input.claim.id, input.leaseClaim?.id, ...(input.claim.queueItemIds ?? [])]),
|
|
4591
|
+
jobId: input.claim.jobId,
|
|
4592
|
+
taskId: input.claim.taskId,
|
|
4593
|
+
queueItemIds: input.claim.queueItemIds,
|
|
4594
|
+
queueId: input.claim.queueIdForAssignment ?? input.claim.queueId,
|
|
4595
|
+
lane: input.claim.lane,
|
|
4596
|
+
action: assignment?.action,
|
|
4597
|
+
assignedAction: assignment?.action,
|
|
4598
|
+
decision: input.decision ?? (leaseBlocked ? 'blocked' : assignment ? coordinatorAgentDrainDecisionForAction(assignment.action) : 'blocked'),
|
|
4599
|
+
category: input.category,
|
|
4600
|
+
outcome: input.outcome,
|
|
4601
|
+
terminal: input.terminal,
|
|
4602
|
+
reasons: uniqueStrings([
|
|
4603
|
+
...input.claim.reasons,
|
|
4604
|
+
...(input.leaseClaim?.reasons ?? []),
|
|
4605
|
+
...(input.reasons ?? [])
|
|
4606
|
+
]),
|
|
4607
|
+
disposition: assignment?.disposition,
|
|
4608
|
+
mergeReadiness: assignment?.mergeReadiness,
|
|
4609
|
+
status: input.status,
|
|
4610
|
+
conflictingJobIds: input.conflictingJobIds ?? assignment?.conflictingJobIds,
|
|
4611
|
+
generatedAt,
|
|
4612
|
+
metadata: mergeSwarmMetadata([
|
|
4613
|
+
toJsonObject(input.metadata),
|
|
4614
|
+
{
|
|
4615
|
+
source: 'frontier-swarm.coordinator-claim',
|
|
4616
|
+
claimId: input.claim.id,
|
|
4617
|
+
leaseClaimId: input.leaseClaim?.id,
|
|
4618
|
+
semanticLeaseId: input.leaseClaim?.lease?.id,
|
|
4619
|
+
fencingToken: input.leaseClaim?.lease?.fencingToken,
|
|
4620
|
+
requiredLeaseScopeIds: input.claim.requiredLeaseScopeIds,
|
|
4621
|
+
requiredLeaseKeys: input.claim.requiredLeaseKeys
|
|
4622
|
+
}
|
|
4623
|
+
])
|
|
4624
|
+
});
|
|
4625
|
+
return {
|
|
4626
|
+
claim: input.claim,
|
|
4627
|
+
...(input.leaseClaim ? { leaseClaim: input.leaseClaim } : {}),
|
|
4628
|
+
decision,
|
|
4629
|
+
runEvents: createRunEventsFromCoordinatorClaimDecision(decision)
|
|
4630
|
+
};
|
|
4631
|
+
}
|
|
4632
|
+
export function createRunEventsFromCoordinatorClaimDecision(decision, options = {}) {
|
|
4633
|
+
return createRunEventsFromCoordinatorDecision(decision, {
|
|
4634
|
+
actorId: options.actorId ?? 'frontier-swarm-coordinator',
|
|
4635
|
+
...options
|
|
4636
|
+
});
|
|
4637
|
+
}
|
|
4484
4638
|
export function summarizeSwarmCoordinatorAgentDrainWork(work) {
|
|
4485
4639
|
const activeAssignments = work.assignments.filter((assignment) => !coordinatorAgentDrainAssignmentIsTerminal(assignment));
|
|
4486
4640
|
const terminalAssignments = work.assignments.filter((assignment) => coordinatorAgentDrainAssignmentIsTerminal(assignment));
|
|
@@ -6126,6 +6280,16 @@ function semanticLeaseScopesForCoordinatorAssignment(queue, assignment, input =
|
|
|
6126
6280
|
])
|
|
6127
6281
|
})];
|
|
6128
6282
|
}
|
|
6283
|
+
function findCoordinatorQueueAssignment(queue, input) {
|
|
6284
|
+
if (input.queueItemId) {
|
|
6285
|
+
const byQueueItem = queue.assignments.find((assignment) => assignment.queueItemIds.includes(input.queueItemId));
|
|
6286
|
+
if (byQueueItem)
|
|
6287
|
+
return byQueueItem;
|
|
6288
|
+
}
|
|
6289
|
+
if (input.jobId)
|
|
6290
|
+
return queue.assignments.find((assignment) => assignment.jobId === input.jobId);
|
|
6291
|
+
return queue.assignments[0];
|
|
6292
|
+
}
|
|
6129
6293
|
function coordinatorAssignmentRequiredLeaseScopeIds(assignment) {
|
|
6130
6294
|
const explicit = assignment.requiredLeaseScopeIds ?? [];
|
|
6131
6295
|
if (explicit.length)
|