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, parseJson,
queueLabel, queueLabel,
shortPath, shortPath,
stillSameAgent,
statusText, statusText,
targetAgent, targetAgent,
} from "./view.js"; } from "./view.js";
@@ -294,6 +295,25 @@ export default class Composer extends View {
cx.notify(); 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. */ /** The draft of the agent (and its caret) goes into the field. */
showDraft(key) { showDraft(key) {
const draft = this.state.drafts[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. */ /** The field changed: the draft follows and the menu looks at the new text. */
changed(cx) { 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 next = this.textarea.value();
const previous = this.value; const previous = this.value;
this.value = next; this.value = next;
@@ -944,6 +970,10 @@ export default class Composer extends View {
/** The message goes out, or it waits in the queue. */ /** The message goes out, or it waits in the queue. */
submit(text, cx, mode = "auto", force = false) { 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(); const agent = this.target();
if (!agent) { if (!agent) {
this.notice("Composer nie ma do kogo pisać — brak agenta.", "error", cx); 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. * start it again.
*/ */
interrupt(agent, cx) { 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 key = agentKey(agent);
const status = this.statusOf(agent); const status = this.statusOf(agent);
const flagged = status === "working" || status === "blocked"; const flagged = status === "working" || status === "blocked";
@@ -1725,6 +1758,7 @@ export default class Composer extends View {
* @param {"drop"|"paste"|"picker"} origin * @param {"drop"|"paste"|"picker"} origin
*/ */
addFiles(entries, origin, cx) { addFiles(entries, origin, cx) {
if (!this.syncOk(cx)) return;
const agent = this.target(); const agent = this.target();
if (!agent) { if (!agent) {
this.notice("Wybierz agenta — nie ma do kogo dodać pliku.", "error", cx); 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) { export function canAutoSend(ready, key) {
return ready instanceof Set && ready.has(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;
}
+12
View File
@@ -69,3 +69,15 @@ test("queueLabel for every state", () => {
assert.equal(queueLabel({ state: "uncertain" }), "Nie potwierdzono"); assert.equal(queueLabel({ state: "uncertain" }), "Nie potwierdzono");
assert.equal(queueLabel({ state: "countdown", left: 2 }), "Wysyłam za 2 s"); assert.equal(queueLabel({ state: "countdown", left: 2 }), "Wysyłam za 2 s");
}); });
import { stillSameAgent } from "../mod/ovis-composer/view.js";
test("stillSameAgent: only when the field and the focus are on one agent", () => {
assert.equal(stillSameAgent({ before: "t2", after: "t2", target: "t2" }), true);
// the focus moved to t3 while the field still showed t2: the sync switched the field
assert.equal(stillSameAgent({ before: "t2", after: "t3", target: "t3" }), false);
// the focus is on something that is not an agent
assert.equal(stillSameAgent({ before: "t2", after: "", target: "" }), false);
assert.equal(stillSameAgent({ before: "", after: "", target: "" }), false);
assert.equal(stillSameAgent({ before: "", after: "t2", target: "t2" }), false);
});