Repository navigation
Expand file tree
/
Copy pathvenv_worker.py
More file actions
1509 lines (1367 loc) · 64.3 KB
/
Copy pathvenv_worker.py
File metadata and controls
1509 lines (1367 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
"""Host-side venv worker: warm subprocess IPC and run_code_in_user_venv."""
from __future__ import annotations
import contextlib
import logging
import os
import queue
import subprocess
import threading
import time
import uuid
from dataclasses import dataclass, field
from typing import Any, Callable, IO, Iterator, NoReturn
from plugin.framework.config import get_config_str
from plugin.framework.thread_guard import background, on_main_thread
from plugin.framework.constants import (
EMBEDDINGS_HEARTBEAT_GRACE_S,
HEARTBEAT_ABSOLUTE_CAP_SEC,
WORKER_POOL_DEFAULT,
WORKER_POOL_EMBEDDINGS,
)
from plugin.framework.worker_pool import (
BackgroundHandle,
StderrTail,
run_in_background,
)
from plugin.framework.managed_subprocess import ManagedSubprocess, kill_process_tree
from plugin.scripting.config_limits import (
HOST_IPC_READ_GRACE_SEC,
VENV_IPC_WRITE_TIMEOUT_SEC,
WARM_WORKER_TIMEOUT_SEC,
configured_python_exec_timeout,
python_exec_timeout_default,
resolve_python_exec_timeout,
)
from plugin.scripting.ipc import (
_stop_requested,
DEFAULT_MAX_PAYLOAD_BYTES,
EXEC_STARTED,
IpcFrameError,
IpcPartialFrameTimeout,
pack_pickle_frame,
read_frame_payload,
unpack_pickle_frame,
write_packed_frame_with_timeout,
)
from plugin.scripting.payload_codec import _HOST_UNPACK_ERRORS, host_unpack_data
from plugin.scripting.sandbox import (
resolve_libreoffice_python,
resolve_venv_python,
scrub_subprocess_env,
wrap_command_for_sandbox,
)
log = logging.getLogger(__name__)
_TIMEOUT_AFTER = " timed out after "
# Non-UI callers wait this long for the pipe, then get WORKER_REENTRY.
# Matches the scripting timeout ceiling so a second background script can
# still run after a long one. The UI thread does not wait to acquire
# _io_lock. Killing a worker does not wait on the caller either: the
# process tree is reaped in the background. The next spawn joins that
# reap first. A waiter also leaves within _IO_LOCK_POLL_SEC if the
# holder enters a tool RPC.
_IO_LOCK_ACQUIRE_TIMEOUT_SEC = 600.0
_IO_LOCK_POLL_SEC = 0.05
_WORKER_REENTRY_MESSAGE = (
"This Python tool called back into the same worker and would deadlock the script pipe."
)
_WORKER_BUSY_MESSAGE = (
"Python worker is busy; waiting on its pipe would block this thread."
)
def _worker_error(code: str, message: str, *, details: dict[str, Any] | None = None) -> dict[str, Any]:
"""Host-constructed error dict. Child payloads still go through ``_normalize_response``."""
return {
"status": "error",
"code": code,
"message": message,
"details": details or {},
}
class _NonReplayableIpcWriteTimeout(RuntimeError):
"""A mid-turn host response timed out after side effects may have occurred."""
class _WorkerRetired(RuntimeError):
"""This manager was replaced by a new venv path and must not be restarted."""
class _StopRequested(Exception):
"""The user pressed Stop while the host waited on the worker pipe."""
class _NoTerminalFrame(Exception):
"""The request was written and the child died before a terminal frame.
Not an OSError/RuntimeError: those still retry a failed initial write.
This one must not, or the same request id runs again.
"""
_SHARED_WORKER_RESTART_HINT = " Shared Python process restarted (all workbooks)."
def _clear_host_state_after_worker_death() -> None:
"""IPC is desynced after a kill; drop add-in scalar cache so the next turn is cold."""
from plugin.calc.python.function import clear_python_addin_cache
clear_python_addin_cache()
def _worker_error_message(exc: BaseException) -> str:
"""Build a short user-facing worker error without subprocess command paths."""
if isinstance(exc, subprocess.TimeoutExpired):
return f"Python worker failed: timed out after {exc.timeout} seconds"
text = str(exc)
if text.startswith("Command ") and _TIMEOUT_AFTER in text:
return f"Python worker failed:{text[text.index(_TIMEOUT_AFTER):]}"
return f"Python worker failed: {text}"
def _maybe_dispatch_ppt_master_response(
response: dict[str, Any],
*,
stdin_write: Callable[[bytes], None],
on_worker_event: Callable[[dict[str, Any]], None] | None = None,
stop_checker: Callable[[], bool] | None = None,
cancellation_scope: Any | None = None,
) -> bool:
"""Handle ppt-master intermediate worker frames; no-op when ppt_master is not bundled."""
try:
from plugin.ppt_master.venv.host_rpc import dispatch_worker_response
except ImportError:
return False
return dispatch_worker_response(
response,
stdin_write=stdin_write,
on_worker_event=on_worker_event,
stop_checker=stop_checker,
cancellation_scope=cancellation_scope,
)
def host_script_session_id(request_session_id: Any, script_session_id: str | None) -> str | None:
"""Document id for host tool RPC.
An explicit pin (chat ``ctx.doc``) wins. Otherwise the worker namespace
id on the request is used (Run Python Script, ``=PY()``, PPT-Master).
The child does not choose this value, and a pin is not written into the
request, so Isolated mode still gets a fresh namespace.
"""
explicit = script_session_id.strip() if isinstance(script_session_id, str) else ""
if explicit:
return explicit
if isinstance(request_session_id, str):
raw = request_session_id.strip()
if raw:
return raw
return None
def _maybe_dispatch_intermediate_response(
response: dict[str, Any],
*,
stdin_write: Callable[[bytes], None],
allowed_tools: frozenset[str] | None = None,
caller: str = "script",
on_worker_event: Callable[[dict[str, Any]], None] | None = None,
stop_checker: Callable[[], bool] | None = None,
cancellation_scope: Any | None = None,
script_session_id: str | None = None,
) -> bool:
"""Handle tool_call (any build). ppt-master llm_request / worker_event only for that caller."""
from plugin.scripting.host_rpc import handle_tool_call_frame
# Tool RPC is the shared venv→LO path (Run Python Script, chat python, ppt-master).
# Handle it here so LibrePy / WriterAgent-without-ppt_master still round-trip.
if handle_tool_call_frame(
response,
stdin_write=stdin_write,
allowed_tools=allowed_tools,
caller=caller,
script_session_id=script_session_id,
stop_checker=stop_checker,
# This is the chat SendCancellation; ToolContext.send_cancellation is what registers the HTTP call.
send_cancellation=cancellation_scope,
):
return True
# Only the ppt-master worker may emit llm_request. Every other caller
# (including "script" and =PY()) used to fall through into that dispatcher,
# which runs llm_request with the host's API credentials. tool_call above
# stays open to every caller.
if caller != "ppt_master_venv":
return False
return _maybe_dispatch_ppt_master_response(
response,
stdin_write=stdin_write,
on_worker_event=on_worker_event,
stop_checker=stop_checker,
cancellation_scope=cancellation_scope,
)
_HARNESS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "venv", "worker_harness.py")
_instances: dict[str, PythonWorkerManager] = {}
_registry_lock = threading.Lock()
def _worker_registry_key(exe: str, pool: str) -> str:
return f"{pool}:{exe}"
def _reap_worker_process(proc: subprocess.Popen[Any] | None, stderr_drain: StderrTail | None) -> None:
"""Kill a detached child and join its stderr drain. Runs off the caller thread.
``_terminate_worker`` used to wait on this thread for the kill, a second
``wait``, and the stderr join (about 12s when the tree ignores the first
signal). ``PythonWorkerManager.get`` did that on the caller, which is the
UI thread when Settings changes the venv path. The caller has already
dropped ``_proc`` under ``_proc_lock``. This thread only reaps that
snapshot. The next spawn joins it first, so the new child does not
overlap the old process tree.
"""
if proc is not None:
try:
kill_process_tree(proc)
proc.wait(timeout=5)
except (subprocess.TimeoutExpired, ProcessLookupError, OSError):
try:
proc.kill()
proc.wait(timeout=5)
except (OSError, subprocess.TimeoutExpired):
pass
if stderr_drain is not None:
stderr_drain.join(timeout=2 if proc is not None else 1)
if stderr_drain.is_alive:
log.debug("Python worker stderr drain still exiting after process termination")
class _IoSessionUnavailable(Exception):
"""``_io_session`` could not start; ``args[0]`` is the error dict to return."""
@dataclass
class _TurnState:
"""Replay guards for one request; survives an exception out of the read loop."""
execution_started: bool = False
dispatched_intermediate: bool = False
@property
def may_have_run(self) -> bool:
return self.execution_started or self.dispatched_intermediate
@dataclass
class _SpawnJob:
done: threading.Event
start: Callable[[], None]
error: BaseException | None = None
@dataclass(frozen=True)
class _ExecRequest:
"""One host execute. ``wire()`` is the child pickle; host-only fields stay off it.
``request_id`` is minted once so a pre-start retry writes the same ``id``.
"""
code: str | None = None
data: Any = None
bindings: dict[str, Any] | None = None
timeout_sec: int | None = None
session_id: str | None = None
action: str | None = None
init_script: str | None = None
init_session_id: str | None = None
init_script_hash: str | None = None
allow_heartbeat: bool = False
heartbeat_grace_sec: int | None = None
on_heartbeat: Callable[[dict[str, Any]], None] | None = None
on_worker_event: Callable[[dict[str, Any]], None] | None = None
stop_checker: Callable[[], bool] | None = None
cancellation_scope: Any | None = None
python_tool_domain: str | None = None
caller: str = "script"
script_session_id: str | None = None
request_id: str = field(default_factory=lambda: str(uuid.uuid4()))
def wire(self) -> dict[str, Any]:
# Key insertion order is the pickle order. Do not sort, do not
# dataclasses.asdict (it would copy host fields and deepcopy ``data``).
request: dict[str, Any] = {"id": self.request_id}
if self.timeout_sec is not None:
request["timeout_sec"] = self.timeout_sec
if self.session_id:
request["session_id"] = self.session_id
if self.data is not None:
request["data"] = self.data
# allow_heartbeat belongs on every long job, not only the code branch.
# run_trusted_action (embeddings, folder index) never received it, so
# the child sent no heartbeat and the host killed a long job.
if self.allow_heartbeat:
request["allow_heartbeat"] = True
if self.action:
request["action"] = self.action
else:
request["code"] = self.code if self.code is not None else ""
if self.bindings:
request["bindings"] = self.bindings
if self.init_script:
request["init_script"] = self.init_script
if self.init_session_id:
request["init_session_id"] = self.init_session_id
if self.init_script_hash:
request["init_script_hash"] = self.init_script_hash
return request
class PythonWorkerManager:
"""One warm child process per (pool, Python executable path) pair."""
exe: str
env: dict[str, str]
_io_lock: threading.Lock
_primed: bool
_retired: bool
_proc_lock: threading.Lock
_reap_handles: list[BackgroundHandle]
_spawn_handle: BackgroundHandle | None
_spawn_queue: queue.Queue[_SpawnJob | None] | None
_spawn_ident: int | None
_spawn_ready: threading.Event | None
def __init__(self, exe: str, env: dict[str, str]) -> None:
self.exe = exe
self.env = dict(env)
self.env["WRITERAGENT_IS_WORKER"] = "1"
self._proc: subprocess.Popen[Any] | None = None
self._io_lock = threading.Lock()
# Same-thread owner -> WORKER_REENTRY; tool RPC in flight or UI thread while
# locked -> busy; UI never blocks (_acquire_io timeout=0). _io_owner and
# _serving_tool_call are plain GIL-atomic attributes read without _io_lock on
# purpose: the holder keeps it across host RPC. Readers load self._proc once
# into a local: _terminate_worker stores None without taking _io_lock.
self._io_owner: int | None = None
self._serving_tool_call: bool = False
self._primed = False
self._retired = False
self._proc_lock = threading.Lock()
self._stderr_drain: StderrTail | None = None
# In-flight background reaps. get() moves a replaced manager's handles
# onto the replacement so the next spawn waits for the old tree.
self._reap_handles = []
self._spawn_handle = None
self._spawn_queue = None
self._spawn_ident = None
self._spawn_ready = None
@contextlib.contextmanager
def _io_session(self) -> Iterator[None]:
"""Hold the IO lock with a warm worker; raise ``_IoSessionUnavailable`` otherwise."""
reentry = self._acquire_io()
if reentry is not None:
raise _IoSessionUnavailable(reentry)
try:
warm_err = self._ensure_warmed_unlocked()
if warm_err is not None:
raise _IoSessionUnavailable(warm_err)
yield
finally:
self._release_io()
def _run_in_session(self, body: Callable[[], dict[str, Any]]) -> dict[str, Any]:
"""Run *body* while this manager holds the pipe, or return the session error."""
try:
with self._io_session():
return body()
except _IoSessionUnavailable as e:
return e.args[0]
@classmethod
def get(cls, exe: str, env: dict[str, str], *, pool: str = WORKER_POOL_DEFAULT) -> PythonWorkerManager:
"""Return the singleton worker for *pool* + *exe* (caller should pass a scrubbed env dict)."""
key = _worker_registry_key(exe, pool)
prefix = f"{pool}:"
stale: list[PythonWorkerManager] = []
with _registry_lock:
# The registry key is pool:exe. A new Settings path used to leave
# the previous child running until LibreOffice exited.
for other_key in list(_instances):
if other_key.startswith(prefix) and other_key != key:
old = _instances.pop(other_key, None)
if old is not None:
stale.append(old)
mgr = _instances.get(key)
if mgr is None:
mgr = cls(exe, dict(env))
_instances[key] = mgr
else:
# The live process keeps the env it was spawned with. The next
# Popen (crash, or a path change) must see a later scrub, including
# WRITERAGENT_DEBUG_LOG_PATH once logging is up.
fresh = dict(env)
fresh["WRITERAGENT_IS_WORKER"] = "1"
mgr.env = fresh
for old in stale:
# In-flight execute holds _io_lock. Do not take it here: a tool
# call is waiting on the UI thread. The flag stops the retry
# from Popen-ing a child this registry no longer owns.
old._retired = True
inherited: list[BackgroundHandle] = []
for old in stale:
# Do not wait: Settings calls get() on the UI thread. The
# replacement joins these handles before its next Popen.
old._terminate_worker(wait=False)
inherited.extend(old._take_reap_handles())
old._stop_spawn_thread(wait=False)
if inherited:
mgr._inherit_reaps(inherited)
return mgr
@classmethod
def shutdown_all(cls) -> None:
"""Terminate all workers (tests / extension teardown)."""
# shutdown_all must not skip _retired: the retry spawned an untracked
# warm process. get() already sets the flag before terminate so an
# in-flight execute cannot Popen a child the registry dropped. Drop
# the lock before terminate: an in-flight tool call may need the UI
# thread, and terminate must not hold _registry_lock across that.
with _registry_lock:
managers = list(_instances.values())
for mgr in managers:
mgr._retired = True
_instances.clear()
for mgr in managers:
mgr._terminate_worker()
mgr._stop_spawn_thread(wait=True)
@classmethod
def pool_is_running(cls, pool: str = WORKER_POOL_DEFAULT) -> bool:
"""True when *pool* already has a live child. Does not spawn one."""
prefix = f"{pool}:"
with _registry_lock:
for key, mgr in _instances.items():
if key.startswith(prefix) and mgr._is_worker_alive():
return True
return False
def _is_worker_alive(self) -> bool:
# One load: _terminate_worker may store None concurrently.
proc = self._proc
return proc is not None and proc.poll() is None
def _ensure_warmed_unlocked(self) -> dict[str, Any] | None:
"""Spawn worker and prime auto-imports. Returns error dict or None."""
if self._primed and self._is_worker_alive():
return None
prime = self._execute_ipc_unlocked(
_ExecRequest("result = None", timeout_sec=WARM_WORKER_TIMEOUT_SEC)
)
if prime.get("status") != "ok":
return prime
self._primed = True
return None
def _reentry_error(self) -> dict[str, Any] | None:
"""Refuse a nested execute that would deadlock this worker's pipe."""
me = threading.get_ident()
# Any other thread used to call Lock.acquire() with no timeout. The
# holder can be inside a tool RPC that is waiting on the thread stuck
# in acquire (the UI pump, or whichever worker must run the host
# callback), so the wait never ends and the UI stays frozen. Refuse
# the owner, anyone while a tool call is in progress, and the UI
# thread while the pipe lock is held.
# _serving_tool_call is per manager. =PY() during a chat script is a
# different thread, not a callback into the same worker. The reentry
# sentence is only true when this thread already holds the pipe.
if self._io_owner == me:
return _worker_error("WORKER_REENTRY", _WORKER_REENTRY_MESSAGE)
if self._serving_tool_call:
return _worker_error("WORKER_REENTRY", _WORKER_BUSY_MESSAGE)
if on_main_thread() and self._io_lock.locked():
return _worker_error("WORKER_REENTRY", _WORKER_BUSY_MESSAGE)
return None
def _acquire_io(self) -> dict[str, Any] | None:
err = self._reentry_error()
if err is not None:
return err
# The locked() check above can pass, then another thread takes the
# pipe and blocks on the UI thread. A non-blocking acquire closes
# that race. Other threads wait, but leave if a tool RPC starts.
if on_main_thread():
if not self._io_lock.acquire(timeout=0):
return _worker_error("WORKER_REENTRY", _WORKER_BUSY_MESSAGE)
self._io_owner = threading.get_ident()
return None
deadline = time.monotonic() + _IO_LOCK_ACQUIRE_TIMEOUT_SEC
while True:
if self._serving_tool_call:
return _worker_error("WORKER_REENTRY", _WORKER_BUSY_MESSAGE)
remaining = deadline - time.monotonic()
if remaining <= 0:
return _worker_error("WORKER_REENTRY", _WORKER_BUSY_MESSAGE)
if self._io_lock.acquire(timeout=min(_IO_LOCK_POLL_SEC, remaining)):
self._io_owner = threading.get_ident()
return None
def _release_io(self) -> None:
self._serving_tool_call = False
self._io_owner = None
self._io_lock.release()
def _ensure_warmed(self) -> dict[str, Any] | None:
try:
with self._io_session():
return None
except _IoSessionUnavailable as exc:
return exc.args[0]
def warm(self) -> None:
"""Spawn the worker and trigger auto-imports (numpy etc.) so the next real execute is instant."""
err = self._ensure_warmed()
if err is not None:
log.warning("Python worker warm failed: %s", err.get("message"))
def _fail_no_replay(self, code: str, message: str, *, details: dict[str, Any] | None = None) -> dict[str, Any]:
"""Kill the desynced worker and return an error without resending the request.
The reap runs in the background so this return does not sit in
``proc.wait``. The next ``_ensure_running`` joins it before spawn.
"""
self._terminate_worker(wait=False)
_clear_host_state_after_worker_death()
return _worker_error(code, message, details=details if details is not None else {"exe": self.exe})
def _fail_ipc_no_replay(self, exc: BaseException) -> dict[str, Any]:
"""Terminal IPC failure after the request may have run. Same text at each site."""
return self._fail_no_replay("WORKER_IPC_ERROR", f"Python worker failed: {exc}{_SHARED_WORKER_RESTART_HINT}")
def _read_until_terminal(
self,
stdout: IO[bytes],
stdin: IO[bytes],
req: _ExecRequest,
wire: dict[str, Any],
state: _TurnState,
*,
host_read_timeout_sec: float,
write_timeout_sec: float,
allowed_tools: frozenset[str] | None,
resolved_script_session_id: str | None,
) -> dict[str, Any]:
"""Serve intermediate frames until a terminal one; flags go into *state*.
*state* is the caller's object so its except handlers still see
exec_started / tool_call when this raises.
"""
def _stdin_write(blob: bytes) -> None:
try:
self._write_packed_frame_with_timeout(
stdin,
blob,
timeout_sec=write_timeout_sec,
label="host RPC response",
)
except subprocess.TimeoutExpired as exc:
# The worker requested host work before this write. Retrying the
# whole turn could duplicate UNO mutations already performed.
raise _NonReplayableIpcWriteTimeout(
f"host RPC response timed out after {write_timeout_sec:g} seconds"
) from exc
def _closed_without_response() -> NoReturn:
stderr_out = self._drain_stderr()
message = f"Worker closed stdout without a response{stderr_out}"
if state.may_have_run:
# Work may already have run. Resending this id would
# run it again. A close before exec_started is a
# failed start and falls through to the retry.
raise _NoTerminalFrame(message)
raise RuntimeError(message)
while True:
if req.allow_heartbeat:
grace = int(req.heartbeat_grace_sec if req.heartbeat_grace_sec is not None else EMBEDDINGS_HEARTBEAT_GRACE_S)
# Already unpickled. A second unpack of a multi-megabyte
# result frame was pure CPU on this path.
response = self._read_response_with_heartbeats(
stdout,
host_read_timeout_sec,
grace,
req.on_heartbeat,
stop_checker=req.stop_checker,
)
if response is None:
_closed_without_response()
else:
response_bytes = self._read_response_bytes(stdout, host_read_timeout_sec, stop_checker=req.stop_checker)
if not response_bytes:
_closed_without_response()
response = unpack_pickle_frame(response_bytes)
if not isinstance(response, dict):
raise RuntimeError("Worker response must be a dict")
if response.get("type") == EXEC_STARTED:
state.execution_started = True
if response.get("id") != wire.get("id"):
# Hand the mismatched frame back; the caller's id check
# refuses it without replaying the request.
return response
continue
# A tool_call (or ppt llm_request / worker_event) can mutate the
# document and then raise. The flag used to flip only after
# dispatch returned, so a RuntimeError with no exec_started left
# may_have_run false and the attempt loop sent the request again.
if response.get("type") in ("tool_call", "llm_request", "worker_event"):
state.dispatched_intermediate = True
self._serving_tool_call = True
try:
is_intermediate = _maybe_dispatch_intermediate_response(
response,
stdin_write=_stdin_write,
allowed_tools=allowed_tools,
caller=req.caller,
on_worker_event=req.on_worker_event,
stop_checker=req.stop_checker,
cancellation_scope=req.cancellation_scope,
script_session_id=resolved_script_session_id,
)
finally:
self._serving_tool_call = False
if is_intermediate:
state.dispatched_intermediate = True
continue
return response
def _execute_ipc_unlocked(self, req: _ExecRequest) -> dict[str, Any]:
wire = req.wire()
timeout_sec = req.timeout_sec
if timeout_sec is None:
raise RuntimeError("timeout_sec is required")
for attempt in range(2):
try:
self._ensure_running()
proc = self._proc
# shutdown_all can terminate without _io_lock; an assert here
# escaped execute() as AttributeError (or vanished under -O).
if proc is None or proc.stdin is None or proc.stdout is None:
raise RuntimeError("worker terminated concurrently")
stdin = proc.stdin
stdout = proc.stdout
write_timeout_sec = min(float(timeout_sec), float(VENV_IPC_WRITE_TIMEOUT_SEC))
# Resolve tools and session id before writing the request. These are
# host-only; if they fail after the write, the retry path would resend
# the same id to a new child (may_have_run is still false until the
# read starts).
from plugin.scripting.host_rpc import resolve_allowed_tools
allowed_tools = resolve_allowed_tools(req.python_tool_domain)
# Pin wins over the kernel session id. Chat passes doc:… and
# leaves request session_id unset. RPS / =PY() / PPT-Master
# omit the pin and keep the request id.
resolved_script_session_id = host_script_session_id(wire.get("session_id"), req.script_session_id)
# The host read timeout includes a grace buffer so the child's in-process
# signal/thread timeout fires first and returns a clean error frame without
# terminating the warm subprocess (preserving shared workbook sessions).
host_read_timeout_sec = float(timeout_sec) + float(HOST_IPC_READ_GRACE_SEC)
try:
self._write_frame_with_timeout(stdin, wire, timeout_sec=write_timeout_sec, label="request")
except IpcFrameError as e:
# An oversize request fails in pack_pickle_frame before any byte
# reaches the pipe. That must not hit the retry handler,
# which killed the warm worker twice and wiped every shared
# session.
log.warning("Python worker request not sent: %s", e)
return _worker_error(
"WORKER_IPC_ERROR",
f"Failed to serialize request: {e}",
details={"exe": self.exe},
)
# A tool_call frame can already have mutated the document. A later
# pipe error must not resend the original script (the write-timeout
# path below is the same rule). exec_started is the same rule for
# side effects that never sent a tool_call frame. A death before
# that marker has not run the request, so the outer loop may retry.
state = _TurnState()
try:
response = self._read_until_terminal(
stdout,
stdin,
req,
wire,
state,
host_read_timeout_sec=host_read_timeout_sec,
write_timeout_sec=write_timeout_sec,
allowed_tools=allowed_tools,
resolved_script_session_id=resolved_script_session_id,
)
except (subprocess.TimeoutExpired, IpcPartialFrameTimeout) as e:
# Hung user code / C-extension: do not kill and replay (doubles the
# wait and side effects). Windows used to raise IpcPartialFrameTimeout
# (OSError) after buffering any byte, which missed this branch while
# may_have_run was still false, so the outer handler resent the script.
# Both platforms: partial chunk -> IpcPartialFrameTimeout; empty buffer
# -> TimeoutExpired. Catch both here so neither replays.
log.warning("Python worker read timed out: %s", e)
return self._fail_no_replay(
"VENV_TIMEOUT",
_worker_error_message(e) + _SHARED_WORKER_RESTART_HINT,
details={"timeout_sec": timeout_sec, "exe": self.exe},
)
except ValueError as e:
# A bad pickle must not kill the child and resend the same request.
# Side effects that already ran (DuckDB writes, a trusted
# update) would run twice. Id mismatch already refuses that
# replay; unpickle does too.
log.warning("Python worker frame rejected (not replaying): %s", e)
return self._fail_ipc_no_replay(e)
except (OSError, RuntimeError) as e:
# After may_have_run, do not replay on RuntimeError (bad frame,
# closed pipe, or mid-turn host-response write timeout —
# _NonReplayableIpcWriteTimeout). Looking only at
# dispatched_intermediate used to re-raise into the attempt
# loop and run the script again. Before the turn starts, re-raise
# so the dedicated handler below can refuse the replay.
if not state.may_have_run:
raise
log.warning("Python worker failed after execution started (not replaying): %s", e)
return self._fail_ipc_no_replay(e)
req_id = wire.get("id")
# Empty-id error before exec: the harness rejected the pickle and is
# back at read(). Return its message and keep the worker. Treating this
# as an id mismatch used to kill a healthy child. After exec_started or
# a tool frame, any other id mismatch still kills.
if (
response.get("id") == ""
and response.get("status") == "error"
and not state.may_have_run
):
message = response.get("message")
text = message if isinstance(message, str) and message else "Invalid pickle request"
log.warning("Python worker rejected the request frame (worker kept): %s", text)
return _worker_error("WORKER_IPC_ERROR", text, details={"exe": self.exe})
if response.get("id") != req_id:
# exchange_tool_call already checks ids. A mismatched terminal
# frame used to be accepted, so one cell could receive another's result.
log.warning(
"Python worker response id %r does not match request %r",
response.get("id"),
req_id,
)
return self._fail_no_replay(
"WORKER_IPC_ERROR",
f"Python worker response id mismatch.{_SHARED_WORKER_RESTART_HINT}",
)
try:
# host_unpack_data runs after the script has finished. A bad
# envelope used to hit the outer handler and replay the
# request, so a tool call that already mutated the document
# ran twice. The catch is the unpack contract, not only
# ValueError: int(inf) on an int column is OverflowError,
# and a bad shape can be TypeError, AttributeError, or
# KeyError. Those miss the outer replay tuple and used to
# escape as an uncaught exception.
return self._normalize_response(response)
except _HOST_UNPACK_ERRORS as e:
log.warning("Python worker result rejected (not replaying): %s", e)
return self._fail_ipc_no_replay(e)
except _StopRequested:
# Stop while waiting on the pipe: the child may be mid-script, so
# it is killed (never replayed) and the caller sees CANCELLED.
log.info("Python worker stopped by user")
return self._fail_no_replay("CANCELLED", "Python worker stopped by user")
except (_NoTerminalFrame, _NonReplayableIpcWriteTimeout) as e:
log.warning("Python worker failed without replay: %s", e)
return self._fail_ipc_no_replay(e)
except _WorkerRetired as e:
# The registry already dropped this manager. The generic handler
# below would terminate it and clear the Calc add-in cache twice
# even though no worker died.
log.warning("Python worker retired: %s", e)
return _worker_error("WORKER_IPC_ERROR", str(e), details={"exe": self.exe})
except subprocess.TimeoutExpired as e:
# Initial stdin write only. _write_packed_frame_with_timeout already
# detached the child and started the reap. Terminating again
# made it unclear who kills the worker, and a second wait
# would block on that reap. Host read timeouts return above.
log.warning("Python worker failed (attempt %s): %s", attempt + 1, e)
if self._proc is not None:
self._terminate_worker(wait=False)
_clear_host_state_after_worker_death()
if attempt == 0:
continue
return _worker_error(
"VENV_TIMEOUT",
_worker_error_message(e),
details={"exe": self.exe, "attempt": attempt + 1},
)
except (BrokenPipeError, ValueError, RuntimeError, OSError) as e:
log.warning("Python worker failed (attempt %s): %s", attempt + 1, e)
# Do not join the reap on this thread. A retry calls
# _ensure_running, which joins before Popen. The last attempt
# returns the error while the reap continues.
self._terminate_worker(wait=False)
_clear_host_state_after_worker_death()
if attempt == 0:
continue
return _worker_error(
"WORKER_IPC_ERROR",
_worker_error_message(e),
details={"exe": self.exe, "attempt": attempt + 1},
)
raise AssertionError("worker attempt loop exited without a result")
def execute(
self,
code: str | None = None,
*,
data: Any = None,
bindings: dict[str, Any] | None = None,
timeout_sec: int | None = None,
session_id: str | None = None,
action: str | None = None,
init_script: str | None = None,
init_session_id: str | None = None,
init_script_hash: str | None = None,
allow_heartbeat: bool = False,
heartbeat_grace_sec: int | None = None,
on_heartbeat: Callable[[dict[str, Any]], None] | None = None,
python_tool_domain: str | None = None,
script_session_id: str | None = None,
stop_checker: Callable[[], bool] | None = None,
cancellation_scope: Any | None = None,
) -> dict[str, Any]:
"""Run *code* in the warm worker, or handle *action* (e.g. reset_session).
Without *session_id*, each execute uses a fresh namespace in the child. With
*session_id*, the child reuses one LocalPythonExecutor per id.
*script_session_id* is host-only. Tool RPC resolves the document from it
ahead of *session_id*. It is not sent to the child.
Cold start: spawn + auto-imports run first under :data:`WARM_WORKER_TIMEOUT_SEC`
and are not charged against *timeout_sec*.
"""
if timeout_sec is None:
timeout_sec = python_exec_timeout_default()
return self._run_in_session(
lambda: self._execute_ipc_unlocked(
_ExecRequest(
code,
data=data,
bindings=bindings,
timeout_sec=timeout_sec,
session_id=session_id,
action=action,
init_script=init_script,
init_session_id=init_session_id,
init_script_hash=init_script_hash,
allow_heartbeat=allow_heartbeat,
heartbeat_grace_sec=heartbeat_grace_sec,
on_heartbeat=on_heartbeat,
python_tool_domain=python_tool_domain,
script_session_id=script_session_id,
stop_checker=stop_checker,
cancellation_scope=cancellation_scope,
)
)
)
def execute_ppt_master_turn(
self,
payload: dict[str, Any],
*,
timeout_sec: int,
on_worker_event: Callable[[dict[str, Any]], None] | None = None,
stop_checker: Callable[[], bool] | None = None,
cancellation_scope: Any | None = None,
) -> dict[str, Any]:
"""Run one PPT-Master sidebar turn in the venv worker (LLM + scripts + host UNO RPC).
``cancellation_scope`` is host-side only. ``data`` is pickled to the child,
so the scope is passed beside it and attached when an llm_request frame
comes back, the same way as ``stop_checker``.
"""
try:
import plugin.ppt_master # noqa: F401 # pyright: ignore[reportUnusedImport]
except ImportError:
return _worker_error(
"WORKER_IPC_ERROR",
"PPT-Master is not available in this extension build.",
)
# The child reads session_id from payload (skill cache). The request
# field is host-only: tool frames resolve the frame document from it.
# ppt_master_turn does not use it as a Python namespace.
raw_session = payload.get("session_id")
session_id = raw_session.strip() if isinstance(raw_session, str) else ""
raw = self._run_in_session(
lambda: self._execute_ipc_unlocked(
_ExecRequest(
data=payload,
timeout_sec=timeout_sec,
action="ppt_master_turn",
session_id=session_id or None,
on_worker_event=on_worker_event,
stop_checker=stop_checker,
cancellation_scope=cancellation_scope,
caller="ppt_master_venv",
)
)
)
if raw.get("status") == "error":
return raw
inner = raw.get("result")
if isinstance(inner, dict):
return inner
return {"status": "ok", "result": str(inner) if inner is not None else ""}
def _write_frame_with_timeout(
self,
stdin: IO[bytes],
message: Any,
*,
timeout_sec: float,
label: str,
) -> None:
"""Pack, then bound the pipe write. ``IpcFrameError`` means no byte was written."""
frame = pack_pickle_frame(message, max_payload_bytes=DEFAULT_MAX_PAYLOAD_BYTES)
self._write_packed_frame_with_timeout(stdin, frame, timeout_sec=timeout_sec, label=label)
def _write_packed_frame_with_timeout(
self,
stdin: IO[bytes],
frame: bytes,
*,
timeout_sec: float,
label: str,
) -> None:
"""Write a packed frame; on timeout kill the child (a partial frame may be in the pipe).
A dead child raises ``BrokenPipeError`` from ``is_alive``; the caller handles it.
"""
try:
write_packed_frame_with_timeout(
stdin,
frame,
timeout_sec,
is_alive=self._is_worker_alive,
)
except subprocess.TimeoutExpired as exc:
log.warning("%s write timed out after %ss; terminating Python worker", label, timeout_sec)
self._terminate_worker(wait=False)
_clear_host_state_after_worker_death()
raise subprocess.TimeoutExpired(cmd=self.exe, timeout=timeout_sec) from exc
def _normalize_response(self, response: dict[str, Any]) -> dict[str, Any]:
if response.get("status") == "ok":
result = response.get("result")
if result is not None:
result = host_unpack_data(result, as_nested_list=True)
return {
"status": "ok",
"result": result,
"stdout": (response.get("stdout") or "").strip(),
"stderr": "",
}
msg = response.get("message") or response.get("error") or "Unknown worker error"
tb = response.get("traceback")
if tb and isinstance(tb, str):
msg = f"{msg}\n{tb.strip()}"
out = _worker_error(
response.get("code") or "VENV_EXEC_ERROR",
str(msg),
details=response.get("details") or {},
)
out["stdout"] = (response.get("stdout") or "").strip()
out["traceback"] = str(tb or "")
return out
def _ensure_spawn_thread(self) -> None: