ework-daemon 0.4.54 → 0.4.56
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/package.json +1 -1
- package/src/opencode.ts +21 -18
- package/src/runtime/opencode-backend.ts +23 -0
- package/src/runtime/pi-backend.ts +5 -0
- package/src/runtime/types.ts +5 -0
package/package.json
CHANGED
package/src/opencode.ts
CHANGED
|
@@ -98,28 +98,24 @@ const RECENT_BOT_REPLY_THRESHOLD_MS = 5 * 60_000; // 5 minutes
|
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* Determine whether any non-system bot reply exists that is causally after
|
|
101
|
-
* `promptTime` (when provided)
|
|
102
|
-
*
|
|
103
|
-
*
|
|
101
|
+
* `promptTime` (when provided) AND within a 5-minute absolute window.
|
|
102
|
+
* The causal bound prevents preempt/nudge false-done detection (a reply from
|
|
103
|
+
* the previous run must not satisfy this run); the recency bound prevents an
|
|
104
|
+
* early ack from satisfying a long run that ended silently (awork policy).
|
|
105
|
+
* Exported for unit testing.
|
|
104
106
|
*/
|
|
105
107
|
export function hasRecentBotReply(
|
|
106
108
|
comments: TrackerComment[],
|
|
107
109
|
isBotUser: (author: string) => boolean,
|
|
108
110
|
promptTime?: number,
|
|
109
111
|
): boolean {
|
|
110
|
-
if (promptTime) {
|
|
111
|
-
return comments.some(c => {
|
|
112
|
-
if (!isBotUser(c.author) || c.body.startsWith(SYSTEM_PREFIX)) return false;
|
|
113
|
-
if (!c.createdAt) return false;
|
|
114
|
-
return new Date(c.createdAt).getTime() > promptTime;
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
112
|
const now = Date.now();
|
|
118
113
|
return comments.some(c => {
|
|
119
114
|
if (!isBotUser(c.author) || c.body.startsWith(SYSTEM_PREFIX)) return false;
|
|
120
|
-
if (!c.createdAt) return
|
|
121
|
-
const
|
|
122
|
-
|
|
115
|
+
if (!c.createdAt) return !promptTime;
|
|
116
|
+
const created = new Date(c.createdAt).getTime();
|
|
117
|
+
if (promptTime && created <= promptTime) return false;
|
|
118
|
+
return now - created < RECENT_BOT_REPLY_THRESHOLD_MS;
|
|
123
119
|
});
|
|
124
120
|
}
|
|
125
121
|
|
|
@@ -1361,12 +1357,19 @@ export class Engine {
|
|
|
1361
1357
|
const hasRecent = this.hasRecentBotReply(commentsNow, tracker, started ?? undefined);
|
|
1362
1358
|
|
|
1363
1359
|
if (hasRecent) {
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1360
|
+
const nowMs = Date.now();
|
|
1361
|
+
const matched = [...commentsNow].reverse().find(c => {
|
|
1362
|
+
if (!tracker.isBotUser(c.author) || this.isSystemComment(c) || !c.createdAt) return false;
|
|
1363
|
+
const created = new Date(c.createdAt).getTime();
|
|
1364
|
+
if (started && created <= started) return false;
|
|
1365
|
+
return nowMs - created < RECENT_BOT_REPLY_THRESHOLD_MS;
|
|
1366
|
+
});
|
|
1367
1367
|
log.info(`engine: [bot] reply found for ${k} after prompt (comment ${matched?.id ?? "?"} createdAt ${matched?.createdAt ?? "?"}), marking done`);
|
|
1368
|
-
if (matched && usedModel) {
|
|
1369
|
-
|
|
1368
|
+
if (matched && !usedModel) {
|
|
1369
|
+
const fromSession = await this.backend.lastSessionModel(session.opencodeSessionId).catch(() => ({ model: "" }));
|
|
1370
|
+
if (fromSession.model) {
|
|
1371
|
+
void tracker.setCommentModel(ref, matched.id, fromSession.model).catch(() => { /* display-only */ });
|
|
1372
|
+
}
|
|
1370
1373
|
}
|
|
1371
1374
|
this.nudgeRounds.delete(k);
|
|
1372
1375
|
this.emptyResponseRounds.delete(k);
|
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
RuntimeSpawnCallbacks,
|
|
8
8
|
RuntimeHandle,
|
|
9
9
|
SessionOutputResult,
|
|
10
|
+
LastModelResult,
|
|
10
11
|
} from "./types";
|
|
11
12
|
|
|
12
13
|
const ENV_DENY_ALWAYS = ["OPENCODE", "OPENCODE_PID", "OPENCODE_RUN_ID", "OPENCODE_PROCESS_ROLE"] as const;
|
|
@@ -105,6 +106,28 @@ export class OpencodeBackend implements RuntimeBackend {
|
|
|
105
106
|
}
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
async lastSessionModel(sessionId: string | undefined): Promise<LastModelResult> {
|
|
110
|
+
if (!sessionId) return { model: "" };
|
|
111
|
+
let db: Database;
|
|
112
|
+
try {
|
|
113
|
+
db = new Database(this.dbPath, { readonly: true });
|
|
114
|
+
} catch {
|
|
115
|
+
return { model: "" };
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const row = db.prepare(
|
|
119
|
+
"SELECT json_extract(data,'$.modelID') AS model FROM message " +
|
|
120
|
+
"WHERE session_id = ? AND json_extract(data,'$.modelID') IS NOT NULL AND json_extract(data,'$.modelID') != '' " +
|
|
121
|
+
"ORDER BY time_created DESC LIMIT 1"
|
|
122
|
+
).get(sessionId) as { model: string | null } | null;
|
|
123
|
+
return { model: row?.model ?? "" };
|
|
124
|
+
} catch {
|
|
125
|
+
return { model: "" };
|
|
126
|
+
} finally {
|
|
127
|
+
db.close();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
108
131
|
async getSessionOutputTokens(sessionId: string | undefined): Promise<SessionOutputResult> {
|
|
109
132
|
if (!sessionId) return { hasOutput: true, tokenCount: 0 };
|
|
110
133
|
let db: Database;
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
RuntimeSpawnCallbacks,
|
|
9
9
|
RuntimeHandle,
|
|
10
10
|
SessionOutputResult,
|
|
11
|
+
LastModelResult,
|
|
11
12
|
} from "./types";
|
|
12
13
|
|
|
13
14
|
export class PiBackend implements RuntimeBackend {
|
|
@@ -98,6 +99,10 @@ export class PiBackend implements RuntimeBackend {
|
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
async lastSessionModel(_sessionId: string | undefined): Promise<LastModelResult> {
|
|
103
|
+
return { model: "" };
|
|
104
|
+
}
|
|
105
|
+
|
|
101
106
|
async sessionExists(sessionId: string): Promise<boolean> {
|
|
102
107
|
if (!sessionId) return false;
|
|
103
108
|
return this.findSessionFile(sessionId) !== null;
|
package/src/runtime/types.ts
CHANGED
|
@@ -11,6 +11,10 @@ export interface RuntimeSpawnCallbacks {
|
|
|
11
11
|
onSessionId: (id: string) => Promise<void> | void;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
export interface LastModelResult {
|
|
15
|
+
model: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
export interface RuntimeHandle {
|
|
15
19
|
pid: number;
|
|
16
20
|
exited: Promise<number>;
|
|
@@ -26,6 +30,7 @@ export interface RuntimeBackend {
|
|
|
26
30
|
readonly name: string;
|
|
27
31
|
|
|
28
32
|
spawn(opts: RuntimeSpawnOpts, callbacks: RuntimeSpawnCallbacks): Promise<RuntimeHandle>;
|
|
33
|
+
lastSessionModel(sessionId: string | undefined): Promise<LastModelResult>;
|
|
29
34
|
|
|
30
35
|
sessionExists(sessionId: string): Promise<boolean>;
|
|
31
36
|
|