Skip to content

Discovery: Half-Shell codebase audit and technical-debt dependency map #53

Open
6 of 7 issues completed

Description

@diese-tech

Discovery summary

This issue captures the full codebase-wide Half-Shell audit of SwiftDispatch and serves as the parent discovery/tracking issue for the highest-value technical-debt work identified during the review.

The audit did not find a codebase that needs a broad rewrite. SwiftDispatch has already accumulated substantial product infrastructure: role-aware UI routing, tenant isolation, payments, public intake, realtime state, SMS outbox delivery, operational scripts, E2E coverage, and explicit lifecycle logic.

The main risk is policy drift as the product has grown:

  • authorization rules are not enforced consistently at every boundary
  • client mutation reliability differs by workflow
  • background delivery recovery is incomplete
  • test coverage is stronger around utilities than production mutation seams
  • repository quality gates rely too heavily on developer discipline
  • a few framework/tooling assumptions have gone stale

This discovery issue should remain the high-level map. Concrete implementation belongs in linked tracking issues.


Half-Shell findings

Leonardo — system-level verdict

The codebase is not asking for a rewrite. The load-bearing boundaries need reinforcement.

The top four debt buckets are:

  1. Authorization boundary is incomplete.
  2. Client mutation reliability is inconsistent.
  3. SMS background-job recovery is incomplete.
  4. Quality gates are mostly human-enforced.

Second-tier debt includes behavior-oriented test coverage, rate-limit degradation policy, framework maintenance, and minor repository hygiene.


Raphael — authorization boundary drift

SwiftDispatch intentionally distinguishes user capabilities in the UI/proxy layer:

  • technicians use /tech
  • dispatchers/admins use /dispatch
  • admins use /admin
  • super admins use /superadmin

However, several API mutation routes rely on authentication/profile lookup plus tenant scoping rather than an explicit allowed-role check.

Current RLS is primarily tenant-scoped. It is effective for separating companies, but it does not universally express the application's role model.

The result is a split model:

UI authorization: role aware
API authorization: inconsistent
Database authorization: primarily tenant aware

This is especially risky where mutation routes assume that any non-admin actor is a dispatcher when writing audit/status events.

A requireRole() abstraction already exists, so this is mostly a consolidation/enforcement problem rather than a new architecture problem.

Child issue


Donatello — overlapping responsibility boundaries

SwiftDispatch currently has several overlapping authorization/context mechanisms, including variants of:

  • profile requirements
  • admin/super-admin profile helpers
  • generic role requirements
  • manual company checks
  • proxy checks
  • RLS tenant enforcement

Each mechanism makes sense locally, but together they make it harder to answer one simple question:

What must be true for actor X to perform operation Y?

The long-term goal should be one boring application-level authorization contract, with RLS and route gating acting as defense-in-depth rather than competing sources of authority.

Donatello also flagged the same duplication pattern in client mutations: each flow independently implements fetch, optimistic state, error parsing, rollback, and canonical reconciliation.

That duplication is already producing divergent failure behavior.

Child issue


Michelangelo — client/server divergence on transport failure

Several client workflows treat an HTTP error correctly but do not treat a rejected fetch() as the same failed mutation.

Typical failure mode:

optimistic UI update
→ fetch begins
→ transport failure rejects promise
→ HTTP error branch never runs
→ rollback never runs
→ UI disagrees with canonical server state

This was observed in the technician-assignment work addressed by PR #48 and also exists as a broader pattern in adjacent job-board operations such as optimistic job movement and loading-state cleanup.

The fix should not be scattered try/catch patches everywhere. The repo should have a small shared mutation/result contract that normalizes:

  • success
  • non-2xx HTTP failure
  • network/transport failure
  • malformed response
  • rollback
  • canonical reconciliation

Child issue


Casey Jones — stranded SMS work

The SMS outbox has a good basic lifecycle:

pending
→ processing
→ sent
  or retrying
  → failed

It also records locked_at, which suggests a lease model.

However, the worker only loads pending and retrying work. A row claimed into processing can remain there permanently if the worker crashes or is interrupted before the row is finalized.

That means the system already has most of a lease/retry design, but not stale-lease recovery.

The fix should complete the existing model rather than replace it with a new queueing platform.

Child issue


Splinter — quality gates are conventions, not guarantees

The repo has useful local validation commands, but they are not fully enforced by repository automation.

Key observations from the audit:

  • main does not currently require validation checks before merge.
  • There is no baseline PR workflow enforcing the full validation contract.
  • the lint command still reflects an older Next.js assumption (next lint).
  • generated tsconfig.tsbuildinfo is tracked.

As SwiftDispatch grows, the repository should make a safe change the default path rather than depending on someone remembering every validation step.

Child issue


April O'Neil — coverage is utility-heavy, workflow-light

The existing test suite provides useful coverage around several internal utilities and state primitives, but the product's most important business seams need more behavior-level integration coverage.

The important distinction is:

proving a final database state is reachable

versus

proving the same public mutation seam used by production reaches the correct state under success and failure

Issue #45 / PR #48 already exposed this distinction around dispatcher assignment.

Future test investment should focus on representative behavior boundaries rather than only increasing raw test count.

High-value seams include:

  • dispatcher mutation authorization
  • job lifecycle mutation
  • technician assignment
  • quote mutation
  • customer acceptance
  • SMS delivery/recovery
  • public intake abuse controls
  • cross-tenant isolation

This work can be added incrementally alongside #49–#52 rather than opened as a separate broad test rewrite unless the scope grows materially.


Shredder — adversarial view

The most useful adversarial question is:

What happens if the actor ignores the UI entirely and calls the APIs directly?

The first probes should be:

  1. technician attempting dispatcher/admin mutations
  2. dispatcher attempting admin mutations
  3. admin attempting super-admin operations
  4. valid user attempting cross-company mutations
  5. mutations interrupted between optimistic UI and server acknowledgment
  6. background jobs interrupted after claim but before completion

The codebase should answer those with executable tests, not architectural assumptions.


Recommended dependency chart

The audit recommends treating the work as a dependency graph, not four unrelated cleanup tasks.

flowchart TD
    A[Discovery audit: this issue]
    B[#49 Authorization boundary]
    C[PR #48 Assignment divergence fix]
    D[#50 Shared mutation reliability]
    E[#51 SMS lease recovery]
    F[#52 CI quality gates]
    G[Broader behavior-level regression coverage]
    H[Lower-priority maintenance: rate-limit policy, dependency updates, repo hygiene]

    A --> B
    A --> C
    A --> E
    A --> F

    C --> D
    B --> G
    D --> G
    E --> G

    B --> F
    D --> F
    E --> F

    F --> H
Loading

Suggested execution order

Phase 1 — establish correctness boundaries

  1. Centralize API role authorization and enforce a tested permission matrix #49 Authorization boundary

    • establishes who may perform each operation
    • creates an executable permission matrix
    • reduces security/correctness ambiguity before more feature work
  2. Finish and merge PR Fix dispatcher assignment / Kanban state divergence (issue #45) #48

    • resolves the concrete assignment/Kanban divergence already under review
    • provides the first known-good canonical-state reconciliation pattern

Phase 2 — generalize the reliability lessons

  1. Standardize client mutation handling and rollback semantics #50 Shared mutation reliability

  2. Add stale SMS outbox lease recovery and worker resilience #51 SMS lease recovery

    • can proceed mostly independently after the authorization model is understood
    • should complete the background-work reliability model before notification volume grows

Phase 3 — lock in the gains

  1. Add CI quality gates and protect main from unvalidated changes #52 CI quality gates

  2. Broader behavior-level regression coverage

    • expand around real production seams as the above work lands
    • avoid a giant one-time test rewrite
  3. Lower-priority maintenance

    • rate-limit fail-open/fail-closed policy by endpoint
    • dependency/version verification and patching
    • generated-file/repo hygiene
    • stale abstractions/dead-code cleanup

Dependency rules / exit gates

Use these gates before moving to dependent work:

Before #50 is considered complete

Before #52 branch protection is enabled

Before declaring this discovery resolved


Priority summary

Priority Work Reason
1 #49 Authorization boundary Security and correctness boundary
2 PR #48 then #50 mutation reliability Prevents UI/server divergence
3 #51 SMS lease recovery Prevents silently stranded operational work
4 #52 CI / branch protection Prevents the same classes of debt from returning
5 Behavior-level integration coverage Makes production seams trustworthy
6 Rate-limit degradation review Make security/availability tradeoffs explicit
7 Framework/dependency maintenance Remove stale assumptions and patch risk
8 Repo hygiene Low-risk cleanup after load-bearing work

Non-goals

This discovery does not recommend:

  • rewriting SwiftDispatch
  • replacing Supabase
  • replacing the SMS outbox with Kafka/SQS
  • adopting a large frontend state library solely for mutation handling
  • implementing enterprise RBAC/custom roles
  • performing a giant test-suite rewrite

The recurring recommendation from the audit is to make the existing architecture more explicit and dependable, not more complicated.

Sub-issues

Sub-issues

6 of 7 Issues completed
Loading 7 sub-issues

Activity

  1. diese-tech commented on Sep 19, 2026

    @diese-tech
    OwnerAuthor

    Status update: all four Phase 1–3 child issues are now merged.

    Checking this issue's own "Before declaring this discovery resolved" gates:

    What's left per the audit's own Phase 3/priority list is lower-priority and not yet broken into concrete tracked issues:

    I'll leave this issue open pending direction on whether to open tracked follow-up issues for that remaining list, or treat it as accepted/deferred debt for now.


    Generated by Claude Code

  2. diese-tech commented on Sep 19, 2026

    @diese-tech
    OwnerAuthor

    Full scope for the remaining test/security work is now tracked, per the repo owner's follow-up request after #58 merged:

    #62 and #64 both build directly on #58's findings — #62 finishes the route-coverage sweep #58 started, #64 goes looking for more instances of the exact cross-tenant bug class #58's review caught in POST /api/jobs.


    Generated by Claude Code

  3. diese-tech commented on Sep 20, 2026

    @diese-tech
    OwnerAuthor

    Status update — this discovery's tracked follow-on work is essentially done:

    Closed: #49, #50, #51, #52, #58, #60, #62, #63, #64, #66.

    Open, with a clear reason each:

    Notable finding from this pass, fixed in PR #70 (#66): status_events inserts for dispatcher/admin-created and -updated jobs were being silently dropped by RLS in production — confirmed directly against the live project that 654 real, non-demo jobs have zero audit-trail rows. Fixed by routing those inserts through the admin client, matching intake/tech-action's existing pattern. No backfill was done for the already-affected 654 jobs — that's a data decision I left for you rather than doing unilaterally.

    Everything else from the original Half-Shell findings (authorization boundary, client mutation reliability, SMS lease recovery, CI quality gates, and the systematic role/coverage audits) has landed. I'd consider this discovery's tracked scope resolved except for #61's live smoke-test step and the optional nextval follow-up.


    Generated by Claude Code


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions