소스 검색

fix: `text` must not satisfy the vector-insert guard - it is not embedded

The guard added moments ago accepted a document carrying `text` on the
assumption that a row with text becomes searchable. That is true of
`POST .../vectors` and FALSE of `POST .../documents`, which does not
embed anything - only vectors.cpp does. So the guard accepted a
text-only row, stored it without a vector, and produced exactly the
permanently unfindable document it exists to refuse, while looking like
it had permitted ordinary ingest. Same hole in
`ctx.collections(...).insert()`, where `.store()` is the embedding path
and `.insert()` is not.

Found by a consumer asking whether an EMPTY `text` would satisfy the
guard, which prompted checking whether a NON-empty one should. The
question was about a case their chunker cannot produce; the answer was
that the rule was wrong for every case.

The rule is now simply: a vector collection takes a vector. Empty text,
non-empty text and absent text are one rule instead of three, which
removes the special case rather than deciding it. Both refusals name the
route that DOES embed, so a caller is pointed somewhere rather than
merely refused.

THE TEST WAS ALSO AT FAULT and is rewritten. It asserted that a
text-carrying POST did not return `vector_missing` - that the refusal
did not fire - and treated that as a pass. Asserting "the refusal did
not fire" is not the same as asserting the outcome is good, and only the
second is worth testing. It now checks outcomes: the reported shape
refused with nothing stored, text-only refused, empty text refused, an
explicit vector accepted AND FINDABLE BY A SEARCH, and the vectors route
still accepting a write - that last one proving the refusal has not
blocked the path it tells callers to use. It would fail against the
guard it replaces.

884 tests pass.
fszontagh 1 개월 전
부모
커밋
11a8fd751c
3개의 변경된 파일과 80개의 추가작업 그리고 38개의 파일을 삭제
  1. 0 0
      api/llms.txt
  2. 23 19
      src/handlers/documents.cpp
  3. 57 19
      tests/test_api_integration.cpp

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
api/llms.txt


+ 23 - 19
src/handlers/documents.cpp

@@ -92,28 +92,32 @@ void registerDocumentRoutes(ApiServer& s) {
         nlohmann::json data = documentBody(body);
 
         // The sibling of `vector_replace_unsafe` above, arriving through a
-        // different door. A document inserted into a VECTOR collection with
-        // neither a vector nor text to embed reads back perfectly and can
-        // never be found by any search - it has no embedding, and no read path
-        // returns one, so nothing about it looks wrong.
+        // different door. A document inserted into a VECTOR collection with no
+        // vector reads back perfectly and can never be found by any search - it
+        // has no embedding, and no read path returns one, so nothing about it
+        // looks wrong.
         //
-        // Reported from production with the arithmetic that exposed it: a
-        // 12448-document collection whose three indexed facets each summed to
-        // 12447, the extra row being a query body posted to this route by
-        // mistake. The consumer spent real time explaining that discrepancy.
+        // Reported from production with the only symptom it has: a 12448-row
+        // collection whose three indexed facets each summed to 12447, the extra
+        // row being a query body posted to this route by mistake.
         //
-        // A metadata-only row in a vector collection is invisible to search BY
-        // DEFINITION, so refusing it cannot take away anything anyone could
-        // have been using deliberately. Named both ways out, as the PUT guard
-        // does - and deliberately not repaired by embedding something, which
-        // would spend the project's credit on a request that never asked.
-        if (t.meta.kind == "vector" && !data.contains("_vector") && !data.contains("vector") &&
-            !data.contains("text"))
+        // `text` DOES NOT SATISFY THIS, and that is the whole subtlety. THIS
+        // ROUTE DOES NOT EMBED - only the vectors route does. An earlier
+        // version of this guard accepted `text` on the assumption that a row
+        // with text becomes searchable, which is true of `.../vectors` and
+        // false here: it would have stored the exact invisible row the guard
+        // exists to refuse, while looking like it had allowed ordinary ingest.
+        // Caught by a consumer asking whether an EMPTY text would satisfy it,
+        // which made me check whether a NON-empty one should.
+        //
+        // So the rule is simply: a vector collection takes a vector. Text goes
+        // to `.../vectors`, which embeds it.
+        if (t.meta.kind == "vector" && !data.contains("_vector") && !data.contains("vector"))
             throw ApiError(ErrCode::Unprocessable, "vector_missing",
-                           "a document inserted into a vector collection must carry a vector or "
-                           "text to embed, or it can never be found by a search. Provide 'text', "
-                           "or POST .../vectors to store a document together with its vector, or "
-                           "use a json collection if this row is not meant to be searchable");
+                           "a document inserted into a vector collection must carry '_vector', or "
+                           "it can never be found by a search. This route does not embed: POST "
+                           ".../vectors with 'text' to have it embedded for you, or use a json "
+                           "collection if this row is not meant to be searchable");
 
         const uint32_t ttl = ttlParam(req).value_or(0);
         std::string newId;

+ 57 - 19
tests/test_api_integration.cpp

@@ -2389,10 +2389,18 @@ TEST_F(ApiFixture, ARecycledCollectionNameInheritsNoConstraint) {
 // it. On data that cost money to produce.
 TEST_F(ApiFixture, InsertingAVectorlessDocumentIntoAVectorCollectionIsRefused) {
     // The sibling of the PUT refusal below, through a different door. Reported
-    // from production: a 12448-document collection whose three indexed facets
-    // each summed to 12447, the extra row being a query body posted to the
-    // documents route by mistake. It read back perfectly and could never be
-    // found by a search, which is why the arithmetic was the only symptom.
+    // from production with the only symptom it has: a 12448-document collection
+    // whose three indexed facets each summed to 12447, the extra row being a
+    // query body posted to the documents route by mistake. It read back
+    // perfectly and could never be found by a search.
+    //
+    // THE FIRST VERSION OF THIS TEST WAS TOO WEAK AND LET A HOLE THROUGH. It
+    // asserted that a `text`-carrying POST did not return `vector_missing` -
+    // that is, that the refusal did not fire - and called that a pass. But this
+    // route DOES NOT EMBED; only `.../vectors` does. So the row it accepted was
+    // stored with no vector and was exactly the invisible document the guard
+    // exists to refuse. Asserting "the refusal did not fire" is not the same as
+    // asserting the outcome is good, and only the latter is worth testing.
     auto c = admin();
     const std::string base = "/api/v1/projects/" + project_ + "/collections";
     const std::string ct = "application/json";
@@ -2405,9 +2413,15 @@ TEST_F(ApiFixture, InsertingAVectorlessDocumentIntoAVectorCollectionIsRefused) {
         if (!r || r->status != 200) return std::size_t{0};
         return nlohmann::json::parse(r->body)["documents"].size();
     };
+    auto searchable = [&]() {
+        auto r = c.Post((base + "/vins/search").c_str(),
+                        nlohmann::json{{"query_vector",{1.0,0.0,0.0,0.5}},{"top_k",10}}.dump(), ct);
+        if (!r || r->status != 200) return std::size_t{0};
+        return nlohmann::json::parse(r->body)["results"].size();
+    };
     ASSERT_EQ(count(), 0u);
 
-    // Exactly the shape that was reported: a body with no vector and no text.
+    // 1. Exactly the shape that was reported: no vector, no text.
     auto bad = c.Post((base + "/vins/documents").c_str(),
                       nlohmann::json{{"filter",{{"sourceType","email"}}},{"limit",60}}.dump(), ct);
     ASSERT_TRUE(bad);
@@ -2415,23 +2429,47 @@ TEST_F(ApiFixture, InsertingAVectorlessDocumentIntoAVectorCollectionIsRefused) {
     EXPECT_NE(bad->body.find("vector_missing"), std::string::npos) << bad->body;
     EXPECT_EQ(count(), 0u) << "the refused insert must not have stored anything";
 
-    // An explicit vector is accepted - the refusal must not block the real
-    // write path, or it would be a far worse bug than the one it fixes.
+    // 2. TEXT ALONE IS ALSO REFUSED, and this is the case that caught me. This
+    // route does not embed, so a text-only row would be stored without a vector
+    // and be unfindable forever. The message must point at the route that DOES
+    // embed rather than just saying no.
+    auto textOnly = c.Post((base + "/vins/documents").c_str(),
+                           nlohmann::json{{"text","hello"},{"tag","b"}}.dump(), ct);
+    ASSERT_TRUE(textOnly);
+    EXPECT_EQ(textOnly->status, 422) << textOnly->body;
+    EXPECT_NE(textOnly->body.find("vector_missing"), std::string::npos) << textOnly->body;
+    EXPECT_NE(textOnly->body.find("vectors"), std::string::npos)
+        << "the refusal must name the route that embeds: " << textOnly->body;
+    EXPECT_EQ(count(), 0u);
+
+    // 3. An EMPTY text is refused too - the question that exposed all of this.
+    // It needs no special case now: text does not satisfy the guard at all, so
+    // "" and "hello" and absent are one rule rather than three.
+    auto emptyText = c.Post((base + "/vins/documents").c_str(),
+                            nlohmann::json{{"text",""}}.dump(), ct);
+    ASSERT_TRUE(emptyText);
+    EXPECT_EQ(emptyText->status, 422) << emptyText->body;
+    EXPECT_EQ(count(), 0u);
+
+    // 4. An explicit vector is accepted AND IS SEARCHABLE. The second half is
+    // the assertion that matters: a guard that let a write through while
+    // leaving it unfindable is the defect, so the test has to look for the row
+    // through a SEARCH rather than through a read.
     auto withVec = c.Post((base + "/vins/documents").c_str(),
                           nlohmann::json{{"_vector",{1.0,0.0,0.0,0.5}},{"tag","a"}}.dump(), ct);
     ASSERT_TRUE(withVec);
-    EXPECT_EQ(withVec->status, 201) << withVec->body;
-
-    // And `text` satisfies it too, because a row with text gets embedded and IS
-    // searchable. Refusing it would have broken ordinary ingest, which is the
-    // common case - so this asserts the guard is not over-broad. The insert may
-    // fail for want of an embedding provider in this environment; what must NOT
-    // happen is a 422 naming vector_missing.
-    auto withText = c.Post((base + "/vins/documents").c_str(),
-                           nlohmann::json{{"text","hello"},{"tag","b"}}.dump(), ct);
-    ASSERT_TRUE(withText);
-    EXPECT_EQ(withText->body.find("vector_missing"), std::string::npos)
-        << "text must satisfy the guard: " << withText->body;
+    ASSERT_EQ(withVec->status, 201) << withVec->body;
+    EXPECT_EQ(count(), 1u);
+    EXPECT_EQ(searchable(), 1u) << "an accepted row must be findable, which is the entire point";
+
+    // 5. And the vectors route, which embeds, still accepts a bare vector -
+    // proof the refusal has not blocked the path it tells callers to use.
+    auto viaVectors = c.Post((base + "/vins/vectors").c_str(),
+                             nlohmann::json{{"vector",{0.0,1.0,0.0,0.0}},
+                                            {"metadata",{{"tag","c"}}}}.dump(), ct);
+    ASSERT_TRUE(viaVectors);
+    EXPECT_EQ(viaVectors->status, 201) << viaVectors->body;
+    EXPECT_EQ(searchable(), 2u);
 
     auto del = c.Delete((base + "/vins").c_str());
     ASSERT_TRUE(del);

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.