Skip to content

Add dynamic overlay lookup discovery and bounded host scheduling - #518

Open
sirdeggen wants to merge 66 commits into
mainfrom
codex/overlay-discovery-c04
Open

sirdeggen wants to merge 66 commits into
mainfrom
codex/overlay-discovery-c04

Conversation

@sirdeggen

@sirdeggen sirdeggen commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Overlay reliability program

This change is one slice of Make Overlays Great Again.

Do not merge until the stacked TypeScript and Go overlay branches have been proven together locally. Isolated CI green is not integration acceptance. Keep this PR draft. No npm publication, live database migration, or Mongo/BASM default activation.

C05 (verified progressive API) is parked. C02 (ts-stack#517) and C04 (ts-stack#518) both edit LookupResolver.ts and must not merge independently.

Keep this pull request in draft until local validation is complete.

Program and scope

  • Tracker or issue: Overlay reliability plan package C04
  • Why this change is needed: Lookup discovery must keep accepting later tracker hosts during an active query, bound hanging peers and response bytes, and preserve 2s/5s delays plus reputation.
  • Explicitly out of scope: C05 verified progressive API (parked), cryptographic verification (C02), publication
  • Exact head SHA reviewed: e82b77eeabfaf78e1a5b60f3367e56dc00fa828b

Impact

  • Public package source or manifest changed; affected packages are listed below
  • Public API, exports, types, runtime targets, or browser/mobile behavior changed
  • Documentation or examples changed

Affected packages: @bsv/sdk

Verification

  • Local commands and results: overlay-tools Jest 12 suites / 260 passed plus BRC100 overlay transport; oxlint overlay-tools clean (implementer evidence)
  • Hosted CI run: not yet (draft)
  • All applicable checks are terminal and successful on the exact head

Security and dependencies

  • No dependency or lockfile change

Release and operations

  • No npm publication was performed from a workstation or from this PR

Notes for reviewers

Sibling C02/C03 PR also edits LookupResolver.ts. Merge conflict expected. C05 stays parked. query() still throws when a deadline admits no host; query$ remains an unverified transport aggregate.

…duling

Subscribe to SLAP tracker updates during an active query so later
advertised hosts can join a fair, concurrency-bounded queue. Read lookup
bodies incrementally with honest resource-limit accounting, keep
trackers as routing sources, and leave raw query$ unverified while
exposing the C02 onEvidence intake seam.

Preserves 2s/5s delays, reputation/backoff, query/query$/freeform APIs,
and existing CORS/public lookup request headers.
Preserve per-entry script results so a rejected sibling cannot poison a
shared ancestor. LocalChainTracker tokens use participating sources only
and fail closed on missing identity. Reset ownership is rechecked after
dispose before destructive hooks. Built-in remote ChainTracks clients
advertise reorg-event capability explicitly. Services.getChainTracker
publishes one coalesced wrapper before yielding.
BHServiceClient no longer caches the queried merkle root. Validity is
decided from a freshly read header, so a false result cannot invert on
retry and a reorg cannot reuse a stale positive.
Store discovery bounds on the SLAP host cache so a tighter-limit query
cannot freeze a truncated host set for a later broader attempt. Throw
the historical no-competent-hosts error from query()/queryDetailed when
a deadline expires before any host is admitted; query$ still emits the
deadline snapshot.
LookupResolver is a BRC-100 JSON boundary file, so the in-flight
discovery key cannot use JSON.stringify. stringifyBRC100 preserves the
same service-plus-limit tuple without changing cache cover semantics.
Drop redundant optional-undefined on lookup evidence limits, extract
parseEvidence and Chaintracks header-retry helpers below S3776, and
rename coordinator catch params to error_.
…e-c02

# Conflicts:
#	docs/reference/package-api-migrations.md
#	governance/package-release-notes.json
Extract query/session and host-tracking helpers so cognitive complexity
stays within Sonar's limit, and clear the remaining overlay-tools
findings without changing lookup behavior.
S7746 on LookupResolver: prefer throw over return Promise.reject in the facilitator start chain.
discoverOverlayCertificates called getServices(), which throws on wallets
constructed without services and broke BRC-100 discoverBy* conformance.
Missing chain context now yields no identities instead of throwing.
Jest's istanbul graph still resolved 3.15.1, which fails pnpm audit --audit-level=high.
Jest's istanbul graph still resolved 3.15.1, which fails pnpm audit --audit-level=high.
Health requires overrideRemovalReview.retainedCount to match the live registry.
Health requires overrideRemovalReview.retainedCount to match the live registry.
LookupResolver and evidence helpers pushed the UMD/Vite/Metro payloads
over the previous raw-size gates.
LookupResolver and evidence helpers pushed the UMD/Vite/Metro payloads
over the previous raw-size gates.
Message-box UMD, wallet Vite gzip, and Hermes bytecode now cover the
evidence/discovery helper payload growth.
Message-box UMD, wallet Vite gzip, and Hermes bytecode now cover the
evidence/discovery helper payload growth.
…ry-c04

# Conflicts:
#	packages/messaging/message-box-client/browser-budget.json
#	packages/sdk/browser-budget.json
@sirdeggen
sirdeggen marked this pull request as ready for review September 10, 2026 21:49
sirdeggen and others added 21 commits September 14, 2026 13:45
Merge origin/main (bdaebe6) into prep/517 and resolve the metadata
drift this branch carried.

- Restore pnpm-workspace.yaml, governance/repository-health/exceptions.json,
  governance/dependency-release-policy.json and pnpm-lock.yaml to main's
  copies. Main already ships the js-yaml 3.15.2 override and registers it as
  js-yaml-jest-istanbul-advisory-override, so this branch's relocation and its
  duplicate js-yaml-jest-advisory-override entry were redundant and produced a
  duplicated overrides key in the merged lockfile (ERR_PNPM_BROKEN_LOCKFILE).
- Raise the bundle and platform budgets to the wave ceilings, which an earlier
  main merge on this branch had silently reverted.
- Drop the stray .claude/tasks/context_session_c02.md working note.
- Regenerate the ts2md reference docs for the overlay-tools and transaction
  modules so they match the source in this branch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Resolve conflicts in browser/platform budget JSONs (take the
field-wise maximum ceilings) and pnpm-lock.yaml (take main's
lockfile; no new dependencies introduced by this PR). Restore
scripts/dependency-release-governance.test.mjs to main's copy,
which already contains the single assertion this PR duplicated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Merge #517's onEvidence/evidenceLimits evidence-intake seam onto
#518's bounded discovery/scheduling architecture in LookupResolver.ts.
Keep #518's LookupDiscovery/LookupHostQueue/LookupResources rewrite as
the base; it already delivers owned per-host output copies to
onEvidence before legacy txid/outpoint dedup, caps evidence intake at
512 outputs / 16 MiB by default, and emits exactly one {type:'limit'}
event per query. Restore the exact "Evidence intake limits must be
positive safe integers" validation message for the evidenceLimits
shorthand (previously only in #517's LookupQuerySession constructor)
by validating it in a new lookupQueryLimits/evidenceLimitOverrides
helper before any host is queried, and document the precedence between
evidenceLimits and limits.maxEvidenceOutputs/maxEvidenceBytes when both
are supplied for the same call. Keep a single formatting choice for the
now-identical LookupEvidenceEvent union.

Regenerate packages/sdk/docs/reference/overlay-tools.md (and the rest
of the ts2md-managed SDK reference docs, which had drifted out of sync
with source) via `pnpm --filter @bsv/sdk run doc` instead of hand
merging.

No version bumps. No behavior from either PR's test suite is removed
or loosened.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ling

The main integration merge raised the SDK esbuild raw ceiling to 600000,
a value that belongs to the lookup discovery change (#518). This branch
measured 590000; restore it so the diff carries only its own budget.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Restore the eleven ts2md reference pages for modules this change does
not touch to the incoming copies; the generator run had imported
unrelated drift that already exists on main. Keep the regenerated
overlay-tools page. Reinstate this change's own 600000 esbuild raw
ceiling after restacking on the corrected evidence branch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
query() and queryDetailed() flatten query$ to its first snapshot. The
terminal gate only threw when hostCount was 0, and explicitly skipped
the throw for terminalReason 'cancelled', so a caller abort resolved as
{ type: 'output-list', outputs: [] }. At any host count an aborted
attempt was indistinguishable from an authoritative "this service has
no outputs" answer, which is exactly the completion-vs-empty confusion
queryDetailed exists to prevent.

Throw lookupAbortError() before the host-count gate whenever the
terminal snapshot reports 'cancelled'. query$ is unchanged and still
emits its terminalReason: 'cancelled' snapshot for progressive callers.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
chargeLookupDiscoveryBytes can exhaust maxTotalBytes while SLAP trackers
are still being read, before any host is admitted. That leaves the
terminal snapshot with hostCount 0, terminalReason 'resource-limit' and
the limit recorded in limitsHit. queryDetailed()'s hostCount gate did not
look at terminalReason, so it threw the historical "No competent <network>
hosts found by the SLAP trackers" Error and discarded both fields.
Security-sensitive Promise callers could not tell "the trackers named no
hosts" from "this attempt ran out of its own byte budget".

Branch the gate on terminalReason: 'resource-limit' throws
LookupResourceLimitError carrying the first limit that was hit, while
'deadline' and a settled empty discovery keep the historical error and
message unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
performLookupRequest built its RequestInit without a redirect field, so
fetch defaulted to 'follow'. normalizeLookupHost and the https: guard in
lookup() validate the advertised URL only; neither runs again on the
final URL. A SLAP-advertised HTTPS host could therefore answer the
lookup POST with a 307/308 and have the serialized
stringifyBRC100({ service, query }) body re-sent to http:, loopback or a
link-local address. This PR queries every eligible advertised host, so
the hop sat on the hot path for untrusted SLAP domains, and the SLAP
tracker discovery requests reach the network through this same method.

Set redirect: 'error' so the transport fails closed. fetch rejects, and
the existing failure path records the advertised host as an ordinary
availability failure instead of crashing the query or following the hop.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… them

TransactionEvidenceCoordinator.run() shifts the next candidate off a job's
queue before calling attempt(). If every concurrency slot is already in use
(including slots still reserved by non-abortable, already-finished attempts;
see the "does not free a non-abortable backend slot..." test), attempt()
threw a plain TransactionEvidenceError('limit') and called the caller's
`settled` callback. run()'s catch treated that exactly like a genuine
verification failure: the candidate was gone (already shift()ed) and never
pushed back, so a same-txid alternate candidate that had already been
admitted to the job could be silently discarded without ever being tried,
even though it was perfectly valid.

Fix: attempt() now throws a dedicated ConcurrencyLimitSignal when the
operation never started, and does not invoke `settled` (nothing was
consumed, so no byte/candidate bookkeeping should be released). run()'s
candidate loop catches that signal specifically, unshifts the candidate
back onto job.candidates, marks the job not-running, and returns without
looping or retrying itself. Re-admission is driven solely by pump() being
invoked again when some other active attempt settles, so this cannot spin
or busy-wait for a slot.

Added a regression test that builds a genuine concurrency race: a
non-abortable "ghost" attempt (cancelled while its script verification is
still in flight) holds one of two slots, a job with two same-txid
candidates (one invalid, one valid) takes the other, and a third waiting
job is admitted into the slot freed by the first candidate's failure before
the job's own retry can reclaim it. Before the fix this made the valid
alternate candidate reject with 'limit'; the test fails for that reason on
the old code and passes with the fix.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Every TransactionEvidenceCoordinator attempt calls
ChaintracksChainTracker.getVerificationContextToken(), which requires
chaintracks.findChainTipHash(). BHServiceClient implemented that method
as `throw new Error('Not implemented')` even though it already exposes a
working findChainTipHeader() against /api/v1/chain/tip/longest. Any wallet
using a BHServiceClient as options.chaintracks, or as a LocalChainTracker
participating source, broke on every verification attempt.

Fix: implement findChainTipHash() by delegating to the existing
findChainTipHeader() and returning its hash, matching the same pattern
GoChaintracksServiceClient already uses for the same interface method.
No other ChaintracksClientApi contract surface changes.

Added a regression test on BHServiceClient confirming findChainTipHash()
resolves to findChainTipHeader()'s hash instead of throwing, and a
regression test on ChaintracksChainTracker confirming
getVerificationContextToken() succeeds end-to-end when backed by a
BHServiceClient. Both fail with "Not implemented" on the old code and
pass with the fix.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Document the TransactionEvidenceCoordinator candidate-requeue fix and the
BHServiceClient.findChainTipHash implementation in their packages'
CHANGELOG.md, matching this repo's convention of recording behavior-
affecting fixes there alongside the commit history.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The concurrency-limit signal is handled only in the candidate loop. The
cached-positive recheck does not need the same handling: run() is
reached solely from pump(), which checks the attempt limit synchronously
before starting the job, and no await separates that check from the
recheck attempt. Record the invariant next to the call so a later
refactor of pump() or run() revisits it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…dles

The ceilings carried on this branch were authored before main grew
(#534, #547), so the merged wallet bundles exceeded them and CI stopped
at the first over-budget dimension. Measured from the exact packed
dependency graph at 83c0670 (JS bundle sizes verified byte-identical
between macOS and hosted Linux; Hermes gzip estimated at the +1.3% Linux
ratio recorded in #547):

  client  Vite    1,759,717 raw / 416,284 gzip / 324,287 brotli
  client  esbuild 1,372,320 raw / 378,741 gzip / 302,937 brotli
  mobile  Metro   1,811,581 raw / 461,355 gzip / 355,871 brotli
  mobile  Hermes  3,688,871 raw / 1,502,972 gzip / 1,166,821 brotli

Only the dimensions that exceeded are raised, with about 0.25% headroom
(1% on the Hermes gzip estimate, 0.5% on Hermes brotli run variance).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Bounded discovery, the host queue and the streaming response reader grow
every bundle that embeds @bsv/sdk. Measured from the exact packed
dependency graph at the top of the ordered overlay wave (max of 5eef0fc
and 9077394; JS bundle sizes verified byte-identical between macOS and
hosted Linux; Hermes gzip estimated at the +1.3% Linux ratio from #547):

  @bsv/sdk UMD         591,705 raw / 168,531 gzip / 140,485 brotli
  @bsv/simple Vite     752,122 raw / 182,787 gzip / 150,699 brotli
  wallet client Vite   1,782,018 raw / 421,917 gzip / 328,724 brotli
  wallet client esbuild 1,389,928 raw / 383,620 gzip / 306,783 brotli
  wallet mobile Metro  1,834,234 raw / 466,804 gzip / 360,591 brotli
  wallet mobile Hermes 3,725,120 raw / 1,520,644 gzip / 1,181,384 brotli

Only the dimensions that exceeded are raised, with about 0.25% headroom
(1% on the Hermes gzip estimate, 0.5% on Hermes brotli run variance).
Later branches in the wave move these bundles by under 150 bytes, so
they inherit these ceilings unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

overlay-reliability Part of org overlay reliability program (project 19)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant