lex-llm-vllm 0.4.6 → 0.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +34 -0
- data/Gemfile +7 -2
- data/RULES.md +97 -0
- data/lex-llm-vllm.gemspec +6 -3
- data/lib/legion/extensions/llm/vllm/actors/discovery.rb +23 -0
- data/lib/legion/extensions/llm/vllm/helpers/callable.rb +215 -0
- data/lib/legion/extensions/llm/vllm/provider.rb +80 -352
- data/lib/legion/extensions/llm/vllm/runners/discovery.rb +214 -0
- data/lib/legion/extensions/llm/vllm/runners/fleet_worker.rb +0 -2
- data/lib/legion/extensions/llm/vllm/translator.rb +199 -203
- data/lib/legion/extensions/llm/vllm/version.rb +1 -1
- data/lib/legion/extensions/llm/vllm.rb +17 -5
- metadata +7 -14
- data/lib/legion/extensions/llm/vllm/actors/discovery_refresh.rb +0 -46
- data/lib/legion/extensions/llm/vllm/callable.rb +0 -189
- data/lib/legion/extensions/llm/vllm/helpers/offering_builder.rb +0 -233
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/claim_activation.rb +0 -126
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/health_display.rb +0 -107
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/http.rb +0 -150
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/instance_lifecycle.rb +0 -105
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/offering_comparison.rb +0 -51
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/probing.rb +0 -104
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh/weight_publication.rb +0 -89
- data/lib/legion/extensions/llm/vllm/runners/discovery_refresh.rb +0 -77
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 30dbfef3f26068d2f15e577896bc3e2964d5ef9fbb319282677eb89e2e084cdb
|
|
4
|
+
data.tar.gz: 8ff7f24fa3596e787f07a5877344a954af4e1e3c607d5483db0b7f3adc4a8dbc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bb1987843cd6920607c409b0b29b1f7730ed9cf2fcd914abc1a84cd48c3f577c54a7bd442e75747c0d085de1f8299112559e0960b02f280e7db429af29f10976
|
|
7
|
+
data.tar.gz: d776249cffeaa2fd169ffa899cb005ce2ac84e22ab1725c8ed8fba183a4a677489bffb3006c31eaad42827a65cf9c1d1cd5cacfa9840d9b0c305687feff39389
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,39 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.8] - 2026-08-25
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
- **Canonical Thinking::Config transposition.** The vLLM translator now renders the full expanded thinking config to wire:
|
|
7
|
+
- `enabled: false` renders `enable_thinking: false` (forces a default-ON model off).
|
|
8
|
+
- `enabled: true` renders `enable_thinking: true` + `thinking_budget` derived from `Thinking::Config#resolved_budget` (effort ladder: none|low|medium|high|xhigh|max → budget).
|
|
9
|
+
- The `params.max_thinking_tokens` dual-home is removed — `resolved_budget` is the single derivation path.
|
|
10
|
+
- **Usage normalization at the provider boundary (O03a).** `canonical_usage` translates the OpenAI Chat wire usage dialect (`prompt_tokens`/`completion_tokens` + nested `*_details`) into canonical keys before `Usage.from_hash`, preventing wire spellings from folding into metadata with nil token counts.
|
|
11
|
+
|
|
12
|
+
### Removed
|
|
13
|
+
- `log_unsupported_params` (referenced deleted `params.max_thinking_tokens`).
|
|
14
|
+
|
|
15
|
+
## [0.4.7] - 2026-08-20
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- **lex-llm 0.8.0 conformance (SSOT v4 contract cut).** Migrate the provider, callable, and translator to the Canonical-only boundary:
|
|
19
|
+
- The provider render/parse boundaries render FROM canonical values and parse TO canonical types (`Canonical::Response` for sync, `Canonical::Chunk` for streaming); the `to_legacy_message`/`to_legacy_chunk` bridges and the legacy `Llm::Message`/`Llm::Chunk`/`Llm::ToolCall` reconstruction are deleted.
|
|
20
|
+
- `temperature` is no longer a render kwarg (05 O4): the funnel's `Canonical::Params` (or Hash) flows into the vLLM dialect translator, and the edge `schema` folds into `params.response_format` at the render boundary.
|
|
21
|
+
- The callable's `chat`/`stream_chat` adopt the 0.8.0 callable contract (positional canonical messages; temperature travels inside `params`).
|
|
22
|
+
- Streaming tool-call deltas emit the documented fragment shape (`{ id:, name:, arguments: <String fragment>, index: }`) instead of a full `Canonical::ToolCall` with a String `arguments` member (O03a: `arguments` is Hash-only, and the accumulator reads the fragment by symbol keys); the sync tool-call path parses through the ONE shared strict arguments parser (`Responses::ToolArguments.parse!`, 10 U2) — the rescue-to-`{}` tolerance is gone (04 L7: invalid JSON is a contract error).
|
|
23
|
+
- The offerings read path (`discover_offerings`) is served from the SSOT Registry snapshot by the base read path (07 C5); the legacy `offering_from_model` → `Routing::ModelOffering` production path and its filter cache are deleted — the per-gem writer (`Helpers::OfferingBuilder` + `Runners::DiscoveryRefresh`) is the sole publication path.
|
|
24
|
+
- Enforce the canonical dispatch boundary end to end: the fleet callable's chat, stream_chat, and count_tokens operations reject plain-Hash messages with a loud ArgumentError instead of silently re-canonicalizing them. The lenient hash re-canonicalization masked the 2026-08-19 hash-bypass defect for 25 failed openai dispatches; it is removed.
|
|
25
|
+
- Prompt-cache `cache_control` rides as a first-class `Canonical::Message` member; the vLLM OpenAI-compatible wire render drops it, so the wire format is unchanged.
|
|
26
|
+
- Raise the `lex-llm` dependency floor to 0.8.0.
|
|
27
|
+
- The local-tree `lex-llm` path dependency in the test group resolves against the 0.8.0 contract cut during development.
|
|
28
|
+
|
|
29
|
+
### Removed
|
|
30
|
+
- `LegacyCoordinatorAdapter` compatibility wiring from the discovery runner (`Inventory::ScopedRefresher` is deleted in lex-llm 0.8.0); the `Inventory::Publisher` is constructed without a compatibility adapter.
|
|
31
|
+
- The render-seam `build_canonical_messages` re-check — central enforcement (`Provider#enforce_canonical_messages!` in the `complete` funnel) is the one check point (08 F2).
|
|
32
|
+
|
|
33
|
+
### Added
|
|
34
|
+
- Conformance kit B1–B4 boundary groups run against the production `VllmCallable`: central canonical enforcement (08 F2), canonical outputs asserted by type (05 O5, 08 R2), operation preservation (PR #189 defect class), and no model re-derivation (PR #45 law).
|
|
35
|
+
- Cover the canonical boundary in the dispatch-boundary conformance block: the canonical `cache_control` member survives on the message, the wire never leaks it, and plain-Hash input raises at the callable boundary and the `complete` funnel.
|
|
36
|
+
|
|
3
37
|
## [0.4.6] - 2026-08-19
|
|
4
38
|
|
|
5
39
|
### Changed
|
data/Gemfile
CHANGED
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
source 'https://rubygems.org'
|
|
4
4
|
|
|
5
|
-
gem 'lex-llm'
|
|
6
|
-
|
|
7
5
|
gemspec
|
|
8
6
|
|
|
7
|
+
# Local-tree resolution: use the adjacent lex-llm checkout when present so the
|
|
8
|
+
# 0.8.0 conformance work resolves against the contract cut during development.
|
|
9
|
+
group :test do
|
|
10
|
+
lex_llm_path = File.expand_path('../lex-llm', __dir__)
|
|
11
|
+
gem 'lex-llm', path: lex_llm_path if Dir.exist?(lex_llm_path)
|
|
12
|
+
end
|
|
13
|
+
|
|
9
14
|
group :development do
|
|
10
15
|
gem 'bundler', '>= 2.0'
|
|
11
16
|
gem 'rake', '>= 13.0'
|
data/RULES.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
RULES.md — Legion LLM Architecture Law
|
|
2
|
+
These rules apply to every task, file, repository, agent, model, session, test, refactor, migration, incident, and release.
|
|
3
|
+
The requested task defines what may change. These rules define how the system ALWAYS works.
|
|
4
|
+
Every rule remains active 100% of the time. If requested work conflicts with these rules, stop and surface the conflict before changing code.
|
|
5
|
+
These are architecture laws. Scope, compatibility, urgency, convenience, tests, existing behavior, and model judgment do not change them.
|
|
6
|
+
1. Canonical is the only internal language.
|
|
7
|
+
Every client translates client wire -> Canonical before shared execution.
|
|
8
|
+
Shared execution carries Canonical through context, tools, routing, direct dispatch, fleet dispatch, and response handling.
|
|
9
|
+
Every provider translates Canonical -> provider wire at the provider boundary, then provider wire -> Canonical before returning to shared execution.
|
|
10
|
+
Every internal boundary validates the Canonical type it is defined to receive and raises immediately when that contract is violated.
|
|
11
|
+
Client Wire -> Client Translator -> Canonical -> Shared Execution -> Canonical -> Provider Translator -> Provider Wire.
|
|
12
|
+
2. Serialization preserves Canonical.
|
|
13
|
+
Transport may serialize Canonical state. The receiving transport boundary ALWAYS rehydrates the exact Canonical type before execution continues.
|
|
14
|
+
Fleet follows Canonical -> serialize -> wire -> deserialize -> rehydrate Canonical -> Canonical.
|
|
15
|
+
Serialization changes encoding only. Ownership, identity, model, operation, capability, selection, and meaning remain exactly the same.
|
|
16
|
+
After rehydration, shared execution continues only with Canonical objects.
|
|
17
|
+
3. Every authoritative fact has exactly one owner.
|
|
18
|
+
The owner creates the fact once. Every downstream layer carries, projects, serializes, rehydrates, verifies, or executes that exact fact.
|
|
19
|
+
A downstream layer receiving missing or contradictory authoritative state raises and returns the defect to the owning layer.
|
|
20
|
+
Authority ALWAYS moves forward by preservation.
|
|
21
|
+
Authority is created once and is never recreated downstream.
|
|
22
|
+
4. Requirements describe the request. Inventory describes reality. Router chooses. Dispatch executes.
|
|
23
|
+
Canonical request construction owns request semantics. RequestRequirements expresses operation, capabilities, modality, context, output, tools, and explicit pins.
|
|
24
|
+
Providers publish exact executable facts into Inventory. Inventory owns canonical instance, offering, lane, capability, context, quota, health, and published weight state.
|
|
25
|
+
Router.next_lane consumes Requirements plus one immutable Inventory snapshot and produces one authoritative Selection.
|
|
26
|
+
Dispatch executes that Selection exactly. Once Selection exists, routing is finished.
|
|
27
|
+
5. Inventory facts are immutable executable facts.
|
|
28
|
+
Providers publish exact instances and complete offering snapshots through the Inventory publication contract.
|
|
29
|
+
Identity, capability evidence, context evidence, quota domains, availability, and write-time weights are consumed from published Inventory state.
|
|
30
|
+
A changed fact becomes authoritative only through the owning publication or reconciliation path and a new Inventory snapshot.
|
|
31
|
+
Routing reads Inventory. Dispatch verifies and executes Inventory-backed Selection.
|
|
32
|
+
6. Identity, capabilities, weights, and context policy retain exact ownership.
|
|
33
|
+
Inventory::Identity owns instance, offering, and lane identity; canonical instance identity is provider family plus the operator/configured instance name; physical endpoint data remains secondary.
|
|
34
|
+
Providers publish capability evidence. Requirements state required capabilities. Candidate evaluation compares the two and determines capability eligibility.
|
|
35
|
+
The weight owner computes lane weight at publication time; Inventory stores it; ranking consumes that stored weight; a stored zero disables the lane.
|
|
36
|
+
Preferred-context binning orders eligible candidates into preference bands and preserves eligibility. Capability, health, binning, and weight ALWAYS retain distinct meanings.
|
|
37
|
+
7. Routing chooses exactly once.
|
|
38
|
+
Router.next_lane is the sole routing authority.
|
|
39
|
+
Candidate evaluation determines eligibility from Requirements and Inventory. Ranking orders eligible candidates from published routing facts.
|
|
40
|
+
Selection freezes the exact provider, instance, offering, lane, model, operation, and routing identity required for execution.
|
|
41
|
+
Every downstream component consumes the Selection it receives.
|
|
42
|
+
Selection is preserved, not reconstructed.
|
|
43
|
+
8. Exact execution stays exact through every boundary.
|
|
44
|
+
Direct dispatch executes the exact Selection-derived binding it receives.
|
|
45
|
+
Fleet dispatch serializes and signs that exact binding; fleet validation verifies it; fleet rehydration restores it; worker resolution verifies it against authoritative Inventory.
|
|
46
|
+
The selected provider, instance, offering, lane, model, and operation remain identical through projection, signing, transport, validation, rehydration, resolution, and callable invocation.
|
|
47
|
+
A mismatch raises before provider execution.
|
|
48
|
+
An exact execution request ALWAYS remains exact execution.
|
|
49
|
+
9. Health and errors preserve one authoritative meaning.
|
|
50
|
+
Inventory owns exact-instance availability. An authoritative instance-unavailable result removes that exact instance; readiness probing owns recovery; successful readiness republish re-admits it.
|
|
51
|
+
Overload, timeout, rate limit, model-not-ready, and transient provider failures remain request-local according to ProviderOutcome semantics.
|
|
52
|
+
The first layer that can authoritatively classify an error performs that classification once. Every downstream layer preserves it.
|
|
53
|
+
Programming errors remain programming errors. Contract violations remain contract violations. Routing exhaustion remains the defined typed Rejection.
|
|
54
|
+
10. Compatibility exists only at explicit edges.
|
|
55
|
+
Supported legacy clients and protocols are translated into the current Canonical and SSOT architecture at explicit compatibility boundaries.
|
|
56
|
+
Shared execution remains Canonical. Routing remains SSOT-driven. Exact execution remains exact.
|
|
57
|
+
Compatibility code adapts an external contract to the current internal architecture.
|
|
58
|
+
The current internal architecture ALWAYS has one representation, one routing authority, one identity system, and one execution truth.
|
|
59
|
+
11. Fix every defect at its owner.
|
|
60
|
+
Trace the incorrect value to the layer that owns it, then fix that owner.
|
|
61
|
+
Fix client wire in the client translator; Canonical shape in Canonical construction; Requirements in Requirements construction; provider facts in publication; identity in Inventory identity; weights in publication/reconciliation.
|
|
62
|
+
Fix eligibility in candidate evaluation; ordering in ranking; choice in Router.next_lane; execution preservation in dispatch; provider wire in the provider translator.
|
|
63
|
+
The layer where a defect becomes visible is evidence. The owning layer is where the correction belongs.
|
|
64
|
+
12. A discovered issue remains in its owning domain.
|
|
65
|
+
Complete the requested task inside its stated scope.
|
|
66
|
+
When investigation exposes a separate defect owned by another architectural domain, record and surface it as separate work unless the requested task is explicitly expanded.
|
|
67
|
+
Routing work consumes existing Canonical Requirements and Inventory facts. Canonical work changes Canonical contracts. Provider work changes publication or translation. Transport work changes transport.
|
|
68
|
+
Nearby code never changes ownership. “While we are here” never changes architecture.
|
|
69
|
+
13. N x N ALWAYS converges through Canonical.
|
|
70
|
+
Equivalent client semantics produce equivalent Canonical state before shared execution. Every provider consumes the same Canonical semantics for the same request.
|
|
71
|
+
When two paths disagree, capture the state at every involved boundary and locate the FIRST point where Canonical meaning diverges.
|
|
72
|
+
Fix that first divergent boundary, then run the exact failing path again.
|
|
73
|
+
Client behavior is proven at client-wire <-> Canonical. Provider behavior is proven at Canonical <-> provider-wire. Shared execution is proven with Canonical throughout.
|
|
74
|
+
14. Debug from captured authoritative state.
|
|
75
|
+
Capture the actual input at the failing boundary before reasoning from symptoms.
|
|
76
|
+
For translation or transport defects, capture Canonical immediately before and after every involved boundary.
|
|
77
|
+
For routing or dispatch defects, capture Requirements, relevant Inventory facts, Selection, execution binding, and ProviderOutcome.
|
|
78
|
+
Compare each captured value to the contract owned by that layer. Find the first divergence. Fix its owner. Re-run the exact path.
|
|
79
|
+
Then inspect sibling implementations for the same defect class.
|
|
80
|
+
15. Tests prove the real boundary and the invariant.
|
|
81
|
+
A boundary test exercises the real boundary it claims to protect.
|
|
82
|
+
Fleet tests exercise real serialization, deserialization, Canonical rehydration, signing, validation, exact resolution, and callable dispatch.
|
|
83
|
+
Provider tests exercise the real callable boundary and provider translator. Routing tests exercise real Requirements, Inventory records, candidate evaluation, ranking, and Selection.
|
|
84
|
+
Regression tests prove the violated invariant, not only the observed symptom.
|
|
85
|
+
A green suite is release evidence only when the tested path traverses the real architecture.
|
|
86
|
+
16. Shared contracts are consumed directly.
|
|
87
|
+
Shared Canonical types own execution representation. Shared Inventory types own inventory state. Shared Routing types own routing state.
|
|
88
|
+
Shared taxonomy owns canonical mappings. Shared ProviderOutcome owns provider-neutral outcomes. Shared fleet protocol owns exact execution claims.
|
|
89
|
+
Every repository consumes these shared owners directly.
|
|
90
|
+
A defect in one shared boundary triggers an audit of every sibling implementation of that boundary. Fix the shared owner centrally whenever the defect belongs to a shared contract.
|
|
91
|
+
17. Architecture is the release gate.
|
|
92
|
+
Every change preserves every rule in this file.
|
|
93
|
+
Tests, compatibility, historical behavior, migration phase, patch urgency, nearby code, task wording, and model judgment are evaluated UNDER these rules.
|
|
94
|
+
A contradiction between existing behavior and these rules is surfaced as an architecture conflict and resolved at the owning boundary before release.
|
|
95
|
+
Limited scope means do less. Limited scope NEVER means fewer rules apply.
|
|
96
|
+
These rules apply 100% of the time.
|
|
97
|
+
These are the law.
|
data/lex-llm-vllm.gemspec
CHANGED
|
@@ -27,7 +27,10 @@ Gem::Specification.new do |spec|
|
|
|
27
27
|
spec.add_dependency 'legion-logging', '>= 1.3.2'
|
|
28
28
|
spec.add_dependency 'legion-settings', '>= 1.4.2'
|
|
29
29
|
spec.add_dependency 'legion-transport', '>= 1.4.14'
|
|
30
|
-
# 0.
|
|
31
|
-
#
|
|
32
|
-
|
|
30
|
+
# 0.8.0 is the canonical contract cut: Canonical-only funnel (central
|
|
31
|
+
# enforcement, temperature in Params, positional-messages callable
|
|
32
|
+
# contract), the legacy Llm::Message/Chunk/ToolCall/ModelOffering/
|
|
33
|
+
# ScopedRefresher surface is deleted, and the per-gem writer publishes
|
|
34
|
+
# OfferingDrafts through the SSOT Registry.
|
|
35
|
+
spec.add_dependency 'lex-llm', '>= 0.8.0'
|
|
33
36
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/llm/discovery/actor'
|
|
4
|
+
|
|
5
|
+
# The base discovery actor only exists inside the daemon (it inherits the
|
|
6
|
+
# LegionIO time-based Every actor). In a standalone load, define nothing.
|
|
7
|
+
return unless defined?(Legion::Extensions::Llm::Discovery::Actor)
|
|
8
|
+
|
|
9
|
+
module Legion
|
|
10
|
+
module Extensions
|
|
11
|
+
module Llm
|
|
12
|
+
module Vllm
|
|
13
|
+
module Actor
|
|
14
|
+
# vLLM discovery actor: an EMPTY subclass of the shared base. The timer,
|
|
15
|
+
# the dispatch, and the runner-resolution convention are inherited —
|
|
16
|
+
# this class redefines nothing. The vLLM-specific work lives in
|
|
17
|
+
# Vllm::Runners::Discovery, resolved by the base from this namespace.
|
|
18
|
+
class Discovery < Legion::Extensions::Llm::Discovery::Actor; end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/llm/routing/provider_outcome'
|
|
4
|
+
require 'legion/extensions/llm/vllm/provider'
|
|
5
|
+
|
|
6
|
+
module Legion
|
|
7
|
+
module Extensions
|
|
8
|
+
module Llm
|
|
9
|
+
module Vllm
|
|
10
|
+
module Helpers
|
|
11
|
+
# Callable wrapper for a vLLM provider instance. Implements the
|
|
12
|
+
# `disconnect` and `normalize_dispatch_error(error:)` contracts required
|
|
13
|
+
# by Inventory::CallableHandle and Routing::ProviderOutcome, plus the
|
|
14
|
+
# fleet dispatch operations the coordinator invokes (chat, stream_chat,
|
|
15
|
+
# embed, count_tokens). Each dispatch delegates to a per-instance
|
|
16
|
+
# Vllm::Provider built lazily from the instance config; provider and
|
|
17
|
+
# Faraday errors are NOT rescued here so the coordinator's
|
|
18
|
+
# normalize_dispatch_error can classify them.
|
|
19
|
+
class Callable
|
|
20
|
+
# Keys the base Provider exposes as named kwargs for the completion
|
|
21
|
+
# operations. Anything else the fleet passes is folded into the
|
|
22
|
+
# payload `params` hash (temperature is a Canonical::Params member,
|
|
23
|
+
# never a named kwarg — 05 O4).
|
|
24
|
+
COMPLETION_NAMED_KEYS = %i[tools schema thinking tool_prefs headers].freeze
|
|
25
|
+
EMBED_NAMED_KEYS = %i[dimensions headers].freeze
|
|
26
|
+
|
|
27
|
+
def initialize(instance_cfg:, logger:)
|
|
28
|
+
@instance_cfg = instance_cfg
|
|
29
|
+
@logger = logger
|
|
30
|
+
@disconnected = false
|
|
31
|
+
@inference_calls = 0
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def call_count
|
|
35
|
+
@inference_calls
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def disconnected?
|
|
39
|
+
@disconnected
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def disconnect
|
|
43
|
+
@disconnected = true
|
|
44
|
+
@provider&.disconnect
|
|
45
|
+
@logger.debug { '[vllm][callable] disconnected' }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# ── Fleet dispatch operations ──────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
# 0.8.0 callable contract: chat/stream_chat take the canonical
|
|
51
|
+
# messages positionally (the fleet dispatch and the conformance kit
|
|
52
|
+
# both call `callable.chat(messages, model:, ...)`); count_tokens
|
|
53
|
+
# and embed keep their keyword forms.
|
|
54
|
+
def chat(messages, model:, **rest)
|
|
55
|
+
record_inference
|
|
56
|
+
# Canonical boundary (N x N law): pipeline dispatch delivers
|
|
57
|
+
# Canonical::Message objects only. Hash/legacy shapes are the
|
|
58
|
+
# bypass class — reject loudly, never coerce.
|
|
59
|
+
provider.enforce_canonical_messages!(messages)
|
|
60
|
+
named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
|
|
61
|
+
provider.chat(messages, model: model, params: canonical_params(params), **named)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def stream_chat(messages, model:, **rest, &)
|
|
65
|
+
record_inference
|
|
66
|
+
provider.enforce_canonical_messages!(messages)
|
|
67
|
+
named, params = split_fleet_kwargs(rest, COMPLETION_NAMED_KEYS)
|
|
68
|
+
provider.stream_chat(messages, model: model, params: canonical_params(params), **named, &)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def embed(text:, model:, **rest)
|
|
72
|
+
record_inference
|
|
73
|
+
named, params = split_fleet_kwargs(rest, EMBED_NAMED_KEYS)
|
|
74
|
+
provider.embed(text: text, model: model, params: params, **named)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def count_tokens(messages:, model:, **rest)
|
|
78
|
+
record_inference
|
|
79
|
+
provider.enforce_canonical_messages!(messages)
|
|
80
|
+
_named, params = split_fleet_kwargs(rest, [])
|
|
81
|
+
provider.count_tokens(messages: messages, model: model, params: params)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# V5: the reason is the bounded exception CLASS name (the base
|
|
85
|
+
# contract) — never the message, which embeds endpoint URLs and
|
|
86
|
+
# provider error bodies that must not reach shared state. The
|
|
87
|
+
# kind override stays: vLLM's explicit offline body phrases are
|
|
88
|
+
# stronger evidence than a raw status (the base law that raw 503
|
|
89
|
+
# alone never manufactures unavailability is preserved).
|
|
90
|
+
def normalize_dispatch_error(error:)
|
|
91
|
+
kind = classify_dispatch_error(error: error)
|
|
92
|
+
reason = error.class.name
|
|
93
|
+
reason = 'UnknownError' if reason.nil? || reason.empty?
|
|
94
|
+
Legion::Extensions::Llm::Routing::ProviderOutcome.new(kind: kind, reason: reason)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def record_inference
|
|
100
|
+
@inference_calls += 1
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def provider
|
|
104
|
+
@provider ||= Legion::Extensions::Llm::Vllm::Provider.new(@instance_cfg)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The 0.8.0 completion funnel receives canonical values only
|
|
108
|
+
# (08 F3): the folded wire params become a Canonical::Params at
|
|
109
|
+
# the dispatch boundary — temperature is a params member (05 O4),
|
|
110
|
+
# never a kwarg.
|
|
111
|
+
def canonical_params(params)
|
|
112
|
+
Legion::Extensions::Llm::Canonical::Params.from_hash(params)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Split the fleet's **rest into the base Provider's named kwargs and a
|
|
116
|
+
# payload params hash (any passed :params merged with unknown keys).
|
|
117
|
+
def split_fleet_kwargs(rest, named_keys)
|
|
118
|
+
named = rest.slice(*named_keys)
|
|
119
|
+
extra = rest.reject { |key, _| named.key?(key) }
|
|
120
|
+
params = (extra.delete(:params) || {}).to_h.merge(extra)
|
|
121
|
+
[named, params]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# D17: in production the base Connection's ErrorMiddleware raises
|
|
125
|
+
# Legion::Extensions::Llm::*Error (NOT raw Faraday classes), so this
|
|
126
|
+
# maps the full Llm error set (mirroring base Provider#
|
|
127
|
+
# normalize_dispatch_error) and layers the vLLM offline-body detection
|
|
128
|
+
# on top. Raw Faraday classes are also handled for the direct-HTTP
|
|
129
|
+
# paths that bypass the middleware.
|
|
130
|
+
def classify_dispatch_error(error:)
|
|
131
|
+
case error
|
|
132
|
+
when Legion::Extensions::Llm::OverloadedError then :overloaded
|
|
133
|
+
when Legion::Extensions::Llm::RateLimitError then :rate_limited
|
|
134
|
+
when Legion::Extensions::Llm::UnauthorizedError then :authentication
|
|
135
|
+
when Legion::Extensions::Llm::PaymentRequiredError then :billing
|
|
136
|
+
when Legion::Extensions::Llm::ForbiddenError then :authorization
|
|
137
|
+
when Legion::Extensions::Llm::ContextLengthExceededError then :context_rejected
|
|
138
|
+
when Legion::Extensions::Llm::BadRequestError then :invalid_request
|
|
139
|
+
when Legion::Extensions::Llm::ModelNotFoundError then :model_missing
|
|
140
|
+
when Legion::Extensions::Llm::ModelNotAllowedError then :policy
|
|
141
|
+
when Legion::Extensions::Llm::ServiceUnavailableError
|
|
142
|
+
if explicit_vllm_offline?(status: dispatch_error_status(error:),
|
|
143
|
+
body: dispatch_error_body(error:))
|
|
144
|
+
:instance_unavailable
|
|
145
|
+
else
|
|
146
|
+
:provider_error
|
|
147
|
+
end
|
|
148
|
+
when Legion::Extensions::Llm::ServerError then :provider_error
|
|
149
|
+
when Faraday::TimeoutError, Timeout::Error then :timeout
|
|
150
|
+
when Faraday::ConnectionFailed, Errno::ECONNREFUSED, Errno::ECONNRESET, SocketError
|
|
151
|
+
:connection_failure
|
|
152
|
+
when Faraday::ClientError then classify_client_error(error:)
|
|
153
|
+
when Faraday::ServerError then classify_server_error(error:)
|
|
154
|
+
else :provider_error
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def classify_client_error(error:)
|
|
159
|
+
case dispatch_error_status(error:)
|
|
160
|
+
when 401 then :authentication
|
|
161
|
+
when 403 then :authorization
|
|
162
|
+
when 404 then :model_missing
|
|
163
|
+
when 429 then :rate_limited
|
|
164
|
+
else :invalid_request
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def classify_server_error(error:)
|
|
169
|
+
status = dispatch_error_status(error:)
|
|
170
|
+
return :instance_unavailable if explicit_vllm_offline?(status: status, body: dispatch_error_body(error:))
|
|
171
|
+
|
|
172
|
+
case status
|
|
173
|
+
when 503, 529 then :overloaded
|
|
174
|
+
else :provider_error
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Read the HTTP status off a dispatch error whether its response is a
|
|
179
|
+
# plain Hash, a Faraday::Response, or a Faraday::Env. Real Faraday
|
|
180
|
+
# errors carry a Faraday::Env (a Struct, NOT a Hash), so a legacy
|
|
181
|
+
# `error.response.is_a?(Hash)` gate would never fire in production.
|
|
182
|
+
def dispatch_error_status(error:)
|
|
183
|
+
return error.response_status if error.respond_to?(:response_status) && !error.response_status.nil?
|
|
184
|
+
|
|
185
|
+
response = error.response if error.respond_to?(:response)
|
|
186
|
+
return response.status if response.respond_to?(:status) && !response.status.nil?
|
|
187
|
+
|
|
188
|
+
response[:status] if response.respond_to?(:[]) && !response[:status].nil?
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def dispatch_error_body(error:)
|
|
192
|
+
return error.response_body.to_s if error.respond_to?(:response_body) && !error.response_body.nil?
|
|
193
|
+
|
|
194
|
+
response = error.response if error.respond_to?(:response)
|
|
195
|
+
return response.body.to_s if response.respond_to?(:body) && !response.body.nil?
|
|
196
|
+
|
|
197
|
+
response[:body].to_s if response.respond_to?(:[]) && !response[:body].nil?
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# An explicit flat vLLM service-offline body (never just HTTP 503
|
|
201
|
+
# alone). connection_failure, timeout, overload, 429, and generic 5xx
|
|
202
|
+
# are request-local per §8 and must not map to instance_unavailable.
|
|
203
|
+
def explicit_vllm_offline?(status:, body:)
|
|
204
|
+
status == 503 && (
|
|
205
|
+
body.to_s.downcase.include?('instance not available') ||
|
|
206
|
+
body.to_s.downcase.include?('server is going offline') ||
|
|
207
|
+
body.to_s.downcase.include?('service unavailable, server offline')
|
|
208
|
+
)
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|