mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-02 19:50:03 +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>
89 lines
4.5 KiB
TypeScript
89 lines
4.5 KiB
TypeScript
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { withProcessLock } from '../shared/process-lock.ts';
|
|
|
|
export type LedgerTaskType = 'asset_import' | 'edit' | 'export';
|
|
export type LedgerOutcome = 'success' | 'failure' | 'unknown' | 'cancelled';
|
|
export type LedgerTask = {
|
|
task_id: string;
|
|
task_type: LedgerTaskType;
|
|
project_id?: string;
|
|
run_id?: string;
|
|
operation_id?: string;
|
|
accepted_at: string;
|
|
deadline_at: string;
|
|
terminal_at?: string;
|
|
outcome?: LedgerOutcome;
|
|
applied?: 'true' | 'false' | 'unknown';
|
|
error_code?: string;
|
|
};
|
|
|
|
const id = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
// Project directories accept the built-in project and any UUID, so the ledger does too.
|
|
const projectIdPattern = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i;
|
|
const iso = (value: string) => /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(value) && Number.isFinite(Date.parse(value));
|
|
function validTask(value: unknown): value is LedgerTask {
|
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const task = value as LedgerTask;
|
|
return id.test(task.task_id) && ['asset_import', 'edit', 'export'].includes(task.task_type)
|
|
&& iso(task.accepted_at) && iso(task.deadline_at)
|
|
&& (task.project_id === undefined || task.project_id === 'project' || projectIdPattern.test(task.project_id))
|
|
&& (task.run_id === undefined || id.test(task.run_id))
|
|
&& (task.operation_id === undefined || id.test(task.operation_id))
|
|
&& (task.terminal_at === undefined || iso(task.terminal_at))
|
|
&& (task.outcome === undefined || ['success', 'failure', 'unknown', 'cancelled'].includes(task.outcome))
|
|
&& (task.applied === undefined || ['true', 'false', 'unknown'].includes(task.applied))
|
|
&& (task.error_code === undefined || /^[A-Za-z0-9_.-]{1,128}$/.test(task.error_code));
|
|
}
|
|
|
|
export class TaskLedger {
|
|
readonly folder: string;
|
|
readonly file: string;
|
|
constructor(directory: string) {
|
|
this.folder = path.join(directory, 'tracking');
|
|
this.file = path.join(this.folder, 'tasks.json');
|
|
}
|
|
private async read(): Promise<Record<string, LedgerTask>> {
|
|
try {
|
|
const value = JSON.parse(await readFile(this.file, 'utf8')) as Record<string, unknown>;
|
|
if (!value || typeof value !== 'object' || Object.values(value).some(task => !validTask(task))) throw new Error('TASK_LEDGER_CORRUPT');
|
|
return value as Record<string, LedgerTask>;
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return {};
|
|
if (error instanceof Error && error.message === 'TASK_LEDGER_CORRUPT') throw error;
|
|
throw new Error('TASK_LEDGER_UNREADABLE');
|
|
}
|
|
}
|
|
private async write(tasks: Record<string, LedgerTask>) {
|
|
await mkdir(this.folder, { recursive: true, mode: 0o700 });
|
|
const temporary = path.join(this.folder, 'tasks.' + crypto.randomUUID() + '.tmp');
|
|
try { await writeFile(temporary, JSON.stringify(tasks), { mode: 0o600, flag: 'wx' }); await rename(temporary, this.file); }
|
|
finally { await rm(temporary, { force: true }); }
|
|
}
|
|
async accept(task: LedgerTask) {
|
|
if (!validTask(task)) throw new Error('TASK_LEDGER_INVALID');
|
|
return withProcessLock(path.join(this.folder, 'tasks.lock'), async () => {
|
|
const tasks = await this.read(), existing = tasks[task.task_id];
|
|
if (existing) {
|
|
if (JSON.stringify(existing) !== JSON.stringify(task)) throw new Error('TASK_LEDGER_CONFLICT');
|
|
return existing;
|
|
}
|
|
tasks[task.task_id] = task; await this.write(tasks); return task;
|
|
});
|
|
}
|
|
async terminal(taskId: string, outcome: LedgerOutcome, applied: LedgerTask['applied'], errorCode?: string) {
|
|
if (!id.test(taskId)) throw new Error('TASK_LEDGER_INVALID');
|
|
return withProcessLock(path.join(this.folder, 'tasks.lock'), async () => {
|
|
const tasks = await this.read(), task = tasks[taskId]; if (!task) throw new Error('TASK_LEDGER_NOT_FOUND');
|
|
if (task.outcome) {
|
|
if (task.outcome !== outcome || task.applied !== applied) throw new Error('TASK_LEDGER_CONFLICT');
|
|
// Repeating the same observed result never moves the recorded terminal time.
|
|
return task;
|
|
}
|
|
task.outcome = outcome; task.applied = applied; task.terminal_at = new Date().toISOString();
|
|
if (errorCode) task.error_code = errorCode; await this.write(tasks); return task;
|
|
});
|
|
}
|
|
async snapshot() { return withProcessLock(path.join(this.folder, 'tasks.lock'), () => this.read()); }
|
|
}
|