Skip to content

fix(platform): list the caller's slugs on every org slug refusal - #3526

Merged
yannickmonney merged 17 commits into
mainfrom
fix/org-slug-refusals-list-slugs
Sep 28, 2026
Merged

yannickmonney merged 17 commits into
mainfrom
fix/org-slug-refusals-list-slugs

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Only 400 ORG_SLUG_REQUIRED carried data.organizations. If a key holder in several organizations mistyped X-Organization-Slug (404 ORG_SLUG_INVALID) or named an organization they are not a member of (403 ORG_FORBIDDEN), the error said what was wrong but not which slugs are valid. GET /api/v1/me sits behind the same header, so it couldn't help either. The getting-started page and the tutorial already promised the list on both refusals (#3097 has the GlitchTip trail of a caller guessing vatplus, vat_plus, vatplus-ch).

Change

  • listSelectableOrganizations in backend/domains/organizations/service.ts returns {slug, name} for every membership that is not disabled and whose organization has a slug, oldest first. Every refusal thrown by resolveUserOrganization uses it: ORG_SLUG_INVALID, ORG_FORBIDDEN (an empty list when the key holder has no membership left), ORG_SLUG_REQUIRED (unchanged behaviour, same helper).
  • backend/rest/v1.ts: the door's own two refusals carry the same list:
    • the 404 for a header that cannot be a slug. The value itself is still never sent to the database; the test now asserts that.
    • the 403 when a membership is removed while the door resolves it.
  • API contract 3.2.0. The OpenAPI descriptions now name data.organizations on the header parameter, on the door-wide 403/404 on every operation and in the Error.data.organizations schema. public/openapi.json and the contract fingerprint are regenerated.
  • Docs in EN/DE/FR:
    • the organization-selection table in develop/api-reference.md, as the task asks;
    • the refusal sentence in develop/mcp-endpoint.md;
    • the troubleshooting rows in tutorials/developer/call-tale-from-a-script.md.
  • Manual layer: the ORG_SLUG_INVALID/ORG_FORBIDDEN row in reference/error-codes.md and a new row in reference/automation.md. No new suite box, because the specs own this end to end.

Verification

  • backend/rest/v1.test.ts: six new cases, all failing on origin/main (checked before the fix) and passing now:

    • mistyped slug (404);
    • a value that cannot be a slug (404);
    • foreign organization (403);
    • membership removed mid-request (403);
    • no membership (403, empty list);
    • sole membership whose organization has no slug (404).

    The existing malformed-header case now also asserts that the value never reaches a query.

  • New backend:integration lane checkOrgSlugRefusals (backend/rest/org-slug-refusals.integration.ts), run on a fresh tale-db with ITEST_LANES=checkOrgSlugRefusals. A key with two live memberships and one disabled membership sends a mistyped, a malformed, a foreign and a disabled slug, then no header, then a listed slug on /me.

    • With the fix: 21/21 checks passed.
    • Without the fix: 3 FAIL: mistyped, malformed, and foreign/disabled all answer with no data.
  • vitest --project server backend/rest passed: 26 files, 930 tests. Also backend/domains/organizations and scripts/openapi (the openapi.json, fingerprint and version guards) pass.

  • oxlint --type-aware on the changed files, tsc --noEmit (platform), bun run lint:manual and bun run --filter @tale/docs test (34 files, 166 tests) all passed.

The final combined branch was checked directly with 5 server files / 264 tests (REST refusals, automation contract, organizations and OpenAPI), the manual guard and three suite guides. The real HTTP/Postgres slug lane passed 21/21 again after the schedule migration was applied to the prior isolated test database.

Left open

  • backend:integration is not a CI job. It was proven locally with only this lane (ITEST_LANES), not as a full run.
  • Merge after feat(platform): pause a schedule after five permanent failures in a row #3533. This branch includes its reviewed schedule change at e8f0970c5506e87a12f7b009b1c5d6f099d68bb9, preserves that change's contract 3.1.0 entry, and publishes the slug-refusal additions as 3.2.0 with regenerated OpenAPI and fingerprint.

Closes #3097

yannickmonney and others added 9 commits September 27, 2026 19:15
Only 400 ORG_SLUG_REQUIRED carried data.organizations. A key holder in
several organizations who mistyped X-Organization-Slug (404
ORG_SLUG_INVALID) or named a foreign one (403 ORG_FORBIDDEN) learned
what was wrong but not what to send, and GET /api/v1/me sits behind the
same header. The getting-started page already promised the list on
both.

listSelectableOrganizations (every membership that is not disabled and
has a slug, oldest first) now backs every refusal the resolver throws
and the door's own two: the malformed-header 404, which still never
looks the value up, and the 403 for a membership removed mid-request.

Contract 3.1.0; the OpenAPI descriptions, the organization-selection
table (EN/DE/FR), the MCP and tutorial pages and the manual registers
say so. A new backend:integration lane, checkOrgSlugRefusals, sends a
mistyped, a malformed, a foreign and a disabled slug with a two-org key.
A schedule whose automation fails the same way at every occurrence used
to fire forever: every run failed, spent what it could and told nobody.
finishRun now keeps a failure streak per trigger, in the transaction that
lands a run a trigger started (`trigger:<id>`):

- a success resets it; a failure with a code the next occurrence would
  repeat (node_error, connector_error, llm_output_invalid, auth_error,
  missing_api_key, credit_exhausted, model_not_found) counts; transient
  codes, unclassified failures and cancels neither count nor reset;
- runs started before the trigger was last saved never count, and every
  save resets the streak;
- the fifth permanent failure in a row turns an enabled schedule off
  (enabled=false, new skip reason `paused_after_failures`), writes an
  `automation.trigger.paused` audit row and notifies each owner and admin
  with the revived `automation_failed` type (bell + email, gated by the
  `automation_alerts` preference, opening the automation's General tab);
- webhook and event triggers keep a streak but are never paused, since a
  paused one would drop the deliveries and events that arrive meanwhile;
- the bookkeeping rides a savepoint so it can never leave a run
  unfinished, and the notice a nested one so a failed bell keeps the pause.

Migration 0123 adds the streak columns (consecutive_failures,
last_failed_at_ms, last_failure_code, last_failed_run_id) and widens the
skip-reason CHECK. The Trigger section shows the pause, the streak, the
last failure's code and a link to its run; saving the trigger clears the
pause and marks the notices read. Settings > Notifications offers
Automation alerts to owners and admins again. REST contract 3.1.0 adds the
streak to `Trigger` and the new reason to `lastSkipReason`; the MCP
trigger view carries the same fields. Docs (EN/DE/FR), manual boxes and a
real-Postgres lane (`checkTriggerPauseAfterFailures`) cover it.

Refs #3092
#3519 landed 0123_project_agent_runs_mention_source.sql on main first,
so the trigger failure streak takes the next free number.
finishRun audited the run (the org's audit-chain lock) and only then
wrote the trigger row, the reverse of the order a producer that stamps
an event trigger and then audits takes, e.g. a comment edit's
comment.mentioned dispatch. The two could deadlock. finishRun now locks
the trigger row first (lockTriggerForRunOutcome, in a savepoint, and only
for the rows the streak will write), audits the run, then keeps the
streak.

A success that lands after its schedule paused (a run that overlapped
the pause) no longer resets the streak: the paused schedule keeps the
count that paused it until someone saves the trigger.
- deleteTrigger and deleteAutomationCascade mark the automation_failed
  notices of a paused schedule read, and only of a paused one.
- The MCP trigger view selects and carries the failure streak, omitting
  a last failure there never was.
- The real-Postgres lane reads the paused schedule back through
  listTriggers and checks that a success landing after the pause keeps
  the streak.
The Trigger section's View run always opened the organization's run
route, even on a project's General tab, dropping the project breadcrumbs
and sidebar. The project route now hands its projectId down and the link
picks the project run route, as the run list does. The failure code and
the streak lines use @tale/ui InlineCode and Text instead of hand-rolled
copies.
9ab5ea7 had finishRun lock the trigger row before its run audit, to
match a producer that stamps an event trigger and audits afterwards. Most
producers do the opposite: they audit, then emit (createTask,
appendTaskComment, the contact writes, addMessageToConversation,
createProject). A run landing a permanent failure, or a success that
resets a streak, then deadlocked against them. Either the producer's
dispatch lost, silently, inside emitEvent's savepoint, or finishRun's
audit row did and the terminal write rolled back.

One order now holds everywhere: the organization's audit chain before
any trigger row.

- finishRun writes the run's audit row and keeps the streak after it
  (lockTriggerForRunOutcome is gone).
- dispatchAutomationEvent takes the chain (lockAuditChain, the advisory
  lock every append takes first) before it stamps a trigger. The
  producers that emit before they audit (editTaskComment,
  upsertTaskByExternalRef's create, and every createConversation caller:
  inbound ingest, compose, API sync) hold it by then as well.
- The schedule scan, the webhook door and trigger saves take no chain.

The checkTriggerStreakLockOrder lane runs the real finishRun beside a
real emitEvent producer held between its audit and its emit, in both
orders: both commit, the dispatch's stamp lands and the streak counts.
It fails on the previous order (finishRun: deadlock detected) and when
only finishRun's order is restored (the streak lost to a deadlock).
@yannickmonney
yannickmonney merged commit 5dafc7a into main Sep 28, 2026
57 checks passed
@yannickmonney
yannickmonney deleted the fix/org-slug-refusals-list-slugs branch September 28, 2026 07:25
yannickmonney added a commit that referenced this pull request Sep 28, 2026
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.

Improvement: ORG_SLUG_REQUIRED / ORG_SLUG_INVALID never tell an authenticated API caller their own valid org slugs

1 participant