-
Notifications
You must be signed in to change notification settings - Fork 146
feat(cli): add lk agent session for headless text-mode agent runs
#857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
toubatbrian
wants to merge
5
commits into
main
Choose a base branch
from
feat/agent-session-daemon
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+839
−7
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
25c7f4f
feat(cli): add `lk agent session` for headless text-mode agent runs
toubatbrian 4fc871a
fix(cli): skip empty function-tool output line in session renderer
toubatbrian 5f0ca88
refactor(cli): dispatch session daemon via hidden subcommand entrypoint
toubatbrian 0a32ba0
fix(cli): reject direct invocation of hidden session daemon entrypoint
toubatbrian 7514534
Update session.go
toubatbrian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| // Copyright 2025 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "encoding/binary" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| // Single-session model: the fixed loopback port is the singleton registry. | ||
| // The daemon binds it; whoever wins the bind() is "the session". start, say, | ||
| // and end all rendezvous on this one port. No session id, manifest, or dir. | ||
| const ( | ||
| sessionMagic = "LKCP" // 4-byte preamble that marks a control connection | ||
| sessionHost = "127.0.0.1" | ||
| defaultSessionPort = 8775 | ||
|
|
||
| envSessionDaemon = "LK_SESSION_DAEMON" // "1" in the re-exec'd daemon child | ||
| envSessionPort = "LK_SESSION_PORT" // fixed port | ||
| envSessionDir = "LK_SESSION_DIR" // resolved project dir | ||
| envSessionEntry = "LK_SESSION_ENTRY" // resolved entrypoint (project-relative) | ||
| envSessionPType = "LK_SESSION_PTYPE" // agentfs.ProjectType string | ||
| envSessionReadyFD = "LK_SESSION_READY_FD" | ||
| ) | ||
|
|
||
| var sessionPortFlag = &cli.IntFlag{ | ||
| Name: "port", | ||
| Sources: cli.EnvVars(envSessionPort), | ||
| Value: defaultSessionPort, | ||
| Usage: "Fixed loopback port shared by the agent and control connections", | ||
| } | ||
|
|
||
| func init() { | ||
| // Register under the `agent` group as `lk agent session`, mirroring how | ||
| // `lk agent console` attaches itself. Unlike console, this command is not | ||
| // gated behind the `console` build tag: it is CGO-free and ships in the | ||
| // default binary. | ||
| AgentCommands[0].Commands = append(AgentCommands[0].Commands, agentSessionCommand) | ||
| } | ||
|
|
||
| var agentSessionCommand = &cli.Command{ | ||
| Name: "session", | ||
| Usage: "Drive a single local agent session in text mode (start/say/end)", | ||
| Category: "Core", | ||
| Commands: []*cli.Command{ | ||
| { | ||
| Name: "start", | ||
| Usage: "Start a detached agent session daemon", | ||
| ArgsUsage: "[entrypoint]", | ||
| Flags: []cli.Flag{sessionPortFlag}, | ||
| Action: runSessionStart, | ||
| }, | ||
| { | ||
| Name: "say", | ||
| Usage: "Send a text turn to the running session and print the reply", | ||
| ArgsUsage: "<text>", | ||
| Flags: []cli.Flag{sessionPortFlag}, | ||
| Action: runSessionSay, | ||
| }, | ||
| { | ||
| Name: "end", | ||
| Usage: "Stop the running session and its agent", | ||
| Flags: []cli.Flag{sessionPortFlag}, | ||
| Action: runSessionEnd, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| func sessionAddr(port int) string { | ||
| return fmt.Sprintf("%s:%d", sessionHost, port) | ||
| } | ||
|
|
||
| func runSessionStart(ctx context.Context, cmd *cli.Command) error { | ||
| projectDir, projectType, entrypoint, err := detectProject(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| port := int(cmd.Int("port")) | ||
|
|
||
| exe, err := os.Executable() | ||
| if err != nil { | ||
| return fmt.Errorf("could not resolve own binary: %w", err) | ||
| } | ||
|
|
||
| // Pipe the daemon uses to report readiness (or a startup error) before we | ||
| // return. This avoids racing a TCP probe against the agent's own connect. | ||
| readyR, readyW, err := os.Pipe() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer readyR.Close() | ||
|
|
||
| // The daemon is detached, so its own stdout/stderr (panics etc.) go to a | ||
| // temp log rather than the user's terminal. | ||
| logFile, err := os.CreateTemp("", "lk-session-daemon-*.log") | ||
| if err != nil { | ||
| readyW.Close() | ||
| return err | ||
| } | ||
|
|
||
| daemon := exec.Command(exe) | ||
| daemon.Env = append(os.Environ(), | ||
| envSessionDaemon+"=1", | ||
| envSessionPort+"="+strconv.Itoa(port), | ||
| envSessionDir+"="+projectDir, | ||
| envSessionEntry+"="+entrypoint, | ||
| envSessionPType+"="+string(projectType), | ||
| envSessionReadyFD+"=3", // ExtraFiles[0] is fd 3 in the child | ||
| ) | ||
| daemon.ExtraFiles = []*os.File{readyW} | ||
| daemon.Stdout = logFile | ||
| daemon.Stderr = logFile | ||
| setDetachedProcAttr(daemon) | ||
|
|
||
| if err := daemon.Start(); err != nil { | ||
| readyW.Close() | ||
| logFile.Close() | ||
| return fmt.Errorf("failed to start session daemon: %w", err) | ||
| } | ||
| // Close our copy of the write end so the read below sees EOF if the daemon dies. | ||
| readyW.Close() | ||
| logFile.Close() | ||
|
|
||
| status, _ := bufio.NewReader(readyR).ReadString('\n') | ||
| status = strings.TrimSpace(status) | ||
| switch { | ||
| case status == "ready": | ||
| fmt.Fprintf(os.Stderr, "Detected %s agent (%s in %s)\n", projectType.Lang(), entrypoint, projectDir) | ||
| fmt.Printf("Session started. Use `lk agent session say \"...\"` to talk, `lk agent session end` to stop.\n") | ||
| return nil | ||
| case strings.HasPrefix(status, "error:"): | ||
| return fmt.Errorf("%s", strings.TrimSpace(strings.TrimPrefix(status, "error:"))) | ||
| default: | ||
| return fmt.Errorf("session daemon exited before becoming ready (see %s)", logFile.Name()) | ||
| } | ||
| } | ||
|
|
||
| func runSessionSay(ctx context.Context, cmd *cli.Command) error { | ||
| text := strings.TrimSpace(strings.Join(cmd.Args().Slice(), " ")) | ||
| if text == "" { | ||
| return fmt.Errorf("usage: lk agent session say <text>") | ||
| } | ||
| conn, err := dialControl(int(cmd.Int("port"))) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| if err := writeControlFrame(conn, controlRequest{Cmd: "say", Text: text}); err != nil { | ||
| return err | ||
| } | ||
| return streamControlReplies(conn) | ||
| } | ||
|
|
||
| func runSessionEnd(ctx context.Context, cmd *cli.Command) error { | ||
| conn, err := dialControl(int(cmd.Int("port"))) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| if err := writeControlFrame(conn, controlRequest{Cmd: "end"}); err != nil { | ||
| return err | ||
| } | ||
| if err := streamControlReplies(conn); err != nil { | ||
| return err | ||
| } | ||
| fmt.Println("Session ended.") | ||
| return nil | ||
| } | ||
|
|
||
| // dialControl connects to the session daemon and sends the control preamble. | ||
| func dialControl(port int) (net.Conn, error) { | ||
| conn, err := net.Dial("tcp", sessionAddr(port)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("no session running on %s (run `lk agent session start` first)", sessionAddr(port)) | ||
| } | ||
| if _, err := conn.Write([]byte(sessionMagic)); err != nil { | ||
| conn.Close() | ||
| return nil, err | ||
| } | ||
| return conn, nil | ||
| } | ||
|
|
||
| func streamControlReplies(conn net.Conn) error { | ||
| for { | ||
| var reply controlReply | ||
| if err := readControlFrame(conn, &reply); err != nil { | ||
| if err == io.EOF { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
| if reply.Line != "" { | ||
| fmt.Println(reply.Line) | ||
| } | ||
| if reply.Done { | ||
| if reply.Error != "" { | ||
| return fmt.Errorf("%s", reply.Error) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Control protocol: a 4-byte big-endian length prefix + a JSON payload, mirroring | ||
| // pkg/ipc's framing but with JSON instead of protobuf (no new protobufs needed). | ||
| type controlRequest struct { | ||
| Cmd string `json:"cmd"` | ||
| Text string `json:"text,omitempty"` | ||
| } | ||
|
|
||
| type controlReply struct { | ||
| Line string `json:"line,omitempty"` | ||
| Done bool `json:"done,omitempty"` | ||
| Error string `json:"error,omitempty"` | ||
| } | ||
|
|
||
| func writeControlFrame(w io.Writer, v any) error { | ||
| data, err := json.Marshal(v) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| var hdr [4]byte | ||
| binary.BigEndian.PutUint32(hdr[:], uint32(len(data))) | ||
| if _, err := w.Write(hdr[:]); err != nil { | ||
| return err | ||
| } | ||
| _, err = w.Write(data) | ||
| return err | ||
| } | ||
|
|
||
| func readControlFrame(r io.Reader, v any) error { | ||
| var hdr [4]byte | ||
| if _, err := io.ReadFull(r, hdr[:]); err != nil { | ||
| return err | ||
| } | ||
| length := binary.BigEndian.Uint32(hdr[:]) | ||
| if length > 1<<20 { | ||
| return fmt.Errorf("control frame too large: %d bytes", length) | ||
| } | ||
| data := make([]byte, length) | ||
| if _, err := io.ReadFull(r, data); err != nil { | ||
| return err | ||
| } | ||
| return json.Unmarshal(data, v) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we create a separate entrypoint instead?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — moved it to a dedicated hidden subcommand entrypoint (
lk agent session daemon) instead of the env gate inmain().Done in 5f0ca88 (entrypoint) + 0a32ba0 (guard).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I mean is can't this be it's own binary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no then it's prob OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a hard reason we need two binaries? One works fine for us today. Re-exec'ing os.Executable() guarantees the daemon is the exact same version as the CLI (no skew), it reuses the console/ipc/detection code directly, and it's a hidden impl detail — nobody installs or runs it on its own. A second binary would also double our release/build matrix. Happy to split it out if there's a concrete need though.