refactor(server): move the install audit trail from core to the server (P1.2, UF-040)
Core keeps only shared infrastructure, so InstallAuditStore leaves it and lives in packages/server/src/local/install-audit.ts. The trail itself stays: the Audit and Activity tabs, the extend and export routes, and the threat model entry keep working on the same waggle.db table. - @waggle/shared gets RecordAuditInput and the narrow InstallAuditRecorder (record only), which the agent's skill tools now depend on. - The server creates the audit table before the one-time legacy copy, which no longer constructs it in core. - UF-040: GET /api/debug/logs called prepare() on the store's private db wrapper, which has none, so the support bundle never carried recent installs. It now reads them through getRecentSummary().
This commit is contained in:
@@ -10,8 +10,9 @@ import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import websocket from '@fastify/websocket';
|
||||
import { WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, InstallAuditStore, CronStore, AwarenessLayer, VaultStore, SkillHashStore, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore } from '@waggle/core';
|
||||
import { WaggleDB, importLegacyMindData, workspaceDataScope, type LegacyMindSource, WorkspaceManager, WaggleConfig, createEmbeddingProvider, type EmbeddingProviderConfig, type EmbeddingProviderInstance, CronStore, AwarenessLayer, VaultStore, SkillHashStore, TelemetryStore, TELEMETRY_EVENTS, ExecutionTraceStore } from '@waggle/core';
|
||||
import { corsOriginAllowed } from './cors-config.js';
|
||||
import { InstallAuditStore } from './install-audit.js';
|
||||
import { getStorageProvider } from './storage/index.js';
|
||||
import { isLoopbackBind, resolveBindHost } from './net-config.js';
|
||||
import { isLocalRequest } from './origin-guard.js';
|
||||
@@ -362,7 +363,7 @@ declare module 'fastify' {
|
||||
eventBus: EventEmitter;
|
||||
agentRunner?: AgentRunner;
|
||||
agentState: AgentState;
|
||||
auditStore: import('@waggle/core').InstallAuditStore;
|
||||
auditStore: InstallAuditStore;
|
||||
cronStore: import('@waggle/core').CronStore;
|
||||
waggleDb: import('@waggle/core').WaggleDB;
|
||||
/** Memory (ADR-0007): personal and workspace memories, reached only through the contract. */
|
||||
@@ -501,6 +502,11 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
// A workspace whose directory fails the path checks has nothing to copy.
|
||||
}
|
||||
}
|
||||
// Install audit store — persistent trail for capability install events. Its
|
||||
// table must exist before the legacy copy below, which fills it.
|
||||
const auditStore = new InstallAuditStore(waggleDb);
|
||||
server.decorate('auditStore', auditStore);
|
||||
|
||||
for (const report of importLegacyMindData(waggleDb, legacySources)) {
|
||||
if (report.status === 'imported') {
|
||||
log.info(`[waggle-db] copied legacy data for ${report.scope}: ${JSON.stringify(report.copied)}`);
|
||||
@@ -509,10 +515,6 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Install audit store — persistent trail for capability install events
|
||||
const auditStore = new InstallAuditStore(waggleDb);
|
||||
server.decorate('auditStore', auditStore);
|
||||
|
||||
// Cron store — SQLite-backed schedule persistence for Solo
|
||||
const cronStore = new CronStore(waggleDb);
|
||||
server.decorate('cronStore', cronStore);
|
||||
@@ -1933,13 +1935,10 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
};
|
||||
try { payload.health = await server.inject({ method: 'GET', url: '/health' }).then(r => JSON.parse(r.payload)); } catch { /* non-blocking */ }
|
||||
try {
|
||||
const raw = (server.auditStore as unknown as { db?: { prepare: (sql: string) => { all: () => unknown[] } } })?.db;
|
||||
if (raw) {
|
||||
// R2-006: expose only a coarse, non-recon column subset (no capability
|
||||
// names/versions/sources, risk_level, approval_class, initiator or detail)
|
||||
// and cap the rows — the support bundle needs activity shape, not an inventory.
|
||||
payload.auditRecent = raw.prepare('SELECT timestamp, action, capability_type FROM install_audit ORDER BY timestamp DESC LIMIT 50').all();
|
||||
}
|
||||
// R2-006: expose only a coarse, non-recon column subset (no capability
|
||||
// names/versions/sources, risk_level, approval_class, initiator or detail)
|
||||
// and cap the rows — the support bundle needs activity shape, not an inventory.
|
||||
payload.auditRecent = server.auditStore.getRecentSummary(50);
|
||||
} catch { /* non-blocking */ }
|
||||
// R2-006: vault key NAMES dropped — even names are recon material and the
|
||||
// support bundle does not need them.
|
||||
|
||||
208
packages/server/src/local/install-audit.ts
Normal file
208
packages/server/src/local/install-audit.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* Install Audit Store — persistent audit trail for capability install events.
|
||||
*
|
||||
* Records every install-relevant action (proposed, approved, installed, rejected,
|
||||
* failed) so there is a verifiable history of what was installed, when, why,
|
||||
* and by whom.
|
||||
*
|
||||
* Lives in waggle.db with Waggle's other own data (ADR-0007). The server owns
|
||||
* the store; core keeps only shared infrastructure (P1.2), and producers outside
|
||||
* the server write through InstallAuditRecorder from @waggle/shared.
|
||||
*/
|
||||
|
||||
import type { WaggleDB } from '@waggle/core';
|
||||
import {
|
||||
sqlInList, RISK_LEVELS, APPROVAL_CLASSES, AUDIT_ACTIONS,
|
||||
AUDIT_CAPABILITY_TYPES, AUDIT_INITIATORS, TRUST_SOURCES,
|
||||
} from '@waggle/shared';
|
||||
import type {
|
||||
AuditAction, AuditCapabilityType, AuditInitiator, ApprovalClass,
|
||||
InstallAuditRecorder, RecordAuditInput, RiskLevel, TrustSource,
|
||||
} from '@waggle/shared';
|
||||
|
||||
// ── Types ──────────────────────────────────────────────────────────────
|
||||
|
||||
export interface InstallAuditEntry {
|
||||
id: number;
|
||||
timestamp: string;
|
||||
capability_name: string;
|
||||
capability_type: AuditCapabilityType;
|
||||
source: string;
|
||||
version: string | null;
|
||||
risk_level: RiskLevel;
|
||||
trust_source: TrustSource;
|
||||
approval_class: ApprovalClass;
|
||||
action: AuditAction;
|
||||
initiator: AuditInitiator;
|
||||
detail: string;
|
||||
}
|
||||
|
||||
/** The coarse columns the support bundle may carry (R2-006). */
|
||||
export type InstallAuditSummary = Pick<InstallAuditEntry, 'timestamp' | 'action' | 'capability_type'>;
|
||||
|
||||
// ── Table DDL ──────────────────────────────────────────────────────────
|
||||
|
||||
// P7/D15 A3: the CHECK lists are generated from the canonical @waggle/shared
|
||||
// arrays via sqlInList, so the SQLite constraint and the TS union can no longer
|
||||
// drift (divergence #14 — the old comment admitted "drift silently crashes
|
||||
// record()").
|
||||
// P7/D15 #15: trust_source now also carries a CHECK (was unconstrained at the DB
|
||||
// while the TS type claimed a closed set). Every historical value came from the
|
||||
// typed AuditTrustSource (the pre-security-gate 6-set ⊂ the current 7-set), so
|
||||
// the rebuild migration's row copy can never violate it.
|
||||
export const INSTALL_AUDIT_TABLE_SQL = `
|
||||
CREATE TABLE IF NOT EXISTS install_audit (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
capability_name TEXT NOT NULL,
|
||||
capability_type TEXT NOT NULL CHECK (capability_type IN (${sqlInList(AUDIT_CAPABILITY_TYPES)})),
|
||||
source TEXT NOT NULL,
|
||||
version TEXT,
|
||||
risk_level TEXT NOT NULL CHECK (risk_level IN (${sqlInList(RISK_LEVELS)})),
|
||||
trust_source TEXT NOT NULL CHECK (trust_source IN (${sqlInList(TRUST_SOURCES)})),
|
||||
approval_class TEXT NOT NULL CHECK (approval_class IN (${sqlInList(APPROVAL_CLASSES)})),
|
||||
action TEXT NOT NULL CHECK (action IN (${sqlInList(AUDIT_ACTIONS)})),
|
||||
initiator TEXT NOT NULL CHECK (initiator IN (${sqlInList(AUDIT_INITIATORS)})),
|
||||
detail TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_capability ON install_audit (capability_name, action);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_timestamp ON install_audit (timestamp DESC);
|
||||
`;
|
||||
|
||||
// ── Store ──────────────────────────────────────────────────────────────
|
||||
|
||||
export class InstallAuditStore implements InstallAuditRecorder {
|
||||
private db: WaggleDB;
|
||||
|
||||
constructor(db: WaggleDB) {
|
||||
this.db = db;
|
||||
this.ensureTable();
|
||||
}
|
||||
|
||||
private ensureTable(): void {
|
||||
const raw = this.db.getDatabase();
|
||||
const existing = raw.prepare(
|
||||
"SELECT sql FROM sqlite_master WHERE type='table' AND name='install_audit'",
|
||||
).get() as { sql: string } | undefined;
|
||||
if (!existing) {
|
||||
raw.exec(INSTALL_AUDIT_TABLE_SQL);
|
||||
return;
|
||||
}
|
||||
// P5/D4 migration: pre-'uninstalled' installs carry a narrower action CHECK
|
||||
// baked into the table DDL. SQLite can't ALTER a CHECK, so rebuild the table
|
||||
// when the stored DDL lacks the new value. Idempotent — a no-op once migrated.
|
||||
// #15: also rebuild when the stored DDL has no trust_source CHECK (the column
|
||||
// was previously unconstrained). "CHECK (trust_source IN" is a safe sentinel —
|
||||
// it appears nowhere else in this DDL.
|
||||
const needsActionWiden = !existing.sql.includes("'uninstalled'");
|
||||
const needsTrustSourceCheck = !existing.sql.includes('CHECK (trust_source IN');
|
||||
if (needsActionWiden || needsTrustSourceCheck) {
|
||||
this.rebuildForWidenedActionCheck(raw);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebuild install_audit with the widened `action` CHECK, preserving all rows.
|
||||
* Classic SQLite 12-step table redefinition, wrapped in a transaction so a
|
||||
* crash mid-rebuild leaves the original table intact.
|
||||
*/
|
||||
private rebuildForWidenedActionCheck(raw: ReturnType<WaggleDB['getDatabase']>): void {
|
||||
const migrate = raw.transaction(() => {
|
||||
raw.exec('ALTER TABLE install_audit RENAME TO install_audit_legacy');
|
||||
// SQLite carries indexes along with RENAME (still named idx_audit_*), so
|
||||
// INSTALL_AUDIT_TABLE_SQL's CREATE INDEX IF NOT EXISTS would no-op and the
|
||||
// DROP below would take the indexes with the legacy table. Drop them first
|
||||
// so they get recreated on the new table.
|
||||
raw.exec('DROP INDEX IF EXISTS idx_audit_capability');
|
||||
raw.exec('DROP INDEX IF EXISTS idx_audit_timestamp');
|
||||
raw.exec(INSTALL_AUDIT_TABLE_SQL);
|
||||
raw.exec(`
|
||||
INSERT INTO install_audit (
|
||||
id, timestamp, capability_name, capability_type, source, version,
|
||||
risk_level, trust_source, approval_class, action, initiator, detail
|
||||
)
|
||||
SELECT id, timestamp, capability_name, capability_type, source, version,
|
||||
risk_level, trust_source, approval_class, action, initiator, detail
|
||||
FROM install_audit_legacy
|
||||
`);
|
||||
raw.exec('DROP TABLE install_audit_legacy');
|
||||
});
|
||||
migrate();
|
||||
}
|
||||
|
||||
/** Record an install audit event. */
|
||||
record(input: RecordAuditInput): InstallAuditEntry {
|
||||
const raw = this.db.getDatabase();
|
||||
|
||||
raw.prepare(`
|
||||
INSERT INTO install_audit (
|
||||
capability_name, capability_type, source, version,
|
||||
risk_level, trust_source, approval_class, action, initiator, detail
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
input.capabilityName,
|
||||
input.capabilityType,
|
||||
input.source,
|
||||
input.version ?? null,
|
||||
input.riskLevel,
|
||||
input.trustSource,
|
||||
input.approvalClass,
|
||||
input.action,
|
||||
input.initiator,
|
||||
input.detail ?? '',
|
||||
);
|
||||
|
||||
// Return the inserted row
|
||||
return raw.prepare(
|
||||
'SELECT * FROM install_audit ORDER BY id DESC LIMIT 1',
|
||||
).get() as InstallAuditEntry;
|
||||
}
|
||||
|
||||
/** Get audit history for a specific capability. */
|
||||
getByCapability(name: string): InstallAuditEntry[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT * FROM install_audit WHERE capability_name = ? ORDER BY id DESC',
|
||||
).all(name) as InstallAuditEntry[];
|
||||
}
|
||||
|
||||
/** Get audit history filtered by action type. */
|
||||
getByAction(action: AuditAction): InstallAuditEntry[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT * FROM install_audit WHERE action = ? ORDER BY id DESC',
|
||||
).all(action) as InstallAuditEntry[];
|
||||
}
|
||||
|
||||
/** Get recent audit entries (most recent first). */
|
||||
getRecent(limit: number = 20): InstallAuditEntry[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT * FROM install_audit ORDER BY id DESC LIMIT ?',
|
||||
).all(limit) as InstallAuditEntry[];
|
||||
}
|
||||
|
||||
/** Get recent entries for one capability type (most recent first) — backs
|
||||
* the shared Extend-layer audit read (GET /api/extend/audit?type=, C18). */
|
||||
getRecentByType(type: AuditCapabilityType, limit: number = 20): InstallAuditEntry[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT * FROM install_audit WHERE capability_type = ? ORDER BY id DESC LIMIT ?',
|
||||
).all(type, limit) as InstallAuditEntry[];
|
||||
}
|
||||
|
||||
/** Recent entries without names, versions, sources, risk or detail. */
|
||||
getRecentSummary(limit: number = 50): InstallAuditSummary[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT timestamp, action, capability_type FROM install_audit ORDER BY id DESC LIMIT ?',
|
||||
).all(limit) as InstallAuditSummary[];
|
||||
}
|
||||
|
||||
/** Get all entries (for testing). */
|
||||
getAll(): InstallAuditEntry[] {
|
||||
return this.db.getDatabase().prepare(
|
||||
'SELECT * FROM install_audit ORDER BY id ASC',
|
||||
).all() as InstallAuditEntry[];
|
||||
}
|
||||
|
||||
/** Clear all entries (for testing). */
|
||||
clear(): void {
|
||||
this.db.getDatabase().prepare('DELETE FROM install_audit').run();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { ConnectorHealth } from '@waggle/shared';
|
||||
import type { RecordAuditInput } from '@waggle/core';
|
||||
import type { ConnectorHealth, RecordAuditInput } from '@waggle/shared';
|
||||
import {
|
||||
JiraConnector, SalesforceConnector,
|
||||
getConnectorCredential, setConnectorCredential, setConnectorCredentialBundle,
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
*/
|
||||
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { EXTENSION_TYPES, type ExtensionType } from '@waggle/shared';
|
||||
import type { AuditCapabilityType, InstallAuditEntry } from '@waggle/core';
|
||||
import { EXTENSION_TYPES, type AuditCapabilityType, type ExtensionType } from '@waggle/shared';
|
||||
import type { InstallAuditEntry } from '../install-audit.js';
|
||||
import { authHeaders } from './validate.js';
|
||||
|
||||
/** ExtensionType → marketplace InstallationType, where representable (A5). */
|
||||
|
||||
@@ -30,9 +30,10 @@ import {
|
||||
MCP_CATALOG,
|
||||
type McpInstance,
|
||||
type McpServer,
|
||||
type RecordAuditInput,
|
||||
} from '@waggle/shared';
|
||||
import type { McpRuntime, McpServerState } from '@waggle/agent';
|
||||
import { scanForInjection, type RecordAuditInput } from '@waggle/core';
|
||||
import { scanForInjection } from '@waggle/core';
|
||||
import {
|
||||
MCP_SERVERS,
|
||||
MarketplaceInstaller,
|
||||
|
||||
Reference in New Issue
Block a user