feat(runtime+admin): A2 LLM异步提取接线 + A4 Tracing接线 + A6通知后端聚合
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

A2 异步记忆提取:
- loop/memory.go: asyncExtractor接口(ExtractAsync)+extractMemoryAsync(60s超时,WithOutCancel,dispatch即返回,后台落库,失败仅Warn)+persistMemories共用
- run.go 三个回合落定点改异步提取; 删除run.go逐步提取(design/03'每轮后', 避免多轮工具N次LLM调用/重复写)
- cmd/cyrene/extractor.go buildMemoryExtractor: llmGroupUsable为真→LLMExtractor, 否则回落ManualExtractor(日志说明); keylessEndpoint放行loopback/私网

A4 可观测性 Tracing:
- cmd/cyrene/observability.go: newAppTraceWriter+traceFanout(多sink)+gatewaySpanSink(走gw.ObserveSpan,喂环形缓冲+LLM/subagent指标)
- wire.go 注入; 未改trace/gateway数据结构

A6 通知后端:
- internal/services/gateway/notifications.go(311行): GET /admin/notifications?type=&before=&limit= (adminAuth)
- 聚合已有源(obs.audit SQLite + s.backthink), createdAt倒序, id=source:id; 类型派生(channel/system/audit/backthink); 无新建表/无新Deps/wire零改动
- 诚实: backthink仅shouldPush计, 未注入源为空, 报错500, 未读游标后置
- 契约与前端Y一致: {id,type,title,body,source,createdAt}, before含游标(<=), type未知400
- Docs/api-client v0.33

全仓 build/vet/test绿(56包)+win/darwin/linux-arm64交叉编译绿; 新增测试(memory_async/extractor/observability/notifications)
This commit is contained in:
AskaEth committed 2026-09-30 09:03:16 +08:00
1 parent 34c8cc4dca
commit 153a9d6211
12 files changed
+1427 -11

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
package main
import (
"net"
"net/url"
"strings"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// memoryOwnerRoleID is the role id the composition root authors extracted
// memories with; the owner is the only actor whose private memory is written
// (group/stranger conversations never extract at all, design/11 §2).
const memoryOwnerRoleID = "owner"
// buildMemoryExtractor selects the memory extractor the loop runs after each
// settled turn (design/03 §1 LLM 异步自动提取):
//
// - the LLM extractor when the provider stack has a usable active group, so
// the model denoises and deduplicates what is worth remembering;
// - the deterministic ManualExtractor otherwise, so a keyless deployment still
// records the owner's explicit "记住 X" directives instead of failing a model
// call on every turn.
//
// The loop detaches the LLM extractor to a background goroutine (ExtractAsync);
// see internal/runtime/loop/memory.go.
func (a *app) buildMemoryExtractor(personaID string) memory.Extractor {
if llmGroupUsable(a.llm) {
a.log.Info("memory extractor: llm", "model", a.llm.Model(), "provider", a.llm.Provider())
return memory.NewLLMExtractor(a.llm, personaID, memoryOwnerRoleID)
}
a.log.Warn("memory extractor: manual (no usable llm model group)")
return memory.NewManualExtractor(personaID, memoryOwnerRoleID)
}
// llmGroupUsable reports whether the active model group of the runtime stack can
// actually be called: a model and a base URL are mandatory, and either an API key
// is configured or the endpoint is a local/private address, where a keyless call
// is the normal case (a self-hosted OpenAI-compatible server).
//
// It is deliberately a cheap structural check, never a probe request: boot must
// not spend a model call. A group that looks usable but fails at call time only
// logs an extraction error (extraction failures are never fatal).
func llmGroupUsable(llm *model.RuntimeLLM) bool {
if llm == nil {
return false
}
group := llm.Group()
if strings.TrimSpace(group.Model) == "" || strings.TrimSpace(group.BaseURL) == "" {
return false
}
if strings.TrimSpace(group.APIKey) != "" {
return true
}
return keylessEndpoint(group.BaseURL)
}
// keylessEndpoint reports whether baseURL points at a loopback or private-network
// host, where a missing API key is expected rather than a misconfiguration.
func keylessEndpoint(baseURL string) bool {
parsed, err := url.Parse(strings.TrimSpace(baseURL))
if err != nil {
return false
}
host := parsed.Hostname()
if host == "" {
return false
}
if strings.EqualFold(host, "localhost") {
return true
}
ip := net.ParseIP(host)
if ip == nil {
return false
}
return ip.IsLoopback() || ip.IsPrivate()
}
+79
View File
@@ -0,0 +1,79 @@
package main
import (
"path/filepath"
"testing"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory"
modelpkg "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
)
// testRuntimeLLM builds a provider stack from env alone (the pointed-at
// models.json does not exist), so a test controls the active group through the
// flat LLM config exactly as a keyless .env boot would.
func testRuntimeLLM(t *testing.T, env config.LLMConfig) *modelpkg.RuntimeLLM {
t.Helper()
return modelpkg.NewRuntimeLLM(filepath.Join(t.TempDir(), "models.json"), env, testLog())
}
// TestBuildMemoryExtractor_LLMWhenCredentials proves a configured provider group
// selects the LLM extractor (design/03 §1).
func TestBuildMemoryExtractor_LLMWhenCredentials(t *testing.T) {
t.Parallel()
a := &app{
log: testLog(),
llm: testRuntimeLLM(t, config.LLMConfig{
BaseURL: "https://api.example.com/v1",
APIKey: "sk-test",
Model: "gpt-4o-mini",
}),
}
if _, ok := a.buildMemoryExtractor("persona-a").(*memory.LLMExtractor); !ok {
t.Fatalf("extractor = %T, want *memory.LLMExtractor (credentials configured)", a.buildMemoryExtractor("persona-a"))
}
}
// TestBuildMemoryExtractor_ManualWhenNoCredentials proves the keyless fallback: a
// boot without an API key (and without a local endpoint) keeps the deterministic
// ManualExtractor instead of firing a doomed model call on every turn.
func TestBuildMemoryExtractor_ManualWhenNoCredentials(t *testing.T) {
t.Parallel()
a := &app{log: testLog(), llm: testRuntimeLLM(t, config.LLMConfig{})}
if _, ok := a.buildMemoryExtractor("persona-a").(*memory.ManualExtractor); !ok {
t.Fatalf("extractor = %T, want *memory.ManualExtractor (no usable llm group)", a.buildMemoryExtractor("persona-a"))
}
bare := &app{log: testLog()}
if _, ok := bare.buildMemoryExtractor("persona-a").(*memory.ManualExtractor); !ok {
t.Fatalf("extractor = %T, want *memory.ManualExtractor (nil provider stack)", bare.buildMemoryExtractor("persona-a"))
}
}
// TestLLMGroupUsable_KeylessLocalEndpoint locks the ollama-style case: a local
// OpenAI-compatible server needs no API key, so extraction stays LLM-backed.
func TestLLMGroupUsable_KeylessLocalEndpoint(t *testing.T) {
t.Parallel()
cases := []struct {
name string
env config.LLMConfig
ready bool
}{
{name: "api key", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", APIKey: "sk", Model: "m"}, ready: true},
{name: "loopback keyless", env: config.LLMConfig{BaseURL: "http://127.0.0.1:11434/v1", Model: "llama3"}, ready: true},
{name: "localhost keyless", env: config.LLMConfig{BaseURL: "http://localhost:1234/v1", Model: "m"}, ready: true},
{name: "private keyless", env: config.LLMConfig{BaseURL: "http://192.168.1.20:11434/v1", Model: "m"}, ready: true},
{name: "remote keyless", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", Model: "m"}, ready: false},
{name: "no model", env: config.LLMConfig{BaseURL: "https://api.example.com/v1", APIKey: "sk"}, ready: false},
{name: "no base url", env: config.LLMConfig{APIKey: "sk", Model: "m"}, ready: false},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := llmGroupUsable(testRuntimeLLM(t, tc.env)); got != tc.ready {
t.Fatalf("llmGroupUsable = %v, want %v", got, tc.ready)
}
})
}
}
+98
View File
@@ -0,0 +1,98 @@
package main
import (
"fmt"
"os"
"sync"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
"git.yeij.top/AskaEth/Cyrene_v2/internal/services/gateway"
)
// newAppTraceWriter builds the composition root's trace writer: the JSONL stderr
// recorder (the local structured log of every span) plus room for the gateway's
// observability sink, which can only be registered once the gateway exists (see
// wireGatewayTraceSink).
func newAppTraceWriter() *traceFanout {
return newTraceFanout(trace.NewJSONRecorder(os.Stderr))
}
// wireGatewayTraceSink registers the gateway's observability sink on the app's
// trace fanout. It is called from wireFrontend — the first moment the gateway
// exists — because the gateway is built after the loop that emits the spans.
// Without it the admin trace ring buffer is never fed and
// GET /api/v1/admin/observability/trace answers an empty list forever
// (design/19 §2). It is a no-op on a boot without a tracer or gateway.
func wireGatewayTraceSink(a *app, gw *gateway.Server) {
if a.traceWriter == nil || gw == nil {
return
}
a.traceWriter.Add(gatewaySpanSink{srv: gw})
a.log.Info("observability trace sink wired")
}
// traceFanout is the composition root's trace.Writer. It fans every completed
// span out to the sinks installed at boot (the JSONL stderr recorder) plus any
// sink registered later in the boot sequence: the gateway's observability buffer
// only exists once wireFrontend has built the gateway, while the loop that emits
// the spans is built earlier in wireRuntime.
//
// It is safe for concurrent use — the loop finishes spans from several
// goroutines (per-step, per-tool-call, sub-agents).
type traceFanout struct {
mu sync.RWMutex
writers []trace.Writer
}
// newTraceFanout builds a fanout over the given writers. Nil writers are dropped.
func newTraceFanout(writers ...trace.Writer) *traceFanout {
f := &traceFanout{}
for _, w := range writers {
f.Add(w)
}
return f
}
// Add installs one more sink. A nil writer is ignored.
func (f *traceFanout) Add(w trace.Writer) {
if w == nil {
return
}
f.mu.Lock()
defer f.mu.Unlock()
f.writers = append(f.writers, w)
}
// WriteSpan writes rec to every sink and returns the first error. Every sink is
// tried even after a failure, so one broken sink cannot hide a span from the
// others; the tracer only logs the returned error (trace.Tracer.emit).
func (f *traceFanout) WriteSpan(rec trace.SpanRecord) error {
f.mu.RLock()
defer f.mu.RUnlock()
var firstErr error
for _, w := range f.writers {
if w == nil {
continue
}
if err := w.WriteSpan(rec); err != nil && firstErr == nil {
firstErr = fmt.Errorf("trace fanout: %w", err)
}
}
return firstErr
}
// gatewaySpanSink adapts *gateway.Server to trace.Writer through its ObserveSpan
// entry point, so an emitted span lands in the admin panel's trace ring buffer
// AND in the LLM/subagent metric counters (ObserveSpan appends to the buffer and
// observes the metrics; the buffer is what GET /api/v1/admin/observability/trace
// serves).
type gatewaySpanSink struct{ srv *gateway.Server }
// WriteSpan feeds rec to the gateway. A missing server is a no-op.
func (g gatewaySpanSink) WriteSpan(rec trace.SpanRecord) error {
if g.srv == nil {
return nil
}
g.srv.ObserveSpan(rec)
return nil
}
+164
View File
@@ -0,0 +1,164 @@
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.yeij.top/AskaEth/Cyrene_v2/internal/auth"
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
"git.yeij.top/AskaEth/Cyrene_v2/internal/services/gateway"
)
const obsTestSecret = "0123456789abcdef0123456789abcdef"
// recordingWriter captures the spans a fanout hands it.
type recordingWriter struct {
spans []trace.SpanRecord
}
func (r *recordingWriter) WriteSpan(rec trace.SpanRecord) error {
r.spans = append(r.spans, rec)
return nil
}
// TestTraceFanout_LateSinkReceivesSpans proves the composition root can register
// a sink after the loop's tracer is already running: spans finished before the
// sink is added do not reach it, every span after does (the gateway is built
// after the loop, so this ordering is what A4 depends on).
func TestTraceFanout_LateSinkReceivesSpans(t *testing.T) {
t.Parallel()
early := &recordingWriter{}
fanout := newTraceFanout(early)
tracer := trace.New(fanout)
_, first := tracer.StartSpan(context.Background(), "before", trace.SpanKindStateTransition)
first.Finish(trace.SpanStatusOK)
late := &recordingWriter{}
fanout.Add(late)
_, second := tracer.StartSpan(context.Background(), "after", trace.SpanKindLLM)
second.Finish(trace.SpanStatusOK)
if len(early.spans) != 2 {
t.Fatalf("early sink spans = %d, want 2", len(early.spans))
}
if len(late.spans) != 1 || late.spans[0].ID != second.ID {
t.Fatalf("late sink spans = %+v, want exactly the span %q", late.spans, second.ID)
}
}
// TestWireGatewayTraceSink_Guards proves the boot helper tolerates a missing
// tracer or gateway, so a degraded boot cannot panic on the observability path.
func TestWireGatewayTraceSink_Guards(t *testing.T) {
t.Parallel()
wireGatewayTraceSink(&app{log: testLog()}, nil)
a := &app{log: testLog(), traceWriter: newAppTraceWriter()}
wireGatewayTraceSink(a, nil)
if len(a.traceWriter.writers) != 1 {
t.Fatalf("writers = %d, want 1 (the stderr recorder only)", len(a.traceWriter.writers))
}
}
// TestGatewaySpanSink_FeedsTraceEndpointAndMetrics is the A4 acceptance: a span
// emitted by the tracer is written through the gateway sink (registered by the
// same helpers wire.go calls) and surfaces both in
// GET /api/v1/admin/observability/trace (a real span, not an empty buffer) and in
// the LLM metric counter.
func TestGatewaySpanSink_FeedsTraceEndpointAndMetrics(t *testing.T) {
t.Parallel()
jwt := auth.New(config.AuthConfig{JWTSecret: obsTestSecret})
gw, err := gateway.New(&config.Config{}, gateway.Deps{Log: testLog(), Auth: jwt})
if err != nil {
t.Fatalf("gateway.New: %v", err)
}
t.Cleanup(gw.Close)
ts := httptest.NewServer(gw.Handler())
t.Cleanup(ts.Close)
// Exactly the boot path: the app's fanout is built in wireRuntime and the
// gateway sink is registered in wireFrontend.
a := &app{log: testLog(), traceWriter: newAppTraceWriter()}
wireGatewayTraceSink(a, gw)
tracer := trace.New(a.traceWriter)
ctx := context.Background()
_, llmSpan := tracer.StartSpan(ctx, "llm.chat", trace.SpanKindLLM)
llmSpan.Finish(trace.SpanStatusOK)
_, stepSpan := tracer.StartSpan(ctx, "loop.step", trace.SpanKindStateTransition)
_ = stepSpan.SetProp("session", "web")
stepSpan.Finish(trace.SpanStatusOK)
tok, err := jwt.Sign("acct", auth.RoleOwner, time.Hour)
if err != nil {
t.Fatalf("sign token: %v", err)
}
var traceBody struct {
Data []struct {
ID string `json:"id"`
Name string `json:"name"`
Kind string `json:"kind"`
} `json:"data"`
}
status, raw := obsGet(t, ts.URL+"/api/v1/admin/observability/trace", tok)
if status != http.StatusOK {
t.Fatalf("trace status = %d, want 200 (body=%s)", status, raw)
}
if err := json.Unmarshal(raw, &traceBody); err != nil {
t.Fatalf("decode trace: %v (body=%s)", err, raw)
}
seen := map[string]string{}
for _, sp := range traceBody.Data {
seen[sp.ID] = sp.Kind
}
if seen[stepSpan.ID] != string(trace.SpanKindStateTransition) {
t.Fatalf("trace endpoint = %+v, want the loop.step span %q", traceBody.Data, stepSpan.ID)
}
if seen[llmSpan.ID] != string(trace.SpanKindLLM) {
t.Fatalf("trace endpoint = %+v, want the llm span %q", traceBody.Data, llmSpan.ID)
}
var metricsBody struct {
Data struct {
LLMCalls int64 `json:"llmCalls"`
} `json:"data"`
}
status, raw = obsGet(t, ts.URL+"/api/v1/admin/observability/metrics", tok)
if status != http.StatusOK {
t.Fatalf("metrics status = %d, want 200 (body=%s)", status, raw)
}
if err := json.Unmarshal(raw, &metricsBody); err != nil {
t.Fatalf("decode metrics: %v (body=%s)", err, raw)
}
if metricsBody.Data.LLMCalls != 1 {
t.Fatalf("llmCalls = %d, want 1 (ObserveSpan counts the emitted llm span)", metricsBody.Data.LLMCalls)
}
}
// obsGet performs an authenticated owner GET and returns the status plus the raw
// body.
func obsGet(t *testing.T, url, token string) (int, []byte) {
t.Helper()
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatalf("new request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("GET %s: %v", url, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read %s: %v", url, err)
}
return resp.StatusCode, raw
}
+14 -3
View File
@@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
@@ -117,6 +116,11 @@ type app struct {
// satisfied by *gateway.Server (BroadcastMood); held as an interface so the
// composition root is unit-testable with a fake sink.
moodSink moodEmitter
// traceWriter fans finished trace spans out to the JSONL stderr recorder and,
// once the gateway exists, to the gateway's observability buffer behind
// GET /api/v1/admin/observability/trace (design/19 §2). It is held so
// wireFrontend can register the gateway sink on the already-running tracer.
traceWriter *traceFanout
}
// capacityEmitter is the gateway-facing sink for capacity_awaken broadcasts.
@@ -360,7 +364,10 @@ func (a *app) wireRuntime() error {
personaID := a.defaultPersonaID()
store := a.memStore
tracer := trace.New(trace.NewJSONRecorder(os.Stderr))
// Spans fan out to the JSONL stderr recorder and — once wireFrontend has built
// the gateway — to its observability buffer (cmd/cyrene/observability.go).
a.traceWriter = newAppTraceWriter()
tracer := trace.New(a.traceWriter)
// Realtime flow visualization (design/19 §1): the publisher accumulates the
// loop's trace spans into a per-session flow graph and publishes
// ws.graph_update on each span transition. It is gated to the owner's web
@@ -413,7 +420,7 @@ func (a *app) wireRuntime() error {
OnTurnEnd: a.onTurnEnd,
Memory: store,
Retriever: a.retriever,
Extractor: memory.NewManualExtractor(personaID, "owner"),
Extractor: a.buildMemoryExtractor(personaID),
Checkpoint: checkpoint,
Tracer: tracer,
Flow: flowPub,
@@ -738,6 +745,10 @@ func (a *app) wireFrontend() error {
}
gw.SetVoice(a.voice)
a.gw = gw // assign BEFORE wiring hooks below, which capture a.gw / gw (nil-receiver panic otherwise)
// Observability trace sink (design/19 §2): now that the gateway exists, its
// ring buffer must be fed by the loop's spans, or the admin trace/metrics
// endpoints stay empty.
wireGatewayTraceSink(a, gw)
if a.channelBridge != nil {
// Route the channel bridge's owner-private persisted messages into the same
// `message_persisted` broadcast the orchestrator uses, so the Web client mirrors