@ngockhoale/ckit 0.0.0-stage → 0.1.1
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/.env.example +21 -0
- package/README.md +70 -2
- package/dist/src/adapters/agent/claude/errors.d.ts +13 -0
- package/dist/src/adapters/agent/claude/errors.js +16 -0
- package/dist/src/adapters/agent/claude/normalize.d.ts +11 -0
- package/dist/src/adapters/agent/claude/normalize.js +83 -0
- package/dist/src/adapters/agent/errors.d.ts +13 -0
- package/dist/src/adapters/agent/errors.js +19 -0
- package/dist/src/adapters/agent/fixtures/claude-transcript.d.ts +70 -0
- package/dist/src/adapters/agent/fixtures/claude-transcript.js +15 -0
- package/dist/src/adapters/agent/fixtures/omp-transcript.d.ts +60 -0
- package/dist/src/adapters/agent/fixtures/omp-transcript.js +14 -0
- package/dist/src/adapters/agent/normalize.d.ts +18 -0
- package/dist/src/adapters/agent/normalize.js +77 -0
- package/dist/src/adapters/agent/transport.d.ts +35 -0
- package/dist/src/adapters/agent/transport.js +39 -0
- package/dist/src/adapters/agent/worker-service.d.ts +49 -0
- package/dist/src/adapters/agent/worker-service.js +77 -0
- package/dist/src/adapters/fake/fake-advisor.d.ts +15 -0
- package/dist/src/adapters/fake/fake-advisor.js +29 -0
- package/dist/src/adapters/fake/fake-agent.d.ts +12 -0
- package/dist/src/adapters/fake/fake-agent.js +21 -0
- package/dist/src/adapters/fake/fake-store.d.ts +13 -0
- package/dist/src/adapters/fake/fake-store.js +23 -0
- package/dist/src/adapters/fake/fake-web.d.ts +12 -0
- package/dist/src/adapters/fake/fake-web.js +21 -0
- package/dist/src/adapters/fake/fault-injector.d.ts +18 -0
- package/dist/src/adapters/fake/fault-injector.js +64 -0
- package/dist/src/adapters/fake/virtual-clock.d.ts +12 -0
- package/dist/src/adapters/fake/virtual-clock.js +35 -0
- package/dist/src/adapters/launch/discover.d.ts +18 -0
- package/dist/src/adapters/launch/discover.js +55 -0
- package/dist/src/adapters/launch/executor.d.ts +117 -0
- package/dist/src/adapters/launch/executor.js +383 -0
- package/dist/src/adapters/launch/profile.d.ts +35 -0
- package/dist/src/adapters/launch/profile.js +73 -0
- package/dist/src/adapters/launch/spawn.d.ts +29 -0
- package/dist/src/adapters/launch/spawn.js +18 -0
- package/dist/src/adapters/openai/openai-advisor.d.ts +56 -0
- package/dist/src/adapters/openai/openai-advisor.js +146 -0
- package/dist/src/cli/ckit.d.ts +5 -0
- package/dist/src/cli/ckit.js +384 -0
- package/dist/src/cli/demo.d.ts +1 -0
- package/dist/src/cli/demo.js +30 -0
- package/dist/src/cli/lifecycle.d.ts +19 -0
- package/dist/src/cli/lifecycle.js +90 -0
- package/dist/src/cli/sessions.d.ts +10 -0
- package/dist/src/cli/sessions.js +381 -0
- package/dist/src/cli/setup.d.ts +11 -0
- package/dist/src/cli/setup.js +115 -0
- package/dist/src/committee/decision.d.ts +84 -0
- package/dist/src/committee/decision.js +167 -0
- package/dist/src/committee/handover.d.ts +115 -0
- package/dist/src/committee/handover.js +191 -0
- package/dist/src/committee/no-progress.d.ts +44 -0
- package/dist/src/committee/no-progress.js +64 -0
- package/dist/src/committee/primary-registry.d.ts +15 -0
- package/dist/src/committee/primary-registry.js +60 -0
- package/dist/src/committee/reply-parser.d.ts +28 -0
- package/dist/src/committee/reply-parser.js +53 -0
- package/dist/src/committee/reviewer-scheduler.d.ts +101 -0
- package/dist/src/committee/reviewer-scheduler.js +163 -0
- package/dist/src/committee/role-epoch.d.ts +91 -0
- package/dist/src/committee/role-epoch.js +154 -0
- package/dist/src/core/control.d.ts +18 -0
- package/dist/src/core/control.js +29 -0
- package/dist/src/core/entities.d.ts +166 -0
- package/dist/src/core/entities.js +119 -0
- package/dist/src/core/errors.d.ts +9 -0
- package/dist/src/core/errors.js +17 -0
- package/dist/src/core/events.d.ts +72 -0
- package/dist/src/core/events.js +125 -0
- package/dist/src/core/ids.d.ts +47 -0
- package/dist/src/core/ids.js +32 -0
- package/dist/src/core/machine.d.ts +55 -0
- package/dist/src/core/machine.js +110 -0
- package/dist/src/core/policy.d.ts +34 -0
- package/dist/src/core/policy.js +59 -0
- package/dist/src/core/provenance.d.ts +20 -0
- package/dist/src/core/provenance.js +21 -0
- package/dist/src/core/record.d.ts +35 -0
- package/dist/src/core/record.js +67 -0
- package/dist/src/core/replay.d.ts +10 -0
- package/dist/src/core/replay.js +36 -0
- package/dist/src/core/run-task.d.ts +20 -0
- package/dist/src/core/run-task.js +48 -0
- package/dist/src/diagnostics/doctor.d.ts +78 -0
- package/dist/src/diagnostics/doctor.js +365 -0
- package/dist/src/diagnostics/journal-lock.d.ts +38 -0
- package/dist/src/diagnostics/journal-lock.js +181 -0
- package/dist/src/evidence/linker.d.ts +44 -0
- package/dist/src/evidence/linker.js +75 -0
- package/dist/src/evidence/report-parser.d.ts +27 -0
- package/dist/src/evidence/report-parser.js +17 -0
- package/dist/src/evidence/store.d.ts +36 -0
- package/dist/src/evidence/store.js +57 -0
- package/dist/src/gui/server.d.ts +27 -0
- package/dist/src/gui/server.js +497 -0
- package/dist/src/host/checkpoint.d.ts +22 -0
- package/dist/src/host/checkpoint.js +74 -0
- package/dist/src/host/export.d.ts +52 -0
- package/dist/src/host/export.js +45 -0
- package/dist/src/host/ipc.d.ts +57 -0
- package/dist/src/host/ipc.js +124 -0
- package/dist/src/host/leases.d.ts +27 -0
- package/dist/src/host/leases.js +40 -0
- package/dist/src/host/retention.d.ts +47 -0
- package/dist/src/host/retention.js +130 -0
- package/dist/src/host/supervisor.d.ts +59 -0
- package/dist/src/host/supervisor.js +64 -0
- package/dist/src/intake/owner-intake.d.ts +31 -0
- package/dist/src/intake/owner-intake.js +97 -0
- package/dist/src/intake/types.d.ts +46 -0
- package/dist/src/intake/types.js +2 -0
- package/dist/src/intent/context-packet.d.ts +68 -0
- package/dist/src/intent/context-packet.js +76 -0
- package/dist/src/intent/idea-ledger.d.ts +50 -0
- package/dist/src/intent/idea-ledger.js +136 -0
- package/dist/src/intent/ledger.d.ts +35 -0
- package/dist/src/intent/ledger.js +40 -0
- package/dist/src/intent/registry.d.ts +51 -0
- package/dist/src/intent/registry.js +44 -0
- package/dist/src/intent/revision.d.ts +37 -0
- package/dist/src/intent/revision.js +48 -0
- package/dist/src/orchestration/dispatcher.d.ts +32 -0
- package/dist/src/orchestration/dispatcher.js +136 -0
- package/dist/src/orchestration/milestone-loop.d.ts +149 -0
- package/dist/src/orchestration/milestone-loop.js +132 -0
- package/dist/src/orchestration/recovery-coordinator.d.ts +74 -0
- package/dist/src/orchestration/recovery-coordinator.js +180 -0
- package/dist/src/orchestration/work-order-contract.d.ts +44 -0
- package/dist/src/orchestration/work-order-contract.js +85 -0
- package/dist/src/orchestration/work-queue.d.ts +56 -0
- package/dist/src/orchestration/work-queue.js +95 -0
- package/dist/src/ports/advisor.d.ts +33 -0
- package/dist/src/ports/advisor.js +2 -0
- package/dist/src/ports/agent.d.ts +31 -0
- package/dist/src/ports/agent.js +2 -0
- package/dist/src/ports/capabilities.d.ts +3 -0
- package/dist/src/ports/capabilities.js +5 -0
- package/dist/src/ports/clock.d.ts +5 -0
- package/dist/src/ports/clock.js +5 -0
- package/dist/src/ports/store.d.ts +13 -0
- package/dist/src/ports/store.js +2 -0
- package/dist/src/ports/web.d.ts +29 -0
- package/dist/src/ports/web.js +2 -0
- package/dist/src/run/api-run.d.ts +49 -0
- package/dist/src/run/api-run.js +173 -0
- package/dist/src/run/plan-run.d.ts +22 -0
- package/dist/src/run/plan-run.js +233 -0
- package/dist/src/run/plan.d.ts +36 -0
- package/dist/src/run/plan.js +119 -0
- package/dist/src/run/verify.d.ts +25 -0
- package/dist/src/run/verify.js +81 -0
- package/dist/src/security/redact.d.ts +7 -0
- package/dist/src/security/redact.js +21 -0
- package/dist/src/settings/env-config.d.ts +64 -0
- package/dist/src/settings/env-config.js +232 -0
- package/dist/src/storage/artifacts.d.ts +30 -0
- package/dist/src/storage/artifacts.js +105 -0
- package/dist/src/storage/audit.d.ts +6 -0
- package/dist/src/storage/audit.js +6 -0
- package/dist/src/storage/db.d.ts +9 -0
- package/dist/src/storage/db.js +43 -0
- package/dist/src/storage/events.d.ts +20 -0
- package/dist/src/storage/events.js +69 -0
- package/dist/src/storage/migrations.d.ts +8 -0
- package/dist/src/storage/migrations.js +48 -0
- package/dist/src/storage/outbox.d.ts +25 -0
- package/dist/src/storage/outbox.js +40 -0
- package/dist/src/storage/projections.d.ts +18 -0
- package/dist/src/storage/projections.js +40 -0
- package/dist/src/storage/reconcile.d.ts +14 -0
- package/dist/src/storage/reconcile.js +93 -0
- package/dist/src/storage/reservations.d.ts +12 -0
- package/dist/src/storage/reservations.js +80 -0
- package/dist/src/storage/store.d.ts +34 -0
- package/dist/src/storage/store.js +105 -0
- package/dist/src/team/attachments.d.ts +22 -0
- package/dist/src/team/attachments.js +211 -0
- package/dist/src/team/cli-models.d.ts +24 -0
- package/dist/src/team/cli-models.js +72 -0
- package/dist/src/team/config.d.ts +20 -0
- package/dist/src/team/config.js +113 -0
- package/dist/src/team/engine.d.ts +28 -0
- package/dist/src/team/engine.js +443 -0
- package/dist/src/team/journal.d.ts +30 -0
- package/dist/src/team/journal.js +70 -0
- package/dist/src/team/runners.d.ts +79 -0
- package/dist/src/team/runners.js +436 -0
- package/dist/src/team/secrets.d.ts +10 -0
- package/dist/src/team/secrets.js +68 -0
- package/dist/src/team/types.d.ts +157 -0
- package/dist/src/team/types.js +5 -0
- package/dist/src/tui/app.d.ts +29 -0
- package/dist/src/tui/app.js +96 -0
- package/dist/src/tui/control-model.d.ts +101 -0
- package/dist/src/tui/control-model.js +109 -0
- package/dist/src/tui/control-screens.d.ts +27 -0
- package/dist/src/tui/control-screens.js +85 -0
- package/dist/src/tui/graph-model.d.ts +34 -0
- package/dist/src/tui/graph-model.js +29 -0
- package/dist/src/tui/graph-screens.d.ts +13 -0
- package/dist/src/tui/graph-screens.js +4 -0
- package/dist/src/tui/inspector-model.d.ts +150 -0
- package/dist/src/tui/inspector-model.js +15 -0
- package/dist/src/tui/inspector-screens.d.ts +14 -0
- package/dist/src/tui/inspector-screens.js +61 -0
- package/dist/src/tui/live.d.ts +16 -0
- package/dist/src/tui/live.js +39 -0
- package/dist/src/tui/mission-action-screens.d.ts +15 -0
- package/dist/src/tui/mission-action-screens.js +105 -0
- package/dist/src/tui/mission-actions.d.ts +46 -0
- package/dist/src/tui/mission-actions.js +53 -0
- package/dist/src/tui/mission-model.d.ts +120 -0
- package/dist/src/tui/mission-model.js +164 -0
- package/dist/src/tui/mission-nav.d.ts +82 -0
- package/dist/src/tui/mission-nav.js +131 -0
- package/dist/src/tui/mission-plan-screens.d.ts +11 -0
- package/dist/src/tui/mission-plan-screens.js +40 -0
- package/dist/src/tui/mission-plan.d.ts +45 -0
- package/dist/src/tui/mission-plan.js +166 -0
- package/dist/src/tui/mission-screens.d.ts +7 -0
- package/dist/src/tui/mission-screens.js +263 -0
- package/dist/src/tui/provenance-label.d.ts +11 -0
- package/dist/src/tui/provenance-label.js +16 -0
- package/dist/src/tui/screens.d.ts +10 -0
- package/dist/src/tui/screens.js +34 -0
- package/dist/src/tui/session-outcome.d.ts +14 -0
- package/dist/src/tui/session-outcome.js +57 -0
- package/dist/src/tui/setup-model.d.ts +159 -0
- package/dist/src/tui/setup-model.js +234 -0
- package/dist/src/tui/setup-screens.d.ts +66 -0
- package/dist/src/tui/setup-screens.js +72 -0
- package/dist/src/tui/team/app.d.ts +24 -0
- package/dist/src/tui/team/app.js +228 -0
- package/dist/src/tui/team/demo-handle.d.ts +22 -0
- package/dist/src/tui/team/demo-handle.js +42 -0
- package/dist/src/tui/team/model.d.ts +81 -0
- package/dist/src/tui/team/model.js +133 -0
- package/gui/app.css +177 -0
- package/gui/app.js +677 -0
- package/gui/index.html +51 -0
- package/gui/network.js +413 -0
- package/package.json +44 -4
- package/src/storage/schema.sql +42 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type IntentRevisionId, type RequirementId } from '../core/ids.js';
|
|
2
|
+
import type { AuthorizationScope } from './registry.js';
|
|
3
|
+
export interface RevisionDiff {
|
|
4
|
+
readonly text: {
|
|
5
|
+
readonly before: string;
|
|
6
|
+
readonly after: string;
|
|
7
|
+
};
|
|
8
|
+
readonly scope?: {
|
|
9
|
+
readonly before: AuthorizationScope;
|
|
10
|
+
readonly after: AuthorizationScope;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface RequirementRevision {
|
|
14
|
+
readonly id: IntentRevisionId;
|
|
15
|
+
readonly requirementId: RequirementId;
|
|
16
|
+
readonly number: number;
|
|
17
|
+
readonly text: string;
|
|
18
|
+
readonly scope: AuthorizationScope;
|
|
19
|
+
readonly supersedesRevisionId: IntentRevisionId | null;
|
|
20
|
+
readonly diff: RevisionDiff | null;
|
|
21
|
+
}
|
|
22
|
+
export declare class IntentRevisionStore {
|
|
23
|
+
private readonly revisions;
|
|
24
|
+
private readonly tips;
|
|
25
|
+
create(input: {
|
|
26
|
+
readonly requirementId: string;
|
|
27
|
+
readonly text: string;
|
|
28
|
+
readonly scope: AuthorizationScope;
|
|
29
|
+
}): RequirementRevision;
|
|
30
|
+
supersede(priorId: string, change: {
|
|
31
|
+
readonly text: string;
|
|
32
|
+
readonly scope: AuthorizationScope;
|
|
33
|
+
}): RequirementRevision;
|
|
34
|
+
get(id: string): RequirementRevision | undefined;
|
|
35
|
+
list(requirement: string): readonly RequirementRevision[];
|
|
36
|
+
private add;
|
|
37
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { intentRevisionId, requirementId } from '../core/ids.js';
|
|
3
|
+
export class IntentRevisionStore {
|
|
4
|
+
revisions = new Map();
|
|
5
|
+
tips = new Map();
|
|
6
|
+
create(input) {
|
|
7
|
+
const req = requirementId(input.requirementId);
|
|
8
|
+
if (this.tips.has(req))
|
|
9
|
+
throw new TypeError('requirement already has a revision');
|
|
10
|
+
return this.add(req, input.text, input.scope, null, null);
|
|
11
|
+
}
|
|
12
|
+
supersede(priorId, change) {
|
|
13
|
+
const prior = this.get(priorId);
|
|
14
|
+
if (!prior || this.tips.get(prior.requirementId) !== prior.id)
|
|
15
|
+
throw new TypeError('only current revision can be superseded');
|
|
16
|
+
const diff = Object.freeze({
|
|
17
|
+
text: Object.freeze({ before: prior.text, after: change.text }),
|
|
18
|
+
...(JSON.stringify(prior.scope) !== JSON.stringify(change.scope)
|
|
19
|
+
? { scope: Object.freeze({ before: prior.scope, after: change.scope }) } : {}),
|
|
20
|
+
});
|
|
21
|
+
return this.add(prior.requirementId, change.text, change.scope, prior.id, diff);
|
|
22
|
+
}
|
|
23
|
+
get(id) { return this.revisions.get(intentRevisionId(id)); }
|
|
24
|
+
list(requirement) {
|
|
25
|
+
const req = requirementId(requirement);
|
|
26
|
+
return Object.freeze([...this.revisions.values()].filter(r => r.requirementId === req));
|
|
27
|
+
}
|
|
28
|
+
add(req, text, scope, supersedesRevisionId, diff) {
|
|
29
|
+
if (typeof text !== 'string' || !text.trim() || !scope || typeof scope !== 'object' || !['read', 'write', 'session'].includes(scope.kind))
|
|
30
|
+
throw new TypeError('revision text and explicit scope required');
|
|
31
|
+
if (scope.kind === 'session' && (!scope.sessionId || typeof scope.sessionId !== 'string'))
|
|
32
|
+
throw new TypeError('session scope requires sessionId');
|
|
33
|
+
const number = this.list(req).length + 1;
|
|
34
|
+
const id = intentRevisionId(`intent-rev-${createHash('sha256').update(req).digest('hex').slice(0, 48)}-${number.toString().padStart(4, '0')}`);
|
|
35
|
+
if (this.revisions.has(id))
|
|
36
|
+
throw new TypeError('revision id collision');
|
|
37
|
+
const frozenScope = Object.freeze({ ...scope });
|
|
38
|
+
const frozenDiff = diff === null ? null : Object.freeze({
|
|
39
|
+
text: Object.freeze({ ...diff.text }),
|
|
40
|
+
...(diff.scope ? { scope: Object.freeze({ before: Object.freeze({ ...diff.scope.before }), after: Object.freeze({ ...diff.scope.after }) }) } : {}),
|
|
41
|
+
});
|
|
42
|
+
const revision = Object.freeze({ id, requirementId: req, number, text, scope: frozenScope, supersedesRevisionId, diff: frozenDiff });
|
|
43
|
+
this.revisions.set(id, revision);
|
|
44
|
+
this.tips.set(req, id);
|
|
45
|
+
return revision;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=revision.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AgentPort } from '../ports/agent.js';
|
|
2
|
+
import type { TransportLifecycle } from '../adapters/agent/transport.js';
|
|
3
|
+
import { WorkQueue, type QueueEvent, type QueuedWorkOrder, type WorkOrderInput } from './work-queue.js';
|
|
4
|
+
export interface DispatchResult {
|
|
5
|
+
readonly status: 'accepted' | 'running' | 'rejected' | 'unknown_delivery' | 'failed' | 'cancelled';
|
|
6
|
+
readonly requestId?: string;
|
|
7
|
+
readonly workOrderId?: string;
|
|
8
|
+
readonly runId?: string;
|
|
9
|
+
readonly reasonId?: string;
|
|
10
|
+
readonly reconcile?: 'confirmed' | 'not-applied' | 'unknown';
|
|
11
|
+
}
|
|
12
|
+
export interface DispatcherOptions {
|
|
13
|
+
readonly currentRevision: (order: QueuedWorkOrder) => number;
|
|
14
|
+
readonly currentEpoch: (order: QueuedWorkOrder) => number;
|
|
15
|
+
readonly authorize: (authorizationRef: string, scope: WorkOrderInput['requestedScope'], order: QueuedWorkOrder) => boolean;
|
|
16
|
+
readonly permissionGranted: (order: QueuedWorkOrder) => boolean;
|
|
17
|
+
readonly audit?: (event: QueueEvent) => void;
|
|
18
|
+
readonly beforeCommit?: () => void | Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/** Serial dispatch boundary: every order is re-authorized and fenced at the commit immediately before send. */
|
|
21
|
+
export declare class Dispatcher {
|
|
22
|
+
private readonly queue;
|
|
23
|
+
private readonly options;
|
|
24
|
+
private agent;
|
|
25
|
+
beforeCommit: (() => void | Promise<void>) | undefined;
|
|
26
|
+
constructor(queue: WorkQueue, agent: AgentPort, options: DispatcherOptions);
|
|
27
|
+
get events(): readonly QueueEvent[];
|
|
28
|
+
setAgent(agent: AgentPort): void;
|
|
29
|
+
dispatchNext(lifecycle: TransportLifecycle): Promise<DispatchResult | null>;
|
|
30
|
+
reconcile(runId: string): Promise<'confirmed' | 'not-applied' | 'unknown'>;
|
|
31
|
+
cancel(workOrderId: string): boolean;
|
|
32
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { WorkQueue } from './work-queue.js';
|
|
2
|
+
const payloadFields = (payload) => {
|
|
3
|
+
if (!payload || typeof payload !== 'object')
|
|
4
|
+
throw new TypeError('work-order payload must include cwd and instruction');
|
|
5
|
+
const value = payload;
|
|
6
|
+
if (typeof value['cwd'] !== 'string' || !value['cwd'].trim() || typeof value['instruction'] !== 'string' || !value['instruction'].trim())
|
|
7
|
+
throw new TypeError('work-order payload must include cwd and instruction');
|
|
8
|
+
return { cwd: value['cwd'], instruction: value['instruction'] };
|
|
9
|
+
};
|
|
10
|
+
/** Serial dispatch boundary: every order is re-authorized and fenced at the commit immediately before send. */
|
|
11
|
+
export class Dispatcher {
|
|
12
|
+
queue;
|
|
13
|
+
options;
|
|
14
|
+
agent;
|
|
15
|
+
beforeCommit;
|
|
16
|
+
constructor(queue, agent, options) {
|
|
17
|
+
this.queue = queue;
|
|
18
|
+
this.options = options;
|
|
19
|
+
if (!agent.isSimulated)
|
|
20
|
+
throw new TypeError('live agent transport is not enabled in this slice');
|
|
21
|
+
this.agent = agent;
|
|
22
|
+
this.beforeCommit = options.beforeCommit;
|
|
23
|
+
}
|
|
24
|
+
get events() { return this.queue.events(); }
|
|
25
|
+
setAgent(agent) { if (!agent.isSimulated)
|
|
26
|
+
throw new TypeError('live agent transport is not enabled in this slice'); this.agent = agent; }
|
|
27
|
+
async dispatchNext(lifecycle) {
|
|
28
|
+
const order = this.queue.next();
|
|
29
|
+
if (!order)
|
|
30
|
+
return null;
|
|
31
|
+
const fail = (reasonId) => { this.queue.mark(order.workOrderId, 'rejected', reasonId); const event = Object.freeze({ type: 'rejected', requestId: order.requestId, workOrderId: order.workOrderId, reasonId }); this.queue.record(event); this.options.audit?.(event); return Object.freeze({ status: 'rejected', requestId: order.requestId, workOrderId: order.workOrderId, reasonId }); };
|
|
32
|
+
// Final authorization/revision/epoch/permission checks precede the dispatch commit.
|
|
33
|
+
if (!order.authorizationRef.trim() || !this.options.authorize(order.authorizationRef, order.requestedScope, order))
|
|
34
|
+
return fail('WORK_ORDER_AUTHORIZATION_REQUIRED');
|
|
35
|
+
if (this.options.currentRevision(order) !== order.intentRevision)
|
|
36
|
+
return fail('WORK_ORDER_REVISION_MISMATCH');
|
|
37
|
+
if (this.options.currentEpoch(order) !== order.epoch)
|
|
38
|
+
return fail('WORK_ORDER_EPOCH_STALE');
|
|
39
|
+
if (!this.options.permissionGranted(order) || !order.permissionGranted)
|
|
40
|
+
return fail('WORK_ORDER_PERMISSION_DENIED');
|
|
41
|
+
try {
|
|
42
|
+
await this.beforeCommit?.();
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return fail('WORK_ORDER_COMMIT_GUARD_FAILED');
|
|
46
|
+
}
|
|
47
|
+
// Policy callbacks may observe a concurrent owner/primary change during the commit hook.
|
|
48
|
+
if (!order.authorizationRef.trim() || !this.options.authorize(order.authorizationRef, order.requestedScope, order))
|
|
49
|
+
return fail('WORK_ORDER_AUTHORIZATION_REQUIRED');
|
|
50
|
+
if (this.options.currentRevision(order) !== order.intentRevision)
|
|
51
|
+
return fail('WORK_ORDER_REVISION_MISMATCH');
|
|
52
|
+
if (this.options.currentEpoch(order) !== order.epoch)
|
|
53
|
+
return fail('WORK_ORDER_EPOCH_STALE');
|
|
54
|
+
if (!this.options.permissionGranted(order) || !order.permissionGranted)
|
|
55
|
+
return fail('WORK_ORDER_PERMISSION_DENIED');
|
|
56
|
+
let request;
|
|
57
|
+
try {
|
|
58
|
+
request = payloadFields(order.payload);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return fail('WORK_ORDER_INVALID_PAYLOAD');
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
let accepted = false, runId, running = false, unknown = false;
|
|
65
|
+
for await (const observation of this.agent.dispatch({ workOrderId: order.workOrderId, ...request })) {
|
|
66
|
+
if (observation.kind === 'accepted') {
|
|
67
|
+
if (accepted)
|
|
68
|
+
continue;
|
|
69
|
+
accepted = true;
|
|
70
|
+
runId = observation.runId;
|
|
71
|
+
this.queue.mark(order.workOrderId, 'accepted');
|
|
72
|
+
lifecycle.transition('accepted', { runId });
|
|
73
|
+
const event = Object.freeze({ type: 'accepted', requestId: order.requestId, workOrderId: order.workOrderId });
|
|
74
|
+
this.options.audit?.(event);
|
|
75
|
+
}
|
|
76
|
+
else if (observation.kind === 'state' && observation.state === 'running') {
|
|
77
|
+
if (!accepted) {
|
|
78
|
+
unknown = true;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (!running) {
|
|
82
|
+
running = true;
|
|
83
|
+
this.queue.mark(order.workOrderId, 'running');
|
|
84
|
+
lifecycle.transition('running');
|
|
85
|
+
const event = Object.freeze({ type: 'running', requestId: order.requestId, workOrderId: order.workOrderId });
|
|
86
|
+
this.options.audit?.(event);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (observation.kind === 'unknown-outcome') {
|
|
90
|
+
unknown = true;
|
|
91
|
+
runId = observation.runId;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
else if (observation.kind === 'state' && observation.state === 'failed') {
|
|
95
|
+
const reasonId = 'AGENT_EXECUTION_FAILED';
|
|
96
|
+
this.queue.mark(order.workOrderId, 'failed', reasonId);
|
|
97
|
+
lifecycle.transition('failed', { cause: 'explicit-failure' });
|
|
98
|
+
const event = Object.freeze({ type: 'failed', requestId: order.requestId, workOrderId: order.workOrderId, reasonId });
|
|
99
|
+
this.options.audit?.(event);
|
|
100
|
+
return Object.freeze({ status: 'failed', requestId: order.requestId, workOrderId: order.workOrderId, ...(runId ? { runId } : {}), reasonId });
|
|
101
|
+
}
|
|
102
|
+
else if (observation.kind === 'state' && (observation.state === 'completed' || observation.state === 'waiting-input'))
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (unknown || !accepted) {
|
|
106
|
+
// If request iteration ended without a durable acknowledgement, delivery is ambiguous: make terminal and reconcile only.
|
|
107
|
+
if (order.status === 'enqueued')
|
|
108
|
+
this.queue.mark(order.workOrderId, 'unknown_delivery');
|
|
109
|
+
lifecycle.transition('unknown_delivery', { cause: 'ack-lost' });
|
|
110
|
+
const event = Object.freeze({ type: 'unknown_delivery', requestId: order.requestId, workOrderId: order.workOrderId });
|
|
111
|
+
this.queue.record(event);
|
|
112
|
+
this.options.audit?.(event);
|
|
113
|
+
const reconcile = runId ? await this.agent.reconcile(runId) : 'unknown';
|
|
114
|
+
return Object.freeze({ status: 'unknown_delivery', requestId: order.requestId, workOrderId: order.workOrderId, ...(runId ? { runId } : {}), reconcile });
|
|
115
|
+
}
|
|
116
|
+
return Object.freeze({ status: running ? 'running' : 'accepted', requestId: order.requestId, workOrderId: order.workOrderId, ...(runId ? { runId } : {}) });
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
if (order.status === 'enqueued')
|
|
120
|
+
this.queue.mark(order.workOrderId, 'unknown_delivery');
|
|
121
|
+
if (lifecycle.receipt().state !== 'unknown_delivery')
|
|
122
|
+
lifecycle.transition('unknown_delivery', { cause: 'ack-lost' });
|
|
123
|
+
const event = Object.freeze({ type: 'unknown_delivery', requestId: order.requestId, workOrderId: order.workOrderId });
|
|
124
|
+
this.queue.record(event);
|
|
125
|
+
this.options.audit?.(event);
|
|
126
|
+
return Object.freeze({ status: 'unknown_delivery', requestId: order.requestId, workOrderId: order.workOrderId, reconcile: 'unknown' });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async reconcile(runId) {
|
|
130
|
+
if (typeof runId !== 'string' || !runId.trim())
|
|
131
|
+
throw new TypeError('runId required');
|
|
132
|
+
return this.agent.reconcile(runId);
|
|
133
|
+
}
|
|
134
|
+
cancel(workOrderId) { return this.queue.cancel(workOrderId); }
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=dispatcher.js.map
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { type FinalDecision, type ReviewerRequest, type ScheduledReviewer } from '../committee/reviewer-scheduler.js';
|
|
2
|
+
import type { SourceMessage } from '../core/entities.js';
|
|
3
|
+
import { RoleEpoch } from '../committee/role-epoch.js';
|
|
4
|
+
import type { QueueEvent, WorkOrderInput } from './work-queue.js';
|
|
5
|
+
import { WorkQueue } from './work-queue.js';
|
|
6
|
+
import type { AuthorizationScope } from '../intent/registry.js';
|
|
7
|
+
import type { AgentPort } from '../ports/agent.js';
|
|
8
|
+
import type { TransportLifecycle } from '../adapters/agent/transport.js';
|
|
9
|
+
import { type DispatchResult } from './dispatcher.js';
|
|
10
|
+
import type { EvidenceLinker } from '../evidence/linker.js';
|
|
11
|
+
import type { EvidenceRecord, EvidenceStore } from '../evidence/store.js';
|
|
12
|
+
import type { IdeaLedger, IdeaRecord } from '../intent/idea-ledger.js';
|
|
13
|
+
import { type AcceptanceCriterionInput, type PrimaryContextPacket } from '../intent/context-packet.js';
|
|
14
|
+
export type MilestoneDisposition = 'execute' | 'investigate' | 'needs-owner' | 'done';
|
|
15
|
+
export interface MilestoneResult {
|
|
16
|
+
readonly status: string;
|
|
17
|
+
readonly summary?: string;
|
|
18
|
+
readonly changes?: readonly string[];
|
|
19
|
+
readonly blockers?: readonly string[];
|
|
20
|
+
readonly questions?: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
export interface MilestoneInput {
|
|
23
|
+
readonly milestoneId: string;
|
|
24
|
+
readonly result: MilestoneResult;
|
|
25
|
+
readonly evidenceIds: readonly string[];
|
|
26
|
+
readonly newIdeas?: readonly {
|
|
27
|
+
readonly text: string;
|
|
28
|
+
readonly source: SourceMessage;
|
|
29
|
+
}[];
|
|
30
|
+
readonly disposition?: MilestoneDisposition;
|
|
31
|
+
}
|
|
32
|
+
export interface MilestoneAcceptanceCriterion extends AcceptanceCriterionInput {
|
|
33
|
+
readonly requiredLevel?: 'reported' | 'observed' | 'verified';
|
|
34
|
+
}
|
|
35
|
+
export interface MilestoneDispatchOptions {
|
|
36
|
+
readonly agent: AgentPort;
|
|
37
|
+
readonly lifecycle: (order: WorkOrderInput) => TransportLifecycle;
|
|
38
|
+
readonly roleEpoch: RoleEpoch;
|
|
39
|
+
readonly authorizationRef: string;
|
|
40
|
+
readonly scope: AuthorizationScope;
|
|
41
|
+
readonly taskId: string;
|
|
42
|
+
readonly intentRevision: number;
|
|
43
|
+
readonly epoch: number;
|
|
44
|
+
readonly permissionGranted: boolean;
|
|
45
|
+
readonly currentRevision: () => number;
|
|
46
|
+
readonly currentEpoch: () => number;
|
|
47
|
+
readonly authorize: (authorizationRef: string, scope: AuthorizationScope) => boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface MilestoneLoopOptions {
|
|
50
|
+
readonly intent: string;
|
|
51
|
+
readonly constraints: readonly string[];
|
|
52
|
+
readonly acceptanceCriteria: readonly MilestoneAcceptanceCriterion[];
|
|
53
|
+
readonly revision: string;
|
|
54
|
+
readonly cwd: string;
|
|
55
|
+
readonly budget: number;
|
|
56
|
+
readonly queue: WorkQueue;
|
|
57
|
+
readonly evidence: EvidenceStore;
|
|
58
|
+
readonly linker: EvidenceLinker;
|
|
59
|
+
readonly ideas: IdeaLedger;
|
|
60
|
+
readonly reviewPolicy: (input: {
|
|
61
|
+
readonly packet: PrimaryContextPacket;
|
|
62
|
+
readonly aggregate: MilestoneAggregate;
|
|
63
|
+
readonly acceptance: AcceptanceEvaluation;
|
|
64
|
+
}) => FinalDecision;
|
|
65
|
+
readonly primaryId?: string;
|
|
66
|
+
readonly reviewerPolicy?: (input: {
|
|
67
|
+
readonly packet: PrimaryContextPacket;
|
|
68
|
+
readonly aggregate: MilestoneAggregate;
|
|
69
|
+
}) => ReviewerRequest | null;
|
|
70
|
+
readonly dispatch?: MilestoneDispatchOptions;
|
|
71
|
+
readonly roleEpoch?: RoleEpoch;
|
|
72
|
+
}
|
|
73
|
+
export interface MilestoneAggregate {
|
|
74
|
+
readonly milestoneId: string;
|
|
75
|
+
readonly resultStatus: string;
|
|
76
|
+
readonly evidence: readonly {
|
|
77
|
+
readonly id: string;
|
|
78
|
+
readonly level: EvidenceRecord['verification'];
|
|
79
|
+
readonly revision: string;
|
|
80
|
+
}[];
|
|
81
|
+
readonly ideas: readonly IdeaRecord[];
|
|
82
|
+
readonly summary: '';
|
|
83
|
+
}
|
|
84
|
+
export interface AcceptanceEvaluation {
|
|
85
|
+
readonly complete: boolean;
|
|
86
|
+
readonly openCriteria: readonly AcceptanceCriterionInput[];
|
|
87
|
+
readonly outcomes: readonly {
|
|
88
|
+
readonly id: string;
|
|
89
|
+
readonly satisfied: boolean;
|
|
90
|
+
}[];
|
|
91
|
+
}
|
|
92
|
+
export interface DispositionRecord {
|
|
93
|
+
readonly recordId: string;
|
|
94
|
+
readonly milestoneId: string;
|
|
95
|
+
readonly kind: MilestoneDisposition;
|
|
96
|
+
readonly path: MilestoneDisposition;
|
|
97
|
+
readonly reason: string;
|
|
98
|
+
}
|
|
99
|
+
export interface MilestoneCollection {
|
|
100
|
+
readonly aggregate: MilestoneAggregate;
|
|
101
|
+
readonly packet: PrimaryContextPacket;
|
|
102
|
+
readonly acceptance: AcceptanceEvaluation;
|
|
103
|
+
readonly decision: FinalDecision | null;
|
|
104
|
+
readonly accounting: {
|
|
105
|
+
readonly primaryCalls: number;
|
|
106
|
+
readonly acknowledgementCalls: number;
|
|
107
|
+
};
|
|
108
|
+
readonly reviewers: readonly ScheduledReviewer[];
|
|
109
|
+
readonly disposition: DispositionRecord;
|
|
110
|
+
readonly path: MilestoneDisposition;
|
|
111
|
+
readonly workOrder: WorkOrderInput | null;
|
|
112
|
+
readonly queueEvents: readonly QueueEvent[];
|
|
113
|
+
readonly persistence: 'in-memory';
|
|
114
|
+
}
|
|
115
|
+
export interface HandoverFixture {
|
|
116
|
+
readonly intent: string;
|
|
117
|
+
readonly currentWork: string | null;
|
|
118
|
+
readonly counters: Readonly<{
|
|
119
|
+
primaryCalls: number;
|
|
120
|
+
reviewerCalls: number;
|
|
121
|
+
}>;
|
|
122
|
+
readonly fixture: {
|
|
123
|
+
readonly kind: 'quota';
|
|
124
|
+
readonly label: 'SIMULATED';
|
|
125
|
+
readonly remaining: number;
|
|
126
|
+
};
|
|
127
|
+
readonly liveObservation: 'NOT_RUN';
|
|
128
|
+
}
|
|
129
|
+
export declare class MilestoneLoop {
|
|
130
|
+
private readonly options;
|
|
131
|
+
private readonly records;
|
|
132
|
+
private readonly primaryId;
|
|
133
|
+
constructor(options: MilestoneLoopOptions);
|
|
134
|
+
collect(input: MilestoneInput): MilestoneCollection;
|
|
135
|
+
dispatchNext(): Promise<DispatchResult | null>;
|
|
136
|
+
resolveDisposition(milestoneId: string, disposition: MilestoneDisposition): DispositionRecord;
|
|
137
|
+
handover(input: {
|
|
138
|
+
readonly intent: string;
|
|
139
|
+
readonly currentWork: string | null;
|
|
140
|
+
readonly counters: Readonly<{
|
|
141
|
+
primaryCalls: number;
|
|
142
|
+
reviewerCalls: number;
|
|
143
|
+
}>;
|
|
144
|
+
readonly quota: {
|
|
145
|
+
readonly kind: 'fixture';
|
|
146
|
+
readonly remaining: number;
|
|
147
|
+
};
|
|
148
|
+
}): HandoverFixture;
|
|
149
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { ReviewerScheduler } from '../committee/reviewer-scheduler.js';
|
|
2
|
+
import { RoleEpoch } from '../committee/role-epoch.js';
|
|
3
|
+
import { WorkQueue } from './work-queue.js';
|
|
4
|
+
import { Dispatcher } from './dispatcher.js';
|
|
5
|
+
import { buildPrimaryContextPacket, evaluateAcceptanceCompletion } from '../intent/context-packet.js';
|
|
6
|
+
export class MilestoneLoop {
|
|
7
|
+
options;
|
|
8
|
+
records = new Map();
|
|
9
|
+
primaryId;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
if (!options || !options.intent.trim() || !options.revision.trim() || !options.cwd.trim())
|
|
13
|
+
throw new TypeError('intent, revision and cwd required');
|
|
14
|
+
if (options.roleEpoch !== undefined && options.dispatch !== undefined && options.roleEpoch !== options.dispatch.roleEpoch)
|
|
15
|
+
throw new TypeError('milestone roleEpoch must be the dispatch roleEpoch');
|
|
16
|
+
this.primaryId = options.primaryId ?? 'primary-offline';
|
|
17
|
+
}
|
|
18
|
+
collect(input) {
|
|
19
|
+
if (!input || typeof input.milestoneId !== 'string' || !input.milestoneId.trim() || !input.result || typeof input.result.status !== 'string' || !Array.isArray(input.evidenceIds))
|
|
20
|
+
throw new TypeError('milestone ID, result and evidence references required');
|
|
21
|
+
if (this.records.has(input.milestoneId))
|
|
22
|
+
throw new TypeError('milestone already collected');
|
|
23
|
+
const evidenceRecords = input.evidenceIds.map((id) => { const record = this.options.evidence.get(id); if (!record)
|
|
24
|
+
throw new TypeError(`unknown evidence: ${id}`); const linked = this.options.acceptanceCriteria.some((criterion) => this.options.linker.evaluate(criterion.id, { revision: this.options.revision, cwd: this.options.cwd }).links.some((link) => link.evidenceId === id)); if (!linked)
|
|
25
|
+
throw new TypeError(`unlinked evidence is not accepted: ${id}`); if (record.revision !== this.options.revision || record.cwd !== this.options.cwd)
|
|
26
|
+
throw new TypeError(`evidence is bound to another revision or cwd: ${id}`); return record; });
|
|
27
|
+
const ideas = (input.newIdeas ?? []).map((idea) => this.options.ideas.ingest(idea));
|
|
28
|
+
const outcomes = this.options.acceptanceCriteria.map((criterion) => {
|
|
29
|
+
const evaluation = this.options.linker.evaluate(criterion.id, { revision: this.options.revision, cwd: this.options.cwd });
|
|
30
|
+
const bound = evaluation.links.some((link) => { const record = this.options.evidence.get(link.evidenceId); return !!record && record.revision === this.options.revision && record.cwd === this.options.cwd; });
|
|
31
|
+
return { id: criterion.id, satisfied: evaluation.satisfied && bound };
|
|
32
|
+
});
|
|
33
|
+
const frozenOutcomes = Object.freeze(outcomes.map((outcome) => Object.freeze({ ...outcome })));
|
|
34
|
+
const criteria = this.options.acceptanceCriteria.map(({ id, text, status }) => ({ id, text, status: outcomes.find((outcome) => outcome.id === id)?.satisfied ? 'satisfied' : status === 'satisfied' ? 'incomplete' : status }));
|
|
35
|
+
const acceptanceSource = evaluateAcceptanceCompletion({ ...(input.result.summary === undefined ? {} : { summary: input.result.summary }), acceptanceCriteria: criteria, sourceComplete: outcomes.every(({ satisfied }) => satisfied) });
|
|
36
|
+
const acceptance = Object.freeze({ complete: acceptanceSource.complete, openCriteria: acceptanceSource.openCriteria, outcomes: frozenOutcomes });
|
|
37
|
+
const aggregate = Object.freeze({ milestoneId: input.milestoneId, resultStatus: input.result.status, evidence: Object.freeze(evidenceRecords.map((record) => Object.freeze({ id: record.id, level: record.verification, revision: record.revision }))), ideas: Object.freeze(ideas), summary: '' });
|
|
38
|
+
const evidence = evidenceRecords.map((record) => `${record.id} level=${record.verification} revision=${record.revision}`);
|
|
39
|
+
const packet = buildPrimaryContextPacket({ intent: this.options.intent, constraints: this.options.constraints, acceptanceCriteria: criteria, delta: `milestone=${input.milestoneId}; result=${input.result.status}`, evidence, budget: this.options.budget });
|
|
40
|
+
// Evidence revisions use the explicit `rev-N` fixture format; other opaque labels map to baseline 1.
|
|
41
|
+
const evidenceRevision = Math.max(1, ...evidenceRecords.map((record) => {
|
|
42
|
+
const match = /^rev-(\d+)$/.exec(record.revision);
|
|
43
|
+
const parsed = match === null ? 1 : Number(match[1]);
|
|
44
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : 1;
|
|
45
|
+
}));
|
|
46
|
+
const scheduler = new ReviewerScheduler({ decisionId: `decision-${input.milestoneId}`, primaryId: this.primaryId, evidenceRevision, snapshot: packet });
|
|
47
|
+
const primary = scheduler.startPrimary();
|
|
48
|
+
const selectedReviewers = this.options.reviewerPolicy?.({ packet, aggregate }) ?? null;
|
|
49
|
+
let proposed;
|
|
50
|
+
let finalRequest = primary;
|
|
51
|
+
if (selectedReviewers !== null) {
|
|
52
|
+
scheduler.requestReviewers(primary.requestId, selectedReviewers);
|
|
53
|
+
for (const reviewer of scheduler.dispatchedReviewers)
|
|
54
|
+
scheduler.submitOpinion(reviewer.requestId, { status: 'offline-fixture', label: 'SIMULATED' });
|
|
55
|
+
const synthesis = scheduler.synthesize();
|
|
56
|
+
finalRequest = scheduler.startPrimary();
|
|
57
|
+
proposed = this.options.reviewPolicy({ packet, aggregate, acceptance });
|
|
58
|
+
proposed = { ...proposed, rationale: `${proposed.rationale}; offline reviewer opinions=${synthesis.opinions.length} (SIMULATED)` };
|
|
59
|
+
}
|
|
60
|
+
else
|
|
61
|
+
proposed = this.options.reviewPolicy({ packet, aggregate, acceptance });
|
|
62
|
+
const decision = proposed.outcome === 'done' && !acceptance.complete ? { outcome: 'investigate', rationale: 'Acceptance source is incomplete; completion claim is not authoritative' } : proposed;
|
|
63
|
+
scheduler.finalize(finalRequest.requestId, decision);
|
|
64
|
+
const requested = input.disposition ?? (decision.outcome === 'done' ? 'done' : decision.outcome === 'execute' ? 'execute' : decision.outcome === 'needs_owner' ? 'needs-owner' : 'investigate');
|
|
65
|
+
if (!['execute', 'investigate', 'needs-owner', 'done'].includes(requested))
|
|
66
|
+
throw new TypeError('invalid milestone disposition');
|
|
67
|
+
const finalKind = requested === 'done' && !acceptance.complete ? 'investigate' : requested;
|
|
68
|
+
const record = Object.freeze({ recordId: `disposition-${input.milestoneId}`, milestoneId: input.milestoneId, kind: finalKind, path: finalKind, reason: finalKind === 'done' ? 'acceptance-source-complete' : finalKind === 'needs-owner' ? 'owner decision required' : `primary ${decision.outcome}` });
|
|
69
|
+
let workOrder = null;
|
|
70
|
+
if (finalKind === 'execute' && this.options.dispatch) {
|
|
71
|
+
const dispatch = this.options.dispatch;
|
|
72
|
+
workOrder = Object.freeze({ commandId: `command-${input.milestoneId}`, requestId: `milestone-${input.milestoneId}`, workOrderId: `work-${input.milestoneId}`, taskId: dispatch.taskId, currentTaskId: dispatch.taskId, intentRevision: dispatch.intentRevision, currentRevision: dispatch.currentRevision(), epoch: dispatch.epoch, currentEpoch: dispatch.currentEpoch(), authorizationRef: dispatch.authorizationRef, scope: dispatch.scope, requestedScope: dispatch.scope, permissionGranted: dispatch.permissionGranted, priority: 0, payload: Object.freeze({ cwd: this.options.cwd, instruction: this.options.intent, milestoneId: input.milestoneId }) });
|
|
73
|
+
if (!this.options.queue.enqueue(workOrder).accepted)
|
|
74
|
+
workOrder = null;
|
|
75
|
+
}
|
|
76
|
+
this.records.set(input.milestoneId, record);
|
|
77
|
+
const accounting = scheduler.accounting;
|
|
78
|
+
return Object.freeze({ aggregate, packet, acceptance, decision, accounting: Object.freeze({ primaryCalls: accounting.primaryCalls, acknowledgementCalls: accounting.acknowledgementCalls }), reviewers: scheduler.dispatchedReviewers, disposition: record, path: record.path, workOrder, queueEvents: this.options.queue.events(), persistence: 'in-memory' });
|
|
79
|
+
}
|
|
80
|
+
async dispatchNext() {
|
|
81
|
+
const dispatch = this.options.dispatch;
|
|
82
|
+
if (!dispatch)
|
|
83
|
+
throw new TypeError('dispatch configuration required');
|
|
84
|
+
const order = this.options.queue.next();
|
|
85
|
+
if (!order)
|
|
86
|
+
return null;
|
|
87
|
+
const epoch = dispatch.roleEpoch;
|
|
88
|
+
const refuse = (reasonId) => {
|
|
89
|
+
this.options.queue.mark(order.workOrderId, 'rejected', reasonId);
|
|
90
|
+
this.options.queue.record({ type: 'rejected', requestId: order.requestId, workOrderId: order.workOrderId, reasonId });
|
|
91
|
+
return Object.freeze({ status: 'rejected', requestId: order.requestId, workOrderId: order.workOrderId, reasonId });
|
|
92
|
+
};
|
|
93
|
+
if (epoch.events.some((event) => { if (event.kind !== 'work-order-accepted')
|
|
94
|
+
return false; const state = epoch.workOrder(event.workOrderId)?.state; return state === 'accepted' || state === 'running'; }))
|
|
95
|
+
return refuse('MILESTONE_WORK_ORDER_ACTIVE');
|
|
96
|
+
const slot = epoch.primarySlotState(order.workOrderId);
|
|
97
|
+
if (slot === 'unknown')
|
|
98
|
+
return refuse('MILESTONE_SLOT_STATE_INVALID');
|
|
99
|
+
if ((slot === undefined || slot === 'released') && epoch.primarySlotsRemaining === 0)
|
|
100
|
+
return refuse('MILESTONE_SLOT_BUDGET_EXHAUSTED');
|
|
101
|
+
const dispatcher = new Dispatcher(this.options.queue, dispatch.agent, { currentRevision: () => dispatch.currentRevision(), currentEpoch: () => dispatch.currentEpoch(), authorize: (authorizationRef, scope) => dispatch.authorize(authorizationRef, scope), permissionGranted: () => dispatch.permissionGranted });
|
|
102
|
+
const result = await dispatcher.dispatchNext(dispatch.lifecycle(order));
|
|
103
|
+
if (result?.status === 'accepted' || result?.status === 'running')
|
|
104
|
+
epoch.acceptWorkOrder(order.workOrderId, { checkpoint: { revision: order.intentRevision, epoch: order.epoch }, state: 'accepted' });
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
resolveDisposition(milestoneId, disposition) {
|
|
108
|
+
const existing = this.records.get(milestoneId);
|
|
109
|
+
if (!existing)
|
|
110
|
+
throw new TypeError('unknown milestone disposition');
|
|
111
|
+
if (existing.kind === 'needs-owner' && (disposition === 'execute' || disposition === 'done'))
|
|
112
|
+
throw new Error('explicit owner authorization required before disposition transition');
|
|
113
|
+
if (existing.kind === disposition)
|
|
114
|
+
throw new Error('disposition transition must change the current state');
|
|
115
|
+
const acceptanceCriteria = this.options.acceptanceCriteria.map(({ id, text, status }) => {
|
|
116
|
+
const evaluation = this.options.linker.evaluate(id, { revision: this.options.revision, cwd: this.options.cwd });
|
|
117
|
+
const bound = evaluation.links.some((link) => { const record = this.options.evidence.get(link.evidenceId); return !!record && record.revision === this.options.revision && record.cwd === this.options.cwd; });
|
|
118
|
+
return { id, text, status: evaluation.satisfied && bound ? 'satisfied' : status === 'satisfied' ? 'incomplete' : status };
|
|
119
|
+
});
|
|
120
|
+
if (disposition === 'done' && !evaluateAcceptanceCompletion({ acceptanceCriteria, sourceComplete: acceptanceCriteria.every(({ status }) => status === 'satisfied') }).complete)
|
|
121
|
+
throw new Error('acceptance source is incomplete; done disposition is not authorized');
|
|
122
|
+
const updated = Object.freeze({ ...existing, recordId: `${existing.recordId}-${disposition}`, kind: disposition, path: disposition, reason: `explicit transition from ${existing.kind}` });
|
|
123
|
+
this.records.set(milestoneId, updated);
|
|
124
|
+
return updated;
|
|
125
|
+
}
|
|
126
|
+
handover(input) {
|
|
127
|
+
if (!input || !input.intent.trim() || !input.counters || !Number.isSafeInteger(input.counters.primaryCalls) || !Number.isSafeInteger(input.counters.reviewerCalls) || !input.quota || input.quota.kind !== 'fixture' || !Number.isSafeInteger(input.quota.remaining))
|
|
128
|
+
throw new TypeError('valid handover state and quota fixture required');
|
|
129
|
+
return Object.freeze({ intent: input.intent, currentWork: input.currentWork, counters: Object.freeze({ ...input.counters }), fixture: Object.freeze({ kind: 'quota', label: 'SIMULATED', remaining: input.quota.remaining }), liveObservation: 'NOT_RUN' });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=milestone-loop.js.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Envelope } from '../core/events.js';
|
|
2
|
+
import type { RestoredCheckpoint } from '../host/checkpoint.js';
|
|
3
|
+
import { RetentionManager } from '../host/retention.js';
|
|
4
|
+
import { type DeliveryBoundary } from '../storage/events.js';
|
|
5
|
+
import type { StorageDatabase } from '../storage/db.js';
|
|
6
|
+
import { type DeliveryState } from '../storage/reconcile.js';
|
|
7
|
+
export type InflightVerdict = 'confirmed' | 'not-applied' | 'unknown';
|
|
8
|
+
export interface RecoveryDecision {
|
|
9
|
+
readonly requestId: string;
|
|
10
|
+
readonly outboxStatus: string;
|
|
11
|
+
readonly deliveryState: DeliveryState;
|
|
12
|
+
readonly boundaryAudit: readonly DeliveryBoundary[];
|
|
13
|
+
readonly verdict: 'sent' | 'redeliverable' | 'unknown-surfaced' | 'unresolved';
|
|
14
|
+
readonly resolvedBy: InflightVerdict | null;
|
|
15
|
+
}
|
|
16
|
+
export interface StartupReport {
|
|
17
|
+
readonly lease: {
|
|
18
|
+
readonly role: 'controller' | 'follower';
|
|
19
|
+
readonly fence: number | null;
|
|
20
|
+
};
|
|
21
|
+
readonly decisions: readonly RecoveryDecision[];
|
|
22
|
+
readonly surfacedUnknown: readonly string[];
|
|
23
|
+
readonly redeliverable: readonly string[];
|
|
24
|
+
readonly checkpoint: RestoredCheckpoint | null;
|
|
25
|
+
readonly replayedFrom: number;
|
|
26
|
+
readonly replayedCount: number;
|
|
27
|
+
readonly replayBatches: number;
|
|
28
|
+
readonly partialArtifacts: readonly string[];
|
|
29
|
+
readonly dispatchPaused: boolean;
|
|
30
|
+
readonly recoveryError: {
|
|
31
|
+
readonly reason: string;
|
|
32
|
+
} | null;
|
|
33
|
+
}
|
|
34
|
+
export interface RecoveryCoordinatorOptions {
|
|
35
|
+
readonly sessionId: string;
|
|
36
|
+
readonly ownerId: string;
|
|
37
|
+
readonly sessionIdentity: string;
|
|
38
|
+
readonly leaseTtlMs: number;
|
|
39
|
+
readonly now?: () => number;
|
|
40
|
+
readonly resolve: (decision: RecoveryDecision) => Promise<InflightVerdict>;
|
|
41
|
+
readonly checkpoint?: {
|
|
42
|
+
restore(request: {
|
|
43
|
+
sessionId: string;
|
|
44
|
+
sessionIdentity: string;
|
|
45
|
+
}): RestoredCheckpoint;
|
|
46
|
+
};
|
|
47
|
+
readonly retention?: RetentionManager;
|
|
48
|
+
readonly replayBatchSize?: number;
|
|
49
|
+
}
|
|
50
|
+
export declare class RecoveryCoordinator {
|
|
51
|
+
private readonly db;
|
|
52
|
+
private readonly options;
|
|
53
|
+
private readonly leases;
|
|
54
|
+
private readonly reconciliation;
|
|
55
|
+
private readonly journal;
|
|
56
|
+
private readonly clock;
|
|
57
|
+
private readonly batchSize;
|
|
58
|
+
private lease;
|
|
59
|
+
private paused;
|
|
60
|
+
private lastReplayBatches;
|
|
61
|
+
constructor(db: StorageDatabase, options: RecoveryCoordinatorOptions);
|
|
62
|
+
start(): {
|
|
63
|
+
role: 'controller' | 'follower';
|
|
64
|
+
fence: number | null;
|
|
65
|
+
};
|
|
66
|
+
reconcileOnStartup(): Promise<StartupReport>;
|
|
67
|
+
authorizeDispatch<T>(dispatch: () => T): T;
|
|
68
|
+
isDispatchPaused(): boolean;
|
|
69
|
+
recordEvent(input: unknown): void;
|
|
70
|
+
replayEvents(afterSequence: number): Iterable<Envelope>;
|
|
71
|
+
private applyResolution;
|
|
72
|
+
private mutate;
|
|
73
|
+
private report;
|
|
74
|
+
}
|