Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(session): flush session writes before subagent result delivery

yushen2ec25cb32a3b
diff --git a/src/agent/async-agent.ts b/src/agent/async-agent.ts
index 4fdfb616e3..ebc06317b3 100644
--- a/src/agent/async-agent.ts
+++ b/src/agent/async-agent.ts
@@ -42,6 +42,9 @@ export class AsyncAgent {
.then(async () => {
if (this._closed) return;
const result = await this.agent.run(content);
+ // Flush pending session writes so waitForIdle() callers
+ // can safely read session data from disk.
+ await this.agent.flushSession();
// Normal text is delivered via message_end event; only handle errors here
if (result.error) {
this.channel.send({ id: uuidv7(), content: `[error] ${result.error}` });
diff --git a/src/agent/runner.ts b/src/agent/runner.ts
index 98f233d9e8..00c0e3b2f2 100644
--- a/src/agent/runner.ts
+++ b/src/agent/runner.ts
@@ -455,6 +455,13 @@ export class Agent {
}
}
+ /**
+ * Wait for all pending session storage writes to complete.
+ */
+ async flushSession(): Promise<void> {
+ await this.session.flush();
+ }
+
/**
* Reload tools from profile config.
* Call this after updating tool status to apply changes
diff --git a/src/agent/session/session-manager.ts b/src/agent/session/session-manager.ts
index ec1c7cd66a..9ff1961462 100644
--- a/src/agent/session/session-manager.ts
+++ b/src/agent/session/session-manager.ts
@@ -272,6 +272,13 @@ export class SessionManager {
return result;
}
+ /**
+ * Wait for all pending storage writes to complete.
+ */
+ async flush(): Promise<void> {
+ await this.queue;
+ }
+
private enqueue(task: () => Promise<void>) {
this.queue = this.queue.then(task, task).catch(() => {
// Swallow storage errors to prevent unhandled rejections.