Files
multica/server/pkg/db/generated/terminal_session.sql.go
Jiayuan Zhang 0529d28133 feat(terminal): Phase 4 — GC hook, audit log, issue runs entry (MUL-2295)
- terminal.Manager: OnSessionStart/OnSessionStop hooks fire around the
  manager's register/deregister so callers can safely mark/unmark
  external state (GC protection, audit rows) without racing Get/Done.
- daemon: wires the hooks to markActiveEnvRoot(filepath.Dir(workDir))
  so an idle terminal on a done/cancelled issue can't have its workdir
  reclaimed mid-session, and emits structured slog audit records
  (user_id/task_id/duration; no keystrokes — RFC §Auth).
- server: persists every PTY open/close to a new terminal_sessions
  table (migration 091) as the source behind the audit log and the
  new `type=terminal` rows in `multica issue runs`. New endpoint
  GET /api/issues/{id}/terminal-sessions.
- CLI: `multica issue runs` merges the agent task runs feed with the
  terminal sessions feed, sorted by started_at; terminal rows render
  with agent="terminal" and close_reason in the ERROR column. Old
  servers without the endpoint degrade silently.
- server/handler/terminal_ws.go: pass parsed Cols/Rows query values
  to sendOpenToDaemon so the first PTY frame matches the client's
  viewport; post-open resize is now just a defensive patch (addresses
  Emacs's Phase 3 non-blocking nit).

Co-authored-by: multica-agent <github@multica.ai>
2026-05-16 18:55:20 +08:00

130 lines
3.3 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: terminal_session.sql
package db
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const closeTerminalSession = `-- name: CloseTerminalSession :exec
UPDATE terminal_sessions
SET ended_at = $2,
exit_code = $3,
close_reason = $4
WHERE id = $1 AND ended_at IS NULL
`
type CloseTerminalSessionParams struct {
ID pgtype.UUID `json:"id"`
EndedAt pgtype.Timestamptz `json:"ended_at"`
ExitCode pgtype.Int4 `json:"exit_code"`
CloseReason string `json:"close_reason"`
}
// Idempotent: ended_at IS NULL guards against double-close (e.g. the
// daemon emitting terminal.exit and the user closing the tab racing).
// A second call after the first has stamped ended_at is a no-op.
func (q *Queries) CloseTerminalSession(ctx context.Context, arg CloseTerminalSessionParams) error {
_, err := q.db.Exec(ctx, closeTerminalSession,
arg.ID,
arg.EndedAt,
arg.ExitCode,
arg.CloseReason,
)
return err
}
const createTerminalSession = `-- name: CreateTerminalSession :one
INSERT INTO terminal_sessions (
id, workspace_id, issue_id, task_id, runtime_id, user_id, work_dir, shell, started_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
) RETURNING id, workspace_id, issue_id, task_id, runtime_id, user_id, work_dir, shell, started_at, ended_at, exit_code, close_reason
`
type CreateTerminalSessionParams struct {
ID pgtype.UUID `json:"id"`
WorkspaceID pgtype.UUID `json:"workspace_id"`
IssueID pgtype.UUID `json:"issue_id"`
TaskID pgtype.UUID `json:"task_id"`
RuntimeID pgtype.UUID `json:"runtime_id"`
UserID pgtype.UUID `json:"user_id"`
WorkDir string `json:"work_dir"`
Shell string `json:"shell"`
StartedAt pgtype.Timestamptz `json:"started_at"`
}
func (q *Queries) CreateTerminalSession(ctx context.Context, arg CreateTerminalSessionParams) (TerminalSession, error) {
row := q.db.QueryRow(ctx, createTerminalSession,
arg.ID,
arg.WorkspaceID,
arg.IssueID,
arg.TaskID,
arg.RuntimeID,
arg.UserID,
arg.WorkDir,
arg.Shell,
arg.StartedAt,
)
var i TerminalSession
err := row.Scan(
&i.ID,
&i.WorkspaceID,
&i.IssueID,
&i.TaskID,
&i.RuntimeID,
&i.UserID,
&i.WorkDir,
&i.Shell,
&i.StartedAt,
&i.EndedAt,
&i.ExitCode,
&i.CloseReason,
)
return i, err
}
const listTerminalSessionsByIssue = `-- name: ListTerminalSessionsByIssue :many
SELECT id, workspace_id, issue_id, task_id, runtime_id, user_id, work_dir, shell, started_at, ended_at, exit_code, close_reason FROM terminal_sessions
WHERE issue_id = $1
ORDER BY started_at DESC
`
func (q *Queries) ListTerminalSessionsByIssue(ctx context.Context, issueID pgtype.UUID) ([]TerminalSession, error) {
rows, err := q.db.Query(ctx, listTerminalSessionsByIssue, issueID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []TerminalSession{}
for rows.Next() {
var i TerminalSession
if err := rows.Scan(
&i.ID,
&i.WorkspaceID,
&i.IssueID,
&i.TaskID,
&i.RuntimeID,
&i.UserID,
&i.WorkDir,
&i.Shell,
&i.StartedAt,
&i.EndedAt,
&i.ExitCode,
&i.CloseReason,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}