Skip to content

Commit 7e98914

Browse files
committed
🤖 refactor: unify ancestor traversal in TaskService
1 parent 378804c commit 7e98914

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/node/services/taskService.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,25 @@ function buildParentAwaitSystemInstructions(activeTaskIds: string[]): string {
219219
"Once all are completed, write your final response integrating their reports."
220220
);
221221
}
222+
223+
function* iterateAncestorWorkspaceIds(
224+
parentById: Map<string, string>,
225+
taskId: string
226+
): Generator<string, void> {
227+
let current = taskId;
228+
for (let i = 0; i < 32; i++) {
229+
const parent = parentById.get(current);
230+
if (!parent) {
231+
return;
232+
}
233+
yield parent;
234+
current = parent;
235+
}
236+
237+
throw new Error(
238+
`iterateAncestorWorkspaceIds: possible parentWorkspaceId cycle starting at ${taskId}`
239+
);
240+
}
222241
function getIsoNow(): string {
223242
return new Date().toISOString();
224243
}
@@ -1374,17 +1393,13 @@ export class TaskService {
13741393
ancestorWorkspaceId: string,
13751394
taskId: string
13761395
): boolean {
1377-
let current = taskId;
1378-
for (let i = 0; i < 32; i++) {
1379-
const parent = parentById.get(current);
1380-
if (!parent) return false;
1381-
if (parent === ancestorWorkspaceId) return true;
1382-
current = parent;
1396+
for (const parent of iterateAncestorWorkspaceIds(parentById, taskId)) {
1397+
if (parent === ancestorWorkspaceId) {
1398+
return true;
1399+
}
13831400
}
13841401

1385-
throw new Error(
1386-
`isDescendantAgentTaskUsingParentById: possible parentWorkspaceId cycle starting at ${taskId}`
1387-
);
1402+
return false;
13881403
}
13891404

13901405
// --- Internal orchestration ---
@@ -1393,19 +1408,7 @@ export class TaskService {
13931408
parentById: Map<string, string>,
13941409
taskId: string
13951410
): string[] {
1396-
const ancestors: string[] = [];
1397-
1398-
let current = taskId;
1399-
for (let i = 0; i < 32; i++) {
1400-
const parent = parentById.get(current);
1401-
if (!parent) return ancestors;
1402-
ancestors.push(parent);
1403-
current = parent;
1404-
}
1405-
1406-
throw new Error(
1407-
`listAncestorWorkspaceIdsUsingParentById: possible parentWorkspaceId cycle starting at ${taskId}`
1408-
);
1411+
return [...iterateAncestorWorkspaceIds(parentById, taskId)];
14091412
}
14101413

14111414
private listAgentTaskWorkspaces(

0 commit comments

Comments
 (0)