@agenticprimitives/coordination 0.0.0-alpha.29 → 0.0.0-alpha.30
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/README.md +5 -1
- package/dist/attribution/index.d.ts +6 -1
- package/dist/attribution/index.d.ts.map +1 -1
- package/dist/attribution/index.js +26 -2
- package/dist/attribution/index.js.map +1 -1
- package/dist/core/events.d.ts +2 -0
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/provenance.d.ts.map +1 -1
- package/dist/core/provenance.js +44 -1
- package/dist/core/provenance.js.map +1 -1
- package/dist/core/reducer.d.ts +4 -0
- package/dist/core/reducer.d.ts.map +1 -1
- package/dist/core/reducer.js +163 -46
- package/dist/core/reducer.js.map +1 -1
- package/dist/decisions/types.d.ts +4 -0
- package/dist/decisions/types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/participation/index.d.ts +2 -0
- package/dist/participation/index.d.ts.map +1 -1
- package/dist/participation/index.js +1 -1
- package/dist/participation/index.js.map +1 -1
- package/dist/participation/payload.d.ts +17 -0
- package/dist/participation/payload.d.ts.map +1 -0
- package/dist/participation/payload.js +35 -0
- package/dist/participation/payload.js.map +1 -0
- package/dist/planning/types.d.ts +2 -0
- package/dist/planning/types.d.ts.map +1 -1
- package/dist/projections/continuation.d.ts.map +1 -1
- package/dist/projections/continuation.js +2 -1
- package/dist/projections/continuation.js.map +1 -1
- package/package.json +5 -4
package/dist/core/reducer.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// Deterministic: ids and instants come from commands/events, never from this module.
|
|
4
4
|
import { COORDINATION_COMMAND_KINDS } from './commands.js';
|
|
5
5
|
import { planContentHash } from '../planning/hash.js';
|
|
6
|
+
import { jcsCanonicalize } from '@agenticprimitives/situations';
|
|
7
|
+
import { commitmentPayloadHash } from '../participation/payload.js';
|
|
6
8
|
import { validatePlanContent } from '../planning/validate.js';
|
|
7
9
|
export function initialCoordinationState() {
|
|
8
10
|
return {
|
|
@@ -29,6 +31,20 @@ export const planKey = (planId, revision) => `${planId}@${revision}`;
|
|
|
29
31
|
const TERMINAL_LIFECYCLES = new Set(['satisfied', 'abandoned']);
|
|
30
32
|
const eventId = (revision, kind) => `evt_${String(revision).padStart(4, '0')}_${kind}`;
|
|
31
33
|
const reject = (reason) => ({ ok: false, reason });
|
|
34
|
+
/** OWN-PROPERTY lookup (spec 445 / REM-15). Every map here is keyed by a caller-supplied id; a bare `map[id]`
|
|
35
|
+
* answers `constructor` / `toString` / `__proto__` with an inherited member, which reads as "already exists". */
|
|
36
|
+
function own(map, key) {
|
|
37
|
+
return Object.hasOwn(map, key) ? map[key] : undefined;
|
|
38
|
+
}
|
|
39
|
+
/** Two definitions are THE SAME only when their RFC 8785 bytes are (spec 445 / REM-7). */
|
|
40
|
+
const sameContent = (a, b) => a !== undefined && b !== undefined && jcsCanonicalize(a) === jcsCanonicalize(b);
|
|
41
|
+
/** The step `stepId` as plan revision `ref` defined it, or undefined. */
|
|
42
|
+
function stepIn(state, ref, stepId) {
|
|
43
|
+
if (!ref)
|
|
44
|
+
return undefined;
|
|
45
|
+
const plan = own(state.plans, planKey(ref.planId, ref.revision));
|
|
46
|
+
return plan && plan.contentHash === ref.hash ? plan.steps.find((s) => s.stepId === stepId) : undefined;
|
|
47
|
+
}
|
|
32
48
|
function activeRoles(state, address) {
|
|
33
49
|
const roles = new Set();
|
|
34
50
|
for (const p of Object.values(state.participations)) {
|
|
@@ -72,7 +88,7 @@ function adoptedPlan(state) {
|
|
|
72
88
|
const ref = state.endeavor?.adoptedPlanRef;
|
|
73
89
|
if (!ref)
|
|
74
90
|
return undefined;
|
|
75
|
-
return state.plans
|
|
91
|
+
return own(state.plans, planKey(ref.planId, ref.revision));
|
|
76
92
|
}
|
|
77
93
|
function samePlanRef(a, b) {
|
|
78
94
|
return a.planId === b.planId && a.revision === b.revision && a.hash === b.hash;
|
|
@@ -80,6 +96,20 @@ function samePlanRef(a, b) {
|
|
|
80
96
|
export function validateCoordinationCommand(state, command) {
|
|
81
97
|
const nextRevision = state.revision + 1;
|
|
82
98
|
const at = command.issuedAt;
|
|
99
|
+
// Unknown kinds are refused FIRST and in their own words (conversation never mutates state).
|
|
100
|
+
if (!COORDINATION_COMMAND_KINDS.has(command.kind ?? '')) {
|
|
101
|
+
return reject(`unknown command kind ${command.kind ?? '<none>'}`);
|
|
102
|
+
}
|
|
103
|
+
// THE CLOCK (spec 445 / REM-5). `issuedAt` is the SERVING PLANE's instant, never the caller's: it must be a
|
|
104
|
+
// real instant, and it may not precede the last event applied. Every time-bound rule below (a decision
|
|
105
|
+
// window, a breach's deadline) is judged against it, so a back-dated or post-dated command is refused here
|
|
106
|
+
// rather than believed there.
|
|
107
|
+
const atMs = typeof at === 'string' ? Date.parse(at) : Number.NaN;
|
|
108
|
+
if (Number.isNaN(atMs))
|
|
109
|
+
return reject('issuedAt must be an ISO-8601 instant — the serving plane stamps it');
|
|
110
|
+
if (state.lastEventAt !== undefined && atMs < Date.parse(state.lastEventAt)) {
|
|
111
|
+
return reject(`issuedAt ${at} precedes the last recorded event (${state.lastEventAt}) — an instant is stamped by the serving plane and never runs backwards`);
|
|
112
|
+
}
|
|
83
113
|
switch (command.kind) {
|
|
84
114
|
case 'SubmitEndeavorRequest': {
|
|
85
115
|
if (state.request)
|
|
@@ -243,7 +273,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
243
273
|
const structural = validatePlanContent(content);
|
|
244
274
|
if (!structural.ok)
|
|
245
275
|
return reject(structural.reason);
|
|
246
|
-
if (state.plans
|
|
276
|
+
if (own(state.plans, planKey(command.planId, command.revision))) {
|
|
247
277
|
return reject(`plan revision ${planKey(command.planId, command.revision)} already exists`);
|
|
248
278
|
}
|
|
249
279
|
let maxRevision = 0;
|
|
@@ -280,7 +310,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
280
310
|
return reject(ctx.reason);
|
|
281
311
|
if (!isSteward(state, command.actor))
|
|
282
312
|
return reject('only a sponsor/coordinator may adopt a plan');
|
|
283
|
-
const plan = state.plans
|
|
313
|
+
const plan = own(state.plans, planKey(command.planRef.planId, command.planRef.revision));
|
|
284
314
|
if (!plan)
|
|
285
315
|
return reject(`unknown plan revision ${planKey(command.planRef.planId, command.planRef.revision)}`);
|
|
286
316
|
if (plan.status !== 'proposed')
|
|
@@ -311,7 +341,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
311
341
|
return reject(ctx.reason);
|
|
312
342
|
if (!isSteward(state, command.actor))
|
|
313
343
|
return reject('only a sponsor/coordinator may reject a plan');
|
|
314
|
-
const plan = state.plans
|
|
344
|
+
const plan = own(state.plans, planKey(command.planId, command.revision));
|
|
315
345
|
if (!plan)
|
|
316
346
|
return reject(`unknown plan revision ${planKey(command.planId, command.revision)}`);
|
|
317
347
|
if (plan.status !== 'proposed')
|
|
@@ -339,7 +369,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
339
369
|
return reject(ctx.reason);
|
|
340
370
|
if (!isSteward(state, command.actor))
|
|
341
371
|
return reject('only a sponsor/coordinator may invite');
|
|
342
|
-
if (state.participations
|
|
372
|
+
if (own(state.participations, command.participationId)) {
|
|
343
373
|
return reject(`participation ${command.participationId} already exists`);
|
|
344
374
|
}
|
|
345
375
|
return {
|
|
@@ -364,7 +394,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
364
394
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
365
395
|
if ('reason' in ctx)
|
|
366
396
|
return reject(ctx.reason);
|
|
367
|
-
const participation = state.participations
|
|
397
|
+
const participation = own(state.participations, command.participationId);
|
|
368
398
|
if (!participation)
|
|
369
399
|
return reject(`unknown participation ${command.participationId}`);
|
|
370
400
|
if (participation.status !== 'invited')
|
|
@@ -393,7 +423,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
393
423
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
394
424
|
if ('reason' in ctx)
|
|
395
425
|
return reject(ctx.reason);
|
|
396
|
-
const participation = state.participations
|
|
426
|
+
const participation = own(state.participations, command.participationId);
|
|
397
427
|
if (!participation)
|
|
398
428
|
return reject(`unknown participation ${command.participationId}`);
|
|
399
429
|
if (participation.status !== 'active')
|
|
@@ -421,9 +451,9 @@ export function validateCoordinationCommand(state, command) {
|
|
|
421
451
|
if (participationStatus(state, command.actor) === 'none') {
|
|
422
452
|
return reject('contribution proposer must be a participant or invitee');
|
|
423
453
|
}
|
|
424
|
-
if (state.proposals
|
|
454
|
+
if (own(state.proposals, command.proposalId))
|
|
425
455
|
return reject(`proposal ${command.proposalId} already exists`);
|
|
426
|
-
const plan = state.plans
|
|
456
|
+
const plan = own(state.plans, planKey(command.planRef.planId, command.planRef.revision));
|
|
427
457
|
if (!plan)
|
|
428
458
|
return reject(`unknown plan revision ${planKey(command.planRef.planId, command.planRef.revision)}`);
|
|
429
459
|
if (command.planRef.hash !== plan.contentHash)
|
|
@@ -459,16 +489,18 @@ export function validateCoordinationCommand(state, command) {
|
|
|
459
489
|
return reject(ctx.reason);
|
|
460
490
|
if (!isSteward(state, command.actor))
|
|
461
491
|
return reject('only a sponsor/coordinator may allocate');
|
|
462
|
-
const proposal = state.proposals
|
|
492
|
+
const proposal = own(state.proposals, command.proposalRef);
|
|
463
493
|
if (!proposal)
|
|
464
494
|
return reject(`unknown proposal ${command.proposalRef}`);
|
|
465
495
|
if (proposal.status !== 'open')
|
|
466
496
|
return reject(`proposal is ${proposal.status}, not open`);
|
|
467
|
-
if (state.allocations
|
|
497
|
+
if (own(state.allocations, command.allocationId))
|
|
468
498
|
return reject(`allocation ${command.allocationId} already exists`);
|
|
469
499
|
if (participationStatus(state, command.participant) === 'none') {
|
|
470
500
|
return reject('allocated participant must be a participant or invitee');
|
|
471
501
|
}
|
|
502
|
+
if (command.steps.length === 0)
|
|
503
|
+
return reject('an allocation must name at least one step');
|
|
472
504
|
const proposed = new Set(proposal.steps);
|
|
473
505
|
for (const stepId of command.steps) {
|
|
474
506
|
if (!proposed.has(stepId))
|
|
@@ -496,7 +528,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
496
528
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
497
529
|
if ('reason' in ctx)
|
|
498
530
|
return reject(ctx.reason);
|
|
499
|
-
const allocation = state.allocations
|
|
531
|
+
const allocation = own(state.allocations, command.allocationRef);
|
|
500
532
|
if (!allocation)
|
|
501
533
|
return reject(`unknown allocation ${command.allocationRef}`);
|
|
502
534
|
if (allocation.status !== 'allocated')
|
|
@@ -506,7 +538,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
506
538
|
if (participationStatus(state, command.actor) === 'none') {
|
|
507
539
|
return reject('commitment requires an existing participation or invitation');
|
|
508
540
|
}
|
|
509
|
-
if (state.commitments
|
|
541
|
+
if (own(state.commitments, command.commitmentId))
|
|
510
542
|
return reject(`commitment ${command.commitmentId} already exists`);
|
|
511
543
|
const adopted = ctx.endeavor.adoptedPlanRef;
|
|
512
544
|
if (!adopted)
|
|
@@ -514,6 +546,8 @@ export function validateCoordinationCommand(state, command) {
|
|
|
514
546
|
if (!samePlanRef(command.planRef, adopted)) {
|
|
515
547
|
return reject('commitment plan revision hash is stale — commitments bind the adopted revision hash');
|
|
516
548
|
}
|
|
549
|
+
if (command.steps.length === 0)
|
|
550
|
+
return reject('a commitment must name at least one step');
|
|
517
551
|
const allocated = new Set(allocation.steps);
|
|
518
552
|
for (const stepId of command.steps) {
|
|
519
553
|
if (!allocated.has(stepId))
|
|
@@ -521,6 +555,17 @@ export function validateCoordinationCommand(state, command) {
|
|
|
521
555
|
}
|
|
522
556
|
if (command.signature.signer !== command.actor)
|
|
523
557
|
return reject('commitment signature signer must be the actor');
|
|
558
|
+
// A deadline is what a breach is later judged against: it is an instant, and it is still ahead.
|
|
559
|
+
if (command.bounds?.deadline !== undefined && !(Date.parse(command.bounds.deadline) > atMs)) {
|
|
560
|
+
return reject('a commitment deadline must be an ISO-8601 instant after the commitment itself');
|
|
561
|
+
}
|
|
562
|
+
// THE SIGNED PREIMAGE (spec 445 / REM-8): the hash the participant signed must be THIS commitment's —
|
|
563
|
+
// endeavor, allocation, the exact adopted revision, the steps and the bounds. "Some hash this signer
|
|
564
|
+
// once signed" is not a commitment.
|
|
565
|
+
const expectedPayloadHash = commitmentPayloadHash({ endeavorId: command.endeavorId, allocationRef: command.allocationRef, planRef: command.planRef, steps: command.steps, ...(command.bounds ? { bounds: command.bounds } : {}) });
|
|
566
|
+
if (typeof command.signature.payloadHash !== 'string' || command.signature.payloadHash.toLowerCase() !== expectedPayloadHash) {
|
|
567
|
+
return reject('commitment signature payloadHash is not the canonical commitment payload hash (commitmentPayloadHash) — the signature must be over THIS commitment');
|
|
568
|
+
}
|
|
524
569
|
return {
|
|
525
570
|
ok: true,
|
|
526
571
|
events: [
|
|
@@ -545,7 +590,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
545
590
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
546
591
|
if ('reason' in ctx)
|
|
547
592
|
return reject(ctx.reason);
|
|
548
|
-
const commitment = state.commitments
|
|
593
|
+
const commitment = own(state.commitments, command.commitmentId);
|
|
549
594
|
if (!commitment)
|
|
550
595
|
return reject(`unknown commitment ${command.commitmentId}`);
|
|
551
596
|
if (commitment.status !== 'active')
|
|
@@ -573,13 +618,13 @@ export function validateCoordinationCommand(state, command) {
|
|
|
573
618
|
return reject(ctx.reason);
|
|
574
619
|
if (!isSteward(state, command.actor))
|
|
575
620
|
return reject('only a sponsor/coordinator may reallocate');
|
|
576
|
-
const commitment = state.commitments
|
|
621
|
+
const commitment = own(state.commitments, command.commitmentId);
|
|
577
622
|
if (!commitment)
|
|
578
623
|
return reject(`unknown commitment ${command.commitmentId}`);
|
|
579
624
|
if (commitment.status !== 'active' && commitment.status !== 'withdrawn') {
|
|
580
625
|
return reject(`commitment is ${commitment.status}; only active/withdrawn commitments reallocate`);
|
|
581
626
|
}
|
|
582
|
-
if (state.allocations
|
|
627
|
+
if (own(state.allocations, command.allocationId))
|
|
583
628
|
return reject(`allocation ${command.allocationId} already exists`);
|
|
584
629
|
if (participationStatus(state, command.participant) === 'none') {
|
|
585
630
|
return reject('reallocated participant must be a participant or invitee');
|
|
@@ -612,7 +657,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
612
657
|
participationStatus(state, command.actor) !== 'active') {
|
|
613
658
|
return reject('a run is attached by the managing principal or an active participant');
|
|
614
659
|
}
|
|
615
|
-
const already = state.attachedRuns
|
|
660
|
+
const already = own(state.attachedRuns, command.runRef);
|
|
616
661
|
if (already) {
|
|
617
662
|
// IDEMPOTENT, but only for the SAME record. Re-attaching the same run is the same assertion made
|
|
618
663
|
// twice. A different digest under the same runRef is a different claim and is refused rather than
|
|
@@ -642,7 +687,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
642
687
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
643
688
|
if ('reason' in ctx)
|
|
644
689
|
return reject(ctx.reason);
|
|
645
|
-
const c = state.commitments
|
|
690
|
+
const c = own(state.commitments, command.commitmentId);
|
|
646
691
|
if (!c)
|
|
647
692
|
return reject(`unknown commitment ${command.commitmentId}`);
|
|
648
693
|
if (c.status !== 'active')
|
|
@@ -653,6 +698,11 @@ export function validateCoordinationCommand(state, command) {
|
|
|
653
698
|
return reject('a discharge must cite receipt references or a satisfied plan step');
|
|
654
699
|
if (command.actor !== c.participant)
|
|
655
700
|
return reject('only the committing participant may discharge their own commitment');
|
|
701
|
+
// A participant who has WITHDRAWN no longer writes to this endeavor's record (spec 445 / REM-6): what is
|
|
702
|
+
// left of their commitment is the steward's to record as a lapse.
|
|
703
|
+
if (participationStatus(state, command.actor) === 'none') {
|
|
704
|
+
return reject('a discharge is recorded by a current participant — a withdrawn participant\'s commitment is the steward\'s to record as lapsed');
|
|
705
|
+
}
|
|
656
706
|
return {
|
|
657
707
|
ok: true,
|
|
658
708
|
events: [
|
|
@@ -673,7 +723,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
673
723
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
674
724
|
if ('reason' in ctx)
|
|
675
725
|
return reject(ctx.reason);
|
|
676
|
-
const c = state.commitments
|
|
726
|
+
const c = own(state.commitments, command.commitmentId);
|
|
677
727
|
if (!c)
|
|
678
728
|
return reject(`unknown commitment ${command.commitmentId}`);
|
|
679
729
|
if (c.status !== 'active')
|
|
@@ -708,7 +758,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
708
758
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
709
759
|
if ('reason' in ctx)
|
|
710
760
|
return reject(ctx.reason);
|
|
711
|
-
const c = state.commitments
|
|
761
|
+
const c = own(state.commitments, command.commitmentId);
|
|
712
762
|
if (!c)
|
|
713
763
|
return reject(`unknown commitment ${command.commitmentId}`);
|
|
714
764
|
if (c.status !== 'active')
|
|
@@ -739,10 +789,15 @@ export function validateCoordinationCommand(state, command) {
|
|
|
739
789
|
const adopted = ctx.endeavor.adoptedPlanRef;
|
|
740
790
|
if (!plan || !adopted)
|
|
741
791
|
return reject('no adopted plan');
|
|
742
|
-
|
|
792
|
+
const step = plan.steps.find((s) => s.stepId === command.stepId);
|
|
793
|
+
if (!step)
|
|
743
794
|
return reject(`step ${command.stepId} is not in the adopted plan`);
|
|
795
|
+
// A DECISION step is satisfied by a decision of its DECLARED approvers and by nothing else (spec 393 /
|
|
796
|
+
// spec 445 NAME-12): recorded directly, any participant could clear a checkpoint alone.
|
|
797
|
+
if (step.kind === 'decision') {
|
|
798
|
+
return reject(`step ${command.stepId} is a decision step — it is satisfied only by a decision of its declared approvers (RaiseDecisionRequest → RecordDecision)`);
|
|
744
799
|
}
|
|
745
|
-
if (state.satisfiedSteps
|
|
800
|
+
if (own(state.satisfiedSteps, command.stepId))
|
|
746
801
|
return reject(`step ${command.stepId} already satisfied`);
|
|
747
802
|
if (command.evidenceRefs.length === 0)
|
|
748
803
|
return reject('step satisfaction requires completion evidence');
|
|
@@ -777,10 +832,15 @@ export function validateCoordinationCommand(state, command) {
|
|
|
777
832
|
if (!plan.milestones.some((m) => m.milestoneId === command.milestoneId)) {
|
|
778
833
|
return reject(`milestone ${command.milestoneId} is not in the adopted plan`);
|
|
779
834
|
}
|
|
780
|
-
if (state.milestones
|
|
835
|
+
if (own(state.milestones, command.milestoneId))
|
|
781
836
|
return reject(`milestone ${command.milestoneId} already achieved`);
|
|
782
837
|
if (command.evidenceRefs.length === 0)
|
|
783
838
|
return reject('milestone achievement requires criteria evidence');
|
|
839
|
+
// The same gate as a step (spec 445 / REM-6): the first writer fixes the evidence, so it is not a stranger.
|
|
840
|
+
if (command.actor !== ctx.endeavor.managingPrincipal &&
|
|
841
|
+
participationStatus(state, command.actor) !== 'active') {
|
|
842
|
+
return reject('milestone achievement must be recorded by the managing principal or an active participant');
|
|
843
|
+
}
|
|
784
844
|
return {
|
|
785
845
|
ok: true,
|
|
786
846
|
events: [
|
|
@@ -789,6 +849,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
789
849
|
eventId: eventId(nextRevision, 'MilestoneAchieved'),
|
|
790
850
|
endeavorId: command.endeavorId,
|
|
791
851
|
milestoneId: command.milestoneId,
|
|
852
|
+
...(ctx.endeavor.adoptedPlanRef ? { planRef: ctx.endeavor.adoptedPlanRef } : {}),
|
|
792
853
|
evidenceRefs: command.evidenceRefs,
|
|
793
854
|
recordedBy: command.actor,
|
|
794
855
|
occurredAt: at,
|
|
@@ -842,7 +903,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
842
903
|
};
|
|
843
904
|
}
|
|
844
905
|
case 'ProposeRecommendation': {
|
|
845
|
-
if (state.recommendations
|
|
906
|
+
if (own(state.recommendations, command.recommendationId)) {
|
|
846
907
|
return reject(`recommendation ${command.recommendationId} already exists`);
|
|
847
908
|
}
|
|
848
909
|
if (command.recommendationKind.trim().length === 0)
|
|
@@ -916,7 +977,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
916
977
|
if (command.actor !== ctx.endeavor.managingPrincipal && !isSteward(state, command.actor) && participationStatus(state, command.actor) !== 'active') {
|
|
917
978
|
return reject('only the managing principal, a steward or an active participant may raise a decision');
|
|
918
979
|
}
|
|
919
|
-
if (state.decisionRequests
|
|
980
|
+
if (own(state.decisionRequests, command.decisionId))
|
|
920
981
|
return reject(`decision ${command.decisionId} already raised`);
|
|
921
982
|
const approvers = [...new Set(command.approvers.map((a) => a.toLowerCase()))];
|
|
922
983
|
if (approvers.length === 0)
|
|
@@ -927,12 +988,25 @@ export function validateCoordinationCommand(state, command) {
|
|
|
927
988
|
return reject('quorum must be between 1 and the number of approvers');
|
|
928
989
|
if (!command.title.trim())
|
|
929
990
|
return reject('a decision has a title');
|
|
991
|
+
// A window is an instant that is still ahead; one that does not parse would never expire (REM-5).
|
|
992
|
+
if (command.dueAt !== undefined && !(Date.parse(command.dueAt) > atMs))
|
|
993
|
+
return reject('dueAt must be an ISO-8601 instant after the request itself');
|
|
994
|
+
const scopedPlanRef = command.stepIds?.length ? ctx.endeavor.adoptedPlanRef : undefined;
|
|
930
995
|
if (command.stepIds?.length) {
|
|
931
996
|
const plan = adoptedPlan(state);
|
|
932
997
|
const known = new Set(plan?.steps.map((st) => st.stepId) ?? []);
|
|
933
998
|
for (const id of command.stepIds)
|
|
934
999
|
if (!known.has(id))
|
|
935
1000
|
return reject(`step ${id} is not in the adopted plan`);
|
|
1001
|
+
// A decision SCOPED TO PLAN STEPS has a consequence — its approval satisfies the plan's checkpoints —
|
|
1002
|
+
// so who may choose its approvers is narrower than who may ask a question (spec 445 / NAME-12): the
|
|
1003
|
+
// organization or a steward raises it, and whoever raises it is not among those who decide it.
|
|
1004
|
+
if (command.actor !== ctx.endeavor.managingPrincipal && !isSteward(state, command.actor)) {
|
|
1005
|
+
return reject('only the managing principal or a steward may raise a decision scoped to plan steps');
|
|
1006
|
+
}
|
|
1007
|
+
if (approvers.includes(command.actor.toLowerCase())) {
|
|
1008
|
+
return reject('the requester of a decision scoped to plan steps is not one of its approvers — a requester never approves their own request');
|
|
1009
|
+
}
|
|
936
1010
|
}
|
|
937
1011
|
const request = {
|
|
938
1012
|
schema: 'apcoord/DecisionRequestV1',
|
|
@@ -943,7 +1017,9 @@ export function validateCoordinationCommand(state, command) {
|
|
|
943
1017
|
...(command.summary?.trim() ? { summary: command.summary.trim() } : {}),
|
|
944
1018
|
requestedBy: command.actor,
|
|
945
1019
|
requirement: { schema: 'apcoord/ApprovalRequirementV1', approvers, ...(command.quorum !== undefined ? { quorum: command.quorum } : {}) },
|
|
946
|
-
scope
|
|
1020
|
+
// The scope binds the EXACT plan revision the steps were read from (spec 445 / REM-7): an approval given
|
|
1021
|
+
// for one plan is never read as given for its successor.
|
|
1022
|
+
scope: { ...(command.stepIds?.length ? { stepIds: [...command.stepIds] } : {}), ...(scopedPlanRef ? { planRef: scopedPlanRef } : {}) },
|
|
947
1023
|
requestedAt: at,
|
|
948
1024
|
...(command.dueAt ? { dueAt: command.dueAt } : {}),
|
|
949
1025
|
status: 'pending',
|
|
@@ -954,12 +1030,13 @@ export function validateCoordinationCommand(state, command) {
|
|
|
954
1030
|
const ctx = endeavorContext(state, command.endeavorId);
|
|
955
1031
|
if ('reason' in ctx)
|
|
956
1032
|
return reject(ctx.reason);
|
|
957
|
-
const request = state.decisionRequests
|
|
1033
|
+
const request = own(state.decisionRequests, command.decisionId);
|
|
958
1034
|
if (!request)
|
|
959
1035
|
return reject(`unknown decision ${command.decisionId}`);
|
|
960
1036
|
if (request.status !== 'pending')
|
|
961
1037
|
return reject(`decision ${command.decisionId} is already decided — a reversal is a new request`);
|
|
962
|
-
|
|
1038
|
+
// Fail closed: a window that cannot be read is a window that has closed (REM-5).
|
|
1039
|
+
if (request.dueAt !== undefined && !(atMs <= Date.parse(request.dueAt)))
|
|
963
1040
|
return reject(`decision ${command.decisionId} expired at ${request.dueAt}`);
|
|
964
1041
|
// THE DECLARED APPROVER, AND ONLY THEM. The managing principal counts when the organization was named
|
|
965
1042
|
// (a steward decides as it — the door set the actor); a steward who is not named, whose organization
|
|
@@ -972,7 +1049,14 @@ export function validateCoordinationCommand(state, command) {
|
|
|
972
1049
|
}
|
|
973
1050
|
if (!command.rationale.trim())
|
|
974
1051
|
return reject('a decision carries its rationale');
|
|
975
|
-
|
|
1052
|
+
// ON WHOSE BEHALF is not the caller's to choose (spec 445 / COV-1): a record is the actor's own, or the
|
|
1053
|
+
// organization's when a steward decided as it. Anything else is a claim about a third party.
|
|
1054
|
+
if (command.principal !== undefined && command.principal !== command.actor) {
|
|
1055
|
+
const asOrganization = command.principal === ctx.endeavor.managingPrincipal && isSteward(state, command.actor);
|
|
1056
|
+
if (!asOrganization)
|
|
1057
|
+
return reject(`${command.actor} may record a decision on its own behalf, or on the organization's when it is a steward — not on behalf of ${command.principal}`);
|
|
1058
|
+
}
|
|
1059
|
+
const prior = own(state.decisions, command.decisionId) ?? [];
|
|
976
1060
|
if (prior.some((r) => r.actor.toLowerCase() === actor && r.outcome !== 'deferred')) {
|
|
977
1061
|
return reject(`${command.actor} already recorded their decision on ${command.decisionId} — a reversal is a new request`);
|
|
978
1062
|
}
|
|
@@ -1003,7 +1087,12 @@ export function validateCoordinationCommand(state, command) {
|
|
|
1003
1087
|
const plan = adoptedPlan(state);
|
|
1004
1088
|
for (const stepId of request.scope.stepIds) {
|
|
1005
1089
|
const step = plan?.steps.find((st) => st.stepId === stepId);
|
|
1006
|
-
if (!step || step.kind !== 'decision' || state.satisfiedSteps
|
|
1090
|
+
if (!step || step.kind !== 'decision' || own(state.satisfiedSteps, stepId) || !ctx.endeavor.adoptedPlanRef)
|
|
1091
|
+
continue;
|
|
1092
|
+
// The approval was given for the step AS THE PLAN IT WAS RAISED AGAINST defined it (REM-7). A later
|
|
1093
|
+
// revision that redefined the step under the same id is not what was approved; a request with no
|
|
1094
|
+
// bound revision (a log that predates the binding) satisfies nothing — raise it again.
|
|
1095
|
+
if (!sameContent(stepIn(state, request.scope.planRef, stepId), step))
|
|
1007
1096
|
continue;
|
|
1008
1097
|
const rev = nextRevision + events.length;
|
|
1009
1098
|
events.push({ kind: 'PlanStepSatisfied', eventId: eventId(rev, 'PlanStepSatisfied'), endeavorId: command.endeavorId, stepId, planRef: ctx.endeavor.adoptedPlanRef, evidenceRefs: [{ kind: 'resource', iri: `urn:ap:decision:${command.decisionId}` }], recordedBy: command.actor, occurredAt: at, revision: rev });
|
|
@@ -1012,7 +1101,7 @@ export function validateCoordinationCommand(state, command) {
|
|
|
1012
1101
|
return { ok: true, events };
|
|
1013
1102
|
}
|
|
1014
1103
|
case 'ResolveRecommendation': {
|
|
1015
|
-
const rec = state.recommendations
|
|
1104
|
+
const rec = own(state.recommendations, command.recommendationId);
|
|
1016
1105
|
if (!rec)
|
|
1017
1106
|
return reject(`unknown recommendation ${command.recommendationId}`);
|
|
1018
1107
|
if (rec.status !== 'proposed')
|
|
@@ -1057,7 +1146,16 @@ export function validateCoordinationCommand(state, command) {
|
|
|
1057
1146
|
return reject(`unknown command kind ${command.kind ?? '<none>'}`);
|
|
1058
1147
|
}
|
|
1059
1148
|
}
|
|
1149
|
+
/** Apply one event. The ONLY state-transition path; also advances the log's clock (`lastEventAt`, REM-5). */
|
|
1060
1150
|
export function applyCoordinationEvent(state, event) {
|
|
1151
|
+
const next = applyEvent(state, event);
|
|
1152
|
+
const occurred = Date.parse(event.occurredAt);
|
|
1153
|
+
if (Number.isNaN(occurred))
|
|
1154
|
+
return next;
|
|
1155
|
+
const prior = state.lastEventAt !== undefined ? Date.parse(state.lastEventAt) : Number.NaN;
|
|
1156
|
+
return { ...next, lastEventAt: Number.isNaN(prior) || occurred >= prior ? event.occurredAt : state.lastEventAt };
|
|
1157
|
+
}
|
|
1158
|
+
function applyEvent(state, event) {
|
|
1061
1159
|
if (event.revision !== state.revision + 1) {
|
|
1062
1160
|
throw new CoordinationEventError(`non-monotonic revision: state at ${state.revision}, event ${event.kind} carries ${event.revision}`);
|
|
1063
1161
|
}
|
|
@@ -1137,26 +1235,44 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1137
1235
|
}
|
|
1138
1236
|
case 'PlanAdopted': {
|
|
1139
1237
|
const key = planKey(event.planRef.planId, event.planRef.revision);
|
|
1140
|
-
const plan = state.plans
|
|
1238
|
+
const plan = own(state.plans, key);
|
|
1141
1239
|
if (!plan)
|
|
1142
1240
|
throw new CoordinationEventError(`PlanAdopted for unknown plan revision ${key}`);
|
|
1143
1241
|
const plans = { ...state.plans, [key]: { ...plan, status: 'adopted' } };
|
|
1144
1242
|
if (event.supersededPlanRef) {
|
|
1145
1243
|
const priorKey = planKey(event.supersededPlanRef.planId, event.supersededPlanRef.revision);
|
|
1146
|
-
const prior = plans
|
|
1244
|
+
const prior = own(plans, priorKey);
|
|
1147
1245
|
if (prior)
|
|
1148
1246
|
plans[priorKey] = { ...prior, status: 'superseded' };
|
|
1149
1247
|
}
|
|
1248
|
+
// SATISFACTION DOES NOT SURVIVE A CHANGE OF MEANING (spec 445 / REM-7). A step or milestone recorded
|
|
1249
|
+
// under the prior revision stays recorded only when the NEW revision defines it byte-for-byte the same
|
|
1250
|
+
// (RFC 8785); one the new plan redefined — or dropped — is cleared, so an approval or a completion given
|
|
1251
|
+
// for one plan never reads as given for its successor. The events stay in the log.
|
|
1252
|
+
const satisfiedSteps = {};
|
|
1253
|
+
for (const [stepId, rec] of Object.entries(state.satisfiedSteps)) {
|
|
1254
|
+
if (sameContent(stepIn(state, rec.planRef, stepId), plan.steps.find((s) => s.stepId === stepId)))
|
|
1255
|
+
satisfiedSteps[stepId] = rec;
|
|
1256
|
+
}
|
|
1257
|
+
const milestones = {};
|
|
1258
|
+
for (const [milestoneId, rec] of Object.entries(state.milestones)) {
|
|
1259
|
+
const ref = rec.planRef ?? event.supersededPlanRef;
|
|
1260
|
+
const priorPlan = ref ? own(state.plans, planKey(ref.planId, ref.revision)) : undefined;
|
|
1261
|
+
if (sameContent(priorPlan?.milestones.find((m) => m.milestoneId === milestoneId), plan.milestones.find((m) => m.milestoneId === milestoneId)))
|
|
1262
|
+
milestones[milestoneId] = rec;
|
|
1263
|
+
}
|
|
1150
1264
|
return {
|
|
1151
1265
|
...state,
|
|
1152
1266
|
revision: event.revision,
|
|
1153
1267
|
endeavor: { ...endeavorAt(event.revision), adoptedPlanRef: event.planRef, lifecycle: 'active' },
|
|
1154
1268
|
plans,
|
|
1269
|
+
satisfiedSteps,
|
|
1270
|
+
milestones,
|
|
1155
1271
|
};
|
|
1156
1272
|
}
|
|
1157
1273
|
case 'PlanRejected': {
|
|
1158
1274
|
const key = planKey(event.planId, event.planRevision);
|
|
1159
|
-
const plan = state.plans
|
|
1275
|
+
const plan = own(state.plans, key);
|
|
1160
1276
|
if (!plan)
|
|
1161
1277
|
throw new CoordinationEventError(`PlanRejected for unknown plan revision ${key}`);
|
|
1162
1278
|
return {
|
|
@@ -1205,7 +1321,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1205
1321
|
};
|
|
1206
1322
|
}
|
|
1207
1323
|
case 'ParticipationWithdrawn': {
|
|
1208
|
-
const participation = state.participations
|
|
1324
|
+
const participation = own(state.participations, event.participationId);
|
|
1209
1325
|
if (!participation) {
|
|
1210
1326
|
throw new CoordinationEventError(`ParticipationWithdrawn for unknown participation ${event.participationId}`);
|
|
1211
1327
|
}
|
|
@@ -1243,7 +1359,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1243
1359
|
};
|
|
1244
1360
|
}
|
|
1245
1361
|
case 'ContributionAllocated': {
|
|
1246
|
-
const proposal = state.proposals
|
|
1362
|
+
const proposal = own(state.proposals, event.proposalRef);
|
|
1247
1363
|
if (!proposal)
|
|
1248
1364
|
throw new CoordinationEventError(`ContributionAllocated for unknown proposal ${event.proposalRef}`);
|
|
1249
1365
|
const allocation = {
|
|
@@ -1278,7 +1394,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1278
1394
|
};
|
|
1279
1395
|
}
|
|
1280
1396
|
case 'CommitmentDischarged': {
|
|
1281
|
-
const c = state.commitments
|
|
1397
|
+
const c = own(state.commitments, event.commitmentId);
|
|
1282
1398
|
if (!c)
|
|
1283
1399
|
throw new CoordinationEventError(`CommitmentDischarged for unknown commitment ${event.commitmentId}`);
|
|
1284
1400
|
// `fulfilled` is the state the reducer has always persisted; discharge is the act that reaches it.
|
|
@@ -1290,7 +1406,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1290
1406
|
};
|
|
1291
1407
|
}
|
|
1292
1408
|
case 'CommitmentBreached': {
|
|
1293
|
-
const c = state.commitments
|
|
1409
|
+
const c = own(state.commitments, event.commitmentId);
|
|
1294
1410
|
if (!c)
|
|
1295
1411
|
throw new CoordinationEventError(`CommitmentBreached for unknown commitment ${event.commitmentId}`);
|
|
1296
1412
|
return {
|
|
@@ -1301,7 +1417,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1301
1417
|
};
|
|
1302
1418
|
}
|
|
1303
1419
|
case 'CommitmentLapsed': {
|
|
1304
|
-
const c = state.commitments
|
|
1420
|
+
const c = own(state.commitments, event.commitmentId);
|
|
1305
1421
|
if (!c)
|
|
1306
1422
|
throw new CoordinationEventError(`CommitmentLapsed for unknown commitment ${event.commitmentId}`);
|
|
1307
1423
|
return {
|
|
@@ -1312,7 +1428,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1312
1428
|
};
|
|
1313
1429
|
}
|
|
1314
1430
|
case 'ContributionCommitted': {
|
|
1315
|
-
const allocation = state.allocations
|
|
1431
|
+
const allocation = own(state.allocations, event.allocationRef);
|
|
1316
1432
|
if (!allocation)
|
|
1317
1433
|
throw new CoordinationEventError(`ContributionCommitted for unknown allocation ${event.allocationRef}`);
|
|
1318
1434
|
const commitment = {
|
|
@@ -1336,7 +1452,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1336
1452
|
};
|
|
1337
1453
|
}
|
|
1338
1454
|
case 'CommitmentWithdrawn': {
|
|
1339
|
-
const commitment = state.commitments
|
|
1455
|
+
const commitment = own(state.commitments, event.commitmentId);
|
|
1340
1456
|
if (!commitment)
|
|
1341
1457
|
throw new CoordinationEventError(`CommitmentWithdrawn for unknown commitment ${event.commitmentId}`);
|
|
1342
1458
|
return {
|
|
@@ -1347,10 +1463,10 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1347
1463
|
};
|
|
1348
1464
|
}
|
|
1349
1465
|
case 'ContributionReallocated': {
|
|
1350
|
-
const commitment = state.commitments
|
|
1466
|
+
const commitment = own(state.commitments, event.commitmentId);
|
|
1351
1467
|
if (!commitment)
|
|
1352
1468
|
throw new CoordinationEventError(`ContributionReallocated for unknown commitment ${event.commitmentId}`);
|
|
1353
|
-
const priorAllocation = state.allocations
|
|
1469
|
+
const priorAllocation = own(state.allocations, commitment.allocationRef);
|
|
1354
1470
|
if (!priorAllocation) {
|
|
1355
1471
|
throw new CoordinationEventError(`ContributionReallocated: commitment ${event.commitmentId} references unknown allocation ${commitment.allocationRef}`);
|
|
1356
1472
|
}
|
|
@@ -1394,6 +1510,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1394
1510
|
schema: 'apcoord/MilestoneAchievementV1',
|
|
1395
1511
|
milestoneId: event.milestoneId,
|
|
1396
1512
|
endeavorId: event.endeavorId,
|
|
1513
|
+
...(event.planRef ? { planRef: event.planRef } : {}),
|
|
1397
1514
|
evidenceRefs: event.evidenceRefs,
|
|
1398
1515
|
achievedAt: event.occurredAt,
|
|
1399
1516
|
};
|
|
@@ -1419,7 +1536,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1419
1536
|
case 'DecisionRequested':
|
|
1420
1537
|
return { ...state, revision: event.revision, endeavor: endeavorAt(event.revision), decisionRequests: { ...state.decisionRequests, [event.decisionId]: event.request } };
|
|
1421
1538
|
case 'DecisionRecorded': {
|
|
1422
|
-
const request = state.decisionRequests
|
|
1539
|
+
const request = own(state.decisionRequests, event.decisionId);
|
|
1423
1540
|
if (!request)
|
|
1424
1541
|
throw new CoordinationEventError(`DecisionRecorded for unknown decision ${event.decisionId}`);
|
|
1425
1542
|
return {
|
|
@@ -1427,7 +1544,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1427
1544
|
revision: event.revision,
|
|
1428
1545
|
endeavor: endeavorAt(event.revision),
|
|
1429
1546
|
decisionRequests: { ...state.decisionRequests, [event.decisionId]: event.resolution ? { ...request, status: 'decided', outcome: event.resolution } : request },
|
|
1430
|
-
decisions: { ...state.decisions, [event.decisionId]: [...(state.decisions
|
|
1547
|
+
decisions: { ...state.decisions, [event.decisionId]: [...(own(state.decisions, event.decisionId) ?? []), event.record] },
|
|
1431
1548
|
};
|
|
1432
1549
|
}
|
|
1433
1550
|
case 'RecommendationProposed':
|
|
@@ -1438,7 +1555,7 @@ export function applyCoordinationEvent(state, event) {
|
|
|
1438
1555
|
};
|
|
1439
1556
|
case 'RecommendationAccepted':
|
|
1440
1557
|
case 'RecommendationRejected': {
|
|
1441
|
-
const rec = state.recommendations
|
|
1558
|
+
const rec = own(state.recommendations, event.recommendationId);
|
|
1442
1559
|
if (!rec)
|
|
1443
1560
|
throw new CoordinationEventError(`${event.kind} for unknown recommendation ${event.recommendationId}`);
|
|
1444
1561
|
return {
|