Skip to content

Latest commit

 

History

History
151 lines (110 loc) · 6.65 KB

fc_timer.md

File metadata and controls

151 lines (110 loc) · 6.65 KB

fc_timer.sh

Revision: 1 Last modified: 2026-12-04T19:00:00Z

Overview

This is a library you source, not a command you run. Sourcing it defines functions; it has no useful standalone main, and invoking it directly exits 1 with a usage message.

fc_timer.sh is the monotonic per-section / per-gate timer for fastcycle instrumentation. source it once near the top of any bash script — a pre-build suite, a commit wrapper, an exporter, a test runner — and wrap each timeable unit of work in fc_timer_start <id> … fc_timer_end.

Every completed pair appends exactly one row to a caller-supplied TSV. Never a fixed or guessed row count: as many rows as there were completed units, and no more.

Why a shared library rather than per-script timing code: the elapsed figures feed the cycle-time report and the SC-002 gate-speed claims, so they must be comparable across every instrumented script. Two scripts timing themselves slightly differently would produce a series that cannot be aggregated.

Prerequisites

  • bash (uses arrays, BASH_SOURCE, EPOCHREALTIME-class nanosecond clocks).
  • FC_TIMER_TSV — required unless timing is disabled. The caller sets it to the TSV to append to.
  • No other environment variable is required.

Usage examples

# source once, near the top
. "$(dirname "$0")/../constitution/scripts/fastcycle/timing/fc_timer.sh"

# tell it where rows go
export FC_TIMER_TSV="qa-results/fastcycle/${FC_TIMER_RUN_ID}/prebuild_sections.tsv"

fc_timer_init

fc_timer_start "section:governance"
run_governance_sections
fc_timer_end "section:governance"

# a single gate that may run long
fc_timer_gate_start "gate:cm_zero_findings_sweep"
bash scripts/gates/cm_zero_findings_sweep.sh
fc_timer_gate_end "gate:cm_zero_findings_sweep"

# always flush what is open
fc_timer_install_exit_flush

Exported surface

Control

Function Contract
fc_timer_init Initialises the run (run id, fingerprint, TSV path). Call once, early.
fc_timer_enabled Reports whether timing is active (so a caller can skip work cheaply).
fc_timer_reset Clears in-memory state.
fc_timer_install_exit_flush Installs a trap so frames still open at exit are flushed rather than lost.

Timing pairs

Function Contract
fc_timer_start <id> / fc_timer_end <id> Open and close a generic timed unit.
fc_timer_gate_start <id> / fc_timer_gate_end <id> The gate-specific pair (a long, check-bearing unit). Use the matching end for the start.
fc_timer_track <id> -- <cmd...> Times a command in one call.
fc_timer_run <...> Convenience wrapper around a tracked run.
fc_timer_close_all Closes every open frame (used by the exit flush).

Introspection

Function Contract
fc_timer_run_id The current run id.
fc_timer_candidate_fingerprint The candidate fingerprint every row is keyed to.
fc_timer_stack_depth How many frames are currently open.
fc_timer_rows_written How many rows have been appended so far.

The _fc_timer_* prefixed helpers are internal and not part of the caller contract.

Row shape

One row per completed pair, DEC-05's shape plus the fastcycle additions:

Column Meaning
run_id The run this row belongs to.
candidate_fingerprint T-A01's requirement that every row be keyed to the candidate fingerprint.
section (the id) The <id> you passed to start/end.
start_ns, end_ns Monotonic nanosecond timestamps.
duration_ms Convenience elapsed column.
verdict PASS / FAIL / WARN for that unit.
checks, fails, warns Count deltas attributable to that unit.
extra Trailing free-form column, so an integration (for example a per-remote push row) can attach fields this generic mechanism does not know about without changing the column count of any other row.

The trailing extra column is why the schema can be extended without breaking existing consumers.

Exit codes

As a sourced library it defines functions and returns normally; it does not exit.

Invoked directly with no valid usage it prints a usage message and exits 1 — it has no useful standalone behaviour, so a direct invocation is a caller error.

Edge cases

  • FC_TIMER_TSV not set. Timing cannot record; set it or leave timing disabled. Do not let the row land in a guessed path.
  • A script that exits with frames still open. fc_timer_install_exit_flush closes and flushes them; without it, an interrupted unit leaves no row (loss, not a wrong row).
  • Failed creation of the TSV directory. Handled at row-write time, not assumed at init.
  • An unusual signal environment. A directive worth noting: an earlier iteration had an exported _FC_TIMER_SIGNAL_RC=143 in a parent that made a clean exit 0 record a failure. The flush path is written to end cooperatively (rather than forcing exit 130), so a clean exit stays clean.
  • fc_timer_gate_end after fc_timer_start. Mismatching the pair is a caller error; use the same family for start and end.
  • Nested frames. A stack is maintained, so nesting is supported and fc_timer_stack_depth reports it.

Internal behaviour

  1. On source, sets defaults and records the clock source.
  2. fc_timer_init resolves the run id, candidate fingerprint and TSV path, ensuring the TSV directory exists at first write.
  3. fc_timer_start pushes a frame with a monotonic start timestamp.
  4. fc_timer_end pops the matching frame, reads the monotonic end timestamp, and appends exactly one row (run id, fingerprint, id, start/end ns, duration, verdict, checks/fails/warns deltas, extra).
  5. fc_timer_install_exit_flush installs a trap calling fc_timer_close_all so open frames are written rather than dropped.

Related scripts

Sourced by fastcycle instrumented scripts and test runners; the pre_build_verification.sh suite is the canonical large consumer (its 183 sections produced the 1133.78 s figure quoted in collect_baseline.py's reduced-scope note).

Self-tests: scripts/fastcycle/timing/fc_timer_selftest.sh (standalone self-test) and scripts/fastcycle/timing/fc_timer_selftest_mutations.sh (paired §1.1 mutation runner).

Related: scripts/fastcycle/lib/host_guard.sh is used by callers that fan out timed gates.

See also docs/scripts/cycle_report.py.md (consumes the cycle-time evidence these rows feed) and docs/scripts/collect_baseline.py.md.

Last verified

2026-12-04 — bash -n scripts/fastcycle/timing/fc_timer.sh → exit 0. bash scripts/fastcycle/timing/fc_timer.sh invoked directly (not sourced) prints a usage message and exits 1.