-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscheduler.ts
More file actions
330 lines (301 loc) · 12.1 KB
/
Copy pathscheduler.ts
File metadata and controls
330 lines (301 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Scheduler: orchestrates loop command parsing, task firing, and adaptive rescheduling.
*
* Per-session scoping:
* - Every task is bound to a sessionID at creation time
* - Strict: cancel/pause/resume only operate on tasks in the current session
* - --all flag: bypass session filter for global operations
*
* Implementation note: factory pattern (no `this` reliance) so opencode's
* plugin loader can call us with or without `new`.
*/
import { existsSync, readFileSync } from "node:fs"
import { join } from "node:path"
import type { LoopTask } from "./types.js"
import type { LoopStoreInstance as LoopStore } from "./store.js"
import type { CronParserInstance as CronParser } from "./cron-parser.js"
import type { JitterInstance as Jitter } from "./jitter.js"
import { errorMessage, type LoopLogger } from "./runtime-feedback.js"
export interface SchedulerOptions {
store: LoopStore
cron: CronParser
jitter: Jitter
adaptiveMinMs: number
adaptiveMaxMs: number
logger?: LoopLogger
}
export interface CommandParseResult {
message: string
task?: LoopTask
}
interface SchedulerInstance {
opts: SchedulerOptions
currentSessionID: string | null
inflight: Set<string>
setCurrentSession(sessionID: string | null): void
handleUserCommand(args: string, directory: string, sessionID?: string | null): Promise<CommandParseResult>
handleCancel(id: string, allFlag: boolean): CommandParseResult | Promise<CommandParseResult>
handlePause(id: string, allFlag: boolean): CommandParseResult | Promise<CommandParseResult>
handleResume(id: string, allFlag: boolean): Promise<CommandParseResult>
formatTaskList(tasks: LoopTask[], showSession?: boolean): string
loadDefaultPrompt(directory: string): string
getDueTasks(now?: number): Promise<LoopTask[]>
getDueTasksForSession(sessionID: string, now?: number): Promise<LoopTask[]>
nextDueAt(task: LoopTask, now?: number): Promise<number>
fireTask(task: LoopTask, ctx: any): Promise<void>
rearmFixed(task: LoopTask, now?: number): Promise<void>
clampAdaptive(ms: number): number
}
export type { SchedulerInstance }
export function Scheduler(this: unknown, opts: SchedulerOptions): SchedulerInstance {
void this
const logger: LoopLogger = opts.logger ?? (async () => {})
const inst: SchedulerInstance = {
opts,
currentSessionID: null,
inflight: new Set<string>(),
setCurrentSession(sessionID) {
inst.currentSessionID = sessionID
},
async handleUserCommand(args, directory, sessionID) {
if (sessionID !== undefined) inst.currentSessionID = sessionID
const trimmed = args.trim()
const tokens = trimmed.split(/\s+/)
const allFlag = tokens.includes("--all")
const head = tokens[0]?.toLowerCase()
if (head === "cancel" || head === "stop") {
const id = tokens[1]
if (!id) return { message: "❌ 用法: /loop cancel <taskId> [--all]" }
return inst.handleCancel(id, allFlag)
}
if (head === "list" || head === "status") {
const tasks = allFlag
? inst.opts.store.list()
: inst.opts.store.listBySession(inst.currentSessionID ?? "")
return { message: inst.formatTaskList(tasks, allFlag) }
}
if (head === "pause") {
const id = tokens[1]
if (!id) return { message: "❌ 用法: /loop pause <taskId> [--all]" }
return inst.handlePause(id, allFlag)
}
if (head === "resume") {
const id = tokens[1]
if (!id) return { message: "❌ 用法: /loop resume <taskId> [--all]" }
return inst.handleResume(id, allFlag)
}
if (head === "stop-all") {
if (allFlag) {
const n = await inst.opts.store.cancelAll()
return { message: `🛑 Cancelled ${n} task(s) across all sessions` }
}
const removed = await inst.opts.store.cancelBySession(inst.currentSessionID ?? "")
return { message: `🛑 Cancelled ${removed} task(s) in current session` }
}
if (!sessionID) {
return { message: "❌ /loop requires an active session context" }
}
if (!trimmed) {
const prompt = inst.loadDefaultPrompt(directory)
const task = await inst.opts.store.create({
prompt,
mode: "maintenance",
adaptiveMaxMs: inst.opts.adaptiveMaxMs,
directory,
source: "default",
sessionID,
})
return {
task,
message: `🔁 Loop started (maintenance mode): task ${task.id} (session ${sessionID.slice(0, 8)}). Auto re-arms every ${inst.opts.adaptiveMaxMs / 1000}s. Use \`/loop cancel ${task.id}\` to stop.`,
}
}
const { interval, rest } = inst.opts.cron.extractInterval(trimmed)
if (interval && rest.trim()) {
const task = await inst.opts.store.create({
prompt: rest.trim(),
mode: "fixed",
intervalMs: interval.ms,
directory,
source: "user",
sessionID,
})
return {
task,
message: `🔁 Loop started: every ${interval.display}, prompt "${rest.trim().slice(0, 50)}${rest.length > 50 ? "..." : ""}" [id=${task.id}] [s=${sessionID.slice(0, 8)}]. Cancel: \`/loop cancel ${task.id}\``,
}
}
if (trimmed) {
const task = await inst.opts.store.create({
prompt: trimmed,
mode: "adaptive",
adaptiveMinMs: inst.opts.adaptiveMinMs,
adaptiveMaxMs: inst.opts.adaptiveMaxMs,
directory,
source: "user",
sessionID,
})
return {
task,
message: `🔁 Loop started (adaptive ${inst.opts.adaptiveMinMs / 1000}s–${inst.opts.adaptiveMaxMs / 1000}s): "${trimmed.slice(0, 50)}${trimmed.length > 50 ? "..." : ""}" [id=${task.id}] [s=${sessionID.slice(0, 8)}]. Cancel: \`/loop cancel ${task.id}\``,
}
}
return { message: "❌ Empty loop command" }
},
handleCancel(id, allFlag) {
const task = inst.opts.store.get(id)
if (!task) return { message: `❌ No task ${id}` }
if (!allFlag && task.sessionID !== inst.currentSessionID) {
return {
message: `❌ Task ${id} belongs to another session (${task.sessionID.slice(0, 8)}). Add \`--all\` to override.`,
}
}
return inst.opts.store.cancel(id).then((r) => ({
message: r ? `🛑 Cancelled ${id}` : `❌ No task ${id}`,
}))
},
handlePause(id, allFlag) {
const task = inst.opts.store.get(id)
if (!task) return { message: `❌ No task ${id}` }
if (!allFlag && task.sessionID !== inst.currentSessionID) {
return {
message: `❌ Task ${id} belongs to another session. Add \`--all\` to override.`,
}
}
return inst.opts.store.setPaused(id, true).then((r) => ({
message: r ? `⏸ Paused ${id}` : `❌ No task ${id}`,
}))
},
async handleResume(id, allFlag) {
const task = inst.opts.store.get(id)
if (!task) return { message: `❌ No task ${id}` }
if (!allFlag && task.sessionID !== inst.currentSessionID) {
return {
message: `❌ Task ${id} belongs to another session. Add \`--all\` to override.`,
}
}
const r = await inst.opts.store.setPaused(id, false)
if (r && r.mode === "fixed" && r.intervalMs) {
await inst.rearmFixed(r)
}
return { message: r ? `▶ Resumed ${id}` : `❌ No task ${id}` }
},
formatTaskList(tasks, showSession = false) {
if (tasks.length === 0)
return "📭 No loop tasks. Use `/loop <prompt>` or `/loop 5m <prompt>` to create one."
const lines = [`📋 ${tasks.length} loop task(s):`]
for (const t of tasks) {
const interval =
t.mode === "fixed"
? `every ${(t.intervalMs ?? 0) / 1000}s`
: t.mode === "adaptive"
? `adaptive ${(t.adaptiveMinMs ?? 0) / 1000}s–${(t.adaptiveMaxMs ?? 0) / 1000}s`
: `maintenance ${(t.adaptiveMaxMs ?? 0) / 1000}s`
const status = t.paused ? "⏸ paused" : "▶ active"
const sessionTag =
showSession && t.sessionID ? ` [s:${t.sessionID.slice(0, 8)}]` : ""
const preview = t.prompt.length > 60 ? t.prompt.slice(0, 60) + "..." : t.prompt
lines.push(` [${t.id}]${sessionTag} ${status} • ${interval} • ${preview}`)
}
lines.push(
`Manage: \`/loop cancel|pause|resume <id>\` (add \`--all\` to cross sessions) or \`/loop stop-all\``
)
return lines.join("\n")
},
loadDefaultPrompt(directory) {
const candidates = [
join(directory, ".opencode", "loop.md"),
join(directory, "loop.md"),
]
for (const p of candidates) {
if (existsSync(p)) {
try {
const content = readFileSync(p, "utf-8").trim()
if (content) return content
} catch {
// ignore
}
}
}
return DEFAULT_MAINTENANCE_PROMPT
},
async getDueTasks(now: number = Date.now()) {
return inst.opts.store.getDueTasks(now)
},
async getDueTasksForSession(sessionID: string, now: number = Date.now()) {
return inst.opts.store.getDueTasksForSession(sessionID, now)
},
async nextDueAt(task, now: number = Date.now()) {
if (task.mode === "fixed" && task.intervalMs) {
const jitter = inst.opts.jitter.compute(task.id, task.intervalMs, now)
return now + task.intervalMs + jitter
}
if (task.mode === "maintenance" && task.adaptiveMaxMs) {
return now + task.adaptiveMaxMs
}
if (task.mode === "adaptive" && task.adaptiveMaxMs) {
return now + task.adaptiveMaxMs
}
return now + 60_000
},
async fireTask(task, ctx) {
if (inst.inflight.has(task.id)) return
inst.inflight.add(task.id)
try {
const sessionID = task.sessionID
const text = task.prompt
const directory = task.directory || ctx?.directory || process.cwd()
const client = ctx?.client
if (!sessionID) {
await logger("warn", "task has no sessionID; skipping", { taskId: task.id })
await inst.opts.store.logFire(task, false)
return
}
if (!client?.session?.prompt) {
await logger("warn", "client.session.prompt not available", { taskId: task.id })
await inst.opts.store.logFire(task, false)
return
}
try {
await client.session.prompt({
path: { id: sessionID },
body: {
parts: [
{
type: "text",
text,
synthetic: true,
metadata: { loopTaskId: task.id, loopMode: task.mode },
},
],
},
query: { directory },
})
await inst.opts.store.logFire(task, true)
} catch (err) {
await inst.opts.store.logFire(task, false)
await logger("error", "failed to fire task", {
taskId: task.id,
error: errorMessage(err),
})
}
} finally {
inst.inflight.delete(task.id)
}
},
async rearmFixed(task, now: number = Date.now()) {
if (!task.intervalMs) return
const jitterMs = inst.opts.jitter.compute(task.id, task.intervalMs, now)
await inst.opts.store.reschedule(task.id, now + task.intervalMs + jitterMs)
},
clampAdaptive(ms) {
return Math.max(inst.opts.adaptiveMinMs, Math.min(inst.opts.adaptiveMaxMs, ms))
},
}
return inst
}
/**
* Default maintenance prompt — mirrors Claude Code's built-in.
*/
export const DEFAULT_MAINTENANCE_PROMPT = `Continue any unfinished work from this conversation. Tend to the current branch's pull request: review comments, failed CI runs, merge conflicts. Run cleanup passes such as bug hunts or simplification when nothing else is pending.
Do not start new initiatives outside the above scope. Irreversible actions such as pushing or deleting only proceed when they continue something the transcript already authorized. After completing the work, call loop_schedule(action="cancel", taskId="<your id>") to end the loop, or call loop_schedule(action="reschedule", taskId="<your id>", nextDueAtMs=<pick a value between 60000 and 3600000>) to continue.`