Add operations controls and SLO drills

This commit is contained in:
ZacharyZhang-NY committed 2026-05-23 18:26:56 -04:00
1 parent 7116ae2a7b
commit bc14a2fbcb
23 files changed
+873 -5

No files matched your search

@@ -16,6 +16,7 @@ import { OnboardingService } from "../services/onboarding-service";
import { ComplianceService } from "../services/compliance-service";
import { SearchService } from "../services/search-service";
import { IdentityAccessService } from "../services/identity-access-service";
import { OperationsService } from "../services/operations-service";
export interface IpcDeps {
connection: DatabaseConnection;
@@ -36,6 +37,7 @@ export function registerIpcHandlers(deps: IpcDeps): void {
const compliance = new ComplianceService(deps.connection.db);
const search = new SearchService(deps.connection.db);
const identityAccess = new IdentityAccessService(deps.connection.db);
const operations = new OperationsService(deps.connection.db);
const onboarding = new OnboardingService({
db: deps.connection.db,
saveProviderCredential: (input) => providers.saveCredential(input),
@@ -153,6 +155,8 @@ export function registerIpcHandlers(deps: IpcDeps): void {
});
ipcMain.handle(IPC_CHANNELS.createMigrationReport, (_event, input) => mutate(() => migrations.createReport(input)));
ipcMain.handle(IPC_CHANNELS.setFeatureFlag, (_event, input) => mutate(() => operations.setFeatureFlag(input)));
ipcMain.handle(IPC_CHANNELS.runChaosDrill, (_event, input) => mutate(() => operations.runChaosDrill(input)));
ipcMain.handle(IPC_CHANNELS.createComplianceExport, (_event, input) => mutate(() => compliance.createExport(input)));
ipcMain.handle(IPC_CHANNELS.createLegalHold, (_event, input) => mutate(() => compliance.createLegalHold(input)));
ipcMain.handle(IPC_CHANNELS.setLegalHoldState, (_event, input) => mutate(() => compliance.setLegalHoldState(input)));
@@ -0,0 +1,109 @@
export const operationsControlsSchema = `
create table if not exists feature_flags (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
key text not null,
label text not null,
enabled integer not null check(enabled in (0, 1)),
created_at text not null,
updated_at text not null,
unique(org_id, key)
);
create table if not exists slo_targets (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
signal_key text not null,
label text not null,
target_basis_points integer not null,
window_minutes integer not null,
created_at text not null,
updated_at text not null,
unique(org_id, signal_key)
);
create table if not exists operational_events (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
signal_key text not null,
source text not null,
success integer not null check(success in (0, 1)),
duration_ms integer,
detail_json text not null,
created_at text not null
);
create table if not exists chaos_drills (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
key text not null,
label text not null,
scenario_key text not null,
enabled integer not null check(enabled in (0, 1)),
created_at text not null,
updated_at text not null,
unique(org_id, key)
);
create table if not exists chaos_drill_runs (
id text primary key,
org_id text not null references organizations(id) on delete cascade,
drill_id text not null references chaos_drills(id) on delete cascade,
state text not null,
result_json text not null,
started_at text not null,
completed_at text not null
);
create index if not exists idx_operational_events_org_signal_created
on operational_events(org_id, signal_key, created_at);
create index if not exists idx_chaos_drill_runs_org_drill_started
on chaos_drill_runs(org_id, drill_id, started_at);
insert into feature_flags(id, org_id, key, label, enabled, created_at, updated_at)
select 'ffg_' || lower(hex(randomblob(16))), organizations.id, seed.key, seed.label, seed.enabled, datetime('now'), datetime('now')
from organizations
cross join (
select 'provider_background_execution' as key, 'Provider background execution' as label, 1 as enabled
union all select 'social_publish_approval', 'Social publish approval', 1
union all select 'board_intervention', 'Board intervention', 1
union all select 'chaos_drills', 'Chaos drills', 1
) seed
where not exists (
select 1 from feature_flags
where feature_flags.org_id = organizations.id and feature_flags.key = seed.key
);
insert into slo_targets(id, org_id, signal_key, label, target_basis_points, window_minutes, created_at, updated_at)
select 'slo_' || lower(hex(randomblob(16))), organizations.id, seed.signal_key, seed.label, seed.target_basis_points, seed.window_minutes, datetime('now'), datetime('now')
from organizations
cross join (
select 'provider_request_success' as signal_key, 'Provider requests' as label, 9950 as target_basis_points, 43200 as window_minutes
union all select 'approval_route_coverage', 'Approval routing', 10000, 43200
union all select 'browser_session_boundary', 'Browser session boundary', 10000, 43200
union all select 'skill_resolve_success', 'Skill resolve', 9950, 43200
union all select 'migration_validation_success', 'Migration validation', 10000, 43200
) seed
where not exists (
select 1 from slo_targets
where slo_targets.org_id = organizations.id and slo_targets.signal_key = seed.signal_key
);
insert into chaos_drills(id, org_id, key, label, scenario_key, enabled, created_at, updated_at)
select 'cdr_' || lower(hex(randomblob(16))), organizations.id, seed.key, seed.label, seed.scenario_key, 1, datetime('now'), datetime('now')
from organizations
cross join (
select 'provider-outage-policy' as key, 'Provider outage policy' as label, 'provider_outage_policy_gate' as scenario_key
union all select 'approval-backlog-routing', 'Approval backlog routing', 'approval_backlog_gate'
union all select 'browser-session-boundary', 'Browser session boundary', 'browser_session_boundary'
union all select 'migration-rollback-pack', 'Migration rollback pack', 'migration_rollback_pack'
) seed
where not exists (
select 1 from chaos_drills
where chaos_drills.org_id = organizations.id and chaos_drills.key = seed.key
);
insert into schema_migrations(version, name, applied_at)
values (17, 'operations_controls', datetime('now'));
`;
@@ -15,6 +15,7 @@ import { searchDocumentsSchema } from "./migrations/013-search-documents";
import { providerPolicyUsageSchema } from "./migrations/014-provider-policy-usage";
import { identityAccessSchema } from "./migrations/015-identity-access";
import { orgGovernanceSchema } from "./migrations/016-org-governance";
import { operationsControlsSchema } from "./migrations/017-operations-controls";
interface Migration {
version: number;
@@ -39,6 +40,7 @@ const MIGRATIONS: Migration[] = [
{ version: 14, name: "provider_policy_usage", sql: providerPolicyUsageSchema },
{ version: 15, name: "identity_access", sql: identityAccessSchema },
{ version: 16, name: "org_governance", sql: orgGovernanceSchema },
{ version: 17, name: "operations_controls", sql: operationsControlsSchema },
];
const SCHEMA_VERSION = MIGRATIONS[MIGRATIONS.length - 1].version;
@@ -3,8 +3,10 @@ import { createHash } from "node:crypto";
import { resolve } from "node:path";
import { DatabaseSync, type StatementSync } from "node:sqlite";
import type { CreateMigrationReportInput } from "../../shared/commands";
import { SLO_SIGNAL_KEY } from "../../shared/domain";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
import { OperationsService } from "./operations-service";
const LEGACY_TABLES = [
"organizations",
@@ -24,6 +26,11 @@ const LEGACY_TABLES = [
"approvals",
"approval_routing_rules",
"approval_assignments",
"feature_flags",
"slo_targets",
"operational_events",
"chaos_drills",
"chaos_drill_runs",
"chat_spaces",
"chat_messages",
"chat_threads",
@@ -129,6 +136,12 @@ const RELATION_CHECKS: RelationCheck[] = [
{ table: "approval_assignments", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "approval_assignments", column: "approval_id", referencedTable: "approvals", referencedColumn: "id", reason: "dangling_approval_id" },
{ table: "approval_assignments", column: "routing_rule_id", referencedTable: "approval_routing_rules", referencedColumn: "id", reason: "dangling_routing_rule_id" },
{ table: "feature_flags", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "slo_targets", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "operational_events", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "chaos_drills", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "chaos_drill_runs", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "chaos_drill_runs", column: "drill_id", referencedTable: "chaos_drills", referencedColumn: "id", reason: "dangling_drill_id" },
{ table: "chat_spaces", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "chat_messages", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
{ table: "chat_messages", column: "space_id", referencedTable: "chat_spaces", referencedColumn: "id", reason: "dangling_space_id" },
@@ -199,9 +212,11 @@ const RELATION_CHECKS: RelationCheck[] = [
export class MigrationService {
private readonly audit: AuditService;
private readonly operations: OperationsService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
this.operations = new OperationsService(db);
}
createReport(input: CreateMigrationReportInput): string {
@@ -270,6 +285,14 @@ export class MigrationService {
action: "created",
payload: { sourceRef: sourceDatabasePath, tableCount: Object.keys(tableCounts).length, sourceFingerprint },
});
this.operations.recordSignal({
orgId: input.orgId,
signalKey: SLO_SIGNAL_KEY.MIGRATION_VALIDATION_SUCCESS,
source: "migration_validation",
success: failures.length === 0,
durationMs: null,
detail: { reportId, sourceFingerprint, failureCount: failures.length },
});
return reportId;
} finally {
source.close();
@@ -0,0 +1,233 @@
import type { DatabaseSync } from "node:sqlite";
import type { RunChaosDrillInput, SetFeatureFlagInput } from "../../shared/commands";
import {
CHAOS_DRILL_SCENARIO,
CHAOS_DRILL_STATE,
FEATURE_FLAG_KEY,
SLO_SIGNAL_KEY,
type ChaosDrillScenario,
type FeatureFlagKey,
type SloSignalKey,
} from "../../shared/domain";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
const FEATURE_FLAGS = [
{ key: FEATURE_FLAG_KEY.PROVIDER_BACKGROUND_EXECUTION, label: "Provider background execution", enabled: true },
{ key: FEATURE_FLAG_KEY.SOCIAL_PUBLISH_APPROVAL, label: "Social publish approval", enabled: true },
{ key: FEATURE_FLAG_KEY.BOARD_INTERVENTION, label: "Board intervention", enabled: true },
{ key: FEATURE_FLAG_KEY.CHAOS_DRILLS, label: "Chaos drills", enabled: true },
] as const;
const SLO_TARGETS = [
{ signalKey: SLO_SIGNAL_KEY.PROVIDER_REQUEST_SUCCESS, label: "Provider requests", targetBasisPoints: 9950, windowMinutes: 43200 },
{ signalKey: SLO_SIGNAL_KEY.APPROVAL_ROUTE_COVERAGE, label: "Approval routing", targetBasisPoints: 10000, windowMinutes: 43200 },
{ signalKey: SLO_SIGNAL_KEY.BROWSER_SESSION_BOUNDARY, label: "Browser session boundary", targetBasisPoints: 10000, windowMinutes: 43200 },
{ signalKey: SLO_SIGNAL_KEY.SKILL_RESOLVE_SUCCESS, label: "Skill resolve", targetBasisPoints: 9950, windowMinutes: 43200 },
{ signalKey: SLO_SIGNAL_KEY.MIGRATION_VALIDATION_SUCCESS, label: "Migration validation", targetBasisPoints: 10000, windowMinutes: 43200 },
] as const;
const CHAOS_DRILLS = [
{ key: "provider-outage-policy", label: "Provider outage policy", scenarioKey: CHAOS_DRILL_SCENARIO.PROVIDER_OUTAGE_POLICY_GATE },
{ key: "approval-backlog-routing", label: "Approval backlog routing", scenarioKey: CHAOS_DRILL_SCENARIO.APPROVAL_BACKLOG_GATE },
{ key: "browser-session-boundary", label: "Browser session boundary", scenarioKey: CHAOS_DRILL_SCENARIO.BROWSER_SESSION_BOUNDARY },
{ key: "migration-rollback-pack", label: "Migration rollback pack", scenarioKey: CHAOS_DRILL_SCENARIO.MIGRATION_ROLLBACK_PACK },
] as const;
interface DrillOutcome {
success: boolean;
checks: Record<string, number | boolean | string>;
}
interface ChaosDrillRow {
id: string;
scenario_key: ChaosDrillScenario;
enabled: number;
}
export class OperationsService {
private readonly audit: AuditService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
}
seedOrganizationOperations(orgId: string, now: string): void {
for (const flag of FEATURE_FLAGS) {
this.db.prepare(`
insert into feature_flags(id, org_id, key, label, enabled, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?)
on conflict(org_id, key) do nothing
`).run(createId("ffg"), orgId, flag.key, flag.label, flag.enabled ? 1 : 0, now, now);
}
for (const target of SLO_TARGETS) {
this.db.prepare(`
insert into slo_targets(id, org_id, signal_key, label, target_basis_points, window_minutes, created_at, updated_at)
values (?, ?, ?, ?, ?, ?, ?, ?)
on conflict(org_id, signal_key) do nothing
`).run(createId("slo"), orgId, target.signalKey, target.label, target.targetBasisPoints, target.windowMinutes, now, now);
}
for (const drill of CHAOS_DRILLS) {
this.db.prepare(`
insert into chaos_drills(id, org_id, key, label, scenario_key, enabled, created_at, updated_at)
values (?, ?, ?, ?, ?, 1, ?, ?)
on conflict(org_id, key) do nothing
`).run(createId("cdr"), orgId, drill.key, drill.label, drill.scenarioKey, now, now);
}
}
setFeatureFlag(input: SetFeatureFlagInput): void {
const flag = this.db.prepare("select key from feature_flags where id = ? and org_id = ?")
.get(input.flagId, input.orgId) as { key: FeatureFlagKey } | undefined;
if (!flag) throw new Error("Feature flag was not found.");
const now = new Date().toISOString();
const result = this.db.prepare("update feature_flags set enabled = ?, updated_at = ? where id = ? and org_id = ?")
.run(input.enabled ? 1 : 0, now, input.flagId, input.orgId);
if (result.changes === 0) throw new Error("Feature flag was not found.");
this.audit.record({
orgId: input.orgId,
entityType: "feature_flag",
entityId: input.flagId,
actorType: "system",
actorId: null,
action: "updated",
payload: { key: flag.key, enabled: input.enabled },
});
}
runChaosDrill(input: RunChaosDrillInput): string {
if (!this.isFeatureEnabled(input.orgId, FEATURE_FLAG_KEY.CHAOS_DRILLS)) throw new Error("Chaos drills are disabled.");
const drill = this.db.prepare("select id, scenario_key, enabled from chaos_drills where id = ? and org_id = ?")
.get(input.drillId, input.orgId) as ChaosDrillRow | undefined;
if (!drill) throw new Error("Chaos drill was not found.");
if (drill.enabled !== 1) throw new Error("Chaos drill is disabled.");
const startedAt = new Date().toISOString();
const outcome = this.evaluateScenario(input.orgId, drill.scenario_key);
const runId = createId("cdrun");
const completedAt = new Date().toISOString();
this.db.prepare(`
insert into chaos_drill_runs(id, org_id, drill_id, state, result_json, started_at, completed_at)
values (?, ?, ?, ?, ?, ?, ?)
`).run(
runId,
input.orgId,
input.drillId,
outcome.success ? CHAOS_DRILL_STATE.PASSED : CHAOS_DRILL_STATE.FAILED,
JSON.stringify(outcome.checks),
startedAt,
completedAt,
);
this.recordSignal({
orgId: input.orgId,
signalKey: this.signalForScenario(drill.scenario_key),
source: "chaos_drill",
success: outcome.success,
durationMs: new Date(completedAt).getTime() - new Date(startedAt).getTime(),
detail: { drillId: input.drillId, scenarioKey: drill.scenario_key },
});
this.audit.record({
orgId: input.orgId,
entityType: "chaos_drill",
entityId: input.drillId,
actorType: "system",
actorId: null,
action: "run",
payload: { runId, state: outcome.success ? CHAOS_DRILL_STATE.PASSED : CHAOS_DRILL_STATE.FAILED },
});
return runId;
}
isFeatureEnabled(orgId: string, key: FeatureFlagKey): boolean {
const row = this.db.prepare("select enabled from feature_flags where org_id = ? and key = ?")
.get(orgId, key) as { enabled: number } | undefined;
if (!row) throw new Error("Feature flag is required.");
return row.enabled === 1;
}
recordSignal(input: {
orgId: string;
signalKey: SloSignalKey;
source: string;
success: boolean;
durationMs: number | null;
detail: unknown;
}): void {
this.db.prepare(`
insert into operational_events(id, org_id, signal_key, source, success, duration_ms, detail_json, created_at)
values (?, ?, ?, ?, ?, ?, ?, ?)
`).run(
createId("ope"),
input.orgId,
input.signalKey,
input.source,
input.success ? 1 : 0,
input.durationMs,
JSON.stringify(input.detail),
new Date().toISOString(),
);
}
private evaluateScenario(orgId: string, scenario: ChaosDrillScenario): DrillOutcome {
if (scenario === CHAOS_DRILL_SCENARIO.PROVIDER_OUTAGE_POLICY_GATE) return this.providerOutagePolicyGate(orgId);
if (scenario === CHAOS_DRILL_SCENARIO.APPROVAL_BACKLOG_GATE) return this.approvalBacklogGate(orgId);
if (scenario === CHAOS_DRILL_SCENARIO.BROWSER_SESSION_BOUNDARY) return this.browserSessionBoundary(orgId);
if (scenario === CHAOS_DRILL_SCENARIO.MIGRATION_ROLLBACK_PACK) return this.migrationRollbackPack(orgId);
throw new Error("Chaos drill scenario is invalid.");
}
private providerOutagePolicyGate(orgId: string): DrillOutcome {
const total = count(this.db, "select count(*) as count from providers where org_id = ?", orgId);
const policyConflicts = count(this.db, `
select count(*) as count
from providers
where org_id = ? and data_policy = 'zero_data_retention' and background_enabled = 1
`, orgId);
const invalidLimits = count(this.db, "select count(*) as count from providers where org_id = ? and monthly_request_limit < 1", orgId);
return {
success: total > 0 && policyConflicts === 0 && invalidLimits === 0,
checks: { providers: total, policyConflicts, invalidLimits },
};
}
private approvalBacklogGate(orgId: string): DrillOutcome {
const routes = count(this.db, "select count(*) as count from approval_routing_rules where org_id = ?", orgId);
const unassigned = count(this.db, `
select count(*) as count
from approvals
where org_id = ? and id not in (
select approval_id from approval_assignments where org_id = ?
)
`, orgId, orgId);
return { success: routes > 0 && unassigned === 0, checks: { routes, unassignedApprovals: unassigned } };
}
private browserSessionBoundary(orgId: string): DrillOutcome {
const accounts = count(this.db, "select count(*) as count from social_accounts where org_id = ?", orgId);
const grants = count(this.db, "select count(*) as count from social_account_grants where org_id = ?", orgId);
const actions = count(this.db, "select count(*) as count from browser_actions where org_id = ?", orgId);
return { success: grants >= accounts, checks: { accounts, grants, actions } };
}
private migrationRollbackPack(orgId: string): DrillOutcome {
const reports = this.db.prepare("select report_json from migration_reports where org_id = ?").all(orgId) as Array<{ report_json: string }>;
const complete = reports.filter((report) => hasRollbackPack(report.report_json)).length;
return { success: reports.length === complete, checks: { reports: reports.length, rollbackPacks: complete } };
}
private signalForScenario(scenario: ChaosDrillScenario): SloSignalKey {
if (scenario === CHAOS_DRILL_SCENARIO.PROVIDER_OUTAGE_POLICY_GATE) return SLO_SIGNAL_KEY.PROVIDER_REQUEST_SUCCESS;
if (scenario === CHAOS_DRILL_SCENARIO.APPROVAL_BACKLOG_GATE) return SLO_SIGNAL_KEY.APPROVAL_ROUTE_COVERAGE;
if (scenario === CHAOS_DRILL_SCENARIO.BROWSER_SESSION_BOUNDARY) return SLO_SIGNAL_KEY.BROWSER_SESSION_BOUNDARY;
if (scenario === CHAOS_DRILL_SCENARIO.MIGRATION_ROLLBACK_PACK) return SLO_SIGNAL_KEY.MIGRATION_VALIDATION_SUCCESS;
throw new Error("Chaos drill scenario is invalid.");
}
}
function count(db: DatabaseSync, sql: string, ...params: string[]): number {
return (db.prepare(sql).get(...params) as { count: number }).count;
}
function hasRollbackPack(reportJson: string): boolean {
const parsed = JSON.parse(reportJson) as { rollbackPack?: unknown };
return typeof parsed.rollbackPack === "object" && parsed.rollbackPack !== null;
}
@@ -1,17 +1,20 @@
import type { DatabaseSync } from "node:sqlite";
import type { CreateAgentInput } from "../../shared/commands";
import { RISK_LEVEL, ROUTE_TO_TYPE, type RiskLevel } from "../../shared/domain";
import { RISK_LEVEL, ROUTE_TO_TYPE, SLO_SIGNAL_KEY, type RiskLevel } from "../../shared/domain";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
import { OperationsService } from "./operations-service";
const DEFAULT_DEPARTMENTS = ["Executive", "Engineering", "Product", "Design", "Marketing", "Operations", "Support"] as const;
const RISK_RANK: Record<RiskLevel, number> = { low: 0, medium: 1, high: 2 };
export class OrgGovernanceService {
private readonly audit: AuditService;
private readonly operations: OperationsService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
this.operations = new OperationsService(db);
}
seedOrganizationGovernance(orgId: string, boardEmployeeId: string, now: string): void {
@@ -61,6 +64,14 @@ export class OrgGovernanceService {
action: "routed",
payload: { routingRuleId: rule.id, routeToType: rule.route_to_type, routeToId: rule.route_to_id },
});
this.operations.recordSignal({
orgId: input.orgId,
signalKey: SLO_SIGNAL_KEY.APPROVAL_ROUTE_COVERAGE,
source: "approval_routing",
success: true,
durationMs: null,
detail: { approvalId: input.approvalId, routingRuleId: rule.id },
});
}
private seedDepartmentPolicy(orgId: string, departmentName: string, now: string): void {
@@ -7,6 +7,7 @@ import { AuditService } from "./audit-service";
import { PolicyService } from "./policy-service";
import { IdentityAccessService } from "./identity-access-service";
import { OrgGovernanceService } from "./org-governance-service";
import { OperationsService } from "./operations-service";
import { assertUsableMentionName } from "./mention-handles";
export class OrganizationService {
@@ -14,12 +15,14 @@ export class OrganizationService {
private readonly policy: PolicyService;
private readonly identityAccess: IdentityAccessService;
private readonly governance: OrgGovernanceService;
private readonly operations: OperationsService;
constructor(private readonly db: DatabaseSync) {
this.audit = new AuditService(db);
this.policy = new PolicyService(db);
this.identityAccess = new IdentityAccessService(db);
this.governance = new OrgGovernanceService(db);
this.operations = new OperationsService(db);
}
createOrganization(input: CreateOrganizationInput): string {
@@ -48,6 +51,7 @@ export class OrganizationService {
.run(employeeId, orgId, input.employeeName.trim(), "Board Chair", now);
this.identityAccess.seedEmployeeAccess(orgId, employeeId, now);
this.governance.seedOrganizationGovernance(orgId, employeeId, now);
this.operations.seedOrganizationOperations(orgId, now);
for (const space of [
["company", "general", "Company coordination", "workspace"],
@@ -2,10 +2,11 @@ import type { DatabaseSync } from "node:sqlite";
import { createHash } from "node:crypto";
import { BrowserWindow, session } from "electron";
import type { CreateProviderResponseInput, SaveProviderCredentialInput, UpdateProviderPolicyInput } from "../../shared/commands";
import { PROVIDER_AUTH_TYPE, PROVIDER_DATA_POLICY, PROVIDER_EXECUTION_MODE, PROVIDER_REQUEST_STATE, type ProviderExecutionMode } from "../../shared/domain";
import { FEATURE_FLAG_KEY, PROVIDER_AUTH_TYPE, PROVIDER_DATA_POLICY, PROVIDER_EXECUTION_MODE, PROVIDER_REQUEST_STATE, SLO_SIGNAL_KEY, type ProviderExecutionMode } from "../../shared/domain";
import { PROVIDER_CATALOG } from "../../shared/provider-catalog";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
import { OperationsService } from "./operations-service";
import { SecretVault } from "./secret-vault";
import { providerAdapters, type ProviderAdapterRegistry } from "./provider-adapters";
@@ -23,6 +24,7 @@ function displayHint(secret: string): string {
export class ProviderService {
private readonly audit: AuditService;
private readonly operations: OperationsService;
constructor(
private readonly db: DatabaseSync,
@@ -30,6 +32,7 @@ export class ProviderService {
private readonly vault: SecretVault = new SecretVault(),
) {
this.audit = new AuditService(db);
this.operations = new OperationsService(db);
}
saveCredential(input: SaveProviderCredentialInput): void {
@@ -187,6 +190,7 @@ export class ProviderService {
const requestId = createId("preq");
const now = new Date().toISOString();
const startedAt = Date.now();
this.db.prepare(`
insert into provider_requests(
id, org_id, provider_id, provider_key, model, prompt_sha256,
@@ -232,6 +236,14 @@ export class ProviderService {
action: "response_created",
payload: { providerKey: provider.provider_key, model: provider.model },
});
this.operations.recordSignal({
orgId: input.orgId,
signalKey: SLO_SIGNAL_KEY.PROVIDER_REQUEST_SUCCESS,
source: "provider_gateway",
success: true,
durationMs: Date.now() - startedAt,
detail: { requestId, providerKey: provider.provider_key, executionMode },
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
this.db.prepare(`
@@ -241,6 +253,14 @@ export class ProviderService {
`).run(PROVIDER_REQUEST_STATE.FAILED, message, new Date().toISOString(), requestId, input.orgId);
this.recordUsage(input.orgId, provider.id, prompt.length, 0);
this.recordProviderHealth(provider.id, input.orgId, HEALTH_STATE_FAILED, message);
this.operations.recordSignal({
orgId: input.orgId,
signalKey: SLO_SIGNAL_KEY.PROVIDER_REQUEST_SUCCESS,
source: "provider_gateway",
success: false,
durationMs: Date.now() - startedAt,
detail: { requestId, providerKey: provider.provider_key, executionMode },
});
throw error;
}
}
@@ -250,6 +270,9 @@ export class ProviderService {
if (executionMode === PROVIDER_EXECUTION_MODE.BACKGROUND && provider.data_policy === PROVIDER_DATA_POLICY.ZERO_DATA_RETENTION) {
throw new Error("Background execution conflicts with zero data retention policy.");
}
if (executionMode === PROVIDER_EXECUTION_MODE.BACKGROUND && !this.operations.isFeatureEnabled(orgId, FEATURE_FLAG_KEY.PROVIDER_BACKGROUND_EXECUTION)) {
throw new Error("Provider background execution is disabled by feature flag.");
}
if (executionMode === PROVIDER_EXECUTION_MODE.BACKGROUND && provider.background_enabled !== 1) {
throw new Error("Background execution is disabled for this provider policy.");
}
@@ -1,8 +1,10 @@
import { existsSync, readFileSync } from "node:fs";
import type { DatabaseSync } from "node:sqlite";
import type { BindSkillInput, ImportSkillDirectoryInput, ResolveAgentSkillsInput } from "../../shared/commands";
import { SLO_SIGNAL_KEY } from "../../shared/domain";
import { createId } from "../../shared/ids";
import { AuditService } from "./audit-service";
import { OperationsService } from "./operations-service";
import { copySkillRevision, recursiveSkillChecksum } from "./skill-artifacts";
import { loadSkillManifest, type SkillManifest } from "./skill-manifest";
@@ -12,9 +14,11 @@ function slugify(value: string): string {
export class SkillService {
private readonly audit: AuditService;
private readonly operations: OperationsService;
constructor(private readonly db: DatabaseSync, private readonly artifactRoot: string) {
this.audit = new AuditService(db);
this.operations = new OperationsService(db);
}
importDirectory(input: ImportSkillDirectoryInput): string {
@@ -166,6 +170,14 @@ export class SkillService {
action: "resolved",
payload: { agentId: input.agentId, runId: input.runId, revisionIds },
});
this.operations.recordSignal({
orgId: input.orgId,
signalKey: SLO_SIGNAL_KEY.SKILL_RESOLVE_SUCCESS,
source: "skill_resolver",
success: true,
durationMs: null,
detail: { agentId: input.agentId, runId: input.runId, revisionIds },
});
return snapshotId;
}
@@ -53,6 +53,12 @@ export class SnapshotService {
.map((freeze) => ({ ...freeze, active: toBool(freeze.active) }));
const employeeIdentities = list<AppSnapshot["employeeIdentities"][number]>(this.db, "employee_identities", "created_at desc")
.map((identity) => ({ ...identity, active: toBool(identity.active) }));
const featureFlags = list<AppSnapshot["featureFlags"][number]>(this.db, "feature_flags", "key asc")
.map((flag) => ({ ...flag, enabled: toBool(flag.enabled) }));
const operationalEvents = list<AppSnapshot["operationalEvents"][number]>(this.db, "operational_events", "created_at desc")
.map((event) => ({ ...event, success: toBool(event.success) }));
const chaosDrills = list<AppSnapshot["chaosDrills"][number]>(this.db, "chaos_drills", "key asc")
.map((drill) => ({ ...drill, enabled: toBool(drill.enabled) }));
return {
activeOrgId,
@@ -100,6 +106,11 @@ export class SnapshotService {
hiringPolicies: list(this.db, "hiring_policies", "department_name asc"),
approvalRoutingRules: list(this.db, "approval_routing_rules", "created_at asc"),
approvalAssignments: list(this.db, "approval_assignments", "created_at desc"),
featureFlags,
sloTargets: list(this.db, "slo_targets", "signal_key asc"),
operationalEvents,
chaosDrills,
chaosDrillRuns: list(this.db, "chaos_drill_runs", "started_at desc"),
auditEvents: list<AppSnapshot["auditEvents"][number]>(this.db, "audit_events", "created_at desc").slice(0, 200),
};
}