: asapien/signet

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
# PIM resync safety repair — tracker19 (2026-11-14)

Implemented and verified in isolated source/stacks, not installed on preserved
:18087. Do NOT invoke Resync in that existing preview until it is upgraded:
its old PIM binary still uses FLUSHDB. No production/shared service changed.

## Root cause and change

`h_resync()` used FLUSHDB before rebuilding PIM hashes. An authenticated PIM
administrator could silently erase waiting/processing sign/enrol jobs and every
unrelated Redis key in that database. A disposable red regression fails with
`FAIL - resync deleted unrelated Redis key` against the old verified image.

The replacement uses existing PostgreSQL/Redis capabilities, no new dependency:

- PostgreSQL constructs the canonical attribute array.
- A Redis Lua script validates all input/registry names/types before mutation,
  atomically reconciles only `<organisation>:metadata`/`:images` for organisations
  present in the canonical table or existing `pim:orgs` registry, and records
  those organisations. It does not scan/delete arbitrary similarly named keys.
- Upserts atomically remove the old key from both type hashes before publishing
  the canonical current type. Empty value deletes both types, matching the
  database's org/key deletion semantics. Every write tracks its organisation.
- A component-level PostgreSQL session advisory lock orders committed SQL updates
  and their Redis mirror publication against resync across PIM workers. Keeping
  it across SQL commit prevents an older mirror write overwriting a newer resync.
  Every acquired lock has an explicit unlock attempt on success/error response
  paths; the fixture verifies release after cache errors.
- Missing/invalid Redis replies produce503, including an empty canonical table;
  NULL replies drop the cached Redis connection. A committed SQL change with a
  failed cache write retains the existing 'saved, Redis down; resync later'
  contract. Cache reconstruction is retryable, not a distributed transaction.

Redis scripts prevent interleaved partial publication; they do NOT provide
rollback after arbitrary runtime/OOM failure or atomic commitment with PostgreSQL.
Direct out-of-band SQL writers must coordinate/quiesce or use the same lock;
normal PIM API writers participate. Native lifecycle/limits remain unverified.

## Namespace and migration boundary

The PIM-owned namespace remains `pim:orgs` plus known organisation hashes. This
must be reserved separately from configured signing queues and other services
before production. No allocation/ACL policy is invented here. A hash whose
organisation has neither a canonical row nor a registry entry is NOT safe to
infer as PIM-owned and delete. Historical manually orphaned/unregistered keys
need an owner-reviewed inventory/reconciliation, not a broad SCAN/DEL fallback.
Catalog size, Redis script/shared-service resource limits, and native containment
remain open with ticket14/20; this is not production acceptance.

## Retained evidence

`test/pim-contract.sh` runs in full E2E, only inside a disposable dev stack. It
pauses its worker, plants waiting/processing markers and unrelated keys, and
checks exact list contents/order, foreign values/TTL, and an unowned `:metadata`
hash. It verifies:

- Resync preserves signing/enrol waiting/processing and foreign Redis state.
- Text-to-image changes and deletion with a differing supplied type leave no
  stale opposite-type field; stale attributes/types are reconciled from SQL;
  historical empty-value rows retain their uncached/deleted semantics.
- A previously managed organisation with no SQL rows is cleaned.
- Wrong-type/invalid-name registry returns503 before cache mutation; failed upsert still
  commits its SQL value and reports503, then resync recovers it.
- Two real PIM workers wait on an independently owned mirror lock. No SQL write
  slips past it; after release, concurrent upsert/resync succeed and keep the
  newest canonical value, with no advisory locks left behind. The fixture
  restores the one-worker PIM pool and resumes the worker on exit.

Logs `/tmp/signet-pim-{red,green,build,e2e,render}.log`; image identity
`/tmp/signet-pim-image.txt`. Fresh clean Alpine build has no warning/error matches;
full E2E (identity, snapshots, signatures, metadata and PIM) and render/hang/replay
contracts pass. Renderer/JS geometry are unchanged. Redis failures call `log_err`, but actual
both-API/dev log capture needs follow-up22; current runtime stdout did not expose
those fixture diagnostics, so no logging-transport pass is claimed. API/worker/UI on preserved
:18087 were NOT replaced during this work. Source is uncommitted integration.