{"modulePath":"github.com/kernel/hypeman","version":"v0.4.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package phasetracking\n\nPackage phasetracking accumulates cumulative time-in-phase for instance lifecycle phases. The tracker is embedded in instance metadata and updated at every externally-observable state transition so consumers can use the resulting durations for billing, observability, and analytics.\n\nPhases mirror the externally-observable values of instances.State (lowercased so they remain stable in the API surface even if the internal enum is renamed). The Initializing→Running transition is detected lazily when guest boot markers are persisted, so the tracker reflects the same view of guest readiness that the public State machine reports — not the bare moment the VMM process came up.\n\nTransient internal substates that no external observer can see — for example the Paused/Shutdown steps inside a single Standby or Stop orchestration — are intentionally not recorded; they would be sub-millisecond blips inside a non-yielding function call that adds noise without truth.\n\nOnly the transition orchestration sites in lib/instances should call Record. The tracker intentionally does not subscribe to the lifecycle event bus — that bus is best-effort and lossy, which is unsuitable for billing.\n\n## Types\n\n```go\ntype Phase string\n```\nPhase is the canonical lifecycle phase name. Values mirror instances.State lowercased so they remain stable in the API surface even if the internal State enum is renamed.\n\n```go\nconst (\n\tPhaseStopped      Phase = \"stopped\"\n\tPhaseCreated      Phase = \"created\"\n\tPhaseInitializing Phase = \"initializing\"\n\tPhaseRunning      Phase = \"running\"\n\tPhaseStandby      Phase = \"standby\"\n)\n```\n\n```go\ntype Tracker struct {\n\tCurrent    Phase           `json:\"current,omitempty\"`\n\tSince      time.Time       `json:\"since,omitempty\"`\n\tCumulative map[Phase]int64 `json:\"cumulative,omitempty\"`\n}\n```\nTracker accumulates cumulative wall-clock time spent in each phase.\n\nInvariants:\n\n  - Cumulative\\[phase] is the total ms spent in \\`phase\\` across all prior completed visits to that phase.\n  - Time spent in the \\*current\\* phase (since \\`Since\\`) is NOT yet rolled into Cumulative — callers that want \"live\" totals should use Snapshot.\n  - Current and Since must be updated atomically with Cumulative; that's the contract of Record. Direct mutation is not supported.\n\nThe zero value is valid: it represents an instance that has not entered any phase yet. The first Record call sets Current and Since without accruing time (there is no prior phase to accrue from).\n\n```go\nfunc (t Tracker) Clone() Tracker\n```\nClone returns a deep copy of the tracker. The returned tracker shares no state with the receiver, so independent Record/Reset calls do not interfere.\n\n```go\nfunc (t *Tracker) Record(newPhase Phase, now time.Time)\n```\nRecord transitions into newPhase as of \\`now\\`, first accruing time-in-current into Cumulative. Safe to call on a zero-value Tracker (first transition has no prior phase, so no accrual happens).\n\n\\`now\\` is a parameter rather than time.Now() so tests can pin time and so callers can use the same \\`now\\` value they're persisting elsewhere on the metadata (e.g. StartedAt) without drift.\n\n```go\nfunc (t *Tracker) Reset()\n```\nReset clears all accumulated state. Used when forking — the fork is a new instance and must not inherit the source's phase history.\n\n```go\nfunc (t Tracker) Snapshot(now time.Time) map[Phase]int64\n```\nSnapshot returns a copy of Cumulative with the in-flight time-in-current rolled in, without mutating the tracker. Use this when reporting \"running time so far\" — typically in the API response path.\n\n","path":"github.com/kernel/hypeman/lib/instances/phasetracking","name":"phasetracking","synopsis":"Package phasetracking accumulates cumulative time-in-phase for instance lifecycle phases.","isRedistributable":true}
