gitoria
All repositories: gitoria
9.6 KB
# W43 — signed events to connected apps (gitoria#43)Branch `w43-events` in `/media/STORAGE/claude-scratch/gitoria/W43/wt`, based on main 1997e77 and merged with main 5a996ea (#42) as asked. Last commit cd44206. Not pushed, not deployed, no tickets written.## What was built- `lib/events.hl`, `lib/events-helpers.hl` (new): `raiseEvent(repo, event, action, extra, senderId)`, `raisePing`, `syncAndRaise(slug, userId)`, `sendDue()`, `redeliver(appId, deliveryId)`, `deliveryPage(appId, cursor, limit)`.- `deliveries.db`: {id (uuid), app, repo, event, action, body, tries, step, nextAt, status pending|delivered|failed, lastCode, created, updated, claimedBy, claimedUntil}.- `tries` counts attempts and is never reset. `step` is the position in the retry schedule.- `body` is built once at raise time and stored as the exact bytes that are signed and sent, so a retry or redelivery sends the same bytes and the same uuid.- `lib/apps-table.hl` (new): `appsTable`, `appsOfRepo` and `appWith` moved out of `lib/apps.hl`. `lib/events.hl` needs to read the apps while `apps.hl` raises the ping, and a circular import was the alternative. Behaviour is unchanged. `lib/app-auth.hl` now imports from the new file.- Wiring:- `syncAndRaise` replaces `syncPulls` in the three places a push or merge lands: `lib/transport.hl` (HTTPS push), `lib/api.hl` gitPushed `after` (ssh push), and the Merge button in `components/pulls.hl`.- It raises `pull_request` opened / reopened / synchronize{before, after, forced} / closed / merged{mergeCommit}, with `pull` = `pullObject`.- `pingAfterConnect` raises the ping for both ways A and B.- Settings → Apps has a "Send ping" button per app (`sendPing` in apps.hl, face `gitoriaAppPing`, owner only).- Sender: `sender = every(tickSeconds())` and `on sender.tick() { sendDue() }` in `project.hl`.- Headers, HMAC and `fetch` are as the brief says: `hkdfExtract(toBytes(secret), toBytes(body)).hex()` is the HMAC, timeout 10 s, `maxRedirects` 0, 2xx = delivered.- Retry waits are 1 min, 5 min, 30 min, 2 h, 6 h, then `failed`. `urlAllowed` is checked again at send time.- The app's `lastDelivery` / `lastStatus` are updated, e.g. "pull_request delivered (204)". `lastStatus` is not live-updated on the open Settings page: it shows after a reload.- Routes from #42's helpers: `GET {api}/deliveries?cursor=&limit=` and `POST {api}/deliveries/:id/redeliver` (`appDeliveries`, `appRedeliver` in `lib/api.hl`; `apiPatterns` for all three address forms).- An app sees only its own rows. Another app's id gives 404. The cursor is "created_id" of the last row, so rows raised meanwhile do not shift the next page.- Test-only env: `GITORIA_EVENTS_TICK` and `GITORIA_EVENTS_RETRY_MS`. README has a section; `tests/events.mjs` is in GATES in deploy.sh.## Claim: why only ONE process sends a rowThe table has no compare-and-set, and an mpackdb handle does not see another process's writes (README "Another process's appends and deletes are not seen"). So the claim is the file system's: `mkdir storage/claims/<delivery>.<tries+1>` via `run(['mkdir', …])`. A POSIX mkdir without `-p` fails atomically when the folder exists, across processes.- Only the process whose mkdir succeeds sends that attempt. The next attempt has a new number, so old folders never block later ones.- `claimedBy` / `claimedUntil` and `tries` are written to the row BEFORE the POST. The other process skips a row whose `claimedUntil` is in the future. The gate found this gap: a second process claimed attempt number 2 under an in-flight attempt 1, and the receiver got a duplicate. It is fixed and the two-process check is green.- A claimed attempt whose process died (`claimedUntil` + 30 s passed, row unchanged) is closed by a second folder `<delivery>.<n>.dead` (again one winner) and counted as failed.- Retention (every minute, in the sender): rows older than 7 days go, then at most 200 per app (oldest first). Claim folders of deleted rows are removed.## Tests`LC_ALL=C node tests/events.mjs`: **48 passed, 0 failed**, green in the last 2 runs (the earlier runs found the real bugs above). Checks:- Signature: RFC 4231 case 2 gives `5bdcc146…3843`. Cross-check against `openssl dgst -sha256 -hmac` with a 64-hex key and a UTF-8 body matches. Delivery id is a uuid v4.- Events and headers: ping at connect (way B) only to the app of that repo, with headers, signature verified by the receiver, and the common body. opened (umlaut title, tickets, pull object, `refs/pull/1/head` = `pull.head` at delivery), synchronize not forced, synchronize force-push (`forced` true, before/after), merged by the Merge button (face `gitoriaMergePull` as the logged-in owner, `mergeCommit` = new tip of main, sender), closed by deleting the branch, "Send ping" button (non-owner and signed-out refused), no delivery to the other repo's app.- Retries and failure: receiver cutting the connection twice gives 3 tries with the same delivery id and the same bytes, waits of at least 1.3 s. Answering 500 gives 4 tries and then `failed (500)` in Settings. A 302 is a failure and is not followed.- Speed: PR pushes with a hanging receiver (open and synchronize) stay within base + 4 s, and `/api/repos` answers while a delivery hangs.- Two gitoria processes on one storage: 6 events give 6 deliveries, none twice, and claim folders exist.- `/deliveries` paging, `/deliveries/:id/redeliver` (same uuid and same bytes, log shows delivered afterwards), and 401 / 404 / 405 cases.- Retention: old rows gone, exactly 200 kept per app.Other gates (LC_ALL=C), all 0 failed:- `check-theme` 69 files, 0 problems- `signin-return` 3- `security` 82- `private` 158- `ident-access-gitoria` 30- `apps` 95- `pull-numbers` 48- `pull-ssh` 20- `app-token` 42, after fixing `tests/app-auth-unit.hl`'s import of `appsTable`- `events` 48- `browser` 266, run on ports 8729 / 8728 / 8727 with Chrome ports 8720-8726 (`GITORIA_GATE_*` env).- The default port 8700 was held by another process (pid 3917192), and 8722 is held by someone throughout.- With only 5 Chrome ports (8725-8729) the gate crashed twice with "no bindable port" after 184 and 220 passes. That is port scarcity of this slot, not a code failure. It needs ≥6 usable Chrome ports.## Contract pointsCovered: §4 headers and signature, delivery rules, retries, no redirects, common body, pull_request events, ping at connect and via the button, `GET /deliveries`, `POST /redeliver`.Not covered:- `exceptApp` (no event for the app's own action) is implemented in `raiseEvent` but has no gate check, because no caller exists until #44/#45.- `review_*`, `pull_comment` and `ready` events are for #44/#45.## DOCS/GAP- hl:fs has no exclusive create or compare-and-set, and mpackdb has no cross-process visibility. Worked around with the OS `mkdir` through `hl:proc` `run`, no JavaScript. A doc line on cross-process claims would help.- `listDir` returns an iterator of {name, path, type…} (not an array of names). Its signature is undocumented in the README table.- Hybriel: two imports of the same name from different modules clash in the browser build: "'withChange' is already imported" (I renamed mine to `appWith`). Repro: import `withChange` from `repos-helpers.hl` and from another file in one component tree.- A fetch that fails is absorbed by `on Error` in project.hl and logged as "error absorbed: hl:fetch POST …" in the server log. That is the documented behaviour, but it is noisy for normal retries.## Open risks- Stale cross-process records: `appWith` (lastDelivery / lastStatus) re-reads the app row just before the write, but another process's `update()` of the same app (rotate or re-connect) in that instant could be overwritten by a stale copy. The window is small, and the standby only serves during a deploy.- The sender is one serial timer: a hanging receiver holds the ticks for up to 10 s, so deliveries to other apps wait behind it. Pushes and pages are not affected (tested). Fix later: a worker per app.- Browser proof of the new "Send ping" button in his Chrome on lisbeth :9223 (the rule for UI changes) was NOT done: no tunnel / lisbeth Chrome from this worker. The gate covers the button in the served HTML, the face, and the delivery. A single quiet button next to the existing ones, no layout change. **Needs a look in his Chrome at 390 / 800 / 1280.** No tab of mine was opened anywhere.- The ssh path (`/__git/pushed after` → `syncAndRaise`) is covered by `pull-ssh.mjs` (green) but not by an event check over ssh.## ProcessesEverything I started was stopped by the gates themselves (my last check found nothing of mine listening on 8720-8729; 8722 is someone else's). Test storage stays under `wt/.scratch/` (ignored by git).## Addendum (review fix): the sender never writes the apps row- `noteOnApp` and the `appsTable.update` in `lib/events.hl` are gone, so the sender only writes `deliveries.db`. `appWith` is no longer imported there.- `events.hl lastDeliveryOf(appId)` returns the newest delivery (by `updated`) as text and time. `apps.hl statusText` uses it for Settings → Apps. The `lastDelivery` / `lastStatus` fields of the apps row are now unused and are never written by the sender.- Settings now shows a freshly connected app's ping as "ping is waiting" or "will be tried again" instead of "no delivery yet". I changed one assertion in `tests/apps.mjs` (line 165) accordingly.- `tests/events.mjs` has new checks: rotate the secret, send two deliveries, and the receiver verifies both with the NEW secret (and not the old one). Settings still shows "ping delivered (204)" and no secret.- Rerun with LC_ALL=C: events 52/0, apps 95/0, security 82/0, app-token 42/0, check-theme 0 problems. Not rerun after this change: browser, private, signin-return, ident-access-gitoria, pull-numbers, pull-ssh (none touch events or the apps status text).
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 26117255|||PR apps API: review comments on files with spaces in their namemre
- b8796cecMerge branch 'f1-broken-json' into mainGitoria
- 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
- f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
- 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
- 16a3530fMerge branch 'heap-revert' into mainGitoria
- 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
- 13f0c342Merge branch 'hl25-records' into mainGitoria
- a002ecb7|||PR records: HL25 report, heap helper, logmre
- 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
- fefaedd2log: tracker PR-only, HL25 startedmre
- 21aa57calog: parkmre
- e9f950a8reports: G25c soakmre
- a7783686log: deploy 9f41ffcmre
- 9f41ffc1log: W30b mergedmre
- bef635c1Merge branch 'w30b'mre
- 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
- c4f6d9e3reports: G25c growth huntmre
- b88e39f1log: privacy cleanup donemre
- bfd7826ddocs: neutral example organisation namemre