跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

stream.go

170 行 · 4.0 KB历史下载
1package diagnostics
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8 "regexp"
9 "strings"
10 "sync"
11 "time"
12)
13
14// Stream frames arbitrary stdout/stderr writes before redacting. It never
15// writes a partial overlong line, so split credentials cannot leak at a bound.
16type Stream struct {
17 mu sync.Mutex
18 store *Store
19 source, level string
20 sink func(string)
21 line []byte
22 dropping, privateKey bool
23 last string
24 lastLevel string
25 repeated uint64
26 reported time.Time
27 batch []Event
28 batchBytes int
29}
30
31func (s *Store) Stream(source, level string) *Stream {
32 return &Stream{store: s, source: source, level: level}
33}
34func NewStream(store *Store, source, level string, sink func(string)) *Stream {
35 return &Stream{store: store, source: source, level: level, sink: sink}
36}
37
38// flush writes at most 64 KiB of redacted messages; no background queue can
39// grow while a slow disk is being drained.
40func (s *Stream) flush() {
41 if s.store != nil {
42 _ = s.store.appendEvents(s.batch)
43 }
44 s.batch = nil
45 s.batchBytes = 0
46}
47func (s *Stream) record(level, message string) {
48 message = Redact(message)
49 if s.batchBytes+len(message) > 64<<10 {
50 s.flush()
51 }
52 if s.sink != nil {
53 s.sink(message + "\n")
54 }
55 s.batch = append(s.batch, Event{Level: level, Source: Redact(s.source), Message: message})
56 s.batchBytes += len(message)
57 if s.batchBytes >= 64<<10 || len(s.batch) >= 128 {
58 s.flush()
59 }
60}
61func (s *Stream) summary() {
62 if s.repeated > 0 {
63 s.record(s.lastLevel, fmt.Sprintf("[previous output repeated %d additional times]", s.repeated))
64 s.repeated = 0
65 }
66}
67func (s *Stream) output(level, line string) {
68 // Compare complete raw lines, so distinct credentials never become identical
69 // just because redaction removes their values. Never coalesce progress records.
70 if line == s.last && !strings.HasPrefix(line, "CAGE_") {
71 s.repeated++
72 if time.Since(s.reported) >= time.Second {
73 s.summary()
74 s.reported = time.Now()
75 }
76 return
77 }
78 s.summary()
79 s.last = line
80 s.lastLevel = level
81 s.reported = time.Now()
82 s.record(level, line)
83}
84func (s *Stream) emit() {
85 line := string(s.line)
86 if bytes.Contains(s.line, []byte("-----BEGIN ")) {
87 s.privateKey = true
88 }
89 if s.privateKey {
90 if bytes.Contains(s.line, []byte("-----END ")) {
91 s.privateKey = false
92 }
93 } else if !s.dropping && len(s.line) > 0 {
94 s.output(s.outputLevel(line), line)
95 }
96 if s.dropping {
97 s.output("warn", "[oversized output line omitted]")
98 }
99 s.line = nil
100 s.dropping = false
101}
102
103var logfmtLevel = regexp.MustCompile(`^time=(?:"[^"]*"|\S+)\s+level=(\w+)(?:\s|$)`)
104
105// These managed producers provide their own severity; stderr is not an error
106// classification. Other streams keep the severity chosen by their caller.
107func (s *Stream) outputLevel(line string) string {
108 switch s.source {
109 case "vm-hostagent", "proxy", "proxy-validation":
110 default:
111 return s.level
112 }
113 var entry struct {
114 Level string `json:"level"`
115 }
116 _ = json.Unmarshal([]byte(line), &entry)
117 if entry.Level == "" {
118 if match := logfmtLevel.FindStringSubmatch(line); len(match) == 2 {
119 entry.Level = match[1]
120 }
121 }
122 switch entry.Level {
123 case "trace", "debug":
124 return "debug"
125 case "info":
126 return "info"
127 case "warn", "warning":
128 return "warn"
129 case "error", "fatal", "panic":
130 return "error"
131 default:
132 return s.level
133 }
134}
135func (s *Stream) Write(p []byte) (int, error) {
136 s.mu.Lock()
137 defer s.mu.Unlock()
138 for _, b := range p {
139 if b == '\n' || b == '\r' {
140 s.emit()
141 continue
142 }
143 if s.dropping {
144 continue
145 }
146 if len(s.line) >= MaxMessage {
147 s.line = nil
148 s.dropping = true
149 continue
150 }
151 s.line = append(s.line, b)
152 }
153 s.flush()
154 return len(p), nil
155}
156func (s *Stream) Close() error {
157 s.mu.Lock()
158 defer s.mu.Unlock()
159 s.emit()
160 s.summary()
161 s.flush()
162 s.last = ""
163 return nil
164}
165func (s *Store) Capture(source, level string, r io.Reader) error {
166 w := s.Stream(source, level)
167 _, err := io.Copy(w, r)
168 _ = w.Close()
169 return err
170}