@lumenflow/cli 4.24.0 → 5.0.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/README.md +54 -52
- package/dist/agent-issues-query.js +10 -2
- package/dist/agent-issues-query.js.map +1 -1
- package/dist/agent-runtime-enrollment-events.js +44 -0
- package/dist/agent-runtime-enrollment-events.js.map +1 -0
- package/dist/agent-session-end.js +47 -0
- package/dist/agent-session-end.js.map +1 -1
- package/dist/agent-session-heartbeat.js +250 -0
- package/dist/agent-session-heartbeat.js.map +1 -0
- package/dist/agent-session.js +299 -5
- package/dist/agent-session.js.map +1 -1
- package/dist/capacity-snapshot-emitter.js +73 -0
- package/dist/capacity-snapshot-emitter.js.map +1 -0
- package/dist/claim-queue.js +276 -0
- package/dist/claim-queue.js.map +1 -0
- package/dist/config-set.js +22 -3
- package/dist/config-set.js.map +1 -1
- package/dist/control-plane-sidecar-runner.js +145 -0
- package/dist/control-plane-sidecar-runner.js.map +1 -0
- package/dist/delegation-list.js +160 -1
- package/dist/delegation-list.js.map +1 -1
- package/dist/delegation-role-resolver.js +69 -0
- package/dist/delegation-role-resolver.js.map +1 -0
- package/dist/docs-generate-pack-reference.js +500 -0
- package/dist/docs-generate-pack-reference.js.map +1 -0
- package/dist/docs-sync.js +116 -1
- package/dist/docs-sync.js.map +1 -1
- package/dist/file-edit.js +28 -8
- package/dist/file-edit.js.map +1 -1
- package/dist/file-write.js +29 -5
- package/dist/file-write.js.map +1 -1
- package/dist/gate-co-change.js +25 -7
- package/dist/gate-co-change.js.map +1 -1
- package/dist/gate-conditional.js +19 -7
- package/dist/gate-conditional.js.map +1 -1
- package/dist/gates-runners.js +42 -33
- package/dist/gates-runners.js.map +1 -1
- package/dist/gates-utils.js +34 -20
- package/dist/gates-utils.js.map +1 -1
- package/dist/gates.js +79 -7
- package/dist/gates.js.map +1 -1
- package/dist/hooks/config-resolver.js +10 -1
- package/dist/hooks/config-resolver.js.map +1 -1
- package/dist/init-package-config.js +1 -1
- package/dist/init-package-config.js.map +1 -1
- package/dist/init-scaffolding.js +5 -1
- package/dist/init-scaffolding.js.map +1 -1
- package/dist/init-templates.js +10 -0
- package/dist/init-templates.js.map +1 -1
- package/dist/init.js +1 -1
- package/dist/init.js.map +1 -1
- package/dist/initiative-create.js +17 -0
- package/dist/initiative-create.js.map +1 -1
- package/dist/initiative-remove-wu.js +17 -3
- package/dist/initiative-remove-wu.js.map +1 -1
- package/dist/kernel-event-sync/emitters.js +104 -0
- package/dist/kernel-event-sync/emitters.js.map +1 -0
- package/dist/kernel-event-sync/index.js +13 -0
- package/dist/kernel-event-sync/index.js.map +1 -0
- package/dist/kernel-event-sync/lifecycle-emitters.js +160 -0
- package/dist/kernel-event-sync/lifecycle-emitters.js.map +1 -0
- package/dist/kernel-event-sync/narrow-emissions.js +89 -0
- package/dist/kernel-event-sync/narrow-emissions.js.map +1 -0
- package/dist/kernel-event-sync/software-delivery-emitters.js +297 -0
- package/dist/kernel-event-sync/software-delivery-emitters.js.map +1 -0
- package/dist/lane-lock.js +14 -1
- package/dist/lane-lock.js.map +1 -1
- package/dist/lane-suggest.js +21 -0
- package/dist/lane-suggest.js.map +1 -1
- package/dist/lumenflow-upgrade.js +7 -5
- package/dist/lumenflow-upgrade.js.map +1 -1
- package/dist/mem-context.js +145 -0
- package/dist/mem-context.js.map +1 -1
- package/dist/mem-create.js +39 -6
- package/dist/mem-create.js.map +1 -1
- package/dist/mem-inbox.js +16 -0
- package/dist/mem-inbox.js.map +1 -1
- package/dist/mem-roster.js +95 -0
- package/dist/mem-roster.js.map +1 -0
- package/dist/mem-signal.js +97 -2
- package/dist/mem-signal.js.map +1 -1
- package/dist/metrics-snapshot.js +3 -2
- package/dist/metrics-snapshot.js.map +1 -1
- package/dist/orchestrate-init-status.js +117 -2
- package/dist/orchestrate-init-status.js.map +1 -1
- package/dist/orchestrate-initiative.js +83 -10
- package/dist/orchestrate-initiative.js.map +1 -1
- package/dist/orchestrate-monitor-quality.js +289 -0
- package/dist/orchestrate-monitor-quality.js.map +1 -0
- package/dist/orchestrate-monitor.js +85 -0
- package/dist/orchestrate-monitor.js.map +1 -1
- package/dist/pack-validate.js +127 -2
- package/dist/pack-validate.js.map +1 -1
- package/dist/plan-create.js +18 -0
- package/dist/plan-create.js.map +1 -1
- package/dist/plan-link.js +13 -0
- package/dist/plan-link.js.map +1 -1
- package/dist/plan-promote.js +14 -0
- package/dist/plan-promote.js.map +1 -1
- package/dist/pre-commit-check.js +4 -3
- package/dist/pre-commit-check.js.map +1 -1
- package/dist/public-manifest.js +17 -3
- package/dist/public-manifest.js.map +1 -1
- package/dist/release.js +28 -10
- package/dist/release.js.map +1 -1
- package/dist/session-cross-link.js +139 -0
- package/dist/session-cross-link.js.map +1 -0
- package/dist/sidecar-manager.js +208 -0
- package/dist/sidecar-manager.js.map +1 -0
- package/dist/state-path-resolvers.js +18 -0
- package/dist/state-path-resolvers.js.map +1 -1
- package/dist/stream-heartbeat.js +151 -0
- package/dist/stream-heartbeat.js.map +1 -0
- package/dist/sync-templates.js +56 -2
- package/dist/sync-templates.js.map +1 -1
- package/dist/wu-block.js +47 -5
- package/dist/wu-block.js.map +1 -1
- package/dist/wu-claim-branch.js +8 -4
- package/dist/wu-claim-branch.js.map +1 -1
- package/dist/wu-claim-state.js +5 -3
- package/dist/wu-claim-state.js.map +1 -1
- package/dist/wu-claim-worktree.js +5 -3
- package/dist/wu-claim-worktree.js.map +1 -1
- package/dist/wu-claim.js +261 -9
- package/dist/wu-claim.js.map +1 -1
- package/dist/wu-done-auto-cleanup.js +3 -2
- package/dist/wu-done-auto-cleanup.js.map +1 -1
- package/dist/wu-done-git-ops.js +12 -8
- package/dist/wu-done-git-ops.js.map +1 -1
- package/dist/wu-done-preflight.js +3 -3
- package/dist/wu-done-preflight.js.map +1 -1
- package/dist/wu-done.js +46 -10
- package/dist/wu-done.js.map +1 -1
- package/dist/wu-lifecycle-sync/gate-scope-resolver.js +16 -0
- package/dist/wu-lifecycle-sync/gate-scope-resolver.js.map +1 -0
- package/dist/wu-lifecycle-sync/kernel-event-sync-shim.js +10 -0
- package/dist/wu-lifecycle-sync/kernel-event-sync-shim.js.map +1 -0
- package/dist/wu-prep.js +363 -22
- package/dist/wu-prep.js.map +1 -1
- package/dist/wu-prune.js +68 -27
- package/dist/wu-prune.js.map +1 -1
- package/dist/wu-release.js +34 -3
- package/dist/wu-release.js.map +1 -1
- package/dist/wu-review.js +167 -0
- package/dist/wu-review.js.map +1 -0
- package/dist/wu-spawn-prompt-builders.js +296 -40
- package/dist/wu-spawn-prompt-builders.js.map +1 -1
- package/dist/wu-spawn-strategy-resolver.js +126 -14
- package/dist/wu-spawn-strategy-resolver.js.map +1 -1
- package/dist/wu-unblock.js +52 -22
- package/dist/wu-unblock.js.map +1 -1
- package/package.json +13 -8
- package/packs/agent-runtime/agent-heartbeat.ts +163 -0
- package/packs/agent-runtime/auto-session-integration.ts +874 -0
- package/packs/agent-runtime/delegation-registry-schema.ts +220 -0
- package/packs/agent-runtime/delegation-registry-store.ts +269 -0
- package/packs/agent-runtime/delegation-tree.ts +328 -0
- package/packs/agent-runtime/index.ts +9 -0
- package/packs/agent-runtime/manifest.ts +109 -19
- package/packs/agent-runtime/manifest.yaml +150 -0
- package/packs/agent-runtime/memory-coordination-contract.ts +86 -0
- package/packs/agent-runtime/memory.d.ts +19 -0
- package/packs/agent-runtime/orchestration.ts +238 -23
- package/packs/agent-runtime/package.json +11 -2
- package/packs/agent-runtime/remote-controls/index.ts +7 -0
- package/packs/agent-runtime/remote-controls/operations.ts +399 -0
- package/packs/agent-runtime/remote-controls/port.ts +48 -0
- package/packs/agent-runtime/remote-controls/state-store.ts +258 -0
- package/packs/agent-runtime/remote-controls/types.ts +105 -0
- package/packs/agent-runtime/session-schema.ts +423 -0
- package/packs/agent-runtime/tool-impl/index.ts +1 -0
- package/packs/agent-runtime/tool-impl/remote-controls.mock.ts +252 -0
- package/packs/agent-runtime/tool-impl/remote-controls.ts +273 -0
- package/packs/agent-runtime/tsconfig.json +1 -1
- package/packs/agent-runtime/turn-lifecycle-events.ts +501 -0
- package/packs/sidekick/channel-ingress.ts +137 -0
- package/packs/sidekick/manifest.ts +74 -0
- package/packs/sidekick/manifest.yaml +88 -0
- package/packs/sidekick/package.json +3 -1
- package/packs/sidekick/sidekick-events.ts +517 -0
- package/packs/sidekick/src/adapters/cloud-queue.ts +101 -0
- package/packs/sidekick/src/adapters/control-plane-bridge.adapter.ts +378 -0
- package/packs/sidekick/src/adapters/filesystem-bridge.adapter.ts +224 -0
- package/packs/sidekick/src/domain/channel.types.ts +84 -0
- package/packs/sidekick/src/ports/channel-bridge.port.ts +75 -0
- package/packs/sidekick/src/routines/commit.ts +74 -0
- package/packs/sidekick/tool-impl/channel-tools.ts +47 -0
- package/packs/sidekick/tool-impl/memory-tools.ts +17 -0
- package/packs/sidekick/tool-impl/routine-commit.ts +102 -0
- package/packs/sidekick/tool-impl/routine-tools.ts +67 -7
- package/packs/sidekick/tool-impl/runtime-context.ts +4 -0
- package/packs/sidekick/tool-impl/storage.ts +3 -0
- package/packs/sidekick/tool-impl/system-tools.ts +7 -0
- package/packs/sidekick/tool-impl/task-tools.ts +46 -0
- package/packs/sidekick/tsconfig.json +1 -1
- package/packs/software-delivery/manifest-schema.ts +30 -0
- package/packs/software-delivery/manifest.ts +160 -11
- package/packs/software-delivery/manifest.yaml +210 -230
- package/packs/software-delivery/package.json +88 -3
- package/packs/software-delivery/src/commands/index.ts +5 -0
- package/packs/software-delivery/src/config/delivery-review-contract.ts +20 -0
- package/packs/software-delivery/src/config/env-accessors.ts +19 -0
- package/packs/software-delivery/src/config/index.ts +8 -0
- package/packs/software-delivery/src/config/normalize-config-keys.ts +19 -0
- package/packs/software-delivery/src/config/schemas/lumenflow-config-schema-types.ts +436 -0
- package/packs/software-delivery/src/config/workspace-reader.ts +310 -0
- package/packs/software-delivery/src/constants/backlog-patterns.ts +31 -0
- package/packs/software-delivery/src/constants/client-ids.ts +19 -0
- package/packs/software-delivery/src/constants/config-contract.ts +7 -0
- package/packs/software-delivery/src/constants/docs-layout-presets.ts +50 -0
- package/packs/software-delivery/src/constants/duration-constants.ts +20 -0
- package/packs/software-delivery/src/constants/gate-constants.ts +32 -0
- package/packs/software-delivery/src/constants/index.ts +29 -0
- package/packs/software-delivery/src/constants/lock-constants.ts +35 -0
- package/packs/software-delivery/src/constants/object-guards.ts +12 -0
- package/packs/software-delivery/src/constants/section-headings.ts +107 -0
- package/packs/software-delivery/src/constants/wu-cli-constants.ts +485 -0
- package/packs/software-delivery/src/constants/wu-domain-constants.ts +466 -0
- package/packs/software-delivery/src/constants/wu-git-constants.ts +7 -0
- package/packs/software-delivery/src/constants/wu-id-format.ts +327 -0
- package/packs/software-delivery/src/constants/wu-paths-constants.ts +358 -0
- package/packs/software-delivery/src/constants/wu-statuses.ts +287 -0
- package/packs/software-delivery/src/constants/wu-type-helpers.ts +67 -0
- package/packs/software-delivery/src/constants/wu-ui-constants.ts +267 -0
- package/packs/software-delivery/src/constants/wu-validation-constants.ts +73 -0
- package/packs/software-delivery/src/domain/index.ts +5 -0
- package/packs/software-delivery/src/domain/orchestration.constants.ts +168 -0
- package/packs/software-delivery/src/domain/orchestration.schemas.ts +239 -0
- package/packs/software-delivery/src/domain/orchestration.types.ts +178 -0
- package/packs/software-delivery/src/methodology/incremental-test.ts +90 -0
- package/packs/software-delivery/src/methodology/index.ts +6 -0
- package/packs/software-delivery/src/methodology/manual-test-validator.ts +292 -0
- package/packs/software-delivery/src/policy/coverage-gate.ts +270 -0
- package/packs/software-delivery/src/policy/gates-agent-mode.ts +223 -0
- package/packs/software-delivery/src/policy/gates-config-internal.ts +121 -0
- package/packs/software-delivery/src/policy/gates-config.ts +293 -0
- package/packs/software-delivery/src/policy/gates-coverage.ts +247 -0
- package/packs/software-delivery/src/policy/gates-presets.ts +134 -0
- package/packs/software-delivery/src/policy/gates-schemas.ts +173 -0
- package/packs/software-delivery/src/policy/index.ts +22 -0
- package/packs/software-delivery/src/policy/package-manager-resolver.ts +319 -0
- package/packs/software-delivery/src/policy/resolve-policy.ts +518 -0
- package/packs/software-delivery/src/ports/config.ports.ts +90 -0
- package/packs/software-delivery/src/ports/dashboard-renderer.port.ts +125 -0
- package/packs/software-delivery/src/ports/index.ts +10 -0
- package/packs/software-delivery/src/ports/sync-validator.ports.ts +59 -0
- package/packs/software-delivery/src/ports/wu-helpers.ports.ts +168 -0
- package/packs/software-delivery/src/ports/wu-state.ports.ts +241 -0
- package/packs/software-delivery/src/primitives/index.ts +5 -0
- package/packs/software-delivery/src/runtime/index.ts +6 -0
- package/packs/software-delivery/src/runtime/work-classifier.ts +561 -0
- package/packs/software-delivery/src/sandbox/index.ts +10 -0
- package/packs/software-delivery/src/sandbox/sandbox-allowlist.ts +118 -0
- package/packs/software-delivery/src/sandbox/sandbox-backend-linux.ts +88 -0
- package/packs/software-delivery/src/sandbox/sandbox-backend-macos.ts +154 -0
- package/packs/software-delivery/src/sandbox/sandbox-backend-windows.ts +47 -0
- package/packs/software-delivery/src/sandbox/sandbox-profile.ts +153 -0
- package/packs/software-delivery/src/schemas/index.ts +5 -0
- package/packs/software-delivery/src/state/date-utils.ts +158 -0
- package/packs/software-delivery/src/state/index.ts +15 -0
- package/packs/software-delivery/src/state/state-machine.ts +119 -0
- package/packs/software-delivery/src/state/wu-doc-types.ts +51 -0
- package/packs/software-delivery/src/state/wu-paths.ts +381 -0
- package/packs/software-delivery/src/state/wu-schema.ts +1139 -0
- package/packs/software-delivery/src/state/wu-state-schema.ts +255 -0
- package/packs/software-delivery/src/state/wu-yaml.ts +338 -0
- package/packs/software-delivery/src/types.d.ts +16 -0
- package/packs/software-delivery/tool-impl/wu-lifecycle-tools.ts +18 -0
- package/packs/software-delivery/tsconfig.json +28 -2
- package/templates/core/AGENTS.md.template +76 -17
- package/templates/core/LUMENFLOW.md.template +265 -66
- package/templates/core/_frameworks/lumenflow/wu-sizing-guide.md.template +180 -116
- package/templates/core/ai/onboarding/agent-invocation-guide.md.template +26 -8
- package/templates/core/ai/onboarding/existing-project-bootstrap.md.template +171 -0
- package/templates/core/ai/onboarding/first-15-mins.md.template +3 -1
- package/templates/core/ai/onboarding/first-wu-mistakes.md.template +1 -1
- package/templates/core/ai/onboarding/initiative-orchestration.md.template +46 -30
- package/templates/core/ai/onboarding/quick-ref-commands.md.template +36 -33
- package/templates/core/ai/onboarding/release-process.md.template +8 -7
- package/templates/core/ai/onboarding/starting-prompt.md.template +2 -0
- package/templates/core/ai/onboarding/troubleshooting-wu-done.md.template +62 -0
- package/templates/vendors/claude/.claude/CLAUDE.md.template +29 -54
- package/templates/vendors/cursor/.cursor/rules/lumenflow.md.template +24 -52
- package/templates/vendors/windsurf/.windsurf/rules/lumenflow.md.template +24 -52
- package/packs/agent-runtime/.turbo/turbo-build.log +0 -4
- package/packs/sidekick/.turbo/turbo-build.log +0 -4
- package/packs/software-delivery/.turbo/turbo-build.log +0 -4
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
// Copyright (c) 2026 Hellmai Ltd
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
+
|
|
6
|
+
const SCHEMA_VERSION_V2 = 2 as const;
|
|
7
|
+
const CONTENT_HASH_ALGORITHM = 'sha256';
|
|
8
|
+
const CONTENT_HASH_ENCODING = 'hex';
|
|
9
|
+
const CONTENT_HASH_PREFIX_LENGTH = 32;
|
|
10
|
+
|
|
11
|
+
export const AGENT_RUNTIME_CHANNEL_ID = 'agent-runtime' as const;
|
|
12
|
+
|
|
13
|
+
export const AGENT_RUNTIME_EVENT_KINDS = {
|
|
14
|
+
TURN_STARTED: 'agent-runtime:turn_started',
|
|
15
|
+
TURN_COMPLETED: 'agent-runtime:turn_completed',
|
|
16
|
+
TURN_ABORTED: 'agent-runtime:turn_aborted',
|
|
17
|
+
TOOL_CALLED: 'agent-runtime:tool_called',
|
|
18
|
+
APPROVAL_REQUIRED: 'agent-runtime:approval_required',
|
|
19
|
+
WORKFLOW_NODE_STARTED: 'agent-runtime:workflow_node_started',
|
|
20
|
+
WORKFLOW_NODE_COMPLETED: 'agent-runtime:workflow_node_completed',
|
|
21
|
+
WORKFLOW_NODE_FAILED: 'agent-runtime:workflow_node_failed',
|
|
22
|
+
WORKFLOW_PAUSED: 'agent-runtime:workflow_paused',
|
|
23
|
+
SCHEDULED_WAKEUP_SET: 'agent-runtime:scheduled_wakeup_set',
|
|
24
|
+
BUDGET_THRESHOLD: 'agent-runtime:budget_threshold',
|
|
25
|
+
AGENT_SESSION_ENROLLED: 'agent-runtime:agent_session_enrolled',
|
|
26
|
+
AGENT_SESSION_ENDED: 'agent-runtime:agent_session_ended',
|
|
27
|
+
AGENT_SESSION_STALLED: 'agent-runtime:agent_session_stalled',
|
|
28
|
+
// WU-2733 (INIT-060 Phase 3, ADR-013 §6 governance):
|
|
29
|
+
// autonomy_changed emitted by elevate_autonomy / lower_autonomy real
|
|
30
|
+
// remote-control impls. Cloud conductor subscribes to render the new
|
|
31
|
+
// autonomy band in the session UI.
|
|
32
|
+
AUTONOMY_CHANGED: 'agent-runtime:autonomy_changed',
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
export const AGENT_RUNTIME_EVENT_KIND_VALUES = [
|
|
36
|
+
AGENT_RUNTIME_EVENT_KINDS.TURN_STARTED,
|
|
37
|
+
AGENT_RUNTIME_EVENT_KINDS.TURN_COMPLETED,
|
|
38
|
+
AGENT_RUNTIME_EVENT_KINDS.TURN_ABORTED,
|
|
39
|
+
AGENT_RUNTIME_EVENT_KINDS.TOOL_CALLED,
|
|
40
|
+
AGENT_RUNTIME_EVENT_KINDS.APPROVAL_REQUIRED,
|
|
41
|
+
AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_STARTED,
|
|
42
|
+
AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_COMPLETED,
|
|
43
|
+
AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_FAILED,
|
|
44
|
+
AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_PAUSED,
|
|
45
|
+
AGENT_RUNTIME_EVENT_KINDS.SCHEDULED_WAKEUP_SET,
|
|
46
|
+
AGENT_RUNTIME_EVENT_KINDS.BUDGET_THRESHOLD,
|
|
47
|
+
AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENROLLED,
|
|
48
|
+
AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENDED,
|
|
49
|
+
AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_STALLED,
|
|
50
|
+
AGENT_RUNTIME_EVENT_KINDS.AUTONOMY_CHANGED,
|
|
51
|
+
] as const;
|
|
52
|
+
|
|
53
|
+
export type AgentRuntimeEventKind = (typeof AGENT_RUNTIME_EVENT_KIND_VALUES)[number];
|
|
54
|
+
|
|
55
|
+
interface AgentRuntimeEventEnvelope {
|
|
56
|
+
schema_version: typeof SCHEMA_VERSION_V2;
|
|
57
|
+
timestamp: string;
|
|
58
|
+
event_id: string;
|
|
59
|
+
channel_id: typeof AGENT_RUNTIME_CHANNEL_ID;
|
|
60
|
+
seq: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface AgentRuntimeTurnCostBreakdown {
|
|
64
|
+
input_tokens_usd: number;
|
|
65
|
+
output_tokens_usd: number;
|
|
66
|
+
tool_calls_usd: number;
|
|
67
|
+
total_usd: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type AgentRuntimeCleanupStatus = 'clean' | 'partial' | 'needs_recovery';
|
|
71
|
+
|
|
72
|
+
export interface TurnStartedEvent extends AgentRuntimeEventEnvelope {
|
|
73
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.TURN_STARTED;
|
|
74
|
+
session_id: string;
|
|
75
|
+
turn_index: number;
|
|
76
|
+
model_profile: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface TurnCompletedEvent extends AgentRuntimeEventEnvelope {
|
|
80
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.TURN_COMPLETED;
|
|
81
|
+
session_id: string;
|
|
82
|
+
turn_index: number;
|
|
83
|
+
status: string;
|
|
84
|
+
cost_breakdown: AgentRuntimeTurnCostBreakdown;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface TurnAbortedEvent extends AgentRuntimeEventEnvelope {
|
|
88
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.TURN_ABORTED;
|
|
89
|
+
session_id: string;
|
|
90
|
+
turn_index: number;
|
|
91
|
+
cleanup_status: AgentRuntimeCleanupStatus;
|
|
92
|
+
recovery_action: string | null;
|
|
93
|
+
reason: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ToolCalledEvent extends AgentRuntimeEventEnvelope {
|
|
97
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.TOOL_CALLED;
|
|
98
|
+
session_id: string;
|
|
99
|
+
turn_index: number;
|
|
100
|
+
tool_name: string;
|
|
101
|
+
tool_call_id: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface ApprovalRequiredEvent extends AgentRuntimeEventEnvelope {
|
|
105
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.APPROVAL_REQUIRED;
|
|
106
|
+
session_id: string;
|
|
107
|
+
turn_index: number;
|
|
108
|
+
tool_name: string;
|
|
109
|
+
request_id: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface WorkflowNodeStartedEvent extends AgentRuntimeEventEnvelope {
|
|
113
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_STARTED;
|
|
114
|
+
session_id: string;
|
|
115
|
+
node_id: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface WorkflowNodeCompletedEvent extends AgentRuntimeEventEnvelope {
|
|
119
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_COMPLETED;
|
|
120
|
+
session_id: string;
|
|
121
|
+
node_id: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface WorkflowNodeFailedEvent extends AgentRuntimeEventEnvelope {
|
|
125
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_FAILED;
|
|
126
|
+
session_id: string;
|
|
127
|
+
node_id: string;
|
|
128
|
+
error_code: string;
|
|
129
|
+
error_message: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface WorkflowPausedEvent extends AgentRuntimeEventEnvelope {
|
|
133
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_PAUSED;
|
|
134
|
+
session_id: string;
|
|
135
|
+
node_id: string;
|
|
136
|
+
reason: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface ScheduledWakeupSetEvent extends AgentRuntimeEventEnvelope {
|
|
140
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.SCHEDULED_WAKEUP_SET;
|
|
141
|
+
session_id: string;
|
|
142
|
+
node_id: string;
|
|
143
|
+
wake_at: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface BudgetThresholdEvent extends AgentRuntimeEventEnvelope {
|
|
147
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.BUDGET_THRESHOLD;
|
|
148
|
+
session_id: string;
|
|
149
|
+
budget_name: string;
|
|
150
|
+
threshold: number;
|
|
151
|
+
observed_value: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface AgentSessionEnrolledEvent extends AgentRuntimeEventEnvelope {
|
|
155
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENROLLED;
|
|
156
|
+
session_id: string;
|
|
157
|
+
wu_id: string;
|
|
158
|
+
lane: string;
|
|
159
|
+
client_type: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface AgentSessionEndedEvent extends AgentRuntimeEventEnvelope {
|
|
163
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENDED;
|
|
164
|
+
session_id: string;
|
|
165
|
+
wu_id: string;
|
|
166
|
+
lane: string;
|
|
167
|
+
incidents_logged: number;
|
|
168
|
+
incidents_major: number;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface AgentSessionStalledEvent extends AgentRuntimeEventEnvelope {
|
|
172
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_STALLED;
|
|
173
|
+
session_id: string;
|
|
174
|
+
wu_id: string;
|
|
175
|
+
lane: string;
|
|
176
|
+
stage: string;
|
|
177
|
+
reason: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* WU-2733 (INIT-060 Phase 3, ADR-013 §6 governance): autonomy_changed
|
|
182
|
+
* carries the transition between autonomy bands ("manual" ↔ "assisted"
|
|
183
|
+
* ↔ "auto") so cloud renders the shift and operators see the change
|
|
184
|
+
* reflected in the evidence feed. `reason` surfaces the operator-supplied
|
|
185
|
+
* rationale when provided.
|
|
186
|
+
*/
|
|
187
|
+
export interface AutonomyChangedEvent extends AgentRuntimeEventEnvelope {
|
|
188
|
+
kind: typeof AGENT_RUNTIME_EVENT_KINDS.AUTONOMY_CHANGED;
|
|
189
|
+
session_id: string;
|
|
190
|
+
previous_level: string;
|
|
191
|
+
new_level: string;
|
|
192
|
+
direction: 'elevate' | 'lower';
|
|
193
|
+
reason: string | null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export type AgentRuntimeEvent =
|
|
197
|
+
| TurnStartedEvent
|
|
198
|
+
| TurnCompletedEvent
|
|
199
|
+
| TurnAbortedEvent
|
|
200
|
+
| ToolCalledEvent
|
|
201
|
+
| ApprovalRequiredEvent
|
|
202
|
+
| WorkflowNodeStartedEvent
|
|
203
|
+
| WorkflowNodeCompletedEvent
|
|
204
|
+
| WorkflowNodeFailedEvent
|
|
205
|
+
| WorkflowPausedEvent
|
|
206
|
+
| ScheduledWakeupSetEvent
|
|
207
|
+
| BudgetThresholdEvent
|
|
208
|
+
| AgentSessionEnrolledEvent
|
|
209
|
+
| AgentSessionEndedEvent
|
|
210
|
+
| AgentSessionStalledEvent
|
|
211
|
+
| AutonomyChangedEvent;
|
|
212
|
+
|
|
213
|
+
export type AgentRuntimeEventSink = (event: AgentRuntimeEvent) => void;
|
|
214
|
+
|
|
215
|
+
export interface AgentRuntimeEventBuildOptions {
|
|
216
|
+
timestamp?: string;
|
|
217
|
+
idempotencyKey?: string;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function resetAgentRuntimeSeqCounter(): void {
|
|
221
|
+
channelSeqCounters.clear();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function createZeroTurnCostBreakdown(): AgentRuntimeTurnCostBreakdown {
|
|
225
|
+
return {
|
|
226
|
+
input_tokens_usd: 0,
|
|
227
|
+
output_tokens_usd: 0,
|
|
228
|
+
tool_calls_usd: 0,
|
|
229
|
+
total_usd: 0,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function buildTurnStartedEvent(
|
|
234
|
+
input: Omit<TurnStartedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
235
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
236
|
+
): TurnStartedEvent {
|
|
237
|
+
return buildAgentRuntimeEvent(
|
|
238
|
+
{
|
|
239
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.TURN_STARTED,
|
|
240
|
+
...input,
|
|
241
|
+
},
|
|
242
|
+
options,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function buildTurnCompletedEvent(
|
|
247
|
+
input: Omit<TurnCompletedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
248
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
249
|
+
): TurnCompletedEvent {
|
|
250
|
+
return buildAgentRuntimeEvent(
|
|
251
|
+
{
|
|
252
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.TURN_COMPLETED,
|
|
253
|
+
...input,
|
|
254
|
+
},
|
|
255
|
+
options,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function buildTurnAbortedEvent(
|
|
260
|
+
input: Omit<TurnAbortedEvent, keyof AgentRuntimeEventEnvelope | 'kind' | 'recovery_action'> & {
|
|
261
|
+
recovery_action?: string | null;
|
|
262
|
+
},
|
|
263
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
264
|
+
): TurnAbortedEvent {
|
|
265
|
+
return buildAgentRuntimeEvent(
|
|
266
|
+
{
|
|
267
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.TURN_ABORTED,
|
|
268
|
+
...input,
|
|
269
|
+
recovery_action: resolveRecoveryAction(input.cleanup_status, input.recovery_action),
|
|
270
|
+
},
|
|
271
|
+
options,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function buildToolCalledEvent(
|
|
276
|
+
input: Omit<ToolCalledEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
277
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
278
|
+
): ToolCalledEvent {
|
|
279
|
+
return buildAgentRuntimeEvent(
|
|
280
|
+
{
|
|
281
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.TOOL_CALLED,
|
|
282
|
+
...input,
|
|
283
|
+
},
|
|
284
|
+
options,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function buildApprovalRequiredEvent(
|
|
289
|
+
input: Omit<ApprovalRequiredEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
290
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
291
|
+
): ApprovalRequiredEvent {
|
|
292
|
+
return buildAgentRuntimeEvent(
|
|
293
|
+
{
|
|
294
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.APPROVAL_REQUIRED,
|
|
295
|
+
...input,
|
|
296
|
+
},
|
|
297
|
+
options,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function buildWorkflowNodeStartedEvent(
|
|
302
|
+
input: Omit<WorkflowNodeStartedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
303
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
304
|
+
): WorkflowNodeStartedEvent {
|
|
305
|
+
return buildAgentRuntimeEvent(
|
|
306
|
+
{
|
|
307
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_STARTED,
|
|
308
|
+
...input,
|
|
309
|
+
},
|
|
310
|
+
options,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function buildWorkflowNodeCompletedEvent(
|
|
315
|
+
input: Omit<WorkflowNodeCompletedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
316
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
317
|
+
): WorkflowNodeCompletedEvent {
|
|
318
|
+
return buildAgentRuntimeEvent(
|
|
319
|
+
{
|
|
320
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_COMPLETED,
|
|
321
|
+
...input,
|
|
322
|
+
},
|
|
323
|
+
options,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function buildWorkflowNodeFailedEvent(
|
|
328
|
+
input: Omit<WorkflowNodeFailedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
329
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
330
|
+
): WorkflowNodeFailedEvent {
|
|
331
|
+
return buildAgentRuntimeEvent(
|
|
332
|
+
{
|
|
333
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_NODE_FAILED,
|
|
334
|
+
...input,
|
|
335
|
+
},
|
|
336
|
+
options,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function buildWorkflowPausedEvent(
|
|
341
|
+
input: Omit<WorkflowPausedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
342
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
343
|
+
): WorkflowPausedEvent {
|
|
344
|
+
return buildAgentRuntimeEvent(
|
|
345
|
+
{
|
|
346
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.WORKFLOW_PAUSED,
|
|
347
|
+
...input,
|
|
348
|
+
},
|
|
349
|
+
options,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function buildScheduledWakeupSetEvent(
|
|
354
|
+
input: Omit<ScheduledWakeupSetEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
355
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
356
|
+
): ScheduledWakeupSetEvent {
|
|
357
|
+
return buildAgentRuntimeEvent(
|
|
358
|
+
{
|
|
359
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.SCHEDULED_WAKEUP_SET,
|
|
360
|
+
...input,
|
|
361
|
+
},
|
|
362
|
+
options,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function buildBudgetThresholdEvent(
|
|
367
|
+
input: Omit<BudgetThresholdEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
368
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
369
|
+
): BudgetThresholdEvent {
|
|
370
|
+
return buildAgentRuntimeEvent(
|
|
371
|
+
{
|
|
372
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.BUDGET_THRESHOLD,
|
|
373
|
+
...input,
|
|
374
|
+
},
|
|
375
|
+
options,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function buildAgentSessionEnrolledEvent(
|
|
380
|
+
input: Omit<AgentSessionEnrolledEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
381
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
382
|
+
): AgentSessionEnrolledEvent {
|
|
383
|
+
return buildAgentRuntimeEvent(
|
|
384
|
+
{
|
|
385
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENROLLED,
|
|
386
|
+
...input,
|
|
387
|
+
},
|
|
388
|
+
options,
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export function buildAgentSessionEndedEvent(
|
|
393
|
+
input: Omit<AgentSessionEndedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
394
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
395
|
+
): AgentSessionEndedEvent {
|
|
396
|
+
return buildAgentRuntimeEvent(
|
|
397
|
+
{
|
|
398
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_ENDED,
|
|
399
|
+
...input,
|
|
400
|
+
},
|
|
401
|
+
options,
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export function buildAgentSessionStalledEvent(
|
|
406
|
+
input: Omit<AgentSessionStalledEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
407
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
408
|
+
): AgentSessionStalledEvent {
|
|
409
|
+
return buildAgentRuntimeEvent(
|
|
410
|
+
{
|
|
411
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.AGENT_SESSION_STALLED,
|
|
412
|
+
...input,
|
|
413
|
+
},
|
|
414
|
+
options,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export function buildAutonomyChangedEvent(
|
|
419
|
+
input: Omit<AutonomyChangedEvent, keyof AgentRuntimeEventEnvelope | 'kind'>,
|
|
420
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
421
|
+
): AutonomyChangedEvent {
|
|
422
|
+
return buildAgentRuntimeEvent(
|
|
423
|
+
{
|
|
424
|
+
kind: AGENT_RUNTIME_EVENT_KINDS.AUTONOMY_CHANGED,
|
|
425
|
+
...input,
|
|
426
|
+
},
|
|
427
|
+
options,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export function emitAgentRuntimeEvent(
|
|
432
|
+
sink: AgentRuntimeEventSink | undefined,
|
|
433
|
+
event: AgentRuntimeEvent,
|
|
434
|
+
): void {
|
|
435
|
+
if (!sink) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
sink(event);
|
|
440
|
+
} catch {
|
|
441
|
+
// Ephemeral telemetry must never break the host/session workflow.
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const channelSeqCounters = new Map<string, number>();
|
|
446
|
+
|
|
447
|
+
function buildAgentRuntimeEvent<T extends { kind: AgentRuntimeEventKind }>(
|
|
448
|
+
payload: T,
|
|
449
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
450
|
+
): T & AgentRuntimeEventEnvelope {
|
|
451
|
+
const timestamp = options?.timestamp ?? new Date().toISOString();
|
|
452
|
+
const payloadRecord = payload as unknown as Record<string, unknown>;
|
|
453
|
+
return {
|
|
454
|
+
...payload,
|
|
455
|
+
schema_version: SCHEMA_VERSION_V2,
|
|
456
|
+
timestamp,
|
|
457
|
+
event_id: resolveEventId(payload.kind, payloadRecord, timestamp, options),
|
|
458
|
+
channel_id: AGENT_RUNTIME_CHANNEL_ID,
|
|
459
|
+
seq: nextSeq(AGENT_RUNTIME_CHANNEL_ID),
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function resolveEventId(
|
|
464
|
+
kind: string,
|
|
465
|
+
payload: Record<string, unknown>,
|
|
466
|
+
timestamp: string,
|
|
467
|
+
options?: AgentRuntimeEventBuildOptions,
|
|
468
|
+
): string {
|
|
469
|
+
const key = options?.idempotencyKey;
|
|
470
|
+
if (!key) {
|
|
471
|
+
return randomUUID();
|
|
472
|
+
}
|
|
473
|
+
const canonicalPayload = JSON.stringify(payload, Object.keys(payload).sort());
|
|
474
|
+
return createHash(CONTENT_HASH_ALGORITHM)
|
|
475
|
+
.update(`${key}|${kind}|${timestamp}|${canonicalPayload}`)
|
|
476
|
+
.digest(CONTENT_HASH_ENCODING)
|
|
477
|
+
.slice(0, CONTENT_HASH_PREFIX_LENGTH);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function nextSeq(channelId: string): number {
|
|
481
|
+
const current = channelSeqCounters.get(channelId) ?? 0;
|
|
482
|
+
const next = current + 1;
|
|
483
|
+
channelSeqCounters.set(channelId, next);
|
|
484
|
+
return next;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function resolveRecoveryAction(
|
|
488
|
+
cleanupStatus: AgentRuntimeCleanupStatus,
|
|
489
|
+
recoveryAction?: string | null,
|
|
490
|
+
): string | null {
|
|
491
|
+
if (cleanupStatus === 'clean') {
|
|
492
|
+
return null;
|
|
493
|
+
}
|
|
494
|
+
if (recoveryAction && recoveryAction.trim().length > 0) {
|
|
495
|
+
return recoveryAction.trim();
|
|
496
|
+
}
|
|
497
|
+
if (cleanupStatus === 'partial') {
|
|
498
|
+
return 'Review the partially applied state and re-run wu:prep before resuming.';
|
|
499
|
+
}
|
|
500
|
+
return 'Run wu:recover after reviewing the current session state.';
|
|
501
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Copyright (c) 2026 Hellmai Ltd
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WU-2731 (INIT-060 phase 3, ADR-013 §5 Identity):
|
|
6
|
+
* Pure inbound-channel ingress sanitizer.
|
|
7
|
+
*
|
|
8
|
+
* ADR-013 §5 rules, enforced here:
|
|
9
|
+
*
|
|
10
|
+
* - The authoritative `from` value for an inbound phone POST comes from the
|
|
11
|
+
* authenticated token subject (`{workspace_id}:phone:{device_id}`). Cloud
|
|
12
|
+
* MUST NOT trust a `from` field supplied in the request body.
|
|
13
|
+
*
|
|
14
|
+
* - Body-supplied `from` (and any alias under `metadata.from`) is stripped
|
|
15
|
+
* before the envelope reaches domain code. The surface that calls this
|
|
16
|
+
* module is expected to pass the resolved `from` separately; attempting to
|
|
17
|
+
* ingest a body-only envelope (no authoritative from) fails closed.
|
|
18
|
+
*
|
|
19
|
+
* The module is HTTP-independent: both the kernel tool-api surface and the
|
|
20
|
+
* future control-plane adapter (WU-2737) call the same sanitizer so the
|
|
21
|
+
* ignore-body-from rule is enforced in one place.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const ENVELOPE_BODY_FIELD_FROM = 'from' as const;
|
|
25
|
+
const ENVELOPE_METADATA_KEY = 'metadata' as const;
|
|
26
|
+
const ENVELOPE_FROM_SOURCE_TOKEN = 'token' as const;
|
|
27
|
+
|
|
28
|
+
export interface PhoneChannelIngressInput {
|
|
29
|
+
/**
|
|
30
|
+
* Raw request body as parsed from JSON. Opaque record; the sanitizer does
|
|
31
|
+
* not inspect `body`, only the `from` attribution fields.
|
|
32
|
+
*/
|
|
33
|
+
body: Readonly<Record<string, unknown>>;
|
|
34
|
+
/**
|
|
35
|
+
* Authoritative identity claim resolved from the bearer token. Required:
|
|
36
|
+
* without a token subject, cloud cannot attribute the message, and the
|
|
37
|
+
* ingress path rejects the request.
|
|
38
|
+
*/
|
|
39
|
+
authoritativeFrom: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface PhoneChannelIngressEnvelope {
|
|
43
|
+
/**
|
|
44
|
+
* Sanitised request body with any body-level `from` stripped. Everything
|
|
45
|
+
* else the caller supplied is preserved so downstream code (channel.send,
|
|
46
|
+
* routing) can consume arbitrary payload fields.
|
|
47
|
+
*/
|
|
48
|
+
body: Record<string, unknown>;
|
|
49
|
+
/** Authoritative attribution string (`{workspace_id}:phone:{device_id}`). */
|
|
50
|
+
from: string;
|
|
51
|
+
/** Always `'token'` — cloud-safe invariant per ADR-013 §5. */
|
|
52
|
+
from_source: typeof ENVELOPE_FROM_SOURCE_TOKEN;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class PhoneChannelIngressError extends Error {
|
|
56
|
+
readonly statusCode: number;
|
|
57
|
+
|
|
58
|
+
constructor(message: string, statusCode: number) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = 'PhoneChannelIngressError';
|
|
61
|
+
this.statusCode = statusCode;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Sanitize an inbound phone-channel request. Returns a cleaned envelope with
|
|
67
|
+
* the authoritative `from` baked in. Throws `PhoneChannelIngressError` (HTTP
|
|
68
|
+
* 400) when the authoritative `from` is missing — the surface translates the
|
|
69
|
+
* error to a 400/403 response.
|
|
70
|
+
*/
|
|
71
|
+
export function sanitizePhoneChannelIngress(
|
|
72
|
+
input: PhoneChannelIngressInput,
|
|
73
|
+
): PhoneChannelIngressEnvelope {
|
|
74
|
+
if (typeof input.authoritativeFrom !== 'string' || input.authoritativeFrom.length === 0) {
|
|
75
|
+
throw new PhoneChannelIngressError(
|
|
76
|
+
'Inbound phone channel requires authoritative from (token subject); body-only attribution is rejected.',
|
|
77
|
+
400,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const sanitizedBody = stripKey(input.body, ENVELOPE_BODY_FIELD_FROM);
|
|
82
|
+
const rawMetadata = sanitizedBody[ENVELOPE_METADATA_KEY];
|
|
83
|
+
if (
|
|
84
|
+
rawMetadata !== null &&
|
|
85
|
+
typeof rawMetadata === 'object' &&
|
|
86
|
+
!Array.isArray(rawMetadata) &&
|
|
87
|
+
ENVELOPE_BODY_FIELD_FROM in (rawMetadata as Record<string, unknown>)
|
|
88
|
+
) {
|
|
89
|
+
sanitizedBody[ENVELOPE_METADATA_KEY] = stripKey(
|
|
90
|
+
rawMetadata as Record<string, unknown>,
|
|
91
|
+
ENVELOPE_BODY_FIELD_FROM,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
body: sanitizedBody,
|
|
97
|
+
from: input.authoritativeFrom,
|
|
98
|
+
from_source: ENVELOPE_FROM_SOURCE_TOKEN,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Copy `record` omitting the given key. Implemented with a filtered
|
|
104
|
+
* `Object.entries` rather than `delete` to satisfy the linter's ban on
|
|
105
|
+
* dynamic-delete (which would otherwise be a no-op for literal keys but
|
|
106
|
+
* fails the rule uniformly).
|
|
107
|
+
*/
|
|
108
|
+
function stripKey(record: Readonly<Record<string, unknown>>, key: string): Record<string, unknown> {
|
|
109
|
+
const result: Record<string, unknown> = {};
|
|
110
|
+
for (const [entryKey, entryValue] of Object.entries(record)) {
|
|
111
|
+
if (entryKey !== key) {
|
|
112
|
+
result[entryKey] = entryValue;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Convenience: reports whether the given `from` subject matches the
|
|
120
|
+
* phone-device grammar (`{workspace_id}:phone:{device_id}`). Useful to
|
|
121
|
+
* tell workspace-scoped actors from phone-device actors in the audit log
|
|
122
|
+
* without reaching for the enrollment parser.
|
|
123
|
+
*/
|
|
124
|
+
export function isPhoneSubject(subject: string): boolean {
|
|
125
|
+
const parts = subject.split(':');
|
|
126
|
+
if (parts.length !== 3) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const [workspace, kind, device] = parts;
|
|
130
|
+
return (
|
|
131
|
+
kind === 'phone' &&
|
|
132
|
+
typeof workspace === 'string' &&
|
|
133
|
+
workspace.length > 0 &&
|
|
134
|
+
typeof device === 'string' &&
|
|
135
|
+
device.length > 0
|
|
136
|
+
);
|
|
137
|
+
}
|