File size: 851 Bytes
06c3755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ADR-002: the gate is the funnel's valve.
// Running → flow continues.
// Tripped → the valve is closed; the caller (IM) MUST terminate.

import type { State } from './state.js'
import type { GuardHit } from './guards.js'

export type GateOk = { ok: true }

export class ShellTerminatedError extends Error {
  readonly state: State
  readonly hits: GuardHit[]

  constructor(state: State, hits: GuardHit[]) {
    const summary = hits.length > 0
      ? hits.map(h => `${h.id}:${h.reason}`).join('; ')
      : 'shell terminated'
    super(`Shell terminated (${state}) — ${summary}`)
    this.name = 'ShellTerminatedError'
    this.state = state
    this.hits = hits
  }
}

export const gate = (state: State, hits: GuardHit[]): GateOk => {
  if (state === 'Running') {
    return { ok: true }
  }
  throw new ShellTerminatedError(state, hits)
}