Scope task run isolation
This commit is contained in:
1 parent
648544114a
commit
9cc4c3b44a
9 files changed
+165
-16
No files matched your search
@@ -0,0 +1,16 @@
|
||||
export const taskRunIsolationSchema = `
|
||||
alter table task_runs add column execution_context_id text;
|
||||
alter table task_runs add column service_identity_id text references service_identities(id) on delete set null;
|
||||
alter table task_runs add column workspace_mount_json text not null default '[]';
|
||||
alter table task_runs add column secret_scope_json text not null default '{}';
|
||||
|
||||
create unique index if not exists idx_task_runs_execution_context
|
||||
on task_runs(execution_context_id)
|
||||
where execution_context_id is not null;
|
||||
create index if not exists idx_task_runs_service_identity
|
||||
on task_runs(service_identity_id)
|
||||
where service_identity_id is not null;
|
||||
|
||||
insert into schema_migrations(version, name, applied_at)
|
||||
values (35, 'task_run_isolation', datetime('now'));
|
||||
`;
|
||||
@@ -33,6 +33,7 @@ import { providerRequestWorkspaceSchema } from "./migrations/031-provider-reques
|
||||
import { browserActionMetadataSchema } from "./migrations/032-browser-action-metadata";
|
||||
import { auditEventImmutabilitySchema } from "./migrations/033-audit-event-immutability";
|
||||
import { providerAuthSessionsSchema } from "./migrations/034-provider-auth-sessions";
|
||||
import { taskRunIsolationSchema } from "./migrations/035-task-run-isolation";
|
||||
|
||||
interface Migration {
|
||||
version: number;
|
||||
@@ -75,6 +76,7 @@ const MIGRATIONS: Migration[] = [
|
||||
{ version: 32, name: "browser_action_metadata", sql: browserActionMetadataSchema },
|
||||
{ version: 33, name: "audit_event_immutability", sql: auditEventImmutabilitySchema },
|
||||
{ version: 34, name: "provider_auth_sessions", sql: providerAuthSessionsSchema },
|
||||
{ version: 35, name: "task_run_isolation", sql: taskRunIsolationSchema },
|
||||
];
|
||||
|
||||
const SCHEMA_VERSION = MIGRATIONS[MIGRATIONS.length - 1].version;
|
||||
|
||||
@@ -151,6 +151,7 @@ const RELATION_CHECKS: RelationCheck[] = [
|
||||
{ table: "task_runs", column: "agent_id", referencedTable: "agents", referencedColumn: "id", reason: "dangling_agent_id" },
|
||||
{ table: "task_runs", column: "provider_id", referencedTable: "providers", referencedColumn: "id", reason: "dangling_provider_id" },
|
||||
{ table: "task_runs", column: "provider_request_id", referencedTable: "provider_requests", referencedColumn: "id", reason: "dangling_provider_request_id" },
|
||||
{ table: "task_runs", column: "service_identity_id", referencedTable: "service_identities", referencedColumn: "id", reason: "dangling_service_identity_id" },
|
||||
{ table: "approvals", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
|
||||
{ table: "approvals", column: "decided_by_employee_id", referencedTable: "employees", referencedColumn: "id", reason: "dangling_decider_id" },
|
||||
{ table: "approval_routing_rules", column: "org_id", referencedTable: "organizations", referencedColumn: "id", reason: "dangling_org_id" },
|
||||
|
||||
@@ -67,7 +67,7 @@ export class OnboardingService {
|
||||
const projectId = this.createProject(orgId, goalId, ceoAgentId, input, now);
|
||||
if (workspaceId) this.bindWorkspaceToProject(workspaceId, projectId, now);
|
||||
const taskId = this.createKickoffTask(orgId, goalId, projectId, workspaceId, ceoAgentId, input, now);
|
||||
const runId = this.createTaskRun(orgId, taskId, ceoAgentId, providerId, now);
|
||||
const runId = this.createTaskRun(orgId, taskId, ceoAgentId, providerId, workspaceId, now);
|
||||
|
||||
this.audit.record({
|
||||
orgId,
|
||||
@@ -193,17 +193,54 @@ export class OnboardingService {
|
||||
return taskId;
|
||||
}
|
||||
|
||||
private createTaskRun(orgId: string, taskId: string, ceoAgentId: string, providerId: string, now: string): string {
|
||||
private createTaskRun(orgId: string, taskId: string, ceoAgentId: string, providerId: string, workspaceId: string | null, now: string): string {
|
||||
const runId = createId("run");
|
||||
const executionContextId = createId("ctx");
|
||||
const serviceIdentityId = this.requireServiceIdentity(orgId, ceoAgentId);
|
||||
const workspaceMountJson = JSON.stringify(workspaceId ? [{
|
||||
workspaceId,
|
||||
mode: "read_write",
|
||||
}] : []);
|
||||
const secretScopeJson = JSON.stringify({
|
||||
serviceIdentityId,
|
||||
providerIds: [providerId],
|
||||
scope: "bootstrap",
|
||||
credentialAccess: "provider_runtime_only",
|
||||
});
|
||||
this.deps.db.prepare(`
|
||||
insert into task_runs(
|
||||
id, org_id, task_id, agent_id, provider_id, trigger, state, created_at, started_at, completed_at, error_message
|
||||
id, org_id, task_id, agent_id, provider_id, provider_request_id,
|
||||
execution_context_id, service_identity_id, workspace_mount_json, secret_scope_json,
|
||||
trigger, state, created_at, started_at, completed_at, error_message
|
||||
)
|
||||
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(runId, orgId, taskId, ceoAgentId, providerId, "bootstrap", "queued", now, null, null, null);
|
||||
values (?, ?, ?, ?, ?, null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
runId,
|
||||
orgId,
|
||||
taskId,
|
||||
ceoAgentId,
|
||||
providerId,
|
||||
executionContextId,
|
||||
serviceIdentityId,
|
||||
workspaceMountJson,
|
||||
secretScopeJson,
|
||||
"bootstrap",
|
||||
"queued",
|
||||
now,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
return runId;
|
||||
}
|
||||
|
||||
private requireServiceIdentity(orgId: string, agentId: string): string {
|
||||
const row = this.deps.db.prepare("select id from service_identities where org_id = ? and agent_id = ?")
|
||||
.get(orgId, agentId) as { id: string } | undefined;
|
||||
if (!row) throw new Error("Agent service identity was not found.");
|
||||
return row.id;
|
||||
}
|
||||
|
||||
private createKickoffTaskDescription(input: BootstrapOnboardingInput): string {
|
||||
return [
|
||||
`Company: ${input.companyName.trim()}`,
|
||||
|
||||
@@ -56,6 +56,7 @@ interface GoalRow {
|
||||
}
|
||||
|
||||
interface WorkspaceRow {
|
||||
id: string;
|
||||
name: string;
|
||||
local_path: string;
|
||||
repo_url: string;
|
||||
@@ -74,6 +75,13 @@ interface SkillRuntimeSnapshotRow {
|
||||
source_revision_ids: string;
|
||||
}
|
||||
|
||||
interface TaskRunIsolation {
|
||||
executionContextId: string;
|
||||
serviceIdentityId: string;
|
||||
workspaceMountJson: string;
|
||||
secretScopeJson: string;
|
||||
}
|
||||
|
||||
export class TaskExecutionService {
|
||||
private readonly audit: AuditService;
|
||||
|
||||
@@ -90,7 +98,9 @@ export class TaskExecutionService {
|
||||
this.assertExecutionApproval(input.orgId, task);
|
||||
const agent = this.requireAssignedAgent(input.orgId, task);
|
||||
const provider = this.requireAgentProvider(input.orgId, agent);
|
||||
const runId = this.createRun(input.orgId, task.id, agent.id, provider.id);
|
||||
const workspace = this.workspace(task.workspace_id);
|
||||
const isolation = this.createIsolationContext(input.orgId, task, agent, provider, workspace);
|
||||
const runId = this.createRun(input.orgId, task.id, agent.id, provider.id, isolation);
|
||||
let providerRequestId: string | null = null;
|
||||
let skillRuntimeSnapshot: SkillRuntimeSnapshotRow | null = null;
|
||||
|
||||
@@ -106,7 +116,8 @@ export class TaskExecutionService {
|
||||
provider,
|
||||
project: this.project(task.project_id),
|
||||
goal: this.goal(task.goal_id),
|
||||
workspace: this.workspace(task.workspace_id),
|
||||
workspace,
|
||||
isolation,
|
||||
skillRuntimeSnapshot,
|
||||
}),
|
||||
instructions: buildAgentInstructions(agent),
|
||||
@@ -129,6 +140,8 @@ export class TaskExecutionService {
|
||||
agentId: agent.id,
|
||||
providerId: provider.id,
|
||||
providerRequestId: requestId,
|
||||
executionContextId: isolation.executionContextId,
|
||||
serviceIdentityId: isolation.serviceIdentityId,
|
||||
skillRuntimeSnapshotId: skillRuntimeSnapshot?.id ?? null,
|
||||
},
|
||||
});
|
||||
@@ -148,6 +161,8 @@ export class TaskExecutionService {
|
||||
agentId: agent.id,
|
||||
providerId: provider.id,
|
||||
providerRequestId,
|
||||
executionContextId: isolation.executionContextId,
|
||||
serviceIdentityId: isolation.serviceIdentityId,
|
||||
skillRuntimeSnapshotId: skillRuntimeSnapshot?.id ?? null,
|
||||
errorMessage: message,
|
||||
},
|
||||
@@ -201,15 +216,30 @@ export class TaskExecutionService {
|
||||
return provider;
|
||||
}
|
||||
|
||||
private createRun(orgId: string, taskId: string, agentId: string, providerId: string): string {
|
||||
private createRun(orgId: string, taskId: string, agentId: string, providerId: string, isolation: TaskRunIsolation): string {
|
||||
const runId = createId("run");
|
||||
const now = new Date().toISOString();
|
||||
this.db.prepare(`
|
||||
insert into task_runs(
|
||||
id, org_id, task_id, agent_id, provider_id, provider_request_id,
|
||||
execution_context_id, service_identity_id, workspace_mount_json, secret_scope_json,
|
||||
trigger, state, created_at, started_at, completed_at, error_message
|
||||
) values (?, ?, ?, ?, ?, null, ?, ?, ?, ?, null, null)
|
||||
`).run(runId, orgId, taskId, agentId, providerId, "manual", TASK_RUN_STATE.RUNNING, now, now);
|
||||
) values (?, ?, ?, ?, ?, null, ?, ?, ?, ?, ?, ?, ?, ?, null, null)
|
||||
`).run(
|
||||
runId,
|
||||
orgId,
|
||||
taskId,
|
||||
agentId,
|
||||
providerId,
|
||||
isolation.executionContextId,
|
||||
isolation.serviceIdentityId,
|
||||
isolation.workspaceMountJson,
|
||||
isolation.secretScopeJson,
|
||||
"manual",
|
||||
TASK_RUN_STATE.RUNNING,
|
||||
now,
|
||||
now,
|
||||
);
|
||||
this.audit.record({
|
||||
orgId,
|
||||
entityType: "task_run",
|
||||
@@ -217,7 +247,7 @@ export class TaskExecutionService {
|
||||
actorType: "system",
|
||||
actorId: null,
|
||||
action: "started",
|
||||
payload: { taskId, agentId, providerId, trigger: "manual" },
|
||||
payload: { taskId, agentId, providerId, trigger: "manual", executionContextId: isolation.executionContextId, serviceIdentityId: isolation.serviceIdentityId },
|
||||
});
|
||||
return runId;
|
||||
}
|
||||
@@ -272,7 +302,44 @@ export class TaskExecutionService {
|
||||
|
||||
private workspace(workspaceId: string | null): WorkspaceRow | null {
|
||||
if (!workspaceId) return null;
|
||||
return (this.db.prepare("select name, local_path, repo_url, repo_ref from workspaces where id = ?").get(workspaceId) as unknown as WorkspaceRow | undefined) ?? null;
|
||||
return (this.db.prepare("select id, name, local_path, repo_url, repo_ref from workspaces where id = ?").get(workspaceId) as unknown as WorkspaceRow | undefined) ?? null;
|
||||
}
|
||||
|
||||
private createIsolationContext(
|
||||
orgId: string,
|
||||
task: TaskExecutionTaskRow,
|
||||
agent: TaskExecutionAgentRow,
|
||||
provider: TaskExecutionProviderRow,
|
||||
workspace: WorkspaceRow | null,
|
||||
): TaskRunIsolation {
|
||||
if (task.requires_filesystem === 1 && !workspace) throw new Error("Filesystem tasks require a workspace.");
|
||||
const serviceIdentityId = this.requireServiceIdentity(orgId, agent.id);
|
||||
const executionContextId = createId("ctx");
|
||||
const workspaceMounts = workspace ? [{
|
||||
workspaceId: workspace.id,
|
||||
name: workspace.name,
|
||||
localPath: workspace.local_path,
|
||||
repoRef: workspace.repo_ref,
|
||||
mode: task.requires_filesystem === 1 ? "read_write" : "read_only",
|
||||
}] : [];
|
||||
return {
|
||||
executionContextId,
|
||||
serviceIdentityId,
|
||||
workspaceMountJson: JSON.stringify(workspaceMounts),
|
||||
secretScopeJson: JSON.stringify({
|
||||
serviceIdentityId,
|
||||
providerIds: [provider.id],
|
||||
scope: "task_run",
|
||||
credentialAccess: "provider_runtime_only",
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
private requireServiceIdentity(orgId: string, agentId: string): string {
|
||||
const row = this.db.prepare("select id from service_identities where org_id = ? and agent_id = ?")
|
||||
.get(orgId, agentId) as { id: string } | undefined;
|
||||
if (!row) throw new Error("Agent service identity was not found.");
|
||||
return row.id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,6 +359,7 @@ function buildTaskPrompt(input: {
|
||||
project: ProjectRow | null;
|
||||
goal: GoalRow | null;
|
||||
workspace: WorkspaceRow | null;
|
||||
isolation: TaskRunIsolation;
|
||||
skillRuntimeSnapshot: SkillRuntimeSnapshotRow | null;
|
||||
}): string {
|
||||
const lines = [
|
||||
@@ -305,6 +373,8 @@ function buildTaskPrompt(input: {
|
||||
`Requires filesystem: ${input.task.requires_filesystem === 1 ? "yes" : "no"}`,
|
||||
`Assignee: ${input.agent.name} (${input.agent.role})`,
|
||||
`Provider: ${input.provider.label} ${input.provider.model ?? input.agent.model_id ?? ""}`.trim(),
|
||||
`Execution context: ${input.isolation.executionContextId}`,
|
||||
`Service identity: ${input.isolation.serviceIdentityId}`,
|
||||
];
|
||||
if (input.goal) lines.push("", `Goal: ${input.goal.title}`, `Goal detail: ${input.goal.description}`);
|
||||
if (input.project) lines.push("", `Project: ${input.project.name}`, `Project detail: ${input.project.description}`);
|
||||
@@ -315,8 +385,10 @@ function buildTaskPrompt(input: {
|
||||
`Local path: ${input.workspace.local_path}`,
|
||||
`Repo: ${input.workspace.repo_url}`,
|
||||
`Ref: ${input.workspace.repo_ref}`,
|
||||
`Mounts: ${input.isolation.workspaceMountJson}`,
|
||||
);
|
||||
}
|
||||
lines.push("", `Secret scope: ${input.isolation.secretScopeJson}`);
|
||||
if (input.skillRuntimeSnapshot) {
|
||||
lines.push(
|
||||
"",
|
||||
|
||||
Reference in new issue
Block a user