core

package
v0.153.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 36 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewJobAdopter

func NewJobAdopter(hub *wshub.Hub, jobs *job.Service) wshub.Adopter

NewJobAdopter builds the adoption seam that lets a hub starting after a serve restart adopt the `recovering` jobs a PREVIOUS serve process left in the store. It is exported so a harness standing up the same hub+job pair wires exactly what the server wires.

Types

type BuildOption

type BuildOption func(*buildOptions)

BuildOption customises Build.

func WithAgentDetector

func WithAgentDetector(d agent.Detector) BuildOption

WithAgentDetector injects the agent.Detector that gates materialization of the built-in agent templates (agent.Resolve).

Production callers omit it and get agent.DefaultDetector(). TESTS must inject agent.NoopDetector{} (or a fake): with the real probe, the resolved agent set — and therefore cfg.Agents, the caps report and any agent-name assertion — would depend on which CLIs happen to be installed on the machine running `go test`.

The detector is remembered on the Core and reused by ReloadWith.

func WithBuiltinDefaultProject

func WithBuiltinDefaultProject() BuildOption

WithBuiltinDefaultProject injects the built-in `default` project (the host's default workspace) when the config declares none. Only the processes that run jobs for the server's own projects opt in (serve, the MCP local backend): a worker builds its Core from worker.yaml and must not advertise a project it was never given. ReloadWith keeps the same choice.

func WithConfigPath

func WithConfigPath(path string) BuildOption

WithConfigPath tells the Core which file its write transaction (Core.Update) persists to. serve passes the discovered config path; when empty the save path is resolved lazily to the user-level config (config.UserConfigPath), matching the old registry write-back behaviour.

type ConfigSnapshot

type ConfigSnapshot struct {
	Cfg *config.Config
	Rev int64
}

ConfigSnapshot is one atomic (config, revision) generation. Rev is the config generation counter — Build seeds it at 1 and every write transaction (reloadLocked) bumps it by 1, so a worker/consumer can drop any Policy whose Rev is not strictly newer than the one it already applied. A given Rev value therefore identifies exactly ONE config content (B2 verification 5).

type Core

type Core struct {
	Projects *project.Registry
	Agents   *agent.Registry
	Runners  map[string]runner.Runner
	Store    *jobstore.Store
	Jobs     *job.Service
	// Presence is the E36 driver-agent identity/mailbox service over the same
	// metadata Store. serve injects it into httpapi (SetPresence) + drives its
	// prune sweeper; the mcp standalone path passes it to the local backend.
	Presence *presence.Service

	// Hub is the ws-worker hub singleton (serve mounts it on /v1/workers/connect;
	// every type=worker runner references this one instance). Always non-nil.
	Hub *wshub.Hub

	// RelayNonces and PtyRelays are live-only serve-side PTY relay state shared by
	// worker dispatch, worker pty-connect (T5) and browser attach (T7).
	RelayNonces *ptyrelay.NonceStore
	PtyRelays   *ptyrelay.Registry
	// contains filtered or unexported fields
}

Core bundles the runtime objects assembled from a loaded config: the project/agent registries, the runner set, the SQLite metadata store and the job service. Both `serve` (HTTP control plane) and `mcp` (stdio MCP server) wire the same Core so the MCP tools reuse the identical job.Service (plan §P8: "MCP 内部复用 job.Service", 不复制执行逻辑).

func Build

func Build(cfg *config.Config, opts ...BuildOption) (*Core, error)

Build assembles the registries, runner set, metadata store and job service from cfg. It is the single wiring point shared by serve and mcp; peer-http runners declared in config are registered too (plan §11.1, P7). It opens the SQLite metadata db (design §11 ResolveDBPath) and returns an error if that fails, since the job service cannot operate without it.

func (*Core) Close

func (c *Core) Close() error

Close releases the Core's owned resources — currently the SQLite metadata store. Callers (serve/mcp) defer it for graceful shutdown so WAL is checkpointed and the db handle closed cleanly (design §14).

Worker connections close first (wshub.Hub.Shutdown waits until each dropped connection has put its jobs in `recovering` and persisted that). The job service shuts down next (job.Service.Shutdown: admission closed, the jobs this process runs cancelled and waited for, then their background work), so nothing still holds a connection when the store closes. The work / steward loops are not the Core's: they belong to the HTTP server, and serve stops them before this runs.

func (*Core) Config

func (c *Core) Config() *config.Config

Config returns the current config pointer (= Snapshot().Cfg). It is the read-only accessor serve/etc use in place of the old bare Cfg field.

func (*Core) Reload

func (c *Core) Reload(path string) error

Reload re-loads the config from path and atomically swaps it into every component that holds a config snapshot — the project/agent registries and the job service (C3 SIGHUP hot-reload). It does NOT restart the process, touch in-flight jobs or reopen the jobstore.

🔴 It holds updateMu from the entry and reads the file INSIDE the lock (D-HIGH-4): a SIGHUP that read the file off-lock could load a stale file A, race a web Update that publishes B, then re-publish A as a newer Rev — disk B, memory A, the web edit lost. Locking the whole stat→load→overlay→reload closes that window.

Fail-safe: if the new config fails to load/validate, the OLD generation is kept (nothing is swapped) and the error is returned so the caller can log and keep serving.

LIMITATION: the runner instances in Core.Runners are built once at assemble time and are NOT rebuilt on reload. Swapping the config makes the peer-runner classification and every allowlist/validation observe the new config, but adding a brand-new runner TYPE (a new peer-http entry) still needs a restart to instantiate its runner. Reload covers adding/removing projects and agents and any config-derived validation.

func (*Core) ReloadConfig

func (c *Core) ReloadConfig() error

ReloadConfig re-reads the config file this Core OWNS — the same path its write transaction saves to (WithConfigPath, or the lazily resolved user-level config when none was configured) — and hot-swaps it in. It is the manual reload behind POST /v1/config/reload (WEB-04③ V1.1): Windows has no SIGHUP, so an operator who edited the file by hand has no other way to apply it short of a restart.

The path resolution is deliberately the SAME code path as saveConfig, so "the file the console writes" and "the file a reload re-reads" can never be two different files (serve passes -c; a mismatch would make an edit vanish on the next reload).

func (*Core) ReloadConfigReport

func (c *Core) ReloadConfigReport() (config.ReloadResult, error)

ReloadConfigReport reloads the startup-resolved config path and returns the same structured result as ReloadDetailed.

func (*Core) ReloadDetailed

func (c *Core) ReloadDetailed(path string) (config.ReloadResult, error)

ReloadDetailed is the reporting form of Reload. It keeps the legacy Reload error-only method available to embedders while exposing the generation and restart-only keys needed by HTTP and CLI callers.

func (*Core) ReloadWith

func (c *Core) ReloadWith(cfg *config.Config) error

ReloadWith swaps an ALREADY-BUILT config into every component that holds a config snapshot. It is the apply half of Reload, split out so a caller that obtains its config from somewhere other than a server config file (a worker builds one from worker.yaml) reuses the exact same apply path instead of re-implementing it.

Error semantics: the component Reloads are plain atomic.Store calls and CANNOT fail. The only error ReloadWith can return is the nil-config precondition. "A bad config keeps the old one" is achieved by BUILDING the new config completely (load/parse/validate) BEFORE calling this, so a bad config never reaches the apply stage.

The apply is safe against concurrent Submit: every consumer that needs more than one config-derived fact takes ONE snapshot and uses it throughout (job.Service.Submit resolves policy AND the agent argv from that one snapshot), so a submit racing this swap sees either the old config or the new one, never a mix. See Reload for the runner-instance limitation.

func (*Core) ReloadWithReport

func (c *Core) ReloadWithReport(cfg *config.Config, path string) (config.ReloadResult, error)

ReloadWithReport is the worker-side apply form of ReloadDetailed. path is the worker.yaml path used for the receipt; the config has already been parsed and validated by the caller before entering this method.

func (*Core) Rules

func (c *Core) Rules() *rule.Store

Rules returns the JOB-06① rule library store (nil when the library was never built). It is the SAME store the job seam resolves bindings against and the one serve injects into httpapi (SetRules), so the CLI/HTTP surface and the injection path can never disagree about what a rule says.

func (*Core) SetReloadHook

func (c *Core) SetReloadHook(hook func(*config.Config))

SetReloadHook installs a process-local observer for successful reloads. The callback must not perform network I/O; it runs in the serialized config transaction after the new snapshot is published.

func (*Core) Skills

func (c *Core) Skills() *skill.Store

Skills returns the JOB-10 skill library store (nil when the library was never built). It is the SAME store the job seam above resolves bindings against and the one serve injects into httpapi (SetSkills), so the CLI/HTTP surface and the dispatch path can never disagree about what a skill contains.

Nil is a "not wired" answer, not a panic: an HTTP surface without it answers 503 (the same degradation /v1/xfer uses), which is what a caller built before Build wired the library must see.

func (*Core) Snapshot

func (c *Core) Snapshot() ConfigSnapshot

Snapshot returns the current (cfg, rev) generation in a SINGLE atomic load, so the caller never observes a cfg from one generation and a rev from another.

func (*Core) Update

func (c *Core) Update(mut func(*config.Config) error) error

Update is THE single serial config write transaction (B2). It clones the current snapshot, applies mut to the clone (project whole-value add/remove ONLY, per the project applier contract), saves the副本 to disk, then swaps the new generation into every component and marks it for broadcast. The whole clone→mut→save→reload runs under updateMu, so two concurrent writers can never clone the same snapshot / lose an update / mint two configs at one Rev.

Fail-safe: if mut or the save fails the OLD generation is untouched — nothing is swapped, disk is unchanged (save writes a副本, never the live config) and no broadcast happens. The policy broadcast is done by flushPush AFTER updateMu is released (never a network write under the lock, D-HIGH-5).

func (*Core) Workflow

func (c *Core) Workflow() *workflow.Engine

Workflow returns the Core's workflow engine (the handle serve/httpapi consume to drive job-chain workflows). Always non-nil after Build.

func (*Core) Xfer

func (c *Core) Xfer() *xfer.Manager

Xfer returns the XFER-01 transfer manager. It is always non-nil after Build: the transfer surface (HTTP routes, the CLI) is mounted unconditionally, and a transfer that cannot reach a worker fails with a reason instead of the feature being absent.

Jump to

Keyboard shortcuts

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