fix: always compare draft identity, gate auto-send until checked

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:18:24 +01:00
1 parent e7deca480a
commit 57761326fd
7 files changed
+152 -25

No files matched your search

+33 -10
View File
@@ -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 });
// 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) this.showDraft(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.
+40 -15
View File
@@ -97,6 +97,9 @@ export function reduce(state, action) {
case "restore_draft":
return restoreDraft(state, action);
case "hold_queue":
return holdQueue(state, action.agent);
case "send":
return { ...state, drafts: { ...state.drafts, [action.agent]: "" } };
@@ -157,20 +160,27 @@ export function reduce(state, action) {
* The draft of an agent comes back from the disk.
*
* The draft carries the identity of the terminal it was written for. The
* address (`t2`) can be reused by another terminal after a restart: when the
* start time differs, the draft is still restored - the text is the person's
* - but it says so, and the whole queue of that address is held, so nothing
* goes on its own to an agent it was not written for. An `uncertain` entry
* stays `uncertain`: it has no "send now" button, and "held" has.
* address (`t2`) can be reused by another terminal after a restart, so the
* identity is compared EVERY time - also when the person already typed
* something (`keepCurrent`: the text and the files they have now are not
* overwritten) and also when there is no draft at all.
*
* The queue is trusted only when the live `created_ms` is known and equals the
* saved one. Otherwise the whole queue is held, so nothing goes on its own to
* an agent it was not written for: an unknown live identity counts as a
* mismatch. The notice appears when the two identities differ (null/null is
* "cannot tell": the queue is held, but there is nothing to say). An
* `uncertain` entry stays `uncertain`: it has no "send now" button, and
* "held" has.
*/
function restoreDraft(state, action) {
const draft = action.draft;
if (!draft || typeof draft.text !== "string") return state;
const draft = action.draft && typeof action.draft.text === "string" ? action.draft : null;
const agent = action.agent;
const same = (draft.identity?.created_ms ?? null) === (action.identity?.created_ms ?? null);
const live = action.identity?.created_ms ?? null;
const next = {
let next = state;
if (draft && !action.keepCurrent) {
next = {
...state,
drafts: { ...state.drafts, [agent]: draft.text },
carets: { ...(state.carets ?? {}), [agent]: Number.isInteger(draft.caret) ? draft.caret : draft.text.length },
@@ -179,14 +189,29 @@ function restoreDraft(state, action) {
[agent]: (draft.attachments ?? []).map((file) => ({ ...file })),
},
};
if (same) return next;
}
// Without a draft there is no saved identity to compare - only an unknown
// live identity is a reason to hold.
const saved = draft ? (draft.identity?.created_ms ?? null) : live;
if (live !== null && live === saved) return next;
const held = holdQueue(next, agent);
if (draft && saved !== live) {
return { ...held, notice: { text: "Szkic z poprzedniej sesji agenta", level: "warn" } };
}
return held;
}
/** Everything that could go out on its own waits for the person. */
function holdQueue(state, agent) {
const items = list(state.queue, agent);
if (!items.some((item) => item.state !== "uncertain" && item.state !== "held")) return state;
return {
...next,
notice: { text: "Szkic z poprzedniej sesji agenta", level: "warn" },
...state,
queue: {
...next.queue,
[agent]: list(next.queue, agent).map((item) =>
...state.queue,
[agent]: items.map((item) =>
item.state === "uncertain" || item.state === "held" ? item : { ...item, state: "held" },
),
},
+6
View File
@@ -211,6 +211,12 @@ export function saveDraft(store, agent, draft) {
if (typeof agent !== "string" || agent === "") return;
const clean = toDraft(draft);
if (!clean) return;
// A terminal whose start time could not be read must not wipe the start
// time that is already on the disk: the next restart compares against it.
if (clean.identity.created_ms === null) {
const stored = toDraft(read(store, draftKey(agent)));
if (stored && stored.identity.created_ms !== null) clean.identity = stored.identity;
}
write(store, draftKey(agent), clean);
}
+14
View File
@@ -462,3 +462,17 @@ export function overLimit(text) {
const length = typeof text === "string" ? text.length : 0;
return Math.max(0, length - LIMITS.messageChars);
}
/**
* May the composer send on its own for this agent? Only after its draft and
* the identity of its terminal were checked (see main.js `restoreDraft`): the
* queue comes back from the disk before that, and an address can belong to a
* different terminal now.
*
* @param {Set<string>|null} ready - the agents whose check is done
* @param {string} key
* @returns {boolean}
*/
export function canAutoSend(ready, key) {
return ready instanceof Set && ready.has(key);
}
+40
View File
@@ -99,3 +99,43 @@ test("draft action keeps the caret", () => {
const s = reduce(createState(), { type: "draft", agent: "a", text: "hi", caret: 1 });
assert.equal(s.carets.a, 1);
});
const q3 = () => ({ type: "restore", agent: "t2", items: [
{ id: "1", text: "a", state: "queued", createdAt: 1 },
{ id: "2", text: "b", state: "sending", createdAt: 2 },
{ id: "3", text: "c", state: "uncertain", createdAt: 3 },
] });
test("restore_draft keepCurrent: mismatch holds the queue, keeps what the user typed", () => {
let s = reduce(createState(), q3());
s = reduce(s, { type: "draft", agent: "t2", text: "nowy tekst", caret: 4 });
s = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 9, app: null }, keepCurrent: true });
assert.equal(s.drafts.t2, "nowy tekst");
assert.equal(s.carets.t2, 4);
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(s.notice.text, "Szkic z poprzedniej sesji agenta");
});
test("restore_draft: unknown live identity holds the queue; notice only when saved identity was known", () => {
let s = reduce(createState(), q3());
const live = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: null, app: null } });
assert.deepEqual(live.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(live.notice.text, "Szkic z poprzedniej sesji agenta");
const both = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(null), identity: { created_ms: null, app: null } });
assert.deepEqual(both.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(both.notice, null);
assert.equal(both.drafts.t2, "szkic");
});
test("restore_draft without a draft: unknown live identity still holds the queue", () => {
const s = reduce(createState(), q3());
const out = reduce(s, { type: "restore_draft", agent: "t2", draft: null, identity: { created_ms: null, app: null } });
assert.deepEqual(out.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(out.notice, null);
});
test("hold_queue holds queued and sending, keeps uncertain", () => {
const s = reduce(reduce(createState(), q3()), { type: "hold_queue", agent: "t2" });
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
});
+11
View File
@@ -114,3 +114,14 @@ test("loadDraft: text-only draft still loads, with defaults", () => {
text: "a", caret: 1, attachments: [], identity: { created_ms: null, app: null }, savedAt: 0,
});
});
test("saveDraft does not overwrite a known created_ms with null", () => {
const store = memStore();
saveDraft(store, "t2", draft());
saveDraft(store, "t2", draft({ text: "nowy", identity: { created_ms: null, app: null } }));
const out = loadDraft(store, "t2");
assert.equal(out.text, "nowy");
assert.equal(out.identity.created_ms, 1794818438639);
saveDraft(store, "t2", draft({ text: "x", identity: { created_ms: 7, app: null } }));
assert.equal(loadDraft(store, "t2").identity.created_ms, 7);
});
+8
View File
@@ -53,3 +53,11 @@ test("insertedNewline finds the index", () => {
assert.deepEqual(insertedNewline("ab", "a\nb"), { index: 1 });
assert.equal(insertedNewline("ab", "abc"), null);
});
test("canAutoSend waits for the draft/identity check of the agent", async () => {
const { canAutoSend } = await import("../mod/ovis-composer/view.js");
assert.equal(canAutoSend(new Set(), "t2"), false);
assert.equal(canAutoSend(new Set(["t2"]), "t2"), true);
assert.equal(canAutoSend(new Set(["t3"]), "t2"), false);
assert.equal(canAutoSend(null, "t2"), false);
});