feat: queue countdown, hold on stop, reply when blocked

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:22:30 +01:00
1 parent 0558449439
commit 7ec35215c6
9 files changed
+361 -44

No files matched your search

+38 -3
View File
@@ -1,15 +1,15 @@
import test from "node:test";
import assert from "node:assert/strict";
import { decide, gateAfter, onRestart } from "../mod/ovis-composer/delivery.js";
import { decide, gateAfter, onRestart, shouldDrain, drain } from "../mod/ovis-composer/delivery.js";
test("decide idle/done -> send", () => {
assert.equal(decide("idle").kind, "send");
assert.equal(decide("done").kind, "send");
});
test("decide working -> queue, blocked -> hold", () => {
test("decide working -> queue, blocked -> send (reply to the agent)", () => {
assert.equal(decide("working").kind, "queue");
assert.equal(decide("blocked").kind, "hold");
assert.equal(decide("blocked").kind, "send");
});
test("decide unknown status -> queue", () => {
@@ -35,3 +35,38 @@ test("onRestart sending -> uncertain, others untouched", () => {
test("onRestart tolerates non-array", () => {
assert.deepEqual(onRestart(null), []);
});
test("shouldDrain: only working|blocked -> idle|done, not after a stop by the composer", () => {
const all = ["idle", "working", "blocked", "done"];
for (const prev of all) {
for (const next of all) {
const want = (prev === "working" || prev === "blocked") && (next === "idle" || next === "done");
assert.equal(shouldDrain(prev, next, false), want, `${prev}->${next}`);
assert.equal(shouldDrain(prev, next, true), false, `${prev}->${next} stopped`);
}
}
});
test("onRestart: countdown -> held without left, sending -> uncertain", () => {
const out = onRestart([
{ id: "1", state: "countdown", left: 2 },
{ id: "2", state: "sending" },
]);
assert.deepEqual(out, [{ id: "1", state: "held" }, { id: "2", state: "uncertain" }]);
});
test("drain sends an entry whose countdown ran out", async () => {
const { createState, reduce } = await import("../mod/ovis-composer/state.js");
let s = reduce(createState(), { type: "queue", agent: "t2", item: { id: "1", text: "a", createdAt: 1 } });
s = reduce(s, { type: "countdown_start", agent: "t2", id: "1" });
const calls = [];
const ctx = { agent: "t2", terminal: "t2", status: () => "idle", gate: () => true,
hc: async (args) => (calls.push(args), { code: 0, stdout: "{}", stderr: "" }) };
let r = await drain(ctx, s);
assert.equal(r.sent, false); // left is still 3
for (let i = 0; i < 3; i++) s = reduce(s, { type: "countdown_tick", agent: "t2", id: "1" });
r = await drain(ctx, s);
assert.equal(r.sent, true);
assert.equal(calls.length, 1);
assert.deepEqual(r.state.queue.t2, []);
});
+52
View File
@@ -139,3 +139,55 @@ 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"]);
});
const mk = (...items) => reduce(createState(), { type: "restore", agent: "a", items });
const it = (id, state, extra = {}) => ({ id, text: id, state, createdAt: 1, ...extra });
const states = (s) => s.queue.a.map((i) => i.state);
test("countdown_start starts the first queued entry, uncertain untouched", () => {
let s = mk(it("u", "uncertain"), it("q1", "queued"), it("q2", "queued"));
s = reduce(s, { type: "countdown_start", agent: "a", id: "q1" });
assert.deepEqual(states(s), ["uncertain", "countdown", "queued"]);
assert.equal(s.queue.a[1].left, 3);
});
test("countdown_start ignores a non-first queued entry and a busy queue", () => {
const s = mk(it("q1", "queued"), it("q2", "queued"));
assert.equal(reduce(s, { type: "countdown_start", agent: "a", id: "q2" }), s);
const c = reduce(s, { type: "countdown_start", agent: "a", id: "q1" });
assert.equal(reduce(c, { type: "countdown_start", agent: "a", id: "q2" }), c);
const w = mk(it("s", "sending"), it("q", "queued"));
assert.equal(reduce(w, { type: "countdown_start", agent: "a", id: "q" }), w);
});
test("countdown_tick decrements and stops at 0", () => {
let s = reduce(mk(it("q", "queued")), { type: "countdown_start", agent: "a", id: "q" });
for (let i = 0; i < 5; i++) s = reduce(s, { type: "countdown_tick", agent: "a", id: "q" });
assert.equal(s.queue.a[0].left, 0);
assert.equal(s.queue.a[0].state, "countdown");
});
test("countdown_cancel -> held, countdown_reset -> queued, both drop left", () => {
const c = reduce(mk(it("q", "queued")), { type: "countdown_start", agent: "a", id: "q" });
const h = reduce(c, { type: "countdown_cancel", agent: "a", id: "q" });
assert.deepEqual(h.queue.a[0], { id: "q", text: "q", state: "held", createdAt: 1 });
const r = reduce(c, { type: "countdown_reset", agent: "a", id: "q" });
assert.deepEqual(r.queue.a[0], { id: "q", text: "q", state: "queued", createdAt: 1 });
});
test("hold_all holds queued and countdown; sending and uncertain stay", () => {
const s = mk(it("a1", "queued"), it("c", "countdown", { left: 2 }), it("s", "sending"), it("u", "uncertain"), it("h", "held"));
const out = reduce(s, { type: "hold_all", agent: "a" });
assert.deepEqual(states(out), ["held", "held", "sending", "uncertain", "held"]);
assert.equal(out.queue.a[1].left, undefined);
});
test("claim takes a countdown entry", () => {
const s = reduce(mk(it("c", "countdown", { left: 0 })), { type: "claim", agent: "a", id: "c" });
assert.equal(s.queue.a[0].state, "sending");
});
test("restart turns a countdown into held", () => {
const s = reduce(mk(it("c", "countdown", { left: 2 })), { type: "restart", agent: "a" });
assert.deepEqual(states(s), ["held"]);
});
+7
View File
@@ -125,3 +125,10 @@ test("saveDraft does not overwrite a known created_ms with null", () => {
saveDraft(store, "t2", draft({ text: "x", identity: { created_ms: 7, app: null } }));
assert.equal(loadDraft(store, "t2").identity.created_ms, 7);
});
test("queue roundtrip keeps a countdown entry (restart turns it into held)", () => {
const store = memStore();
const q = [{ id: "1", text: "t", state: "countdown", createdAt: 5, left: 2 }];
saveQueue(store, "a", q);
assert.deepEqual(loadQueue(store, "a"), q);
});
+8
View File
@@ -61,3 +61,11 @@ test("canAutoSend waits for the draft/identity check of the agent", async () =>
assert.equal(canAutoSend(new Set(["t3"]), "t2"), false);
assert.equal(canAutoSend(null, "t2"), false);
});
test("queueLabel for every state", () => {
assert.equal(queueLabel({ state: "queued" }), "Następna tura");
assert.equal(queueLabel({ state: "held" }), "Wstrzymana — wyślij ręcznie");
assert.equal(queueLabel({ state: "sending" }), "Wysyłanie…");
assert.equal(queueLabel({ state: "uncertain" }), "Nie potwierdzono");
assert.equal(queueLabel({ state: "countdown", left: 2 }), "Wysyłam za 2 s");
});