-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon_process_watchers.go
More file actions
226 lines (213 loc) · 5.4 KB
/
Copy pathdaemon_process_watchers.go
File metadata and controls
226 lines (213 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"bytes"
"context"
"crypto/sha1"
"fmt"
"os/exec"
"os/user"
"runtime"
"strconv"
"strings"
"time"
)
const defaultProcessLimit = 4
type ProcessCaptureOptions struct {
Limit int
Now time.Time
}
type ProcessSnapshot struct {
CapturedAt time.Time
Processes []ProcessInfo
}
type ProcessInfo struct {
PID int
Command string
CommandLine string
User string
TTY string
Elapsed time.Duration
}
var processSnapshotCollector = captureProcessSnapshotPlatform
// CaptureProcessSnapshot returns a snapshot of the most recent terminal-owned processes on this machine.
func CaptureProcessSnapshot(ctx context.Context, options ProcessCaptureOptions) (ProcessSnapshot, error) {
if options.Limit <= 0 {
options.Limit = defaultProcessLimit
}
if options.Now.IsZero() {
options.Now = time.Now().UTC()
}
return processSnapshotCollector(ctx, options)
}
func captureProcessSnapshotPlatform(ctx context.Context, options ProcessCaptureOptions) (ProcessSnapshot, error) {
procSnapshot := ProcessSnapshot{CapturedAt: options.Now}
switch runtime.GOOS {
case "windows":
return procSnapshot, nil
default:
processes, err := captureUnixProcesses(ctx, options.Limit)
if err != nil {
return ProcessSnapshot{}, err
}
if len(processes) > options.Limit {
processes = processes[:options.Limit]
}
procSnapshot.Processes = processes
return procSnapshot, nil
}
}
func captureUnixProcesses(ctx context.Context, limit int) ([]ProcessInfo, error) {
cmd := exec.CommandContext(ctx, "ps", "-eo", "pid=,uid=,etimes=,tty=,comm=,args=", "-ww", "--sort=-etimes")
output, err := cmd.Output()
if err != nil {
return nil, err
}
var result []ProcessInfo
lines := bytes.Split(output, []byte{'\n'})
for _, rawLine := range lines {
if len(result) >= limit {
break
}
line := strings.TrimSpace(string(rawLine))
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) < 5 {
continue
}
tty := parts[3]
if tty == "" || tty == "?" {
continue
}
pid, err := strconv.Atoi(parts[0])
if err != nil {
continue
}
elapsedSeconds, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
continue
}
username := parts[1]
if u, err := user.LookupId(parts[1]); err == nil && u.Username != "" {
username = u.Username
}
command := parts[4]
args := ""
if len(parts) > 5 {
args = strings.Join(parts[5:], " ")
}
commandLine := command
if args != "" {
commandLine = command + " " + args
}
result = append(result, ProcessInfo{
PID: pid,
Command: command,
CommandLine: strings.TrimSpace(commandLine),
User: username,
TTY: tty,
Elapsed: time.Duration(elapsedSeconds) * time.Second,
})
}
return result, nil
}
func daemonWatchTerminalProcesses(ctx context.Context, snapshot daemonLoopSnapshot) ([]AssistantFeedItem, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
procSnapshot, err := CaptureProcessSnapshot(ctx, ProcessCaptureOptions{Limit: 4, Now: snapshot.Now})
if err != nil || len(procSnapshot.Processes) == 0 {
return nil, nil
}
lines := make([]string, 0, len(procSnapshot.Processes))
refs := make([]string, 0, len(procSnapshot.Processes))
for _, proc := range procSnapshot.Processes {
lines = append(lines, formatProcessLine(proc))
refs = append(refs, fmt.Sprintf("pid:%d", proc.PID))
}
summary := strings.Join(lines, " | ")
body := strings.Join(lines, "\n")
if summary == "" {
return nil, nil
}
hash := sha1.Sum([]byte(summary))
id := fmt.Sprintf("local:process-context:%x", hash[:6])
title := "Terminal activity snapshot"
if len(procSnapshot.Processes) > 0 && procSnapshot.Processes[0].Command != "" {
title = fmt.Sprintf("Terminal activity: %s", procSnapshot.Processes[0].Command)
}
item := AssistantFeedItem{
ID: id,
Key: id,
Kind: AssistantFeedKindResearchBrief,
Status: AssistantFeedStatusNew,
Title: title,
Summary: summary,
Body: body,
Reason: "Snapshot of interactive terminal processes",
SourceType: "process",
SourceID: id,
SourceRefs: refs,
CreatedAt: snapshot.Now,
UpdatedAt: snapshot.Now,
Importance: 35,
}
return []AssistantFeedItem{item}, nil
}
func formatProcessLine(proc ProcessInfo) string {
cmd := proc.CommandLine
if cmd == "" {
cmd = proc.Command
}
if cmd == "" {
cmd = fmt.Sprintf("pid %d", proc.PID)
}
cmd = truncateString(cmd, 140)
parts := make([]string, 0, 3)
if proc.User != "" {
parts = append(parts, proc.User)
}
if proc.TTY != "" {
parts = append(parts, fmt.Sprintf("tty=%s", proc.TTY))
}
if proc.Elapsed > 0 {
parts = append(parts, fmt.Sprintf("up %s", humanDuration(proc.Elapsed)))
}
if len(parts) == 0 {
return cmd
}
return fmt.Sprintf("%s (%s)", cmd, strings.Join(parts, " | "))
}
func truncateString(value string, limit int) string {
if limit <= 0 || len(value) <= limit {
return value
}
if limit <= 3 {
return value[:limit]
}
return value[:limit-3] + "..."
}
func humanDuration(d time.Duration) string {
if d <= time.Second {
return "1s"
}
mins := int(d.Minutes())
if mins >= 60 {
hours := mins / 60
mins = mins % 60
if mins > 0 {
return fmt.Sprintf("%dh%dm", hours, mins)
}
return fmt.Sprintf("%dh", hours)
}
if mins > 0 {
secs := int(d.Seconds()) % 60
if secs > 0 {
return fmt.Sprintf("%dm%ds", mins, secs)
}
return fmt.Sprintf("%dm", mins)
}
secs := int(d.Seconds())
return fmt.Sprintf("%ds", secs)
}