@dibs.ai/dibs 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/package.json +78 -4
- package/schema.json +33 -0
- package/src/cli/index.ts +94 -0
- package/src/cli/mcp.ts +79 -0
- package/src/core/adapters/dex/README.md +40 -0
- package/src/core/adapters/dex/dex.schema.json +17 -0
- package/src/core/adapters/dex/index.ts +82 -0
- package/src/core/adapters/sync-all.ts +15 -0
- package/src/core/adapters/sync.ts +81 -0
- package/src/core/adapters/things/README.md +49 -0
- package/src/core/adapters/things/index.ts +73 -0
- package/src/core/adapters/things/things.schema.json +17 -0
- package/src/core/ctx.ts +6 -0
- package/src/core/identity.ts +19 -0
- package/src/core/panel.ts +39 -0
- package/src/core/store.ts +184 -0
- package/src/core/tasks.ts +150 -0
- package/src/core/todo.ts +80 -0
- package/src/core/util.ts +59 -0
package/src/core/todo.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
// One JSONL record per task. todo.txt fields get their own keys; +projects, @contexts and other key:value tags stay in name (todo.txt description).
|
|
4
|
+
export interface Task {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
completed: boolean;
|
|
8
|
+
priority?: string; // A-Z
|
|
9
|
+
created?: string; // YYYY-MM-DD
|
|
10
|
+
completed_date?: string; // YYYY-MM-DD
|
|
11
|
+
started_at: string | null;
|
|
12
|
+
description?: string;
|
|
13
|
+
parent?: string;
|
|
14
|
+
result?: string;
|
|
15
|
+
claimed_by?: string; // "<sessionId>:<pid>"; local only, never synced
|
|
16
|
+
created_by?: string; // agent name (pi, claude-code, cli, ...) or "@user" when a human created it
|
|
17
|
+
human?: string; // GitHub username (no @) of the human in the loop
|
|
18
|
+
blocked_by?: string[]; // ids of tasks that must complete first; local only
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const date = /^\d{4}-\d{2}-\d{2}$/;
|
|
22
|
+
const optional = ["priority", "created", "completed_date", "description", "parent", "result", "claimed_by", "created_by", "human"] as const;
|
|
23
|
+
|
|
24
|
+
// Validates a parsed JSONL record and returns it as a Task; unknown keys are rejected so typos never pass silently.
|
|
25
|
+
export function toTask(record: any): Task {
|
|
26
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) throw new Error("not an object");
|
|
27
|
+
for (const key of Object.keys(record)) if (!["id", "name", "completed", "started_at", "blocked_by", ...optional].includes(key)) throw new Error(`unknown key ${key}`);
|
|
28
|
+
if (typeof record.id !== "string" || !record.id) throw new Error("bad id");
|
|
29
|
+
if (typeof record.name !== "string" || !record.name.trim() || /[\r\n]/.test(record.name)) throw new Error("bad name");
|
|
30
|
+
if (typeof record.completed !== "boolean") throw new Error("bad completed");
|
|
31
|
+
if (record.started_at !== null && typeof record.started_at !== "string") throw new Error("bad started_at");
|
|
32
|
+
for (const key of optional) if (record[key] !== undefined && typeof record[key] !== "string") throw new Error(`bad ${key}`);
|
|
33
|
+
if (record.blocked_by !== undefined && (!Array.isArray(record.blocked_by) || record.blocked_by.some((id: any) => typeof id !== "string"))) throw new Error("bad blocked_by");
|
|
34
|
+
if (record.priority !== undefined && !/^[A-Z]$/.test(record.priority)) throw new Error("bad priority");
|
|
35
|
+
for (const key of ["created", "completed_date"] as const) if (record[key] !== undefined && !date.test(record[key])) throw new Error(`bad ${key}`);
|
|
36
|
+
return record;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Drops empty optional fields and writes keys in schema order, so records stay minimal and stable.
|
|
40
|
+
const order = ["id", "name", "completed", "priority", "created", "completed_date", "started_at", "description", "parent", "result", "claimed_by", "created_by", "human", "blocked_by"];
|
|
41
|
+
export const clean = (task: Task): Task => Object.fromEntries(Object.entries(task).sort(([a], [b]) => order.indexOf(a) - order.indexOf(b)).filter(([key, value]) => value !== undefined && (value !== "" || key === "name") && !(Array.isArray(value) && !value.length))) as Task;
|
|
42
|
+
|
|
43
|
+
export function parseTodo(line: string): Task {
|
|
44
|
+
const meta: Record<string, string> = {};
|
|
45
|
+
let body = line.replace(/(?:^|\s)(dibsid|dibsstart|dibsdesc|dibsparent|dibsresult|dibsby|dibshuman|pri):([^\s:]+)/g, (match, key, value) => {
|
|
46
|
+
if (key === "pri" && !/^[A-Z]$/.test(value)) return match;
|
|
47
|
+
try { meta[key] = decodeURIComponent(value); return ""; } catch { return match; }
|
|
48
|
+
}).trim();
|
|
49
|
+
const done = body.match(/^x\s+(?:(\d{4}-\d{2}-\d{2})\s+)?/);
|
|
50
|
+
if (done) body = body.slice(done[0].length);
|
|
51
|
+
const pri = done ? undefined : body.match(/^\(([A-Z])\)\s+/);
|
|
52
|
+
if (pri) body = body.slice(pri[0].length);
|
|
53
|
+
const created = body.match(/^(\d{4}-\d{2}-\d{2})\s+/);
|
|
54
|
+
if (created) body = body.slice(created[0].length);
|
|
55
|
+
return toTask(clean({
|
|
56
|
+
id: meta.dibsid || `line-${createHash("sha256").update(line).digest("hex").slice(0, 12)}`,
|
|
57
|
+
name: body, completed: !!done, priority: pri?.[1] ?? meta.pri, created: created?.[1], completed_date: done?.[1],
|
|
58
|
+
started_at: meta.dibsstart || null, description: meta.dibsdesc, parent: meta.dibsparent, result: meta.dibsresult, created_by: meta.dibsby, human: meta.dibshuman,
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Renders a task as a todo.txt line (export). Completed tasks move priority to pri: per the todo.txt spec.
|
|
63
|
+
export function todoLine(task: Task): string {
|
|
64
|
+
const head = task.completed ? ["x", task.completed_date] : [task.priority && `(${task.priority})`];
|
|
65
|
+
const tags = Object.entries({ pri: task.completed ? task.priority : undefined, dibsid: task.id, dibsstart: task.started_at, dibsdesc: task.description, dibsparent: task.parent, dibsresult: task.result, dibsby: task.created_by, dibshuman: task.human })
|
|
66
|
+
.filter(([, value]) => typeof value === "string" && value !== "").map(([key, value]) => `${key}:${encodeURIComponent(value as string)}`);
|
|
67
|
+
const line = [...head, task.created, task.name.trim(), ...tags].filter(Boolean).join(" ");
|
|
68
|
+
// todo.txt has no escape, so a name that reads back as a marker (x, (A), date) must be refused rather than exported wrong.
|
|
69
|
+
const back = parseTodo(line);
|
|
70
|
+
if (back.name !== task.name.trim() || back.completed !== task.completed) throw new Error(`Task ${task.id} name cannot be exported to todo.txt without changing it: ${task.name}`);
|
|
71
|
+
return line;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Names must survive a todo.txt export/import round trip and must not reach Dex as a flag.
|
|
75
|
+
export function checkName(name: string) {
|
|
76
|
+
if (/[\r\n]/.test(name) || !name.trim()) throw new Error("Task name must be a non-empty single line");
|
|
77
|
+
if (/^\s*x\s/.test(name)) throw new Error('Task name must not start with "x " (todo.txt completion marker)');
|
|
78
|
+
if (/^\s*-/.test(name)) throw new Error('Task name must not start with "-"');
|
|
79
|
+
if (/(?:^|\s)dibs(?:id|start|desc|parent|result|by|human):/.test(name)) throw new Error("Task name must not contain dibs metadata tokens (dibsid:, dibsstart:, dibsdesc:, dibsparent:, dibsresult:, dibsby:, dibshuman:)");
|
|
80
|
+
}
|
package/src/core/util.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { linkSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
export const home = (path: string) => path === "~" ? homedir() : path.startsWith("~/") ? join(homedir(), path.slice(2)) : path;
|
|
7
|
+
|
|
8
|
+
export function isAlive(pid: number): boolean {
|
|
9
|
+
try { process.kill(pid, 0); return true; }
|
|
10
|
+
catch (error: any) { return error.code !== "ESRCH"; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A lock holds "<pid>:<token>"; it is stale when that pid is dead. tries * 25ms is the wait limit (~11s).
|
|
14
|
+
export async function withLock<T>(file: string, fn: () => T | Promise<T>, tries = 440): Promise<T> {
|
|
15
|
+
const lock = `${file}.lock`;
|
|
16
|
+
const mine = `${process.pid}:${randomUUID()}`;
|
|
17
|
+
for (let i = 0; ; i++) {
|
|
18
|
+
try { writeFileSync(lock, mine, { flag: "wx" }); break; }
|
|
19
|
+
catch (error: any) {
|
|
20
|
+
if (error.code !== "EEXIST") throw error;
|
|
21
|
+
let seen: string, pid: number | undefined;
|
|
22
|
+
try {
|
|
23
|
+
seen = readFileSync(lock, "utf8");
|
|
24
|
+
pid = /^(\d+):./.test(seen) ? Number(seen.split(":")[0]) : undefined;
|
|
25
|
+
// an empty or malformed lock may be mid-write: stale only once it is 5s old.
|
|
26
|
+
if (pid === undefined ? Date.now() - statSync(lock).mtimeMs > 5_000 : !isAlive(pid)) {
|
|
27
|
+
// rename is atomic, so one waiter wins; if it moved a new holder's lock, put it back.
|
|
28
|
+
const stale = `${lock}.${randomUUID()}.stale`;
|
|
29
|
+
renameSync(lock, stale);
|
|
30
|
+
try { if (readFileSync(stale, "utf8") !== seen) linkSync(stale, lock); } catch {}
|
|
31
|
+
try { unlinkSync(stale); } catch {}
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
if (i >= tries) throw new Error(`Task file is locked: ${lock} (held by pid ${pid ?? "unknown"}). If that pid is not a dibs process, delete the file.`);
|
|
36
|
+
await new Promise((r) => setTimeout(r, 25));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
try { return await fn(); }
|
|
40
|
+
finally { try { if (readFileSync(lock, "utf8") === mine) unlinkSync(lock); } catch {} }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// check validates each record and gives it its type; it throws on a bad record.
|
|
44
|
+
export function readJsonl<T>(path: string, check: (record: Record<string, unknown>) => T): T[] {
|
|
45
|
+
let data: string;
|
|
46
|
+
try { data = readFileSync(path, "utf8"); }
|
|
47
|
+
catch (error: any) { if (error.code === "ENOENT") return []; throw new Error(`Cannot read ${path}: ${error.message}`); }
|
|
48
|
+
const lines = data.split("\n").filter((line) => line.trim());
|
|
49
|
+
return lines.flatMap((line, index) => {
|
|
50
|
+
let record: any;
|
|
51
|
+
try { record = JSON.parse(line); } catch {
|
|
52
|
+
// A crash mid-append leaves an unterminated last line; drop it rather than refuse the whole file.
|
|
53
|
+
if (index === lines.length - 1 && !data.endsWith("\n")) return [];
|
|
54
|
+
throw new Error(`Invalid JSON on line ${index + 1} of ${path}`);
|
|
55
|
+
}
|
|
56
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) throw new Error(`Invalid record on line ${index + 1} of ${path}`);
|
|
57
|
+
try { return [check(record)]; } catch (error: any) { throw new Error(`Invalid record on line ${index + 1} of ${path}: ${error.message}`); }
|
|
58
|
+
});
|
|
59
|
+
}
|