Compare commits

...
3 Commits
23 changed files with 971 additions and 35 deletions
+3 -1
View File
@@ -842,7 +842,9 @@ export default function App() {
laboratoryAnnotation.control
) : activeDefinition.kind === "observatory" ? (
<StatusBadge tone="neutral">Только наблюдение</StatusBadge>
) : activeDefinition.kind === "contour-health" ? null : activeDefinition.root === "system" ? (
) : activeDefinition.kind === "contour-health" ? (
<div ref={setWorkspaceHeaderToolsHost} />
) : activeDefinition.root === "system" ? (
<SystemWorkspaceSelector
value={activeDefinition.id}
onChange={openNavigationView}
@@ -1,10 +1,12 @@
import {fetchComputeContours,type ComputeContour} from './computeContours';
import {fetchWorkerTelemetry,type WorkerTelemetry} from './workerTelemetry';
import type {Vehicle} from '../fleet/useFleet';
import {workerConnectionStatus} from './workerConnectionStatus';
export interface CoreHealth {
ok:boolean; service:string; version:string;
plugin_runtimes:{ready:number;total:number};
components?:{recording_cache?:{status?:string}};
}
export interface ContourSnapshot {
core:CoreHealth|null; latency:number|null;
@@ -59,8 +61,10 @@ export function boardAvailable(vehicle:Vehicle):boolean{
export function contourSummary(snapshot:ContourSnapshot|null){
if(!snapshot)return {tone:'warning' as const,label:'Проверяем состояние',online:null,total:null};
const total=snapshot.workers!==null&&snapshot.vehicles!==null?1+snapshot.workers.length+snapshot.vehicles.length:null;
const online=Number(Boolean(snapshot.core?.ok))+(snapshot.workers??[]).filter(item=>workerAvailable(item.telemetry)).length+(snapshot.vehicles??[]).filter(boardAvailable).length;
const online=Number(snapshot.core!==null)+(snapshot.workers??[]).filter(item=>workerAvailable(item.telemetry)).length+(snapshot.vehicles??[]).filter(boardAvailable).length;
if(snapshot.errors.length)return {tone:'warning' as const,label:'Проверка неполная',online,total};
if(!snapshot.core?.ok)return {tone:'danger' as const,label:'Mission Core недоступен',online,total};
return {tone:online===total?'success' as const:'warning' as const,label:online===total?'Все узлы на связи':'Часть узлов не на связи',online,total};
if(!snapshot.core)return {tone:'danger' as const,label:'Mission Core не в сети',online,total};
if(!snapshot.core.ok)return {tone:'warning' as const,label:'Mission Core работает с ограничениями',online,total};
if(snapshot.workers?.some(item=>workerConnectionStatus(item.telemetry).state==='unknown'))return {tone:'warning' as const,label:'Состояние части узлов не подтверждено',online,total};
return {tone:online===total?'success' as const:'warning' as const,label:online===total?'Все узлы в сети':'Часть узлов не в сети',online,total};
}
@@ -0,0 +1,13 @@
import type {WorkerTelemetry} from './workerTelemetry';
// Receiver failure is not evidence that the remote host has shut down.
export function workerConnectionStatus(telemetry: WorkerTelemetry | null) {
const connection = telemetry?.connection;
if (!connection) return {state:'unknown', tone:'warning', label:'Не в сети', detail:'Состояние вычислителя пока не подтверждено.'} as const;
if (connection.reachable && !connection.identity_matches) return {state:'unknown', tone:'warning', label:'Не в сети', detail:'Ответивший узел не совпадает с выбранным вычислителем.'} as const;
if (connection.reachable && connection.identity_matches && telemetry.node) return {state:'online', tone:'success', label:'В сети', detail:null} as const;
if (['telemetry-receiver-unavailable','telemetry-agent-unavailable','invalid-telemetry-response'].includes(connection.error_code ?? '')) {
return {state:'unknown', tone:'warning', label:'Не в сети', detail:'Mission Core не получает данные через службу телеметрии. Состояние самого вычислителя не подтверждено.'} as const;
}
return {state:'unknown', tone:'warning', label:'Не в сети', detail:'Последние данные не подтверждают текущее состояние вычислителя.'} as const;
}
@@ -1,7 +1,9 @@
import {useEffect,useState} from 'react';
import {Button,Icon,StatusBadge} from '@nodedc/ui-react';
import {createPortal} from 'react-dom';
import {workerConnectionStatus} from '../core/system/workerConnectionStatus';
import {IconButton,Icon,StatusBadge} from '@nodedc/ui-react';
import type {MissionRuntimeState} from '../core/runtime/contracts';
import {boardAvailable,contourSummary,readContourSnapshot,workerAvailable,type ContourSnapshot} from '../core/system/contourHealth';
import {boardAvailable,contourSummary,readContourSnapshot,type ContourSnapshot} from '../core/system/contourHealth';
import {startSequentialPolling} from '../core/system/sequentialPolling';
export function contourRuntimeAuthorityPresentation(
@@ -28,7 +30,7 @@ function observed(value:number|string|null|undefined){
const date=new Date(value);
return Number.isFinite(date.getTime())?date.toLocaleString('ru-RU'):'—';
}
export function ContourHealthWorkspace({state}:{state:MissionRuntimeState|null}){
export function ContourHealthWorkspace({state,headerToolsHost}:{state:MissionRuntimeState|null;headerToolsHost?:HTMLElement|null}){
const [snapshot,setSnapshot]=useState<ContourSnapshot|null>(null);
const [loading,setLoading]=useState(true),[generation,setGeneration]=useState(0);
useEffect(()=>{
@@ -43,33 +45,36 @@ export function ContourHealthWorkspace({state}:{state:MissionRuntimeState|null})
const summary=contourSummary(snapshot);
const core=snapshot?.core;
const {controlledDevice,deviceControlConnectivity}=contourRuntimeAuthorityPresentation(state);
const refresh=<IconButton label={loading?"Проверяем состояние":"Обновить состояние контура"} disabled={loading} onClick={()=>setGeneration(value=>value+1)}><Icon name="refresh" size={16}/></IconButton>;
return <div className="contour-health-dashboard">
{headerToolsHost?createPortal(refresh,headerToolsHost):refresh}
<section className="contour-health-summary">
<div><h2>Узлы и связь</h2><p>Mission Core, зарегистрированные вычислители и бортовые компьютеры аппаратов.</p></div>
<div className="contour-health-summary__status"><StatusBadge tone={loading?'warning':summary.tone}>{loading?'Проверяем состояние':summary.label}</StatusBadge><Button size="compact" variant="secondary" disabled={loading} onClick={()=>setGeneration(value=>value+1)}><Icon name="refresh" size={14}/>{loading?'Проверяем':'Обновить'}</Button></div>
<div className="contour-health-summary__status"><StatusBadge tone={loading?'warning':summary.tone}>{loading?'Проверяем состояние':summary.label}</StatusBadge></div>
</section>
<section className="contour-health-kpis" aria-label="Сводка состояния контура">
<div><span>Узлы на связи</span><strong>{summary.online??'—'} / {summary.total??'—'}</strong><small>Core, вычислители и бортовые ПК</small></div>
<div><span>Узлы в сети</span><strong>{summary.online??'—'} / {summary.total??'—'}</strong><small>Core, вычислители и бортовые ПК</small></div>
<div><span>Вычислители</span><strong>{snapshot?.workers?.length??'—'}</strong><small>из настроек системы</small></div>
<div><span>Бортовые компьютеры</span><strong>{snapshot?.vehicles?.length??'—'}</strong><small>из реестра аппаратов</small></div>
<div><span>Последняя проверка</span><strong>{observed(snapshot?.observedAt)}</strong><small>обновление каждые 5 секунд</small></div>
</section>
<section className="contour-node-grid" aria-label="Узлы контура">
<article className="contour-node" data-state={core?.ok?'online':'offline'}>
<header><div><span className="section-eyebrow">КОМПЬЮТЕР ОПЕРАТОРА</span><h3>Mission Core</h3></div><StatusBadge tone={core?.ok?'success':core?'danger':'warning'}>{core?(core.ok?'Доступен':'Недоступен'):snapshot?'Нет свежих данных':'Проверяем'}</StatusBadge></header>
<article className="contour-node" data-state={core?(core.ok?'online':'degraded'):'unknown'}>
<header><div><span className="section-eyebrow">КОМПЬЮТЕР ОПЕРАТОРА</span><h3>Mission Core</h3></div><StatusBadge tone={core?.ok?'success':'warning'}>{core?'В сети':snapshot?'Не в сети':'Проверяем состояние'}</StatusBadge></header>
<dl><div><dt>Версия</dt><dd>{core?.version??'—'}</dd></div><div><dt>Время ответа</dt><dd>{snapshot?.latency==null?'—':`${Math.max(1,Math.round(snapshot.latency))} мс`}</dd></div><div><dt>Обработчики устройств готовы</dt><dd>{core?`${core.plugin_runtimes.ready} / ${core.plugin_runtimes.total}`:'—'}</dd></div></dl>
{!core?.ok&&core?.components?.recording_cache?.status==='capacity-pressure'&&<small>Недостаточно свободного места для новых записей.</small>}
</article>
{snapshot?.workers?.map(({contour,telemetry})=>{
const online=workerAvailable(telemetry);
const mismatch=telemetry?.connection.reachable&&!telemetry.connection.identity_matches;
return <article key={contour.contour_id} className="contour-node" data-state={online?'online':'offline'}>
<header><div><span className="section-eyebrow">ВЫЧИСЛИТЕЛЬ</span><h3>{contour.display_name}</h3></div><StatusBadge tone={online?'success':!telemetry||mismatch?'warning':'neutral'}>{online?'На связи':mismatch?'Другой узел':telemetry?'Нет свежих данных':'Проверка недоступна'}</StatusBadge></header>
const status=workerConnectionStatus(telemetry);
return <article key={contour.contour_id} className="contour-node" data-state={status.state}>
<header><div><span className="section-eyebrow">ВЫЧИСЛИТЕЛЬ</span><h3>{contour.display_name}</h3></div><StatusBadge tone={status.tone}>{status.label}</StatusBadge></header>
<dl><div><dt>Узел</dt><dd>{contour.expected_node_id}</dd></div><div><dt>Телеметрия</dt><dd>{contour.telemetry_mode==='agent-mqtt'?'Агент':'Диагностика SSH'}</dd></div><div><dt>Проверено</dt><dd>{observed(telemetry?.connection.observed_at_utc)}</dd></div><div><dt>Последнее измерение</dt><dd>{observed(telemetry?.node?.observed_at_utc)}</dd></div><div><dt>Операционная система</dt><dd>{telemetry?.node?.os.caption??'—'}</dd></div></dl>
{status.detail&&<small>{status.detail}</small>}
</article>;
})}
{snapshot?.vehicles?.map(vehicle=>{
const online=boardAvailable(vehicle);
const status=vehicle.enrollment==='revoked'?'Привязка отозвана':vehicle.enrollment==='pending'?'Подключение':vehicle.enrollment==='failed'?'Не подключён':online?'На связи':'Нет связи';
const status=vehicle.enrollment==='revoked'?'Привязка отозвана':vehicle.enrollment==='pending'?'Подключение':vehicle.enrollment==='failed'?'Не подключён':online?'В сети':'Не в сети';
return <article key={vehicle.id} className="contour-node" data-state={online?'online':'offline'}>
<header><div><span className="section-eyebrow">БОРТОВОЙ КОМПЬЮТЕР</span><h3>{vehicle.name}</h3></div><StatusBadge tone={online?'success':'neutral'}>{status}</StatusBadge></header>
<dl><div><dt>Имя БК</dt><dd>{vehicle.host?.hostname??'—'}</dd></div><div><dt>Операционная система</dt><dd>{vehicle.host?.os??'—'}</dd></div><div><dt>Последняя связь</dt><dd>{observed(vehicle.last_seen===null?null:vehicle.last_seen*1000)}</dd></div></dl>
@@ -437,6 +437,7 @@ export function WorkspaceRenderer(props: WorkspaceRendererProps) {
case "contour-health":
return (
<ContourHealthWorkspace
headerToolsHost={props.headerToolsHost}
state={props.state}
/>
);
@@ -1,7 +1,7 @@
import {Button,Icon,IconButton,ResourceRow,StatusBadge,Window} from '@nodedc/ui-react';
import type {Vehicle} from '../../core/fleet/useFleet';
export const boardStatus=(item:Vehicle,available=true)=>item.enrollment==='pending'?'Подтверждаем привязку':item.enrollment==='revoked'?'Не привязан':item.enrollment==='failed'?'Привязка не завершена':item.connectivity==='online'?(available?'В сети':'Нет свежих данных'):'Нет связи';
export const boardStatus=(item:Vehicle,available=true)=>item.enrollment==='pending'?'Подтверждаем привязку':item.enrollment==='revoked'?'Не привязан':item.enrollment==='failed'?'Привязка не завершена':item.connectivity==='online'?(available?'В сети':'Не в сети'):'Не в сети';
export function BoardComputerRow({vehicle,enabled,showingSavedDevices,onSettings,onMonitor,onAdd,open,onToggle}:{vehicle:Vehicle;enabled:boolean;showingSavedDevices:boolean;onSettings:()=>void;onMonitor:()=>void;onAdd?:()=>void;open:boolean;onToggle:()=>void}){
const label=boardStatus(vehicle,enabled);
return <ResourceRow icon={<Icon name="apps"/>} title="Бортовой компьютер" description={vehicle.host?.hostname||'Mission Core Node'} metadata={`${label}${showingSavedDevices?' (показаны последние сохранённые устройства)':''}`} statusPlacement="leading"
@@ -1,3 +1,4 @@
import {workerConnectionStatus} from '../../core/system/workerConnectionStatus';
import {
GlassSurface,
StatusBadge,
@@ -77,9 +78,7 @@ export function ComputeModulesWorkspace() {
const node = telemetry?.node ?? null;
const missionCoreRuntimes = telemetry?.runtimes.filter((runtime) => !runtime.external) ?? [];
const externalRuntimes = telemetry?.runtimes.filter((runtime) => runtime.external) ?? [];
const connected = Boolean(
telemetry?.connection.reachable && telemetry.connection.identity_matches && node,
);
const connectionStatus = workerConnectionStatus(telemetry);
const agentTelemetry = telemetry?.connection.source === "agent-mqtt";
const history = telemetry?.history ?? [];
const cpuPercent = node?.cpu.load_percent;
@@ -105,14 +104,13 @@ export function ComputeModulesWorkspace() {
</p>
</div>
<div className="system-workspace__actions">
<StatusBadge tone={connected ? "success" : "danger"}>
{connected
? agentTelemetry ? "Агент доступен" : "SSH-диагностика"
: "Нет свежих данных"}
<StatusBadge tone={connectionStatus.tone}>
{connectionStatus.label}
</StatusBadge>
</div>
</section>
{connectionStatus.detail && <p role="status">{connectionStatus.detail}</p>}
{error ? (
<GlassSurface className="system-workspace__notice" padding="md" tone="soft">
<StatusBadge tone="warning">{error}</StatusBadge>
@@ -1,3 +1,4 @@
import {workerConnectionStatus} from '../../core/system/workerConnectionStatus';
import {
GlassSurface,
StatusBadge,
@@ -31,6 +32,7 @@ export function NetworkWorkspace() {
const connected = Boolean(
telemetry?.connection.reachable && telemetry.connection.identity_matches,
);
const connectionStatus = workerConnectionStatus(telemetry);
const agentTelemetry = telemetry?.connection.source === "agent-mqtt";
const runtimeOnline = telemetry?.runtimes.some(
(runtime) => !runtime.external && runtime.state === "running",
@@ -43,17 +45,18 @@ export function NetworkWorkspace() {
<span className="section-eyebrow">СИСТЕМА / СЕТЬ</span>
<h2>{selectedContour?.display_name ?? "Контур не выбран"}</h2>
<p>
Фактический локальный маршрут и сетевые счётчики выбранного вычислительного
Фактический маршрут и сетевые счётчики выбранного вычислительного
контура. Адрес, транспорт и установка агента находятся в настройках контура.
</p>
</div>
<div className="system-workspace__actions">
<StatusBadge tone={connected ? "success" : "danger"}>
{connected ? "Маршрут доступен" : "Нет свежих данных"}
<StatusBadge tone={connectionStatus.tone}>
{connectionStatus.label}
</StatusBadge>
</div>
</section>
{connectionStatus.detail && <p role="status">{connectionStatus.detail}</p>}
{error ? (
<GlassSurface className="system-workspace__notice" padding="md" tone="soft">
<StatusBadge tone="warning">{error}</StatusBadge>
@@ -109,7 +112,7 @@ export function NetworkWorkspace() {
<small>127.0.0.1:8000</small>
</div>
<i aria-hidden="true" data-state="online" />
<div data-state={agentTelemetry ? "online" : "offline"}>
<div data-state={connected ? "online" : "unknown"}>
<span>Telemetry plane</span>
<strong>{agentTelemetry ? "Mosquitto + Timescale" : "SSH diagnostic"}</strong>
<small>
@@ -33,7 +33,7 @@ test('actual registries determine node count and offline boards prevent all-onli
const calls=installFetch(t);
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(snapshot.workers[0].contour.display_name,'Test compute');
assert.deepEqual(contourSummary(snapshot),{tone:'warning',label:'Часть узлов не на связи',online:2,total:3});
assert.deepEqual(contourSummary(snapshot),{tone:'warning',label:'Часть узлов не в сети',online:2,total:3});
assert.equal(calls.length,4);
});
test('failed checks discard previous online data and distinguish unknown from empty',async t=>{
@@ -54,5 +54,24 @@ test('invalid health never becomes reachable; identity mismatch and stale worker
});
test('an empty configured contour contains only the operator, with no fabricated worker',async t=>{
installFetch(t,{data:{'/api/v1/system/contours':{schema_version:'missioncore.compute-contour-catalog/v1',contours:[]},'/api/v1/fleet':{items:[]}}});
assert.deepEqual(contourSummary(await readContourSnapshot(new AbortController().signal)),{tone:'success',label:'Все узлы на связи',online:1,total:1});
assert.deepEqual(contourSummary(await readContourSnapshot(new AbortController().signal)),{tone:'success',label:'Все узлы в сети',online:1,total:1});
});
test('receiver failure leaves worker unknown instead of declaring it offline',async t=>{
installFetch(t,{data:{'/api/v1/system/contours/test-compute/telemetry?history=90':{...worker,node:null,connection:{reachable:false,identity_matches:false,error_code:'telemetry-receiver-unavailable'}}}});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(contourSummary(snapshot).label,'Состояние части узлов не подтверждено');
const {workerConnectionStatus}=await server.ssrLoadModule('/src/core/system/workerConnectionStatus.ts');
const status=workerConnectionStatus(snapshot.workers[0].telemetry);
assert.equal(status.state,'unknown');
assert.equal(status.label,'Не в сети');
assert.equal(workerConnectionStatus({...worker,node:null,connection:{reachable:false,error_code:'telemetry-agent-stale'}}).label,'Не в сети');
assert.equal(workerConnectionStatus(worker).state,'online');
});
test('a responding Core with recording capacity pressure remains reachable',async t=>{
installFetch(t,{data:{'/api/health':{...core,ok:false,components:{recording_cache:{status:'capacity-pressure'}}}}});
const snapshot=await readContourSnapshot(new AbortController().signal);
assert.equal(contourSummary(snapshot).online,2);
assert.equal(contourSummary(snapshot).label,'Mission Core работает с ограничениями');
});
+28 -3
View File
@@ -149,9 +149,11 @@ globally unique because Mosquitto ACL ownership is username-based.
## Worker agent
MQTT outputs use `startup_error_behavior="retry"`, with a 2000-metric buffer and
the configured flush interval. A missing broker at agent startup must not end
the service. See [Telegraf's startup policy](https://docs.influxdata.com/telegraf/v1/configuration/plugin-options/).
MQTT outputs request `startup_error_behavior="retry"`, with a 2000-metric buffer
and the configured flush interval. This setting alone does not guarantee
recovery: Telegraf 1.38.4 can exit cleanly when MQTT is unavailable at startup.
The Tailscale profile below therefore also installs a system recovery task.
See [Telegraf's startup policy](https://docs.influxdata.com/telegraf/v1/configuration/plugin-options/).
If Docker lost a published listener while the saved LAN address is unchanged,
the explicit broker Apply action reconciles that listener; a telemetry GET never
restarts infrastructure. Node connectivity does not prove a profile is ready.
@@ -228,3 +230,26 @@ changing the K1 command sequence.
The stack and agent are intentionally not started by repository tests. Provisioning a
machine is a separate, explicit operation.
## Tailscale operator profile and startup recovery
The accepted 2026-09-25 operator profile no longer depends on a shared LAN or
DHCP address. A prepared Windows agent uses `127.0.0.1:1883`; the existing strict
Tailscale SSH identity carries a loopback reverse forward to the operator
broker. Operator deployment is owned by `scripts/manage_telemetry_startup.py`
(plan, hash-bound apply, rollback). This is a macOS **login** profile, not a
pre-login Docker daemon. Preserve the explicit prepared stack root and its
private credentials when moving the Core source checkout.
For this profile the Windows install/update bundle includes and invokes
`Install-NdcMissionCoreTelemetryRecovery.ps1`: boot + once-per-minute SYSTEM
reconciliation for a stopped Telegraf service, independent of login. Its
maintenance/rollback boundary is described in the audit. Do not rely solely on
SCM failure actions or the MQTT `startup_error_behavior` setting: the pinned
1.38.4 release was observed to terminate with a clean service exit when the
broker was unavailable during boot.
See [the measured recovery audit](../../docs/audits/2026-09-25-worker-telemetry-recovery.md)
for exact acceptance, remaining cold-boot/clean-host gates and bounded test
entrypoints. The earlier LAN workflow remains a legacy explicit configuration;
it is not the current Worker 006 transport.
+1
View File
@@ -29,6 +29,7 @@ SAFE_IDENTIFIER: Final = re.compile(
)
SAFE_INTERVAL: Final = re.compile(r"^[1-9][0-9]{0,2}s$")
WINDOWS_BUNDLE_FILES: Final = (
"Install-NdcMissionCoreTelemetryRecovery.ps1",
"Get-NdcMissionCorePipelineTelemetry.ps1",
"Install-NdcMissionCoreTelegraf.ps1",
"Update-NdcMissionCoreTelegraf.ps1",
@@ -33,6 +33,10 @@ foreach ($name in @(
Set-Item -Path "Env:$name" -Value ([string]$value)
}
$recoveryInstaller = Join-Path $PSScriptRoot 'Install-NdcMissionCoreTelemetryRecovery.ps1'
if ($payload.MISSIONCORE_MQTT_HOST -eq '127.0.0.1' -and -not (Test-Path $recoveryInstaller -PathType Leaf)) {
throw 'Telemetry recovery installer missing from bundle'
}
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
throw "Service '$serviceName' already exists; refusing an implicit replacement"
}
@@ -137,6 +141,10 @@ try {
if ($LASTEXITCODE -ne 0) {
throw "Failed to enable Telegraf recovery for non-crash failures"
}
$recoveryTask = $null
if ($payload.MISSIONCORE_MQTT_HOST -eq '127.0.0.1') {
$recoveryTask = & $recoveryInstaller -ExpectedNodeId $env:COMPUTERNAME -Action Apply | ConvertFrom-Json
}
Start-Service -Name $serviceName
$service = Get-Service -Name $serviceName
$service.WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running, [TimeSpan]::FromSeconds(20))
@@ -150,6 +158,7 @@ try {
Status = $service.Status.ToString()
StartType = $service.StartType.ToString()
RecoveryConfigured = $true
RecoveryTask = $recoveryTask
Configuration = $configurationPath
PipelineCollector = $collectorPath
PipelineJournal = $PipelineJournal
@@ -0,0 +1,87 @@
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][string]$ExpectedNodeId,
[ValidateSet('Plan','Apply','Rollback')][string]$Action = 'Plan',
[string]$BackupDirectory
)
# Telegraf 1.38.4 can report MQTT startup failure as a clean Windows-service exit.
# SCM failure actions alone cannot recover that stopped service.
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
if ($env:COMPUTERNAME -cne $ExpectedNodeId) { throw 'Worker identity mismatch' }
$taskName = 'ndc-mission-core-telemetry-recovery'
$root = Join-Path $env:ProgramFiles 'NDC\Mission Core\TelemetryRecovery'
$guardPath = Join-Path $root 'Resume-Telemetry.ps1'
$service = Get-CimInstance Win32_Service -Filter "Name='telegraf'"
if (-not $service -or $service.PathName -notlike '*NDC\Mission Core\Telegraf\telegraf.exe*') { throw 'Managed Telegraf service required' }
$guard = @'
$ErrorActionPreference = 'Stop'
$service = Get-Service -Name 'telegraf'
if ($service.Status -eq 'Running') { exit 0 }
if ($service.Status -ne 'Stopped') { exit 0 }
$values = @{}
foreach ($entry in (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\telegraf').Environment) {
$name, $value = $entry -split '=', 2
if ($name -in @('MISSIONCORE_MQTT_HOST','MISSIONCORE_MQTT_PORT')) { $values[$name] = $value }
}
if ($values['MISSIONCORE_MQTT_HOST'] -ne '127.0.0.1' -or $values['MISSIONCORE_MQTT_PORT'] -ne '1883') { exit 1 }
$client = [Net.Sockets.TcpClient]::new()
try {
$connect = $client.ConnectAsync('127.0.0.1', 1883)
if (-not $connect.Wait(3000) -or -not $client.Connected) { exit 0 }
} catch { exit 0 } finally { $client.Dispose() }
Start-Service -Name 'telegraf'
(Get-Service -Name 'telegraf').WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running, [TimeSpan]::FromSeconds(15))
'@
if ($Action -eq 'Plan') {
[ordered]@{node=$env:COMPUTERNAME;task=$taskName;path=$guardPath;startMode=$service.StartMode;triggers=@('boot','every-minute');scope='Start stopped telemetry service only';requiresInteractiveLogin=$false}|ConvertTo-Json -Compress
exit 0
}
if ($Action -eq 'Rollback') {
if (-not $BackupDirectory) { throw 'BackupDirectory required' }
$receipt = Get-Content (Join-Path $BackupDirectory 'receipt.json') -Raw | ConvertFrom-Json
if ($receipt.node -cne $ExpectedNodeId -or $receipt.task -ne $taskName) { throw 'Backup identity mismatch' }
if ((Get-FileHash $guardPath -Algorithm SHA256).Hash -ne $receipt.installedHash) { throw 'Installed recovery script changed' }
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
if (Test-Path (Join-Path $BackupDirectory 'task.xml')) {
Register-ScheduledTask -TaskName $taskName -Xml (Get-Content (Join-Path $BackupDirectory 'task.xml') -Raw) -Force | Out-Null
}
if (Test-Path (Join-Path $BackupDirectory 'guard.ps1')) {
Copy-Item (Join-Path $BackupDirectory 'guard.ps1') $guardPath -Force
} else { Remove-Item $guardPath }
[ordered]@{restored=$true;task=$taskName}|ConvertTo-Json -Compress
exit 0
}
$principal = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Administrative installer required' }
if (Test-Path $root) {
if ((Get-Item $root -Force).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'Recovery directory must not be a reparse point' }
}
New-Item -ItemType Directory -Path $root -Force | Out-Null
# SYSTEM executes this file: only SYSTEM and Administrators may modify it.
& icacls.exe $root /inheritance:r /grant:r '*S-1-5-18:(OI)(CI)F' '*S-1-5-32-544:(OI)(CI)F' | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Recovery directory ACL failed' }
$backup = Join-Path $root ('backups\' + [Guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $backup -Force | Out-Null
$previousTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($previousTask) { Export-ScheduledTask -TaskName $taskName | Set-Content (Join-Path $backup 'task.xml') -Encoding UTF8 }
if (Test-Path $guardPath) { Copy-Item $guardPath (Join-Path $backup 'guard.ps1') }
try {
[IO.File]::WriteAllText($guardPath, $guard, [Text.UTF8Encoding]::new($false))
$exe = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'
$run = New-ScheduledTaskAction -Execute $exe -Argument ('-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "' + $guardPath + '"')
$triggers = @((New-ScheduledTaskTrigger -AtStartup), (New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 1)))
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew -ExecutionTimeLimit (New-TimeSpan -Seconds 45) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$identity = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -Action $run -Trigger $triggers -Settings $settings -Principal $identity -Force | Out-Null
$receipt = [ordered]@{node=$env:COMPUTERNAME;task=$taskName;installedHash=(Get-FileHash $guardPath -Algorithm SHA256).Hash;backup=$backup}
$receipt|ConvertTo-Json|Set-Content (Join-Path $backup 'receipt.json') -Encoding UTF8
Start-ScheduledTask -TaskName $taskName
$receipt|ConvertTo-Json -Compress
} catch {
if ($previousTask) { Register-ScheduledTask -TaskName $taskName -Xml (Get-Content (Join-Path $backup 'task.xml') -Raw) -Force | Out-Null }
else { Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue }
if (Test-Path (Join-Path $backup 'guard.ps1')) { Copy-Item (Join-Path $backup 'guard.ps1') $guardPath -Force }
else { Remove-Item $guardPath -ErrorAction SilentlyContinue }
throw
}
@@ -0,0 +1,26 @@
[CmdletBinding()]
param([Parameter(Mandatory=$true)][string]$ExpectedNodeId)
$ErrorActionPreference='Stop'
$ProgressPreference='SilentlyContinue'
if ($env:COMPUTERNAME -cne $ExpectedNodeId) { throw 'Worker identity mismatch' }
$task=Get-ScheduledTask -TaskName 'ndc-mission-core-telemetry-recovery'
$account=[Security.Principal.NTAccount]::new($task.Principal.UserId)
$sid=$account.Translate([Security.Principal.SecurityIdentifier]).Value
if (-not $task.Settings.Enabled -or $sid -ne 'S-1-5-18') { throw 'Recovery task not enabled as SYSTEM' }
$before=Get-CimInstance Win32_Service -Filter "Name='telegraf'"
if ($before.State -ne 'Running') { throw 'A running baseline is required' }
$started=[DateTime]::UtcNow
Stop-Service telegraf
(Get-Service telegraf).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped,[TimeSpan]::FromSeconds(20))
$automatic=$false
try {
while (([DateTime]::UtcNow-$started).TotalSeconds -lt 80) {
Start-Sleep -Seconds 2
$after=Get-CimInstance Win32_Service -Filter "Name='telegraf'"
if ($after.State -eq 'Running' -and $after.ProcessId -ne $before.ProcessId) { $automatic=$true;break }
}
[ordered]@{node=$ExpectedNodeId;test='clean-service-stop';automaticRecovery=$automatic;elapsedSeconds=[Math]::Round(([DateTime]::UtcNow-$started).TotalSeconds,2);previousPid=$before.ProcessId;newPid=$after.ProcessId;state=$after.State}|ConvertTo-Json -Compress
if (-not $automatic) { throw 'Automatic recovery failed; restoring baseline' }
} finally {
if ((Get-Service telegraf).Status -ne 'Running') { Start-Service telegraf }
}
@@ -67,6 +67,10 @@ if (-not ($serviceEnvironmentCandidate | Where-Object {
)
}
$recoveryInstaller = Join-Path $PSScriptRoot 'Install-NdcMissionCoreTelemetryRecovery.ps1'
if ($env:MISSIONCORE_MQTT_HOST -eq '127.0.0.1' -and -not (Test-Path $recoveryInstaller -PathType Leaf)) {
throw 'Telemetry recovery installer missing from bundle'
}
$temporaryRoot = Join-Path $env:TEMP "ndc-mission-core-telegraf-update-$([Guid]::NewGuid().ToString('N'))"
$validationOutput = Join-Path $temporaryRoot "validation.out.log"
$validationError = Join-Path $temporaryRoot "validation.error.log"
@@ -127,6 +131,10 @@ try {
if ($LASTEXITCODE -ne 0) {
throw "Failed to enable Telegraf recovery for non-crash failures"
}
$recoveryTask = $null
if ($env:MISSIONCORE_MQTT_HOST -eq '127.0.0.1') {
$recoveryTask = & $recoveryInstaller -ExpectedNodeId $env:COMPUTERNAME -Action Apply | ConvertFrom-Json
}
}
catch {
Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue
@@ -155,6 +163,7 @@ try {
ServiceName = $serviceName
Status = (Get-Service -Name $serviceName).Status.ToString()
RecoveryConfigured = $true
RecoveryTask = $recoveryTask
Configuration = $configurationPath
Backup = $backupPath
PipelineCollector = $collectorPath
@@ -0,0 +1,124 @@
# Worker telemetry recovery, 2026-09-25
## Failure and evidence
Worker 006 is a remote compute host, not a device required to share the
operator's LAN. The owner explicitly requires Tailscale access across networks.
At the initial inspection the Worker was disconnected from its network; after
Wi-Fi returned the existing strict-pinned Tailscale SSH profile connected.
Read-only audit then established:
- Tailscale and OpenSSH were Running/Automatic; Tailscale ForceDaemon was true.
- Compute Docker containers were running. A failed historical StartCompute task
did not mean the entire Worker or every container was offline.
- Native Telegraf 1.38.4 was Stopped/Automatic, exit code zero. SCM restart actions
and non-crash recovery were already configured.
- Windows Application events on September 19, 21 and 23 showed Telegraf starting,
then terminating about 15 seconds later because its old LAN MQTT destination
was unreachable. Source inspection of the pinned upstream release confirms
that output retry handles only errors marked retryable; merely configuring
`startup_error_behavior=retry` did not cover the observed MQTT failure.
- Operator Docker was stopped, with login autostart disabled. No broker/query
listeners existed. Its saved broker bind belonged to an older DHCP lease.
- Private telemetry configuration remained in the original installation while
Core's network-apply handler assumed its current source checkout owned it.
No credentials, database volumes, Worker computation profiles or inference jobs
were replaced. Docker Desktop restored its existing unrelated restart-policy
containers when its engine started; those were not renamed or reconfigured.
## Implemented installation profile
Windows native Telegraf publishes to Worker `127.0.0.1:1883`. A dedicated
Mac-owned SSH reverse forward exposes that loopback listener and carries data
through the existing strictly pinned Tailscale SSH profile to operator
`127.0.0.1:1883`. Broker publication is loopback-only. Query remains
`127.0.0.1:18030`; Core remains `127.0.0.1:8000`. No `.local` name or LAN lease is
required by this transport. MQTT credentials and per-agent ACLs are preserved.
SSH supplies authenticated encryption; this does not claim public MQTT/mTLS
or multi-tenant enrollment acceptance.
`scripts/manage_telemetry_startup.py` owns a hash-bound plan/apply/rollback:
- preserved prepared stack directory, credentials and named volumes;
- explicit `MISSIONCORE_TELEMETRY_PLANE_ROOT` for the existing Core handler;
- `com.nodedc.telemetry-startup.local`: login startup and bounded reconciliation
every 30 seconds, including delayed Docker Desktop availability;
- `com.nodedc.telemetry-tunnel.local`: keepalive/reconnect and loopback-only
reverse forwarding, strict known-host verification;
- only broker, Timescale and normalizer are selected by Compose, no build or
image pull; existing database-bootstrap is an idempotent dependency;
- configuration backups outside Git; apply preserves canonical Core health;
- rollback restores declarations and credentials; it does not reset volumes or
stop Docker/unrelated containers. The restored broker declaration is applied
on subsequent reconciliation. Rollback rehearsal is not yet accepted.
`Install-NdcMissionCoreTelemetryRecovery.ps1` is included in the Windows agent
bundle. Loopback agent install/update invokes it. It installs one SYSTEM task
at boot and every minute, with no interactive-login requirement. Its script is
writable only by SYSTEM/Administrators. It starts only a stopped managed
Telegraf service, and only after the configured loopback endpoint is reachable.
A missing connection leaves the task retryable. It never restarts a healthy
agent or starts inference. Maintenance can disable this named task before an
intentional extended Telegraf stop; rollback restores its predecessor.
## Product status and refresh
Receiver failure now has `telemetry-receiver-unavailable`, distinct from stale
agent observations. Fleet contour, compute and network views show receiver
failure as an unconfirmed Worker state, not proof that its host is off. Visible
connectivity labels are consistently «В сети» / «Не в сети»; failure details
explain which part of the observation path failed. The
network view no longer paints the receiver green merely because the configured
source is agent-mqtt.
A responding Core with `recording_cache=capacity-pressure` is reachable with a
recording limitation. It is displayed as «В сети» with the limitation separately. This matters on the
operator host, where free space fell below the existing 2 GiB recording reserve
(about 1.8 GiB observed); no owner data was deleted.
The contour Refresh action now uses the canonical circular IconButton in the
outer window header, alongside expand/close. The body copy button is removed.
## Measured acceptance
- Fresh agent-mqtt data reached the canonical Core, with expected node identity.
- Native Windows service clean-stop test: automatic recovery in 51.7 s, new PID;
no manual Start-Service was needed (failure restoration path was not used).
- Dedicated tunnel SIGKILL: new tunnel process and new source observations in
10.43 s. The API's 30 s freshness window did not expire during this short cut.
- Normalizer stop: receiver-unavailable was observed, then automatic Compose
recovery and new source observations in 19.58 s.
- Tests are reproducible with `Test-NdcMissionCoreTelemetryRecovery.ps1` and
`scripts/check_telemetry_recovery.py`; they target telemetry only.
- 36 focused backend tests, Ruff, 984 frontend tests, TypeScript and production
build passed. Four installer/test PowerShell artifacts were parsed by the
native Windows parser; install/update hooks were not a clean-host rehearsal.
- Browser acceptance on canonical port 8000 confirms fresh Worker observations,
standardized connectivity labels and the circular Refresh control in the
window header (including its checking state).
## Qualification boundary
The accepted live profile is macOS operator session + Windows native telemetry
agent + the existing Tailscale trust relationship. Automatic recovery of the
three injected failures is proved. A full physical host power cycle, distinct
physical networks, extended network-loss soak and clean-host reinstall have
not been run in this increment. Tailscale may choose a direct encrypted path
when the peers happen to share a LAN; that does not reintroduce LAN addressing.
Mac LaunchAgents and Docker Desktop start after operator login, not before
macOS login. Windows Tailscale/SSH/telemetry recovery use system services/tasks.
Worker Docker computation still has its separate interactive-session lifecycle;
this work does not claim all GPU jobs should auto-resume after power loss.
Linux agent lifecycle and an unattended headless compute installation need a
separate qualified OS profile. Do not label the whole system universally
portable or cold-boot accepted from these component-level tests.
## Upstream references
- [Telegraf 1.38.4 output lifecycle](https://github.com/influxdata/telegraf/blob/v1.38.4/models/running_output.go)
- [Telegraf 1.38.4 MQTT connect](https://github.com/influxdata/telegraf/blob/v1.38.4/plugins/outputs/mqtt/mqtt.go)
- [Windows Tailscale unattended](https://tailscale.com/docs/how-to/run-unattended)
- [Docker Desktop login startup](https://docs.docker.com/desktop/settings-and-maintenance/settings/)
+99
View File
@@ -0,0 +1,99 @@
"""Bounded operator acceptance of installed telemetry recovery, no inference jobs.
Each case injects one failure in an exact Mission Core telemetry component.
An existing service's automatic supervisor must restore fresh worker telemetry.
"""
import argparse
import json
import os
import re
import subprocess
import time
import urllib.request
from datetime import datetime
DOCKER = "/Applications/Docker.app/Contents/Resources/bin/docker"
BASE = "http://127.0.0.1:8000/api/v1/system/contours/worker-006/telemetry?history=1"
def read():
return json.load(urllib.request.urlopen(BASE, timeout=5))
def tunnel_pid():
r = subprocess.run(
["launchctl", "print", f"gui/{os.getuid()}/com.nodedc.telemetry-tunnel.local"],
capture_output=True,
text=True,
)
m = re.search(r"\bpid = (\d+)", r.stdout)
return int(m.group(1)) if m else None
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("case", choices=["tunnel", "receiver"])
args = parser.parse_args()
baseline = read()["connection"]
if not baseline["reachable"] or not baseline["identity_matches"]:
raise RuntimeError("Fresh confirmed baseline required")
started = time.monotonic()
baseline_time = datetime.fromisoformat(baseline["observed_at_utc"].replace("Z", "+00:00"))
before_pid = tunnel_pid()
if args.case == "tunnel":
subprocess.run(
[
"launchctl",
"kill",
"SIGKILL",
f"gui/{os.getuid()}/com.nodedc.telemetry-tunnel.local",
],
check=True,
capture_output=True,
)
else:
subprocess.run(
[DOCKER, "stop", "--time", "5", "ndc-mission-core-telemetry-normalizer"],
check=True,
capture_output=True,
timeout=15,
)
seen = set()
while time.monotonic() - started < 90:
time.sleep(2)
try:
probe = read()["connection"]
code = probe.get("error_code")
seen.add(code or "fresh")
observed = datetime.fromisoformat(probe["observed_at_utc"].replace("Z", "+00:00"))
if (
probe["reachable"]
and probe["identity_matches"]
and (observed - baseline_time).total_seconds() >= 10
and time.monotonic() - started >= 10
and (
args.case != "tunnel"
or (tunnel_pid() is not None and tunnel_pid() != before_pid)
)
and (args.case != "receiver" or "telemetry-receiver-unavailable" in seen)
):
print(
json.dumps(
{
"case": args.case,
"automaticRecovery": True,
"elapsedSeconds": round(time.monotonic() - started, 2),
"observedStates": sorted(seen),
"observedAt": probe["observed_at_utc"],
}
)
)
return
except (OSError, ValueError):
seen.add("query-unavailable")
raise RuntimeError("Automatic telemetry recovery not accepted within 90 seconds")
if __name__ == "__main__":
main()
+333
View File
@@ -0,0 +1,333 @@
#!/usr/bin/env python3
"""Versioned macOS telemetry startup: plan/apply/rollback, no worker job launch.
The prepared stack owns credentials and volumes. SSH provides only a loopback
MQTT forward through an existing strictly pinned Tailscale SSH profile.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import plistlib
import re
import subprocess
import time
import urllib.request
from pathlib import Path
from manage_mission_core_launch_agent import _wait_for_health, _write_atomic
from migrate_worker_tunnel_logs import reload_agent
from k1link.launchd_logs import launchd_log_path, prepare_launchd_log
from k1link.web.compute_contour_network import _replace_environment_value
STARTUP = "com.nodedc.telemetry-startup.local"
TUNNEL = "com.nodedc.telemetry-tunnel.local"
CORE = "com.nodedc.mission-core.local"
DOCKER = "/Applications/Docker.app/Contents/Resources/bin/docker"
def command(args, timeout=30):
# Never echo Compose output: expanded environments can contain credentials.
return subprocess.run(args, capture_output=True, timeout=timeout, check=False)
def receiver_ready():
try:
with urllib.request.urlopen("http://127.0.0.1:18030/health", timeout=3) as r:
d = json.loads(r.read())
return (
d.get("ok") is True
and d.get("mqtt_connected") is True
and d.get("database_reachable") is True
)
except (OSError, ValueError):
return False
def broker_loopback():
result = command(
[
DOCKER,
"inspect",
"--format",
"{{json .NetworkSettings.Ports}}",
"ndc-mission-core-mqtt-broker",
],
10,
)
try:
bindings = json.loads(result.stdout).get("1883/tcp", [])
return result.returncode == 0 and bindings == [{"HostIp": "127.0.0.1", "HostPort": "1883"}]
except (ValueError, AttributeError):
return False
def reconcile(stack: Path):
"""One bounded retry, repeated by launchd after delayed Docker/network start."""
if receiver_ready() and broker_loopback():
return "ready"
if command([DOCKER, "info", "--format", "{{.ServerVersion}}"], 10).returncode:
# Desktop's own login setting is not a dependency manager. No VM/resource
# setting is changed and no user inference job is started here.
command(["/usr/bin/open", "-g", "-a", "/Applications/Docker.app"], 10)
return "waiting-for-docker"
compose = [
DOCKER,
"compose",
"--project-directory",
str(stack),
"--env-file",
str(stack / ".env"),
"-f",
str(stack / "compose.yaml"),
]
if command([*compose, "config", "--quiet"], 20).returncode:
return "configuration-unavailable"
if command(
[
*compose,
"up",
"-d",
"--no-build",
"--pull",
"never",
"--wait",
"--wait-timeout",
"45",
"broker",
"timescale",
"normalizer",
],
60,
).returncode:
return "waiting-for-receiver"
return "ready" if receiver_ready() and broker_loopback() else "waiting-for-receiver"
def private_file(path):
if path.is_symlink() or not path.is_file() or path.stat().st_uid != os.getuid():
raise ValueError("Expected owned regular configuration file")
return path.read_bytes()
def plan(stack: Path, ssh_alias: str, repository: Path, agents: Path):
if not stack.is_absolute() or stack.resolve() != stack or not repository.is_absolute():
raise ValueError("Canonical absolute installation paths required")
if re.fullmatch(r"[a-zA-Z0-9][a-zA-Z0-9_.-]{0,80}", ssh_alias) is None:
raise ValueError("Invalid SSH profile")
for name in (
"compose.yaml",
"runtime/agents.json",
"runtime/mosquitto/acl",
"runtime/mosquitto/passwords",
):
private_file(stack / name)
env = private_file(stack / ".env")
public = dict(
line.split("=", 1)
for line in env.decode().splitlines()
if "=" in line and not line.startswith("#")
)
if (
public.get("MISSIONCORE_MQTT_PORT", "1883") != "1883"
or public.get("MISSIONCORE_TELEMETRY_QUERY_PORT", "18030") != "18030"
):
raise ValueError("This deployment profile requires MQTT 1883 and query 18030")
# Verify the existing trust boundary; never enroll a host or accept a new key.
ssh = command(["/usr/bin/ssh", "-G", ssh_alias])
settings = dict(line.split(" ", 1) for line in ssh.stdout.decode().splitlines() if " " in line)
if (
ssh.returncode
or settings.get("stricthostkeychecking") not in ("true", "yes")
or not settings.get("hostname", "").endswith(".ts.net")
):
raise ValueError("A strictly pinned Tailscale SSH profile is required")
python = repository / ".venv/bin/python"
if not python.is_file() or not Path(DOCKER).is_file():
raise ValueError("Prepared application Python and Docker Desktop are required")
common = dict(
RunAtLoad=True, AbandonProcessGroup=False, ProcessType="Background", ExitTimeOut=15
)
startup = dict(
common,
Label=STARTUP,
StartInterval=30,
ProgramArguments=[
str(python),
str(repository / "scripts/manage_telemetry_startup.py"),
"reconcile",
"--stack-root",
str(stack),
],
StandardOutPath="/dev/null",
StandardErrorPath=str(launchd_log_path("telemetry-startup.log")),
)
tunnel = dict(
common,
Label=TUNNEL,
KeepAlive=True,
ThrottleInterval=10,
ProgramArguments=[
"/usr/bin/ssh",
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=yes",
"-o",
"ExitOnForwardFailure=yes",
"-o",
"ConnectTimeout=8",
"-o",
"ServerAliveInterval=10",
"-o",
"ServerAliveCountMax=3",
"-N",
"-T",
"-R",
"127.0.0.1:1883:127.0.0.1:1883",
ssh_alias,
],
StandardOutPath="/dev/null",
StandardErrorPath=str(launchd_log_path("telemetry-tunnel.log")),
)
core_path = agents / (CORE + ".plist")
core = plistlib.loads(private_file(core_path))
if core.get("Label") != CORE or core.get("WorkingDirectory") != str(repository):
raise ValueError("Canonical Mission Core installation changed")
core["EnvironmentVariables"]["MISSIONCORE_TELEMETRY_PLANE_ROOT"] = str(stack)
desired = {
stack / ".env": _replace_environment_value(
env.decode(), "MISSIONCORE_MQTT_BIND_ADDRESS", "127.0.0.1"
).encode()
}
for label, doc in ((STARTUP, startup), (TUNNEL, tunnel), (CORE, core)):
path = agents / (label + ".plist")
if path.exists() and plistlib.loads(private_file(path)).get("Label") != label:
raise ValueError("Foreign LaunchAgent at target path")
desired[path] = plistlib.dumps(doc, sort_keys=True)
changes = []
for path, data in desired.items():
before = private_file(path) if path.exists() else None
changes.append(
dict(
path=str(path),
before=hashlib.sha256(before).hexdigest() if before is not None else None,
after=hashlib.sha256(data).hexdigest(),
)
)
document = dict(
schema_version="missioncore.telemetry-startup-plan/v1",
changes=changes,
stack_root=str(stack),
ssh_profile=ssh_alias,
mqtt="loopback-over-ssh-tailscale",
startup="macOS user login; retry every 30s",
worker_jobs_started=False,
)
document["artifact_sha256"] = hashlib.sha256(Path(__file__).read_bytes()).hexdigest()
document["compose_sha256"] = hashlib.sha256((stack / "compose.yaml").read_bytes()).hexdigest()
document["sha256"] = hashlib.sha256(json.dumps(document, sort_keys=True).encode()).hexdigest()
return document, desired
def restore(backup: Path, agents: Path):
manifest = json.loads(private_file(backup / "manifest.json"))
for item in manifest["changes"]:
path = Path(item["path"])
current = hashlib.sha256(private_file(path)).hexdigest() if path.exists() else None
if current not in (item["before"], item["after"]):
raise ValueError("Installed configuration changed; refusing rollback")
for label in (STARTUP, TUNNEL):
command(["launchctl", "bootout", f"gui/{os.getuid()}/{label}"])
for i, item in enumerate(manifest["changes"]):
path = Path(item["path"])
if item["before"] is None:
path.unlink(missing_ok=True)
else:
old = private_file(backup / str(i))
if hashlib.sha256(old).hexdigest() != item["before"]:
raise ValueError("Rollback payload changed")
_write_atomic(path, old)
for label in (STARTUP, TUNNEL, CORE):
path = agents / (label + ".plist")
if path.exists():
reload_agent(path, label)
if not _wait_for_health(45):
raise RuntimeError("Core health not accepted after rollback")
return {
"restored": True,
"container_state": "not changed; declared endpoint restored on next reconciliation",
}
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("action", choices=["plan", "apply", "reconcile", "rollback"])
p.add_argument("--stack-root", type=Path)
p.add_argument("--repository-root", type=Path, default=Path(__file__).resolve().parents[1])
p.add_argument("--ssh-alias", default="mission-gpu")
p.add_argument("--expected-sha256")
p.add_argument("--backup", type=Path)
a = p.parse_args()
agents = Path.home() / "Library/LaunchAgents"
if a.action == "rollback":
if a.backup is None:
p.error("--backup required")
print(json.dumps(restore(a.backup, agents)))
return
if a.stack_root is None:
p.error("--stack-root required")
if a.action == "reconcile":
try:
phase = reconcile(a.stack_root)
except (OSError, subprocess.SubprocessError, ValueError):
phase = "receiver-unavailable"
# No repeating stdout log; health remains independently observable.
print(json.dumps({"phase": phase}))
return
document, desired = plan(a.stack_root, a.ssh_alias, a.repository_root, agents)
if a.action == "plan":
print(json.dumps(document, indent=2))
return
if a.expected_sha256 != document["sha256"]:
raise ValueError("Startup plan changed before apply")
backup = (
a.repository_root
/ ".runtime/mission-core/telemetry-service-backups"
/ (str(time.time_ns()))
)
backup.mkdir(parents=True, mode=0o700)
_write_atomic(backup / "manifest.json", json.dumps(document).encode())
for i, path in enumerate(desired):
if path.exists():
_write_atomic(backup / str(i), private_file(path))
for name in ("telemetry-startup.log", "telemetry-tunnel.log"):
prepare_launchd_log(launchd_log_path(name))
try:
for path, data in desired.items():
_write_atomic(path, data)
for label in (STARTUP, TUNNEL, CORE):
reload_agent(agents / (label + ".plist"), label)
if not _wait_for_health(45):
raise RuntimeError("Canonical Core failed health acceptance")
except BaseException:
restore(backup, agents)
raise
print(
json.dumps(
{
"installed": True,
"backup": str(backup),
"plan_sha256": document["sha256"],
"telemetry_acceptance": "pending fresh worker sample",
}
)
)
if __name__ == "__main__":
main()
+3 -1
View File
@@ -423,7 +423,9 @@ def build_compute_contour_router(*, root_provider: RootProvider) -> APIRouter:
try:
contour = store.get(contour_id)
telemetry_plane_root = (
Path(__file__).resolve().parents[3] / "deploy" / "telemetry-plane"
Path(os.environ["MISSIONCORE_TELEMETRY_PLANE_ROOT"])
if os.environ.get("MISSIONCORE_TELEMETRY_PLANE_ROOT")
else Path(__file__).resolve().parents[3] / "deploy" / "telemetry-plane"
)
return apply_broker_network(
_network_target(contour),
+1 -1
View File
@@ -722,7 +722,7 @@ def run_worker_agent_probe(
):
return _failed_probe(
profile,
"telemetry-agent-unavailable",
"telemetry-receiver-unavailable",
started,
source="agent-mqtt",
)
+14
View File
@@ -144,3 +144,17 @@ def test_contour_router_returns_404_for_unknown_contour(tmp_path: Path) -> None:
with pytest.raises(HTTPException) as error:
install("missing")
assert error.value.status_code == 404
def test_broker_apply_uses_explicit_installation_root(tmp_path, monkeypatch):
from k1link.web import compute_contour_api as api
root = tmp_path / 'prepared-stack'
monkeypatch.setenv('MISSIONCORE_TELEMETRY_PLANE_ROOT', str(root))
seen = []
def apply(target, installation):
seen.append(installation)
return {'ready': True}
monkeypatch.setattr(api, 'apply_broker_network', apply)
router = api.build_compute_contour_router(root_provider=lambda: tmp_path / 'system')
_endpoint(router, '/api/v1/system/contours/{contour_id}/network/broker', 'POST')('worker-006')
assert seen == [root]
+16
View File
@@ -497,3 +497,19 @@ def test_profile_apply_fails_closed_on_wrong_node_and_keeps_old_profile(
assert error.value.status_code == 409
assert WorkerProfileStore(tmp_path / "system").read().revision == 0
assert not WorkerProfileStore(tmp_path / "system").path.exists()
def test_receiver_failure_is_not_reported_as_worker_failure(monkeypatch):
import urllib.error
from k1link.web import system_telemetry_api as api
def unavailable(*args, **kwargs):
raise urllib.error.URLError("receiver down")
monkeypatch.setattr(api.urllib.request, "urlopen", unavailable)
result = api.run_worker_agent_probe(
api._profile_from_compute_contour(default_compute_contour())
)
assert result["error_code"] == "telemetry-receiver-unavailable"
assert result["reachable"] is False and result["raw"] is None
+146
View File
@@ -0,0 +1,146 @@
from __future__ import annotations
import importlib.util
import json
import plistlib
import subprocess
import sys
from pathlib import Path
import pytest
SCRIPTS = Path(__file__).parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS))
try:
spec = importlib.util.spec_from_file_location(
"telemetry_startup", SCRIPTS / "manage_telemetry_startup.py"
)
manager = importlib.util.module_from_spec(spec)
spec.loader.exec_module(manager)
finally:
sys.path.pop(0)
def setup(tmp_path, monkeypatch):
stack = tmp_path / "state"
repo = tmp_path / "release"
agents = tmp_path / "agents"
for p in (stack, repo, agents):
p.mkdir()
for name in (
"compose.yaml",
"runtime/agents.json",
"runtime/mosquitto/acl",
"runtime/mosquitto/passwords",
):
p = stack / name
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text("fixture")
(stack / ".env").write_text("MISSIONCORE_MQTT_BIND_ADDRESS=192.168.1.5\nSECRET=preserve-me\n")
(repo / ".venv/bin").mkdir(parents=True)
(repo / ".venv/bin/python").write_text("fixture")
docker = tmp_path / "docker"
docker.write_text("fixture")
monkeypatch.setattr(manager, "DOCKER", str(docker))
(agents / (manager.CORE + ".plist")).write_bytes(
plistlib.dumps(
{
"Label": manager.CORE,
"WorkingDirectory": str(repo),
"EnvironmentVariables": {"MISSIONCORE_DATA_DIR": "/existing-data"},
}
)
)
monkeypatch.setattr(
manager,
"command",
lambda *args: subprocess.CompletedProcess(
[], 0, b"hostname worker.example.ts.net\nstricthostkeychecking true\n", b""
),
)
return stack, repo, agents
def test_plan_preserves_credentials_and_pins_loopback_transport(tmp_path, monkeypatch):
stack, repo, agents = setup(tmp_path, monkeypatch)
before = (stack / ".env").read_bytes()
doc, files = manager.plan(stack, "worker-test", repo, agents)
assert (stack / ".env").read_bytes() == before
assert files[stack / ".env"] == b"MISSIONCORE_MQTT_BIND_ADDRESS=127.0.0.1\nSECRET=preserve-me\n"
assert "preserve-me" not in json.dumps(doc)
startup = plistlib.loads(files[agents / (manager.STARTUP + ".plist")])
assert startup["RunAtLoad"] and startup["StartInterval"] == 30
tunnel = plistlib.loads(files[agents / (manager.TUNNEL + ".plist")])
assert "127.0.0.1:1883:127.0.0.1:1883" in tunnel["ProgramArguments"]
assert "StrictHostKeyChecking=yes" in tunnel["ProgramArguments"]
assert tunnel["KeepAlive"] and tunnel["ThrottleInterval"] >= 5
core = plistlib.loads(files[agents / (manager.CORE + ".plist")])
assert core["EnvironmentVariables"]["MISSIONCORE_DATA_DIR"] == "/existing-data"
assert core["EnvironmentVariables"]["MISSIONCORE_TELEMETRY_PLANE_ROOT"] == str(stack)
def test_plan_refuses_non_tailscale_and_changed_input(tmp_path, monkeypatch):
stack, repo, agents = setup(tmp_path, monkeypatch)
before, _ = manager.plan(stack, "worker-test", repo, agents)
with (stack / ".env").open("a") as f:
f.write("NEW=setting\n")
after, _ = manager.plan(stack, "worker-test", repo, agents)
assert before["sha256"] != after["sha256"]
monkeypatch.setattr(
manager,
"command",
lambda *args: subprocess.CompletedProcess(
[], 0, b"hostname worker.local\nstricthostkeychecking true\n", b""
),
)
with pytest.raises(ValueError, match="Tailscale"):
manager.plan(stack, "worker-test", repo, agents)
def test_delayed_docker_retries_without_compose_or_workload_launch(monkeypatch, tmp_path):
calls = []
monkeypatch.setattr(manager, "receiver_ready", lambda: False)
def run(args, timeout):
calls.append(args)
return subprocess.CompletedProcess(args, 1 if "info" in args else 0)
monkeypatch.setattr(manager, "command", run)
assert manager.reconcile(tmp_path) == "waiting-for-docker"
assert len(calls) == 2 and calls[1][0] == "/usr/bin/open"
assert not any("compose" in c for c in calls)
def test_ready_receiver_is_not_restarted(monkeypatch, tmp_path):
monkeypatch.setattr(manager, "receiver_ready", lambda: True)
monkeypatch.setattr(manager, "broker_loopback", lambda: True)
monkeypatch.setattr(
manager, "command", lambda *args: pytest.fail("Must not restart healthy services")
)
assert manager.reconcile(tmp_path) == "ready"
def test_old_lan_binding_is_reconciled_even_when_receiver_healthy(monkeypatch, tmp_path):
calls = []
bindings = iter((False, True))
monkeypatch.setattr(manager, "receiver_ready", lambda: True)
monkeypatch.setattr(manager, "broker_loopback", lambda: next(bindings))
def run(args, timeout):
calls.append(args)
return subprocess.CompletedProcess(args, 0)
monkeypatch.setattr(manager, "command", run)
assert manager.reconcile(tmp_path) == "ready"
assert calls[-1][-3:] == ["broker", "timescale", "normalizer"]
assert "--no-build" in calls[-1] and "--pull" in calls[-1] and "never" in calls[-1]
def test_failed_compose_reports_waiting_without_ready(monkeypatch, tmp_path):
monkeypatch.setattr(manager, "receiver_ready", lambda: False)
monkeypatch.setattr(
manager,
"command",
lambda args, timeout: subprocess.CompletedProcess(args, int("up" in args)),
)
assert manager.reconcile(tmp_path) == "waiting-for-receiver"