Spaces:
Paused
Zawieszony strumien REDUCE mogl zjesc caly budzet zadania
Browse filesZnalezione przez przypadek, na wlasnym przebiegu kontrolnym: proces stal
szesnascie minut na zerowym procesorze, z kompletem siedmiu wpisow w cache,
czyli MAP dawno skonczyl, a REDUCE siedzial na streamie, ktory sie otworzyl i
zamilkl. Powtorzony przebieg zajal 22 sekundy, wiec to byla chwilowa awaria
dostawcy - dokladnie ta, ktorej nikt nie pilnowal.
LLM_TIMEOUT pokrywa dotarcie do dostawcy, nie strumien, ktory juz plynie, a
iterowanie zamilklego strumienia blokuje bez wyjscia. Strumien jest teraz
konsumowany przez kolejke z wątku demona: cisza dluzsza niz 45 sekund albo
caly strumien dluzszy niz 300 sekund konczy odbior i oddaje to, co splynelo,
z dopiskiem, ze Notatka urywa sie w tym miejscu. Oba progi sa pod
osmiominutowym limitem strony, wiec to LUNA decyduje o zakonczeniu, a nie
worker ucinajacy ja w polowie zdania.
Trzy testy: zamilkniecie przerywane w niecale 8 sekund zamiast czekania 30,
normalne zakonczenie nietkniete, blad w polowie strumienia dalej zachowuje
to, co juz przyszlo.
Testy: 67 -> 70.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
- config.py +7 -0
- synthesis.py +51 -7
- test_luna.py +90 -0
|
@@ -35,6 +35,13 @@ MAP_MAX_TOKENS_RETRY = int(os.getenv("LUNA_MAP_MAX_TOKENS_RETRY", "3200"))
|
|
| 35 |
REDUCE_MAX_TOKENS = 4000 # the final Note can be long
|
| 36 |
MAP_TEMPERATURE = 0.0
|
| 37 |
REDUCE_TEMPERATURE = 0.15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
LLM_TIMEOUT = 180
|
| 39 |
|
| 40 |
VLM_FRAME_MAX_PX = 1024 # downscale an image's long side before sending (caps tokens)
|
|
|
|
| 35 |
REDUCE_MAX_TOKENS = 4000 # the final Note can be long
|
| 36 |
MAP_TEMPERATURE = 0.0
|
| 37 |
REDUCE_TEMPERATURE = 0.15
|
| 38 |
+
# Guards on the streaming reply. LLM_TIMEOUT covers reaching the provider, not a
|
| 39 |
+
# stream that opens and then goes quiet: one such stall sat for sixteen minutes in
|
| 40 |
+
# testing, which on the website would have burned the whole eight-minute budget and
|
| 41 |
+
# delivered nothing. STALL is the longest silence allowed between two pieces of text,
|
| 42 |
+
# MAX the longest the whole reply may take. Both stay under the website's own cap.
|
| 43 |
+
REDUCE_STALL_SECONDS = int(os.getenv("LUNA_REDUCE_STALL", "45"))
|
| 44 |
+
REDUCE_MAX_SECONDS = int(os.getenv("LUNA_REDUCE_MAX_SECONDS", "300"))
|
| 45 |
LLM_TIMEOUT = 180
|
| 46 |
|
| 47 |
VLM_FRAME_MAX_PX = 1024 # downscale an image's long side before sending (caps tokens)
|
|
@@ -3,10 +3,14 @@
|
|
| 3 |
# (cheap model for text; VLM for images). Strict-JSON output.
|
| 4 |
# REDUCE - fuse all kept notes into one structured Note (strong model, streamed).
|
| 5 |
import json
|
|
|
|
| 6 |
import re
|
|
|
|
|
|
|
| 7 |
import unicodedata
|
| 8 |
|
| 9 |
from config import (
|
|
|
|
| 10 |
PER_FILE_CHARS,
|
| 11 |
DEBUG, MAP_MAX_TOKENS, MAP_MODEL, MAP_TEMPERATURE, REDUCE_BUDGET,
|
| 12 |
MAP_MAX_TOKENS_RETRY, REDUCE_MAX_TOKENS, REDUCE_MODEL, REDUCE_TEMPERATURE, VLM_MODEL,
|
|
@@ -650,13 +654,53 @@ def reduce_stream(prompt: str, kept: list[dict], skipped: list[dict], fmt: str =
|
|
| 650 |
temperature=REDUCE_TEMPERATURE,
|
| 651 |
stream=True,
|
| 652 |
)
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
if not acc.strip():
|
| 661 |
raise RuntimeError("empty REDUCE response")
|
| 662 |
fixed = _fix_filenames(acc, [k["name"] for k in kept] + [x["name"] for x in skipped])
|
|
|
|
| 3 |
# (cheap model for text; VLM for images). Strict-JSON output.
|
| 4 |
# REDUCE - fuse all kept notes into one structured Note (strong model, streamed).
|
| 5 |
import json
|
| 6 |
+
import queue
|
| 7 |
import re
|
| 8 |
+
import threading
|
| 9 |
+
import time
|
| 10 |
import unicodedata
|
| 11 |
|
| 12 |
from config import (
|
| 13 |
+
REDUCE_MAX_SECONDS, REDUCE_STALL_SECONDS,
|
| 14 |
PER_FILE_CHARS,
|
| 15 |
DEBUG, MAP_MAX_TOKENS, MAP_MODEL, MAP_TEMPERATURE, REDUCE_BUDGET,
|
| 16 |
MAP_MAX_TOKENS_RETRY, REDUCE_MAX_TOKENS, REDUCE_MODEL, REDUCE_TEMPERATURE, VLM_MODEL,
|
|
|
|
| 654 |
temperature=REDUCE_TEMPERATURE,
|
| 655 |
stream=True,
|
| 656 |
)
|
| 657 |
+
# Consumed through a queue rather than iterated directly, because iterating a
|
| 658 |
+
# stalled stream blocks with no way out: a provider that opens the connection
|
| 659 |
+
# and then goes quiet held one run for sixteen minutes here, and the website
|
| 660 |
+
# would have spent its entire eight-minute budget waiting and shipped nothing.
|
| 661 |
+
# The reader is a daemon, so if it never returns it dies with the process.
|
| 662 |
+
started = time.monotonic()
|
| 663 |
+
box: "queue.Queue" = queue.Queue()
|
| 664 |
+
|
| 665 |
+
def _pump():
|
| 666 |
+
try:
|
| 667 |
+
for ev in stream:
|
| 668 |
+
if not ev.choices:
|
| 669 |
+
continue
|
| 670 |
+
piece = ev.choices[0].delta.content or ""
|
| 671 |
+
if piece:
|
| 672 |
+
box.put(piece)
|
| 673 |
+
except Exception as err:
|
| 674 |
+
box.put(err)
|
| 675 |
+
finally:
|
| 676 |
+
box.put(None)
|
| 677 |
+
|
| 678 |
+
threading.Thread(target=_pump, name="reduce-stream", daemon=True).start()
|
| 679 |
+
stalled = False
|
| 680 |
+
while True:
|
| 681 |
+
try:
|
| 682 |
+
item = box.get(timeout=REDUCE_STALL_SECONDS)
|
| 683 |
+
except queue.Empty:
|
| 684 |
+
print(f"[LUNA] REDUCE stalled: nothing for {REDUCE_STALL_SECONDS}s, "
|
| 685 |
+
f"keeping {len(acc)} chars")
|
| 686 |
+
stalled = True
|
| 687 |
+
break
|
| 688 |
+
if item is None:
|
| 689 |
+
break
|
| 690 |
+
if isinstance(item, Exception):
|
| 691 |
+
raise item
|
| 692 |
+
acc += item
|
| 693 |
+
yield acc
|
| 694 |
+
if time.monotonic() - started > REDUCE_MAX_SECONDS:
|
| 695 |
+
print(f"[LUNA] REDUCE over {REDUCE_MAX_SECONDS}s, keeping {len(acc)} chars")
|
| 696 |
+
stalled = True
|
| 697 |
+
break
|
| 698 |
+
if stalled and acc.strip():
|
| 699 |
+
acc = _fix_filenames(acc.rstrip(),
|
| 700 |
+
[k["name"] for k in kept] + [x["name"] for x in skipped])
|
| 701 |
+
yield acc + ("\n\n_(The Note stops here: the model stopped sending. "
|
| 702 |
+
"Everything above it is complete.)_")
|
| 703 |
+
return
|
| 704 |
if not acc.strip():
|
| 705 |
raise RuntimeError("empty REDUCE response")
|
| 706 |
fixed = _fix_filenames(acc, [k["name"] for k in kept] + [x["name"] for x in skipped])
|
|
@@ -7,6 +7,7 @@ uploads through. Run with: python -m pytest test_luna.py -q
|
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
import tempfile
|
|
|
|
| 10 |
|
| 11 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
|
|
@@ -706,3 +707,92 @@ def test_a_normal_file_is_passed_through_untouched():
|
|
| 706 |
from synthesis import _map_user
|
| 707 |
out = _map_user("p", "a.txt", "text", "krotka tresc pliku")
|
| 708 |
assert "krotka tresc pliku" in out and "longer than" not in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import os
|
| 8 |
import sys
|
| 9 |
import tempfile
|
| 10 |
+
import threading
|
| 11 |
|
| 12 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
|
|
|
|
| 707 |
from synthesis import _map_user
|
| 708 |
out = _map_user("p", "a.txt", "text", "krotka tresc pliku")
|
| 709 |
assert "krotka tresc pliku" in out and "longer than" not in out
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
# --- a stream that opens and then goes quiet ------------------------------------
|
| 713 |
+
|
| 714 |
+
class _Delta:
|
| 715 |
+
def __init__(self, c): self.content = c
|
| 716 |
+
|
| 717 |
+
|
| 718 |
+
class _Choice:
|
| 719 |
+
def __init__(self, c): self.delta = _Delta(c)
|
| 720 |
+
|
| 721 |
+
|
| 722 |
+
class _Ev:
|
| 723 |
+
def __init__(self, c): self.choices = [_Choice(c)]
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
def _kept_files(n=1):
|
| 727 |
+
return [{"name": f"plik{i}.txt", "category": "text", "relevance": "high",
|
| 728 |
+
"topics": [], "notes": "tresc " * 20} for i in range(n)]
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def test_a_stalled_stream_is_cut_and_what_arrived_is_kept_files():
|
| 732 |
+
"""A provider that opens the connection and then goes quiet held a real run for
|
| 733 |
+
sixteen minutes; the website would have spent its whole budget and shipped
|
| 734 |
+
nothing."""
|
| 735 |
+
import time as _t
|
| 736 |
+
import synthesis
|
| 737 |
+
stop = threading.Event()
|
| 738 |
+
|
| 739 |
+
def fake_stream(**kw):
|
| 740 |
+
def gen():
|
| 741 |
+
yield _Ev("# Notatka\n\nPierwsza czesc tresci. ")
|
| 742 |
+
yield _Ev("Druga czesc tresci.")
|
| 743 |
+
stop.wait(30) # goes quiet; the guard must not wait this out
|
| 744 |
+
return gen()
|
| 745 |
+
|
| 746 |
+
old_chat, old_stall = synthesis.chat, synthesis.REDUCE_STALL_SECONDS
|
| 747 |
+
synthesis.chat = fake_stream
|
| 748 |
+
synthesis.REDUCE_STALL_SECONDS = 1
|
| 749 |
+
try:
|
| 750 |
+
t0 = _t.monotonic()
|
| 751 |
+
out = list(synthesis.reduce_stream("zrob notatke", _kept_files(), []))
|
| 752 |
+
took = _t.monotonic() - t0
|
| 753 |
+
finally:
|
| 754 |
+
synthesis.chat, synthesis.REDUCE_STALL_SECONDS = old_chat, old_stall
|
| 755 |
+
stop.set()
|
| 756 |
+
|
| 757 |
+
assert took < 8, f"the guard did not fire: took {took:.1f}s"
|
| 758 |
+
final = out[-1]
|
| 759 |
+
assert "Pierwsza czesc" in final and "Druga czesc" in final
|
| 760 |
+
assert "stops here" in final
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
def test_a_stream_that_finishes_normally_is_untouched():
|
| 764 |
+
import synthesis
|
| 765 |
+
|
| 766 |
+
def fake_stream(**kw):
|
| 767 |
+
def gen():
|
| 768 |
+
yield _Ev("# Notatka\n\n")
|
| 769 |
+
yield _Ev("Pelna tresc do samego konca.")
|
| 770 |
+
return gen()
|
| 771 |
+
|
| 772 |
+
old = synthesis.chat
|
| 773 |
+
synthesis.chat = fake_stream
|
| 774 |
+
try:
|
| 775 |
+
out = list(synthesis.reduce_stream("zrob notatke", _kept_files(), []))
|
| 776 |
+
finally:
|
| 777 |
+
synthesis.chat = old
|
| 778 |
+
assert out[-1].endswith("Pelna tresc do samego konca.")
|
| 779 |
+
assert "stops here" not in out[-1]
|
| 780 |
+
|
| 781 |
+
|
| 782 |
+
def test_an_error_mid_stream_keeps_what_arrived():
|
| 783 |
+
import synthesis
|
| 784 |
+
|
| 785 |
+
def fake_stream(**kw):
|
| 786 |
+
def gen():
|
| 787 |
+
yield _Ev("# Notatka\n\n" + "tresc ktora juz splynela " * 30)
|
| 788 |
+
raise RuntimeError("provider dropped")
|
| 789 |
+
return gen()
|
| 790 |
+
|
| 791 |
+
old = synthesis.chat
|
| 792 |
+
synthesis.chat = fake_stream
|
| 793 |
+
try:
|
| 794 |
+
out = list(synthesis.reduce_stream("zrob notatke", _kept_files(), []))
|
| 795 |
+
finally:
|
| 796 |
+
synthesis.chat = old
|
| 797 |
+
assert "tresc ktora juz splynela" in out[-1]
|
| 798 |
+
assert "cut short" in out[-1]
|