Documentation
¶
Overview ¶
Package workspace manages execution workspaces for runs (方案 §4.8): one workspace per work item below a configured root, its lifecycle hooks, and the path invariants that keep an agent inside its workspace. A git project uses a linked worktree; a directory without git uses a plain directory.
The package is pure filesystem and git: it knows nothing about .devsys state, runs or events. The application service binds it to the project and records what happened.
Index ¶
- Constants
- Variables
- func BranchSHA(root, branch string) (string, error)
- func GitProject(dir string) bool
- func HeadSHA(dir string) (string, error)
- func HookEnv(projectRoot, workspacePath, branch, identifier, hookName string) []string
- func Key(identifier string) string
- func Root(projectRoot, configured string) (string, error)
- func Validate(root, path string) error
- type EnsureOptions
- type GitError
- type Hook
- type HookError
- type HookReport
- type Listing
- type RemoveOptions
- type RemoveReport
- type RunHookOptions
- type Workspace
Constants ¶
const ( HookAfterCreate = "after_create" HookBeforeRun = "before_run" HookAfterRun = "after_run" HookBeforeRemove = "before_remove" )
Hook names and semantics (方案 §4.8):
after_create workspace newly created fatal: abort creation before_run before each attempt fatal: abort the attempt after_run after each attempt logged only before_remove before workspace deletion logged only
Variables ¶
var ErrGitMissing = errors.New("git is not available on PATH")
ErrGitMissing reports that the git executable is not on PATH. Git is an optional capability: worktree operations need it, directory workspaces do not.
Functions ¶
func GitProject ¶
GitProject reports whether dir is inside a git work tree and git is on PATH. Directory workspaces are used when this is false.
func HeadSHA ¶
HeadSHA is the commit a checkout currently points at (the completion check compares the claim head with it, 方案 §4.8).
func HookEnv ¶
HookEnv is the environment a hook (or an attempt) sees, so a script does not have to parse the policy or guess where it is running (方案 §4.8).
func Key ¶
Key derives the workspace directory name for an identifier (方案 §4.8).
Every character outside [A-Za-z0-9._-] becomes '_'. When sanitization changes the identifier — or when the result would not be a usable directory name ("", ".", "..", a trailing dot, a reserved device name, ".git", longer than keyMax) — a stable hash of the original identifier is appended, so identifiers that sanitize to the same text keep distinct keys. Identifiers that survive sanitization unchanged keep their deterministic key, with no hash.
func Root ¶
Root resolves the workspace root for a project: the configured path when set (absolute, or relative to the project root), the default <project>/.devsys/workspaces otherwise. A root that exists as a file is a precondition failure; a root that does not exist yet is fine (it is created with the first workspace).
func Validate ¶
Validate enforces the workspace path invariant of 方案 §4.8: an execution workspace must sit inside the configured root. Both paths are made absolute and cleaned, and symlinks on the deepest existing ancestor of each are resolved, so neither a ".." segment nor a symlink can point outside the root. The root itself is not a workspace.
Types ¶
type EnsureOptions ¶
type EnsureOptions struct {
ProjectRoot string
// Root is the configured workspace root; empty uses the default below
// the project's .devsys/.
Root string
Identifier string
// Branch overrides the derived branch; empty uses devsys/<key>.
Branch string
Hooks map[string]Hook
}
EnsureOptions describes the workspace a work item needs.
type Hook ¶
type Hook struct {
Command string `json:"command,omitempty"`
TimeoutSeconds int `json:"timeout_seconds,omitempty"`
}
Hook is one lifecycle hook as the workflow policy declares it.
type HookError ¶
type HookError struct {
Name string
ExitCode int
TimedOut bool
Canceled bool
Timeout time.Duration
Output string
}
HookError is a hook that failed, timed out or was canceled. Callers decide the consequence: after_create and before_run are fatal, after_run and before_remove are recorded only (方案 §4.8).
type HookReport ¶
type HookReport struct {
Name string `json:"name"`
Ran bool `json:"ran"`
Command string `json:"command,omitempty"`
ExitCode int `json:"exit_code,omitempty"`
TimedOut bool `json:"timed_out,omitempty"`
Canceled bool `json:"canceled,omitempty"`
DurationMS int64 `json:"duration_ms,omitempty"`
Output string `json:"output,omitempty"`
}
HookReport is what one hook execution produced; Ran is false when the policy declared no command for the hook.
func RunHook ¶
func RunHook(ctx context.Context, opts RunHookOptions) (HookReport, error)
RunHook executes one lifecycle hook in dir. The command is a shell script: "sh -c" on POSIX hosts, "cmd /c" on Windows (the policy file is platform-specific by nature — 方案 §4.8 declares hooks as shell scripts).
Execution goes through the harness shell adapter (M6.1), so a hook gets the same line streaming, timeout and process-tree termination an attempt does.
type Listing ¶
type Listing struct {
Key string `json:"key"`
Path string `json:"path"`
Branch string `json:"branch,omitempty"`
Registered bool `json:"registered"`
}
Listing is one workspace found below the root (List).
type RemoveOptions ¶
type RemoveOptions struct {
ProjectRoot string
Root string
// Path names the workspace directly; Identifier derives it from the key.
Path string
Identifier string
Force bool
Hooks map[string]Hook
}
RemoveOptions describes the workspace to take down.
type RemoveReport ¶
type RemoveReport struct {
Path string `json:"path"`
Removed bool `json:"removed"`
Notice string `json:"notice,omitempty"`
Hook *HookReport `json:"hook,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
RemoveReport says what removal did.
func Remove ¶
func Remove(ctx context.Context, opts RemoveOptions) (RemoveReport, error)
Remove takes a workspace down: the before_remove hook first (its failure is recorded, never fatal — 方案 §4.8), then the worktree registration (git projects) and the directory. A workspace that is already gone is a no-op, not an error. The branch is kept: it may hold the run's commits, and removing it is a deliberate act, not a cleanup side effect.
type RunHookOptions ¶
RunHookOptions is one hook execution.
type Workspace ¶
type Workspace struct {
Key string `json:"key"`
Path string `json:"path"`
Branch string `json:"branch"`
Created bool `json:"created"`
Hook *HookReport `json:"hook,omitempty"`
}
Workspace is one execution workspace, addressed by a sanitized key and reused across runs while it exists (方案 §4.8). Branch is set for git worktrees and empty for directory workspaces.
func Ensure ¶
func Ensure(ctx context.Context, opts EnsureOptions) (Workspace, error)
Ensure creates the workspace for an identifier or reuses the existing one.
Git projects get a worktree of the repository on the expected branch; a workspace that already exists is reused as it stands and after_create is not run again. Non-git projects get a plain directory under the workspace root, with no branch. A newly created workspace whose after_create hook fails is taken back down so a failed bootstrap leaves no half-made workspace behind.