sessionprocess

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package sessionprocess tracks session-owned OS processes independently of agent lifetime.

Index

Constants

View Source
const (
	TreeRoleLead    = "lead"
	TreeRoleWrapper = "wrapper"
	TreeRoleHelper  = "helper"
)

Tree roles: the lead names the tree, wrappers launched it, helpers run beside or below it.

View Source
const CaptureTimeout = 2 * time.Second

CaptureTimeout bounds best-effort ownership capture before keeping descendants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Identity

type Identity struct {
	PID       int
	StartedAt time.Time
}

type Observation

type Observation struct {
	Identity
	Owner           Owner
	PGID            int
	FirstSeen       time.Time
	GroupObservedAt time.Time
	Attribution     string
	KernelToken     string
}

Observation deliberately has no session foreign key: deleting a session keeps its processes discoverable.

type Owner

type Owner struct {
	SessionID, WorkspaceID, ProfileID, Name string
	LeaderPID                               int
	LeaderStartedAt                         time.Time
	LeaderKernelToken                       string
	Live, ToolBusy, Starting                bool
}

type Process

type Process struct {
	procutil.ScannedProcess
	Owner                         Owner
	Attribution, Kind, TerminalID string
	// Tree is the identity of the tree's root; TreeRole is lead, wrapper, or helper.
	Tree     Identity
	TreeRole string
}

type Result

type Result struct {
	Identity
	Outcome, Error string
}

type Scope

type Scope int

Scope selects what a session-wide stop covers besides its explicit targets.

const (
	// Targets stops only the trees of the given targets.
	Targets Scope = iota
	// AllLeftRunning adds every Background and Terminal tree; tool calls stay with their turn.
	AllLeftRunning
	// AllIncludingTools adds every tree, tool calls included (stop, archive, delete cleanup).
	AllIncludingTools
	// Captured stops exactly the given identities, never their trees' later members: delayed
	// cleanup captured them before the session could resume and start new work.
	Captured
)

type Service

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

func New

func New(
	ctx context.Context,
	source Source,
	persistence Store,
	logger *slog.Logger,
	stopBudget time.Duration,
) (*Service, error)

func (*Service) Acquire

func (s *Service) Acquire() func()

func (*Service) AfterStop

func (s *Service) AfterStop(sessionID string, await func(context.Context) error)

AfterStop owns and joins cleanup after an accepted stop outlives its caller.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context) (Snapshot, error)

func (*Service) Shutdown

func (s *Service) Shutdown()

func (*Service) Summary

func (s *Service) Summary(sessionID string) Summary

func (*Service) Terminate

func (s *Service) Terminate(
	ctx context.Context,
	sessionID string,
	targets []Identity,
	scope Scope,
	allowed func(Owner) bool,
) ([]Result, error)

Terminate resolves targets only inside the caller's authorized ownership set and stops each target's whole tree. An empty session ID is reserved for the operator aggregate boundary.

type Snapshot

type Snapshot struct {
	Supported bool
	ScannedAt time.Time
	Duration  time.Duration
	Processes []Process
}

type Source

type Source interface {
	ProcessActivity() (live bool, toolBusy bool)
	Owners(context.Context) ([]Owner, error)
	Owner(context.Context, string) (Owner, bool, error)
	OrphanOwner(context.Context, procutil.ScannedProcess) (Owner, bool, error)
	Terminals(context.Context) ([]Terminal, error)
	CloseTerminal(context.Context, Process) error
	ProcessesChanged(context.Context, string)
	RecordTermination(context.Context, Process, Result) error
}

type Store

type Store interface {
	LoadProcessObservations(context.Context) ([]Observation, error)
	SaveProcessObservations(context.Context, []Observation, []Identity) error
}

type Summary

type Summary struct {
	Supported             bool
	Background, Terminals int
	// Trees counts the Background and Terminal trees: what the session left running.
	Trees int
	Ports []int
}

type Terminal

type Terminal struct {
	PID       int
	StartedAt time.Time
	ID        string
	Owner     Owner
}

type Tree

type Tree struct {
	Root, Lead Identity
	SessionID  string
	Kind       string
	// Wrappers run above Lead, outermost first; Members lists every process, Root first.
	Wrappers []Identity
	Members  []Identity
	Ports    []int
}

Tree is one logical thing a session started: a dev server behind `bun run`, a browser with its helpers, a Terminal. Stopping it signals every member; its kind is the root's kind.

func Trees

func Trees(rows []Process) []Tree

Trees groups rows that assignTrees labeled, ordered by root start. Ports are the members' union.

Jump to

Keyboard shortcuts

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