fix: 恢复成功后整体替换待写队列,修首屏自愈抢跑写空

This commit is contained in:
2026-11-02 23:30:30 +08:00
parent 62f5168dfe
commit ff1abee438
9 changed files with 101 additions and 43 deletions
+23 -2
View File
@@ -225,7 +225,7 @@ function resolveDshHomeSafe() {
cachedDshHome = baseDshHome();
return cachedDshHome;
}
var PLUGIN_VERSION = false ? "dev" : "0.5.2-local.3";
var PLUGIN_VERSION = false ? "dev" : "0.5.2-local.6";
var name = "dsh-worktable";
var inject = ["webServer", "sessions"];
var HEALTH_PATH = "/api/worktable/health";
@@ -483,12 +483,24 @@ function apply(ctx) {
}
const previews = createPreviewFiles();
ctx.effect(() => () => previews.clear(), "dsh-worktable: preview capabilities");
const stateLog = (line) => {
void (async () => {
try {
const fsx = await import("node:fs/promises");
const dir = pathResolve(resolveDshHomeSafe(), "logs");
await fsx.mkdir(dir, { recursive: true });
await fsx.appendFile(pathResolve(dir, "worktable-state.log"), (/* @__PURE__ */ new Date()).toISOString() + " " + line + "\n", "utf8");
} catch {
}
})();
};
const register = (route, publicAsset = false) => ctx.effect(() => webServer.register({
...route,
handler: async (req, res) => {
if (!publicAsset && route.path !== HEALTH_PATH) {
const rejected = await requestRejection(ctx, req);
if (rejected !== void 0) {
if (route.path === "/api/worktable/state") stateLog("REJECT " + req.method + " -> " + rejected);
json(res, rejected, { error: rejectionText(rejected) });
return;
}
@@ -841,12 +853,15 @@ function apply(ctx) {
try {
const raw = await readFile(file, "utf8");
const text = raw.charCodeAt(0) === 65279 ? raw.slice(1) : raw;
json(res, 200, { ok: true, path: file, state: JSON.parse(text) });
const parsed = JSON.parse(text);
json(res, 200, { ok: true, path: file, state: parsed });
} catch (err) {
if (err?.code === "ENOENT") {
stateLog("GET empty (no file)");
json(res, 200, { ok: true, path: file, state: null });
return;
}
stateLog("GET error " + String(err));
json(res, 500, { error: String(err) });
}
return;
@@ -859,6 +874,7 @@ function apply(ctx) {
const body = await readJsonBody(req);
const incoming = body && typeof body.state === "object" && body.state !== null ? body.state : null;
if (!incoming) {
stateLog("POST 400 missing state");
json(res, 400, { error: "missing state" });
return;
}
@@ -870,6 +886,11 @@ function apply(ctx) {
}
const state = previous && typeof previous === "object" && !Array.isArray(previous) ? { ...previous, ...incoming } : incoming;
const text = JSON.stringify(state);
const inN = incoming?.projects?.layouts?.length ?? 0;
const prevN = previous?.projects?.layouts?.length ?? 0;
if (inN < prevN) {
stateLog("WARN projects shrink " + prevN + " -> " + inN + " (keys=" + Object.keys(incoming).join("+") + ")");
}
if (text.length > 20 * 1024 * 1024) {
json(res, 413, { error: "state too large" });
return;