@@ -5927,7 +6045,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
title={
annotationDirty
? "请先确认当前编辑,再进入工作页"
- : "开启连续工作:优先继续未完成任务,真实送检后随机领取下一条"
+ : "按任务池旁的工作模式进入;手动模式不会自动领取新任务"
}
className="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white disabled:opacity-50"
>
@@ -5981,33 +6099,16 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
>
{workSession.continuous
? "连续工作中:后台切片期间接续其他任务;真实送检成功后补足容量。"
- : workSession.pauseReason || "连续工作尚未开启;可完成当前任务或显式开始连续工作。"}
+ : workSession.pauseReason || "手动模式:可直接处理已领取任务,点击随机领取才接取新任务。"}
- {workSession.continuous ? (
-
- ) : (
-
- )}
+
在个人任务池旁切换自动接取或手动接取;选择会在当前浏览器保留。
) : null}
{workPage && workSession.supported && !workSession.active ? (
继续已领取任务
- 刷新或重新打开后需要显式继续。继续后恢复已保存内容并按容量补齐;未保存草稿和撤销历史无法跨刷新恢复。
+ 刷新或重新打开后需要显式继续。继续后恢复已保存内容,并沿用任务池旁的工作模式;手动模式不会补领。未保存草稿和撤销历史无法跨刷新恢复。
{loading ? 正在核对已领取任务…
: null}
{recoverableWork.map((item) => (
@@ -6046,25 +6147,8 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
>
{workEntryBusy ? "正在核对…" : "继续工作"}
-
- 连续工作会先核对上次未确认的领取结果并继续已领取任务;没有未完成任务时才随机领取。
+ 连续工作会按任务池旁的开关控制自动接取;手动模式可直接选择已领取任务,或点击随机领取。
) : null}
diff --git a/tests/browser/fixtures/annotation-work-mode.html b/tests/browser/fixtures/annotation-work-mode.html
new file mode 100644
index 00000000..3de9bbd4
--- /dev/null
+++ b/tests/browser/fixtures/annotation-work-mode.html
@@ -0,0 +1 @@
+
工作模式本地验证
diff --git a/tests/browser/fixtures/annotation-work-mode.tsx b/tests/browser/fixtures/annotation-work-mode.tsx
new file mode 100644
index 00000000..9d26b255
--- /dev/null
+++ b/tests/browser/fixtures/annotation-work-mode.tsx
@@ -0,0 +1,131 @@
+import { useEffect, useRef, useState } from "react";
+import { createRoot } from "react-dom/client";
+import { AnnotationWorkModeToggle } from "../../../src/components/AnnotationWorkModeToggle";
+import { useAnnotationWorkSession } from "../../../src/hooks/useAnnotationWorkSession";
+import { refillAnnotationWorkBuffer } from "../../../src/lib/annotation-work-mode.js";
+import "../../../src/index.css";
+
+function Fixture() {
+ const [user, setUser] = useState("synthetic-mode-user");
+ const [focused, setFocused] = useState(new URLSearchParams(location.search).has("focused"));
+ const session = useAnnotationWorkSession(user, "synthetic-mode-project", focused);
+ const [items, setItems] = useState([
+ { id: "owned", reviewRound: 0, currentStageId: "annotation_combined", status: "claimed" },
+ ]);
+ const inventory = useRef(items);
+ inventory.current = items;
+ const [requests, setRequests] = useState(0);
+ const [slow, setSlow] = useState(false);
+ const [draft, setDraft] = useState("正在编辑的草稿");
+ const [message, setMessage] = useState("");
+ const flight = useRef(false);
+ const readBuffer = async () => ({
+ enabled: true,
+ capacity: 3,
+ occupied: inventory.current.length,
+ items: [...inventory.current],
+ });
+ const claim = async () => {
+ setRequests((n) => n + 1);
+ if (slow) await new Promise((r) => setTimeout(r, 2500));
+ const item = {
+ id: `allocated-${inventory.current.length}`,
+ reviewRound: 0,
+ currentStageId: "annotation_combined",
+ status: "claimed",
+ };
+ inventory.current = [...inventory.current, item];
+ setItems(inventory.current);
+ return { item, task: { id: item.id } };
+ };
+ const enter = async () => {
+ const result = await session.acquire();
+ if (result.status !== "acquired") return;
+ session.setContinuous(result.session, session.prefersContinuous());
+ session.activate(result.session);
+ setFocused(true);
+ };
+ useEffect(() => {
+ if (!session.active || !session.continuous) return;
+ const run = async () => {
+ if (flight.current) return;
+ flight.current = true;
+ try {
+ await refillAnnotationWorkBuffer({
+ refill: true,
+ readBuffer,
+ claim,
+ shouldContinue: () => session.isContinuous(),
+ onClaim: () => setMessage("已确认的任务保留"),
+ });
+ } finally {
+ flight.current = false;
+ }
+ };
+ void run();
+ const timer = setInterval(run, 500);
+ return () => clearInterval(timer);
+ }, [session.active, session.continuous]);
+ const toggle = async () => {
+ try {
+ const enabled = !session.prefersContinuous();
+ session.setContinuousEnabled(enabled);
+ if (enabled) await enter();
+ } catch {
+ setMessage("无法保存工作模式,未开启自动接取");
+ }
+ };
+ return (
+
+ 个人任务池工作模式(本地合成数据)
+
+ 个人任务池 {items.length}/3{" "}
+
void toggle()} />
+
+
+ 当前账号:{user};页面:{focused ? "专注页" : "任务池"};运行:{session.isContinuous() ? "自动" : "手动/未恢复"}
+
+ 领取请求数:{requests}
+
+
+
+
+
+
+
+
+
+ 重新打开专注页
+
+ {message}
+
+ );
+}
+createRoot(document.getElementById("root")!).render(
);
diff --git a/tests/frontend/annotation-work-entry.test.mjs b/tests/frontend/annotation-work-entry.test.mjs
new file mode 100644
index 00000000..6f33104e
--- /dev/null
+++ b/tests/frontend/annotation-work-entry.test.mjs
@@ -0,0 +1,179 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import fs from "node:fs";
+import ts from "typescript";
+const source = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
+const ast = ts.createSourceFile("page.tsx", source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
+function actualCallback(name, env) {
+ let text;
+ function visit(node) {
+ if (ts.isVariableDeclaration(node) && node.name.getText(ast) === name) text = "const " + node.getText(ast) + ";";
+ if (ts.isFunctionDeclaration(node) && node.name?.getText(ast) === name) text = node.getText(ast);
+ ts.forEachChild(node, visit);
+ }
+ visit(ast);
+ assert.ok(text, name);
+ const js = ts.transpileModule(text, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+ return new Function("env", `with(env){${js}; return ${name};}`)(
+ new Proxy(env, {
+ has: () => true,
+ get: (o, k) => (k === Symbol.unscopables ? undefined : k in o ? o[k] : globalThis[k]),
+ }),
+ );
+}
+function entry({ items = [], pending = "", continuous = false } = {}) {
+ const ctx = {},
+ taskScope = {};
+ let epoch = 0,
+ allocations = 0,
+ opened = "",
+ refill,
+ notice = "",
+ cleared = false;
+ const session = { active: false };
+ const env = {
+ viewportBlocksDispatch: () => false,
+ blindAnnotator: true,
+ canAccessWorkspace: true,
+ projectId: "p",
+ currentUser: { id: "u" },
+ projects: [{ id: "p" }],
+ loading: false,
+ projectLoadError: "",
+ annotationDirty: false,
+ saving: false,
+ randomClaimFlight: { current: false },
+ workEntryFlight: { current: false },
+ claimContextRef: { current: ctx },
+ annotationMutationContextRef: { current: { current: () => taskScope } },
+ leaveDirtyRef: { current: false },
+ claimPermissionRef: { current: true },
+ viewportDeferredEntryRef: { current: null },
+ workPage: true,
+ detailRef: { current: null },
+ selectedTaskId: "task",
+ workSession: {
+ continuousEpoch: () => epoch,
+ owns: () => true,
+ acquire: async () => ({ status: "acquired", session }),
+ setContinuous: () => epoch++,
+ prefersContinuous: () => continuous,
+ release: () => {},
+ activate: () => true,
+ pauseContinuous: () => {},
+ },
+ pendingClaimRequest: () => pending,
+ randomClaimTask: async () => allocations++,
+ reconcileWorkBuffer: async (_, auto) => {
+ refill = auto;
+ if (auto) allocations++;
+ return { enabled: true, items, occupied: items.length, capacity: 2 };
+ },
+ annotationOperationsApi: { getTaskSummary: async (id) => ({ task: { id } }) },
+ isOwnedRecoverableAnnotationItem: () => true,
+ readyBufferedItems: (x) => x,
+ projectLoadRequestRef: { current: 0 },
+ setLoading: () => {},
+ setProjectLoadError: () => {},
+ setHumanPools: () => {},
+ setTasks: () => {},
+ clearEditHistory: () => {
+ cleared = true;
+ },
+ setDetail: () => {},
+ setTaskDetailRefreshVersion: () => {},
+ openWorkTask: (id, s) => {
+ opened = id;
+ s.active = true;
+ },
+ navigate: () => {},
+ setRandomClaimNotice: (x) => (notice = x),
+ setWorkEntryBusy: () => {},
+ getActionErrorMessage: (e) => {
+ throw e;
+ },
+ };
+ return { run: actualCallback("startWork", env), state: () => ({ allocations, opened, refill, notice, cleared }) };
+}
+test("actual focused entry keeps manual mode and opens the selected owned task without allocation", async () => {
+ const x = entry({ items: [{ id: "owned", legacyTaskId: "task" }], pending: "unresolved-request" });
+ await x.run();
+ assert.deepEqual(x.state(), { allocations: 0, opened: "task", refill: false, notice: "", cleared: true });
+});
+test("actual focused entry with manual empty buffer never allocates", async () => {
+ const x = entry();
+ await x.run();
+ assert.equal(x.state().allocations, 0);
+ assert.equal(x.state().opened, "");
+});
+test("actual focused entry only reconciles an unresolved claim automatically when mode is explicitly on", async () => {
+ const x = entry({ continuous: true, pending: "unresolved-request" });
+ await x.run();
+ assert.equal(x.state().allocations, 1);
+});
+test("actual click on an owned task resumes a manual focused session without a claim", async () => {
+ const session = { active: false };
+ let opened = "",
+ mode,
+ claims = 0;
+ const scope = {};
+ const env = {
+ viewportBlocksDispatch: () => false,
+ canAccessWorkspace: true,
+ workEntryFlight: { current: false },
+ claimContextRef: { current: scope },
+ claimPermissionRef: { current: true },
+ setWorkEntryBusy: () => {},
+ workSession: {
+ acquire: async () => ({ status: "acquired", session }),
+ prefersContinuous: () => false,
+ setContinuous: (_, value) => (mode = value),
+ release: () => {},
+ },
+ explicitBufferTask: { current: "" },
+ openWorkTask: (id, s) => {
+ opened = id;
+ s.active = true;
+ },
+ randomClaimTask: () => claims++,
+ };
+ await actualCallback("resumeOwnedTask", env)("owned");
+ assert.equal(opened, "owned");
+ assert.equal(mode, false);
+ assert.equal(claims, 0);
+});
+
+test("actual manual random-click acquires a session and makes exactly one explicit claim", async () => {
+ const session = { active: false };
+ const scope = {};
+ let epoch = 0,
+ claims = 0;
+ const env = {
+ workPage: true,
+ viewportBlocksDispatch: () => false,
+ canAccessWorkspace: true,
+ annotationDirty: false,
+ workEntryFlight: { current: false },
+ claimContextRef: { current: scope },
+ claimPermissionRef: { current: true },
+ setWorkEntryBusy: () => {},
+ workSession: {
+ active: false,
+ acquire: async () => ({ status: "acquired", session }),
+ prefersContinuous: () => false,
+ setContinuous: () => epoch++,
+ continuousEpoch: () => epoch,
+ owns: () => true,
+ release: () => {},
+ },
+ randomClaimTask: async (enter, current, s) => {
+ assert.equal(enter, true);
+ assert.equal(current(), true);
+ assert.equal(s, session);
+ claims++;
+ s.active = true;
+ },
+ };
+ await actualCallback("claimTaskManually", env)();
+ assert.equal(claims, 1);
+});
diff --git a/tests/frontend/annotation-work-mode.test.mjs b/tests/frontend/annotation-work-mode.test.mjs
new file mode 100644
index 00000000..b9015c1c
--- /dev/null
+++ b/tests/frontend/annotation-work-mode.test.mjs
@@ -0,0 +1,145 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import {
+ readContinuousWorkMode,
+ writeContinuousWorkMode,
+ refillAnnotationWorkBuffer,
+} from "../../src/lib/annotation-work-mode.js";
+const storage = () => {
+ const values = new Map();
+ return { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => values.set(key, value) };
+};
+test("manual by default; explicit account choice survives reload without leaking to another account", () => {
+ const store = storage();
+ assert.equal(readContinuousWorkMode("alice", store), false);
+ writeContinuousWorkMode("alice", true, store);
+ assert.equal(readContinuousWorkMode("alice", store), true);
+ assert.equal(readContinuousWorkMode("bob", store), false);
+ writeContinuousWorkMode("alice", false, store);
+ assert.equal(readContinuousWorkMode("alice", store), false);
+ const broken = {
+ getItem() {
+ throw Error("blocked");
+ },
+ setItem() {
+ throw Error("blocked");
+ },
+ };
+ assert.equal(readContinuousWorkMode("alice", broken), false);
+ assert.throws(() => writeContinuousWorkMode("alice", true, broken));
+});
+test("manual reconciliation never claims; returned owned rework stops refill after one request", async () => {
+ let claims = 0;
+ const item = { id: "rework" };
+ const buffer = { enabled: true, occupied: 1, capacity: 8, items: [item] };
+ const options = {
+ readBuffer: async () => buffer,
+ claim: async () => {
+ claims++;
+ return { item, task: { id: "task" } };
+ },
+ shouldContinue: () => true,
+ onClaim: () => {},
+ };
+ await refillAnnotationWorkBuffer({ ...options, refill: false });
+ assert.equal(claims, 0);
+ await refillAnnotationWorkBuffer({ ...options, refill: true });
+ assert.equal(claims, 1);
+});
+test("fresh allocations stop at capacity and an empty response stops immediately", async () => {
+ let items = [],
+ claims = 0;
+ const options = {
+ readBuffer: async () => ({ enabled: true, occupied: items.length, capacity: 3, items: [...items] }),
+ claim: async () => {
+ claims++;
+ const item = { id: String(claims) };
+ items.push(item);
+ return { item, task: { id: "task" } };
+ },
+ shouldContinue: () => true,
+ onClaim: () => {},
+ refill: true,
+ };
+ const result = await refillAnnotationWorkBuffer(options);
+ assert.equal(claims, 3);
+ assert.equal(result.occupied, 3);
+ let empty = 0;
+ await refillAnnotationWorkBuffer({
+ ...options,
+ readBuffer: async () => ({ enabled: true, occupied: 0, capacity: 8, items: [] }),
+ claim: async () => {
+ empty++;
+ return { item: null };
+ },
+ });
+ assert.equal(empty, 1);
+});
+test("off during a request keeps its confirmed allocation but prevents another request", async () => {
+ let enabled = true,
+ seen = 0,
+ claims = 0,
+ items = [];
+ let release;
+ const pending = new Promise((r) => (release = r));
+ const operation = refillAnnotationWorkBuffer({
+ refill: true,
+ readBuffer: async () => ({ enabled: true, occupied: items.length, capacity: 8, items: [...items] }),
+ shouldContinue: () => enabled,
+ onClaim: () => seen++,
+ claim: async () => {
+ claims++;
+ await pending;
+ const item = { id: "confirmed" };
+ items.push(item);
+ return { item, task: { id: "task" } };
+ },
+ });
+ await Promise.resolve();
+ enabled = false;
+ release();
+ await operation;
+ assert.equal(claims, 1);
+ assert.equal(seen, 1);
+});
+test("unchanged occupancy and pathological capacities cannot produce an unbounded loop", async () => {
+ let claims = 0;
+ await refillAnnotationWorkBuffer({
+ refill: true,
+ readBuffer: async () => ({ enabled: true, occupied: 1, capacity: 8, items: [] }),
+ shouldContinue: () => true,
+ onClaim: () => {},
+ claim: async () => ({ item: { id: String(++claims) }, task: {} }),
+ });
+ assert.equal(claims, 1);
+});
+
+test("known directed rework blocks every automatic refill poll until submitted", async () => {
+ let claims = 0;
+ const items = [{ id: "owned-rework", reviewRound: 1, status: "claimed", currentStageId: "annotation_combined" }];
+ const options = {
+ refill: true,
+ readBuffer: async () => ({ enabled: true, occupied: 1, capacity: 8, items }),
+ shouldContinue: () => true,
+ onClaim: () => {},
+ claim: async () => {
+ claims++;
+ return { item: items[0], task: {} };
+ },
+ };
+ await refillAnnotationWorkBuffer(options);
+ await refillAnnotationWorkBuffer(options);
+ assert.equal(claims, 0);
+});
+
+test("off still stops this document when browser refuses to persist the choice", () => {
+ const store = {
+ getItem: () => "on",
+ setItem() {
+ throw Error("quota");
+ },
+ };
+ assert.equal(readContinuousWorkMode("quota-user", store), true);
+ assert.throws(() => writeContinuousWorkMode("quota-user", false, store));
+ assert.equal(readContinuousWorkMode("quota-user", store), false);
+});