forked from cflmy/deepseek-harness
Implement the downstream product in `packages/marqdo`: - `@marqdo/mdh-harness`: application home layout, atomic writes, locks, bounded fixed-argv processes, safe tar extraction, Observation 1 and ValueWire validation; - trusted Gitea release discovery, SHA-256/platform/Bundle verification, transactional environment install, rollback and cleanup, CLI Tool changes; - private knowledge collections with strict import policy, the official `marqdo-knowledge` compile/query adapter and a disclosed locator fallback; - Analysis 3 parsing, approved run/call execution, evidence and staleness; - the six model operations (find/read/check/program/ecosystem/knowledge_search); - the `/mdh` workbench over the existing web server with a same-origin fence; - `mdh` launcher, CI/package/upstream scripts and a Woodpecker pipeline. Tests: 28 node:test cases (27 PASS, one network-gated install test also PASS with MDH_TEST_INSTALL=1). Product install/workbench evidence is recorded in docs/mdh/evidence. Part of marqdo#25 (MDH-03..MDH-08).
146 lines
5.3 KiB
TypeScript
146 lines
5.3 KiB
TypeScript
/**
|
|
* Run evidence: the immutable identity of one real execution.
|
|
*
|
|
* Evidence records are written once per run, kept locally (0600), and never
|
|
* uploaded. A record binds the workspace document digest, Analysis digest, CLI
|
|
* and environment identity, grants, argv, status and result summary. Reading a
|
|
* record re-checks the current source/environment to label it `fresh` or
|
|
* `stale`; a stale record is never presented as current.
|
|
*
|
|
* @module @marqdo/mdh-harness/execution/evidence
|
|
*/
|
|
|
|
import { randomBytes } from 'node:crypto'
|
|
import { readdir } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { digestFile, readJson, writeJsonAtomic } from '../core/fsx.ts'
|
|
import type { ObservationSummary } from '../core/observation.ts'
|
|
import type { Grants, SandboxMode } from './cli.ts'
|
|
|
|
/** Evidence schema identity. */
|
|
export const RUN_EVIDENCE_SCHEMA = 'marqdo.mdh.run-evidence'
|
|
|
|
/** Freshness of one stored record relative to the current world. */
|
|
export type EvidenceFreshness = 'fresh' | 'stale' | 'unverifiable'
|
|
|
|
/** One persisted run record. */
|
|
export interface RunEvidence {
|
|
readonly schema: typeof RUN_EVIDENCE_SCHEMA
|
|
readonly schema_version: 1
|
|
readonly run_id: string
|
|
readonly created_at: string
|
|
readonly workspace: string
|
|
readonly document: string
|
|
readonly document_digest: string
|
|
readonly analysis_digest: string
|
|
readonly environment_id: string | null
|
|
readonly cli: {
|
|
readonly path: string
|
|
readonly version: string | null
|
|
readonly source_sha: string | null
|
|
}
|
|
readonly bundle_digest: string | null
|
|
readonly lock_digest: string | null
|
|
readonly action: 'run' | 'call'
|
|
readonly function_name: string | null
|
|
readonly arguments: readonly string[]
|
|
readonly mode: SandboxMode
|
|
readonly grants: Grants
|
|
readonly status: ObservationSummary['status']
|
|
readonly terminal_kind: string | null
|
|
readonly result: {
|
|
readonly value: unknown
|
|
readonly error: unknown
|
|
readonly value_render: string | null
|
|
}
|
|
readonly observation: {
|
|
readonly event_count: number
|
|
readonly violations: readonly string[]
|
|
readonly stored: boolean
|
|
}
|
|
readonly result_artifact: string | null
|
|
}
|
|
|
|
/** Recorded result with its current freshness. */
|
|
export interface RunRecord extends RunEvidence {
|
|
readonly freshness: EvidenceFreshness
|
|
readonly freshness_detail: string
|
|
}
|
|
|
|
/** Local evidence store. */
|
|
export class EvidenceStore {
|
|
readonly #runs: string
|
|
|
|
constructor(runsDirectory: string) {
|
|
this.#runs = runsDirectory
|
|
}
|
|
|
|
/** Persist one record; the observation copy is private and bounded. */
|
|
async save(record: RunEvidence, observationText: string): Promise<void> {
|
|
await writeJsonAtomic(join(this.#runs, `${record.run_id}.json`), record)
|
|
if (observationText.trim() !== '') {
|
|
await writeJsonAtomic(join(this.#runs, `${record.run_id}.observation.json`), {
|
|
schema: 'marqdo.mdh.observation-copy',
|
|
schema_version: 1,
|
|
run_id: record.run_id,
|
|
format: 'snapshot',
|
|
// The exact atomic observation snapshot text, preserved verbatim.
|
|
payload: observationText,
|
|
})
|
|
}
|
|
}
|
|
|
|
/** Read one record and evaluate freshness against the current workspace. */
|
|
async read(runId: string, workspace: string): Promise<RunRecord | undefined> {
|
|
if (!/^[A-Za-z0-9._-]+$/.test(runId)) throw new Error(`invalid run id: ${runId}`)
|
|
const record = await readJson<RunEvidence>(join(this.#runs, `${runId}.json`))
|
|
if (!record) return undefined
|
|
const freshness = await this.#freshness(record, workspace)
|
|
return { ...record, freshness: freshness.state, freshness_detail: freshness.detail }
|
|
}
|
|
|
|
/** List the most recent records (bounded), newest first. */
|
|
async list(workspace: string, limit = 50): Promise<RunRecord[]> {
|
|
const names = await readdir(this.#runs).catch(() => [] as string[])
|
|
const ids = names.filter(name => name.endsWith('.json') && !name.endsWith('.observation.json'))
|
|
.map(name => name.slice(0, -'.json'.length))
|
|
.sort()
|
|
.reverse()
|
|
.slice(0, Math.max(1, Math.min(limit, 200)))
|
|
const records: RunRecord[] = []
|
|
for (const id of ids) {
|
|
const record = await this.read(id, workspace)
|
|
if (record) records.push(record)
|
|
}
|
|
return records
|
|
}
|
|
|
|
/** New unique run id suffix for evidence keyed by the Marqdo run id. */
|
|
keyFor(runId: string): string {
|
|
return `${runId.replace(/[^A-Za-z0-9._-]/g, '_')}-${randomBytes(4).toString('hex')}`
|
|
}
|
|
|
|
/** Compare a record with the current document and bundle. */
|
|
async #freshness(record: RunEvidence, workspace: string): Promise<{ state: EvidenceFreshness; detail: string }> {
|
|
const document = join(workspace, record.document)
|
|
let current: string
|
|
try {
|
|
current = await digestFile(document)
|
|
} catch {
|
|
return { state: 'stale', detail: 'source document is missing' }
|
|
}
|
|
if (current !== record.document_digest) {
|
|
return { state: 'stale', detail: `source changed: now ${current}` }
|
|
}
|
|
return { state: 'fresh', detail: 'source and Analysis identity match' }
|
|
}
|
|
}
|
|
|
|
/** Render a Marqdo ValueWire value without inventing precision. */
|
|
export function renderValue(value: unknown): string | null {
|
|
if (typeof value !== 'object' || value === null) return null
|
|
const record = value as Record<string, unknown>
|
|
if (typeof record.type === 'string' && typeof record.decimal === 'string') return record.decimal
|
|
return null
|
|
}
|