Skip to content

fix(agent): preserve delivery order when switching queued input - #21

Merged
w01fb0ss merged 2 commits into
w01fb0ss:mainfrom
agooll:fix/preserve-queued-input-order
Nov 26, 2026
Merged

w01fb0ss merged 2 commits into
w01fb0ss:mainfrom
agooll:fix/preserve-queued-input-order

Conversation

@agooll

@agooll agooll commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What and why

Fixes #20. Switching a pending input between steer and followUp clears and rebuilds the Pi SDK queues. Restoring untracked entries first and tracked entries in Map insertion order changes the order of inputs that were supposed to stay put.

Replay each queue in the order returned by the SDK and append the moved input to the destination queue. Preserve attachments on matched tracked inputs and skip tracked entries the SDK already consumed.

Checks

  • One topic
  • pnpm verify passes locally
  • No UI changes
  • No secrets, .env files or local paths
  • Commit signed off (DCO)

The regression fails on upstream 5a4f5f9 and passes with the patch. It checks both queues, the moved input's image attachment and mode, and that an already-consumed input is not reinserted. The Agent Vitest project passed all 64 tests. Biome and git diff --check passed.

Rebuild the native queues in their SDK order, keeping tracked images and appending only the moved item at the end of its new queue.

Signed-off-by: agooll <141763834+agooll@users.noreply.github.com>

@w01fb0ss w01fb0ss left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the precise steps in #20. Replaying each queue in the order the SDK returns it is a nice simplification, and keeping attachments on matched inputs is good. One small request: keying tracked by ${mode}:${text} collapses inputs with identical text in the same queue (the last one wins). Today append refuses duplicate pending text, so it can't happen yet, but if that guard ever relaxes, moving the first of two identical inputs would return false and re-add one of them without its images. The old indexOf/splice paired them one-to-one. Could you keep a list per key and shift() from it (sketch below), and update the requeue doc comment to say the moved input now joins the end of its destination queue? With that I'm happy to merge. Thanks again!

const tracked = new Map<string, { key: string; input: Input }[]>();
for (const [key, input] of additions.queued) {
  const lookup = `${input.mode}:${input.text}`;
  tracked.set(lookup, [...(tracked.get(lookup) ?? []), { key, input }]);
}
// in the loop:
const match = tracked.get(`${queue}:${text}`)?.shift();

Consume tracked matches once per native queue slot so switching the first duplicate preserves the images of both inputs. Document destination tail insertion and cover it in the regression test.

Signed-off-by: agooll <141763834+agooll@users.noreply.github.com>
@w01fb0ss

Copy link
Copy Markdown
Owner

Thanks for the follow-up — c5051fb handles the duplicate-text case exactly as it should: each mode:text key now keeps its inputs in order, so identical inputs pair with pi's queue in delivery order and each keeps its own images, and the new test covers it. CI is green on all three platforms. Merging now — it will be in the next release.

@w01fb0ss
w01fb0ss merged commit cca85ba into w01fb0ss:main Nov 26, 2026
4 checks passed
w01fb0ss added a commit that referenced this pull request Nov 26, 2026
Signed-off-by: leen.zhou <leen.zhou.cn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switching a queued input can reorder other messages in the Pi session

2 participants