* feat(annotations): add image annotation schema with restore validation
Install the image annotation group beside image_favorites and saved_views:
media_annotation_metadata (one row, schema version 1), media_annotation_files
(one private locator per physical file), media_annotation_bytes (one record
per observed SHA-256 with the verified size and mtime_ns) and media_image_tags
(the ordered tag set of a byte record, joined into the global tags registry).
The group migrates in a SAVEPOINT that validates before and after creating
it, with no backfill from prompt or pair tags and no filesystem probe.
annotation_schema_error reads only: it compares the catalog objects after
whitespace compaction, the columns, foreign keys and indexes, and the
persisted values (UUIDs, hex digests, bounds, the 4096-character locator cap
measured past embedded NULs, exactly one metadata row). verify_database_file
checks it with allow_missing=not require_library_state, so old backups
restore and a migrated restore stage must carry it.
Tests pin the exact DDL, a fresh install and idempotent reopen, a current
database without the group gaining it with legacy rows untouched, every
partial or malformed shape rejected without repair, install failure rolling
back and retrying, a WAL backup/restore round trip, staged-failure
immutability of source, live and safety files with every handle closed, and
the require_library_state gate.
* feat(annotations): persist per-image tag sets in the shared vocabulary
Add database/annotations.py (AnnotationOperations, mixed into
PromptDatabase): read_image_tags, replace_image_tags and add_image_tags
run in one _pair_transaction fenced by the storage epoch, own the byte
record by exact locator and digest, always supply UUID ids, rewrite the
ordered junction and bump tag_revision plus image_index_revision.
Revision mismatches raise AnnotationCommandError("tag-conflict") with
the current set; the identical set at the current or previous revision
is replayed without writes; a union over 100 names is "tag-limit".
normalize_image_tags validates each name with validate_vocabulary_tag_name
and folds case-insensitive repeats keeping the first spelling.
refresh_image_fingerprint, current_image_tags (chunked, fingerprint
matched, newest verification wins) and iter_image_tag_records serve the
S1 cheap reads.
Vocabulary participation: _move_tag_links also carries media_image_tags,
legacy merge_tags moves image-only sources, tag rows gain image_count
(files with any byte record carrying the tag), impact gains images and
rename/delete receipts gain images_changed. vocabulary-state.mjs accepts
the optional counts and names images in the delete text.
Browser fixtures clear the three annotation data tables on reset (never
the metadata row); TESTING.md documents the schema beside favorites.
* feat(annotations): add bounded private media capture store
Add utils/media_capture.py: CaptureStore copies one rooted media file
into an owner-only prompt-manager-captures-* temp directory, streaming
it in 1 MiB chunks while computing the whole-file SHA-256, and compares
the opened descriptor with the path (dev, ino where meaningful, size,
mtime_ns, ctime_ns) before and after the copy; any disagreement raises
CaptureChanged and removes the copy. Symlinked files or directories,
root escapes, non-media names and directories raise CaptureDenied; the
locator is normcase(join(realpath(root), relative)), the listing's
identity, so a symlinked root matches it.
Limits are the plan's: 256 MiB per file on stat and stream
(CaptureTooLarge), 8 completed captures, 512 MiB reserved and 2
capture/verify workers with nonblocking admission (CaptureCapacity),
900 s idle and 1800 s hard lifetimes on an injected clock. acquire and
release_use keep an in-use capture past expiry, release and
invalidate_all until its last use ends; close removes only owned files
and the owned directory. A header-only probe of the copy marks Generate
unsupported above 4096 px per side or 16,777,216 px, for unreadable
headers and for video; verify re-hashes the current file without a
copy. Every handle closes before unlink.
tests/test_media_capture.py covers exact bytes and owner-only modes,
alias and escape denial, the symlinked-root locator, change detection
during the copy (size, same-size rewrite with restored mtime through
the POSIX change time, inode replacement), both size caps, count, byte
and worker admission, idle and hard expiry with in-use retention,
cleanup scope, handle release at unlink, dimension and video
readiness, and the allowlist equality with the inspection service.
TESTING.md documents the store and the Windows change-time limit.
* test(annotations): wait on the POSIX change time only where it exists
The same-size rewrite case waits for the filesystem clock to pass the
source's ctime so the detection is deterministic on Linux's coarse
timestamps; Windows reports the creation time as st_ctime, where the
probe could never advance, so the wait runs only on POSIX.
* fix(annotations): compare the change time only between fstat reads
Python 3.12+ on Windows reports the change time from stat but the
creation time from fstat, so a freshly written file whose creation and
write straddle a clock tick failed the pre-read identity check with
CaptureChanged. The path-versus-descriptor checks now compare size,
mtime_ns and dev/ino; ctime_ns is compared between the fstat before
and after the copy, where it still detects a same-size rewrite with a
restored mtime on POSIX.
The symlinked-root test asserted the locator against the unresolved
temp path, which macOS (/var -> /private/var) and Windows (short names)
resolve differently; it now compares against the real path.
* test(browser): give the inspector evidence matrix the concept comparisons' budget
The touch run of 'full inspector attaches matched theme and mobile
evidence with reachable content' captures 32 screenshots across four
viewports and took 23.5 s, then 31.6 s, in the sharded coverage job,
hitting the 30 s default. Like the concept comparison specs it now sets
an explicit 120 s timeout; the CI-like pinned run (4 CPUs, 3 workers,
repeat-each 3) passes 198/198 with the matrix at 18-20 s on touch.
* fix(annotations): compare capture identity like with like and keep paths out of errors
Native Windows CI (Python 3.12/3.13) rejected untouched files with
CaptureChanged because the path stat and the descriptor fstat disagree
there (change time versus creation time, possibly dev/ino encodings).
The descriptor is now checked against the path only on the API-stable
facts (size, mtime_ns); the full identity (size, mtime_ns, ctime_ns,
dev/ino where meaningful) is compared between the fstat before and
after the copy and between the path stats before and after. A test
injects shifted fstat results to simulate the Windows discrepancy, and
a fresh file is captured twice with no platform guard.
Error messages carry strerror only, never the path (the path goes to
the debug log); a test asserts no CaptureError names the root. Files
are unlinked outside the store lock; close() during an in-flight
capture lets that worker remove its temp file and then the directory.
get() documents that readers of capture.path must acquire/release_use.
The test fixture resolves its temp root (macOS /private/var, Windows
8.3 names). TESTING.md states the real identity contract.
* fix(annotations): move image revisions when a fingerprint or the vocabulary changes
A replayed save that observes a new verified (size, mtime_ns) now bumps
image_index_revision, like refresh_image_fingerprint, so caches keyed
on it see the record become current again; an identical replay still
moves nothing.
Vocabulary rename, merge and delete (native and legacy paths) bump the
tag_revision of every image set carrying the source tag, in the same
transaction and before the links move or cascade, plus one
image_index_revision bump when any set was affected. A Tag dialog
opened before the change therefore gets tag-conflict with the renamed
set as current instead of recreating the old name.
* test(annotations): accept change detection wherever fstat exposes a change time
Native Windows CI on Python 3.13 detected the same-size, mtime-restored
rewrite through the descriptor's change time (3.10-3.12 report the
creation time there and complete the torn copy). The test asserted the
platform, not the signal: it now accepts CaptureChanged plus temp
removal wherever it happens, requires detection on POSIX and on
Windows 3.13+, and otherwise requires the digest to describe the
copied bytes. A new test injects a drifting fstat change time so the
detection path runs on every platform; TESTING.md records the limit as
"Windows before Python 3.13".
* test(annotations): assert the rewrite outcome, not the platform
Native Windows CI detected the same-size, mtime-restored rewrite on
3.12 and missed it on 3.13 in the same run: the path stat's change time
(Python 3.12+ fast path) only moves when the clock ticks between the
pre-stat and the rewrite, and the tick wait was skipped on Windows. The
wait now runs everywhere with fresh probe names (Windows tunnels the
creation time of a reused name), and the test asserts the outcome: a
detected change leaves no temp file; an undetected one is accepted on
Windows only and must produce a digest of the copied bytes; POSIX must
detect it. The injected-fstat test keeps the detection path covered on
every platform.
* fix(images): keep the Filters label node across a listing reload
Every listing reload reports the active refinement count, and
setActiveFilters rewrote the button's textContent even when the label
was unchanged, replacing its text node. WebKit drops a click whose
mousedown hit a node that was detached before mouseup, so a Safari
user who types a search and clicks Filters inside the 250 ms debounce
lost the click, and the webkit page workflows timed out in the
source-change recovery tests (1 in ~30 runs of the pinned image; an
instrumented probe showed pointerdown/mousedown, a DOM mutation,
pointerup/mouseup and no click event).
The label is rewritten only when it changes. The toolbar unit test
counts textContent writes (1 for an unchanged count before, 0 after),
and a workspace-images test pins the text node identity across a
search reload: 0/6 before the fix on chromium and webkit, 6/6 after,
with the recovery and Refine tests 36/36 in the pinned CI-like run.
599 lines
24 KiB
Python
599 lines
24 KiB
Python
"""Per-image tag sets (#249 A2): byte-exact storage in the shared vocabulary."""
|
|
|
|
import hashlib
|
|
import os
|
|
import re
|
|
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
try:
|
|
from tests.open_handles import assert_closed
|
|
except ImportError: # discovered with tests/ as the top-level directory
|
|
from open_handles import assert_closed
|
|
|
|
from database.annotation_schema import annotation_schema_error
|
|
from database.library_commands import LibraryCommandError
|
|
from database.operations import PromptDatabase
|
|
from utils.hashing import generate_prompt_hash
|
|
|
|
try: # guarded so a missing module fails assertions, not test loading
|
|
from database import annotations as annotations_module
|
|
from database.annotations import (
|
|
MAX_IMAGE_TAGS,
|
|
AnnotationCommandError,
|
|
normalize_image_tags,
|
|
)
|
|
except ImportError:
|
|
annotations_module = None
|
|
MAX_IMAGE_TAGS = None
|
|
AnnotationCommandError = None
|
|
normalize_image_tags = None
|
|
|
|
UUID = re.compile(r"[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}")
|
|
DIGEST_A = hashlib.sha256(b"bytes a").hexdigest()
|
|
DIGEST_B = hashlib.sha256(b"bytes b").hexdigest()
|
|
SIZE = 7
|
|
MTIME = 1_700_000_000_123_456_789
|
|
ANNOTATION_TABLES = (
|
|
"media_annotation_files",
|
|
"media_annotation_bytes",
|
|
"media_image_tags",
|
|
"media_annotation_metadata",
|
|
)
|
|
LEGACY_TABLES = (
|
|
"tags",
|
|
"prompt_tags",
|
|
"prompt_pair_tags",
|
|
"generated_images",
|
|
"image_prompt_pairs",
|
|
)
|
|
|
|
|
|
class AnnotationTestCase(unittest.TestCase):
|
|
def setUp(self):
|
|
self.assertIsNotNone(
|
|
annotations_module, "database.annotations is not implemented"
|
|
)
|
|
directory = tempfile.TemporaryDirectory()
|
|
self.addCleanup(directory.cleanup)
|
|
self.addCleanup(assert_closed, self, directory.name)
|
|
self.root = directory.name
|
|
self.db = PromptDatabase(os.path.join(self.root, "annotations.db"))
|
|
self.addCleanup(self.db.close_all)
|
|
self.epoch = self.db.get_execution_epoch()
|
|
|
|
def locator(self, name: str) -> str:
|
|
return os.path.normcase(os.path.join(os.path.realpath(self.root), name))
|
|
|
|
def replace(self, loc, digest=DIGEST_A, tags=(), *, revision=0, **over):
|
|
options = {
|
|
"locator": loc,
|
|
"sha256": digest,
|
|
"size": SIZE,
|
|
"mtime_ns": MTIME,
|
|
"expected_epoch": self.epoch,
|
|
"expected_tag_revision": revision,
|
|
"tags": list(tags) if isinstance(tags, tuple) else tags,
|
|
}
|
|
options.update(over)
|
|
return self.db.replace_image_tags(**options)
|
|
|
|
def add(self, loc, digest=DIGEST_A, tags=(), **over):
|
|
options = {
|
|
"locator": loc,
|
|
"sha256": digest,
|
|
"size": SIZE,
|
|
"mtime_ns": MTIME,
|
|
"expected_epoch": self.epoch,
|
|
"tags": list(tags) if isinstance(tags, tuple) else tags,
|
|
}
|
|
options.update(over)
|
|
return self.db.add_image_tags(**options)
|
|
|
|
def read(self, loc, digest=DIGEST_A):
|
|
return self.db.read_image_tags(loc, digest)
|
|
|
|
def rows(self, *tables):
|
|
"""Every row of each table; ``verified_at`` is a fingerprint, not content."""
|
|
conn = self.db.model.get_connection()
|
|
result = []
|
|
for table in tables:
|
|
columns = [
|
|
row[1]
|
|
for row in conn.execute(f"PRAGMA table_info({table})")
|
|
if row[1] != "verified_at"
|
|
]
|
|
result.append(
|
|
[
|
|
tuple(row)
|
|
for row in conn.execute(
|
|
f"SELECT {','.join(columns)} FROM {table} ORDER BY 1,2"
|
|
)
|
|
]
|
|
)
|
|
return result
|
|
|
|
def counts(self):
|
|
conn = self.db.model.get_connection()
|
|
return {
|
|
table: conn.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0]
|
|
for table in ANNOTATION_TABLES
|
|
}
|
|
|
|
def index_revision(self):
|
|
return (
|
|
self.db.model.get_connection()
|
|
.execute("SELECT image_index_revision FROM media_annotation_metadata")
|
|
.fetchone()[0]
|
|
)
|
|
|
|
def schema_clean(self):
|
|
self.assertIsNone(
|
|
annotation_schema_error(self.db.model.get_connection(), allow_missing=False)
|
|
)
|
|
|
|
|
|
class NormalizeImageTagsTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.assertIsNotNone(
|
|
normalize_image_tags, "database.annotations is not implemented"
|
|
)
|
|
|
|
def test_case_insensitive_dedupe_keeps_first_spelling_and_position(self):
|
|
self.assertEqual(
|
|
normalize_image_tags([" Forest ", "mist", "forest", "MIST", "Rain"]),
|
|
["Forest", "mist", "Rain"],
|
|
)
|
|
self.assertEqual(normalize_image_tags([]), [])
|
|
self.assertEqual(MAX_IMAGE_TAGS, 100)
|
|
|
|
def test_limit_and_invalid_names_rejected(self):
|
|
for bad in (
|
|
None,
|
|
"forest",
|
|
{"a": 1},
|
|
[1],
|
|
[None],
|
|
["a,b"],
|
|
[""],
|
|
[" "],
|
|
["x" * 101],
|
|
[f"t{i}" for i in range(MAX_IMAGE_TAGS + 1)],
|
|
):
|
|
with self.subTest(bad=bad), self.assertRaises(ValueError):
|
|
normalize_image_tags(bad)
|
|
# Duplicates fold before the cap is applied.
|
|
many = [f"t{i}" for i in range(MAX_IMAGE_TAGS)]
|
|
self.assertEqual(normalize_image_tags(many + ["T0", "T99"]), many)
|
|
|
|
|
|
class ReplaceImageTagsTests(AnnotationTestCase):
|
|
def test_first_save_creates_owner_and_byte_atomically_including_empty_set(self):
|
|
loc = self.locator("a.png")
|
|
receipt = self.replace(loc, tags=["Forest", "mist", "forest"])
|
|
self.assertEqual(receipt["tags"], ["Forest", "mist"])
|
|
self.assertEqual(receipt["tag_revision"], 1)
|
|
self.assertEqual(
|
|
receipt,
|
|
{
|
|
"library_epoch": self.epoch,
|
|
"sha256": DIGEST_A,
|
|
"tags": ["Forest", "mist"],
|
|
"tag_revision": 1,
|
|
"replayed": False,
|
|
},
|
|
)
|
|
conn = self.db.model.get_connection()
|
|
files = conn.execute("SELECT * FROM media_annotation_files").fetchall()
|
|
self.assertEqual(len(files), 1)
|
|
self.assertRegex(files[0]["id"], UUID)
|
|
self.assertEqual(files[0]["locator"], loc)
|
|
byte = conn.execute("SELECT * FROM media_annotation_bytes").fetchone()
|
|
self.assertRegex(byte["id"], UUID)
|
|
self.assertEqual(
|
|
(byte["file_id"], byte["sha256"], byte["size"], byte["mtime_ns"]),
|
|
(files[0]["id"], DIGEST_A, SIZE, MTIME),
|
|
)
|
|
self.assertEqual(byte["tag_revision"], 1)
|
|
self.assertEqual(byte["verified_at"], byte["first_seen_at"])
|
|
self.assertEqual(
|
|
[
|
|
tuple(row)
|
|
for row in conn.execute(
|
|
"SELECT t.name, mit.position FROM media_image_tags mit "
|
|
"JOIN tags t ON t.id = mit.tag_id ORDER BY mit.position"
|
|
)
|
|
],
|
|
[("Forest", 0), ("mist", 1)],
|
|
)
|
|
self.assertEqual(self.index_revision(), 1)
|
|
# An explicit empty set is a real save: owner and byte record exist.
|
|
empty = self.replace(self.locator("b.png"), DIGEST_B, [])
|
|
self.assertEqual((empty["tags"], empty["tag_revision"]), ([], 1))
|
|
self.assertEqual(self.counts()["media_annotation_files"], 2)
|
|
self.assertEqual(self.counts()["media_annotation_bytes"], 2)
|
|
self.assertEqual(
|
|
self.db.read_image_tags(self.locator("b.png"), DIGEST_B),
|
|
{"tags": [], "tag_revision": 1, "prior_count": 0},
|
|
)
|
|
self.assertEqual(self.index_revision(), 2)
|
|
self.schema_clean()
|
|
|
|
def test_save_changes_only_target_byte_set(self):
|
|
a, b, c = self.locator("a.png"), self.locator("b.png"), self.locator("c.png")
|
|
prompt = self.db.save_prompt(
|
|
"Shared forest",
|
|
tags=["shared"],
|
|
prompt_hash=generate_prompt_hash("Shared forest"),
|
|
)
|
|
pair = self.db.save_prompt_pair("Shared forest", "blurry", tags=["pairtag"])
|
|
image_a = self.db.link_image_to_prompt(prompt, a)
|
|
image_b = self.db.link_image_to_prompt(prompt, b)
|
|
self.db.link_image_to_pair(pair, image_a, {"seed": 1})
|
|
self.db.link_image_to_pair(pair, image_b, {"seed": 2})
|
|
self.replace(b, DIGEST_A, ["sibling"])
|
|
self.replace(c, DIGEST_A, ["copy"]) # equal bytes at another path
|
|
before = self.rows(*LEGACY_TABLES)
|
|
sibling_before = self.db.read_image_tags(b, DIGEST_A)
|
|
copy_before = self.db.read_image_tags(c, DIGEST_A)
|
|
|
|
receipt = self.replace(a, DIGEST_A, ["only a"])
|
|
|
|
self.assertEqual((receipt["tags"], receipt["tag_revision"]), (["only a"], 1))
|
|
after = self.rows(*LEGACY_TABLES)
|
|
# The registry gained the new name; nothing else moved.
|
|
self.assertEqual(after[1:], before[1:])
|
|
self.assertEqual([row for row in after[0] if row[1] != "only a"], before[0])
|
|
self.assertEqual(self.db.read_image_tags(b, DIGEST_A), sibling_before)
|
|
self.assertEqual(self.db.read_image_tags(c, DIGEST_A), copy_before)
|
|
self.assertEqual(self.db.get_prompt_by_id(prompt)["tags"], ["shared"])
|
|
self.assertEqual(self.db.get_prompt_pair(pair)["tags"], ["pairtag"])
|
|
|
|
def test_changed_bytes_at_same_locator_start_empty_and_keep_prior(self):
|
|
loc = self.locator("a.png")
|
|
self.replace(loc, DIGEST_A, ["old"])
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_B),
|
|
{"tags": [], "tag_revision": 0, "prior_count": 1},
|
|
)
|
|
receipt = self.replace(loc, DIGEST_B, ["new"], mtime_ns=MTIME + 5)
|
|
self.assertEqual((receipt["tags"], receipt["tag_revision"]), (["new"], 1))
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_A),
|
|
{"tags": ["old"], "tag_revision": 1, "prior_count": 1},
|
|
)
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_B),
|
|
{"tags": ["new"], "tag_revision": 1, "prior_count": 1},
|
|
)
|
|
self.assertEqual(self.counts()["media_annotation_files"], 1)
|
|
self.assertEqual(self.counts()["media_annotation_bytes"], 2)
|
|
self.assertEqual(
|
|
self.db.read_image_tags(self.locator("unseen.png"), DIGEST_A),
|
|
{"tags": [], "tag_revision": 0, "prior_count": 0},
|
|
)
|
|
|
|
def test_revision_conflict_returns_current_set(self):
|
|
loc = self.locator("a.png")
|
|
self.replace(loc, tags=["a"])
|
|
before = self.rows(*ANNOTATION_TABLES, "tags")
|
|
with self.assertRaises(AnnotationCommandError) as ctx:
|
|
self.replace(loc, tags=["b"], revision=0)
|
|
self.assertEqual(
|
|
(ctx.exception.code, ctx.exception.current["tag_revision"]),
|
|
("tag-conflict", 1),
|
|
)
|
|
self.assertIsInstance(ctx.exception, LibraryCommandError)
|
|
self.assertEqual(ctx.exception.epoch, self.epoch)
|
|
self.assertEqual(ctx.exception.current["tags"], ["a"])
|
|
self.assertEqual(ctx.exception.current["sha256"], DIGEST_A)
|
|
with self.assertRaises(AnnotationCommandError) as ahead:
|
|
self.replace(loc, tags=["a"], revision=5)
|
|
self.assertEqual(ahead.exception.code, "tag-conflict")
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES, "tags"), before)
|
|
self.assertFalse(self.db.model.get_connection().in_transaction)
|
|
|
|
def test_identical_replay_after_commit_is_success_replayed(self):
|
|
loc = self.locator("a.png")
|
|
first = self.replace(loc, tags=["a", "b"])
|
|
self.assertEqual((first["tag_revision"], first["replayed"]), (1, False))
|
|
before = self.rows(*ANNOTATION_TABLES)
|
|
# The lost-response retry: same set, the revision it was sent against.
|
|
retry = self.replace(loc, tags=["a", "b"], revision=0)
|
|
self.assertEqual(retry, {**first, "replayed": True})
|
|
# Saving the current set at the current revision writes nothing either.
|
|
again = self.replace(loc, tags=["a", "b"], revision=1)
|
|
self.assertEqual(again, {**first, "replayed": True})
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES), before)
|
|
# Order is part of the set.
|
|
reordered = self.replace(loc, tags=["b", "a"], revision=1)
|
|
self.assertEqual(
|
|
(reordered["tags"], reordered["tag_revision"], reordered["replayed"]),
|
|
(["b", "a"], 2, False),
|
|
)
|
|
self.assertEqual(self.index_revision(), 2)
|
|
|
|
def test_replay_with_new_fingerprint_moves_only_the_index_revision(self):
|
|
loc = self.locator("a.png")
|
|
first = self.replace(loc, tags=["a"])
|
|
index = self.index_revision()
|
|
same = self.replace(loc, tags=["a"], revision=1)
|
|
self.assertEqual(same, {**first, "replayed": True})
|
|
self.assertEqual(self.index_revision(), index) # identical: nothing moved
|
|
touched = self.replace(loc, tags=["a"], revision=1, mtime_ns=MTIME + 5)
|
|
self.assertEqual(touched, {**first, "replayed": True})
|
|
self.assertEqual(self.index_revision(), index + 1) # currency changed
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME + 5)}), {loc: ["a"]}
|
|
)
|
|
self.assertEqual(self.db.current_image_tags({loc: (SIZE, MTIME)}), {})
|
|
self.assertEqual(self.read(loc)["tag_revision"], 1)
|
|
|
|
def test_epoch_checked_inside_transaction(self):
|
|
loc = self.locator("a.png")
|
|
seen = []
|
|
original = PromptDatabase._library_epoch
|
|
|
|
def spy(db, conn, expected_epoch):
|
|
seen.append(conn.in_transaction)
|
|
return original(db, conn, expected_epoch)
|
|
|
|
with patch.object(PromptDatabase, "_library_epoch", spy):
|
|
self.replace(loc, tags=["a"])
|
|
self.add(loc, tags=["b"])
|
|
self.assertEqual(seen, [True, True])
|
|
stale = "11111111-2222-4333-8444-555555555555"
|
|
for call in (
|
|
lambda: self.replace(
|
|
self.locator("x.png"), tags=["a"], expected_epoch=stale
|
|
),
|
|
lambda: self.add(self.locator("x.png"), tags=["a"], expected_epoch=stale),
|
|
):
|
|
with self.assertRaises(LibraryCommandError) as ctx:
|
|
call()
|
|
self.assertEqual(ctx.exception.code, "storage-changed")
|
|
self.assertEqual(ctx.exception.epoch, self.epoch)
|
|
self.assertEqual(self.counts()["media_annotation_files"], 1)
|
|
for bad_epoch in (None, 7, "not-a-uuid"):
|
|
with self.assertRaises(ValueError):
|
|
self.replace(loc, tags=["a"], expected_epoch=bad_epoch)
|
|
|
|
def test_limit_and_invalid_names_rejected_without_partial_write(self):
|
|
loc = self.locator("a.png")
|
|
before = self.rows(*ANNOTATION_TABLES, "tags")
|
|
for tags in (
|
|
["ok", "a,b"],
|
|
["ok", ""],
|
|
["ok", 3],
|
|
"ok",
|
|
[f"t{i}" for i in range(101)],
|
|
):
|
|
with self.subTest(tags=tags), self.assertRaises(ValueError):
|
|
self.replace(loc, tags=tags)
|
|
with self.subTest(tags=tags, command="add"), self.assertRaises(ValueError):
|
|
self.add(loc, tags=tags)
|
|
for field, value in (
|
|
("locator", ""),
|
|
("locator", None),
|
|
("locator", "x" * 4097),
|
|
("sha256", DIGEST_A.upper()),
|
|
("sha256", DIGEST_A[:-1]),
|
|
("sha256", None),
|
|
("size", -1),
|
|
("size", True),
|
|
("size", "7"),
|
|
("mtime_ns", 1.5),
|
|
("mtime_ns", None),
|
|
("expected_tag_revision", -1),
|
|
("expected_tag_revision", False),
|
|
):
|
|
with self.subTest(field=field, value=value), self.assertRaises(ValueError):
|
|
self.replace(loc, tags=["ok"], **{field: value})
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES, "tags"), before)
|
|
self.assertEqual(self.index_revision(), 0)
|
|
|
|
def test_writers_supply_uuid_ids_and_refuse_to_write_without_one(self):
|
|
loc = self.locator("a.png")
|
|
before = self.rows(*ANNOTATION_TABLES)
|
|
with patch.object(annotations_module, "uuid4", return_value=""):
|
|
with self.assertRaises(ValueError):
|
|
self.replace(loc, tags=["a"])
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES), before)
|
|
self.replace(loc, tags=["a"])
|
|
self.add(self.locator("b.png"), DIGEST_B, ["b"])
|
|
conn = self.db.model.get_connection()
|
|
for table in ("media_annotation_files", "media_annotation_bytes"):
|
|
for (value,) in conn.execute(f"SELECT id FROM {table}"):
|
|
self.assertRegex(value, UUID)
|
|
self.schema_clean()
|
|
|
|
def test_never_resolves_owner_through_prompt_lookup(self):
|
|
loc = self.locator("a.png")
|
|
prompt = self.db.save_prompt("Linked", tags=["prompt-tag"])
|
|
self.db.link_image_to_prompt(prompt, loc)
|
|
with patch.object(
|
|
PromptDatabase,
|
|
"get_prompt_id_for_image",
|
|
side_effect=AssertionError("prompt lookup is not an owner"),
|
|
):
|
|
self.replace(loc, tags=["img"])
|
|
self.add(loc, tags=["more"])
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_A)["tags"], ["img", "more"]
|
|
)
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME)}),
|
|
{loc: ["img", "more"]},
|
|
)
|
|
self.assertTrue(
|
|
self.db.refresh_image_fingerprint(loc, DIGEST_A, SIZE, MTIME + 1)
|
|
)
|
|
list(self.db.iter_image_tag_records())
|
|
self.assertEqual(self.db.get_prompt_by_id(prompt)["tags"], ["prompt-tag"])
|
|
|
|
|
|
class AddImageTagsTests(AnnotationTestCase):
|
|
def test_add_merges_without_removing_and_caps_at_100(self):
|
|
loc = self.locator("a.png")
|
|
self.replace(loc, tags=["Forest", "mist"])
|
|
receipt = self.add(loc, tags=["forest", "Rain", "rain"])
|
|
self.assertEqual(
|
|
receipt,
|
|
{
|
|
"library_epoch": self.epoch,
|
|
"sha256": DIGEST_A,
|
|
"tags": ["Forest", "mist", "Rain"],
|
|
"tag_revision": 2,
|
|
"replayed": False,
|
|
"added": 1,
|
|
},
|
|
)
|
|
nothing = self.add(loc, tags=["MIST"])
|
|
self.assertEqual(
|
|
(nothing["tags"], nothing["tag_revision"], nothing["replayed"]),
|
|
(["Forest", "mist", "Rain"], 2, True),
|
|
)
|
|
self.assertEqual(nothing["added"], 0)
|
|
before = self.rows(*ANNOTATION_TABLES, "tags")
|
|
with self.assertRaises(AnnotationCommandError) as ctx:
|
|
self.add(loc, tags=[f"t{i}" for i in range(98)])
|
|
self.assertEqual(ctx.exception.code, "tag-limit")
|
|
self.assertEqual(ctx.exception.current["tag_revision"], 2)
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES, "tags"), before)
|
|
exact = self.add(loc, tags=[f"t{i}" for i in range(97)])
|
|
self.assertEqual((len(exact["tags"]), exact["added"]), (100, 97))
|
|
# An unseen file: add creates the owner and byte record like a save.
|
|
fresh = self.add(self.locator("b.png"), DIGEST_B, ["one"])
|
|
self.assertEqual(
|
|
(fresh["tags"], fresh["tag_revision"], fresh["added"]), (["one"], 1, 1)
|
|
)
|
|
self.schema_clean()
|
|
|
|
|
|
class CurrentImageTagsTests(AnnotationTestCase):
|
|
def test_current_tags_require_matching_fingerprint(self):
|
|
loc = self.locator("a.png")
|
|
other = self.locator("b.png")
|
|
self.replace(loc, tags=["a"])
|
|
self.replace(other, DIGEST_B, [], mtime_ns=MTIME + 9)
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME), other: (SIZE, MTIME + 9)}),
|
|
{loc: ["a"], other: []},
|
|
)
|
|
self.assertEqual(self.db.current_image_tags({loc: (SIZE, MTIME + 1)}), {})
|
|
self.assertEqual(self.db.current_image_tags({loc: (SIZE + 1, MTIME)}), {})
|
|
self.assertEqual(self.db.current_image_tags({}), {})
|
|
self.assertEqual(
|
|
self.db.current_image_tags({self.locator("none.png"): (SIZE, MTIME)}), {}
|
|
)
|
|
# Refreshing the verified fingerprint makes the record current again.
|
|
self.assertTrue(
|
|
self.db.refresh_image_fingerprint(loc, DIGEST_A, SIZE, MTIME + 1)
|
|
)
|
|
self.assertFalse(self.db.refresh_image_fingerprint(loc, DIGEST_B, SIZE, MTIME))
|
|
self.assertFalse(
|
|
self.db.refresh_image_fingerprint(
|
|
self.locator("x.png"), DIGEST_A, SIZE, MTIME
|
|
)
|
|
)
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME + 1)}), {loc: ["a"]}
|
|
)
|
|
self.assertEqual(self.db.current_image_tags({loc: (SIZE, MTIME)}), {})
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_A),
|
|
{"tags": ["a"], "tag_revision": 1, "prior_count": 0},
|
|
)
|
|
for bad in (None, [], {loc: (SIZE,)}, {loc: (SIZE, 1.5)}, {3: (SIZE, MTIME)}):
|
|
with self.subTest(bad=bad), self.assertRaises(ValueError):
|
|
self.db.current_image_tags(bad)
|
|
|
|
def test_newest_verification_wins_on_equal_fingerprints(self):
|
|
loc = self.locator("a.png")
|
|
stamps = iter(f"2026-10-22T00:00:0{i}.000000+00:00" for i in range(10))
|
|
with patch.object(annotations_module, "utc_now_iso", side_effect=stamps):
|
|
self.replace(loc, DIGEST_A, ["old bytes"])
|
|
self.replace(loc, DIGEST_B, ["new bytes"]) # same size and mtime
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME)}), {loc: ["new bytes"]}
|
|
)
|
|
with patch.object(
|
|
annotations_module,
|
|
"utc_now_iso",
|
|
return_value="2026-10-22T00:01:00.000000+00:00",
|
|
):
|
|
self.assertTrue(
|
|
self.db.refresh_image_fingerprint(loc, DIGEST_A, SIZE, MTIME)
|
|
)
|
|
self.assertEqual(
|
|
self.db.current_image_tags({loc: (SIZE, MTIME)}), {loc: ["old bytes"]}
|
|
)
|
|
|
|
def test_lookup_is_chunked_and_records_stream_in_position_order(self):
|
|
locators = [self.locator(f"{i:04}.png") for i in range(503)]
|
|
self.replace(locators[0], tags=["z", "y"])
|
|
self.replace(locators[502], DIGEST_B, ["x"], mtime_ns=MTIME + 2)
|
|
self.replace(locators[250], DIGEST_B, [], mtime_ns=MTIME + 1)
|
|
statements = []
|
|
conn = self.db.model.get_connection()
|
|
conn.set_trace_callback(statements.append)
|
|
try:
|
|
current = self.db.current_image_tags(
|
|
{
|
|
loc: (SIZE, MTIME + (2 if i == 502 else 0))
|
|
for i, loc in enumerate(locators)
|
|
}
|
|
)
|
|
finally:
|
|
conn.set_trace_callback(None)
|
|
self.assertEqual(current, {locators[0]: ["z", "y"], locators[502]: ["x"]})
|
|
lookups = [s for s in statements if "media_annotation_files" in s]
|
|
self.assertEqual(len(lookups), 2)
|
|
self.assertEqual(
|
|
list(self.db.iter_image_tag_records()),
|
|
[
|
|
(locators[0], SIZE, MTIME, ["z", "y"]),
|
|
(locators[250], SIZE, MTIME + 1, []),
|
|
(locators[502], SIZE, MTIME + 2, ["x"]),
|
|
],
|
|
)
|
|
|
|
|
|
class AnnotationIsolationTests(AnnotationTestCase):
|
|
def test_cascade_from_vocabulary_delete_keeps_byte_records(self):
|
|
loc = self.locator("a.png")
|
|
self.replace(loc, tags=["gone", "kept"])
|
|
self.db.delete_tag_all_prompts("gone")
|
|
self.assertEqual(
|
|
self.db.read_image_tags(loc, DIGEST_A),
|
|
{
|
|
"tags": ["kept"],
|
|
"tag_revision": 2,
|
|
"prior_count": 0,
|
|
}, # moved by the delete
|
|
)
|
|
self.schema_clean()
|
|
|
|
def test_junction_failure_leaves_nothing_behind(self):
|
|
loc = self.locator("a.png")
|
|
self.replace(loc, tags=["a"])
|
|
conn = self.db.model.get_connection()
|
|
conn.execute(
|
|
"CREATE TRIGGER reject_image_tag BEFORE INSERT ON media_image_tags "
|
|
"BEGIN SELECT RAISE(ABORT,'image tag failure'); END"
|
|
)
|
|
conn.commit()
|
|
before = self.rows(*ANNOTATION_TABLES, "tags")
|
|
with self.assertRaises(sqlite3.IntegrityError):
|
|
self.replace(loc, tags=["b"], revision=1)
|
|
with self.assertRaises(sqlite3.IntegrityError):
|
|
self.add(self.locator("new.png"), DIGEST_B, ["c"])
|
|
self.assertFalse(conn.in_transaction)
|
|
self.assertEqual(self.rows(*ANNOTATION_TABLES, "tags"), before)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|