feat(monitor): 死信中文提示与成功/取消自动退信
This commit is contained in:
@@ -134,15 +134,28 @@ func (handler *UploadHandler) Handle(ctx context.Context, job jobs.Job) error {
|
||||
return jobs.Permanent(jobs.ErrCodeArtifactInvalid, ErrUploadArtifactInvalid)
|
||||
}
|
||||
if uploadErr != nil {
|
||||
if !errors.Is(uploadErr, weknora.ErrConflict) && !weknora.IsRetryable(uploadErr) {
|
||||
return ErrUploadRemoteFailed
|
||||
}
|
||||
knowledge, err = handler.recoverAmbiguousUpload(ctx, candidate)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrUploadRemoteIdentity) {
|
||||
return handler.recordTerminalUploadFailure(ctx, candidate.ID, "upload_identity_ambiguous")
|
||||
if errors.Is(uploadErr, weknora.ErrConflict) && knowledge.ID != "" {
|
||||
// WeKnora dedupes identical file bytes across publications: when the
|
||||
// same document was already uploaded under a sibling publication (the
|
||||
// same file living in two source folders), the conflict response names
|
||||
// the existing item. Adopt it instead of dead-lettering forever.
|
||||
if verified, verifyErr := handler.client.GetKnowledge(ctx, knowledge.ID); verifyErr == nil &&
|
||||
verified.ID == knowledge.ID && verified.KnowledgeBaseID == candidate.TestKBID &&
|
||||
shaMatches(verified, candidate.SHA256) {
|
||||
knowledge = verified
|
||||
} else {
|
||||
return ErrUploadRemoteFailed
|
||||
}
|
||||
} else if !errors.Is(uploadErr, weknora.ErrConflict) && !weknora.IsRetryable(uploadErr) {
|
||||
return ErrUploadRemoteFailed
|
||||
} else {
|
||||
knowledge, err = handler.recoverAmbiguousUpload(ctx, candidate)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrUploadRemoteIdentity) {
|
||||
return handler.recordTerminalUploadFailure(ctx, candidate.ID, "upload_identity_ambiguous")
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
if knowledge.ID == "" || knowledge.KnowledgeBaseID != candidate.TestKBID {
|
||||
@@ -201,6 +214,15 @@ func metadataString(metadata map[string]any, key string) (string, bool) {
|
||||
return value, ok
|
||||
}
|
||||
|
||||
// shaMatches reports whether a remote item carries our content hash, either in
|
||||
// its metadata (uploads through governance) or as its file hash.
|
||||
func shaMatches(item weknora.Knowledge, sha string) bool {
|
||||
if value, ok := metadataString(item.Metadata, "content_sha256"); ok && value == sha {
|
||||
return true
|
||||
}
|
||||
return sha != "" && strings.EqualFold(item.FileHash, sha)
|
||||
}
|
||||
|
||||
func (handler *UploadHandler) recordTerminalUploadFailure(ctx context.Context, publicationID, code string) error {
|
||||
if err := handler.store.RecordUploadFailure(ctx, publicationID, code, handler.now()); err != nil {
|
||||
return ErrUploadStateConflict
|
||||
|
||||
@@ -352,3 +352,53 @@ func (uploadClientStub) ListKnowledge(context.Context, string, int, int) ([]wekn
|
||||
func (uploadClientStub) GetKnowledge(context.Context, string) (weknora.Knowledge, error) {
|
||||
return weknora.Knowledge{}, errors.New("unexpected get")
|
||||
}
|
||||
|
||||
func TestUploadHandlerAdoptsSameSHAItemOnDuplicateConflict(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "objects"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := []byte("duplicated across two folders\n")
|
||||
if err := os.WriteFile(filepath.Join(root, "objects", "artifact.md"), body, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest := sha256.Sum256(body)
|
||||
sha := hex.EncodeToString(digest[:])
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == http.MethodPost:
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_, _ = w.Write([]byte(`{"success":false,"code":"duplicate_file","message":"file exists","data":{"id":"knowledge-sibling","knowledge_base_id":"kb-1","file_hash":"` + sha + `"}}`))
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/knowledge/knowledge-sibling":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"success":true,"data":{"id":"knowledge-sibling","knowledge_base_id":"kb-1","file_hash":"` + sha + `","metadata":{"publication_id":"publication-other","content_sha256":"` + sha + `"}}}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: server.URL, APIKey: "test-key", Timeout: time.Second})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := &uploadStoreStub{candidate: postgresstore.PublicationForUpload{
|
||||
ID: "publication-1", TestKBID: "kb-1", IngestState: governance.IngestPending,
|
||||
ProcessingState: governance.ProcessingPending, SHA256: sha, SizeBytes: int64(len(body)),
|
||||
ValidatedFormat: "md", LocalSpoolRef: "objects/artifact.md", Title: "Safe title",
|
||||
}}
|
||||
handler, err := NewUploadHandler(UploadHandlerConfig{
|
||||
Store: store, Client: client, SpoolRoot: root, MaxUploadBytes: 50 * 1024 * 1024,
|
||||
FirstPollDelay: time.Second, Now: func() time.Time { return time.Now() },
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := handler.Handle(context.Background(), jobs.Job{Payload: []byte(`{"publication_id":"publication-1"}`)}); err != nil {
|
||||
t.Fatalf("Handle() error = %v, want duplicate adoption", err)
|
||||
}
|
||||
if store.recordedKnowledgeID != "knowledge-sibling" {
|
||||
t.Fatalf("knowledge=%q", store.recordedKnowledgeID)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user