Add versioned script library, reconcile permit refusals and compact helper captures

This commit is contained in:
2026-11-17 15:45:56 +01:00
parent a576c10e8a
commit 8e2fef7007
29 changed files with 1225 additions and 50 deletions
@@ -0,0 +1,25 @@
-- Keep the serialized snapshot visible to existing readers and repair tools.
-- Splitting the original bytes preserves signatures, including unknown fields.
ALTER TABLE helper_sync_captures RENAME TO helper_capture_headers;
ALTER TABLE helper_capture_headers ADD COLUMN tail_id INTEGER REFERENCES helper_capture_tails(id);
CREATE TABLE helper_capture_tails(id INTEGER PRIMARY KEY, tail BLOB NOT NULL UNIQUE);
CREATE VIEW helper_sync_captures AS
SELECT h.checkpoint,
CASE WHEN h.tail_id IS NULL THEN h.snapshot
ELSE CAST(h.snapshot || (SELECT tail FROM helper_capture_tails WHERE id=h.tail_id) AS BLOB)
END AS snapshot, h.fingerprint, h.captured_at
FROM helper_capture_headers h;
CREATE TRIGGER insert_helper_capture INSTEAD OF INSERT ON helper_sync_captures BEGIN
INSERT INTO helper_capture_tails(tail)
SELECT substr(NEW.snapshot,instr(NEW.snapshot,CAST(',"attempts":' AS BLOB)))
WHERE instr(NEW.snapshot,CAST(',"attempts":' AS BLOB))>0
ON CONFLICT(tail) DO NOTHING;
INSERT INTO helper_capture_headers(checkpoint,snapshot,fingerprint,captured_at,tail_id)
SELECT NEW.checkpoint,CASE WHEN split=0 THEN NEW.snapshot ELSE substr(NEW.snapshot,1,split-1) END,
NEW.fingerprint,NEW.captured_at,
CASE WHEN split>0 THEN (SELECT id FROM helper_capture_tails WHERE tail=substr(NEW.snapshot,split)) END
FROM (SELECT instr(NEW.snapshot,CAST(',"attempts":' AS BLOB)) AS split);
END;
CREATE TRIGGER delete_helper_capture INSTEAD OF DELETE ON helper_sync_captures BEGIN
DELETE FROM helper_capture_headers WHERE checkpoint=OLD.checkpoint;
END;
+14
View File
@@ -1038,6 +1038,20 @@ fn initialize(db: &Connection, binding: &Binding) -> Result<()> {
CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?;
let legacy_captures: bool = db.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='helper_sync_captures')",
[], |row| row.get(0),
)?;
if legacy_captures {
// initialize runs under the snapshot transaction; savepoint also makes
// the schema change atomic for callers that initialize independently.
db.execute_batch("SAVEPOINT helper_capture_schema")?;
if let Err(error) = db.execute_batch(include_str!("helper_capture_storage.sql")) {
db.execute_batch("ROLLBACK TO helper_capture_schema; RELEASE helper_capture_schema")?;
return Err(error.into());
}
db.execute_batch("RELEASE helper_capture_schema")?;
}
let binding_hash = hash(&serde_json::to_vec(binding)?);
db.execute(
"INSERT OR IGNORE INTO helper_sync_meta(id,journal_id,binding_sha256) VALUES(1,?1,?2)",
+24
View File
@@ -260,6 +260,30 @@ pub fn recover_interrupted_outcome_with(
Ok(Some(saved))
}
/// A refusal carries no execution authority. An unbound or ambiguous refusal
/// remains local uncertainty until the signed reconciliation resolves it.
pub fn denied_request(db: &Connection, offer: &JobOffer, permit: &ExecutionPermit) -> Result<()> {
ensure!(
!permit.denial_code.is_empty() && permit.payload_bytes.is_empty() && permit.signature.is_empty(),
"INVALID_PERMIT_DENIAL"
);
let Some(request) = &permit.unreserved_request else {
return interrupted_request(db, offer);
};
let job: JobPayload = serde_json::from_slice(&offer.payload_bytes)?;
ensure!(
request.job_id == offer.job_id && request.attempt_id == offer.attempt_id
&& request.job_payload_hash == offer.job_payload_hash
&& request.assignment_version == job.assignment_version,
"PERMIT_DENIAL_BINDING"
);
ensure!(db.execute(
"UPDATE job_attempts SET state='accepted' WHERE job=?1 AND attempt=?2 AND state='requesting' AND grant_id=''",
params![offer.job_id, offer.attempt_id],
)? == 1, "PERMIT_DENIAL_STATE");
Ok(())
}
pub fn interrupted_request(db: &Connection, offer: &JobOffer) -> Result<()> {
ensure!(
db.execute(