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
+161 -34

No files matched your search

+48 -23
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,36 +160,58 @@ 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 = {
...state,
drafts: { ...state.drafts, [agent]: draft.text },
carets: { ...(state.carets ?? {}), [agent]: Number.isInteger(draft.caret) ? draft.caret : draft.text.length },
attachments: {
...state.attachments,
[agent]: (draft.attachments ?? []).map((file) => ({ ...file })),
},
};
if (same) return 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 },
attachments: {
...state.attachments,
[agent]: (draft.attachments ?? []).map((file) => ({ ...file })),
},
};
}
// 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" },
),
},