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

+8 -2
View File
@@ -47,6 +47,12 @@ test("onboards, hires, and creates work in the Electron shell", async () => {
await page.getByRole("button", { name: "Policies" }).click();
await expect(page.getByText("Hiring policies")).toBeVisible();
await expect(page.getByText("Review routing")).toBeVisible();
await page.getByRole("button", { name: "Operations", exact: true }).click();
await expect(page.getByText("Feature flags")).toBeVisible();
await expect(page.getByText("SLO targets")).toBeVisible();
await expect(page.getByText("Chaos drills").first()).toBeVisible();
await page.getByRole("button", { name: "Run" }).first().click();
await expect(page.getByText("passed").first()).toBeVisible();
await page.getByRole("button", { name: "Work", exact: true }).click();
await page.getByRole("button", { name: "New task" }).click();
@@ -128,9 +134,9 @@ async function verifyPagesDoNotOverflow(page: Page): Promise<void> {
await page.getByRole("button", { name: nav, exact: true }).click();
await expectPageToFit(page);
}
for (const tab of ["Workspace", "Members", "Billing", "Models", "Providers", "Skills", "Policies", "Compliance", "Migration", "Profile", "Notifications", "Security"]) {
for (const tab of ["Workspace", "Members", "Billing", "Models", "Providers", "Skills", "Policies", "Operations", "Compliance", "Migration", "Profile", "Notifications", "Security"]) {
await page.getByRole("button", { name: "Settings" }).click();
await page.getByRole("button", { name: tab }).click();
await page.getByRole("button", { name: tab, exact: true }).click();
await expectPageToFit(page);
}
}
@@ -23,7 +23,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]);
connection.close();
});
@@ -48,6 +48,9 @@ describe("domain services", () => {
expect(state.orgDepartments.map((department) => department.name)).toContain("Engineering");
expect(state.hiringPolicies.some((policy) => policy.departmentName === "Engineering")).toBe(true);
expect(state.approvalRoutingRules[0]).toMatchObject({ kind: "*", minimumRisk: "low", routeToType: "employee" });
expect(state.featureFlags.map((flag) => flag.key)).toContain("chaos_drills");
expect(state.sloTargets.map((target) => target.signalKey)).toContain("provider_request_success");
expect(state.chaosDrills.map((drill) => drill.scenarioKey)).toContain("approval_backlog_gate");
expect(state.chatSpaces.map((space) => space.name)).toContain("board");
expect(state.policyRules[0]?.action).toBe("task_create");
expect(state.auditEvents[0]?.action).toBe("created");
@@ -108,6 +108,11 @@ describe("migration service", () => {
create table approvals(id text primary key, org_id text, decided_by_employee_id text);
create table approval_routing_rules(id text primary key, org_id text);
create table approval_assignments(id text primary key, org_id text, approval_id text, routing_rule_id text);
create table feature_flags(id text primary key, org_id text);
create table slo_targets(id text primary key, org_id text);
create table operational_events(id text primary key, org_id text);
create table chaos_drills(id text primary key, org_id text);
create table chaos_drill_runs(id text primary key, org_id text, drill_id text);
create table chat_spaces(id text primary key, org_id text);
create table chat_messages(id text primary key, org_id text, space_id text, thread_id text);
create table chat_threads(id text primary key, org_id text, space_id text, root_message_id text);
@@ -163,6 +168,11 @@ describe("migration service", () => {
insert into approvals(id, org_id, decided_by_employee_id) values ('approval-1', 'org-1', 'emp-1');
insert into approval_routing_rules(id, org_id) values ('route-1', 'org-1');
insert into approval_assignments(id, org_id, approval_id, routing_rule_id) values ('assign-1', 'org-1', 'approval-1', 'route-1');
insert into feature_flags(id, org_id) values ('flag-1', 'org-1');
insert into slo_targets(id, org_id) values ('slo-1', 'org-1');
insert into operational_events(id, org_id) values ('op-1', 'org-1');
insert into chaos_drills(id, org_id) values ('drill-1', 'org-1');
insert into chaos_drill_runs(id, org_id, drill_id) values ('drill-run-1', 'org-1', 'drill-1');
insert into chat_threads(id, org_id, space_id, root_message_id) values ('thread-1', 'org-1', 'space-1', null);
insert into chat_messages(id, org_id, space_id, thread_id) values ('msg-1', 'org-1', 'space-1', 'thread-1');
insert into chat_memberships(space_id, principal_type, principal_id) values ('space-1', 'employee', 'emp-1');
@@ -207,6 +217,8 @@ describe("migration service", () => {
expect(body.tableCounts.employee_identities).toBe(1);
expect(body.tableCounts.org_departments).toBe(1);
expect(body.tableCounts.hiring_policies).toBe(1);
expect(body.tableCounts.feature_flags).toBe(1);
expect(body.tableCounts.chaos_drill_runs).toBe(1);
expect(body.tableCounts.role_grants).toBe(1);
expect(body.tableCounts.skill_artifacts).toBe(1);
expect(body.tableCounts.chat_read_state).toBe(1);
@@ -221,6 +233,8 @@ describe("migration service", () => {
expect(body.idMaps.service_identities).toEqual(["sid-1"]);
expect(body.idMaps.access_decisions).toEqual(["access-1"]);
expect(body.idMaps.approval_assignments).toEqual(["assign-1"]);
expect(body.idMaps.operational_events).toEqual(["op-1"]);
expect(body.idMaps.chaos_drill_runs).toEqual(["drill-run-1"]);
expect(body.idMaps.board_interventions).toEqual(["bdi-1"]);
expect(body.idMaps.retention_freezes).toEqual(["freeze-1"]);
expect(body.idMaps.provider_usage_windows).toEqual(["usage-1"]);
@@ -0,0 +1,124 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { OperationsService } from "../../src/main/services/operations-service";
import { OrganizationService } from "../../src/main/services/organization-service";
import { ProviderService } from "../../src/main/services/provider-service";
import { providerAdapters, type ProviderAdapterRegistry } from "../../src/main/services/provider-adapters";
import { SnapshotService } from "../../src/main/services/snapshot-service";
import { PROVIDER_DATA_POLICY, PROVIDER_EXECUTION_MODE } from "../../src/shared/domain";
function createConnection(): DatabaseConnection {
return new DatabaseConnection(join(tmpdir(), `agentcompany-new-operations-${randomUUID()}.db`));
}
function createOrganization(organization: OrganizationService): string {
return organization.createOrganization({
organizationName: "Acme Operations",
employeeName: "Dana Lee",
mission: "Coordinate agents with auditable approvals.",
goal: "Ship a verified control plane.",
});
}
function providerId(connection: DatabaseConnection, orgId: string, key: string): string {
return (connection.db.prepare("select id from providers where org_id = ? and provider_key = ?")
.get(orgId, key) as { id: string }).id;
}
const testVault = {
encryptSecret: (secret: string) => Buffer.from(secret, "utf8"),
decryptSecret: (ciphertext: Buffer) => ciphertext.toString("utf8"),
};
describe("operations service", () => {
test("seeds feature flags, SLO targets, and chaos drills for new organizations", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const snapshot = new SnapshotService(connection.db);
connection.transaction(() => {
createOrganization(organization);
});
const state = snapshot.load();
expect(state.featureFlags.map((flag) => flag.key).sort()).toEqual([
"board_intervention",
"chaos_drills",
"provider_background_execution",
"social_publish_approval",
]);
expect(state.sloTargets.map((target) => target.signalKey)).toContain("browser_session_boundary");
expect(state.chaosDrills).toHaveLength(4);
connection.close();
});
test("feature flags gate provider background execution", async () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const operations = new OperationsService(connection.db);
const adapters: ProviderAdapterRegistry = {
...providerAdapters,
openai: {
...providerAdapters.openai,
parseAuthProfile: (secret) => ({ type: "api_key", apiKey: secret.trim() }),
checkHealth: async () => ({ checkedAt: new Date().toISOString() }),
createResponse: async () => ({ text: "accepted", rawResponseJson: "{}" }),
},
};
const providers = new ProviderService(connection.db, adapters, testVault);
let orgId = "";
let openAiProviderId = "";
let flagId = "";
connection.transaction(() => {
orgId = createOrganization(organization);
openAiProviderId = providerId(connection, orgId, "openai");
flagId = (connection.db.prepare("select id from feature_flags where org_id = ? and key = ?")
.get(orgId, "provider_background_execution") as { id: string }).id;
providers.saveCredential({ orgId, providerId: openAiProviderId, secret: "sk-live-provider", model: "gpt-5.4" });
providers.updatePolicy({
orgId,
providerId: openAiProviderId,
dataPolicy: PROVIDER_DATA_POLICY.STANDARD,
backgroundEnabled: true,
monthlyRequestLimit: 10,
workspaceScope: "organization",
});
operations.setFeatureFlag({ orgId, flagId, enabled: false });
});
await expect(providers.createResponse({
orgId,
providerId: openAiProviderId,
prompt: "Run background analysis",
instructions: null,
executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND,
})).rejects.toThrow("Provider background execution is disabled by feature flag.");
connection.close();
});
test("runs chaos drills and records operational signals", () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
const operations = new OperationsService(connection.db);
const snapshot = new SnapshotService(connection.db);
let orgId = "";
let drillId = "";
connection.transaction(() => {
orgId = createOrganization(organization);
drillId = (connection.db.prepare("select id from chaos_drills where org_id = ? and scenario_key = ?")
.get(orgId, "approval_backlog_gate") as { id: string }).id;
operations.runChaosDrill({ orgId, drillId });
});
const state = snapshot.load();
expect(state.chaosDrillRuns[0]).toMatchObject({ orgId, drillId, state: "passed" });
expect(state.operationalEvents[0]).toMatchObject({ orgId, signalKey: "approval_route_coverage", source: "chaos_drill", success: true });
expect(state.auditEvents.some((event) => event.entityType === "chaos_drill" && event.action === "run")).toBe(true);
connection.close();
});
});
@@ -118,6 +118,7 @@ describe("provider service", () => {
responseText: "api_key:gpt-5.4:Draft board memo",
state: "succeeded",
});
expect(state.operationalEvents[0]).toMatchObject({ signalKey: "provider_request_success", source: "provider_gateway", success: true });
expect(state.providers.find((provider) => provider.id === openAiProviderId)?.healthState).toBe("healthy");
connection.close();
});