|
| 1 | +const WIRE_SOURCES = Object.freeze({ |
| 2 | + activity : 'fleet:activity-adapters', |
| 3 | + repoStatus: 'fleet:fleetStatus', |
| 4 | + roster : 'fleet:listAgents', |
| 5 | + runtime : 'fleet:runtimeStatus', |
| 6 | + lifecycle : 'fleet:lifecycle' |
| 7 | + }) |
| 8 | + |
| 9 | +export const FLEET_COCKPIT_EVENT_TYPES = Object.freeze([ |
| 10 | + 'lifecycle-request', |
| 11 | + 'lifecycle-success', |
| 12 | + 'lifecycle-failure', |
| 13 | + 'bridge-unavailable', |
| 14 | + 'bridge-gated' |
| 15 | +]) |
| 16 | + |
| 17 | +/** |
| 18 | + * @summary Source labels for the Fleet Manager cockpit DTO. These labels are deliberately stable and |
| 19 | + * transport-agnostic: the Body-side cockpit can explain which live substrate produced each row or |
| 20 | + * event without importing the Node-only FleetControlBridge / registry / lifecycle service chain. |
| 21 | + * @type {Object} |
| 22 | + */ |
| 23 | +export const FLEET_COCKPIT_SOURCES = Object.freeze({...WIRE_SOURCES}) |
| 24 | + |
| 25 | +/** |
| 26 | + * @summary Build the first Fleet Manager cockpit snapshot from the already-shipped bridge reads: |
| 27 | + * `listAgents()` for the redacted roster and `fleetStatus()` for repo-provisioning state. Runtime |
| 28 | + * process truth and A2A/PR/lane activity are explicit capability slots, not guessed browser state; an |
| 29 | + * unwired adapter is rendered as `not-wired` so Lane 1 never mistakes placeholder data for fact. |
| 30 | + * |
| 31 | + * @param {Object} options={} |
| 32 | + * @param {Object[]} options.agents Public agent definitions from `registryBridge.listAgents()`. |
| 33 | + * @param {Object[]} options.fleetStatus Repo-status entries from `registryBridge.fleetStatus()`. |
| 34 | + * @param {Object[]} options.events Optional already-normalized cockpit events. |
| 35 | + * @returns {Object} serializable cockpit DTO `{sources, capabilities, rows, events}`. |
| 36 | + */ |
| 37 | +export function createFleetCockpitStatus({agents = [], fleetStatus = [], events = []} = {}) { |
| 38 | + const statusByAgentId = new Map( |
| 39 | + fleetStatus.map(status => [status.agentId || status.id, sanitizePayload(status)]) |
| 40 | + ) |
| 41 | + |
| 42 | + return { |
| 43 | + sources : FLEET_COCKPIT_SOURCES, |
| 44 | + capabilities: { |
| 45 | + activity: createNotWiredCapability(FLEET_COCKPIT_SOURCES.activity, 'A2A / PR / lane activity adapter not wired'), |
| 46 | + runtime : createNotWiredCapability(FLEET_COCKPIT_SOURCES.runtime, 'runtime process status is pending the Fleet runtime-status wire method') |
| 47 | + }, |
| 48 | + rows: agents.map(agent => { |
| 49 | + const publicAgent = sanitizePayload(agent), |
| 50 | + agentId = publicAgent.id, |
| 51 | + repoStatus = statusByAgentId.get(agentId) || null |
| 52 | + |
| 53 | + return { |
| 54 | + id : agentId, |
| 55 | + githubUsername: publicAgent.githubUsername ?? null, |
| 56 | + harnessType : publicAgent.harnessType ?? null, |
| 57 | + displayName : publicAgent.displayName ?? publicAgent.name ?? publicAgent.githubUsername ?? agentId ?? null, |
| 58 | + agent : publicAgent, |
| 59 | + repoStatus, |
| 60 | + lifecycle : { |
| 61 | + source : FLEET_COCKPIT_SOURCES.runtime, |
| 62 | + state : 'not-wired', |
| 63 | + confidence: 'none' |
| 64 | + }, |
| 65 | + sources: { |
| 66 | + roster: { |
| 67 | + source : FLEET_COCKPIT_SOURCES.roster, |
| 68 | + state : 'wired', |
| 69 | + confidence: 'observed' |
| 70 | + }, |
| 71 | + repoStatus: { |
| 72 | + source : FLEET_COCKPIT_SOURCES.repoStatus, |
| 73 | + state : repoStatus ? 'wired' : 'missing', |
| 74 | + confidence: repoStatus ? 'observed' : 'none' |
| 75 | + }, |
| 76 | + runtime: { |
| 77 | + source : FLEET_COCKPIT_SOURCES.runtime, |
| 78 | + state : 'not-wired', |
| 79 | + confidence: 'none' |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + }), |
| 84 | + events: events.map(event => createFleetCockpitEvent(event)) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @summary Normalize a bounded fleet cockpit activity event. The type set is intentionally narrow so |
| 90 | + * UI and whitebox tests can prove lifecycle request/success/failure and bridge unavailable/gated |
| 91 | + * states without inventing opaque free-form activity. |
| 92 | + * @param {Object} event |
| 93 | + * @returns {Object} |
| 94 | + */ |
| 95 | +export function createFleetCockpitEvent({type, source, agentId = null, confidence = 'observed', payload = null, occurredAt = null} = {}) { |
| 96 | + if (!FLEET_COCKPIT_EVENT_TYPES.includes(type)) { |
| 97 | + throw new Error(`createFleetCockpitEvent: unsupported event type '${type}'`) |
| 98 | + } |
| 99 | + if (!source) { |
| 100 | + throw new Error('createFleetCockpitEvent: source is required') |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + type, |
| 105 | + source, |
| 106 | + agentId, |
| 107 | + confidence, |
| 108 | + occurredAt, |
| 109 | + payload: sanitizePayload(payload) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @summary Capability placeholder for adapters that are planned but not wired. A missing adapter is a |
| 115 | + * first-class fact, not sample data. |
| 116 | + * @param {String} source |
| 117 | + * @param {String} reason |
| 118 | + * @returns {Object} |
| 119 | + */ |
| 120 | +export function createNotWiredCapability(source, reason) { |
| 121 | + return { |
| 122 | + source, |
| 123 | + state : 'not-wired', |
| 124 | + confidence: 'none', |
| 125 | + reason |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function sanitizePayload(value) { |
| 130 | + if (Array.isArray(value)) { |
| 131 | + return value.map(item => sanitizePayload(item)) |
| 132 | + } |
| 133 | + |
| 134 | + if (!value || typeof value !== 'object') { |
| 135 | + return value |
| 136 | + } |
| 137 | + |
| 138 | + return Object.fromEntries( |
| 139 | + Object.entries(value) |
| 140 | + .filter(([key]) => !isSecretKey(key)) |
| 141 | + .map(([key, item]) => [key, sanitizePayload(item)]) |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +function isSecretKey(key) { |
| 146 | + const normalized = key.toLowerCase().replace(/[_-]/g, '') |
| 147 | + |
| 148 | + return normalized === 'credential' || |
| 149 | + normalized.endsWith('credential') || |
| 150 | + normalized === 'pat' || |
| 151 | + normalized.endsWith('pat') || |
| 152 | + normalized.includes('token') || |
| 153 | + normalized.includes('secret') || |
| 154 | + normalized.includes('password') || |
| 155 | + normalized.includes('signingkey') || |
| 156 | + normalized.includes('privatekey') |
| 157 | +} |
0 commit comments