Extract session subpackage with pure guest-session helpers

Moves the stateless parts of the guest-session subsystem into
internal/daemon/session:

- consts (BackendSSH, attach/transport kinds, StateRoot, LogTailLineDefault)
- StateSnapshot plus ParseState / InspectStateFromDir / ApplyStateSnapshot / StateChanged
- 10 on-guest path helpers (StateDir, StdoutLogPath, StdinPipePath, …)
- 3 bash script generators (Script, InspectScript, SignalScript)
- small utilities (ShellQuote, ExitCode, CloneStringMap, TailFileContent,
  ProcessAlive + syscallKill test seam, FormatStepError)
- launch helpers (DefaultName, DefaultCWD, FailLaunch,
  NormalizeRequiredCommands, CWDPreflightScript, CommandPreflightScript,
  AttachInputCommand, AttachTailCommand, EnvLines)

Callers inside the daemon package import the new package under the
alias "sess" to avoid colliding with the local `session model.GuestSession`
variables threaded through the orchestrator code. guest_sessions.go
shrinks from 616 → 156 LOC; session_stream.go, session_attach.go,
session_lifecycle.go, workspace.go, and guest_sessions_test.go rewire to
the exported names.

The orchestrator methods (StartGuestSession, BeginGuestSessionAttach,
SendToGuestSession, GuestSessionLogs, refresh/inspect, sessionRegistry,
guestSessionController) stay on *Daemon. Full Manager-style extraction
would need prerequisite phases (operation protocol, workdisk helpers),
mirroring Phase 4a's trade-off.

All tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Thales Maciel 2026-04-15 16:33:12 -03:00
parent c13c8b11af
commit 37e02b1576
No known key found for this signature in database
GPG key ID: 33112E6833C34679
8 changed files with 612 additions and 566 deletions

View file

@ -10,6 +10,7 @@ import (
"strings"
"banger/internal/api"
sess "banger/internal/daemon/session"
"banger/internal/guest"
"banger/internal/model"
"banger/internal/system"
@ -30,7 +31,7 @@ func (d *Daemon) GuestSessionLogs(ctx context.Context, params api.GuestSessionLo
}
tailLines := params.TailLines
if tailLines <= 0 {
tailLines = guestSessionLogTailLine
tailLines = sess.LogTailLineDefault
}
path := session.StdoutLogPath
if streamName == "stderr" {
@ -76,9 +77,9 @@ func (d *Daemon) SendToGuestSession(ctx context.Context, params api.GuestSession
}
sendScript := fmt.Sprintf(
"set -euo pipefail\ncat %s >> %s\nrm -f %s\n",
guestShellQuote(tmpPath),
guestShellQuote(guestSessionStdinPipePath(session.ID)),
guestShellQuote(tmpPath),
sess.ShellQuote(tmpPath),
sess.ShellQuote(sess.StdinPipePath(session.ID)),
sess.ShellQuote(tmpPath),
)
var sendLog bytes.Buffer
if err := client.RunScript(ctx, sendScript, &sendLog); err != nil {
@ -99,9 +100,9 @@ func (d *Daemon) readGuestSessionLog(ctx context.Context, vm model.VMRecord, ses
path = session.StderrLogPath
}
var output bytes.Buffer
script := fmt.Sprintf("set -euo pipefail\nif [ -f %s ]; then tail -n %d %s; fi\n", guestShellQuote(path), tailLines, guestShellQuote(path))
script := fmt.Sprintf("set -euo pipefail\nif [ -f %s ]; then tail -n %d %s; fi\n", sess.ShellQuote(path), tailLines, sess.ShellQuote(path))
if err := client.RunScript(ctx, script, &output); err != nil {
return "", formatGuestSessionStepError("read guest session log", err, output.String())
return "", sess.FormatStepError("read guest session log", err, output.String())
}
return output.String(), nil
}
@ -114,6 +115,6 @@ func (d *Daemon) readGuestSessionLog(ctx context.Context, vm model.VMRecord, ses
return "", err
}
defer cleanup()
logPath := filepath.Join(workMount, guestSessionRelativeStateDir(session.ID), stream+".log")
return tailFileContent(logPath, tailLines)
logPath := filepath.Join(workMount, sess.RelativeStateDir(session.ID), stream+".log")
return sess.TailFileContent(logPath, tailLines)
}