local

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 22 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 local is the JSONL-backed issue store for a repo that tracks work locally instead of in GitHub Issues. It implements gh.IssueStore, so every command runs against it unchanged.

The store is a directory (.hew at the main checkout) holding one issues.jsonl. Every mutation takes an exclusive lock, rewrites the file to a temp name and renames it into place, so concurrent agents in different worktrees never lose each other's writes and a reader never sees a torn file. Reads take no lock for the same reason. A writer waits for the lock only until its context is done or a timeout passes, and opening the store sweeps up temp files a killed writer left behind.

The directory ignores itself, so git treats it as untracked-and-ignored: `git clean -fdx` at the main checkout deletes the only copy of the tracker.

Index

Constants

View Source
const (
	BackendLocal  = "local"
	BackendGitHub = "github"
)

Backend names recorded in config.json.

View Source
const DirName = ".hew"

DirName is the store directory, created at the root of the main checkout.

View Source
const IssuesFileName = "issues.jsonl"

IssuesFileName is the tracker's issue file inside its directory. Init's notice for a recreated file names it, and so may a caller reporting on it.

View Source
const MigratedFile = "migrated.json"

MigratedFile records where each local issue went. It sits beside the archived issues.jsonl, which a finished migration leaves untouched.

Variables

This section is empty.

Functions

func DefaultViewer

func DefaultViewer() (string, error)

DefaultViewer is who the local tracker says this agent is. HEW_USER wins when set: it is the escape hatch for a harness, and it must be unique per concurrent agent, because a shared identity cannot tell "this claim is mine" from "a sibling session holds it". Otherwise the OS login is qualified with the current linked worktree, so two agents under one OS account in different worktrees are distinct claimants and start's exit codes stay meaningful; the main checkout keeps the bare login. It is login-shaped on purpose — it lands in assignee lists and "@user" output, where a git display name with spaces would read badly.

It fails when the checkout cannot be determined rather than falling back to the bare login: the identity would then change between invocations, and a claim of your own would read as someone else's.

func Init

func Init(dir string) error

Init creates a local tracker in dir, leaving anything already there alone so that running it twice is harmless. It never overwrites issues.

func Locate

func Locate(dir string) (trackerDir string, ok bool, err error)

Locate reports the tracker directory when the repository around dir is configured to track issues locally. No `.hew` at all means the repo is not local, and neither does a config that names the `github` backend (a migrated repo keeps its local issues as an archive), a `.hew` that is not a directory, or a directory git says is not in a checkout at all — those are left for the GitHub path to report in its own terms. Anything else that goes wrong is an error, because guessing GitHub there would send writes to the wrong tracker: git failing for another reason, a config that exists but cannot be read, a config naming a backend this build does not know, and a `.hew` holding an issues.jsonl or lock but no config, which is a local tracker that lost the file saying so.

func MainDir

func MainDir(dir string) (string, error)

MainDir is where the tracker lives for the checkout containing dir (the working directory when empty): the `.hew` directory at the main checkout, so every linked worktree of one repository shares it.

func RecreatedIssuesNotice

func RecreatedIssuesNotice(path string) string

RecreatedIssuesNotice is what init says after recreating a lost issues.jsonl.

func UntrackedNotice added in v0.19.0

func UntrackedNotice(dir string) string

UntrackedNotice is what init says when it creates a tracker: nothing outside .hew records that the repo is local, so a deleted .hew is indistinguishable from an opt-in that never happened.

func ViewerFor added in v0.19.0

func ViewerFor(login, worktree string) string

ViewerFor returns the claimant identity for login working in worktree, the root path of a linked worktree (empty for the main checkout): the bare login in the main checkout, and the login qualified with the worktree's tag in a linked worktree. An empty login reads as "local". It reads no environment; DefaultViewer supplies login and worktree from HEW_USER, USER and the checkout.

func WriteConfig

func WriteConfig(dir string, cfg Config) error

WriteConfig replaces dir's config.

Types

type Comment

type Comment struct {
	Author    string
	CreatedAt time.Time
	Body      string
}

Comment is one comment as stored, with the author and time the GitHub comment API cannot carry over.

type Config

type Config struct {
	Backend string `json:"backend"`
	Version int    `json:"version"`
	// ID is the tracker's stable identity, generated once when the tracker is
	// created. Resume checkpoints bind to it rather than the checkout's
	// directory name: two checkouts can share a name, and a rename must not
	// invalidate a checkpoint kept outside the tracker.
	ID string `json:"id,omitempty"`
	// contains filtered or unexported fields
}

Config says which backend owns a repo. It is the switch the commands read: migrating to GitHub flips it, leaving the local issues behind as an archive.

func LocateConfig

func LocateConfig(dir string) (trackerDir string, cfg Config, ok bool, err error)

LocateConfig is Locate plus the config that selected the tracker. A caller that reports the tracker's identity reads it from the config validated here instead of reading the file again and disagreeing with the selection. cfg is the zero Config unless ok is true.

func ReadConfig

func ReadConfig(dir string) (Config, error)

ReadConfig reads dir's config. A missing file satisfies errors.Is(err, fs.ErrNotExist), which callers treat as "this repo is not local".

func (Config) MarshalJSON

func (c Config) MarshalJSON() ([]byte, error)

MarshalJSON writes the fields UnmarshalJSON carried past, after the known ones.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(raw []byte) error

UnmarshalJSON keeps the fields this build does not know, which the typed decode would otherwise drop.

type Frozen

type Frozen struct {
	*Snapshot
	// contains filtered or unexported fields
}

Frozen is a tracker held still for the length of a migration. Every writer takes the same lock, so none can change the issues between the snapshot and the seal; once sealed they find the tracker no longer local and refuse.

func Freeze

func Freeze(ctx context.Context, dir string) (*Frozen, error)

Freeze takes the tracker's exclusive lock and snapshots it, leaving a marker that tells waiting writers a migration holds it. Release it when the migration ends, sealed or not.

func (*Frozen) Release

func (f *Frozen) Release()

Release lets writers back in. Safe to call more than once.

func (*Frozen) Seal

func (f *Frozen) Seal(repo string, mapping map[int]int) error

Seal records where each issue went and flips the tracker to GitHub, leaving issues.jsonl as an archive. The record is written first: if the process dies between the two, the tracker is still local and the migration can be rerun, whereas the other order would strand a migrated tracker with no record.

type InitResult

type InitResult struct {
	// IssuesRecreated is set when issues.jsonl was missing beside an existing
	// config: the tracker lost its issues, and the file is now empty.
	IssuesRecreated bool
}

InitResult reports what InitTracker did beyond creating a fresh tracker.

func InitTracker

func InitTracker(dir string) (InitResult, error)

InitTracker is Init that says whether it had to recreate a lost issues.jsonl. That case also gives the tracker a new id: the empty file starts a new history, and checkpoints bound to the old id would map plan items onto issue numbers that no longer exist.

type Issue

type Issue struct {
	Number      int
	Title       string
	Body        string
	State       string
	StateReason string
	CreatedAt   time.Time
	Labels      []string
	Assignees   []string
	Parent      int
	BlockedBy   []int
	Comments    []Comment
}

Issue is one stored issue with its relationships as numbers and its full comment history, the shape migration reads. State and StateReason are the GraphQL spellings gh uses (OPEN, CLOSED, NOT_PLANNED).

type LockTimeoutError added in v0.19.0

type LockTimeoutError struct {
	Path    string
	Timeout time.Duration
	Detail  string
}

LockTimeoutError reports that a writer waited out its lock timeout. Detail says what was holding the lock, when the store could tell — a running migration, or another hew process — and is empty when it could not. The tracker package maps it to its public LockTimeoutError.

func (*LockTimeoutError) Error added in v0.19.0

func (e *LockTimeoutError) Error() string

type Migrated

type Migrated struct {
	Version    int       `json:"version"`
	Repo       string    `json:"repo"`
	MigratedAt time.Time `json:"migrated_at"`
	// Mapping is local issue number → GitHub issue number.
	Mapping map[int]int `json:"mapping"`
}

Migrated is the migrated.json record.

func ReadMigrated

func ReadMigrated(dir string) (Migrated, error)

ReadMigrated reads dir's migration record. A tracker that has not been migrated satisfies errors.Is(err, fs.ErrNotExist).

type MigratedError added in v0.19.0

type MigratedError struct {
	Dir     string
	Backend string
	Repo    string
}

MigratedError reports a write refused because the tracker no longer says local: a finished migration left issues.jsonl as an archive. Repo is the GitHub repository the migration record names, empty when that record is missing or unreadable. The tracker package maps it to its public MigratedError, so a caller outside this module never names this type.

func (*MigratedError) Error added in v0.19.0

func (e *MigratedError) Error() string

type NotClaimedError added in v0.19.0

type NotClaimedError struct{ Issue int }

NotClaimedError reports a release of an issue nobody holds, found under the store's lock rather than in the caller's earlier read. The tracker package maps it to its public NotClaimedError.

func (*NotClaimedError) Error added in v0.19.0

func (e *NotClaimedError) Error() string

type NotOwnerError added in v0.19.0

type NotOwnerError struct {
	Issue     int
	Assignees []string
	Actor     string
}

NotOwnerError reports a release the acting identity may not make: under the lock the claim is not solely theirs. Assignees is empty for a label-only claim; Actor is the identity the claim was compared with. The tracker package maps it to its public NotOwnerError.

func (*NotOwnerError) Error added in v0.19.0

func (e *NotOwnerError) Error() string

type Options added in v0.19.0

type Options struct {
	// Viewer is the claimant identity. Empty means DefaultViewer, which reads
	// HEW_USER and the process working directory.
	Viewer string
	// Notice receives lock-wait and migration notices. Nil discards them;
	// OpenWithOptions never falls back to stderr.
	Notice io.Writer
	// Context bounds the initial lock a tracker without an id takes while it
	// backfills one. Nil means context.Background().
	Context context.Context
	// LockTimeout bounds how long a write waits for the lock. Zero means the
	// default: long enough that reaching it means the holder is stuck, not
	// busy. A caller that must fail fast, or a test that has to reach the
	// timeout, sets it.
	LockTimeout time.Duration
}

Options configures OpenWithOptions.

type Snapshot

type Snapshot struct {
	Digest string
	Labels []gh.Label
	Issues []Issue
}

Snapshot is a tracker's whole contents at one moment. Digest is a hash of the file's bytes, so a checkpoint made against a snapshot can tell whether the tracker changed underneath it.

func ReadSnapshot

func ReadSnapshot(dir string) (*Snapshot, error)

ReadSnapshot reads the local tracker in dir without locking it. It refuses a tracker that is no longer local: that one is an archive already.

type Store

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

Store is an open local tracker.

func Open

func Open(dir, viewer string) (*Store, error)

Open opens the tracker in dir as viewer, reporting lock waits on stderr. It refuses a directory whose config no longer says local: after migration the issues are an archive, and writing to them would quietly fork the tracker from GitHub.

func OpenWithOptions added in v0.19.0

func OpenWithOptions(dir string, opts Options) (*Store, error)

OpenWithOptions is Open with the identity, notice destination and context injected, for a caller that must not read the environment or write to stderr.

func (*Store) AddAssignee

func (s *Store) AddAssignee(ctx context.Context, number int, login string) error

func (*Store) AddBlockedBy

func (s *Store) AddBlockedBy(ctx context.Context, number, blockingNumber int) error

AddBlockedBy refuses a self-block and a direct two-issue cycle, the cases GitHub's API refuses. Longer cycles are accepted here too: the commands run the transitive check before they write, and the read path reports cycles.

func (*Store) AddLabels

func (s *Store) AddLabels(ctx context.Context, number int, labels []string) error

func (*Store) AddSubIssue

func (s *Store) AddSubIssue(ctx context.Context, parentNumber, childNumber int, replaceParent bool) error

AddSubIssue makes child a sub-issue of parent. An issue has one parent, so an existing different parent is an error unless replaceParent is set. A parent that is the child or one of its descendants is refused: epics are trees.

func (*Store) As added in v0.19.0

func (s *Store) As(viewer string) *Store

As returns a store that claims as viewer, sharing this store's tracker, id and lock. The receiver is unchanged. An empty or unchanged viewer returns the receiver.

func (*Store) ClaimIssue

func (s *Store) ClaimIssue(ctx context.Context, number int, login string, force bool) error

ClaimIssue is the atomic claim: under the store's lock it checks the issue is unclaimed and writes the assignee and the in-progress label together, so two concurrent starts cannot both succeed. It is the Claimer capability Start uses instead of check-then-act.

func (*Store) CloseIssue

func (s *Store) CloseIssue(ctx context.Context, number int, reason gh.CloseReason) error

func (*Store) Comment

func (s *Store) Comment(ctx context.Context, number int, body string) error

func (*Store) CreateIssue

func (s *Store) CreateIssue(ctx context.Context, title, body string, labels []string) (model.Issue, error)

CreateIssue stores raw issue fields. User-facing title and area validation belongs to the shared CLI write path, so local and GitHub commands refuse the same inputs. Labels here can include convention labels such as the in-progress claim; they are not necessarily user-supplied areas.

func (*Store) CreateLabel

func (s *Store) CreateLabel(ctx context.Context, label gh.Label) error

CreateLabel records a label's cosmetics. Creating one that exists is a no-op rather than an error, so bootstrapping is safe to repeat; label names are case-insensitive, so an existing spelling wins.

func (*Store) EditBody

func (s *Store) EditBody(ctx context.Context, number int, body string) error

func (*Store) EditBodyWithCheck added in v0.20.0

func (s *Store) EditBodyWithCheck(ctx context.Context, number int, body string, check func(model.Issue) error) error

EditBodyWithCheck replaces a body only if check accepts the current issue under the write lock. The check must not call back into the store.

func (*Store) EditTitle

func (s *Store) EditTitle(ctx context.Context, number int, title string) error

func (*Store) GetIssue

func (s *Store) GetIssue(ctx context.Context, number int) (model.Issue, error)

func (*Store) ID

func (s *Store) ID() string

ID is the tracker's stable identity, the same from every checkout and across a rename of the directory holding it.

func (*Store) ListIssues

func (s *Store) ListIssues(ctx context.Context, states []gh.IssueState) ([]model.Issue, error)

func (*Store) ListLabels

func (s *Store) ListLabels(ctx context.Context) ([]gh.Label, error)

ListLabels returns the labels created explicitly plus any in use on an issue, so a label applied by hand still shows up.

func (*Store) ReleaseIssueClaim

func (s *Store) ReleaseIssueClaim(ctx context.Context, number int, login string, force bool) error

ReleaseIssueClaim clears a claim under the store lock. The refusals are structured, not formatted strings: the tracker package turns them into its public NotClaimedError and NotOwnerError, and the CLI supplies the wording that names its own --force flag.

func (*Store) RemoveAssignees

func (s *Store) RemoveAssignees(ctx context.Context, number int, logins []string) error

func (*Store) RemoveBlockedBy

func (s *Store) RemoveBlockedBy(ctx context.Context, number, blockingNumber int) error

func (*Store) RemoveLabel

func (s *Store) RemoveLabel(ctx context.Context, number int, label string) error

RemoveLabel treats an absent label as success, as the GitHub client does. Label names are case-insensitive, so the removal matches the same way.

func (*Store) RemoveSubIssue

func (s *Store) RemoveSubIssue(ctx context.Context, parentNumber, childNumber int) error

func (*Store) ReopenIssue

func (s *Store) ReopenIssue(ctx context.Context, number int) error

ReopenIssue records REOPENED as the state reason rather than clearing the close reason, which is what GitHub does.

func (*Store) SearchIssues

func (s *Store) SearchIssues(ctx context.Context, terms string, states []gh.IssueState) ([]model.Issue, int, error)

SearchIssues matches issues containing every whitespace-separated term, case-insensitively, in the title, body or labels, across both states. Issues whose title carries all the terms come first. Qualifiers such as label:bug are GitHub syntax and are matched as literal text here.

func (*Store) Viewer

func (s *Store) Viewer(ctx context.Context) (string, error)

Jump to

Keyboard shortcuts

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