test: cover agentId threading on plugin lease RPCs and call sites

Adds focused tests for every code path the agentId addition touches:

- environment-runtime.test.ts (4 new tests):
  - plugin-driver acquireLease forwards agentId in RPC payload when present
  - plugin-driver acquireLease omits agentId from RPC payload when null
  - sandbox-provider acquireLease forwards agentId when present
  - sandbox-provider resumeLease forwards agentId when reuseLease=true matches
  - seedEnvironment helper now exposes the seeded agentId

- environment-run-orchestrator.test.ts (2 new tests):
  - acquireForRun threads agentId through to runtime.acquireRunLease
  - logActivity records the same agentId on environment.lease_acquired
  - new vi.hoisted mocks for environmentService.getById + ensureLocalEnvironment

- agent-test-environment-routes.test.ts (1 new assertion):
  - ad-hoc operator test-environment probe calls acquireRunLease with
    agentId: null and heartbeatRunId: null (no agent context)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-13 18:52:28 -04:00
parent 73f4685729
commit 4c4eeaba2b
3 changed files with 380 additions and 2 deletions
@@ -10,6 +10,8 @@ const mockBuildWorkspaceRealizationRequest = vi.hoisted(() => vi.fn());
const mockUpdateLeaseMetadata = vi.hoisted(() => vi.fn());
const mockUpdateExecutionWorkspace = vi.hoisted(() => vi.fn());
const mockLogActivity = vi.hoisted(() => vi.fn());
const mockEnvironmentsEnsureLocal = vi.hoisted(() => vi.fn());
const mockEnvironmentsGetById = vi.hoisted(() => vi.fn());
vi.mock("../services/environment-execution-target.js", () => ({
resolveEnvironmentExecutionTarget: mockResolveEnvironmentExecutionTarget,
@@ -26,8 +28,8 @@ vi.mock("../services/workspace-realization.js", () => ({
vi.mock("../services/environments.js", () => ({
environmentService: vi.fn(() => ({
ensureLocalEnvironment: vi.fn(),
getById: vi.fn(),
ensureLocalEnvironment: mockEnvironmentsEnsureLocal,
getById: mockEnvironmentsGetById,
acquireLease: vi.fn(),
releaseLease: vi.fn(),
updateLeaseMetadata: mockUpdateLeaseMetadata,
@@ -548,3 +550,75 @@ describe("environmentRunOrchestrator — realizeForRun", () => {
expect(mockResolveEnvironmentExecutionTarget).not.toHaveBeenCalled();
});
});
describe("environmentRunOrchestrator — acquireForRun threads agentId", () => {
const mockDb = {} as any;
beforeEach(() => {
vi.clearAllMocks();
// selectedEnvironmentId !== defaultEnvironmentId in our inputs so the
// resolver goes through getById rather than ensureLocalEnvironment.
mockEnvironmentsGetById.mockResolvedValue(makeEnvironment("sandbox"));
mockResolveEnvironmentExecutionTarget.mockResolvedValue({
kind: "local",
environmentId: "env-1",
leaseId: "lease-1",
});
mockAdapterExecutionTargetToRemoteSpec.mockReturnValue(null);
});
function makeAcquireInput(overrides: { agentId?: string } = {}) {
return {
companyId: "company-1",
// distinct from defaultEnvironmentId so resolveEnvironment hits getById
selectedEnvironmentId: "env-1",
defaultEnvironmentId: "env-default",
adapterType: "claude_local",
issueId: null as string | null,
heartbeatRunId: "run-1",
agentId: overrides.agentId ?? "agent-uuid-abc",
persistedExecutionWorkspace: null,
};
}
it("passes agentId from acquireForRun's input through to runtime.acquireRunLease", async () => {
const runtime = makeMockRuntime({
acquireRunLease: vi.fn().mockResolvedValue({
lease: makeLease(),
leaseContext: { executionWorkspaceId: null, executionWorkspaceMode: null },
}),
});
const orchestrator = environmentRunOrchestrator(mockDb, { environmentRuntime: runtime });
await orchestrator.acquireForRun(makeAcquireInput({ agentId: "agent-uuid-abc" }));
expect(runtime.acquireRunLease).toHaveBeenCalledWith(
expect.objectContaining({
agentId: "agent-uuid-abc",
heartbeatRunId: "run-1",
companyId: "company-1",
}),
);
});
it("logs the lease-acquired activity with the same agentId it threads to the runtime", async () => {
const runtime = makeMockRuntime({
acquireRunLease: vi.fn().mockResolvedValue({
lease: makeLease(),
leaseContext: { executionWorkspaceId: null, executionWorkspaceMode: null },
}),
});
const orchestrator = environmentRunOrchestrator(mockDb, { environmentRuntime: runtime });
await orchestrator.acquireForRun(makeAcquireInput({ agentId: "agent-uuid-xyz" }));
expect(mockLogActivity).toHaveBeenCalledWith(
mockDb,
expect.objectContaining({
action: "environment.lease_acquired",
agentId: "agent-uuid-xyz",
actorId: "agent-uuid-xyz",
}),
);
});
});