ework-daemon 0.4.16 → 0.4.18

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ework-daemon",
3
- "version": "0.4.16",
3
+ "version": "0.4.18",
4
4
  "description": "Issue-driven AI development daemon. Spawns opencode subprocesses to resolve Gitea issues.",
5
5
  "module": "src/index.ts",
6
6
  "type": "module",
@@ -70,10 +70,76 @@ export class OpencodeReader {
70
70
  }
71
71
 
72
72
  async exportSession(id: string): Promise<unknown> {
73
+ try {
74
+ const fromDB = this.exportSessionFromDB(id);
75
+ if (fromDB) return fromDB;
76
+ } catch {
77
+ // DB read failed (schema drift, locked, etc.) — fall back to CLI
78
+ }
73
79
  const raw = await this.runJSON(["export", id]);
74
80
  return raw;
75
81
  }
76
82
 
83
+ private exportSessionFromDB(id: string): unknown | null {
84
+ let db: Database;
85
+ try {
86
+ db = new Database(this.dbPath, { readonly: true });
87
+ } catch {
88
+ return null;
89
+ }
90
+ try {
91
+ const srow = db
92
+ .prepare("SELECT id, title, directory, version, time_created, time_updated FROM session WHERE id = ?")
93
+ .get(id) as { id: string; title: string; directory: string; version: string; time_created: number; time_updated: number } | null;
94
+ if (!srow) return null;
95
+
96
+ const mrows = db
97
+ .prepare("SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id")
98
+ .all(id) as Array<{ id: string; data: string }>;
99
+ const prows = db
100
+ .prepare("SELECT message_id, data FROM part WHERE session_id = ? ORDER BY message_id, id")
101
+ .all(id) as Array<{ message_id: string; data: string }>;
102
+
103
+ const partsByMsg = new Map<string, unknown[]>();
104
+ for (const p of prows) {
105
+ let pd: unknown;
106
+ try { pd = JSON.parse(p.data); } catch { continue; }
107
+ if (!pd || typeof pd !== "object") continue;
108
+ const arr = partsByMsg.get(p.message_id);
109
+ if (arr) arr.push(pd); else partsByMsg.set(p.message_id, [pd]);
110
+ }
111
+
112
+ const messages: unknown[] = [];
113
+ for (const m of mrows) {
114
+ let md: Record<string, unknown>;
115
+ try { md = JSON.parse(m.data); } catch { continue; }
116
+ const info: Record<string, unknown> = { id: m.id, role: md.role };
117
+ if (md.agent) info.agent = md.agent;
118
+ const modelID = typeof md.modelID === "string" ? md.modelID : (typeof md.model === "string" ? md.model : undefined);
119
+ if (modelID) info.modelID = modelID;
120
+ const tRaw = md.time && typeof md.time === "object" ? (md.time as Record<string, unknown>) : null;
121
+ if (tRaw && typeof tRaw.created === "number") info.time = { created: tRaw.created };
122
+ if (md.tokens) info.tokens = md.tokens;
123
+ messages.push({ info, parts: partsByMsg.get(m.id) ?? [] });
124
+ }
125
+
126
+ return {
127
+ info: {
128
+ id: srow.id,
129
+ title: srow.title || "(untitled)",
130
+ directory: srow.directory ?? "",
131
+ version: srow.version ?? "",
132
+ time: { created: srow.time_created, updated: srow.time_updated },
133
+ },
134
+ messages,
135
+ };
136
+ } catch {
137
+ return null;
138
+ } finally {
139
+ db.close();
140
+ }
141
+ }
142
+
77
143
  async exportSessionRaw(id: string): Promise<string> {
78
144
  const { stdout, code } = await this.run(["export", id]);
79
145
  if (code !== 0) {
package/src/opencode.ts CHANGED
@@ -153,6 +153,23 @@ export async function checkSessionOutput(
153
153
  }
154
154
  }
155
155
 
156
+ export async function opencodeSessionExists(dbPath: string, sessionId: string): Promise<boolean> {
157
+ let db: Database;
158
+ try {
159
+ db = new Database(dbPath, { readonly: true });
160
+ } catch {
161
+ return false;
162
+ }
163
+ try {
164
+ const row = db.prepare("SELECT 1 FROM session WHERE id = ? LIMIT 1").get(sessionId);
165
+ return !!row;
166
+ } catch {
167
+ return false;
168
+ } finally {
169
+ db.close();
170
+ }
171
+ }
172
+
156
173
  /**
157
174
  * Default TakeoverStrategy: deterministic per-issue workdir under
158
175
  * `<baseWorkdir>/<owner>--<repo>/<issueId>/<sessionName>`, with a best-effort
@@ -885,6 +902,11 @@ export class Engine {
885
902
  const fromStrategy = await this.takeover.resumeOpenCodeSession(session);
886
903
  if (fromStrategy) resumeSessionId = fromStrategy;
887
904
  }
905
+ if (resumeSessionId && !(await opencodeSessionExists(this.cfg.opencode.dbPath, resumeSessionId))) {
906
+ log.warn(`stale opencode session ${resumeSessionId} not found in db, starting fresh`);
907
+ resumeSessionId = undefined;
908
+ await this.store.updateSession(session.id, { opencodeSessionId: undefined });
909
+ }
888
910
  if (resumeSessionId) {
889
911
  args.push("--session", resumeSessionId);
890
912
  }