Reject unknown task assignees (#80)

This commit is contained in:
phucnguyen1707 2026-06-16 17:19:08 +07:00 committed by GitHub
parent e38688f16e
commit 41ab662afa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 17 deletions

View file

@ -129,16 +129,29 @@ describe("AgentStack coordinator", () => {
expect(listener).toHaveBeenCalledWith(expect.objectContaining({ type: "delegation.granted" })); expect(listener).toHaveBeenCalledWith(expect.objectContaining({ type: "delegation.granted" }));
}); });
it("rejects unknown agents and invalid DIDs", () => { it("rejects unknown agents and invalid DIDs", () => {
const stack = new AgentStack(); const stack = new AgentStack();
expect(() => stack.createTask({ ownerDid: "nope", sourceApp: "x", title: "t" })).toThrow(); expect(() => stack.createTask({ ownerDid: "nope", sourceApp: "x", title: "t" })).toThrow();
const task = stack.createTask({ ownerDid: owner, sourceApp: "x", title: "t" }); const task = stack.createTask({ ownerDid: owner, sourceApp: "x", title: "t" });
expect(() => stack.assignTask(task.id, agentDid("ghost"))).toThrow(/Unknown agent/); expect(() => stack.assignTask(task.id, agentDid("ghost"))).toThrow(/Unknown agent/);
}); });
it("filters tasks in snapshots", () => { it("rejects tasks preassigned to unknown agents", () => {
const stack = new AgentStack(); const stack = new AgentStack();
stack.createTask({ ownerDid: owner, sourceApp: "x", title: "a" }); expect(() =>
stack.createTask({
ownerDid: owner,
sourceApp: "ugig.net",
title: "Ghost assignment",
assigneeDid: agentDid("ghost")
})
).toThrow(/Unknown agent/);
expect(stack.listTasks({ assigneeDid: agentDid("ghost") })).toHaveLength(0);
});
it("filters tasks in snapshots", () => {
const stack = new AgentStack();
stack.createTask({ ownerDid: owner, sourceApp: "x", title: "a" });
stack.createTask({ ownerDid: userDid("999"), sourceApp: "x", title: "b" }); stack.createTask({ ownerDid: userDid("999"), sourceApp: "x", title: "b" });
expect(stack.listTasks({ ownerDid: owner })).toHaveLength(1); expect(stack.listTasks({ ownerDid: owner })).toHaveLength(1);
expect(stack.snapshot().tasks).toHaveLength(2); expect(stack.snapshot().tasks).toHaveLength(2);

View file

@ -89,13 +89,16 @@ export class AgentStack {
return this.agents.get(did); return this.agents.get(did);
} }
createTask(input: CreateTaskInput): DidTask { createTask(input: CreateTaskInput): DidTask {
if (!parseDid(input.ownerDid)) { if (!parseDid(input.ownerDid)) {
throw new Error(`Invalid owner DID: ${input.ownerDid}`); throw new Error(`Invalid owner DID: ${input.ownerDid}`);
} }
const ts = this.now(); if (input.assigneeDid && !this.agents.has(input.assigneeDid)) {
const task: DidTask = { throw new Error(`Unknown agent: ${input.assigneeDid}`);
id: this.nextId("task"), }
const ts = this.now();
const task: DidTask = {
id: this.nextId("task"),
ownerDid: input.ownerDid, ownerDid: input.ownerDid,
assigneeDid: input.assigneeDid, assigneeDid: input.assigneeDid,
sourceApp: input.sourceApp, sourceApp: input.sourceApp,