Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions docs/product/releases/v2.4.0.md

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions loopx/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from .control_plane.coordination.local_authority import (
LocalCoordinationAuthorityUnavailable,
)
from .public_safe_text import LOCAL_PATH_SURFACE_PATTERN
from .control_plane.work_items.governed_transition_proposal import (
STEWARD_TEAM_PLAN_PREVIEW_KIND,
)
Expand Down Expand Up @@ -54,6 +53,18 @@
_ABSOLUTE_LOCAL_PATH = re.compile(
r"(?<![A-Za-z0-9])(?:/(?:Users|home|private|tmp|var)/[^\s`'\"<>]+|[A-Za-z]:[\\/][^\s`'\"<>]+)"
)
# Display redaction only hides paths that carry a descendant segment, so inline
# code examples such as ``C:\`` or ``/Users/`` stay readable while the public-safe
# validator keeps using ``public_safe_text.LOCAL_PATH_SURFACE_PATTERN``.
_CHAT_LOCAL_PATH_SURFACE_PATTERN = re.compile(
r"(?<![:/A-Za-z0-9])(?:"
r"/(?:Users|home|Volumes|private|tmp|var|etc|opt|srv|mnt|root|data|workspace|workspaces)/"
r"[^\s`'\"<>]+|"
r"[A-Za-z]:[\\/][^\s`'\"<>]+|"
r"\\\\[A-Za-z0-9_.-]+\\[^\s`'\"<>]+"
r")",
re.IGNORECASE,
)


def _protected_path_replacements(
Expand Down Expand Up @@ -112,7 +123,7 @@ def _root_character_atoms(

def _local_path_pattern(replacements: list[tuple[str, str]]) -> re.Pattern[str]:
if not replacements:
return LOCAL_PATH_SURFACE_PATTERN
return _CHAT_LOCAL_PATH_SURFACE_PATTERN
separator = r"(?:[/\\]|%2[fF]|%5[cC])"

def component_pattern(component: str, *, is_windows: bool) -> str:
Expand Down Expand Up @@ -141,7 +152,7 @@ def component_pattern(component: str, *, is_windows: bool) -> str:
+ r"[^\s`'\"<>()\[\]{},。!?]*)?"
+ r"|(?<![:/A-Za-z0-9_.\\])/(?:[^\s`'\"<>()\[\]{},。!?]+)"
+ "|(?i:"
+ LOCAL_PATH_SURFACE_PATTERN.pattern
+ _CHAT_LOCAL_PATH_SURFACE_PATTERN.pattern
+ ")"
)

Expand Down Expand Up @@ -327,7 +338,7 @@ def replace_absolute_path(match: re.Match[str]) -> str:
# The broad absolute-path candidate lets a protected root be recognized
# after harmless dot-segment aliases. Preserve unrelated custom paths;
# the shared classifier still redacts its canonical machine-local set.
if LOCAL_PATH_SURFACE_PATTERN.fullmatch(candidate):
if _CHAT_LOCAL_PATH_SURFACE_PATTERN.fullmatch(candidate):
return f"[local-path]{suffix}"
return matched

Expand Down
5 changes: 3 additions & 2 deletions loopx/cli_commands/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ def project_preparation_effects(value: dict[str, Any]) -> None:
execution_preparer is None
and args.turn_command == "run-once"
and bool(getattr(args, "execute", False))
and (args.host != "generic-cli" or getattr(args, "validation_command_json", None))
and uses_effect_bound_start_identity(
args.host, getattr(args, "host_adapter_request_schema", None),
args.host,
getattr(args, "host_adapter_request_schema", None),
getattr(args, "validation_command_json", None),
)
):
from ..control_plane.turn_driver.codex_execution_preparation import (
Expand Down
10 changes: 8 additions & 2 deletions loopx/cli_commands/turn_cadence.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ class ManagedCadenceStart(NamedTuple):
confirm: Callable[[], None]


def uses_effect_bound_start_identity(host: str, adapter_request_schema: str | None) -> bool:
def uses_effect_bound_start_identity(
host: str,
adapter_request_schema: str | None,
validation_command_json: str | None = None,
) -> bool:
"""These Host routes can reserve the same start before a Turn plan exists."""
return host == "codex-cli" or (
host == "generic-cli" and adapter_request_schema == COMPLETE_TURN_HOST_REQUEST_SCHEMA
host == "generic-cli"
and adapter_request_schema == COMPLETE_TURN_HOST_REQUEST_SCHEMA
and bool(validation_command_json)
)


Expand Down
4 changes: 3 additions & 1 deletion loopx/cli_commands/turn_run_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,9 @@ def on_managed_start_admitted() -> None:
manual_reason=args.manual_interval_bypass_reason,
on_admitted=on_managed_start_admitted,
request_id=settlement_identity.effect_id if uses_effect_bound_start_identity(
args.host, getattr(args, "host_adapter_request_schema", None),
args.host,
getattr(args, "host_adapter_request_schema", None),
getattr(args, "validation_command_json", None),
) else None,
)

Expand Down
51 changes: 48 additions & 3 deletions loopx/extensions/lark/goal_topic_runtime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def __init__(
self._listener_snapshot_at = 0.0
self._listener_snapshot_generation = 0
self._workers: dict[str, ProfileWorker] = {}
self._retiring_workers: set[threading.Thread] = set()
self._message_consumers: dict[str, MessageConsumer] = {}
self._default_home = Path.home()
self._health: dict[str, Payload] = {}
self._closed = threading.Event()
self._delivery_wakeup = threading.Event()
Expand Down Expand Up @@ -497,6 +499,29 @@ def _consumer_lease_key(profile: str, config: Mapping[str, str]) -> str:
identity = config.get("bot_app_id") or profile
return str(config.get("consumer_ref") or hashlib.sha256(identity.encode("utf-8")).hexdigest()[:32])

def _consumer_lease_dir(self) -> Path:
"""Keep unpatched consumer lease paths scoped to construction HOME."""
home_func = getattr(Path.home, "__func__", None)
unpatched = (
home_func is not None
and home_func.__module__.startswith("pathlib")
and home_func.__name__ == "home"
)
home = self._default_home if unpatched else Path.home()
return home / ".loopx" / "lark-consumers"

@staticmethod
def _stop_consumer_process(consumer: MessageConsumer | None) -> None:
if consumer is None:
return
consumer_stop, process, _requested = consumer
consumer_stop.set()
if process.poll() is None:
try:
process.terminate()
except OSError:
pass

def _listener_configs(self, snapshot: PayloadView) -> tuple[dict[str, dict[str, str]], set[str]]:
from . import goal_topic_runtime as runtime

Expand Down Expand Up @@ -756,8 +781,9 @@ def start_consumer(args: list[str]) -> Popen[str]:
# A legacy/shared consumer holds both identities: wait for
# the old process and fence new aliases on other servers.
with ExitStack() as leases:
lease_dir = self._consumer_lease_dir()
acquired = [leases.enter_context(try_exclusive_file_lock(
Path.home() / ".loopx" / "lark-consumers" / key,
lease_dir / key,
operation="lark_event_consumer")) for key in sorted(digests)]
if any(lease is None for lease in acquired):
self._update_health(profile, status="standby", error_code="lark_event_consumer_owned_elsewhere")
Expand Down Expand Up @@ -853,7 +879,8 @@ def register_callback_stream(stream: TeamPlanReviewCallbackStream) -> None:
worker = self._workers.get(profile)
if worker is not None and worker[1] is current_thread:
self._workers.pop(profile, None)
if not self._closed.is_set():
self._retiring_workers.discard(current_thread)
if managed_worker and not self._closed.is_set():
try:
reconfigured = profile in runtime._active_profile_configs(
self.snapshot_provider()
Expand Down Expand Up @@ -938,7 +965,11 @@ def _refresh_profile_workers(self, *, resume_queues: bool = True) -> bool:
self._workers[profile] = (stop, thread, next_fingerprint)
continue
self._workers.pop(profile)
if thread.is_alive():
self._retiring_workers.add(thread)
stop.set()
if consumer is not None and consumer[0] is stop:
self._stop_consumer_process(consumer)
missing = set(desired) - set(self._workers)
for profile in sorted(missing):
stop = threading.Event()
Expand Down Expand Up @@ -993,15 +1024,29 @@ def close(self) -> None:
self._closed.set()
self._invalidate_listener_snapshot()
self._delivery_wakeup.set()
current_thread = threading.current_thread()
with self._lock:
if self._refresh_retry is not None:
self._refresh_retry.cancel()
self._refresh_retry = None
workers = list(self._workers.values())
self._workers.clear()
retiring = list(self._retiring_workers)
self._retiring_workers.clear()
consumers = list(self._message_consumers.values())
for stop, _thread, _fingerprint in workers:
stop.set()
for _stop, thread, _fingerprint in workers:
for consumer in consumers:
self._stop_consumer_process(consumer)
threads = [
thread
for thread in (
*(thread for _stop, thread, _fingerprint in workers),
*retiring,
)
if thread is not None and thread is not current_thread
]
for thread in threads:
thread.join(timeout=3)
self._delivery_worker.close(timeout=3)

Expand Down
6 changes: 3 additions & 3 deletions packages/dsh-loopx-plugin/src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export const RUN_IDENTITY_FIELDS = [
const IDENTITY_TOKEN = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,120}$/u
const SUMMARY_TOKEN = /^[A-Za-z0-9][A-Za-z0-9_./:-]{0,79}$/u
const LOCAL_PATH_SURFACES = [
/(?<![:/A-Za-z0-9])\/(?:Users|home|Volumes|private|tmp|var|etc|opt|srv|mnt|root|data|workspace|workspaces)\/[^\s`'"<>]+/iu,
/(?<![:/A-Za-z0-9])[A-Za-z]:[\\/][^\s`'"<>]+/iu,
/(?<![:/A-Za-z0-9])\/(?:Users|home|Volumes|private|tmp|var|etc|opt|srv|mnt|root|data|workspace|workspaces)\/(?:[^\s`'"<>]+)?/iu,
/(?<![:/A-Za-z0-9])[A-Za-z]:[\\/](?:[^\s`'"<>]+)?/iu,
/(?<![:/A-Za-z0-9])\\\\[A-Za-z0-9_.-]+\\[^\s`'"<>]+/iu,
/(?<![\w~])~[\\/][^\s`'"<>]+/iu,
/(?<![\w~])~[\\/](?:[^\s`'"<>]+)?/iu,
/(?<![\w:])path:[\\/][^\s`'"<>]+/iu,
/\bfile:\/+/iu,
] as const
Expand Down
2 changes: 2 additions & 0 deletions packages/dsh-loopx-plugin/tests/observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ describe('shadow observer envelopes', () => {
['password_label', 'pass' + 'word:abcdefghijkl'],
['secret_label', 'sec' + 'ret:abcdefghijkl'],
['windows_drive_outside_user_profile', drivePath],
['bare_unix_root_in_summary_token', 't_' + '/' + 'Users/'],
['bare_drive_root_in_summary_token', 'C' + ':/'],
]
for (const [label, value] of refused) {
const { appended, observer } = observerWithCapture()
Expand Down
56 changes: 34 additions & 22 deletions tests/extensions/test_lark_realtime_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,25 +676,30 @@ def fake_stream(*, profile: str, stop: threading.Event, **_kwargs: object) -> di
runtime_root=tmp_path / "runtime",
runtime_controller=object(),
)
direct_stop = threading.Event()
service._poll_profile("mew", direct_stop)
assert entered == ["mew"]

entered.clear()
other_stop = threading.Event()
fingerprint = ProfileWorkerFingerprint(
transport=ProfileTransport(
cli_bin="fake-lark",
app_id="",
consumer_lease_key="mew",
callback_chats=(),
other_binding_id="",
try:
direct_stop = threading.Event()
service._poll_profile("mew", direct_stop)
assert entered == ["mew"]
assert service.active_profiles() == []

entered.clear()
other_stop = threading.Event()
fingerprint = ProfileWorkerFingerprint(
transport=ProfileTransport(
cli_bin="fake-lark",
app_id="",
consumer_lease_key="mew",
callback_chats=(),
other_binding_id="",
)
)
)
with service._lock:
service._workers["mew"] = (threading.Event(), threading.current_thread(), fingerprint)
service._poll_profile("mew", other_stop)
assert entered == []
with service._lock:
service._workers["mew"] = (threading.Event(), threading.current_thread(), fingerprint)
service._poll_profile("mew", other_stop)
assert entered == []
assert service.active_profiles() == []
finally:
service.close()

worker_started = threading.Event()
seen_stop: list[threading.Event] = []
Expand All @@ -710,10 +715,17 @@ def managed_poller(_profile: str, stop: threading.Event) -> None:
runtime_controller=object(),
profile_poller=managed_poller,
)
managed_service.refresh()
assert worker_started.wait(5)
worker_stop = seen_stop[0]
managed_service.close()
try:
managed_service.refresh()
assert worker_started.wait(5)
worker_stop = seen_stop[0]
finally:
managed_service.close()
assert worker_stop.is_set()
with managed_service._lock:
assert managed_service._workers == {}
assert [
thread.name
for thread in threading.enumerate()
if thread.name.startswith("loopx-lark-") and thread.is_alive()
] == []
15 changes: 15 additions & 0 deletions tests/test_chat_path_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,18 @@ def test_acp_stdio_stream_hides_encoded_windows_unicode_case_alias(tmp_path):
for kind, payload in events
if kind == "answer.final"
] == ["[local-path]"]


def test_redact_local_paths_preserves_bare_root_examples():
samples = [
r"A path ending with a slash, `C:\`, is code.",
"A Unix prefix example, `/Users/`, is code.",
]
for sample in samples:
for protected in ((), ["/custom/project"]):
assert redact_local_paths(sample, protected_paths=protected) == sample
assert redact_response_markdown(sample, protected_paths=protected) == sample
stream = VisibleResponseStreamFilter(protected_paths=protected)
assert stream.feed(sample[:12]) + stream.feed(sample[12:]) + stream.finish() == sample
assert redact_local_paths(r"Real path C:\Users\alice\repo") == "Real path [local-path]"
assert redact_local_paths("Real path /Users/alice/repo") == "Real path [local-path]"
12 changes: 12 additions & 0 deletions tests/test_loopx_turn_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4950,3 +4950,15 @@ def test_claim_effect_remains_visible_when_codex_is_unavailable(tmp_path, monkey
code = cli_main([*prefix, "task-lease", "inspect", "--goal-id", "loopx-turn-fixture", "--todo-id", "todo_fixture0001"])
assert code == 0 and json.loads(output.getvalue())["active"] is True
assert result["effects"]["quota_spent"] is False


def test_uses_effect_bound_start_identity_requires_validator_for_generic_cli() -> None:
from loopx.cli_commands.turn_cadence import uses_effect_bound_start_identity
from loopx.control_plane.quota.turn_envelope import COMPLETE_TURN_HOST_REQUEST_SCHEMA

assert uses_effect_bound_start_identity("codex-cli", None) is True
assert uses_effect_bound_start_identity("generic-cli", COMPLETE_TURN_HOST_REQUEST_SCHEMA, None) is False
assert uses_effect_bound_start_identity("generic-cli", COMPLETE_TURN_HOST_REQUEST_SCHEMA, "") is False
assert uses_effect_bound_start_identity(
"generic-cli", COMPLETE_TURN_HOST_REQUEST_SCHEMA, '["python3", "-c", "pass"]',
) is True
3 changes: 3 additions & 0 deletions tests/test_registry_git_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def test_registry_git_probe_retires_the_process_tree_on_timeout(
fake_git.chmod(0o755)
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}")
monkeypatch.setenv("GIT_DIR", str(tmp_path / "fake.git"))
# Under heavy parallel test contention, spawning /bin/sh and writing the
# grandchild PID can exceed the default 1.5s probe deadline before line 3 runs.
monkeypatch.setattr(registry_module, "_GIT_PROBE_TIMEOUT_SECONDS", 4.0)

observed = registry_module._registry_git_probe(probe / "registry.json")
assert observed["probe_status"] == "git_unavailable"
Expand Down
Loading