Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(tui): prompt resolver map keyed by prompt id; prompts queue instead of dropping

Salman Qureshida07740ab6d5
diff --git a/src/channels/cli-prompt-queue.test.ts b/src/channels/cli-prompt-queue.test.ts
new file mode 100644
index 00000000..de7b815c
--- /dev/null
+++ b/src/channels/cli-prompt-queue.test.ts
@@ -0,0 +1,77 @@
+import { describe, expect, it } from 'vitest';
+import { CLIChannel } from './cli.js';
+
+/**
+ * The permission resolver used to be ONE slot: a second concurrent prompt
+ * (sub-agent, bot, a choice during a permission ask) overwrote it and the
+ * first promise never settled. Now every prompt has its own resolver keyed
+ * by id and the TUI shows them in order.
+ */
+describe('CLIChannel prompt resolver map', () => {
+ it('shows prompts in arrival order and settles each promise with its own answer', async () => {
+ const channel = new CLIChannel();
+ const first = channel.askPermission('write a.txt?');
+ const second = channel.presentChoicePrompt('pick one', [{ value: 'x', label: 'X' }, { value: 'y', label: 'Y' }]);
+ const third = channel.askToContinue('keep going?');
+
+ expect(channel.getTuiState().permissionPrompt?.message).toBe('write a.txt?');
+ expect(channel.pendingPromptIds()).toHaveLength(3);
+
+ // Answer what is on screen → the next queued prompt appears.
+ channel.getTuiState().permissionPrompt!.resolve('always');
+ await expect(first).resolves.toBe('always');
+ expect(channel.getTuiState().permissionPrompt?.message).toBe('pick one');
+
+ channel.getTuiState().permissionPrompt!.resolve('y');
+ await expect(second).resolves.toBe('y');
+ expect(channel.getTuiState().permissionPrompt?.type).toBe('continue');
+
+ channel.getTuiState().permissionPrompt!.resolve('yes');
+ await expect(third).resolves.toBe(true);
+ expect(channel.getTuiState().permissionPrompt).toBeNull();
+ expect(channel.pendingPromptIds()).toHaveLength(0);
+ });
+
+ it('a queued prompt can be settled by id before it is shown, without disturbing the visible one', async () => {
+ const channel = new CLIChannel();
+ const shown = channel.prompt('name?');
+ const queued = channel.presentChoicePrompt('later', [{ value: 'a', label: 'A' }]);
+ const [shownId, queuedId] = channel.pendingPromptIds();
+
+ expect(channel.resolvePrompt(queuedId, 'a')).toBe(true);
+ await expect(queued).resolves.toBe('a');
+ expect(channel.getTuiState().permissionPrompt?.id).toBe(shownId);
+ expect(channel.pendingPromptIds()).toEqual([shownId]);
+
+ expect(channel.resolvePrompt(queuedId, 'again')).toBe(false); // already settled
+ expect(channel.resolvePrompt(shownId, 'mercury')).toBe(true);
+ await expect(shown).resolves.toBe('mercury');
+ expect(channel.getTuiState().permissionPrompt).toBeNull();
+ });
+
+ it('a timed-out choice resolves the OLDEST unanswered choice prompt, not whatever is on screen', async () => {
+ const channel = new CLIChannel();
+ const ask = channel.askPermission('run ls?');
+ const choice = channel.presentChoicePrompt('which?', [{ value: '0', label: 'zero' }, { value: '1', label: 'one' }]);
+
+ channel.resolveChoicePromptWithDefault('1');
+ await expect(choice).resolves.toBe('1');
+ // The permission ask is still on screen and still pending.
+ expect(channel.getTuiState().permissionPrompt?.message).toBe('run ls?');
+ channel.getTuiState().permissionPrompt!.resolve('no');
+ await expect(ask).resolves.toBe('no');
+ });
+
+ it('clearPermissionPrompt settles every pending prompt with its declined answer', async () => {
+ const channel = new CLIChannel();
+ const ask = channel.askPermission('x?');
+ const cont = channel.askToContinue('y?');
+ const choice = channel.presentChoicePrompt('z?', [{ value: 'a', label: 'A' }]);
+ channel.clearPermissionPrompt();
+ await expect(ask).resolves.toBe('no');
+ await expect(cont).resolves.toBe(false);
+ await expect(choice).resolves.toBe('');
+ expect(channel.getTuiState().permissionPrompt).toBeNull();
+ expect(channel.pendingPromptIds()).toEqual([]);
+ });
+});
diff --git a/src/channels/cli.ts b/src/channels/cli.ts
index 5182a452..c0e3f91b 100644
--- a/src/channels/cli.ts
+++ b/src/channels/cli.ts
@@ -533,7 +533,17 @@ export class CLIChannel extends BaseChannel {
/** Esc-during-a-turn: installed by the boot path with the agent's stop
* routine (the same one `/stop` calls). Null until set. */
private interruptHandler: (() => unknown) | null = null;
- private permissionResolver: ((value: string | boolean) => void) | null = null;
+ /**
+ * Pending interactive prompts, keyed by prompt id. Several callers can
+ * prompt concurrently (the foreground turn, sub-agents, bots): a single
+ * resolver slot was overwritten by the newest prompt and the earlier
+ * promises never settled. Every prompt now keeps its own resolver; the
+ * TUI shows them in arrival order — `state.permissionPrompt` is the one
+ * on screen, `promptQueue` holds the rest.
+ */
+ private promptResolvers = new Map<string, (value: string | boolean) => void>();
+ private promptQueue: PermissionPromptState[] = [];
+ private promptSeq = 0;
private menuDepth = 0;
private menuAbortController: AbortController | null = null;
private heartbeatMsgId: string | null = null;
@@ -1251,11 +1261,11 @@ export class CLIChannel extends BaseChannel {
channel: this,
onInput: (text: string) => { this.inputHandler?.(text); },
onPermissionResolve: (value: string | boolean) => {
- if (this.permissionResolver) {
- this.permissionResolver(value);
- this.permissionResolver = null;
- }
- this.update({ permissionPrompt: null });
+ const shown = this.state.permissionPrompt;
+ if (shown?.id && this.resolvePrompt(shown.id, value)) return;
+ // No live resolver for what is on screen (stale state): clear it
+ // and surface whatever is queued next.
+ this.update({ permissionPrompt: this.promptQueue.shift() ?? null });
},
onExit: () => {
this.scheduleTuiExit();
@@ -1963,107 +1973,138 @@ export class CLIChannel extends BaseChannel {
}
}
- async prompt(question: string): Promise<string> {
- return new Promise((resolve) => {
- this.permissionResolver = (val) => resolve(String(val));
- this.update({
- permissionPrompt: {
- type: 'ask',
- message: question,
- resolve: () => {},
- },
- });
+ /**
+ * Register a prompt: its resolver lives in the map under a fresh id, and
+ * it is shown now or queued behind the prompt already on screen. The
+ * returned promise settles exactly once, whenever ITS prompt is answered.
+ */
+ private enqueuePrompt<T>(
+ prompt: Omit<PermissionPromptState, 'id' | 'resolve'>,
+ map: (value: string | boolean) => T,
+ ): Promise<T> {
+ return new Promise<T>((resolve) => {
+ const id = `prompt-${++this.promptSeq}`;
+ this.promptResolvers.set(id, (value) => resolve(map(value)));
+ const entry: PermissionPromptState = { ...prompt, id, resolve: (value) => { this.resolvePrompt(id, value); } };
+ if (this.state.permissionPrompt) {
+ this.promptQueue.push(entry);
+ } else {
+ this.update({ permissionPrompt: entry });
+ }
});
}
+ /**
+ * Settle the prompt with this id. Returns false when no such prompt is
+ * pending (already answered, or unknown). If it was the one on screen,
+ * the next queued prompt is shown; if it was queued, it is removed.
+ */
+ resolvePrompt(id: string, value: string | boolean): boolean {
+ const resolver = this.promptResolvers.get(id);
+ if (!resolver) return false;
+ this.promptResolvers.delete(id);
+ if (this.state.permissionPrompt?.id === id) {
+ this.update({ permissionPrompt: this.promptQueue.shift() ?? null });
+ } else {
+ this.promptQueue = this.promptQueue.filter((p) => p.id !== id);
+ }
+ resolver(value);
+ return true;
+ }
+
+ /** Ids of every unanswered prompt: the visible one first, then the queue. */
+ pendingPromptIds(): string[] {
+ const shown = this.state.permissionPrompt?.id;
+ const queued = this.promptQueue.map((p) => p.id).filter((id): id is string => typeof id === 'string');
+ return shown ? [shown, ...queued] : queued;
+ }
+
+ async prompt(question: string): Promise<string> {
+ return this.enqueuePrompt({ type: 'ask', message: question }, String);
+ }
+
async askPermissionMode(): Promise<PermissionMode> {
if (!process.stdout.isTTY) return 'ask-me';
-
- return new Promise((resolve) => {
- this.permissionResolver = (val) => resolve(val as PermissionMode);
- this.update({
- permissionPrompt: {
- type: 'mode',
- message: 'Choose how Mercury handles risky actions this session.',
- options: [
- { value: 'allow-all', label: 'Allow All — auto-approve everything (scopes, commands, loop continuation)' },
- { value: 'ask-me', label: 'Ask Me — confirm before file writes, shell commands, and scope changes' },
- ],
- resolve: () => {},
- },
- });
- });
+ return this.enqueuePrompt({
+ type: 'mode',
+ message: 'Choose how Mercury handles risky actions this session.',
+ options: [
+ { value: 'allow-all', label: 'Allow All — auto-approve everything (scopes, commands, loop continuation)' },
+ { value: 'ask-me', label: 'Ask Me — confirm before file writes, shell commands, and scope changes' },
+ ],
+ }, (val) => val as PermissionMode);
}
async askPermission(prompt: string): Promise<string> {
- return new Promise((resolve) => {
- this.permissionResolver = (val) => resolve(String(val));
- this.update({
- permissionPrompt: {
- type: 'ask',
- message: prompt,
- options: [
- { value: 'yes', label: 'Yes — approve once' },
- { value: 'always', label: 'Always — remember this permission' },
- { value: 'no', label: 'No — deny' },
- ],
- resolve: () => {},
- },
- });
- });
+ return this.enqueuePrompt({
+ type: 'ask',
+ message: prompt,
+ options: [
+ { value: 'yes', label: 'Yes — approve once' },
+ { value: 'always', label: 'Always — remember this permission' },
+ { value: 'no', label: 'No — deny' },
+ ],
+ }, String);
}
async presentChoicePrompt(question: string, options: Array<{ value: string; label: string }>): Promise<string> {
- return new Promise((resolve) => {
- this.permissionResolver = (val) => resolve(String(val));
- this.update({
- permissionPrompt: {
- type: 'choice',
- message: question,
- options,
- resolve: () => {},
- },
- });
- });
+ return this.enqueuePrompt({ type: 'choice', message: question, options }, String);
}
/**
* Resolve a pending choice prompt with a default value (timeout / dismissal).
* Time-weighted prompts use this so an unanswered question is answered FOR
- * the user and the box disappears instead of lingering forever.
+ * the user and the box disappears instead of lingering forever. With
+ * `id` the exact prompt is settled; without it, the OLDEST unanswered
+ * choice prompt (the one whose timer fired first) — visible or queued.
*/
- resolveChoicePromptWithDefault(value: string): void {
- if (!this.state.permissionPrompt) return;
- const resolver = this.permissionResolver;
- this.permissionResolver = null;
- this.update({ permissionPrompt: null });
- resolver?.(value);
+ resolveChoicePromptWithDefault(value: string, id?: string): void {
+ if (id) {
+ this.resolvePrompt(id, value);
+ return;
+ }
+ const target = [this.state.permissionPrompt, ...this.promptQueue]
+ .find((p): p is PermissionPromptState => Boolean(p && p.type === 'choice' && p.id && this.promptResolvers.has(p.id)));
+ if (target?.id) this.resolvePrompt(target.id, value);
}
async askToContinue(question: string, _targetId?: string): Promise<boolean> {
- return new Promise((resolve) => {
- this.permissionResolver = (val) => {
- const normalized = typeof val === 'string' ? val.trim().toLowerCase() : val;
- resolve(normalized === true || normalized === 'yes' || normalized === 'y');
- };
- this.update({
- permissionPrompt: {
- type: 'continue',
- message: question,
- options: [
- { value: 'yes', label: 'Yes — continue' },
- { value: 'no', label: 'No — stop' },
- ],
- resolve: () => {},
- },
- });
+ return this.enqueuePrompt({
+ type: 'continue',
+ message: question,
+ options: [
+ { value: 'yes', label: 'Yes — continue' },
+ { value: 'no', label: 'No — stop' },
+ ],
+ }, (val) => {
+ const normalized = typeof val === 'string' ? val.trim().toLowerCase() : val;
+ return normalized === true || normalized === 'yes' || normalized === 'y';
});
}
+ /**
+ * Dismiss every pending prompt. Each promise still settles — with the
+ * prompt's "declined" answer — so no caller hangs on a box that is gone.
+ */
clearPermissionPrompt(): void {
+ for (const id of this.pendingPromptIds()) {
+ const entry = this.state.permissionPrompt?.id === id ? this.state.permissionPrompt : this.promptQueue.find((p) => p.id === id);
+ this.resolvePrompt(id, CLIChannel.dismissedAnswer(entry));
+ }
+ this.promptQueue = [];
this.update({ permissionPrompt: null });
}
+ /** The answer a dismissed prompt reports (mirrors the TUI's Esc handling). */
+ private static dismissedAnswer(prompt: PermissionPromptState | null | undefined): string | boolean {
+ switch (prompt?.type) {
+ case 'mode': return 'ask-me';
+ case 'choice': return '';
+ case 'continue': return false;
+ default: return prompt?.options ? 'no' : '';
+ }
+ }
+
setSkills(skills: SkillInfo[]): void {
this.update({ skills });
}
diff --git a/src/ui/types.ts b/src/ui/types.ts
index b19cd4a0..a2a9901f 100644
--- a/src/ui/types.ts
+++ b/src/ui/types.ts
@@ -205,9 +205,13 @@ export interface BackgroundTaskInfo {
}
export interface PermissionPromptState {
+ /** Prompt id — the key the channel resolves by. Set on every prompt the
+ * channel creates; optional only for hand-built view fixtures. */
+ id?: string;
type: 'mode' | 'ask' | 'continue' | 'choice';
message: string;
options?: Array<{ value: string; label: string }>;
+ /** Settle THIS prompt (routes to the channel's resolver map). */
resolve: (value: string | boolean) => void;
}