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

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

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

View File

@@ -1,23 +0,0 @@
export { KvarkClient } from './kvark-client.js';
export { KvarkAuth } from './kvark-auth.js';
export { getKvarkConfig, type VaultLike } from './kvark-config.js';
export type {
KvarkClientConfig,
KvarkLoginRequest,
KvarkLoginResponse,
KvarkUser,
KvarkSearchResult,
KvarkSearchResponse,
KvarkAskRequest,
KvarkAskResponse,
KvarkChatEvent,
KvarkTokenUsage,
KvarkErrorResponse,
} from './kvark-types.js';
export {
KvarkAuthError,
KvarkNotFoundError,
KvarkNotImplementedError,
KvarkServerError,
KvarkUnavailableError,
} from './kvark-types.js';

View File

@@ -1,106 +0,0 @@
/**
* KVARK Auth — login, token caching, auto-refresh on 401.
*
* Manages the JWT lifecycle for KVARK API access:
* - Calls POST /api/auth/login to obtain a Bearer token
* - Caches the token in memory (re-login on server restart is fine)
* - Re-authenticates automatically on 401 responses
*/
import type { KvarkLoginResponse } from './kvark-types.js';
import { KvarkAuthError, KvarkUnavailableError } from './kvark-types.js';
export interface KvarkAuthConfig {
baseUrl: string;
identifier: string;
password: string;
timeoutMs?: number;
}
export class KvarkAuth {
private token: string | null = null;
private tokenObtainedAt: number = 0;
private readonly config: KvarkAuthConfig;
private readonly timeoutMs: number;
/** Injectable fetch for testing */
private readonly fetchFn: typeof globalThis.fetch;
constructor(config: KvarkAuthConfig, fetchFn?: typeof globalThis.fetch) {
this.config = config;
this.timeoutMs = config.timeoutMs ?? 30_000;
this.fetchFn = fetchFn ?? globalThis.fetch;
}
/**
* Get a valid Bearer token. Logs in if no token cached.
* Throws KvarkAuthError on auth failure, KvarkUnavailableError on network error.
*/
async getToken(): Promise<string> {
if (this.token) return this.token;
return this.login();
}
/**
* Force a fresh login (used after 401 responses).
*/
async login(): Promise<string> {
this.token = null;
let res: Response;
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
res = await this.fetchFn(`${this.config.baseUrl}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
identifier: this.config.identifier,
password: this.config.password,
}),
signal: controller.signal,
});
clearTimeout(timeout);
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') {
throw new KvarkUnavailableError('KVARK login timed out');
}
throw new KvarkUnavailableError(`KVARK unreachable: ${err instanceof Error ? err.message : String(err)}`);
}
if (!res.ok) {
const detail = await res.text().catch(() => 'Unknown error');
throw new KvarkAuthError(`KVARK login failed (${res.status}): ${detail}`);
}
const body = await res.json() as KvarkLoginResponse;
if (!body.success || !body.access_token) {
throw new KvarkAuthError(body.error ?? 'KVARK login returned no token');
}
this.token = body.access_token;
this.tokenObtainedAt = Date.now();
return this.token;
}
/**
* Clear cached token (called when a 401 is received, before retry).
*/
invalidate(): void {
this.token = null;
}
/** Whether a token is currently cached. */
get hasToken(): boolean {
return this.token !== null;
}
/** Milliseconds since token was obtained (0 if no token). */
get tokenAgeMs(): number {
if (!this.token) return 0;
return Date.now() - this.tokenObtainedAt;
}
}

View File

@@ -1,248 +0,0 @@
/**
* KvarkClient — the single boundary between Waggle and KVARK.
*
* All Waggle-side KVARK interaction flows through this client.
* Handles auth, request formatting, response normalization, and errors.
* Uses KvarkAuth for automatic token management.
*
* Design rules:
* - Waggle never calls KVARK endpoints directly — always through KvarkClient
* - Waggle never re-ranks KVARK results
* - Waggle never duplicates KVARK's retrieval logic
* - KVARK remains a black box from KvarkClient's perspective
*/
import { KvarkAuth } from './kvark-auth.js';
import type {
KvarkActionRequest,
KvarkActionResponse,
KvarkAskRequest,
KvarkAskResponse,
KvarkClientConfig,
KvarkFeedbackRequest,
KvarkFeedbackResponse,
KvarkSearchResponse,
KvarkUser,
} from './kvark-types.js';
import {
KvarkAuthError,
KvarkNotFoundError,
KvarkNotImplementedError,
KvarkServerError,
KvarkUnavailableError,
} from './kvark-types.js';
export class KvarkClient {
private readonly auth: KvarkAuth;
private readonly baseUrl: string;
private readonly timeoutMs: number;
private readonly retryOnServerError: boolean;
private readonly fetchFn: typeof globalThis.fetch;
constructor(config: KvarkClientConfig, fetchFn?: typeof globalThis.fetch) {
const fetch = fetchFn ?? globalThis.fetch;
this.auth = new KvarkAuth(
{ baseUrl: config.baseUrl, identifier: config.identifier, password: config.password, timeoutMs: config.timeoutMs },
fetch,
);
this.baseUrl = config.baseUrl.replace(/\/+$/, '');
this.timeoutMs = config.timeoutMs ?? 30_000;
this.retryOnServerError = config.retryOnServerError ?? true;
this.fetchFn = fetch;
}
// ── Public API ─────────────────────────────────────────────────────────
/**
* Search KVARK's document index.
* GET /api/search?q=...&limit=...&offset=...
*/
async search(query: string, opts?: { limit?: number; offset?: number }): Promise<KvarkSearchResponse> {
const params = new URLSearchParams({ q: query });
if (opts?.limit) params.set('limit', String(opts.limit));
if (opts?.offset) params.set('offset', String(opts.offset));
return this.get<KvarkSearchResponse>(`/api/search?${params}`);
}
/**
* Ask a focused question about a specific document.
* POST /api/chat/ask
* Note: This endpoint is currently stubbed (501) on KVARK — Waggle handles gracefully.
*/
async askDocument(documentId: string, question: string): Promise<KvarkAskResponse> {
const body: KvarkAskRequest = { document_id: documentId, question };
return this.post<KvarkAskResponse>('/api/chat/ask', body);
}
/**
* Send retrieval feedback to KVARK's reinforcement system.
* POST /api/feedback
* Fire-and-ack — Waggle should not treat this as a reasoning step.
*/
async feedback(documentId: number, query: string, useful: boolean, reason?: string): Promise<KvarkFeedbackResponse> {
const body: KvarkFeedbackRequest = {
feedbackType: 'search_result',
target: { documentId },
signal: {
rating: useful ? 'positive' : 'negative',
label: useful ? 'useful' : 'not_useful',
comment: reason,
},
context: { query },
};
return this.post<KvarkFeedbackResponse>('/api/feedback', body);
}
/**
* Execute a governed enterprise action via KVARK.
* POST /api/actions
* Requires user approval. KVARK enforces its own governance policy.
*/
async action(
actionType: string,
target: { entityType: string; entityId: string },
payload: Record<string, unknown>,
reason: string,
approvalReference?: string,
workspaceId?: string,
): Promise<KvarkActionResponse> {
const body: KvarkActionRequest = {
actionType,
target,
payload,
governance: { userApproved: true, approvalReference },
context: { workspaceId, reason },
};
return this.post<KvarkActionResponse>('/api/actions', body);
}
/**
* Verify KVARK connectivity and auth.
* GET /api/auth/me — returns current user if token is valid.
*/
async ping(): Promise<KvarkUser> {
return this.get<KvarkUser>('/api/auth/me');
}
// ── Internal HTTP methods ──────────────────────────────────────────────
private async get<T>(path: string): Promise<T> {
return this.request<T>('GET', path);
}
private async post<T>(path: string, body: unknown): Promise<T> {
return this.request<T>('POST', path, body);
}
private async request<T>(method: string, path: string, body?: unknown): Promise<T> {
const MAX_RETRIES = 3;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
const token = await this.auth.getToken();
const result = await this.doFetch<T>(method, path, token, body);
// On 401, re-auth once and retry
if (result.status === 401) {
this.auth.invalidate();
const freshToken = await this.auth.login();
const retry = await this.doFetch<T>(method, path, freshToken, body);
if (retry.status === 401) {
throw new KvarkAuthError('KVARK authentication failed after re-login');
}
return this.handleResponse<T>(retry);
}
// W5.3: On 429 rate-limit, wait with exponential backoff and retry
if (result.status === 429) {
if (attempt >= MAX_RETRIES) {
throw new KvarkServerError(`KVARK rate limit exceeded after ${MAX_RETRIES} retries`, 429);
}
const retryAfter = parseInt(result.error ?? '', 10) || 0;
const backoffMs = retryAfter > 0 ? retryAfter * 1000 : Math.min(1000 * Math.pow(2, attempt), 30_000);
await new Promise(resolve => setTimeout(resolve, backoffMs));
continue;
}
// W5.3: On transient 5xx (not 501 Not Implemented), retry with exponential backoff
if (result.status >= 500 && result.status !== 501 && this.retryOnServerError && attempt < MAX_RETRIES) {
const backoffMs = Math.min(1000 * Math.pow(2, attempt), 10_000);
await new Promise(resolve => setTimeout(resolve, backoffMs));
continue;
}
return this.handleResponse<T>(result);
}
// Should not reach here, but TypeScript needs it
throw new KvarkServerError('KVARK request failed after max retries', 500);
}
private async doFetch<T>(
method: string,
path: string,
token: string,
body?: unknown,
): Promise<{ status: number; data?: T; error?: string }> {
let res: Response;
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), this.timeoutMs);
const headers: Record<string, string> = {
'Authorization': `Bearer ${token}`,
'Accept': 'application/json',
};
if (body !== undefined) {
headers['Content-Type'] = 'application/json';
}
res = await this.fetchFn(`${this.baseUrl}${path}`, {
method,
headers,
body: body !== undefined ? JSON.stringify(body) : undefined,
signal: controller.signal,
});
clearTimeout(timeout);
} catch (err) {
if (err instanceof DOMException && err.name === 'AbortError') {
throw new KvarkUnavailableError(`KVARK request timed out: ${method} ${path}`);
}
throw new KvarkUnavailableError(`KVARK unreachable: ${err instanceof Error ? err.message : String(err)}`);
}
if (res.ok) {
const data = await res.json() as T;
return { status: res.status, data };
}
const errorText = await res.text().catch(() => 'Unknown error');
return { status: res.status, error: errorText };
}
private handleResponse<T>(result: { status: number; data?: T; error?: string }): T {
if (result.data !== undefined) return result.data;
const detail = result.error ?? 'Unknown error';
switch (result.status) {
case 401:
throw new KvarkAuthError(detail);
case 403:
// W5.4: Explicit 403 handling — governance/permission denial
throw new KvarkServerError(`KVARK access denied (forbidden): ${detail}`, 403);
case 404:
throw new KvarkNotFoundError(detail);
case 429:
// W5.3: Should be handled in request() retry loop, but catch here as fallback
throw new KvarkServerError(`KVARK rate limited: ${detail}`, 429);
case 501:
throw new KvarkNotImplementedError(detail);
default:
if (result.status >= 500) {
throw new KvarkServerError(detail, result.status);
}
throw new KvarkServerError(`KVARK error (${result.status}): ${detail}`, result.status);
}
}
}

View File

@@ -1,44 +0,0 @@
/**
* KVARK Config — reads KVARK connection details from the Waggle vault.
*
* Credentials stored in vault as 'kvark:connection' with JSON value:
* { "baseUrl": "http://localhost:8000", "identifier": "user@example.com", "password": "..." }
*/
import type { KvarkClientConfig } from './kvark-types.js';
/** Minimal vault interface — matches VaultStore.get() signature */
export interface VaultLike {
get(name: string): { value: string } | null;
}
/**
* Load KVARK client config from vault. Returns null if not configured.
*/
export function getKvarkConfig(vault: VaultLike): KvarkClientConfig | null {
const entry = vault.get('kvark:connection');
if (!entry) return null;
try {
const parsed = JSON.parse(entry.value) as Record<string, unknown>;
const baseUrl = parsed.baseUrl;
const identifier = parsed.identifier;
const password = parsed.password;
if (typeof baseUrl !== 'string' || typeof identifier !== 'string' || typeof password !== 'string') {
return null;
}
if (!baseUrl || !identifier || !password) {
return null;
}
return {
baseUrl,
identifier,
password,
timeoutMs: typeof parsed.timeoutMs === 'number' ? parsed.timeoutMs : undefined,
};
} catch {
return null;
}
}

View File

@@ -1,202 +0,0 @@
/**
* KVARK TypeScript types — verified from GitHub KVARK Pydantic DTOs.
*
* Source: github.com/UkisAI-Egzakta/KVARK backend/src/models/dto/
* These types are the contract between Waggle and KVARK.
* All KVARK interaction flows through KvarkClient using these types.
*/
// ── Auth ─────────────────────────────────────────────────────────────────
export interface KvarkLoginRequest {
identifier: string;
password: string;
}
export interface KvarkUser {
id: number;
identifier: string;
first_name: string | null;
last_name: string | null;
admin: boolean;
developer: boolean;
status: string | null;
created_at: string | null;
}
export interface KvarkLoginResponse {
success: boolean;
access_token: string | null;
token_type: string;
user: KvarkUser | null;
error: string | null;
}
// ── Search ───────────────────────────────────────────────────────────────
export interface KvarkSearchResult {
document_id: number;
title: string;
snippet: string;
score: number;
document_type: string | null;
// Future enrichment (KVARK-side):
// connector?: string;
// source_path?: string;
// page?: number;
// citation_label?: string;
}
export interface KvarkSearchResponse {
results: KvarkSearchResult[];
total: number;
query: string;
}
// ── Document Ask ─────────────────────────────────────────────────────────
export interface KvarkAskRequest {
document_id: string;
question: string;
}
export interface KvarkAskResponse {
answer: string;
sources: string[];
}
// ── Feedback ─────────────────────────────────────────────────────────────
export interface KvarkFeedbackRequest {
feedbackType: 'search_result';
target: {
documentId: number;
};
signal: {
rating: 'positive' | 'negative';
label: 'useful' | 'not_useful';
comment?: string;
};
context: {
query: string;
};
}
export interface KvarkFeedbackResponse {
ok: boolean;
data: {
stored: boolean;
feedbackId?: string;
};
error: string | null;
}
// ── Governed Actions ─────────────────────────────────────────────────────
export interface KvarkActionRequest {
actionType: string;
target: {
entityType: string;
entityId: string;
};
payload: Record<string, unknown>;
governance: {
userApproved: boolean;
approvalReference?: string;
};
context: {
workspaceId?: string;
reason: string;
};
}
export interface KvarkActionResponse {
ok: boolean;
data: {
status: 'executed' | 'denied' | 'queued';
actionId?: string;
auditRef?: string;
result?: Record<string, unknown>;
} | null;
error: {
code: string;
message: string;
details?: Record<string, unknown>;
} | null;
}
// ── Chat SSE Events ──────────────────────────────────────────────────────
export interface KvarkTokenUsage {
input_tokens: number;
output_tokens: number;
latency_ms: number;
}
export type KvarkChatEvent =
| { type: 'status'; msg: string }
| { type: 'token'; chunk: string }
| { type: 'tool_call'; name: string; args: Record<string, unknown> }
| { type: 'tool_result'; name: string; summary: string; duration_ms: number }
| { type: 'thought'; text: string }
| { type: 'done'; session_id: number; answer: string; usage?: KvarkTokenUsage }
| { type: 'error'; msg: string };
// ── Errors ───────────────────────────────────────────────────────────────
/** Standard FastAPI HTTPException shape */
export interface KvarkErrorResponse {
detail: string;
}
// ── Client Config ────────────────────────────────────────────────────────
export interface KvarkClientConfig {
/** KVARK API base URL, e.g. "http://localhost:8000" */
baseUrl: string;
/** Login identifier (username or email) */
identifier: string;
/** Login password */
password: string;
/** Request timeout in ms (default: 30000) */
timeoutMs?: number;
/** Retry once on 5xx errors (default: true) */
retryOnServerError?: boolean;
}
// ── Typed Errors ─────────────────────────────────────────────────────────
export class KvarkAuthError extends Error {
constructor(message: string) {
super(message);
this.name = 'KvarkAuthError';
}
}
export class KvarkNotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = 'KvarkNotFoundError';
}
}
export class KvarkNotImplementedError extends Error {
constructor(message: string) {
super(message);
this.name = 'KvarkNotImplementedError';
}
}
export class KvarkServerError extends Error {
constructor(message: string, public statusCode: number) {
super(message);
this.name = 'KvarkServerError';
}
}
export class KvarkUnavailableError extends Error {
constructor(message: string) {
super(message);
this.name = 'KvarkUnavailableError';
}
}

View File

@@ -10,7 +10,7 @@ import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify';
import cors from '@fastify/cors';
import fastifyStatic from '@fastify/static';
import websocket from '@fastify/websocket';
import { MindDB, MultiMind, MultiMindCache, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, FrameStore, SessionStore, SuppressionStore, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, OptimizationLogStore, ImprovementSignalStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TeamSync, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, EvolutionRunStore, ComplianceTemplateStore, harvestSetHash, type WorkspaceConfig } from '@waggle/core';
import { MindDB, MultiMind, MultiMindCache, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, FrameStore, SessionStore, SuppressionStore, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, OptimizationLogStore, ImprovementSignalStore, HarvestSourceStore, ClaudeCodeAdapter, reconcileIndexes, TeamSync, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore, EvolutionRunStore, harvestSetHash, type WorkspaceConfig } from '@waggle/core';
import { corsOriginAllowed } from './cors-config.js';
import { getBoundTeamServer } from './team-server-binding.js';
import { fetchTeamServer } from './team-server-egress.js';
@@ -172,7 +172,6 @@ import { identityRoutes } from './routes/identity.js';
import { agentRunRoutes } from './routes/agent-run.js';
import { agentRunsRoutes } from './routes/agent-runs.js';
import { localInferenceRoutes } from './routes/local-inference.js';
import { complianceRoutes } from './routes/compliance.js';
import { OfflineManager } from './offline-manager.js';
import { listOllamaChatModelIds } from './model-availability.js';
import { log, createLogger } from './logger.js';
@@ -528,17 +527,6 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
const auditStore = new InstallAuditStore(multiMind.personal);
server.decorate('auditStore', auditStore);
// M-06: seed the built-in KVARK enterprise template on first boot.
// Idempotent — subsequent boots see the row and no-op.
try {
const templateStore = new ComplianceTemplateStore(multiMind.personal);
templateStore.seedKvarkTemplateIfMissing();
} catch (err) {
// Non-fatal: users can create the template manually from the UI if the
// seed fails (disk full, schema drift, etc.).
server.log.warn({ err }, 'Failed to seed KVARK compliance template');
}
// Cron store — SQLite-backed schedule persistence for Solo
const cronStore = new CronStore(multiMind.personal);
server.decorate('cronStore', cronStore);
@@ -2890,7 +2878,6 @@ Return ONLY the improved system prompt text. No commentary, no markdown fences,
await server.register(agentRunRoutes);
await server.register(agentRunsRoutes);
await server.register(localInferenceRoutes);
await server.register(complianceRoutes);
// ── Static file serving for web mode ──────────────────────────
// When WAGGLE_FRONTEND_DIR is set, serve the React frontend as static files.

View File

@@ -1,312 +0,0 @@
/**
* Compliance Routes — AI Act compliance API endpoints.
*
* GET /api/compliance/status — get compliance status
* POST /api/compliance/export — generate audit report
* GET /api/compliance/interactions — list recent interactions
* POST /api/compliance/interactions — record an interaction (internal use)
*/
import type { FastifyInstance } from 'fastify';
import { z } from 'zod';
import {
InteractionStore, ComplianceStatusChecker, ReportGenerator,
HarvestSourceStore, ComplianceTemplateStore,
type RecordInteractionInput, type AuditReportRequest,
type CreateComplianceTemplateInput, type UpdateComplianceTemplateInput,
type AIActRiskLevel,
} from '@waggle/core';
import { renderComplianceReportPdf, type PdfTemplateOverrides } from '@waggle/agent';
/**
* M-03: the `/export` and `/export-pdf` routes accept three optional template-
* sourced overrides on the request body. They do not change section selection
* (which is already merged client-side into `include`) — they only affect how
* the PDF renders org name, footer text, and the risk-classification label.
*/
interface TemplateOverrideFields {
templateOrgName?: string | null;
templateFooterText?: string | null;
templateRiskClassification?: AIActRiskLevel | null;
}
function extractPdfOverrides(body: unknown): PdfTemplateOverrides | undefined {
if (!body || typeof body !== 'object') return undefined;
const b = body as TemplateOverrideFields;
if (b.templateOrgName == null && b.templateFooterText == null && b.templateRiskClassification == null) {
return undefined;
}
return {
orgName: b.templateOrgName ?? null,
footerText: b.templateFooterText ?? null,
riskClassification: b.templateRiskClassification ?? null,
};
}
const sectionsSchema = z.object({
interactions: z.boolean(),
oversight: z.boolean(),
models: z.boolean(),
provenance: z.boolean(),
riskAssessment: z.boolean(),
fria: z.boolean(),
});
const riskSchema = z.enum(['minimal', 'limited', 'high-risk', 'unacceptable']);
const createTemplateSchema = z.object({
name: z.string().min(1),
description: z.string().nullable().optional(),
sections: sectionsSchema,
riskClassification: riskSchema.nullable().optional(),
orgName: z.string().nullable().optional(),
footerText: z.string().nullable().optional(),
});
const updateTemplateSchema = z.object({
name: z.string().min(1).optional(),
description: z.string().nullable().optional(),
sections: sectionsSchema.optional(),
riskClassification: riskSchema.nullable().optional(),
orgName: z.string().nullable().optional(),
footerText: z.string().nullable().optional(),
});
export async function complianceRoutes(fastify: FastifyInstance) {
// GET /api/compliance/status — evaluate current compliance
fastify.get<{ Querystring: { workspaceId?: string } }>('/api/compliance/status', async (_request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const store = new InteractionStore(personalDb);
const checker = new ComplianceStatusChecker(store);
const workspaceId = _request.query?.workspaceId;
return checker.check(workspaceId || undefined);
});
// POST /api/compliance/export — generate audit report
fastify.post('/api/compliance/export', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const body = request.body as AuditReportRequest;
if (!body.from || !body.to) {
return reply.code(400).send({ error: 'from and to dates are required' });
}
const interactionStore = new InteractionStore(personalDb);
const harvestStore = new HarvestSourceStore(personalDb);
const wsManager = fastify.workspaceManager;
const generator = new ReportGenerator({
interactionStore,
harvestStore,
getWorkspaceName: (id) => wsManager?.get(id)?.name ?? id,
getWorkspaceRisk: (id) => wsManager?.get(id)?.riskLevel ?? 'minimal',
getWorkspaceRiskClassifiedAt: (id) => wsManager?.get(id)?.riskClassifiedAt ?? null,
});
const report = generator.generate({
workspaceId: body.workspaceId,
from: body.from,
to: body.to,
format: body.format ?? 'json',
include: body.include ?? {
interactions: true,
oversight: true,
models: true,
provenance: true,
riskAssessment: true,
fria: false,
},
});
return report;
});
// GET /api/compliance/interactions — list recent interactions
fastify.get('/api/compliance/interactions', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const store = new InteractionStore(personalDb);
const { limit, workspaceId } = request.query as { limit?: string; workspaceId?: string };
const parsedLimit = Math.min(Number(limit) || 20, 100);
if (workspaceId) {
return { interactions: store.getByWorkspace(workspaceId) };
}
return { interactions: store.getRecent(parsedLimit) };
});
// POST /api/compliance/interactions — record an AI interaction
fastify.post('/api/compliance/interactions', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const body = request.body as RecordInteractionInput;
if (!body.model || !body.provider) {
return reply.code(400).send({ error: 'model and provider are required' });
}
const store = new InteractionStore(personalDb);
const entry = store.record(body);
return entry;
});
// POST /api/compliance/export-pdf — M-02: renders the same AuditReport
// shape as /export through the compliance-pdf module and returns a PDF
// binary. Body accepts the full AuditReportRequest, identical to /export,
// so the UI can reuse its existing "which sections should we include"
// state. Returns application/pdf with a Content-Disposition hint so
// browsers trigger a Save dialog.
fastify.post('/api/compliance/export-pdf', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const body = request.body as AuditReportRequest;
if (!body.from || !body.to) {
return reply.code(400).send({ error: 'from and to dates are required' });
}
const interactionStore = new InteractionStore(personalDb);
const harvestStore = new HarvestSourceStore(personalDb);
const wsManager = fastify.workspaceManager;
const generator = new ReportGenerator({
interactionStore,
harvestStore,
getWorkspaceName: (id) => wsManager?.get(id)?.name ?? id,
getWorkspaceRisk: (id) => wsManager?.get(id)?.riskLevel ?? 'minimal',
getWorkspaceRiskClassifiedAt: (id) => wsManager?.get(id)?.riskClassifiedAt ?? null,
});
const report = generator.generate({
workspaceId: body.workspaceId,
from: body.from,
to: body.to,
format: body.format ?? 'json',
include: body.include ?? {
interactions: true,
oversight: true,
models: true,
provenance: true,
riskAssessment: true,
fria: false,
},
});
try {
const pdfBuffer = await renderComplianceReportPdf(report, extractPdfOverrides(body));
const filename = `ai-act-compliance-${body.from.slice(0, 10)}-to-${body.to.slice(0, 10)}.pdf`;
return reply
.header('Content-Type', 'application/pdf')
.header('Content-Disposition', `attachment; filename="${filename}"`)
.send(pdfBuffer);
} catch (err) {
return reply.code(500).send({
error: err instanceof Error ? err.message : 'PDF generation failed',
});
}
});
// GET /api/compliance/models — get model inventory
fastify.get('/api/compliance/models', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) {
return reply.code(503).send({ error: 'Personal mind not available' });
}
const store = new InteractionStore(personalDb);
const { from, to, workspaceId } = request.query as { from?: string; to?: string; workspaceId?: string };
return { models: store.getModelInventory(from, to, workspaceId) };
});
// ── M-03: Compliance templates CRUD ──
// Templates are stored on the personal mind (same DB as ai_interactions) so a
// single "My templates" list is visible across all workspaces. Sections merge
// with the runtime /export body's `include` flags (union semantics); the
// merge happens in the UI layer before the POST, not here, so the /export
// and /export-pdf routes stay template-agnostic.
fastify.get('/api/compliance/templates', async (_request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) return reply.code(503).send({ error: 'Personal mind not available' });
const store = new ComplianceTemplateStore(personalDb);
return { templates: store.list() };
});
fastify.get<{ Params: { id: string } }>(
'/api/compliance/templates/:id',
async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) return reply.code(503).send({ error: 'Personal mind not available' });
const id = Number(request.params.id);
if (!Number.isFinite(id)) return reply.code(400).send({ error: 'Invalid id' });
const store = new ComplianceTemplateStore(personalDb);
const template = store.getById(id);
if (!template) return reply.code(404).send({ error: 'Template not found' });
return { template };
},
);
fastify.post('/api/compliance/templates', async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) return reply.code(503).send({ error: 'Personal mind not available' });
const parsed = createTemplateSchema.safeParse(request.body);
if (!parsed.success) {
return reply.code(400).send({ error: 'Invalid template body', detail: parsed.error.issues });
}
const store = new ComplianceTemplateStore(personalDb);
try {
const template = store.create(parsed.data as CreateComplianceTemplateInput);
return reply.code(201).send({ template });
} catch (err) {
return reply.code(400).send({ error: err instanceof Error ? err.message : 'Create failed' });
}
});
fastify.patch<{ Params: { id: string } }>(
'/api/compliance/templates/:id',
async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) return reply.code(503).send({ error: 'Personal mind not available' });
const id = Number(request.params.id);
if (!Number.isFinite(id)) return reply.code(400).send({ error: 'Invalid id' });
const parsed = updateTemplateSchema.safeParse(request.body);
if (!parsed.success) {
return reply.code(400).send({ error: 'Invalid template patch', detail: parsed.error.issues });
}
const store = new ComplianceTemplateStore(personalDb);
try {
const template = store.update(id, parsed.data as UpdateComplianceTemplateInput);
if (!template) return reply.code(404).send({ error: 'Template not found' });
return { template };
} catch (err) {
return reply.code(400).send({ error: err instanceof Error ? err.message : 'Update failed' });
}
},
);
fastify.delete<{ Params: { id: string } }>(
'/api/compliance/templates/:id',
async (request, reply) => {
const personalDb = fastify.multiMind?.personal;
if (!personalDb) return reply.code(503).send({ error: 'Personal mind not available' });
const id = Number(request.params.id);
if (!Number.isFinite(id)) return reply.code(400).send({ error: 'Invalid id' });
const store = new ComplianceTemplateStore(personalDb);
const deleted = store.delete(id);
if (!deleted) return reply.code(404).send({ error: 'Template not found' });
return { deleted: true };
},
);
}

View File

@@ -12,11 +12,10 @@ import type { FastifyInstance, FastifyReply } from 'fastify';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { MarketplaceDB, MarketplaceInstaller, MarketplaceSync, SecurityGate, ENTERPRISE_PACKS, PACKAGE_CATEGORIES, recategorizeAll, isCiscoScannerAvailable, resolveSkillSource, SkillSourceError, createMarketplaceMcpProvenance } from '@waggle/marketplace';
import { MarketplaceDB, MarketplaceInstaller, MarketplaceSync, SecurityGate, PACKAGE_CATEGORIES, recategorizeAll, isCiscoScannerAvailable, resolveSkillSource, SkillSourceError, createMarketplaceMcpProvenance } from '@waggle/marketplace';
import type { InstallationType, SearchSort, ScanResult, MarketplacePackage, MarketplaceMcpProvenance, MarketplaceApprovalIdentity, FetchFn } from '@waggle/marketplace';
import { validateSkillMd } from '@waggle/sdk';
import { safeFetch, assertUrlAllowed, scanForInjection } from '@waggle/agent';
import { getKvarkConfig } from '../../kvark/kvark-config.js';
import { emitNotification } from './notifications.js';
import { loadMcpConfig, removeMcpServerEntry } from '../mcp-config.js';
import { enqueueHeldAction } from '../held-action-executor.js';
@@ -182,30 +181,6 @@ export async function marketplaceRoutes(fastify: FastifyInstance) {
return result;
});
// ── GET /api/marketplace/enterprise-packs ─────────────────────────────
// Returns enterprise packs that require KVARK. Only populated when
// KVARK credentials are configured in the vault.
fastify.get('/api/marketplace/enterprise-packs', async (request, reply) => {
const vault = fastify.vault;
const kvarkConfigured = vault ? getKvarkConfig(vault) !== null : false;
if (!kvarkConfigured) {
return {
packs: [],
total: 0,
kvarkRequired: true,
hint: 'Enterprise packs require a KVARK connection. Configure KVARK credentials in the vault to unlock.',
};
}
return {
packs: ENTERPRISE_PACKS,
total: ENTERPRISE_PACKS.length,
kvarkRequired: false,
};
});
// ── POST /api/marketplace/install ───────────────────────────────────
// Install a package from the marketplace with SecurityGate pre-scan.
// Severity-based gating:

View File

@@ -319,8 +319,6 @@ const UNSAFE_WORKSPACE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
*/
const VIEWER_READ_ONLY_POST_EXEMPT_PATHS = new Set([
'/api/export',
'/api/compliance/export',
'/api/compliance/export-pdf',
'/api/automations/test',
'/api/command/interpret',
]);