Files
ComfyAssets-ComfyUI_PromptM…/tools/workflow_text_digests.py
Vito 6f02277a1c fix: small cleanup from the audit true-up (#662) (#665)
* refactor(api): one bounded JSON body reader for the API and images.py (#662)

images.py kept its own copy of the chunked, capped JSON object reader.
Both now use py/api/json_body.py; images.py keeps its 1 MiB cap and its
raising form for the thumbnail job.

* fix(api): refuse malformed bodies on the legacy prompt routes with a 400 (#662)

PUT /prompts/{id}, PUT /prompts/{id}/rating, POST and DELETE
/prompts/{id}/tags, POST /prompts/tags, PUT /tags/{tag}, POST /tags/merge
and POST /bulk/delete|tags|category read the body with the bounded JSON
reader /save uses and check field types, so a body that is not a JSON
object, or a field such as {"text": 123}, is a 400 instead of a 500.
A prompt id may still be a number or its text, as the legacy page sends
it; a boolean is refused (prompt_ids [true] deleted prompt 1).
POST /jobs/image-tags refuses a mode that is no text instead of a 500.

* refactor(inspect): both resolvers read PROVEN_VALUE_CATEGORIES (#662)

proves_value hard-coded the four tables in Python and in the browser.
Semantics.proves_value now reads the constant (class-keyed categories by
class, the rest by output slot), the browser resolver loops over its
mirrored copy in local-resolver-support.mjs, and
tests/test_proven_value_parity.py fails when that copy drifts. A test
patches the constant to show the resolver follows it.

* refactor: remove the write-only preview batch count and the test-only node search (#662)

AutoThumbnailWorker set self._batch in four places and never read it.
PromptManagerBase._search_prompts had no caller but its own tests: the
node UIs search through GET /prompt_manager/search. Prompt search itself
stays covered by the database and route tests.

* fix(tools): open libraries through the shared read-only URI helper (#662)

review_image_links, repair_foreign_links (twice) and workflow_text_digests
built their own file: URIs, which SQLite refuses for a library on a
Windows network share (file://server/share/...). They now use
database/sqlite_uri.readonly_uri; repair_foreign_links opens its own
read-write backup by path. The guard in tests/test_sqlite_uri.py scans
tools/ too and also catches a conditional ?mode=ro suffix.

* ci: bound the publish and Viewer.js update jobs by measured limits (#662)

publish.yml and viewerjs-update.yml had no timeout-minutes, and the
workflow guard listed only four workflows. The guard now reads every
workflow and recomputes both new limits from their comments: publish
5 min from its last three green runs; the Viewer.js update 18 min, from
an estimate of its never-run update path (the three measured nightly
runs found no release and give the 5-minute floor).

* docs: correct the redesign plan's CI and PR rules, renumber the second D205 (#662)

docs/application-redesign.md named a Python 3.10-3.13 matrix with macOS,
asked for PR screenshots and said Overview links the legacy metadata
page. It now names the 3.12 floor and the current Linux/Windows split,
asks for visual changes in words, and says the metadata page is reached
by address or from the legacy admin page. TESTING.md used D205 for both
#431 (saved instructions) and #620 (Library sorts); #620's entry is
D206 now.
2026-11-21 09:46:31 -08:00

101 lines
3.7 KiB
Python

"""Digests of every image record's stored workflow and prompt graph text.
Proves the workflow storage rewrite (#249 S6) kept every text byte for byte.
Run it from this repository on the database before the rewrite (or on its
``prompts.db.pre-workflow-storage_*`` backup) and again after, then compare::
python3 tools/workflow_text_digests.py prompts.db.pre-workflow-storage_<stamp> \\
before.sha
python3 tools/workflow_text_digests.py prompts.db after.sha
diff before.sha after.sha # prints nothing when every text is identical
Each line is ``<image id> <sha256 of workflow_data> <sha256 of prompt_metadata>``
(``-`` for none), hashing the exact stored bytes: the inline column before the
rewrite, the document read through the one accessor after it.
Read-only, with no side effects: the database is opened with ``mode=ro``,
and with ``immutable=1`` when no ``-wal``/``-shm`` file exists beside it (a
stopped database or a backup), so SQLite creates none. A live database
already has them and is read through its WAL. Only
``database/workflow_documents.py`` is loaded (by path; it needs only the
standard library), so no package start-up runs and no log file or handler is
added. Run it as the user that runs ComfyUI.
"""
import argparse
import hashlib
import importlib.util
import os
import sqlite3
import sys
from pathlib import Path
_ROOT = Path(__file__).resolve().parents[1]
def _load_database_module(name):
"""``database/<name>.py`` alone, without the ``database`` package."""
path = _ROOT / "database" / f"{name}.py"
spec = importlib.util.spec_from_file_location(f"_pm_{name}", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def _load_documents():
"""``database/workflow_documents.py`` alone, without the ``database`` package."""
return _load_database_module("workflow_documents")
def database_uri(db_path):
"""``mode=ro``, plus ``immutable=1`` when nothing has the database open."""
path = os.path.abspath(db_path)
# #662: the one read-only URI helper, so a library on a network share opens.
uri = _load_database_module("sqlite_uri").readonly_uri(path)
if not any(os.path.exists(path + suffix) for suffix in ("-wal", "-shm")):
uri += "&immutable=1"
return uri
def _digest(value):
if value is None:
return "-"
raw = value if isinstance(value, bytes) else value.encode("utf-8")
return hashlib.sha256(raw).hexdigest()
def write_digests(db_path, out):
"""Write one digest line per image record of ``db_path`` to ``out``."""
documents = _load_documents()
conn = sqlite3.connect(database_uri(db_path), uri=True)
try:
conn.row_factory = sqlite3.Row
conn.text_factory = bytes # inline text exactly as stored, even undecodable
reader = documents.DocumentReader(conn)
count = 0
with open(out, "w", encoding="utf-8") as handle:
for row in conn.execute("SELECT * FROM generated_images ORDER BY id"):
texts = documents.resolve_image_row(reader, row)
handle.write(
f"{row['id']} {_digest(texts.get('workflow_data'))} "
f"{_digest(texts.get('prompt_metadata'))}\n"
)
count += 1
return count
finally:
conn.close()
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("database", help="prompts.db or one of its backups")
parser.add_argument("out", help="file to write the digests to")
args = parser.parse_args(argv)
count = write_digests(args.database, args.out)
print(f"{count} image records")
return 0
if __name__ == "__main__":
sys.exit(main())