Ver Fonte

docs: embedTimeoutMs resolves in whole seconds, with a 1 s floor per phase

Reported from production as "embedTimeoutMs is ignored on .store()",
with two measurements: a store succeeding with embedTimeoutMs 1, and a
search 504 arriving after 1041 ms rather than 1 ms.

Both are the same thing, and neither is a dropped option. .search() and
.store() resolve their vector through one function, which is the only
place the embedding call is made and the only place the per-call
timeout is read, so there is no separate store path to forget. The HTTP
client underneath takes WHOLE SECONDS and treats 0 as "no timeout", so
the budget split rounds up and floors each phase at 1 s: an ask of 1 ms
buys 1 s to connect plus 1 s to read. The timeout fired at its real
value. The store succeeded because the provider answered inside that
second, which is correct behaviour and indistinguishable from the
option being ignored unless the embedding takes over a second.

So the behaviour was right and the DOCUMENTATION was the defect:
llms.txt described the option as bounding the call and narrowing-only,
and said nothing about its resolution, leaving a consumer to discover
by measurement that 1 and 999 are the same value. It now states the
floor, that tuning below 1000 ms does nothing, to ask for 2000 or more,
that a timeout surfaces on a whole-second boundary, and that search and
store behave identically.

Three tests pin it: a sub-second ask resolves to 1 s and still reports
boundByCall, every value under a second is indistinguishable (asserted
across the range so nobody makes the floor value-dependent), and an ask
above the floor really does buy more read time - otherwise the first
two would be describing a dead option.

862 tests pass.
fszontagh há 1 mês atrás
pai
commit
cc4452d594
2 ficheiros alterados com 51 adições e 1 exclusões
  1. 2 1
      api/llms.txt
  2. 49 0
      tests/test_embed_budget.cpp

+ 2 - 1
api/llms.txt

@@ -221,7 +221,8 @@ export default async function (ctx) {
 - `ctx.collections(name)` - a chainable builder. `.filter('field:op:value')`, `.limit(n)`, `.offset(n)`, `.sort('field')` or `.sort('-field')` for descending, then a terminal: `.find()` returns an **array**, `.get(id)` returns the document or **null**, `.insert(doc)` returns `{id}`, `.update(id, doc)` returns `{id}`, `.patch(id, fields)` returns `{id, version}`, `.increment(id, {field: n})` returns the **new values** object (the same atomic increment as the HTTP route, needing the `update` grant; a missing document throws), `.delete(id)` returns `{deleted}`. Filters use the same `field:op:value` grammar as `?filter=`, parsed by the same code and evaluated by the database, so a filter means the same thing here as over HTTP.
 - **Write options**, previously undocumented. `ttl_seconds` is accepted by `.insert()`, `.update()`, `.patch()` and `.store()`; it sets or clears a document's expiry, and **absent is not zero** - omitting it leaves an existing expiry untouched while `0` clears it, so a plain `.patch()` cannot accidentally make a temporary document permanent. **`ifVersion: n`** makes the write conditional on the document still being at version `n` (as returned in `_version`); a lost race is **409** `version_conflict` carrying `current_version`, which means "someone else has it" and should be handled by dropping the work or re-reading, never by retrying the same write. `ifVersion` must be a positive integer - version 0 does not exist, since a first write produces 1. It is accepted by **`.update()` and `.patch()` only**: `.insert()` refuses it by name, because there is no existing version for a document that does not yet exist. `ttl_seconds` together with `ifVersion` is refused (`unsupported_combination`) rather than silently dropping one. A conditional `.update()` returns `{id, version}`; the unconditional form returns `{id}`.
 - Vector collections additionally take `.search({query_text|query_vector, top_k, min_score, filters, embedTimeoutMs?})`, returning an **array** of hits, and `.store({text|vector, metadata, embedTimeoutMs?})`. Embedding resolves through the normal precedence, so a project's provider override applies.
-- **`embedTimeoutMs` bounds the whole embedding call, connect phase included**, and is narrowing only - it cannot exceed what is left of the run's budget. Exceeding it is **504** `embedding_timeout`. A non-integer or non-positive value is **422**: it is validated rather than ignored, because an option that is accepted and does nothing is worse than one that is refused.
+- **`embedTimeoutMs` bounds the whole embedding call, connect phase included**, and is narrowing only - it cannot exceed what is left of the run's budget. It applies identically to `.search()` and `.store()`: both resolve their vector through the same code, so there is no asymmetry between them. Exceeding it is **504** `embedding_timeout`. A non-integer or non-positive value is **422**: it is validated rather than ignored, because an option that is accepted and does nothing is worse than one that is refused.
+- **Its RESOLUTION IS ONE SECOND, and the effective floor is 1 s per phase.** The HTTP client underneath takes whole seconds and treats 0 as "no timeout", so every value below 1000 ms resolves to the same thing: 1 s to connect plus 1 s to read. `embedTimeoutMs: 1` therefore allows up to **2 s**, not 1 ms, and a provider that answers inside that second succeeds normally - which reads exactly like the option being ignored. It is not: a value of 1 and a value of 999 are genuinely indistinguishable, and tuning inside that range does nothing. Ask for **2000 or more** if you want the split to be meaningful, and expect a timeout to surface at a whole-second boundary rather than at the millisecond you named. Connect never takes more than HALF the available time, so a 4000 ms ask becomes roughly 2 s connect + 2 s read.
 - **A blocking host call cannot be interrupted.** The run's wall budget is enforced between bytecode operations, so while an embedding or `ctx.fetch` request is in flight the deadline cannot be noticed - the budget ends the run once the call returns. Host calls are bounded by giving them timeouts that already fit the remaining budget (connect **and** read together), not by preemption. For a hard latency ceiling, catch the error and have a fallback path.
 - `ctx.fetch(url, {method, headers, body, timeoutMs, responseType})` - outbound HTTP, **SSRF-guarded**: loopback, private and link-local destinations are refused, which means a function cannot call this API back. The refusal is reported in the run's console rather than as a network error.
 - **A job, cron or db run's `ctx` is NOT the HTTP shape.** It carries `trigger`, `depth`, `enqueued_by` and `enqueued_at_ms`, with your payload under **`ctx.body`** - there is no `ctx.method`, `ctx.path`, `ctx.query` or `ctx.params`. A cron payload is `{schedule, scheduled_at}`; a database-event payload is `{collection, table, id, event, document}`.

+ 49 - 0
tests/test_embed_budget.cpp

@@ -131,4 +131,53 @@ TEST(EmbedBudget, RoundsUpSoATimeLeftBudgetIsNotTruncatedToNothing) {
     EXPECT_EQ(b.connectSec + b.readSec, 2);
 }
 
+
+TEST(EmbedBudget, AMillisecondTimeoutBecomesAWholeSecond) {
+    // Reported from production as "embedTimeoutMs is ignored on store": a
+    // consumer passed 1 and watched a store SUCCEED, and separately measured a
+    // search 504 arriving after 1041 ms rather than 1 ms. Both are this floor,
+    // not a missing implementation. The HTTP client takes WHOLE SECONDS and
+    // treats 0 as "no timeout", so anything under a second can only become 1 -
+    // and a provider that answers inside that second legitimately succeeds.
+    //
+    // So a sub-second embedTimeoutMs does not do what its name suggests, and
+    // that has to be stated in the consumer docs rather than discovered.
+    svapi::functions::EmbedBudgetRequest in;
+    in.remainingBudgetMs = 60000;
+    in.callTimeoutMs = 1;
+    const auto out = svapi::functions::resolveEmbedBudget(in);
+    EXPECT_EQ(out.connectSec, 1);
+    EXPECT_EQ(out.readSec, 1);
+    EXPECT_TRUE(out.boundByCall) << "a 1 ms ask is still the binding constraint, so a failure "
+                                    "must be reported as the script's choice";
+    // The observable consequence: up to 2 s, which is what was measured.
+    EXPECT_LE(out.connectSec + out.readSec, 2);
+}
+
+TEST(EmbedBudget, EveryTimeoutUnderASecondCollapsesToTheSameThing) {
+    // 1 ms and 999 ms are indistinguishable, so a caller tuning inside that
+    // range is tuning nothing. Asserted across the range so nobody "fixes"
+    // this by making the floor depend on the value.
+    for (const int64_t ms : {1, 50, 250, 500, 750, 999}) {
+        svapi::functions::EmbedBudgetRequest in;
+        in.remainingBudgetMs = 60000;
+        in.callTimeoutMs = ms;
+        const auto out = svapi::functions::resolveEmbedBudget(in);
+        EXPECT_EQ(out.connectSec, 1) << ms;
+        EXPECT_EQ(out.readSec, 1) << ms;
+    }
+}
+
+TEST(EmbedBudget, AboveTheFloorTheAskIsActuallyHonoured) {
+    // The other half of the story: once the ask clears a second it does
+    // something, or the two tests above would be describing a dead option.
+    svapi::functions::EmbedBudgetRequest in;
+    in.remainingBudgetMs = 60000;
+    in.callTimeoutMs = 4000;  // what the reporting consumer actually runs
+    const auto out = svapi::functions::resolveEmbedBudget(in);
+    EXPECT_LE(out.connectSec + out.readSec, 4);
+    EXPECT_GT(out.readSec, 1) << "a 4 s ask must buy more read time than a 1 ms one";
+    EXPECT_TRUE(out.boundByCall);
+}
+
 } // namespace