refactor: return Results up the stack, throw only at boundaries#14
Closed
kastriotkastrati wants to merge 0 commit into
Closed
refactor: return Results up the stack, throw only at boundaries#14kastriotkastrati wants to merge 0 commit into
kastriotkastrati wants to merge 0 commit into
Conversation
7626865 to
8b9e972
Compare
ada6911 to
f03dbef
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Reworks the branch's error handling into a railway: inner functions return
Result<T, Error>and propagate failures as values; only boundary frames throw, and only when the runtime needs it.The previous state of this branch wrapped ~182 calls in
Result.from(...)and then immediatelythrow x.error— a behavior-preserving no-op (identical to a bareawait) that added ~500 lines and never turned an error into a value (return Errcount was 0). It also didn't compile (coordinator.tslost env-var narrowing inside a closure).How
Result:Store(28 methods),Platform(10),LlmAdapter.completenow returnPromise<Result<T, Error>>(interfaces + all implementations change in lockstep).return Err. Domain invariants useErr(new Error("MESSAGE"))so the error channel stays uniformlyError(nostring | Errorunions).queue→msg.retry();scheduled, DOalarm/union→ throw (cron/DO retry); HTTP routes → 5xx for crypto/KV/queue failures, 4xx for client faults. Best-effort work (synth/route/notify) logs and continues.asyncMap/asyncForEach/asyncUnfoldcan't short-circuit on anErrand raw loops are eslint-banned, a fallible loop is wrapped in oneResult.from(() => asyncMap(... unwrap(await f()) ...)). Adds a singleunwraphelper tocommon.helper.ts.coordinator.tstype error.Resultsignatures (unwrap.data, assertr.ok,rejects.toThrow→!r.ok+error.messagechecks).Rebased onto
main(org pulse)Rebased onto
main(f03dbef feat(worker): post daily org pulse to Slack). That feature touched the same files the railway rewrote, so the merge re-expresses the org pulse in railway form rather than picking a side:aggregateOrg/postDailyOrgPulse/buildDailyOrgPulse(clusters.ts) now returnResult<void|string, Error>and propagate thelistOpenSignals/getIdentity/getWorkingNotes/upsertWorkingNotes/postMessage/editMessageresults; the identity fan-out uses the loop-island; the post-vs-editexternalRefchoice is an IIFE returningResult<string, Error>.SlackAdapter.postMessagekeeps the new channel-post branch (meta.channelId) inResultform.D1Storeadopts therowToSignalhelper and addslistOpenSignals(railway).aggregateOrg'soptsis required (no default param) per house style; the two no-arg test calls pass{}.postMessage-to-channel test asserted a raw{ id }return where the method now returns aResult.Cleanup — boundary literals
Replaced
Result.fromSync(() => null)/Result.fromSync(() => undefined)withOk(null)/Ok(undefined)at the 5 KV-dedupe boundary sites inroutes/github.tsandroutes/slack.ts— wrapping a non-throwing literal infromSyncis noise. The legitimateResult.fromSync(() => JSON.parse(...))sites are untouched (parse can throw).Verification
pnpm typecheck(13/13),pnpm lint:check, andpnpm test(174 tests, 13 packages) all pass. A 4-lens adversarial audit (feature-preservation, railway invariants, merge completeness, cleanup equivalence) found no issues. 43 files, +2496/−1248.These are behavior changes that predate the rework and were preserved as-is — flagging for an explicit decision:
isGithubWebhookBody/isSlackEnvelopeguards +400 "invalid payload"/"invalid json"responses. Original code did a bareJSON.parse(raw) as X(malformed body → threw → 500, which providers retry). The guards are permissive (all fields optional), so real webhooks pass; the real delta is malformed JSON now returns 400 (not retried) vs the old 500 (retried). Keep the validation, or restore pre-branch behavior?safeParse(identity-source): added per-row string validation that throws on a non-string roster field; the original tolerated it. Static deploy-time config, so low risk — fail-fast on bad config is arguably correct.extractText(adapter-llm): now returns""for non-arraycontentinstead of throwing. Required to makeextractTextnever-throw; only affects malformed model output.