refactor: remove KVARK, enterprise packs and the compliance subsystem (P1.4, P1.6)

KVARK is the commercial enterprise product and is not part of the community
edition. The agent's KVARK tools were never wired into the server, so
removing them changes no runtime behaviour beyond the routes listed here.

- agent: delete kvark-tools; CombinedRetrieval and formatCombinedResult now
  merge workspace and personal memory only (no KVARK lane, no source
  conflict note); connector substrate is always 'waggle'
- agent: promote_skill stops at workspace scope; team and enterprise scopes
  answer "not available in this edition" instead of an upgrade prompt
- server: delete src/kvark and GET /api/marketplace/enterprise-packs
- marketplace: delete ENTERPRISE_PACKS
- compliance (EU AI Act dashboard, its templates and the KVARK template
  seeded on every boot): delete core/src/compliance, the /api/compliance/*
  routes, agent compliance-pdf and the Cockpit compliance panel. The
  ai_interactions table lives in the memory substrate and goes with P1.5.
This commit is contained in:
shumeiko.it
2026-10-20 00:27:02 +02:00
parent 10ad678547
commit b734c1d3d2
53 changed files with 56 additions and 6936 deletions

View File

@@ -1,25 +1,17 @@
/**
* Combined Retrieval — B1 tests.
* Combined Retrieval — merge engine tests.
*
* Tests the merge engine in isolation. All dependencies are mocked:
* - MemorySearchLike (workspace + personal HybridSearch)
* - KvarkClientLike (KVARK HTTP client)
*
* No server, no vault, no DB, no wiring.
* Tests the merge engine in isolation. MemorySearchLike (workspace + personal
* HybridSearch) is mocked. No server, no vault, no DB, no wiring.
*/
import { describe, it, expect, vi } from 'vitest';
import {
CombinedRetrieval,
mapMemoryResult,
mapKvarkResult,
hasSufficientLocalCoverage,
shouldQueryKvark,
type MemorySearchLike,
type MemorySearchResultLike,
type CombinedResult,
} from '../src/combined-retrieval.js';
import type { KvarkClientLike, KvarkSearchResponseLike } from '../src/kvark-tools.js';
// ── Factories ─────────────────────────────────────────────────────────────
@@ -41,22 +33,6 @@ function makeSearchMock(results: MemorySearchResultLike[] = []): MemorySearchLik
return { search: vi.fn(async () => results) };
}
const KVARK_RESPONSE: KvarkSearchResponseLike = {
results: [
{ document_id: 42, title: 'Project Status Update', snippet: 'API review postponed.', score: 0.92, document_type: 'pdf' },
{ document_id: 108, title: 'Q1 Budget', snippet: 'Engineering budget increased.', score: 0.87, document_type: 'spreadsheet' },
],
total: 8,
query: 'project',
};
function makeKvarkClient(overrides?: Partial<KvarkClientLike>): KvarkClientLike {
return {
search: overrides?.search ?? vi.fn(async () => KVARK_RESPONSE),
askDocument: overrides?.askDocument ?? vi.fn(async () => ({ answer: '', sources: [] })),
};
}
// ── Helper unit tests ─────────────────────────────────────────────────────
describe('mapMemoryResult', () => {
@@ -83,121 +59,34 @@ describe('mapMemoryResult', () => {
});
});
describe('mapKvarkResult', () => {
it('preserves attribution from parseSearchResults', () => {
const structured = {
content: 'API review postponed.',
documentId: 42,
title: 'Project Status Update',
score: 0.92,
documentType: 'pdf' as string | null,
attribution: '[KVARK: pdf: Project Status Update]',
};
const result = mapKvarkResult(structured);
expect(result.source).toBe('kvark');
expect(result.attribution).toBe('[KVARK: pdf: Project Status Update]');
expect(result.score).toBe(0.92);
expect(result.metadata.documentId).toBe(42);
expect(result.metadata.documentType).toBe('pdf');
});
});
describe('hasSufficientLocalCoverage', () => {
it('returns true when 3+ results score >= 0.7', () => {
const results: CombinedResult[] = [
{ content: '', source: 'workspace', attribution: '', score: 0.9, metadata: {} },
{ content: '', source: 'workspace', attribution: '', score: 0.8, metadata: {} },
{ content: '', source: 'personal', attribution: '', score: 0.7, metadata: {} },
];
expect(hasSufficientLocalCoverage(results)).toBe(true);
});
it('returns false when fewer than 3 results score >= 0.7', () => {
const results: CombinedResult[] = [
{ content: '', source: 'workspace', attribution: '', score: 0.9, metadata: {} },
{ content: '', source: 'workspace', attribution: '', score: 0.8, metadata: {} },
{ content: '', source: 'personal', attribution: '', score: 0.5, metadata: {} },
];
expect(hasSufficientLocalCoverage(results)).toBe(false);
});
it('returns false for empty results', () => {
expect(hasSufficientLocalCoverage([])).toBe(false);
});
});
describe('shouldQueryKvark', () => {
const weakLocal: CombinedResult[] = [
{ content: '', source: 'workspace', attribution: '', score: 0.3, metadata: {} },
];
it('returns false when kvarkClient is null', () => {
expect(shouldQueryKvark(null, 'all', weakLocal)).toBe(false);
});
it('returns false when scope is personal', () => {
expect(shouldQueryKvark(makeKvarkClient(), 'personal', weakLocal)).toBe(false);
});
it('returns false when scope is workspace', () => {
expect(shouldQueryKvark(makeKvarkClient(), 'workspace', weakLocal)).toBe(false);
});
it('returns false when local coverage is sufficient', () => {
const strong: CombinedResult[] = [
{ content: '', source: 'workspace', attribution: '', score: 0.9, metadata: {} },
{ content: '', source: 'workspace', attribution: '', score: 0.8, metadata: {} },
{ content: '', source: 'personal', attribution: '', score: 0.75, metadata: {} },
];
expect(shouldQueryKvark(makeKvarkClient(), 'all', strong)).toBe(false);
});
it('returns true when client exists, scope=all, and local coverage insufficient', () => {
expect(shouldQueryKvark(makeKvarkClient(), 'all', weakLocal)).toBe(true);
});
});
// ── CombinedRetrieval class tests ─────────────────────────────────────────
describe('CombinedRetrieval', () => {
it('returns workspace + personal results when kvarkClient is null', async () => {
it('returns workspace + personal results', async () => {
const wsResults = [makeMemoryResult({ id: 1, content: 'ws fact', score: 0.9 })];
const pResults = [makeMemoryResult({ id: 2, content: 'personal fact', score: 0.8 })];
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock(wsResults),
personalSearch: makeSearchMock(pResults),
kvarkClient: null,
});
const result = await cr.search('test');
expect(result.workspaceResults).toHaveLength(1);
expect(result.personalResults).toHaveLength(1);
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkAvailable).toBe(false);
expect(result.kvarkSkipped).toBe(false); // not available, so not "skipped"
expect(result.kvarkError).toBeUndefined();
expect(result.query).toBe('test');
expect(result.workspaceResults.map(r => r.content)).toEqual(['ws fact']);
expect(result.personalResults.map(r => r.content)).toEqual(['personal fact']);
});
it('searches only personal memory when scope=personal', async () => {
const wsMock = makeSearchMock([makeMemoryResult({ id: 1 })]);
const pMock = makeSearchMock([makeMemoryResult({ id: 2, content: 'personal only' })]);
const cr = new CombinedRetrieval({
workspaceSearch: wsMock,
personalSearch: pMock,
kvarkClient: makeKvarkClient(),
});
const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock });
const result = await cr.search('test', { scope: 'personal' });
expect(result.workspaceResults).toHaveLength(0);
expect(result.personalResults).toHaveLength(1);
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkSkipped).toBe(true);
expect(wsMock.search).not.toHaveBeenCalled();
});
@@ -205,173 +94,57 @@ describe('CombinedRetrieval', () => {
const wsMock = makeSearchMock([makeMemoryResult({ id: 1, content: 'ws only' })]);
const pMock = makeSearchMock([makeMemoryResult({ id: 2 })]);
const cr = new CombinedRetrieval({
workspaceSearch: wsMock,
personalSearch: pMock,
kvarkClient: makeKvarkClient(),
});
const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock });
const result = await cr.search('test', { scope: 'workspace' });
expect(result.workspaceResults).toHaveLength(1);
expect(result.personalResults).toHaveLength(0);
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkSkipped).toBe(true);
expect(pMock.search).not.toHaveBeenCalled();
});
it('calls KVARK when local results are insufficient', async () => {
const searchFn = vi.fn(async () => KVARK_RESPONSE);
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock([makeMemoryResult({ score: 0.3 })]),
personalSearch: makeSearchMock([]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
const result = await cr.search('project');
expect(searchFn).toHaveBeenCalledWith('project', { limit: 10 });
expect(result.kvarkResults).toHaveLength(2);
expect(result.kvarkResults[0].source).toBe('kvark');
expect(result.kvarkResults[0].attribution).toBe('[KVARK: pdf: Project Status Update]');
expect(result.kvarkResults[1].attribution).toBe('[KVARK: spreadsheet: Q1 Budget]');
expect(result.kvarkAvailable).toBe(true);
expect(result.kvarkSkipped).toBe(false);
});
it('skips KVARK when local results are sufficient', async () => {
const searchFn = vi.fn(async () => KVARK_RESPONSE);
const strongResults = [
makeMemoryResult({ id: 1, score: 0.9 }),
makeMemoryResult({ id: 2, score: 0.85 }),
makeMemoryResult({ id: 3, score: 0.75 }),
];
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock(strongResults),
personalSearch: makeSearchMock([]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
const result = await cr.search('test');
expect(searchFn).not.toHaveBeenCalled();
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkAvailable).toBe(true);
expect(result.kvarkSkipped).toBe(true);
});
it('degrades gracefully when KVARK throws an Error', async () => {
const searchFn = vi.fn(async () => { throw new Error('KVARK is down'); });
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock([makeMemoryResult({ id: 1, score: 0.4 })]),
personalSearch: makeSearchMock([makeMemoryResult({ id: 2, score: 0.3 })]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
const result = await cr.search('test');
expect(result.workspaceResults).toHaveLength(1);
expect(result.personalResults).toHaveLength(1);
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkError).toBe('KVARK is down');
expect(result.kvarkAvailable).toBe(true);
expect(result.kvarkSkipped).toBe(false);
});
it('captures non-Error throws from KVARK as generic message', async () => {
const searchFn = vi.fn(async () => { throw 'string error'; });
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock([]),
personalSearch: makeSearchMock([]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
const result = await cr.search('test');
expect(result.kvarkError).toBe('Unknown KVARK error');
expect(result.kvarkResults).toHaveLength(0);
});
it('works correctly when no workspace search is available', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: null,
personalSearch: makeSearchMock([makeMemoryResult({ id: 1, content: 'personal fact', score: 0.6 })]),
kvarkClient: makeKvarkClient(),
});
const result = await cr.search('test');
expect(result.workspaceResults).toHaveLength(0);
expect(result.personalResults).toHaveLength(1);
expect(result.kvarkResults).toHaveLength(2);
expect(result.kvarkAvailable).toBe(true);
expect(result.kvarkSkipped).toBe(false);
});
it('passes limit and profile through to memory search', async () => {
const wsMock = makeSearchMock([]);
const pMock = makeSearchMock([]);
const cr = new CombinedRetrieval({
workspaceSearch: wsMock,
personalSearch: pMock,
kvarkClient: null,
});
const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock });
await cr.search('test', { limit: 5, profile: 'recent' });
expect(wsMock.search).toHaveBeenCalledWith('test', { limit: 5, profile: 'recent' });
expect(pMock.search).toHaveBeenCalledWith('test', { limit: 5, profile: 'recent' });
});
it('passes limit to KVARK client search', async () => {
const searchFn = vi.fn(async () => KVARK_RESPONSE);
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock([]),
personalSearch: makeSearchMock([]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
await cr.search('query', { limit: 7 });
expect(searchFn).toHaveBeenCalledWith('query', { limit: 7 });
});
it('uses sensible defaults (limit=10, profile=balanced, scope=all)', async () => {
const wsMock = makeSearchMock([]);
const pMock = makeSearchMock([]);
const cr = new CombinedRetrieval({
workspaceSearch: wsMock,
personalSearch: pMock,
kvarkClient: null,
});
const cr = new CombinedRetrieval({ workspaceSearch: wsMock, personalSearch: pMock });
await cr.search('test');
expect(wsMock.search).toHaveBeenCalledWith('test', { limit: 10, profile: 'balanced' });
expect(pMock.search).toHaveBeenCalledWith('test', { limit: 10, profile: 'balanced' });
});
it('handles KVARK returning zero results without error', async () => {
const emptyResponse: KvarkSearchResponseLike = { results: [], total: 0, query: 'nothing' };
const searchFn = vi.fn(async () => emptyResponse);
it('returns empty lists when nothing matches', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: makeSearchMock([]),
personalSearch: makeSearchMock([]),
kvarkClient: makeKvarkClient({ search: searchFn }),
});
const result = await cr.search('nothing');
expect(result.kvarkResults).toHaveLength(0);
expect(result.kvarkAvailable).toBe(true);
expect(result.kvarkSkipped).toBe(false);
expect(result.kvarkError).toBeUndefined();
expect(result.workspaceResults).toHaveLength(0);
expect(result.personalResults).toHaveLength(0);
});
});

View File

@@ -1,193 +0,0 @@
/**
* Skills 2.0 gap H — compliance report PDF generator.
*
* Tests the doc-definition builder (buildComplianceDocDefinition) without
* actually rendering a PDF — that would require pdfmake font bundles that
* inflate test time and CI brittleness. The render function itself is a
* thin pdfmake wrapper; if the doc def is structurally correct and
* pdfmake has its own test coverage upstream, the rendered artifact is
* trustworthy.
*/
import { describe, it, expect } from 'vitest';
import type { AuditReport } from '@waggle/core';
import { buildComplianceDocDefinition } from '../src/compliance-pdf.js';
function sampleReport(overrides: Partial<AuditReport> = {}): AuditReport {
return {
report: {
version: '1.0',
generatedAt: '2026-04-15T12:00:00.000Z',
period: { from: '2026-01-01T00:00:00.000Z', to: '2026-04-15T00:00:00.000Z' },
generatedBy: 'Waggle OS',
},
workspace: {
id: 'ws-alpha',
name: 'Alpha Corp Legal',
riskLevel: 'high-risk',
riskClassifiedAt: '2026-01-01T00:00:00.000Z',
},
complianceStatus: {
overall: 'compliant',
art12Logging: { status: 'compliant', detail: 'All interactions logged', totalInteractions: 1247 },
art14Oversight: { status: 'compliant', detail: '83% approval rate', humanActions: 47, approvalRate: 0.83 },
art19Retention: { status: 'warning', detail: 'Oldest log near retention limit', oldestLogDate: '2025-04-15T00:00:00.000Z', retentionDays: 365 },
art26Monitoring: { status: 'compliant', detail: 'Active monitors: drift, bias, throughput', activeMonitors: ['drift', 'bias', 'throughput'] },
art50Transparency: { status: 'compliant', detail: 'Model disclosure active', modelsDisclosed: true },
},
modelInventory: [
{ model: 'claude-sonnet-4-6', provider: 'anthropic', calls: 500, inputTokens: 1_000_000, outputTokens: 200_000, costUsd: 3.5 },
{ model: 'claude-haiku-4-5', provider: 'anthropic', calls: 747, inputTokens: 500_000, outputTokens: 80_000, costUsd: 0.42 },
],
humanOversightLog: [
{ timestamp: '2026-04-10T09:15:00.000Z', action: 'approved', tool: 'save_memory', detail: 'Client contract summary — confirmed' },
{ timestamp: '2026-04-11T14:22:00.000Z', action: 'denied', tool: 'send_email', detail: 'Draft to opposing counsel — halted' },
],
harvestProvenance: [
{ source: 'Claude Code exports', importedAt: '2026-03-01T00:00:00.000Z', itemsImported: 156, framesCreated: 89 },
],
interactionCount: 1247,
...overrides,
};
}
describe('buildComplianceDocDefinition — gap H', () => {
it('produces a valid pdfmake doc definition with key metadata', () => {
const doc = buildComplianceDocDefinition(sampleReport());
expect(doc.info?.title).toContain('Alpha Corp Legal');
expect(doc.info?.creator).toContain('Waggle');
expect(doc.pageSize).toBe('A4');
expect(doc.pageMargins).toEqual([50, 60, 50, 60]);
});
it('includes the workspace name and risk level on the cover', () => {
const doc = buildComplianceDocDefinition(sampleReport());
const flat = JSON.stringify(doc.content);
expect(flat).toContain('Alpha Corp Legal');
expect(flat).toContain('HIGH-RISK');
expect(flat).toContain('AI ACT COMPLIANCE AUDIT');
});
it('falls back to "Personal Mind" when workspace is null', () => {
const doc = buildComplianceDocDefinition(sampleReport({ workspace: null }));
const flat = JSON.stringify(doc.content);
expect(flat).toContain('Personal Mind');
expect(flat).toContain('MINIMAL'); // default risk fallback
});
it('renders a row per article status with matching badges', () => {
const doc = buildComplianceDocDefinition(sampleReport());
const flat = JSON.stringify(doc.content);
// All 5 article labels appear
expect(flat).toContain('Art. 12');
expect(flat).toContain('Art. 14');
expect(flat).toContain('Art. 19');
expect(flat).toContain('Art. 26');
expect(flat).toContain('Art. 50');
// Status badges appear
expect(flat).toContain('COMPLIANT');
expect(flat).toContain('WARNING');
});
it('includes a model inventory total row', () => {
const doc = buildComplianceDocDefinition(sampleReport());
const flat = JSON.stringify(doc.content);
expect(flat).toContain('claude-sonnet-4-6');
expect(flat).toContain('claude-haiku-4-5');
// Total calls = 500 + 747 = 1,247
expect(flat).toContain('1,247');
expect(flat).toContain('TOTAL');
});
it('shows an empty-state message for empty model inventory', () => {
const doc = buildComplianceDocDefinition(sampleReport({ modelInventory: [] }));
const flat = JSON.stringify(doc.content);
expect(flat).toContain('No model calls recorded');
});
it('shows an empty-state message for empty oversight log', () => {
const doc = buildComplianceDocDefinition(sampleReport({ humanOversightLog: [] }));
const flat = JSON.stringify(doc.content);
expect(flat).toContain('No human oversight events');
});
it('caps oversight log display at 50 most-recent entries with a footer', () => {
const log = Array.from({ length: 120 }, (_, i) => ({
timestamp: `2026-04-${String(i + 1).padStart(2, '0')}T00:00:00.000Z`,
action: 'approved' as const,
tool: 'save_memory',
detail: `Event ${i}`,
}));
const doc = buildComplianceDocDefinition(sampleReport({ humanOversightLog: log }));
const flat = JSON.stringify(doc.content);
expect(flat).toContain('Showing last 50 of 120 events');
});
it('empty provenance produces an empty-state message', () => {
const doc = buildComplianceDocDefinition(sampleReport({ harvestProvenance: [] }));
const flat = JSON.stringify(doc.content);
expect(flat).toContain('No harvest provenance data');
});
it('summary lists totals derived from report fields', () => {
const doc = buildComplianceDocDefinition(sampleReport());
const flat = JSON.stringify(doc.content);
expect(flat).toContain('Total interactions logged: 1,247');
expect(flat).toContain('Models in inventory: 2');
expect(flat).toContain('Oversight events: 2');
expect(flat).toContain('Harvest sources: 1');
});
it('styles contain Hive DS honey color', () => {
const doc = buildComplianceDocDefinition(sampleReport());
const styles = JSON.stringify(doc.styles);
expect(styles).toContain('#E5A000'); // HIVE_HONEY
});
});
describe('buildComplianceDocDefinition — M-03 template overrides', () => {
it('overrides workspace name with templateOrgName in title + header + cover', () => {
const doc = buildComplianceDocDefinition(sampleReport(), { orgName: 'KVARK Sovereign Cloud' });
expect(doc.info?.title).toContain('KVARK Sovereign Cloud');
expect(doc.info?.title).not.toContain('Alpha Corp Legal');
const flat = JSON.stringify(doc.content);
expect(flat).toContain('KVARK Sovereign Cloud');
});
it('overrides workspace risk level with templateRiskClassification', () => {
const doc = buildComplianceDocDefinition(sampleReport(), { riskClassification: 'minimal' });
const flat = JSON.stringify(doc.content);
expect(flat).toContain('MINIMAL');
// Original workspace risk (HIGH-RISK) should no longer appear on the cover
// since the cover only renders the override.
expect(flat).not.toContain('HIGH-RISK');
});
it('appends footerText to the rendered footer', () => {
const doc = buildComplianceDocDefinition(sampleReport(), {
footerText: 'Confidential — board only',
});
// pdfmake `footer` is a function (currentPage, pageCount) => Content.
// Invoke it to get the rendered cell tree and stringify that.
const footerFn = doc.footer as ((currentPage: number, pageCount: number) => unknown) | undefined;
expect(typeof footerFn).toBe('function');
const footerContent = JSON.stringify(footerFn!(1, 1));
expect(footerContent).toContain('Confidential — board only');
});
it('blank overrides fall back to workspace defaults', () => {
const doc = buildComplianceDocDefinition(sampleReport(), {
orgName: '',
footerText: '',
riskClassification: null,
});
const flat = JSON.stringify(doc.content);
expect(flat).toContain('Alpha Corp Legal'); // workspace name still used
expect(flat).toContain('HIGH-RISK'); // workspace risk still used
});
it('ignores undefined overrides (no-op call path)', () => {
const baseline = buildComplianceDocDefinition(sampleReport());
const overridden = buildComplianceDocDefinition(sampleReport(), undefined);
expect(JSON.stringify(overridden.info)).toBe(JSON.stringify(baseline.info));
});
});

View File

@@ -1,196 +0,0 @@
/**
* Conflict detection tests — post-Milestone-B trust slice.
*
* Tests the detectConflict heuristic and its integration with
* CombinedRetrieval and formatCombinedResult.
*/
import { describe, it, expect, vi } from 'vitest';
import {
detectConflict,
CombinedRetrieval,
type CombinedResult,
type MemorySearchLike,
type MemorySearchResultLike,
} from '../src/combined-retrieval.js';
import { formatCombinedResult } from '../src/tools.js';
import type { KvarkClientLike, KvarkSearchResponseLike } from '../src/kvark-tools.js';
// ── Helpers ──────────────────────────────────────────────────────────────
function makeResult(source: 'workspace' | 'kvark', content: string, score: number): CombinedResult {
return {
content,
source,
attribution: source === 'workspace' ? '[workspace memory]' : '[KVARK: doc]',
score,
metadata: {},
};
}
function makeMemoryResult(id: number, content: string, score: number): MemorySearchResultLike {
return { frame: { id, content, frame_type: 'fact', importance: 'normal' }, finalScore: score };
}
function makeSearch(results: MemorySearchResultLike[]): MemorySearchLike {
return { search: vi.fn().mockResolvedValue(results) };
}
function makeKvarkClient(results: Array<{ title: string; snippet: string; score: number }>): KvarkClientLike {
const response: KvarkSearchResponseLike = {
query: 'test',
total: results.length,
results: results.map((r, i) => ({
document_id: i + 1,
title: r.title,
snippet: r.snippet,
score: r.score,
document_type: null,
})),
};
return {
search: vi.fn().mockResolvedValue(response),
askDocument: vi.fn().mockResolvedValue({ answer: '', sources: [] }),
};
}
// ── detectConflict unit tests ────────────────────────────────────────────
describe('detectConflict', () => {
it('returns null when only workspace results are present', () => {
const ws = [makeResult('workspace', 'project approved by board', 0.9)];
expect(detectConflict(ws, [])).toBeNull();
});
it('returns null when only KVARK results are present', () => {
const kvark = [makeResult('kvark', 'project cancelled per policy', 0.8)];
expect(detectConflict([], kvark)).toBeNull();
});
it('returns null when both sources agree (both positive)', () => {
const ws = [makeResult('workspace', 'The migration was approved last week', 0.85)];
const kvark = [makeResult('kvark', 'Migration approved and confirmed by CTO', 0.9)];
expect(detectConflict(ws, kvark)).toBeNull();
});
it('returns null when both sources agree (both negative)', () => {
const ws = [makeResult('workspace', 'Feature was rejected in review', 0.8)];
const kvark = [makeResult('kvark', 'Feature rejected — not aligned with roadmap', 0.85)];
expect(detectConflict(ws, kvark)).toBeNull();
});
it('returns null when neither source has status language', () => {
const ws = [makeResult('workspace', 'The project uses PostgreSQL for data storage', 0.9)];
const kvark = [makeResult('kvark', 'Database architecture uses Oracle Enterprise', 0.85)];
expect(detectConflict(ws, kvark)).toBeNull();
});
it('flags conflict when workspace is positive and KVARK is negative', () => {
const ws = [makeResult('workspace', 'We decided to use the new API and approved the integration', 0.9)];
const kvark = [makeResult('kvark', 'API integration has been cancelled due to security review', 0.85)];
const result = detectConflict(ws, kvark);
expect(result).not.toBeNull();
expect(result).toContain('affirmative language');
expect(result).toContain('contradictory language');
expect(result).toContain('out of sync');
});
it('flags conflict when workspace is negative and KVARK is positive', () => {
const ws = [makeResult('workspace', 'The proposal was rejected by the committee', 0.8)];
const kvark = [makeResult('kvark', 'Proposal approved and budget confirmed for Q2', 0.9)];
const result = detectConflict(ws, kvark);
expect(result).not.toBeNull();
expect(result).toContain('enterprise source may be more current');
});
it('returns null when results are below score threshold', () => {
const ws = [makeResult('workspace', 'project approved', 0.4)];
const kvark = [makeResult('kvark', 'project cancelled', 0.5)];
expect(detectConflict(ws, kvark)).toBeNull();
});
it('handles mixed polarity within same source (neutral)', () => {
const ws = [makeResult('workspace', 'Some features approved, others rejected during review', 0.8)];
const kvark = [makeResult('kvark', 'Project cancelled after initial approval', 0.85)];
// workspace has both positive AND negative => neutral => no conflict
expect(detectConflict(ws, kvark)).toBeNull();
});
});
// ── Integration: CombinedRetrieval with conflict detection ──────────────
describe('CombinedRetrieval conflict integration', () => {
it('sets hasConflict=false when KVARK is not called', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: makeSearch([makeMemoryResult(1, 'approved by team', 0.9)]),
personalSearch: makeSearch([]),
kvarkClient: null,
});
const result = await cr.search('test');
expect(result.hasConflict).toBe(false);
expect(result.conflictNote).toBeUndefined();
});
it('sets hasConflict=true when workspace and KVARK results conflict', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: makeSearch([makeMemoryResult(1, 'The vendor was selected and approved', 0.85)]),
personalSearch: makeSearch([]),
kvarkClient: makeKvarkClient([
{ title: 'Vendor Policy', snippet: 'Vendor agreement has been cancelled effective immediately', score: 0.9 },
]),
});
const result = await cr.search('vendor status');
expect(result.hasConflict).toBe(true);
expect(result.conflictNote).toBeDefined();
expect(result.conflictNote).toContain('out of sync');
});
it('sets hasConflict=false when sources do not conflict', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: makeSearch([makeMemoryResult(1, 'project status is active and healthy', 0.8)]),
personalSearch: makeSearch([]),
kvarkClient: makeKvarkClient([
{ title: 'Status', snippet: 'Project confirmed active with full funding', score: 0.85 },
]),
});
const result = await cr.search('project status');
expect(result.hasConflict).toBe(false);
});
});
// ── formatCombinedResult with conflict ──────────────────────────────────
describe('formatCombinedResult conflict rendering', () => {
it('includes Source Conflict section when hasConflict is true', () => {
const output = formatCombinedResult({
query: 'test',
workspaceResults: [makeResult('workspace', 'approved', 0.9)],
personalResults: [],
kvarkResults: [makeResult('kvark', 'cancelled', 0.85)],
kvarkAvailable: true,
kvarkSkipped: false,
hasConflict: true,
conflictNote: 'Sources may disagree.',
}, true);
expect(output).toContain('## Source Conflict');
expect(output).toContain('Sources may disagree.');
expect(output).toContain('Review both sources carefully');
});
it('omits Source Conflict section when hasConflict is false', () => {
const output = formatCombinedResult({
query: 'test',
workspaceResults: [makeResult('workspace', 'some fact', 0.9)],
personalResults: [],
kvarkResults: [makeResult('kvark', 'some other fact', 0.85)],
kvarkAvailable: true,
kvarkSkipped: false,
hasConflict: false,
}, true);
expect(output).not.toContain('## Source Conflict');
});
});

View File

@@ -1,138 +0,0 @@
/**
* KVARK Pipeline Smoke - assembled path validation (Milestone E1).
* Exercises real B1-B4 + conflict + C pipeline code.
* Only KVARK HTTP boundary is mocked via KvarkClientLike.
*/
import { describe, it, expect, vi } from 'vitest';
import { CombinedRetrieval, detectConflict, type MemorySearchLike, type MemorySearchResultLike } from '../src/combined-retrieval.js';
import { formatCombinedResult } from '../src/tools.js';
import { createKvarkTools, type KvarkClientLike, type KvarkSearchResponseLike } from '../src/kvark-tools.js';
function memStub(r: Array<{ id: number; content: string; score: number; type?: string }>): MemorySearchLike {
const m: MemorySearchResultLike[] = r.map(x => ({ frame: { id: x.id, content: x.content, frame_type: x.type ?? 'fact', importance: 'normal' }, finalScore: x.score }));
return { search: vi.fn().mockResolvedValue(m) };
}
function kStub(r: Array<{ id: number; title: string; snippet: string; score: number; type?: string }>): KvarkClientLike {
const resp: KvarkSearchResponseLike = { query: 'q', total: r.length, results: r.map(x => ({ document_id: x.id, title: x.title, snippet: x.snippet, score: x.score, document_type: x.type ?? null })) };
return { search: vi.fn().mockResolvedValue(resp), askDocument: vi.fn().mockResolvedValue({ answer: '', sources: [] }), feedback: vi.fn().mockResolvedValue({ ok: true }) };
}
describe('Pipeline: happy path', () => {
it('all sources produce formatted output with attribution', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: memStub([{ id: 1, content: 'We use PostgreSQL', score: 0.75, type: 'decision' }]),
personalSearch: memStub([{ id: 2, content: 'User prefers concise', score: 0.5 }]),
kvarkClient: kStub([{ id: 42, title: 'ADR', snippet: 'PostgreSQL selected', score: 0.88, type: 'pdf' }]),
});
const r = await cr.search('db');
const o = formatCombinedResult(r, true);
expect(o).toContain('## Workspace Memory');
expect(o).toContain('## Personal Memory');
expect(o).toContain('## Enterprise Knowledge (KVARK)');
expect(o).toContain('[workspace memory]');
expect(o).toContain('[personal memory]');
expect(o).toContain('[KVARK: pdf: ADR]');
expect(o).not.toContain('## Source Conflict');
expect(r.hasConflict).toBe(false);
});
});
describe('Pipeline: conflict detection', () => {
it('approved vs cancelled triggers conflict', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: memStub([{ id: 1, content: 'Vendor approved and confirmed', score: 0.85 }]),
personalSearch: memStub([]),
kvarkClient: kStub([{ id: 99, title: 'Policy', snippet: 'Vendor cancelled due to compliance', score: 0.9 }]),
});
const r = await cr.search('vendor');
expect(r.hasConflict).toBe(true);
const o = formatCombinedResult(r, true);
expect(o).toContain('## Source Conflict');
expect(o).toContain('## Workspace Memory');
expect(o).toContain('## Enterprise Knowledge');
});
it('detectConflict consistent with CombinedRetrieval', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: memStub([{ id: 1, content: 'Project rejected', score: 0.8 }]),
personalSearch: memStub([]),
kvarkClient: kStub([{ id: 50, title: 'Minutes', snippet: 'Project approved', score: 0.85 }]),
});
const r = await cr.search('status');
const d = detectConflict(r.workspaceResults, r.kvarkResults);
expect(r.hasConflict).toBe(d !== null);
});
});
describe('Pipeline: KVARK skip', () => {
it('strong local results skip KVARK', async () => {
const k = kStub([{ id: 1, title: 'D', snippet: 't', score: 0.9 }]);
const cr = new CombinedRetrieval({
workspaceSearch: memStub([{ id: 1, content: 'A', score: 0.9 }, { id: 2, content: 'B', score: 0.85 }, { id: 3, content: 'C', score: 0.75 }]),
personalSearch: memStub([]), kvarkClient: k,
});
const r = await cr.search('test');
expect(r.kvarkSkipped).toBe(true);
expect(k.search).not.toHaveBeenCalled();
expect(formatCombinedResult(r, true)).not.toContain('Enterprise');
});
});
describe('Pipeline: KVARK failure', () => {
it('local results preserved on error', async () => {
const cr = new CombinedRetrieval({
workspaceSearch: memStub([{ id: 1, content: 'Local fact', score: 0.6 }]),
personalSearch: memStub([{ id: 2, content: 'Note', score: 0.4 }]),
kvarkClient: { search: vi.fn().mockRejectedValue(new Error('down')), askDocument: vi.fn() },
});
const r = await cr.search('x');
const o = formatCombinedResult(r, true);
expect(r.kvarkError).toBe('down');
expect(r.hasConflict).toBe(false);
expect(o).toContain('Local fact');
expect(o).toContain('error');
});
});
describe('Pipeline: feedback round-trip', () => {
it('search doc IDs valid for feedback', async () => {
const fb = vi.fn().mockResolvedValue({ ok: true });
const k = kStub([{ id: 42, title: 'Q3', snippet: 'Rev up', score: 0.9, type: 'pdf' }]);
k.feedback = fb;
const cr = new CombinedRetrieval({ workspaceSearch: memStub([]), personalSearch: memStub([]), kvarkClient: k });
const s = await cr.search('rev');
const did = s.kvarkResults[0].metadata.documentId;
expect(did).toBe(42);
const t = createKvarkTools({ client: k }).find(x => x.name === 'kvark_feedback')!;
const o = await t.execute({ document_id: did, query: 'rev', useful: true });
expect(fb).toHaveBeenCalledWith(42, 'rev', true, undefined);
expect(o).toContain('Feedback');
});
});
describe('Pipeline: scope filtering', () => {
it('personal skips workspace and KVARK', async () => {
const w = memStub([{ id: 1, content: 'ws', score: 0.8 }]);
const p = memStub([{ id: 2, content: 'pers', score: 0.7 }]);
const k = kStub([{ id: 42, title: 'D', snippet: 'e', score: 0.9 }]);
const cr = new CombinedRetrieval({ workspaceSearch: w, personalSearch: p, kvarkClient: k });
const r = await cr.search('t', { scope: 'personal' });
expect(r.workspaceResults).toHaveLength(0);
expect(r.kvarkResults).toHaveLength(0);
expect(w.search).not.toHaveBeenCalled();
expect(k.search).not.toHaveBeenCalled();
});
it('workspace skips personal and KVARK', async () => {
const w = memStub([{ id: 1, content: 'ws', score: 0.8 }]);
const p = memStub([{ id: 2, content: 'pers', score: 0.7 }]);
const k = kStub([{ id: 42, title: 'D', snippet: 'e', score: 0.9 }]);
const cr = new CombinedRetrieval({ workspaceSearch: w, personalSearch: p, kvarkClient: k });
const r = await cr.search('t', { scope: 'workspace' });
expect(r.personalResults).toHaveLength(0);
expect(r.kvarkResults).toHaveLength(0);
expect(p.search).not.toHaveBeenCalled();
expect(k.search).not.toHaveBeenCalled();
});
});

View File

@@ -1,431 +0,0 @@
/**
* KVARK Agent Tools — tests with mocked KvarkClient.
*
* Tests mock the KvarkClient interface, NOT HTTP.
* This validates tool behavior: output formatting, error handling, attribution.
*/
import { describe, it, expect, vi } from 'vitest';
import {
createKvarkTools,
parseSearchResults,
type KvarkClientLike,
type KvarkSearchResponseLike,
type KvarkAskResponseLike,
} from '../src/kvark-tools.js';
// ── Mock client factory ──────────────────────────────────────────────────
function mockClient(overrides?: Partial<KvarkClientLike>): KvarkClientLike {
return {
search: overrides?.search ?? vi.fn(async () => SEARCH_RESPONSE),
askDocument: overrides?.askDocument ?? vi.fn(async () => ASK_RESPONSE),
feedback: overrides?.feedback ?? vi.fn(async () => ({ ok: true })),
action: overrides?.action ?? vi.fn(async () => ({ ok: true, data: { status: 'executed' as const, actionId: 'act_001', auditRef: 'aud_001' }, error: null })),
};
}
const SEARCH_RESPONSE: KvarkSearchResponseLike = {
results: [
{ document_id: 42, title: 'Project Status Update', snippet: 'API design review was postponed to next sprint due to unresolved auth decisions.', score: 0.92, document_type: 'pdf' },
{ document_id: 108, title: 'Q1 Budget Analysis', snippet: 'Budget allocation for engineering increased by 15%.', score: 0.87, document_type: 'spreadsheet' },
{ document_id: 215, title: 'Architecture Decision Records', snippet: 'Auth boundary decision pending review by security team.', score: 0.81, document_type: null },
],
total: 12,
query: 'project status',
};
const ASK_RESPONSE: KvarkAskResponseLike = {
answer: 'The blocker is unresolved identity boundary design, which delayed the API review to next sprint.',
sources: ['Project Status Update.pdf'],
};
// ── Tests ────────────────────────────────────────────────────────────────
describe('KVARK Agent Tools', () => {
describe('kvark_search', () => {
it('calls client.search with query and limit', async () => {
const searchFn = vi.fn(async () => SEARCH_RESPONSE);
const client = mockClient({ search: searchFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
await tool.execute({ query: 'project status', limit: 5 });
expect(searchFn).toHaveBeenCalledWith('project status', { limit: 5 });
});
it('formats search results with type, title, score, snippet, and ID', async () => {
const client = mockClient();
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
const output = await tool.execute({ query: 'project status' });
expect(output).toContain('KVARK Search: "project status"');
expect(output).toContain('3 of 12 results');
expect(output).toContain('[pdf] Project Status Update');
expect(output).toContain('score: 0.92');
expect(output).toContain('ID: 42');
expect(output).toContain('[spreadsheet] Q1 Budget');
expect(output).toContain('[document] Architecture Decision Records'); // null type → [document]
});
it('returns clear message on empty results', async () => {
const client = mockClient({
search: vi.fn(async () => ({ results: [], total: 0, query: 'nonexistent' })),
});
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
const output = await tool.execute({ query: 'nonexistent' });
expect(output).toContain('no results found');
});
it('returns graceful message when KVARK is unavailable', async () => {
const err = new Error('KVARK unreachable');
err.name = 'KvarkUnavailableError';
const client = mockClient({ search: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
const output = await tool.execute({ query: 'test' });
expect(output).toContain('not reachable');
expect(output).toContain('workspace memory');
});
it('returns graceful message on auth failure', async () => {
const err = new Error('Invalid token');
err.name = 'KvarkAuthError';
const client = mockClient({ search: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
const output = await tool.execute({ query: 'test' });
expect(output).toContain('authentication failed');
});
it('defaults limit to 10 when not specified', async () => {
const searchFn = vi.fn(async () => SEARCH_RESPONSE);
const client = mockClient({ search: searchFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_search')!;
await tool.execute({ query: 'test' });
expect(searchFn).toHaveBeenCalledWith('test', { limit: 10 });
});
});
describe('kvark_ask_document', () => {
it('calls client.askDocument with document_id and question', async () => {
const askFn = vi.fn(async () => ASK_RESPONSE);
const client = mockClient({ askDocument: askFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_ask_document')!;
await tool.execute({ document_id: '42', question: 'What is the blocker?' });
expect(askFn).toHaveBeenCalledWith('42', 'What is the blocker?');
});
it('formats answer with document ID and sources', async () => {
const client = mockClient();
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_ask_document')!;
const output = await tool.execute({ document_id: '42', question: 'test' });
expect(output).toContain('KVARK Document Answer (doc #42)');
expect(output).toContain('identity boundary design');
expect(output).toContain('Sources: Project Status Update.pdf');
});
it('returns graceful message when endpoint returns 501', async () => {
const err = new Error('Not implemented');
err.name = 'KvarkNotImplementedError';
const client = mockClient({ askDocument: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_ask_document')!;
const output = await tool.execute({ document_id: '42', question: 'test' });
expect(output).toContain('not yet available');
expect(output).toContain('kvark_search');
});
it('returns graceful message when document not found', async () => {
const err = new Error('Not found');
err.name = 'KvarkNotFoundError';
const client = mockClient({ askDocument: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_ask_document')!;
const output = await tool.execute({ document_id: '999', question: 'test' });
expect(output).toContain('not found');
});
});
describe('parseSearchResults (Milestone B helper)', () => {
it('converts search response to structured results with attribution', () => {
const results = parseSearchResults(SEARCH_RESPONSE);
expect(results).toHaveLength(3);
// First result
expect(results[0].documentId).toBe(42);
expect(results[0].title).toBe('Project Status Update');
expect(results[0].score).toBe(0.92);
expect(results[0].documentType).toBe('pdf');
expect(results[0].attribution).toBe('[KVARK: pdf: Project Status Update]');
expect(results[0].content).toContain('API design review');
// Null document_type → attribution without type segment
expect(results[2].attribution).toBe('[KVARK: Architecture Decision Records]');
});
it('returns empty array for empty results', () => {
const results = parseSearchResults({ results: [], total: 0, query: 'empty' });
expect(results).toEqual([]);
});
});
describe('tool definitions', () => {
it('creates exactly 4 tools', () => {
const tools = createKvarkTools({ client: mockClient() });
expect(tools).toHaveLength(4);
expect(tools.map(t => t.name)).toEqual(['kvark_search', 'kvark_feedback', 'kvark_action', 'kvark_ask_document']);
});
it('kvark_search requires query parameter', () => {
const tools = createKvarkTools({ client: mockClient() });
const search = tools[0];
expect((search.parameters as Record<string, unknown>).required).toEqual(['query']);
});
it('kvark_ask_document requires document_id and question', () => {
const tools = createKvarkTools({ client: mockClient() });
const ask = tools.find(t => t.name === 'kvark_ask_document')!;
expect((ask.parameters as Record<string, unknown>).required).toEqual(['document_id', 'question']);
});
it('kvark_feedback requires document_id, query, useful', () => {
const tools = createKvarkTools({ client: mockClient() });
const feedback = tools.find(t => t.name === 'kvark_feedback')!;
expect(feedback).toBeDefined();
expect((feedback.parameters as Record<string, unknown>).required).toEqual(['document_id', 'query', 'useful']);
});
it('kvark_action requires action_type, entity_type, entity_id, payload, reason', () => {
const tools = createKvarkTools({ client: mockClient() });
const action = tools.find(t => t.name === 'kvark_action')!;
expect(action).toBeDefined();
expect((action.parameters as Record<string, unknown>).required).toEqual(['action_type', 'entity_type', 'entity_id', 'payload', 'reason']);
});
});
// ── kvark_feedback ────────────────────────────────────────────────
describe('kvark_feedback', () => {
it('calls client.feedback with correct parameters', async () => {
const feedbackFn = vi.fn(async () => ({ ok: true }));
const client = mockClient({ feedback: feedbackFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
await tool.execute({ document_id: 42, query: 'project status', useful: true, reason: 'Exactly what I needed' });
expect(feedbackFn).toHaveBeenCalledWith(42, 'project status', true, 'Exactly what I needed');
});
it('returns confirmation on success', async () => {
const client = mockClient();
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
const output = await tool.execute({ document_id: 42, query: 'test', useful: true });
expect(output).toContain('Feedback recorded');
expect(output).toContain('useful');
expect(output).toContain('42');
});
it('returns negative feedback message', async () => {
const client = mockClient();
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
const output = await tool.execute({ document_id: 108, query: 'budget', useful: false });
expect(output).toContain('not useful');
expect(output).toContain('108');
});
it('handles feedback failure gracefully', async () => {
const err = new Error('Network error');
err.name = 'KvarkUnavailableError';
const client = mockClient({ feedback: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
const output = await tool.execute({ document_id: 42, query: 'test', useful: true });
expect(output).toContain('not reachable');
});
it('handles 501 Not Implemented gracefully', async () => {
const err = new Error('Not implemented');
err.name = 'KvarkNotImplementedError';
const client = mockClient({ feedback: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
const output = await tool.execute({ document_id: 42, query: 'test', useful: true });
expect(output).toContain('not yet available');
expect(output).toContain('non-blocking');
});
it('handles client without feedback method', async () => {
const client: KvarkClientLike = {
search: vi.fn(async () => SEARCH_RESPONSE),
askDocument: vi.fn(async () => ASK_RESPONSE),
// no feedback method
};
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
const output = await tool.execute({ document_id: 42, query: 'test', useful: true });
expect(output).toContain('not supported');
});
it('works without optional reason parameter', async () => {
const feedbackFn = vi.fn(async () => ({ ok: true }));
const client = mockClient({ feedback: feedbackFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_feedback')!;
await tool.execute({ document_id: 42, query: 'test', useful: false });
expect(feedbackFn).toHaveBeenCalledWith(42, 'test', false, undefined);
});
});
// ── kvark_action ──────────────────────────────────────────────────
describe('kvark_action', () => {
it('calls client.action with correct parameters', async () => {
const actionFn = vi.fn(async () => ({ ok: true, data: { status: 'executed' as const, actionId: 'act_001' }, error: null }));
const client = mockClient({ action: actionFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
await tool.execute({
action_type: 'jira.create_comment',
entity_type: 'issue',
entity_id: 'PROJ-142',
payload: { comment: 'Follow-up from Waggle' },
reason: 'User requested follow-up',
});
expect(actionFn).toHaveBeenCalledWith(
'jira.create_comment',
{ entityType: 'issue', entityId: 'PROJ-142' },
{ comment: 'Follow-up from Waggle' },
'User requested follow-up',
);
});
it('returns confirmation on successful execution', async () => {
const client = mockClient();
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
const output = await tool.execute({
action_type: 'jira.create_comment',
entity_type: 'issue',
entity_id: 'PROJ-142',
payload: { comment: 'test' },
reason: 'test',
});
expect(output).toContain('Action executed');
expect(output).toContain('jira.create_comment');
expect(output).toContain('act_001');
});
it('reports denial from KVARK governance', async () => {
const actionFn = vi.fn(async () => ({
ok: false,
data: null,
error: { code: 'approval_required_or_denied', message: 'Action not permitted under current policy.' },
}));
const client = mockClient({ action: actionFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
const output = await tool.execute({
action_type: 'jira.create_comment',
entity_type: 'issue',
entity_id: 'X-1',
payload: {},
reason: 'test',
});
expect(output).toContain('Action denied');
expect(output).toContain('not permitted');
});
it('handles 501 Not Implemented gracefully', async () => {
const err = new Error('Not implemented');
err.name = 'KvarkNotImplementedError';
const client = mockClient({ action: vi.fn(async () => { throw err; }) });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
const output = await tool.execute({
action_type: 'test', entity_type: 'x', entity_id: '1', payload: {}, reason: 'test',
});
expect(output).toContain('not yet available');
expect(output).toContain('not executed');
});
it('handles client without action method', async () => {
const client: KvarkClientLike = {
search: vi.fn(async () => SEARCH_RESPONSE),
askDocument: vi.fn(async () => ASK_RESPONSE),
};
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
const output = await tool.execute({
action_type: 'test', entity_type: 'x', entity_id: '1', payload: {}, reason: 'test',
});
expect(output).toContain('not supported');
});
it('reports queued status correctly', async () => {
const actionFn = vi.fn(async () => ({
ok: true,
data: { status: 'queued' as const, actionId: 'act_q1' },
error: null,
}));
const client = mockClient({ action: actionFn });
const tools = createKvarkTools({ client });
const tool = tools.find(t => t.name === 'kvark_action')!;
const output = await tool.execute({
action_type: 'email.send', entity_type: 'contact', entity_id: 'c_1', payload: { subject: 'hi' }, reason: 'outreach',
});
expect(output).toContain('queued');
});
});
});

View File

@@ -1,9 +1,9 @@
/**
* Skills 2.0 gap E — promote_skill tool.
*
* Tests the one-step promotion chain personal → workspace → team → enterprise,
* tier gating, frontmatter rewrite with promoted_from history, file move,
* and skill_promotion improvement signal emission.
* Tests the one-step promotion personal → workspace, the refusal of team and
* enterprise scopes in this edition, frontmatter rewrite with promoted_from
* history, file move, and skill_promotion improvement signal emission.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs';
@@ -32,8 +32,6 @@ describe('promote_skill — gap E', () => {
onSkillsChanged: () => { reloadCount++; },
getWorkspaceId: () => 'ws-alpha',
getTeamId: () => 'team-bravo',
hasTeamSkillLibrary: () => true,
isEnterprise: () => true,
improvementSignals: signals,
...overrides,
});
@@ -87,22 +85,6 @@ describe('promote_skill — gap E', () => {
// onSkillsChanged fired
expect(reloadCount).toBe(1);
});
it('appends to existing promoted_from across multiple hops', async () => {
const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!;
// Skill already promoted once (personal → workspace), now promote to team
fs.mkdirSync(wsDir, { recursive: true });
const content = `---\nname: already-moved\nscope: workspace\npromoted_from: [personal]\n---\nbody`;
fs.writeFileSync(path.join(wsDir, 'already-moved.md'), content, 'utf-8');
const tools = createTools();
await run(tools, 'promote_skill', { skill_name: 'already-moved' });
const teamDir = getSkillDirForScope(tmpDir, 'team', { teamId: 'team-bravo' })!;
const parsed = parseSkillFrontmatter(fs.readFileSync(path.join(teamDir, 'already-moved.md'), 'utf-8'));
expect(parsed.frontmatter.scope).toBe('team');
expect(parsed.frontmatter.promoted_from).toEqual(['personal', 'workspace']);
});
});
describe('validation', () => {
@@ -136,30 +118,22 @@ describe('promote_skill — gap E', () => {
});
});
describe('tier gating', () => {
it('denies team promotion when hasTeamSkillLibrary is false', async () => {
describe('edition limits', () => {
it('refuses team promotion and leaves the skill in place', async () => {
const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!;
seedSkill(wsDir, 'paid-feature', 'workspace');
const tools = createTools({ hasTeamSkillLibrary: () => false });
const out = await run(tools, 'promote_skill', { skill_name: 'paid-feature' });
expect(out).toContain('your plan does not include');
expect(out).toContain('TEAMS or ENTERPRISE');
seedSkill(wsDir, 'shared-helper', 'workspace');
const out = await run(createTools(), 'promote_skill', { skill_name: 'shared-helper' });
expect(out).toContain('not available in this edition');
expect(fs.existsSync(path.join(wsDir, 'shared-helper.md'))).toBe(true);
expect(signals.getByCategory('skill_promotion')).toHaveLength(0);
});
it('denies team promotion when no team is linked even if capability allows', async () => {
const wsDir = getSkillDirForScope(tmpDir, 'workspace', { workspaceId: 'ws-alpha' })!;
seedSkill(wsDir, 'solo-ws', 'workspace');
const tools = createTools({ getTeamId: () => null });
const out = await run(tools, 'promote_skill', { skill_name: 'solo-ws' });
expect(out).toContain('not linked to a team');
});
it('denies enterprise promotion without isEnterprise', async () => {
it('refuses enterprise promotion', async () => {
const teamDir = getSkillDirForScope(tmpDir, 'team', { teamId: 'team-bravo' })!;
seedSkill(teamDir, 'hot-skill', 'team');
const tools = createTools({ isEnterprise: () => false });
const out = await run(tools, 'promote_skill', { skill_name: 'hot-skill' });
expect(out).toContain('requires ENTERPRISE tier');
const out = await run(createTools(), 'promote_skill', { skill_name: 'hot-skill' });
expect(out).toContain('Cannot promote to enterprise scope');
expect(out).toContain('not available in this edition');
});
it('denies workspace promotion when no workspace is active', async () => {

View File

@@ -1,11 +1,8 @@
/**
* B2 wiring tests — search_memory integration with CombinedRetrieval.
*
* Tests the search_memory tool's behavior with and without kvarkClient,
* and the formatCombinedResult helper.
* formatCombinedResult — renders a CombinedRetrieval result for the agent.
*/
import { describe, it, expect, vi } from 'vitest';
import { describe, it, expect } from 'vitest';
import { formatCombinedResult } from '../src/tools.js';
import type { CombinedRetrievalResult, CombinedResult } from '../src/combined-retrieval.js';
@@ -26,10 +23,6 @@ function makeRetrievalResult(overrides: Partial<CombinedRetrievalResult> = {}):
query: overrides.query ?? 'test',
workspaceResults: overrides.workspaceResults ?? [],
personalResults: overrides.personalResults ?? [],
kvarkResults: overrides.kvarkResults ?? [],
kvarkAvailable: overrides.kvarkAvailable ?? false,
kvarkSkipped: overrides.kvarkSkipped ?? false,
kvarkError: overrides.kvarkError,
};
}
@@ -88,92 +81,14 @@ describe('formatCombinedResult', () => {
expect(result).toContain('some pref');
});
it('formats KVARK results with Enterprise Knowledge header and attribution', () => {
const result = formatCombinedResult(makeRetrievalResult({
kvarkResults: [
makeCombinedResult({
source: 'kvark',
attribution: '[KVARK: pdf: Q3 Report]',
content: 'Revenue grew 15%',
score: 0.92,
metadata: { documentId: 42, documentType: 'pdf' },
}),
],
kvarkAvailable: true,
}), true);
expect(result).toContain('## Enterprise Knowledge (KVARK)');
expect(result).toContain('[KVARK: pdf: Q3 Report]');
expect(result).toContain('Revenue grew 15%');
expect(result).toContain('score: 0.920');
});
it('appends KVARK error note while preserving local results', () => {
const result = formatCombinedResult(makeRetrievalResult({
workspaceResults: [
makeCombinedResult({ content: 'local fact', score: 0.7 }),
],
kvarkError: 'Connection refused',
kvarkAvailable: true,
}), true);
expect(result).toContain('local fact');
expect(result).toContain('Enterprise search encountered an error: Connection refused');
expect(result).toContain('Results shown are from local memory only');
});
it('formats all three sources together correctly', () => {
it('formats workspace and personal sources in order', () => {
const result = formatCombinedResult(makeRetrievalResult({
workspaceResults: [makeCombinedResult({ content: 'ws data', score: 0.9 })],
personalResults: [makeCombinedResult({ source: 'personal', content: 'personal data', score: 0.7 })],
kvarkResults: [makeCombinedResult({ source: 'kvark', attribution: '[KVARK: doc]', content: 'enterprise data', score: 0.85 })],
kvarkAvailable: true,
}), true);
expect(result).toContain('## Workspace Memory');
expect(result).toContain('## Personal Memory');
expect(result).toContain('## Enterprise Knowledge (KVARK)');
// Verify ordering: workspace → personal → KVARK
const wsIdx = result.indexOf('## Workspace Memory');
const pIdx = result.indexOf('## Personal Memory');
const kIdx = result.indexOf('## Enterprise Knowledge');
expect(wsIdx).toBeLessThan(pIdx);
expect(pIdx).toBeLessThan(kIdx);
});
it('KVARK results use score-only format (no frame_type/importance)', () => {
const result = formatCombinedResult(makeRetrievalResult({
kvarkResults: [
makeCombinedResult({
source: 'kvark',
attribution: '[KVARK: pdf: Doc]',
content: 'content',
score: 0.9,
}),
],
kvarkAvailable: true,
}), true);
// KVARK results should NOT have frame_type or importance
expect(result).not.toContain('type:');
expect(result).not.toContain('importance:');
expect(result).toContain('score: 0.900');
});
});
// ── MindToolDeps.kvarkClient integration (structural tests) ─────────────
describe('MindToolDeps kvarkClient integration', () => {
it('MindToolDeps accepts optional kvarkClient', async () => {
// This is a compile-time test — if MindToolDeps didn't have kvarkClient,
// TypeScript would catch it. We verify the type exists by importing it.
const { type } = await import('../src/tools.js');
// If we got here, the import succeeded and the type exists
expect(true).toBe(true);
});
it('OrchestratorConfig accepts optional kvarkClient', async () => {
const { type } = await import('../src/orchestrator.js');
expect(true).toBe(true);
expect(result.indexOf('## Workspace Memory')).toBeLessThan(result.indexOf('## Personal Memory'));
});
});