# `core/charts/repairs.ts`

<p class="generated-note">Generated from the exports of <a href="https://github.com/altasystem-ai/alta/blob/main/core/charts/repairs.ts"><code>core/charts/repairs.ts</code></a> · <code>npm run docs:gen</code></p>

Repairs of alta.chart/0.1 (docs/standards/alta-chart-0.1.md §13): the closed list of repair kinds a chart may declare, and which of them the owner may allow to run without being asked each time (a standing grant). Pure.

**Standards that cite this file**: [alta.chart/0.1: the Agent Chart](https://docs.altasystem.ai/reference/advanced/specs/alta-chart-0.1)

## Exports

### `RepairKind` 
```ts
type RepairKind =
  | "service.restart" | "model.pull" | "signin.refresh" | "gateway.recover" | "automation.rerun" | "channel.reconnect"
  | "part.reapply" | "signin.again" | "channel.pair";
```

### `RepairKindInfo` 
```ts
interface RepairKindInfo {
  kind: RepairKind;
  /**
   * The executor that does it; "part" and "signin": the executor of the part or the sign-in the repair names.
   */
  executor: string | "part" | "signin";
  /**
   * "yes": the owner may tick its box. "not-yet": in the closed list, but no box is offered until the real command of
   * each tool was verified on a machine. "never": always a plan card or an answer of the owner.
   */
  grant: "yes" | "not-yet" | "never";
  /** The arguments the declaration gives (literals only), each one naming something the chart declares. */
  args: readonly string[];
  /** Seconds to wait after a run before the proof is read again (model.pull: until its job ends). */
  settleS: number;
  /** Runs allowed per repair id and per 24 h. */
  dailyCap: number;
}
```

### `REPAIR_KINDS` 
The closed list. Owner's decision (5 December): at launch only service.restart, model.pull and gateway.recover can
be granted. signin.refresh and channel.reconnect stay "not-yet" until a real command of each tool was verified;
automation.rerun is always asked (a run may cost a model call).

```ts
const REPAIR_KINDS: readonly RepairKindInfo[]
```

### `REPAIR_KIND_IDS` 
```ts
const REPAIR_KIND_IDS: readonly string[]
```

### `repairKind` 
```ts
const repairKind = (k: unknown) => …
```

### `grantable` 
```ts
const grantable = (k: unknown) => …
```

### `Repair` 
A repair a chart declares.

```ts
interface Repair { id: string; kind: RepairKind; args?: Record<string, string | number | boolean>; title?: { en?: string; fr?: string }; suggest?: boolean }
```

### `GRANTS_SCHEMA` 
```ts
const GRANTS_SCHEMA = "alta.grants/0.1"
```

### `Grant` 
One standing yes: one repair id of one chart, for the declaration it had when the owner ticked the box.

```ts
interface Grant {
  id: string;
  chart: string;
  repair: string;
  kind: RepairKind;
  /** sha256 of grantSubject(chart, repair): a new version that changes the repair or its target voids it. */
  digest: string;
  grantedAt: string;
  /** Always null: a grant lasts until the owner revokes it (owner's decision, 5 December). */
  expiresAt?: null;
}
```

### `GrantsFile` 
```ts
interface GrantsFile { schema: typeof GRANTS_SCHEMA; paused: boolean; grants: Grant[] }
```

### `emptyGrants` 
```ts
const emptyGrants = (): GrantsFile => …
```

### `checkGrants` 
Problems of a grants file (`grants.*`): the server refuses to use a file that has one.

```ts
function checkGrants(g: unknown): string[]
```

### `AliveLike` 
```ts
interface AliveLike { id: string; grace?: number; repair?: string[] }
```

### `grantSubject` 
What a grant's digest is the sha256 of (the caller hashes it): null when the chart has no such repair.

```ts
function grantSubject(chart: ChartLike, repairId: string): string | null
```

### `grantFor` 
The owner's grant for this repair of this chart, if it still holds (same kind, same digest, grantable).

```ts
function grantFor(grants: Pick<GrantsFile, "grants">, chartId: string, repair: Repair, digest: string | undefined): Grant | undefined
```

### `REPAIR_RULES` 
The decision table (docs/standards/alta-chart-0.1.md §13), in one place.

```ts
const REPAIR_RULES = { /** Failures in a row before an incident opens, when the proof gives no `grace`. */ grace: 2, /** The delay before …
```

### `Incident` 
An open incident: an alive proof failed `grace` times in a row. It closes only when the proof passes again.

```ts
interface Incident {
  chart: string;
  proof: string;
  openedAt: number;
  /** Failures in a row. */
  failures: number;
  /** Per repair id: the attempts of this incident and when the last one ran. */
  attempts: Record<string, { count: number; last: number }>;
  /** The last run, for the settle time. */
  lastRun?: { repair: string; kind: RepairKind; at: number };
}
```

### `RepairHistory` 
What happened before, read from the ledger and the guard's state.

```ts
interface RepairHistory {
  /** Automatic runs: one per run, on this machine. */
  runs: { at: number; chart: string; repair: string; kind: RepairKind }[];
  /** Incidents opened (this one included), per proof. */
  incidents: { at: number; chart: string; proof: string }[];
}
```

### `RepairState` 
```ts
interface RepairState {
  /** The machine switch `--allow-repair`. */
  capOn: boolean;
  /** The owner paused every automatic repair (grants.json `paused`). */
  paused: boolean;
  /** Why the machine is busy, or null: an apply holds the engine's lock, a job of the chart runs, the owner has a run open on the same target. */
  busy: null | "apply" | "job" | "owner-run";
  /** sha256 of grantSubject(chart, repair id), per repair id of the chart. */
  digests: Record<string, string>;
}
```

### `RepairDecision` 
```ts
type RepairDecision =
  | { do: "run"; repair: Repair; grant: Grant; attempt: number }
  | { do: "wait"; until: number; reason: "grace" | "settle" | "backoff" | "busy" }
  | { do: "ask"; repairs: string[]; reason: "not-granted" | "nothing-declared" | "switch-off" | "paused" | "flapping" | "daily-cap" | "circuit-breaker"; pause?: true }
  | { do: "give-up"; reason: "RepairGaveUp"; condition: "Drifted" };
```

### `nextIncident` 
The incident after one reading of the proof: null when it passes (the only success), else one more failure.

```ts
function nextIncident(prev: Incident | null, chart: string, proof: string, passed: boolean, now: number): Incident | null
```

### `decideRepair` 
What the guard loop does for one failing alive proof, now. Pure: every rule of the table, in this order: grace,
the proof's repairs, the machine switch and the pause, flapping, the circuit breaker, busy, settle, then each repair
of the proof's list in order (granted, under its daily cap and its three attempts, after its backoff).

```ts
function decideRepair(incident: Incident, chart: ChartLike, grants: Pick<GrantsFile, "grants">, history: RepairHistory, state: RepairState, now: number): RepairDecision
```
