fix: always compare draft identity, gate auto-send until checked
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
e7deca480a
commit
57761326fd
7 files changed
+161
-34
No files matched your search
+34
-11
@@ -46,6 +46,7 @@ import {
|
||||
agentName,
|
||||
agentStatus,
|
||||
applySuggestion,
|
||||
canAutoSend,
|
||||
closeGate,
|
||||
createGate,
|
||||
drainAll,
|
||||
@@ -100,6 +101,8 @@ export default class Composer extends View {
|
||||
this.identities = new Map();
|
||||
this.draftReady = new Set();
|
||||
this.draftLoading = new Set();
|
||||
// Agents whose queue wanted to drain before their identity was checked.
|
||||
this.pendingDrain = new Set();
|
||||
this.draftTimer = null;
|
||||
|
||||
// The menu of suggestions: what it shows, which row is picked.
|
||||
@@ -246,20 +249,32 @@ export default class Composer extends View {
|
||||
const typed = here ? this.textarea.value() : (this.state.drafts[key] ?? "");
|
||||
const untouched = typed === "" && (this.state.attachments[key] ?? []).length === 0;
|
||||
|
||||
// What the person typed while the identity was read is not
|
||||
// overwritten.
|
||||
if (stored && untouched) {
|
||||
this.state = reduce(this.state, { type: "restore_draft", agent: key, draft: stored, identity });
|
||||
this.persistQueue(key);
|
||||
if (here) this.showDraft(key);
|
||||
}
|
||||
// The identity is compared always; what the person typed while it
|
||||
// was read is not overwritten (`keepCurrent`).
|
||||
this.state = reduce(this.state, {
|
||||
type: "restore_draft",
|
||||
agent: key,
|
||||
draft: stored,
|
||||
identity,
|
||||
keepCurrent: !untouched,
|
||||
});
|
||||
this.persistQueue(key);
|
||||
if (here && stored && untouched) this.showDraft(key);
|
||||
} catch {
|
||||
// The draft stays on the disk; the next switch to this agent tries again.
|
||||
this.draftLoading.delete(key);
|
||||
return;
|
||||
// Nothing is known about the terminal: the queue waits for the person
|
||||
// and the saving of drafts is not blocked for ever.
|
||||
if (!this.identities.has(key)) this.identities.set(key, { created_ms: null, app: null });
|
||||
try {
|
||||
this.state = reduce(this.state, { type: "hold_queue", agent: key });
|
||||
this.persistQueue(key);
|
||||
} catch {
|
||||
// The queue stays as it is.
|
||||
}
|
||||
}
|
||||
this.draftLoading.delete(key);
|
||||
this.draftReady.add(key);
|
||||
// The sending that waited for the check goes on.
|
||||
if (this.pendingDrain.delete(key)) this.drainQueue(agent, cx);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
@@ -640,7 +655,10 @@ export default class Composer extends View {
|
||||
|
||||
// A message that is already on the wire: the new one waits in the queue
|
||||
// instead of vanishing.
|
||||
if (decision.kind === "send" && !this.draining.has(key)) {
|
||||
// Not before the draft and the identity of the agent were checked - the
|
||||
// person's own "send now" (Ctrl+Enter) is the exception.
|
||||
const allowed = mode === "now" || canAutoSend(this.draftReady, key);
|
||||
if (decision.kind === "send" && allowed && !this.draining.has(key)) {
|
||||
this.sendNow(agent, message, cx);
|
||||
return;
|
||||
}
|
||||
@@ -771,6 +789,11 @@ export default class Composer extends View {
|
||||
*/
|
||||
async drainQueue(agent, cx) {
|
||||
const key = agentKey(agent);
|
||||
// The queue came back from the disk, but whose is it? Wait for the check.
|
||||
if (!canAutoSend(this.draftReady, key)) {
|
||||
this.pendingDrain.add(key);
|
||||
return;
|
||||
}
|
||||
// One drain at a time per agent: the state it works on is immutable, so
|
||||
// two of them would send the same message twice. The queue is watched
|
||||
// anyway - the drain that is in flight will not empty it.
|
||||
|
||||
Reference in new issue
Block a user