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
+59
View File
@@ -34,6 +34,8 @@
; ============================================================================
XIncludeFile "../../Commons/Networking/Names.pbi"
; #BakaShrot_MaxAttachmentSize.
XIncludeFile "../../Commons/Networking/Packets.pbi"
; Some topics or nodes, or all of them: what a topic list (or node list)
@@ -228,6 +230,16 @@ Structure BakaShrot_ServerConfig
; max age ([BakaShrot.Retention]). Default 60.
RetentionInterval.i
; Biggest attachment this server takes, in bytes: pushed with a message,
; or replicated (a bigger one's message is kept, not its attachment).
; Written with a unit: 500M, 1G (K M G T, 1024-based). Default 1G.
MaxAttachmentSize.q
MaxAttachmentSizeText.s
; Seconds an upload may take, before it's pushed with its message: past
; that it's deleted. Default 3600.
UploadTimeout.i
EndStructure
@@ -283,6 +295,8 @@ Macro Ptr_BakaShrot_CoreConfig : i : EndMacro
#BakaShrot_Config_DefaultPeerSyncInterval = 5
#BakaShrot_Config_DefaultPeerBatchSize = 100
#BakaShrot_Config_DefaultRetentionInterval = 60
#BakaShrot_Config_DefaultMaxAttachmentSize = "1G"
#BakaShrot_Config_DefaultUploadTimeout = 3600
; What ReadPreferenceString() returns for a missing key, to tell it from an
; empty one: an empty PushTopics allows nothing, a missing one the default.
@@ -422,6 +436,38 @@ Procedure.q ParseRetentionAge(Text.s)
EndProcedure
; A size as MaxAttachmentSize writes it ("500M", "1G", "1048576") in bytes,
; -1 if it isn't one. Units are 1024-based: K M G T.
Procedure.q ParseConfigSize(Text.s)
Protected Unit.q = 1, Digits.s, Last.s, i.i
Text = UCase(Trim(Text))
If Text = ""
ProcedureReturn -1
EndIf
Last = Right(Text, 1)
Select Last
Case "K" : Unit = 1024
Case "M" : Unit = 1024 * 1024
Case "G" : Unit = 1024 * 1024 * 1024
Case "T" : Unit = 1024 * 1024 * 1024 * 1024
EndSelect
Digits = Text
If Unit > 1
Digits = Left(Text, Len(Text) - 1)
EndIf
If Digits = "" Or Len(Digits) > 13
ProcedureReturn -1
EndIf
For i = 1 To Len(Digits)
If Mid(Digits, i, 1) < "0" Or Mid(Digits, i, 1) > "9"
ProcedureReturn -1
EndIf
Next
ProcedureReturn Val(Digits) * Unit
EndProcedure
; Reads [BakaShrot.Retention] into *Config\Retention().
; Returns #False on an invalid line, with the reason in BakaShrot_ConfigError.
Procedure.i ParseRetentionSection(*Config.BakaShrot_CoreConfig)
@@ -835,6 +881,17 @@ Procedure.Ptr_BakaShrot_CoreConfig LoadConfig(IniFilePath.s)
BakaShrot_ConfigError = "[BakaShrot.Server] RetentionInterval must be 1 second or more"
Goto LoadFailed
EndIf
*Config\Server\MaxAttachmentSizeText = Trim(ReadPreferenceString("MaxAttachmentSize", #BakaShrot_Config_DefaultMaxAttachmentSize))
*Config\Server\MaxAttachmentSize = ParseConfigSize(*Config\Server\MaxAttachmentSizeText)
If *Config\Server\MaxAttachmentSize < 0 Or *Config\Server\MaxAttachmentSize > #BakaShrot_MaxAttachmentSize
BakaShrot_ConfigError = "[BakaShrot.Server] MaxAttachmentSize = '" + *Config\Server\MaxAttachmentSizeText + "' isn't a size up to 1T: bytes, or a number with K, M, G or T, like 500M (0: no attachments)"
Goto LoadFailed
EndIf
*Config\Server\UploadTimeout = ReadPreferenceLong("UploadTimeout", #BakaShrot_Config_DefaultUploadTimeout)
If *Config\Server\UploadTimeout < 10
BakaShrot_ConfigError = "[BakaShrot.Server] UploadTimeout must be 10 seconds or more"
Goto LoadFailed
EndIf
*Config\Server\ReplicatingTopicsText = ReadConfigKey("ReplicatingTopics", "ReplicatingTopicIds", "")
If Not ParseTopicSet(*Config, *Config\Server\ReplicatingTopicsText, @*Config\Server\ReplicatingTopics, "[BakaShrot.Server] ReplicatingTopics")
Goto LoadFailed
@@ -997,6 +1054,8 @@ Procedure.i SaveConfig(IniFilePath.s, *Config.BakaShrot_CoreConfig)
WritePreferenceLong("PeerSyncInterval", *Config\Server\PeerSyncInterval)
WritePreferenceLong("PeerBatchSize", *Config\Server\PeerBatchSize)
WritePreferenceLong("RetentionInterval", *Config\Server\RetentionInterval)
WritePreferenceString("MaxAttachmentSize", *Config\Server\MaxAttachmentSizeText)
WritePreferenceLong("UploadTimeout", *Config\Server\UploadTimeout)
PreferenceGroup("BakaShrot.Topics")
ForEach *Config\TopicNames()
+10 -1
View File
@@ -102,9 +102,18 @@ PeerSyncInterval = 5
PeerBatchSize = 100
; Seconds between two passes deleting the messages past their topic's age
; ([BakaShrot.Retention] above). Only runs if a topic has one.
; ([BakaShrot.Retention] above), and the attachments' leftovers.
RetentionInterval = 60
; Biggest attachment this server takes (one per message, see
; Design/Attachments.md): pushed to it, or replicated (a bigger one's message
; is kept, its attachment isn't). Bytes, or K M G T (1024-based). 0: none.
MaxAttachmentSize = 1G
; Seconds a client may take to upload an attachment and push its message,
; after which the upload is deleted.
UploadTimeout = 3600
; One [BakaShrot.Peer.<name>] section per peer this node talks to.
; <name> is only a key for this file; it is never sent on the wire.