gate

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gate grants the gates: named resources (code generation, the local stack, an end-to-end run, a paid model run), granted only while the machine is not under pressure and has the memory the command peaked at. It also admits each command a worker's Bash runs that neither reads nor edits (Request.Shared, djinn gate hook): shared with the other commands, it waits only for a machine out of pressure and the memory its measured peak needs. The cost of a command, not its gate's name, says how it passes (Q17 B): a light command under a gate's name shares it, as many at once as the machine holds; a command measured heavy (Heavy) holds its gate alone and goes one at a time, whatever its gate: it also waits for the heavy command another task runs. A lit ifrit, or a request that names no command, holds its gate alone too. Djinn's own requests to merge a task's work, settle a merge or check a push go first, the first come first; then the others, the first wish of the rank first (Q26), then the work of its first azima (Task.rank). A holder keeps its gate until it gives it back, its connection ends, its process ends or the timeout it asked for passes: none is taken back for a request that goes first, nor for holding long (G5 of L04); one held past 3 times its usual hold, or an hour unmeasured, is told to the lead (Tasks.HoldsLong). A lit ifrit's gate has no timeout: it is held until the ifrit goes out or ends, at its out time at the latest (T51), and goes to no one first for it. A gate held outside a running worker takes a worker's slot.

Index

Constants

View Source
const (
	LongTimes      = 3
	MinLongHold    = 10 * time.Minute
	UnmeasuredLong = time.Hour
)

A hold that asked for no timeout is never taken back while its holder lives: past LongTimes its usual hold, the 95th percentile of its command's measured holds, but never before MinLongHold, or past UnmeasuredLong when its command was never measured, its task's lead is told (Tasks.HoldsLong).

View Source
const (
	DefaultHeavyMemory = 2 << 30
	DefaultHeavyCPU    = time.Minute
)

A command is heavy when it was measured past DefaultHeavyMemory of peak memory or DefaultHeavyCPU of CPU time, unless Heavy says otherwise (the window's settings).

View Source
const Commands = harness.AdmissionGate

Commands is the name the admissions of the workers' commands go by (Request.Shared): in Held, Waiting, a task's events and the intervals of the gates.

Variables

This section is empty.

Functions

func Handler

func Handler(g *Gates) (string, http.Handler)

Handler returns the Connect handler of GateService on g, and its path prefix.

func Hook added in v0.4.0

func Hook(in io.Reader, out io.Writer, admit func(what, dir, match string) (string, error)) error

Hook is Claude's PreToolUse hook on Bash and Monitor: it reads the call from in and, when its command neither reads nor edits (Plain), has admit wait for its admission, given the command as its cost is kept (machine.Key), its folder and what its shell is found by (machine.Match). admit says why the command waited, "" when it did not: the hook then writes it to out, for the worker to read beside the command's result. It writes nothing else: Claude runs the call as it was. It fails only when it cannot read the call; admit failing (no Djinn answers) lets the command run.

func Name

func Name(s string) string

Name is a gate's name as Djinn keeps it: names ignore case.

func Plain added in v0.4.0

func Plain(command string) bool

Plain says whether command only reads or edits, its every part being a command of readHeads or a reading git: it passes with no admission. One it cannot read through, a substitution, a subshell, a find that runs or deletes, is not plain.

func Run

func Run(ctx context.Context, client machinev1connect.GateServiceClient, name, taskID string, c Command) (int, error)

Run takes the gate name from the server, for the task taskID when not empty, runs the command once it holds the gate, and gives the gate back when the command ends, fails or is interrupted. It returns the command's exit code. When the caller is killed, its connection ends, and the server gives the gate back.

func Shown added in v0.4.0

func Shown(iv *machinev1.GateInterval) bool

Shown says whether the interval iv goes in the series of the gates unless all the admissions are asked for: every gate, shared or held alone, and an admission only when it waited a second or more or its command is heavy; the many that pass at once would hide the gates from the Hourglass's frieze. Every interval is kept all the same (Log).

Types

type Command

type Command struct {
	Args                   []string
	Dir                    string
	Stdin                  io.Reader
	Stdout, Stderr, Notice io.Writer // Notice gets what djinn says: why it waits, a gate lost
	// Costs, when not nil, records what the command cost once it ended by itself: CPU time, peak memory, duration.
	Costs machinev1connect.CommandServiceClient
}

Command is a command to run under a gate, with its standard streams.

type Costs

type Costs interface {
	Cost(ctx context.Context, taskID, dir, command string) machine.Cost
	Ask(ctx context.Context, gate, taskID, dir, command string)
	Record(ctx context.Context, run *machinev1.CommandServiceRecordRequest)
}

Costs gives what a command was measured to cost, in its project: the task's, else the one holding dir; zero when never measured. Ask counts a run of the command asked under the gate, whether it then runs to its end or not. Record counts a run Djinn measured itself: an admitted command, which no djinn gate run waits for. machine.Peaks is one.

type Gates

type Gates struct {
	// contains filtered or unexported fields
}

Gates grants the gates. Its zero value is not usable: New makes one.

func New

func New(m Machine, costs Costs, tasks Tasks) *Gates

New returns the gates of machine m (nil: never under pressure, its memory unknown), the commands' measured costs being costs (nil: none measured), the holders' tasks being tasks (nil: none known).

func (*Gates) Freed

func (g *Gates) Freed(f func())

Freed calls f each time a gate is given or taken back: a slot may have freed. djinn up wakes the scheduler.

func (*Gates) Heavy added in v0.4.0

func (g *Gates) Heavy(memory uint64, cpu time.Duration)

Heavy sets when a command is heavy: measured past memory bytes of peak memory, or cpu of CPU time; 0 keeps the default of either. djinn up reads them in the window's settings.

func (*Gates) Held

func (g *Gates) Held(taskID string) []string

Held gives the names of the gates the task holds now, alone or shared, in order, Commands among them while one of its commands is admitted: its worker is neither paused, nor stopped for a slot, nor judged silent meanwhile.

func (*Gates) IfritWaits added in v0.2.0

func (g *Gates) IfritWaits() map[string][]string

IfritWaits names, by the task of each lit ifrit, who waits for a gate it holds, the first come first: "W15 (Its title): go tool task e2e, for stack". The lead's brief says it, to offer the developer to end the ifrit.

func (*Gates) Intervals added in v0.1.1

func (g *Gates) Intervals(cutoff time.Time, all bool) []*machinev1.GateInterval

Intervals gives the intervals of the gates released after cutoff, the first released first, then those held now: all of them when all is true, else those Shown says.

func (*Gates) Keep added in v0.4.0

func (g *Gates) Keep(log Log)

Keep keeps the intervals of the gates in log: djinn up gives machine.Intervals, which keeps them in the store.

func (*Gates) List

func (g *Gates) List() []State

List gives the gates held or waited for, by name, alone or shared; the admissions are not listed.

func (*Gates) Measure

func (g *Gates) Measure(every time.Duration, read Read)

Measure reads, every interval, what each holder whose process is known uses, with read: djinn up gives machine.ReadWorker, where the system lets it measure (machine.NotMeasured).

func (*Gates) Outside

func (g *Gates) Outside() (n, merges int)

Outside counts the gates held outside any running worker, by a person's terminal, a lead, a script or Djinn's merges and pushes, and of them the merges' and pushes' (Request.First): each takes a worker's slot, so no new worker starts on it while it is held. A worker's gate is in the worker's own slot. A gate held shared, an admission or a light command under a gate's name, takes no slot (G9 of L04): only one held alone does, and a heavy admission.

func (*Gates) ProjectResolver added in v0.1.1

func (g *Gates) ProjectResolver(fn func(taskID, dir string) string)

ProjectResolver sets the function that resolves a task and directory to a project name.

func (*Gates) Take

func (g *Gates) Take(ctx context.Context, r Request, waiting func(why string)) (*Hold, error)

Take waits until the gate is granted, and returns it held. waiting is called each time the reason to wait changes. Take fails when ctx ends first.

func (*Gates) Track added in v0.1.2

func (g *Gates) Track(f func(pid int, taskID string) func())

Track calls f with the process of each worker's command that holds a gate, djinn gate run, and its task, as it takes it; what f returns is called once the gate is given back. djinn up records them: none outlives it (W514).

func (*Gates) Waiting

func (g *Gates) Waiting(taskID string) []string

Waiting gives the names of the gates the task waits for now, in order, Commands among them while one of its commands waits for its admission.

type Hold

type Hold struct {
	// contains filtered or unexported fields
}

Hold is a gate held.

func (*Hold) Done

func (h *Hold) Done() <-chan struct{}

Done is closed once the gate is no longer held: given back, or taken back (TakenBack says why).

func (*Hold) Give

func (h *Hold) Give()

Give gives the gate back; calling it again, or once Djinn took it back, does nothing.

func (*Hold) TakenBack

func (h *Hold) TakenBack() string

TakenBack says why Djinn took the gate back before its holder gave it: its process ended, it held it past its timeout, or its ifrit went out or ended; "" when it did not.

type HookInput added in v0.4.0

type HookInput struct {
	Tool  string         `json:"tool_name"`
	Input map[string]any `json:"tool_input"`
	Cwd   string         `json:"cwd"`
}

HookInput is what Claude's PreToolUse hook reads on its standard input: the tool and its input, the folder.

type Log added in v0.4.0

type Log interface {
	Add(ctx context.Context, iv *machinev1.GateInterval)
	Since(ctx context.Context, cutoff time.Time) []*machinev1.GateInterval
}

Log keeps the intervals of the gates: Add one released, Since those released after cutoff, the first released first. machine.Intervals keeps them in the store, for a week; without one, the last 2,000 stay in memory.

type Machine

type Machine interface {
	Snapshot() machine.Snapshot
	Policy() machine.Policy
}

Machine is what the gates need from the machine: its pressure, its memory, and the policy that judges them. The monitor is one.

type Read

type Read func(pid int) (machine.Group, error)

Read reads what the process pid uses, with every process it started: machine.ReadWorker.

type Request

type Request struct {
	Name string
	// TaskID, when not empty, is the task the gate is taken for: its events say when it waits, takes and gives the
	// gate back.
	TaskID string
	// What says what the holder runs, in the folder Dir: a command measured in its project waits until the machine
	// has the memory it peaked at.
	What, Dir string
	// PID is the holder's process, 0 when unknown: it is measured while it holds the gate, and the gate goes back
	// once it ends.
	PID int
	// Timeout is how long the gate may be held: Djinn takes it back after that. 0: none, the gate held as long as its
	// holder lives, its task's lead told when it holds long (LongTimes); a lit ifrit holds its gates until it goes out
	// or ends.
	Timeout time.Duration
	// First tells that Djinn takes the gate to merge a task's work into its branch (its wish's or its azima's), to
	// settle such a merge, or to check a push: it blocks what comes after the task, so it goes before every other
	// request, the first come first, and never waits for one that came after it (Q26).
	First bool
	// Shared asks an admission rather than a gate, for the command a worker's Bash runs (djinn gate hook): Name is
	// left out, the admission goes by Commands. It waits only for a machine out of pressure and the memory its
	// measured peak needs, in the gates' order, and is held by as many as it admits; a command measured heavy waits
	// for the heavy command of another task. It needs PID or Find: the admission is held until that process ends.
	// A gate's name asked for a light command (What, not measured heavy) is shared the same way, without being asked:
	// it waits only for one that holds its name alone (a heavy command, a lit ifrit) and a merge that goes first.
	Shared bool
	// Find, when PID is 0, finds the process of the admitted command, leaving out those skip says, once it starts:
	// 0 while it is not found. The admission is given back when no process is found within a few seconds: its
	// command ran too short to be found, or did not run.
	Find func(skip func(pid int) bool) int
}

Request is what a holder asks a gate for.

type State

type State struct {
	Name, Holder, HolderTaskID string
	Since, Until               time.Time // Until: when it is taken back at the latest; zero when not known
	Waiting                    []string
	WaitingTaskIDs             []string          // the tasks of the waiters, in the same order; a waiter outside any task left out
	TakesSlot                  bool              // held alone outside a running worker
	Shared                     int               // how many light commands share it; Holder is the first of them unless one holds it alone
	Ifrit                      bool              // held by a lit ifrit, until it goes out or ends
	Use                        *planv1.Resources // what the holder's process uses; nil when not read
}

State is a gate held or waited for.

type Tasks

type Tasks interface {
	Describe(ctx context.Context, taskID string) (string, error)
	Ranks(ctx context.Context, taskIDs []string) map[string]int
	// Places gives the position of each task's azima among the azimas of its wish (Task.rank), from 0; a task part of
	// no azima, or unknown, is not in the map.
	Places(ctx context.Context, taskIDs []string) map[string]int
	Note(taskID string, ev harness.Event)
	Works(taskID string) bool
	// HoldsLong tells the lead of the task's wish that its holder holds a gate long, line saying which and how long:
	// nothing is taken back. It must not call the gates back.
	HoldsLong(taskID, line string)
	// Ifrit tells whether the task is an ifrit lit now, and when it goes out unless the developer comes back (zero:
	// not known yet); once it no longer is, why says why: "went out: …", "ended". A task that is no ifrit is not lit,
	// and has no why. It must not call the gates back.
	Ifrit(ctx context.Context, taskID string) (lit bool, out time.Time, why string)
}

Tasks is what the gates need from the tasks: to name a holder, to rank waiters by their wish and their azima, to tell a task's events, whether a task's worker runs, which holds its gates in its own slot, and whether a task is a lit ifrit, which holds its gates until it goes out or ends. The harness is one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL