mirror of
https://github.com/Kaelio/ktx.git
synced 2026-07-01 08:59:39 +02:00
* docs: add spider2-specs handoff directory for benchmark-driven feature specs
* feat(cli): connection-scoped wiki pages
Add an optional `connections` frontmatter field so database-specific wiki
knowledge can be scoped to a connection without polluting searches about other
databases, while page keys stay a flat, globally-unique namespace.
- connections: single string or list; absent/empty ⇒ unscoped (applies to all)
- wiki_search (MCP) and `ktx wiki --connection` return unscoped ∪ matching
pages, filtered at the disk-load seam so all three search lanes draw their
candidate pool from the already-scoped set (not a post-filter)
- wiki_write accepts connections with REPLACE semantics and rejects a
connection-scoped write whose key collides with a disjoint-connection page
(data-loss guard; hard error, no silent clobber)
- explicit connection-id args (wiki_search, memory_ingest, ktx wiki) are
validated against ktx.yaml via a shared assertConfiguredConnectionId, which
also closes the prior gap where memory_ingest's connectionId was unvalidated;
persisted ids absent from config warn (not fail) in `ktx status`
- prompt guidance in the wiki_capture skill and external-ingest prompt; the
session connectionId is surfaced to the memory agent and ingest work units
Implements spider2-specs/specs/01-connection-scoped-wiki.md; intake draft moved
to spider2-specs/done/.
* docs(spider2-specs): add specs/ refinement stage and composite-key join spec
Describe the todo/ → specs/ → done/ pipeline in the README (refined specs are
the durable artifact; intake drafts move to done/ on ship) and add a
MEDIUM-priority spec for multi-column composite-key join detection found during
the first sqlite smoke test.
* feat(cli): add --verbatim ingest mode for authoritative documents
Store each --text/--file document body unchanged as a GLOBAL wiki page
instead of routing it through the memory agent, which may rewrite,
condense, or re-title it. The LLM derives only metadata (summary, tags,
sl_refs) and only for frontmatter fields the document does not already
set; the stored body is written by code and never edited.
- Deterministic page key: files derive it from the filename, inline
text from its leading Markdown heading (headless inline text is
rejected — pass it as --file instead).
- Idempotent: re-running the same body is a no-op; a different body at
the same key fails loudly rather than overwriting.
- Works with llm.provider.backend: none, deriving a degraded summary
from the heading or first sentence.
- Existing frontmatter (including unmodeled fields like effective_date)
passes through untouched; --connection-id scopes the page.
* feat(cli): SQL-authoring craft and per-dialect notes tool for the analytics skill
Spec 07: add a dialect-agnostic <sql_craft> block to the ktx-analytics skill (schema discovery, composition, window-function correctness, numeric precision, answer completeness) with one worked window-then-filter example. Workflow steps gain pointers into it; existing guidance is unchanged.
Spec 08: add a read-only sql_dialect_notes MCP tool returning a connection's engine SQL conventions (FQTN form, identifier quoting/case, date/time, top-N idiom, JSON access), resolved through the existing sqlAnalysisDialectForDriver path. Notes are per-dialect markdown files under context/sql-analysis/dialects, served by the tool and copied to dist (package-internal, never installed). Non-SQL connections return a clear KtxExpectedError. The flat skill gains a one-line pointer to the tool.
Both spider2-specs intake drafts move to done/ with implementation notes.
* feat(cli): tolerate objects that fail introspection during scan
Isolate per-object introspection failures so one broken or inaccessible object no longer zeroes out a connection's whole semantic layer: the sqlite and bigquery connectors introspect each object defensively (tryIntrospectObject), the live-database adapter records a scan outcome and fetch report, and enabled_tables accepts catalog.db.name, db.name, or bare names with a clear no-match error. Includes matching ktx-daemon introspection changes, docs, and tests.
* docs(spider2-specs): add 06-scan-tolerate-broken-objects spec
* feat(cli): generalize analytics fan-out rule to multi-hop join chains
The ktx-analytics skill's fan-out rule only reliably caught single-hop
inflation; agents still silently fanned out on multi-hop chains where the
offending one-to-many join sits several hops below the SUM/COUNT and is easy
to miss.
Rewrite the Composition rule so the danger reads as cumulative across the whole
chain (pre-aggregate per measure-owning table), add an affirmative
grain-verification habit (default: pre-aggregate to grain; escape hatch:
COUNT(DISTINCT key) for pure counts only; SUM/AVG of a fanned-out measure must
pre-aggregate), and add one generic wrong-vs-right worked example. Content-only
and dialect-agnostic; no new tool, flag, or config.
Implements spider2-specs/specs/09 and annotates spec 07's one-example
constraint as superseded.
* feat(cli): add panel-completeness, time-series window, and text-encoded numeric SQL craft
Extend the analytics skill's <sql_craft> with three correctness habits and
route the dialect-specific halves through sql_dialect_notes:
- Panel completeness (spec 10): full-domain spine -> LEFT JOIN -> COALESCE for
"each/every/all/per" questions, defaulted by measure additivity.
- Time-series windows (spec 11): explicit cumulative frames, calendar-range
rolling windows with minimum-periods guards, and period-over-period via LAG.
- Text-encoded numerics (spec 12): sample distinct values, strip/scale/cast in
one early CTE, and confirm coverage with a failure-detecting cast.
Add per-dialect Series, Rolling window, and Safe cast notes to all seven
dialect files so the skill stays dialect-agnostic while the engine-specific
syntax lives in sql_dialect_notes. Tests updated and passing (19).
* docs(spider2-specs): add specs 10-12 for analytics SQL-craft additions
Refined specs and completion records for the panel-completeness spine (10),
time-series window recipes (11), and text-encoded numeric parsing (12)
implemented in the preceding commit.
* docs(spider2-specs): add backlog intake drafts 13-14
- 13: canonical authoritative-source measures
- 14: output-completeness final check
* skill(analytics): spec 14 output-completeness + iter1 (active column planning)
Bundles two changes (entangled in SKILL.md; future spider2 iterations land as
separate commits):
- spec 14 (output-completeness): multi-part "answer every requested output" rule
+ a "Final completeness check" in workflow Step 6 and <sql_craft>; analytics
skill-content test updated; intake draft -> done/, refined spec added.
- iter1 experiment: spec 14's passive end-check did not change behavior on the
benchmark's output-completeness failures, so (a) the Plan step now writes the
exact output-column list UP FRONT as a contract the final SELECT must match,
and (b) "expose identity" -> "project BOTH the entity id and its name" (covers
both omission directions). All generic craft.
Driven by the Spider 2.0-Lite failure analysis (incomplete output was the
largest failure bucket); benchmark only as motivation.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* skill(analytics): iter2 — deterministic order in string/array aggregation
GROUP_CONCAT/string_agg/array_agg element order is undefined without an explicit
ORDER BY; also note SQLite's default text sort is binary/case-sensitive (uppercase
before lowercase) vs case-insensitive (COLLATE NOCASE). Generic SQLite craft.
Spider 2.0-Lite motivation: an ordered-ingredient-list question failed only on the
within-string element order (right elements, wrong order); benchmark as motivation only.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* feat(mcp): structured, leveled logging for the MCP server
Add one synchronous pino logger per MCP server process, written through the
io.stderr sink: plain JSON when stderr is not a TTY, colorized pino-pretty
(sync, in-process) when it is. Every tool call logs tool.start with its raw
params BEFORE the handler runs and tool.end after (info / warn past
KTX_MCP_SLOW_TOOL_MS / error), correlated by callId plus sessionId, so a
runaway sql_execution leaves a recoverable start line with its exact SQL and
no matching end. HTTP logs session.open/close and wires the previously-dead
transport.onerror to transport.error; stdio routes its transport error
through the logger. Level via KTX_MCP_LOG_LEVEL (default info). Existing
mcp_request_completed telemetry and registerParsedTool are unchanged; no
worker/async transport and no redaction in v1 (logs are local-only).
Implements spider2-specs/specs/15-mcp-server-structured-logging.md and moves
the intake draft to done/.
* feat(mcp): report uptimeMs in MCP server /health
The /health endpoint now includes uptimeMs (monotonic elapsed time since
the server started), mirroring the Python daemon's uptime_ms telemetry
field.
* feat(cli): bound read-query execution with a per-connection deadline
Enforce one shared query deadline (default 30s, overridable per connection via
query_timeout_ms) on every executeReadOnly path, so an accidentally-expensive
LLM-authored query returns a fast "query exceeded Ns" KtxQueryError instead of
hanging the MCP server.
- New shared contract context/connections/query-deadline.ts
(resolveQueryDeadlineMs, queryDeadlineExceededError); query_timeout_ms added to
the shared warehouse schema; BigQuery's job_timeout_ms removed.
- SQLite runs the read query in a short-lived forked child process and enforces
the deadline with SIGKILL. worker_threads + terminate() was tried first but
cannot interrupt a synchronous better-sqlite3 scan (the native loop never
yields); SIGKILL reclaims the process in ~2ms and keeps the event loop free.
- Remote connectors apply a real server-side statement timeout and re-wrap their
own timeout signal as KtxQueryError: Postgres statement_timeout/57014, MySQL
max_execution_time/3024, Snowflake STATEMENT_TIMEOUT_IN_SECONDS/604, ClickHouse
max_execution_time + aligned request_timeout/159, SQL Server requestTimeout/
ETIMEOUT, BigQuery jobTimeoutMs.
- Relationship validation skips a candidate to review on a deadline timeout
instead of aborting the pass; the deadline surfaces through the existing MCP
pino logger as a matched tool.start/tool.end(error) pair (no new logging code).
Also fixes a pre-existing, unrelated invalid cast in mcp-server-factory.test.ts
that was breaking tsc -p tsconfig.test.json.
* docs(spider2-specs): mark spec 16 (bounded query execution) done
Append Implementation notes to the refined spec (what shipped, where, and the
worker-thread -> child-process+SIGKILL deviation with its evidence) and move the
intake draft from todo/ to done/.
* skill(analytics): iter3 — measure-as-amount, inter-event gap, top-per-metric career
Three generic interpretation rules: a named business measure (sales/revenue/spend)
means its amount not a row count; "inter-event duration/gap" is LAG/LEAD time-between
events not a magnitude column; "highest across several achievements" aggregates per
metric over the whole history. All three demonstrably FIRE (verified on local008/003/152
SQL). local008 flips to correct (mechanism-aligned). 003/152 still fail on a different
axis (source-column / grouping). Generic craft; benchmark only as motivation.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* skill(analytics): spine-for-extreme-selection + aggregate-over-selected-set
Two generic answer-completeness refinements:
- Selecting the extreme group (lowest/highest count over a period/category
domain) must rank over the COMPLETE spine, not only groups with fact rows —
an empty period is a genuine 0 and often the true minimum.
- An aggregate scoped to a per-entity selected set ('avg revenue per actor in
those top-3 films') is computed ACROSS that set, distinct from the per-item
value; project both.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): iter2 — sharpen extreme-selection spine + top-N ranking-measure
- spine-for-extreme: concrete cue that a zero-row period never appears in a
GROUP BY of the facts; generate the full calendar, LEFT JOIN, COALESCE, then rank.
- aggregate-over-selected-set: top-N selection ranks by the named ranking measure
(the item's own revenue), independent of the per-item share that feeds the aggregate.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): iter3 — comparison-between-two-extremes is one wide row
Distinguishes a cross-item comparison ('the difference between the highest and
lowest month' -> single wide row, both extremes side by side + the comparison
column) from 'report a metric for each group' (-> stays long). Generic, question-
derived; targets the wide-vs-long shape gap without affecting per-group long output.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): iter4 — anchor a period bucket to the named lifecycle event
When a record carries multiple lifecycle timestamps (created/placed, approved,
shipped, delivered, completed, settled) and the question counts/measures records
in a named *completed state* by period ("delivered orders by month", "shipped
items per week"), bucket the period by that named event's own timestamp, not the
record-creation timestamp; the state value is the qualifying filter, the matching
timestamp is the time anchor. Wording priority is explicit — purchased/placed/
created/submitted/ordered keep the start-event timestamp — and a non-temporal
state filter (counts by customer/city/seller with no period) introduces no anchor.
Generic analytics craft: counting completed-state records by their creation date
silently answers "records that later reached that state, grouped by when they
started" instead of the question asked. Surfaced via the spider2-autofix loop;
FAIR_PRODUCT (adversary-screened, restatable from question wording + schema/
semantic-layer lifecycle descriptions, no gold dependency).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): iter5 — canonicalize observed URL-path variants before page-level analysis
When a question groups/filters/sequences web pages by a path/url column, sample
its distinct values; if the data itself shows /route and /route/ variants for the
same page context, canonicalize in an early CTE (preserve / as root, strip trailing
slashes from non-root paths, map an observed empty path to / only when the column is
a URL path with blank root-page events) and use the canonical path everywhere above.
Explicitly forbids inventing aliases the data doesn't show: no merging different
route names, no stripping query/fragment/host/scheme, no lowercasing, and no
canonicalization when the question asks for raw URL/path or slash-vs-no-slash diffs.
Generic web-analytics craft: raw request logs routinely store the same user-visible
page with and without a trailing slash, so grouping raw labels silently splits one
page into several. Surfaced via the spider2-autofix loop (Codex runner, round r2);
FAIR_PRODUCT (adversary-screened, restatable from URL-path semantics + page-grain
question wording + solver-observed distinct values, no gold dependency). The rule
fired mechanism-aligned on both targets; flipped local330 (landing/exit page counts),
local331 residual is a separate sequence-semantics axis beyond canonicalization.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): iter6 — coverage over a selected group is a set-membership aggregate
When a question first selects a group of entities ("the top 5 actors", "these
products") and then asks what count/share/percentage of a DIFFERENT subject domain
relates to *these* selected entities ("what % of customers rented films featuring
these actors"), the subject set is the UNION across the whole group: count DISTINCT
subject ids once across the selected entities and return one collective value at the
subject-domain grain — not one row per selected entity (which double-counts subjects
related to more than one entity and answers a different question). Narrowly guarded:
emit one row per entity only when the wording says "for each / per / by / list" or
asks for each entity's own metric ("top 5 players and their batting averages").
The collective-coverage cousin of the existing per-entity selected-set rule. Generic
analytics craft (per-entity metric vs set-level coverage). Surfaced via the
spider2-autofix loop (Codex runner, round r3); FAIR_PRODUCT (adversary-screened,
restatable from wording alone, no gold dependency). Flipped local195 mechanism-aligned
(union COUNT(DISTINCT customer)/total, one scalar); 0 regression across 5 passing
per-entity top-N guards (local023/024/029/212/221 stayed long).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* skill(analytics): label-only joins must LEFT JOIN — incomplete dims silently drop fact rows
Mirror of the existing fan-out rule for the DROP direction: an inner JOIN to a
dimension table used only to attach a display attribute silently discards every
fact row whose key has no parent when the dimension is incomplete (trimmed
catalogs, late-arriving / SCD-gap rows), shrinking counts/sums and the universe
over which shares/averages/medians are computed. Guidance: LEFT JOIN pure
enrichment; inner-join a dimension only when intended as a filter; key the
aggregate/GROUP BY on the fact column, not the dimension column.
Spider2 autofix round 'joindim': flips complex_oracle local050 (FAIL->PASS,
official scorer) — solver dropped the gratuitous products inner-join and
recovered the exact gold. local060/063 also adopt LEFT JOIN (rule fires) but
remain gold-convention-blocked. Guards local061/067 held.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* docs(spider2-specs): add todo/17 — lifecycle-event metrics (semantic-layer)
Draft intake spec surfaced by the spider2-autofix loop (round r1): the model-layer
form of the shipped iter4 lifecycle-date-anchoring skill rule — infer per-state
lifecycle-event metrics (e.g. delivered_orders with defaultTimeDimension = the
delivery timestamp) during enrichment so the correct time anchor is the default for
any consumer, not only an agent that loaded the skill. Generic; FAIR_PRODUCT.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(connectors): accept leading underscore in connection/identifier ids
The safe-identifier validator regex /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/ allowed an
underscore everywhere except the first character, so a connection id / database
name that legitimately starts with '_' (valid in Snowflake, e.g. _1000_GENOMES)
could never be ingested or queried. Allow a leading underscore across all 16
duplicated validators (connection ids, source ids, page/wiki keys, warehouse-
verification tool schemas). Path-safety is unaffected — '.' and '/' remain
excluded, and assertSafePathToken still blocks traversal.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* feat(analytics): generic geospatial query guidance
Add a Snowflake ST_* dialect note (ST_MAKEPOINT lon-first, ST_DWITHIN/ST_CONTAINS/
ST_WITHIN/ST_INTERSECTS, bbox->polygon via ST_MAKEPOLYGON/ST_MAKELINE) and a
dialect-agnostic 'Spatial predicates' recipe in the analytics skill (resolve the
entity geometry, build an area-of-interest polygon, test with the engine's
containment/proximity/overlap predicate; mind lon/lat argument order). Steers the
solver off hand-rolled lat/lon BETWEEN boxes toward correct, index-assisted
geospatial predicates.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* feat(analytics): parse code/dependency text by language grammar
Add two generic <sql_craft> rules: (1) parse imported/required/loaded packages by
the language or manifest format (Java import keep-package-path allowing underscores/
mixed-case; Python import/from + alias stripping; R library/require; .ipynb parse
JSON cell source before language rules; JSON manifests flatten the dependency object
keys), stripping comments/prose and splitting multi-import lines; (2) on a
de-duplicated table with a documented copy/occurrence count, choose COUNT(*) vs the
weight column from the population the question names, not silently. Steers off one
broad regex that drops valid identifiers and matches prose.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* feat(analytics): source filters/dates/measures from the owning fact grain
Add a <sql_craft> rule for joined fact tables at different grains (parent order
vs child line item): read each predicate, calendar bucket, and measure from the
table whose grain the question names, not whichever is in scope post-join. An
order-grain filter ("orders that are Complete", "the order's creation date")
must come from the parent even though the child carries its own status/created_at;
line price/cost come from the child. Mirror at metric grain: don't combine a
parent-grain count with child rows (num_of_item * SUM(line_price) per line) —
aggregate each measure at its own grain before combining.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* feat(analytics): collapse multi-valued classes to one representative per entity before counting/concentration
When an entity carries a multi-valued classification array (IPC/CPC codes, tags)
and the methodology counts entities-per-class or a concentration/diversity metric
(HHI, originality, share), pick ONE representative per entity first (the array's
main/primary/first flag, else a defined fallback like most-frequent), then
aggregate; and use COUNT(DISTINCT entity) when the denominator is defined as a
count of entities. Unnesting the array otherwise multiplies an entity's weight by
its code count, inflating per-class frequencies and skewing the ranking/score.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* feat(connectors): introspect BigQuery datasets hosted in foreign projects
A dataset_ids/dataset_id entry may now be written `project.dataset` to
introspect a dataset hosted in another project while query jobs still bill to
credentials.project_id. Entries are parsed once at the config boundary into
canonical {project, dataset} pairs; introspection, primary-key discovery,
testConnection, getTableRowCount, and listTables (grouped per project) all
resolve in the dataset's own project, and scanned tables are labeled with that
project so sampling, distinct-value, and read queries resolve. Bare entries are
unchanged.
Implements spider2-specs/specs/18-bigquery-cross-project-datasets.md.
* feat(scan): durable, resumable, bounded relationship detection during enrichment
Move the enrichment persistence boundary to the cost boundary and bound the
open-ended relationship stage (spec 19).
- Checkpoint descriptions + embeddings into the queryable `_schema` manifest
(and the raw enrichment artifacts) before relationship detection runs, via a
new `onCheckpoint` hook + `writeLocalScanEnrichmentCheckpoint`. An interrupted,
budget-truncated, or failed relationship stage now degrades to "no joins",
never "no descriptions".
- Resume the enrichment cache by content identity: re-key the SQLite stage store
on `(connection_id, stage, input_hash)` so a re-run with a fresh runId resumes
finished descriptions/embeddings instead of re-paying for LLM work. The
disposable cache recreates its table if the on-disk key shape differs.
- Make the relationship stage observable and bounded: a sticky wall-clock budget
(`scan.relationships.detectionBudgetMs`, default 600000 ms) + per-unit progress
+ honored `ctx.signal`, threaded through profiling, validation, and composite
detection. On exhaustion/abort it stops scheduling, finalizes, and returns a
partial result instead of throwing or hanging.
- Mark a budget/abort-truncated result partial (diagnostics `partial`/`partialReason`
+ recoverable `relationship_detection_partial` warning). A graceful partial saves
as a completed stage and resumes cheaply; raising the budget changes inputHash
and forces a fresh, fuller run. A process killed mid-stage saves nothing.
Document `detectionBudgetMs` in the ktx.yaml reference. Append implementation
notes to specs/19 and move the intake draft to done/.
Also carries the in-tree per-table enrichment LLM timeout work it builds on
(`description-generation.ts` + the `enrichment_timeout` warning code), which is
intertwined in `local-enrichment.ts`/`types.ts` and cannot be split into a
separately-building commit.
* feat(scan): bound + retry the per-table enrichment LLM call
The batched table-description call had no retry (sampleTable retried 3x, this did
not), so a single transient backend error (e.g. an overloaded/burst rejection when
many tables enrich concurrently) silently nulled a whole table's descriptions —
observed dropping ~70% of a db's tables during a bad window despite ample quota.
- Wrap generateObject in retryAsync (3 attempts + backoff; KTX_ENRICH_LLM_ATTEMPTS).
- Fresh per-attempt timeout (KTX_ENRICH_LLM_TIMEOUT_MS, default 120s) still bounds a
wedged wide table; a timeout is surfaced as KtxAbortedError so it is NOT retried
(one wedge stays one timeout, not 3x).
- Granular per-table progress + start/done/retry/timeout logging.
Composes with spec 19 (its non-goal #1): spec 19 makes completed descriptions durable;
this makes more of them complete.
* feat(scan): survive a hung LLM enrichment backend and resume descriptions
Two compounding failure modes on the per-table description-enrichment path (spec 20):
Enforced per-table timeout for subprocess backends. The runtime declares whether it owns an SDK subprocess (subprocessForkSpec on KtxLlmRuntimePort); codex/claude-code calls run behind a ktx-owned detached child that is tree-killed (SIGKILL of the process group on POSIX, taskkill /T on Windows) on the deadline or ctx.signal, reaping the wedged model grandchild. HTTP backends keep native fetch abort. Default stays 120s, one-wedge-one-timeout.
Incremental, resumable descriptions persistence. generateDescriptions flushes enriched tables per batch to an inputHash-tagged durable record (at a stable, non-syncId path) plus only the changed manifest shards, skips already-enriched tables on resume, and never lets one table's failure discard the stage (a skipped table costs one missing description, not the whole stage's output).
Spec 20 refined + intake draft moved to done/.
* feat(scan): selective enrichment stages (--stages) + per-stage cache keys
Split the single coarse enrichment cache key into per-stage hashes
(descriptions <- snapshot + LLM identity; embeddings <- snapshot + embedding
identity + description digest; relationships <- snapshot + relationship settings
+ LLM identity), so changing one stage's inputs invalidates only that stage and
never throws away the expensive per-table descriptions on an unrelated edit.
Add `ktx ingest --stages <list>` to force-re-run a chosen subset on an
already-ingested connection: a named stage bypasses the completed-stage
short-circuit while the per-table descriptions resume record still skips
already-enriched tables, and unselected stages are left untouched on disk. Feed
embeddings + relationships their description context from the on-disk _schema
when descriptions do not run this invocation, and carry descriptions into the
llmProposals evidence packet (closing a latent gap on the full-run path too).
Surface an enrichment_stage_stale warning when an unselected stage's inputs have
drifted, rather than silently cascading the work.
Implements spider2-specs/specs/21-selective-enrichment-stages.md.
* test(analytics): realign SKILL.md acceptance test with the evolved skill
Three assertions in analytics-skill-content.test.ts drifted from the analytics
SKILL.md as later iterations edited the skill without updating the test:
- the sub-heading was renamed Window functions -> Ordering & aggregation
determinism (iter2), so follow the source name;
- the rule "Expose identity, not just the label" was renamed to "Project BOTH
identity and label" (spec 14), so match the new wording;
- the dialect-FQTN guard false-positived on the Java package example
com.planet_ink.coffee_mud, whose backticks made a 3-segment package path read
as a BigQuery/Snowflake `a.b.c` table reference. Drop the backticks so the
guard stays at full strength without weakening it.
* fix(scan): --stages subset must not delete unselected stages' on-disk artifacts
A --stages subset that omitted descriptions wiped all on-disk ai/db descriptions
from the written _schema. runLocalScan writes the structural manifest shard from
the bare snapshot BEFORE enrichment runs, and the shard merge treats ai/db as
scan-managed and overwrites them with whatever the run emits — none, on a subset
that skips descriptions. Enrichment then read the already-wiped shard via
loadPriorDescriptions and had nothing to restore.
runLocalScanEnrichment now returns the best-available descriptions (fresh-this-run
if descriptions ran, else loaded from the on-disk _schema) instead of [], and
runLocalScan captures the prior descriptions before the structural write and feeds
them to both the structural write and enrichment, so an unselected stage's
artifacts survive. Joins were already preserved for --stages descriptions via the
manual/inferred preservedJoins path.
Tests: a full runLocalScan --stages relationships path test (RED without the fix,
GREEN with it — the earlier unit test missed the structural-pre-write ordering),
plus enrichment-layer contract tests for both directions. Validated live on
northwind: --stages relationships keeps all 110 descriptions + 22 joins (was
wiping to 0); --stages descriptions restores descriptions from the spec-20 resume
record (no LLM calls) while keeping joins.
* feat(dialects): bigquery nested-data (ARRAY/STRUCT/UNNEST), geospatial (GEOGRAPHY), SAFE_DIVIDE
bigquery.md lacked the two sections that define BigQuery analytics (present in snowflake.md):
- Nested & repeated data: UNNEST to flatten arrays of STRUCTs (GA360 hits, GA4 event_params),
dot-notation field access, key-value param scalar-subquery extraction, fan-out/COUNT(DISTINCT) guard.
- Geospatial (GEOGRAPHY): ST_GEOGPOINT (lon-first), containment/proximity/distance/intersection
predicates, areal allocation via ST_AREA(ST_INTERSECTION()).
- SAFE_DIVIDE for zero-denominator-safe rates; sharded-table shard-presence note.
Generic BigQuery craft surfaced by sql_dialect_notes; product-completeness (any BQ analyst benefits).
* spec(ingest): resumable + fault-tolerant source ingest (#22)
Refined spec for two source-ingest durability gaps surfaced by a real
user report on a ~2-day dbt ingest: (1) interrupted runs restart every
work unit from scratch (no cross-run reuse), and (2) the final
integration gate is all-or-nothing — one unfixable artifact discards the
whole run.
Design: automatic content-keyed work-unit resume reusing the scan
durability primitive (specs 19/20), plus a deterministic dangling-edge
prune that replaces the fatal final-gate throw so a single bad model
costs only that model, not the run. Prune operates on the integrated
tree and never poisons the cache, so resume and prune self-heal.
* refactor(scan): route enrichment resume through shared cache
* feat(ingest): replay cached work unit patches
* refactor(ingest): return structured final gate findings
* feat(ingest): prune final gates without LLM repair
* docs(ingest): document final gate pruning
* test(ingest): cover stale work unit cache recompute
* fix(ingest): refresh stale cache recompute metadata
* test(ingest): cover missing-target prune and self-heal
* fix(ingest): defer pruneable final gate findings
* fix(ingest): replay pruned cached work unit intent
* chore(ingest): verify resumable source ingest self-heal
* test(ingest): cover final gate prune source path resolution
* fix(ingest): resolve final gate prune sources canonically
* fix: defer wiki ref cleanup out of stage 3
* test: cover non-cascading final gate join pruning
* test: cover intrinsic final gate source drops
* docs(spec): record implementation notes for resumable source ingest (#22)
* fix(ingest): prune dangling joins on untouched sources and stop storing cache patch text
- final gate: drop a dropped source's dangling join edges from every owner on
the connection, including untouched siblings the touched-scoped gate never
revisits, so a committed orphan join can't break SL queries
- work-unit cache: drop the stored patch text; replay re-derives the diff from
the before/after artifact snapshots, carrying each touched file only once
- scan enrichment: checkpoint recomputed embeddings before the kill-prone
relationship stage even when descriptions load from disk, using the
best-available description set so the manifest merge can't delete them
- sl: extract listSlSourceFiles so the final gate and resolveSlSourceFile
share one listing path
* fix(scan): accept relationships mode in enrichment state metadata
Listing run stages after a relationships-mode scan threw "Invalid scan
enrichment cache metadata" because the parser hand-enumerated only the
structural/enriched modes while a relationships scan persists its stage with
mode 'relationships'. Derive the mode and stage allowlists from the canonical
KTX_SCAN_MODES and KTX_SCAN_ENRICHMENT_STAGES registries so the runtime check
cannot drift from the type again.
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2814 lines
118 KiB
TypeScript
2814 lines
118 KiB
TypeScript
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import YAML from 'yaml';
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
import { GitService } from '../../../src/context/core/git.service.js';
|
|
import { SessionWorktreeService } from '../../../src/context/core/session-worktree.service.js';
|
|
import { LocalGitFileStore } from '../../../src/context/project/local-git-file-store.js';
|
|
import { SqliteContentResultCache } from '../../../src/context/cache/sqlite-content-result-cache.js';
|
|
import { slSourceFilePath } from '../../../src/context/sl/source-files.js';
|
|
import { addTouchedSlSource } from '../../../src/context/tools/touched-sl-sources.js';
|
|
import { IngestBundleRunner } from '../../../src/context/ingest/ingest-bundle.runner.js';
|
|
import type { IngestBundleRunnerDeps } from '../../../src/context/ingest/ports.js';
|
|
|
|
async function makeRealGitRuntime() {
|
|
const homeDir = await mkdtemp(join(tmpdir(), 'ktx-isolated-runner-'));
|
|
const configDir = join(homeDir, 'config');
|
|
const git = new GitService({
|
|
storage: { configDir, homeDir },
|
|
git: {
|
|
userName: 'System User',
|
|
userEmail: 'system@example.com',
|
|
bootstrapMessage: 'init',
|
|
bootstrapAuthor: 'system',
|
|
bootstrapAuthorEmail: 'system@example.com',
|
|
},
|
|
});
|
|
await git.onModuleInit();
|
|
const configService = new LocalGitFileStore({ rootDir: configDir, git });
|
|
const sessionWorktreeService = new SessionWorktreeService({
|
|
coreConfig: {
|
|
storage: { configDir, homeDir },
|
|
git: {
|
|
userName: 'System User',
|
|
userEmail: 'system@example.com',
|
|
bootstrapMessage: 'init',
|
|
bootstrapAuthor: 'system',
|
|
bootstrapAuthorEmail: 'system@example.com',
|
|
},
|
|
},
|
|
gitService: git,
|
|
configService,
|
|
});
|
|
return { homeDir, configDir, git, configService, sessionWorktreeService };
|
|
}
|
|
|
|
function rootOfConfig(configService: unknown, fallback: string): string {
|
|
const rootDir = (configService as { rootDir?: unknown }).rootDir;
|
|
return typeof rootDir === 'string' ? rootDir : fallback;
|
|
}
|
|
|
|
async function loadSourcesFromRoot(root: string, connectionId = 'warehouse') {
|
|
const dir = join(root, 'semantic-layer', connectionId);
|
|
const entries = await readdir(dir).catch(() => []);
|
|
const sources = await Promise.all(
|
|
entries
|
|
.filter((entry) => entry.endsWith('.yaml') || entry.endsWith('.yml'))
|
|
.sort()
|
|
.map(async (entry) => {
|
|
const parsed = YAML.parse(await readFile(join(dir, entry), 'utf-8')) as Record<string, unknown> | null;
|
|
return parsed && typeof parsed.name === 'string'
|
|
? {
|
|
name: parsed.name,
|
|
grain: Array.isArray(parsed.grain) ? parsed.grain : [],
|
|
columns: Array.isArray(parsed.columns) ? parsed.columns : [],
|
|
joins: Array.isArray(parsed.joins) ? parsed.joins : [],
|
|
measures: Array.isArray(parsed.measures) ? parsed.measures : [],
|
|
segments: Array.isArray(parsed.segments) ? parsed.segments : [],
|
|
table: parsed.table,
|
|
}
|
|
: null;
|
|
}),
|
|
);
|
|
return {
|
|
sources: sources.filter((source): source is NonNullable<typeof source> => source !== null),
|
|
loadErrors: [],
|
|
};
|
|
}
|
|
|
|
// Mirrors the production contract: resolve the standalone/overlay file for a
|
|
// source, null when absent. Fixtures keep filename == name, so a direct read
|
|
// is a faithful shortcut.
|
|
async function readSourceFileFromRoot(root: string, connectionId: string, sourceName: string) {
|
|
const relPath = `semantic-layer/${connectionId}/${sourceName}.yaml`;
|
|
const content = await readFile(join(root, relPath), 'utf-8').catch(() => null);
|
|
return content === null ? null : { content, path: relPath };
|
|
}
|
|
|
|
async function listGlobalWikiPageKeys(root: string): Promise<string[]> {
|
|
const dir = join(root, 'wiki/global');
|
|
const entries = await readdir(dir).catch(() => []);
|
|
return entries
|
|
.filter((entry) => entry.endsWith('.md'))
|
|
.map((entry) => entry.slice(0, -'.md'.length))
|
|
.sort();
|
|
}
|
|
|
|
function frontmatterList(yaml: string, key: string): string[] {
|
|
const pattern = new RegExp(`(?:^|\\n)${key}:\\n((?: - .+\\n?)*)`);
|
|
return (
|
|
pattern
|
|
.exec(yaml)?.[1]
|
|
?.split('\n')
|
|
.map((line) => line.trim().replace(/^- /, ''))
|
|
.filter(Boolean) ?? []
|
|
);
|
|
}
|
|
|
|
function legacyFallbackSettingKey(): string {
|
|
return ['sharedWorktree', 'SourceKeys'].join('');
|
|
}
|
|
|
|
function legacySharedTraceEvent(): string {
|
|
return ['shared', 'worktree', 'path', 'enabled'].join('_');
|
|
}
|
|
|
|
function workUnitRunLoopCalls(deps: IngestBundleRunnerDeps) {
|
|
return vi
|
|
.mocked(deps.agentRunner.runLoop)
|
|
.mock.calls.filter(([params]: any[]) => params.telemetryTags?.operationName === 'ingest-bundle-wu');
|
|
}
|
|
|
|
function makeWikiService(root: string) {
|
|
return {
|
|
listPageKeys: vi.fn(async (scope: string) => (scope === 'GLOBAL' ? listGlobalWikiPageKeys(root) : [])),
|
|
readPage: vi.fn(async (_scope: string, _scopeId: string | null, key: string) => {
|
|
const path = join(root, 'wiki/global', `${key}.md`);
|
|
const raw = await readFile(path, 'utf-8').catch(() => null);
|
|
if (!raw) {
|
|
return null;
|
|
}
|
|
const [, yaml = '', content = ''] = /^---\n([\s\S]*?)\n---\n?([\s\S]*)$/.exec(raw) ?? [];
|
|
return {
|
|
pageKey: key,
|
|
frontmatter: {
|
|
summary: key,
|
|
usage_mode: 'auto',
|
|
refs: frontmatterList(yaml, 'refs'),
|
|
sl_refs: frontmatterList(yaml, 'sl_refs'),
|
|
},
|
|
content: content.trim(),
|
|
};
|
|
}),
|
|
writePage: vi.fn(
|
|
async (
|
|
_scope: string,
|
|
_scopeId: string | null,
|
|
key: string,
|
|
frontmatter: { summary?: string; usage_mode?: string; refs?: string[]; sl_refs?: string[] },
|
|
content: string,
|
|
) => {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
const refs = (frontmatter.refs ?? []).map((ref) => ` - ${ref}`).join('\n');
|
|
const slRefs = (frontmatter.sl_refs ?? []).map((ref) => ` - ${ref}`).join('\n');
|
|
await writeFile(
|
|
join(root, 'wiki/global', `${key}.md`),
|
|
[
|
|
'---',
|
|
`summary: ${frontmatter.summary ?? key}`,
|
|
`usage_mode: ${frontmatter.usage_mode ?? 'auto'}`,
|
|
'refs:',
|
|
refs,
|
|
'sl_refs:',
|
|
slRefs,
|
|
'---',
|
|
'',
|
|
content,
|
|
'',
|
|
].join('\n'),
|
|
);
|
|
},
|
|
),
|
|
syncFromCommit: vi.fn(),
|
|
};
|
|
}
|
|
|
|
function makeDeps(
|
|
runtime: Awaited<ReturnType<typeof makeRealGitRuntime>>,
|
|
sourceKey = 'metabase',
|
|
settings: Partial<IngestBundleRunnerDeps['settings']> = {},
|
|
) {
|
|
const adapter: any = {
|
|
source: sourceKey,
|
|
skillNames: [],
|
|
detect: vi.fn().mockResolvedValue(true),
|
|
chunk: vi.fn().mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'card-wiki', rawFiles: ['cards/wiki.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'card-source', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
}),
|
|
};
|
|
const wikiService = makeWikiService(runtime.configDir);
|
|
const semanticLayerService: any = {
|
|
loadAllSources: vi.fn(async (connectionId: string) => loadSourcesFromRoot(runtime.configDir, connectionId)),
|
|
listFilesForConnection: vi.fn().mockResolvedValue(['mart_account_segments.yaml']),
|
|
readSourceFile: vi.fn((connectionId: string, sourceName: string) =>
|
|
readSourceFileFromRoot(runtime.configDir, connectionId, sourceName),
|
|
),
|
|
};
|
|
semanticLayerService.forWorktree = vi.fn((workdir: string) => ({
|
|
...semanticLayerService,
|
|
loadAllSources: vi.fn(async (connectionId: string) => loadSourcesFromRoot(workdir, connectionId)),
|
|
listFilesForConnection: vi.fn().mockResolvedValue(['mart_account_segments.yaml']),
|
|
readSourceFile: vi.fn((connectionId: string, sourceName: string) =>
|
|
readSourceFileFromRoot(workdir, connectionId, sourceName),
|
|
),
|
|
}));
|
|
|
|
const deps: IngestBundleRunnerDeps = {
|
|
runs: { create: vi.fn().mockResolvedValue({ id: 'run-1' }), markCompleted: vi.fn(), markFailed: vi.fn() },
|
|
provenance: {
|
|
insertMany: vi.fn(),
|
|
findLatestHashesForCompletedSyncs: vi.fn().mockResolvedValue(new Map()),
|
|
findLatestArtifactsForRawPaths: vi.fn().mockResolvedValue(new Map()),
|
|
},
|
|
reports: { create: vi.fn().mockResolvedValue({ id: 'report-1' }), findByJobId: vi.fn().mockResolvedValue(null), markSuperseded: vi.fn() },
|
|
canonicalPins: { listPins: vi.fn().mockResolvedValue([]) },
|
|
contentCache: new SqliteContentResultCache({ dbPath: join(runtime.homeDir, 'cache.sqlite') }),
|
|
registry: { get: vi.fn().mockReturnValue(adapter), register: vi.fn(), has: vi.fn(), list: vi.fn() },
|
|
diffSetService: {
|
|
compute: vi.fn().mockResolvedValue({ added: ['cards/wiki.json', 'cards/source.json'], modified: [], deleted: [], unchanged: [] }),
|
|
},
|
|
sessionWorktreeService: runtime.sessionWorktreeService,
|
|
agentRunner: { runLoop: vi.fn() },
|
|
gitService: runtime.git,
|
|
lockingService: { withLock: vi.fn(async (_key, fn) => fn()) },
|
|
storage: {
|
|
homeDir: join(runtime.configDir, '.ktx'),
|
|
systemGitAuthor: { name: 'ktx Test', email: 'system@ktx.local' },
|
|
resolveUploadDir: (id) => join(runtime.homeDir, 'upload', id),
|
|
resolvePullDir: (id) => join(runtime.homeDir, 'pull', id),
|
|
resolveTranscriptDir: (id) => join(runtime.configDir, '.ktx/ingest-transcripts', id),
|
|
resolveTracePath: (id) => join(runtime.configDir, '.ktx/ingest-traces', id, 'trace.jsonl'),
|
|
},
|
|
settings: {
|
|
memoryIngestionModel: 'test',
|
|
cliVersion: '0.0.0-test',
|
|
probeRowCount: 1,
|
|
ingestTraceLevel: 'trace',
|
|
...settings,
|
|
},
|
|
skillsRegistry: {
|
|
listSkills: vi.fn().mockResolvedValue([]),
|
|
getSkill: vi.fn().mockResolvedValue(null),
|
|
buildSkillsPrompt: vi.fn().mockReturnValue(''),
|
|
stripFrontmatter: vi.fn((body) => body),
|
|
} as never,
|
|
promptService: { loadPrompt: vi.fn().mockResolvedValue('base') } as never,
|
|
wikiService: { ...wikiService, forWorktree: vi.fn((workdir: string) => makeWikiService(workdir)) } as never,
|
|
knowledgeIndex: { listPagesForUser: vi.fn().mockResolvedValue([]) },
|
|
knowledgeSlRefs: { syncFromWiki: vi.fn() },
|
|
semanticLayerService,
|
|
slSearchService: { indexSources: vi.fn() } as never,
|
|
slSourcesRepository: {} as never,
|
|
slValidator: { validateSingleSource: vi.fn().mockResolvedValue({ errors: [], warnings: [] }) },
|
|
connections: { listEnabledConnections: vi.fn().mockResolvedValue([]), getConnectionById: vi.fn() } as never,
|
|
toolsetFactory: { createIngestWuToolset: vi.fn(() => ({ toRuntimeTools: vi.fn(() => ({})) })) },
|
|
commitMessages: { enqueueForExternalCommit: vi.fn() },
|
|
embedding: { maxBatchSize: 64, computeEmbedding: vi.fn(), computeEmbeddingsBulk: vi.fn() },
|
|
};
|
|
return { deps, adapter };
|
|
}
|
|
|
|
async function mockStageRawFiles(
|
|
runner: IngestBundleRunner,
|
|
runtime: Awaited<ReturnType<typeof makeRealGitRuntime>>,
|
|
hashes: [string, string][],
|
|
sourceKey = 'metabase',
|
|
) {
|
|
(runner as any).resolveStagedDir = vi.fn().mockResolvedValue(join(runtime.homeDir, 'stage'));
|
|
(runner as any).stageRawFilesStage1 = vi.fn(async ({ worktreeRoot }: any) => {
|
|
const rawDir = join(worktreeRoot, 'raw-sources/warehouse', sourceKey, 's');
|
|
const stagedDir = join(runtime.homeDir, 'stage');
|
|
await mkdir(rawDir, { recursive: true });
|
|
for (const [rawPath, rawHash] of hashes) {
|
|
await mkdir(join(stagedDir, rawPath.split('/').slice(0, -1).join('/')), { recursive: true });
|
|
await mkdir(join(rawDir, rawPath.split('/').slice(0, -1).join('/')), { recursive: true });
|
|
const content = JSON.stringify({ rawHash });
|
|
await writeFile(join(stagedDir, rawPath), content);
|
|
await writeFile(join(rawDir, rawPath), content);
|
|
}
|
|
return { currentHashes: new Map(hashes), rawDirInWorktree: `raw-sources/warehouse/${sourceKey}/s` };
|
|
});
|
|
}
|
|
|
|
describe('IngestBundleRunner isolated diff path', () => {
|
|
it('routes an unlisted direct-writing source through isolated diffs by default', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const sourceKey = 'custom-direct-source';
|
|
const { deps, adapter } = makeDeps(runtime, sourceKey);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{
|
|
unitKey: 'custom-wiki',
|
|
rawFiles: ['custom/page.json'],
|
|
peerFileIndex: [],
|
|
dependencyPaths: [],
|
|
},
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/custom-isolated.md'),
|
|
'---\nsummary: Custom isolated write\nusage_mode: auto\n---\n\nCustom isolated write.\n',
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'custom-isolated',
|
|
detail: 'Custom isolated write',
|
|
rawPaths: ['custom/page.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/custom-isolated.md'],
|
|
'custom wiki',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['custom/page.json', 'h1']], sourceKey);
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-custom-default',
|
|
connectionId: 'warehouse',
|
|
sourceKey,
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).resolves.toMatchObject({
|
|
jobId: 'job-custom-default',
|
|
failedWorkUnits: [],
|
|
workUnitCount: 1,
|
|
});
|
|
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-custom-default/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('isolated_diff_enabled');
|
|
expect(trace).toContain('work_unit_child_created');
|
|
expect(trace).not.toContain(legacySharedTraceEvent());
|
|
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0];
|
|
const reportBody = reportCreate?.body as { isolatedDiff?: unknown } | undefined;
|
|
expect(reportBody?.isolatedDiff).toMatchObject({
|
|
enabled: true,
|
|
acceptedPatches: 1,
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('does not support shared-worktree fallback settings', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const sourceKey = 'legacy-source';
|
|
const staleSettings = {
|
|
[legacyFallbackSettingKey()]: ['legacy-source'],
|
|
} as Partial<IngestBundleRunnerDeps['settings']> & Record<string, unknown>;
|
|
const { deps, adapter } = makeDeps(runtime, sourceKey, staleSettings);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{
|
|
unitKey: 'legacy-wiki',
|
|
rawFiles: ['legacy/page.json'],
|
|
peerFileIndex: [],
|
|
dependencyPaths: [],
|
|
},
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/legacy-isolated.md'),
|
|
'---\nsummary: Legacy isolated write\nusage_mode: auto\n---\n\nLegacy isolated write.\n',
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'legacy-isolated',
|
|
detail: 'Legacy isolated write',
|
|
rawPaths: ['legacy/page.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/legacy-isolated.md'],
|
|
'legacy isolated wiki',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['legacy/page.json', 'h1']], sourceKey);
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-legacy-isolated',
|
|
connectionId: 'warehouse',
|
|
sourceKey,
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).resolves.toMatchObject({
|
|
jobId: 'job-legacy-isolated',
|
|
failedWorkUnits: [],
|
|
workUnitCount: 1,
|
|
});
|
|
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-legacy-isolated/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('isolated_diff_enabled');
|
|
expect(trace).toContain('work_unit_child_created');
|
|
expect(trace).not.toContain(legacySharedTraceEvent());
|
|
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0];
|
|
const reportBody = reportCreate?.body as { isolatedDiff?: unknown } | undefined;
|
|
expect(reportBody?.isolatedDiff).toMatchObject({
|
|
enabled: true,
|
|
acceptedPatches: 1,
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('does not integrate failed isolated WorkUnit patches', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'fake');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'wu-good', rawFiles: ['good.raw'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'wu-bad', rawFiles: ['bad.raw'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
deps.diffSetService.compute = vi.fn().mockResolvedValue({
|
|
added: ['good.raw', 'bad.raw'],
|
|
modified: [],
|
|
deleted: [],
|
|
unchanged: [],
|
|
});
|
|
deps.slValidator.validateSingleSource = vi.fn(
|
|
async (_validationDeps: unknown, _connectionId: string, sourceName: string) => ({
|
|
errors: sourceName === 'bad' ? [{ message: 'bad source rejected' }] : [],
|
|
warnings: [],
|
|
}),
|
|
) as never;
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
if (unitKey === 'wu-good') {
|
|
await writeFile(join(root, 'semantic-layer/warehouse/good.yaml'), 'name: good\n', 'utf-8');
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'good');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'good',
|
|
detail: 'good source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['good.raw'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/good.yaml'],
|
|
'test: add good source',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
if (unitKey === 'wu-bad') {
|
|
await writeFile(join(root, 'semantic-layer/warehouse/bad.yaml'), 'name: bad\n', 'utf-8');
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'bad');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'bad',
|
|
detail: 'bad source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['bad.raw'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/bad.yaml'],
|
|
'test: add bad source',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(
|
|
runner,
|
|
runtime,
|
|
[
|
|
['good.raw', 'good-hash'],
|
|
['bad.raw', 'bad-hash'],
|
|
],
|
|
'fake',
|
|
);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-failed-wu-isolated',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'fake',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.failedWorkUnits).toEqual(['wu-bad']);
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/good.yaml'), 'utf-8')).resolves.toContain(
|
|
'good',
|
|
);
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/bad.yaml'), 'utf-8')).rejects.toThrow();
|
|
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0];
|
|
const reportBody = reportCreate?.body as {
|
|
isolatedDiff?: { acceptedPatches?: number };
|
|
failedWorkUnits?: string[];
|
|
};
|
|
expect(reportBody.failedWorkUnits).toEqual(['wu-bad']);
|
|
expect(reportBody.isolatedDiff).toMatchObject({ enabled: true, acceptedPatches: 1 });
|
|
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-failed-wu-isolated/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('work_unit_failed_before_patch');
|
|
expect(trace).toContain('patch_accepted');
|
|
expect(trace).not.toContain(legacySharedTraceEvent());
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('replays completed work units on a second identical run without an agent loop', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'customers', rawFiles: ['models/customers.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, `wiki/global/${unitKey}.md`),
|
|
`---\nsummary: ${unitKey}\nusage_mode: auto\n---\n\n${unitKey}\n`,
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: unitKey, detail: unitKey });
|
|
await currentSession.gitService.commitFiles([`wiki/global/${unitKey}.md`], `wu ${unitKey}`, 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['models/orders.sql', 'orders-hash'],
|
|
['models/customers.sql', 'customers-hash'],
|
|
], 'dbt');
|
|
|
|
await expect(
|
|
runner.run({ jobId: 'job-resume-1', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).resolves.toMatchObject({ failedWorkUnits: [] });
|
|
expect(workUnitRunLoopCalls(deps)).toHaveLength(2);
|
|
|
|
await expect(
|
|
runner.run({ jobId: 'job-resume-2', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).resolves.toMatchObject({ failedWorkUnits: [] });
|
|
expect(workUnitRunLoopCalls(deps)).toHaveLength(2);
|
|
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-resume-2/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('work_unit_cache_hit');
|
|
expect(trace.match(/work_unit_cache_replayed/g)).toHaveLength(2);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('recomputes only the changed work unit after an input byte changes', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'customers', rawFiles: ['models/customers.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, `wiki/global/${unitKey}.md`),
|
|
`---\nsummary: ${unitKey}\nusage_mode: auto\n---\n\n${unitKey}\n`,
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({ target: 'wiki', type: 'updated', key: unitKey, detail: unitKey });
|
|
await currentSession.gitService.commitFiles([`wiki/global/${unitKey}.md`], `wu ${unitKey}`, 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['models/orders.sql', 'orders-hash'],
|
|
['models/customers.sql', 'customers-hash'],
|
|
], 'dbt');
|
|
await runner.run({ jobId: 'job-input-1', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } });
|
|
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['models/orders.sql', 'orders-hash-changed'],
|
|
['models/customers.sql', 'customers-hash'],
|
|
], 'dbt');
|
|
await runner.run({ jobId: 'job-input-2', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } });
|
|
|
|
const wuCalls = workUnitRunLoopCalls(deps);
|
|
expect(wuCalls).toHaveLength(3);
|
|
const secondRunUnitKeys = wuCalls.slice(2).map(([params]: any[]) => params.telemetryTags.unitKey);
|
|
expect(secondRunUnitKeys).toEqual(['orders']);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('does not cache failed work units and retries them on the next run', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
let attempt = 0;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
attempt += 1;
|
|
if (attempt === 1) {
|
|
return { stopReason: 'error', error: new Error('provider disconnected') };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(join(root, 'wiki/global/orders.md'), '---\nsummary: orders\nusage_mode: auto\n---\n\norders\n', 'utf-8');
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: 'orders', detail: 'orders' });
|
|
await currentSession.gitService.commitFiles(['wiki/global/orders.md'], 'wu orders', 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['models/orders.sql', 'orders-hash']], 'dbt');
|
|
|
|
await expect(
|
|
runner.run({ jobId: 'job-failed-cache-1', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).resolves.toMatchObject({ failedWorkUnits: ['orders'] });
|
|
await expect(
|
|
runner.run({ jobId: 'job-failed-cache-2', connectionId: 'warehouse', sourceKey: 'dbt', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).resolves.toMatchObject({ failedWorkUnits: [] });
|
|
expect(workUnitRunLoopCalls(deps)).toHaveLength(2);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes a missing sibling join, then self-heals from the cached owner patch without rerunning it', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'customers', rawFiles: ['models/customers.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
let customersAttempt = 0;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags?.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
if (unitKey === 'orders') {
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/orders.yaml'),
|
|
[
|
|
'name: orders',
|
|
'grain: [order_id]',
|
|
'columns: [{name: order_id, type: string}, {name: customer_id, type: string}]',
|
|
'joins:',
|
|
' - to: customers',
|
|
' on: orders.customer_id = customers.customer_id',
|
|
'measures: []',
|
|
'',
|
|
].join('\n'),
|
|
'utf-8',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'orders');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'orders',
|
|
detail: 'orders with customer join',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['models/orders.sql'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/orders.yaml'],
|
|
'wu orders',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}
|
|
|
|
customersAttempt += 1;
|
|
if (customersAttempt === 1) {
|
|
return { stopReason: 'error', error: new Error('provider disconnected') };
|
|
}
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/customers.yaml'),
|
|
'name: customers\ngrain: [customer_id]\ncolumns: [{name: customer_id, type: string}]\njoins: []\nmeasures: []\n',
|
|
'utf-8',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'customers');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'customers',
|
|
detail: 'customers source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['models/customers.sql'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/customers.yaml'],
|
|
'wu customers',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(
|
|
runner,
|
|
runtime,
|
|
[
|
|
['models/orders.sql', 'orders-hash'],
|
|
['models/customers.sql', 'customers-hash'],
|
|
],
|
|
'dbt',
|
|
);
|
|
|
|
const first = await runner.run({
|
|
jobId: 'job-join-prune-1',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
expect(first.commitSha).toBeTruthy();
|
|
expect(first.failedWorkUnits).toEqual(['customers']);
|
|
expect(first.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'join',
|
|
artifact: 'semantic-layer/warehouse/orders',
|
|
removedRef: 'customers',
|
|
absentTarget: 'customers',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/orders.yaml'), 'utf-8')).resolves.not.toContain(
|
|
'to: customers',
|
|
);
|
|
|
|
const second = await runner.run({
|
|
jobId: 'job-join-prune-2',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
expect(second.failedWorkUnits).toEqual([]);
|
|
expect(second.finalGatePrunedReferences ?? []).toEqual([]);
|
|
expect(workUnitRunLoopCalls(deps).map(([params]: any[]) => params.telemetryTags.unitKey)).toEqual([
|
|
'orders',
|
|
'customers',
|
|
'customers',
|
|
]);
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/orders.yaml'), 'utf-8')).resolves.toContain(
|
|
'to: customers',
|
|
);
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/customers.yaml'), 'utf-8')).resolves.toContain(
|
|
'name: customers',
|
|
);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-join-prune-2/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('work_unit_cache_hit');
|
|
expect(trace).toContain('work_unit_cache_replayed');
|
|
expect(trace).not.toContain('work_unit_cache_stale_recompute');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes a failed sibling join without pruning a valid surviving join', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'customers', rawFiles: ['models/customers.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'products', rawFiles: ['models/products.sql'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags?.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
|
|
if (unitKey === 'orders') {
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/orders.yaml'),
|
|
[
|
|
'name: orders',
|
|
'grain: [order_id]',
|
|
'columns: [{name: order_id, type: string}, {name: customer_id, type: string}, {name: product_id, type: string}]',
|
|
'joins:',
|
|
' - to: customers',
|
|
' on: orders.customer_id = customers.customer_id',
|
|
' - to: products',
|
|
' on: orders.product_id = products.product_id',
|
|
'measures: []',
|
|
'',
|
|
].join('\n'),
|
|
'utf-8',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'orders');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'orders',
|
|
detail: 'orders with customer and product joins',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['models/orders.sql'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/orders.yaml'],
|
|
'wu orders',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}
|
|
|
|
if (unitKey === 'customers') {
|
|
return { stopReason: 'error', error: new Error('provider disconnected') };
|
|
}
|
|
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/products.yaml'),
|
|
'name: products\ngrain: [product_id]\ncolumns: [{name: product_id, type: string}]\njoins: []\nmeasures: []\n',
|
|
'utf-8',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'products');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'products',
|
|
detail: 'products source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['models/products.sql'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/products.yaml'],
|
|
'wu products',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(
|
|
runner,
|
|
runtime,
|
|
[
|
|
['models/orders.sql', 'orders-hash'],
|
|
['models/customers.sql', 'customers-hash'],
|
|
['models/products.sql', 'products-hash'],
|
|
],
|
|
'dbt',
|
|
);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-join-prune-no-cascade',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.commitSha).toBeTruthy();
|
|
expect(result.failedWorkUnits).toEqual(['customers']);
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'join',
|
|
artifact: 'semantic-layer/warehouse/orders',
|
|
removedRef: 'customers',
|
|
absentTarget: 'customers',
|
|
});
|
|
expect(result.finalGatePrunedReferences).not.toContainEqual({
|
|
kind: 'join',
|
|
artifact: 'semantic-layer/warehouse/orders',
|
|
removedRef: 'products',
|
|
absentTarget: 'products',
|
|
});
|
|
const ordersYaml = await readFile(join(runtime.configDir, 'semantic-layer/warehouse/orders.yaml'), 'utf-8');
|
|
expect(ordersYaml).not.toContain('to: customers');
|
|
expect(ordersYaml).toContain('to: products');
|
|
await expect(readFile(join(runtime.configDir, 'semantic-layer/warehouse/products.yaml'), 'utf-8')).resolves.toContain(
|
|
'name: products',
|
|
);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('drops an intrinsically invalid uppercase source at the final gate and reports the producing work unit', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'signed-up', rawFiles: ['models/signed_up.sql'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
|
|
const sourceName = 'SIGNED_UP';
|
|
const sourcePath = slSourceFilePath('warehouse', sourceName);
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
let signedUpValidationCount = 0;
|
|
deps.slValidator.validateSingleSource = vi.fn(
|
|
async (_validationDeps: any, _connectionId: string, validatedSourceName: string) => {
|
|
if (validatedSourceName === sourceName) {
|
|
signedUpValidationCount += 1;
|
|
if (signedUpValidationCount > 1) {
|
|
return { errors: ['intrinsic final validation failed'], warnings: [] };
|
|
}
|
|
}
|
|
return { errors: [], warnings: [] };
|
|
},
|
|
) as never;
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags?.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(root, sourcePath),
|
|
'name: SIGNED_UP\ngrain: [USER_ID]\ncolumns: [{name: USER_ID, type: string}]\njoins: []\nmeasures: []\n',
|
|
'utf-8',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', sourceName);
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: sourceName,
|
|
detail: 'uppercase signed up source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['models/signed_up.sql'],
|
|
});
|
|
await currentSession.gitService.commitFiles([sourcePath], 'wu signed up', 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['models/signed_up.sql', 'signed-up-hash']], 'dbt');
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-final-gate-intrinsic-drop',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.commitSha).toBeTruthy();
|
|
expect(result.failedWorkUnits).toEqual(['signed-up']);
|
|
expect(result.finalGateDroppedSources).toContainEqual({
|
|
connectionId: 'warehouse',
|
|
sourceName,
|
|
reason: 'intrinsic final validation failed',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, sourcePath), 'utf-8')).rejects.toMatchObject({ code: 'ENOENT' });
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('recomputes a stale cached patch and reports recomputed metadata', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, 'dbt');
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'orders', rawFiles: ['models/orders.sql'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
let agentAttempt = 0;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
return {
|
|
toRuntimeTools: vi.fn(() => {
|
|
currentSession = toolSession;
|
|
return {};
|
|
}),
|
|
};
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags?.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
agentAttempt += 1;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
const detail = agentAttempt === 1 ? 'cached first output' : 'fresh recompute output';
|
|
const body = agentAttempt === 1 ? 'orders cached' : 'orders recomputed';
|
|
await writeFile(
|
|
join(root, 'wiki/global/orders.md'),
|
|
`---\nsummary: orders\nusage_mode: auto\n---\n\n${body}\n`,
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: agentAttempt === 1 ? 'created' : 'updated',
|
|
key: 'orders',
|
|
detail,
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/orders.md'],
|
|
`wu orders ${agentAttempt}`,
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['models/orders.sql', 'orders-hash']], 'dbt');
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-stale-cache-1',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).resolves.toMatchObject({ failedWorkUnits: [] });
|
|
expect(workUnitRunLoopCalls(deps)).toHaveLength(1);
|
|
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/orders.md'),
|
|
'---\nsummary: orders\nusage_mode: auto\n---\n\noperator drift\n',
|
|
'utf-8',
|
|
);
|
|
await runtime.git.commitFiles(['wiki/global/orders.md'], 'manual drift', 'ktx Test', 'system@ktx.local');
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-stale-cache-2',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'dbt',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).resolves.toMatchObject({ failedWorkUnits: [] });
|
|
|
|
expect(workUnitRunLoopCalls(deps)).toHaveLength(2);
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/orders.md'), 'utf-8')).resolves.toContain(
|
|
'orders recomputed',
|
|
);
|
|
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-stale-cache-2/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('work_unit_cache_unsafe_drift');
|
|
expect(trace).not.toContain('work_unit_cache_hit');
|
|
expect(trace).not.toContain('work_unit_cache_stale_recompute');
|
|
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0] as any;
|
|
expect(reportCreate.body.workUnits).toContainEqual(
|
|
expect.objectContaining({
|
|
unitKey: 'orders',
|
|
actions: [expect.objectContaining({ type: 'updated', detail: 'fresh recompute output' })],
|
|
}),
|
|
);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it.each(['notion', 'lookml', 'looker', 'dbt', 'metricflow'] as const)(
|
|
'routes %s direct writes through isolated child worktrees',
|
|
async (sourceKey) => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime, sourceKey);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{
|
|
unitKey: `${sourceKey}-wiki`,
|
|
rawFiles: [`${sourceKey}/page.json`],
|
|
peerFileIndex: [],
|
|
dependencyPaths: [],
|
|
},
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
|
|
expect(params.telemetryTags).toMatchObject({
|
|
operationName: 'ingest-bundle-wu',
|
|
source: sourceKey,
|
|
unitKey: `${sourceKey}-wiki`,
|
|
});
|
|
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global', `${sourceKey}-isolated.md`),
|
|
`---\nsummary: ${sourceKey} isolated write\nusage_mode: auto\n---\n\nIsolated ${sourceKey} write.\n`,
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: `${sourceKey}-isolated`,
|
|
detail: `${sourceKey} isolated write`,
|
|
rawPaths: [`${sourceKey}/page.json`],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
[`wiki/global/${sourceKey}-isolated.md`],
|
|
`${sourceKey} wiki`,
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [[`${sourceKey}/page.json`, 'h1']], sourceKey);
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: `job-${sourceKey}`,
|
|
connectionId: 'warehouse',
|
|
sourceKey,
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).resolves.toMatchObject({
|
|
jobId: `job-${sourceKey}`,
|
|
failedWorkUnits: [],
|
|
workUnitCount: 1,
|
|
});
|
|
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces', `job-${sourceKey}`, 'trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('isolated_diff_enabled');
|
|
expect(trace).toContain('work_unit_child_created');
|
|
expect(trace).toContain('work_unit_patch_collected');
|
|
expect(trace).toContain('patch_apply_started');
|
|
expect(trace).not.toContain(legacySharedTraceEvent());
|
|
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0];
|
|
const reportBody = reportCreate?.body as { isolatedDiff?: unknown } | undefined;
|
|
expect(reportBody?.isolatedDiff).toMatchObject({
|
|
enabled: true,
|
|
acceptedPatches: 1,
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
},
|
|
);
|
|
|
|
it('prunes the Metabase stale-measure wiki body regression before squash', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.project = vi.fn(async ({ workdir }) => {
|
|
await mkdir(join(workdir, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(workdir, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr_cents\n expr: sum(contract_arr)\n',
|
|
);
|
|
return {
|
|
warnings: [],
|
|
errors: [],
|
|
touchedSources: [{ connectionId: 'warehouse', sourceName: 'mart_account_segments' }],
|
|
changedWikiPageKeys: [],
|
|
};
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
if (params.telemetryTags.unitKey === 'card-wiki') {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\nsl_refs:\n - mart_account_segments\n---\n\nARR is `mart_account_segments.total_contract_arr_cents`.\n',
|
|
);
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: 'account-segments', detail: 'Account segments' });
|
|
await currentSession.gitService.commitFiles(['wiki/global/account-segments.md'], 'wu wiki', 'ktx Test', 'system@ktx.local');
|
|
}
|
|
if (params.telemetryTags.unitKey === 'card-source') {
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'updated',
|
|
key: 'mart_account_segments',
|
|
detail: 'Dollar measure',
|
|
targetConnectionId: 'warehouse',
|
|
});
|
|
await currentSession.gitService.commitFiles(['semantic-layer/warehouse/mart_account_segments.yaml'], 'wu source', 'ktx Test', 'system@ktx.local');
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['cards/wiki.json', 'h1'],
|
|
['cards/source.json', 'h2'],
|
|
]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-1',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-1/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('input_snapshot');
|
|
expect(trace).toContain('isolated_diff_enabled');
|
|
expect(trace).toContain('work_unit_child_created');
|
|
expect(trace).toContain('work_unit_patch_collected');
|
|
expect(trace).toContain('patch_apply_started');
|
|
expect(trace).toContain('final_artifact_gates_finished');
|
|
expect(trace).toContain('final_gate_prune_finished');
|
|
expect(trace).toContain('squash_finished');
|
|
expect(trace).not.toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes unchanged wiki body refs made stale by isolated semantic-layer changes', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
await mkdir(join(runtime.configDir, 'semantic-layer/warehouse'), { recursive: true });
|
|
await mkdir(join(runtime.configDir, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(runtime.configDir, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr_cents\n expr: sum(contract_arr)\n',
|
|
);
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\n---\n\nExisting ARR uses `mart_account_segments.total_contract_arr_cents`.\n',
|
|
);
|
|
await runtime.git.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml', 'wiki/global/account-segments.md'],
|
|
'seed existing wiki body ref',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'source-only', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'updated',
|
|
key: 'mart_account_segments',
|
|
detail: 'Rename ARR measure',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml'],
|
|
'wu source rename',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-existing-body-stale',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(await runtime.git.revParseHead()).not.toBe(preRunHead);
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/account-segments.md'), 'utf-8')).resolves.not.toContain(
|
|
'total_contract_arr_cents',
|
|
);
|
|
const events = (await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-existing-body-stale/trace.jsonl'), 'utf-8'))
|
|
.trim()
|
|
.split('\n')
|
|
.map((line) => JSON.parse(line));
|
|
expect(events.map((event) => event.event)).toEqual(
|
|
expect.arrayContaining([
|
|
'final_artifact_gates_started',
|
|
'final_artifact_gates_finished',
|
|
'final_gate_reference_pruned',
|
|
'final_gate_prune_committed',
|
|
'final_gate_prune_finished',
|
|
'squash_finished',
|
|
]),
|
|
);
|
|
expect(events.map((event) => event.event)).not.toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('accepts two isolated work units that edit different wiki pages', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'page-a', rawFiles: ['pages/a.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'page-b', rawFiles: ['pages/b.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const unitKey = params.telemetryTags.unitKey;
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(join(root, `wiki/global/${unitKey}.md`), `---\nsummary: ${unitKey}\nusage_mode: auto\n---\n\n${unitKey}\n`);
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: unitKey, detail: unitKey });
|
|
await currentSession.gitService.commitFiles([`wiki/global/${unitKey}.md`], `wu ${unitKey}`, 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['pages/a.json', 'h1'],
|
|
['pages/b.json', 'h2'],
|
|
]);
|
|
|
|
const result = await runner.run({ jobId: 'job-clean', connectionId: 'warehouse', sourceKey: 'metabase', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } });
|
|
expect(result.failedWorkUnits).toEqual([]);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-clean/trace.jsonl'), 'utf-8');
|
|
expect(trace.match(/patch_accepted/g)).toHaveLength(2);
|
|
expect(trace).toContain('ingest_finished');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('classifies same-source patch application failure as a textual conflict', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'orders-a', rawFiles: ['orders/a.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'orders-b', rawFiles: ['orders/b.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName === 'ingest-isolated-diff-textual-resolver') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const suffix = params.telemetryTags.unitKey === 'orders-a' ? 'a' : 'b';
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/orders.yaml'),
|
|
`name: orders\ngrain: [id]\ncolumns: [{name: id, type: string}]\njoins: []\nmeasures:\n - name: order_count_${suffix}\n expr: count(*)\n`,
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'orders');
|
|
currentSession.actions.push({ target: 'sl', type: 'updated', key: 'orders', detail: suffix, targetConnectionId: 'warehouse' });
|
|
await currentSession.gitService.commitFiles(['semantic-layer/warehouse/orders.yaml'], `wu ${suffix}`, 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['orders/a.json', 'h1'],
|
|
['orders/b.json', 'h2'],
|
|
]);
|
|
|
|
await expect(
|
|
runner.run({ jobId: 'job-text-conflict', connectionId: 'warehouse', sourceKey: 'metabase', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).rejects.toThrow(/isolated diff textual conflict/);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-text-conflict/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('patch_textual_conflict');
|
|
expect(trace).toContain('textual_conflict_resolver_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('makes deterministic projection visible to child worktrees before WorkUnit synthesis', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'wiki-projected', rawFiles: ['projected/wiki.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
adapter.project = vi.fn(async ({ workdir }) => {
|
|
await mkdir(join(workdir, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(workdir, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
return {
|
|
warnings: [],
|
|
errors: [],
|
|
touchedSources: [{ connectionId: 'warehouse', sourceName: 'mart_account_segments' }],
|
|
changedWikiPageKeys: [],
|
|
};
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await expect(readFile(join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'), 'utf-8')).resolves.toContain(
|
|
'total_contract_arr',
|
|
);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/projected-orders.md'),
|
|
'---\nsummary: Projected orders\nusage_mode: auto\nsl_refs:\n - mart_account_segments\n---\n\nARR `mart_account_segments.total_contract_arr`.\n',
|
|
);
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: 'projected-orders', detail: 'Projected orders' });
|
|
await currentSession.gitService.commitFiles(['wiki/global/projected-orders.md'], 'wu projected wiki', 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['projected/wiki.json', 'h1']]);
|
|
|
|
const result = await runner.run({ jobId: 'job-projection', connectionId: 'warehouse', sourceKey: 'metabase', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } });
|
|
expect(result.failedWorkUnits).toEqual([]);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-projection/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('deterministic_projection_finished');
|
|
expect(trace).toContain('deterministic_projection_committed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes direct missing wiki sl_refs instead of rejecting the work unit', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'notion-page', rawFiles: ['pages/notion.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags?.operationName !== 'ingest-bundle-wu') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/notion-page.md'),
|
|
'---\nsummary: Notion page\nusage_mode: auto\nsl_refs:\n - missing_source\n---\n\nBody\n',
|
|
'utf-8',
|
|
);
|
|
currentSession.actions.push({ target: 'wiki', type: 'created', key: 'notion-page', detail: 'Notion page' });
|
|
await currentSession.gitService.commitFiles(['wiki/global/notion-page.md'], 'wu notion', 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['pages/notion.json', 'h1']]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-invalid-slrefs',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.commitSha).toBeTruthy();
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_sl_ref',
|
|
artifact: 'wiki/global/notion-page',
|
|
removedRef: 'missing_source',
|
|
absentTarget: 'missing_source',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/notion-page.md'), 'utf-8')).resolves.not.toContain(
|
|
'missing_source',
|
|
);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('runs final artifact gates after reconciliation mutates the integration tree', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'card-source', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
if (params.telemetryTags.operationName === 'ingest-bundle-wu') {
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'mart_account_segments',
|
|
detail: 'Source with renamed ARR measure',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml'],
|
|
'wu source',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
} else {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\nsl_refs:\n - mart_account_segments\n---\n\nReconcile wrote stale ARR `mart_account_segments.total_contract_arr_cents`.\n',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'account-segments',
|
|
detail: 'Stale reconcile wiki page',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(['wiki/global/account-segments.md'], 'reconcile wiki', 'ktx Test', 'system@ktx.local');
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-reconcile-stale',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-reconcile-stale/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('reconciliation_finished');
|
|
expect(trace).toContain('final_artifact_gates_finished');
|
|
expect(trace).toContain('final_gate_prune_finished');
|
|
expect(trace).toContain('squash_finished');
|
|
expect(trace).not.toContain('ingest_failed');
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/account-segments.md'), 'utf-8')).resolves.not.toContain(
|
|
'total_contract_arr_cents',
|
|
);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('stores final gate prune details in the success report and trace', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
const createdReports: any[] = [];
|
|
deps.reports.create = vi.fn(async (args: any) => {
|
|
createdReports.push(args);
|
|
return { id: `report-${createdReports.length}` };
|
|
});
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'card-wiki', rawFiles: ['cards/wiki.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'card-source', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
if (params.telemetryTags.unitKey === 'card-wiki') {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\n---\n\nARR is `mart_account_segments.total_contract_arr_cents`.\n',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'account-segments',
|
|
detail: 'Account segments',
|
|
rawPaths: ['cards/wiki.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(['wiki/global/account-segments.md'], 'wu wiki', 'ktx Test', 'system@ktx.local');
|
|
}
|
|
if (params.telemetryTags.unitKey === 'card-source') {
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'mart_account_segments',
|
|
detail: 'Dollar measure',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml'],
|
|
'wu source',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['cards/wiki.json', 'h1'],
|
|
['cards/source.json', 'h2'],
|
|
]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-trace-failure',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
const successReport = createdReports.find((report) => report.body.status === 'completed');
|
|
expect(successReport.body.tracePath).toContain('job-trace-failure/trace.jsonl');
|
|
expect(successReport.body.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
|
|
const events = (await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-trace-failure/trace.jsonl'), 'utf-8'))
|
|
.trim()
|
|
.split('\n')
|
|
.map((line) => JSON.parse(line));
|
|
expect(events.map((event) => event.event)).toEqual(
|
|
expect.arrayContaining([
|
|
'ingest_started',
|
|
'input_snapshot',
|
|
'work_units_planned',
|
|
'isolated_diff_enabled',
|
|
'work_unit_child_created',
|
|
'work_unit_patch_collected',
|
|
'patch_apply_started',
|
|
'patch_accepted',
|
|
'reconciliation_finished',
|
|
'final_artifact_gates_finished',
|
|
'final_gate_reference_pruned',
|
|
'final_gate_prune_committed',
|
|
'final_gate_prune_finished',
|
|
'squash_finished',
|
|
]),
|
|
);
|
|
expect(events.map((event) => event.event)).not.toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('rejects invalid provenance raw paths before squash reaches main', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
const createdReports: any[] = [];
|
|
deps.reports.create = vi.fn(async (args: any) => {
|
|
createdReports.push(args);
|
|
return { id: `report-${createdReports.length}` };
|
|
});
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{
|
|
unitKey: 'card-valid-artifacts',
|
|
rawFiles: ['cards/source.json'],
|
|
peerFileIndex: [],
|
|
dependencyPaths: [],
|
|
},
|
|
],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
await writeFile(
|
|
join(root, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\nsl_refs:\n - mart_account_segments\n---\n\nARR is `mart_account_segments.total_contract_arr`.\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'mart_account_segments',
|
|
detail: 'Valid source',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'account-segments',
|
|
detail: 'Valid wiki with invalid provenance raw path',
|
|
rawPaths: ['cards/missing.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml', 'wiki/global/account-segments.md'],
|
|
'valid artifacts with invalid provenance',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-invalid-provenance',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).rejects.toThrow(/provenance row references raw path outside this snapshot: cards\/missing\.json/);
|
|
|
|
expect(await runtime.git.revParseHead()).toBe(preRunHead);
|
|
expect(deps.provenance.insertMany).not.toHaveBeenCalled();
|
|
|
|
const failureReport = createdReports.find((report) => report.body.status === 'failed');
|
|
expect(failureReport.body.tracePath).toContain('job-invalid-provenance/trace.jsonl');
|
|
expect(failureReport.body.failure).toMatchObject({
|
|
phase: 'provenance_validation',
|
|
message: expect.stringContaining('cards/missing.json'),
|
|
});
|
|
expect(failureReport.body.failure.details).toMatchObject({
|
|
invalidRawPaths: ['cards/missing.json'],
|
|
currentRawPaths: ['cards/source.json'],
|
|
invalidRows: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
row: expect.objectContaining({
|
|
rawPath: 'cards/missing.json',
|
|
artifactKind: 'wiki',
|
|
artifactKey: 'account-segments',
|
|
actionType: 'wiki_written',
|
|
}),
|
|
origin: expect.objectContaining({
|
|
source: 'work_unit_action',
|
|
unitKey: 'card-valid-artifacts',
|
|
actionIndex: 1,
|
|
unitRawFiles: ['cards/source.json'],
|
|
action: expect.objectContaining({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'account-segments',
|
|
rawPaths: ['cards/missing.json'],
|
|
}),
|
|
}),
|
|
}),
|
|
]),
|
|
});
|
|
expect(failureReport.body.provenanceRows).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({ rawPath: 'cards/source.json', artifactKind: 'sl', artifactKey: 'mart_account_segments' }),
|
|
expect.objectContaining({ rawPath: 'cards/missing.json', artifactKind: 'wiki', artifactKey: 'account-segments' }),
|
|
]),
|
|
);
|
|
expect(failureReport.body.workUnits).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
unitKey: 'card-valid-artifacts',
|
|
rawFiles: ['cards/source.json'],
|
|
actions: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
target: 'wiki',
|
|
key: 'account-segments',
|
|
rawPaths: ['cards/missing.json'],
|
|
}),
|
|
]),
|
|
}),
|
|
]),
|
|
);
|
|
|
|
const events = (await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-invalid-provenance/trace.jsonl'), 'utf-8'))
|
|
.trim()
|
|
.split('\n')
|
|
.map((line) => JSON.parse(line));
|
|
expect(events.map((event) => event.event)).toEqual(
|
|
expect.arrayContaining([
|
|
'final_artifact_gates_finished',
|
|
'provenance_rows_validation_failed',
|
|
'ingest_failed',
|
|
'failure_report_created',
|
|
]),
|
|
);
|
|
expect(events.map((event) => event.event)).not.toContain('squash_finished');
|
|
const validationFailure = events.find((event) => event.event === 'provenance_rows_validation_failed');
|
|
expect(validationFailure).toMatchObject({
|
|
phase: 'provenance',
|
|
data: {
|
|
invalidRawPaths: ['cards/missing.json'],
|
|
currentRawPaths: ['cards/source.json'],
|
|
invalidRows: expect.arrayContaining([
|
|
expect.objectContaining({
|
|
row: expect.objectContaining({ rawPath: 'cards/missing.json' }),
|
|
origin: expect.objectContaining({
|
|
source: 'work_unit_action',
|
|
unitKey: 'card-valid-artifacts',
|
|
actionIndex: 1,
|
|
}),
|
|
}),
|
|
]),
|
|
},
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('rejects slDisallowed patches that touch semantic-layer files', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{
|
|
unitKey: 'lookml-mismatch',
|
|
rawFiles: ['views/orders.lkml'],
|
|
peerFileIndex: [],
|
|
dependencyPaths: [],
|
|
slDisallowed: true,
|
|
slDisallowedReason: 'lookml_connection_mismatch',
|
|
},
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/orders.yaml'),
|
|
'name: orders\ngrain: [id]\ncolumns: [{name: id, type: string}]\njoins: []\nmeasures: []\n',
|
|
);
|
|
currentSession.actions.push({ target: 'sl', type: 'created', key: 'orders', detail: 'forbidden', targetConnectionId: 'warehouse' });
|
|
await currentSession.gitService.commitFiles(['semantic-layer/warehouse/orders.yaml'], 'forbidden sl', 'ktx Test', 'system@ktx.local');
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['views/orders.lkml', 'h1']]);
|
|
|
|
await expect(
|
|
runner.run({ jobId: 'job-sl-disallowed', connectionId: 'warehouse', sourceKey: 'metabase', trigger: 'upload', bundleRef: { kind: 'upload', uploadId: 'upload' } }),
|
|
).rejects.toThrow(/isolated diff textual conflict/);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-sl-disallowed/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('patch_policy_rejected');
|
|
expect(trace).toContain('slDisallowed WorkUnit lookml-mismatch touched semantic-layer/warehouse/orders.yaml');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes final wiki refs broken by another accepted WorkUnit before squash', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
await mkdir(join(runtime.configDir, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/source-page.md'),
|
|
'---\nsummary: Source page\nusage_mode: auto\n---\n\nSource page\n',
|
|
);
|
|
await runtime.git.commitFiles(['wiki/global/source-page.md'], 'seed source page', 'ktx Test', 'system@ktx.local');
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [
|
|
{ unitKey: 'page-ref', rawFiles: ['pages/ref.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
{ unitKey: 'page-delete', rawFiles: ['pages/delete.json'], peerFileIndex: [], dependencyPaths: [] },
|
|
],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
if (params.telemetryTags.unitKey === 'page-ref') {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\nrefs:\n - source-page\n---\n\nSee [[source-page]].\n',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'account-segments',
|
|
detail: 'Page with wiki ref',
|
|
rawPaths: ['pages/ref.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/account-segments.md'],
|
|
'wu page ref',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
if (params.telemetryTags.unitKey === 'page-delete') {
|
|
await rm(join(root, 'wiki/global/source-page.md'), { force: true });
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'removed',
|
|
key: 'source-page',
|
|
detail: 'Delete referenced page',
|
|
rawPaths: ['pages/delete.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/source-page.md'],
|
|
'wu delete source page',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['pages/ref.json', 'h1'],
|
|
['pages/delete.json', 'h2'],
|
|
]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-wiki-ref-conflict',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(await runtime.git.revParseHead()).not.toBe(preRunHead);
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'source-page',
|
|
absentTarget: 'source-page',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/account-segments.md'), 'utf-8')).resolves.not.toContain(
|
|
'source-page',
|
|
);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-wiki-ref-conflict/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('final_artifact_gates_finished');
|
|
expect(trace).toContain('final_gate_reference_pruned');
|
|
expect(trace).toContain('squash_finished');
|
|
expect(trace).not.toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes unchanged inbound wiki refs broken by an isolated wiki deletion', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
await mkdir(join(runtime.configDir, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/source-page.md'),
|
|
'---\nsummary: Source page\nusage_mode: auto\n---\n\nSource page\n',
|
|
);
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\nrefs:\n - source-page\n---\n\nSee [[source-page]].\n',
|
|
);
|
|
await runtime.git.commitFiles(
|
|
['wiki/global/source-page.md', 'wiki/global/account-segments.md'],
|
|
'seed inbound wiki refs',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'delete-target-page', rawFiles: ['pages/delete.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.unitKey !== 'delete-target-page') {
|
|
return { stopReason: 'natural' };
|
|
}
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await rm(join(root, 'wiki/global/source-page.md'), { force: true });
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'removed',
|
|
key: 'source-page',
|
|
detail: 'Delete referenced page',
|
|
rawPaths: ['pages/delete.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/source-page.md'],
|
|
'wu delete target page',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['pages/delete.json', 'h1']]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-existing-wiki-ref-stale',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(await runtime.git.revParseHead()).not.toBe(preRunHead);
|
|
expect(result.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'source-page',
|
|
absentTarget: 'source-page',
|
|
});
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/account-segments.md'), 'utf-8')).resolves.not.toContain(
|
|
'source-page',
|
|
);
|
|
const events = (await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-existing-wiki-ref-stale/trace.jsonl'), 'utf-8'))
|
|
.trim()
|
|
.split('\n')
|
|
.map((line) => JSON.parse(line));
|
|
expect(events.map((event) => event.event)).toEqual(
|
|
expect.arrayContaining([
|
|
'final_artifact_gates_started',
|
|
'final_artifact_gates_finished',
|
|
'final_gate_reference_pruned',
|
|
'final_gate_prune_committed',
|
|
'final_gate_prune_finished',
|
|
'squash_finished',
|
|
]),
|
|
);
|
|
expect(events.map((event) => event.event)).not.toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('rejects WorkUnit patches that touch unauthorized semantic-layer target connections', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'finance-source', rawFiles: ['cards/finance.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/finance'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/finance/orders.yaml'),
|
|
'name: orders\ngrain: [id]\ncolumns: [{name: id, type: string}]\njoins: []\nmeasures: []\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'finance', 'orders');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'orders',
|
|
detail: 'Unauthorized target',
|
|
targetConnectionId: 'finance',
|
|
rawPaths: ['cards/finance.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/finance/orders.yaml'],
|
|
'wu unauthorized target',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/finance.json', 'h1']]);
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-unauthorized-wu-target',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).rejects.toThrow(/isolated diff textual conflict.*semantic-layer target connection not allowed/);
|
|
|
|
expect(await runtime.git.revParseHead()).toBe(preRunHead);
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-unauthorized-wu-target/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('patch_policy_rejected');
|
|
expect(trace).toContain('semantic-layer/finance/orders.yaml');
|
|
expect(trace).toContain('allowedTargetConnectionIds');
|
|
expect(trace).toContain('ingest_failed');
|
|
expect(trace).toContain('failure_report_created');
|
|
expect(trace).not.toContain('squash_finished');
|
|
|
|
const failureReport = (deps.reports.create as any).mock.calls
|
|
.map((call: any[]) => call[0])
|
|
.find((report: any) => report.body.status === 'failed');
|
|
expect(failureReport.body.failure).toMatchObject({
|
|
phase: 'integration',
|
|
message: expect.stringContaining('semantic-layer target connection not allowed'),
|
|
});
|
|
expect(failureReport.body.failure.details).toMatchObject({
|
|
unitKey: 'finance-source',
|
|
allowedTargetConnectionIds: ['warehouse'],
|
|
touchedPaths: ['semantic-layer/finance/orders.yaml'],
|
|
reason: expect.stringContaining('semantic-layer/finance/orders.yaml (finance)'),
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('rejects reconciliation mutations that touch unauthorized semantic-layer target connections before squash', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'valid-page', rawFiles: ['pages/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
if (params.telemetryTags.operationName === 'ingest-bundle-wu') {
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(join(root, 'wiki/global/valid-page.md'), '---\nsummary: Valid page\nusage_mode: auto\n---\n\nValid\n');
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'valid-page',
|
|
detail: 'Valid page',
|
|
rawPaths: ['pages/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(['wiki/global/valid-page.md'], 'wu valid page', 'ktx Test', 'system@ktx.local');
|
|
} else {
|
|
await mkdir(join(root, 'semantic-layer/finance'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'semantic-layer/finance/reconcile_orders.yaml'),
|
|
'name: reconcile_orders\ngrain: [id]\ncolumns: [{name: id, type: string}]\njoins: []\nmeasures: []\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'finance', 'reconcile_orders');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'reconcile_orders',
|
|
detail: 'Unauthorized reconcile target',
|
|
targetConnectionId: 'finance',
|
|
rawPaths: ['pages/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/finance/reconcile_orders.yaml'],
|
|
'reconcile unauthorized target',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
}
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['pages/source.json', 'h1']]);
|
|
const preRunHead = await runtime.git.revParseHead();
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-unauthorized-reconcile-target',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).rejects.toThrow(/semantic-layer target connection not allowed/);
|
|
|
|
expect(await runtime.git.revParseHead()).toBe(preRunHead);
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-unauthorized-reconcile-target/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('semantic_layer_target_policy_started');
|
|
expect(trace).toContain('semantic_layer_target_policy_failed');
|
|
expect(trace).toContain('allowedTargetConnectionIds');
|
|
expect(trace).toContain('semantic-layer/finance/reconcile_orders.yaml');
|
|
expect(trace).toContain('ingest_failed');
|
|
expect(trace).toContain('failure_report_created');
|
|
expect(trace).not.toContain('squash_finished');
|
|
const failureReport = (deps.reports.create as any).mock.calls
|
|
.map((call: any[]) => call[0])
|
|
.find((report: any) => report.body.status === 'failed');
|
|
expect(failureReport.body.failure).toMatchObject({
|
|
phase: 'target_policy',
|
|
message: expect.stringContaining('semantic-layer target connection not allowed'),
|
|
});
|
|
expect(failureReport.body.failure.details).toMatchObject({
|
|
allowedTargetConnectionIds: ['warehouse'],
|
|
touchedPaths: expect.arrayContaining(['semantic-layer/finance/reconcile_orders.yaml']),
|
|
});
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('repairs additive same-source textual conflicts before final gates and squash', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps } = makeDeps(runtime);
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.telemetryTags.operationName === 'ingest-isolated-diff-textual-resolver') {
|
|
const current = await params.toolSet.read_repair_file.execute({
|
|
path: 'semantic-layer/warehouse/mart_account_segments.yaml',
|
|
});
|
|
expect(current.markdown).toContain('total_contract_arr_cents');
|
|
const patch = await params.toolSet.read_failed_patch.execute({});
|
|
expect(patch.markdown).toContain('account_count');
|
|
await params.toolSet.write_repair_file.execute({
|
|
path: 'semantic-layer/warehouse/mart_account_segments.yaml',
|
|
content:
|
|
'name: mart_account_segments\n' +
|
|
'grain: [account_id]\n' +
|
|
'columns: [{name: account_id, type: string}]\n' +
|
|
'joins: []\n' +
|
|
'measures:\n' +
|
|
' - name: total_contract_arr_cents\n' +
|
|
' expr: sum(contract_arr)\n' +
|
|
' - name: account_count\n' +
|
|
' expr: count_distinct(account_id)\n',
|
|
});
|
|
return { stopReason: 'natural' };
|
|
}
|
|
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'semantic-layer/warehouse'), { recursive: true });
|
|
if (params.telemetryTags.unitKey === 'card-wiki') {
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\n' +
|
|
'grain: [account_id]\n' +
|
|
'columns: [{name: account_id, type: string}]\n' +
|
|
'joins: []\n' +
|
|
'measures:\n' +
|
|
' - name: total_contract_arr_cents\n' +
|
|
' expr: sum(contract_arr)\n',
|
|
);
|
|
} else if (params.telemetryTags.unitKey === 'card-source') {
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\n' +
|
|
'grain: [account_id]\n' +
|
|
'columns: [{name: account_id, type: string}]\n' +
|
|
'joins: []\n' +
|
|
'measures:\n' +
|
|
' - name: account_count\n' +
|
|
' expr: count_distinct(account_id)\n',
|
|
);
|
|
}
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'updated',
|
|
key: 'mart_account_segments',
|
|
detail: 'Updated account segments source',
|
|
targetConnectionId: 'warehouse',
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml'],
|
|
`wu ${params.telemetryTags.unitKey}`,
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [
|
|
['cards/wiki.json', 'hash-a'],
|
|
['cards/source.json', 'hash-b'],
|
|
]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-resolver-e2e',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'manual_resync',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload-1' },
|
|
});
|
|
|
|
expect(result.commitSha).toBeTruthy();
|
|
const source = await readFile(join(runtime.configDir, 'semantic-layer/warehouse/mart_account_segments.yaml'), 'utf-8');
|
|
expect(source).toContain('total_contract_arr_cents');
|
|
expect(source).toContain('account_count');
|
|
expect(deps.agentRunner.runLoop).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
modelRole: 'repair',
|
|
telemetryTags: expect.objectContaining({
|
|
operationName: 'ingest-isolated-diff-textual-resolver',
|
|
unitKey: 'card-source',
|
|
}),
|
|
}),
|
|
);
|
|
const successReport = (deps.reports.create as any).mock.calls.at(-1)?.[0]?.body;
|
|
expect(successReport.isolatedDiff).toMatchObject({
|
|
acceptedPatches: 2,
|
|
textualConflicts: 1,
|
|
semanticConflicts: 0,
|
|
resolverAttempts: 1,
|
|
resolverRepairs: 1,
|
|
resolverFailures: 0,
|
|
});
|
|
const trace = await readFile(join(runtime.configDir, '.ktx/ingest-traces/job-resolver-e2e/trace.jsonl'), 'utf-8');
|
|
expect(trace).toContain('textual_conflict_resolver_repaired');
|
|
expect(trace).toContain('patch_accepted_after_textual_resolution');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('prunes final wiki body refs before squash', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
await mkdir(join(runtime.configDir, 'semantic-layer/warehouse'), { recursive: true });
|
|
await mkdir(join(runtime.configDir, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(runtime.configDir, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr_cents\n expr: sum(contract_arr)\n',
|
|
);
|
|
await writeFile(
|
|
join(runtime.configDir, 'wiki/global/account-segments.md'),
|
|
'---\nsummary: Account segments\nusage_mode: auto\n---\n\nExisting ARR uses `mart_account_segments.total_contract_arr_cents`.\n',
|
|
);
|
|
await runtime.git.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml', 'wiki/global/account-segments.md'],
|
|
'seed stale wiki body ref',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'source-only', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async (params: any) => {
|
|
if (params.modelRole === 'reconcile') {
|
|
return { stopReason: 'natural' as const };
|
|
}
|
|
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await writeFile(
|
|
join(root, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
addTouchedSlSource(currentSession.touchedSlSources, 'warehouse', 'mart_account_segments');
|
|
currentSession.actions.push({
|
|
target: 'sl',
|
|
type: 'updated',
|
|
key: 'mart_account_segments',
|
|
detail: 'Rename ARR measure',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['semantic-layer/warehouse/mart_account_segments.yaml'],
|
|
'wu source rename',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' as const };
|
|
}) as never;
|
|
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
const result = await runner.run({
|
|
jobId: 'job-final-gate-prune',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
expect(result.commitSha).toBeTruthy();
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/account-segments.md'), 'utf-8')).resolves.not.toContain(
|
|
'total_contract_arr_cents',
|
|
);
|
|
const reportCreate = vi.mocked(deps.reports.create).mock.calls.at(-1)?.[0] as any;
|
|
expect(reportCreate.body.finalGatePrunedReferences).toContainEqual({
|
|
kind: 'wiki_body_ref',
|
|
artifact: 'wiki/global/account-segments',
|
|
removedRef: 'mart_account_segments.total_contract_arr_cents',
|
|
absentTarget: 'mart_account_segments.total_contract_arr_cents',
|
|
});
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-final-gate-prune/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace).toContain('final_gate_reference_pruned');
|
|
expect(trace).toContain('final_gate_prune_finished');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
it('runs finalization before wiki sl-ref repair and final gates', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'wiki-page', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
adapter.finalize = vi.fn(async ({ workdir }) => {
|
|
await mkdir(join(workdir, 'semantic-layer/warehouse'), { recursive: true });
|
|
await mkdir(join(workdir, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(workdir, 'semantic-layer/warehouse/mart_account_segments.yaml'),
|
|
'name: mart_account_segments\ngrain: [account_id]\ncolumns: [{name: account_id, type: string}]\njoins: []\nmeasures:\n - name: total_contract_arr\n expr: sum(contract_arr)\n',
|
|
);
|
|
await writeFile(
|
|
join(workdir, 'wiki/global/finalized-accounts.md'),
|
|
'---\nsummary: Finalized accounts\nusage_mode: auto\nsl_refs:\n - mart_account_segments\n - missing_source\n---\n\nAccounts use `mart_account_segments.total_contract_arr`.\n',
|
|
);
|
|
return {
|
|
warnings: [],
|
|
errors: [],
|
|
touchedSources: [{ connectionId: 'warehouse', sourceName: 'mart_account_segments' }],
|
|
changedWikiPageKeys: ['finalized-accounts'],
|
|
actions: [
|
|
{
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'mart_account_segments',
|
|
detail: 'Finalized accounts',
|
|
targetConnectionId: 'warehouse',
|
|
rawPaths: ['cards/source.json'],
|
|
},
|
|
{
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'finalized-accounts',
|
|
detail: 'Finalized wiki',
|
|
rawPaths: ['cards/source.json'],
|
|
},
|
|
],
|
|
};
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => ({ stopReason: 'natural' as const })) as never;
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
await runner.run({
|
|
jobId: 'job-finalization',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
});
|
|
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-finalization/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
expect(trace.indexOf('finalization_committed')).toBeLessThan(trace.indexOf('wiki_sl_refs_repaired'));
|
|
expect(trace.indexOf('wiki_sl_refs_repaired')).toBeLessThan(trace.indexOf('final_artifact_gates'));
|
|
await expect(readFile(join(runtime.configDir, 'wiki/global/finalized-accounts.md'), 'utf-8')).resolves.toContain(
|
|
'sl_refs:\n - mart_account_segments',
|
|
);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('fails when finalization edits a path already changed earlier in the run', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({
|
|
workUnits: [{ unitKey: 'wiki-page', rawFiles: ['cards/source.json'], peerFileIndex: [], dependencyPaths: [] }],
|
|
});
|
|
let currentSession: any = null;
|
|
deps.toolsetFactory.createIngestWuToolset = vi.fn((toolSession: any) => {
|
|
currentSession = toolSession;
|
|
return { toRuntimeTools: vi.fn(() => ({})) };
|
|
});
|
|
deps.agentRunner.runLoop = vi.fn(async () => {
|
|
const root = rootOfConfig(currentSession.configService, runtime.configDir);
|
|
await mkdir(join(root, 'wiki/global'), { recursive: true });
|
|
await writeFile(
|
|
join(root, 'wiki/global/orders.md'),
|
|
'---\nsummary: Orders\nusage_mode: auto\n---\n\nWU body\n',
|
|
);
|
|
currentSession.actions.push({
|
|
target: 'wiki',
|
|
type: 'created',
|
|
key: 'orders',
|
|
detail: 'WU orders',
|
|
rawPaths: ['cards/source.json'],
|
|
});
|
|
await currentSession.gitService.commitFiles(
|
|
['wiki/global/orders.md'],
|
|
'wu orders',
|
|
'ktx Test',
|
|
'system@ktx.local',
|
|
);
|
|
return { stopReason: 'natural' as const };
|
|
}) as never;
|
|
adapter.finalize = vi.fn(async ({ workdir }) => {
|
|
await writeFile(
|
|
join(workdir, 'wiki/global/orders.md'),
|
|
'---\nsummary: Orders\nusage_mode: auto\n---\n\nFinalized body\n',
|
|
);
|
|
return {
|
|
warnings: [],
|
|
errors: [],
|
|
touchedSources: [],
|
|
changedWikiPageKeys: ['orders'],
|
|
actions: [{ target: 'wiki', type: 'updated', key: 'orders', detail: 'Conflicting finalization' }],
|
|
};
|
|
});
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-finalization-overlap',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).rejects.toThrow(/finalization modified path\(s\) already changed earlier in this run: wiki\/global\/orders\.md/);
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('rejects finalization writes to unauthorized semantic-layer targets', async () => {
|
|
const runtime = await makeRealGitRuntime();
|
|
try {
|
|
const { deps, adapter } = makeDeps(runtime);
|
|
adapter.chunk.mockResolvedValue({ workUnits: [] });
|
|
adapter.finalize = vi.fn(async ({ workdir }) => {
|
|
await mkdir(join(workdir, 'semantic-layer/other-warehouse'), { recursive: true });
|
|
await writeFile(
|
|
join(workdir, 'semantic-layer/other-warehouse/orders.yaml'),
|
|
'name: orders\ngrain: [order_id]\ncolumns: [{name: order_id, type: string}]\njoins: []\nmeasures: []\n',
|
|
);
|
|
return {
|
|
warnings: [],
|
|
errors: [],
|
|
touchedSources: [{ connectionId: 'other-warehouse', sourceName: 'orders' }],
|
|
changedWikiPageKeys: [],
|
|
actions: [
|
|
{
|
|
target: 'sl',
|
|
type: 'created',
|
|
key: 'orders',
|
|
targetConnectionId: 'other-warehouse',
|
|
detail: 'Forbidden target',
|
|
rawPaths: ['cards/source.json'],
|
|
},
|
|
],
|
|
};
|
|
});
|
|
const runner = new IngestBundleRunner(deps);
|
|
await mockStageRawFiles(runner, runtime, [['cards/source.json', 'h1']]);
|
|
|
|
await expect(
|
|
runner.run({
|
|
jobId: 'job-finalization-target-policy',
|
|
connectionId: 'warehouse',
|
|
sourceKey: 'metabase',
|
|
trigger: 'upload',
|
|
bundleRef: { kind: 'upload', uploadId: 'upload' },
|
|
}),
|
|
).rejects.toThrow(/semantic-layer target connection not allowed/);
|
|
const trace = await readFile(
|
|
join(runtime.configDir, '.ktx/ingest-traces/job-finalization-target-policy/trace.jsonl'),
|
|
'utf-8',
|
|
);
|
|
// The policy check runs inside finalization, before touched-source
|
|
// derivation — an out-of-scope write fails the finalization stage
|
|
// instead of reading as committed.
|
|
expect(trace).not.toContain('finalization_committed');
|
|
expect(trace).toContain('semantic_layer_target_policy_failed');
|
|
expect(trace).toContain('ingest_failed');
|
|
} finally {
|
|
await rm(runtime.homeDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|