test: split cli tests from source tree (#216)

* feat(cli): define full warehouse dialect contract

* test(cli): keep dialect edge tests focused

* fix(cli): stabilize dialect contract foundation

* refactor(connectors): own read-only query preparation

* refactor(connectors): resolve dialects through registry

* refactor(connectors): keep concrete dialect classes internal

* chore(workspace): enforce dialect import boundary

* refactor(cli): resolve relationship dialect at scan boundary

* refactor(cli): use dialect display parsing for entity details

* refactor(cli): use dialect display parsing for warehouse catalog

* refactor(cli): use dialect SQL in relationship workflows

* test(cli): verify solid dialect scan workflow closure

* test: split cli tests from source tree

* refactor(cli): standardize BigQuery scope listing

* feat(sqlite): implement connector scope listing

* test(connectors): cover required table listing

* feat(cli): add warehouse driver registry

* refactor(setup): route scope discovery through driver registry

* refactor(cli): route local query execution through driver registry

* refactor(historic-sql): route dialect support through driver registry

* refactor(cli): test warehouse connections through driver registry

* fix(cli): close driver registry type export gaps

* Improve setup daemon diagnostics

* refactor(setup): centralize rail-prefixed diagnostics + query-history fallback

Extract errorMessage, writePrefixedLines, and flushPrefixedBufferedCommandOutput
into clack.ts so the setup wizard, managed daemons, and embedding/agent steps
share one rail-formatted writer. setup-databases.ts also adds a
"disable query history and retry" option when the schema-context build fails
and query history is the likely culprit, surfaced via a new
failed-query-history-unavailable status.

* fix(cli): carry catalog through the picker so BigQuery/Snowflake/SQL Server scope filters match

The setup picker's KtxTableListEntry was a 2-level { schema, name }, so
qualifiedTableId always wrote db.name into enabled_tables. When BigQuery,
Snowflake, or SQL Server later ran fast ingest, their introspect step filtered
the scope set with scopedTableNames(scope, { catalog: projectId|database, db })
— catalog was non-null on the introspect side but null in the scope refs, so
every entry was rejected, the live-database adapter staged zero table files,
and detect() failed with 'Adapter "live-database" did not recognize fetched
source output'.

Align the picker boundary with the canonical 3-level KtxTableRef:

- Add catalog: string | null to KtxTableListEntry.
- BigQuery/Snowflake/SQL Server listTables populate catalog from the
  resolved projectId / database; Postgres/MySQL/ClickHouse/SQLite set null.
- qualifiedTableId emits catalog.schema.name when catalog is non-null
  (resolveEnabledTables already accepts the 3-part shape) and
  schemasFromEnabledTables now goes through parseDottedTableEntry so it
  recovers the schema correctly from both 2-part and 3-part entries.
- Export parseDottedTableEntry from enabled-tables.ts (@internal) for picker
  reuse.

Update listTables expectations in all seven connector tests and the setup /
picker test fixtures. Add a picker regression test that covers the
catalog-bearing round-trip (save + refine).

* fix(cli): allow debug telemetry under opt-out env
This commit is contained in:
Andrey Avtomonov 2026-05-26 08:49:05 +02:00 committed by GitHub
parent 924868841d
commit 56985b7e09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
548 changed files with 5048 additions and 2228 deletions

View file

@ -0,0 +1,168 @@
import type { MemoryFlowReplayInput } from '../../../../src/context/ingest/memory-flow/types.js';
function baseScenario(overrides: Partial<MemoryFlowReplayInput> = {}): MemoryFlowReplayInput {
return {
runId: 'run-success',
connectionId: 'warehouse',
adapter: 'metricflow',
status: 'done',
sourceDir: '/tmp/source',
syncId: 'sync-success',
reportPath: 'ingest-report.json',
errors: [],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 4 },
{ type: 'scope_detected', fingerprint: 'metricflow:demo' },
{ type: 'raw_snapshot_written', syncId: 'sync-success', rawFileCount: 4 },
{ type: 'diff_computed', added: 2, modified: 1, deleted: 0, unchanged: 1 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 2, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'orders', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'orders', target: 'wiki', action: 'created', key: 'wiki/global/orders.md' },
{ type: 'candidate_action', unitKey: 'orders', target: 'sl', action: 'updated', key: 'warehouse.orders' },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'success' },
{ type: 'work_unit_started', unitKey: 'revenue', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'revenue', target: 'wiki', action: 'updated', key: 'wiki/global/revenue.md' },
{ type: 'work_unit_finished', unitKey: 'revenue', status: 'success' },
{ type: 'reconciliation_finished', conflictCount: 0, fallbackCount: 0 },
{ type: 'saved', commitSha: 'abc123456789', wikiCount: 2, slCount: 1 }, // pragma: allowlist secret
{ type: 'provenance_recorded', rowCount: 4 },
{ type: 'report_created', runId: 'run-success', reportPath: 'ingest-report.json' },
],
plannedWorkUnits: [
{ unitKey: 'orders', rawFiles: ['models/orders.yml', 'models/customers.yml'], peerFileCount: 1, dependencyCount: 1 },
{ unitKey: 'revenue', rawFiles: ['docs/revenue.md'], peerFileCount: 0, dependencyCount: 0 },
],
details: {
actions: [
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/global/orders.md',
summary: 'Captured order definitions',
rawFiles: ['models/orders.yml'],
status: 'success',
},
{
unitKey: 'orders',
target: 'sl',
action: 'updated',
key: 'warehouse.orders',
summary: 'Updated orders source',
rawFiles: ['models/orders.yml'],
status: 'success',
},
{
unitKey: 'revenue',
target: 'wiki',
action: 'updated',
key: 'wiki/global/revenue.md',
summary: 'Updated revenue notes',
rawFiles: ['docs/revenue.md'],
status: 'success',
},
],
provenance: [
{
rawPath: 'models/orders.yml',
artifactKind: 'wiki',
artifactKey: 'wiki/global/orders.md',
actionType: 'created',
},
{ rawPath: 'models/orders.yml', artifactKind: 'sl', artifactKey: 'warehouse.orders', actionType: 'updated' },
],
transcripts: [
{
unitKey: 'orders',
path: 'transcripts/orders.json',
toolCallCount: 3,
errorCount: 0,
toolNames: ['wiki_write', 'sl_write_source'],
},
],
},
...overrides,
};
}
export function successfulReplayScenario(): MemoryFlowReplayInput {
return baseScenario();
}
export function deletedRawPathsScenario(): MemoryFlowReplayInput {
return baseScenario({
events: baseScenario().events.map((event) =>
event.type === 'diff_computed'
? { ...event, deleted: 2 }
: event.type === 'chunks_planned'
? { ...event, evictionCount: 2 }
: event,
),
});
}
export function validationRevertScenario(): MemoryFlowReplayInput {
return baseScenario({
runId: 'run-validation-failure',
status: 'error',
errors: ['semantic-layer validation failed for warehouse.orders'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 1 },
{ type: 'raw_snapshot_written', syncId: 'sync-validation', rawFileCount: 1 },
{ type: 'diff_computed', added: 1, modified: 0, deleted: 0, unchanged: 0 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'orders', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'orders', target: 'sl', action: 'updated', key: 'warehouse.orders' },
{
type: 'work_unit_finished',
unitKey: 'orders',
status: 'failed',
reason: 'semantic-layer validation failed for warehouse.orders',
},
],
plannedWorkUnits: [{ unitKey: 'orders', rawFiles: ['models/orders.yml'], peerFileCount: 0, dependencyCount: 0 }],
details: {
actions: [
{
unitKey: 'orders',
target: 'sl',
action: 'updated',
key: 'warehouse.orders',
summary: 'Invalid measure was reverted',
rawFiles: ['models/orders.yml'],
status: 'failed',
},
],
provenance: [],
transcripts: [
{
unitKey: 'orders',
path: 'transcripts/orders.json',
toolCallCount: 2,
errorCount: 1,
toolNames: ['sl_write_source'],
},
],
},
});
}
export function flaggedFallbackScenario(): MemoryFlowReplayInput {
return baseScenario({
runId: 'run-flagged-fallback',
events: baseScenario().events.map((event) =>
event.type === 'reconciliation_finished' ? { ...event, fallbackCount: 1 } : event,
),
});
}
export function postSaveSecretFailureScenario(): MemoryFlowReplayInput {
return baseScenario({
runId: 'run-post-save-failure',
status: 'error',
errors: ['index refresh failed https://example.com/private token=abc123'],
events: baseScenario().events.map((event) =>
event.type === 'saved' ? { ...event, commitSha: 'def456789012' } : event, // pragma: allowlist secret
),
});
}

View file

@ -0,0 +1,62 @@
import { describe, expect, it } from 'vitest';
import {
deletedRawPathsScenario,
flaggedFallbackScenario,
postSaveSecretFailureScenario,
successfulReplayScenario,
validationRevertScenario,
} from './acceptance-fixtures.js';
import { renderMemoryFlowReplay } from '../../../../src/context/ingest/memory-flow/render.js';
import { buildMemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/view-model.js';
function renderScenario(input = successfulReplayScenario(), terminalWidth = 140): string {
return renderMemoryFlowReplay(buildMemoryFlowViewModel(input), { terminalWidth });
}
describe('memory-flow acceptance scenarios', () => {
it('renders a completed replay with a clear saved-memory completion line', () => {
const output = renderScenario(successfulReplayScenario());
expect(output).toContain('KTX memory flow warehouse/metricflow done');
expect(output).toContain('Saved 3 memories from 4 raw files: 2 wiki pages, 1 SL updates.');
expect(output).toContain('Commit: abc12345 Run: run-success Report: ingest-report.json');
});
it('renders deleted raw paths as eviction candidates without listing every raw path by default', () => {
const output = renderScenario(deletedRawPathsScenario());
expect(output).toContain('2 deletions');
expect(output).toContain('Eviction candidates: 2');
expect(output).not.toContain('/full/local/path/private/orders-2024.sql');
});
it('renders invalid semantic-layer writes as reverted, not saved', () => {
const output = renderScenario(validationRevertScenario());
expect(output).toContain('orders reverted: semantic-layer validation failed for warehouse.orders');
expect(output).toContain('Invalid semantic-layer writes were not saved.');
expect(output).not.toContain('Saved 1 memories');
});
it('renders flagged fallbacks in gates details', () => {
const output = renderScenario(flaggedFallbackScenario());
expect(output).toContain('0 conflict, 1 fallback');
expect(output).toContain('Flagged fallbacks: 1');
});
it('renders no ANSI color codes in the text fallback for terminals without color support', () => {
const output = renderScenario(successfulReplayScenario(), 80);
expect(output).toContain('KTX memory flow warehouse/metricflow done');
expect(output).not.toMatch(/\u001b\[[0-9;]*m/);
});
it('redacts secrets in visible post-save failure text', () => {
const output = renderScenario(postSaveSecretFailureScenario());
expect(output).toContain('Post-save error: index refresh failed https://[redacted] token=[redacted]');
expect(output).not.toContain('abc123');
expect(output).not.toContain('https://example.com/private');
});
});

View file

@ -0,0 +1,332 @@
import { describe, expect, it } from 'vitest';
import type { LocalIngestRunRecord } from '../../../../src/context/ingest/local-stage-ingest.js';
import type { IngestReportSnapshot } from '../../../../src/context/ingest/reports.js';
import { ingestReportToMemoryFlowReplay, localIngestRunToMemoryFlowReplay } from '../../../../src/context/ingest/memory-flow/events.js';
function localRecord(): LocalIngestRunRecord {
return {
runId: 'local-run-1',
jobId: 'local-run-1',
status: 'done',
adapter: 'metricflow',
connectionId: 'warehouse',
sourceDir: '/tmp/source',
syncId: 'sync-1',
startedAt: '2026-04-30T10:00:00.000Z',
completedAt: '2026-04-30T10:00:01.000Z',
progress: 1,
done: true,
previousRunId: null,
diffSummary: { added: 2, modified: 1, deleted: 1, unchanged: 4 },
diffPaths: {
added: ['models/orders.yml', 'models/revenue.yml'],
modified: ['models/customers.yml'],
deleted: ['models/old.yml'],
unchanged: ['models/a.yml', 'models/b.yml', 'models/c.yml', 'models/d.yml'],
},
workUnitCount: 2,
rawFileCount: 7,
workUnits: [
{
unitKey: 'orders',
rawFiles: ['models/orders.yml'],
peerFileIndex: ['models/customers.yml'],
dependencyPaths: ['models/base.yml'],
},
{
unitKey: 'revenue',
rawFiles: ['models/revenue.yml'],
peerFileIndex: [],
dependencyPaths: [],
},
],
evictionDeletedRawPaths: ['raw-sources/warehouse/metricflow/sync-1/models/old.yml'],
errors: [],
};
}
function reportSnapshot(): IngestReportSnapshot {
return {
id: 'report-1',
runId: 'run-1',
jobId: 'job-1',
connectionId: 'warehouse',
sourceKey: 'lookml',
createdAt: '2026-04-30T10:00:02.000Z',
body: {
syncId: 'sync-2',
diffSummary: { added: 1, modified: 1, deleted: 0, unchanged: 3 },
commitSha: 'abc123456789', // pragma: allowlist secret
failedWorkUnits: ['customers'],
reconciliationSkipped: false,
conflictsResolved: [
{
kind: 'near_duplicate',
artifactKey: 'warehouse.orders',
detail: 'kept candidate definition',
flaggedForHuman: false,
},
],
evictionsApplied: [],
unmappedFallbacks: [{ rawPath: 'cards/42.json', reason: 'no_connection_mapping', fallback: 'flagged' }],
evictionInputs: [],
unresolvedCards: [],
supersededBy: null,
overrideOf: null,
provenanceRows: [
{
rawPath: 'views/orders.view.lkml',
artifactKind: 'wiki',
artifactKey: 'wiki/global/orders.md',
actionType: 'wiki_written',
},
{
rawPath: 'views/orders.view.lkml',
artifactKind: 'sl',
artifactKey: 'warehouse.orders',
actionType: 'measure_added',
},
{
rawPath: 'views/customers.view.lkml',
artifactKind: null,
artifactKey: null,
actionType: 'skipped',
},
],
toolTranscripts: [
{
unitKey: 'orders',
path: '/tmp/ktx/run/wu-transcripts/job-1/orders.jsonl',
toolCallCount: 3,
errorCount: 0,
toolNames: ['read_raw_span', 'wiki_write', 'sl_write_source'],
},
{
unitKey: 'customers',
path: '/tmp/ktx/run/wu-transcripts/job-1/customers.jsonl',
toolCallCount: 2,
errorCount: 1,
toolNames: ['read_raw_span', 'sl_write_source'],
},
],
workUnits: [
{
unitKey: 'orders',
rawFiles: ['views/orders.view.lkml'],
status: 'success',
actions: [
{ target: 'wiki', type: 'created', key: 'wiki/global/orders.md', detail: 'order facts' },
{ target: 'sl', type: 'updated', key: 'warehouse.orders', detail: 'order measures' },
],
touchedSlSources: [{ connectionId: 'warehouse', sourceName: 'warehouse.orders' }],
},
{
unitKey: 'customers',
rawFiles: ['views/customers.view.lkml'],
status: 'failed',
reason: 'semantic-layer validation failed',
actions: [{ target: 'sl', type: 'created', key: 'warehouse.customers', detail: 'invalid source' }],
touchedSlSources: [{ connectionId: 'warehouse', sourceName: 'warehouse.customers' }],
},
],
},
};
}
describe('memory-flow event mapping', () => {
it('maps a local ingest run to source, snapshot, diff, chunk, and report events', () => {
const replay = localIngestRunToMemoryFlowReplay(localRecord());
expect(replay).toMatchObject({
runId: 'local-run-1',
connectionId: 'warehouse',
adapter: 'metricflow',
status: 'done',
sourceDir: '/tmp/source',
syncId: 'sync-1',
plannedWorkUnits: [
{ unitKey: 'orders', rawFiles: ['models/orders.yml'], peerFileCount: 1, dependencyCount: 1 },
{ unitKey: 'revenue', rawFiles: ['models/revenue.yml'], peerFileCount: 0, dependencyCount: 0 },
],
});
expect(replay.events).toEqual([
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 7 },
{ type: 'scope_detected', fingerprint: null },
{ type: 'raw_snapshot_written', syncId: 'sync-1', rawFileCount: 7 },
{ type: 'diff_computed', added: 2, modified: 1, deleted: 1, unchanged: 4 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 2, evictionCount: 1 },
{ type: 'report_created', runId: 'local-run-1' },
]);
});
it('maps an ingest report snapshot to work-unit, candidate, gate, saved, provenance, and report events', () => {
const replay = ingestReportToMemoryFlowReplay(reportSnapshot(), { provenanceRowCount: 5 });
expect(replay).toMatchObject({
runId: 'run-1',
connectionId: 'warehouse',
adapter: 'lookml',
status: 'error',
sourceDir: null,
syncId: 'sync-2',
reportId: 'report-1',
plannedWorkUnits: [
{ unitKey: 'orders', rawFiles: ['views/orders.view.lkml'], peerFileCount: 0, dependencyCount: 0 },
{ unitKey: 'customers', rawFiles: ['views/customers.view.lkml'], peerFileCount: 0, dependencyCount: 0 },
],
});
expect(replay.events).toContainEqual({
type: 'candidate_action',
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/global/orders.md',
});
expect(replay.events).toContainEqual({
type: 'work_unit_finished',
unitKey: 'customers',
status: 'failed',
reason: 'semantic-layer validation failed',
});
expect(replay.events).toContainEqual({ type: 'reconciliation_finished', conflictCount: 1, fallbackCount: 1 });
expect(replay.events).toContainEqual({ type: 'saved', commitSha: 'abc123456789', wikiCount: 1, slCount: 2 }); // pragma: allowlist secret
expect(replay.events).toContainEqual({ type: 'provenance_recorded', rowCount: 5 });
expect(replay.events).toContainEqual({ type: 'report_created', runId: 'run-1', reportPath: 'report-1' });
expect(replay.details.actions).toEqual([
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/global/orders.md',
summary: 'order facts',
rawFiles: ['views/orders.view.lkml'],
status: 'success',
},
{
unitKey: 'orders',
target: 'sl',
action: 'updated',
key: 'warehouse.orders',
summary: 'order measures',
rawFiles: ['views/orders.view.lkml'],
status: 'success',
},
{
unitKey: 'customers',
target: 'sl',
action: 'created',
key: 'warehouse.customers',
summary: 'invalid source',
rawFiles: ['views/customers.view.lkml'],
status: 'failed',
},
]);
expect(replay.details.provenance).toEqual([
{
rawPath: 'views/orders.view.lkml',
artifactKind: 'wiki',
artifactKey: 'wiki/global/orders.md',
actionType: 'wiki_written',
},
{
rawPath: 'views/orders.view.lkml',
artifactKind: 'sl',
artifactKey: 'warehouse.orders',
actionType: 'measure_added',
},
{
rawPath: 'views/customers.view.lkml',
artifactKind: null,
artifactKey: null,
actionType: 'skipped',
},
]);
expect(replay.details.transcripts).toEqual([
{
unitKey: 'orders',
path: '/tmp/ktx/run/wu-transcripts/job-1/orders.jsonl',
toolCallCount: 3,
errorCount: 0,
toolNames: ['read_raw_span', 'wiki_write', 'sl_write_source'],
},
{
unitKey: 'customers',
path: '/tmp/ktx/run/wu-transcripts/job-1/customers.jsonl',
toolCallCount: 2,
errorCount: 1,
toolNames: ['read_raw_span', 'sl_write_source'],
},
]);
});
it('prefers captured memory-flow snapshots from report bodies', () => {
const report = reportSnapshot();
Object.assign(report.body, {
memoryFlow: {
metadata: {
schemaVersion: 1,
mode: 'full',
origin: 'captured',
timing: 'captured',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: null,
sourceReportPath: null,
fallbackReason: null,
},
runId: 'run-1',
connectionId: 'warehouse',
adapter: 'lookml',
status: 'running',
sourceDir: null,
syncId: 'sync-2',
errors: [],
plannedWorkUnits: [
{ unitKey: 'orders', rawFiles: ['views/orders.view.lkml'], peerFileCount: 1, dependencyCount: 2 },
],
details: { actions: [], provenance: [], transcripts: [] },
events: [
{
type: 'source_acquired',
adapter: 'lookml',
trigger: 'manual_resync',
fileCount: 1,
emittedAt: '2026-05-01T10:00:00.000Z',
},
],
},
});
const replay = ingestReportToMemoryFlowReplay(report);
expect(replay.metadata).toEqual({
schemaVersion: 1,
mode: 'full',
origin: 'captured',
timing: 'captured',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: 'report-1',
sourceReportPath: 'report-1',
fallbackReason: null,
});
expect(replay.status).toBe('error');
expect(replay.reportId).toBe('report-1');
expect(replay.reportPath).toBe('report-1');
expect(replay.events[0]).toMatchObject({ type: 'source_acquired', emittedAt: '2026-05-01T10:00:00.000Z' });
expect(replay.events).toContainEqual({ type: 'report_created', runId: 'run-1', reportPath: 'report-1' });
});
it('labels reconstructed report replays as synthetic when no captured snapshot exists', () => {
const replay = ingestReportToMemoryFlowReplay(reportSnapshot(), { provenanceRowCount: 5 });
expect(replay.metadata).toEqual({
schemaVersion: 1,
mode: 'full',
origin: 'synthetic-report',
timing: 'synthetic',
capturedAt: '2026-04-30T10:00:02.000Z',
sourceReportId: 'report-1',
sourceReportPath: 'report-1',
fallbackReason: 'report did not include captured memory-flow events',
});
});
});

View file

@ -0,0 +1,326 @@
import { describe, expect, it } from 'vitest';
import {
createInitialMemoryFlowInteractionState,
findMemoryFlowSearchMatches,
reduceMemoryFlowInteractionState,
selectMemoryFlowChip,
selectMemoryFlowColumn,
selectedMemoryFlowColumn,
selectedMemoryFlowDetails,
visibleMemoryFlowChips,
} from '../../../../src/context/ingest/memory-flow/interaction.js';
import type { MemoryFlowInteractionState, MemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/types.js';
function view(): MemoryFlowViewModel {
return {
title: 'KTX memory flow warehouse/metricflow running',
subtitle: 'Run run-1 Sync sync-1',
status: 'running',
activeLine: 'active: WorkUnit orders step 2/4',
selectedTitle: 'WORKUNITS',
selectedDetails: ['orders: 1 raw, 0 peers, 1 deps'],
completionLine: null,
trustIssues: [
{
id: 'flagged-fallbacks',
severity: 'warning',
title: 'Flagged fallbacks',
detail: '1 fallback needs review',
columnId: 'gates',
},
{
id: 'work-unit-failed:customers',
severity: 'failed',
title: 'WorkUnit failed',
detail: 'customers failed: semantic-layer validation failed',
columnId: 'workUnits',
targetLabel: 'customers',
},
],
details: {
actions: [
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/orders.md',
summary: 'order facts',
rawFiles: ['orders.yml'],
status: 'success',
},
],
provenance: [
{
rawPath: 'orders.yml',
artifactKind: 'wiki',
artifactKey: 'wiki/orders.md',
actionType: 'wiki_written',
},
],
transcripts: [
{
unitKey: 'customers',
path: '/tmp/transcripts/customers.jsonl',
toolCallCount: 2,
errorCount: 1,
toolNames: ['read_raw_span', 'sl_write_source'],
},
],
},
columns: [
{
id: 'source',
title: 'SOURCE',
status: 'complete',
headline: '2 raw files',
counters: ['sync sync-1', 'scope none'],
chips: [{ label: 'metricflow', status: 'complete' }],
details: ['Trigger: manual_resync', 'Adapter: metricflow'],
},
{
id: 'chunks',
title: 'CHUNKS',
status: 'complete',
headline: '2 chunks',
counters: ['+1 ~1 -0 =0', '0 deletions'],
chips: [{ label: 'orders', status: 'complete' }],
details: ['Work units planned: 2', 'Eviction candidates: 0'],
},
{
id: 'workUnits',
title: 'WORKUNITS',
status: 'active',
headline: '2 WUs',
counters: ['1 done', '1 failed', '1 active'],
chips: [
{ label: 'orders', status: 'complete', detail: '1 raw span' },
{ label: 'customers', status: 'failed', detail: 'semantic-layer validation failed' },
],
details: ['orders: 1 raw, 0 peers, 1 deps', 'customers: 1 raw, 0 peers, 0 deps'],
},
{
id: 'actions',
title: 'ACTIONS',
status: 'complete',
headline: '2 candidates',
counters: ['1 wiki', '1 SL'],
chips: [{ label: 'wiki/orders.md', status: 'complete' }],
details: ['wiki created: wiki/orders.md', 'sl updated: warehouse.orders'],
},
{
id: 'gates',
title: 'GATES',
status: 'warning',
headline: '0 conflict, 1 fallback',
counters: ['1 failed', '1 flagged'],
chips: [{ label: 'customers', status: 'failed' }],
details: ['Failed work units: 1', 'Flagged fallbacks: 1', 'customers: semantic-layer validation failed'],
},
{
id: 'saved',
title: 'SAVED',
status: 'complete',
headline: '2 memories',
counters: ['1 wiki', '1 SL', '2 provenance'],
chips: [{ label: 'abc12345', status: 'complete' }],
details: ['Commit: abc12345', 'Run: run-1', 'Report: report-1', 'Provenance rows: 2'],
},
],
};
}
describe('memory-flow interaction reducer', () => {
it('selects the active work-unit column by default', () => {
const state = createInitialMemoryFlowInteractionState(view());
expect(state).toEqual({
selectedColumnId: 'workUnits',
selectedChipIndex: 0,
expanded: false,
pane: 'overview',
filter: 'all',
search: { editing: false, query: '', matchIndex: 0 },
shouldQuit: false,
});
expect(selectedMemoryFlowColumn(view(), state).title).toBe('WORKUNITS');
});
it('moves between columns and clamps chip selection', () => {
let state = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'down', view());
state = reduceMemoryFlowInteractionState(state, 'down', view());
expect(state.selectedChipIndex).toBe(1);
state = reduceMemoryFlowInteractionState(state, 'right', view());
expect(state.selectedColumnId).toBe('actions');
expect(state.selectedChipIndex).toBe(0);
state = reduceMemoryFlowInteractionState(state, 'left', view());
expect(state.selectedColumnId).toBe('workUnits');
expect(state.selectedChipIndex).toBe(0);
});
it('selects a column directly for mouse-driven renderers', () => {
const initial = createInitialMemoryFlowInteractionState(view());
const selected = selectMemoryFlowColumn(view(), initial, 'actions');
expect(selected).toMatchObject({
selectedColumnId: 'actions',
selectedChipIndex: 0,
expanded: true,
shouldQuit: false,
});
expect(selectedMemoryFlowColumn(view(), selected).title).toBe('ACTIONS');
expect(selectedMemoryFlowDetails(view(), selected)).toContain('wiki created: wiki/orders.md');
});
it('selects and clamps a chip directly for mouse-driven renderers', () => {
const initial = createInitialMemoryFlowInteractionState(view());
const selected = selectMemoryFlowChip(view(), initial, 'workUnits', 99);
expect(selected).toMatchObject({
selectedColumnId: 'workUnits',
selectedChipIndex: 1,
expanded: true,
shouldQuit: false,
});
expect(selectedMemoryFlowDetails(view(), selected)).toContain(
'Selected chip: customers (semantic-layer validation failed)',
);
});
it('ignores direct selection of an unknown column', () => {
const initial = createInitialMemoryFlowInteractionState(view());
const selected = selectMemoryFlowColumn(view(), initial, 'missing' as never);
expect(selected).toEqual({ ...initial, shouldQuit: false });
});
it('toggles expansion, attention filtering, all panes, and quit', () => {
let state: MemoryFlowInteractionState = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'enter', view());
expect(state.expanded).toBe(true);
expect(selectedMemoryFlowDetails(view(), state)).toContain('orders: 1 raw, 0 peers, 1 deps');
state = reduceMemoryFlowInteractionState(state, 'filter', view());
expect(state.filter).toBe('failed_or_flagged');
expect(visibleMemoryFlowChips(selectedMemoryFlowColumn(view(), state), state)).toEqual([
{ label: 'customers', status: 'failed', detail: 'semantic-layer validation failed' },
]);
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('trust');
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('details');
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('log');
expect(selectedMemoryFlowDetails(view(), state)).toContain('WORKUNITS active: 2 WUs');
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('provenance');
expect(selectedMemoryFlowDetails(view(), state)).toContain(
'orders.yml -> wiki:wiki/orders.md (wiki_written)',
);
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('transcript');
expect(selectedMemoryFlowDetails(view(), state)).toContain(
'customers: 2 tool calls, 1 errors, tools read_raw_span, sl_write_source',
);
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('overview');
state = reduceMemoryFlowInteractionState(state, 'provenance', view());
expect(state.pane).toBe('provenance');
expect(selectedMemoryFlowDetails(view(), state)).toContain(
'orders.yml -> wiki:wiki/orders.md (wiki_written)',
);
state = reduceMemoryFlowInteractionState(state, 'transcript', view());
expect(state.pane).toBe('transcript');
expect(selectedMemoryFlowDetails(view(), state)).toContain(
'customers: 2 tool calls, 1 errors, tools read_raw_span, sl_write_source',
);
state = reduceMemoryFlowInteractionState(state, 'quit', view());
expect(state.shouldQuit).toBe(true);
});
it('shows trust issue details and filters chips using issue targets', () => {
let state: MemoryFlowInteractionState = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'tab', view());
expect(state.pane).toBe('trust');
expect(selectedMemoryFlowDetails(view(), state)).toEqual([
'FAILED WorkUnit failed: customers failed: semantic-layer validation failed',
'WARNING Flagged fallbacks: 1 fallback needs review',
]);
state = reduceMemoryFlowInteractionState(state, 'filter', view());
expect(visibleMemoryFlowChips(selectedMemoryFlowColumn(view(), state), state, view())).toEqual([
{ label: 'customers', status: 'failed', detail: 'semantic-layer validation failed' },
]);
});
it('searches across columns, trust issues, actions, provenance, and transcripts', () => {
const matches = findMemoryFlowSearchMatches(view(), 'customers');
expect(matches.map((match) => match.label)).toEqual([
'WORKUNITS > customers',
'GATES',
'Trust > WorkUnit failed',
'Transcript > customers',
]);
let state = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'search-start', view());
state = reduceMemoryFlowInteractionState(state, { type: 'search-input', value: 'customers' }, view());
expect(state.search).toEqual({
editing: true,
query: 'customers',
matchIndex: 0,
});
expect(state.selectedColumnId).toBe('workUnits');
expect(state.selectedChipIndex).toBe(1);
state = reduceMemoryFlowInteractionState(state, 'search-submit', view());
expect(state.search.editing).toBe(false);
});
it('cycles search matches forward and backward with wraparound', () => {
let state = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'search-start', view());
state = reduceMemoryFlowInteractionState(state, { type: 'search-input', value: 'customers' }, view());
expect(state.search).toEqual({ editing: true, query: 'customers', matchIndex: 0 });
expect(state.selectedColumnId).toBe('workUnits');
expect(state.selectedChipIndex).toBe(1);
state = reduceMemoryFlowInteractionState(state, 'search-next', view());
expect(state.search).toEqual({ editing: true, query: 'customers', matchIndex: 1 });
expect(state.selectedColumnId).toBe('gates');
expect(state.selectedChipIndex).toBe(0);
state = reduceMemoryFlowInteractionState(state, 'search-next', view());
expect(state.search).toEqual({ editing: true, query: 'customers', matchIndex: 2 });
expect(state.selectedColumnId).toBe('workUnits');
state = reduceMemoryFlowInteractionState(state, 'search-previous', view());
expect(state.search).toEqual({ editing: true, query: 'customers', matchIndex: 1 });
expect(state.selectedColumnId).toBe('gates');
state = reduceMemoryFlowInteractionState(state, 'search-previous', view());
state = reduceMemoryFlowInteractionState(state, 'search-previous', view());
expect(state.search).toEqual({ editing: true, query: 'customers', matchIndex: 3 });
expect(state.selectedColumnId).toBe('workUnits');
});
});

View file

@ -0,0 +1,177 @@
import { describe, expect, it } from 'vitest';
import { createInitialMemoryFlowInteractionState, reduceMemoryFlowInteractionState } from '../../../../src/context/ingest/memory-flow/interaction.js';
import { renderMemoryFlowInteractive } from '../../../../src/context/ingest/memory-flow/interactive-render.js';
import type { MemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/types.js';
function view(): MemoryFlowViewModel {
return {
title: 'KTX memory flow warehouse/metricflow done',
subtitle: 'Run run-1 Sync sync-1',
status: 'done',
activeLine: 'active: complete',
selectedTitle: 'WORKUNITS',
selectedDetails: ['orders: 1 raw, 0 peers, 1 deps'],
completionLine:
'Saved 2 memories from 2 raw files: 1 wiki pages, 1 SL updates. Commit: abc12345 Run: run-1 Report: report-1',
trustIssues: [
{
id: 'work-unit-failed:customers',
severity: 'failed',
title: 'WorkUnit failed',
detail: 'customers failed: validation reset',
columnId: 'workUnits',
targetLabel: 'customers',
},
{
id: 'flagged-fallbacks',
severity: 'warning',
title: 'Flagged fallbacks',
detail: '1 fallback needs review',
columnId: 'gates',
},
],
details: {
actions: [
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/orders.md',
summary: 'order facts',
rawFiles: ['orders.yml'],
status: 'success',
},
],
provenance: [
{
rawPath: 'orders.yml',
artifactKind: 'wiki',
artifactKey: 'wiki/orders.md',
actionType: 'wiki_written',
},
],
transcripts: [
{
unitKey: 'customers',
path: '/tmp/transcripts/customers.jsonl',
toolCallCount: 2,
errorCount: 1,
toolNames: ['read_raw_span', 'sl_write_source'],
},
],
},
columns: [
{
id: 'source',
title: 'SOURCE',
status: 'complete',
headline: '2 raw files',
counters: ['sync sync-1', 'scope none'],
chips: [{ label: 'metricflow', status: 'complete' }],
details: ['Trigger: manual_resync', 'Adapter: metricflow'],
},
{
id: 'chunks',
title: 'CHUNKS',
status: 'complete',
headline: '2 chunks',
counters: ['+1 ~1 -0 =0', '0 deletions'],
chips: [{ label: 'orders', status: 'complete' }],
details: ['Work units planned: 2', 'Eviction candidates: 0'],
},
{
id: 'workUnits',
title: 'WORKUNITS',
status: 'warning',
headline: '2 WUs',
counters: ['1 done', '1 failed', '0 active'],
chips: [
{ label: 'orders', status: 'complete', detail: '1 raw span' },
{ label: 'customers', status: 'failed', detail: 'validation reset' },
],
details: ['orders: 1 raw, 0 peers, 1 deps', 'customers: 1 raw, 0 peers, 0 deps'],
},
{
id: 'actions',
title: 'ACTIONS',
status: 'complete',
headline: '2 candidates',
counters: ['1 wiki', '1 SL'],
chips: [{ label: 'wiki/orders.md', status: 'complete' }],
details: ['wiki created: wiki/orders.md', 'sl updated: warehouse.orders'],
},
{
id: 'gates',
title: 'GATES',
status: 'warning',
headline: '0 conflict, 1 fallback',
counters: ['1 failed', '1 flagged'],
chips: [{ label: 'customers', status: 'failed' }],
details: ['Failed work units: 1', 'Flagged fallbacks: 1'],
},
{
id: 'saved',
title: 'SAVED',
status: 'complete',
headline: '2 memories',
counters: ['1 wiki', '1 SL', '2 provenance'],
chips: [{ label: 'abc12345', status: 'complete' }],
details: ['Commit: abc12345', 'Run: run-1', 'Report: report-1', 'Provenance rows: 2'],
},
],
};
}
describe('renderMemoryFlowInteractive', () => {
it('marks the selected column and selected chip in a wide layout', () => {
const state = createInitialMemoryFlowInteractionState(view());
const output = renderMemoryFlowInteractive(view(), state, { terminalWidth: 140 });
expect(output).toContain('KTX memory flow warehouse/metricflow done');
expect(output).toContain('OK SOURCE -> OK CHUNKS -> !! WORKUNITS -> OK ACTIONS -> !! GATES -> OK SAVED');
expect(output).toContain('[WORKUNITS]');
expect(output).toContain('> orders');
expect(output).toContain('Selected: WORKUNITS > orders');
expect(output).toContain('Pane: overview Filter: all');
expect(output).toContain('- Selected chip: orders (1 raw span)');
expect(output).toContain(
'Saved 2 memories from 2 raw files: 1 wiki pages, 1 SL updates. Commit: abc12345 Run: run-1 Report: report-1',
);
});
it('renders attention-filtered details in a narrow layout', () => {
let state = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'filter', view());
state = reduceMemoryFlowInteractionState(state, 'enter', view());
const output = renderMemoryFlowInteractive(view(), state, { terminalWidth: 72 });
expect(output).toContain('OK SOURCE -> OK CHUNKS -> !! WORKUNITS -> OK ACTIONS -> !! GATES -> OK SAVED');
expect(output).toContain('[WORKUNITS]');
expect(output).toContain('Filter: failed_or_flagged');
expect(output).toContain('> customers');
expect(output).toContain('- customers: 1 raw, 0 peers, 0 deps');
});
it('renders report-backed transcript detail pane rows', () => {
let state = createInitialMemoryFlowInteractionState(view());
state = reduceMemoryFlowInteractionState(state, 'down', view());
state = reduceMemoryFlowInteractionState(state, 'transcript', view());
const output = renderMemoryFlowInteractive(view(), state, { terminalWidth: 100 });
expect(output).toContain('Pane: transcript Filter: all');
expect(output).toContain('- customers: 2 tool calls, 1 errors, tools read_raw_span, sl_write_source');
});
it('keeps trust issues visible in the interactive renderer', () => {
const state = createInitialMemoryFlowInteractionState(view());
const output = renderMemoryFlowInteractive(view(), state, { terminalWidth: 140 });
expect(output).toContain('Trust issues');
expect(output).toContain('FAILED WorkUnit failed: customers failed: validation reset');
expect(output).toContain('WARNING Flagged fallbacks: 1 fallback needs review');
});
});

View file

@ -0,0 +1,91 @@
import { describe, expect, it, vi } from 'vitest';
import { createMemoryFlowLiveBuffer, sanitizeMemoryFlowError } from '../../../../src/context/ingest/memory-flow/live-buffer.js';
import type { MemoryFlowReplayInput } from '../../../../src/context/ingest/memory-flow/types.js';
function initialReplay(): MemoryFlowReplayInput {
return {
runId: 'live-run-1',
connectionId: 'warehouse',
adapter: 'fake',
status: 'running',
sourceDir: '/tmp/source',
syncId: 'pending',
errors: [],
events: [],
plannedWorkUnits: [],
details: { actions: [], provenance: [], transcripts: [] },
};
}
describe('createMemoryFlowLiveBuffer', () => {
it('emits immutable replay snapshots on every live change', () => {
const onChange = vi.fn();
const buffer = createMemoryFlowLiveBuffer(initialReplay(), { onChange });
buffer.emit({ type: 'source_acquired', adapter: 'fake', trigger: 'manual_resync', fileCount: 2 });
buffer.update({
syncId: 'sync-1',
plannedWorkUnits: [
{
unitKey: 'fake-orders',
rawFiles: ['orders.json'],
peerFileCount: 0,
dependencyCount: 0,
},
],
});
buffer.emit({ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 });
buffer.finish('done');
expect(onChange).toHaveBeenCalledTimes(4);
expect(buffer.snapshot()).toMatchObject({
runId: 'live-run-1',
status: 'done',
syncId: 'sync-1',
plannedWorkUnits: [{ unitKey: 'fake-orders' }],
});
expect(buffer.snapshot().events.map((event) => event.type)).toEqual(['source_acquired', 'chunks_planned']);
const staleSnapshot = onChange.mock.calls[1][0] as MemoryFlowReplayInput;
expect(staleSnapshot.details).toEqual({ actions: [], provenance: [], transcripts: [] });
staleSnapshot.events.push({ type: 'report_created', runId: 'mutated' });
expect(buffer.snapshot().events.map((event) => event.type)).toEqual(['source_acquired', 'chunks_planned']);
});
it('stamps live events with emittedAt without mutating caller events', () => {
const event = { type: 'source_acquired', adapter: 'fake', trigger: 'manual_resync', fileCount: 2 } as const;
const buffer = createMemoryFlowLiveBuffer(initialReplay(), {
now: () => new Date('2026-05-01T10:00:00.000Z'),
});
buffer.emit(event);
expect(event).not.toHaveProperty('emittedAt');
expect(buffer.snapshot().events).toEqual([
{
type: 'source_acquired',
adapter: 'fake',
trigger: 'manual_resync',
fileCount: 2,
emittedAt: '2026-05-01T10:00:00.000Z',
},
]);
});
it('marks failed runs with sanitized error messages', () => {
const onChange = vi.fn();
const buffer = createMemoryFlowLiveBuffer(initialReplay(), { onChange });
buffer.finish('error', [
sanitizeMemoryFlowError(
new Error('Connection failed for postgres://user:password@localhost:5432/db?api_key=abc password=secret'), // pragma: allowlist secret
),
]);
expect(buffer.snapshot()).toMatchObject({
status: 'error',
errors: ['Connection failed for postgres://[redacted] password=[redacted]'],
});
expect(onChange).toHaveBeenCalledTimes(1);
});
});

View file

@ -0,0 +1,114 @@
import { describe, expect, it } from 'vitest';
import type { MemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/types.js';
import { renderMemoryFlowReplay } from '../../../../src/context/ingest/memory-flow/render.js';
function view(): MemoryFlowViewModel {
return {
title: 'KTX memory flow warehouse/metricflow done',
subtitle: 'Run run-1 Sync sync-1',
status: 'done',
activeLine: 'active: complete',
selectedTitle: 'SOURCE',
selectedDetails: ['Trigger: manual_resync', 'Adapter: metricflow'],
completionLine:
'Saved 2 memories from 2 raw files: 1 wiki pages, 1 SL updates. Commit: abc12345 Run: run-1 Report: report-1',
trustIssues: [],
details: { actions: [], provenance: [], transcripts: [] },
columns: [
{
id: 'source',
title: 'SOURCE',
status: 'complete',
headline: '2 raw files',
counters: ['sync sync-1', 'scope none'],
chips: [{ label: 'metricflow', status: 'complete' }],
details: ['Trigger: manual_resync'],
},
{
id: 'chunks',
title: 'CHUNKS',
status: 'complete',
headline: '2 chunks',
counters: ['+1 ~1 -0 =3', '0 deletions'],
chips: [{ label: 'orders', status: 'complete' }],
details: ['Work units planned: 2'],
},
{
id: 'workUnits',
title: 'WORKUNITS',
status: 'warning',
headline: '2 WUs',
counters: ['1 done', '1 failed', '0 active'],
chips: [{ label: 'orders', status: 'complete' }],
details: ['orders: 1 raw, 1 peers, 1 deps'],
},
{
id: 'actions',
title: 'ACTIONS',
status: 'complete',
headline: '2 candidates',
counters: ['1 wiki', '1 SL'],
chips: [{ label: 'wiki/orders.md', status: 'complete' }],
details: ['wiki created: wiki/orders.md'],
},
{
id: 'gates',
title: 'GATES',
status: 'warning',
headline: '1 conflict, 1 fallback',
counters: ['1 failed', '1 flagged'],
chips: [{ label: 'customers', status: 'failed' }],
details: ['Failed work units: 1'],
},
{
id: 'saved',
title: 'SAVED',
status: 'complete',
headline: '2 memories',
counters: ['1 wiki', '1 SL', '3 provenance'],
chips: [{ label: 'abc12345', status: 'complete' }],
details: ['Commit: abc12345'],
},
],
};
}
describe('renderMemoryFlowReplay', () => {
it('renders a six-column wide terminal snapshot', () => {
expect(renderMemoryFlowReplay(view(), { terminalWidth: 140 })).toContain(
'OK SOURCE -> OK CHUNKS -> !! WORKUNITS -> OK ACTIONS -> !! GATES -> OK SAVED',
);
expect(renderMemoryFlowReplay(view(), { terminalWidth: 140 })).toMatchInlineSnapshot(`
"KTX memory flow warehouse/metricflow done
active: complete
Run run-1 Sync sync-1
OK SOURCE -> OK CHUNKS -> !! WORKUNITS -> OK ACTIONS -> !! GATES -> OK SAVED
SOURCE CHUNKS WORKUNITS ACTIONS GATES SAVED
2 raw files 2 chunks 2 WUs 2 candidates 1 conflict, 1 fallb 2 memories
sync sync-1 +1 ~1 -0 =3 1 done 1 wiki 1 failed 1 wiki
scope none 0 deletions 1 failed 1 SL 1 flagged 1 SL
Selected: SOURCE
- Trigger: manual_resync
- Adapter: metricflow
Saved 2 memories from 2 raw files: 1 wiki pages, 1 SL updates. Commit: abc12345 Run: run-1 Report: report-1
"
`);
});
it('renders a stacked narrow terminal snapshot', () => {
expect(renderMemoryFlowReplay(view(), { terminalWidth: 72 })).toContain(
'OK SOURCE -> OK CHUNKS -> !! WORKUNITS -> OK ACTIONS -> !! GATES -> OK SAVED',
);
expect(renderMemoryFlowReplay(view(), { terminalWidth: 72 })).toContain(`SOURCE
2 raw files
sync sync-1
scope none`);
expect(renderMemoryFlowReplay(view(), { terminalWidth: 72 })).toContain(`GATES
1 conflict, 1 fallback
1 failed
1 flagged`);
});
});

View file

@ -0,0 +1,165 @@
import { describe, expect, it } from 'vitest';
import {
memoryFlowReplayInputSchema,
memoryFlowStreamEventSchema,
parseMemoryFlowReplayInput,
} from '../../../../src/context/ingest/memory-flow/schema.js';
import type { MemoryFlowReplayInput } from '../../../../src/context/ingest/memory-flow/types.js';
function snapshot(overrides: Partial<MemoryFlowReplayInput> = {}): MemoryFlowReplayInput {
return {
runId: 'job-1',
connectionId: 'connection-1',
adapter: 'metabase',
status: 'running',
sourceDir: null,
syncId: 'sync-1',
errors: [],
events: [
{ type: 'source_acquired', adapter: 'metabase', trigger: 'manual_resync', fileCount: 2 },
{ type: 'scope_detected', fingerprint: 'scope-1' },
{ type: 'raw_snapshot_written', syncId: 'sync-1', rawFileCount: 2 },
{ type: 'diff_computed', added: 1, modified: 1, deleted: 0, unchanged: 0 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'stage_progress', stage: 'integration', percent: 80, message: 'Integrating 1/1 patches: orders' },
{ type: 'work_unit_started', unitKey: 'orders', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'work_unit_step', unitKey: 'orders', stepIndex: 1, stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'orders', target: 'wiki', action: 'created', key: 'wiki/orders.md' },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'success' },
{ type: 'reconciliation_finished', conflictCount: 0, fallbackCount: 0 },
{ type: 'saved', commitSha: 'abc12345', wikiCount: 1, slCount: 0 },
{ type: 'provenance_recorded', rowCount: 1 },
{ type: 'report_created', runId: 'run-1', reportPath: 'ingest-report.json' },
],
plannedWorkUnits: [{ unitKey: 'orders', rawFiles: ['orders.md'], peerFileCount: 0, dependencyCount: 1 }],
details: {
actions: [
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/orders.md',
summary: 'Created orders page',
rawFiles: ['orders.md'],
status: 'success',
},
],
provenance: [
{
rawPath: 'orders.md',
artifactKind: 'wiki',
artifactKey: 'wiki/orders.md',
actionType: 'wiki_written',
},
],
transcripts: [
{
unitKey: 'orders',
path: 'transcripts/orders.jsonl',
toolCallCount: 2,
errorCount: 0,
toolNames: ['wiki_write'],
},
],
},
...overrides,
};
}
describe('memory-flow schemas', () => {
it('parses a full replay input snapshot', () => {
expect(parseMemoryFlowReplayInput(snapshot())).toEqual(snapshot());
});
it('parses replay metadata and timestamped events', () => {
const parsed = parseMemoryFlowReplayInput(
snapshot({
metadata: {
schemaVersion: 1,
mode: 'full',
origin: 'captured',
timing: 'captured',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: 'report-1',
sourceReportPath: 'reports/report-1.json',
fallbackReason: null,
},
events: [
{
type: 'source_acquired',
adapter: 'metabase',
trigger: 'manual_resync',
fileCount: 2,
emittedAt: '2026-05-01T10:00:00.000Z',
},
],
}),
);
expect(parsed.metadata).toEqual({
schemaVersion: 1,
mode: 'full',
origin: 'captured',
timing: 'captured',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: 'report-1',
sourceReportPath: 'reports/report-1.json',
fallbackReason: null,
});
expect(parsed.events).toEqual([
{
type: 'source_acquired',
adapter: 'metabase',
trigger: 'manual_resync',
fileCount: 2,
emittedAt: '2026-05-01T10:00:00.000Z',
},
]);
});
it('parses skipped deterministic stages', () => {
const parsed = parseMemoryFlowReplayInput(
snapshot({
status: 'done',
events: [
{ type: 'source_acquired', adapter: 'live-database', trigger: 'demo_deterministic', fileCount: 7 },
{ type: 'scope_detected', fingerprint: 'sqlite' },
{ type: 'raw_snapshot_written', syncId: 'sync-demo', rawFileCount: 7 },
{ type: 'diff_computed', added: 7, modified: 0, deleted: 0, unchanged: 0 },
{ type: 'chunks_planned', chunkCount: 7, workUnitCount: 0, evictionCount: 0 },
{ type: 'stage_skipped', stage: 'workUnits', reason: 'deterministic mode' },
{ type: 'stage_skipped', stage: 'actions', reason: 'requires LLM' },
{ type: 'stage_skipped', stage: 'gates', reason: 'requires candidate actions' },
{ type: 'stage_skipped', stage: 'saved', reason: 'requires LLM memory synthesis' },
{ type: 'saved', commitSha: null, wikiCount: 0, slCount: 0 },
{ type: 'provenance_recorded', rowCount: 0 },
{
type: 'report_created',
runId: 'scan-demo',
reportPath: 'raw-sources/orbit_demo/live-database/sync-demo/scan-report.json',
},
],
}),
);
expect(parsed.events).toContainEqual({ type: 'stage_skipped', stage: 'workUnits', reason: 'deterministic mode' });
expect(parsed.events).toContainEqual({ type: 'stage_skipped', stage: 'actions', reason: 'requires LLM' });
});
it('parses snapshot and closed stream events', () => {
expect(memoryFlowStreamEventSchema.parse({ type: 'snapshot', snapshot: snapshot({ status: 'done' }) })).toEqual({
type: 'snapshot',
snapshot: snapshot({ status: 'done' }),
});
expect(memoryFlowStreamEventSchema.parse({ type: 'closed', status: 'done', errors: [] })).toEqual({
type: 'closed',
status: 'done',
errors: [],
});
});
it('rejects invalid replay status values', () => {
expect(() => memoryFlowReplayInputSchema.parse({ ...snapshot(), status: 'complete' })).toThrow();
});
});

View file

@ -0,0 +1,155 @@
import { describe, expect, it } from 'vitest';
import type { MemoryFlowReplayInput } from '../../../../src/context/ingest/memory-flow/types.js';
import { formatMemoryFlowFinalSummary } from '../../../../src/context/ingest/memory-flow/summary.js';
function input(overrides: Partial<MemoryFlowReplayInput> = {}): MemoryFlowReplayInput {
return {
runId: 'run-1',
connectionId: 'warehouse',
adapter: 'metricflow',
status: 'done',
sourceDir: '/tmp/source',
syncId: 'sync-1',
errors: [],
plannedWorkUnits: [{ unitKey: 'orders', rawFiles: ['orders.yml'], peerFileCount: 0, dependencyCount: 0 }],
details: { actions: [], provenance: [], transcripts: [] },
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 2 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'success' },
{ type: 'saved', commitSha: 'abc12345', wikiCount: 1, slCount: 1 },
{ type: 'provenance_recorded', rowCount: 2 },
{ type: 'report_created', runId: 'run-1', reportPath: 'report-1' },
],
...overrides,
};
}
describe('formatMemoryFlowFinalSummary', () => {
it('summarizes a successful full memory-flow run', () => {
expect(formatMemoryFlowFinalSummary(input())).toBe(
[
'Memory-flow summary: done',
'Connection: warehouse',
'Adapter: metricflow',
'Run: run-1',
'Sync: sync-1',
'Source files: 2',
'Table reviews: 1 total, 1 done, 0 failed',
'Saved memory: 1 wiki, 1 semantic layer',
'Provenance rows: 2',
'Report: report-1',
'',
].join('\n'),
);
});
it('includes trust issues and sanitized errors for failed runs', () => {
expect(
formatMemoryFlowFinalSummary(
input({
status: 'error',
errors: ['failed token=secret'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 2 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'failed', reason: 'validation failed token=secret' },
],
}),
),
).toContain('Trust issues: 3');
});
it('explains expired Notion authorization with fix suggestions', () => {
const rawReason =
'notion-cluster-1 failed: {"error":"invalid_grant","error_description":"reauth related error (invalid_rapt)","error_uri":"https://accounts.example/reauth"}';
const summary = formatMemoryFlowFinalSummary(
input({
connectionId: 'notion-main',
adapter: 'notion',
status: 'error',
events: [
{ type: 'source_acquired', adapter: 'notion', trigger: 'manual_resync', fileCount: 37 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 2, evictionCount: 0 },
{ type: 'work_unit_finished', unitKey: 'notion-cluster-1', status: 'failed', reason: rawReason },
],
}),
);
expect(summary).toContain('Memory-flow summary: error');
expect(summary).toContain(
'Notion authorization expired: notion-cluster-1 could not read Notion because the saved OAuth grant expired or requires reauthentication (invalid_grant / invalid_rapt).',
);
expect(summary).toContain('Fix suggestions:');
expect(summary).toContain(
'- Refresh the Notion token referenced by auth_token_ref for notion-main. If it uses env:NAME, export a fresh token in that variable; if it uses file:/path, replace that file.',
);
expect(summary).toContain(
'- Run ktx setup and reconfigure the Notion source to confirm page access, then rerun ktx ingest notion-main.',
);
expect(summary).not.toContain('error_uri');
});
it('labels replay source metadata in final summaries', () => {
const summary = formatMemoryFlowFinalSummary({
metadata: {
schemaVersion: 1,
mode: 'replay',
origin: 'packaged',
timing: 'captured',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: 'demo-replay-report',
sourceReportPath: 'replays/replay.memory-flow.v1.json',
fallbackReason: null,
},
runId: 'demo-replay-orbit',
connectionId: 'orbit_demo',
adapter: 'live-database',
status: 'done',
sourceDir: null,
syncId: 'demo-replay-sync',
reportPath: 'replays/replay.memory-flow.v1.json',
errors: [],
events: [
{ type: 'source_acquired', adapter: 'live-database', trigger: 'demo_replay', fileCount: 7 },
{ type: 'saved', commitSha: null, wikiCount: 3, slCount: 2 },
{ type: 'provenance_recorded', rowCount: 5 },
{ type: 'report_created', runId: 'demo-replay-orbit', reportPath: 'replays/replay.memory-flow.v1.json' },
],
plannedWorkUnits: [],
details: { actions: [], provenance: [], transcripts: [] },
});
expect(summary).toContain('Replay source: packaged replay (captured timing)');
expect(summary).toContain('Replay captured: 2026-05-01T10:00:03.000Z');
});
it('labels synthetic report replays with the reconstruction reason', () => {
const summary = formatMemoryFlowFinalSummary({
metadata: {
schemaVersion: 1,
mode: 'full',
origin: 'synthetic-report',
timing: 'synthetic',
capturedAt: '2026-05-01T10:00:03.000Z',
sourceReportId: 'report-1',
sourceReportPath: 'report-1',
fallbackReason: 'report did not include captured memory-flow events',
},
runId: 'run-1',
connectionId: 'warehouse',
adapter: 'lookml',
status: 'done',
sourceDir: null,
syncId: 'sync-1',
reportPath: 'report-1',
errors: [],
events: [{ type: 'report_created', runId: 'run-1', reportPath: 'report-1' }],
plannedWorkUnits: [],
details: { actions: [], provenance: [], transcripts: [] },
});
expect(summary).toContain('Replay source: synthetic report replay (synthetic timing)');
expect(summary).toContain('Replay note: report did not include captured memory-flow events');
});
});

View file

@ -0,0 +1,436 @@
import { describe, expect, it } from 'vitest';
import type { MemoryFlowReplayInput } from '../../../../src/context/ingest/memory-flow/types.js';
import { buildMemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/view-model.js';
function replayInput(): MemoryFlowReplayInput {
return {
runId: 'run-1',
connectionId: 'warehouse',
adapter: 'metricflow',
status: 'done',
sourceDir: '/tmp/source',
syncId: 'sync-1',
errors: [],
plannedWorkUnits: [
{ unitKey: 'orders', rawFiles: ['orders.yml'], peerFileCount: 1, dependencyCount: 1 },
{ unitKey: 'revenue', rawFiles: ['revenue.yml'], peerFileCount: 0, dependencyCount: 0 },
],
details: {
actions: [
{
unitKey: 'orders',
target: 'wiki',
action: 'created',
key: 'wiki/orders.md',
summary: 'order facts',
rawFiles: ['orders.yml'],
status: 'success',
},
{
unitKey: 'orders',
target: 'sl',
action: 'updated',
key: 'warehouse.orders',
summary: 'order measures',
rawFiles: ['orders.yml'],
status: 'success',
},
],
provenance: [
{
rawPath: 'orders.yml',
artifactKind: 'wiki',
artifactKey: 'wiki/orders.md',
actionType: 'wiki_written',
},
],
transcripts: [
{
unitKey: 'orders',
path: '/tmp/transcripts/orders.jsonl',
toolCallCount: 3,
errorCount: 0,
toolNames: ['read_raw_span', 'wiki_write', 'sl_write_source'],
},
],
},
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 2 },
{ type: 'scope_detected', fingerprint: 'scope-abc' },
{ type: 'raw_snapshot_written', syncId: 'sync-1', rawFileCount: 2 },
{ type: 'diff_computed', added: 1, modified: 1, deleted: 0, unchanged: 3 },
{ type: 'chunks_planned', chunkCount: 2, workUnitCount: 2, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'orders', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'orders', target: 'wiki', action: 'created', key: 'wiki/orders.md' },
{ type: 'candidate_action', unitKey: 'orders', target: 'sl', action: 'updated', key: 'warehouse.orders' },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'success' },
{ type: 'work_unit_finished', unitKey: 'revenue', status: 'failed', reason: 'validation failed' },
{ type: 'reconciliation_finished', conflictCount: 1, fallbackCount: 1 },
{ type: 'saved', commitSha: 'abc123456789', wikiCount: 1, slCount: 1 }, // pragma: allowlist secret
{ type: 'provenance_recorded', rowCount: 3 },
{ type: 'report_created', runId: 'run-1', reportPath: 'report-1' },
],
};
}
function baseReplayInput(overrides: Partial<MemoryFlowReplayInput> = {}): MemoryFlowReplayInput {
return {
runId: 'run-errors',
connectionId: 'warehouse',
adapter: 'metricflow',
status: 'error',
sourceDir: '/tmp/source',
syncId: 'sync-errors',
errors: [],
events: [],
plannedWorkUnits: [],
details: { actions: [], provenance: [], transcripts: [] },
...overrides,
};
}
describe('buildMemoryFlowViewModel', () => {
it('builds six readable columns from replay events', () => {
const view = buildMemoryFlowViewModel(replayInput());
expect(view.title).toBe('KTX memory flow warehouse/metricflow done');
expect(view.activeLine).toBe('active: complete');
expect(view.columns.map((column) => column.id)).toEqual([
'source',
'chunks',
'workUnits',
'actions',
'gates',
'saved',
]);
expect(view.columns.map((column) => column.headline)).toEqual([
'2 raw files',
'2 chunks',
'2 WUs',
'2 candidates',
'1 conflict, 1 fallback',
'2 memories',
]);
expect(view.columns.find((column) => column.id === 'workUnits')?.counters).toEqual([
'1 done',
'1 failed',
'0 active',
]);
expect(view.columns.find((column) => column.id === 'actions')?.counters).toEqual(['1 wiki', '1 SL']);
expect(view.details.actions).toHaveLength(2);
expect(view.details.provenance).toEqual([
{
rawPath: 'orders.yml',
artifactKind: 'wiki',
artifactKey: 'wiki/orders.md',
actionType: 'wiki_written',
},
]);
expect(view.details.transcripts).toEqual([
{
unitKey: 'orders',
path: '/tmp/transcripts/orders.jsonl',
toolCallCount: 3,
errorCount: 0,
toolNames: ['read_raw_span', 'wiki_write', 'sl_write_source'],
},
]);
expect(view.columns.find((column) => column.id === 'actions')?.details).toContain(
'orders wiki created wiki/orders.md: order facts',
);
expect(view.columns.find((column) => column.id === 'saved')?.details).toContain('Commit: abc12345');
expect(view.completionLine).toBe(
'Saved 2 memories from 2 raw files: 1 wiki pages, 1 SL updates. Commit: abc12345 Run: run-1 Report: report-1',
);
});
it('shows all seeded demo source families and sums raw files in the completion line', () => {
const view = buildMemoryFlowViewModel({
runId: 'demo-seeded-orbit',
connectionId: 'orbit_demo',
adapter: 'live-database',
status: 'done',
sourceDir: null,
syncId: 'demo-seeded-sync',
errors: [],
events: [
{ type: 'source_acquired', adapter: 'live-database', trigger: 'demo_seeded', fileCount: 8 },
{ type: 'source_acquired', adapter: 'dbt_descriptions', trigger: 'demo_seeded', fileCount: 6 },
{ type: 'source_acquired', adapter: 'looker', trigger: 'demo_seeded', fileCount: 7 },
{ type: 'source_acquired', adapter: 'notion', trigger: 'demo_seeded', fileCount: 8 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'revenue-and-contracts', skills: ['wiki_capture'], stepBudget: 40 },
{
type: 'candidate_action',
unitKey: 'revenue-and-contracts',
target: 'wiki',
action: 'created',
key: 'wiki/global/arr-contract-first.md',
},
{ type: 'work_unit_finished', unitKey: 'revenue-and-contracts', status: 'success' },
{ type: 'reconciliation_finished', conflictCount: 0, fallbackCount: 0 },
{ type: 'saved', commitSha: 'demo-seeded', wikiCount: 10, slCount: 6 },
{ type: 'provenance_recorded', rowCount: 23 },
{ type: 'report_created', runId: 'demo-seeded-orbit', reportPath: 'reports/seeded-demo-report.json' },
],
plannedWorkUnits: [
{ unitKey: 'revenue-and-contracts', rawFiles: ['contracts'], peerFileCount: 1, dependencyCount: 1 },
],
details: { actions: [], provenance: [], transcripts: [] },
});
expect(view.title).toBe('KTX memory flow Warehouse + dbt + BI + Docs done');
expect(view.columns.find((column) => column.id === 'source')?.counters[0]).toBe('Warehouse, dbt, BI, Docs');
expect(view.completionLine).toContain('Saved 16 memories from 29 raw files');
});
it('derives sticky trust issues from failed work units, gates, and provenance mismatch', () => {
const input = replayInput();
const view = buildMemoryFlowViewModel({
...input,
events: [
...input.events.filter((event) => event.type !== 'provenance_recorded'),
{ type: 'provenance_recorded', rowCount: 1 },
],
});
expect(view.trustIssues).toEqual([
{
id: 'work-unit-failed:revenue',
severity: 'failed',
title: 'WorkUnit failed',
detail: 'revenue failed: validation failed',
columnId: 'workUnits',
targetLabel: 'revenue',
},
{
id: 'sl-validation-reverted:revenue',
severity: 'warning',
title: 'SL validation revert',
detail: 'revenue reverted after semantic-layer validation failure',
columnId: 'gates',
targetLabel: 'revenue',
},
{
id: 'reconciliation-conflicts',
severity: 'warning',
title: 'Reconciliation conflicts',
detail: '1 conflict resolved during reconciliation',
columnId: 'gates',
},
{
id: 'flagged-fallbacks',
severity: 'warning',
title: 'Flagged fallbacks',
detail: '1 fallback needs review',
columnId: 'gates',
},
{
id: 'provenance-mismatch',
severity: 'warning',
title: 'Provenance mismatch',
detail: '2 saved memories but 1 provenance rows recorded',
columnId: 'saved',
},
]);
expect(view.columns.find((column) => column.id === 'workUnits')?.chips).toContainEqual({
label: 'revenue',
status: 'failed',
detail: 'validation failed',
});
});
it('accepts multiple provenance rows per saved memory', () => {
const input = replayInput();
const view = buildMemoryFlowViewModel({
...input,
events: [
...input.events.filter((event) => event.type !== 'provenance_recorded'),
{ type: 'provenance_recorded', rowCount: 23 },
],
});
expect(view.trustIssues.find((issue) => issue.id === 'provenance-mismatch')).toBeUndefined();
});
it('derives deterministic mode as a degraded trust issue', () => {
const view = buildMemoryFlowViewModel({
runId: 'demo-deterministic-scan',
connectionId: 'orbit_demo',
adapter: 'live-database',
status: 'done',
sourceDir: 'raw-sources/orbit_demo/live-database/sync-demo',
syncId: 'sync-demo',
reportPath: 'raw-sources/orbit_demo/live-database/sync-demo/scan-report.json',
errors: [],
plannedWorkUnits: [],
details: { actions: [], provenance: [], transcripts: [] },
events: [
{ type: 'source_acquired', adapter: 'live-database', trigger: 'demo_deterministic', fileCount: 7 },
{ type: 'chunks_planned', chunkCount: 7, workUnitCount: 0, evictionCount: 0 },
{ type: 'stage_skipped', stage: 'workUnits', reason: 'deterministic mode' },
{ type: 'stage_skipped', stage: 'actions', reason: 'requires LLM' },
{ type: 'stage_skipped', stage: 'gates', reason: 'requires candidate actions' },
{ type: 'stage_skipped', stage: 'saved', reason: 'requires LLM memory synthesis' },
],
});
expect(view.trustIssues).toEqual([
{
id: 'degraded-mode:workUnits',
severity: 'warning',
title: 'Degraded mode',
detail: 'WORKUNITS skipped: deterministic mode',
columnId: 'workUnits',
targetLabel: 'skipped',
},
{
id: 'degraded-mode:actions',
severity: 'warning',
title: 'Degraded mode',
detail: 'ACTIONS skipped: requires LLM',
columnId: 'actions',
targetLabel: 'skipped',
},
{
id: 'degraded-mode:gates',
severity: 'warning',
title: 'Degraded mode',
detail: 'GATES skipped: requires candidate actions',
columnId: 'gates',
targetLabel: 'skipped',
},
{
id: 'degraded-mode:saved',
severity: 'warning',
title: 'Degraded mode',
detail: 'SAVED skipped: requires LLM memory synthesis',
columnId: 'saved',
targetLabel: 'skipped',
},
]);
});
it('keeps local planning-only runs honest about unsaved memory', () => {
const view = buildMemoryFlowViewModel({
runId: 'local-run-1',
connectionId: 'warehouse',
adapter: 'fake',
status: 'done',
sourceDir: '/tmp/source',
syncId: 'sync-local',
errors: [],
plannedWorkUnits: [{ unitKey: 'orders', rawFiles: ['orders.json'], peerFileCount: 0, dependencyCount: 0 }],
details: { actions: [], provenance: [], transcripts: [] },
events: [
{ type: 'source_acquired', adapter: 'fake', trigger: 'manual_resync', fileCount: 1 },
{ type: 'scope_detected', fingerprint: null },
{ type: 'raw_snapshot_written', syncId: 'sync-local', rawFileCount: 1 },
{ type: 'diff_computed', added: 1, modified: 0, deleted: 0, unchanged: 0 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'report_created', runId: 'local-run-1' },
],
});
expect(view.columns.find((column) => column.id === 'actions')?.headline).toBe('0 candidates');
expect(view.columns.find((column) => column.id === 'gates')?.headline).toBe('not run');
expect(view.columns.find((column) => column.id === 'saved')?.headline).toBe('not saved');
expect(view.completionLine).toBe(null);
});
it('surfaces a sanitized source acquisition error when no source event exists', () => {
const view = buildMemoryFlowViewModel(
baseReplayInput({
errors: ['failed to read https://example.com/source?token=abc123 password=hunter2'],
}),
);
expect(view.activeLine).toBe('active: source failed - failed to read https://[redacted] password=[redacted]');
expect(view.selectedTitle).toBe('SOURCE');
expect(view.selectedDetails).toContain('Source acquisition failed: failed to read https://[redacted] password=[redacted]');
});
it('surfaces a sanitized planning error after source acquisition but before chunks', () => {
const view = buildMemoryFlowViewModel(
baseReplayInput({
errors: ['adapter detection failed api_key=abc123'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 3 },
{ type: 'raw_snapshot_written', syncId: 'sync-errors', rawFileCount: 3 },
],
}),
);
expect(view.activeLine).toBe('active: planning failed - adapter detection failed api_key=[redacted]');
const source = view.columns.find((column) => column.id === 'source');
expect(source?.details).toContain('Error: adapter detection failed api_key=[redacted]');
});
it('labels failed semantic-layer WorkUnits as reverted in gates details', () => {
const view = buildMemoryFlowViewModel(
baseReplayInput({
status: 'error',
errors: ['semantic-layer validation failed for warehouse.orders'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 2 },
{ type: 'raw_snapshot_written', syncId: 'sync-errors', rawFileCount: 2 },
{ type: 'diff_computed', added: 2, modified: 0, deleted: 0, unchanged: 0 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'orders', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'candidate_action', unitKey: 'orders', target: 'sl', action: 'updated', key: 'warehouse.orders' },
{
type: 'work_unit_finished',
unitKey: 'orders',
status: 'failed',
reason: 'semantic-layer validation failed for warehouse.orders',
},
],
plannedWorkUnits: [{ unitKey: 'orders', rawFiles: ['orders.yml'], peerFileCount: 0, dependencyCount: 0 }],
}),
);
const gates = view.columns.find((column) => column.id === 'gates');
expect(gates?.details).toContain('orders reverted: semantic-layer validation failed for warehouse.orders');
expect(gates?.details).toContain('Invalid semantic-layer writes were not saved.');
});
it('keeps non-validation WorkUnit failures actionable', () => {
const view = buildMemoryFlowViewModel(
baseReplayInput({
status: 'error',
errors: ['agent step budget exhausted'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 1 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_started', unitKey: 'docs', skills: ['wiki_capture'], stepBudget: 40 },
{ type: 'work_unit_finished', unitKey: 'docs', status: 'failed', reason: 'agent step budget exhausted' },
],
plannedWorkUnits: [{ unitKey: 'docs', rawFiles: ['docs.md'], peerFileCount: 0, dependencyCount: 0 }],
}),
);
const gates = view.columns.find((column) => column.id === 'gates');
expect(gates?.details).toContain('docs failed: agent step budget exhausted');
});
it('shows whether durable memory landed before a post-save failure', () => {
const view = buildMemoryFlowViewModel(
baseReplayInput({
status: 'error',
errors: ['index refresh failed token=abc123'],
events: [
{ type: 'source_acquired', adapter: 'metricflow', trigger: 'manual_resync', fileCount: 2 },
{ type: 'chunks_planned', chunkCount: 1, workUnitCount: 1, evictionCount: 0 },
{ type: 'work_unit_finished', unitKey: 'orders', status: 'success' },
{ type: 'reconciliation_finished', conflictCount: 0, fallbackCount: 0 },
{ type: 'saved', commitSha: 'abc123456789', wikiCount: 1, slCount: 1 }, // pragma: allowlist secret
],
}),
);
const saved = view.columns.find((column) => column.id === 'saved');
expect(saved?.details).toContain('Durable memory landed before failure.');
expect(saved?.details).toContain('Post-save error: index refresh failed token=[redacted]');
expect(view.activeLine).toBe('active: save failed - index refresh failed token=[redacted]');
});
});

View file

@ -0,0 +1,70 @@
import { describe, expect, it } from 'vitest';
import {
buildMemoryFlowVisualModel,
memoryFlowStatusBadge,
renderMemoryFlowConnectorLine,
} from '../../../../src/context/ingest/memory-flow/visuals.js';
import type { MemoryFlowViewModel } from '../../../../src/context/ingest/memory-flow/types.js';
function viewWithStatuses(statuses: Array<'waiting' | 'active' | 'complete' | 'warning' | 'failed'>): MemoryFlowViewModel {
const titles = ['SOURCE', 'CHUNKS', 'WORKUNITS', 'ACTIONS', 'GATES', 'SAVED'];
const ids = ['source', 'chunks', 'workUnits', 'actions', 'gates', 'saved'] as const;
return {
title: 'KTX memory flow warehouse/metricflow running',
subtitle: 'Run run-1 Sync sync-1',
status: 'running',
activeLine: 'active: WorkUnit orders',
selectedTitle: 'WORKUNITS',
selectedDetails: ['orders: 1 raw, 0 peers, 1 deps'],
completionLine: null,
trustIssues: [],
details: { actions: [], provenance: [], transcripts: [] },
columns: statuses.map((status, index) => ({
id: ids[index],
title: titles[index],
status,
headline: `${titles[index].toLowerCase()} headline`,
counters: [],
chips: [],
details: [],
})),
};
}
describe('memory-flow visual helpers', () => {
it('uses ASCII badges with text meaning for every status', () => {
expect(memoryFlowStatusBadge('waiting')).toEqual({ label: '..', text: 'waiting' });
expect(memoryFlowStatusBadge('active')).toEqual({ label: '>>', text: 'active' });
expect(memoryFlowStatusBadge('complete')).toEqual({ label: 'OK', text: 'complete' });
expect(memoryFlowStatusBadge('warning')).toEqual({ label: '!!', text: 'warning' });
expect(memoryFlowStatusBadge('failed')).toEqual({ label: 'XX', text: 'failed' });
});
it('renders a no-color connector line with status badges and six columns', () => {
const view = viewWithStatuses(['complete', 'complete', 'active', 'waiting', 'waiting', 'waiting']);
expect(renderMemoryFlowConnectorLine(view)).toBe(
'OK SOURCE -> OK CHUNKS -> >> WORKUNITS -> .. ACTIONS -> .. GATES -> .. SAVED',
);
});
it('moves the pulse to the active column, then warnings, failures, and the last completed column', () => {
expect(
buildMemoryFlowVisualModel(viewWithStatuses(['complete', 'complete', 'active', 'waiting', 'waiting', 'waiting']))
.pulseColumnId,
).toBe('workUnits');
expect(
buildMemoryFlowVisualModel(viewWithStatuses(['complete', 'warning', 'complete', 'waiting', 'waiting', 'waiting']))
.pulseColumnId,
).toBe('chunks');
expect(
buildMemoryFlowVisualModel(viewWithStatuses(['complete', 'complete', 'failed', 'waiting', 'waiting', 'waiting']))
.pulseColumnId,
).toBe('workUnits');
expect(
buildMemoryFlowVisualModel(viewWithStatuses(['complete', 'complete', 'complete', 'complete', 'waiting', 'waiting']))
.pulseColumnId,
).toBe('actions');
});
});