Skip to content

Shared helpers: one home for small pure helpers #1840

Done
1 of 1 issue completed
Done

Description

@V3RON

Request: none

Problem

Small helpers that many parts of Simlock need have no home. The clearest
case: turning a thrown value into its message text,
error instanceof Error ? error.message : String(error), is written by hand
95 times in the daemon's source, over 19 modules, and wrapped in a one-line
errorMessage function 10 more times. Each copy can drift, and a new one is
added with every change that handles an error.

The helper cannot simply go in an existing module: the CLI's router may load
no other module at startup (so --help stays fast), and some modules (the
updater, the optional-module host) may import only a short list. A helper
there would have to be copied again.

Who it is for

Contributors and coding agents changing Simlock's source, who today copy the
unwrap or a private errorMessage into each file that reports an error.

Outcome

There is one place for small pure helpers that two or more modules use,
src/shared/. Every module with a copy may import it, the CLI's router
included, because it imports nothing. errorMessage is its first helper: every copy is gone,
and a test refuses a new one. Users notice nothing: every error text stays
the same.

How it works for the user

flowchart TD
  A[Contributor handles a thrown value] --> B{How?}
  B -- "import errorMessage from src/shared" --> C[pnpm check passes]
  B -- "writes the unwrap by hand" --> D[copy check fails, naming file and line]
  E[Contributor adds a helper to src/shared] --> F{Imports anything?}
  F -- yes --> G[pnpm lint refuses: shared-imports-nothing]
  F -- no --> H[pnpm lint passes]
Loading

Before:

$ git grep -nE 'instanceof Error \? [A-Za-z_.]+\.message : String\(' -- 'src/*.ts' ':!*.test.ts' | wc -l
105
$ git grep -n 'function errorMessage' -- 'src/*.ts' ':!*.test.ts' | wc -l
10

After:

$ git grep -nE 'instanceof Error \? [A-Za-z_.]+\.message : String\(' -- 'src/*.ts' ':!*.test.ts'
src/shared/error-message.ts:…:  return error instanceof Error ? error.message : String(error);
$ git grep -n 'function errorMessage' -- 'src/*.ts'
src/shared/error-message.ts:…:export function errorMessage(error: unknown): string {

A simlock user sees no difference: the same CLI lines, log lines and HTTP
messages.

Examples

Thrown value Today (any copy) After (errorMessage)
new Error("boom") boom boom
a SimlockError with message Lease not found Lease not found Lease not found
"plain text" plain text plain text
42 42 42
undefined undefined undefined
Source line (non-test src/, outside src/shared/) Copy check
const m = err instanceof Error ? err.message : String(err); fails
the same split over three lines fails
`failed: ${e instanceof Error ? e.message : String(e)}` fails
// e instanceof Error ? e.message : String(e) passes
"e instanceof Error ? e.message : String(e)" passes
the same line in a *.test.ts file, ui/, e2e/ or packages/ not checked

What could go wrong

  • An error's text changes somewhere (a CLI line, a log line, an HTTP
    message) if a site is converted wrongly. Unlikely: every site is the same
    expression.
  • simlock --help gets slower if the router's import of the new module pulls
    in code. It cannot: the module imports nothing, and the CLI's lazy-loading
    test stays green.

Words used

  • unwrap: the expression that turns a thrown value into text, e.g.
    error instanceof Error ? error.message : String(error).
  • component: a directory under src/ with an index.ts, entered only
    through that file, e.g. src/agent-identity/.
  • CLI router: the CLI files that answer --help and a usage error before
    any command loads, e.g. src/cli/main.ts, src/cli/args.ts.
  • copy check: the repo test that refuses a hand-written unwrap outside
    src/shared/.

Non-goals

  • Moving any other helper into src/shared/.
  • The unwraps in test files (*.test.ts), ui/, e2e/ and packages/:
    they stay, and the copy check skips them.
  • The .catch(() => undefined) idiom.
  • Any change a user can see.

Completion conditions

  • src/shared/ exists, exports errorMessage, and its manual says what may
    go in it.
  • The unwrap grep of Existing code prints one line, in src/shared/, and so
    does git grep -n 'function errorMessage' -- 'src/*.ts'.
  • A file in src/shared/ importing anything but a sibling fails pnpm lint.
  • A hand-written unwrap added outside src/shared/ fails pnpm check.
  • src/cli/lazy-loading.test.ts passes unchanged, and pnpm check passes.

Open questions

Decisions

  • No ADR: a new leaf component under the existing rules (a component's
    surface is its index.ts; the dependency rules live in
    .dependency-cruiser.mjs and are proved in src/lint-rules.test.ts).
  • Decided by the maintainer (2026-11-24): the helper's home is a new leaf
    module src/shared/ for small pure helpers that imports nothing, not
    src/ports/, so the CLI's router may import it beside agent-identity and
    every copy goes, the CLI's included.
  • Decided by the maintainer: a small feature umbrella with Shared helper: errorMessage for the error-message unwrap #1442 as its one
    task.
  • Decided in the session: src/shared/'s admission rule — pure (no I/O, no
    clock, no state), imports nothing, used by two or more components — lives
    in its manual, because the manual is where a module's rules live.
  • Decided in the session: the copy check is a repo test over blankSource
    output, because oxlint 1.74 has no no-restricted-syntax and the repo's
    the time outside src/ports/ test is the precedent for a source-wide ban.
  • Decided in the session: src/mcp/connect.ts's isSimlockError fast path
    goes, because a SimlockError is an Error with the same message, so it
    changes nothing (X3).
  • Decided by the maintainer (2026-11-25): architecture rule 18 (a new module
    exports a factory that returns a handle a host plugs in) gets an exception,
    written into the rule: a module that imports nothing and that no host wires
    exports its pure functions directly. src/shared/ and agent-identity
    are such modules (X9).
  • Decided in the session: the contract, auth and alerts keep their short
    import lists and may not import src/shared/, because none of them has a
    copy and widening a list is a change of its own.
  • Decided in the session: host-load-imports-ports-only keeps its name,
    because accepted ADR 0068 cites it; its comment and its architecture-map
    row say it also allows shared (X10).
  • Decided in the session: test helpers (testing.ts, test-*.ts) are
    converted and checked; only *.test.ts(x) files are skipped, because test
    helpers are imported by many tests and are where a copy spreads from.

Behaviours

Id Behaviour (invoker, action, what it sees) Seam Cases Task
B1 A component calls errorMessage(value) and gets the thrown value's text: an Error's message, else String(value). src/shared/index.ts Error → its message; SimlockError → its message; a string, a number, undefined → String of the value #1442
B2 A contributor adds an import to a file in src/shared/: pnpm lint refuses any import outside src/shared/ — another component, a package, a Node built-in — resolved or not (shared-imports-nothing and shared-imports-nothing-unresolved, I2); a sibling passes; a test file is exempt. src/lint-rules.test.ts pathCases ../core/index.js fails; zod (unbuilt, so unresolved) fails; node:path fails (a built-in no other rule refuses); ./y.js passes #1442
B3 A CLI router file, the updater and the modules host import src/shared/: pnpm lint passes them; the router still fails a value import from any other component but agent-identity. src/lint-rules.test.ts pathCases src/cli/errors.ts → ../shared/index.js passes; → ../agent-identity/index.js passes; → ../ports/index.js fails; src/updater/x.ts → ../shared/index.js passes; src/modules/x.ts → ../shared/index.js passes #1442
B4 A contributor writes the unwrap by hand in a non-test src/ file outside src/shared/: pnpm check fails, naming the file and line. Comments and string text do not count; code inside ${} does. src/lint-rules.test.ts, a test like the time outside src/ports/ over git ls-files sources read through blankSource(text, { strings: true }) every source under src/ but src/shared/ and *.test.ts(x) has none; a sample counts a one-line unwrap, one split over lines, one inside ${}, and not one in a comment or a string #1442
B5 A simlock user sees every error text as today: each of X1's and X2's sites prints the same text through errorMessage, --help, -h and a usage error still load no watched component. existing tests: src/cli/lazy-loading.test.ts unchanged, and the suite under pnpm check --help, -h, a usage error load no watched component (ports included); src/mcp/connect.ts gives a SimlockError's message without its fast path (B1's SimlockError case) #1442
B6 A contributor reads the manuals and the architecture map: src/shared/AGENTS.md holds the admission rule, docs/internal/ARCHITECTURE.md lists the module and its rule, architecture rule 18 holds the exception of X9, and no manual line or rule comment of X7 is false. src/docs-manuals.repo.test.ts; the manuals, read shared row in the module table; shared-imports-nothing row in the dependency table; rule 18's exception; X7's and X10's lines true #1442

Stored state

none

Interfaces

Id What Shape and bounds Errors and limits Where Task Used by
I1 errorMessage errorMessage(error: unknown): string, exported from src/shared/index.ts; body return error instanceof Error ? error.message : String(error);. A plain function: no factory, no handle, no interface never throws src/shared/ #1442 —
I2 Dependency rules shared-imports-nothing: from ^src/shared/, to anything whose path is not ^src/shared/ (components, node_modules/…, Node built-ins), as a direction rule so test files are exempt; shared-imports-nothing-unresolved: from ^src/shared/, couldNotResolve: true with no path filter. Unlike contract-imports-nothing (X8), neither lets a package through. cli-router-imports-no-component-values, updater-imports-little, modules-imports-little and host-load-imports-ports-only allow ^src/shared/, each comment saying so a refused import fails pnpm lint .dependency-cruiser.mjs #1442 —

Existing code

Commit 200afe5.

Searches: git grep -nE 'instanceof Error \? [A-Za-z_.]+\.message : String\(' -- 'src/*.ts' ':!*.test.ts' → 105 (95 without the 10 definition bodies; a whitespace-tolerant match across lines finds the same 105); git grep -n 'function errorMessage' -- 'src/*.ts' ':!*.test.ts' → 10; every import { … errorMessage … } from in src/ → 17 files; every rule naming a site module in .dependency-cruiser.mjs and .oxlintrc.json; the manuals of every site module and docs/internal/ for claims about what a module imports.

Id Code (path:line) Today With this feature Handled by
X1 src/cli/errors.ts:32, src/core/doctor-types.ts:195, src/daemon/composition-support.ts:63, src/daemon/server-connection.ts:129, src/drivers/android/device-profile-source.ts:873, src/drivers/android/screen/scrcpy-link.ts:22, src/drivers/ios/command-run.ts:89, src/gateway/worker-link.ts:685, src/http/app-helpers.ts:164, src/mcp/connect.ts:152 10 hand-written errorMessage definitions, 31 lines, seven exported deleted; each caller imports src/shared/ #1442 (B5)
X2 95 inline unwraps (94 over error, 1 over cause): core 17, drivers 15, gateway 12, daemon 12, agent-device 8, ports 6, leasing 6, simlock-client 3, tunnel 2, stream 2, lease-files 2, history 2, cli 2, updater 1, state 1, modules 1, mcp 1, host-load 1, fseventsd 1; six in test helpers (src/leasing/testing.ts, src/leasing/test-lease-acquisition.ts, src/gateway/test-dispatcher.ts, src/gateway/test-fleet.ts, src/gateway/test-relay.ts, src/daemon/test-relay-support.ts) written by hand each calls errorMessage(x), 3,325 characters shorter in all #1442 (B4, B5)
X3 src/mcp/connect.ts:152-155; src/contract/errors.ts:697, src/contract/errors.ts:717-718 first returns error.message when isSimlockError(error) (instanceof SimlockError, which extends Error) the fast path goes: the unwrap returns the same message #1442 (B1, B5)
X4 Importers of a copy: src/drivers/ios/slim-boot.ts:5, src/drivers/ios/simctl.ts:4, src/drivers/ios/components.ts:16, src/drivers/ios/seeds.ts:12, src/drivers/ios/lineage-files.ts:8, src/drivers/ios/index.ts:43, src/cli/args.ts:3, src/cli/environment.ts:20, src/cli/commands/config.ts:10, src/daemon/server-requests.ts:7, src/daemon/server-hello.ts, src/daemon/server.ts, src/daemon/main.ts, src/daemon/worker-composition.ts, src/core/doctor.ts, src/http/app.ts, src/drivers/android/screen/session.ts import a copy from X1; none re-exports one, no test imports one import errorMessage from src/shared/ #1442
X5 .dependency-cruiser.mjs:321-332; src/cli/lazy-loading.test.ts:49-52 cli-router-imports-no-component-values lets the router files reach another component only for types, by dynamic import, or statically for agent-identity; the lazy-loading test watches ports; no case in src/lint-rules.test.ts covers the rule the rule also allows shared; B3 adds its cases; the lazy-loading test is unchanged #1442 (B3, B5)
X6 .dependency-cruiser.mjs:188-193, .dependency-cruiser.mjs:249-255 updater-imports-little (ports, contract, admin) and modules-imports-little (ports, contract, core, bus) refuse shared; the other rules on site modules list what they refuse and name no shared (host-load's comment and map row still claim it imports no other component: X10); .oxlintrc.json's one no-restricted-imports entry limits the gateway's names from core and leasing both allow shared #1442 (B3)
X7 src/agent-identity/AGENTS.md:7, src/cli/AGENTS.md:11, src/drivers/AGENTS.md:21, src/daemon/AGENTS.md:28 (site src/daemon/service-main.ts:128), src/state/AGENTS.md:14 (site src/state/open.ts:43), docs/internal/ARCHITECTURE.md:89, and X5's and X6's rule comments claim the module imports only its list, or that agent-identity is the router's one static import each names shared #1442 (B6)
X8 .dependency-cruiser.mjs:333-344, .dependency-cruiser.mjs:46-52; src/lint-rules.test.ts:285-300, src/lint-rules.test.ts:534-536 contract-imports-nothing refuses an import into src/, dist/, e2e/ or ui/, and its twin only an unresolved relative path, so a package and a built-in pass (case "passes a contract file importing a package"); direction exempts test files; the harness passes a fails case on any rule's violation the model for the shape of shared-imports-nothing and its cases, not for its to (I2) #1442 (B2)
X9 docs/internal/agent-rules/architecture.md:170-178, docs/internal/agent-rules/always-in-scope.md:49-53 rule 18: a module a change adds exports a factory that takes its dependencies and returns a handle; always-in-scope item 9 repeats it rule 18 gains the exception of Decisions for a module that imports nothing and that no host wires; item 9 points to it #1442 (B6)
X10 .dependency-cruiser.mjs:182-186; docs/internal/ARCHITECTURE.md:92; site src/host-load/sampler.ts:208 host-load-imports-ports-only says host-load "imports no other component"; the map row says "host-load stands alone" the comment and the row name shared as allowed; the rule name stays #1442 (B6)

Tasks

  • Shared helper: errorMessage for the error-message unwrap #1442 Shared helper: errorMessage for the error-message unwrap — every module that reports an error uses one errorMessage from src/shared/, and a test refuses a new copy.
    Risk: an error text a user sees (a CLI line, a log line, an HTTP message) changes.

Every Completion condition is delivered by #1442's Done when.

Open work in the same area: several open features (#1843, #1835, #1833, #1154, #944 among them) will edit files that hold a site. They share lines, not behaviour, so none gets a Depends on: whichever merges second converts the unwraps it touches, and the copy check names any it missed.

Written by an agent.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    feature:spec
    on Nov 25, 2026
  2. V3RON commented on Nov 25, 2026

    @V3RON
    ContributorAuthor

    Design posted: business sections, Decisions, Behaviours B1–B6, Interfaces I1–I2 and Existing code X1–X8, taken from the design of #1442.

    Written by an agent.

  3. V3RON commented on Nov 25, 2026

    @V3RON
    ContributorAuthor

    Spec updated: T1 is #1442, Tasks filled. #1442 now uses today's task template: its design moved into #1840's rows (B1–B6, I1–I2, X1–X10). Two changes came from the design check: the shared-imports-nothing rule gets its own targets, and architecture rule 18 gets an exception for modules that import nothing (maintainer decision, 2026-11-25). The host-load rule comment and map row are now in scope too.

    Written by an agent.

  4. added
    feature:planned
    and removed
    feature:spec
    on Nov 25, 2026
  5. github-actions commented on Nov 25, 2026

    @github-actions
    Contributor

    Closing: all 1 sub-issues are closed. The PR that closed the last one walks the completion conditions (delivery rule 9); reopen this if one does not hold on main. Flip its ADRs to Accepted if delivery has not.

  6. V3RON commented on Nov 25, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: #1849 (#1442)
    Parked: none
    Waiting: none
    Decided: none

    Written by an agent.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:planned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions