@pal-code/workflow 0.0.0-stage → 0.0.19
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/README.md +193 -2
- package/package.json +21 -4
- package/src/backlog.ts +261 -0
- package/src/config.ts +93 -0
- package/src/doc.ts +224 -0
- package/src/health.ts +223 -0
- package/src/index.ts +411 -0
- package/src/work.ts +325 -0
package/src/work.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Work folders: one folder per multi-session task, with a README that survives
|
|
3
|
+
* a session reset.
|
|
4
|
+
*
|
|
5
|
+
* The invariant that keeps this honest: a work folder is identified by its
|
|
6
|
+
* DIRECTORY NAME (a dated slug), never by its title. A slug is stable, sortable
|
|
7
|
+
* and unambiguous on a command line; a title is prose the agent will reword.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here is ever deleted — `done` is a status, not a removal — because
|
|
10
|
+
* the point of the folder is to be readable after the fact.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync} from 'node:fs';
|
|
14
|
+
import {join} from 'node:path';
|
|
15
|
+
import type {Dirent} from 'node:fs';
|
|
16
|
+
|
|
17
|
+
import {workRoot} from './config.js';
|
|
18
|
+
import type {WorkflowSettings} from './config.js';
|
|
19
|
+
import {newWorkDoc, parseWorkDoc, renderWorkDoc, sectionOf, toStatus} from './doc.js';
|
|
20
|
+
import type {WorkDoc, WorkStatus} from './doc.js';
|
|
21
|
+
|
|
22
|
+
/** One work folder, as listed. */
|
|
23
|
+
export interface WorkEntry {
|
|
24
|
+
/** Directory name, e.g. `2026-11-ui-revamp`. The stable handle. */
|
|
25
|
+
slug: string;
|
|
26
|
+
title: string;
|
|
27
|
+
status: WorkStatus;
|
|
28
|
+
/** `updated:` from frontmatter, verbatim. */
|
|
29
|
+
updated: string;
|
|
30
|
+
/** The Goal section's first line — enough to tell two tasks apart. */
|
|
31
|
+
goal: string;
|
|
32
|
+
/** True when the folder has no parseable README. */
|
|
33
|
+
broken: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SLUG_RE = /^[a-z0-9][a-z0-9._-]*$/;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Turn a title into a dated slug.
|
|
40
|
+
*
|
|
41
|
+
* Dated rather than numbered because two people (or one person in two
|
|
42
|
+
* worktrees) will eventually start a task on the same day, and a date prefix
|
|
43
|
+
* keeps the listing chronological without coordination.
|
|
44
|
+
*/
|
|
45
|
+
export function slugify(title: string, date: Date = new Date()): string {
|
|
46
|
+
const stamp = [
|
|
47
|
+
date.getFullYear(),
|
|
48
|
+
String(date.getMonth() + 1).padStart(2, '0'),
|
|
49
|
+
String(date.getDate()).padStart(2, '0'),
|
|
50
|
+
].join('-');
|
|
51
|
+
const body = title
|
|
52
|
+
.toLowerCase()
|
|
53
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
54
|
+
.replace(/^-+|-+$/g, '')
|
|
55
|
+
.slice(0, 60)
|
|
56
|
+
.replace(/-+$/, '');
|
|
57
|
+
return body === '' ? stamp : `${stamp}-${body}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Reject anything that is not a plain directory name inside the work root. */
|
|
61
|
+
export function isValidSlug(slug: string): boolean {
|
|
62
|
+
return SLUG_RE.test(slug) && !slug.includes('..');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The work directory itself, whether or not it exists yet. */
|
|
66
|
+
export function workDir(settings: WorkflowSettings, cwd: string = process.cwd()): string {
|
|
67
|
+
return workRoot(settings, cwd);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Absolute path to one work folder. */
|
|
71
|
+
export function entryDir(
|
|
72
|
+
settings: WorkflowSettings,
|
|
73
|
+
slug: string,
|
|
74
|
+
cwd: string = process.cwd(),
|
|
75
|
+
): string {
|
|
76
|
+
return join(workRoot(settings, cwd), slug);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function readmePath(settings: WorkflowSettings, slug: string, cwd: string): string {
|
|
80
|
+
return join(entryDir(settings, slug, cwd), 'README.md');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** First non-empty, non-heading line of a markdown blob. */
|
|
84
|
+
function firstLine(text: string): string {
|
|
85
|
+
for (const line of text.split(/\r?\n/)) {
|
|
86
|
+
const trimmed = line.trim();
|
|
87
|
+
if (trimmed !== '' && !trimmed.startsWith('#')) return trimmed;
|
|
88
|
+
}
|
|
89
|
+
return '';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Parse one folder into a listing entry, tolerating a missing/broken README. */
|
|
93
|
+
function entryFrom(settings: WorkflowSettings, slug: string, cwd: string): WorkEntry {
|
|
94
|
+
const path = readmePath(settings, slug, cwd);
|
|
95
|
+
let text: string;
|
|
96
|
+
try {
|
|
97
|
+
text = readFileSync(path, 'utf-8');
|
|
98
|
+
} catch {
|
|
99
|
+
return {slug, title: slug, status: 'open', updated: '', goal: '', broken: true};
|
|
100
|
+
}
|
|
101
|
+
const doc = parseWorkDoc(text);
|
|
102
|
+
return {
|
|
103
|
+
slug,
|
|
104
|
+
title: doc.title === '' ? slug : doc.title,
|
|
105
|
+
status: toStatus(doc.fields.status),
|
|
106
|
+
updated: doc.fields.updated ?? '',
|
|
107
|
+
goal: firstLine(sectionOf(doc, 'Goal')),
|
|
108
|
+
broken: doc.title === '',
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Every work folder, most recently updated first.
|
|
114
|
+
*
|
|
115
|
+
* Dirs without a README are reported as `broken` rather than skipped — a
|
|
116
|
+
* folder that exists but lost its README is exactly the state a user needs to
|
|
117
|
+
* be told about, not one to hide.
|
|
118
|
+
*/
|
|
119
|
+
export function listWork(settings: WorkflowSettings, cwd: string = process.cwd()): WorkEntry[] {
|
|
120
|
+
const root = workRoot(settings, cwd);
|
|
121
|
+
let entries: Dirent[];
|
|
122
|
+
try {
|
|
123
|
+
entries = readdirSync(root, {withFileTypes: true});
|
|
124
|
+
} catch {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
return entries
|
|
128
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith('_'))
|
|
129
|
+
.map((entry) => entryFrom(settings, entry.name, cwd))
|
|
130
|
+
.sort((a, b) => {
|
|
131
|
+
// `updated` is an ISO date string, so lexicographic order is chronological.
|
|
132
|
+
// Folders that never set it fall back to their dated slug, which is also ISO.
|
|
133
|
+
const keyA = a.updated !== '' ? a.updated : a.slug.slice(0, 10);
|
|
134
|
+
const keyB = b.updated !== '' ? b.updated : b.slug.slice(0, 10);
|
|
135
|
+
return keyB.localeCompare(keyA);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Load one work folder's full document, or an error string. */
|
|
140
|
+
export function loadWork(
|
|
141
|
+
settings: WorkflowSettings,
|
|
142
|
+
slug: string,
|
|
143
|
+
cwd: string = process.cwd(),
|
|
144
|
+
): {ok: true; doc: WorkDoc; path: string; entry: WorkEntry} | {ok: false; error: string} {
|
|
145
|
+
if (!isValidSlug(slug)) {
|
|
146
|
+
return {ok: false, error: `"${slug}" is not a valid work folder name.`};
|
|
147
|
+
}
|
|
148
|
+
const path = readmePath(settings, slug, cwd);
|
|
149
|
+
if (!existsSync(path)) {
|
|
150
|
+
const known = listWork(settings, cwd).map((e) => e.slug);
|
|
151
|
+
const hint =
|
|
152
|
+
known.length > 0 ? `Known folders:\n ${known.join('\n ')}` : 'No work folders yet.';
|
|
153
|
+
return {ok: false, error: `No work folder "${slug}".\n${hint}`};
|
|
154
|
+
}
|
|
155
|
+
let text: string;
|
|
156
|
+
try {
|
|
157
|
+
text = readFileSync(path, 'utf-8');
|
|
158
|
+
} catch (err) {
|
|
159
|
+
return {ok: false, error: err instanceof Error ? err.message : String(err)};
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
ok: true,
|
|
163
|
+
doc: parseWorkDoc(text),
|
|
164
|
+
path,
|
|
165
|
+
entry: entryFrom(settings, slug, cwd),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function today(): string {
|
|
170
|
+
return new Date().toISOString().slice(0, 10);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Create a work folder.
|
|
175
|
+
*
|
|
176
|
+
* Refuses to reuse an existing slug: silently returning the old folder would
|
|
177
|
+
* look like success while the caller's goal silently vanished into someone
|
|
178
|
+
* else's README.
|
|
179
|
+
*/
|
|
180
|
+
export function createWork(
|
|
181
|
+
settings: WorkflowSettings,
|
|
182
|
+
input: {slug?: string; title: string; goal: string; status?: WorkStatus},
|
|
183
|
+
cwd: string = process.cwd(),
|
|
184
|
+
): {ok: true; slug: string; path: string} | {ok: false; error: string} {
|
|
185
|
+
const title = input.title.trim();
|
|
186
|
+
const goal = input.goal.trim();
|
|
187
|
+
if (title === '') return {ok: false, error: '`title` is required.'};
|
|
188
|
+
if (goal === '') {
|
|
189
|
+
return {
|
|
190
|
+
ok: false,
|
|
191
|
+
error:
|
|
192
|
+
'`goal` is required — write what must be true when the work is complete. A work folder without a goal cannot be resumed.',
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const slug = (input.slug?.trim() || slugify(title)).trim();
|
|
197
|
+
if (!isValidSlug(slug)) {
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
error: `"${slug}" is not a valid folder name — use lowercase letters, digits, dot, dash or underscore.`,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const dir = entryDir(settings, slug, cwd);
|
|
205
|
+
if (existsSync(dir)) {
|
|
206
|
+
return {
|
|
207
|
+
ok: false,
|
|
208
|
+
error: `Work folder "${slug}" already exists — resume it with work_status, or pick another title.`,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const doc = newWorkDoc({
|
|
213
|
+
title,
|
|
214
|
+
goal,
|
|
215
|
+
status: input.status,
|
|
216
|
+
fields: {created: today(), updated: today()},
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const path = readmePath(settings, slug, cwd);
|
|
220
|
+
try {
|
|
221
|
+
mkdirSync(dir, {recursive: true});
|
|
222
|
+
writeFileSync(path, renderWorkDoc(doc), 'utf-8');
|
|
223
|
+
} catch (err) {
|
|
224
|
+
return {ok: false, error: err instanceof Error ? err.message : String(err)};
|
|
225
|
+
}
|
|
226
|
+
return {ok: true, slug, path};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Update a work folder: replace the named sections, optionally set status.
|
|
231
|
+
*
|
|
232
|
+
* Section replacement is whole-body, not append. Append-only editing is what
|
|
233
|
+
* turns a handoff into a log, and a log does not answer "what is true now?" —
|
|
234
|
+
* which is the only question a resuming session asks.
|
|
235
|
+
*/
|
|
236
|
+
export function updateWork(
|
|
237
|
+
settings: WorkflowSettings,
|
|
238
|
+
input: {
|
|
239
|
+
slug: string;
|
|
240
|
+
sections?: Record<string, string>;
|
|
241
|
+
status?: WorkStatus;
|
|
242
|
+
title?: string;
|
|
243
|
+
},
|
|
244
|
+
cwd: string = process.cwd(),
|
|
245
|
+
): {ok: true; slug: string; path: string; created: string[]} | {ok: false; error: string} {
|
|
246
|
+
const loaded = loadWork(settings, input.slug, cwd);
|
|
247
|
+
if (!loaded.ok) return loaded;
|
|
248
|
+
|
|
249
|
+
const doc = loaded.doc;
|
|
250
|
+
const created: string[] = [];
|
|
251
|
+
for (const [name, body] of Object.entries(input.sections ?? {})) {
|
|
252
|
+
if (name.trim() === '') continue;
|
|
253
|
+
// Preserve the author's casing when the section already exists.
|
|
254
|
+
const existing = Object.keys(doc.sections).find(
|
|
255
|
+
(key) => key.toLowerCase() === name.trim().toLowerCase(),
|
|
256
|
+
);
|
|
257
|
+
const key = existing ?? titleCase(name);
|
|
258
|
+
if (existing === undefined) created.push(key);
|
|
259
|
+
doc.sections[key] = body.trim();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (input.title !== undefined && input.title.trim() !== '') doc.title = input.title.trim();
|
|
263
|
+
doc.fields.status = toStatus(input.status ?? doc.fields.status);
|
|
264
|
+
doc.fields.updated = today();
|
|
265
|
+
if (doc.fields.kind === undefined) doc.fields.kind = 'work';
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
writeFileSync(loaded.path, renderWorkDoc(doc), 'utf-8');
|
|
269
|
+
} catch (err) {
|
|
270
|
+
return {ok: false, error: err instanceof Error ? err.message : String(err)};
|
|
271
|
+
}
|
|
272
|
+
return {ok: true, slug: input.slug, path: loaded.path, created};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Normalise a new section name to the README's Title-Case style.
|
|
277
|
+
*
|
|
278
|
+
* Only the FIRST character is raised. Title-casing every word would turn a
|
|
279
|
+
* caller's "Open questions" into "Open Questions", silently changing the label
|
|
280
|
+
* they asked for (and breaking any later lookup by that name).
|
|
281
|
+
*/
|
|
282
|
+
function titleCase(name: string): string {
|
|
283
|
+
const trimmed = name.trim().replace(/\s+/g, ' ');
|
|
284
|
+
if (trimmed === '') return trimmed;
|
|
285
|
+
return trimmed[0].toUpperCase() + trimmed.slice(1);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Mark a folder done (or reopen it). Never deletes anything. */
|
|
289
|
+
export function setWorkStatus(
|
|
290
|
+
settings: WorkflowSettings,
|
|
291
|
+
slug: string,
|
|
292
|
+
status: WorkStatus,
|
|
293
|
+
cwd: string = process.cwd(),
|
|
294
|
+
): {ok: true; slug: string} | {ok: false; error: string} {
|
|
295
|
+
const result = updateWork(settings, {slug, status}, cwd);
|
|
296
|
+
if (!result.ok) return result;
|
|
297
|
+
return {ok: true, slug};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Rename a folder's slug, keeping its README.
|
|
302
|
+
*
|
|
303
|
+
* Exists because the auto-generated slug is derived from a title the user may
|
|
304
|
+
* only settle on later, and a stale folder name in a backlog link is a broken
|
|
305
|
+
* link.
|
|
306
|
+
*/
|
|
307
|
+
export function renameWork(
|
|
308
|
+
settings: WorkflowSettings,
|
|
309
|
+
from: string,
|
|
310
|
+
to: string,
|
|
311
|
+
cwd: string = process.cwd(),
|
|
312
|
+
): {ok: true; slug: string} | {ok: false; error: string} {
|
|
313
|
+
if (!isValidSlug(from)) return {ok: false, error: `"${from}" is not a valid work folder name.`};
|
|
314
|
+
if (!isValidSlug(to)) return {ok: false, error: `"${to}" is not a valid work folder name.`};
|
|
315
|
+
const source = entryDir(settings, from, cwd);
|
|
316
|
+
const target = entryDir(settings, to, cwd);
|
|
317
|
+
if (!existsSync(source)) return {ok: false, error: `No work folder "${from}".`};
|
|
318
|
+
if (existsSync(target)) return {ok: false, error: `"${to}" already exists.`};
|
|
319
|
+
try {
|
|
320
|
+
renameSync(source, target);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
return {ok: false, error: err instanceof Error ? err.message : String(err)};
|
|
323
|
+
}
|
|
324
|
+
return {ok: true, slug: to};
|
|
325
|
+
}
|