{"record":{"id":"299058032ba7afd7","repo":"Tencent/WeKnora","slug":"create-memory-item-w","errorCode":null,"errorMessage":"create memory item: %w","messagePattern":"create memory item: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/memory/service.go","lineNumber":396,"sourceCode":"\t\tTenantID:        scope.TenantID,\n\t\tSubjectID:       scope.SubjectID,\n\t\tKind:            item.Kind,\n\t\tContent:         content,\n\t\tTopic:           topic,\n\t\tNormalizedKey:   normalizedKey,\n\t\tImportance:      types.ClampMemoryImportance(item.Importance),\n\t\tOrigin:          item.Origin,\n\t\tStatus:          statusForWrite(item),\n\t\tSourceSessionID: item.SourceSessionID,\n\t\tSourceMessageID: item.SourceMessageID,\n\t\tValidFrom:       time.Now(),\n\t\tExpiresAt:       item.ExpiresAt,\n\t}\n\tif stored.Origin == \"\" {\n\t\tstored.Origin = types.MemoryOriginExtracted\n\t}\n\tif err := s.repo.CreateItem(ctx, stored); err != nil {\n\t\treturn nil, fmt.Errorf(\"create memory item: %w\", err)\n\t}\n\tif existing != nil {\n\t\t// Supersede rather than delete: the old statement keeps its content\n\t\t// and gains invalid_at, so the memory manager can show what changed.\n\t\tif err := s.repo.SupersedeItem(ctx, scope, existing.ID, stored.ID); err != nil {\n\t\t\tlogger.Warnf(ctx, \"memory: supersede %s failed: %v\", existing.ID, err)\n\t\t}\n\t}\n\n\ts.enforceCapacity(ctx, scope, cfg)\n\ts.rebuildBlock(ctx, scope)\n\t// A memory with no vector is invisible to semantic recall, so this runs on\n\t// every write. It is best effort: failing to embed must not fail the write,\n\t// and the backfill pass picks up whatever this missed.\n\ts.storeItemEmbedding(ctx, scope, cfg, stored)\n\treturn stored, nil\n}\n","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/memory/service.go#L378-L414","documentation":"Wraps a failure from repo.CreateItem when persisting a new or superseding memory item. The service constructed the stored item (including default Origin) but the storage layer refused or failed the insert. The write cannot complete, so no memory is recorded.","triggerScenarios":"Any of write/Remember/PromoteTopic/CreateItem/mergeRedundant/applyDecisions/observeTopics reaching repo.CreateItem and the insert failing — DB down, unique constraint conflict on item key, invalid column value (e.g. oversized content), or context cancellation.","commonSituations":"Duplicate ID collision when generating item IDs; content exceeding a DB column limit; database read-only or disk full in self-hosted deployments; context canceled because the HTTP request timed out mid-write.","solutions":["Inspect the wrapped error for the concrete DB failure (constraint, size, connection) and address it.","Verify item ID generation and unique indexes to rule out key collisions.","Check content/topic sanitization and length limits against the schema.","Retry after transient DB errors; supersede logic preserves history so a retry is safe."],"exampleFix":"// before\nif err := s.repo.CreateItem(ctx, stored); err != nil {\n    return nil, fmt.Errorf(\"create memory item: %w\", err)\n}\n// after\nif err := s.repo.CreateItem(ctx, stored); err != nil {\n    if ctx.Err() != nil {\n        return nil, fmt.Errorf(\"create memory item: %w\", ctx.Err())\n    }\n    return nil, fmt.Errorf(\"create memory item: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// pre-validate before calling write\nif len(item.Content) > maxContentLen { return fmt.Errorf(\"content too long: %d\", len(item.Content)) }\nif strings.TrimSpace(item.Topic) == \"\" { return fmt.Errorf(\"topic required\") }","typeGuard":null,"tryCatchPattern":"stored, err := svc.Remember(ctx, scope, item)\nif err != nil && strings.Contains(err.Error(), \"create memory item\") {\n    if isTransient(err) { stored, err = svc.Remember(ctx, scope, item) } // idempotent retry\n}","preventionTips":["Keep content within DB column limits","Ensure unique index matches your ID generation to avoid collisions","Retry idempotently — identical content returns the existing item"],"tags":["database","memory","insert"],"backgroundTag":"memory-write-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}