mirror of
https://github.com/Kaelio/ktx.git
synced 2026-07-04 10:52:13 +02:00
chore(workspace): gate dead-code with knip production mode (#196)
* refactor(workspace): relocate @ktx/llm source into packages/cli/src/llm * refactor(workspace): rewrite @ktx/llm imports to relative paths * refactor(workspace): fold internal packages into cli * chore(workspace): gate dead-code with knip production mode Turn on production-mode knip plus an autofix run in pre-commit and the `pnpm dead-code` script, document the `/** @internal */` convention for test-only exports in AGENTS.md, annotate test-only exports across the CLI with that JSDoc, and drop dead exports/wrappers the new gate surfaced (e.g. `cli-project.ts`, `lookerRuntimeSourceToFileAdapterSource`, `createLocalScanEnrichmentProvidersFromConfig`, `PGLITE_OWNER_PROCESS_BACKEND_CAPABILITIES`, stale type re-exports). Replace the loose `ignoreIssues` allowlist in `knip.json` with explicit production entries so cross-package barrel leaks are caught. * refactor(cli): delete internal barrel index.ts files The 34 `index.ts` re-export barrels inside `packages/cli/src/` were holdovers from the pre-fold multi-workspace structure. Post-fold-in they served no production purpose: external consumers go through the single package main entry, and in-repo callers mostly imported through them only because the path was short. Internally, knip flagged most barrel re-exports as production-dead (only reached via tests). This change: - Deletes every internal barrel except `packages/cli/src/index.ts` (the published package entry). - Rewrites ~270 source/test files to import each name directly from the file that defines it. - Moves `tools/warehouse-verification/index.ts` to `create-warehouse-verification-tools.ts` (the function it defined locally) and updates its single consumer. - Renames `search/backend-conformance.ts` → `.test-utils.ts` to match the existing test-helper file convention. - Deletes 13 dead test-only chains (dbt-descriptions/*, live-database/extracted-schema, live-database/structural-sync, relationship-* feedback/review chain) plus their tests and a cascading orphan integration test. - Updates test mocks that pointed at deleted barrel paths (notion-client, connector barrels in scan/local-scan-connectors tests) to mock the source files instead. - Points the maintainer benchmark script (`scripts/relationship-benchmark-report.mjs`) at source files instead of `dist/context/scan/index.js`. - Drops the barrel `!` entries from `knip.json`; adds explicit production entries only for the benchmark code reached via dist by the maintainer script. Net: 413 files changed, ~1.2k insertions, ~9.4k deletions. `pnpm run dead-code` (Biome + knip default + knip production) and `pnpm run type-check` are clean; 2277 tests pass. * refactor(workspace): rename @ktx/cli to @kaelio/ktx and pack it directly Promote the CLI workspace package to the public name `@kaelio/ktx` and drop the separate `scripts/build-public-npm-package.mjs` wrapper. The CLI package is now publishable in place (`publishConfig.access: public`, `provenance: true`), so artifact packing uses `pnpm pack` against `packages/cli/` instead of assembling a parallel package tree. Updates all workspace filter invocations, docs, tests, and release readiness checks to reference the new package name, and folds the tarball-name helper into `scripts/public-npm-release-metadata.mjs`. * docs: align "agent clients" and "data agents" terminology Replace "client agents" with "agent clients" and "database agents" with "data agents" across AGENTS.md, README.md, the docs-site copy, and the matching setup-agents test description, matching the canonical vocabulary in docs/terminology.md. Also moves packages/cli/tsconfig.json's tsBuildInfoFile from node_modules/.cache/ to dist/.tsbuildinfo so incremental builds survive node_modules reinstalls. * refactor(release): single source of truth for package version Make packages/cli/package.json the single source of truth for the @kaelio/ktx version. publicNpmPackageVersion() now reads it directly, so artifact filenames, release-readiness checks, and the Python wheel version all derive from one field. The duplicate release-policy.json.publicNpmPackageVersion is removed. Previously the two fields could drift: tarballs were named kaelio-ktx-0.4.1.tgz while internally containing @kaelio/ktx@0.0.0-private. - update-public-release-version.mjs rewrites both Python pyproject.toml files (ktx-daemon, ktx-sl) alongside the npm package.jsons, normalizing the version for PEP 440 (e.g. 0.1.0-rc.2 -> 0.1.0rc2). - semantic-release-config.cjs adds the two pyproject.toml files to @semantic-release/git assets so the release commit back to main carries every version source in lockstep. - The six "?? '0.0.0-private'" fallback literals across the CLI are replaced with "?? getKtxCliPackageInfo().version", and createDefaultKtxMcpServer makes its version arg required. - docs/release.md describes the actual commit-back model: the dev tree always reflects the most recent release; no sentinel pin to maintain. Verified: pnpm run artifacts:build now produces kaelio-ktx-0.4.1.tgz and kaelio_ktx-0.4.1-py3-none-any.whl with @kaelio/ktx@0.4.1 inside. Full type-check, dead-code, and 2287 vitests + 173 script tests pass. * refactor(cli): inject embedding provider resolution and detect sentence-transformers runtime Make resolveProjectEmbeddingProvider and runtimeIo injectable in ingest and scan command entrypoints so tests can stub them, and teach resolvePublicIngestRuntimeRequirements to flag the local-embeddings runtime feature when ktx.yaml selects sentence-transformers. * chore(cli): mark buildLocalStatsStatus and LocalStatsStatus as @internal Both symbols are consumed only by status-project.test.ts. Annotating with /** @internal */ keeps knip's production-mode check clean without changing runtime behavior. * fix(cli): use real package metadata in print-command-tree The stubbed package name embedded a forbidden product identifier that tripped the boundary check in CI. Read the metadata from package.json instead — keeps the rendered tree unchanged and removes a duplicate source of truth. * feat(cli): show embedding coverage in `ktx status`, drop duplicate disk counts Inline `(N embedded)` next to the Wiki scope counts and Semantic-layer source counts, computed with `SUM(embedding_json IS NOT NULL)` over `knowledge_pages` and `local_sl_sources`. Rename the "Knowledge" label to "Wiki" (canonical per `docs/terminology.md`) and rename the matching `localStats.knowledgePages` field to `localStats.wikiPages`. Drop `wiki=N md` and `semantic-layer=N yaml` from the Disk row — those duplicated the per-surface rows above. Disk now reports only actual byte usage (db, cache, raw-sources). The unused `wikiGlobalMarkdownCount` / `semanticLayerYamlCount` fields, the `isMarkdownEntry` / `isYamlEntry` helpers, and the `filter` arg on `summarizeDir` are removed.
This commit is contained in:
parent
a1cfb03d73
commit
2366b00301
1002 changed files with 2286 additions and 12051 deletions
600
packages/cli/src/context/tools/context-evidence-tools.test.ts
Normal file
600
packages/cli/src/context/tools/context-evidence-tools.test.ts
Normal file
|
|
@ -0,0 +1,600 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import { mkdtemp, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { KtxEmbeddingPort } from '../../context/core/embedding.js';
|
||||
import { SqliteContextEvidenceStore } from '../ingest/context-evidence/sqlite-context-evidence-store.js';
|
||||
import { ContextCandidateMarkTool } from './context-candidate-mark.tool.js';
|
||||
import { ContextCandidateWriteTool } from './context-candidate-write.tool.js';
|
||||
import { ContextEvidenceNeighborsTool } from './context-evidence-neighbors.tool.js';
|
||||
import { ContextEvidenceReadTool } from './context-evidence-read.tool.js';
|
||||
import { ContextEvidenceSearchTool } from './context-evidence-search.tool.js';
|
||||
import type { ContextEvidenceToolStorePort } from './context-evidence-tool-store.js';
|
||||
import { createTouchedSlSources } from '../../context/tools/touched-sl-sources.js';
|
||||
import type { ToolContext } from '../../context/tools/base-tool.js';
|
||||
import type { ToolSession } from '../../context/tools/tool-session.js';
|
||||
|
||||
const ingestContext = (): ToolContext => ({
|
||||
sourceId: 'ingest',
|
||||
messageId: 'job-1-wu-unit-1',
|
||||
userId: 'system',
|
||||
connectionId: '00000000-0000-0000-0000-000000000001',
|
||||
ingest: {
|
||||
runId: '10000000-0000-0000-0000-000000000001',
|
||||
jobId: 'job-1',
|
||||
syncId: 'sync-1',
|
||||
sourceKey: 'notion',
|
||||
},
|
||||
session: {
|
||||
connectionId: '00000000-0000-0000-0000-000000000001',
|
||||
isWorktreeScoped: true,
|
||||
preHead: 'abc123',
|
||||
touchedSlSources: createTouchedSlSources(),
|
||||
actions: [],
|
||||
ingest: {
|
||||
runId: '10000000-0000-0000-0000-000000000001',
|
||||
jobId: 'job-1',
|
||||
syncId: 'sync-1',
|
||||
sourceKey: 'notion',
|
||||
},
|
||||
} as unknown as ToolSession,
|
||||
});
|
||||
|
||||
const makeEmbeddingService = (overrides: Partial<KtxEmbeddingPort> = {}) =>
|
||||
({
|
||||
computeEmbedding: vi.fn().mockResolvedValue([0.25, 0.5, 0.75]),
|
||||
...overrides,
|
||||
}) as Partial<KtxEmbeddingPort> as KtxEmbeddingPort;
|
||||
|
||||
describe('context evidence tools', () => {
|
||||
it('searches context evidence with ingest defaults', async () => {
|
||||
const repository = {
|
||||
searchRRF: vi.fn().mockResolvedValue([
|
||||
{
|
||||
chunkId: 'chunk-1',
|
||||
documentId: 'doc-1',
|
||||
externalId: 'page-1',
|
||||
title: 'Revenue Recognition',
|
||||
path: 'Company Handbook / Finance / Revenue Recognition',
|
||||
url: 'https://notion.example/page-1',
|
||||
snippet: 'Booked revenue excludes refunds and test accounts.',
|
||||
score: 0.35,
|
||||
citation: { source: 'notion', pageId: 'page-1', rawPath: 'pages/page-1/page.md' },
|
||||
stableCitationKey: 'notion:page-1:policy:abc',
|
||||
syncId: 'sync-1',
|
||||
lastEditedAt: new Date('2026-04-12T10:15:00.000Z'),
|
||||
matchReasons: ['lexical', 'semantic'],
|
||||
lanes: [
|
||||
{
|
||||
lane: 'lexical',
|
||||
status: 'available',
|
||||
requestedCandidatePoolLimit: 25,
|
||||
effectiveCandidatePoolLimit: 25,
|
||||
returnedCandidateCount: 1,
|
||||
weight: 1.5,
|
||||
},
|
||||
{
|
||||
lane: 'semantic',
|
||||
status: 'available',
|
||||
requestedCandidatePoolLimit: 25,
|
||||
effectiveCandidatePoolLimit: 25,
|
||||
returnedCandidateCount: 1,
|
||||
weight: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
]),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
const embeddings = {
|
||||
computeEmbedding: vi.fn().mockResolvedValue([0.1, ...Array.from({ length: 383 }, () => 0)]),
|
||||
} as Partial<KtxEmbeddingPort> as KtxEmbeddingPort;
|
||||
|
||||
const tool = new ContextEvidenceSearchTool(repository, embeddings);
|
||||
const result = await tool.call({ query: 'revenue refunds', limit: 5, includeDeleted: false }, ingestContext());
|
||||
|
||||
expect(repository.searchRRF).toHaveBeenCalledWith({
|
||||
connectionId: '00000000-0000-0000-0000-000000000001',
|
||||
sourceKey: 'notion',
|
||||
queryEmbedding: [0.1, ...Array.from({ length: 383 }, () => 0)],
|
||||
queryText: 'revenue refunds',
|
||||
limit: 5,
|
||||
includeDeleted: false,
|
||||
currentRunId: '10000000-0000-0000-0000-000000000001',
|
||||
});
|
||||
expect(result.markdown).toContain('Revenue Recognition');
|
||||
expect(result.markdown).toContain('matchReasons: lexical, semantic');
|
||||
expect(result.structured.success).toBe(true);
|
||||
if (result.structured.success) {
|
||||
expect(result.structured.results[0]).toMatchObject({
|
||||
chunkId: 'chunk-1',
|
||||
stableCitationKey: 'notion:page-1:policy:abc',
|
||||
matchReasons: ['lexical', 'semantic'],
|
||||
lanes: expect.arrayContaining([expect.objectContaining({ lane: 'lexical', status: 'available' })]),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('returns a structured ingest metadata error outside ingest sessions', async () => {
|
||||
const tool = new ContextEvidenceSearchTool(
|
||||
{ searchRRF: vi.fn() } as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort,
|
||||
{ computeEmbedding: vi.fn() } as Partial<KtxEmbeddingPort> as KtxEmbeddingPort,
|
||||
);
|
||||
|
||||
const result = await tool.call(
|
||||
{ query: 'revenue', limit: 5, includeDeleted: false },
|
||||
{ sourceId: 'research', messageId: 'm1', userId: 'user-1' },
|
||||
);
|
||||
|
||||
expect(result.structured).toMatchObject({ success: false, error: 'INGEST_METADATA_REQUIRED' });
|
||||
});
|
||||
|
||||
it('reads a full document by external id', async () => {
|
||||
const repository = {
|
||||
readDocumentByExternalId: vi.fn().mockResolvedValue({
|
||||
document: {
|
||||
id: 'doc-1',
|
||||
title: 'Onboarding SOP',
|
||||
path: 'Ops / Onboarding SOP',
|
||||
external_id: 'page-ops',
|
||||
raw_path: 'pages/page-ops/page.md',
|
||||
url: 'https://notion.example/page-ops',
|
||||
},
|
||||
chunks: [
|
||||
{
|
||||
id: 'chunk-1',
|
||||
heading_path: ['Onboarding SOP', 'Checklist'],
|
||||
content: 'Create account, invite to workspace, confirm dashboard access.',
|
||||
citation: { source: 'notion', pageId: 'page-ops' },
|
||||
},
|
||||
],
|
||||
}),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
|
||||
const tool = new ContextEvidenceReadTool(repository);
|
||||
const result = await tool.call({ externalId: 'page-ops', includeNeighborChunks: false }, ingestContext());
|
||||
|
||||
expect(repository.readDocumentByExternalId).toHaveBeenCalledWith(
|
||||
'00000000-0000-0000-0000-000000000001',
|
||||
'notion',
|
||||
'page-ops',
|
||||
'10000000-0000-0000-0000-000000000001',
|
||||
);
|
||||
expect(result.markdown).toContain('## Onboarding SOP');
|
||||
expect(result.markdown).toContain('Create account');
|
||||
expect(result.structured.success).toBe(true);
|
||||
if (result.structured.success) {
|
||||
expect(result.structured.found).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('reads documents and chunks by id with connection and source scope', async () => {
|
||||
const repository = {
|
||||
readDocumentById: vi.fn().mockResolvedValue({
|
||||
document: {
|
||||
id: '00000000-0000-0000-0000-000000000201',
|
||||
title: 'Scoped Document',
|
||||
path: 'Scoped Document',
|
||||
external_id: 'page-scoped',
|
||||
url: null,
|
||||
},
|
||||
chunks: [{ id: 'chunk-1', content: 'Scoped content.' }],
|
||||
}),
|
||||
readChunkById: vi.fn().mockResolvedValue({
|
||||
document: {
|
||||
id: '00000000-0000-0000-0000-000000000201',
|
||||
title: 'Scoped Document',
|
||||
path: 'Scoped Document',
|
||||
external_id: 'page-scoped',
|
||||
url: null,
|
||||
},
|
||||
chunk: {
|
||||
id: '00000000-0000-0000-0000-000000000301',
|
||||
content: 'Scoped chunk.',
|
||||
citation: { source: 'notion' },
|
||||
},
|
||||
}),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
|
||||
const tool = new ContextEvidenceReadTool(repository);
|
||||
await tool.call(
|
||||
{ documentId: '00000000-0000-0000-0000-000000000201', includeNeighborChunks: false },
|
||||
ingestContext(),
|
||||
);
|
||||
await tool.call({ chunkId: '00000000-0000-0000-0000-000000000301', includeNeighborChunks: false }, ingestContext());
|
||||
|
||||
expect(repository.readDocumentById).toHaveBeenCalledWith(
|
||||
'00000000-0000-0000-0000-000000000201',
|
||||
'00000000-0000-0000-0000-000000000001',
|
||||
'notion',
|
||||
'10000000-0000-0000-0000-000000000001',
|
||||
);
|
||||
expect(repository.readChunkById).toHaveBeenCalledWith(
|
||||
'00000000-0000-0000-0000-000000000301',
|
||||
'00000000-0000-0000-0000-000000000001',
|
||||
'notion',
|
||||
'10000000-0000-0000-0000-000000000001',
|
||||
);
|
||||
});
|
||||
|
||||
it('lists evidence neighbors', async () => {
|
||||
const repository = {
|
||||
findNeighborDocuments: vi.fn().mockResolvedValue([
|
||||
{
|
||||
documentId: 'doc-child',
|
||||
externalId: 'page-child',
|
||||
title: 'Revenue Caveats',
|
||||
path: 'Company Handbook / Finance / Revenue Caveats',
|
||||
relation: 'children',
|
||||
url: null,
|
||||
lastEditedAt: null,
|
||||
},
|
||||
]),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
|
||||
const tool = new ContextEvidenceNeighborsTool(repository);
|
||||
const result = await tool.call({ documentId: 'doc-1', relation: 'children', limit: 10 }, ingestContext());
|
||||
|
||||
expect(repository.findNeighborDocuments).toHaveBeenCalledWith({
|
||||
connectionId: '00000000-0000-0000-0000-000000000001',
|
||||
sourceKey: 'notion',
|
||||
documentId: 'doc-1',
|
||||
relation: 'children',
|
||||
limit: 10,
|
||||
currentRunId: '10000000-0000-0000-0000-000000000001',
|
||||
});
|
||||
expect(result.markdown).toContain('Revenue Caveats');
|
||||
});
|
||||
|
||||
it('writes a cited candidate with durable evidence refs', async () => {
|
||||
const repository = {
|
||||
readChunksByIds: vi.fn().mockResolvedValue([
|
||||
{
|
||||
chunkId: '00000000-0000-0000-0000-000000000101',
|
||||
documentId: 'doc-1',
|
||||
externalId: 'page-1',
|
||||
title: 'Revenue Recognition',
|
||||
path: 'Company Handbook / Finance / Revenue Recognition',
|
||||
url: 'https://notion.example/page-1',
|
||||
rawPath: 'pages/page-1/page.md',
|
||||
content: 'Booked revenue excludes refunds and test accounts.',
|
||||
citation: { source: 'notion', pageId: 'page-1', rawPath: 'pages/page-1/page.md' },
|
||||
stableCitationKey: 'notion:page-1:policy:abc',
|
||||
syncId: 'sync-1',
|
||||
lastEditedAt: new Date('2026-04-12T10:15:00.000Z'),
|
||||
},
|
||||
]),
|
||||
insertCandidate: vi.fn().mockResolvedValue({
|
||||
id: 'candidate-1',
|
||||
candidate_key: 'revenue-definition',
|
||||
promotion_score: 10,
|
||||
status: 'pending',
|
||||
}),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
|
||||
const embeddings = makeEmbeddingService();
|
||||
const tool = new ContextCandidateWriteTool(repository, embeddings);
|
||||
const result = await tool.call(
|
||||
{
|
||||
candidateKey: 'revenue-definition',
|
||||
topic: 'Revenue Recognition',
|
||||
assertion: 'Booked revenue excludes refunds and test accounts.',
|
||||
rationale: 'Finance handbook is the source of truth and describes the reusable revenue rule.',
|
||||
evidenceChunkIds: ['00000000-0000-0000-0000-000000000101'],
|
||||
suggestedPageKey: 'revenue-definition',
|
||||
actionHint: 'create',
|
||||
durabilityScore: 3,
|
||||
authorityScore: 3,
|
||||
reuseScore: 3,
|
||||
noveltyScore: 2,
|
||||
riskScore: 1,
|
||||
},
|
||||
ingestContext(),
|
||||
);
|
||||
|
||||
expect(repository.readChunksByIds).toHaveBeenCalledWith(
|
||||
['00000000-0000-0000-0000-000000000101'],
|
||||
'00000000-0000-0000-0000-000000000001',
|
||||
'notion',
|
||||
'10000000-0000-0000-0000-000000000001',
|
||||
);
|
||||
|
||||
expect(repository.insertCandidate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
runId: '10000000-0000-0000-0000-000000000001',
|
||||
connectionId: '00000000-0000-0000-0000-000000000001',
|
||||
sourceKey: 'notion',
|
||||
candidateKey: 'revenue-definition',
|
||||
promotionScore: 10,
|
||||
status: 'pending',
|
||||
evidenceRefs: [
|
||||
expect.objectContaining({
|
||||
chunkId: '00000000-0000-0000-0000-000000000101',
|
||||
stableCitationKey: 'notion:page-1:policy:abc',
|
||||
snippetHash: createHash('sha256')
|
||||
.update('Booked revenue excludes refunds and test accounts.')
|
||||
.digest('hex'),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
);
|
||||
expect(embeddings.computeEmbedding).toHaveBeenCalledWith(
|
||||
'Revenue Recognition - Booked revenue excludes refunds and test accounts.',
|
||||
);
|
||||
expect(repository.insertCandidate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
embedding: [0.25, 0.5, 0.75],
|
||||
}),
|
||||
);
|
||||
expect(result.structured).toMatchObject({ success: true, candidateKey: 'revenue-definition', promotionScore: 10 });
|
||||
});
|
||||
|
||||
it('saves candidate writes with a null embedding when embedding generation fails', async () => {
|
||||
const repository = {
|
||||
readChunksByIds: vi.fn().mockResolvedValue([
|
||||
{
|
||||
chunkId: '00000000-0000-0000-0000-000000000101',
|
||||
documentId: 'doc-1',
|
||||
externalId: 'page-1',
|
||||
title: 'Revenue Recognition',
|
||||
path: 'Company Handbook / Finance / Revenue Recognition',
|
||||
url: 'https://notion.example/page-1',
|
||||
rawPath: 'pages/page-1/page.md',
|
||||
content: 'Booked revenue excludes refunds and test accounts.',
|
||||
citation: { source: 'notion', pageId: 'page-1', rawPath: 'pages/page-1/page.md' },
|
||||
stableCitationKey: 'notion:page-1:policy:abc',
|
||||
syncId: 'sync-1',
|
||||
lastEditedAt: new Date('2026-04-12T10:15:00.000Z'),
|
||||
},
|
||||
]),
|
||||
insertCandidate: vi.fn().mockResolvedValue({
|
||||
id: 'candidate-1',
|
||||
candidate_key: 'revenue-definition',
|
||||
promotion_score: 10,
|
||||
status: 'pending',
|
||||
}),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
const embeddings = makeEmbeddingService({
|
||||
computeEmbedding: vi.fn().mockRejectedValue(new Error('embedding provider unavailable')),
|
||||
});
|
||||
|
||||
const tool = new ContextCandidateWriteTool(repository, embeddings);
|
||||
const result = await tool.call(
|
||||
{
|
||||
candidateKey: 'revenue-definition',
|
||||
topic: 'Revenue Recognition',
|
||||
assertion: 'Booked revenue excludes refunds and test accounts.',
|
||||
rationale: 'Finance handbook is the source of truth and describes the reusable revenue rule.',
|
||||
evidenceChunkIds: ['00000000-0000-0000-0000-000000000101'],
|
||||
suggestedPageKey: 'revenue-definition',
|
||||
actionHint: 'create',
|
||||
durabilityScore: 3,
|
||||
authorityScore: 3,
|
||||
reuseScore: 3,
|
||||
noveltyScore: 2,
|
||||
riskScore: 1,
|
||||
},
|
||||
ingestContext(),
|
||||
);
|
||||
|
||||
expect(embeddings.computeEmbedding).toHaveBeenCalledWith(
|
||||
'Revenue Recognition - Booked revenue excludes refunds and test accounts.',
|
||||
);
|
||||
expect(repository.insertCandidate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
embedding: null,
|
||||
}),
|
||||
);
|
||||
expect(result.structured).toMatchObject({ success: true, candidateKey: 'revenue-definition', promotionScore: 10 });
|
||||
});
|
||||
|
||||
it('rejects candidate writes without evidence chunks', async () => {
|
||||
const embeddings = makeEmbeddingService();
|
||||
const tool = new ContextCandidateWriteTool(
|
||||
{
|
||||
insertCandidate: vi.fn(),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort,
|
||||
embeddings,
|
||||
);
|
||||
|
||||
const result = await tool.call(
|
||||
{
|
||||
candidateKey: 'uncited',
|
||||
topic: 'Uncited',
|
||||
assertion: 'This has no evidence.',
|
||||
rationale: 'No evidence was provided.',
|
||||
evidenceChunkIds: [],
|
||||
actionHint: 'create',
|
||||
durabilityScore: 1,
|
||||
authorityScore: 1,
|
||||
reuseScore: 1,
|
||||
noveltyScore: 1,
|
||||
riskScore: 1,
|
||||
},
|
||||
ingestContext(),
|
||||
);
|
||||
|
||||
expect(result.structured).toMatchObject({ success: false, error: 'EVIDENCE_REQUIRED' });
|
||||
expect(embeddings.computeEmbedding).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('marks a candidate status during reconciliation', async () => {
|
||||
const repository = {
|
||||
updateCandidateStatus: vi.fn().mockResolvedValue({
|
||||
id: 'candidate-1',
|
||||
candidate_key: 'revenue-definition',
|
||||
status: 'promoted',
|
||||
}),
|
||||
} as Partial<ContextEvidenceToolStorePort> as ContextEvidenceToolStorePort;
|
||||
|
||||
const tool = new ContextCandidateMarkTool(repository);
|
||||
const result = await tool.call(
|
||||
{ candidateKey: 'revenue-definition', status: 'promoted', rejectionReason: null },
|
||||
ingestContext(),
|
||||
);
|
||||
|
||||
expect(repository.updateCandidateStatus).toHaveBeenCalledWith({
|
||||
runId: '10000000-0000-0000-0000-000000000001',
|
||||
candidateKey: 'revenue-definition',
|
||||
status: 'promoted',
|
||||
rejectionReason: null,
|
||||
});
|
||||
expect(result.structured).toMatchObject({ success: true, candidateKey: 'revenue-definition', status: 'promoted' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('context evidence tools against real SqliteContextEvidenceStore', () => {
|
||||
let tempDir: string;
|
||||
let dbPath: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
tempDir = await mkdtemp(join(tmpdir(), 'ktx-context-tools-sqlite-'));
|
||||
dbPath = join(tempDir, '.ktx', 'db.sqlite');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const realStoreContext = (): ToolContext => ({
|
||||
sourceId: 'ingest',
|
||||
messageId: 'job-1-wu-unit-1',
|
||||
userId: 'system',
|
||||
connectionId: 'conn-1',
|
||||
ingest: {
|
||||
runId: 'run-1',
|
||||
jobId: 'job-1',
|
||||
syncId: 'sync-1',
|
||||
sourceKey: 'notion',
|
||||
},
|
||||
session: {
|
||||
connectionId: 'conn-1',
|
||||
isWorktreeScoped: true,
|
||||
preHead: 'abc123',
|
||||
touchedSlSources: createTouchedSlSources(),
|
||||
actions: [],
|
||||
ingest: {
|
||||
runId: 'run-1',
|
||||
jobId: 'job-1',
|
||||
syncId: 'sync-1',
|
||||
sourceKey: 'notion',
|
||||
},
|
||||
} as unknown as ToolSession,
|
||||
});
|
||||
|
||||
async function seedChunk(store: SqliteContextEvidenceStore): Promise<string> {
|
||||
const doc = await store.upsertDocument({
|
||||
runId: 'run-1',
|
||||
connectionId: 'conn-1',
|
||||
sourceKey: 'notion',
|
||||
externalId: 'page-1',
|
||||
externalParentId: null,
|
||||
databaseId: null,
|
||||
dataSourceId: null,
|
||||
title: 'Revenue Recognition',
|
||||
path: 'Company Handbook / Finance / Revenue Recognition',
|
||||
url: 'https://notion.test/page-1',
|
||||
objectType: 'page',
|
||||
lastEditedAt: new Date('2026-04-30T10:00:00.000Z'),
|
||||
lastEditedBy: 'user-1',
|
||||
rawPath: 'pages/page-1/page.md',
|
||||
syncId: 'sync-1',
|
||||
contentHash: 'hash-page-1',
|
||||
publishState: 'published',
|
||||
metadata: {},
|
||||
});
|
||||
await store.replaceChunks(doc.id, [
|
||||
{
|
||||
chunkKey: 'intro',
|
||||
headingPath: ['Revenue'],
|
||||
ordinal: 0,
|
||||
content: 'Booked revenue excludes refunds and test accounts.',
|
||||
searchText: 'booked revenue excludes refunds test accounts',
|
||||
embedding: [1, 0, 0],
|
||||
tokenCount: 8,
|
||||
citation: { source: 'notion', pageId: 'page-1', rawPath: 'pages/page-1/page.md' },
|
||||
stableCitationKey: 'notion:page-1:intro',
|
||||
syncId: 'sync-1',
|
||||
contentHash: 'chunk-page-1',
|
||||
},
|
||||
]);
|
||||
const read = await store.readDocumentByExternalId('conn-1', 'notion', 'page-1', 'run-1');
|
||||
if (!read) {
|
||||
throw new Error('seeded chunk not readable');
|
||||
}
|
||||
return read.chunks[0].id;
|
||||
}
|
||||
|
||||
it('candidate write accepts the prefixed chunkId returned by the real store and persists', async () => {
|
||||
const store = new SqliteContextEvidenceStore({ dbPath });
|
||||
const chunkId = await seedChunk(store);
|
||||
expect(chunkId).toMatch(/^ctxchunk-[0-9a-f-]{36}$/);
|
||||
|
||||
const tool = new ContextCandidateWriteTool(store, {
|
||||
computeEmbedding: vi.fn().mockResolvedValue([0.1, 0.2, 0.3]),
|
||||
} as Partial<KtxEmbeddingPort> as KtxEmbeddingPort);
|
||||
|
||||
const parsed = tool.parseInput({
|
||||
candidateKey: 'revenue-definition',
|
||||
topic: 'Revenue Recognition',
|
||||
assertion: 'Booked revenue excludes refunds and test accounts.',
|
||||
rationale: 'The Finance handbook is the source of truth.',
|
||||
evidenceChunkIds: [chunkId],
|
||||
actionHint: 'create',
|
||||
durabilityScore: 3,
|
||||
authorityScore: 3,
|
||||
reuseScore: 3,
|
||||
noveltyScore: 2,
|
||||
riskScore: 1,
|
||||
});
|
||||
|
||||
const result = await tool.call(parsed, realStoreContext());
|
||||
expect(result.structured).toMatchObject({
|
||||
success: true,
|
||||
candidateKey: 'revenue-definition',
|
||||
promotionScore: 10,
|
||||
status: 'pending',
|
||||
});
|
||||
});
|
||||
|
||||
it('candidate write schema rejects a bare UUID without the ctxchunk- prefix', () => {
|
||||
const tool = new ContextCandidateWriteTool(
|
||||
{} as ContextEvidenceToolStorePort,
|
||||
{ computeEmbedding: vi.fn() } as Partial<KtxEmbeddingPort> as KtxEmbeddingPort,
|
||||
);
|
||||
|
||||
expect(() =>
|
||||
tool.parseInput({
|
||||
candidateKey: 'revenue-definition',
|
||||
topic: 'Revenue Recognition',
|
||||
assertion: 'Booked revenue excludes refunds and test accounts.',
|
||||
rationale: 'Finance handbook is the source of truth.',
|
||||
evidenceChunkIds: ['00000000-0000-0000-0000-000000000101'],
|
||||
actionHint: 'create',
|
||||
durabilityScore: 3,
|
||||
authorityScore: 3,
|
||||
reuseScore: 3,
|
||||
noveltyScore: 2,
|
||||
riskScore: 1,
|
||||
}),
|
||||
).toThrow(/ctxchunk/);
|
||||
});
|
||||
|
||||
it('evidence read schema rejects bare UUIDs for chunkId and documentId', () => {
|
||||
const tool = new ContextEvidenceReadTool({} as ContextEvidenceToolStorePort);
|
||||
|
||||
expect(() =>
|
||||
tool.parseInput({ chunkId: '00000000-0000-0000-0000-000000000301', includeNeighborChunks: false }),
|
||||
).toThrow(/ctxchunk/);
|
||||
expect(() =>
|
||||
tool.parseInput({ documentId: '00000000-0000-0000-0000-000000000201', includeNeighborChunks: false }),
|
||||
).toThrow(/ctxdoc/);
|
||||
});
|
||||
|
||||
it('evidence neighbors schema rejects bare UUIDs for documentId', () => {
|
||||
const tool = new ContextEvidenceNeighborsTool({} as ContextEvidenceToolStorePort);
|
||||
expect(() =>
|
||||
tool.parseInput({ documentId: '00000000-0000-0000-0000-000000000201', relation: 'children', limit: 10 }),
|
||||
).toThrow(/ctxdoc/);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue