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:
@@ -1,27 +1,20 @@
|
||||
/**
|
||||
* Combined Retrieval — merges workspace memory, personal memory, and KVARK enterprise search.
|
||||
* Combined Retrieval — merges workspace memory and personal memory.
|
||||
*
|
||||
* This is the core merge engine for Milestone B. It does NOT format output for
|
||||
* the agent (that's search_memory's job in B2). It returns structured data with
|
||||
* source attribution so the consumer can format however it needs.
|
||||
* It does NOT format output for the agent (that's search_memory's job). It
|
||||
* returns structured data with source attribution so the consumer can format
|
||||
* however it needs.
|
||||
*
|
||||
* Design:
|
||||
* - Pure data in, pure data out — no side effects, no framework deps
|
||||
* - KVARK is only called when local results are insufficient
|
||||
* - Every result carries explicit source attribution
|
||||
* - KVARK failures degrade gracefully (local results preserved, error captured)
|
||||
*/
|
||||
|
||||
import {
|
||||
parseSearchResults,
|
||||
type KvarkClientLike,
|
||||
type KvarkStructuredResult,
|
||||
} from './kvark-tools.js';
|
||||
import { logTurnEvent } from './turn-context.js';
|
||||
|
||||
// ── Public types ──────────────────────────────────────────────────────────
|
||||
|
||||
export type ResultSource = 'workspace' | 'personal' | 'kvark';
|
||||
export type ResultSource = 'workspace' | 'personal';
|
||||
|
||||
export interface CombinedResult {
|
||||
content: string;
|
||||
@@ -29,13 +22,9 @@ export interface CombinedResult {
|
||||
attribution: string;
|
||||
score: number;
|
||||
metadata: {
|
||||
// Memory-sourced
|
||||
frameId?: number;
|
||||
frameType?: string;
|
||||
importance?: string;
|
||||
// KVARK-sourced
|
||||
documentId?: number;
|
||||
documentType?: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -43,14 +32,6 @@ export interface CombinedRetrievalResult {
|
||||
query: string;
|
||||
workspaceResults: CombinedResult[];
|
||||
personalResults: CombinedResult[];
|
||||
kvarkResults: CombinedResult[];
|
||||
kvarkAvailable: boolean;
|
||||
kvarkSkipped: boolean;
|
||||
kvarkError?: string;
|
||||
/** True when workspace memory and KVARK results may disagree on the same topic */
|
||||
hasConflict: boolean;
|
||||
/** Human-readable note explaining detected conflict (undefined when no conflict) */
|
||||
conflictNote?: string;
|
||||
}
|
||||
|
||||
export interface CombinedSearchOptions {
|
||||
@@ -83,71 +64,6 @@ export interface MemorySearchResultLike {
|
||||
export interface CombinedRetrievalDeps {
|
||||
workspaceSearch: MemorySearchLike | null;
|
||||
personalSearch: MemorySearchLike;
|
||||
kvarkClient: KvarkClientLike | null;
|
||||
}
|
||||
|
||||
// ── Constants ─────────────────────────────────────────────────────────────
|
||||
|
||||
/** Minimum local results with strong scores before we skip KVARK */
|
||||
const LOCAL_COVERAGE_MIN_COUNT = 3;
|
||||
/** Score threshold to consider a local result "strong" */
|
||||
const LOCAL_COVERAGE_SCORE_THRESHOLD = 0.7;
|
||||
/** Minimum score for a result to participate in conflict detection */
|
||||
const CONFLICT_SCORE_THRESHOLD = 0.6;
|
||||
|
||||
// ── Conflict detection ───────────────────────────────────────────────────
|
||||
|
||||
/** Status/decision keywords grouped by polarity */
|
||||
const POSITIVE_STATUS = ['approved', 'accepted', 'selected', 'chose', 'chosen', 'decided', 'confirmed', 'active', 'completed', 'launched', 'enabled'];
|
||||
const NEGATIVE_STATUS = ['rejected', 'cancelled', 'canceled', 'postponed', 'deprecated', 'deferred', 'suspended', 'disabled', 'abandoned', 'declined', 'revoked'];
|
||||
|
||||
/**
|
||||
* Detect potential conflict between workspace memory and KVARK results.
|
||||
*
|
||||
* Conservative heuristic — only flags when:
|
||||
* 1. Both sources have relevant results (score ≥ threshold)
|
||||
* 2. Top results contain contradictory status/decision language
|
||||
*
|
||||
* Returns null when no conflict detected, or a short explanatory note.
|
||||
*/
|
||||
export function detectConflict(
|
||||
workspaceResults: CombinedResult[],
|
||||
kvarkResults: CombinedResult[],
|
||||
): string | null {
|
||||
// Need strong results from both sources
|
||||
const strongWs = workspaceResults.filter(r => r.score >= CONFLICT_SCORE_THRESHOLD);
|
||||
const strongKvark = kvarkResults.filter(r => r.score >= CONFLICT_SCORE_THRESHOLD);
|
||||
if (strongWs.length === 0 || strongKvark.length === 0) return null;
|
||||
|
||||
// Check top results (up to 3 from each) for status polarity conflict
|
||||
const wsTexts = strongWs.slice(0, 3).map(r => r.content.toLowerCase());
|
||||
const kvarkTexts = strongKvark.slice(0, 3).map(r => r.content.toLowerCase());
|
||||
|
||||
const wsPolarity = extractPolarity(wsTexts);
|
||||
const kvarkPolarity = extractPolarity(kvarkTexts);
|
||||
|
||||
// Conflict: one source is positive, the other is negative
|
||||
if (wsPolarity === 'positive' && kvarkPolarity === 'negative') {
|
||||
return 'Workspace memory contains affirmative language (approved/selected/active) while enterprise documents contain contradictory language (rejected/cancelled/deprecated). These sources may be out of sync.';
|
||||
}
|
||||
if (wsPolarity === 'negative' && kvarkPolarity === 'positive') {
|
||||
return 'Enterprise documents contain affirmative language while workspace memory contains contradictory language. The enterprise source may be more current.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
type Polarity = 'positive' | 'negative' | 'neutral';
|
||||
|
||||
function extractPolarity(texts: string[]): Polarity {
|
||||
const combined = texts.join(' ');
|
||||
const hasPositive = POSITIVE_STATUS.some(w => combined.includes(w));
|
||||
const hasNegative = NEGATIVE_STATUS.some(w => combined.includes(w));
|
||||
|
||||
// Only assign polarity when one side dominates
|
||||
if (hasPositive && !hasNegative) return 'positive';
|
||||
if (hasNegative && !hasPositive) return 'negative';
|
||||
return 'neutral';
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
@@ -170,38 +86,6 @@ export function mapMemoryResult(
|
||||
};
|
||||
}
|
||||
|
||||
/** Map a KvarkStructuredResult into a CombinedResult */
|
||||
export function mapKvarkResult(result: KvarkStructuredResult): CombinedResult {
|
||||
return {
|
||||
content: result.content,
|
||||
source: 'kvark',
|
||||
attribution: result.attribution,
|
||||
score: result.score,
|
||||
metadata: {
|
||||
documentId: result.documentId,
|
||||
documentType: result.documentType,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Check whether local results are strong enough to skip KVARK */
|
||||
export function hasSufficientLocalCoverage(results: CombinedResult[]): boolean {
|
||||
const strongResults = results.filter(r => r.score >= LOCAL_COVERAGE_SCORE_THRESHOLD);
|
||||
return strongResults.length >= LOCAL_COVERAGE_MIN_COUNT;
|
||||
}
|
||||
|
||||
/** Decide whether KVARK should be queried */
|
||||
export function shouldQueryKvark(
|
||||
kvarkClient: KvarkClientLike | null,
|
||||
scope: 'all' | 'personal' | 'workspace',
|
||||
localResults: CombinedResult[],
|
||||
): boolean {
|
||||
if (!kvarkClient) return false;
|
||||
if (scope === 'personal' || scope === 'workspace') return false;
|
||||
if (hasSufficientLocalCoverage(localResults)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ── Main class ────────────────────────────────────────────────────────────
|
||||
|
||||
export class CombinedRetrieval {
|
||||
@@ -215,61 +99,15 @@ export class CombinedRetrieval {
|
||||
const { limit = 10, profile = 'balanced', scope = 'all', turnId } = opts;
|
||||
logTurnEvent(turnId, { stage: 'retrieval.enter', queryChars: query.length, limit, profile, scope });
|
||||
|
||||
// 1. Search workspace memory
|
||||
const workspaceResults = await this.searchWorkspace(query, limit, profile, scope);
|
||||
|
||||
// 2. Search personal memory
|
||||
const personalResults = await this.searchPersonal(query, limit, profile, scope);
|
||||
|
||||
// 3. Decide whether to call KVARK
|
||||
const localResults = [...workspaceResults, ...personalResults];
|
||||
const kvarkAvailable = this.deps.kvarkClient !== null;
|
||||
const callKvark = shouldQueryKvark(this.deps.kvarkClient, scope, localResults);
|
||||
|
||||
if (!callKvark) {
|
||||
logTurnEvent(turnId, {
|
||||
stage: 'retrieval.exit',
|
||||
workspaceHits: workspaceResults.length,
|
||||
personalHits: personalResults.length,
|
||||
kvarkHits: 0,
|
||||
kvarkSkipped: kvarkAvailable,
|
||||
});
|
||||
return {
|
||||
query,
|
||||
workspaceResults,
|
||||
personalResults,
|
||||
kvarkResults: [],
|
||||
kvarkAvailable,
|
||||
kvarkSkipped: kvarkAvailable, // skipped only if it was available but we chose not to call
|
||||
hasConflict: false,
|
||||
};
|
||||
}
|
||||
|
||||
// 4. Call KVARK (with graceful degradation)
|
||||
const { kvarkResults, kvarkError } = await this.searchKvark(query, limit);
|
||||
|
||||
// 5. Detect potential conflict between workspace memory and KVARK
|
||||
const conflictNote = detectConflict(workspaceResults, kvarkResults);
|
||||
|
||||
logTurnEvent(turnId, {
|
||||
stage: 'retrieval.exit',
|
||||
workspaceHits: workspaceResults.length,
|
||||
personalHits: personalResults.length,
|
||||
kvarkHits: kvarkResults.length,
|
||||
kvarkError: kvarkError ?? null,
|
||||
hasConflict: conflictNote !== null,
|
||||
});
|
||||
return {
|
||||
query,
|
||||
workspaceResults,
|
||||
personalResults,
|
||||
kvarkResults,
|
||||
kvarkAvailable: true,
|
||||
kvarkSkipped: false,
|
||||
kvarkError,
|
||||
hasConflict: conflictNote !== null,
|
||||
conflictNote: conflictNote ?? undefined,
|
||||
};
|
||||
return { query, workspaceResults, personalResults };
|
||||
}
|
||||
|
||||
private async searchWorkspace(
|
||||
@@ -290,17 +128,4 @@ export class CombinedRetrieval {
|
||||
const results = await this.deps.personalSearch.search(query, { limit, profile });
|
||||
return results.map(r => mapMemoryResult(r, 'personal'));
|
||||
}
|
||||
|
||||
private async searchKvark(
|
||||
query: string, limit: number,
|
||||
): Promise<{ kvarkResults: CombinedResult[]; kvarkError?: string }> {
|
||||
try {
|
||||
const response = await this.deps.kvarkClient!.search(query, { limit });
|
||||
const structured = parseSearchResults(response);
|
||||
return { kvarkResults: structured.map(mapKvarkResult) };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown KVARK error';
|
||||
return { kvarkResults: [], kvarkError: message };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,343 +0,0 @@
|
||||
/**
|
||||
* Skills 2.0 gap H — boardroom-grade compliance report PDF.
|
||||
*
|
||||
* Consumes an AuditReport (from @waggle/core/compliance) and produces a
|
||||
* multi-page PDF styled with Waggle's Hive DS tokens (honey #E5A000).
|
||||
* The layout is designed for the KVARK sales pitch: readable at arm's
|
||||
* length, executive summary on page 1, detail tables on subsequent
|
||||
* pages, page numbers, metadata header.
|
||||
*
|
||||
* Sections:
|
||||
* 1. Cover — org name, risk level, report period, generated-at
|
||||
* 2. Executive Summary — compliance status + status badges
|
||||
* 3. Article status grid (Art 12/14/19/26/50)
|
||||
* 4. Model Inventory table
|
||||
* 5. Human Oversight Log table
|
||||
* 6. Harvest Provenance table
|
||||
* 7. Closing: totals + signature line
|
||||
*/
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import type { AuditReport, ComplianceStatus, ArticleStatus, AIActRiskLevel } from '@waggle/core';
|
||||
import type { TDocumentDefinitions, Content, TableCell } from 'pdfmake/interfaces.js';
|
||||
|
||||
// Minimal surface of the pdfmake static we use (createPdf().getBuffer()).
|
||||
interface PdfPrinter {
|
||||
getBuffer(cb: (buffer: Buffer) => void): void;
|
||||
}
|
||||
interface PdfMakeStatic {
|
||||
createPdf(docDef: TDocumentDefinitions): PdfPrinter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional template-sourced overrides (M-03). Applied over the workspace-derived
|
||||
* values so a single workspace can render under multiple branded templates without
|
||||
* mutating the underlying WorkspaceConfig. Logo is deferred to Bucket 2.
|
||||
*/
|
||||
export interface PdfTemplateOverrides {
|
||||
orgName?: string | null;
|
||||
footerText?: string | null;
|
||||
riskClassification?: AIActRiskLevel | null;
|
||||
}
|
||||
|
||||
/** Hive DS color palette — match design-system conventions */
|
||||
const HIVE_HONEY = '#E5A000';
|
||||
const HIVE_DARK = '#08090C';
|
||||
const HIVE_ACCENT = '#A78BFA';
|
||||
const STATUS_COLORS: Record<ComplianceStatus['overall'], string> = {
|
||||
compliant: '#22A06B',
|
||||
warning: '#E5A000',
|
||||
'non-compliant': '#C4342F',
|
||||
};
|
||||
|
||||
const ARTICLE_LABELS: Record<string, string> = {
|
||||
art12Logging: 'Art. 12 — Logging',
|
||||
art14Oversight: 'Art. 14 — Human Oversight',
|
||||
art19Retention: 'Art. 19 — Data Retention',
|
||||
art26Monitoring: 'Art. 26 — Risk Classification',
|
||||
art50Transparency: 'Art. 50 — Transparency',
|
||||
};
|
||||
|
||||
function statusBadge(status: ArticleStatus['status']): Content {
|
||||
const color = status === 'compliant' ? STATUS_COLORS.compliant
|
||||
: status === 'warning' ? STATUS_COLORS.warning
|
||||
: STATUS_COLORS['non-compliant'];
|
||||
const label = status === 'compliant' ? 'COMPLIANT'
|
||||
: status === 'warning' ? 'WARNING'
|
||||
: 'NON-COMPLIANT';
|
||||
return { text: label, bold: true, color, fontSize: 9 };
|
||||
}
|
||||
|
||||
function coverContent(report: AuditReport, overrides?: PdfTemplateOverrides): Content[] {
|
||||
const wsName = (overrides?.orgName && overrides.orgName.trim()) || report.workspace?.name || 'Personal Mind';
|
||||
const riskLevel = (overrides?.riskClassification ?? report.workspace?.riskLevel ?? 'minimal').toUpperCase();
|
||||
return [
|
||||
{ text: 'AI ACT COMPLIANCE AUDIT', style: 'titleKicker', margin: [0, 120, 0, 6] },
|
||||
{ text: wsName, style: 'title', margin: [0, 0, 0, 12] },
|
||||
{ canvas: [{ type: 'line', x1: 0, y1: 0, x2: 500, y2: 0, lineWidth: 1.5, lineColor: HIVE_HONEY }], margin: [0, 0, 0, 24] },
|
||||
{
|
||||
columns: [
|
||||
[
|
||||
{ text: 'Risk Level', style: 'metaLabel' },
|
||||
{ text: riskLevel, style: 'metaValue', margin: [0, 2, 0, 12] },
|
||||
{ text: 'Period', style: 'metaLabel' },
|
||||
{ text: `${report.report.period.from.slice(0, 10)} → ${report.report.period.to.slice(0, 10)}`, style: 'metaValue', margin: [0, 2, 0, 12] },
|
||||
],
|
||||
[
|
||||
{ text: 'Overall Status', style: 'metaLabel' },
|
||||
{ text: report.complianceStatus.overall.toUpperCase(), style: 'metaValue', color: STATUS_COLORS[report.complianceStatus.overall], margin: [0, 2, 0, 12] },
|
||||
{ text: 'Generated', style: 'metaLabel' },
|
||||
{ text: report.report.generatedAt.slice(0, 19).replace('T', ' ') + ' UTC', style: 'metaValue', margin: [0, 2, 0, 12] },
|
||||
],
|
||||
],
|
||||
},
|
||||
{ text: '', pageBreak: 'after' },
|
||||
];
|
||||
}
|
||||
|
||||
function articleGrid(status: ComplianceStatus): Content {
|
||||
const rows: TableCell[][] = [
|
||||
[
|
||||
{ text: 'Article', style: 'tableHead' },
|
||||
{ text: 'Status', style: 'tableHead' },
|
||||
{ text: 'Detail', style: 'tableHead' },
|
||||
],
|
||||
];
|
||||
for (const [key, label] of Object.entries(ARTICLE_LABELS)) {
|
||||
const article = (status as unknown as Record<string, ArticleStatus>)[key];
|
||||
if (!article) continue;
|
||||
rows.push([
|
||||
{ text: label, bold: true, fontSize: 10 },
|
||||
statusBadge(article.status),
|
||||
{ text: article.detail, fontSize: 9 },
|
||||
]);
|
||||
}
|
||||
return {
|
||||
table: { headerRows: 1, widths: [150, 80, '*'], body: rows },
|
||||
layout: { hLineColor: () => '#E5E5E5', vLineColor: () => '#E5E5E5' },
|
||||
margin: [0, 0, 0, 20],
|
||||
};
|
||||
}
|
||||
|
||||
function modelInventoryTable(report: AuditReport): Content {
|
||||
if (report.modelInventory.length === 0) {
|
||||
return { text: 'No model calls recorded in this period.', italics: true, color: '#6B6B6B', margin: [0, 0, 0, 16] };
|
||||
}
|
||||
const rows: TableCell[][] = [[
|
||||
{ text: 'Model', style: 'tableHead' },
|
||||
{ text: 'Provider', style: 'tableHead' },
|
||||
{ text: 'Calls', style: 'tableHead', alignment: 'right' },
|
||||
{ text: 'Input tok', style: 'tableHead', alignment: 'right' },
|
||||
{ text: 'Output tok', style: 'tableHead', alignment: 'right' },
|
||||
{ text: 'Cost (USD)', style: 'tableHead', alignment: 'right' },
|
||||
]];
|
||||
let totalCalls = 0, totalIn = 0, totalOut = 0, totalCost = 0;
|
||||
for (const m of report.modelInventory) {
|
||||
totalCalls += m.calls;
|
||||
totalIn += m.inputTokens;
|
||||
totalOut += m.outputTokens;
|
||||
totalCost += m.costUsd;
|
||||
rows.push([
|
||||
{ text: m.model, fontSize: 9 },
|
||||
{ text: m.provider, fontSize: 9 },
|
||||
{ text: m.calls.toLocaleString('en-US'), fontSize: 9, alignment: 'right' },
|
||||
{ text: m.inputTokens.toLocaleString('en-US'), fontSize: 9, alignment: 'right' },
|
||||
{ text: m.outputTokens.toLocaleString('en-US'), fontSize: 9, alignment: 'right' },
|
||||
{ text: `$${m.costUsd.toFixed(4)}`, fontSize: 9, alignment: 'right' },
|
||||
]);
|
||||
}
|
||||
rows.push([
|
||||
{ text: 'TOTAL', bold: true, fontSize: 9, fillColor: '#FAFAFA' },
|
||||
{ text: '', fillColor: '#FAFAFA' },
|
||||
{ text: totalCalls.toLocaleString('en-US'), bold: true, fontSize: 9, alignment: 'right', fillColor: '#FAFAFA' },
|
||||
{ text: totalIn.toLocaleString('en-US'), bold: true, fontSize: 9, alignment: 'right', fillColor: '#FAFAFA' },
|
||||
{ text: totalOut.toLocaleString('en-US'), bold: true, fontSize: 9, alignment: 'right', fillColor: '#FAFAFA' },
|
||||
{ text: `$${totalCost.toFixed(4)}`, bold: true, fontSize: 9, alignment: 'right', fillColor: '#FAFAFA' },
|
||||
]);
|
||||
return {
|
||||
table: { headerRows: 1, widths: ['*', 70, 40, 60, 60, 60], body: rows },
|
||||
layout: { hLineColor: () => '#E5E5E5', vLineColor: () => '#E5E5E5' },
|
||||
margin: [0, 0, 0, 20],
|
||||
};
|
||||
}
|
||||
|
||||
function oversightLogTable(report: AuditReport): Content {
|
||||
if (report.humanOversightLog.length === 0) {
|
||||
return { text: 'No human oversight events in this period.', italics: true, color: '#6B6B6B', margin: [0, 0, 0, 16] };
|
||||
}
|
||||
// Cap at 50 most recent events to keep the PDF tight; detail goes in JSON report
|
||||
const shown = report.humanOversightLog.slice(-50);
|
||||
const rows: TableCell[][] = [[
|
||||
{ text: 'Timestamp', style: 'tableHead' },
|
||||
{ text: 'Action', style: 'tableHead' },
|
||||
{ text: 'Tool', style: 'tableHead' },
|
||||
{ text: 'Detail', style: 'tableHead' },
|
||||
]];
|
||||
for (const e of shown) {
|
||||
rows.push([
|
||||
{ text: e.timestamp.slice(0, 19).replace('T', ' '), fontSize: 8 },
|
||||
{ text: e.action, fontSize: 8, bold: true },
|
||||
{ text: e.tool, fontSize: 8 },
|
||||
{ text: e.detail.slice(0, 80), fontSize: 8 },
|
||||
]);
|
||||
}
|
||||
const contents: Content[] = [{
|
||||
table: { headerRows: 1, widths: [95, 60, 80, '*'], body: rows },
|
||||
layout: { hLineColor: () => '#E5E5E5', vLineColor: () => '#E5E5E5' },
|
||||
margin: [0, 0, 0, 8],
|
||||
}];
|
||||
if (report.humanOversightLog.length > 50) {
|
||||
contents.push({
|
||||
text: `Showing last 50 of ${report.humanOversightLog.length} events. Full log in JSON report.`,
|
||||
fontSize: 8, italics: true, color: '#6B6B6B', margin: [0, 0, 0, 16],
|
||||
});
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
|
||||
function provenanceTable(report: AuditReport): Content {
|
||||
if (report.harvestProvenance.length === 0) {
|
||||
return { text: 'No harvest provenance data for this period.', italics: true, color: '#6B6B6B', margin: [0, 0, 0, 16] };
|
||||
}
|
||||
const rows: TableCell[][] = [[
|
||||
{ text: 'Source', style: 'tableHead' },
|
||||
{ text: 'Imported At', style: 'tableHead' },
|
||||
{ text: 'Items', style: 'tableHead', alignment: 'right' },
|
||||
{ text: 'Frames', style: 'tableHead', alignment: 'right' },
|
||||
]];
|
||||
for (const p of report.harvestProvenance) {
|
||||
rows.push([
|
||||
{ text: p.source, fontSize: 9 },
|
||||
{ text: p.importedAt.slice(0, 19).replace('T', ' '), fontSize: 9 },
|
||||
{ text: p.itemsImported.toLocaleString('en-US'), fontSize: 9, alignment: 'right' },
|
||||
{ text: p.framesCreated.toLocaleString('en-US'), fontSize: 9, alignment: 'right' },
|
||||
]);
|
||||
}
|
||||
return {
|
||||
table: { headerRows: 1, widths: ['*', 120, 60, 60], body: rows },
|
||||
layout: { hLineColor: () => '#E5E5E5', vLineColor: () => '#E5E5E5' },
|
||||
margin: [0, 0, 0, 20],
|
||||
};
|
||||
}
|
||||
|
||||
/** Build the pdfmake document definition. Exported for unit-test introspection. */
|
||||
export function buildComplianceDocDefinition(
|
||||
report: AuditReport,
|
||||
overrides?: PdfTemplateOverrides,
|
||||
): TDocumentDefinitions {
|
||||
const wsName = (overrides?.orgName && overrides.orgName.trim()) || report.workspace?.name || 'Personal';
|
||||
const footerExtra = overrides?.footerText?.trim() || null;
|
||||
const content: Content[] = [
|
||||
...coverContent(report, overrides),
|
||||
|
||||
{ text: 'Compliance Status', style: 'h1', margin: [0, 0, 0, 6] },
|
||||
{
|
||||
text: report.complianceStatus.overall === 'compliant'
|
||||
? 'All monitored articles pass. The deployment operates within the AI Act framework for the selected period.'
|
||||
: report.complianceStatus.overall === 'warning'
|
||||
? 'One or more articles report warnings. Review the detail column for remediation.'
|
||||
: 'At least one article is non-compliant. Remediation is required before the next audit.',
|
||||
fontSize: 10, italics: true, color: '#333333', margin: [0, 0, 0, 16],
|
||||
},
|
||||
articleGrid(report.complianceStatus),
|
||||
|
||||
{ text: 'Model Inventory', style: 'h1', margin: [0, 0, 0, 6] },
|
||||
{ text: `Tracked LLM/embedding model calls across the selected period. Totals appear on the bottom row.`, fontSize: 10, color: '#333333', margin: [0, 0, 0, 10] },
|
||||
modelInventoryTable(report),
|
||||
|
||||
{ text: 'Human Oversight Log', style: 'h1', margin: [0, 0, 0, 6] },
|
||||
{ text: `Art. 14 record of human approve/deny/modify actions on agent-proposed tool calls. Total this period: ${report.humanOversightLog.length}.`, fontSize: 10, color: '#333333', margin: [0, 0, 0, 10] },
|
||||
...([] as Content[]).concat(oversightLogTable(report) as Content[] | Content),
|
||||
|
||||
{ text: 'Harvest Provenance', style: 'h1', margin: [0, 0, 0, 6] },
|
||||
{ text: 'Art. 10 data-quality record of conversation imports and their downstream frame counts.', fontSize: 10, color: '#333333', margin: [0, 0, 0, 10] },
|
||||
provenanceTable(report),
|
||||
|
||||
{ text: 'Summary', style: 'h1', margin: [0, 12, 0, 6] },
|
||||
{
|
||||
ul: [
|
||||
`Total interactions logged: ${report.interactionCount.toLocaleString('en-US')}`,
|
||||
`Models in inventory: ${report.modelInventory.length}`,
|
||||
`Oversight events: ${report.humanOversightLog.length}`,
|
||||
`Harvest sources: ${report.harvestProvenance.length}`,
|
||||
],
|
||||
fontSize: 10, color: '#333333', margin: [0, 0, 0, 20],
|
||||
},
|
||||
|
||||
{ canvas: [{ type: 'line', x1: 0, y1: 0, x2: 500, y2: 0, lineWidth: 0.5, lineColor: '#CCCCCC' }], margin: [0, 12, 0, 6] },
|
||||
{
|
||||
text: `Report v${report.report.version} — generated by ${report.report.generatedBy}.`,
|
||||
fontSize: 8, color: '#95A5A6', alignment: 'center',
|
||||
},
|
||||
];
|
||||
|
||||
return {
|
||||
info: {
|
||||
title: `Waggle AI Act Compliance Audit — ${wsName}`,
|
||||
author: 'Waggle OS',
|
||||
creator: 'Waggle OS Compliance Module',
|
||||
subject: `AI Act compliance audit for period ${report.report.period.from} → ${report.report.period.to}`,
|
||||
},
|
||||
pageSize: 'A4',
|
||||
pageMargins: [50, 60, 50, 60],
|
||||
header: (currentPage: number) => currentPage > 1 ? {
|
||||
columns: [
|
||||
{ text: 'Waggle — AI Act Compliance Audit', fontSize: 8, color: '#95A5A6', margin: [50, 20, 0, 0] },
|
||||
{ text: wsName, alignment: 'right', fontSize: 8, color: '#95A5A6', margin: [0, 20, 50, 0] },
|
||||
],
|
||||
} : undefined,
|
||||
footer: (currentPage: number, pageCount: number) => ({
|
||||
columns: [
|
||||
{
|
||||
text: footerExtra
|
||||
? `Generated ${report.report.generatedAt.slice(0, 10)} · ${footerExtra}`
|
||||
: `Generated ${report.report.generatedAt.slice(0, 10)}`,
|
||||
fontSize: 8, color: '#95A5A6', margin: [50, 0, 0, 0],
|
||||
},
|
||||
{ text: `${currentPage} / ${pageCount}`, alignment: 'right', fontSize: 8, color: '#95A5A6', margin: [0, 0, 50, 0] },
|
||||
],
|
||||
}),
|
||||
content,
|
||||
styles: {
|
||||
titleKicker: { fontSize: 11, color: HIVE_HONEY, bold: true, characterSpacing: 2 },
|
||||
title: { fontSize: 30, bold: true, color: HIVE_DARK },
|
||||
metaLabel: { fontSize: 8, color: '#95A5A6', characterSpacing: 1 },
|
||||
metaValue: { fontSize: 14, bold: true, color: HIVE_DARK },
|
||||
h1: { fontSize: 16, bold: true, color: HIVE_HONEY },
|
||||
tableHead: { fontSize: 9, bold: true, color: HIVE_DARK, fillColor: '#FAFAFA' },
|
||||
},
|
||||
defaultStyle: { fontSize: 10, lineHeight: 1.35, color: '#333333' },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Render an AuditReport to a PDF Buffer.
|
||||
* Uses dynamic import so pdfmake is only loaded when PDF generation runs.
|
||||
*/
|
||||
export async function renderComplianceReportPdf(
|
||||
report: AuditReport,
|
||||
overrides?: PdfTemplateOverrides,
|
||||
): Promise<Buffer> {
|
||||
const docDef = buildComplianceDocDefinition(report, overrides);
|
||||
const pdfMakeModule = await import('pdfmake/build/pdfmake.js');
|
||||
const pdfMake = (pdfMakeModule.default ?? pdfMakeModule) as unknown as PdfMakeStatic;
|
||||
const printer = pdfMake.createPdf(docDef);
|
||||
return new Promise<Buffer>((resolve, reject) => {
|
||||
printer.getBuffer((buffer: Buffer) => {
|
||||
if (buffer) resolve(buffer);
|
||||
else reject(new Error('PDF generation returned empty buffer'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** Convenience: write the PDF to disk. Returns the absolute path. */
|
||||
export async function writeComplianceReportPdf(
|
||||
report: AuditReport,
|
||||
outputPath: string,
|
||||
overrides?: PdfTemplateOverrides,
|
||||
): Promise<string> {
|
||||
const buffer = await renderComplianceReportPdf(report, overrides);
|
||||
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
||||
fs.writeFileSync(outputPath, buffer);
|
||||
return path.resolve(outputPath);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ export interface WaggleConnector {
|
||||
*/
|
||||
readonly harvestAction?: { action: string; params?: Record<string, unknown> };
|
||||
/** Which substrate manages this connector */
|
||||
readonly substrate: 'waggle' | 'kvark';
|
||||
readonly substrate: 'waggle';
|
||||
/** CDN URL for SVG logo */
|
||||
readonly logoUrl?: string;
|
||||
/** Connector category */
|
||||
@@ -78,7 +78,7 @@ export abstract class BaseConnector implements WaggleConnector {
|
||||
abstract readonly service: string;
|
||||
abstract readonly authType: 'bearer' | 'oauth2' | 'api_key' | 'basic';
|
||||
abstract readonly actions: ConnectorAction[];
|
||||
abstract readonly substrate: 'waggle' | 'kvark';
|
||||
abstract readonly substrate: 'waggle';
|
||||
readonly logoUrl?: string;
|
||||
readonly category?: 'productivity' | 'development' | 'crm' | 'data' | 'communication' | 'storage' | 'integration';
|
||||
readonly setupGuide?: string;
|
||||
|
||||
@@ -382,11 +382,6 @@ export { CommandRegistry, AGENT_LOOP_REROUTE_PREFIX, type CommandDefinition, typ
|
||||
export { registerWorkflowCommands } from './commands/workflow-commands.js';
|
||||
export { registerMarketplaceCommands } from './commands/marketplace-commands.js';
|
||||
export { createCronTools, type TurnOrigin } from './cron-tools.js';
|
||||
export {
|
||||
createKvarkTools, parseSearchResults,
|
||||
type KvarkClientLike, type KvarkToolsDeps, type KvarkSearchResponseLike,
|
||||
type KvarkAskResponseLike, type KvarkStructuredResult, type KvarkFeedbackResponseLike, type KvarkActionResponseLike,
|
||||
} from './kvark-tools.js';
|
||||
export { PERSONAS, getPersona, listPersonas, composePersonaPrompt, setPersonaDataDir, type AgentPersona } from './personas.js';
|
||||
export {
|
||||
loadCustomPersonas, saveCustomPersona, deleteCustomPersona,
|
||||
@@ -470,12 +465,6 @@ export {
|
||||
type SkillForAudit, type AuditTask, type SkillAuditOptions, type SkillAuditHooks,
|
||||
type SkillAuditOutcome, type SkillAuditReport, type RewriteValidation,
|
||||
} from './skill-audit.js';
|
||||
export {
|
||||
buildComplianceDocDefinition,
|
||||
renderComplianceReportPdf,
|
||||
writeComplianceReportPdf,
|
||||
type PdfTemplateOverrides,
|
||||
} from './compliance-pdf.js';
|
||||
export {
|
||||
watchSkillDirectory,
|
||||
type SkillWatcherOptions, type SkillWatcherHandle,
|
||||
|
||||
@@ -1,311 +0,0 @@
|
||||
/**
|
||||
* KVARK Agent Tools — kvark_search + kvark_ask_document.
|
||||
*
|
||||
* These tools are the agent-facing interface to KVARK's retrieval pipeline.
|
||||
* All KVARK calls go through KvarkClient (from @waggle/server/kvark).
|
||||
* Tools are only registered when KVARK is configured (Business/Enterprise tiers).
|
||||
* Solo/Team users never see these tools.
|
||||
*
|
||||
* Design rules:
|
||||
* - Tools use KvarkClient only — no direct fetch
|
||||
* - Output is formatted text for the agent, not raw DTOs
|
||||
* - Attribution structure preserved for Milestone B combined retrieval
|
||||
* - Graceful degradation on all error paths
|
||||
*/
|
||||
|
||||
import type { ToolDefinition } from './tools.js';
|
||||
|
||||
// ── Types from KVARK client (imported by interface, not package dep) ─────
|
||||
|
||||
/** Minimal KvarkClient interface — matches KvarkClient from @waggle/server/kvark */
|
||||
export interface KvarkClientLike {
|
||||
search(query: string, opts?: { limit?: number; offset?: number }): Promise<KvarkSearchResponseLike>;
|
||||
askDocument(documentId: string, question: string): Promise<KvarkAskResponseLike>;
|
||||
feedback?(documentId: number, query: string, useful: boolean, reason?: string): Promise<KvarkFeedbackResponseLike>;
|
||||
action?(actionType: string, target: { entityType: string; entityId: string }, payload: Record<string, unknown>, reason: string, approvalReference?: string, workspaceId?: string): Promise<KvarkActionResponseLike>;
|
||||
}
|
||||
|
||||
export interface KvarkFeedbackResponseLike {
|
||||
ok: boolean;
|
||||
}
|
||||
|
||||
export interface KvarkActionResponseLike {
|
||||
ok: boolean;
|
||||
data: {
|
||||
status: 'executed' | 'denied' | 'queued';
|
||||
actionId?: string;
|
||||
auditRef?: string;
|
||||
result?: Record<string, unknown>;
|
||||
} | null;
|
||||
error: { code: string; message: string } | null;
|
||||
}
|
||||
|
||||
export interface KvarkSearchResponseLike {
|
||||
results: KvarkSearchResultLike[];
|
||||
total: number;
|
||||
query: string;
|
||||
}
|
||||
|
||||
export interface KvarkSearchResultLike {
|
||||
document_id: number;
|
||||
title: string;
|
||||
snippet: string;
|
||||
score: number;
|
||||
document_type: string | null;
|
||||
}
|
||||
|
||||
export interface KvarkAskResponseLike {
|
||||
answer: string;
|
||||
sources: string[];
|
||||
}
|
||||
|
||||
// ── Structured result for Milestone B consumption ────────────────────────
|
||||
|
||||
/** Structured KVARK search result — used by combined retrieval (Milestone B) */
|
||||
export interface KvarkStructuredResult {
|
||||
content: string;
|
||||
documentId: number;
|
||||
title: string;
|
||||
score: number;
|
||||
documentType: string | null;
|
||||
/** Attribution label for agent responses, e.g. "[KVARK: pdf / Project Status]" */
|
||||
attribution: string;
|
||||
}
|
||||
|
||||
/** Parses KvarkClient search response into structured results */
|
||||
export function parseSearchResults(response: KvarkSearchResponseLike): KvarkStructuredResult[] {
|
||||
return response.results.map(r => ({
|
||||
content: r.snippet,
|
||||
documentId: r.document_id,
|
||||
title: r.title,
|
||||
score: r.score,
|
||||
documentType: r.document_type,
|
||||
attribution: formatKvarkAttribution(r),
|
||||
}));
|
||||
}
|
||||
|
||||
/** Format a KVARK result into an attribution label */
|
||||
function formatKvarkAttribution(result: KvarkSearchResultLike): string {
|
||||
const parts = ['KVARK'];
|
||||
if (result.document_type) parts.push(result.document_type);
|
||||
parts.push(result.title);
|
||||
return `[${parts.join(': ')}]`;
|
||||
// Future: add connector, page when KVARK exposes them
|
||||
// e.g. [KVARK: SharePoint / Project Status.pdf p.3]
|
||||
}
|
||||
|
||||
// ── Tool output formatting ───────────────────────────────────────────────
|
||||
|
||||
function formatSearchOutput(response: KvarkSearchResponseLike, query: string): string {
|
||||
if (response.results.length === 0) {
|
||||
return `KVARK Search: "${query}" — no results found.`;
|
||||
}
|
||||
|
||||
const header = `KVARK Search: "${query}" (${response.results.length} of ${response.total} results)\n`;
|
||||
const items = response.results.map((r, i) => {
|
||||
const typeLabel = r.document_type ? `[${r.document_type}]` : '[document]';
|
||||
const snippet = r.snippet.length > 200 ? r.snippet.slice(0, 197) + '...' : r.snippet;
|
||||
return `${i + 1}. ${typeLabel} ${r.title} (score: ${r.score.toFixed(2)})\n "${snippet}"\n ID: ${r.document_id}`;
|
||||
});
|
||||
|
||||
return header + items.join('\n\n');
|
||||
}
|
||||
|
||||
function formatAskOutput(response: KvarkAskResponseLike, documentId: string): string {
|
||||
const sources = response.sources.length > 0
|
||||
? `\n\nSources: ${response.sources.join(', ')}`
|
||||
: '';
|
||||
return `KVARK Document Answer (doc #${documentId}):\n\n${response.answer}${sources}`;
|
||||
}
|
||||
|
||||
// ── Tool factory ─────────────────────────────────────────────────────────
|
||||
|
||||
export interface KvarkToolsDeps {
|
||||
client: KvarkClientLike;
|
||||
}
|
||||
|
||||
export function createKvarkTools(deps: KvarkToolsDeps): ToolDefinition[] {
|
||||
const { client } = deps;
|
||||
|
||||
return [
|
||||
// ── kvark_search ─────────────────────────────────────────────
|
||||
{
|
||||
name: 'kvark_search',
|
||||
description:
|
||||
'Search enterprise knowledge base via KVARK. Returns documents from connected sources (SharePoint, Jira, Slack, etc.) with relevance scores and source attribution. Use when the user needs information from enterprise systems beyond workspace memory.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
query: { type: 'string', description: 'Search query' },
|
||||
limit: { type: 'number', description: 'Max results to return (default 10)' },
|
||||
},
|
||||
required: ['query'],
|
||||
},
|
||||
execute: async (args) => {
|
||||
const query = args.query as string;
|
||||
const limit = (args.limit as number) ?? 10;
|
||||
|
||||
try {
|
||||
const response = await client.search(query, { limit });
|
||||
return formatSearchOutput(response, query);
|
||||
} catch (err) {
|
||||
return handleKvarkError(err, 'search');
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// ── kvark_feedback ─────────────────────────────────────────────
|
||||
{
|
||||
name: 'kvark_feedback',
|
||||
description:
|
||||
'Send feedback to KVARK about whether an enterprise search result was useful. Use this ONLY when a KVARK result materially influenced your answer — not for every search. Helps KVARK improve future retrieval quality.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
document_id: { type: 'number', description: 'Document ID from kvark_search or search_memory KVARK results' },
|
||||
query: { type: 'string', description: 'The original search query that produced this result' },
|
||||
useful: { type: 'boolean', description: 'Whether the result was useful for answering the user' },
|
||||
reason: { type: 'string', description: 'Brief explanation of why the result was or was not useful (optional)' },
|
||||
},
|
||||
required: ['document_id', 'query', 'useful'],
|
||||
},
|
||||
execute: async (args) => {
|
||||
const documentId = args.document_id as number;
|
||||
const query = args.query as string;
|
||||
const useful = args.useful as boolean;
|
||||
const reason = args.reason as string | undefined;
|
||||
|
||||
if (!client.feedback) {
|
||||
return 'Feedback not supported by this KVARK instance.';
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await client.feedback(documentId, query, useful, reason);
|
||||
return response.ok
|
||||
? `Feedback recorded: ${useful ? 'useful' : 'not useful'} for document #${documentId}.`
|
||||
: 'Feedback was sent but KVARK did not confirm storage.';
|
||||
} catch (err) {
|
||||
return handleKvarkError(err, 'feedback');
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// ── kvark_action ──────────────────────────────────────────────
|
||||
{
|
||||
name: 'kvark_action',
|
||||
description:
|
||||
'Execute a governed enterprise action through KVARK (e.g., create a Jira ticket, post a Slack message, update a record). Requires user approval before execution. Use only when the user has explicitly asked for an action that touches enterprise systems.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
action_type: { type: 'string', description: 'Action identifier (e.g., "jira.create_comment", "slack.post_message", "sharepoint.update_file")' },
|
||||
entity_type: { type: 'string', description: 'Target entity type (e.g., "issue", "channel", "document")' },
|
||||
entity_id: { type: 'string', description: 'Target entity ID (e.g., "PROJ-142", "#general", "doc_123")' },
|
||||
payload: { type: 'object', description: 'Action-specific data (e.g., { "comment": "..." } for a comment action)' },
|
||||
reason: { type: 'string', description: 'Why this action is being performed — shown in governance audit trail' },
|
||||
},
|
||||
required: ['action_type', 'entity_type', 'entity_id', 'payload', 'reason'],
|
||||
},
|
||||
execute: async (args) => {
|
||||
const actionType = args.action_type as string;
|
||||
const entityType = args.entity_type as string;
|
||||
const entityId = args.entity_id as string;
|
||||
const payload = (args.payload as Record<string, unknown>) ?? {};
|
||||
const reason = args.reason as string;
|
||||
|
||||
if (!client.action) {
|
||||
return 'Governed actions are not supported by this KVARK instance.';
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await client.action(
|
||||
actionType,
|
||||
{ entityType, entityId },
|
||||
payload,
|
||||
reason,
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errMsg = response.error?.message ?? 'Action was denied by KVARK governance policy.';
|
||||
return `Action denied: ${errMsg}`;
|
||||
}
|
||||
|
||||
const data = response.data!;
|
||||
const parts = [`Action executed: ${actionType} on ${entityType}/${entityId}`];
|
||||
if (data.actionId) parts.push(`Action ID: ${data.actionId}`);
|
||||
if (data.auditRef) parts.push(`Audit reference: ${data.auditRef}`);
|
||||
if (data.status === 'queued') parts.push('Status: queued for execution');
|
||||
return parts.join('\n');
|
||||
} catch (err) {
|
||||
return handleKvarkError(err, 'action');
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// ── kvark_ask_document ───────────────────────────────────────
|
||||
{
|
||||
name: 'kvark_ask_document',
|
||||
description:
|
||||
'Ask a focused question about a specific enterprise document. Returns an answer with source references. Use after kvark_search identifies a relevant document.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
document_id: { type: 'string', description: 'Document ID from kvark_search results' },
|
||||
question: { type: 'string', description: 'Question about the document' },
|
||||
},
|
||||
required: ['document_id', 'question'],
|
||||
},
|
||||
execute: async (args) => {
|
||||
const documentId = args.document_id as string;
|
||||
const question = args.question as string;
|
||||
|
||||
try {
|
||||
const response = await client.askDocument(documentId, question);
|
||||
return formatAskOutput(response, documentId);
|
||||
} catch (err) {
|
||||
return handleKvarkError(err, 'ask');
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// ── Error handling ───────────────────────────────────────────────────────
|
||||
|
||||
function handleKvarkError(err: unknown, operation: 'search' | 'ask' | 'feedback' | 'action'): string {
|
||||
if (!(err instanceof Error)) {
|
||||
return 'KVARK encountered an unexpected error. Try again or use workspace memory.';
|
||||
}
|
||||
|
||||
switch (err.name) {
|
||||
case 'KvarkUnavailableError':
|
||||
return 'KVARK is not reachable. Using workspace memory only. Configure KVARK connection in Settings.';
|
||||
|
||||
case 'KvarkAuthError':
|
||||
return 'KVARK authentication failed. Check KVARK credentials in Settings.';
|
||||
|
||||
case 'KvarkNotImplementedError':
|
||||
if (operation === 'ask') {
|
||||
return 'Document Q&A is not yet available on this KVARK instance. Use kvark_search to find relevant documents instead.';
|
||||
}
|
||||
if (operation === 'feedback') {
|
||||
return 'Feedback is not yet available on this KVARK instance. This is non-blocking — continue normally.';
|
||||
}
|
||||
if (operation === 'action') {
|
||||
return 'Governed actions are not yet available on this KVARK instance. The action was not executed.';
|
||||
}
|
||||
return 'This KVARK feature is not yet available.';
|
||||
|
||||
case 'KvarkNotFoundError':
|
||||
if (operation === 'ask') {
|
||||
return 'Document not found. The document may have been removed or the ID may be incorrect.';
|
||||
}
|
||||
return 'The requested resource was not found in KVARK.';
|
||||
|
||||
case 'KvarkServerError':
|
||||
return `KVARK server error: ${err.message}. Try again in a moment.`;
|
||||
|
||||
default:
|
||||
return `KVARK error: ${err.message}`;
|
||||
}
|
||||
}
|
||||
@@ -42,24 +42,6 @@ export function formatCombinedResult(result: CombinedRetrievalResult, hasWorkspa
|
||||
}
|
||||
}
|
||||
|
||||
// KVARK results
|
||||
if (result.kvarkResults.length > 0) {
|
||||
const header = '## Enterprise Knowledge (KVARK)';
|
||||
// KVARK results: score only, no frame_type/importance
|
||||
const entries = result.kvarkResults.map(r => formatResultEntry(r, false)).join('\n');
|
||||
sections.push(`${header}\n${entries}`);
|
||||
}
|
||||
|
||||
// Source conflict notice
|
||||
if (result.hasConflict && result.conflictNote) {
|
||||
sections.push(`## Source Conflict\n${result.conflictNote}\nReview both sources carefully and note which is more recent or authoritative.`);
|
||||
}
|
||||
|
||||
// KVARK error notice
|
||||
if (result.kvarkError && result.kvarkAvailable) {
|
||||
sections.push(`> Enterprise search encountered an error: ${result.kvarkError}\n> Results shown are from local memory only.`);
|
||||
}
|
||||
|
||||
if (sections.length === 0) {
|
||||
return 'No relevant memories found.';
|
||||
}
|
||||
|
||||
@@ -57,13 +57,10 @@ export interface SkillToolsDeps {
|
||||
/**
|
||||
* Skills 2.0 gap E: promotion infrastructure. Each callback is optional.
|
||||
* - getWorkspaceId / getTeamId resolve scope dirs dynamically at tool-call time.
|
||||
* - hasTeamSkillLibrary / isEnterprise gate tier-restricted scopes.
|
||||
* - improvementSignals records skill_promotion signals on successful moves.
|
||||
*/
|
||||
getWorkspaceId?: () => string | null;
|
||||
getTeamId?: () => string | null;
|
||||
hasTeamSkillLibrary?: () => boolean;
|
||||
isEnterprise?: () => boolean;
|
||||
improvementSignals?: ImprovementSignalStore;
|
||||
/**
|
||||
* Skills 2.0 gap A: session-message accessor for auto-extracting skills
|
||||
@@ -668,14 +665,13 @@ Only use this after acquire_capability has identified a specific installable can
|
||||
},
|
||||
|
||||
// Skills 2.0 gap E: promote_skill — move a skill up one scope rung.
|
||||
// personal → workspace → team → enterprise. Enforces one-step-at-a-time,
|
||||
// tier gates, and records a skill_promotion improvement signal on success.
|
||||
// personal → workspace. Enforces one-step-at-a-time and records a
|
||||
// skill_promotion improvement signal on success.
|
||||
{
|
||||
name: 'promote_skill',
|
||||
description:
|
||||
'Promote an installed skill one scope up (personal → workspace → team → enterprise). '
|
||||
+ 'Team scope requires the teamSkillLibrary capability (TEAMS or ENTERPRISE tier). '
|
||||
+ 'Enterprise scope requires ENTERPRISE tier. '
|
||||
'Promote an installed skill one scope up (personal → workspace). '
|
||||
+ 'Team and enterprise scopes are not available in this edition. '
|
||||
+ 'The file is moved to the appropriate directory, frontmatter is rewritten '
|
||||
+ '(scope + promoted_from history), and a skill_promotion signal is recorded.',
|
||||
parameters: {
|
||||
@@ -746,20 +742,13 @@ Only use this after acquire_capability has identified a specific installable can
|
||||
);
|
||||
}
|
||||
|
||||
// 4. Tier + context gating
|
||||
// 4. Context gating. Team and enterprise skill libraries belong to
|
||||
// team mode, which this edition does not ship.
|
||||
if (target === 'workspace' && !workspaceId) {
|
||||
return `Cannot promote to workspace scope: no active workspace. Switch to a workspace first.`;
|
||||
}
|
||||
if (target === 'team') {
|
||||
if (!deps.hasTeamSkillLibrary?.()) {
|
||||
return `Cannot promote to team scope: your plan does not include the team skill library. Upgrade to TEAMS or ENTERPRISE.`;
|
||||
}
|
||||
if (!teamId) {
|
||||
return `Cannot promote to team scope: this workspace is not linked to a team.`;
|
||||
}
|
||||
}
|
||||
if (target === 'enterprise' && !deps.isEnterprise?.()) {
|
||||
return `Cannot promote to enterprise scope: requires ENTERPRISE tier. Contact sales for KVARK.`;
|
||||
if (target === 'team' || target === 'enterprise') {
|
||||
return `Cannot promote to ${target} scope: team and enterprise skill libraries are not available in this edition.`;
|
||||
}
|
||||
|
||||
// 5. Resolve destination dir + path
|
||||
@@ -818,8 +807,7 @@ Only use this after acquire_capability has identified a specific installable can
|
||||
+ `- Promotion history: ${updatedFrontmatter.promoted_from!.join(' → ')} → ${target}\n`
|
||||
+ `- Signal recorded: skill_promotion\n\n`
|
||||
+ `The skill is available immediately at the new scope. Agents in other `
|
||||
+ `workspaces${target === 'team' ? ' on this team' : target === 'enterprise' ? ' across the enterprise' : ''} `
|
||||
+ `will pick it up on their next skill reload.`
|
||||
+ `sessions will pick it up on their next skill reload.`
|
||||
);
|
||||
},
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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'));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user