Files
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

86 lines
2.7 KiB
Go

package memory
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// RecoverExtractions wakes only durable pending scopes. It never enumerates
// conversation history or changes a session checkpoint.
func (s *Service) RecoverExtractions(ctx context.Context) error {
subjects, err := s.repo.ListDueExtractions(ctx, 32)
if err != nil {
return err
}
for _, subject := range subjects {
if err := ctx.Err(); err != nil {
return err
}
scope := interfaces.MemoryScope{TenantID: subject.TenantID, SubjectID: subject.SubjectID}
cfg := s.workspaceConfig(ctx, scope.TenantID)
if !cfg.AutoExtractEnabled() {
// Release expired task identities before deferring disabled scopes,
// so they do not monopolize the bounded recovery scan.
if err := s.repo.PauseExtraction(ctx, scope, subject.ExtractAttemptID, 15*time.Minute); err != nil {
return err
}
continue
}
delay := extractFollowUpDelay
if subject.LastExtractedAt != nil {
if remaining := cfg.ExtractMinInterval() - time.Since(*subject.LastExtractedAt); remaining > delay {
delay = remaining
}
}
recoveryCtx := context.WithValue(ctx, types.TenantIDContextKey, scope.TenantID)
if subject.ExtractLanguage != "" {
recoveryCtx = context.WithValue(recoveryCtx, types.LanguageContextKey, subject.ExtractLanguage)
}
if err := s.enqueueExtraction(recoveryCtx, scope, "", "", subject.ExtractChatModelID, delay); err != nil {
logger.Warnf(ctx, "memory: recover pending extraction failed: %v", err)
}
}
return nil
}
func (s *Service) RetryExtraction(ctx context.Context) error {
scope, cfg, enabled := s.enabledScope(ctx)
if !enabled || !cfg.AutoExtractEnabled() {
return ErrMemoryDisabled
}
reset, err := s.repo.ResetExhaustedExtraction(ctx, scope)
if err != nil || !reset {
return err
}
return s.enqueueExtraction(ctx, scope, "", "", "", extractFollowUpDelay)
}
// StartExtractionRecovery is called after both Redis and Lite register the
// handler. The returned stop waits for the bounded sweep to exit.
func (s *Service) StartExtractionRecovery(parent context.Context) func() {
ctx, cancel := context.WithCancel(parent)
done := make(chan struct{})
go func() {
defer close(done)
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
sweepCtx, sweepCancel := context.WithTimeout(ctx, 30*time.Second)
if err := s.RecoverExtractions(sweepCtx); err != nil && ctx.Err() == nil {
logger.Warnf(ctx, "memory: extraction recovery sweep failed: %v", err)
}
sweepCancel()
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}()
return func() { cancel(); <-done }
}