-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencodeCore.ts
More file actions
485 lines (415 loc) · 16.8 KB
/
Copy pathopencodeCore.ts
File metadata and controls
485 lines (415 loc) · 16.8 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { maybeHandleYoloCommand } from "./commands"
import { replyForAssistantText, AGGRESSIVE_FALLBACK } from "./isQuestion"
import type { YoloMode } from "./state"
type HookEvent = {
event: {
type: string
properties?: {
info?: {
id?: string
sessionID?: string
role?: string
time?: {
completed?: number
}
}
name?: string
sessionID?: string
arguments?: string
messageID?: string
status?: {
type?: string
}
}
}
}
type ChatMessageHook = {
sessionID?: string
}
type ChatMessageOutput = {
message?: {
id?: string
sessionID?: string
role?: string
}
parts?: Array<{ type?: string; text?: string }>
}
export interface QuestionInfo {
question: string
header: string
options: Array<{ label: string; description: string }>
multiple?: boolean
custom?: boolean
}
export interface QuestionRequest {
id: string
sessionID: string
questions: QuestionInfo[]
}
export interface RuntimeDeps {
readMode: () => Promise<YoloMode>
writeMode: (mode: YoloMode) => Promise<void>
loadMessageText: (sessionID: string, messageID: string) => Promise<string>
sendUserMessage: (sessionID: string, text: string) => Promise<void>
answerQuestion: (requestID: string, answers: string[][]) => Promise<void>
}
export function textFromParts(parts: Array<{ type?: string; text?: string }> = []): string {
return parts
.filter((part) => part.type === "text" && typeof part.text === "string")
.map((part) => part.text!.trim())
.filter(Boolean)
.join("\n")
}
export const IDLE_DELAY_MS = 350
export const WATCHDOG_STALE_MS = 1_000
export const HUMAN_TURN_TIMEOUT_MS = 10_000
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000
import { appendFileSync } from "node:fs"
const YOLO_LOG_FILE = "/tmp/yolo-debug.log"
function yoloLog(...args: unknown[]) {
const line = `${new Date().toISOString()} [YOLO] ${args.map(a => typeof a === "string" ? a : JSON.stringify(a)).join(" ")}\n`
try { appendFileSync(YOLO_LOG_FILE, line) } catch {}
}
export async function createOpencodeYoloHooks(deps: RuntimeDeps) {
let mode = await deps.readMode()
yoloLog("plugin loaded, mode:", mode)
// --- Per-session state ---
const waitingForHumanTurnBySession = new Set<string>()
const seenAssistantMessages = new Set<string>()
const pendingSyntheticUserBySession = new Set<string>()
const activeYoloCommandSessions = new Set<string>()
// --- Idle scheduling (notifier pattern) + watchdog fallback ---
const pendingReplies = new Map<string, string>()
const pendingReplySetAt = new Map<string, number>()
const idleTimers = new Map<string, ReturnType<typeof setTimeout>>()
const idleSequence = new Map<string, number>()
const errorSuppressionAt = new Map<string, number>()
const lastBusyAt = new Map<string, number>()
const humanTurnTimers = new Map<string, ReturnType<typeof setTimeout>>()
// Memory cleanup: remove stale session entries every 5 minutes
const cleanupTimer = setInterval(() => {
const cutoff = Date.now() - CLEANUP_INTERVAL_MS
for (const [sessionID] of idleSequence) {
if (!idleTimers.has(sessionID)) {
idleSequence.delete(sessionID)
}
}
for (const [sessionID, timestamp] of errorSuppressionAt) {
if (timestamp < cutoff) errorSuppressionAt.delete(sessionID)
}
for (const [sessionID, timestamp] of lastBusyAt) {
if (timestamp < cutoff) lastBusyAt.delete(sessionID)
}
}, CLEANUP_INTERVAL_MS)
// Allow GC if the plugin is unloaded (Node won't hold the process open)
if (typeof cleanupTimer === "object" && cleanupTimer !== null && "unref" in (cleanupTimer as any)) {
;(cleanupTimer as any).unref()
}
function bumpSequence(sessionID: string): number {
const next = (idleSequence.get(sessionID) ?? 0) + 1
idleSequence.set(sessionID, next)
return next
}
function hasCurrentSequence(sessionID: string, seq: number): boolean {
return idleSequence.get(sessionID) === seq
}
function clearIdleTimer(sessionID: string) {
const timer = idleTimers.get(sessionID)
if (timer) {
clearTimeout(timer)
idleTimers.delete(sessionID)
}
}
function cancelPendingReply(sessionID: string) {
pendingReplies.delete(sessionID)
pendingReplySetAt.delete(sessionID)
bumpSequence(sessionID)
clearIdleTimer(sessionID)
}
function markBusy(sessionID: string) {
yoloLog("markBusy", sessionID)
lastBusyAt.set(sessionID, Date.now())
errorSuppressionAt.delete(sessionID)
cancelPendingReply(sessionID)
}
function markError(sessionID: string) {
errorSuppressionAt.set(sessionID, Date.now())
cancelPendingReply(sessionID)
}
function shouldSuppressIdle(sessionID: string): boolean {
const errorAt = errorSuppressionAt.get(sessionID)
if (errorAt === undefined) return false
const busyAt = lastBusyAt.get(sessionID)
if (typeof busyAt === "number" && busyAt > errorAt) {
errorSuppressionAt.delete(sessionID)
return false
}
errorSuppressionAt.delete(sessionID)
return true
}
function clearHumanTurnTimer(sessionID: string) {
const timer = humanTurnTimers.get(sessionID)
if (timer) {
clearTimeout(timer)
humanTurnTimers.delete(sessionID)
}
}
function startHumanTurnTimeout(sessionID: string) {
clearHumanTurnTimer(sessionID)
const timer = setTimeout(() => {
humanTurnTimers.delete(sessionID)
// promptAsync delivery failed silently — force-clear guards so plugin resumes
waitingForHumanTurnBySession.delete(sessionID)
pendingSyntheticUserBySession.delete(sessionID)
}, HUMAN_TURN_TIMEOUT_MS)
humanTurnTimers.set(sessionID, timer)
}
function humanTookOver(sessionID: string) {
cancelPendingReply(sessionID)
clearHumanTurnTimer(sessionID)
if (pendingSyntheticUserBySession.has(sessionID)) {
pendingSyntheticUserBySession.delete(sessionID)
} else {
waitingForHumanTurnBySession.delete(sessionID)
}
}
async function deliverReply(sessionID: string) {
const pendingReply = pendingReplies.get(sessionID)
if (!pendingReply) { yoloLog("DELIVER SKIP: no pendingReply", sessionID); return }
pendingReplies.delete(sessionID)
pendingReplySetAt.delete(sessionID)
if (mode === "off") { yoloLog("DELIVER SKIP: mode is off", sessionID); return }
if (shouldSuppressIdle(sessionID)) { yoloLog("DELIVER SKIP: error suppression", sessionID); return }
if (waitingForHumanTurnBySession.has(sessionID)) { yoloLog("DELIVER SKIP: waitingForHumanTurn", sessionID); return }
try {
yoloLog("SENDING reply to", sessionID, pendingReply.substring(0, 40) + "...")
waitingForHumanTurnBySession.add(sessionID)
pendingSyntheticUserBySession.add(sessionID)
startHumanTurnTimeout(sessionID)
await deps.sendUserMessage(sessionID, pendingReply)
yoloLog("SENT reply to", sessionID)
} catch (err) {
yoloLog("SEND ERROR", sessionID, String(err))
waitingForHumanTurnBySession.delete(sessionID)
pendingSyntheticUserBySession.delete(sessionID)
}
}
function checkWatchdog() {
// Fallback: if setTimeout callbacks aren't firing, deliver stale pending replies
// directly from the event stream. Any incoming event triggers this check.
const now = Date.now()
for (const [sessionID, setAt] of pendingReplySetAt) {
if (now - setAt < WATCHDOG_STALE_MS) continue
if (!pendingReplies.has(sessionID)) { pendingReplySetAt.delete(sessionID); continue }
if (waitingForHumanTurnBySession.has(sessionID)) continue
if (shouldSuppressIdle(sessionID)) { cancelPendingReply(sessionID); continue }
yoloLog("WATCHDOG: stale reply detected for", sessionID, "age:", now - setAt, "ms — delivering now")
clearIdleTimer(sessionID)
void deliverReply(sessionID)
}
}
function scheduleReplyDelivery(sessionID: string) {
// Notifier pattern: wait IDLE_DELAY_MS after session.idle before delivering.
// This ensures OpenCode has fully transitioned to idle before we send a new message.
clearIdleTimer(sessionID)
const seq = bumpSequence(sessionID)
yoloLog("scheduleReplyDelivery: scheduling timer for", sessionID, "seq:", seq)
const timer = setTimeout(() => {
idleTimers.delete(sessionID)
if (!hasCurrentSequence(sessionID, seq)) {
yoloLog("DELIVER SKIP: sequence mismatch", sessionID, "expected:", seq, "current:", idleSequence.get(sessionID))
return
}
void deliverReply(sessionID)
}, IDLE_DELAY_MS)
idleTimers.set(sessionID, timer)
}
return {
"command.execute.before": async (
input: { command: string; sessionID: string; arguments: string },
output: { parts: Array<{ type: string; text?: string }> },
) => {
yoloLog("command.execute.before:", input.command, "args:", input.arguments)
if (input.command !== "yolo") return
activeYoloCommandSessions.add(input.sessionID)
const args = input.arguments.trim().toLowerCase()
// /yolo start: send the go-to-work prompt without changing mode
if (args === "start") {
if (mode === "off") {
output.parts = [{ type: "text", text: "YOLO mode is off. Enable it first with /yolo on or /yolo aggressive." }]
return
}
yoloLog("command /yolo start: queuing prompt for", input.sessionID)
pendingReplies.set(input.sessionID, AGGRESSIVE_FALLBACK)
pendingReplySetAt.set(input.sessionID, Date.now())
// Delivery will happen when session.idle fires after this command completes
output.parts = [{ type: "text", text: "YOLO: kicking off work." }]
return
}
const commandText = args ? `/yolo ${args}` : "/yolo"
yoloLog("command.execute.before: calling maybeHandleYoloCommand with:", commandText)
const result = await maybeHandleYoloCommand(commandText, {
readMode: deps.readMode,
writeMode: deps.writeMode,
})
yoloLog("command.execute.before: result:", result.handled, result.mode)
if (result.handled && result.mode) {
mode = result.mode
yoloLog("command.execute.before: mode updated to:", mode)
}
const statusLine =
mode === "aggressive"
? "YOLO mode enabled: aggressive."
: mode === "on"
? "YOLO mode enabled: normal."
: "YOLO mode disabled."
output.parts = [
{
type: "text",
text: statusLine,
},
]
},
"permission.ask": async (
input: { type: string; pattern?: string | string[]; sessionID: string },
output: { status: "ask" | "deny" | "allow" },
) => {
const patterns = Array.isArray(input.pattern) ? input.pattern : input.pattern ? [input.pattern] : []
const isYoloShellCall = patterns.some((pattern) => /(^|\s)\/?yolo(\s|$)/.test(pattern))
const isActiveYoloCommandSession = activeYoloCommandSessions.has(input.sessionID)
if (isActiveYoloCommandSession && input.type !== "question") {
output.status = "deny"
return
}
if (input.type !== "bash") return
if (!isYoloShellCall && !isActiveYoloCommandSession) return
output.status = "deny"
},
"tool.execute.before": async (
input: { tool: string; sessionID: string; callID: string },
output: { args: any },
) => {
if (!activeYoloCommandSessions.has(input.sessionID)) return
if (input.tool !== "bash") return
output.args = {
...(output.args ?? {}),
command: ":",
description: "No-op during yolo command",
}
},
"chat.message": async (_input: ChatMessageHook, output: ChatMessageOutput) => {
yoloLog("chat.message:", output.message?.role, textFromParts(output.parts)?.substring(0, 60))
if (output.message?.role !== "user") return
const sessionID = output.message.sessionID
if (sessionID) {
humanTookOver(sessionID)
}
const text = textFromParts(output.parts)
if (!text) return
const result = await maybeHandleYoloCommand(text, {
readMode: deps.readMode,
writeMode: deps.writeMode,
})
yoloLog("chat.message command result:", result.handled, result.mode)
if (result.handled && result.mode) {
mode = result.mode
yoloLog("chat.message updated mode to:", mode)
}
},
event: async ({ event }: HookEvent) => {
// Watchdog: on every event, check for stale pending replies whose timers didn't fire
checkWatchdog()
yoloLog("event:", event.type, JSON.stringify(event.properties?.sessionID ?? event.properties?.info?.sessionID ?? "no-sid"))
if (event.type === "command.executed") {
const sessionID = event.properties?.sessionID
const name = event.properties?.name
if (name === "yolo" && sessionID) {
activeYoloCommandSessions.delete(sessionID)
}
return
}
// question.asked: auto-answer multiple choice questions
if (event.type === "question.asked") {
if (mode === "off") return
const request = event.properties as unknown as QuestionRequest | undefined
if (!request?.id || !request?.questions?.length) return
yoloLog("question.asked:", request.id, "questions:", request.questions.length)
// For each question, pick the first option (or all if multiple)
const answers: string[][] = request.questions.map((q) => {
if (!q.options?.length) return []
if (q.multiple) return q.options.map((o) => o.label)
return [q.options[0].label]
})
yoloLog("question.asked: auto-answering with:", JSON.stringify(answers))
try {
await deps.answerQuestion(request.id, answers)
yoloLog("question.asked: answered", request.id)
} catch (err) {
yoloLog("question.asked: FAILED to answer", request.id, String(err))
}
return
}
// session.status busy: cancel pending reply, bump sequence
if (event.type === "session.status" && event.properties?.status?.type === "busy") {
const sessionID = event.properties?.sessionID
if (sessionID) markBusy(sessionID)
return
}
// session.error: suppress next idle for this session
if (event.type === "session.error") {
const sessionID = event.properties?.sessionID
if (sessionID) markError(sessionID)
return
}
// session.idle: deliver pending reply immediately
// No setTimeout — timers are unreliable in this environment.
// session.idle already means OpenCode is fully idle and ready for input.
if (event.type === "session.idle") {
const sessionID = event.properties?.sessionID
if (!sessionID) return
const reply = pendingReplies.get(sessionID)
yoloLog("session.idle", sessionID, "pendingReply:", !!reply, "waitingForHuman:", waitingForHumanTurnBySession.has(sessionID))
if (!reply) return
if (waitingForHumanTurnBySession.has(sessionID)) {
yoloLog("session.idle BLOCKED by waitingForHumanTurn")
cancelPendingReply(sessionID)
return
}
if (shouldSuppressIdle(sessionID)) {
yoloLog("session.idle SUPPRESSED by error")
cancelPendingReply(sessionID)
return
}
yoloLog("session.idle: delivering reply NOW for", sessionID)
await deliverReply(sessionID)
return
}
if (event.type !== "message.updated") return
const info = event.properties?.info
if (!info?.sessionID) return
if (info.role === "user") {
humanTookOver(info.sessionID)
return
}
if (mode === "off") { yoloLog("SKIP: mode off"); return }
if (info.role !== "assistant") return
if (!info.id) return
if (!info.time?.completed) return
if (waitingForHumanTurnBySession.has(info.sessionID)) { yoloLog("SKIP: waitingForHumanTurn", info.sessionID); return }
if (seenAssistantMessages.has(info.id)) { yoloLog("SKIP: already seen", info.id); return }
seenAssistantMessages.add(info.id)
const text = await deps.loadMessageText(info.sessionID, info.id)
const classifiedReply = replyForAssistantText(text)
const reply = classifiedReply ?? (mode === "aggressive" ? AGGRESSIVE_FALLBACK : undefined)
yoloLog("message.updated classified:", info.id, "reply:", reply ? reply.substring(0, 40) + "..." : "NONE", "mode:", mode)
if (!reply) return
// Store pending reply — delivery is triggered by session.idle (notifier pattern).
// We do NOT deliver here; we wait for OpenCode to fully go idle first.
// Watchdog fallback: if the timer doesn't fire within WATCHDOG_STALE_MS,
// the next incoming event will deliver it directly.
pendingReplies.set(info.sessionID, reply)
pendingReplySetAt.set(info.sessionID, Date.now())
yoloLog("pendingReply SET for", info.sessionID, "(waiting for session.idle)")
},
}
}