zebra 0.0.1-preview.1
Zebra: an incremental build orchestrator for Zena and standalone workspaces
zebra#
An incremental script and service runner for Zena (zena-package.json), standalone Zebra (zebra.json), and Wireit (package.json) workspaces, built on zena-lang:workflow.
Zebra runs a dependency graph of scripts and long-running services with declared inputs, outputs, and dependencies, rerunning only what changed. For each script, Zebra content-hashes the declared inputs and asks the workflow engine what to do: nothing (when the previous run recorded the same fingerprints and declared outputs are intact), restore from the local content-addressed cache, or execute the command (sh -c in the package directory with node_modules/.bin on PATH).
Installation#
zebra is built into the zena CLI (zena build <target>). To use it as a library or standalone runner:
zena pkg add zena-lang:zebra
CLI Usage#
zena build [-j <jobs>] [-w|--watch] [-f|--force] [--no-cache] [-v|--verbose] [<target>] [-- <args>...]
zena run [-j <jobs>] [-w|--watch] [-f|--force] [--no-cache] [-v|--verbose] <target> [-- <args>...]
zena test [-j <jobs>] [-w|--watch] [--force] [--no-cache] [-v|--verbose] [-- <args>...]
zena build --migrate [--dry-run] [-f|--force] [<package-dir>...]
<target>can be a script name in the current package (zena build test,zena run check), a package directory (zena build ./packages/app, which runsbuild), or<package-dir>:<script>(zena build ./packages/app:test). With no<target>,zena buildruns the current package'sbuildscript andzena testruns itstestscript.-- <args>...appends shell-quoted arguments to the entry target's command only, running that entry step uncached without overwriting its saved freshness record.-j/--jobs <n>bounds parallel command execution (defaulting toZEBRA_PARALLEL,WIREIT_PARALLEL, orZENA_AVAILABLE_PARALLELISM).-w/--watchwatches input globs, lockfiles, and package manifests for changes, re-running affected steps incrementally while keeping unchanged services alive in aServicePoolacross rebuilds.-f/--forcebypasses freshness and cached outputs to rerun scripts unconditionally.--no-cachedisables restoring outputs from.zebra/blobs/when a step is not fresh.-v/--verboseprints every step (includingfreshandskipped) and any captured stdout/stderr on the rail.--migrateconverts Wireit blocks inpackage.jsonintozena-package.json(when present) orzebra.json.
Programmatic Usage#
import { buildTarget, reportBuild } from 'zena-lang:zebra/runner';
import { ScriptRef } from 'zena-lang:zebra/wireit-config';
let outcome = await buildTarget('.', new ScriptRef('packages/app', 'build'), {
parallelism: 4,
});
let exitCode = reportBuild(outcome);
Configuration (zena-package.json and zebra.json)#
Zena packages configure scripts and filesets directly in zena-package.json. Non-Zena projects can use a standalone zebra.json (with an optional "workspace": {"packages": [...]} at the workspace root). Both support // and /* ... */ comments:
{
"name": "app",
"dependencies": {
"lib": {"path": "../lib"},
},
"filesets": {
"assets": ["assets/**"],
},
"scripts": {
"build": {
"command": "zena build src/main.zena -o out/app.wasm",
"output": ["out/app.wasm"],
"dependencies": ["source", "assets"],
},
"test": {
"type": "zena:test",
"tests": "test/*_test.zena",
},
"fmt": "zena fmt --check .",
},
}
- Declarative
zena:teststeps and defaulttesttarget: Scripts inzena-package.jsonandzebra.jsoncan set"type": "zena:test"(with optional"tests": true,"tests": "test/*_test.zena", or"tests": ["test/*_test.zena", ...]— defaulting to["test/**/*_test.zena", "tests/**/*_test.zena"]when omitted — and optional"debug": truefor-g) to run Zena tests in-process throughTestStepRunner. Test pattern globs merge intofiles, andzena-package.json"zena:test"steps automatically depend onsource. When azena-package.jsonomits atestscript (and no fallbackpackage.jsondefinestest), Zebra synthesizes a"type": "zena:test"target overtest/**/*_test.zenaandtests/**/*_test.zena. - Automatic
sourcefileset: Every package withzena-package.jsonexposes an automaticsourcefileset covering its library.zenafiles (from"source", defaulting to"src"and excluding*_test.zena,test/**, andtests/**),zena-package.json, any"wit"dependencies, and thesourcefilesets of all"path"dependencies. Scripts opt into it via"dependencies": ["source"](or"../pkg:source"). - Named
filesets: Entries accept either a glob array shorthand ("wit": ["wit/**/*.wit"]) or{"files": [...], "dependencies": [...]}and compose transitively without spawning commands. - Caching and tracking: Task scripts cache automatically whenever
files,output, or a fileset dependency is declared, defaultingoutputto[]when omitted. Setting"cache": falseopts a step out of freshness and caching while keeping itsfilesglobs active in--watchmode. Omittingfiles,output, and fileset dependencies on a command step leaves it untracked so it runs every time. String entries ("fmt": "zena fmt --check .") are shorthands for always-run commands. - Locks:
locksdefaults to["zena-lock.json"]and may be set at the package root or overridden per script ("locks": []disables lockfile hashing). - Services: Scripts with
"service": trueor"service": {"readyWhen": {"lineMatches": "..."}}start on demand when a dependent executes, wait for readiness when configured, are shared across dependents, and are stopped as a Unix process group (SIGTERMthenSIGKILL) when dependents finish. Invoking a service directly as the top-level target runs its dependencies and streams the service in the foreground. - Wireit fallback: When a package directory has no
zebra.jsonand no"scripts"block inzena-package.json, Zebra falls back topackage.jsonWireit scripts while still serving filesets fromzena-package.json. Dependencies can cross freely betweenzena-package.json,zebra.json, andpackage.jsonpackages.
Cache Layout (.zebra/)#
Each package's <pkg>/.zebra/ directory stores:
state.json— freshness records for that package's scripts (the last cache key and output manifest).cache/<key>.json— cache key to output manifest.blobs/— content-addressed output bytes (sha256).
The workspace root's .zebra/ directory stores:
hashes.json— the(mtime, size)tosha256file hash memo shared across packages (with a 20ms racy-timestamp guard).plans/<hash>.plan—stat-verified cachedScriptConfigclosures keyed by target entry.
A dependency contributes the digest of its outputs to dependents' cache keys rather than its input fingerprint, giving early cutoff when a dependency reruns and produces identical outputs.
GitHub Actions Remote Cache#
Setting ZEBRA_CACHE=github layers GitHubActionsCache behind the local .zebra/ DiskCache (TieredCache). On a local cache miss, Zebra queries the GitHub Actions v2 CacheService Twirp JSON API, verifies downloaded blob sha256 digests, populates <pkg>/.zebra/, and materializes outputs into the workspace. On a build step execution, Zebra writes <pkg>/.zebra/ first and uploads the step's entry to GitHub Actions Cache (ZEBRA_CACHE_READ_ONLY=1 disables uploads; ZEBRA_CACHE=none disables both local and remote caching).
In GitHub Actions workflows, run .github/actions/setup-zebra-cache before build steps so ACTIONS_RESULTS_URL and ACTIONS_RUNTIME_TOKEN are served to run: steps over a 127.0.0.1 token custodian (ZEBRA_GITHUB_CUSTODIAN_URL).
Libraries#
zebra(zebra/main) — CLI entry point (main).zebra/runner—buildTarget,watchTarget,BuildOptions,WatchOptions,reportBuild,reportMigration,formatBuildReport, andBuildOutcome.zebra/zebra-config—zena-package.jsonandzebra.jsonparsing (ZebraWorkspace), synthesizedsourcefilesets, and Wireit migration (migrateWorkspaceToZebra,convertPackageJsonToZebraJson,mergeWireitIntoZenaPackageJson).zebra/wireit-config—package.jsonWireit parsing (WireitWorkspace) and shared script configuration types (ScriptConfig,ScriptRef,DependencySpec,ServiceConfig,EnvBinding,ConfigError,refKey,resolveDependency).zebra/plan-cache—.zebra/plans/snapshot serialization andstat-verified warm plan loading (loadOrCollectConfigs,PlanLoadStats,ConfigProbe).zebra/builder— TranslatesScriptConfigclosures intoworkflowsteps and execution callbacks (planScripts,StepExecution,lockfileCandidates).zebra/disk-cache,zebra/github-cache, andzebra/state-json—.zebra/blob/manifest storage (DiskCache,TieredCache,stateDirOf), GitHub Actions remote caching (GitHubActionsCache,GitHubCacheConfig,CacheMode,resolveCacheMode,resolveGitHubCacheConfig), and JSON serialization for state, manifests, and hash memos.zebra/fs-walkandzebra/fs-util— Input glob discovery (FileHasher,expandGlobs,hashFile,validateGlobs,splitBase) and workspace path utilities.zebra/shell— POSIX shell argument quoting (shellQuote,appendShellArgs). Zebra's terminal output useszena-lang:terminal(packages/terminal): colors, the Clack-style rail layout and the spinner.
License#
MIT
/**
* POSIX shell argument quoting utilities.
*
* Kept free of Zebra-internal imports so it can move to a shared shell or CLI
* package alongside `colors`, `layout`, and `animation`.
*
* References:
* - POSIX.1-2017 Shell Command Language, XCU §2.2 (Quoting) and §2.2.2
* (Single-Quotes):
* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
* - Python standard library `shlex.quote` (`Lib/shlex.py`), which leaves
* ASCII `[A-Za-z0-9_@%+=:,./-]` unquoted and wraps everything else in
* single quotes with `'` escaped as `'\''`.
*/
import {StringBuilder} from 'zena:core';
let ASCII_SPACE = 0x20; // ' '
let ASCII_PERCENT = 0x25; // '%'
let ASCII_SINGLE_QUOTE = 0x27; // '\''
let ASCII_PLUS = 0x2b; // '+'
let ASCII_COMMA = 0x2c; // ','
let ASCII_HYPHEN = 0x2d; // '-'
let ASCII_DOT = 0x2e; // '.'
let ASCII_SLASH = 0x2f; // '/'
let ASCII_DIGIT_0 = 0x30; // '0'
let ASCII_DIGIT_9 = 0x39; // '9'
let ASCII_COLON = 0x3a; // ':'
let ASCII_EQUALS = 0x3d; // '='
let ASCII_AT = 0x40; // '@'
let ASCII_UPPER_A = 0x41; // 'A'
let ASCII_UPPER_Z = 0x5a; // 'Z'
let ASCII_UNDERSCORE = 0x5f; // '_'
let ASCII_LOWER_A = 0x61; // 'a'
let ASCII_LOWER_Z = 0x7a; // 'z'
/**
* Whether `b` is in the unquoted safe set `[A-Za-z0-9_@%+=:,./-]` from
* Python's `shlex.quote` (`[^\w@%+=:,./-]` with ASCII `\w`).
*
* None of these bytes are POSIX shell metacharacters (`| & ; < > ( ) $ \` \ " '`,
* whitespace) or word-expansion triggers (`* ? [ # ~` in XCU §2.2 and §2.6).
* `=` is safe in argument position; a bare `name=value` word in command
* position would instead be parsed as an `ASSIGNMENT_WORD` (XCU §2.10.2 Rule 7b).
*/
function isUnquotedShellByte(b: i32) {
return (b >= ASCII_DIGIT_0 && b <= ASCII_DIGIT_9) ||
(b >= ASCII_UPPER_A && b <= ASCII_UPPER_Z) ||
(b >= ASCII_LOWER_A && b <= ASCII_LOWER_Z) ||
b == ASCII_UNDERSCORE ||
b == ASCII_AT ||
b == ASCII_PERCENT ||
b == ASCII_PLUS ||
b == ASCII_EQUALS ||
b == ASCII_COLON ||
b == ASCII_COMMA ||
b == ASCII_DOT ||
b == ASCII_SLASH ||
b == ASCII_HYPHEN;
}
/**
* Returns a shell-escaped form of `arg` for interpolation as an argument
* word in a POSIX `sh -c` command line.
*
* Non-empty strings containing only `[A-Za-z0-9_@%+=:,./-]` are returned
* unchanged. Empty strings return `''`, and all other strings are wrapped in
* single quotes (POSIX XCU §2.2.2), replacing each embedded `'` with `'\''`
* (close single quote, backslash-escaped `'`, reopen single quote).
*/
export function shellQuote(arg: String): String {
if (arg == '') {
return "''";
}
var allSafe = true;
for (var i = 0; i < arg.length; i += 1) {
if (!isUnquotedShellByte(arg.getByteAt(i))) {
allSafe = false;
break;
}
}
if (allSafe) {
return arg;
}
let sb = new StringBuilder(arg.length + 4);
sb.appendByte(ASCII_SINGLE_QUOTE);
for (var i = 0; i < arg.length; i += 1) {
let b = arg.getByteAt(i);
if (b == ASCII_SINGLE_QUOTE) {
sb.append("'\\''");
} else {
sb.appendByte(b);
}
}
sb.appendByte(ASCII_SINGLE_QUOTE);
return sb.toString();
}
/**
* Appends `args` as space-separated, shell-quoted argument words after
* `command`. Returns `command` unchanged when `args` is empty.
*/
export function appendShellArgs(command: String, args: Array<String>): String {
if (args.length == 0) {
return command;
}
let sb = new StringBuilder();
sb.append(command);
for (let arg in args) {
sb.appendByte(ASCII_SPACE);
sb.append(shellQuote(arg));
}
return sb.toString();
}
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.1-preview.1 | 2026-11-19 |
|