@intentic/base
v1.431.0
Published
Runtime primitives every tier shares, when-expressions, disposal, and the async schedulers
Readme
base
Runtime primitives that every tier, from the daemon to the browser, must run identically: when-expressions, disposal, async schedulers, untrusted-content tags, size and token formatting, fuzzy path scoring, and the file, SQLite, worker-thread, whisper and git-runner plumbing the Node tiers share.
flowchart LR
manifest["Extension manifests<br/>when strings"] --> base(["base"])
base -->|"when · fuzzy"| web["Web app<br/>and extension host"]
base -->|"outside-text · plain-text · lifecycle"| daemon["Sandbox daemon<br/>fileq · webq"]
base -->|"fuzzy"| search["File-name search<br/>text-search"]
base -->|"fs · sqlite · worker-calls"| node["Node tiers<br/>daemon, devices, Discord"]
base -->|"async · errors · format · dag"| rest["Every tier<br/>devices, deploy, extensions"]
base -->|"held"| daemon
base -->|"git"| gitusers["Sandbox daemon<br/>and deploy CLI"]One implementation per rule that more than one tier applies: two copies of a condition, a size label or a quick-open ranking disagree on screen. Anything only one surface renders belongs next to that surface.
Subpath exports only, with no index, so a browser bundle pulls nothing it does not use.
outside-textuses Web Crypto, so it does not tie a caller to Node, andone-line,capped-read,escape,jsonandfetch-jsonneed nothing but the language and fetch;fs,contained,json-lines,process,sqlite,worker-calls,web-stream,ws-tcp-pump,acme,gitandwebauthnare Node's.The package carries
srcalongsidedist: consumers selecting@intentic/src, including pnpm's injected workspace copies, need the source files named by the export map.when.tsis a closed grammar with no arithmetic, calls or property access, because its strings arrive from installed extensions.outside-text.tswraps chat, pages and tool results in<untrusted-content>tags whose close carries a fresh id, so content cannot forge its own end. It marks taint and does not defend against a hostile model.lifecycle.tsis teardown as a store: whatever needs undoing registers when it is created, a member that throws does not stop the rest, and failures surface together. Every scheduler inasync.tsis disposable.async.tscancels with anAbortSignal, never with a counter compared after an await:Latestaborts the previous attempt when the next one starts.anySignallinks a caller's signal into your own.retryandpollForstop waiting the moment their signal aborts, andpollForexpires at its deadline rather than on the tick after it.withDeadlineaborts the work when time runs out.withTimeoutandwithinonly stop waiting for it.unlessAbortedstops waiting at an abort. Racing every read of a loop against one promise that stays pending instead keeps every value raced alive until that promise settles.withinclears its timer however the race ends and can unref it, so a CLI's bounded close or a daemon's best-effort wait never holds the process open.startPeriodicruns a chore on a timer: a warm-up, then a run every interval, never two at once (a tick that finds the last run going is skipped), unref'd timers, and one dispose for all of it. Hand-rolled copies kept missing one of the four.Delayertakes amaxWaitMsceiling for a source that never goes quiet,pollForamaxProbescap that holds when the clock does not move, andretryAfterMsis the one reading of aRetry-Afterheader (seconds or a date).
ssh-config.tsis the one reading of a managedIncludeline in someone's~/.ssh/config: only a live line counts (a commented-out one is not an include), ours goes first, and every other line is left byte for byte. The daemon's ssh hosts and the machine agent's sync aliases both write theirs through it, withwriteFileAtomic.utf8-text.tscuts a UTF-8 file into text windows and says when one is lossy (any window that is not UTF-8), the twin ofutf16-text.ts; the daemon's /work reads and the desktop folder server both use it, so a Latin-1 file opens read-only from either.node/ws-tcp-pump.tsis desktop sync's SSH byte pump, run by both ends of the WebSocket: binary frames only, a chunk copied before an async send, backpressure each way, and a ceiling on a WebSocket that cannot pause.held.tsis how a reading kept between asks states its two limits: the change feed that makes it stale, andmaxAgeMs, how late a change that feed missed may show.freshnessis one reading,heldis readings by key. The daemon's directory reads and compiled.gitignorematchers, land standings and history index use it.cachedAsyncis the sibling for a reading that costs a round trip: askers share the load in flight, a failure is never held, andmaxEntriescaps it.BoundedMapis a Map that drops its oldest entry past a limit, for memory kept by a key the process cannot bound itself.dag.tslayers a dependency graph by generation for the two graphs extensions draw, workflows and CI pipelines, so a cycle or a self-need is handled one way in both: a cycle lands in one final layer.cloudflare.tsis the Cloudflare v4 client the platform and the deploy engine's providers both call: a bearer, the success envelope checked by hand (no schema library here; each caller validatesresultwith its own), 30 seconds per call, and the zone list's paging. A 401/403 is aCloudflareTokenErrorand a zone out of DNS records (81045) aCloudflareZoneFullError, so each caller words those for its own reader (2026-10-06; the two used to keep a copy each, and only the platform's knew 81045).node/process.ts(./process) is one bounded child process for every Node tier but the daemon (which keepsrunCheckfor its workload classes): its own process group, ended whole at a deadline or abort (SIGTERM, then SIGKILL; on Windows WM_CLOSE and a Ctrl-Break, thentaskkill /T /F:node/windows-stop.ts, 2026-11-02), settled once it exits even while a grandchild still holds its pipes, and its output kept as head and tail with a… [N characters cut] …marker, decoded so a split UTF-8 character arrives whole.execBoundedthrows execFile's rejection shape, so apromisify(execFile)caller moves over unchanged (2026-10-12; the device agent's shell and adb runners each had a copy, and its sync runner had none of it and could hang past its own timeout).runBoundedSync/runBoundedSyncBytesare the same bound for a caller that cannot await (a module's load, an exit hook, a CLI's synchronous path): Node'sspawnSynctimeout signals only the direct child, so these give the command its own group and end the rest of it once spawnSync returns from a deadline or a full buffer. A run that hands over the person's own terminal (stdio: "inherit") stays onspawnSync, so Ctrl-C reaches it.untranslatedEnvis the environment for a program whose output is parsed: messages in gettext's C locale, every other category as the caller had it.node/webauthn.ts(./webauthn) is the relying-party half of WebAuthn: the CBOR subset authenticators emit, COSE keys as JWKs, ES256/RS256/EdDSA, the rpIdHash, user presence and verification, and the signature counter. The daemon's passkey sign-in and the machine agent's check of the owner's passkey over an egress change both use it (2026-10-27; it was the daemon's alone). Responses are typed by the fields it reads, so base needs no schema of the contract's. An expectation may list several origins.fetch-json.ts(./fetch-json) is one JSON call over HTTP: a deadline of its own combined with the caller's signal, a refusal's body read up to 1,200 bytes (then cancelled) and quoted in the error at 300 characters, and akindthat tells a timeout from a server that could not be reached. Explicitly accepted statuses keep their complete bodies. No schema library, likecloudflare.ts: each caller validates the body with its own.escape.ts(./escape) holdsescapeHtml(all five HTML-active characters, the apostrophe included),escapeRegExpandisRecord, which a dozen packages each kept a copy of.errors.tslets a catch name the failure it expects:isMissingis ENOENT or ENOTDIR only, andundefinedIfMissinganswers undefined for "not there" while EACCES, EIO and a bug's TypeError still arrive. It reads another program's failure by its structure rather than its sentence:stderrOfandexitCodeOfoff a failed child,networkCodeOffor the errno wherever the runtime hangs it, andisFetchNetworkFailurefor a fetch that got no answer. Node's fetch saysfetch failedwith the errno oncauseand Bun's saysUnable to connectwith it on the error, anderrors.integration.test.tshas both runtimes throw their own (2026-10-16).json.ts(./json) reads text that may not be JSON:parseJsonOrhands back its fallback for a SyntaxError and rethrows anything else, andparseJsonWithruns a schema over the value, undefined when the text is not JSON or the schema refuses it. The schema is anything with zod'ssafeParse, so base still depends on no schema library.node/fs.tsis the one atomic write: staged beside the target under a random name (worker threads share a process id, and two containers on one volume may both be pid 1), renamed over it, the staging file removed on any failure, a given mode exact rather than cut by the umask.renameOveris the rename retried while Windows holds the target open, for any rename that replaces a file someone may have open. A durable write flushes the file and then its directories throughsyncDirectories, which is best effort by contract: a directory it cannot open or a filesystem that cannot flush one never fails a write that already happened.queueOnFileserializes one path's read-modify-write across every handle in the process.readTextIfExists(and itsSynctwin) andreadJsonIfExistsread a file that may be absent: undefined when it is missing, or for JSON, not JSON or refused by its schema, while EACCES, EISDIR and EIO still throw.node/contained.tsasks whether a path is inside a root one way: by whole segments, so..notesis a name and/work/a-otheris not under/work/a, with the root itself inside and another Windows drive outside.realInsidefollows links first, for a disk where a folder can be a link elsewhere.windowsHoldsis the one rule for a name NTFS can hold.node/json-lines.tsreads newline-delimited JSON off a stream: decoded across chunks, split at LF only (readline also splits at U+2028, which JSON allows raw in a string),\rdropped, a last record without its LF kept. It yields lines;parsedJsonLinesskips a line that is not JSON instead of ending the stream at it.capped-read.tsreads a fetched body with a ceiling on the bytes that arrive, cancelling the stream at the cap, since a checked Content-Length is not a ceiling on a chunked answer.text/one-line.tsis outside text as one bounded line: whitespace flattened, control and format characters (ESC, BEL, bidi overrides) removed, cut at a character boundary with the…inside the limit.sqlite.ts: a store holding aDatabaseSyncopens it withopenSqlite(WAL, a busy timeout, foreign keys) and groups writes withimmediateTransaction, which takes the write lock at BEGIN and joins a transaction already open.workers/worker-calls.tsis a call-and-answer channel to a worker thread, spawned on first call and again after a crash, andworkerPoolspreads independent calls over a few of them.acme/acme.tsorders a certificate over DNS-01 and tells the CA to look only once the zone's own nameservers serve the challenge (authoritative-dns.ts), since an early look fails the authorization for good. When that never shows — the host's network blocks direct DNS, or the nameservers answer without the value because something on the network answers in their place (the interception production hit) — a caller'sconfirmChallenge(the DNS provider reading the record back) plusPROVIDER_SETTLE_MSstands in for it, with a warning that says which of the two it saw; without one the wait's deadline fails the order.git(git/) is how the sandbox daemon and the deploy CLI run git and other short commands.defaultGitis theGitRunnerbehind almost every git call the daemon makes: it forks git from a small resident child (forker.ts, kept besiderunner.tssonew URL("./forker.js", import.meta.url)finds it indist/) rather than from the large daemon process, retries only onindex.lockcontention, and marks pathspecs literal so a file named[slug]matches only itself. Every git it starts runs untranslated, since the daemon classifies git's refusals by their English words; tmux throughforkedExeckeeps the caller's locale, as its server hands its environment to every shell it opens.forkedExecruns the daemon's other polled commands (tmux, status probes) from the same child: a spawn from a large process first copies its page tables, 52 ms at 778 MB, where a request to the child costs 0.04 ms.setRunnerEnvadds what every command it starts carries and the process's own environment must not: the daemon's ssh agent socket, which its own git signs through and its other children would inherit fromprocess.env.politeGitruns bulk agent work underniceandionice.commands.tsholds the generic verbs over an injectableGitRunner, andlocks.tsclears a lock only while no git process runs. (2026-10-06: moved here from@intentic/scaffold, which is named for the workspace skeleton; a new package was rejected because a name npm has never seen fails the release plan until it is registered by hand.)Every git the runner starts syncs its writes (
git/durability.ts:core.fsync=all,core.fsyncMethod=batch, andpersistGitDurabilityfor a repository's own config, which a terminal's git reads), and one that fails on damaged storage (isGitCorruption) is told to the listeneronGitCorruptionsets, which the daemon uses to start a repair. (2026-11-03: a host crash kept loose objects' sizes and lost their bytes; git's default syncs packs only.)
Key files
- src/when.ts —
parseWhenandevaluateWhen, the condition language manifests carry. - src/async.ts —
Delayer,Coalescer,SingleFlight,keyedLock,startPeriodic,within,pollFor,retry,createBackoff. - src/lifecycle.ts —
IDisposable,DisposableStoreandMutableDisposable. - src/text/outside-text.ts —
wrapOutsideContent, the envelope for outside text. - src/held.ts —
freshness,heldandcachedAsync: a change feed and a time bound for anything kept, andBoundedMap. - src/fuzzy.ts —
fuzzyScore,rankByFuzzyand the per-keystrokefuzzyRankerboth quick-open ends share. - src/node/fs.ts —
writeFileAtomic(its synchronous twin for a write on the way out, andwriteChunksAtomicfor content produced a chunk at a time),renameOver,syncDirectories,queueOnFile,pathExistsandfreePort, the file primitives of every Node tier.
Commands
pnpm --filter @intentic/base test