fix: events in the 500 ms focus window never touch another agent's draft or queue

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 12:09:09 +01:00
1 parent d852c406dd
commit dd95bf2c68
3 files changed
+63

No files matched your search

+34
View File
@@ -81,6 +81,7 @@ import {
parseJson,
queueLabel,
shortPath,
stillSameAgent,
statusText,
targetAgent,
} from "./view.js";
@@ -294,6 +295,25 @@ export default class Composer extends View {
cx.notify();
}
/**
* The composer is brought up to date with the focus - and told whether an
* event that began before may go on.
*
* The focus is polled every 500 ms, so for a moment the field can show the
* draft of agent A while the focus is already on B. An event that began in
* that window (a key, Enter, Stop, a drop) belongs to A: the sync saves A's
* text under A, and the event is dropped - it must not write A's text into
* B's draft or touch B's queue.
*
* @returns {boolean} true when the focus and the field are on the same agent
*/
syncOk(cx) {
const before = this.currentKey;
this.syncAgent(cx);
const target = this.target();
return stillSameAgent({ before, after: this.currentKey, target: target ? agentKey(target) : "" });
}
/** The draft of the agent (and its caret) goes into the field. */
showDraft(key) {
const draft = this.state.drafts[key] ?? "";
@@ -461,6 +481,12 @@ export default class Composer extends View {
/** The field changed: the draft follows and the menu looks at the new text. */
changed(cx) {
// The text that was just typed belongs to the agent the field showed; the
// sync keeps it there and the rest of this handler is not for another one.
if (!this.syncOk(cx)) {
cx.notify();
return;
}
const next = this.textarea.value();
const previous = this.value;
this.value = next;
@@ -944,6 +970,10 @@ export default class Composer extends View {
/** The message goes out, or it waits in the queue. */
submit(text, cx, mode = "auto", force = false) {
if (!this.syncOk(cx)) {
this.notice("Agent się zmienił — wiadomość nie została wysłana. Zostaje w szkicu poprzedniego agenta.", "error", cx);
return;
}
const agent = this.target();
if (!agent) {
this.notice("Composer nie ma do kogo pisać — brak agenta.", "error", cx);
@@ -1513,6 +1543,9 @@ export default class Composer extends View {
* start it again.
*/
interrupt(agent, cx) {
// Stop is for the agent the person was looking at - not for one the focus
// moved to a moment ago.
if (!this.syncOk(cx) || agentKey(agent) !== this.currentKey) return;
const key = agentKey(agent);
const status = this.statusOf(agent);
const flagged = status === "working" || status === "blocked";
@@ -1725,6 +1758,7 @@ export default class Composer extends View {
* @param {"drop"|"paste"|"picker"} origin
*/
addFiles(entries, origin, cx) {
if (!this.syncOk(cx)) return;
const agent = this.target();
if (!agent) {
this.notice("Wybierz agenta — nie ma do kogo dodać pliku.", "error", cx);
+17
View File
@@ -478,3 +478,20 @@ export function overLimit(text) {
export function canAutoSend(ready, key) {
return ready instanceof Set && ready.has(key);
}
/**
* Is the composer still talking to the agent whose draft the field shows?
*
* The field shows the draft of `before` (the agent the composer was synced to
* when an event began). After a sync the focus may have moved: then the event
* belongs to an agent that is not the target any more, and it must not touch
* the draft or the queue of the new one.
*
* @param {{before:string, after:string, target:string}} keys - `before`: the
* key the composer had before the sync; `after`: the key it has now;
* `target`: the key of the agent under the focus ("" for none)
* @returns {boolean}
*/
export function stillSameAgent({ before, after, target }) {
return before !== "" && before === after && before === target;
}