Files
Vito a6f6697c50 feat(activity): shared job registry, Activity page and Scan for metadata (#269)
* feat(activity): shared session job registry with the scan as its first kind

The output scan now runs as the first kind of an in-memory JobRegistry
(py/api/jobs.py, replacing py/api/scan_job.py). Each job keeps exact
counters, at most 200 outcome rows per group and a log capped at 2,000
lines, with root-relative paths and public root labels only. New routes
list, inspect, follow (SSE) and cancel jobs and start the scan
(POST /prompt_manager/jobs/scan, 409 busy while another record-changing
job runs). The legacy /prompt_manager/scan routes attach to the same job
and stream unchanged payloads.

* feat(activity): concept Activity page, top-bar button and progress pill

The workspace gains the concept's Activity page (renderActivity), its
top-bar button and sidebar row, fed by one session job store: a /jobs
read, one SSE stream per running job, a 1-30 s capped backoff after a
dropped stream and a 15 s list poll while visible. A job the server no
longer lists shows as ended, so no pill stays stuck after a restart.

D52: the counts show running jobs and hide at 0; the phone top-bar row
may wrap. D53: the mockup's progress pill links the running job. D54:
real job items with state chips, detail lines and Cancel job. Start and
finish toasts follow the spec copy. Fake browser jobs come from
tests/browser/workspace_activity_fixture.py.

* feat(activity): Scan for metadata with its review dialog and job view

Images -> Import keeps Rescan and adds the concept's "Review scan" row as
"Scan for metadata". It opens the concept's "Review metadata scan" with the
real source count, the fixed "All files" scope, a backup box that downloads
the existing database backup, and Cancel / Start scan. Start scan starts the
session scan job and opens its view; a start refused by another
record-changing job toasts its title as a link.

#/activity/<id> is now the approved job-view mockup: the heading with
Download report (disabled until Activity-2) and Cancel scan, the Progress
surface with the five counter tiles, the Results surface with the outcome
list, path filter and capped rows (Added and Already known open the viewer
through the listing identity), and the collapsed Scan log. Activity item
titles link their views. The scan's progress line groups thousands.

Deviations D55-D57 are recorded in TESTING.md.

* fix(activity): stop the job store on pagehide and let proxied routes outlive teardown

CI's touch coverage run failed "a resolver failure for the ComfyUI link never
stops the shell from mounting" with "route.fulfill: Fetch response has been
disposed": its trace shows two Library thumbnail requests started by the last
route still in the spec's proxy-mount handler while the page fixture was torn
down.

- The proxy-mount handler ignores only that teardown error, and the workspace
  and Activity specs unroute with ignoreErrors before teardown.
- The Activity store now ends its work on pagehide: the poll and backoff timers
  stop, the job streams close and a list read in flight is aborted (an older
  read is also aborted when a newer one starts). A pageshow from the
  back-forward cache reads the list again.

* test(activity): compare the job view log's line height to the hundredth of a pixel

WebKit reports the mockup's 11px/1.7 line height as 18.700001px, so the job view comparison failed on WebKit only. The measurement now rounds it, as other engine-dependent values are normalized.

* fix(activity): final review round for the job store, job view and scan rows

- Store: one failed list read no longer marks running jobs as ended; two in
  a row, or a good list without them, do (M1). A good read resets the
  backoff, except for a stream that keeps dropping while the list reads fine
  (M2). Finished jobs the server pruned are dropped (M3). Numbers group as
  en-US, like the server's lines (M13).
- Job view: a job the store's list no longer has shows the not-found state
  (M3). Focus moves to the job heading when Cancel scan goes away, and to the
  item's title link in the list (M4). The detail is read only when its listed
  rows change or the open log grows, using the summary's new listed and
  log_length (M12).
- Viewer rows open by exact identity: each scan row records its root's
  listing index, the source must still carry the row's label, and the
  workspace listing gains an exact `path` filter (M8, M9).
- Scan rows name the prompt by its Library title, stored title first (M10).
- A toast with a link stays 20 s, and any toast stays while hovered or
  focused (M14).
- A real two-batch scan cancelled after batch 1 keeps partial counts (M7).
- TESTING.md: D54, D56 and D57 updated (M5, M6).

* test(activity): cover the held and linked toast and a job view pruned by the server

Unit tests for the toast's 6.5 s and 20 s timeouts and its hover and focus hold, and a browser test for a finished job's open view after the server stops listing it. Coverage floors are only raised.
2026-10-21 14:22:30 -07:00

122 lines
4.7 KiB
JavaScript

/**
* State of the output-image scan as seen by the dashboard.
*
* The scan runs on the server as a background job (see py/api/jobs.py) and
* reports progress over SSE. This module turns those events into a small
* immutable state object and answers the UI questions that depend on it:
* what dismissing the modal should do, what the minimized pill should say, and
* how to rebuild the view from GET /prompt_manager/scan/status after a reload.
*
* Loadable in the browser (window.ScanProgress) and in Node for tests.
*/
(function (root) {
"use strict";
const IDLE = "idle";
const RUNNING = "running";
const DONE = "done";
const FAILED = "failed";
function initialState() {
return {
status: IDLE,
progress: 0,
statusText: "",
processed: 0,
found: 0,
added: 0,
linked: 0,
message: "",
};
}
function num(value, fallback) {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function text(value, fallback) {
return typeof value === "string" ? value : fallback;
}
/**
* @param {object} state current state (never mutated)
* @param {object} event a decoded SSE payload: {type: "progress"|"complete"|"error", ...}
* @returns {object} the next state, or `state` itself when the event changes nothing
*/
function reduceScanEvent(state, event) {
if (!event || typeof event !== "object") return state;
if (event.type === "progress") {
return {
...state,
status: RUNNING,
progress: num(event.progress, state.progress),
statusText: text(event.status, state.statusText),
processed: num(event.processed, state.processed),
found: num(event.found, state.found),
};
}
if (event.type === "complete" || event.type === "cancelled") {
const counts = Object.fromEntries(
['pairs_added','pair_links_added','enriched','unchanged','ambiguous','unresolved','conflict','failed']
.map(key => [key, num(event[key], state[key] || 0)])
);
return {
...state,
...counts,
status: DONE,
cancelled: event.type === "cancelled",
progress: event.type === "cancelled" ? state.progress : 100,
statusText: event.type === "cancelled" ? "Scan cancelled." : "Scan completed!",
processed: num(event.processed, state.processed),
found: num(event.found, state.found),
added: num(event.added, state.added),
linked: num(event.linked, state.linked),
};
}
if (event.type === "error") {
return { ...state, status: FAILED, message: text(event.message, "Scan failed") };
}
return state;
}
/** Rebuild the state from the /prompt_manager/scan/status payload. */
function fromStatus(status) {
if (!status || !status.running) return initialState();
const state = reduceScanEvent(initialState(), status.last_event);
if (state.status !== IDLE) return state;
return { ...state, status: RUNNING, statusText: "Scanning..." };
}
/** Closing a running scan's modal only hides it; the scan continues. */
function dismissAction(state) {
return state && state.status === RUNNING ? "minimize" : "close";
}
/** Label for the minimized progress pill; empty when nothing is running. */
function progressLabel(state) {
if (!state || state.status !== RUNNING) return "";
const pct = Math.round(state.progress);
return `Scanning ${pct}% · ${state.processed} files · ${state.found} prompts`;
}
function completionLabel(state) {
const review = (state.ambiguous || 0) + (state.unresolved || 0) + (state.conflict || 0);
return `${state.cancelled ? 'Scan cancelled' : 'Scan completed'}! Processed ${state.processed} files, found ${state.found} prompts; ${state.added} new image records, ${state.linked} links, ${state.enriched || 0} enriched, ${state.unchanged || 0} unchanged; ${state.pairs_added || 0} new pairs, ${state.pair_links_added || 0} pair links; ${review} need review, ${state.failed || 0} failed.`;
}
const api = {
initialState,
completionLabel,
reduceScanEvent,
fromStatus,
dismissAction,
progressLabel,
IDLE,
RUNNING,
DONE,
FAILED,
};
if (typeof module !== "undefined" && module.exports) module.exports = api;
else root.ScanProgress = api;
})(typeof window !== "undefined" ? window : globalThis);