Skip to content

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

Go

fix(agent): resolve merge conflict in runner.ts for credentials reload

Jiang Bohan5716932903ae
diff --cc src/agent/runner.ts
index 9b732e91df,3deef81e03..6abba26eab
--- a/src/agent/runner.ts
+++ b/src/agent/runner.ts
@@@ -352,13 -359,64 +361,65 @@@ export class Agent
}
}
+ /** Emit an error event through the subscriber mechanism */
+ emitError(message: string): void {
+ this.emitMulticaEvent({ type: "agent_error", message });
+ }
+
async run(prompt: string): Promise<AgentRunResult> {
+ // Run-level mutex: prevents concurrent run/runInternal from mis-tagging messages
+ return this.withRunMutex(() => this._run(prompt));
+ }
+
+ /**
+ * Run a prompt as an internal turn.
+ * Messages are persisted with `internal: true` and rolled back from
+ * in-memory state after the turn completes, so they do not pollute
+ * the main conversation context.
+ */
+ async runInternal(prompt: string): Promise<AgentRunResult> {
+ return this.withRunMutex(async () => {
+ const messageCountBefore = this.agent.state.messages.length;
+ this._internalRun = true;
+ try {
+ const result = await this._run(prompt);
+ return result;
+ } finally {
+ this._internalRun = false;
+ // Roll back internal messages from in-memory state
+ const current = this.agent.state.messages;
+ if (current.length > messageCountBefore) {
+ this.agent.replaceMessages(current.slice(0, messageCountBefore));
+ }
+ }
+ });
+ }
+
+ private async withRunMutex<T>(fn: () => Promise<T>): Promise<T> {
+ // Chain on the mutex so only one run executes at a time
+ const prev = this._runMutex;
+ let resolve: () => void;
+ this._runMutex = new Promise<void>((r) => { resolve = r; });
+ await prev;
+ try {
+ return await fn();
+ } finally {
+ resolve!();
+ }
+ }
+
+ private async _run(prompt: string): Promise<AgentRunResult> {
await this.ensureInitialized();
+ this.refreshAuthState();
+ this.output.state.lastAssistantText = "";
+
+ // Early validation: check API key before calling PiAgentCore.prompt(),
+ // because getApiKey errors thrown inside PiAgentCore's internal async
+ // context result in UnhandledPromiseRejection instead of propagating.
if (!this.currentApiKey) {
- throw new Error(`No API key configured for provider: ${this.resolvedProvider}`);
+ const errorMsg = `No API key configured for provider: ${this.resolvedProvider}. Please configure a provider in Agent Settings.`;
+ return { text: "", error: errorMsg };
}
- this.output.state.lastAssistantText = "";
const canRotate = !this.pinnedProfile && this.profileCandidates.length > 1;
let lastError: unknown;