Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unify-delegation-strategy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@perstack/runtime": patch
---

Unify delegation strategy into single DelegationExecutor class
84 changes: 0 additions & 84 deletions packages/runtime/src/helpers/checkpoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Checkpoint, RunSetting } from "@perstack/core"
import { describe, expect, it, vi } from "vitest"
import {
buildDelegateToState,
buildDelegationReturnState,
createInitialCheckpoint,
createNextStepCheckpoint,
Expand Down Expand Up @@ -218,86 +217,3 @@ describe("@perstack/runtime: buildDelegationReturnState", () => {
warnSpy.mockRestore()
})
})

describe("@perstack/runtime: buildDelegateToState", () => {
const baseSetting = {
jobId: "job-123",
runId: "run-123",
model: "claude-sonnet-4-20250514",
providerConfig: { providerName: "anthropic" as const, apiKey: "test-key" },
expertKey: "parent-expert",
input: { text: "parent query" },
experts: {},
reasoningBudget: "low",
maxSteps: 100,
maxRetries: 3,
timeout: 30000,
startedAt: 1000,
updatedAt: 2000,
perstackApiBaseUrl: "https://api.perstack.dev",
env: {},
} satisfies RunSetting
const resultCheckpoint: Checkpoint = {
id: "parent-checkpoint-id",
jobId: "job-123",
runId: "run-123",
expert: { key: "parent-expert", name: "parent", version: "1.0.0" },
stepNumber: 3,
status: "stoppedByDelegate",
messages: [
{ id: "m1", type: "userMessage", contents: [{ id: "c1", type: "textPart", text: "hello" }] },
],
usage: {
inputTokens: 100,
outputTokens: 50,
reasoningTokens: 0,
totalTokens: 150,
cachedInputTokens: 0,
},
delegateTo: [
{
expert: { key: "child-expert", name: "child", version: "2.0.0" },
toolCallId: "tool-call-456",
toolName: "delegateToChild",
query: "please do this",
},
],
}
const currentExpert = { key: "parent-expert", name: "parent", version: "1.0.0" }

it("builds correct setting for delegate target", () => {
const result = buildDelegateToState(baseSetting, resultCheckpoint, currentExpert)
expect(result.setting.expertKey).toBe("child-expert")
expect(result.setting.input).toEqual({ text: "please do this" })
})

it("builds checkpoint with delegate target expert and delegatedBy", () => {
const result = buildDelegateToState(baseSetting, resultCheckpoint, currentExpert)
expect(result.checkpoint.status).toBe("init")
expect(result.checkpoint.messages).toEqual([])
expect(result.checkpoint.expert).toEqual({
key: "child-expert",
name: "child",
version: "2.0.0",
})
expect(result.checkpoint.delegatedBy).toEqual({
expert: { key: "parent-expert", name: "parent", version: "1.0.0" },
toolCallId: "tool-call-456",
toolName: "delegateToChild",
checkpointId: "parent-checkpoint-id",
runId: "run-123",
})
})

it("preserves usage from result checkpoint", () => {
const result = buildDelegateToState(baseSetting, resultCheckpoint, currentExpert)
expect(result.checkpoint.usage).toEqual(resultCheckpoint.usage)
})

it("throws when delegateTo is missing", () => {
const checkpointWithoutDelegateTo = { ...resultCheckpoint, delegateTo: undefined }
expect(() =>
buildDelegateToState(baseSetting, checkpointWithoutDelegateTo, currentExpert),
).toThrow("delegateTo is required")
})
})
46 changes: 0 additions & 46 deletions packages/runtime/src/helpers/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,49 +97,3 @@ export function buildDelegationReturnState(
},
}
}

export function buildDelegateToState(
currentSetting: RunSetting,
resultCheckpoint: Checkpoint,
currentExpert: Pick<Expert, "key" | "name" | "version">,
): DelegationStateResult {
const { delegateTo } = resultCheckpoint
if (!delegateTo || delegateTo.length === 0) {
throw new Error("delegateTo is required for buildDelegateToState")
}
const firstDelegation = delegateTo[0]
const { expert, toolCallId, toolName, query } = firstDelegation
return {
setting: {
...currentSetting,
expertKey: expert.key,
input: {
text: query,
},
},
checkpoint: {
...resultCheckpoint,
status: "init",
messages: [],
expert: {
key: expert.key,
name: expert.name,
version: expert.version,
},
delegatedBy: {
expert: {
key: currentExpert.key,
name: currentExpert.name,
version: currentExpert.version,
},
toolCallId,
toolName,
checkpointId: resultCheckpoint.id,
runId: currentSetting.runId,
},
usage: resultCheckpoint.usage,
pendingToolCalls: undefined,
partialToolResults: undefined,
},
}
}
Loading