gemcode 0.4.24__py3-none-any.whl → 0.4.26__py3-none-any.whl

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.
@@ -1,4 +1,10 @@
1
- """File-based HITL approval bridge for web chat (subprocess-safe)."""
1
+ """File-based HITL approval bridge for web chat (subprocess-safe).
2
+
3
+ Chat SSE runs in a ``gemcode.web.sse_adapter`` subprocess while
4
+ ``POST /api/chat/approve`` is handled by the parent ``gemcode serve`` process.
5
+ In-memory waiter sets therefore cannot detect “live” approvals across that
6
+ boundary — use ``*.waiting`` marker files on the shared approval dir instead.
7
+ """
2
8
 
3
9
  from __future__ import annotations
4
10
 
@@ -17,22 +23,64 @@ def _approval_dir() -> Path:
17
23
  return Path.home() / ".gemcode" / "web_approvals"
18
24
  _DEFAULT_TIMEOUT_S = float(os.environ.get("GEMCODE_WEB_HITL_TIMEOUT_S", "3600"))
19
25
  _POLL_INTERVAL_S = 0.25
20
- # approval_ids currently blocked in wait_for_web_approval (same process)
26
+ # Same-process hints only (parent server never shares these with sse_adapter).
21
27
  _active_waiters: set[str] = set()
22
- # emitted to the UI but wait_for_web_approval not started yet (preflight)
23
28
  _pending_approval_ids: set[str] = set()
24
29
 
25
30
 
31
+ def _safe_id(approval_id: str) -> str:
32
+ return "".join(c if c.isalnum() or c in "-_:" else "_" for c in approval_id)
33
+
34
+
35
+ def _approval_path(approval_id: str) -> Path:
36
+ return _approval_dir() / f"{_safe_id(approval_id)}.json"
37
+
38
+
39
+ def _waiting_path(approval_id: str) -> Path:
40
+ """Marker written by the SSE subprocess while an approval is expected/active."""
41
+ return _approval_dir() / f"{_safe_id(approval_id)}.waiting"
42
+
43
+
44
+ def _is_live_approval(approval_id: str) -> bool:
45
+ """True if a chat turn is still waiting for this approval (cross-process safe)."""
46
+ aid = (approval_id or "").strip()
47
+ if not aid:
48
+ return False
49
+ if aid in _active_waiters or aid in _pending_approval_ids:
50
+ return True
51
+ try:
52
+ path = _waiting_path(aid)
53
+ if not path.is_file():
54
+ return False
55
+ # Heartbeats refresh mtime; stale markers mean the SSE subprocess died.
56
+ age_s = time.time() - path.stat().st_mtime
57
+ return age_s < 90.0
58
+ except OSError:
59
+ return False
60
+
61
+
26
62
  def register_pending_approval(approval_id: str) -> None:
27
63
  """Mark an approval as expected so early UI clicks are not reported as late."""
28
64
  aid = (approval_id or "").strip()
29
- if aid:
30
- _pending_approval_ids.add(aid)
65
+ if not aid:
66
+ return
67
+ _pending_approval_ids.add(aid)
68
+ _touch_waiting(aid)
31
69
 
32
70
 
33
- def _approval_path(approval_id: str) -> Path:
34
- safe = "".join(c if c.isalnum() or c in "-_:" else "_" for c in approval_id)
35
- return _approval_dir() / f"{safe}.json"
71
+ def _touch_waiting(approval_id: str) -> None:
72
+ try:
73
+ _approval_dir().mkdir(parents=True, exist_ok=True)
74
+ _waiting_path(approval_id).write_text(f"{time.time():.3f}\n", encoding="utf-8")
75
+ except OSError:
76
+ pass
77
+
78
+
79
+ def _clear_waiting_marker(approval_id: str) -> None:
80
+ try:
81
+ _waiting_path(approval_id).unlink(missing_ok=True)
82
+ except OSError:
83
+ pass
36
84
 
37
85
 
38
86
  def _read_approval_file(path: Path) -> bool | None:
@@ -60,14 +108,16 @@ def resolve_web_approval(approval_id: str, *, confirmed: bool) -> dict[str, Any]
60
108
  return {"ok": False, "error": "approval_id is required"}
61
109
  _approval_dir().mkdir(parents=True, exist_ok=True)
62
110
  path = _approval_path(approval_id)
111
+ # Detect live wait BEFORE writing; marker is owned by the SSE subprocess.
112
+ live = _is_live_approval(approval_id)
63
113
  payload = {"confirmed": bool(confirmed), "resolved_ms": int(time.time() * 1000)}
64
114
  path.write_text(json.dumps(payload), encoding="utf-8")
65
- late = approval_id not in _active_waiters and approval_id not in _pending_approval_ids
66
115
  return {
67
116
  "ok": True,
68
117
  "approval_id": approval_id,
69
118
  "confirmed": bool(confirmed),
70
- "late": late,
119
+ # late = no waiter — UI may start a recovery turn. Never true while SSE waits.
120
+ "late": not live,
71
121
  }
72
122
 
73
123
 
@@ -81,14 +131,18 @@ async def wait_for_web_approval(
81
131
  """Block until the user approves/denies or timeout (deny on timeout)."""
82
132
  _approval_dir().mkdir(parents=True, exist_ok=True)
83
133
  path = _approval_path(approval_id)
134
+ register_pending_approval(approval_id)
84
135
 
85
136
  # User may have approved before we started waiting (preflight UI is immediate).
86
137
  existing = _read_approval_file(path)
87
138
  if existing is not None:
139
+ _pending_approval_ids.discard(approval_id)
140
+ _clear_waiting_marker(approval_id)
88
141
  return existing
89
142
 
90
143
  _pending_approval_ids.discard(approval_id)
91
144
  _active_waiters.add(approval_id)
145
+ _touch_waiting(approval_id)
92
146
  try:
93
147
  deadline = time.monotonic() + max(1.0, timeout_s)
94
148
  next_hb = time.monotonic()
@@ -98,17 +152,20 @@ async def wait_for_web_approval(
98
152
  if result is not None:
99
153
  return result
100
154
  now = time.monotonic()
101
- if heartbeat is not None and now >= next_hb:
102
- try:
103
- heartbeat()
104
- except Exception:
105
- pass
155
+ if now >= next_hb:
156
+ _touch_waiting(approval_id)
157
+ if heartbeat is not None:
158
+ try:
159
+ heartbeat()
160
+ except Exception:
161
+ pass
106
162
  next_hb = now + max(5.0, heartbeat_s)
107
163
  await asyncio.sleep(_POLL_INTERVAL_S)
108
164
  return False
109
165
  finally:
110
166
  _active_waiters.discard(approval_id)
111
167
  _pending_approval_ids.discard(approval_id)
168
+ _clear_waiting_marker(approval_id)
112
169
 
113
170
 
114
171
  def new_approval_id(session_id: str, fc_id: str | None) -> str:
@@ -489,17 +489,33 @@ def _inject_web_code_context(cfg: GemCodeConfig, prompt: str, req: dict[str, Any
489
489
  "or type @path — unless they mean the whole project (see below)."
490
490
  )
491
491
 
492
- lines.extend(
493
- [
494
- 'When the user says "this file", "the current one", or similar, use the active / @-referenced file above.',
495
- 'When they ask to analyze the whole codebase, project, repo, or "all files", start with `repo_map` and '
496
- '`list_directory` on `.` — do not ask which file first.',
497
- "**Web UI permissions** — shell and mutating tools pause for an **inline Yes/No card in the chat** "
498
- "(above the composer). Do **not** tell the user to open a dialog, popup, terminal, or separate Approve button — "
499
- "just wait; the UI card is the approval UI. After they tap Yes or No, continue automatically.",
500
- "When Auto-approve / Super mode is on, tools run without asking — do not mention approvals.",
501
- ]
492
+ # Permission mode for THIS turn (must match actual HITL / auto-approve behavior).
493
+ lines.append(
494
+ 'When the user says "this file", "the current one", or similar, use the active / @-referenced file above.'
502
495
  )
496
+ lines.append(
497
+ 'When they ask to analyze the whole codebase, project, repo, or "all files", start with `repo_map` and '
498
+ '`list_directory` on `.` — do not ask which file first.'
499
+ )
500
+ auto_on = bool(
501
+ getattr(cfg, "yes_to_all", False)
502
+ or getattr(cfg, "super_mode", False)
503
+ or not getattr(cfg, "_web_interactive_hitl", True)
504
+ )
505
+ if auto_on:
506
+ lines.append(
507
+ "**Web UI permissions — AUTO-APPROVE is ON for this turn.** "
508
+ "Shell and mutating tools run immediately with **no** Yes/No card. "
509
+ "Never mention Approve, Deny, approval prompts, dialogs, or waiting for the user — "
510
+ "just run tools and report what you did."
511
+ )
512
+ else:
513
+ lines.append(
514
+ "**Web UI permissions — interactive.** Shell and mutating tools pause for an "
515
+ "**inline Yes/No card in the chat** (also pinned above the composer). "
516
+ "Do **not** invent Approve/Deny dialog text or tell the user to open a popup — "
517
+ "wait silently; after they tap Yes or No the turn continues."
518
+ )
503
519
 
504
520
  if mode == "agents":
505
521
  lines.extend(
@@ -922,6 +938,7 @@ def _apply_web_permissions(cfg: GemCodeConfig, req: dict[str, Any]) -> None:
922
938
  }
923
939
  object.__setattr__(cfg, "_web_auto_approve", normalized)
924
940
 
941
+ # UI is source of truth for web chat — do not leave env GEMCODE_SUPER_MODE stuck on.
925
942
  super_mode = bool(perms.get("super_mode"))
926
943
  if super_mode:
927
944
  cfg.super_mode = True
@@ -931,6 +948,7 @@ def _apply_web_permissions(cfg: GemCodeConfig, req: dict[str, Any]) -> None:
931
948
  object.__setattr__(cfg, "_web_interactive_hitl", False)
932
949
  return
933
950
 
951
+ cfg.super_mode = False
934
952
  if all(normalized.values()):
935
953
  cfg.yes_to_all = True
936
954
  cfg.interactive_permission_ask = False
@@ -948,13 +966,33 @@ def _apply_web_permissions(cfg: GemCodeConfig, req: dict[str, Any]) -> None:
948
966
 
949
967
 
950
968
  def _configure_web_permissions(cfg: GemCodeConfig, req: dict[str, Any]) -> None:
951
- """Web chat defaults to interactive Approve/Deny unless everything is auto-approved."""
969
+ """Web chat HITL follows the UI request, not process-level GEMCODE_SUPER_MODE.
970
+
971
+ Hosted tenant images historically set ``GEMCODE_SUPER_MODE=1`` so mesh/jobs are
972
+ unattended. That must not silently skip Yes/No cards when the user has
973
+ Auto-approve off in the web UI. Mesh workers still use
974
+ ``GEMCODE_MESH_WORKER_UNATTENDED`` independently.
975
+ """
952
976
  object.__setattr__(cfg, "_gemcode_web_sse", True)
953
- _apply_web_permissions(cfg, req)
954
- if not isinstance(req.get("permissions"), dict):
977
+
978
+ perms = req.get("permissions")
979
+ has_perms = isinstance(perms, dict)
980
+
981
+ # Clear env-applied super mode before applying the UI payload.
982
+ if not (has_perms and bool(perms.get("super_mode"))):
983
+ cfg.super_mode = False
955
984
  cfg.yes_to_all = False
956
985
  cfg.interactive_permission_ask = True
957
986
  object.__setattr__(cfg, "_web_interactive_hitl", True)
987
+
988
+ if has_perms:
989
+ _apply_web_permissions(cfg, req)
990
+ else:
991
+ cfg.super_mode = False
992
+ cfg.yes_to_all = False
993
+ cfg.interactive_permission_ask = True
994
+ object.__setattr__(cfg, "_web_interactive_hitl", True)
995
+
958
996
  _ensure_web_hitl(cfg)
959
997
  if os.environ.get("GEMCODE_WEB_YES_TO_ALL", "").lower() in ("1", "true", "yes", "on"):
960
998
  cfg.yes_to_all = True
@@ -963,8 +1001,9 @@ def _configure_web_permissions(cfg: GemCodeConfig, req: dict[str, Any]) -> None:
963
1001
 
964
1002
 
965
1003
  def _ensure_web_hitl(cfg: GemCodeConfig) -> None:
966
- """Web chat always uses the UI approval bridge when tools are not fully auto-approved."""
1004
+ """Web chat uses the UI approval bridge when tools are not fully auto-approved."""
967
1005
  if getattr(cfg, "yes_to_all", False) or getattr(cfg, "super_mode", False):
1006
+ object.__setattr__(cfg, "_web_interactive_hitl", False)
968
1007
  return
969
1008
  cfg.interactive_permission_ask = True
970
1009
  object.__setattr__(cfg, "_web_interactive_hitl", True)
@@ -1248,6 +1287,10 @@ async def run_adapter(req: dict[str, Any]) -> None:
1248
1287
  "model": model,
1249
1288
  "project_root": str(cfg.project_root),
1250
1289
  "workspace_mode": workspace_mode,
1290
+ "hitl_interactive": bool(getattr(cfg, "_web_interactive_hitl", False)),
1291
+ "auto_approve": bool(
1292
+ getattr(cfg, "yes_to_all", False) or getattr(cfg, "super_mode", False)
1293
+ ),
1251
1294
  **({"skill": resolved.skill_name} if resolved.skill_name else {}),
1252
1295
  }
1253
1296
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gemcode
3
- Version: 0.4.24
3
+ Version: 0.4.26
4
4
  Summary: Local-first coding agent on Google Gemini + ADK
5
5
  Author: GemCode Contributors
6
6
  License: Apache License
@@ -129,7 +129,7 @@ gemcode/web/chat_skills.py,sha256=VMnzKzhPZ96T-Opopbe7zJ2EUBvxrRaRMjYw4WfctuQ,40
129
129
  gemcode/web/customize_api.py,sha256=Et1HY49lleZlCBywsBg9n61KFReqTB0wGQrDmhrNEf4,24559
130
130
  gemcode/web/files_api.py,sha256=sdlSd3gj3TF_qrH2xYy70CTWMXAIzqouWe6uDkMrroo,7724
131
131
  gemcode/web/fleet_api.py,sha256=4lR7lWqpMojaAL7_c6rjjA5hMFnnUgpI1T561RvM-3w,19276
132
- gemcode/web/hitl_bridge.py,sha256=13ZyBNmAOjwHN1oHkJOC_JhJF60kgkrg6_02DMm4FpM,3670
132
+ gemcode/web/hitl_bridge.py,sha256=08rzUmVDiY6eAsMXZ-oE-XLPK5gk-wAGIFxDY2QlobA,5465
133
133
  gemcode/web/preview_api.py,sha256=vz5ddD6JYAQ5h6DEa4w95FUKalw-ujvT-jusbVa-8Ak,5828
134
134
  gemcode/web/project_root.py,sha256=rMKsp2va2z1p9CI-NKzbdJq2hyKXtNzDSLyTk-tV8Ws,1936
135
135
  gemcode/web/runtime_api.py,sha256=BezPpbNnj85aXiL5r7aXGcA3FFubadY7lHtskaNPgMc,12935
@@ -137,16 +137,16 @@ gemcode/web/serve_bind.py,sha256=Q6odfJuoTw8hIn3Hq8jcLe6EvRB1_wj4klrOBjKdTiE,309
137
137
  gemcode/web/serve_state.py,sha256=wTHaZ4CGjZYOxRMlKmFUEMpFoBPnci20GLbMjxGdGG0,6265
138
138
  gemcode/web/server.py,sha256=MovOlagSzWt63AbU55lE-x28D-fQX6DsBxhzKFFU54A,27698
139
139
  gemcode/web/sessions_api.py,sha256=4qudnYLiqIbUixon1PsV8yfBf6tu6llWLc_ESTFK61g,2008
140
- gemcode/web/sse_adapter.py,sha256=AXxpdHJzU_C8JjYEO43jbsFj2npp73yt0uoibsPo_tI,42544
140
+ gemcode/web/sse_adapter.py,sha256=WI0ganr1Dqp7nLjdvN-WNtmMpYhjQIKTohBuIekLv-M,44019
141
141
  gemcode/web/terminal_api.py,sha256=2FWU0XQ6CVUFW_JJiVukFPdj_sgsYa_y7IQb9brxCZw,3329
142
142
  gemcode/web/terminal_repl.py,sha256=fQt895g0qcr6VBhXfv_5b_bsC5zHT5-MO0ysBdgi2Fg,3886
143
143
  gemcode/web/ui_chat_store.py,sha256=sThUOcWC-svx-e2pt88ZazPrWelN2JbqeDicU7s5iUg,2377
144
144
  gemcode/web/web_config_api.py,sha256=1kmk1vSu5z63loddTNBHVsisiYy4CfyVIF93TYCgEqs,2264
145
145
  gemcode/web/web_sse_compat.py,sha256=9A2s-GI7El7AotJqhO263FrLwppCXXkdydZ5EiOQbao,504
146
146
  gemcode/web/workspace_panel_api.py,sha256=uZS11ovlZtc1IJDnOA776Z6IndC0WovcBbmPMnZyIHs,14122
147
- gemcode-0.4.24.dist-info/licenses/LICENSE,sha256=TD4524qn-W8Z07GTDnag-9jJPFutFZNB0a1WbMHPC54,8388
148
- gemcode-0.4.24.dist-info/METADATA,sha256=l2X_Lh-MFGwLSXS0TU7ENdkVzTExZqTntyFjUf_Bo8I,27378
149
- gemcode-0.4.24.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
150
- gemcode-0.4.24.dist-info/entry_points.txt,sha256=cZdLTLDiHbks7OSUCuxCh66dCWeQdpLR8BozoqfEjV4,45
151
- gemcode-0.4.24.dist-info/top_level.txt,sha256=UYrjULLBY2bcgK6KI6flomJWmsbDXu7n0rvW2SWFrbo,8
152
- gemcode-0.4.24.dist-info/RECORD,,
147
+ gemcode-0.4.26.dist-info/licenses/LICENSE,sha256=TD4524qn-W8Z07GTDnag-9jJPFutFZNB0a1WbMHPC54,8388
148
+ gemcode-0.4.26.dist-info/METADATA,sha256=K16ysRvmdkDwT4s6ykdDg_gfQ5C3F1h7UVA9IqNGBqg,27378
149
+ gemcode-0.4.26.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
150
+ gemcode-0.4.26.dist-info/entry_points.txt,sha256=cZdLTLDiHbks7OSUCuxCh66dCWeQdpLR8BozoqfEjV4,45
151
+ gemcode-0.4.26.dist-info/top_level.txt,sha256=UYrjULLBY2bcgK6KI6flomJWmsbDXu7n0rvW2SWFrbo,8
152
+ gemcode-0.4.26.dist-info/RECORD,,