Skip to content

feat: prefix-routed multi-Webhook delivery - #24

Open
japperJ wants to merge 9 commits into
daslearning-org:developfrom
japperJ:feat/multi-webhook-routing
Open

japperJ wants to merge 9 commits into
daslearning-org:developfrom
japperJ:feat/multi-webhook-routing

Conversation

@japperJ

@japperJ japperJ commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds multiple Webhooks with prefix-keyword routing on top of the 1.2.0 inbound SMS webhook. A received SMS is delivered to exactly one enabled Webhook, optionally after removing the route prefix, and only if the sender is allowed for that Webhook.

Behaviour

  • One enabled Webhook → behaves exactly like 1.2.0: keyword and stripping are ignored, full text is forwarded.
  • Two or more enabled Webhooks → the SMS is routed by:
    1. the unique case-insensitive prefix keyword (for example #sales: hello), matched only at the start and only when followed by : or whitespace;
    2. otherwise the single catch-all Webhook (empty keyword);
    3. otherwise the SMS is dropped and logged.
  • Each Webhook chooses its prefix handling: none, keyword only, or keyword_and_separator.
  • Each Webhook has a Sender allowlist. Empty means all; numeric senders compare by digits, alphanumeric sender IDs case-insensitively. A blocked sender is dropped, never rerouted.
  • Different signing secrets, enabled states, names, and status per Webhook.
  • The Outbox is partitioned into per-Webhook FIFO lanes processed by one round-robin worker, so a target in backoff does not delay another target. Capacity remains global drop-oldest.

Remote API

  • settings.webhook becomes settings.webhooks; retry policy moves to top-level webhook_delivery.
  • PUT /api/settings is a full-array replacement with server-assigned opaque ids (wh_…). Entries with an id update, entries without one are created, omitted entries are deleted.
  • Per-entry secret sentinel: omitted keeps by id, "" clears, non-empty replaces.
  • POST /api/webhook/test accepts id (required when several are configured), selects a saved target, and adds X-OpenSMS-Webhook-Id. url/secret overrides remain.
  • GET /api/status returns a per-Webhook webhooks array with pending and last_delivery; new webhook_unrouted warning and per-entry webhook_url_invalid with webhook_id.
  • A legacy single-webhook PUT body is accepted for one release as a one-entry shim (cannot be combined with the new shape). Deprecated; removal targeted at 1.4.0.

Migration

  • Legacy config.json webhook objects migrate on load to a one-element array with id wh_legacy; the next write persists the new shape. Migration does not bump the Revision.
  • Legacy Outbox records without webhook_id attach to wh_legacy and keep their captured URLs.
  • Legacy webhook_status.json maps to the migrated Webhook.
  • Unknown top-level, delivery, and per-Webhook fields are preserved (per-Webhook fields by id).

Settings UI

The single Webhook block becomes collapsible Webhook cards with add/delete/test, per-card last-delivery status, keyword/strip fields (shown when 2+ are configured), and a multiline allowed-senders field. Edits are local until Save; the enabled toggle no longer auto-saves. Delete confirms that pending deliveries for that Webhook will be dropped.

Tests

python -m unittest discover -s tests — 208 tests pass, including new routing, stripping, allowlist, per-target lane, migration, API schema, id/secret semantics, status, and local-save coverage.

Docs

README, docs/remote-api.md, GLOSSARY.md, ADR 0003, CHANGELOG, and version 1.3.0.

Notes

  • One enabled Webhook requires no keyword, per the feature request.
  • A catch-all is optional; /api/status warns when 2+ are enabled without one.
  • Pending deliveries keep their captured URL and payload; the signing secret is read live by id.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 15:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical regressions break Settings callbacks and allow stale saves to overwrite remote edits and delete newly added webhooks.

6 open issues
What changed in this PR

Extends inbound SMS forwarding with multiple prefix-routed Webhooks while preserving single-target behavior and migrating legacy configuration.

Changes:

  • Adds routing, prefix stripping, sender allowlists, and per-Webhook delivery lanes.
  • Updates settings, status, and test APIs for multiple targets.
  • Introduces editable Webhook cards, expands tests, and documents version 1.3.0.
File Description
README.md Documents configuration, routing, and delivery behavior.
GLOSSARY.md Defines routing and multi-Webhook terminology.
docs/​webhook/​receiver_test.md Updates sample request version and headers.
docs/​remote-api.md Describes API schemas, migration, and validation.
docs/​adr/​0003-multi-webhook-routing.md Records multi-Webhook design decisions.
docs/​adr/​0001-inbound-sms-webhook.md Notes decisions superseded by ADR 0003.
CHANGELOG.md Adds the 1.3.0 release summary.
app/​tests/​test_webhook.py Covers routing, allowlists, and independent delivery lanes.
app/​tests/​test_settings_local_save.py Updates local-save and migration coverage.
app/​tests/​test_remote_api.py Tests multi-target settings, status, and test delivery.
app/​services/​webhookSvc.py Implements routing, delivery lanes, and per-target status.
app/​services/​settingsSvc.py Adds array settings, migration, and validation.
app/​services/​postApi.py Exposes multi-Webhook API operations.
app/​services/​config.py Bumps the version to 1.3.0.
app/​services/​appSvc.py Reports per-target pending counts and routing outcomes.
app/​screens/​settings.py Introduces editable Webhook cards.
app/​main.py Connects card management, saving, testing, and status display.

Review effort: Balanced

Comment thread app/main.py
if webhook is None:
return None
form["webhook"] = webhook
form["webhooks"] = [card.read() for card in self.webhook_cards]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef6ef95. The Settings form now carries the loaded Revision into save_local_settings; a stale full save raises SettingsRevisionConflict, the UI shows a notice and reloads instead of replacing the array. Added a regression test (test_a_stale_full_save_is_rejected).

Comment thread app/main.py

def webhook_switch_active(self, instance, value):
"""Apply the toggle immediately, instead of waiting for Save.
def refresh_webhook_view(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in aa056a3. reload_settings_config is restored and both on_settings_entry and the port-change callback use it; also verified on-device that opening Settings no longer raises AttributeError.

Comment thread app/main.py
if webhook is None:
return None
form["webhook"] = webhook
form["webhooks"] = [card.read() for card in self.webhook_cards]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef6ef95. The auto-start switch now saves only {"boot": ...} and updates the local switch state; it no longer touches Webhook cards or secrets. A boot-only save test confirms remote card changes survive.

Comment thread app/main.py
url = (card.ids.url_inp.text or "").strip()
secret = card.ids.secret_inp.text or ""
if (
not secret

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef6ef95. WebhookCard exposes secret_cleared, and the test-delivery path no longer falls back to the stored secret when that sentinel is set; it sends an unsigned test until/unless a new secret is typed.

Comment thread app/services/webhookSvc.py Outdated
Comment on lines +427 to +431
if mode == STRIP_KEYWORD:
return rest.lstrip()
if rest[:1] == ":" or (rest[:1] and rest[0].isspace()):
rest = rest[1:]
return rest.lstrip()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef6ef95. strip_route_prefix now preserves the separator and following whitespace in keyword mode, and removes only the separator plus at most one following space in keyword_and_separator mode. Added space-preservation cases to test_strip_prefix_modes.

Comment on lines +948 to +951
if due <= now:
self._lanes.move_to_end(webhook_id)
return record, None
soonest = due if soonest is None else min(soonest, due)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ef6ef95. The worker reconciles every 0.2 s against the live Settings before checking retry deadlines: records for a disabled or deleted Webhook are failed immediately, their lane is removed, and capacity is freed. Added tests for disabling, deleting, and re-enabling a target.

@japperJ

japperJ commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

All six review findings are addressed in ef6ef95 (the reload helper was restored earlier in aa056a3):

  • Stale saves now carry the loaded Revision; conflicts reload instead of overwriting remote changes.
  • The auto-start switch saves only boot and leaves Webhook cards untouched.
  • Test delivery honors an explicitly cleared secret.
  • Prefix stripping preserves separator whitespace per ADR 0003.
  • The worker reconciles queued records against live target state every 0.2 s, so disabled/deleted targets fail promptly even during backoff.
  • Regression tests cover stale saves, boot-only saves, space-preserving stripping, and disable/delete/re-enable reconciliation.

python -m unittest discover -s tests: 211 tests pass. The Android build was rebuilt/installed and the remote API was exercised on the device (health, GET/PUT settings, status, test delivery, auth, and legacy migration).

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.

2 participants