mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-03 16:50:35 +08:00
* feat: add north-star tracking evidence * fix(distribution): ship the tracking task ledger in packaged applications phase1.ts and server.ts import tracking/task-ledger.ts, but the packaged application file list never gained the new module, so npm artifacts failed to load with ERR_MODULE_NOT_FOUND and the distribution lane failed. Add the module to the positive distribution list so both profiles keep a loadable application. Generated-By: looper 0.16.2 (runner=fixer, agent=pi) * fix(tracking): attribute worker evidence and ledger every accepted export Queue snapshots, heartbeats and technical-export checks now pass the worker entrypoint instead of inheriting the MCP tracker default. Host run failures and cancellations read the recorded work-round mode, and a failure only reports a failed run state after it actually closed the round. Every accepted export is now ledgered inside the shared export entry, so the Studio and legacy routes produce the same evidence as the MCP adapter, and an unreadable ledger is recorded as a diagnostic instead of failing an export that already started rendering. Repeating an identical terminal result keeps the recorded terminal time, and quality-check version ids follow the public version-id contract instead of requiring a v4 UUID. Generated-By: looper 0.16.2 (runner=fixer, agent=pi) * fix: correct instrumentation evidence semantics * fix: preserve work round failure evidence * fix: close interrupted exports with one terminal outcome Preview delivery now needs the confirmed current version and the originating round's fixed version, and preview evidence is emitted only after recordDisplay passes its version guard, so a rejected display no longer claims an observation and a delivery for the same run. Technical export checks report cancelled or unfinished jobs as unreviewed instead of error; only a completed job without a verification report stays an error. The export ledger entry for an interrupted export is now closed once with outcome unknown, and the worker tick that owns that transition emits the matching task_terminal evidence. A second tick or a status read sees the closed record and claim nothing. The tracking README describes that reconciliation path. Generated-By: looper 0.16.2 (runner=fixer, agent=pi) * fix: keep interrupted-export reconciliation out of live renders The worker tick reconciled before taking the working-set guard, so a later tick could close a job this process was still rendering, the local render was left running, and the owning worker then stated a second task_terminal for the same task. Reconciliation now runs only for a tick that does not own the render, it aborts any local controller of a job it closes, and the claiming worker states its terminal evidence through one helper that skips a job the reconciler already closed as unknown. Generated-By: looper 0.16.2 (runner=fixer, agent=pi) * fix: state terminal evidence from the caller that reconciles an export Coalesce interrupted-export reconciliation into one owner so every path that closes a task also reports it: the read path now records the same task_terminal evidence as the worker tick, keeps the ledger terminal time and never restates the outcome on a later read. Stores carry the service tracker, including project-scoped stores and the MCP read paths. Generated-By: looper 0.16.2 (runner=fixer, agent=pi) * fix: preserve provider preview schema --------- Co-authored-by: qindong <qindong@qindongs-MBP.lan> Co-authored-by: 罗高 <luogao@users.noreply.github.com> Co-authored-by: qindong <qindong@qindongs-MacBook-Pro.local>
113 lines
8.5 KiB
TypeScript
113 lines
8.5 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { TaskLedger } from '../tracking/task-ledger.ts';
|
|
import { createTracker, validProperties } from '../tracking/core.ts';
|
|
import type { PendingEvent } from '../tracking/core.ts';
|
|
import { omEvents } from '../tracking/events.ts';
|
|
import { fixture } from './fixtures.ts';
|
|
|
|
test('task ledger persists accepted and terminal export state across instances', async () => {
|
|
const directory = await mkdtemp(path.join(os.tmpdir(), 'openmotion-task-ledger-'));
|
|
try {
|
|
const taskId = crypto.randomUUID(), accepted = new Date().toISOString(), deadline = new Date(Date.now() + 1_000).toISOString();
|
|
const first = new TaskLedger(directory);
|
|
await first.accept({ task_id: taskId, task_type: 'export', project_id: 'project', accepted_at: accepted, deadline_at: deadline });
|
|
await first.accept({ task_id: taskId, task_type: 'export', project_id: 'project', accepted_at: accepted, deadline_at: deadline });
|
|
await assert.rejects(first.accept({ task_id: taskId, task_type: 'export', project_id: 'project', accepted_at: accepted, deadline_at: new Date(Date.now() + 2_000).toISOString() }), /TASK_LEDGER_CONFLICT/);
|
|
await first.terminal(taskId, 'success', 'true');
|
|
const second = new TaskLedger(directory), snapshot = await second.snapshot();
|
|
assert.equal(snapshot[taskId].outcome, 'success');
|
|
assert.equal(snapshot[taskId].applied, 'true');
|
|
assert.ok(snapshot[taskId].terminal_at);
|
|
const evidence = await readFile(path.join(directory, 'tracking', 'tasks.json'), 'utf8');
|
|
await new Promise(resolve => setTimeout(resolve, 5));
|
|
assert.equal((await first.terminal(taskId, 'success', 'true')).terminal_at, snapshot[taskId].terminal_at);
|
|
assert.equal(await readFile(path.join(directory, 'tracking', 'tasks.json'), 'utf8'), evidence, 'a repeated identical terminal result keeps the recorded evidence');
|
|
} finally { await rm(directory, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test('task ledger rejects malformed or conflicting terminal transitions', async () => {
|
|
const directory = await mkdtemp(path.join(os.tmpdir(), 'openmotion-task-ledger-'));
|
|
try {
|
|
const taskId = crypto.randomUUID(), ledger = new TaskLedger(directory), now = new Date().toISOString();
|
|
await assert.rejects(ledger.accept({ task_id: 'bad', task_type: 'export', accepted_at: now, deadline_at: now }), /TASK_LEDGER_INVALID/);
|
|
await ledger.accept({ task_id: taskId, task_type: 'export', accepted_at: now, deadline_at: now });
|
|
await ledger.terminal(taskId, 'failure', 'unknown', 'EXPORT_FAILED');
|
|
const terminal = await ledger.snapshot();
|
|
await new Promise(resolve => setTimeout(resolve, 2));
|
|
const repeated = await ledger.terminal(taskId, 'failure', 'unknown', 'EXPORT_FAILED');
|
|
assert.equal(repeated.terminal_at, terminal[taskId].terminal_at);
|
|
await assert.rejects(ledger.terminal(taskId, 'success', 'true'), /TASK_LEDGER_CONFLICT/);
|
|
} finally { await rm(directory, { recursive: true, force: true }); }
|
|
});
|
|
|
|
test('the shared export entry accepts the task before the worker closes it', () => fixture(async store => {
|
|
const current = await store.get();
|
|
const job = await store.requestExport(current.revision, { requestId: crypto.randomUUID() });
|
|
const task = (await new TaskLedger(store.directory).snapshot())[job.id];
|
|
assert.equal(task.task_type, 'export');
|
|
assert.equal(task.project_id, store.projectId);
|
|
assert.equal(task.accepted_at, new Date(job.acceptedAt!).toISOString());
|
|
assert.equal(task.deadline_at, new Date(job.deadlineAt!).toISOString());
|
|
await assert.doesNotReject(new TaskLedger(store.directory).terminal(job.id, 'cancelled', 'unknown'));
|
|
}));
|
|
|
|
test('an interrupted export is reconciled with a terminal ledger outcome', () => fixture(async store => {
|
|
const events: PendingEvent[] = [], tracking = createTracker({ registry: omEvents, entrypoint: 'mcp', enabled: true, flushIntervalMs: 0,
|
|
send: async batch => { events.push(...batch); return 'accepted'; } });
|
|
store.tracking = tracking;
|
|
const current = await store.get();
|
|
const job = await store.requestExport(current.revision);
|
|
const stale = (await store.job(job.id))!;
|
|
// A crashed worker leaves the job past its deadline with an open accepted task.
|
|
await store.saveJob({ ...stale, expiresAt: Date.now() - 1 });
|
|
const status = await store.status(job.id);
|
|
assert.equal(status.exportJob!.state, 'failed');
|
|
const task = (await new TaskLedger(store.directory).snapshot())[job.id];
|
|
assert.equal(task.outcome, 'unknown');
|
|
assert.equal(task.applied, 'unknown');
|
|
assert.ok(task.terminal_at);
|
|
// The read that closed the task is the one owner of its terminal evidence.
|
|
await tracking.flush();
|
|
const terminal = events.filter(event => event.event === 'task_terminal');
|
|
assert.equal(terminal.length, 1);
|
|
assert.equal(terminal[0].entrypoint, 'worker');
|
|
assert.equal(terminal[0].properties.outcome, 'unknown');
|
|
assert.equal(terminal[0].properties.terminal_at, task.terminal_at, 'the evidence keeps the ledger terminal time');
|
|
await store.status(job.id);
|
|
await tracking.flush();
|
|
assert.equal(events.filter(event => event.event === 'task_terminal').length, 1, 'a later read does not restate the terminal outcome');
|
|
tracking.close();
|
|
}));
|
|
|
|
test('an unreadable task ledger never fails an accepted export', () => fixture(async store => {
|
|
await mkdir(path.join(store.directory, 'tracking'), { recursive: true });
|
|
await writeFile(path.join(store.directory, 'tracking', 'tasks.json'), 'corrupted');
|
|
const current = await store.get();
|
|
const job = await store.requestExport(current.revision);
|
|
assert.equal(job.state, 'pending');
|
|
assert.equal((await store.job(job.id))!.state, 'pending');
|
|
await assert.rejects(new TaskLedger(store.directory).snapshot(), /TASK_LEDGER_UNREADABLE/);
|
|
// Reconciling an interrupted export keeps the degraded ledger from failing the read path.
|
|
await store.saveJob({ ...(await store.job(job.id))!, expiresAt: Date.now() - 1 });
|
|
assert.equal((await store.status(job.id)).exportJob!.state, 'failed');
|
|
}));
|
|
|
|
test('north-star evidence events keep closed, content-free schemas', () => {
|
|
const projectId = crypto.randomUUID(), versionId = crypto.randomUUID(), runId = crypto.randomUUID(), checkId = crypto.randomUUID(), instanceId = crypto.randomUUID();
|
|
// Native history entry ids are the public version identity, so a check never requires a v4 UUID.
|
|
const nativeVersion = 'history-entry-7';
|
|
assert.equal(validProperties(omEvents, 'host_run_started', { run_id: runId, entry_type: 'mcp', task_type: 'create' }), true);
|
|
assert.equal(validProperties(omEvents, 'host_preview_observed', { run_id: runId, project_id: 'project', revision: 'a'.repeat(64), version_id: versionId, visibility: 'visible', content_verified: true, observation_source: 'host_reported' }), true);
|
|
assert.equal(validProperties(omEvents, 'check_completed', { check_id: checkId, check_type: 'technical_export', project_id: projectId, version_id: versionId, subject_id: checkId.slice(0, 32), checklist_version: 'export-v1', checker_type: 'automated', result: 'pass' }), true);
|
|
assert.equal(validProperties(omEvents, 'check_required', { check_id: checkId, check_type: 'preview_observed', project_id: 'project', version_id: nativeVersion, subject_id: checkId.slice(0, 32), checklist_version: 'preview-v1', checker_type: 'host', required: true }), true);
|
|
assert.equal(validProperties(omEvents, 'host_preview_observed', { run_id: runId, project_id: 'project', revision: 'a'.repeat(64), version_id: nativeVersion, visibility: 'visible', content_verified: true, observation_source: 'host_reported' }), true);
|
|
assert.equal(validProperties(omEvents, 'queue_snapshot', { instance_id: instanceId, task_type: 'export', queued_count: 0, running_count: 1, available_slots: 0, counts_complete: true }), true);
|
|
assert.equal(validProperties(omEvents, 'incident_confirmed', { incident_id: crypto.randomUUID(), incident_type: 'false_success', severity: 'sev2', subject_id: checkId.slice(0, 32) }), true);
|
|
assert.equal(validProperties(omEvents, 'host_preview_observed', { run_id: runId, project_id: 'project', revision: 'private/path', version_id: versionId, visibility: 'visible', content_verified: true, observation_source: 'host_reported' }), false);
|
|
assert.equal(validProperties(omEvents, 'check_completed', { check_id: checkId, check_type: 'technical_export', project_id: projectId, version_id: 'native-history-id', subject_id: checkId.slice(0, 32), checklist_version: 'export-v1', checker_type: 'automated', result: 'pass' }), true);
|
|
});
|