refactor(agent): extract reusable core into importable agentd package#6431
Merged
Conversation
Move the agent's core (lifecycle, modes, handlers) out of package main into a new importable library, agent/pkg/agentd, so the agent can run in-process inside another Go program. The agent/ binary stays a thin package main CLI that imports the new package. The Agent type lived in package main, which Go cannot import. ShellHub Desktop (Wails v3) needs to embed the agent in-process to register the host as a device instead of shelling out to a subprocess. Changes that make embedding work, not just compile: - Version/Platform via Config: a library can't read another package's main globals, so the core no longer references AgentVersion or AgentPlatform. Added Config.Version and Config.Platform; the CLI injects them from its ldflags globals. The build is unchanged: the ldflags target is still main.AgentVersion. Self-update stays in the CLI loop, so an embedded agent never self-updates. - Injectable SFTP command: the host SFTP server starts by re-executing /proc/self/exe, which is the host program when embedded, not the agent. Added Config.SFTPServerCommand, threaded through host.NewSessioner; when nil (the CLI path) behavior is unchanged. - os.Exit confinement: all os.Exit calls remain in package main, so embedding the library cannot abort the host process. Public API (NewAgentWithConfig, Initialize, Listen, Close, Config, Mode, HostMode, ConnectorMode, GetInfo, LoadConfigFromEnv) is preserved; no in-repo consumer imported the old package. Fixes: #6430
|
Claude finished @gustavosbarreto's task in 16m 31s —— View job Code Review CompleteReviewed 7 files across code quality, security, testing, Go patterns, and architecture — no issues found. The code looks good as-is. The refactoring is mechanically clean: all To request another review round, comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Extracts the agent's core (lifecycle, modes, handlers) out of
package maininto a new importable library,agent/pkg/agentd, so the agent can run in-process inside another Go program. Theagent/binary stays a thinpackage mainCLI that imports the new package.Why
The Agent type and its lifecycle lived in
package main, which Go cannot import. ShellHub Desktop (Wails v3) needs to embed the agent in-process to register the host as a device, rather than shelling out to a subprocess.Closes #6430
Changes
agent.go,modes.go,handlers.go, and their test out ofpackage main(viagit mv, history preserved) and re-declared them aspackage agentd. Public API is unchanged:NewAgentWithConfig,Initialize,Listen,Close,Config,Mode,HostMode,ConnectorMode,GetInfo,LoadConfigFromEnv.mainglobals, so the core no longer referencesAgentVersion/AgentPlatform. AddedConfig.VersionandConfig.Platform; the CLI injects them from its ldflags globals. The build invocation is unchanged — the ldflags target is stillmain.AgentVersion. Self-update stays entirely in the CLI loop, so an embedded agent never self-updates./proc/self/exe sftp— which breaks when embedded, since that path is the host program, not the agent. AddedConfig.SFTPServerCommand func() *exec.Cmd, threaded throughhost.NewSessioner; when nil (the CLI path) behavior is identical to before. Embedders point it at a binary that runs the SFTP server.os.Exitcalls remain inpackage main(main.go,sftp.go); the library has none, so embedding it cannot abort the host process.Testing
A consumer embedding this package must replicate the agent module's
replace github.com/gliderlabs/ssh => github.com/shellhub-io/sshdirective in its own go.mod — Go ignores replace directives from non-main modules. This is documented in the agentd package comment.Verify the CLI path is unchanged: build with
-ldflags "-X main.AgentVersion=<v>"and confirmagent --versionreports it and a host-mode device still serves SSH and SFTP. Both the native anddockerbuild tags compile;go vetand the agentd tests pass.