<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shubham</title>
    <description>The latest articles on DEV Community by Shubham (@shubham399).</description>
    <link>https://dev.to/shubham399</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366471%2F5ee5ca23-114c-4498-86ed-33a9db44c8a9.png</url>
      <title>DEV Community: Shubham</title>
      <link>https://dev.to/shubham399</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubham399"/>
    <language>en</language>
    <item>
      <title>Use grill-me to Pressure-Test an AI Implementation Plan Before Code</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sat, 15 Aug 2026 23:45:03 +0000</pubDate>
      <link>https://dev.to/shubham399/use-grill-me-to-pressure-test-an-ai-implementation-plan-before-code-11e6</link>
      <guid>https://dev.to/shubham399/use-grill-me-to-pressure-test-an-ai-implementation-plan-before-code-11e6</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F6019e935-fa36-47a3-a057-b164bbb44afe%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F6019e935-fa36-47a3-a057-b164bbb44afe%2Fpublic" width="1152" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Many software mistakes begin as decisions that nobody explicitly made. A feature request sounds clear enough, an AI coding agent begins implementation, and the details get settled by whichever model output appears first. Later, the team discovers that “add roles,” “cache this endpoint,” or “support collaboration” contained several linked product, data, security, and rollout choices.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-grill-me" rel="noopener noreferrer"&gt;grill-me&lt;/a&gt; is designed for the moment before that happens. It is a manually invoked skill that interviews you about a plan or design until the important decision tree is resolved. It asks one question at a time, supplies a recommended answer, and waits for feedback before continuing. Crucially, if an answer can be discovered by inspecting the codebase, the agent should investigate rather than ask you to recreate repository facts from memory.&lt;/p&gt;

&lt;p&gt;The name is playful. The underlying practice is serious: force a proposal to survive questions before code makes it expensive to change.&lt;/p&gt;

&lt;h2&gt;
  
  
  What grill-me is and is not
&lt;/h2&gt;

&lt;p&gt;grill-me is not a general implementation workflow. It does not replace a specification, an issue breakdown, tests, or code review. It is a pressure-test for a direction that is not yet sufficiently settled to build.&lt;/p&gt;

&lt;p&gt;That narrowness is its strength. Teams often need an agent to push back, not agree. A conventional assistant can be biased toward accepting the first plausible framing. A grilling workflow instead asks what would make the plan fail: what is ambiguous, what constraint is missing, what decision is being deferred accidentally, and what evidence should be checked in the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why one question at a time matters
&lt;/h2&gt;

&lt;p&gt;A long questionnaire feels efficient but usually is not. It asks about details before the premise is fixed, and the answers become inconsistent. A sequential interview allows dependencies to resolve in order.&lt;/p&gt;

&lt;p&gt;For example, consider “add organization-level roles.” The first question might be whether roles are global or scoped to each organization. That answer determines whether membership is a separate domain entity. The next question may be whether permissions are static role bundles or configurable. Only then does it make sense to ask about API shape, migration, administration UI, or audit requirements.&lt;/p&gt;

&lt;p&gt;By proceeding one decision at a time, the skill turns a vague feature into a chain of explicit commitments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with a proposal, not a blank page
&lt;/h2&gt;

&lt;p&gt;The best grilling sessions begin with enough context to challenge. State the desired outcome, the affected users, known constraints, existing artifacts, and the decision you want help making. Do not ask “what should we build?” when you really mean “which of two approaches fits this codebase?”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Grill this proposal before implementation.
Goal: organization owners can invite members by email.
Constraints: existing RBAC model, no public workspace enumeration,
and invitations must be revocable.
Inspect the relevant auth and membership code first.
Ask one question at a time. Recommend an answer, explain the trade-off,
and record settled decisions and open risks.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the agent a job: interrogate the plan using evidence, not invent the product from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions worth asking before code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Outcome and scope
&lt;/h3&gt;

&lt;p&gt;What user behavior changes? Who can trigger it? Who can observe it? What explicitly remains out of scope? Scope questions prevent a small feature from quietly becoming a platform redesign.&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain model
&lt;/h3&gt;

&lt;p&gt;What entities exist? Who owns them? What states can they occupy? What transitions are allowed? For invitations: pending, accepted, revoked, expired, perhaps resent. Decide whether resending creates a new token, extends expiry, or simply reuses a record. These are product rules with storage consequences.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authorization
&lt;/h3&gt;

&lt;p&gt;Who may create, view, revoke, accept, or resend the resource? Are permissions checked at an organization boundary, a project boundary, or both? What information should a denial reveal? Authorization is not a final middleware detail; it belongs in the plan.&lt;/p&gt;

&lt;h3&gt;
  
  
  Failure and recovery
&lt;/h3&gt;

&lt;p&gt;What happens when an email is already associated with a member? When delivery fails? When two administrators act simultaneously? When a client retries? A plan that only describes the happy path is still incomplete.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compatibility and rollout
&lt;/h3&gt;

&lt;p&gt;Will the change alter stored data, public APIs, permissions, or client assumptions? How will it be migrated? How will you monitor behavior after release? The right answer may be “no special rollout needed,” but it should be consciously chosen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use repository evidence
&lt;/h2&gt;

&lt;p&gt;One of grill-me’s best rules is that codebase facts should be discovered, not delegated to the user’s memory. Before asking whether an endpoint follows REST or RPC conventions, inspect existing endpoints. Before proposing a new role table, inspect the existing authorization system. Before recommending a migration, inspect database conventions and deployment tooling.&lt;/p&gt;

&lt;p&gt;This does not mean the codebase gets the final word. Existing patterns can be wrong or outdated. It means the agent should distinguish “we should deviate deliberately” from “we did not know the convention existed.”&lt;/p&gt;

&lt;h2&gt;
  
  
  What a successful session produces
&lt;/h2&gt;

&lt;p&gt;The output should be more than a transcript. It should leave behind a concise decision record:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The confirmed outcome and non-goals&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Key decisions and their rationale&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Alternatives considered and rejected&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Assumptions that need validation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open risks or owners&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The recommended next artifact: domain model, PRD, spec, issues, or implementation plan&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This gives a later agent something durable to follow. It also makes review better because reviewers can see what the code is intended to embody.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where grill-me fits now
&lt;/h2&gt;

&lt;p&gt;AI Hero notes an important update from Matt Pocock: for coding-oriented planning, he now generally recommends &lt;strong&gt;domain-model&lt;/strong&gt; as the starting point when you want to align a plan with codebase language, &lt;code&gt;CONTEXT.md&lt;/code&gt;, and architecture decision records. The suggested broader sequence is &lt;code&gt;domain-model → to-prd → to-issues → tdd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This does not make grill-me obsolete. It gives it a clearer role. Use grill-me as a lighter, targeted pressure-test when you have a plan that needs interrogation. Use a broader domain-model workflow when the task needs deep alignment with the application’s vocabulary and existing decisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common failure modes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Asking hypothetical questions forever
&lt;/h3&gt;

&lt;p&gt;Questions should converge toward a decision. If a scenario has little likelihood or impact, note it as a risk rather than letting it block every other choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Letting recommendations become decisions
&lt;/h3&gt;

&lt;p&gt;The agent can recommend; the responsible human should approve. Record when a recommendation is accepted, changed, or deferred.&lt;/p&gt;

&lt;h3&gt;
  
  
  Skipping evidence gathering
&lt;/h3&gt;

&lt;p&gt;A beautiful interview based on false assumptions is still waste. Require inspection of relevant code, documentation, and recent diffs first.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling it planning when no artifact remains
&lt;/h3&gt;

&lt;p&gt;Capture the conclusions. Otherwise the next implementation session will reopen the same questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A repeatable ritual
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Write a one-paragraph proposal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Link relevant files, tickets, ADRs, and constraints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ask the agent to inspect before questioning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run one-question-at-a-time grilling.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Record decisions, non-goals, and unresolved risks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Turn the result into the next formal artifact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Only then begin implementation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The time spent here is not delay. It is a way to move uncertainty from code into conversation, where it is cheaper to resolve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Use grill-me when a feature direction feels plausible but has not earned implementation yet. It is particularly valuable when several decisions depend on each other and you want an agent to challenge you rather than cheerlead. Code should be the result of a decision, not the place where the decision first becomes visible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-grill-me" rel="noopener noreferrer"&gt;AI Hero: grill-me&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/mattpocock/skills/tree/main/skills/productivity/grill-me" rel="noopener noreferrer"&gt;mattpocock/skills: grill-me&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>promptengineering</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Four AI Agent Skills That Make Coding Workflows Sharper</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sat, 08 Aug 2026 23:55:15 +0000</pubDate>
      <link>https://dev.to/shubham399/four-ai-agent-skills-that-make-coding-workflows-sharper-3k5</link>
      <guid>https://dev.to/shubham399/four-ai-agent-skills-that-make-coding-workflows-sharper-3k5</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F4159dc16-2571-4bf0-84c9-40044267081e%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F4159dc16-2571-4bf0-84c9-40044267081e%2Fpublic" width="1152" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AI coding agents are often discussed as though they are a single tool: ask for code, receive code. In practice, useful agent work has stages. You need different behavior when the request is unclear, when a design has to survive scrutiny, when implementation is underway, and when work must move into a new session.&lt;/p&gt;

&lt;p&gt;Trying to solve all four stages with one large prompt usually produces a compromise. The agent may be verbose while you need execution, eager while you need questions, or unable to resume work because the important context is buried in chat history.&lt;/p&gt;

&lt;p&gt;This article covers four skills that address those distinct problems: &lt;strong&gt;Caveman&lt;/strong&gt; for concise execution communication, &lt;strong&gt;Superpowers&lt;/strong&gt; for structured development, &lt;strong&gt;grill-me&lt;/strong&gt; for pressure-testing a proposal, and &lt;strong&gt;handoff&lt;/strong&gt; for transferring the live thread to a fresh agent or session.&lt;/p&gt;

&lt;p&gt;They are complementary. The goal is not to add more ceremony to every edit. It is to apply the smallest useful constraint at the moment it prevents the most waste.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four failure modes of AI-assisted development
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The agent starts coding before the work is understood
&lt;/h3&gt;

&lt;p&gt;A request such as “add organization roles” hides decisions about membership, permission scope, migrations, audit trails, errors, and rollout. An agent can produce a plausible patch before any of those choices are explicit.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The agent agrees instead of challenging
&lt;/h3&gt;

&lt;p&gt;Helpful assistants tend to accept a framing. That is dangerous when the framing is a proposal rather than a settled requirement. You need an interview that exposes dependencies and asks what could fail.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The agent talks too much during routine work
&lt;/h3&gt;

&lt;p&gt;Once a direction is approved, long explanations can become friction. During debugging, review follow-ups, and small implementation loops, the useful output is usually a finding, a change, validation, and a risk note.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Context is lost at a session boundary
&lt;/h3&gt;

&lt;p&gt;A new agent with no context repeats discovery. A new agent with a full transcript has to find the current state among outdated ideas and tool logs. Neither is a reliable way to continue a task.&lt;/p&gt;

&lt;p&gt;The four skills map directly to those problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  grill-me: challenge the proposal before code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-grill-me" rel="noopener noreferrer"&gt;grill-me&lt;/a&gt; is a manually invoked skill that interviews you about a plan or design one question at a time. It provides a recommended answer and waits for feedback. It should inspect the codebase when a fact is available there instead of asking the user to reconstruct it.&lt;/p&gt;

&lt;p&gt;Use it when an idea is plausible but not settled. Good examples include a new API contract, a permission model, a caching strategy, a workflow redesign, or a schema change. The point is not to ask endless hypothetical questions. The point is to resolve the decisions that implementation would otherwise make accidentally.&lt;/p&gt;

&lt;p&gt;Start with a concrete proposal. State the goal, constraints, existing artifacts, and desired output. Ask the agent to inspect relevant files first, then grill the plan one decision at a time. Keep a record of the resulting decisions, non-goals, open risks, and the next artifact to create.&lt;/p&gt;

&lt;p&gt;AI Hero notes that Matt Pocock now generally recommends a broader &lt;strong&gt;domain-model&lt;/strong&gt; workflow when coding plans need to be aligned with a project’s vocabulary, context files, and architecture records. grill-me remains valuable as a focused pressure-test when you want sharp questions rather than a complete modeling process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Superpowers: turn decisions into a controlled delivery process
&lt;/h2&gt;

&lt;p&gt;Once the direction is clear, &lt;a href="https://github.com/obra/superpowers" rel="noopener noreferrer"&gt;Superpowers&lt;/a&gt; supplies the broader development workflow. Its repository describes a process where an agent clarifies the actual outcome, develops a specification and design, gets approval, creates an implementation plan, emphasizes true red/green TDD, and proceeds through implementation and review. It also supports subagent-driven development for work that has real task boundaries.&lt;/p&gt;

&lt;p&gt;Use Superpowers for work where the cost of an incorrect assumption is meaningful: new features, public API changes, complex bugs, behavioral refactors, migrations, or security-sensitive changes. It is deliberately heavier than the process you need for a one-line typo.&lt;/p&gt;

&lt;p&gt;The key benefit is the presence of gates. Clarification catches the wrong outcome. Design approval catches a poor shape. A plan makes sequencing visible. Red/green tests make selected behavior executable. Review compares the delivered diff with the approved intent rather than merely checking whether the code looks reasonable.&lt;/p&gt;

&lt;p&gt;For a team-invitation feature, the workflow could establish expiry and revocation rules, define authorization boundaries, choose the token lifecycle, plan storage and migration, write failure-case tests, implement in small slices, and review the result against the original product behavior. Each step reduces the chance that a polished patch is solving an unowned problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveman: keep execution communication compact
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/juliusbrussee/caveman" rel="noopener noreferrer"&gt;Caveman&lt;/a&gt; changes an agent’s output style toward concise, direct language. Its README frames the goal as shrinking the agent’s “mouth,” not its “brain,” and says code, commands, and errors remain exact. The project claims 65% fewer output tokens; treat that as a project claim rather than a universal benchmark.&lt;/p&gt;

&lt;p&gt;Caveman belongs after the important decisions have been made. It is excellent for a known-scope bug, a test failure, a terminal-heavy task, or a review follow-up. Instead of several paragraphs of acknowledgement, ask for: root cause, files changed, validation run, result, and remaining risk.&lt;/p&gt;

&lt;p&gt;Concise mode is not the right choice when nuance is itself part of the deliverable. Do not compress discovery, architecture, security analysis, incident communication, or onboarding into slogans. The rule is simple: reduce filler, never evidence.&lt;/p&gt;

&lt;p&gt;A practical operating pattern is to work in normal detail until the design is approved, then switch to concise execution reports. Require the agent to remain explicit about blockers and assumptions. A short warning is useful; an omitted warning is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  handoff: carry the live thread across a boundary
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-handoff" rel="noopener noreferrer"&gt;handoff&lt;/a&gt; creates a compact document that a fresh agent can use to resume work. It captures what is in flight, why it matters, what should happen next, and suggested skills for continuation. Crucially, it references existing specifications, plans, ADRs, issues, commits, and diffs rather than copying them. It is saved to the operating system’s temporary directory, not the workspace, and is intended to redact secrets and personally identifiable information.&lt;/p&gt;

&lt;p&gt;Use it before ending work, near a context limit, when moving a task between agents, or when deliberately resetting a conversation. Pass a note about the next session’s purpose so the handoff is tailored to the next action.&lt;/p&gt;

&lt;p&gt;A good handoff names the objective, completed work, settled decisions, blockers, validation, canonical references, and one precise next action. “Continue tomorrow” is not enough. “Service-layer authorization is complete; inspect the notification adapter before implementing email delivery; the approved spec is at this path; targeted tests pass” is actionable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The integrated workflow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Frame the request.&lt;/strong&gt; State desired outcome, constraints, and non-goals.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use grill-me.&lt;/strong&gt; Pressure-test linked decisions and inspect repository evidence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Superpowers.&lt;/strong&gt; Convert settled decisions into an approved design, plan, tests, implementation, and review.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Caveman during execution.&lt;/strong&gt; Keep updates compact while preserving findings, commands, test results, and risks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use handoff at transitions.&lt;/strong&gt; Preserve only the resumable thread and point to durable artifacts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Not every task needs every stage. A trivial local change might use none of them. A medium feature may need Superpowers and Caveman. A risky design change may begin with grill-me and end with handoff. The value is in choosing the appropriate control, not in mechanically invoking every tool.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to adopt the stack
&lt;/h2&gt;

&lt;p&gt;Start small. Pick one feature with meaningful ambiguity. Ask the agent to inspect the relevant code and grill the proposal. Require a reviewable design and test plan. Once approved, switch to short execution reports. At the first planned context switch, create a handoff and ask a fresh agent to continue from it.&lt;/p&gt;

&lt;p&gt;Measure outcomes that matter: how many assumptions were caught before code, whether reviewers can explain why a diff exists, how often agents repeat discovery after a switch, and whether concise reporting shortens the human review loop. Do not judge success only by token count or by how impressive a generated plan looks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits and guardrails
&lt;/h2&gt;

&lt;p&gt;These skills do not replace accountable engineering judgment. They cannot decide product strategy, guarantee secure systems, or prove that acceptance criteria reflect real user needs. They help make decisions, evidence, and transitions visible.&lt;/p&gt;

&lt;p&gt;Use source control, reviews, tests, observability, and normal release controls. Keep secrets out of conversations and handoffs. Treat agent recommendations as inputs to a decision, not the decision itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Reliable AI-assisted development is not about making an agent behave the same way all the time. Ask it to question when the plan is uncertain, follow a disciplined workflow when the change is consequential, communicate briefly when execution is clear, and hand off cleanly when context changes. Caveman, Superpowers, grill-me, and handoff each make one part of that system sharper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/juliusbrussee/caveman" rel="noopener noreferrer"&gt;Caveman repository&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/obra/superpowers" rel="noopener noreferrer"&gt;Superpowers repository&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-grill-me" rel="noopener noreferrer"&gt;AI Hero: grill-me&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.aihero.dev/skills-handoff" rel="noopener noreferrer"&gt;AI Hero: handoff&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>productivity</category>
      <category>ai</category>
      <category>llm</category>
      <category>promptengineering</category>
    </item>
    <item>
      <title>The Golden Rule of Payout Systems: Why "Pending" is Never a Failure</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 02 Aug 2026 01:06:48 +0000</pubDate>
      <link>https://dev.to/shubham399/the-golden-rule-of-payout-systems-why-pending-is-never-a-failure-3n6</link>
      <guid>https://dev.to/shubham399/the-golden-rule-of-payout-systems-why-pending-is-never-a-failure-3n6</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fda90968b-1e44-4ff3-a8cb-8ef877fa4bc8.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fda90968b-1e44-4ff3-a8cb-8ef877fa4bc8.png%2Fpublic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A timeout means your application did not receive a final answer. It does not prove that a bank, payment gateway, or payment partner did not receive or process the payout request.&lt;/p&gt;

&lt;p&gt;That distinction is the foundation of safe payout design. When a payout outcome is uncertain, the system should preserve it as &lt;code&gt;pending&lt;/code&gt; until it receives reliable evidence that the payout completed or failed. Treating uncertainty as failure can make funds available for a second transfer while the first transfer is still moving through the payment rail.&lt;/p&gt;

&lt;p&gt;The result is one of the most expensive errors in payments: a duplicate payout.&lt;/p&gt;

&lt;h2&gt;
  
  
  A payout is not a synchronous request
&lt;/h2&gt;

&lt;p&gt;A simple architecture diagram suggests a clean sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A user requests a payout.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your application calls a payment API.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The partner returns success or failure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Your database records the result.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That model is useful for a happy-path demo. It is incomplete for production money movement.&lt;/p&gt;

&lt;p&gt;A payout can cross several systems before the recipient receives funds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
  |
  v
Your application
  |
  v
Payment partner
  |
  v
Banking or payment rail
  |
  v
Recipient bank
  |
  v
Recipient account
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each boundary introduces failure modes that your application cannot observe directly. A network connection may close after the payment partner has accepted the request. A gateway may return an error while its internal worker continues processing. A payment rail may accept an instruction but delay its final result.&lt;/p&gt;

&lt;p&gt;From your application’s perspective, several different events can look exactly the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request sent
  |
  +--&amp;gt; Partner never received it
  |
  +--&amp;gt; Partner received it but did not process it
  |
  +--&amp;gt; Partner accepted it and is still processing it
  |
  +--&amp;gt; Payout completed, but the response never reached you
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A timeout tells you only one thing: the caller has no final response. It says nothing conclusive about whether money movement started.&lt;/p&gt;

&lt;p&gt;That is why a payment API response is not always the final source of truth. The payment flow may continue after the HTTP request has ended.&lt;/p&gt;

&lt;h2&gt;
  
  
  The state that systems often miss
&lt;/h2&gt;

&lt;p&gt;Many applications model a transaction with two terminal states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;success
failure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Payout systems need a third state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unknown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In implementation, that state is often named &lt;code&gt;pending&lt;/code&gt;, &lt;code&gt;processing&lt;/code&gt;, &lt;code&gt;submitted&lt;/code&gt;, or &lt;code&gt;status_unknown&lt;/code&gt;. The label is less important than its behavior.&lt;/p&gt;

&lt;p&gt;An uncertain payout must not behave like a failed payout.&lt;/p&gt;

&lt;p&gt;It should not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Restore funds for a new payout automatically.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Trigger a new payout attempt with a new external reference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be removed from operational records.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be treated as resolved because an API call failed locally.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should remain visible and traceable until the system can establish an outcome.&lt;/p&gt;

&lt;p&gt;Consider a timeout after a payout submission:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Payout initiated
  |
  v
Request sent to partner
  |
  v
Network timeout
  |
  v
Outcome unknown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An unsafe implementation turns that timeout into a failure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Timeout
  |
  v
Mark payout failed
  |
  v
Restore user balance
  |
  v
Allow a new payout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the first request was accepted before the timeout, the user can now create a second payout using money that is already committed to the first one.&lt;/p&gt;

&lt;p&gt;The application has created a duplicate-payment risk because it replaced uncertainty with an unsupported conclusion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why a false failure costs more than a delay
&lt;/h2&gt;

&lt;p&gt;Every payout platform balances two risks.&lt;/p&gt;

&lt;p&gt;The first risk is a delayed payout. The user sees a pending state for longer than expected. Support may need to answer questions. The payout may require reconciliation before it reaches a terminal state.&lt;/p&gt;

&lt;p&gt;The second risk is a false failure. The original payout succeeds, but the platform decides it failed and makes the funds available again.&lt;/p&gt;

&lt;p&gt;These risks are not equivalent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Delayed resolution
&lt;/h3&gt;

&lt;p&gt;When a payout remains pending:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The user may have to wait.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The funds may remain unavailable temporarily.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support volume may increase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Operations may need to review exceptions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those costs matter. They are usually contained and reversible.&lt;/p&gt;

&lt;h3&gt;
  
  
  False failure
&lt;/h3&gt;

&lt;p&gt;When a completed payout is marked failed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The recipient may receive money twice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The ledger can show an incorrect available balance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A refund or retry may create another transfer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finance teams may need manual investigation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recovering funds may depend on recipient cooperation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Audit and reporting records become harder to explain.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A pending payout creates an operational problem. A false failure can create a financial loss.&lt;/p&gt;

&lt;p&gt;This is why payout systems should prefer delayed certainty over an incorrect terminal state. The default should be conservative:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A payout is not failed until an authoritative source confirms final failure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Build a state machine that preserves uncertainty
&lt;/h2&gt;

&lt;p&gt;The rule must exist in code, not only in documentation. A clear state machine prevents workers, webhooks, API handlers, and support tools from applying conflicting decisions.&lt;/p&gt;

&lt;p&gt;A basic model can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;created
  |
  v
submitting
  |
  +--&amp;gt; pending
  |      |
  |      +--&amp;gt; completed
  |      |
  |      +--&amp;gt; failed
  |
  +--&amp;gt; failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important rule is that &lt;code&gt;failed&lt;/code&gt; requires evidence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;created
  -&amp;gt; submitting
  -&amp;gt; pending
  -&amp;gt; completed

submitting + documented final rejection
  -&amp;gt; failed

submitting + timeout or connection reset
  -&amp;gt; pending

pending + confirmed completion
  -&amp;gt; completed

pending + confirmed final rejection
  -&amp;gt; failed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A timeout is not a final rejection. A connection reset is not a final rejection. An unrecognized response is not a final rejection.&lt;/p&gt;

&lt;p&gt;Even an HTTP 5xx response should not automatically mean the payout failed unless the payment partner explicitly documents that response as proof the request was not accepted or processed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Separate transport status from payout status
&lt;/h3&gt;

&lt;p&gt;Transport signals describe communication between systems. Payment states describe the outcome of money movement. They should not be treated as the same thing.&lt;/p&gt;

&lt;p&gt;Signal&lt;/p&gt;

&lt;p&gt;What it proves&lt;/p&gt;

&lt;p&gt;Safe payout state&lt;/p&gt;

&lt;p&gt;Request accepted by partner&lt;/p&gt;

&lt;p&gt;The partner accepted the submission&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pending&lt;/code&gt; or &lt;code&gt;processing&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Documented terminal success&lt;/p&gt;

&lt;p&gt;The partner reports a completed outcome&lt;/p&gt;

&lt;p&gt;&lt;code&gt;completed&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Documented terminal rejection&lt;/p&gt;

&lt;p&gt;The partner reports the payout cannot proceed&lt;/p&gt;

&lt;p&gt;&lt;code&gt;failed&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Timeout or connection reset&lt;/p&gt;

&lt;p&gt;Your application lacks a response&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pending&lt;/code&gt; or &lt;code&gt;status_unknown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Invalid or incomplete response&lt;/p&gt;

&lt;p&gt;The response cannot establish outcome&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pending&lt;/code&gt; or &lt;code&gt;status_unknown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Persist the original payout intent before sending the external request. Keep the fields needed to resolve it later:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Internal payout ID&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Idempotency key&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Attempt ID&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;External reference, when available&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amount and currency&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recipient identifier or beneficiary reference&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Submission time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Latest known external status&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Raw partner response or error details, where appropriate&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not overwrite earlier evidence when a later status check changes the payout state. An audit trail is essential when support or finance teams need to reconstruct what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retries need idempotency
&lt;/h2&gt;

&lt;p&gt;Retries are necessary in distributed systems. They are also one of the main ways duplicate payouts occur.&lt;/p&gt;

&lt;p&gt;The core rule is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Retry an uncertain submission only when the receiving system can identify it as the same payout attempt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That usually means sending a stable idempotency key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Payout intent&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;payout_123&lt;/span&gt;
&lt;span class="na"&gt;Attempt&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;attempt_001&lt;/span&gt;
&lt;span class="na"&gt;Idempotency key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;idem_abc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the request times out, a transport retry should use the same idempotency key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Retry submission
  |
  v
Idempotency key: idem_abc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The receiving system can then return the existing result or continue the original request instead of creating a second payout.&lt;/p&gt;

&lt;p&gt;A new idempotency key changes the meaning of the request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Retry submission
  |
  v
Idempotency key: idem_xyz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the partner’s behavior, that may be interpreted as a brand-new payout.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transport retries and business retries are different
&lt;/h3&gt;

&lt;p&gt;A transport retry attempts to deliver the same request again. It uses the same idempotency key and should refer to the same payout attempt.&lt;/p&gt;

&lt;p&gt;A business retry happens after a confirmed terminal failure. It creates a new payout attempt because the old one is known not to have completed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Payout intent&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;payout_123&lt;/span&gt;

&lt;span class="s"&gt;Attempt &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
  &lt;span class="na"&gt;idempotency key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;idem_abc&lt;/span&gt;
  &lt;span class="na"&gt;final status&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;failed&lt;/span&gt;

&lt;span class="s"&gt;Attempt &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
  &lt;span class="na"&gt;idempotency key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;idem_def&lt;/span&gt;
  &lt;span class="na"&gt;final status&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pending&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The payout intent connects both attempts. The attempt IDs and idempotency keys keep their external submissions distinct.&lt;/p&gt;

&lt;p&gt;Before creating a new business retry, check whether the earlier attempt has a confirmed terminal status. If the old attempt is still unknown, resolve it through a status query or reconciliation process first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;finalFailureConfirmed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="nf"&gt;createNewAttempt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payoutIntent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;outcomeUnknown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="nf"&gt;queryPartnerStatus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;scheduleReconciliation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nf"&gt;continueNormalProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a provider does not support idempotency, automatic retries after an unknown outcome are unsafe. Queue the payout for review or status resolution instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reconciliation resolves what APIs cannot
&lt;/h2&gt;

&lt;p&gt;A synchronous API response captures one point in time. Reconciliation compares your internal records with external payment records after the fact.&lt;/p&gt;

&lt;p&gt;It closes the gap between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What your application thinks happened.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What the payment partner reports.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What the banking or payment rail processed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For payouts that remain pending, reconciliation should compare the details that identify a transaction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Internal and external references&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Amount and currency&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Beneficiary details or recipient reference&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Submission date and time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Latest known partner status&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Settlement or completion records, where the rail provides them&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cadence should match the reporting and settlement behavior of the rail you operate. Some partners provide near-real-time status APIs. Others provide files or reports later. Your system should account for that delay rather than treating it as a failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reconciliation is also an incident control
&lt;/h3&gt;

&lt;p&gt;Reconciliation can reveal problems that request-response processing cannot:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A payout completed after a client timeout.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A payout marked completed without a matching external record.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiple external submissions for the same payout intent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A pending payout that has exceeded its expected resolution time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A mismatch between ledger entries and payment records.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every mismatch should enter an exception queue with an owner, an audit note, and a next action. A queue without ownership is only a list of unresolved risk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operational rules and common mistakes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Rules that keep uncertainty safe
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep funds unavailable for a second payout while the first outcome is unknown.&lt;/strong&gt; The exact accounting treatment depends on the product, but the same balance must not support two active payout attempts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Make state transitions idempotent.&lt;/strong&gt; A repeated webhook, reconciliation record, or worker retry must not apply ledger changes twice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use webhooks as signals, not the only record.&lt;/strong&gt; Webhooks can arrive late, repeat, or fail to arrive. Combine them with status queries and reconciliation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Record every payout attempt.&lt;/strong&gt; Do not delete ambiguous records. They hold the identifiers required to determine the outcome.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Alert on stale pending payouts.&lt;/strong&gt; Define expected resolution windows by rail and route overdue cases to operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Give support useful context.&lt;/strong&gt; Show the payout reference, current state, latest update, last status check, and planned next step.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common mistakes
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Refunding immediately after a timeout
&lt;/h4&gt;

&lt;p&gt;A timeout can occur after the payment partner accepted the request. Restoring the balance immediately can make the funds available for another payout.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;pending&lt;/code&gt; until a final result is confirmed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Retrying with a new idempotency key
&lt;/h4&gt;

&lt;p&gt;A new key may tell the partner to create another payout rather than resume or return the existing one.&lt;/p&gt;

&lt;p&gt;Reuse the original key for a transport retry. Create a new key only for a confirmed new attempt.&lt;/p&gt;

&lt;h4&gt;
  
  
  Treating HTTP success as settlement
&lt;/h4&gt;

&lt;p&gt;An accepted request is not always a completed payout. It may still be queued, screened, rejected later, or waiting on another system.&lt;/p&gt;

&lt;p&gt;Model acceptance and completion as separate states when the payment rail requires it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Treating a webhook as final proof without validation
&lt;/h4&gt;

&lt;p&gt;A webhook should be authenticated, deduplicated, and checked against the payment partner’s documented status model. It may be delayed or repeated.&lt;/p&gt;

&lt;h4&gt;
  
  
  Removing ambiguous records to keep dashboards clean
&lt;/h4&gt;

&lt;p&gt;An unresolved payout is not noise. It is a financial record that requires a final outcome. Keep it visible until reconciliation resolves it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A timeout means the caller lacks an answer. It does not prove a payout failed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Model uncertainty as a durable &lt;code&gt;pending&lt;/code&gt;, &lt;code&gt;processing&lt;/code&gt;, or &lt;code&gt;status_unknown&lt;/code&gt; state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move a payout to &lt;code&gt;failed&lt;/code&gt; only with reliable evidence of a terminal rejection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use stable idempotency keys for transport retries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Treat confirmed failures as new business attempts, with new attempt records.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reconcile non-final payouts against the authoritative records available for the payment rail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prefer a temporary pending state over a duplicate transfer.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Payout systems must be designed for incomplete information. Networks fail, responses disappear, and external systems may continue processing after your request has timed out.&lt;/p&gt;

&lt;p&gt;The safe response is not to guess.&lt;/p&gt;

&lt;p&gt;When the payout outcome cannot be proven, preserve the state as pending, retain the identifiers needed to investigate it, and reconcile it until a final result is available. That choice may delay resolution, but it prevents the system from turning uncertainty into a duplicate payment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Coming Next Week
&lt;/h3&gt;

&lt;p&gt;Next week, I’ll share four &lt;strong&gt;AI agent skills I use to make my coding workflow faster, clearer, and easier to manage.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A practical look at the small systems behind better AI-assisted engineering.&lt;/p&gt;

</description>
      <category>payments</category>
      <category>fintech</category>
      <category>distributedsystems</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Type-Driven Security: Reducing OWASP Risk With Strong Types</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 26 Jul 2026 01:07:06 +0000</pubDate>
      <link>https://dev.to/shubham399/type-driven-security-reducing-owasp-risk-with-strong-types-3b3o</link>
      <guid>https://dev.to/shubham399/type-driven-security-reducing-owasp-risk-with-strong-types-3b3o</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fa5a9e3dd-9253-49a7-ac2e-8f6cab61ae90.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fa5a9e3dd-9253-49a7-ac2e-8f6cab61ae90.png%2Fpublic" alt="Type-driven security" width="1152" height="768"&gt;&lt;/a&gt;TypeScript cannot sanitize HTML, prevent SQL injection, or authorize requests. Those controls must run at runtime.&lt;/p&gt;

&lt;p&gt;It can reduce mistakes inside an application: returning database rows from an API, mixing tenant and user IDs, treating unvalidated JSON as trusted input, or passing vague permission booleans through several layers. The useful pattern is to model trust boundaries explicitly, then keep runtime checks close to the conversion point.&lt;/p&gt;

&lt;h2&gt;
  
  
  What TypeScript can and cannot do for security
&lt;/h2&gt;

&lt;p&gt;A type system helps when a security property concerns the shape, origin, or permitted flow of values inside an application. It can distinguish a public API response from a database row. It can reject a &lt;code&gt;ProjectId&lt;/code&gt; where a &lt;code&gt;UserId&lt;/code&gt; is required. It can require a caller to provide a verified access capability before invoking a sensitive operation.&lt;/p&gt;

&lt;p&gt;This moves some failures from production behavior to compiler feedback. A refactor that returns &lt;code&gt;passwordHash&lt;/code&gt; can fail review because the endpoint must return a public DTO. A tenant-scoped repository method can reject arguments from the wrong domain before tests run.&lt;/p&gt;

&lt;p&gt;TypeScript is erased at runtime. An attacker does not send a server a &lt;code&gt;TenantId&lt;/code&gt;; they send JSON, headers, cookies, URLs, and request bodies. A cast such as &lt;code&gt;value as TenantId&lt;/code&gt; is a developer assertion, not proof that the value is valid.&lt;/p&gt;

&lt;p&gt;Keep the responsibility of each control clear:&lt;/p&gt;

&lt;p&gt;Control&lt;/p&gt;

&lt;p&gt;Problem it addresses&lt;/p&gt;

&lt;p&gt;Static types&lt;/p&gt;

&lt;p&gt;Accidental misuse of values in application code&lt;/p&gt;

&lt;p&gt;Runtime schemas&lt;/p&gt;

&lt;p&gt;Malformed or untrusted external data&lt;/p&gt;

&lt;p&gt;Authentication and authorization&lt;/p&gt;

&lt;p&gt;Whether a caller may perform an action&lt;/p&gt;

&lt;p&gt;Parameterized SQL&lt;/p&gt;

&lt;p&gt;SQL injection through query data&lt;/p&gt;

&lt;p&gt;Output encoding and sanitization&lt;/p&gt;

&lt;p&gt;Browser injection risks such as XSS&lt;/p&gt;

&lt;p&gt;Tests, monitoring, and review&lt;/p&gt;

&lt;p&gt;Regressions outside compiler coverage&lt;/p&gt;

&lt;p&gt;Types are one layer. They work when they describe boundaries enforced elsewhere in the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Model boundaries instead of passing persistence objects around
&lt;/h2&gt;

&lt;p&gt;A common source of accidental exposure is treating an ORM model or database row as the application’s universal user object. A handler loads a user and returns it. Another handler sends the same object to a logger. Later, a migration adds &lt;code&gt;mfaSecret&lt;/code&gt;, &lt;code&gt;passwordHash&lt;/code&gt;, a reset token, support notes, or billing metadata.&lt;/p&gt;

&lt;p&gt;Every serialization site now needs to remember which fields to remove. This deny-list approach fails when the schema changes faster than callers are audited.&lt;/p&gt;

&lt;p&gt;Separate representations based on where data may travel:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Persistence model&lt;/strong&gt;: Matches stored data and may include sensitive fields.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Domain model&lt;/strong&gt;: Represents business concepts and operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Request DTO&lt;/strong&gt;: Represents data accepted by a specific endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Response DTO&lt;/strong&gt;: Represents data intentionally exposed by an endpoint.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Log event&lt;/strong&gt;: Contains fields approved for observability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Not every entity needs each representation. The split has the most value for users, credentials, payments, tenancy, permissions, and objects with internal-only state.&lt;/p&gt;

&lt;p&gt;This database row must never be emitted directly at an HTTP boundary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserRow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;passwordHash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;mfaSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;internalNotes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;passwordResetToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A response DTO states what the endpoint may reveal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PublicUserDto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toPublicUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserRow&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;PublicUserDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important decision is the allow-list. Adding a column to &lt;code&gt;UserRow&lt;/code&gt; does not add it to &lt;code&gt;PublicUserDto&lt;/code&gt;. A new sensitive field remains private until someone intentionally exposes it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCurrentUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toPublicUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The mapper is a review point. Requirements to add &lt;code&gt;displayName&lt;/code&gt; or an avatar URL appear as a small, visible DTO change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Apply the same rule to logs
&lt;/h3&gt;

&lt;p&gt;Logging arbitrary objects is another data-exposure boundary. This code is easy to add while debugging and hard to audit later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;requestBody&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login attempt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may record credentials, tokens, personal data, or columns added by a later migration. Prefer event-shaped logs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LoginAudit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;outcome&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;failure&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;outcome&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;satisfies&lt;/span&gt; &lt;span class="nx"&gt;LoginAudit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;login attempt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For failures, log a stable error code instead of the raw request body. If support workflows need selected input, create a redaction function and test that sensitive fields never appear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LoginFailureAudit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;emailDomain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;invalid_credentials&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;account_locked&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toLoginFailureAudit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;LoginFailureAudit&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reason&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;LoginFailureAudit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;emailDomain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logger redaction settings and ORM serialization hooks are useful backup controls. The log call site should still show the intended data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use DTOs and allow-list serialization
&lt;/h2&gt;

&lt;p&gt;Explicit DTOs prevent another problem: endpoint contracts changing because internal models change. ORM relations, joins, virtual fields, and serializer defaults can expose fields the endpoint author did not intend to publish.&lt;/p&gt;

&lt;p&gt;Keep response mapping near the API layer. Repository methods should return information needed by the domain layer. Controllers or presentation modules should decide the public contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProjectRow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;private&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;team&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;archivedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;billingAccountId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProjectDto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;private&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;team&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toProjectDto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProjectRow&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;ProjectDto&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This repetition records a policy decision. &lt;code&gt;tenantId&lt;/code&gt;, &lt;code&gt;archivedAt&lt;/code&gt;, and &lt;code&gt;billingAccountId&lt;/code&gt; are not part of this API contract.&lt;/p&gt;

&lt;p&gt;For larger APIs, use a presentation layer or serializer module. Avoid generic &lt;code&gt;pick()&lt;/code&gt; helpers that scatter unreviewed field lists across the codebase. Named mappers are easier to search, test, and review.&lt;/p&gt;

&lt;p&gt;Integration tests should verify real JSON responses, not only TypeScript assignments. Types cannot protect an endpoint that bypasses the mapper or an ORM plugin that serializes a model directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;does not expose credential fields&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/v1/me&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;authorization&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;person@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;passwordHash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toHaveProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mfaSecret&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Make identifiers and authority explicit
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;string&lt;/code&gt; does not state whether it identifies a tenant, user, project, invoice, or external provider account. Plain strings are easy to mix, especially in multi-tenant services where a missing tenant constraint can expose another customer’s data.&lt;/p&gt;

&lt;p&gt;Branded types add nominal meaning to TypeScript’s structural type system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tenantBrand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userBrand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;projectBrand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;TenantId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tenantBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProjectId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;projectBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A repository signature can then state its scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SELECT id, email FROM users WHERE tenant_id = $1 AND id = $2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;loadUser(tenantId, projectId)&lt;/code&gt; is a type error. This does not prove the query is correct, but it prevents swapped-argument bugs that can look valid in review.&lt;/p&gt;

&lt;h3&gt;
  
  
  Construct brands only after validation
&lt;/h3&gt;

&lt;p&gt;Do not export a generic conversion helper that converts any string into any brand. It defeats the purpose. Create domain values after validating data at a boundary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;asTenantId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;TenantId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9a-f-&lt;/span&gt;&lt;span class="se"&gt;]{36}&lt;/span&gt;&lt;span class="sr"&gt;$/i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid tenant ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;TenantId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In production, prefer a UUID parser or the identifier format used by the system instead of maintaining a broad regular expression. One small module should own the cast and validate before casting.&lt;/p&gt;

&lt;p&gt;Brands provide no runtime protection. Database constraints, query predicates, row-level security where appropriate, and authorization checks remain necessary.&lt;/p&gt;

&lt;h3&gt;
  
  
  Model verified authority as a capability
&lt;/h3&gt;

&lt;p&gt;A boolean such as &lt;code&gt;canEdit&lt;/code&gt; is easy to pass through several layers, disconnect from its resource, or accidentally derive from untrusted input. A capability type keeps the checked user and resource together.&lt;/p&gt;

&lt;p&gt;Use a private brand so modules importing &lt;code&gt;ProjectEditor&lt;/code&gt; cannot construct it with a normal object literal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// project-access.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;projectEditorBrand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;projectEditor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProjectEditor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="na"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProjectId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;projectEditorBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;requireProjectEditor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProjectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ProjectEditor&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;membership&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;memberships&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;membership&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;membership&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;editor&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ForbiddenError&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;projectEditorBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;renameProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ProjectEditor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request handler first authenticates the caller, then loads trusted membership data, then obtains the capability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;access&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;requireProjectEditor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;renameProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;access&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping the brand private prevents importing modules from constructing this value with an object literal. It does not prevent unsafe casts. Authorization still depends on trusted runtime checks.&lt;/p&gt;

&lt;p&gt;Every relevant request must verify identity, tenant membership, ownership, and current policy. A stale role cache, an incorrectly scoped lookup, or a cast can still create an authorization flaw.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validate data before creating domain values
&lt;/h2&gt;

&lt;p&gt;External values are &lt;code&gt;unknown&lt;/code&gt; until checked. This includes more than HTTP request bodies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;URL parameters and query strings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Headers, cookies, and bearer-token claims&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Environment variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Webhook payloads&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Queue messages and event streams&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Imported CSV files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Third-party API responses&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database data when schemas or migrations are not fully trusted&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TypeScript interfaces do not validate any of these values. A request annotated as &lt;code&gt;CreateProjectInput&lt;/code&gt; still arrives as bytes over the network.&lt;/p&gt;

&lt;p&gt;Use a runtime schema at the boundary, then infer the TypeScript type from that schema. This avoids maintaining two sources of truth.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zod&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CreateProjectSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;private&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;team&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;strict&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CreateProjectInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;infer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;CreateProjectSchema&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;parseCreateProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;CreateProjectInput&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;CreateProjectSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.strict()&lt;/code&gt; is a policy choice. Rejecting unknown keys can catch client bugs and prevent accidental acceptance of fields such as &lt;code&gt;ownerId&lt;/code&gt; or &lt;code&gt;role&lt;/code&gt;. Some public APIs intentionally ignore unknown fields for forward compatibility. Choose and document the behavior.&lt;/p&gt;

&lt;p&gt;Schema validation checks shape and local constraints. It does not enforce business rules that depend on current state. A valid project name may already be in use. A valid &lt;code&gt;projectId&lt;/code&gt; may belong to another tenant. A valid visibility value may be unavailable to the caller’s plan or role.&lt;/p&gt;

&lt;p&gt;A safe request flow is usually:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Authenticate the request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parse and validate untrusted syntax and shape.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Convert validated primitives into domain values where needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Load current resource and membership state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorize the requested operation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apply business rules and write through scoped data access.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Set resource limits separately. A schema that allows a 120-character string does not limit the HTTP request body. Configure body-size limits, upload limits, timeouts, queue payload limits, and rate limits at the relevant layers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep SQL parameterized
&lt;/h2&gt;

&lt;p&gt;Types can clarify repository inputs. They do not make string interpolation safe. SQL injection prevention depends on a database driver or query builder that sends data separately from SQL syntax.&lt;/p&gt;

&lt;p&gt;Use parameter placeholders for values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SELECT id, email FROM users WHERE tenant_id = $1 AND id = $2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not interpolate values into query text, even if the values have branded types or passed a schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Unsafe. The value becomes SQL source text.&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`SELECT * FROM users WHERE id = '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;'`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A UUID validation rule may reduce risk in one path. It does not make interpolation a safe convention. Validation rules change, other inputs may have weaker constraints, and escaping varies by database and driver.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic identifiers need an allow-list
&lt;/h3&gt;

&lt;p&gt;Placeholders generally cannot represent SQL identifiers or keywords. If an endpoint supports sorting by selected columns, construct that fragment only from source-owned literals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortColumns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;created_at&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;SortKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;sortColumns&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;parseSortKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;SortKey&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;createdAt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;createdAt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parseSortKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortColumn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sortColumns&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;sortKey&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s2"&gt;`SELECT id, name FROM projects WHERE tenant_id = $1 ORDER BY &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;sortColumn&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ASC`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request controls a known key, not arbitrary SQL. Apply the same approach to sort direction, table selection, and other dynamic SQL fragments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Treat HTML as a runtime trust boundary
&lt;/h2&gt;

&lt;p&gt;Most rendering should use framework escaping. React escapes strings rendered in JSX by default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The risk appears when an application intentionally renders HTML, such as rich-text content or imported documentation. Raw user HTML must not reach &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; without a reviewed sanitization step.&lt;/p&gt;

&lt;p&gt;A brand makes the distinction visible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sanitizedHtmlBrand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;SanitizedHtml&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;sanitizedHtmlBrand&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The brand alone has no security value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;unsafe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;SanitizedHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That cast only changes the compile-time label. The constructor must run a maintained sanitizer with an application-specific policy.&lt;/p&gt;

&lt;p&gt;For Node.js or server-side rendering, DOMPurify needs a DOM implementation. The following setup uses JSDOM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;createDOMPurify&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dompurify&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;JSDOM&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;jsdom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JSDOM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DOMPurify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createDOMPurify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sanitizeHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;SanitizedHtml&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DOMPurify&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sanitize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;ALLOWED_TAGS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;strong&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;em&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ol&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;code&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pre&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;ALLOWED_ATTR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;href&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;clean&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;SanitizedHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rendering component can require sanitized content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;RichText&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;html&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SanitizedHtml&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;dangerouslySetInnerHTML&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;__html&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep the sanitizer factory server-only when using SSR. Browser bundles can initialize DOMPurify with the browser &lt;code&gt;window&lt;/code&gt; object instead.&lt;/p&gt;

&lt;p&gt;Sanitizer settings are security-sensitive code. Review allowed tags, attributes, URL protocols, CSS handling, embedded content, and server-rendering behavior. Keep DOMPurify and JSDOM updated. If the product does not need arbitrary HTML, use Markdown with a restricted renderer or a structured rich-text format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adopt the pattern without type theater
&lt;/h2&gt;

&lt;p&gt;Security-focused types lose value when every escape hatch is open. &lt;code&gt;any&lt;/code&gt;, broad &lt;code&gt;as&lt;/code&gt; casts, non-null assertions, and generic conversion helpers can erase the protections. Some are necessary at isolated interoperability boundaries. They should not be the usual way to satisfy the compiler.&lt;/p&gt;

&lt;p&gt;Start at boundaries with direct security consequences:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HTTP responses and logs:&lt;/strong&gt; Add response DTOs and named log events for user, payment, admin, and tenancy data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tenant-scoped access:&lt;/strong&gt; Introduce distinct identifiers where swapped values or missing scope could expose another customer’s data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;External inputs:&lt;/strong&gt; Parse request bodies, webhooks, queues, and environment values with runtime schemas.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SQL access:&lt;/strong&gt; Standardize parameterized database APIs and fixed allow-lists for dynamic identifiers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Rich-text rendering:&lt;/strong&gt; Keep raw and sanitized HTML separate, with one narrow sanitization module.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Do not brand every string. A &lt;code&gt;CurrencyCode&lt;/code&gt;, &lt;code&gt;TenantId&lt;/code&gt;, or &lt;code&gt;SanitizedHtml&lt;/code&gt; type justifies its maintenance cost when mixing it with another value can create a real bug. A type used everywhere but protecting nothing adds friction without improving review quality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enforce conventions with tooling and tests
&lt;/h3&gt;

&lt;p&gt;Compiler settings help expose unsafe assumptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"noUncheckedIndexedAccess"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"exactOptionalPropertyTypes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"noImplicitOverride"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These options are not security controls. They reduce ambiguity around &lt;code&gt;undefined&lt;/code&gt;, optional fields, and inheritance, making boundary code easier to reason about.&lt;/p&gt;

&lt;p&gt;Use linting and review rules for dangerous escape hatches. Examples include requiring justification for &lt;code&gt;as&lt;/code&gt;, prohibiting &lt;code&gt;any&lt;/code&gt; outside approved modules, and flagging direct model serialization in controllers. Keep exceptions narrow and documented.&lt;/p&gt;

&lt;p&gt;Test what types cannot prove:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An authenticated user cannot read or modify another tenant’s resource.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sensitive columns do not appear in real HTTP responses or logs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Webhook verification occurs before payload use.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SQL repository methods retain tenant predicates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sanitized rich text rejects unsafe URLs and event handlers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Role changes and revoked memberships take effect as expected.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Integration tests should exercise real middleware, serializers, and database paths. Mapper unit tests help, but integration tests catch endpoints that bypass the mapper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;p&gt;TypeScript improves application security when it represents meaningful boundaries.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Return explicit response DTOs rather than persistence objects. Allow-list fields that may leave the service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create dedicated log events and redaction functions. Do not log arbitrary models or request bodies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use branded identifiers for values that must not be mixed, especially in tenant-scoped code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Model verified authority with capability types, but check authorization from trusted state for every relevant request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parse external values at runtime before treating them as domain input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keep SQL parameterized. Use fixed source-owned allow-lists for dynamic identifiers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Escape HTML by default. Sanitize only when rendering HTML is required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Back type design with integration tests, database constraints, dependency maintenance, monitoring, and security review.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Explicit DTOs, scoped identifiers, narrow authority-bearing APIs, and visible trust conversions make unsafe flows easier to spot during implementation and review. They do not replace runtime defenses. They reduce the chance that ordinary application mistakes bypass those defenses.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/2/narrowing.html" rel="noopener noreferrer"&gt;TypeScript handbook: narrowing&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Injection_Prevention_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP Injection Prevention Cheat Sheet&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP Cross Site Scripting Prevention Cheat Sheet&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html" rel="noopener noreferrer"&gt;OWASP Authorization Cheat Sheet&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://zod.dev/" rel="noopener noreferrer"&gt;Zod documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/cure53/DOMPurify" rel="noopener noreferrer"&gt;DOMPurify documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/jsdom/jsdom" rel="noopener noreferrer"&gt;JSDOM documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>functional</category>
      <category>security</category>
      <category>programming</category>
      <category>typesystems</category>
    </item>
    <item>
      <title>Architecture from Day One: The Practical Guide to Scalable Backend Systems</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 19 Jul 2026 05:20:11 +0000</pubDate>
      <link>https://dev.to/shubham399/architecture-from-day-one-the-practical-guide-to-scalable-backend-systems-4033</link>
      <guid>https://dev.to/shubham399/architecture-from-day-one-the-practical-guide-to-scalable-backend-systems-4033</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Ff98c60dd-4678-42e3-85ae-4b3df3eb8993%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Ff98c60dd-4678-42e3-85ae-4b3df3eb8993%2Fpublic" alt="Scalable backend systems architecture" width="1152" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every backend engineer wants to build systems that scale. But scalability is not a feature to casually bolt onto version 2.0; it is an emergent property of deliberate architectural choices, operational discipline, and evidence from real workloads.&lt;/p&gt;

&lt;p&gt;After years of building and operating high-throughput systems across fintech and aviation, these are the foundational principles and production patterns that matter when moving from thousands of users to millions.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Define “scalable” beyond the buzzwords
&lt;/h2&gt;

&lt;p&gt;Scaling is not simply handling more users. It is maintaining an agreed level of service as load grows: latency, correctness, availability, and cost all matter. A system that works for 1,000 users but collapses at 10,000 has a bottleneck to understand not merely “high load.”&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A practical rule:&lt;/strong&gt; do not judge scalability by raw requests per second alone. Define SLOs, then observe latency at the tail (especially p95 and p99), error rate, and resource saturation as load increases. A flat median can conceal a failing tail.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run load tests that resemble production: realistic request mixes, payloads, connection behavior, data sizes, and downstream dependencies. Track queue depth, CPU, memory, database connections, disk and network saturation alongside user-facing SLOs. The useful question is: &lt;em&gt;at what load do the SLOs, error budget, or cost envelope stop being acceptable?&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Horizontal and vertical scaling: choose with evidence
&lt;/h3&gt;

&lt;p&gt;Vertical scaling is often the fastest, safest next step. A larger database instance, more memory for a cache, or a faster machine can be appropriate until availability, failure-domain, or cost limits make it unattractive. Horizontal scaling adds capacity and resilience, but also coordination, deployment, and consistency complexity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stateless services:&lt;/strong&gt; scale horizontally when demand and redundancy justify it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stateful services:&lt;/strong&gt; first optimize queries, indexes, schema, and instance sizing; use replicas, partitioning, or sharding when measurements show they are needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mixed workloads:&lt;/strong&gt; separate stateful and stateless responsibilities so each can be tuned and scaled independently.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sharding is not a default milestone. It raises operational and application complexity routing, rebalancing, cross-shard queries, and recovery. Introduce it only when observed data volume, write throughput, storage, or availability requirements exceed what simpler approaches can meet.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Stateless design enables elastic application capacity
&lt;/h2&gt;

&lt;p&gt;Keeping request-specific state out of application memory makes ordinary HTTP request handling easier to distribute across instances. Sessions, shared rate-limit counters, and durable workflow state should live in purpose-built external stores rather than a process-local map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad: process-local session state is lost on restart and is not shared.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sessions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sessions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Better: use a shared store with expiry and appropriate availability controls.&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`session:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Statelessness does &lt;strong&gt;not&lt;/strong&gt; mean every instance can be terminated without coordination. WebSockets, server-sent events, streaming responses, in-flight requests, local uploads, and long-running jobs may still be attached to an instance. Use readiness checks and graceful draining: stop accepting new work, allow bounded in-flight work to finish, notify or reconnect long-lived clients when appropriate, and enforce a termination deadline. Sticky routing may still be useful for connection affinity or performance, even if it is not required for ordinary shared-session HTTP traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Multi-layer caching without correctness surprises
&lt;/h2&gt;

&lt;p&gt;Caching is high leverage for read-heavy workloads, but its common risks are stale reads, inconsistent invalidation, cache-key mistakes, eviction behavior, and thundering herds not an automatic guarantee of data corruption or split brain. Treat the backing datastore as the source of truth unless you have explicitly designed a stronger consistency model.&lt;/p&gt;

&lt;p&gt;Cache layer&lt;/p&gt;

&lt;p&gt;Typical targets&lt;/p&gt;

&lt;p&gt;Useful characteristics&lt;/p&gt;

&lt;p&gt;Edge / CDN&lt;/p&gt;

&lt;p&gt;Public, cacheable assets and responses&lt;/p&gt;

&lt;p&gt;Low latency near users; deliberate cache-control and purge strategy&lt;/p&gt;

&lt;p&gt;Application / distributed cache&lt;/p&gt;

&lt;p&gt;Derived objects, sessions, rate limits&lt;/p&gt;

&lt;p&gt;Shared across instances; explicit TTLs, keys, and invalidation&lt;/p&gt;

&lt;p&gt;Database buffer / replicas&lt;/p&gt;

&lt;p&gt;Frequently read database pages and read traffic&lt;/p&gt;

&lt;p&gt;Helps throughput, but replicas can have replication lag&lt;/p&gt;

&lt;p&gt;Choose a pattern deliberately: cache-aside is simple for many reads; write-through can reduce stale-cache windows; write-behind trades simplicity for durability and recovery concerns. Version cache keys when schemas change, invalidate or update entries on writes, and set bounded TTLs even when invalidation exists.&lt;/p&gt;

&lt;p&gt;Protect the origin from a cache stampede. Coalesce concurrent misses with request locking or single-flight, refresh hot entries ahead of expiry where suitable, and add jitter to TTLs so many keys do not expire together. Monitor hit rate, miss rate, eviction, keyspace growth, memory pressure, origin fall-through, refresh failures, and stale-serving behavior. Test failure modes: cache unavailable, invalidation delayed, and an expired hot key under peak traffic.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Data scaling and the CAP theorem
&lt;/h2&gt;

&lt;p&gt;Replication improves read capacity and resilience, but it introduces lag and failover trade-offs. Route reads only where the consistency required by the operation is available; a user who has just written data may need read-your-writes behavior rather than an asynchronous replica.&lt;/p&gt;

&lt;p&gt;CAP is specifically about what a distributed system does &lt;strong&gt;during a network partition&lt;/strong&gt;. When replicas cannot communicate, a system cannot simultaneously guarantee both a single consistent view of data and availability of every request. The design chooses its behavior per operation: reject or block some requests to preserve consistency, or serve potentially stale/divergent data and reconcile later. Outside a partition, latency, quorum configuration, and implementation choices still determine practical behavior.&lt;/p&gt;

&lt;p&gt;Use evidence before introducing partitions or shards: sustained write bottlenecks, storage limits, noisy-neighbor isolation, geographic requirements, or demonstrated availability needs. Define a shard key that spreads traffic, avoid cross-shard transactions where possible, and plan rebalancing, backups, and repair before the first shard is created.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Control overload at every boundary
&lt;/h2&gt;

&lt;p&gt;Load balancers distribute traffic; they do not create infinite capacity. Set connection and concurrency limits at services and dependencies, and propagate deadlines so doomed work does not continue consuming resources. Timeouts should be explicit and shorter than the caller’s remaining deadline.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Backpressure and load shedding:&lt;/strong&gt; bound queues, reject low-priority work early, and return clear overload responses instead of allowing unbounded latency and memory growth.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Retries:&lt;/strong&gt; retry only operations that are safe or idempotent; use exponential backoff with jitter, a maximum attempt count, and a retry budget so an incident does not become a retry storm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Circuit breakers:&lt;/strong&gt; temporarily stop calls to a demonstrably unhealthy dependency, fail fast or use a defined fallback, and probe recovery carefully.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bulkheads:&lt;/strong&gt; isolate thread pools, connection pools, queues, and tenant limits so one slow dependency or customer does not exhaust the whole service.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instrument saturation and queueing, not only successful request rate. Alert on SLO burn, p95/p99 regressions, error rate, exhausted pools, queue age, and dependency health.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Event-driven architecture needs delivery discipline
&lt;/h2&gt;

&lt;p&gt;Event-driven architecture (EDA) can decouple producers from consumers and smooth bursty work, but a broker does not remove distributed-systems failure modes. Most practical consumers operate with at-least-once delivery, so duplicates are normal rather than exceptional.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make handlers &lt;strong&gt;idempotent&lt;/strong&gt; using stable event IDs, deduplication records, or idempotent writes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retry transient failures with bounded exponential backoff and jitter; send poison messages to a monitored dead-letter queue (DLQ) with a replay process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Document ordering scope. Ordering may exist only within a partition/key, and retries can change the apparent order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the transactional outbox pattern when a database write and event publication must not diverge. Persist the intent with the business transaction, then reliably relay it to the broker.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version event schemas and preserve compatibility during producer and consumer rollouts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Measure lag, consumer throughput, retry counts, DLQ volume, duplicate rate, and end-to-end processing latency. These metrics turn “asynchronous” into an observable service commitment.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Build the feedback loop
&lt;/h2&gt;

&lt;p&gt;Capacity planning is a continuous loop: establish SLOs, instrument real workloads, test failure and load scenarios, remove the measured bottleneck, and repeat. Prefer the simplest architecture that meets current reliability and growth needs, while leaving clean seams for the next proven constraint.&lt;/p&gt;

&lt;p&gt;Scalable systems are not the ones with the most components. They are the ones that make trade-offs explicit, degrade predictably under stress, and give operators enough observability to act before customers notice.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>systemdesign</category>
      <category>distributedsystems</category>
      <category>performance</category>
    </item>
    <item>
      <title>How I Built a Personal AI Assistant That Lives in Telegram</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 19 Jul 2026 05:15:49 +0000</pubDate>
      <link>https://dev.to/shubham399/how-i-built-a-personal-ai-assistant-that-lives-in-telegram-1j8o</link>
      <guid>https://dev.to/shubham399/how-i-built-a-personal-ai-assistant-that-lives-in-telegram-1j8o</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fd751c8d2-02d0-4f0c-8c7e-a0eb64532ab0%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fd751c8d2-02d0-4f0c-8c7e-a0eb64532ab0%2Fpublic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wanted a personal AI assistant that lived where I already communicate: Telegram. Not another dashboard to remember, not a browser tab that disappears into the pile, and not a demo that can write clever text but cannot actually help me do things.&lt;/p&gt;

&lt;p&gt;The result is a Telegram bot that can answer questions, remember useful context, schedule reminders, retrieve information, and use connected services through tightly controlled tools. It is intentionally practical rather than magical. The interesting work was not making a model produce text; it was building the systems around it so that tool use, scheduling, failures, and external side effects behave predictably.&lt;/p&gt;

&lt;p&gt;This post explains the architecture, the trade-offs I made, and the safeguards that make a personal assistant useful without turning it into an unattended automation machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Optimized For
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Telegram-first interaction:&lt;/strong&gt; send a message, receive a useful response, and avoid a separate product surface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tool use with boundaries:&lt;/strong&gt; it can retrieve data and invoke integrations, but it cannot freely perform side effects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Durable personal state:&lt;/strong&gt; reminders, notes, job history, and operational records must survive restarts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Simple operations:&lt;/strong&gt; one deployable service, observable logs, backups, and understandable failure modes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Honest scaling limits:&lt;/strong&gt; start with SQLite and one active bot consumer; change the architecture only when the workload requires it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I did not optimize for a fully autonomous agent. For a personal assistant, reliability and control are more valuable than letting a model take unlimited actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;

&lt;p&gt;At a high level, the system has five layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Telegram ingress:&lt;/strong&gt; Telegraf receives updates and normalizes messages into an application request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Conversation orchestration:&lt;/strong&gt; the application loads relevant context, calls OpenAI, and runs a bounded tool-execution loop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tool layer:&lt;/strong&gt; local capabilities such as notes, reminders, weather, and database reads sit behind explicit schemas and policies. Connected third-party services are accessed through Composio.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Durable state:&lt;/strong&gt; SQLite stores sessions, scheduled jobs, execution attempts, idempotency keys, and operational data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Background worker:&lt;/strong&gt; a scheduler claims due jobs, executes them, records the result, and retries safely where appropriate.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Telegram
   │
   ▼
Telegraf handler ──► auth + rate limits ──► assistant orchestrator
                                              │
                         ┌────────────────────┼────────────────────┐
                         ▼                    ▼                    ▼
                     OpenAI API          local tools           Composio
                         │                    │                    │
                         └──────────────► SQLite ◄─────────────────┘
                                               ▲
                                               │
                                        scheduler worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The application is deliberately not a collection of unconstrained agents talking to each other. A single orchestrator owns the request lifecycle. That makes it easier to trace what happened, apply policy consistently, and keep failures from becoming confusing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Bun, Telegraf, OpenAI, Composio, and SQLite
&lt;/h2&gt;

&lt;p&gt;I chose &lt;strong&gt;Bun&lt;/strong&gt; because it gives me a fast TypeScript runtime, package management, and a straightforward deployment target. It keeps the service compact without requiring a complicated build pipeline for a small application. Bun is not the reason the assistant is reliable, though; explicit application boundaries and durable state are.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Telegraf&lt;/strong&gt; is a mature, ergonomic Telegram framework. It handles the Telegram update format well while leaving routing, middleware, and error handling under my control. The bot layer should be boring: validate the sender, acknowledge the message lifecycle, and hand work to the application layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI&lt;/strong&gt; provides the language model and structured tool-calling interface. Tool definitions help the model select an operation and produce arguments in an expected shape. They do not replace runtime validation. The model can still select an inappropriate tool, provide malformed data, or request an action the current user should not be allowed to take.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composio&lt;/strong&gt; is useful for OAuth-backed integrations. Instead of implementing every third-party OAuth flow, token lifecycle, and API wrapper myself, I can use a consistent connection layer for supported external services. That convenience does not eliminate security work: every integration still needs an allowlist, narrow scopes, and separate treatment for read operations versus side effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQLite&lt;/strong&gt; is the right database while the assistant is a single-user or low-volume system. It is portable, inexpensive to operate, and excellent for transactional local state. I use it for data that must be durable: reminders, job execution records, sessions, and idempotency keys. It is not a distributed queue, and it is not the long-term answer for multiple independently writing application instances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polling Instead of Webhooks
&lt;/h2&gt;

&lt;p&gt;I use Telegram long polling rather than webhooks. For a personal deployment, polling avoids exposing a public HTTPS endpoint, certificate management, reverse-proxy setup, and webhook routing. The process asks Telegram for updates, processes them, and advances through the update stream.&lt;/p&gt;

&lt;p&gt;The important caveat is that polling needs exactly one active consumer for a bot token. Running two polling instances at once can create conflicts and unpredictable update handling. If I deploy a replacement instance, I make sure the previous consumer is stopped before the new one begins polling.&lt;/p&gt;

&lt;p&gt;Offset handling matters too. Telegram updates have monotonically increasing identifiers, and the consumer must advance its offset only after it has safely recorded or processed an update. In practice, I also keep a durable update or message idempotency record. That protects against duplicate handling after a process crash, a network timeout, or a restart near the boundary between receiving and committing an update.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SELECT 1 FROM processed_updates WHERE update_id = ?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TelegramUpdate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update_id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;BEGIN IMMEDIATE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;INSERT INTO processed_updates (update_id, processed_at) VALUES (?, ?)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toISOString&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;COMMIT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ROLLBACK&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;processMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact transaction design depends on what is being processed, but the principle is consistent: duplicate delivery is normal in distributed systems, so handlers should be safe to run more than once.&lt;/p&gt;

&lt;h2&gt;
  
  
  From Message to Tool Call to Reply
&lt;/h2&gt;

&lt;p&gt;When a Telegram message arrives, the bot does not immediately hand raw text to a model and execute whatever comes back. The request follows a controlled pipeline:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Verify that the chat and user are permitted to use the assistant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Apply per-user and global rate limits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Load the minimum relevant conversation context and persistent memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Send the model a system policy, the user message, and a small allowlisted tool catalog.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validate every requested tool call against a runtime schema and authorization policy.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute approved tools, append structured results, and continue the model loop within a strict step limit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Persist useful state and send the final answer back through Telegram.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The model loop is intentionally bounded. A tool-capable model can ask for another tool result after receiving the previous one, so a useful assistant needs multiple steps. But it also needs a ceiling to prevent accidental loops, excessive API cost, or an unexpected chain of actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MAX_TOOL_STEPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;MAX_TOOL_STEPS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;responses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MODEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;allowedToolsFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;calls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extractToolCalls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;extractText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;runApprovedTool&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;chatId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toolResultMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tool loop exceeded its configured limit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also set timeouts at the boundaries: Telegram delivery, model requests, database operations, and external integrations. One slow provider should not hold a message handler forever.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Safe Tool Policy
&lt;/h2&gt;

&lt;p&gt;Tool calling is where an assistant becomes useful and where it can become unsafe. My policy is based on capability rather than prompt wording.&lt;/p&gt;

&lt;p&gt;First, tools are &lt;strong&gt;allowlisted&lt;/strong&gt;. The model sees only tools appropriate for the current user and context. A read-only stock quote tool, for example, is fundamentally different from a tool that sends an email or creates a calendar event. I do not expose administrative or infrastructure operations to a general chat flow just because the model could describe them.&lt;/p&gt;

&lt;p&gt;Second, every tool has &lt;strong&gt;runtime argument validation&lt;/strong&gt;. TypeScript types are useful during development, but they disappear at runtime. Tool arguments from a model or an external API are untrusted input. I validate them with a schema library or explicit checks before calling application code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;createReminderSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;object&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;\d{2}&lt;/span&gt;&lt;span class="sr"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\d{2}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;scheduleType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;once&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;daily&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;weekdays&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;weekly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="na"&gt;dayOfWeek&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateReminder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;createReminderSchema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Third, external side effects require &lt;strong&gt;explicit confirmation&lt;/strong&gt;. If the assistant is about to send a message, create an event, modify a document, or perform another consequential action, it prepares a preview and asks the user to confirm. A confirmation is tied to the intended action, expires quickly, and is consumed once. The system should not interpret “yes” from an unrelated later conversation as approval to send something.&lt;/p&gt;

&lt;p&gt;Fourth, side-effecting operations receive an &lt;strong&gt;idempotency key&lt;/strong&gt;. A network failure after a provider accepts a request is ambiguous: retrying without a key can create duplicate events, emails, or tasks. Where a provider supports idempotency, I pass a stable key. Where it does not, I persist an operation record and use provider-specific lookup or reconciliation where possible.&lt;/p&gt;

&lt;p&gt;Finally, OAuth connections are &lt;strong&gt;scoped and isolated&lt;/strong&gt;. I request the narrowest permissions needed, avoid broad account access by default, store connection references rather than casually exposing raw tokens, and make disconnecting an integration straightforward. Secrets belong in the deployment environment or a secret manager, never in source control, logs, prompts, or tool output.&lt;/p&gt;

&lt;h2&gt;
  
  
  SQLite, WAL, and Scheduling Correctness
&lt;/h2&gt;

&lt;p&gt;I run SQLite in write-ahead logging mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;journal_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;foreign_keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;busy_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WAL improves concurrency for this workload because readers can continue while a writer is committing. It does &lt;em&gt;not&lt;/em&gt; turn SQLite into a multi-writer database. There is still one writer at a time, so transactions should be short, indexes should support the scheduler’s queries, and write-heavy background work should not be mixed carelessly with long interactive transactions.&lt;/p&gt;

&lt;p&gt;The scheduler stores jobs in SQLite rather than trusting in-memory timers. A worker periodically finds due jobs, atomically claims one, runs it, and records the outcome. The claim prevents two worker loops from executing the same job simultaneously in the same database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;scheduled_jobs&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'running'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;locked_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lock_token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;scheduled_jobs&lt;/span&gt;
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;run_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;
  &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;run_at&lt;/span&gt;
  &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A worker is not a durable queue merely because it runs in a loop. Durability comes from the database records: pending jobs, attempts, lock timestamps, completion state, and idempotency keys. If the process dies after claiming a job, recovery logic detects stale locks and returns eligible work to the pending state. If the process dies after an external side effect but before recording completion, idempotency and reconciliation logic determine whether it is safe to retry.&lt;/p&gt;

&lt;p&gt;For recurring jobs, I calculate the next run after a successful execution and store times consistently, typically in UTC with the user’s timezone retained for display and recurrence rules. Timezones and daylight-saving transitions deserve dedicated tests; “every day at 9” is more complicated than adding 24 hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Handling, Rate Limits, and Operations
&lt;/h2&gt;

&lt;p&gt;Every integration can fail. Telegram can time out, an OAuth token can be revoked, OpenAI can rate-limit a request, and a third-party API can return malformed data. The assistant should explain failures plainly without leaking secrets or internal stack traces.&lt;/p&gt;

&lt;p&gt;I categorize errors into retryable and non-retryable classes. Network timeouts, temporary 429 responses, and many 5xx errors can be retried with exponential backoff and jitter. Invalid arguments, revoked permissions, and user-denied confirmations should not be blindly retried. Retries have caps, deadlines, and structured logs so a bad provider does not create an infinite background loop.&lt;/p&gt;

&lt;p&gt;Rate limits exist at multiple layers: Telegram message handling, model calls, tool calls, and external APIs. For interactive chat, a per-user token bucket or short rolling window is usually sufficient. I also limit tool-loop depth, tool-call count, payload size, and concurrent outbound requests. These controls protect both cost and availability.&lt;/p&gt;

&lt;p&gt;Deployment is intentionally simple: one application instance, persistent storage mounted outside ephemeral container layers, environment-based configuration, and a process supervisor or platform health checks. Before each deploy, I run migrations in a controlled step and ensure the prior polling consumer has stopped. Health checks verify that the process is alive; readiness checks should also verify that configuration and the database are usable.&lt;/p&gt;

&lt;p&gt;Backups are not optional because SQLite is the system of record. I take regular backups from a consistent SQLite snapshot, retain multiple recovery points, encrypt backups where appropriate, and periodically test restoration. A backup that has never been restored is only a theory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tests, Observability, and Scaling Boundaries
&lt;/h2&gt;

&lt;p&gt;The highest-value tests are not model snapshot tests. They cover authorization decisions, runtime validation, confirmation expiry, idempotency, scheduler claims, stale-lock recovery, timezone behavior, and duplicate Telegram updates. I use mocked provider clients for deterministic unit tests, then run a small number of integration tests against isolated credentials or test resources.&lt;/p&gt;

&lt;p&gt;For observability, each incoming Telegram update receives a correlation id. Logs include the update id, user or chat identifier where safe, request duration, model request id when available, tool name, retry count, and job id. I record metrics for error rates, latency, tool failures, queue age, worker recovery, and rate-limit rejections. I log metadata, not secrets or private message content by default.&lt;/p&gt;

&lt;p&gt;This architecture has clear scaling boundaries. SQLite with one active polling process is excellent for a personal assistant and modest traffic. It becomes a constraint when multiple application instances need concurrent writes, background work grows significantly, or webhook-based horizontal ingress becomes necessary. At that point, I would move durable state to a server database such as Postgres and use a real queue for independently scalable workers. I would not pretend that adding more containers around one SQLite file solves distributed coordination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build Checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a Telegram bot and restrict initial access to known user or chat ids.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start with Telegraf long polling and ensure only one consumer runs at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build a small OpenAI orchestration loop with a maximum number of tool steps.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expose only an explicit, per-user allowlist of tools.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Validate every tool argument at runtime before execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Split read-only tools from side-effecting tools; require preview and confirmation for the latter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use scoped OAuth connections and keep credentials out of code, prompts, and logs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Persist jobs, attempts, locks, and idempotency keys in SQLite.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable WAL, keep write transactions short, and plan around SQLite’s single-writer model.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implement stale-lock recovery and bounded retry behavior for workers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add structured logs, metrics, alerts, and restoration-tested backups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test duplicates, retries, revocations, crashes, and timezones before relying on automation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Repository and Implementation Notes
&lt;/h2&gt;

&lt;p&gt;The implementation evolves, but the core idea remains stable: keep the chat interface simple and put the engineering effort into policy, persistence, and recoverability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/shubhkumar/ai-agent" rel="noopener noreferrer"&gt;View the project source on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;A useful personal AI assistant is less about giving a model unlimited access and more about designing reliable boundaries around it. Telegram provides the interface, OpenAI provides reasoning and language, Composio can provide controlled access to connected services, and SQLite provides a durable foundation for a small deployment.&lt;/p&gt;

&lt;p&gt;The hard parts are familiar engineering problems: authorization, input validation, duplicate delivery, idempotency, retries, recovery, backups, and observability. Solving those deliberately turns an impressive chatbot demo into an assistant I can trust to use every day.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>architecture</category>
      <category>llm</category>
    </item>
    <item>
      <title>What's All Am I Hosting? Full Infrastructure Breakdown</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sat, 04 Jul 2026 18:52:41 +0000</pubDate>
      <link>https://dev.to/shubham399/whats-all-am-i-hosting-full-infrastructure-breakdown-53b8</link>
      <guid>https://dev.to/shubham399/whats-all-am-i-hosting-full-infrastructure-breakdown-53b8</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Ff9ea98b1-39b2-4983-85cd-a1d67470ae45.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Ff9ea98b1-39b2-4983-85cd-a1d67470ae45.png%2Fpublic" width="1152" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every few months, someone asks me how I run my entire online presence for basically nothing. The short answer is: I don't pay for what I can get for free.&lt;/p&gt;

&lt;p&gt;The long answer is this post.&lt;/p&gt;

&lt;p&gt;My entire infrastructure   this site, APIs, email, monitoring, URL shortener, dev tools   runs on free-tier cloud services. Total cost: &lt;strong&gt;$0/month&lt;/strong&gt;. That's less than a single AWS load balancer costs for an hour.&lt;/p&gt;

&lt;p&gt;Here's exactly how it works, why I chose each piece, and what I'd do differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Philosophy: Free Tier First
&lt;/h2&gt;

&lt;p&gt;I have a simple rule: if a service has a generous free tier that covers my use case, I use it. If I outgrow it, I'll pay   but most of us never outgrow free tiers for personal projects.&lt;/p&gt;

&lt;p&gt;The second rule: own the critical paths. DNS is the backbone, so it's on Cloudflare (free, but best-in-class). The main site is on Vercel (free, seamless Next.js deployment). Everything else   databases, email, APIs, monitoring   is a managed service that solves exactly one problem well.&lt;/p&gt;

&lt;h2&gt;
  
  
  DNS: Cloudflare (The Glue That Holds It All Together)
&lt;/h2&gt;

&lt;p&gt;Everything starts with DNS. Cloudflare runs &lt;strong&gt;chan.ns.cloudflare.com&lt;/strong&gt; and &lt;strong&gt;dave.ns.cloudflare.com&lt;/strong&gt; as my authoritative nameservers. Every subdomain in this post is a DNS record that Cloudflare serves for free.&lt;/p&gt;

&lt;p&gt;I use Cloudflare as a pure DNS provider   no CDN proxying on most records except a few redirects (cal, link, mail). The proxied records hide my origin IP and give me free SSL termination, but I keep most records direct because I want full control over the traffic path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Cloudflare over Route53 or self-hosted?&lt;/strong&gt; Cloudflare's free plan includes unlimited DNS queries, DNSSEC, easy API access, and their dashboard is fast. Route53 charges per query. Self-hosting DNS is unnecessary complexity. Free + best-in-class = no contest.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Main Site: Vercel
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;shubhkumar.in&lt;/strong&gt; is hosted on Vercel's free tier. The apex domain uses an A record to 216.198.79.1 (Vercel's anycast IP for apex domains   you can't use a CNAME at the root).&lt;/p&gt;

&lt;p&gt;Vercel's free tier includes 100GB bandwidth, 6000 build minutes, automatic SSL, and edge network distribution. For a Next.js site with ISR, this is more than enough. The site loads fast everywhere because Vercel serves it from their edge network.&lt;/p&gt;

&lt;p&gt;The www subdomain CNAMEs to the root   standard practice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Vercel-Hosted Sites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;cv.shubhkumar.in&lt;/strong&gt;   Resume site. Next.js, deploys from GitHub.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;news.shubhkumar.in&lt;/strong&gt;   Another Next.js site.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hosted Applications: The Free Tier Dream Team
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Render   API Server
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;api.shubhkumar.in&lt;/strong&gt; runs on Render's free tier. It handles contact forms, webhooks, and server-side endpoints the static site can't handle. Render gives 750 hours/month   plenty for a low-traffic personal API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Render over Railway or Fly.io?&lt;/strong&gt; Render has the most generous free tier for this use case. The deploy experience is smooth (Git push → deploy), and SSL is automatic.&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub Pages   Lightweight Pages
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;connect.shubhkumar.in&lt;/strong&gt; is a simple social link aggregator   Linktree-style but self-hosted. Single HTML page, zero cost, instant deploy from a GitHub repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Infrastructure: The Stuff That Runs in the Background
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Tailscale   Private Network Bridge
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;tail.shubhkumar.in&lt;/strong&gt; points to my Tailscale node at &lt;strong&gt;100.115.133.19&lt;/strong&gt;. Tailscale creates a WireGuard mesh across all my devices   laptop, home server, cloud VMs. The DNS record lets me reach my home lab from anywhere using a proper subdomain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real magic:&lt;/strong&gt; Tailscale handles NAT traversal automatically. My home server is behind CGNAT (common with Indian ISPs), and Tailscale punches through without any port forwarding. Free tier: up to 100 devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Databases   Managed, Never Self-Hosted
&lt;/h3&gt;

&lt;p&gt;I use managed databases exclusively. &lt;strong&gt;Supabase&lt;/strong&gt; (Postgres) for anything that needs relational queries and real-time subscriptions. &lt;strong&gt;MongoDB Atlas&lt;/strong&gt; for document storage when the schema is fluid. Both on free tiers or their cheapest paid plans   whichever covers the workload.&lt;/p&gt;

&lt;p&gt;Self-hosting a database is the fastest way to turn a weekend into an ops nightmare. Backups, replication, patches, disk space   all someone else's problem. The managed premium is worth every rupee.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fly.io   Lightweight Apps
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;track.shubhkumar.in&lt;/strong&gt; hosts WakaAPI (self-hosted WakaTime stats) on Fly.io. Free allowance covers the tiny resource usage   3 shared-CPU VMs with 256MB RAM, 3GB storage, 160GB outbound.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cal.com   Scheduling
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;cal.shubhkumar.in&lt;/strong&gt;   Cloudflare-proxied redirect to Cal.com. Self-hosting a calendar scheduler isn't worth my weekend.&lt;/p&gt;

&lt;h3&gt;
  
  
  Better Uptime   Monitoring
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;status.shubhkumar.in&lt;/strong&gt;   Free tier. Monitors all endpoints, notifies on Slack. 10 monitors with 3-minute checks and a public status page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools and Redirects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Dub.co   URL Shortener
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;go.shubhkumar.in&lt;/strong&gt; runs on Dub.co's free tier. Short, memorable links   &lt;em&gt;go.shubhkumar.in/github&lt;/em&gt;, etc. Open-source, great API, free tier includes custom domains and basic analytics.&lt;/p&gt;

&lt;p&gt;Used to run YOURLS on a VPS. Moving to Dub.co saved maintenance and gave better analytics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple Redirects
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;link.shubhkumar.in&lt;/strong&gt; and &lt;strong&gt;mail.shubhkumar.in&lt;/strong&gt; are Cloudflare-proxied redirects using 192.0.2.1 (Cloudflare's placeholder IP). No server needed   Cloudflare page rules handle the redirects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Email Infrastructure: The Hardest Part
&lt;/h2&gt;

&lt;p&gt;Email is the hardest thing on a personal domain. I run three services for different purposes because each solves a specific problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zoho Mail   Primary
&lt;/h3&gt;

&lt;p&gt;Primary email on Zoho's free plan   5 mailboxes with 5GB each, custom domain, IMAP/SMTP, calendar. The last remaining free tier for professional email on a custom domain after Outlook killed their free custom domain offering and Google Workspace charges $6/user/month.&lt;/p&gt;

&lt;p&gt;MX: mx.zoho.com (priority 10), mx2/3 as fallbacks. DMARC set to &lt;strong&gt;reject&lt;/strong&gt; with Cloudflare reporting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trade-off:&lt;/strong&gt; Zoho's spam filtering is decent but not Gmail-level. For free, acceptable.&lt;/p&gt;

&lt;h3&gt;
  
  
  SimpleLogin   Email Aliases
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;simple.shubhkumar.in&lt;/strong&gt;   Creates aliases that forward to my primary inbox. If a service sells my email, I delete the alias. Free tier: 15 aliases, PGP encryption, open-source.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resend   Transactional + Broadcast Emails
&lt;/h3&gt;

&lt;p&gt;I use &lt;strong&gt;Resend&lt;/strong&gt; for all outgoing emails   transactional notifications, broadcast newsletters, and contact form submissions. It handles everything through a single clean API with good deliverability out of the box.&lt;/p&gt;

&lt;p&gt;Resend's free tier includes 100 emails/day, which covers my low-volume needs. DKIM and SPF configured through forms.shubhkumar.in for proper authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security and Verification Records
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DMARC (reject)&lt;/strong&gt;   Strictest policy. Reports to Cloudflare's DMARC reporting. Caught email spoofing attempts at least twice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BIMI&lt;/strong&gt;   Shows my logo next to authenticated emails. Mostly vanity but looks professional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keybase&lt;/strong&gt;   Domain ownership proof.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Google Search Console&lt;/strong&gt;   Site ownership for search analytics.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I Don't Use (And Why)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Kubernetes.&lt;/strong&gt; Everything fits in docker-compose or managed platforms. K8s is operational overkill for one person.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No AWS.&lt;/strong&gt; Pricing model punishes hobbyists. One misconfigured resource = surprise bill. I use SES for email only when necessary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No self-hosted CI.&lt;/strong&gt; GitHub Actions is free for public repos.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Databases:&lt;/strong&gt; Managed   Supabase (Postgres), MongoDB Atlas, etc. Free tiers + cheapest plans cover everything. Self-hosting a database is unnecessary ops overhead.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cost Breakdown
&lt;/h2&gt;

&lt;p&gt;Service&lt;/p&gt;

&lt;p&gt;Cost&lt;/p&gt;

&lt;p&gt;What It Runs&lt;/p&gt;

&lt;p&gt;Vercel&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Main site (Next.js) + CV + News&lt;/p&gt;

&lt;p&gt;Cloudflare DNS&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;All DNS records, proxied redirects&lt;/p&gt;

&lt;p&gt;Render&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;API server&lt;/p&gt;

&lt;p&gt;GitHub Pages&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Link aggregator&lt;/p&gt;

&lt;p&gt;Fly.io&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;WakaAPI instance&lt;/p&gt;

&lt;p&gt;Better Uptime&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;10 monitors, status page&lt;/p&gt;

&lt;p&gt;Dub.co&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;URL shortener&lt;/p&gt;

&lt;p&gt;Zoho Mail&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Primary email, custom domain&lt;/p&gt;

&lt;p&gt;SimpleLogin&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;15 email aliases&lt;/p&gt;

&lt;p&gt;Resend&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Transactional + broadcast emails&lt;/p&gt;

&lt;p&gt;Tailscale&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Mesh VPN, 100 devices&lt;/p&gt;

&lt;p&gt;Supabase&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Managed Postgres (free tier)&lt;/p&gt;

&lt;p&gt;MongoDB Atlas&lt;/p&gt;

&lt;p&gt;$0&lt;/p&gt;

&lt;p&gt;Managed MongoDB (free tier)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;$0/mo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;~15 services, one domain, full infra&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;You don't need to self-host everything.&lt;/strong&gt; Used to run my own email server, Git server, CI   huge time sink for zero benefit. Managed services let you focus on what matters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;DNS TTL matters.&lt;/strong&gt; Low TTL (1–300s) on frequently changed records. High TTL (86400) on stable records for faster lookups.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Email deliverability is a second job.&lt;/strong&gt; DKIM + SPF + DMARC + BIMI + reverse DNS + feedback loops takes a full day to set up. Get it right once, don't touch it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Free tiers are designed to hook you.&lt;/strong&gt; That's fine as long as you understand the migration cost before you're locked in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Monitoring is not optional.&lt;/strong&gt; Better Uptime caught three outages I wouldn't have noticed until someone emailed me.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Reality
&lt;/h2&gt;

&lt;p&gt;Running your own infrastructure is a trade-off   setup time and occasional debugging for complete control and zero ongoing cost. For me, it's worth it.&lt;/p&gt;

&lt;p&gt;But I also know when to stop. I don't self-host email. I don't run a Docker registry. I don't build custom dashboards. The services I chose handle those well enough that my time is better spent building on top of them.&lt;/p&gt;

</description>
      <category>infrastructure</category>
      <category>architecture</category>
      <category>webdev</category>
      <category>devops</category>
    </item>
    <item>
      <title>Building My Personal Website From Scratch: Tech Stack, Architecture, and Lessons Learned</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sat, 27 Jun 2026 19:12:59 +0000</pubDate>
      <link>https://dev.to/shubham399/building-my-personal-website-from-scratch-tech-stack-architecture-and-lessons-learned-1h04</link>
      <guid>https://dev.to/shubham399/building-my-personal-website-from-scratch-tech-stack-architecture-and-lessons-learned-1h04</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fd4c3d0f2-ed81-44ed-a086-41ebb083e8df%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fd4c3d0f2-ed81-44ed-a086-41ebb083e8df%2Fpublic"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A personal website is more than just a portfolio it's a playground for experimenting with architecture, performance, and production-ready engineering.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ask ten software engineers what their personal website is for, and you'll probably hear the same answer: &lt;em&gt;"It's my portfolio."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While that's true, I wanted mine to be something more.&lt;/p&gt;

&lt;p&gt;I wanted &lt;a href="http://shubhkumar.in" rel="noopener noreferrer"&gt;&lt;strong&gt;shubhkumar.in&lt;/strong&gt;&lt;/a&gt; to be a platform that could grow with me a place to showcase projects, host my CV, publish technical blogs, and experiment with ideas before applying them in production systems.&lt;/p&gt;

&lt;p&gt;Rather than using a static template or website builder, I decided to build everything from scratch. My goal wasn't to use the most technologies possible; it was to create a clean architecture that was fast, maintainable, and easy to extend.&lt;/p&gt;

&lt;p&gt;Today, the website consists of two main parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;Next.js&lt;/strong&gt; frontend deployed on &lt;strong&gt;Vercel&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A &lt;strong&gt;Node.js + Express&lt;/strong&gt; backend deployed on &lt;strong&gt;Render&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Behind the scenes, &lt;strong&gt;MongoDB&lt;/strong&gt; stores dynamic content, while &lt;strong&gt;Redis&lt;/strong&gt; speeds up API responses through caching.&lt;/p&gt;

&lt;p&gt;It sounds like a fairly standard stack and in many ways, it is. But the interesting part wasn't choosing the technologies. It was designing how they work together.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Build It From Scratch?
&lt;/h1&gt;

&lt;p&gt;There are countless templates and portfolio generators available today. They look great, take minutes to deploy, and require almost no maintenance.&lt;/p&gt;

&lt;p&gt;So why spend time building everything yourself?&lt;/p&gt;

&lt;p&gt;For me, the answer was simple.&lt;/p&gt;

&lt;p&gt;I wanted complete control.&lt;/p&gt;

&lt;p&gt;Not just over the design, but over the architecture.&lt;/p&gt;

&lt;p&gt;I wanted a backend that wasn't tightly coupled to a frontend. I wanted my content to live in one place instead of being duplicated across pages. Most importantly, I wanted a project that reflected how I build software professionally.&lt;/p&gt;

&lt;p&gt;Every new feature became an opportunity to solve a real engineering problem instead of simply adding another section to a webpage.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Tech Stack
&lt;/h1&gt;

&lt;p&gt;I deliberately kept the stack simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Next.js&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tailwind CSS&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hosted on Vercel&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next.js gives me everything I need for a modern website:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Server Components&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Static rendering&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Excellent SEO&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fast routing&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built-in image optimization&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Incremental Static Regeneration (ISR)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tailwind CSS keeps styling consistent without maintaining a large CSS codebase.&lt;/p&gt;

&lt;p&gt;Deploying to Vercel makes the frontend almost effortless. Every push automatically builds and deploys the latest version.&lt;/p&gt;




&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;Instead of relying on Next.js API routes, I built a dedicated backend using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Express&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hosted on Render&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This backend acts as the single source of truth for all dynamic content.&lt;/p&gt;

&lt;p&gt;Whether it's:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;portfolio information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;experience&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;projects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;resume data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;future APIs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;everything is served from one backend.&lt;/p&gt;

&lt;p&gt;Keeping the backend independent means it can later power:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a mobile app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;an admin dashboard&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CLI tools&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;browser extensions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;or any future frontend&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;without changing business logic.&lt;/p&gt;




&lt;h2&gt;
  
  
  Database
&lt;/h2&gt;

&lt;p&gt;Dynamic content is stored in &lt;strong&gt;MongoDB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Documents include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Profile information&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Experience&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Skills&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Projects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Portfolio data&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interestingly, &lt;strong&gt;blog posts are not stored in MongoDB&lt;/strong&gt;. They are maintained separately, allowing the website content and blog content to evolve independently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Caching
&lt;/h2&gt;

&lt;p&gt;To reduce unnecessary database queries, the Express API caches responses in Redis.&lt;/p&gt;

&lt;p&gt;The flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client
   │
   ▼
Express API
   │
   ▼
Redis
   │
Cache Hit?
   │
 ┌─┴─────────────┐
 │               │
Yes             No
 │               │
 ▼               ▼
Return      MongoDB
Response        │
                ▼
          Store in Redis
                │
                ▼
          Return Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most requests never reach MongoDB.&lt;/p&gt;

&lt;p&gt;This keeps API responses fast while reducing database load.&lt;/p&gt;




&lt;h1&gt;
  
  
  Overall Architecture
&lt;/h1&gt;

&lt;p&gt;At a high level, the system looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 +----------------------+
                 |      Next.js         |
                 |   Hosted on Vercel   |
                 +----------+-----------+
                            |
                            |
                     HTTP Requests
                            |
                            ▼
                +----------------------+
                |   Express Backend    |
                |   Hosted on Render   |
                +----------+-----------+
                           |
               +-----------+-----------+
               |                       |
               ▼                       ▼
        Redis Cache             MongoDB Atlas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although there are multiple services, each one has a single responsibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Next.js renders pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Express serves business logic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis caches responses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;MongoDB stores data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping responsibilities separate makes the system easier to reason about and easier to extend.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why I Didn't Use Next.js API Routes
&lt;/h1&gt;

&lt;p&gt;This was probably the architectural decision that influenced the project the most.&lt;/p&gt;

&lt;p&gt;Many Next.js applications place all backend logic directly inside API routes.&lt;/p&gt;

&lt;p&gt;There's absolutely nothing wrong with that approach.&lt;/p&gt;

&lt;p&gt;But I wanted something reusable.&lt;/p&gt;

&lt;p&gt;By separating the backend, the frontend becomes just another client.&lt;/p&gt;

&lt;p&gt;Tomorrow, if I decide to build:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;an Android app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;an iOS app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a desktop application&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;another website&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;they can all consume the exact same API.&lt;/p&gt;

&lt;p&gt;No duplicated logic.&lt;/p&gt;

&lt;p&gt;No duplicated validation.&lt;/p&gt;

&lt;p&gt;No duplicated database queries.&lt;/p&gt;

&lt;p&gt;Everything lives in one place.&lt;/p&gt;




&lt;h1&gt;
  
  
  Making the Backend the Single Source of Truth
&lt;/h1&gt;

&lt;p&gt;The homepage.&lt;/p&gt;

&lt;p&gt;The portfolio.&lt;/p&gt;

&lt;p&gt;The CV.&lt;/p&gt;

&lt;p&gt;Future applications.&lt;/p&gt;

&lt;p&gt;All of them consume the same backend.&lt;/p&gt;

&lt;p&gt;Instead of every page maintaining its own copy of data, everything originates from one API.&lt;/p&gt;

&lt;p&gt;Updating my experience in MongoDB automatically updates every place where it's displayed.&lt;/p&gt;

&lt;p&gt;This significantly reduces maintenance and prevents data from going out of sync.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Unexpected Problem: Cache Invalidation
&lt;/h1&gt;

&lt;p&gt;The most interesting problem wasn't building the website.&lt;/p&gt;

&lt;p&gt;It was keeping it fresh.&lt;/p&gt;

&lt;p&gt;Initially, everything looked perfect.&lt;/p&gt;

&lt;p&gt;MongoDB stored the latest content.&lt;/p&gt;

&lt;p&gt;Redis cached API responses.&lt;/p&gt;

&lt;p&gt;Next.js generated static pages.&lt;/p&gt;

&lt;p&gt;Performance was excellent.&lt;/p&gt;

&lt;p&gt;Yet something strange happened.&lt;/p&gt;

&lt;p&gt;Whenever I updated content, users didn't always see the changes immediately.&lt;/p&gt;

&lt;p&gt;Sometimes it took several seconds.&lt;/p&gt;

&lt;p&gt;Sometimes much longer.&lt;/p&gt;

&lt;p&gt;At first, I assumed Redis was serving stale data.&lt;/p&gt;

&lt;p&gt;After debugging for a while, I realized Redis wasn't the problem at all.&lt;/p&gt;

&lt;p&gt;The real issue was that there were &lt;strong&gt;two completely independent caching layers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The API cache and the frontend cache.&lt;/p&gt;

&lt;p&gt;The API could already have fresh data while Next.js continued serving previously generated pages.&lt;/p&gt;

&lt;p&gt;Everything was technically working exactly as intended.&lt;/p&gt;

&lt;p&gt;The architecture, however, wasn't.&lt;/p&gt;




&lt;h1&gt;
  
  
  Solving It with Event-Driven Revalidation
&lt;/h1&gt;

&lt;p&gt;Instead of waiting for caches to expire naturally, I switched to an event-driven approach.&lt;/p&gt;

&lt;p&gt;Whenever content changes, the following sequence happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Admin API

      │

      ▼

Update MongoDB

      │

      ▼

Trigger Next.js Revalidation

      │

      ▼

Flush Redis Cache

      │

      ▼

Users receive fresh content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means content updates propagate almost immediately without waiting for cache expiration.&lt;/p&gt;

&lt;p&gt;The important lesson here is that &lt;strong&gt;caching is only half the problem.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The other half is knowing exactly &lt;strong&gt;when to invalidate that cache.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Designing a reliable invalidation strategy is often harder than adding caching in the first place.&lt;/p&gt;




&lt;h1&gt;
  
  
  Hosting Strategy
&lt;/h1&gt;

&lt;p&gt;Keeping the frontend and backend separate also simplified deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend
&lt;/h2&gt;

&lt;p&gt;The Next.js application is deployed on &lt;strong&gt;Vercel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Benefits include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Automatic deployments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preview environments&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Global CDN&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimized image delivery&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;The Express server runs independently on &lt;strong&gt;Render&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Separating deployments means I can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;deploy backend fixes without rebuilding the frontend&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;deploy UI updates without touching backend services&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;scale each independently in the future&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Lessons Learned
&lt;/h1&gt;

&lt;p&gt;Building this website taught me several lessons that extend far beyond personal projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Simplicity scales
&lt;/h2&gt;

&lt;p&gt;A small, well-structured architecture is easier to maintain than an unnecessarily complex one.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Separate responsibilities
&lt;/h2&gt;

&lt;p&gt;Frontend rendering, backend logic, caching, and persistence all have different jobs.&lt;/p&gt;

&lt;p&gt;Keeping those responsibilities isolated makes the system easier to evolve.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Build reusable APIs
&lt;/h2&gt;

&lt;p&gt;The backend shouldn't exist solely for one website.&lt;/p&gt;

&lt;p&gt;Treating it as a standalone service opens the door for future applications without additional work.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Cache invalidation deserves as much attention as caching
&lt;/h2&gt;

&lt;p&gt;Adding Redis is easy.&lt;/p&gt;

&lt;p&gt;Designing when and how cached data should be refreshed is where the real engineering begins.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Personal projects are the best place to experiment
&lt;/h2&gt;

&lt;p&gt;Production systems often have strict requirements.&lt;/p&gt;

&lt;p&gt;Personal projects provide the freedom to test ideas, refine architectures, and learn from mistakes.&lt;/p&gt;

&lt;p&gt;Many of the lessons learned while building this website are directly applicable to larger production systems.&lt;/p&gt;




&lt;h1&gt;
  
  
  What's Next?
&lt;/h1&gt;

&lt;p&gt;The website continues to evolve.&lt;/p&gt;

&lt;p&gt;Some ideas I'm exploring include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A richer admin experience for managing content&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better analytics and monitoring&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search functionality&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AI-powered features&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Additional APIs for future projects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More automation around content publishing&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because the architecture is modular, adding new capabilities doesn't require rewriting existing components.&lt;/p&gt;

&lt;p&gt;That's exactly how I wanted the system to grow.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Building &lt;a href="http://shubhkumar.in" rel="noopener noreferrer"&gt;&lt;strong&gt;shubhkumar.in&lt;/strong&gt;&lt;/a&gt; wasn't about creating another portfolio website.&lt;/p&gt;

&lt;p&gt;It was about building a platform that reflects how I think about software engineering.&lt;/p&gt;

&lt;p&gt;Choosing &lt;strong&gt;Next.js&lt;/strong&gt;, &lt;strong&gt;Tailwind CSS&lt;/strong&gt;, &lt;strong&gt;Node.js&lt;/strong&gt;, &lt;strong&gt;Express&lt;/strong&gt;, &lt;strong&gt;MongoDB&lt;/strong&gt;, and &lt;strong&gt;Redis&lt;/strong&gt; wasn't about following trends. It was about selecting tools that work well together while keeping the architecture clean and maintainable.&lt;/p&gt;

&lt;p&gt;The biggest lesson wasn't learning a new framework or deploying another application.&lt;/p&gt;

&lt;p&gt;It was realizing that good architecture isn't defined by how many technologies you use.&lt;/p&gt;

&lt;p&gt;It's defined by how clearly each piece of the system is responsible for one job and how well those pieces work together.&lt;/p&gt;

&lt;p&gt;If there's one takeaway I'd leave you with, it's this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Treat your personal projects like production systems.&lt;/strong&gt; Not because they need enterprise-scale complexity, but because they're the best place to learn the engineering practices you'll eventually use in production.&lt;/p&gt;

&lt;p&gt;After all, the best portfolio isn't the one with the fanciest animations it's the one that demonstrates how you think as an engineer.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>architecture</category>
      <category>performance</category>
    </item>
    <item>
      <title>Moving from 60s to 6s: Latency Optimization Lessons from Functional Programming</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Wed, 24 Jun 2026 10:36:02 +0000</pubDate>
      <link>https://dev.to/shubham399/moving-from-60s-to-6s-latency-optimization-lessons-from-functional-programming-2l7i</link>
      <guid>https://dev.to/shubham399/moving-from-60s-to-6s-latency-optimization-lessons-from-functional-programming-2l7i</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F0370399c-e484-465a-80ba-3bef2793cd94%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F0370399c-e484-465a-80ba-3bef2793cd94%2Fpublic" width="760" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The broader tech community often views functional programming (FP) as an elegant academic exercise: useful for type systems, formal reasoning, and compiler guarantees, but distant from high-throughput production systems.&lt;/p&gt;

&lt;p&gt;That framing misses something important. FP can improve the way teams model asynchronous work, failures, and state transitions. But it is not a substitute for finding the actual source of latency.&lt;/p&gt;

&lt;p&gt;In a distributed workflow engine, we reduced observed end-to-end completion time from roughly 60 seconds to under 6 seconds for the common successful path. The primary cause was architectural: we removed repeated polling and queue wait from the synchronous execution path. PureScript and Haskell helped us express the resulting asynchronous flow explicitly and safely; they did not, by themselves, create a 10x latency improvement.&lt;/p&gt;

&lt;p&gt;This is the engineering story behind that change, the measurements it supports, and the tradeoffs it introduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the numbers mean
&lt;/h2&gt;

&lt;p&gt;The 60-second and under-6-second figures are observed end-to-end timings for the workflow’s common success path, measured from request acceptance through the final response. They are not a universal service-level objective, a benchmark of every workflow type, or a claim about every percentile under every load level.&lt;/p&gt;

&lt;p&gt;For a production latency claim, the useful view is a before/after comparison with the same workload and scope:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scope:&lt;/strong&gt; identical successful workflow type, including validation, business-rule evaluation, external calls, state update, and response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Load:&lt;/strong&gt; compare equivalent request rate, worker availability, dependency health, and database conditions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Distribution:&lt;/strong&gt; report p50, p95, and p99, along with sample size and the observation window, rather than relying on a single elapsed time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Boundaries:&lt;/strong&gt; state whether timings include client/network time, queue time, retries, and downstream-service time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, the roughly 60-second to under-6-second result should be read as an observed common-path improvement. The main lesson is diagnostic: most of the old latency was scheduled waiting, not useful computation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bottleneck: polling-based workflow execution
&lt;/h2&gt;

&lt;p&gt;The original system used a pull-based worker architecture. Each request moved through sequential stages: validation, business-rule evaluation, external-service interactions, state transitions, and final reconciliation.&lt;/p&gt;

&lt;p&gt;A database-backed work queue coordinated that workflow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A worker completed a step and persisted the updated state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A later worker polled the database for pending work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After discovering the work, it executed the next stage and persisted the result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The cycle continued until the workflow completed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This design had real benefits. It made work durable, gave operators a visible recovery point, and supported retries. It also inserted a scheduling delay between stages. With several sequential transitions, those polling intervals and queue waits accumulated.&lt;/p&gt;

&lt;p&gt;The system was not primarily compute-bound. It was wait-bound.&lt;/p&gt;

&lt;h2&gt;
  
  
  The architectural change: a fast path and a durable path
&lt;/h2&gt;

&lt;p&gt;We separated the responsibilities that had previously been forced through one path:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast path:&lt;/strong&gt; execute the request directly when the workflow can complete synchronously.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Durable path:&lt;/strong&gt; retain queued execution for retries, recovery, delayed work, and cases that cannot safely finish inline.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A representative fast path is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Request → Validation → Business Rules → External Service Call → State Update → Response&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of persisting and waiting for a poll between every step, the request continues through that chain while the required dependencies are available. Removing those handoffs is what removed the dominant source of delay.&lt;/p&gt;

&lt;p&gt;The queue was not eliminated because it was bad. It was moved out of the successful synchronous path because its durability and scheduling semantics were unnecessary for every transition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where functional programming helped
&lt;/h2&gt;

&lt;p&gt;PureScript’s &lt;code&gt;Aff&lt;/code&gt; runtime gave the direct path a useful execution model: non-blocking asynchronous effects, composable sequencing, structured error handling, and cancellation/resource-safety primitives. Similar properties are available in other ecosystems; the language was an enabler, not the performance mechanism.&lt;/p&gt;

&lt;p&gt;FP techniques improved the implementation in three practical ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Explicit effects:&lt;/strong&gt; database writes, remote calls, logging, and retries are visible in the program’s effectful boundary instead of being hidden in incidental control flow.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Typed outcomes:&lt;/strong&gt; expected failure modes can be represented as data, making it clearer which errors respond immediately, retry, or transfer to durable processing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Composable stages:&lt;/strong&gt; validation, rule evaluation, and external interactions can be assembled and tested as small units without scattering callback or exception handling across the workflow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These properties made the fast path easier to reason about and operate. They did not compensate for a queueing architecture that was adding avoidable wait.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure handling and async decoupling tradeoffs
&lt;/h2&gt;

&lt;p&gt;The direct path makes a request faster by coupling more work to the request lifetime. That tradeoff needs to be deliberate.&lt;/p&gt;

&lt;p&gt;Queued workflows decouple producers from consumers, absorb bursts, provide durable handoff points, and allow retry/recovery to proceed after the original request has ended. A synchronous fast path gives up some of that decoupling in exchange for lower latency. It can increase pressure on downstream dependencies, expose callers to longer in-flight work, and require careful timeout, cancellation, idempotency, and backpressure policies.&lt;/p&gt;

&lt;p&gt;The design therefore needs a clear transfer rule. When the direct path encounters a retryable failure, an unavailable dependency, a deadline risk, or work that must outlive the request, it records enough durable state and hands the workflow to the durable path. That handoff must be idempotent so a timeout or ambiguous response does not duplicate an externally visible action.&lt;/p&gt;

&lt;p&gt;Useful safeguards include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;per-stage deadlines and bounded retries;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;idempotency keys for state-changing external calls;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;circuit breaking and concurrency limits around dependencies;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;durable audit records at defined commit points; and&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;separate metrics for direct completion, fallback, retry, and recovery outcomes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Measure the architecture, not just the aggregate
&lt;/h2&gt;

&lt;p&gt;Average latency can hide both queueing and tail failures. Instrument each transition so the system can distinguish queue wait, execution time, persistence time, and downstream-service time. Then compare p50, p95, and p99 before and after the change under matched load.&lt;/p&gt;

&lt;p&gt;For this workflow, the key measurement was not merely that a request became faster. It was that the old path spent substantial time waiting between otherwise short stages. That evidence justified changing the execution model. The percentile view then verifies whether the fast path improves typical and tail behavior, while fallback and error metrics show whether reliability has regressed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The useful conclusion is not that functional programming delivers a fixed latency multiplier. The observed reduction from roughly 60 seconds to under 6 seconds came primarily from removing polling and queue wait from the common successful path.&lt;/p&gt;

&lt;p&gt;Functional programming contributed by making the asynchronous orchestration, failure cases, and fallback boundary easier to express and review. The durable workflow system continued to matter for the work that needs decoupling, retries, and recovery.&lt;/p&gt;

&lt;p&gt;Find the waiting first. Then choose an architecture that removes unnecessary waiting while preserving the operational guarantees the workload actually requires.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>programming</category>
      <category>performance</category>
    </item>
    <item>
      <title>The Disconnected Edge: How We Solved In-Flight Data Sync at 35,000 Feet</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 14 Jun 2026 01:55:15 +0000</pubDate>
      <link>https://dev.to/shubham399/the-disconnected-edge-how-we-solved-in-flight-data-sync-at-35000-feet-4baf</link>
      <guid>https://dev.to/shubham399/the-disconnected-edge-how-we-solved-in-flight-data-sync-at-35000-feet-4baf</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fb805ea06-69a6-46ed-8545-f24e8e98ae9e.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2Fb805ea06-69a6-46ed-8545-f24e8e98ae9e.png%2Fpublic" alt="Offline-first in-flight data synchronization architecture" width="1344" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most application architectures assume the network is available often enough to repair mistakes: fetch fresh configuration, retry an API call, consult a central database, or stream a missing asset from a CDN.&lt;/p&gt;

&lt;p&gt;An aircraft edge system does not get that safety net. For long stretches, the onboard platform must operate as if the backend does not exist. When a connection appears, it may be short, expensive, slow, or interrupted halfway through a transfer.&lt;/p&gt;

&lt;p&gt;That changed how we designed an in-flight entertainment platform. The hard part was not serving movies, games, catalogs, and passenger experiences locally. The hard part was moving the right data between a central backend and intermittently connected aircraft without leaving either side in an ambiguous state.&lt;/p&gt;

&lt;p&gt;This is the offline-first model we used: make the aircraft independently useful, treat synchronization as a deliberate protocol rather than a background convenience, and make every partial failure recoverable. Specific identifiers and thresholds here are illustrative; the design principles are the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  The operating environment: disconnected by default
&lt;/h2&gt;

&lt;p&gt;Each aircraft carried an embedded edge system responsible for the passenger experience: media, applications, digital publications, catalogs, configuration, and operational telemetry. It had local storage and local services, but no guarantee of a usable path to the internet.&lt;/p&gt;

&lt;p&gt;That creates constraints that are easy to underestimate from a cloud-first mindset:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Connectivity windows are intermittent and may end without warning.&lt;/li&gt;
&lt;li&gt;  Bandwidth can be scarce, variable, and costly.&lt;/li&gt;
&lt;li&gt;  Devices must keep serving known-good content while updates are incomplete.&lt;/li&gt;
&lt;li&gt;  A reboot, power event, or failed transfer cannot corrupt the active passenger experience.&lt;/li&gt;
&lt;li&gt;  Central systems must distinguish “not yet uploaded” from “lost forever.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backend remained authoritative for centrally managed content, configuration, and policy. But the aircraft had to be operationally autonomous. Offline mode was not a degraded fallback; it was the normal mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate the data lanes before designing sync
&lt;/h2&gt;

&lt;p&gt;“Sync everything” is not a protocol. Different kinds of data have different correctness rules, sizes, and priorities. We treated them as separate lanes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Content:&lt;/strong&gt; large immutable media and application assets. Correctness means every byte matches a known version.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Configuration:&lt;/strong&gt; smaller, product-sensitive settings. Correctness means a complete, compatible version is activated atomically.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Telemetry and analytics:&lt;/strong&gt; append-only events generated onboard. Correctness means no silent loss and no harmful double counting.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Operational commands:&lt;/strong&gt; centrally issued intent, such as a requested content set. Correctness means explicit acknowledgement and an auditable lifecycle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the lanes were separated, we could give each one a suitable delivery, retry, and conflict model instead of forcing all data through one generic “sync” abstraction. It also gave the scheduler useful priorities: a small compatible configuration update can be more valuable than the next chunk of a large optional asset.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make sync a persisted state machine
&lt;/h2&gt;

&lt;p&gt;A sync worker that only lives in memory is fragile. It forgets why it stopped after a reboot, cannot distinguish a paused transfer from a failed one, and makes recovery dependent on log archaeology. We modeled synchronization as a small persisted state machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;idle → discovering → planning → transferring → verifying
   ↑                                      ↓
   └──── paused / retry_wait ← activating ← ready
                              ↓
                         quarantined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact labels are less important than the invariant: every transition is durable and restartable. The device records the active release, requested release, manifest version, compatibility result, artifact/chunk progress, retry schedule, event-upload cursor, and the reason for any terminal failure. On startup, the worker reads that state and resumes conservatively rather than guessing from partial files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"activeRelease"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"release-2026-05"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"desiredRelease"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"release-2026-06"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"syncState"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"transferring"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"manifestHash"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"verifiedBytes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;734003200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eventCursor"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;817291&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"nextRetryAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-06-13T08:15:00Z"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Persisted state also creates a useful boundary between mechanism and policy. The transfer mechanism knows whether a chunk is verified. Policy decides whether to retry now, defer a nonessential asset, quarantine a release, or ask an operator to investigate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Versioned manifests made content deterministic
&lt;/h2&gt;

&lt;p&gt;For content and application bundles, we avoided asking the device to infer what changed from a directory listing. The backend produced an immutable, versioned manifest describing the desired release: asset identifiers, sizes, hashes, dependencies, compatibility requirements, and configuration version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"releaseId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ife-2026.06.13"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"minPlatformVersion"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"4.8.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"schemaVersion"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"assets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"movies/example.mp4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"sha256"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"bytes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1789423412&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configVersion"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"cfg-2026-06-13-02"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An edge device first fetched the small manifest, checked signature and compatibility, compared it with local state, and then downloaded only missing or changed artifacts. Every artifact was verified before it could be considered ready. A successful HTTP response was not proof of correctness; the expected hash was.&lt;/p&gt;

&lt;p&gt;Large files were transferred in chunks with persisted progress. A connection loss simply paused work at the latest verified boundary. On the next window, the device resumed instead of starting again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each required asset:
  read verified chunk offsets from local state
  request missing ranges
  verify each completed chunk
  verify final asset hash
  mark asset ready only after verification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compatibility is part of correctness. A manifest can demand a platform version, a schema version, or a migration path that the device does not support. In that case the worker must reject the release explicitly and report why; silently applying a newer structure to an older runtime creates harder failures later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Never activate a half-synced release
&lt;/h2&gt;

&lt;p&gt;Downloading a release and serving it are separate operations. New artifacts were staged outside the active content set. Only when the entire manifest was present, verified, compatible, and accompanied by a valid configuration did the device switch the active pointer in one durable operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BEGIN TRANSACTION;
  assert release is complete and verified;
  assert configuration is compatible;
  set active_release = :releaseId;
  record activated_at = :timestamp;
COMMIT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the key recovery property: a failed or interrupted sync leaves the previous known-good release active. The next sync can resume staging. It never turns an incomplete directory into the passenger experience.&lt;/p&gt;

&lt;p&gt;Rollback followed the same model. Retaining a previous verified release made recovery a pointer change rather than an emergency re-download. The device reported both its desired release and active release so the backend could distinguish “download in progress” from “activation failed.”&lt;/p&gt;

&lt;h2&gt;
  
  
  An end-to-end sync window
&lt;/h2&gt;

&lt;p&gt;A typical connection window followed a deliberately boring sequence. First, the device authenticated and sent a compact status summary: software version, active release, desired-release status, storage pressure, event cursor, and the outcome of the prior attempt. The backend replied with policy and the latest eligible manifest.&lt;/p&gt;

&lt;p&gt;Next, the device planned work. It verified that the release was compatible and that there was enough staging capacity. It then prioritized small metadata, critical configuration, and pending commands before large content artifacts. Meanwhile, telemetry upload ran in bounded batches so that a large backlog could not starve a critical update, and a big download could not starve telemetry indefinitely.&lt;/p&gt;

&lt;p&gt;During transfer, every request had a deadline. Completed ranges were recorded only after verification. If the connection disappeared, the worker retained its exact state and backed off until the next viable attempt. If all assets passed verification, activation was a separate, short transaction. Finally, the device sent an acknowledgement containing the active release and any rejected or quarantined items.&lt;/p&gt;

&lt;p&gt;That acknowledgement closed the loop. The backend could not infer success from having served a manifest. A release was operationally complete only after the device reported it active.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upload events as an idempotent append-only stream
&lt;/h2&gt;

&lt;p&gt;Telemetry and passenger analytics are fundamentally different from content. They are produced locally while disconnected, then uploaded later. The edge system persisted events before attempting delivery and assigned each event a stable identity, such as a device id plus a monotonic sequence number or a generated UUID.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"deviceId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"aircraft-edge-42"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sequence"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;817292&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eventId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"01J…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"content_started"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"occurredAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-06-13T08:12:24Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"payload"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"contentId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"movie-123"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The backend accepted batches idempotently. If a connection failed after the server accepted a batch but before the aircraft received acknowledgement, retrying the same events was safe because the backend could recognize identities it had already processed.&lt;/p&gt;

&lt;p&gt;Server acknowledgements advanced a durable cursor only after accepted events were recorded. The edge node retained data until that acknowledgement was committed, then compacted acknowledged records according to retention policy. This produces at-least-once transport with effectively-once accounting when the consumer deduplicates by event identity.&lt;/p&gt;

&lt;p&gt;Not every failure deserves an infinite retry. A temporarily unavailable endpoint may be retried with backoff. A malformed event, unsupported schema, or permanently rejected payload should move to a quarantine or dead-letter record with a reason, preserving evidence without blocking the entire queue. Operators can inspect, repair, discard, or replay it through an explicit process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conflicts need a taxonomy, not a universal CRDT
&lt;/h2&gt;

&lt;p&gt;Offline systems do create conflicts, but “use CRDTs” is not a complete answer. A conflict policy should follow ownership and business semantics.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Server-authoritative data:&lt;/strong&gt; release manifests, pricing policy, and centrally managed configuration should have a single authority. The device applies a compatible version; it does not merge edits.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Append-only facts:&lt;/strong&gt; telemetry is normally merged by deduplication, ordering metadata, and domain-specific aggregation not by overwriting records.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Locally authored mutable state:&lt;/strong&gt; if multiple offline writers can independently edit the same logical object, a CRDT may be appropriate when its merge semantics match the product. Counters, sets, and collaborative metadata are possible examples.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Irreconcilable changes:&lt;/strong&gt; some domains require explicit rejection or human review. Last-write-wins is a policy choice, not a conflict-resolution strategy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CRDTs are valuable because they can guarantee convergence under particular operations and merge rules. They do not create correct business semantics automatically. For most centrally controlled data, immutable versions plus server authority were easier to reason about and audit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bandwidth, storage, and rollout policy
&lt;/h2&gt;

&lt;p&gt;A device did not attempt every task whenever a network interface looked available. It assessed the connection, applied policy, and worked through a priority queue. Small manifests and critical configuration came first; event uploads and large optional artifacts were scheduled according to remaining budget and product priority.&lt;/p&gt;

&lt;p&gt;Transfers used bounded concurrency, request deadlines, exponential backoff with jitter, and persisted state. Retrying blindly can consume an entire connectivity window, so retries were capped and re-evaluated when conditions changed. Each request carried enough identity to make retries observable and safe.&lt;/p&gt;

&lt;p&gt;Storage required policy too. The active release and a known-good rollback release were protected from normal eviction. Incomplete staging content could be removed safely when it no longer matched the desired manifest. Optional, verified assets could be evicted only according to explicit rules, never based on a generic filesystem cleanup that might remove an activation dependency.&lt;/p&gt;

&lt;p&gt;Rollouts were also progressive. A new release should first be eligible for a limited ring of devices, then expand only when acknowledgements, validation results, and operational signals remain healthy. A spike in compatibility failures, verification errors, activation failures, or abnormal backlog growth should pause expansion. The design needs a clear rollback path before a release is ever offered to a wider fleet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security, credentials, and privacy at the edge
&lt;/h2&gt;

&lt;p&gt;An offline device still needs a trust model. Transport encryption protects an available connection, but it does not prove that a downloaded artifact is an approved release. Manifests and artifacts need integrity verification; depending on the threat model, signed manifests provide stronger provenance than hashes fetched from the same channel as the content.&lt;/p&gt;

&lt;p&gt;Devices should authenticate with distinct, scoped, revocable identities. Credential rotation needs a tolerable overlap period: the device validates a replacement credential, records it durably, and retains the old one only long enough to avoid being stranded during an interrupted rotation. Revocation and expiry must be visible in device state and backend operations.&lt;/p&gt;

&lt;p&gt;Data minimization matters as much as transport security. Sync only data the aircraft needs to operate, collect only telemetry necessary for product and operational use, and define retention boundaries for local queues and backend ingestion. Avoid logging access tokens, passenger-sensitive data, or complete event bodies when metadata will do. Backend authorization should scope every request to the device and fleet it is permitted to access.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operations: measure partial progress
&lt;/h2&gt;

&lt;p&gt;Intermittent systems need better observability than a simple “online” metric. We tracked release state, manifest version, bytes remaining, verified chunks, last successful contact, event backlog age, upload acknowledgement cursor, retry counts, active release, failed validation reasons, and storage pressure.&lt;/p&gt;

&lt;p&gt;Those signals made it possible to answer useful operational questions: which devices are running an old release, which are stuck on a corrupt artifact, which have an increasing analytics backlog, and whether a rollout problem is global or isolated to a connection path.&lt;/p&gt;

&lt;p&gt;The health objective is not constant connectivity. It is eventual convergence without harming the active experience: a device should remain useful on a known-good release, make measurable progress when a connection exists, and surface failures that need intervention. Recovery paths were deliberate: stale transfer leases could be reclaimed, incomplete staging directories could be cleaned safely, old verified releases could be rolled back to, and event batches could be replayed without double counting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test the failure modes, not only the protocol
&lt;/h2&gt;

&lt;p&gt;The system needed tests for conditions that are rare in a development environment but routine at the edge:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Connection loss during manifest retrieval, range download, upload, and acknowledgement.&lt;/li&gt;
&lt;li&gt;  Process restart or power loss before and after an active-release pointer change.&lt;/li&gt;
&lt;li&gt;  Duplicate event batches, reordered responses, and stale acknowledgements.&lt;/li&gt;
&lt;li&gt;  Corrupted chunks, incompatible manifests, expired credentials, and full disks.&lt;/li&gt;
&lt;li&gt;  Clock drift, long offline periods, and recovery after a device misses multiple releases.&lt;/li&gt;
&lt;li&gt;  Stale locks, interrupted credential rotation, and release rollback during an active sync.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fault injection was more useful than happy-path tests alone. We simulated slow links, short connections, partial range responses, duplicate uploads, backend timeouts, and storage exhaustion. The invariant under test was simple: after any interruption, the device must either continue serving the last verified release or recover to a well-defined state without inventing completion.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed in our design process
&lt;/h2&gt;

&lt;p&gt;Building for aircraft made us stop treating the network as a dependable dependency. The device had to remain useful with a stale but verified local state. The backend had to accept delayed, duplicate, and partial communication without losing its understanding of the fleet.&lt;/p&gt;

&lt;p&gt;That led to a few durable rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Make offline operation a first-class product requirement.&lt;/li&gt;
&lt;li&gt; Persist sync state; never rely on a process remembering where it was.&lt;/li&gt;
&lt;li&gt; Use immutable manifests and hashes for large artifacts.&lt;/li&gt;
&lt;li&gt; Stage, verify, and atomically activate releases; retain a known-good rollback target.&lt;/li&gt;
&lt;li&gt; Persist outbound events and make server ingestion idempotent.&lt;/li&gt;
&lt;li&gt; Define conflict policy by ownership and domain semantics; use CRDTs only where their merge model fits.&lt;/li&gt;
&lt;li&gt; Prioritize work intentionally across bandwidth and storage constraints.&lt;/li&gt;
&lt;li&gt; Measure partial progress, backlog age, and recovery not only availability.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Offline-first synchronization is not a smaller version of cloud sync. It is a distributed-systems problem where partitions are normal and recovery must be designed before the failure happens.&lt;/p&gt;

&lt;p&gt;For an in-flight edge platform, immutable releases, resumable verified transfer, atomic activation, idempotent event delivery, explicit conflict rules, and clear operational state made the system dependable even when connectivity was not. Those patterns apply far beyond aircraft: ships, retail stores, factories, field devices, and any product that must keep working when the network disappears.&lt;/p&gt;

</description>
      <category>edgecomputing</category>
      <category>distributedsystems</category>
      <category>offlinefirst</category>
    </item>
    <item>
      <title>Turning Your AI Into an Adversarial Security Agent: The SKILLS.md Framework</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 07 Jun 2026 09:18:06 +0000</pubDate>
      <link>https://dev.to/shubham399/turning-your-ai-into-an-adversarial-security-agent-the-skillsmd-framework-2058</link>
      <guid>https://dev.to/shubham399/turning-your-ai-into-an-adversarial-security-agent-the-skillsmd-framework-2058</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F3f9a92b5-1a1a-4de4-ab07-e5b347d1b179.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F3f9a92b5-1a1a-4de4-ab07-e5b347d1b179.png%2Fpublic"&gt;&lt;/a&gt;A continuation of: &lt;a href="https://www.shubhkumar.in/blogs/breaking-to-build-how-ctf-and-bug-bounty-hunting-rewires-system-design" rel="noopener noreferrer"&gt;&lt;em&gt;Breaking to Build: How CTF and Bug Bounty Hunting Rewires System Design&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my previous article, I explored how offensive security permanently changes the way engineers think about systems. Once you've spent enough time exploiting race conditions, bypassing authorization boundaries, abusing SSRF chains, and breaking assumptions hidden deep inside application logic, you stop viewing software as a collection of features.&lt;/p&gt;

&lt;p&gt;You start viewing it as an &lt;strong&gt;attack surface&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That shift fundamentally changes how you design production systems. The problem is that modern software development is no longer purely human-driven. Today, a massive percentage of engineering work happens alongside AI coding assistants. Tools now generate thousands of lines of code faster than most engineers can review them.&lt;/p&gt;

&lt;p&gt;And that introduces a brand new problem.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AI systems are optimized for one thing:&lt;/strong&gt; Generate code that works.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Attackers are optimized for something completely different:&lt;/strong&gt; Find code that breaks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That difference matters. A generated API endpoint might pass every functional test while still exposing a devastating BOLA (Broken Object Level Authorization) vulnerability. A generated webhook handler might function perfectly while allowing SSRF into your internal infrastructure. A generated payment workflow might appear correct while collapsing into a double-spend condition under concurrent execution.&lt;/p&gt;

&lt;p&gt;The code works. The architecture fails. And that is exactly where real-world vulnerabilities are born.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Missing Layer in AI-Assisted Development
&lt;/h2&gt;

&lt;p&gt;Most teams currently treat AI coding agents like extremely fast junior engineers. They give them instructions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;"Build this feature"&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;"Refactor this service"&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;"Create this migration"&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The model responds by optimizing for correctness, readability, and implementation speed. Security is rarely treated as a first-class objective.&lt;/p&gt;

&lt;p&gt;Most AI systems are never explicitly taught to think like attackers. They are taught how software &lt;em&gt;should&lt;/em&gt; behave; they are not taught how software is &lt;em&gt;abused&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;That distinction becomes increasingly dangerous as organizations move toward autonomous code generation, AI-assisted architecture, and agentic development workflows.&lt;/p&gt;

&lt;p&gt;The solution turns out to be surprisingly simple: instead of prompting for features alone, we inject a persistent security reasoning framework directly into the agent's operating context.&lt;/p&gt;

&lt;p&gt;That framework is &lt;strong&gt;SKILLS.md&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is SKILLS.md?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SKILLS.md&lt;/code&gt; is a structured operational framework that teaches an AI agent how to evaluate software through an adversarial lens. It is not a prompt, a simple checklist, or another copy-paste of the OWASP Top 10. It is a behavioral framework that continuously pushes the model to ask &lt;strong&gt;"How would an attacker abuse this?"&lt;/strong&gt; &lt;em&gt;before&lt;/em&gt; it asks &lt;strong&gt;"How do I implement this?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The goal is to transplant the mindset developed through years of CTF competitions, bug bounty hunting, and incident response directly into the AI’s reasoning process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Traditional Security Checklists Fail
&lt;/h3&gt;

&lt;p&gt;Most security documentation focuses on known vulnerability categories (XSS, SQLi, CSRF, SSRF, IDOR). These are important, but attackers rarely think in categories. &lt;strong&gt;They think in assumptions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every vulnerability exists because somebody assumed something was true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;The frontend won't send invalid values.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Only authenticated users can reach this endpoint.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;This request executes once at a time.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Nobody can access that internal network.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bug bounty hunting teaches you something uncomfortable: assumptions are where systems fail. Security is often less about blocking payloads and more about eliminating dangerous assumptions. &lt;code&gt;SKILLS.md&lt;/code&gt; is built entirely around that philosophy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Evolution From Builder To Breaker
&lt;/h2&gt;

&lt;p&gt;Plaintext&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traditional Engineering:
Requirement ──&amp;gt; Implementation ──&amp;gt; Testing ──&amp;gt; Deployment

Security-Oriented Engineering:
Requirement ──&amp;gt; Implementation ──&amp;gt; Abuse Analysis ──&amp;gt; Boundary Verification ──&amp;gt; Concurrency Analysis ──&amp;gt; Deployment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first workflow asks: &lt;em&gt;Does this feature work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The second asks: &lt;em&gt;What happens when somebody intentionally tries to break it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SKILLS.md&lt;/code&gt; forces AI agents into the second mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Specifications: SKILLS.md
&lt;/h2&gt;

&lt;p&gt;Modern AI tools and tools like &lt;strong&gt;Claude Code&lt;/strong&gt; have evolved past static, single-file home directory configurations. They utilize the &lt;strong&gt;Agent Skills Standard&lt;/strong&gt;, which relies on a nested folder footprint (&lt;code&gt;skills/&amp;lt;skill-name&amp;gt;/SKILL.md&lt;/code&gt;) and mandatory &lt;strong&gt;YAML frontmatter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The frontmatter contains semantic metadata. When you start an AI session, the engine scans the &lt;code&gt;description&lt;/code&gt; block to automatically determine &lt;em&gt;when&lt;/em&gt; to pull this skill into context.&lt;/p&gt;

&lt;p&gt;Here is the production-ready implementation file.&lt;/p&gt;

&lt;p&gt;Markdown&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;secure&lt;/span&gt;
&lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Evaluates&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;software&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;architecture&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;and&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;code&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;through&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;an&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;adversarial&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;lens.&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Automatically&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;invokes&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;when&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;generating&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;APIs,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;designing&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;features,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;reviewing&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;code,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;or&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;managing&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;authentication,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;state,&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;and&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;data&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;boundaries."&lt;/span&gt;
&lt;span class="na"&gt;user-invocable&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nn"&gt;---&lt;/span&gt;

&lt;span class="gh"&gt;# Security-First Architecture Skill&lt;/span&gt;

&lt;span class="gs"&gt;**Axiom:**&lt;/span&gt; Inputs malicious. Clients untrusted. Networks hostile. Dependencies may be compromised. Never trust; always verify at execution point.
&lt;span class="p"&gt;
---
&lt;/span&gt;
&lt;span class="gu"&gt;## Domain Controls&lt;/span&gt;

| # | Domain | Attacks | Key Controls | Core Question |
|---|--------|---------|-------------|---------------|
| 1 | &lt;span class="gs"&gt;**State / Race**&lt;/span&gt; | TOCTOU, double-spend, optimistic-lock loss | &lt;span class="sb"&gt;`SELECT FOR UPDATE`&lt;/span&gt;; distributed lock (Redis &lt;span class="sb"&gt;`SET NX PX`&lt;/span&gt;); validate ETag every write | Can same op succeed twice in parallel, or state change between check and act? |
| 2 | &lt;span class="gs"&gt;**AuthZ / BOLA**&lt;/span&gt; | IDOR, BOLA, mass assignment, GraphQL introspection | Ownership check at data layer, not route; allowlist binding (strong_params/Pydantic DTO); disable introspection in prod; tenant-scope every query | What changes if resource ID or any request field changes? Who verified ownership? |
| 3 | &lt;span class="gs"&gt;**SSRF**&lt;/span&gt; | Metadata endpoint, DNS rebind, redirect chain, &lt;span class="sb"&gt;`gopher://`&lt;/span&gt; | Resolve DNS → re-validate IP vs RFC-1918+169.254+fc00 denylist; allowlist domains; HTTPS-only via egress proxy; proxy follows redirects, not app code | Who controls final destination after DNS resolution and redirects? |
| 4 | &lt;span class="gs"&gt;**Path Traversal**&lt;/span&gt; | &lt;span class="sb"&gt;`../../`&lt;/span&gt;, URL-encode, null byte, Zip Slip, symlink | &lt;span class="sb"&gt;`realpath()`&lt;/span&gt;/&lt;span class="sb"&gt;`Path.resolve()`&lt;/span&gt; → verify under root; never concat user input to paths; use UUIDs as storage keys; validate archive entries before extract | Can user-controlled string, after normalization, escape storage boundary? |
| 5 | &lt;span class="gs"&gt;**Blast Radius**&lt;/span&gt; | Lateral movement, over-permissive IAM, credential reuse | Non-root containers, read-only rootfs, &lt;span class="sb"&gt;`--cap-drop ALL`&lt;/span&gt;; per-service least-privilege IAM; separate creds per env/service; mTLS between services | If this service is fully compromised, what else is reachable without new creds? |
| 6 | &lt;span class="gs"&gt;**Fail-Closed**&lt;/span&gt; | Broad catch-continue, feature-flag default-on, null bypass | Default DENY in every auth check, exception block, conditional; feature flags off for security features; &lt;span class="sb"&gt;`default: deny`&lt;/span&gt; in all security-relevant switches | What does system permit on unexpected error, null, or undefined in auth path? |
| 7 | &lt;span class="gs"&gt;**Secrets / Crypto**&lt;/span&gt; | Log leak, &lt;span class="sb"&gt;`alg:none`&lt;/span&gt; JWT, weak HMAC, IV reuse, timing oracle, weak PRNG | Secrets Manager + rotation; CSPRNG only; pin JWT alg server-side; RS256/ES256 cross-service; &lt;span class="sb"&gt;`timingSafeEqual`&lt;/span&gt;; AES-256-GCM unique nonces; ban MD5/SHA-1/DES/RC4 | Can credential be recovered, forged, or brute-forced from token, log, or build output? |
| 8 | &lt;span class="gs"&gt;**Supply Chain**&lt;/span&gt; | Typosquat, dep-confusion, &lt;span class="sb"&gt;`postinstall`&lt;/span&gt; RCE, unpinned deps | Exact-version pin + lockfile; verify signatures/hashes; audit &lt;span class="sb"&gt;`postinstall`&lt;/span&gt; scripts; private registry namespace; &lt;span class="sb"&gt;`npm audit`&lt;/span&gt;/&lt;span class="sb"&gt;`pip-audit`&lt;/span&gt;/&lt;span class="sb"&gt;`cargo audit`&lt;/span&gt; in CI | What third-party code executes in build/runtime that we don't own and audit? |
| 9 | &lt;span class="gs"&gt;**Event Integrity**&lt;/span&gt; | Replay, out-of-order, schema-invalid crash, webhook spoof | Idempotency keys (Redis TTL); strict schema → DLQ on malformed; HMAC-SHA256 webhook sig + 5-min timestamp window; sequence numbers for ordering | Can replaying/reordering an event corrupt state? Is every inbound event cryptographically authenticated? |
| 10 | &lt;span class="gs"&gt;**LLM / AI**&lt;/span&gt; | Prompt injection (direct/indirect), agentic tool abuse, secondary injection | Treat model output as untrusted input; explicit tool-auth gateway per user permission; strict output schema (JSON Schema/Pydantic); human-in-loop for irreversible actions; no ambient creds in agent env | What auth boundaries exist between model output and execution? Can injected content in retrieved data override system instructions? |
| 11 | &lt;span class="gs"&gt;**Injection**&lt;/span&gt; | SQLi, XSS, CMDi, SSTI, XXE, NoSQLi, ReDoS | Parameterized queries; &lt;span class="sb"&gt;`execFile([...])`&lt;/span&gt; not &lt;span class="sb"&gt;`exec(string)`&lt;/span&gt;; context-aware output encoding; disable XML external entities; reject &lt;span class="sb"&gt;`$`&lt;/span&gt;-prefix JSON keys; audit regexes for backtracking; never eval user input in templates | Does any user-controlled string reach an interpreter without structural separation? |
| 12 | &lt;span class="gs"&gt;**AuthN / Session / OAuth**&lt;/span&gt; | Credential stuffing, session fixation, missing server-side invalidation, redirect-URI wildcard, missing PKCE, token-in-URL | CSPRNG session tokens; rotate on login/priv-escalation; server-side invalidation on logout; &lt;span class="sb"&gt;`Secure;HttpOnly;SameSite=Strict`&lt;/span&gt;; Argon2id (64MB/3-iter) or bcrypt≥12; exact-match &lt;span class="sb"&gt;`redirect_uri`&lt;/span&gt;; PKCE for public clients; bind &lt;span class="sb"&gt;`state`&lt;/span&gt; to session; identical error messages + timing | Can attacker reuse, predict, fix, or intercept session/token/auth-code without knowing original secret? |
| 13 | &lt;span class="gs"&gt;**Log Exposure**&lt;/span&gt; | Credential leak, PII capture, log forgery via &lt;span class="sb"&gt;`\n\r`&lt;/span&gt;, stack-trace disclosure | Scrub tokens/JWTs/keys/PAN/SSN/email before every log write; allowlist loggable fields; sanitize &lt;span class="sb"&gt;`\n\r\033`&lt;/span&gt; in user input; disable verbose stack traces in prod responses | If every log line leaked, what sensitive data would be visible? |
| 14 | &lt;span class="gs"&gt;**Deserialization**&lt;/span&gt; | pickle/ObjectInputStream/&lt;span class="sb"&gt;`yaml.load`&lt;/span&gt;/Marshal RCE, gadget chains, DoS via nested structures | Never deserialize untrusted data with native formats; use JSON/Protobuf + schema validation; if unavoidable: allowlist filter + HMAC-sign payload; always &lt;span class="sb"&gt;`yaml.safe_load`&lt;/span&gt; | Does any code path deserialize attacker-influenced data with a class-instantiating deserializer? |
| 15 | &lt;span class="gs"&gt;**Rate Limits / DoS**&lt;/span&gt; | Credential stuffing, ReDoS, zip bomb, billion-laughs, large upload, unbounded pagination | Rate limits per-IP + per-user at gateway and app; tightest on auth/reset/OTP; cap body/upload size at ingress; GraphQL depth+complexity limits; timeouts on all external calls + queries; queue expensive ops; &lt;span class="sb"&gt;`limit ≤ 100`&lt;/span&gt; on pagination; return 429 + &lt;span class="sb"&gt;`Retry-After`&lt;/span&gt; | Can a single actor trigger resource consumption that degrades availability for others? |
| 16 | &lt;span class="gs"&gt;**HTTP Controls**&lt;/span&gt; | CORS credential theft, XSS via missing CSP, clickjacking, MIME-sniff bypass, SSL-strip | Exact-origin CORS allowlist (never reflect &lt;span class="sb"&gt;`Origin`&lt;/span&gt;, never &lt;span class="sb"&gt;`*`&lt;/span&gt; + credentials); HSTS &lt;span class="sb"&gt;`max-age=63072000;includeSubDomains;preload`&lt;/span&gt;; CSP allowlist &lt;span class="sb"&gt;`script-src`&lt;/span&gt;, no &lt;span class="sb"&gt;`unsafe-inline/eval`&lt;/span&gt;; &lt;span class="sb"&gt;`X-Content-Type-Options:nosniff`&lt;/span&gt;; &lt;span class="sb"&gt;`X-Frame-Options:DENY`&lt;/span&gt;; &lt;span class="sb"&gt;`Referrer-Policy:strict-origin-when-cross-origin`&lt;/span&gt;; &lt;span class="sb"&gt;`__Host-`&lt;/span&gt; cookie prefix | Can cross-origin page, framed page, or MIME-sniffed resource exploit browser trust in this origin? |
| 17 | &lt;span class="gs"&gt;**File Upload**&lt;/span&gt; | Zip Slip, polyglot exec, oversized upload, SVG XSS, archive bomb | UUID storage keys (never user filename); separate origin (S3 bucket/CDN subdomain) + &lt;span class="sb"&gt;`Content-Disposition:attachment`&lt;/span&gt;; validate by magic bytes not MIME; enforce size+count at ingress; reject SVG/HTML or sanitize with DOMPurify; cap archive extraction size + entry count | Can an uploaded file execute code, escape storage, or gain application-origin trust? |
| 18 | &lt;span class="gs"&gt;**Info Disclosure**&lt;/span&gt; | Stack traces, version headers, debug endpoints, username enum, timing leak | Opaque error IDs to clients, full detail server-side only; remove &lt;span class="sb"&gt;`Server`&lt;/span&gt;/&lt;span class="sb"&gt;`X-Powered-By`&lt;/span&gt;/&lt;span class="sb"&gt;`X-AspNet-Version`&lt;/span&gt;; disable debug/admin/introspection in prod (fail startup if debug=prod); identical error messages + response times; scan for exposed debug routes | Does any response, header, error, or timing difference reveal internal structure to unauthorized caller? |
| 19 | &lt;span class="gs"&gt;**Build Pipeline**&lt;/span&gt; | Compromised CI, over-permissive build IAM, unsigned images, secrets in logs, fork PR secret leak | Pin CI actions to commit SHAs; secrets only on protected branches; read-only source + write-only artifact IAM for build; sign images (cosign/Sigstore) + verify at deploy; OIDC ephemeral creds (no long-lived keys); branch protection: review + CI + signed commits required | Can compromised dependency update, CI job, or PR introduce malicious code reaching production without human review? |
&lt;span class="p"&gt;
---
&lt;/span&gt;
&lt;span class="gu"&gt;## Verification Protocol (run sequentially on every review/design)&lt;/span&gt;
&lt;span class="p"&gt;
1.&lt;/span&gt; &lt;span class="gs"&gt;**Trust Boundaries**&lt;/span&gt;   map all input sources (HTTP, queues, webhooks, files, env); strip all safety assumptions
&lt;span class="p"&gt;2.&lt;/span&gt; &lt;span class="gs"&gt;**Concurrency**&lt;/span&gt;   find TOCTOU windows; verify DB-level locking on every state mutation
&lt;span class="p"&gt;3.&lt;/span&gt; &lt;span class="gs"&gt;**AuthZ**&lt;/span&gt;   confirm ownership check at data-access layer, not route layer, on every request
&lt;span class="p"&gt;4.&lt;/span&gt; &lt;span class="gs"&gt;**Privileges**&lt;/span&gt;   least-privilege IAM, service accounts, container caps, DB permissions
&lt;span class="p"&gt;5.&lt;/span&gt; &lt;span class="gs"&gt;**Blast Radius**&lt;/span&gt;   full compromise simulation: map reachable services/creds/data without new credentials
&lt;span class="p"&gt;6.&lt;/span&gt; &lt;span class="gs"&gt;**Interpreter Paths**&lt;/span&gt;   trace user input to SQL/shell/template/XML/YAML/pickle; confirm structural separation
&lt;span class="p"&gt;7.&lt;/span&gt; &lt;span class="gs"&gt;**Auth Surface**&lt;/span&gt;   session lifecycle (issue→rotate→invalidate), MFA, OAuth parameter binding, credential storage
&lt;span class="p"&gt;8.&lt;/span&gt; &lt;span class="gs"&gt;**Log Audit**&lt;/span&gt;   confirm no secrets/PII/payment/raw bodies in any log path including error handlers and APM agents
&lt;span class="p"&gt;9.&lt;/span&gt; &lt;span class="gs"&gt;**Rate Controls**&lt;/span&gt;   sensitive endpoints have per-user + per-IP limits; expensive ops queued with concurrency caps
&lt;span class="p"&gt;10.&lt;/span&gt; &lt;span class="gs"&gt;**HTTP Controls**&lt;/span&gt;   CORS allowlist, security headers, CSP, no verbose error leakage on all response paths
&lt;span class="p"&gt;11.&lt;/span&gt; &lt;span class="gs"&gt;**Upload &amp;amp; Deserialization**&lt;/span&gt;   files stored outside app origin with server-generated keys; no native deserializer on untrusted data
&lt;span class="p"&gt;12.&lt;/span&gt; &lt;span class="gs"&gt;**Build Pipeline**&lt;/span&gt;   CI secrets scoped, actions SHA-pinned, images signed and verified at deploy
&lt;span class="gt"&gt;
&amp;gt; **Target:** Code that behaves predictably when an adversary is actively attempting to shatter it.&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installation Guide
&lt;/h2&gt;

&lt;p&gt;To ensure your AI assistant picks up this framework without breaking file path scopes, use the explicit terminal setups below depending on your favorite environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Claude Code
&lt;/h3&gt;

&lt;p&gt;Claude Code evaluates configurations from your global home configuration space (&lt;code&gt;~/.claude&lt;/code&gt;) or local workspaces (&lt;code&gt;.claude&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Installation&lt;/strong&gt; &lt;em&gt;(Applies across all code repositories on your machine without altering git states)&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Bash&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p ~/.claude/skills/security-review
# Save the Markdown block above into this file:
nano ~/.claude/skills/security-review/SKILL.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;**&lt;/span&gt;Project-Specific Installation&lt;span class="k"&gt;**&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;Committed directly into git to enforce security rules across the whole engineering team&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
  mkdir -p .claude/skills/security-review&lt;br&gt;
  nano .claude/skills/security-review/SKILL.md&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### 2\. Cursor (and custom IDEs)

Cursor indexes markdown definitions gracefully via workspace indexing or dedicated custom instructions.

Bash

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
shell&lt;br&gt;
mkdir -p .cursor/skills/security-review&lt;br&gt;
nano .cursor/skills/security-review/SKILL.md&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
_(Alternatively, you can save it as a top-level_ `SKILLS.md` _file in your root workspace)._

### 3\. Orchestrated Agent Frameworks (CrewAI / LangGraph)

For autonomous multi-agent pipelines, pass the file directly as system background data inside your orchestration configuration:

YAML

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
yaml&lt;br&gt;
agent:&lt;br&gt;
  role: Adversarial Security Auditor&lt;br&gt;
  backstory: You analyze architectural code changes strictly through the lens of SKILLS.md rules.&lt;br&gt;
  instructions:&lt;br&gt;
    - Ingest the custom SKILLS.md baseline constraints.&lt;br&gt;
    - Check every generated code route against Concurrency and Trust Boundaries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
## How to Use the Framework

Once installed, you don’t need to repeatedly copy-paste security prompts. The framework leverages both passive and active execution behaviors.

### Method A: Automated Semantic Triggering (Passive Mode)

Because the custom frontmatter contains a deep `description` string, the AI continuously evaluates your inputs. If you type a standard prompt that crosses defensive boundaries, the engine auto-activates the skill behind the scenes.

*   **Your Prompt:** _"Write an endpoint that takes a user's uploaded image URL, downloads it, and processes metadata."_

*   **The AI's Internal Action:** The engine intercepts words like _URL_ and _downloads_. It auto-loads `security-review` from disk, catches the **SSRF / Deterministic Routing** rule, and adds domain validation code before outputting the feature.


### Method B: Manual Slash Invocation (Active Mode)

If you want to explicitly mandate an application review, call the skill directly via standard interface paths.

*   **In Claude Code:** Use the custom command shortcut directly inside your terminal session:

    Bash

    ```


    /security-review Review our new database migration file for potential data isolation vulnerabilities.


    ```


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
plaintext&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In Cursor Composer:&lt;/strong&gt; Force index mapping by targeting the file handle directly inside the chat bar:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Please build out our stripe payment callback router following the criteria defined in @SKILL.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-World Transformations: Before and After
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;SKILLS.md&lt;/code&gt; is active, the agent stops acting like a passive code generator and starts acting like an unyielding architecture reviewer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Payment Balance Deduction
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Without SKILLS.md:&lt;/strong&gt; The user asks for a simple point redemption function. The AI generates a standard &lt;code&gt;SELECT balance&lt;/code&gt; followed by an &lt;code&gt;UPDATE balance&lt;/code&gt; sequence. It looks clean, passes unit tests, but immediately falls to a race condition exploit when a user executes parallel curl requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;With SKILLS.md:&lt;/strong&gt; The agent's internal reasoning detects a state change trigger. It forces the SQL generation to include row-level isolation via &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; or requires a strict &lt;code&gt;Idempotency-Key&lt;/code&gt; header transaction check.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: User-Configured Webhooks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Without SKILLS.md:&lt;/strong&gt; The user prompts the AI to build an outbound webhook engine so users can get alerts. The AI uses a simple Axios/Fetch call passing the target parameter. An attacker signs up, sets their webhook to &lt;code&gt;[http://169.254.169.254/latest/meta-data/](http://169.254.169.254/latest/meta-data/)&lt;/code&gt;, and extracts cloud infrastructure IAM keys.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;With SKILLS.md:&lt;/strong&gt; The agent flags the user-controlled URL routing pattern. It refuses to output the code until it builds an accompanying domain allowlist check, wraps the execution in an isolated egress proxy, or isolates the protocol rules.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Bigger Shift
&lt;/h2&gt;

&lt;p&gt;Today, engineers review AI-generated code. Tomorrow, AI systems will review AI-generated code. Eventually, entire engineering workflows will become completely autonomous.&lt;/p&gt;

&lt;p&gt;When that happens, security can no longer exist as an afterthought or a final manual compliance checklist performed at the tail end of a sprint. It has to become a core property of the AI's internal reasoning loop.&lt;/p&gt;

&lt;p&gt;AI does not automatically inherit security instincts. It inherits whatever mental models we explicitly give it. If you train an AI to think only like an engineer, it will build systems. If you train it to think like an attacker, it will help you build &lt;strong&gt;resilient&lt;/strong&gt; systems.&lt;/p&gt;

&lt;p&gt;The future belongs to the teams that can do both. Secure software is not created by accident; it is forged when someone spends enough time thinking about how it breaks first.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>llm</category>
      <category>promptengineering</category>
    </item>
    <item>
      <title>Breaking to Build: How CTF and Bug Bounty Hunting Rewires System Design</title>
      <dc:creator>Shubham</dc:creator>
      <pubDate>Sun, 31 May 2026 18:50:37 +0000</pubDate>
      <link>https://dev.to/shubham399/breaking-to-build-how-ctf-and-bug-bounty-hunting-rewires-system-design-2j7c</link>
      <guid>https://dev.to/shubham399/breaking-to-build-how-ctf-and-bug-bounty-hunting-rewires-system-design-2j7c</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F9824899b-3ed9-4d61-8e5a-d49568803653.png%2Fpublic" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimagedelivery.net%2FlLmNeOP7HXG0OqaG97wimw%2F95a7ced4-fd82-4716-a6d0-b434f9e2b1f7%2F9824899b-3ed9-4d61-8e5a-d49568803653.png%2Fpublic" alt="Adversarial thinking for secure system design"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Software engineers are trained to create: read a requirement, design the happy path, write the code, pass tests, and ship. Authorized security practice adds a useful second question: &lt;strong&gt;what assumptions does this design make, and how could those assumptions fail?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Capture The Flag exercises and properly scoped vulnerability research are valuable because they train that question. The point is not to treat production systems as puzzles or to chase clever exploits. It is to bring adversarial thinking back into normal engineering work: define trust boundaries, verify authorization, reduce capability, log important decisions, and make unsafe behavior difficult by default.&lt;/p&gt;

&lt;p&gt;That change in perspective has improved how I design APIs, multi-tenant services, background workers, and operational tooling. These are the system-design lessons that stick.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Data has provenance, not automatic trust
&lt;/h2&gt;

&lt;p&gt;A database is durable storage, not a trust boundary. A value may have originated in a user form, webhook, import, support tool, partner API, or an earlier application bug. Reading it back from PostgreSQL does not make it safe for every new context.&lt;/p&gt;

&lt;p&gt;The key distinction is between a value’s &lt;strong&gt;provenance&lt;/strong&gt; and the &lt;strong&gt;sink&lt;/strong&gt; where it will be used. A display name may be valid as text in a JSON response but unsafe if inserted as HTML, used in a shell command, or interpolated into a query. The correct defense depends on that destination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;untrusted input → validate shape at ingress → store as data
                                             ↓
                                 encode or parameterize at each sink
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For HTML, prefer framework auto-escaping and avoid raw HTML rendering. If rich user-authored HTML is a genuine product requirement, sanitize it with a maintained, context-appropriate library and keep the allowed elements and attributes intentionally small. For SQL, use parameterized queries. For commands, avoid shell interpolation; use APIs that pass arguments separately whenever possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Good: the database driver keeps data separate from SQL syntax.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;select id, name from projects where tenant_id = $1 and id = $2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Prefer UI/framework escaping for text. Avoid injecting user data as raw HTML.&lt;/span&gt;
&lt;span class="nf"&gt;renderText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;“Sanitize everything” is too vague to be a reliable rule. Validate structure at ingress, preserve data as data, and encode or parameterize it for the specific output context. That approach also makes code review more precise: reviewers can ask, “what is the sink, and what protection matches it?”&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Design authorization into every object lookup
&lt;/h2&gt;

&lt;p&gt;Broken object-level authorization (often called IDOR or BOLA) is rarely fixed by choosing less guessable identifiers. UUIDs can reduce accidental enumeration, but they do not establish ownership or permission. The server must decide whether the authenticated principal may perform the requested action on the requested object.&lt;/p&gt;

&lt;p&gt;I prefer authorization-first repository methods over a generic “load by id” followed by an easily forgotten policy check. The method shape makes tenant and actor context mandatory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Actor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;roles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;action&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadProjectForRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Actor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;oneOrNone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`select id, name, status
       from projects
      where id = $1
        and tenant_id = $2`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getProject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Actor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;loadProjectForRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NotFoundError&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;project&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Production policies are often more nuanced than tenant membership: roles, project membership, delegated access, ownership, resource state, and action type may all matter. Centralize that policy where possible, test both allow and deny cases, and return responses that do not reveal unnecessary details about resources outside the caller’s scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Turn ambient power into explicit capabilities
&lt;/h2&gt;

&lt;p&gt;Many high-impact failures begin with code that has more authority than it needs. A request handler that receives an all-powerful database client, a broad cloud credential, or a general-purpose administrative service can accidentally cross boundaries.&lt;/p&gt;

&lt;p&gt;Instead, expose narrow operations that express intent: &lt;code&gt;createInvoiceForTenant&lt;/code&gt;, &lt;code&gt;readOwnProfile&lt;/code&gt;, or &lt;code&gt;queueReportForProject&lt;/code&gt;. Pass an actor or capability explicitly. This makes authorization visible in APIs and reduces the chance that a helper silently acts with system-wide privileges.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;RefundCapability&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;actorId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;maxAmountCents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;requestRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;capability&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RefundCapability&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;invoiceId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;amountCents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountCents&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;capability&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;maxAmountCents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ForbiddenError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Refund amount exceeds approval limit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Scope the invoice lookup and record the decision before side effects.&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;refunds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;capability&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;invoiceId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amountCents&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a replacement for database permissions, network controls, or policy enforcement. It is an application-level design habit that makes least privilege easier to preserve.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Threat model before implementation details harden
&lt;/h2&gt;

&lt;p&gt;A short threat-modeling session early in a feature often prevents expensive rewrites later. It does not need a large ceremony. For a new endpoint or worker, I ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  What assets, actions, and data are worth protecting?&lt;/li&gt;
&lt;li&gt;  Who are the actors, and which identities or credentials can they present?&lt;/li&gt;
&lt;li&gt;  Where does untrusted data enter, cross a boundary, or reach a sensitive sink?&lt;/li&gt;
&lt;li&gt;  Which actions are irreversible, high-value, or easy to replay?&lt;/li&gt;
&lt;li&gt;  What evidence will help us detect and investigate misuse?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The output can be a few bullets in the pull request. The value is forcing assumptions into the open before they become interfaces that every later service depends on.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Secure defaults beat security checklists
&lt;/h2&gt;

&lt;p&gt;Security controls are more reliable when the safe path is the easiest path. Require tenant context in repository queries. Make internal fields opt-in in serializers. Set conservative timeouts. Deny access unless a policy grants it. Keep development diagnostics away from production responses.&lt;/p&gt;

&lt;p&gt;Good defaults also apply to sessions and credentials: short-lived tokens where practical, scoped service accounts, rotation procedures, explicit expiry, and revocation paths. A system should be able to remove access quickly without requiring a redesign during an incident.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Observability is a security control
&lt;/h2&gt;

&lt;p&gt;A policy decision that cannot be investigated is difficult to trust. Audit events should make important actions reconstructable: who initiated an operation, which resource and tenant were involved, which policy path allowed it, and whether the action succeeded.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;audit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;project.read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;actorId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tenantId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;projectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;outcome&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;allowed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;requestId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logs must be useful without becoming a second data leak. Avoid raw credentials, authentication headers, full payment details, and sensitive message bodies. Apply retention controls, restrict access to audit stores, and alert on meaningful patterns such as repeated authorization denials or unusual bursts of privileged actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Test defenses in authorized environments
&lt;/h2&gt;

&lt;p&gt;Security testing belongs in environments where you have permission: local labs, CTFs, intentionally vulnerable training applications, staging systems, or programs with an explicit scope and safe-harbor policy. The goal is to validate defenses without creating risk for other people’s data or services.&lt;/p&gt;

&lt;p&gt;For product code, turn lessons into repeatable tests. Add authorization tests for cross-tenant access, property tests for parsers, integration tests for output encoding and serialization, and regression tests for issues already fixed. Treat a discovered weakness as an opportunity to improve a class of failures, not merely patch one endpoint.&lt;/p&gt;

&lt;p&gt;If you find a vulnerability in someone else’s system, follow the program’s reporting policy or use coordinated disclosure. Share the minimum evidence needed for reproduction, protect sensitive data, and give maintainers time to investigate and remediate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Engineering checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; Document data provenance and trust boundaries for new flows.&lt;/li&gt;
&lt;li&gt; Validate at ingress; encode or parameterize for each output sink.&lt;/li&gt;
&lt;li&gt; Scope every object lookup to the authenticated actor, tenant, and action.&lt;/li&gt;
&lt;li&gt; Expose narrow capabilities instead of ambient administrative access.&lt;/li&gt;
&lt;li&gt; Threat-model irreversible or high-value workflows before implementation.&lt;/li&gt;
&lt;li&gt; Use deny-by-default policies, short-lived credentials, and explicit revocation.&lt;/li&gt;
&lt;li&gt; Log security-relevant decisions without logging secrets or unnecessary personal data.&lt;/li&gt;
&lt;li&gt; Test authorization boundaries and regressions in authorized environments only.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Authorized offensive-security practice is valuable because it makes system design assumptions visible. It teaches that storage is not trust, identifiers are not authorization, and a working happy path is not the same as a safe system.&lt;/p&gt;

&lt;p&gt;The best outcome is not becoming suspicious of every line of code. It is building systems where boundaries, permissions, and recovery paths are explicit enough that ordinary engineering work stays secure by default.&lt;/p&gt;

</description>
      <category>security</category>
      <category>ctf</category>
      <category>bugbounty</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
