← Back to Pull requests

fix(dev): give services local ports in dependency order #350

Closed
unknown closed this pull request without merging on Oct 08, 2026 21:19 UTC
View on GitHub
unknown unknown commented on Oct 08, 2026 18:20 UTC
Author

Linked issue

n/a. Found by the nightly getting-started check, which follows the Composer getting-started guide from an empty directory.

Summary

The getting-started guide builds two services: quotes, and a public gateway that calls it. The guide says quotes runs on port 3000 and gateway on 3001, and has the reader run curl localhost:3001 to get a quote through the gateway. In about half of fresh prisma dev module.ts runs, the ports come out the other way round:

{"kind":"endpoint","name":"gateway","url":"http://localhost:3000",...}
{"kind":"endpoint","name":"quotes","url":"http://localhost:3001",...}

$ curl localhost:3001
{"error":"Unauthorized: missing or invalid service key"}

The curl reaches quotes, which only accepts calls from other services, so the reader gets a 401 instead of a quote. This happened in 3 of 7 fresh runs on Composer 0.29.0.

With this PR, prisma dev gives services their local ports in dependency order: a service gets its port before the services that call it. Services that don't depend on each other go in the order the module declares them. On a fresh start, each service gets the lowest free port from 3000 up, so quotes always gets 3000 and gateway 3001. A warm start keeps the ports services already have, as before.

Why the ports swapped

Locally, each service becomes one Prisma.App resource. When Alchemy applies an App, its provider asks the Compute emulator for the service's port, and the emulator hands a new service the lowest free port from 3000 up.

The App resources depend only on the project, not on each other, so Alchemy applies them at the same time. Whichever request reaches the emulator first gets 3000. The gateway does depend on quotes, but on the quotes App's identity, and that doesn't order the two port requests.

Reserving the ports first, in graph order

Before Alchemy runs, the Prisma Cloud extension's emulators hook now reserves a port for each service, one at a time, in graph.nodes order. Core already sorts that list with dependencies first and ties in declaration order. The work is done by a new reserveServicePorts(container, serviceAppNames) in @internal/local-target, which calls the emulator's ensureService once per service and waits for each.

When Alchemy then applies the App resources in parallel, each provider asks for a port that already exists and gets it back unchanged. A failed reservation names the service it was for.

Making --fresh start from 3000 as well

The new rule is that a fresh start gives the lowest free ports from 3000. prisma dev --fresh is the user's way to get a fresh start, and in two cases it didn't give those ports:

1. The Compute emulator was stopped, for example after a reboot. Teardown skipped the delete for a stopped emulator. The emulator then restarted and reloaded the old ports, including swapped ones. Teardown now starts the Compute emulator before deleting the app, and a failed delete stops the run. Postgres and buckets keep their old behaviour: they are skipped when they aren't running.
2. --fresh ran shortly after the previous run. The emulator chooses ports with the get-port package, which holds every port it hands out for up to 30 seconds. The freed ports were still held, so the app got 3002 and 3003. The emulator now releases those held ports when it deletes an app. Other apps' ports stay safe, because the emulator's saved state already keeps them out of every allocation.

One name for each App and its reservation

A reservation only helps if it uses the same name the App resource uses for that service. A new serviceAppName(address) in the Prisma Cloud extension now produces both, so they can't drift apart.

Docs

The rule is now in docs/design/10-domains/local-dev.md, docs/guides/running-locally.md and the core-concepts skill, worded the same in the guide and the skill. In core, the emulators hook's description now says it also prepares emulator state for each node before Alchemy runs.

Testing performed

  • local-target/src/__tests__/compute-port-order.test.ts (new; a real Compute emulator in its own directory for each test):

  • - each App provider gets the port reserved for its service, even when the providers run at the same time in reverse order, 10 times over;
    - an app that already has ports keeps them;
    - a dotted address maps to one emulator service;
    - a failed reservation names the service.
    The first test fails without the reservation.
  • target/src/__tests__/local-target-emulators.test.ts (new): for a graph shaped like the getting-started app, the hook reserves quotes then gateway, after the Compute emulator is up. When nothing orders the services, it reserves them in declaration order, and only services.

  • target/src/__tests__/local-target-teardown.test.ts (new): teardown starts the Compute emulator before deleting the app.

  • dev-emulators/src/__tests__/compute-multi-app.test.ts (new case): an app that is deleted and then reserved again gets back the ports it freed. Before the fix it got 3002 instead of 3000.

  • Package tests: @internal/local-target 17 pass, @internal/prisma-cloud 395 pass, compute-multi-app and compute-deployment 23 pass.

  • pnpm turbo run typecheck for @internal/local-target, @internal/prisma-cloud, @internal/dev-emulators and @internal/core; biome check on the changed files; pnpm lint:deps.
  • Checklist

  • All commits are signed off (git commit -s) per the [DCO](../CONTRIBUTING.md#developer-certificate-of-origin-dco). The DCO status check will block merge if any commit is missing a Signed-off-by: trailer.

  • I read [CONTRIBUTING.md](../CONTRIBUTING.md) and the change is scoped to one logical concern.

  • The PR title is a conventional commit (feat, fix, chore, docs, refactor, test, build) — PR titles drive the auto-generated release notes.

  • Tests are updated (or n/a if the change is doc-only / refactor with no behavioural delta).
  • Notes for the reviewer

    Alternatives considered:

  • Link the App resources in dependency order, so Alchemy applies them one after another. This would add ordering to the deploy graph that only local dev needs, and slow the hosted deploy for it. Services that don't depend on each other would still race.

  • Make the emulator handle port requests one at a time. Ports would then be handed out in arrival order, which is still random, so they would still swap.

  • Derive each port from the service's name. Ports would no longer start at 3000 or match the guide, and two names could map to the same port.

  • Add a timeout to the reservation. Without one, a hung emulator now stops prisma dev at the reservation; before, it stopped it at the deploy step. The natural place for a timeout is adminFetch in the emulator client, but every emulator call uses it, including the long-running log stream. That is a separate, broader change.
  • Not fixed here: the emulator's port allocation isn't atomic, so two prisma dev processes for different apps can still be handed the same port at the same moment. And --fresh still skips the Postgres and buckets deletes when those emulators are stopped.

    Agent: maui-32

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: wmadden-electric Review duration: 179m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #350"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%