fix(agentstack): assignTask refuses tasks in a terminal status (#47)

This commit is contained in:
6D0N9 2026-06-14 07:54:08 +02:00 committed by GitHub
parent a4cc229120
commit 90fffb124e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 332 additions and 309 deletions

View file

@ -76,6 +76,26 @@ describe("AgentStack coordinator", () => {
expect(() => stack.updateTaskStatus(task.id, "running")).toThrow(/cancelled/);
});
it("refuses to assign a task that is already in a terminal status", () => {
const stack = new AgentStack();
stack.registerAgent(agent);
const other: AgentProfile = { ...agent, did: agentDid("xyz") };
stack.registerAgent(other);
const task = stack.createTask({
ownerDid: owner,
sourceApp: "ugig.net",
title: "Paid task",
paymentIntentId: "pi_1",
escrowId: "esc_1"
});
stack.assignTask(task.id, agent.did);
stack.updateTaskStatus(task.id, "complete", { reputationEventId: "rep_1" });
expect(() => stack.assignTask(task.id, other.did)).toThrow(/already complete and cannot be assigned/);
expect(stack.getTask(task.id)?.assigneeDid).toBe(agent.did);
expect(stack.getTask(task.id)?.status).toBe("complete");
});
it("records delegation grants and emits events", () => {
const stack = new AgentStack();
const listener = vi.fn();

View file

@ -113,6 +113,9 @@ export class AgentStack {
assignTask(taskId: string, agentDidValue: string): DidTask {
const task = this.requireTask(taskId);
if (TERMINAL.has(task.status)) {
throw new Error(`Task ${taskId} is already ${task.status} and cannot be assigned`);
}
if (!this.agents.has(agentDidValue)) {
throw new Error(`Unknown agent: ${agentDidValue}`);
}