Skip to content

Commit a9ece98

Browse files
Revert "fix(app): fix memory leaks, environment compatibility, and error recovery (anomalyco#493)"
This reverts commit 7989e1a.
1 parent 504cad0 commit a9ece98

7 files changed

Lines changed: 75 additions & 230 deletions

File tree

‎packages/app/src/app/app.tsx‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ export default function App() {
663663
setSessionStatusById,
664664
setMessages,
665665
setTodos,
666-
clearSessionCaches,
667666
setPendingPermissions,
668667
} = sessionStore;
669668

@@ -1424,7 +1423,6 @@ export default function App() {
14241423
setSelectedSessionId,
14251424
setMessages,
14261425
setTodos,
1427-
clearSessionCaches,
14281426
setPendingPermissions,
14291427
setSessionStatusById,
14301428
defaultModel,

‎packages/app/src/app/context/server.tsx‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,6 @@ type ServerContextValue = {
2828

2929
const ServerContext = createContext<ServerContextValue | undefined>(undefined);
3030

31-
function createTimeoutSignal(timeoutMs: number) {
32-
if (typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function") {
33-
return {
34-
signal: AbortSignal.timeout(timeoutMs),
35-
cancel: () => undefined,
36-
};
37-
}
38-
39-
const controller = new AbortController();
40-
const timer = globalThis.setTimeout(() => controller.abort(), timeoutMs);
41-
42-
return {
43-
signal: controller.signal,
44-
cancel: () => globalThis.clearTimeout(timer),
45-
};
46-
}
47-
4831
export function ServerProvider(props: ParentProps & { defaultUrl: string }) {
4932
const [list, setList] = createSignal<string[]>([]);
5033
const [active, setActiveRaw] = createSignal("");
@@ -102,17 +85,15 @@ export function ServerProvider(props: ParentProps & { defaultUrl: string }) {
10285

10386
const checkHealth = async (url: string) => {
10487
if (!url) return false;
105-
const timeout = createTimeoutSignal(3000);
10688
const client = createOpencodeClient({
10789
baseUrl: url,
108-
signal: timeout.signal,
90+
signal: AbortSignal.timeout(3000),
10991
fetch: isTauriRuntime() ? tauriFetch : undefined,
11092
});
11193
return client.global
11294
.health()
11395
.then((result) => result.data?.healthy === true)
114-
.catch(() => false)
115-
.finally(() => timeout.cancel());
96+
.catch(() => false);
11697
};
11798

11899
createEffect(() => {

‎packages/app/src/app/context/session.ts‎

Lines changed: 56 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,6 @@ const upsertPartInfo = (list: Part[], next: Part) => {
105105

106106
const removePartInfo = (list: Part[], partID: string) => list.filter((part) => part.id !== partID);
107107

108-
const pruneRecordKeys = <T>(record: Record<string, T>, keep: Set<string>) => {
109-
let changed = false;
110-
const next: Record<string, T> = {};
111-
112-
for (const [key, value] of Object.entries(record)) {
113-
if (!keep.has(key)) {
114-
changed = true;
115-
continue;
116-
}
117-
next[key] = value;
118-
}
119-
120-
return changed ? next : record;
121-
};
122-
123108
export function createSessionStore(options: {
124109
client: () => Client | null;
125110
activeWorkspaceRoot: () => string;
@@ -163,45 +148,6 @@ export function createSessionStore(options: {
163148
const [permissionReplyBusy, setPermissionReplyBusy] = createSignal(false);
164149
const reloadDetectionSet = new Set<string>();
165150

166-
const messageIDFromReloadKey = (key: string) => {
167-
const separator = key.indexOf(":");
168-
if (separator < 0) return "";
169-
return key.slice(0, separator);
170-
};
171-
172-
const trimReloadDetectionSet = () => {
173-
if (reloadDetectionSet.size <= MAX_RELOAD_DETECTION_KEYS) return;
174-
const targetSize = Math.floor(MAX_RELOAD_DETECTION_KEYS * 0.8);
175-
const overflow = reloadDetectionSet.size - targetSize;
176-
if (overflow <= 0) return;
177-
178-
const iterator = reloadDetectionSet.values();
179-
for (let i = 0; i < overflow; i += 1) {
180-
const next = iterator.next();
181-
if (next.done) break;
182-
reloadDetectionSet.delete(next.value);
183-
}
184-
};
185-
186-
const pruneReloadDetectionSetByMessageIDs = (keepMessageIDs: Set<string>) => {
187-
if (reloadDetectionSet.size === 0) return;
188-
for (const key of reloadDetectionSet) {
189-
const messageID = messageIDFromReloadKey(key);
190-
if (!messageID || keepMessageIDs.has(messageID)) continue;
191-
reloadDetectionSet.delete(key);
192-
}
193-
};
194-
195-
const removeReloadDetectionKeysForMessageIDs = (removedMessageIDs: Set<string>) => {
196-
if (reloadDetectionSet.size === 0 || removedMessageIDs.size === 0) return;
197-
for (const key of reloadDetectionSet) {
198-
const messageID = messageIDFromReloadKey(key);
199-
if (messageID && removedMessageIDs.has(messageID)) {
200-
reloadDetectionSet.delete(key);
201-
}
202-
}
203-
};
204-
205151
const skillPathPattern = /[\\/]\.opencode[\\/](skill|skills)[\\/]/i;
206152
const skillNamePattern = /[\\/]\.opencode[\\/](?:skill|skills)[\\/]+([^\\/]+)/i;
207153
const commandPathPattern = /[\\/]\.opencode[\\/](command|commands)[\\/]/i;
@@ -213,16 +159,15 @@ export function createSessionStore(options: {
213159
const openworkConfigPattern = /[\\/]\.opencode[\\/]openwork\.json\b/i;
214160
const mutatingTools = new Set(["write", "edit", "apply_patch"]);
215161

216-
const MAX_SEARCH_TEXT_LENGTH = 20_000;
217-
218162
const extractSearchText = (value: unknown) => {
219-
if (value === null || value === undefined) return "";
220-
if (typeof value === "string") return value.slice(0, MAX_SEARCH_TEXT_LENGTH);
221-
if (typeof value === "number" || typeof value === "boolean") return String(value);
222-
return safeStringify(value).slice(0, MAX_SEARCH_TEXT_LENGTH);
163+
if (!value) return "";
164+
if (typeof value === "string") return value;
165+
if (typeof value === "number") return String(value);
166+
return safeStringify(value);
223167
};
224168

225-
const detectReloadReasonFromText = (text: string): ReloadReason | null => {
169+
const detectReloadReason = (value: unknown): ReloadReason | null => {
170+
const text = extractSearchText(value);
226171
if (!text) return null;
227172
if (openworkConfigPattern.test(text)) return null;
228173
if (skillPathPattern.test(text)) return "skills";
@@ -278,27 +223,63 @@ export function createSessionStore(options: {
278223
return null;
279224
};
280225

226+
const detectReloadReasonDeep = (value: unknown): ReloadReason | null => {
227+
if (!value) return null;
228+
if (typeof value === "string" || typeof value === "number") {
229+
return detectReloadReason(value);
230+
}
231+
if (Array.isArray(value)) {
232+
for (const entry of value) {
233+
const reason = detectReloadReasonDeep(entry);
234+
if (reason) return reason;
235+
}
236+
return null;
237+
}
238+
if (typeof value === "object") {
239+
for (const entry of Object.values(value as Record<string, unknown>)) {
240+
const reason = detectReloadReasonDeep(entry);
241+
if (reason) return reason;
242+
}
243+
}
244+
return null;
245+
};
246+
247+
const detectReloadTriggerDeep = (value: unknown): ReloadTrigger | null => {
248+
if (!value) return null;
249+
if (typeof value === "string" || typeof value === "number") {
250+
return detectReloadTriggerFromText(String(value));
251+
}
252+
if (Array.isArray(value)) {
253+
for (const entry of value) {
254+
const trigger = detectReloadTriggerDeep(entry);
255+
if (trigger) return trigger;
256+
}
257+
return null;
258+
}
259+
if (typeof value === "object") {
260+
for (const entry of Object.values(value as Record<string, unknown>)) {
261+
const trigger = detectReloadTriggerDeep(entry);
262+
if (trigger) return trigger;
263+
}
264+
}
265+
return null;
266+
};
267+
281268
const detectReloadFromPart = (part: Part): { reason: ReloadReason; trigger?: ReloadTrigger } | null => {
282269
if (part.type !== "tool") return null;
283270
const record = part as Record<string, unknown>;
284271
const toolName = typeof record.tool === "string" ? record.tool : "";
285272
if (!mutatingTools.has(toolName)) return null;
286273
const state = (record.state ?? {}) as Record<string, unknown>;
287-
288-
const inputText = extractSearchText(state.input);
289-
const patchText = extractSearchText(state.patch);
290-
const diffText = extractSearchText(state.diff);
291-
292274
const reason =
293-
detectReloadReasonFromText(inputText) ||
294-
detectReloadReasonFromText(patchText) ||
295-
detectReloadReasonFromText(diffText);
275+
detectReloadReasonDeep(state.input) ||
276+
detectReloadReasonDeep(state.patch) ||
277+
detectReloadReasonDeep(state.diff);
296278
if (!reason) return null;
297-
298279
const trigger =
299-
detectReloadTriggerFromText(inputText) ||
300-
detectReloadTriggerFromText(patchText) ||
301-
detectReloadTriggerFromText(diffText);
280+
detectReloadTriggerDeep(state.input) ||
281+
detectReloadTriggerDeep(state.patch) ||
282+
detectReloadTriggerDeep(state.diff);
302283
return { reason, trigger: trigger ?? undefined };
303284
};
304285

@@ -315,13 +296,11 @@ export function createSessionStore(options: {
315296
}
316297
}
317298

318-
const detection = detectReloadFromPart(part);
319-
if (!detection) return;
320-
321299
const key = `${part.messageID}:${part.id}`;
322300
if (reloadDetectionSet.has(key)) return;
301+
const detection = detectReloadFromPart(part);
302+
if (!detection) return;
323303
reloadDetectionSet.add(key);
324-
trimReloadDetectionSet();
325304
options.markReloadRequired(detection.reason, detection.trigger);
326305
};
327306

@@ -376,51 +355,6 @@ export function createSessionStore(options: {
376355
return store.todos[id] ?? [];
377356
});
378357

379-
const pruneSessionCaches = (sessionIDs: Set<string>) => {
380-
const keepMessageIDs = new Set<string>();
381-
382-
setStore(
383-
produce((draft: StoreState) => {
384-
draft.sessionStatus = pruneRecordKeys(draft.sessionStatus, sessionIDs);
385-
draft.messages = pruneRecordKeys(draft.messages, sessionIDs);
386-
draft.todos = pruneRecordKeys(draft.todos, sessionIDs);
387-
388-
for (const messages of Object.values(draft.messages)) {
389-
for (const info of messages ?? []) {
390-
if (info?.id) keepMessageIDs.add(info.id);
391-
}
392-
}
393-
394-
draft.parts = pruneRecordKeys(draft.parts, keepMessageIDs);
395-
}),
396-
);
397-
398-
pruneReloadDetectionSetByMessageIDs(keepMessageIDs);
399-
trimReloadDetectionSet();
400-
};
401-
402-
const removeSessionCaches = (sessionID: string) => {
403-
const removedMessageIDs = new Set<string>();
404-
405-
setStore(
406-
produce((draft: StoreState) => {
407-
const messageIDs = (draft.messages[sessionID] ?? []).map((message) => message.id);
408-
for (const messageID of messageIDs) {
409-
removedMessageIDs.add(messageID);
410-
}
411-
delete draft.sessionStatus[sessionID];
412-
delete draft.messages[sessionID];
413-
delete draft.todos[sessionID];
414-
for (const messageID of messageIDs) {
415-
delete draft.parts[messageID];
416-
}
417-
}),
418-
);
419-
420-
removeReloadDetectionKeysForMessageIDs(removedMessageIDs);
421-
trimReloadDetectionSet();
422-
};
423-
424358
async function loadSessions(scopeRoot?: string) {
425359
const c = options.client();
426360
if (!c) return;
@@ -452,7 +386,6 @@ export function createSessionStore(options: {
452386
: list;
453387
sessionDebug("sessions:load:filtered", { root: root || null, count: filtered.length });
454388
setStore("sessions", reconcile(sortSessionsByActivity(filtered), { key: "id" }));
455-
pruneSessionCaches(new Set(filtered.map((session) => session.id)));
456389
}
457390

458391
async function renameSession(sessionID: string, title: string) {
@@ -639,7 +572,6 @@ export function createSessionStore(options: {
639572

640573
const setSessions = (next: Session[]) => {
641574
setStore("sessions", reconcile(sortSessionsByActivity(next), { key: "id" }));
642-
pruneSessionCaches(new Set(next.map((session) => session.id)));
643575
};
644576

645577
const setSessionStatusById = (next: Record<string, string>) => {
@@ -658,14 +590,6 @@ export function createSessionStore(options: {
658590
setStore("todos", id, next);
659591
};
660592

661-
const clearSessionCaches = () => {
662-
setStore("sessionStatus", {});
663-
setStore("messages", {});
664-
setStore("parts", {});
665-
setStore("todos", {});
666-
reloadDetectionSet.clear();
667-
};
668-
669593
const setPendingPermissions = (next: PendingPermission[]) => {
670594
setStore("pendingPermissions", next);
671595
};
@@ -720,10 +644,6 @@ export function createSessionStore(options: {
720644
const info = record.info as Session | undefined;
721645
if (info?.id) {
722646
setStore("sessions", (current) => removeSession(current, info.id));
723-
removeSessionCaches(info.id);
724-
if (options.selectedSessionId() === info.id) {
725-
options.setSelectedSessionId(null);
726-
}
727647
}
728648
}
729649
}
@@ -1061,7 +981,6 @@ export function createSessionStore(options: {
1061981
setSessionStatusById,
1062982
setMessages,
1063983
setTodos,
1064-
clearSessionCaches,
1065984
setPendingPermissions,
1066985
setPendingQuestions,
1067986
};

‎packages/app/src/app/context/workspace.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export function createWorkspaceStore(options: {
9292
setSelectedSessionId: (value: string | null) => void;
9393
setMessages: (value: any[]) => void;
9494
setTodos: (value: any[]) => void;
95-
clearSessionCaches: () => void;
9695
setPendingPermissions: (value: any[]) => void;
9796
setSessionStatusById: (value: Record<string, string>) => void;
9897
defaultModel: () => any;
@@ -792,7 +791,8 @@ export function createWorkspaceStore(options: {
792791
engineRunning: Boolean(engine()?.running),
793792
});
794793
options.setSelectedSessionId(null);
795-
options.clearSessionCaches();
794+
options.setMessages([]);
795+
options.setTodos([]);
796796
options.setPendingPermissions([]);
797797
options.setSessionStatusById({});
798798

@@ -1060,7 +1060,8 @@ export function createWorkspaceStore(options: {
10601060
}
10611061

10621062
options.setSelectedSessionId(null);
1063-
options.clearSessionCaches();
1063+
options.setMessages([]);
1064+
options.setTodos([]);
10641065
options.setPendingPermissions([]);
10651066
options.setSessionStatusById({});
10661067

@@ -1763,7 +1764,8 @@ export function createWorkspaceStore(options: {
17631764
options.setClient(null);
17641765
options.setConnectedVersion(null);
17651766
options.setSelectedSessionId(null);
1766-
options.clearSessionCaches();
1767+
options.setMessages([]);
1768+
options.setTodos([]);
17671769
options.setPendingPermissions([]);
17681770
options.setSessionStatusById({});
17691771
options.setSseConnected(false);

0 commit comments

Comments
 (0)