@ngockhoale/ckit 0.0.0-stage → 0.1.0
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 +42 -0
- package/README.md +116 -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 +472 -0
- package/dist/src/cli/demo.d.ts +1 -0
- package/dist/src/cli/demo.js +30 -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 +111 -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 +155 -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 +675 -0
- package/gui/index.html +51 -0
- package/gui/network.js +413 -0
- package/package.json +45 -4
- package/src/storage/schema.sql +42 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Envelope } from './events.js';
|
|
2
|
+
import type { EventSeq } from './ids.js';
|
|
3
|
+
import { type DecisionBudget } from './policy.js';
|
|
4
|
+
import type { AgentRunStatus } from './entities.js';
|
|
5
|
+
export interface AgentState {
|
|
6
|
+
readonly runId: string;
|
|
7
|
+
readonly state: AgentRunStatus;
|
|
8
|
+
}
|
|
9
|
+
export interface RequestState {
|
|
10
|
+
readonly requestId: string;
|
|
11
|
+
readonly delivery: 'prepared' | 'sent' | 'unknown' | 'failed';
|
|
12
|
+
readonly slot: number | null;
|
|
13
|
+
readonly outcome: 'pending' | 'partial' | 'done' | 'unknown';
|
|
14
|
+
}
|
|
15
|
+
export interface OrchestrationState {
|
|
16
|
+
readonly session: SessionState;
|
|
17
|
+
readonly agents: readonly AgentState[];
|
|
18
|
+
readonly requests: readonly RequestState[];
|
|
19
|
+
readonly budget: DecisionBudget | null;
|
|
20
|
+
readonly paused: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function initialOrchestration(sessionId: Envelope['sessionId'], budget?: DecisionBudget | null): OrchestrationState;
|
|
23
|
+
/** Pause gates future work; it never fabricates a stopped/interrupted agent state. */
|
|
24
|
+
export declare function pauseOrchestration(state: OrchestrationState): OrchestrationState;
|
|
25
|
+
export declare function canDispatchAgent(state: OrchestrationState, agentState: AgentRunStatus): boolean;
|
|
26
|
+
export declare function recordAgentTurnResult(state: OrchestrationState, runId: string, result: 'partial' | 'done' | 'failed'): OrchestrationState;
|
|
27
|
+
export declare function validateEventReferences(events: readonly Envelope[]): void;
|
|
28
|
+
export type SessionPhase = 'empty' | 'taskActive' | 'awaitingAdvisor' | 'needsReconciliation' | 'closed';
|
|
29
|
+
export interface TaskView {
|
|
30
|
+
readonly id: Envelope['taskId'];
|
|
31
|
+
readonly title: string;
|
|
32
|
+
}
|
|
33
|
+
export interface SessionState {
|
|
34
|
+
readonly sessionId: Envelope['sessionId'];
|
|
35
|
+
readonly phase: SessionPhase;
|
|
36
|
+
readonly task: TaskView | null;
|
|
37
|
+
/** Last applied seq; 0 when empty. Monotonic, gap-free. */
|
|
38
|
+
readonly lastSeq: EventSeq;
|
|
39
|
+
/** Terminal result of the task, when the session closed. */
|
|
40
|
+
readonly outcome: 'success' | 'failed' | 'unknown' | null;
|
|
41
|
+
}
|
|
42
|
+
export declare class TransitionError extends Error {
|
|
43
|
+
readonly from: SessionPhase;
|
|
44
|
+
readonly eventType: string;
|
|
45
|
+
constructor(message: string, from: SessionPhase, eventType: string);
|
|
46
|
+
}
|
|
47
|
+
export declare function initialSession(sessionId: Envelope['sessionId']): SessionState;
|
|
48
|
+
/**
|
|
49
|
+
* Apply one already-validated envelope to the session state.
|
|
50
|
+
* Sequence must be exactly lastSeq+1 (gap-free); the session id and task id must match.
|
|
51
|
+
* Throws {@link TransitionError} on any rule violation; the caller's state is never mutated.
|
|
52
|
+
*/
|
|
53
|
+
export declare function reduce(state: SessionState, env: Envelope): SessionState;
|
|
54
|
+
/** Fold a validated event list over the initial state — replay for determinism checks. */
|
|
55
|
+
export declare function replay(sessionId: Envelope['sessionId'], events: readonly Envelope[]): SessionState;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { canDispatch } from './policy.js';
|
|
2
|
+
export function initialOrchestration(sessionId, budget = null) {
|
|
3
|
+
return Object.freeze({ session: initialSession(sessionId), agents: Object.freeze([]), requests: Object.freeze([]), budget, paused: false });
|
|
4
|
+
}
|
|
5
|
+
/** Pause gates future work; it never fabricates a stopped/interrupted agent state. */
|
|
6
|
+
export function pauseOrchestration(state) {
|
|
7
|
+
return Object.freeze({ ...state, paused: true });
|
|
8
|
+
}
|
|
9
|
+
export function canDispatchAgent(state, agentState) {
|
|
10
|
+
return canDispatch({ paused: state.paused, agentState });
|
|
11
|
+
}
|
|
12
|
+
export function recordAgentTurnResult(state, runId, result) {
|
|
13
|
+
if (!runId)
|
|
14
|
+
throw new TypeError('runId required');
|
|
15
|
+
const agents = state.agents.map(agent => agent.runId === runId ? Object.freeze({ ...agent, state: result === 'failed' ? 'failed' : 'idle' }) : agent);
|
|
16
|
+
const requests = state.requests.map(request => request.requestId === runId ? Object.freeze({ ...request, outcome: result === 'failed' ? 'unknown' : result }) : request);
|
|
17
|
+
return Object.freeze({ ...state, agents: Object.freeze(agents), requests: Object.freeze(requests) });
|
|
18
|
+
} /* an agent/tool turn result does not complete the task/session */
|
|
19
|
+
export function validateEventReferences(events) {
|
|
20
|
+
const ids = new Set(events.map(event => event.eventId));
|
|
21
|
+
for (const event of events) {
|
|
22
|
+
if (event.parentEventId !== null && !ids.has(event.parentEventId))
|
|
23
|
+
throw new TransitionError('parentEventId reference missing', 'empty', event.payload.type);
|
|
24
|
+
if (event.causationId !== null && !ids.has(event.causationId))
|
|
25
|
+
throw new TransitionError('causationId reference missing', 'empty', event.payload.type);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class TransitionError extends Error {
|
|
29
|
+
from;
|
|
30
|
+
eventType;
|
|
31
|
+
constructor(message, from, eventType) {
|
|
32
|
+
super(`${message} (from=${from}, event=${eventType})`);
|
|
33
|
+
this.from = from;
|
|
34
|
+
this.eventType = eventType;
|
|
35
|
+
this.name = 'TransitionError';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function initialSession(sessionId) {
|
|
39
|
+
return { sessionId, phase: 'empty', task: null, lastSeq: 0, outcome: null };
|
|
40
|
+
}
|
|
41
|
+
function must(cond, message, from, eventType) {
|
|
42
|
+
if (!cond)
|
|
43
|
+
throw new TransitionError(message, from, eventType);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Apply one already-validated envelope to the session state.
|
|
47
|
+
* Sequence must be exactly lastSeq+1 (gap-free); the session id and task id must match.
|
|
48
|
+
* Throws {@link TransitionError} on any rule violation; the caller's state is never mutated.
|
|
49
|
+
*/
|
|
50
|
+
export function reduce(state, env) {
|
|
51
|
+
must(env.seq === state.lastSeq + 1, 'seq must be lastSeq+1', state.phase, env.payload.type);
|
|
52
|
+
must(env.sessionId === state.sessionId, 'sessionId mismatch', state.phase, env.payload.type);
|
|
53
|
+
if (state.task !== null) {
|
|
54
|
+
must(env.taskId === state.task.id, 'taskId mismatch', state.phase, env.payload.type);
|
|
55
|
+
}
|
|
56
|
+
const next = applyPayload(state, env);
|
|
57
|
+
return { ...next, lastSeq: env.seq };
|
|
58
|
+
}
|
|
59
|
+
function applyPayload(s, env) {
|
|
60
|
+
const t = env.payload.type;
|
|
61
|
+
switch (s.phase) {
|
|
62
|
+
case 'empty': {
|
|
63
|
+
must(t === 'TaskCreated', `phase empty only accepts TaskCreated`, s.phase, t);
|
|
64
|
+
const p = env.payload;
|
|
65
|
+
if (p.type !== 'TaskCreated')
|
|
66
|
+
throw new TransitionError('unreachable', s.phase, t);
|
|
67
|
+
return { sessionId: s.sessionId, phase: 'taskActive', task: { id: env.taskId, title: p.title }, lastSeq: s.lastSeq, outcome: null };
|
|
68
|
+
}
|
|
69
|
+
case 'taskActive': {
|
|
70
|
+
must(t === 'AdvisorDispatched' || t === 'TaskCompleted', 'taskActive accepts AdvisorDispatched or TaskCompleted', s.phase, t);
|
|
71
|
+
if (t === 'AdvisorDispatched') {
|
|
72
|
+
return { ...s, phase: 'awaitingAdvisor' };
|
|
73
|
+
}
|
|
74
|
+
const p = env.payload;
|
|
75
|
+
if (p.type !== 'TaskCompleted')
|
|
76
|
+
throw new TransitionError('unreachable', s.phase, t);
|
|
77
|
+
must(p.outcome !== 'unknown', 'close with unknown outcome requires needsReconciliation', s.phase, t);
|
|
78
|
+
return { ...s, phase: 'closed', outcome: p.outcome };
|
|
79
|
+
}
|
|
80
|
+
case 'awaitingAdvisor': {
|
|
81
|
+
must(t === 'AdvisorAcked' || t === 'AdvisorCompleted' || t === 'AdvisorUnknownOutcome', 'awaitingAdvisor accepts advisor lifecycle events only', s.phase, t);
|
|
82
|
+
if (t === 'AdvisorAcked') {
|
|
83
|
+
// Ack is transport-level only: the session keeps waiting; it is NOT completion.
|
|
84
|
+
return { ...s };
|
|
85
|
+
}
|
|
86
|
+
if (t === 'AdvisorCompleted') {
|
|
87
|
+
return { ...s, phase: 'taskActive' };
|
|
88
|
+
}
|
|
89
|
+
return { ...s, phase: 'needsReconciliation' };
|
|
90
|
+
}
|
|
91
|
+
case 'needsReconciliation': {
|
|
92
|
+
// Fail closed: no new dispatch from here (prevents blind resend); owner/core reconciles, then closes.
|
|
93
|
+
must(t === 'TaskCompleted' && env.payload.type === 'TaskCompleted', 'needsReconciliation accepts only TaskCompleted after reconciliation', s.phase, t);
|
|
94
|
+
const p = env.payload;
|
|
95
|
+
if (p.type !== 'TaskCompleted')
|
|
96
|
+
throw new TransitionError('unreachable', s.phase, t);
|
|
97
|
+
must(p.outcome !== 'success', 'success cannot be claimed from an unverified outcome', s.phase, t);
|
|
98
|
+
return { ...s, phase: 'closed', outcome: p.outcome };
|
|
99
|
+
}
|
|
100
|
+
case 'closed': {
|
|
101
|
+
throw new TransitionError('session is closed; no further events accepted', s.phase, t);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Fold a validated event list over the initial state — replay for determinism checks. */
|
|
106
|
+
export function replay(sessionId, events) {
|
|
107
|
+
validateEventReferences(events);
|
|
108
|
+
return events.reduce(reduce, initialSession(sessionId));
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=machine.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AgentRunStatus } from './entities.js';
|
|
2
|
+
export interface PrimaryAuthority {
|
|
3
|
+
readonly epoch: number;
|
|
4
|
+
readonly primaryId: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function advanceEpoch(current: PrimaryAuthority, primaryId: string): PrimaryAuthority;
|
|
7
|
+
export declare function selectPrimaryResponse(current: PrimaryAuthority, responseEpoch: number, responderId: string): 'current' | 'stale';
|
|
8
|
+
export type SlotStatus = 'reserved' | 'consumed' | 'needsReconciliation' | 'released';
|
|
9
|
+
export interface BudgetSlot {
|
|
10
|
+
readonly requestId: string;
|
|
11
|
+
readonly status: SlotStatus;
|
|
12
|
+
}
|
|
13
|
+
export interface ReviewerSlot {
|
|
14
|
+
readonly requestId: string;
|
|
15
|
+
readonly reviewerId: string;
|
|
16
|
+
readonly evidenceRevision: number;
|
|
17
|
+
readonly status: 'sent' | 'needsReconciliation';
|
|
18
|
+
}
|
|
19
|
+
export interface DecisionBudget {
|
|
20
|
+
readonly decisionId: string;
|
|
21
|
+
readonly primary: readonly BudgetSlot[];
|
|
22
|
+
readonly reviewers: readonly ReviewerSlot[];
|
|
23
|
+
readonly outcome: 'open' | 'needs_owner';
|
|
24
|
+
}
|
|
25
|
+
export declare function createDecisionBudget(decisionId: string): DecisionBudget;
|
|
26
|
+
export declare function reservePrimary(budget: DecisionBudget, requestId: string): DecisionBudget;
|
|
27
|
+
export declare function settlePrimary(budget: DecisionBudget, requestId: string, result: 'sent' | 'unknown' | 'failedBeforeSend'): DecisionBudget;
|
|
28
|
+
export declare function reserveReviewer(budget: DecisionBudget, requestId: string, reviewerId: string, evidenceRevision: number, maxReviewers?: number): DecisionBudget;
|
|
29
|
+
export declare function canDispatch(input: {
|
|
30
|
+
readonly paused: boolean;
|
|
31
|
+
readonly agentState: AgentRunStatus;
|
|
32
|
+
}): boolean;
|
|
33
|
+
export declare function pausedAgentState(state: AgentRunStatus, paused: boolean): AgentRunStatus;
|
|
34
|
+
export declare function nextPrimaryOutcome(budget: DecisionBudget, proposed: string): 'needs_owner' | 'final' | 'continue';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export function advanceEpoch(current, primaryId) {
|
|
2
|
+
if (!primaryId)
|
|
3
|
+
throw new TypeError('primaryId required');
|
|
4
|
+
return Object.freeze({ epoch: current.epoch + 1, primaryId });
|
|
5
|
+
}
|
|
6
|
+
export function selectPrimaryResponse(current, responseEpoch, responderId) {
|
|
7
|
+
return current.epoch === responseEpoch && current.primaryId === responderId ? 'current' : 'stale';
|
|
8
|
+
}
|
|
9
|
+
export function createDecisionBudget(decisionId) {
|
|
10
|
+
if (!decisionId)
|
|
11
|
+
throw new TypeError('decisionId required');
|
|
12
|
+
return Object.freeze({ decisionId, primary: Object.freeze([]), reviewers: Object.freeze([]), outcome: 'open' });
|
|
13
|
+
}
|
|
14
|
+
export function reservePrimary(budget, requestId) {
|
|
15
|
+
if (!requestId)
|
|
16
|
+
throw new TypeError('requestId required');
|
|
17
|
+
if (budget.primary.some(slot => slot.requestId === requestId))
|
|
18
|
+
return budget;
|
|
19
|
+
const releasedIndex = budget.primary.findIndex(slot => slot.status === 'released');
|
|
20
|
+
if ((budget.outcome !== 'open' && releasedIndex < 0) || (releasedIndex < 0 && budget.primary.length >= 2)) {
|
|
21
|
+
return Object.freeze({ ...budget, outcome: 'needs_owner' });
|
|
22
|
+
}
|
|
23
|
+
const replacement = Object.freeze({ requestId, status: 'reserved' });
|
|
24
|
+
const primary = [...budget.primary];
|
|
25
|
+
if (releasedIndex >= 0)
|
|
26
|
+
primary[releasedIndex] = replacement;
|
|
27
|
+
else
|
|
28
|
+
primary.push(replacement);
|
|
29
|
+
return Object.freeze({ ...budget, primary: Object.freeze(primary), outcome: 'open' });
|
|
30
|
+
}
|
|
31
|
+
export function settlePrimary(budget, requestId, result) {
|
|
32
|
+
const slot = budget.primary.find(s => s.requestId === requestId);
|
|
33
|
+
if (!slot || slot.status !== 'reserved')
|
|
34
|
+
throw new TypeError('primary request is not reserved');
|
|
35
|
+
const status = result === 'failedBeforeSend' ? 'released' : result === 'unknown' ? 'needsReconciliation' : 'consumed';
|
|
36
|
+
return Object.freeze({ ...budget, primary: Object.freeze(budget.primary.map(s => s === slot ? Object.freeze({ ...s, status }) : s)) });
|
|
37
|
+
}
|
|
38
|
+
export function reserveReviewer(budget, requestId, reviewerId, evidenceRevision, maxReviewers = 2) {
|
|
39
|
+
if (!requestId || !reviewerId || !Number.isSafeInteger(evidenceRevision) || evidenceRevision < 1)
|
|
40
|
+
throw new TypeError('invalid reviewer request');
|
|
41
|
+
if (budget.reviewers.some(s => s.requestId === requestId))
|
|
42
|
+
return budget;
|
|
43
|
+
if (budget.reviewers.length >= maxReviewers || budget.reviewers.some(s => s.reviewerId === reviewerId))
|
|
44
|
+
throw new TypeError('reviewer budget exceeded');
|
|
45
|
+
return Object.freeze({ ...budget, reviewers: Object.freeze([...budget.reviewers, Object.freeze({ requestId, reviewerId, evidenceRevision, status: 'sent' })]) });
|
|
46
|
+
}
|
|
47
|
+
export function canDispatch(input) {
|
|
48
|
+
return !input.paused && (input.agentState === 'not_started' || input.agentState === 'idle');
|
|
49
|
+
}
|
|
50
|
+
export function pausedAgentState(state, paused) {
|
|
51
|
+
// Pause is a dispatch gate, not a claim that a running process stopped.
|
|
52
|
+
return state;
|
|
53
|
+
}
|
|
54
|
+
export function nextPrimaryOutcome(budget, proposed) {
|
|
55
|
+
if (budget.primary.filter(s => s.status !== 'released').length >= 2 && proposed !== 'done' && proposed !== 'execute')
|
|
56
|
+
return 'needs_owner';
|
|
57
|
+
return budget.primary.length >= 2 ? 'final' : 'continue';
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SourceRole } from './entities.js';
|
|
2
|
+
export type MessageOrigin = 'ckit' | 'web' | 'repository' | 'agent' | 'unknown';
|
|
3
|
+
export interface IncomingSourceMessage {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly origin: MessageOrigin;
|
|
6
|
+
readonly role: string;
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly transportActor: 'owner' | 'advisor' | 'worker' | 'system';
|
|
9
|
+
}
|
|
10
|
+
export interface ClassifiedSourceMessage {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly origin: MessageOrigin;
|
|
13
|
+
readonly role: SourceRole;
|
|
14
|
+
readonly text: string;
|
|
15
|
+
readonly transportActor: IncomingSourceMessage['transportActor'];
|
|
16
|
+
readonly authoritySource: 'transport';
|
|
17
|
+
}
|
|
18
|
+
export declare function classifySourceMessage(message: IncomingSourceMessage): ClassifiedSourceMessage;
|
|
19
|
+
export declare function promoteRequirement(message: IncomingSourceMessage, actingActor: 'owner' | 'advisor' | 'worker' | 'system'): ClassifiedSourceMessage;
|
|
20
|
+
export declare function isOutboundEcho(message: IncomingSourceMessage, controllerRequestIds: readonly string[]): boolean;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DomainValidationError } from './errors.js';
|
|
2
|
+
export function classifySourceMessage(message) {
|
|
3
|
+
if (!message || typeof message.id !== 'string' || !message.id || typeof message.text !== 'string' || !['ckit', 'web', 'repository', 'agent', 'unknown'].includes(message.origin)) {
|
|
4
|
+
throw new DomainValidationError('ENVELOPE_INVALID', 'source message is malformed');
|
|
5
|
+
}
|
|
6
|
+
const assigned = message.transportActor === 'owner' ? 'owner' : message.transportActor;
|
|
7
|
+
// Claim text is preserved as data; it is never interpreted as authority.
|
|
8
|
+
return { id: message.id, origin: message.origin, role: assigned, text: message.text, transportActor: message.transportActor, authoritySource: 'transport' };
|
|
9
|
+
}
|
|
10
|
+
export function promoteRequirement(message, actingActor) {
|
|
11
|
+
const classified = classifySourceMessage(message);
|
|
12
|
+
if (message.origin !== 'ckit' || message.transportActor !== 'owner' || actingActor !== 'owner') {
|
|
13
|
+
const code = message.role === 'owner' || message.role === 'system' || /"(?:role|policy|command)"\s*:/.test(message.text) ? 'FORGED_AUTHORITY' : 'OWNER_TRANSPORT_REQUIRED';
|
|
14
|
+
throw new DomainValidationError(code, 'requirement promotion requires owner-authenticated transport context');
|
|
15
|
+
}
|
|
16
|
+
return classified;
|
|
17
|
+
}
|
|
18
|
+
export function isOutboundEcho(message, controllerRequestIds) {
|
|
19
|
+
return message.origin === 'web' && message.transportActor !== 'owner' && controllerRequestIds.includes(message.id);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=provenance.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Actor, type CkitEventPayload, type Envelope } from './events.js';
|
|
2
|
+
import type { ProjectId, SessionId, TaskId } from './ids.js';
|
|
3
|
+
export interface EventStep {
|
|
4
|
+
readonly payload: CkitEventPayload;
|
|
5
|
+
readonly actor: Actor;
|
|
6
|
+
readonly status?: string;
|
|
7
|
+
readonly reasonCode?: string | null;
|
|
8
|
+
readonly decision?: {
|
|
9
|
+
readonly decisionId?: string;
|
|
10
|
+
readonly reasonCode: string;
|
|
11
|
+
};
|
|
12
|
+
readonly runId?: string | null;
|
|
13
|
+
readonly attemptId?: string | null;
|
|
14
|
+
readonly adapter?: string | null;
|
|
15
|
+
readonly adapterVersion?: string | null;
|
|
16
|
+
readonly sourceAt?: string | null;
|
|
17
|
+
readonly requirementIds?: readonly string[];
|
|
18
|
+
readonly artifactRefs?: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
export interface RecordingContext {
|
|
21
|
+
readonly sessionId: SessionId;
|
|
22
|
+
readonly taskId: TaskId;
|
|
23
|
+
readonly projectId: ProjectId;
|
|
24
|
+
readonly at: () => string;
|
|
25
|
+
readonly source: 'simulated' | {
|
|
26
|
+
readonly liveRef: string;
|
|
27
|
+
};
|
|
28
|
+
readonly ckitVersion?: string;
|
|
29
|
+
readonly policyVersion?: string;
|
|
30
|
+
readonly promptTemplateVersion?: string;
|
|
31
|
+
readonly redactedConfigHash?: string;
|
|
32
|
+
readonly traceId?: string;
|
|
33
|
+
}
|
|
34
|
+
export declare function recordEvent(context: RecordingContext, step: EventStep, previous?: Envelope): Envelope;
|
|
35
|
+
export declare function recordSteps(context: RecordingContext, steps: readonly EventStep[]): readonly Envelope[];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Event recording seam: every flow step becomes a fail-closed §6.1 envelope.
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { EVENT_SCHEMA_VERSION, validateEnvelope } from './events.js';
|
|
4
|
+
import { attemptId, decisionId, eventId, eventSeq, projectId, runId, sessionId, spanId, taskId, traceId } from './ids.js';
|
|
5
|
+
import { containsSecretText } from '../security/redact.js';
|
|
6
|
+
const sessionSuffix = (session, length) => createHash('sha256').update(session.replace(/[^a-z0-9_-]/g, '')).digest('hex').slice(0, length);
|
|
7
|
+
const eventIdFor = (session, sequence) => eventId(`event-${sessionSuffix(session, 10)}-${String(sequence).padStart(6, '0')}`);
|
|
8
|
+
const spanIdFor = (session, sequence) => spanId(`span-${sessionSuffix(session, 10)}-${String(sequence).padStart(6, '0')}`);
|
|
9
|
+
const traceIdFor = (session) => traceId(`trace-${sessionSuffix(session, 12)}`);
|
|
10
|
+
const SECRET_VALUE = { test: containsSecretText };
|
|
11
|
+
function assertNoSecrets(value) {
|
|
12
|
+
if (typeof value === 'string' && SECRET_VALUE.test(value))
|
|
13
|
+
throw new TypeError('sensitive event content rejected');
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
for (const item of value)
|
|
16
|
+
assertNoSecrets(item);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (typeof value === 'object' && value !== null)
|
|
20
|
+
for (const item of Object.values(value))
|
|
21
|
+
assertNoSecrets(item);
|
|
22
|
+
}
|
|
23
|
+
const safeMetadata = (value) => {
|
|
24
|
+
if (value == null)
|
|
25
|
+
return null;
|
|
26
|
+
if (SECRET_VALUE.test(value))
|
|
27
|
+
throw new TypeError('sensitive metadata rejected');
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
export function recordEvent(context, step, previous) {
|
|
31
|
+
if (previous !== undefined && (previous.sessionId !== context.sessionId || previous.taskId !== context.taskId || previous.projectId !== context.projectId)) {
|
|
32
|
+
throw new TypeError('previous event belongs to a different project/session/task');
|
|
33
|
+
}
|
|
34
|
+
const seq = eventSeq((previous?.seq ?? 0) + 1);
|
|
35
|
+
const event = eventIdFor(context.sessionId, seq);
|
|
36
|
+
assertNoSecrets(step.payload);
|
|
37
|
+
assertNoSecrets(step.actor);
|
|
38
|
+
const reasonCode = step.decision?.reasonCode ?? step.reasonCode ?? null;
|
|
39
|
+
assertNoSecrets(reasonCode);
|
|
40
|
+
assertNoSecrets(context.source === 'simulated' ? null : context.source.liveRef);
|
|
41
|
+
assertNoSecrets(context.redactedConfigHash);
|
|
42
|
+
assertNoSecrets(step.adapter);
|
|
43
|
+
assertNoSecrets(step.adapterVersion);
|
|
44
|
+
if (reasonCode !== null && reasonCode.trim().length === 0)
|
|
45
|
+
throw new TypeError('reasonCode must be non-empty or null');
|
|
46
|
+
const provenance = context.source === 'simulated' ? { source: 'simulated' } : { source: 'live', ref: safeMetadata(context.source.liveRef) ?? '' };
|
|
47
|
+
const candidate = {
|
|
48
|
+
schemaVersion: EVENT_SCHEMA_VERSION, seq, sessionSequence: seq, sessionId: context.sessionId, taskId: context.taskId,
|
|
49
|
+
projectId: context.projectId, at: context.at(), sourceAt: step.sourceAt ?? null, causality: previous?.seq ?? null,
|
|
50
|
+
eventId: event, parentEventId: previous?.eventId ?? null, causationId: previous?.eventId ?? null,
|
|
51
|
+
runId: step.runId == null ? null : runId(step.runId), attemptId: step.attemptId == null ? null : attemptId(step.attemptId),
|
|
52
|
+
traceId: context.traceId === undefined ? traceIdFor(context.sessionId) : traceId(context.traceId), spanId: spanIdFor(context.sessionId, seq),
|
|
53
|
+
actor: step.actor, provenance, adapter: safeMetadata(step.adapter), adapterVersion: safeMetadata(step.adapterVersion),
|
|
54
|
+
eventType: step.payload.type, status: safeMetadata(step.status) ?? 'recorded', reasonCode: safeMetadata(reasonCode),
|
|
55
|
+
artifactRefs: step.artifactRefs ?? [], decisionId: step.decision?.decisionId == null ? null : decisionId(step.decision.decisionId),
|
|
56
|
+
requirementIds: step.requirementIds ?? [], ckitVersion: context.ckitVersion ?? '0.0.0', policyVersion: context.policyVersion ?? '1',
|
|
57
|
+
promptTemplateVersion: context.promptTemplateVersion ?? '1', redactedConfigHash: safeMetadata(context.redactedConfigHash) ?? 'unavailable', payload: step.payload,
|
|
58
|
+
};
|
|
59
|
+
return validateEnvelope(candidate);
|
|
60
|
+
}
|
|
61
|
+
export function recordSteps(context, steps) {
|
|
62
|
+
const result = [];
|
|
63
|
+
for (const step of steps)
|
|
64
|
+
result.push(recordEvent(context, step, result.at(-1)));
|
|
65
|
+
return Object.freeze(result);
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=record.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Envelope } from './events.js';
|
|
2
|
+
import type { ProjectId, SessionId } from './ids.js';
|
|
3
|
+
import { type SessionState } from './machine.js';
|
|
4
|
+
export interface SessionProjection extends SessionState {
|
|
5
|
+
readonly projectId: ProjectId | null;
|
|
6
|
+
}
|
|
7
|
+
/** Pure, read-only projection: groups by session identity and never emits effects. */
|
|
8
|
+
export declare function initialProjection(session: SessionId): SessionProjection;
|
|
9
|
+
export declare function projectSession(session: SessionId, events: readonly Envelope[]): SessionProjection;
|
|
10
|
+
export declare function projectEvents(events: readonly Envelope[]): ReadonlyMap<SessionId, SessionProjection>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { initialSession, reduce, validateEventReferences } from './machine.js';
|
|
2
|
+
/** Pure, read-only projection: groups by session identity and never emits effects. */
|
|
3
|
+
export function initialProjection(session) { return Object.freeze({ ...initialSession(session), projectId: null }); }
|
|
4
|
+
export function projectSession(session, events) {
|
|
5
|
+
let state = initialProjection(session);
|
|
6
|
+
for (const event of events) {
|
|
7
|
+
if (state.projectId !== null && state.projectId !== event.projectId)
|
|
8
|
+
throw new TypeError(`projectId mismatch in session ${session}`);
|
|
9
|
+
state = Object.freeze({ ...reduce(state, event), projectId: state.projectId ?? event.projectId });
|
|
10
|
+
}
|
|
11
|
+
return state;
|
|
12
|
+
}
|
|
13
|
+
export function projectEvents(events) {
|
|
14
|
+
const groups = new Map();
|
|
15
|
+
const canonical = new Map();
|
|
16
|
+
for (const event of events) {
|
|
17
|
+
const previous = canonical.get(event.eventId);
|
|
18
|
+
if (previous !== undefined) {
|
|
19
|
+
if (JSON.stringify(previous) !== JSON.stringify(event))
|
|
20
|
+
throw new TypeError(`conflicting duplicate eventId ${event.eventId}`);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
canonical.set(event.eventId, event);
|
|
24
|
+
const group = groups.get(event.sessionId) ?? [];
|
|
25
|
+
group.push(event);
|
|
26
|
+
groups.set(event.sessionId, group);
|
|
27
|
+
}
|
|
28
|
+
const projections = new Map();
|
|
29
|
+
for (const [session, group] of groups) {
|
|
30
|
+
group.sort((a, b) => a.sessionSequence - b.sessionSequence);
|
|
31
|
+
validateEventReferences(group);
|
|
32
|
+
projections.set(session, projectSession(session, group));
|
|
33
|
+
}
|
|
34
|
+
return projections;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Envelope } from './events.js';
|
|
2
|
+
import { type SessionState } from './machine.js';
|
|
3
|
+
import type { AdvisorPort } from '../ports/advisor.js';
|
|
4
|
+
export interface RunTaskInput {
|
|
5
|
+
readonly sessionId: string;
|
|
6
|
+
readonly taskId: string;
|
|
7
|
+
readonly title: string;
|
|
8
|
+
readonly promptRef: string;
|
|
9
|
+
readonly advisor: AdvisorPort;
|
|
10
|
+
readonly clock?: () => string;
|
|
11
|
+
}
|
|
12
|
+
export declare class RunTaskError extends Error {
|
|
13
|
+
readonly cause_: unknown;
|
|
14
|
+
constructor(message: string, cause_: unknown);
|
|
15
|
+
}
|
|
16
|
+
export interface RunTaskResult {
|
|
17
|
+
readonly state: SessionState;
|
|
18
|
+
readonly events: readonly Envelope[];
|
|
19
|
+
}
|
|
20
|
+
export declare function runTask(input: RunTaskInput): Promise<RunTaskResult>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { projectId, sessionId, taskId } from './ids.js';
|
|
2
|
+
import { recordEvent } from './record.js';
|
|
3
|
+
import { initialSession, replay, reduce } from './machine.js';
|
|
4
|
+
export class RunTaskError extends Error {
|
|
5
|
+
cause_;
|
|
6
|
+
constructor(message, cause_) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.cause_ = cause_;
|
|
9
|
+
this.name = 'RunTaskError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export async function runTask(input) {
|
|
13
|
+
if (!input.advisor.isSimulated) {
|
|
14
|
+
throw new RunTaskError(`offline runTask accepts simulated adapters only (got ${input.advisor.name}); live adapters require the authorized integration path (P03+ / ADR-015)`, input.advisor.name);
|
|
15
|
+
}
|
|
16
|
+
const sid = sessionId(input.sessionId), tid = taskId(input.taskId);
|
|
17
|
+
const now = input.clock ?? (() => new Date().toISOString());
|
|
18
|
+
const recording = { sessionId: sid, taskId: tid, projectId: projectId('project-offline001'), at: now, source: 'simulated', redactedConfigHash: 'offline-demo' };
|
|
19
|
+
const events = [];
|
|
20
|
+
let state = initialSession(sid);
|
|
21
|
+
const push = (step) => {
|
|
22
|
+
const env = recordEvent(recording, step, events.at(-1));
|
|
23
|
+
state = reduce(state, env);
|
|
24
|
+
events.push(env);
|
|
25
|
+
};
|
|
26
|
+
push({ actor: { role: 'owner' }, payload: { type: 'TaskCreated', title: input.title } });
|
|
27
|
+
push({ actor: { role: 'system', adapter: input.advisor.name }, payload: { type: 'AdvisorDispatched', promptRef: input.promptRef, adapter: input.advisor.name } });
|
|
28
|
+
for await (const msg of input.advisor.dispatch({ sessionId: sid, taskId: tid, promptRef: input.promptRef })) {
|
|
29
|
+
if (state.phase !== 'awaitingAdvisor')
|
|
30
|
+
throw new RunTaskError(`advisor streamed ${msg.kind} while session is ${state.phase}; refusing to advance`, msg);
|
|
31
|
+
if (msg.kind === 'ack')
|
|
32
|
+
push({ actor: { role: 'advisor', adapter: input.advisor.name }, payload: { type: 'AdvisorAcked', adapter: input.advisor.name } });
|
|
33
|
+
else if (msg.kind === 'completed') {
|
|
34
|
+
push({ actor: { role: 'advisor', adapter: input.advisor.name }, payload: { type: 'AdvisorCompleted', resultRef: msg.resultRef } });
|
|
35
|
+
push({ actor: { role: 'system' }, payload: { type: 'TaskCompleted', outcome: 'success' } });
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const reason = msg.kind === 'rejected' ? 'adapter-fault' : msg.reason, note = msg.kind === 'rejected' ? `rejected:${msg.reason}` : msg.note;
|
|
39
|
+
push({ actor: { role: 'advisor', adapter: input.advisor.name }, payload: { type: 'AdvisorUnknownOutcome', reason, note } });
|
|
40
|
+
push({ actor: { role: 'system' }, payload: { type: 'TaskCompleted', outcome: 'unknown' } });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const replayed = replay(sid, events);
|
|
44
|
+
if (replayed !== state && JSON.stringify(replayed) !== JSON.stringify(state))
|
|
45
|
+
throw new RunTaskError('replay mismatch: non-deterministic flow', { replayed });
|
|
46
|
+
return { state, events };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=run-task.js.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type ExecutableInspector, type VersionGate } from '../adapters/launch/profile.js';
|
|
2
|
+
import { type ExportBundle } from '../host/export.js';
|
|
3
|
+
import { type JournalLockOptions } from './journal-lock.js';
|
|
4
|
+
export type DoctorStatus = 'ok' | 'warn' | 'fail' | 'not_run';
|
|
5
|
+
export type DoctorCheckId = 'runtime' | 'executable' | 'state-permissions' | 'journal-lock' | 'storage-schema' | 'storage-disk' | 'binding' | 'capabilities' | 'profile-locks' | 'sessions' | 'advisor-config';
|
|
6
|
+
export interface DoctorCheck {
|
|
7
|
+
readonly id: DoctorCheckId;
|
|
8
|
+
readonly status: DoctorStatus;
|
|
9
|
+
readonly summary: string;
|
|
10
|
+
readonly action: string | null;
|
|
11
|
+
readonly tc: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
export interface DoctorReport {
|
|
14
|
+
readonly schemaVersion: 1;
|
|
15
|
+
readonly label: 'OFFLINE';
|
|
16
|
+
readonly overall: 'ok' | 'warn' | 'fail';
|
|
17
|
+
readonly checks: readonly DoctorCheck[];
|
|
18
|
+
readonly connectionCheck?: true;
|
|
19
|
+
}
|
|
20
|
+
export interface DoctorOptions {
|
|
21
|
+
readonly cwd: string;
|
|
22
|
+
readonly now?: () => number;
|
|
23
|
+
readonly nodeVersion?: string;
|
|
24
|
+
readonly minimumNodeMajor?: number;
|
|
25
|
+
readonly executable?: {
|
|
26
|
+
readonly path: string;
|
|
27
|
+
readonly gate?: VersionGate;
|
|
28
|
+
readonly inspector?: ExecutableInspector;
|
|
29
|
+
};
|
|
30
|
+
readonly profileRoots?: readonly string[];
|
|
31
|
+
readonly minFreeBytes?: number;
|
|
32
|
+
readonly statfs?: (path: string) => Promise<{
|
|
33
|
+
readonly bavail: number;
|
|
34
|
+
readonly bsize: number;
|
|
35
|
+
}>;
|
|
36
|
+
readonly lock?: JournalLockOptions;
|
|
37
|
+
/** Pre-loaded advisor configuration state (doctor stays offline and never reads the dotenv file itself). Omitted: the advisor-config check is skipped. */
|
|
38
|
+
/** Executor resolved from CKIT_EXECUTOR / CKIT_EXECUTOR_PATH by the product resolver (ignored when `executable` is given). */
|
|
39
|
+
readonly configuredExecutor?: {
|
|
40
|
+
readonly state: 'resolved';
|
|
41
|
+
readonly kind: string;
|
|
42
|
+
readonly path: string;
|
|
43
|
+
} | {
|
|
44
|
+
readonly state: 'unresolved';
|
|
45
|
+
readonly kind: string;
|
|
46
|
+
readonly problem: string;
|
|
47
|
+
};
|
|
48
|
+
/** True when the caller will make the one bounded GET /models connection check; only changes the header wording. */
|
|
49
|
+
readonly connectionCheck?: boolean;
|
|
50
|
+
readonly advisor?: {
|
|
51
|
+
readonly state: 'unconfigured';
|
|
52
|
+
} | {
|
|
53
|
+
readonly state: 'invalid';
|
|
54
|
+
readonly variables: readonly string[];
|
|
55
|
+
readonly problems?: readonly {
|
|
56
|
+
readonly variable: string;
|
|
57
|
+
readonly problem: string;
|
|
58
|
+
}[];
|
|
59
|
+
} | {
|
|
60
|
+
readonly state: 'ok';
|
|
61
|
+
readonly summary: string;
|
|
62
|
+
readonly versionHint?: boolean;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export declare function projectKey(cwd: string): string;
|
|
66
|
+
/** Read-only diagnostics: never prompts, spawns, opens the network, migrates or repairs anything. */
|
|
67
|
+
export declare function runDoctor(options: DoctorOptions): Promise<DoctorReport>;
|
|
68
|
+
/** Connection row text: a 404 from /models means the endpoint was reached but does not list models. */
|
|
69
|
+
export declare function describeConnection(result: {
|
|
70
|
+
readonly status: string;
|
|
71
|
+
readonly http: number | null;
|
|
72
|
+
}): string;
|
|
73
|
+
export declare function formatDoctorReport(report: DoctorReport): string;
|
|
74
|
+
export declare function buildDiagnosticBundle(report: DoctorReport, input: {
|
|
75
|
+
readonly cwd: string;
|
|
76
|
+
readonly sessionId?: string;
|
|
77
|
+
readonly environment?: Readonly<Record<string, string>>;
|
|
78
|
+
}): ExportBundle;
|