Repository navigation
Expand file tree
/
Copy pathknowledge.go
More file actions
183 lines (175 loc) · 4.78 KB
/
Copy pathknowledge.go
File metadata and controls
183 lines (175 loc) · 4.78 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
package session
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/Agent-Field/codeaf/internal/config"
"github.com/Agent-Field/codeaf/internal/knowledge"
)
// Knowledge is the profile-wide vault the conversation's tools read.
// Capture is opt-in and only persisted, human-facing conversations write it.
func (a *Agent) Knowledge() *knowledge.Vault {
a.knowledgeMu.RLock()
defer a.knowledgeMu.RUnlock()
return a.knowledgeVault
}
// KnowledgeCaptureError reports a failed journal-to-vault copy. The durable
// journal remains the recovery source; a failed copy never advances its cursor.
func (a *Agent) KnowledgeCaptureError() string {
a.knowledgeMu.RLock()
j := a.knowledgeJournal
a.knowledgeMu.RUnlock()
if j == nil {
return ""
}
j.mu.Lock()
defer j.mu.Unlock()
return j.lastError
}
type knowledgeJournal struct {
vault *knowledge.Vault
project string
scope string
mu sync.Mutex
offset int64
skip int
lastError string
cancel context.CancelFunc
done chan struct{}
}
func (a *Agent) initKnowledge() {
if a.config.knowledgeReadVault != nil {
a.knowledgeVault = a.config.knowledgeReadVault
return
}
if strings.TrimSpace(a.config.transcriptPath()) == "" || a.config.InTask || a.config.taskID != 0 {
return
}
a.knowledgeVault = knowledge.Open(config.ProfilePath(a.config.ProfileDir, ""))
}
func (a *Agent) startKnowledge() {
vault := a.Knowledge()
if vault == nil || a.config.InTask || a.config.taskID != 0 || a.config.knowledgeReadVault != nil || a.file == nil {
return
}
path := a.file.journalName()
if path == "" {
return
}
ctx, cancel := context.WithCancel(context.Background())
j := &knowledgeJournal{cancel: cancel, done: make(chan struct{}), vault: vault, project: a.config.Workspace}
a.knowledgeMu.Lock()
a.knowledgeJournal = j
a.knowledgeMu.Unlock()
sessionID := a.id
go func() {
defer close(j.done)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
// A bounded final copy helps small chats close immediately. The
// process worker recovers the remaining durable journal later.
finalCtx, finish := context.WithTimeout(context.Background(), time.Second)
a.scanKnowledge(finalCtx, j, path, sessionID)
finish()
return
case <-ticker.C:
a.scanKnowledge(ctx, j, path, sessionID)
}
}
}()
}
// rebindKnowledgeLocked restarts capture after a persisted project scope change.
// Caller holds a.mu. The retired scanner can finish without holding that lock;
// stable source IDs make overlapping scans safe.
func (a *Agent) rebindKnowledgeLocked(project string) {
a.knowledgeMu.Lock()
old := a.knowledgeJournal
if old != nil {
old.cancel()
a.knowledgeRetired = append(a.knowledgeRetired, old)
}
a.knowledgeVault = knowledge.Open(config.ProfilePath(a.config.ProfileDir, ""))
a.knowledgeJournal = nil
a.knowledgeMu.Unlock()
a.startKnowledge()
}
func (a *Agent) stopKnowledge() {
a.knowledgeMu.RLock()
workers := append([]*knowledgeJournal(nil), a.knowledgeRetired...)
if a.knowledgeJournal != nil {
workers = append(workers, a.knowledgeJournal)
}
a.knowledgeMu.RUnlock()
for _, j := range workers {
j.cancel()
}
timer := time.NewTimer(closeGrace)
defer timer.Stop()
for _, j := range workers {
select {
case <-j.done:
case <-timer.C:
log.Printf("knowledge: capture shutdown deadline; journal recovery will retry")
return
}
}
}
// scanKnowledge reads complete journal lines by byte offset. Re-journaled
// compaction windows are skipped, so stubs and summaries never become new
// claims. IDs survive reopen/retry and do not conflate equal human messages.
func (a *Agent) scanKnowledge(ctx context.Context, j *knowledgeJournal, path, sessionID string) (err error) {
j.mu.Lock()
defer j.mu.Unlock()
defer func() {
if err != nil {
j.lastError = err.Error()
log.Printf("knowledge: capture pending: %v", err)
} else {
j.lastError = ""
}
}()
if !j.vault.Enabled() {
return nil
}
since := j.vault.EnabledSince()
if since.IsZero() {
return fmt.Errorf("knowledge: missing opt-in timestamp")
}
cursor := knowledge.JournalCursor{Offset: j.offset, Skip: j.skip, Scope: j.scope}
if cursor.Scope == "" {
cursor.Scope = j.project
}
history := knowledgeHistory{}
journal := knowledge.Journal{ID: path, Session: sessionID, Project: j.project}
{
batch, e := history.Read(ctx, journal, cursor)
if e != nil {
return e
}
for _, source := range batch.Sources {
if e = ctx.Err(); e != nil {
return e
}
stamp, e := time.Parse(time.RFC3339Nano, source.TS)
if e != nil {
return e
}
if stamp.Before(since) {
continue
}
if e = j.vault.Capture(source); e != nil {
return e
}
}
j.offset = batch.Cursor.Offset
j.skip = batch.Cursor.Skip
j.scope = batch.Cursor.Scope
}
return nil
}