1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Add attachments (protocol 4), their edge-case tests, and handoff docs

- One optional attachment per message, any size up to the server's
  MaxAttachmentSize (default 1G), sent and stored in 1 MB parts, checked
  with its SHA-256. Uploaded before its message (UploadStart, UploadChunk),
  then attached by the PushMessage in the same transaction; uploads never
  pushed expire after UploadTimeout. Read back a part at a time
  (GetAttachment).
- Replicated separately, after the messages: the message carries the size
  and SHA-256, then each check-in lists what the peer is missing
  (MissingAttachments) and sends the parts from where it stopped. Over the
  receiver's limit, the attachment is marked not kept.
- Deleted with its message by the retention, parts cleaned up in the
  background. Schema v7 (attachments, blobs, blob_chunks), replacing the
  unused attachements table.
- DLL: PushMessageWithFile/WithData, SaveAttachment, LoadAttachment,
  GetMessageAttachmentSize, IsMessageAttachmentReady. Python: push(...,
  attachment=), get_attachment(), save_attachment(). Tools: PushMessage -a,
  GetAttachment. The release smoke test round-trips one.
- Tests/Attachments-Test.ps1 (39 checks) and Tests/AttachmentEdges-Test.ps1
  (51 checks, with a raw protocol client): wrong SHA-256, bad parts,
  others' uploads, expiry, replication resuming after a kill, a corrupted
  stored copy. Found: a corrupted copy is re-sent to peers forever, noted
  as open item 3 in Notes.md with a proposed fix.
- CLAUDE.md: how to build, test, the traps and working rules, for the next
  session. Notes.md and the Design docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVPXGLs3KK1MAQn3ouP
This commit is contained in:
2026-11-08 18:56:15 +01:00
parent 9caf816dca
commit df61ef9719
42 changed files with 4920 additions and 106 deletions
+41
View File
@@ -6,7 +6,10 @@ Usage: python check_bakashrot.py <DllPath> <KitchenIni> <LoungeIni>
Expects an empty database. Prints PASS/FAIL per check, exits 1 if any failed.
"""
import hashlib
import os
import sys
import tempfile
import threading
import time
@@ -159,6 +162,44 @@ with kitchen:
not thread.is_alive() and not errors and [m.id for m in followed] == new_ids,
f"{[m.id for m in followed]} vs {new_ids}, errors {errors}")
# --- Attachments (in secret, the other checks count logs) -------------------
blob = os.urandom(3 * 1024 * 1024 + 7)
started = time.perf_counter()
blob_id = kitchen.push({"kind": "blob"}, topic="secret", attachment=blob)
up = time.perf_counter() - started
message = kitchen.latest("secret", "kitchen")
check("push(attachment=bytes): the message has it, ready",
message is not None and message.id == blob_id and message.attachment_size == len(blob) and message.attachment_ready,
message)
started = time.perf_counter()
data = kitchen.get_attachment("secret", "kitchen", blob_id)
down = time.perf_counter() - started
check("get_attachment(): the same bytes", data == blob, f"{len(data)} bytes, {up * 1000:.0f} ms up, {down * 1000:.0f} ms down")
folder = tempfile.mkdtemp()
source = os.path.join(folder, "source.bin")
with open(source, "wb") as file:
file.write(os.urandom(1500 * 1024))
file_id = kitchen.push("from a file", topic="secret", attachment=source)
copy = os.path.join(folder, "copy.bin")
size = kitchen.save_attachment("secret", "kitchen", file_id, copy)
with open(source, "rb") as a, open(copy, "rb") as b:
same = hashlib.sha256(a.read()).digest() == hashlib.sha256(b.read()).digest()
check("push(attachment=path), save_attachment(): the same file", size == os.path.getsize(source) and same, size)
plain_id = kitchen.push("no attachment", topic="secret")
page = kitchen.get_messages("secret", "kitchen", after=blob_id - 1)
sizes = [(m.id, m.attachment_size, m.attachment_ready) for m in page]
check("get_messages(): each message's attachment",
sizes == [(blob_id, len(blob), True), (file_id, os.path.getsize(source), True), (plain_id, 0, False)], sizes)
ok, detail = raises(lambda: kitchen.get_attachment("secret", "kitchen", plain_id), RefusedError, "No attachment")
check("get_attachment() of a message without: RefusedError", ok, detail)
ok, detail = raises(lambda: kitchen.push("x", topic="secret", attachment=b""), BakaShrotError, "empty")
check("An empty attachment: refused before sending", ok, detail)
ok, detail = raises(lambda: kitchen.push("x", topic="secret", attachment=os.path.join(folder, "missing.bin")), BakaShrotError, "no such file")
check("A missing file: refused before sending", ok, detail)
check("Still connected", kitchen.connected)
check("close(): disconnected", not kitchen.connected)
ok, detail = raises(lambda: kitchen.push("x"), ConnectionLostError)
check("A request once closed: ConnectionLostError", ok, detail)