klaus-agent 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,270 @@
1
+ // Task graph — dependency-aware DAG with background execution and auto-unlock
2
+
3
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, renameSync } from "fs";
4
+ import { join } from "path";
5
+ import { generateId } from "../utils/id.js";
6
+ import type { TaskNode, TaskStatus, TaskGraphConfig, CompletedTaskResult } from "./types.js";
7
+
8
+ export class TaskGraph {
9
+ private _tasks = new Map<string, TaskNode>();
10
+ private _config: TaskGraphConfig;
11
+ private _completedQueue: CompletedTaskResult[] = [];
12
+ private _backgroundAborts = new Map<string, AbortController>();
13
+
14
+ constructor(config: TaskGraphConfig = {}) {
15
+ this._config = config;
16
+ if (config.persistDir) {
17
+ mkdirSync(config.persistDir, { recursive: true });
18
+ this._loadFromDisk();
19
+ }
20
+ }
21
+
22
+
23
+ get(id: string): TaskNode | undefined {
24
+ const task = this._tasks.get(id);
25
+ return task ? { ...task } : undefined;
26
+ }
27
+
28
+ listAll(): TaskNode[] {
29
+ return [...this._tasks.values()];
30
+ }
31
+
32
+ /** Tasks that are pending with no unfinished blockers. */
33
+ listReady(): TaskNode[] {
34
+ return this.listAll().filter(
35
+ (t) => t.status === "pending" && t.blockedBy.length === 0,
36
+ );
37
+ }
38
+
39
+ /** Tasks waiting on unfinished blockers. */
40
+ listBlocked(): TaskNode[] {
41
+ return this.listAll().filter(
42
+ (t) => t.status === "pending" && t.blockedBy.length > 0,
43
+ );
44
+ }
45
+
46
+
47
+ create(subject: string, description = ""): TaskNode {
48
+ const max = this._config.maxTasks ?? 100;
49
+ if (this._tasks.size >= max) {
50
+ throw new Error(`Task limit reached: ${max}.`);
51
+ }
52
+
53
+ const node: TaskNode = {
54
+ id: generateId(),
55
+ subject,
56
+ description,
57
+ status: "pending",
58
+ blockedBy: [],
59
+ blocks: [],
60
+ owner: "",
61
+ createdAt: Date.now(),
62
+ updatedAt: Date.now(),
63
+ };
64
+ this._tasks.set(node.id, node);
65
+ this._persist();
66
+ return { ...node };
67
+ }
68
+
69
+ /** Add a dependency: `taskId` is blocked by `blockedById`. */
70
+ addDependency(taskId: string, blockedById: string): void {
71
+ const task = this._require(taskId);
72
+ const blocker = this._require(blockedById);
73
+
74
+ if (task.blockedBy.includes(blockedById)) return;
75
+
76
+ // Cycle detection: if blocker is (transitively) blocked by task, adding this edge creates a cycle
77
+ if (this._isTransitivelyBlockedBy(blockedById, taskId)) {
78
+ throw new Error(`Adding dependency ${blockedById} → ${taskId} would create a cycle.`);
79
+ }
80
+
81
+ task.blockedBy.push(blockedById);
82
+ blocker.blocks.push(taskId);
83
+ task.updatedAt = Date.now();
84
+ blocker.updatedAt = Date.now();
85
+ this._persist();
86
+ }
87
+
88
+ update(taskId: string, fields: { status?: TaskStatus; owner?: string; result?: string }): TaskNode {
89
+ const task = this._require(taskId);
90
+
91
+ if (fields.status !== undefined && fields.status !== task.status) {
92
+ if (fields.status === "in_progress" && task.blockedBy.length > 0) {
93
+ throw new Error(`Task ${taskId} is blocked by: ${task.blockedBy.join(", ")}`);
94
+ }
95
+ task.status = fields.status;
96
+
97
+ if (fields.status === "completed" || fields.status === "failed") {
98
+ task.result = fields.result ?? task.result;
99
+ const unblocked = this._clearDependency(taskId);
100
+
101
+ this._completedQueue.push({
102
+ taskId,
103
+ subject: task.subject,
104
+ result: task.result ?? "",
105
+ status: fields.status,
106
+ unblockedTasks: unblocked,
107
+ });
108
+ }
109
+ }
110
+
111
+ if (fields.owner !== undefined) task.owner = fields.owner;
112
+ // result is already set inside the completion branch above; only apply here for non-completion updates
113
+ if (fields.result !== undefined && task.status !== "completed" && task.status !== "failed") {
114
+ task.result = fields.result;
115
+ }
116
+ task.updatedAt = Date.now();
117
+ this._persist();
118
+ return { ...task };
119
+ }
120
+
121
+
122
+ /**
123
+ * Run an async function in the background for a task.
124
+ * Auto-updates task status to in_progress/completed/failed.
125
+ */
126
+ runBackground(
127
+ taskId: string,
128
+ fn: (signal: AbortSignal) => Promise<string>,
129
+ ): void {
130
+ const task = this._require(taskId);
131
+ if (task.blockedBy.length > 0) {
132
+ throw new Error(`Task ${taskId} is blocked by: ${task.blockedBy.join(", ")}`);
133
+ }
134
+
135
+ const ac = new AbortController();
136
+ const bgId = generateId();
137
+ task.status = "in_progress";
138
+ task.backgroundId = bgId;
139
+ task.updatedAt = Date.now();
140
+ this._backgroundAborts.set(bgId, ac);
141
+ this._persist();
142
+
143
+ fn(ac.signal).then(
144
+ (result) => {
145
+ this._backgroundAborts.delete(bgId);
146
+ try { this.update(taskId, { status: "completed", result }); } catch { /* task may have been removed */ }
147
+ },
148
+ (err) => {
149
+ this._backgroundAborts.delete(bgId);
150
+ try {
151
+ this.update(taskId, {
152
+ status: "failed",
153
+ result: err instanceof Error ? err.message : String(err),
154
+ });
155
+ } catch { /* task may have been removed */ }
156
+ },
157
+ );
158
+ }
159
+
160
+ abortBackground(taskId: string): boolean {
161
+ const task = this._tasks.get(taskId);
162
+ if (!task?.backgroundId) return false;
163
+ const ac = this._backgroundAborts.get(task.backgroundId);
164
+ if (!ac) return false;
165
+ ac.abort();
166
+ return true;
167
+ }
168
+
169
+
170
+ drainCompleted(): CompletedTaskResult[] {
171
+ const results = [...this._completedQueue];
172
+ this._completedQueue = [];
173
+ return results;
174
+ }
175
+
176
+
177
+ render(): string {
178
+ const tasks = this.listAll();
179
+ if (tasks.length === 0) return "No tasks.";
180
+
181
+ const lines = tasks.map((t) => {
182
+ const icon = t.status === "completed" ? "[x]"
183
+ : t.status === "failed" ? "[!]"
184
+ : t.status === "in_progress" ? "[>]"
185
+ : t.blockedBy.length > 0 ? "[~]"
186
+ : "[ ]";
187
+ const deps = t.blockedBy.length > 0 ? ` (blocked by: ${t.blockedBy.join(", ")})` : "";
188
+ return `${icon} ${t.id}: ${t.subject}${deps}`;
189
+ });
190
+
191
+ const done = tasks.filter((t) => t.status === "completed").length;
192
+ const ready = tasks.filter((t) => t.status === "pending" && t.blockedBy.length === 0).length;
193
+ return `Tasks: ${done}/${tasks.length} done, ${ready} ready\n${lines.join("\n")}`;
194
+ }
195
+
196
+ dispose(): void {
197
+ for (const ac of this._backgroundAborts.values()) {
198
+ ac.abort();
199
+ }
200
+ this._backgroundAborts.clear();
201
+ }
202
+
203
+
204
+ private _require(id: string): TaskNode {
205
+ const task = this._tasks.get(id);
206
+ if (!task) throw new Error(`Task not found: ${id}`);
207
+ return task;
208
+ }
209
+
210
+ /** Remove completedId from all tasks' blockedBy. Returns IDs of newly unblocked tasks. */
211
+ private _clearDependency(completedId: string): string[] {
212
+ const completed = this._tasks.get(completedId);
213
+ if (!completed) return [];
214
+
215
+ const unblocked: string[] = [];
216
+ for (const dependentId of completed.blocks) {
217
+ const task = this._tasks.get(dependentId);
218
+ if (!task) continue;
219
+ const idx = task.blockedBy.indexOf(completedId);
220
+ if (idx !== -1) {
221
+ task.blockedBy.splice(idx, 1);
222
+ if (task.blockedBy.length === 0 && task.status === "pending") {
223
+ unblocked.push(task.id);
224
+ }
225
+ }
226
+ }
227
+ return unblocked;
228
+ }
229
+
230
+ /** Check if `taskId` is transitively blocked by `targetId`. */
231
+ private _isTransitivelyBlockedBy(taskId: string, targetId: string): boolean {
232
+ const visited = new Set<string>();
233
+ const stack = [taskId];
234
+ while (stack.length > 0) {
235
+ const current = stack.pop()!;
236
+ if (current === targetId) return true;
237
+ if (visited.has(current)) continue;
238
+ visited.add(current);
239
+ const node = this._tasks.get(current);
240
+ if (node) stack.push(...node.blockedBy);
241
+ }
242
+ return false;
243
+ }
244
+
245
+ private _persist(): void {
246
+ if (!this._config.persistDir) return;
247
+ const data = JSON.stringify([...this._tasks.values()], null, 2);
248
+ const target = join(this._config.persistDir, "tasks.json");
249
+ const tmp = target + ".tmp";
250
+ writeFileSync(tmp, data, "utf-8");
251
+ renameSync(tmp, target);
252
+ }
253
+
254
+ private _loadFromDisk(): void {
255
+ if (!this._config.persistDir) return;
256
+ const filePath = join(this._config.persistDir, "tasks.json");
257
+ if (!existsSync(filePath)) return;
258
+ try {
259
+ const raw = JSON.parse(readFileSync(filePath, "utf-8"));
260
+ if (!Array.isArray(raw)) return;
261
+ for (const entry of raw) {
262
+ if (entry && typeof entry === "object" && typeof entry.id === "string" && typeof entry.subject === "string") {
263
+ this._tasks.set(entry.id, entry as TaskNode);
264
+ }
265
+ }
266
+ } catch {
267
+ // Corrupted file — start fresh
268
+ }
269
+ }
270
+ }
@@ -0,0 +1,109 @@
1
+ // Task graph tools — CRUD + dependency management + background execution
2
+
3
+ import { Type } from "@sinclair/typebox";
4
+ import type { AgentTool, AgentToolResult } from "../tools/types.js";
5
+ import type { TaskGraph } from "./task-graph.js";
6
+ import { TASK_GRAPH_TOOL_NAMES } from "./types.js";
7
+ import type { TaskStatus, TaskNode } from "./types.js";
8
+
9
+ export function createTaskGraphTools(graph: TaskGraph): AgentTool[] {
10
+ return [
11
+ {
12
+ name: TASK_GRAPH_TOOL_NAMES.create,
13
+ label: "Create Task",
14
+ description:
15
+ "Create a new task in the task graph. Tasks start as pending. " +
16
+ "Use task_depend to set up dependency ordering.",
17
+ parameters: Type.Object({
18
+ subject: Type.String({ description: "Short title for the task." }),
19
+ description: Type.Optional(Type.String({ description: "Detailed description." })),
20
+ }),
21
+ async execute(_id, params: { subject: string; description?: string }): Promise<AgentToolResult> {
22
+ const task = graph.create(params.subject, params.description);
23
+ return text(`Created task ${task.id}: ${task.subject}\n\n${graph.render()}`);
24
+ },
25
+ },
26
+ {
27
+ name: TASK_GRAPH_TOOL_NAMES.depend,
28
+ label: "Add Task Dependency",
29
+ description:
30
+ "Add a dependency: task_id cannot start until blocked_by_id completes. " +
31
+ "Rejects if this would create a cycle.",
32
+ parameters: Type.Object({
33
+ task_id: Type.String({ description: "Task that is blocked." }),
34
+ blocked_by_id: Type.String({ description: "Task that must complete first." }),
35
+ }),
36
+ async execute(_id, params: { task_id: string; blocked_by_id: string }): Promise<AgentToolResult> {
37
+ graph.addDependency(params.task_id, params.blocked_by_id);
38
+ return text(`Dependency added: ${params.task_id} blocked by ${params.blocked_by_id}\n\n${graph.render()}`);
39
+ },
40
+ },
41
+ {
42
+ name: TASK_GRAPH_TOOL_NAMES.update,
43
+ label: "Update Task",
44
+ description:
45
+ "Update a task's status, owner, or result. " +
46
+ "Setting status to 'completed' auto-unblocks dependent tasks. " +
47
+ "Cannot start a task that has unfinished blockers.",
48
+ parameters: Type.Object({
49
+ task_id: Type.String({ description: "Task ID." }),
50
+ status: Type.Optional(Type.Union(
51
+ [Type.Literal("pending"), Type.Literal("in_progress"), Type.Literal("completed"), Type.Literal("failed")],
52
+ { description: "New status." },
53
+ )),
54
+ owner: Type.Optional(Type.String({ description: "Assign to an agent or user." })),
55
+ result: Type.Optional(Type.String({ description: "Result summary." })),
56
+ }),
57
+ async execute(_id, params: { task_id: string; status?: string; owner?: string; result?: string }): Promise<AgentToolResult> {
58
+ const task = graph.update(params.task_id, {
59
+ status: params.status as TaskStatus | undefined,
60
+ owner: params.owner,
61
+ result: params.result,
62
+ });
63
+ return text(`Updated task ${task.id}\n\n${graph.render()}`);
64
+ },
65
+ },
66
+ {
67
+ name: TASK_GRAPH_TOOL_NAMES.list,
68
+ label: "List Tasks",
69
+ description: "List all tasks with their status, dependencies, and progress.",
70
+ parameters: Type.Object({
71
+ filter: Type.Optional(Type.Union(
72
+ [Type.Literal("all"), Type.Literal("ready"), Type.Literal("blocked"), Type.Literal("in_progress"), Type.Literal("completed")],
73
+ { description: "Filter tasks by category. Default: all." },
74
+ )),
75
+ }),
76
+ async execute(_id, params: { filter?: string }): Promise<AgentToolResult> {
77
+ let tasks: TaskNode[];
78
+ switch (params.filter) {
79
+ case "ready": tasks = graph.listReady(); break;
80
+ case "blocked": tasks = graph.listBlocked(); break;
81
+ case "in_progress": tasks = graph.listAll().filter((t) => t.status === "in_progress"); break;
82
+ case "completed": tasks = graph.listAll().filter((t) => t.status === "completed"); break;
83
+ default: tasks = graph.listAll();
84
+ }
85
+ if (tasks.length === 0) return text(`No tasks matching filter: ${params.filter ?? "all"}`);
86
+ if (!params.filter || params.filter === "all") return text(graph.render());
87
+ const lines = tasks.map((t) => `${t.id}: ${t.subject} [${t.status}]`);
88
+ return text(`${params.filter}: ${tasks.length} task(s)\n${lines.join("\n")}`);
89
+ },
90
+ },
91
+ {
92
+ name: TASK_GRAPH_TOOL_NAMES.get,
93
+ label: "Get Task",
94
+ description: "Get detailed information about a specific task.",
95
+ parameters: Type.Object({
96
+ task_id: Type.String({ description: "Task ID." }),
97
+ }),
98
+ async execute(_id, params: { task_id: string }): Promise<AgentToolResult> {
99
+ const task = graph.get(params.task_id);
100
+ if (!task) return text(`Task not found: ${params.task_id}`);
101
+ return text(JSON.stringify(task, null, 2));
102
+ },
103
+ },
104
+ ];
105
+ }
106
+
107
+ function text(t: string): AgentToolResult {
108
+ return { content: [{ type: "text", text: t }] };
109
+ }
@@ -0,0 +1,52 @@
1
+ // Task graph types — dependency-aware task DAG with background execution
2
+
3
+ export const TASK_GRAPH_TOOL_NAMES = {
4
+ create: "task_create",
5
+ depend: "task_depend",
6
+ update: "task_update",
7
+ list: "task_list",
8
+ get: "task_get",
9
+ } as const;
10
+
11
+ export type TaskStatus = "pending" | "in_progress" | "completed" | "failed";
12
+
13
+ export interface TaskNode {
14
+ id: string;
15
+ subject: string;
16
+ description: string;
17
+ status: TaskStatus;
18
+ /** IDs of tasks that must complete before this one can start. */
19
+ blockedBy: string[];
20
+ /** IDs of tasks that this task blocks (reverse edges, maintained automatically). */
21
+ blocks: string[];
22
+ /** Agent or user assigned to this task. */
23
+ owner: string;
24
+ /** Result summary after completion/failure. */
25
+ result?: string;
26
+ /** Background execution handle ID, if running in background. */
27
+ backgroundId?: string;
28
+ createdAt: number;
29
+ updatedAt: number;
30
+ }
31
+
32
+ export interface TaskGraphConfig {
33
+ /** Directory for persisting task graph to disk. If omitted, in-memory only. */
34
+ persistDir?: string;
35
+
36
+ /** Maximum number of tasks. Default: 100. */
37
+ maxTasks?: number;
38
+
39
+ /**
40
+ * Auto-inject completed background task results before each LLM call.
41
+ * Default: true.
42
+ */
43
+ autoInjectResults?: boolean;
44
+ }
45
+
46
+ export interface CompletedTaskResult {
47
+ taskId: string;
48
+ subject: string;
49
+ result: string;
50
+ status: "completed" | "failed";
51
+ unblockedTasks: string[];
52
+ }