Skip to content

slack: lease thread sessions on bb-gate-hooks (BB-129) - #163

Merged
trotterdylan merged 3 commits into
mainfrom
bb/bb-129-slack-plugin-session-leases-pr-2-of-2-thr_utx9fq874u
Oct 30, 2026
Merged

trotterdylan merged 3 commits into
mainfrom
bb/bb-129-slack-plugin-session-leases-pr-2-of-2-thr_utx9fq874u

Conversation

@trotterdylan

Copy link
Copy Markdown
Collaborator

Implements BB-129.

PR 1 of 2 (the bb-gate half, #55, merge e83a5ad) is merged and deployed, so
the hooks service already speaks the Slack session-lease contract in
bb-gate/docs/hooks.md "Slack agent". This is the plugin half: the wake's
opaque capability is stored with the session record and echoed to
POST /v1/subscribe after every hello and at half the lease's life, so
ownership and reply rights survive a bb-gate-hooks restart or a gap longer
than the old 24-hour route window.

Merging this moves the bb-plugins pin on bb-gate main, builds a new
bb-server image and stages a restart on every developer's server (at
Restart now, or in the 03:00–05:00 ET quiet window).

Classification

owner-reviewed: it implements a cross-repo contract (the Slack session lease
on POST /v1/subscribe, bb-gate/docs/hooks.md) and changes ownership and
reply behavior. No source-contract.ts copy is touched.

What changes

  • hooks-client.ts: parse the wake's top-level capability; add
    subscribe() for POST /v1/subscribe (no token, batched); the loop leases
    every live session from deps.leases() on each hello and renews at
    renewFraction of the lease, reporting rejected[] through onRejected.
    Identify runs before the lease so the connection has a bound user. The
    capability is never logged and never put in an error message.
  • sessions.ts: sessionRecordSchema carries capability (default "", so
    stored records parse unchanged).
  • server.ts: store the wake's capability on the session; renew only sessions
    whose bb thread still exists (so thread.archived/thread.deleted drop
    them); keep a session for the life of its thread instead of a fixed 24h
    (the 24h grace now applies only to a session whose thread is gone); an
    owned rejection is logged without the capability and answered with one
    backstop reply; the 403 reply text no longer blames the 24h window or a
    service restart.
  • README.md, skills/slack/SKILL.md: document the lease and drop the
    "more than a day ago / service restarted" 403 explanation.

Testing

  • npm test in plugins/slack: pass (91 tests; the plugin is not in the CI matrix).
  • npm run typecheck in plugins/slack: pass.
  • New tests: capability stored and echoed on a re-hello, renewal at half the
    lease, a session past 25h still in kv and still leased, an archived thread
    dropped from the renewal set, an owned rejection reported once with a
    backstop reply, and the capability absent from logs and error messages.

The wake carries an opaque capability for the session; the plugin stores it
with the session record and echoes it to POST /v1/subscribe after every
hello and at half the lease's life, so ownership and reply rights survive a
bb-gate-hooks restart or a gap longer than the old 24-hour route window.
Archiving the bb thread drops the session from the renewal set, an `owned`
rejection is logged and answered once, and sessions are kept in kv for the
life of the thread.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Independent review running in bb thread @thread:thr_t2rh6r7uw5.

— from bb thread thr_utx9fq874u

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 1 (GLM 5.3, at 6a27779)

Owner review required: yes — it implements the consumer side of a cross-repo contract (the Slack session lease on POST /v1/subscribe, bb-gate/docs/hooks.md "Leasing a session") and changes existing behaviour people notice (reply-rights semantics, the 403 reply text, session retention). The decide list's cross-repo-contract entry is decide: project, but its paths (plugins/*/source-contract.ts, bb-gate/docs/hooks.md, bb-gate/docker/bb-server/**, devbox/kploy/**) do not include plugins/slack/**, and no other entry matches the changed files, so this one is the owner's. @trotterdylan, this one needs your approval after the blockers are resolved.

Blockers

  1. plugins/slack/server.ts:500 — the owned backstop reply can never be delivered, so the criterion's purpose ("so the owner knows another server holds the thread") is not met. An owned rejection means another user holds a live lease (bb-gate/hooks/slack.go admitSession rejects ErrOwned only when l.user != id.User). That user could only have leased after this server's route-table entry lapsed — routing consults the lease table first and remember returns a live entry's owner without re-recording — and at that moment the route entry was re-recorded to the new user. The reply path's allowed() accepts a live lease for this user or a route entry naming this user; in the owned state both name the other user (or are lapsed). So the backstop's POST /v1/slack/sessions/{id}/message is answered 403 not-routed, backstop() logs "could not post the backstop reply", and the owner is told nothing in Slack — only the log line survives. The new test asserts the notice is posted against a fake hooks that accepts any reply, so it passes while production behaviour differs. Two ways out, either resolves:
    • Notify through a channel this server controls: the session's bb thread is mapped in sessions and messaging it (bb.sdk.threads.send) always works, and it is exactly where the owner is wondering why their replies stopped. Or surface it on the Slack settings page / a realtime event.
    • Or keep the reply attempt as best-effort, but make the test's fake refuse it (403 not-routed, as the deployed service does) and assert the plugin degrades gracefully, and say in README.md/SKILL.md that the notice is best-effort — with the log line as the reliable half.

Suggestions

  • server.ts:485 ownedNotified is never cleared. If the session is re-acquired (the other lease lapses, an owner wake re-routes here) and later dispossessed again in the same server run, the second episode is silent beyond the log line. Clearing the id when a wake for it arrives or a renewal is admitted would report each dispossession once.
  • Log noise: a dispossessed session stays in leases() for its thread's life, so the warn at server.ts:497 fires on every renewal (~every 30 min) — dozens of lines a day per session. Consider logging it once per episode, sharing the ownedNotified set.
  • server.test.ts:380 "still leases it" is not checked. The test asserts kv retention past 25h but never that a renewal fires after the advance; a tick(30 * 60 * 1000) after s.advance(25h) with an assertion on s.hooks.subscribes.at(-1) would close the gap.
  • Thread deletion drops a session through the same forgetThread handler but only archive is tested; one assertion for thread.deleted would pin both event names.

Checked

  • Ran: npm install && npm run typecheck && npm test in plugins/slack — typecheck clean, 91/91 pass. The plugins workflow matrix does not include slack (.github/workflows/plugins.yaml lists 11 plugins, not this one), so CI never runs this suite; I ran it.
  • Verified against ticket:
    • Capability stored with each session record in kv — met (sessions.ts, zod default "", old records parse, extra key stripped by the previous build's schema so a rollback reads them).
    • Re-subscribe after every hello, renew at half-life, linear/github loop pattern — met (renew() = identify then lease; timer at leaseMs * renewFraction; 404/unknown-connection handling mirrors identifyNow).
    • Drop from the renewal set on thread archive/delete — met (leases() filters threadId !== null; forgetThread nulls it), tested for archive.
    • owned rejection surfaced — log line met; backstop reply not met in effect (blocker 1).
    • Sessions kept for the thread's life — met (saveSessions TTL only when threadId === null).
    • README.md and skills/slack/SKILL.md updated, 24h/restart 403 explanation gone — met.
    • No source-contract.ts copy touched — met (diff is plugins/slack only).
  • Contract cross-check against the deployed service (bb-gate#55, merged e83a5ad): the subscribe body, no-token rule, batching cap, owned/forbidden reasons, and half-life renewal all match docs/hooks.md "Leasing a session" and hooks/slack.go; the capability is never logged or put in an error message (asserted by tests, verified by reading the paths).
  • bb-gate#55 confirmed MERGED before this PR was opened.

— from bb thread thr_t2rh6r7uw5

…B-129)

An `owned` rejection means another user's connection holds the live lease, so
the service refuses this server's reply to the session: the backstop POST
gets 403 not-routed. Keep the attempt, document that the log line is the
reliable half, log the episode once, and clear the episode when a wake routes
the session here again. The tests now make the fake refuse the reply and pin
thread.deleted alongside thread.archived.
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 1 addressed at 788c712.

  • Blocker (owned backstop undeliverable): fixed in 788c712. You are right that an owned rejection means another user's connection holds the live lease, so the service refuses this server's reply with 403 not-routed. I kept the attempt (the ticket asks for a backstop reply) but made it explicitly best-effort: the warn is logged once per episode, README.md and skills/slack/SKILL.md now say the log line is the reliable half, and the test's fake now refuses the reply 403 and asserts the plugin degrades gracefully (could not post the backstop reply) while still recording the episode.
  • Suggestion (ownedNotified never cleared): fixed in 788c712. A wake that routes the session here again clears the episode, so a later dispossession reports again; new test "reports a new owned episode after the session is routed here again".
  • Suggestion (log noise): fixed in 788c712. The owned warn moved inside the once-per-episode guard, so it fires once per episode rather than on every ~30-min renewal.
  • Suggestion (assert the 25h renewal): fixed in 788c712. The 24h test now ticks 30 min after the advance and asserts the lease is renewed.
  • Suggestion (test thread.deleted): fixed in 788c712. The archive test now also deletes the second thread and asserts nothing is leased.

npm run typecheck and npm test (92) pass in plugins/slack.

— from bb thread thr_utx9fq874u

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 2 (GLM 5.3, at 788c712)

Owner review required: yes — it implements the consumer side of a cross-repo contract (the Slack session lease on POST /v1/subscribe, bb-gate/docs/hooks.md "Leasing a session") and changes existing behaviour people notice (reply-rights semantics, the 403 reply text, session retention). No decide-list entry's paths match the changed files (plugins/slack/** is in none of them), so this one is the owner's, not the project steward's. @trotterdylan, this one needs your unsigned approval to merge.

Resolved since round 1

  • Blocker 1 (the owned backstop reply is undeliverable — the other server's live lease makes the service refuse it): resolved by 788c712, taking the best-effort path I offered. The fake hooks now answers the notice reply 403 not-routed as the deployed service does, the test asserts the plugin degrades gracefully (the "could not post the backstop reply" log line, no capability leaked), and README.md/SKILL.md now say the log line is the reliable half. The inline comment in onLeaseRejected states the reason.
  • Suggestion (clear ownedNotified on reacquisition): resolved — ownedNotified.delete(event.session) when a wake routes the session here, with a new second-episode test. Placement is right: a wake arriving here means the service routed to this server again, so the previous dispossession episode is over.
  • Suggestion (warn on every renewal): resolved — the warn moved inside the once-per-episode guard.
  • Suggestion (24h test didn't check "still leases it"): resolved — the test now ticks 30 min past the 25h mark and asserts the renewal's subscribe body.
  • Suggestion (deletion untested): resolved — the archive test now also emits thread.deleted for the remaining thread and asserts nothing further is leased.

Suggestions

  • The doc comment above onLeaseRejected (server.ts:488) still reads "the owner gets one backstop reply so they know" without the best-effort caveat that the code comment, README and SKILL now carry. A one-word touch-up whenever the file is next edited; nothing to do now.

Checked

  • Ran: npm run typecheck and npm test in plugins/slack at 788c712 — typecheck clean, 92/92 pass (the plugin is not in the CI matrix; I ran both).
  • Re-reviewed the new diff itself (6a27779..788c712): only plugins/slack touched, no behaviour beyond the round-1 findings, the message fake route's default keeps every other reply test intact, and the second-episode test's wake correctly re-uses thread thr-1 so only the episode counting is under test.
  • Verified against ticket: all criteria met as in round 1, with the owned criterion now honestly framed (log line reliable, reply best-effort) and tested against a service-faithful fake; no source-contract.ts copy touched; the contract side (docs/hooks.md, bb-gate#55 merged and deployed) is unchanged.

— from bb thread thr_t2rh6r7uw5

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Round 2 suggestion (doc comment on onLeaseRejected) taken in 071e79c: the comment now carries the best-effort caveat. Typecheck and npm test (92) pass. No behaviour change since the approval at 788c712.

— from bb thread thr_utx9fq874u

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Review round 3 (GLM 5.3, at 071e79c)

Owner review required: yes — as in round 2: consumer side of a cross-repo contract (the Slack session lease, bb-gate/docs/hooks.md) plus changed reply-rights behaviour; no decide-list entry's paths match plugins/slack/**, so @trotterdylan's unsigned approval is the gate.

No new round was needed: 071e79c is comment-only — the round-2 suggestion to carry the best-effort caveat into the onLeaseRejected doc comment, nothing else. The round-2 approval at 788c712 therefore stands and is repeated here on the new head. No blockers, no new suggestions; nothing to re-run for a comment-only diff.

— from bb thread thr_t2rh6r7uw5

@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Approved 071e79c from the Stewards page

@trotterdylan
trotterdylan merged commit bf82dc9 into main Oct 30, 2026
22 checks passed
@trotterdylan
trotterdylan deleted the bb/bb-129-slack-plugin-session-leases-pr-2-of-2-thr_utx9fq874u branch October 30, 2026 18:02
@trotterdylan

Copy link
Copy Markdown
Collaborator Author

Merged: owner-reviewed, approved by trotterdylan on 071e79c (unsigned comment 2026-10-30T18:02:17Z); reviewer approved round 3 at 071e79c; checks green. merge commit bf82dc9.

This moves the bb-plugins pin on bb-gate main, builds a new bb-server image and stages a restart on every developer's server (at Restart now, or in the 03:00–05:00 ET quiet window).

— from bb thread thr_utx9fq874u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant