@oyasmi/pipiclaw 0.7.6 → 0.8.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/README.md +13 -0
- package/dist/agent/channel-runner.d.ts +3 -0
- package/dist/agent/channel-runner.js +14 -4
- package/dist/agent/commands.js +8 -2
- package/dist/agent/prompt-builder.js +46 -0
- package/dist/agent/session-events.js +5 -0
- package/dist/agent/types.d.ts +2 -0
- package/dist/memory/recall.js +6 -0
- package/dist/memory/task-digest.js +17 -0
- package/dist/models/api-keys.d.ts +5 -1
- package/dist/models/api-keys.js +10 -4
- package/dist/runtime/bootstrap.d.ts +5 -0
- package/dist/runtime/bootstrap.js +59 -2
- package/dist/runtime/dingtalk.d.ts +2 -0
- package/dist/runtime/dingtalk.js +13 -1
- package/dist/runtime/durable-dispatch.d.ts +28 -0
- package/dist/runtime/durable-dispatch.js +144 -0
- package/dist/runtime/events.d.ts +4 -2
- package/dist/runtime/events.js +12 -8
- package/dist/runtime/task-commands.d.ts +9 -0
- package/dist/runtime/task-commands.js +429 -2
- package/dist/runtime/task-driver.d.ts +32 -0
- package/dist/runtime/task-driver.js +270 -0
- package/dist/settings.d.ts +12 -0
- package/dist/settings.js +30 -0
- package/dist/shared/atomic-file.js +23 -3
- package/dist/shared/task-events.d.ts +32 -0
- package/dist/shared/task-events.js +29 -0
- package/dist/shared/task-ledger.d.ts +53 -0
- package/dist/shared/task-ledger.js +246 -8
- package/dist/subagents/tool.d.ts +13 -0
- package/dist/subagents/tool.js +229 -15
- package/dist/tasks/artifact-subject.d.ts +6 -0
- package/dist/tasks/artifact-subject.js +29 -0
- package/dist/tasks/control.d.ts +83 -0
- package/dist/tasks/control.js +292 -0
- package/dist/tasks/store.d.ts +32 -0
- package/dist/tasks/store.js +142 -0
- package/dist/tasks/verification.d.ts +23 -0
- package/dist/tasks/verification.js +68 -0
- package/dist/tools/registry.d.ts +1 -1
- package/dist/tools/registry.js +6 -4
- package/dist/tools/task-manage.d.ts +51 -3
- package/dist/tools/task-manage.js +491 -37
- package/dist/tools/web-fetch.js +3 -0
- package/dist/tui/app.js +1 -1
- package/dist/tui/commands.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import * as log from "../log.js";
|
|
4
|
+
import { taskEventName } from "../shared/task-events.js";
|
|
5
|
+
import { readActiveTasks } from "../shared/task-ledger.js";
|
|
6
|
+
import { taskBudgetViolation } from "../tasks/control.js";
|
|
7
|
+
import { claimTaskAttempt, dependencyState, escalateTask, releaseTaskAttemptClaim } from "../tasks/store.js";
|
|
8
|
+
import { parseScheduledEventContent } from "./events.js";
|
|
9
|
+
const DEFAULT_SCAN_INTERVAL_MS = 60_000;
|
|
10
|
+
const LEGACY_CHECKIN_HANDOFF_GRACE_MS = 2 * 60_000;
|
|
11
|
+
const CHANNEL_ID_PATTERN = /^(dm|group)_[A-Za-z0-9._:-]+$/;
|
|
12
|
+
function isChannelId(value) {
|
|
13
|
+
return CHANNEL_ID_PATTERN.test(value);
|
|
14
|
+
}
|
|
15
|
+
export async function discoverTaskChannels(workspaceDir, knownChannelIds = []) {
|
|
16
|
+
const channels = new Set();
|
|
17
|
+
for (const channelId of knownChannelIds) {
|
|
18
|
+
if (isChannelId(channelId))
|
|
19
|
+
channels.add(channelId);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const entries = await readdir(workspaceDir, { withFileTypes: true });
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (entry.isDirectory() && isChannelId(entry.name))
|
|
25
|
+
channels.add(entry.name);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// A missing/unreadable workspace simply has no tasks to drive this tick.
|
|
30
|
+
}
|
|
31
|
+
return Array.from(channels).sort();
|
|
32
|
+
}
|
|
33
|
+
function attemptKey(channelId, taskId) {
|
|
34
|
+
return `${channelId}\0${taskId}`;
|
|
35
|
+
}
|
|
36
|
+
async function taskFingerprint(_channelDir, entry) {
|
|
37
|
+
// Do not use mtime/size here. Runtime usage accounting deliberately rewrites
|
|
38
|
+
// task control after every attempt; treating that bookkeeping as progress made
|
|
39
|
+
// governed tasks retry at the short continuation interval forever.
|
|
40
|
+
const control = entry.frontmatter.control;
|
|
41
|
+
return [
|
|
42
|
+
entry.frontmatter.readable ? "readable" : "unreadable",
|
|
43
|
+
entry.frontmatter.status ?? "",
|
|
44
|
+
entry.frontmatter.wake ?? "",
|
|
45
|
+
entry.frontmatter.recurrence ?? "",
|
|
46
|
+
entry.latestNote ?? "",
|
|
47
|
+
control?.nextAction ?? "",
|
|
48
|
+
control?.blockedReason ?? "",
|
|
49
|
+
control?.verification.status ?? "",
|
|
50
|
+
control?.cycleId ?? "",
|
|
51
|
+
].join("\0");
|
|
52
|
+
}
|
|
53
|
+
function isEligible(attempt, fingerprint, nowMs, settings) {
|
|
54
|
+
if (!attempt)
|
|
55
|
+
return true;
|
|
56
|
+
const changed = attempt.fingerprint !== fingerprint;
|
|
57
|
+
const delayMinutes = changed || !attempt.accepted ? settings.continuationDelayMinutes : settings.stalledRetryMinutes;
|
|
58
|
+
return nowMs - attempt.atMs >= delayMinutes * 60_000;
|
|
59
|
+
}
|
|
60
|
+
async function hasLiveLegacyCheckin(workspaceDir, channelId, taskId, nowMs) {
|
|
61
|
+
const name = taskEventName(channelId, taskId, "checkin");
|
|
62
|
+
const path = join(workspaceDir, "events", `${name}.json`);
|
|
63
|
+
try {
|
|
64
|
+
const event = parseScheduledEventContent(await readFile(path, "utf-8"), `${name}.json`);
|
|
65
|
+
if (event.type !== "one-shot")
|
|
66
|
+
return false;
|
|
67
|
+
const atMs = new Date(event.at).getTime();
|
|
68
|
+
return Number.isFinite(atMs) && atMs >= nowMs - LEGACY_CHECKIN_HANDOFF_GRACE_MS;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function createTaskDriverEvent(channelId, entry, nowMs) {
|
|
75
|
+
const verification = entry.frontmatter.control?.verification;
|
|
76
|
+
const verificationInstruction = entry.frontmatter.status === "verifying"
|
|
77
|
+
? verification?.status === "passed"
|
|
78
|
+
? " Independent verification already passed for the current artifact; finish the task with task_manage done and specific evidence."
|
|
79
|
+
: " This task is a verification candidate: call subagent with purpose=verify and taskId, then import its runId with task_manage verify. Do not implement more changes before the verifier reports."
|
|
80
|
+
: "";
|
|
81
|
+
const repair = entry.frontmatter.readable
|
|
82
|
+
? ""
|
|
83
|
+
: " Its frontmatter is unreadable: repair it before using task_manage, and do not silently skip the task.";
|
|
84
|
+
const control = entry.frontmatter.control;
|
|
85
|
+
const capsule = [
|
|
86
|
+
`Task capsule: title=${entry.title}; status=${entry.frontmatter.status ?? "open"};`,
|
|
87
|
+
entry.latestNote ? `latest=${entry.latestNote};` : "",
|
|
88
|
+
control?.nextAction ? `next=${control.nextAction};` : "",
|
|
89
|
+
control ? `budget=${control.usage.attempts}/${control.budget.maxAttempts} attempts;` : "",
|
|
90
|
+
]
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join(" ");
|
|
93
|
+
return {
|
|
94
|
+
type: channelId.startsWith("group_") ? "group" : "dm",
|
|
95
|
+
channelId,
|
|
96
|
+
user: "TASK_DRIVER",
|
|
97
|
+
userName: "TASK_DRIVER",
|
|
98
|
+
text: `[TASK_DRIVER:${entry.id}] Resume task ${entry.id}. ${capsule}${repair} Open tasks/${entry.id}.md, advance the next ` +
|
|
99
|
+
"concrete step, respect its control metadata (budget, dependencies, isolation, and side-effect approval), and " +
|
|
100
|
+
"verify the work against its DoD. If it remains open, atomically record what changed, the " +
|
|
101
|
+
"next step, status, and any future wake with task_manage progress (or carefully update the task file if that " +
|
|
102
|
+
"tool is unavailable). For independent verification, delegate a purpose=verify subagent and register its run " +
|
|
103
|
+
"with task_manage verify before task_manage done. Never perform external side effects unless approval is granted. " +
|
|
104
|
+
"If complete, use task_manage done with specific evidence. If no user-visible update " +
|
|
105
|
+
`is needed, respond with [SILENT].${verificationInstruction}`,
|
|
106
|
+
ts: String(nowMs),
|
|
107
|
+
conversationId: "",
|
|
108
|
+
conversationType: channelId.startsWith("group_") ? "2" : "1",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function taskEscalationEvent(channelId, entry, reason, nowMs) {
|
|
112
|
+
return {
|
|
113
|
+
type: channelId.startsWith("group_") ? "group" : "dm",
|
|
114
|
+
channelId,
|
|
115
|
+
user: "TASK_DRIVER",
|
|
116
|
+
userName: "TASK_DRIVER",
|
|
117
|
+
text: `[TASK_ESCALATION:${entry.id}] Task ${entry.id} (${entry.title}) was stopped by the deterministic task ` +
|
|
118
|
+
`governor: ${reason}. Inform the user concisely, explain the actionable recovery (adjust the task control ` +
|
|
119
|
+
"metadata, resolve the dependency, or explicitly cancel/reopen it), and do not continue the task in this run.",
|
|
120
|
+
ts: String(nowMs),
|
|
121
|
+
conversationId: "",
|
|
122
|
+
conversationType: channelId.startsWith("group_") ? "2" : "1",
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function terminalDependencyReason(reason) {
|
|
126
|
+
return reason?.includes(" is missing") ||
|
|
127
|
+
reason?.includes(" is cancelled") ||
|
|
128
|
+
reason?.includes(" is escalated") ||
|
|
129
|
+
reason?.includes("dependency cycle")
|
|
130
|
+
? reason
|
|
131
|
+
: undefined;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Native, token-gated driver for the persistent task ledger.
|
|
135
|
+
*
|
|
136
|
+
* The scan itself is deterministic and cheap. It wakes at most one actionable task
|
|
137
|
+
* per channel, skips active channels, backs off unchanged ledgers, and round-robins
|
|
138
|
+
* dispatches so a busy channel cannot starve the rest. `wake` is therefore sufficient
|
|
139
|
+
* to resume a task; users do not need to install a heartbeat event or sensor script.
|
|
140
|
+
*/
|
|
141
|
+
export class TaskDriver {
|
|
142
|
+
constructor(options) {
|
|
143
|
+
this.options = options;
|
|
144
|
+
this.timer = null;
|
|
145
|
+
this.running = false;
|
|
146
|
+
this.nextChannelIndex = 0;
|
|
147
|
+
this.attempts = new Map();
|
|
148
|
+
}
|
|
149
|
+
start() {
|
|
150
|
+
if (this.timer)
|
|
151
|
+
return;
|
|
152
|
+
this.timer = setInterval(() => {
|
|
153
|
+
void this.runOnce().catch((error) => {
|
|
154
|
+
log.logWarning("Task driver tick failed", error instanceof Error ? error.message : String(error));
|
|
155
|
+
});
|
|
156
|
+
}, this.options.intervalMs ?? DEFAULT_SCAN_INTERVAL_MS);
|
|
157
|
+
this.timer.unref?.();
|
|
158
|
+
}
|
|
159
|
+
stop() {
|
|
160
|
+
if (!this.timer)
|
|
161
|
+
return;
|
|
162
|
+
clearInterval(this.timer);
|
|
163
|
+
this.timer = null;
|
|
164
|
+
}
|
|
165
|
+
async runOnce(now = new Date()) {
|
|
166
|
+
const settings = this.options.getSettings();
|
|
167
|
+
if (!settings.enabled || this.running)
|
|
168
|
+
return;
|
|
169
|
+
this.running = true;
|
|
170
|
+
try {
|
|
171
|
+
const channels = await discoverTaskChannels(this.options.workspaceDir, this.options.getKnownChannelIds?.());
|
|
172
|
+
if (channels.length === 0) {
|
|
173
|
+
this.attempts.clear();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const seen = new Set();
|
|
177
|
+
const start = this.nextChannelIndex % channels.length;
|
|
178
|
+
let dispatched = 0;
|
|
179
|
+
let lastDispatchOffset = -1;
|
|
180
|
+
for (let offset = 0; offset < channels.length; offset++) {
|
|
181
|
+
const channelId = channels[(start + offset) % channels.length];
|
|
182
|
+
if (!channelId)
|
|
183
|
+
continue;
|
|
184
|
+
const channelDir = join(this.options.workspaceDir, channelId);
|
|
185
|
+
const entries = await readActiveTasks(join(channelDir, "tasks"), now.getTime());
|
|
186
|
+
for (const entry of entries)
|
|
187
|
+
seen.add(attemptKey(channelId, entry.id));
|
|
188
|
+
if (dispatched >= settings.maxDispatchesPerTick || this.options.isChannelActive(channelId))
|
|
189
|
+
continue;
|
|
190
|
+
let governanceHandled = false;
|
|
191
|
+
for (const candidate of entries) {
|
|
192
|
+
const status = candidate.frontmatter.status;
|
|
193
|
+
const control = candidate.frontmatter.control;
|
|
194
|
+
if (!control ||
|
|
195
|
+
status === "done" ||
|
|
196
|
+
status === "cancelled" ||
|
|
197
|
+
status === "escalated" ||
|
|
198
|
+
status === "paused")
|
|
199
|
+
continue;
|
|
200
|
+
const dependencies = await dependencyState(channelDir, control.dependsOn, candidate.id);
|
|
201
|
+
const escalationReason = taskBudgetViolation(control, now.getTime()) ?? terminalDependencyReason(dependencies.reason);
|
|
202
|
+
if (!escalationReason)
|
|
203
|
+
continue;
|
|
204
|
+
governanceHandled = true;
|
|
205
|
+
const accepted = await this.options.dispatch(taskEscalationEvent(channelId, candidate, escalationReason, now.getTime()));
|
|
206
|
+
if (accepted && (await escalateTask(channelDir, candidate.id, escalationReason))) {
|
|
207
|
+
dispatched++;
|
|
208
|
+
lastDispatchOffset = offset;
|
|
209
|
+
log.logWarning(`[${channelId}] Task driver escalated ${candidate.id}`, escalationReason);
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
if (governanceHandled)
|
|
214
|
+
continue;
|
|
215
|
+
const candidates = entries.filter((candidate) => candidate.actionable);
|
|
216
|
+
if (candidates.length === 0)
|
|
217
|
+
continue;
|
|
218
|
+
let entry;
|
|
219
|
+
for (const candidate of candidates) {
|
|
220
|
+
const control = candidate.frontmatter.control;
|
|
221
|
+
if (!control) {
|
|
222
|
+
entry = candidate;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
const dependencies = await dependencyState(channelDir, control.dependsOn, candidate.id);
|
|
226
|
+
if (!dependencies.ready)
|
|
227
|
+
continue;
|
|
228
|
+
entry = candidate;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
if (!entry || dispatched >= settings.maxDispatchesPerTick)
|
|
232
|
+
continue;
|
|
233
|
+
// Upgrade compatibility: while a legacy task-owned one-shot is still
|
|
234
|
+
// about to deliver this same wake, let it own the handoff. Once it is
|
|
235
|
+
// consumed (or stale for >2m), the native driver becomes the recovery path.
|
|
236
|
+
if (await hasLiveLegacyCheckin(this.options.workspaceDir, channelId, entry.id, now.getTime())) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const key = attemptKey(channelId, entry.id);
|
|
240
|
+
let fingerprint = await taskFingerprint(channelDir, entry);
|
|
241
|
+
if (!isEligible(this.attempts.get(key), fingerprint, now.getTime(), settings))
|
|
242
|
+
continue;
|
|
243
|
+
const claim = entry.frontmatter.control ? await claimTaskAttempt(channelDir, entry.id, now) : undefined;
|
|
244
|
+
if (claim) {
|
|
245
|
+
fingerprint = await taskFingerprint(channelDir, entry);
|
|
246
|
+
}
|
|
247
|
+
const accepted = await this.options.dispatch(createTaskDriverEvent(channelId, entry, now.getTime()));
|
|
248
|
+
if (!accepted && claim)
|
|
249
|
+
await releaseTaskAttemptClaim(channelDir, entry.id, claim, now);
|
|
250
|
+
this.attempts.set(key, { fingerprint, atMs: now.getTime(), accepted });
|
|
251
|
+
if (accepted) {
|
|
252
|
+
dispatched++;
|
|
253
|
+
lastDispatchOffset = offset;
|
|
254
|
+
log.logInfo(`[${channelId}] Task driver enqueued ${entry.id}`);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
log.logWarning(`[${channelId}] Task driver could not enqueue ${entry.id}`, "channel queue unavailable");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
for (const key of this.attempts.keys()) {
|
|
261
|
+
if (!seen.has(key))
|
|
262
|
+
this.attempts.delete(key);
|
|
263
|
+
}
|
|
264
|
+
this.nextChannelIndex = (start + (lastDispatchOffset >= 0 ? lastDispatchOffset + 1 : 1)) % channels.length;
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
this.running = false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
package/dist/settings.d.ts
CHANGED
|
@@ -65,6 +65,16 @@ export interface PipiclawTaskDigestSettings {
|
|
|
65
65
|
maxTasks: number;
|
|
66
66
|
maxChars: number;
|
|
67
67
|
}
|
|
68
|
+
export interface PipiclawTaskDriverSettings {
|
|
69
|
+
/** Native runtime wake-up for actionable task-ledger entries. */
|
|
70
|
+
enabled: boolean;
|
|
71
|
+
/** Earliest continuation after a task changed during its previous run. */
|
|
72
|
+
continuationDelayMinutes: number;
|
|
73
|
+
/** Retry delay when a dispatched task made no observable ledger progress. */
|
|
74
|
+
stalledRetryMinutes: number;
|
|
75
|
+
/** Global enqueue cap per scan, with round-robin fairness across channels. */
|
|
76
|
+
maxDispatchesPerTick: number;
|
|
77
|
+
}
|
|
68
78
|
export interface PipiclawSessionMemorySettings {
|
|
69
79
|
enabled: boolean;
|
|
70
80
|
minTurnsBetweenUpdate: number;
|
|
@@ -124,6 +134,7 @@ export interface PipiclawSettings {
|
|
|
124
134
|
retry?: Partial<PipiclawRetrySettings>;
|
|
125
135
|
memoryRecall?: Partial<PipiclawMemoryRecallSettings>;
|
|
126
136
|
taskDigest?: Partial<PipiclawTaskDigestSettings>;
|
|
137
|
+
taskDriver?: Partial<PipiclawTaskDriverSettings>;
|
|
127
138
|
sessionMemory?: Partial<PipiclawSessionMemorySettings>;
|
|
128
139
|
memoryGrowth?: Partial<PipiclawMemoryGrowthSettings>;
|
|
129
140
|
memoryMaintenance?: Partial<PipiclawMemoryMaintenanceSettings>;
|
|
@@ -163,6 +174,7 @@ export declare class PipiclawSettingsManager {
|
|
|
163
174
|
getRetrySettings(): PipiclawRetrySettings;
|
|
164
175
|
getMemoryRecallSettings(): PipiclawMemoryRecallSettings;
|
|
165
176
|
getTaskDigestSettings(): PipiclawTaskDigestSettings;
|
|
177
|
+
getTaskDriverSettings(): PipiclawTaskDriverSettings;
|
|
166
178
|
getSessionMemorySettings(): PipiclawSessionMemorySettings;
|
|
167
179
|
getMemoryGrowthSettings(): PipiclawMemoryGrowthSettings;
|
|
168
180
|
getMemoryMaintenanceSettings(): PipiclawMemoryMaintenanceSettings;
|
package/dist/settings.js
CHANGED
|
@@ -36,6 +36,15 @@ const DEFAULT_TASK_DIGEST = {
|
|
|
36
36
|
maxTasks: 8,
|
|
37
37
|
maxChars: 1000,
|
|
38
38
|
};
|
|
39
|
+
// The driver makes `wake` an executable task property rather than a convention
|
|
40
|
+
// that requires users to install a heartbeat event and sensor script. A changed
|
|
41
|
+
// task can continue promptly; an unchanged task backs off to avoid token loops.
|
|
42
|
+
const DEFAULT_TASK_DRIVER = {
|
|
43
|
+
enabled: true,
|
|
44
|
+
continuationDelayMinutes: 5,
|
|
45
|
+
stalledRetryMinutes: 60,
|
|
46
|
+
maxDispatchesPerTick: 4,
|
|
47
|
+
};
|
|
39
48
|
const DEFAULT_SESSION_MEMORY = {
|
|
40
49
|
enabled: true,
|
|
41
50
|
minTurnsBetweenUpdate: 2,
|
|
@@ -180,6 +189,27 @@ export class PipiclawSettingsManager {
|
|
|
180
189
|
...this.settings.taskDigest,
|
|
181
190
|
};
|
|
182
191
|
}
|
|
192
|
+
getTaskDriverSettings() {
|
|
193
|
+
const configured = {
|
|
194
|
+
...DEFAULT_TASK_DRIVER,
|
|
195
|
+
...this.settings.taskDriver,
|
|
196
|
+
};
|
|
197
|
+
const continuationDelayMinutes = Number.isFinite(configured.continuationDelayMinutes)
|
|
198
|
+
? Math.min(60, Math.max(1, Math.floor(configured.continuationDelayMinutes)))
|
|
199
|
+
: DEFAULT_TASK_DRIVER.continuationDelayMinutes;
|
|
200
|
+
const stalledRetryMinutes = Number.isFinite(configured.stalledRetryMinutes)
|
|
201
|
+
? Math.min(24 * 60, Math.max(continuationDelayMinutes, Math.floor(configured.stalledRetryMinutes)))
|
|
202
|
+
: DEFAULT_TASK_DRIVER.stalledRetryMinutes;
|
|
203
|
+
const maxDispatchesPerTick = Number.isFinite(configured.maxDispatchesPerTick)
|
|
204
|
+
? Math.min(20, Math.max(1, Math.floor(configured.maxDispatchesPerTick)))
|
|
205
|
+
: DEFAULT_TASK_DRIVER.maxDispatchesPerTick;
|
|
206
|
+
return {
|
|
207
|
+
enabled: configured.enabled !== false,
|
|
208
|
+
continuationDelayMinutes,
|
|
209
|
+
stalledRetryMinutes,
|
|
210
|
+
maxDispatchesPerTick,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
183
213
|
getSessionMemorySettings() {
|
|
184
214
|
return {
|
|
185
215
|
...DEFAULT_SESSION_MEMORY,
|
|
@@ -1,14 +1,34 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdir, rename, unlink
|
|
2
|
+
import { mkdir, open, rename, unlink } from "node:fs/promises";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
4
|
export function createAtomicTempPath(path) {
|
|
5
5
|
return `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
6
6
|
}
|
|
7
7
|
export async function writeFileAtomically(path, content, tempPath = createAtomicTempPath(path)) {
|
|
8
|
-
|
|
8
|
+
const dir = dirname(path);
|
|
9
|
+
await mkdir(dir, { recursive: true });
|
|
9
10
|
try {
|
|
10
|
-
await
|
|
11
|
+
const handle = await open(tempPath, "w");
|
|
12
|
+
try {
|
|
13
|
+
await handle.writeFile(content, "utf-8");
|
|
14
|
+
await handle.sync();
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
await handle.close();
|
|
18
|
+
}
|
|
11
19
|
await rename(tempPath, path);
|
|
20
|
+
// Best-effort: the rename already succeeded, so a directory-fsync failure
|
|
21
|
+
// (e.g. unsupported on this platform/filesystem) should not surface as a write failure.
|
|
22
|
+
await open(dir, "r")
|
|
23
|
+
.then(async (dirHandle) => {
|
|
24
|
+
try {
|
|
25
|
+
await dirHandle.sync();
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
await dirHandle.close();
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
.catch(() => undefined);
|
|
12
32
|
}
|
|
13
33
|
catch (error) {
|
|
14
34
|
await unlink(tempPath).catch(() => undefined);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface ParsedTaskEventName {
|
|
2
|
+
id: string;
|
|
3
|
+
use: string;
|
|
4
|
+
}
|
|
5
|
+
export interface TaskEventLike {
|
|
6
|
+
id?: string;
|
|
7
|
+
use?: string;
|
|
8
|
+
event?: {
|
|
9
|
+
type: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
type EventOf<T> = T extends {
|
|
13
|
+
event?: infer E;
|
|
14
|
+
} ? E : never;
|
|
15
|
+
export declare function taskEventPrefix(channelId: string, id?: string): string;
|
|
16
|
+
export declare function taskEventName(channelId: string, id: string, use: string): string;
|
|
17
|
+
export declare function taskScheduleEventName(channelId: string, id: string): string;
|
|
18
|
+
export declare function taskScheduleEventFilename(channelId: string, id: string): string;
|
|
19
|
+
export declare function parseTaskEventName(name: string, channelId: string): ParsedTaskEventName | undefined;
|
|
20
|
+
export declare function isTaskScheduleEvent<T extends TaskEventLike>(info: T): info is T & {
|
|
21
|
+
use: "schedule";
|
|
22
|
+
event: Extract<EventOf<T>, {
|
|
23
|
+
type: "periodic";
|
|
24
|
+
}>;
|
|
25
|
+
};
|
|
26
|
+
export declare function isTaskCheckinEvent<T extends TaskEventLike>(info: T): info is T & {
|
|
27
|
+
use: "checkin";
|
|
28
|
+
event: Extract<EventOf<T>, {
|
|
29
|
+
type: "one-shot";
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function taskEventPrefix(channelId, id) {
|
|
2
|
+
return id ? `task.${channelId}.${id}.` : `task.${channelId}.`;
|
|
3
|
+
}
|
|
4
|
+
export function taskEventName(channelId, id, use) {
|
|
5
|
+
return `${taskEventPrefix(channelId, id)}${use}`;
|
|
6
|
+
}
|
|
7
|
+
export function taskScheduleEventName(channelId, id) {
|
|
8
|
+
return taskEventName(channelId, id, "schedule");
|
|
9
|
+
}
|
|
10
|
+
export function taskScheduleEventFilename(channelId, id) {
|
|
11
|
+
return `${taskScheduleEventName(channelId, id)}.json`;
|
|
12
|
+
}
|
|
13
|
+
export function parseTaskEventName(name, channelId) {
|
|
14
|
+
const normalized = name.endsWith(".json") ? name.slice(0, -".json".length) : name;
|
|
15
|
+
const prefix = taskEventPrefix(channelId);
|
|
16
|
+
if (!normalized.startsWith(prefix))
|
|
17
|
+
return undefined;
|
|
18
|
+
const rest = normalized.slice(prefix.length);
|
|
19
|
+
const lastDot = rest.lastIndexOf(".");
|
|
20
|
+
if (lastDot <= 0 || lastDot === rest.length - 1)
|
|
21
|
+
return undefined;
|
|
22
|
+
return { id: rest.slice(0, lastDot), use: rest.slice(lastDot + 1) };
|
|
23
|
+
}
|
|
24
|
+
export function isTaskScheduleEvent(info) {
|
|
25
|
+
return info.use === "schedule" && info.event?.type === "periodic";
|
|
26
|
+
}
|
|
27
|
+
export function isTaskCheckinEvent(info) {
|
|
28
|
+
return info.use === "checkin" && info.event?.type === "one-shot";
|
|
29
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type TaskControl, type TaskVerificationMode } from "../tasks/control.js";
|
|
1
2
|
/**
|
|
2
3
|
* Shared reader for the task ledger (`workspace/<channelId>/tasks/*.md`).
|
|
3
4
|
*
|
|
@@ -14,6 +15,9 @@ export interface TaskFrontmatter {
|
|
|
14
15
|
status?: string;
|
|
15
16
|
wake?: string;
|
|
16
17
|
recurrence?: string;
|
|
18
|
+
control?: TaskControl;
|
|
19
|
+
/** false only when a control field exists but cannot be parsed/validated. */
|
|
20
|
+
controlReadable?: boolean;
|
|
17
21
|
}
|
|
18
22
|
export interface TaskLedgerEntry {
|
|
19
23
|
/** Filename without `.md`; the task id. */
|
|
@@ -28,6 +32,34 @@ export interface TaskLedgerEntry {
|
|
|
28
32
|
/** First bullet/line under a "当前周期"/"current cycle" heading, if any (digest only). */
|
|
29
33
|
latestNote?: string;
|
|
30
34
|
}
|
|
35
|
+
export interface TaskSkeletonInput {
|
|
36
|
+
title: string;
|
|
37
|
+
goal: string;
|
|
38
|
+
dod: string;
|
|
39
|
+
manual?: string;
|
|
40
|
+
verificationPlan?: string;
|
|
41
|
+
verificationMode?: TaskVerificationMode;
|
|
42
|
+
}
|
|
43
|
+
export declare const DEFAULT_TASK_MANUAL = "- Follow the DoD, keep the current cycle log updated, and schedule check-ins when waiting.";
|
|
44
|
+
export declare const STANDARD_TASK_SECTIONS: readonly [{
|
|
45
|
+
readonly label: "Goal";
|
|
46
|
+
readonly names: readonly ["Goal", "目标"];
|
|
47
|
+
}, {
|
|
48
|
+
readonly label: "DoD";
|
|
49
|
+
readonly names: readonly ["DoD"];
|
|
50
|
+
}, {
|
|
51
|
+
readonly label: "Manual";
|
|
52
|
+
readonly names: readonly ["Manual", "手册"];
|
|
53
|
+
}, {
|
|
54
|
+
readonly label: "Verification";
|
|
55
|
+
readonly names: readonly ["Verification", "验收"];
|
|
56
|
+
}, {
|
|
57
|
+
readonly label: "Current Cycle";
|
|
58
|
+
readonly names: readonly ["Current Cycle", "当前周期"];
|
|
59
|
+
}, {
|
|
60
|
+
readonly label: "History";
|
|
61
|
+
readonly names: readonly ["History", "历史"];
|
|
62
|
+
}];
|
|
31
63
|
/** Validate/normalize a task id (filename without `.md`), rejecting path traversal. */
|
|
32
64
|
export declare function normalizeTaskId(id: string): string;
|
|
33
65
|
/** The document body after the leading frontmatter block, or the whole content when there is none. */
|
|
@@ -42,6 +74,27 @@ export declare function parseTaskFrontmatter(content: string): TaskFrontmatter;
|
|
|
42
74
|
export declare function isTaskActionable(frontmatter: TaskFrontmatter, now: number): boolean;
|
|
43
75
|
/** First `# ` heading after the frontmatter block, or the id when there is none. */
|
|
44
76
|
export declare function extractTaskTitle(content: string, fallbackId: string): string;
|
|
77
|
+
export declare function hasTaskHeading(content: string, names: readonly string[]): boolean;
|
|
78
|
+
export declare function missingStandardTaskSections(content: string): string[];
|
|
79
|
+
/** Unchecked Markdown acceptance boxes under DoD or Verification sections. */
|
|
80
|
+
export declare function uncheckedTaskAcceptanceItems(content: string): string[];
|
|
81
|
+
export declare function renderStandardTaskBody(input: TaskSkeletonInput): string;
|
|
82
|
+
export interface TaskDocumentFields {
|
|
83
|
+
status: string;
|
|
84
|
+
wake?: string;
|
|
85
|
+
recurrence?: string;
|
|
86
|
+
control?: TaskControl;
|
|
87
|
+
}
|
|
88
|
+
export declare function renderTaskDocument(fields: TaskDocumentFields, body: string): string;
|
|
89
|
+
/** Append one progress bullet to the standard Current Cycle section. */
|
|
90
|
+
export declare function appendCurrentCycleNote(content: string, note: string): string;
|
|
91
|
+
/**
|
|
92
|
+
* Close the visible current-cycle notes into History and open a fresh cycle.
|
|
93
|
+
* Periodic task cycles deliberately use this small, deterministic transformation
|
|
94
|
+
* instead of asking the model to hand-edit headings and risk appending future
|
|
95
|
+
* checkpoints to the previous cycle.
|
|
96
|
+
*/
|
|
97
|
+
export declare function startTaskCycle(content: string, cycleId: string): string;
|
|
45
98
|
/** Actionable first; then earliest wake first (unset wake sorts as "ready now"); then id. */
|
|
46
99
|
export declare function compareTaskEntries(a: TaskLedgerEntry, b: TaskLedgerEntry): number;
|
|
47
100
|
/**
|