synth-code 0.0.1__py3-none-macosx_15_0_arm64.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.
Files changed (110) hide show
  1. synth_code/__init__.py +0 -0
  2. synth_code/account.py +551 -0
  3. synth_code/account_http.py +86 -0
  4. synth_code/account_refusal.py +80 -0
  5. synth_code/accounting.py +148 -0
  6. synth_code/activity_grouping.py +149 -0
  7. synth_code/agent_panels.py +127 -0
  8. synth_code/approvals.py +397 -0
  9. synth_code/blobs.py +31 -0
  10. synth_code/cli.py +734 -0
  11. synth_code/client.py +413 -0
  12. synth_code/composer_delivery.py +139 -0
  13. synth_code/conversation.py +949 -0
  14. synth_code/daemon.py +34 -0
  15. synth_code/edition.py +306 -0
  16. synth_code/enrollment.py +304 -0
  17. synth_code/file_edit_facts.py +10 -0
  18. synth_code/freeze.py +234 -0
  19. synth_code/grant.py +59 -0
  20. synth_code/history.py +140 -0
  21. synth_code/history_generated.py +219 -0
  22. synth_code/history_wire.py +166 -0
  23. synth_code/image_input.py +109 -0
  24. synth_code/image_preview.py +105 -0
  25. synth_code/legacy_history.py +161 -0
  26. synth_code/live_turns.py +171 -0
  27. synth_code/model_picker.py +311 -0
  28. synth_code/native/manifest.json +1 -0
  29. synth_code/native/synth-code-runtime +0 -0
  30. synth_code/native.py +95 -0
  31. synth_code/oauth.py +284 -0
  32. synth_code/operator.py +116 -0
  33. synth_code/pack_source.py +65 -0
  34. synth_code/panel_request.py +291 -0
  35. synth_code/panels.py +40 -0
  36. synth_code/plugin_host.py +459 -0
  37. synth_code/protocol.py +35 -0
  38. synth_code/provider.py +313 -0
  39. synth_code/provider_fault.py +173 -0
  40. synth_code/provider_frames.py +571 -0
  41. synth_code/recommendation.py +324 -0
  42. synth_code/report.py +72 -0
  43. synth_code/research_events.py +44 -0
  44. synth_code/research_trace.py +449 -0
  45. synth_code/rex_view.py +58 -0
  46. synth_code/runtime_refusal.py +108 -0
  47. synth_code/schema/budgets.json +17 -0
  48. synth_code/schema/history-v2.json +1002 -0
  49. synth_code/session.py +291 -0
  50. synth_code/shoal.py +564 -0
  51. synth_code/side_panel.py +309 -0
  52. synth_code/skills.py +269 -0
  53. synth_code/slash.py +425 -0
  54. synth_code/source_view.py +135 -0
  55. synth_code/starter.json +13 -0
  56. synth_code/stdio.py +75 -0
  57. synth_code/stream.py +142 -0
  58. synth_code/subscriptions.py +267 -0
  59. synth_code/synth_authority.py +362 -0
  60. synth_code/synth_extra.py +32 -0
  61. synth_code/terminal.py +817 -0
  62. synth_code/terminal_journal.py +284 -0
  63. synth_code/terminal_layout.py +643 -0
  64. synth_code/terminal_surfaces.py +470 -0
  65. synth_code/terminal_viewport.py +125 -0
  66. synth_code/tool_render.py +86 -0
  67. synth_code/transcript_frames.py +212 -0
  68. synth_code/transcript_placement.py +63 -0
  69. synth_code/transcript_rows.py +456 -0
  70. synth_code/transport.py +243 -0
  71. synth_code/v001_history.py +468 -0
  72. synth_code/view.py +854 -0
  73. synth_code/views/__init__.py +93 -0
  74. synth_code/views/charts.py +196 -0
  75. synth_code/views/curses_adapter.py +116 -0
  76. synth_code/views/engine.py +170 -0
  77. synth_code/views/model.py +226 -0
  78. synth_code/views/primitives.py +270 -0
  79. synth_code/views/tiling.py +124 -0
  80. synth_code-0.0.1.dist-info/METADATA +50 -0
  81. synth_code-0.0.1.dist-info/RECORD +110 -0
  82. synth_code-0.0.1.dist-info/WHEEL +5 -0
  83. synth_code-0.0.1.dist-info/entry_points.txt +6 -0
  84. synth_code-0.0.1.dist-info/licenses/temper/LICENSE-APACHE +190 -0
  85. synth_code-0.0.1.dist-info/licenses/temper/LICENSE-MIT +21 -0
  86. synth_code-0.0.1.dist-info/licenses/temper/SOURCE.json +433 -0
  87. synth_code-0.0.1.dist-info/top_level.txt +2 -0
  88. synth_code_plugins/_sdk/__init__.py +19 -0
  89. synth_code_plugins/_sdk/callback_wire.py +240 -0
  90. synth_code_plugins/_sdk/callbacks.py +317 -0
  91. synth_code_plugins/_sdk/compliance.py +51 -0
  92. synth_code_plugins/_sdk/contracts.py +236 -0
  93. synth_code_plugins/_sdk/discovery.py +75 -0
  94. synth_code_plugins/_sdk/job_binding.py +71 -0
  95. synth_code_plugins/_sdk/lifecycle.py +50 -0
  96. synth_code_plugins/_sdk/lifecycle_generated.py +15 -0
  97. synth_code_plugins/_sdk/manifest.py +269 -0
  98. synth_code_plugins/_sdk/pack.py +332 -0
  99. synth_code_plugins/_sdk/push.py +98 -0
  100. synth_code_plugins/_sdk/records.py +26 -0
  101. synth_code_plugins/_sdk/registry.py +491 -0
  102. synth_code_plugins/_sdk/schemas.py +210 -0
  103. synth_code_plugins/_sdk/sources.py +230 -0
  104. synth_code_plugins/_sdk/surfaces.py +54 -0
  105. synth_code_plugins/_sdk/tools.py +172 -0
  106. synth_code_plugins/_sdk/worker_memory.py +49 -0
  107. synth_code_plugins/core/SKILL.md +6 -0
  108. synth_code_plugins/core/__init__.py +21 -0
  109. synth_code_plugins/core/plugin.toml +44 -0
  110. synth_code_plugins/core/source.py +18 -0
synth_code/terminal.py ADDED
@@ -0,0 +1,817 @@
1
+ """Interactive conversation, history navigation and durable composer state."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import curses
6
+ import os
7
+ import sys
8
+ import time
9
+ from uuid import uuid4
10
+
11
+ from . import slash, subscriptions, terminal_layout
12
+ from .agent_panels import agent_panels
13
+ from .client import Client
14
+ from .composer_delivery import RETRY_STATUS, ComposerDelivery, DraftRevision
15
+ from .edition import EditionUnavailable, Feature, require_feature, side_agents_available
16
+ from .image_input import is_slash_command
17
+ from .live_turns import LiveTurns, submit, wake_after
18
+ from .model_picker import ModelPicker, ServiceTierPicker, refresh_picker
19
+ from .operator import operator, preview
20
+ from .panel_request import panel_requests, visible_monitor
21
+ from .session import starter
22
+ from .terminal_journal import JOURNAL_ERRORS, TerminalJournal
23
+ from .terminal_surfaces import TerminalExit, TerminalSurfaces
24
+ from .terminal_viewport import TerminalViewport
25
+ from .view import (
26
+ InputDecoder,
27
+ InputHistory,
28
+ TerminalInput,
29
+ apply_composer_edit,
30
+ decode_batch,
31
+ is_composer_text,
32
+ )
33
+
34
+ __all__ = ["terminal"]
35
+
36
+ def terminal(screen, client, saved, save, workspace=None, *, prompt=None, development=False):
37
+ inp = TerminalInput(sys.stdin.fileno())
38
+ try:
39
+ with panel_requests(client) as take_panel_request:
40
+ if (fd := take_panel_request.wake_fd) is not None:
41
+ inp.external_wake = (fd,)
42
+ return _terminal(
43
+ screen, client, saved, save, workspace, take_panel_request, inp, prompt,
44
+ terminal_layout.header_version(development=development),
45
+ )
46
+ finally:
47
+ inp.close()
48
+
49
+
50
+ def _terminal(screen, client, saved, save, workspace, take_panel_request, inp, prompt, version_label):
51
+ wake = inp.wake
52
+ with client.subscribe(wake=wake, with_history=True) as subscription:
53
+ return _run_terminal(
54
+ screen, client, saved, save, workspace, take_panel_request, inp, prompt,
55
+ version_label, subscription, wake,
56
+ )
57
+
58
+
59
+ def _run_terminal(
60
+ screen, client, saved, save, workspace, take_panel_request, inp, prompt,
61
+ version_label, subscription, wake,
62
+ ):
63
+ def submit_waking(*args, **kwargs):
64
+ return wake_after(submit(*args, **kwargs), wake)
65
+
66
+ deliveries = ComposerDelivery(saved.get("pending_inputs", []), submit_waking)
67
+ frame_cache: dict[str, object] = {}
68
+ force_redraw = True
69
+ input_history = InputHistory()
70
+ live = LiveTurns()
71
+ steering_operation = submitted_turn = None
72
+ commands = saved.get("commands", [])
73
+ overlay_command = pending_command = ""
74
+ trace = saved.get("trace", False)
75
+ styles = terminal_layout.palette()
76
+ decoder = InputDecoder()
77
+ panels = agent_panels(saved.get("seen_findings", {}), client.edition)
78
+ journal = TerminalJournal(client, input_history, live, panels, subscription)
79
+ surfaces = TerminalSurfaces(client, wake)
80
+ viewport = TerminalViewport(journal, commands, surfaces)
81
+ status = saved.get("view_warning", "")
82
+ offset = saved.get("offset", 0)
83
+ follow = saved.get("follow", True)
84
+ draft = saved.get("draft", "")
85
+ caret = min(len(draft), saved.get("caret", len(draft)))
86
+ selected = saved.get("event", 0)
87
+ detail_offset = saved.get("detail_offset", 0)
88
+ overlay = ""
89
+ operation = saved.get("operation") or uuid4().hex
90
+ # The undelivered input last put back into the draft; while the draft still holds its
91
+ # exact text, sending it reuses the input's id (`DraftRevision.RESEND`).
92
+ restored_input = None
93
+ pending = None
94
+ accounts = model_picker = permission_index = None
95
+ account_index = action_index = 0
96
+ completion_draft = None
97
+ completion_index = 0
98
+ dismissed = False
99
+ reviewed = None
100
+ paste_origin = None
101
+ catalog = None
102
+ capabilities = {}
103
+ if prompt is not None:
104
+ submitted_turn = uuid4().hex
105
+ try:
106
+ deliveries.enqueue(prompt, submitted_turn)
107
+ except (OSError, ValueError) as error:
108
+ draft, caret = prompt, len(prompt)
109
+ status = f"Pending draft · {error}"
110
+ selected, trace, follow = 0, False, True
111
+
112
+ def remember_command():
113
+ payload = {"summary": draft, "operation": operation}
114
+ commands.append({"seq": journal.cursor, "kind": "terminal.command", "payload": payload})
115
+
116
+ def store_view():
117
+ save(
118
+ {
119
+ "commands": commands,
120
+ "pending_inputs": deliveries.snapshot(),
121
+ "seen_findings": panels.seen,
122
+ "draft": draft,
123
+ "caret": caret,
124
+ "event": selected,
125
+ "trace": trace,
126
+ "offset": offset,
127
+ "follow": follow,
128
+ "detail_offset": detail_offset,
129
+ "operation": operation,
130
+ }
131
+ )
132
+
133
+ def finish_picker(result):
134
+ commands[-1]["payload"].update(result)
135
+ viewport.invalidate()
136
+ store_view()
137
+
138
+ while True:
139
+ changed, restored, error_status = deliveries.advance(client, draft)
140
+ if restored is not None:
141
+ draft, caret, operation = restored.text, len(restored.text), restored.identity
142
+ restored_input = restored
143
+ if error_status:
144
+ status = error_status
145
+ elif status == RETRY_STATUS:
146
+ status = ""
147
+ if changed:
148
+ store_view()
149
+ if pending is not None and pending.done():
150
+ try:
151
+ value = pending.result()
152
+ command_name = pending_command.split(maxsplit=1)[0] if pending_command else ""
153
+ if isinstance(value, dict):
154
+ if command_name == "/composition":
155
+ value = slash.composition_result(value)
156
+ elif command_name == "/study" and "effect" not in value:
157
+ value = slash.study_result(value)
158
+ if isinstance(value, str):
159
+ overlay_command = pending_command
160
+ overlay = value
161
+ detail_offset = 0
162
+ status = ""
163
+ elif (
164
+ isinstance(value, dict)
165
+ and value.get("digest")
166
+ and value.get("effect")
167
+ ):
168
+ overlay_command = pending_command
169
+ overlay = preview(client, value["effect"])
170
+ detail_offset = 0
171
+ status = f"Approval {value['effect']} {value['digest']}"
172
+ else:
173
+ status = "" if isinstance(value, dict) else str(value)
174
+ except Exception as error: # noqa: BLE001 -- keep the composer usable after a provider failure.
175
+ status = str(error)
176
+ pending = None
177
+ pending_command = ""
178
+ win_size = inp.window_size()
179
+ if win_size is not None and win_size != screen.getmaxyx():
180
+ curses.resize_term(win_size[0], win_size[1])
181
+ force_redraw = True
182
+ height, width = screen.getmaxyx()
183
+ if take_panel_request():
184
+ panels.open("side")
185
+ status, journal_status = journal.update(status, steering_operation, surfaces.observe)
186
+ surfaces.update()
187
+ if surfaces.side_panel_requested:
188
+ surfaces.side_panel_requested = False
189
+ if not surfaces.approval_pending:
190
+ panels.open("side")
191
+ panes = terminal_layout.geometry(
192
+ height, width, panels.group is not None, mounted=surfaces.side,
193
+ draft=draft, caret=caret, state=client.state
194
+ )
195
+ content_width, view = panes.core_width, panes.capacity
196
+ history_inserted, status, selected = journal.apply_history_requests(status, selected)
197
+ if history_inserted:
198
+ viewport.invalidate()
199
+ cursor, profile, profile_revision = journal.cursor, journal.profile, journal.profile_revision
200
+ shown_monitor = visible_monitor(client, journal.workflow)
201
+ view = max(1, view - bool(shown_monitor))
202
+ current_turn = live.current(submitted_turn)
203
+ if catalog and catalog.done() and not catalog.exception():
204
+ capabilities = catalog.result()
205
+ agent_binding, side_binding = journal.model_bindings(capabilities)
206
+ shown = journal.inspect(selected, submit_waking)
207
+ offset = viewport.update(panes, view, offset, follow=follow, trace=trace, side_binding=side_binding)
208
+ projected, side = viewport.projected, viewport.side_rows
209
+ mounted_side, mounted_inline = surfaces.compose(
210
+ width, panes, view, legacy=panels.group is not None,
211
+ protected=bool(overlay or shown or surfaces.approval_pending or
212
+ accounts is not None or model_picker is not None or
213
+ permission_index is not None or slash.completions(draft, client.edition)),
214
+ )
215
+ if surfaces.approvals.cards and not (overlay or shown):
216
+ mounted_inline = terminal_layout.approval_rows(
217
+ surfaces.approvals, content_width, view
218
+ )
219
+ view = max(1, view - len(mounted_inline))
220
+ panels.offset = min(panels.offset, max(0, len(side) - view))
221
+ end_offset = max(0, len(projected) - view)
222
+ offset = end_offset if follow else min(max(0, offset), end_offset)
223
+ visible = terminal_layout.detail_rows(
224
+ overlay, shown, overlay_command, content_width
225
+ )
226
+ detail_offset = min(max(0, detail_offset), max(0, len(visible) - view))
227
+ body = (
228
+ visible[detail_offset : detail_offset + view]
229
+ if overlay or shown
230
+ else projected[offset : offset + view]
231
+ )
232
+ visible_groups = (
233
+ viewport.highlight_groups(body, offset) if not (shown or overlay or panels.group) else {}
234
+ )
235
+ mode = "Event detail" if shown else "Journal" if trace else ""
236
+ if completion_draft != draft:
237
+ completion_draft, completion_index, dismissed = draft, 0, False
238
+ choices = (
239
+ slash.completions(draft, client.edition)
240
+ if not dismissed
241
+ and accounts is None
242
+ and model_picker is None
243
+ and permission_index is None
244
+ and not panels.focused
245
+ else []
246
+ )
247
+ picker, model_picker, status = refresh_picker(
248
+ model_picker, finish_picker, status
249
+ )
250
+ if accounts is not None:
251
+ lines = subscriptions.picker_lines(
252
+ accounts, account_index, profile, subscriptions.ACTIONS[action_index]
253
+ )
254
+ picker.extend(terminal_layout.account_rows(lines, content_width))
255
+ if permission_index is not None:
256
+ picker = terminal_layout.permission_rows(profile, permission_index, slash.permission_modes(client.edition))
257
+ live_status = (
258
+ live.status_line(current_turn) if current_turn and not (picker or choices) else ""
259
+ )
260
+ if inp.window_size() not in (None, (height, width)):
261
+ # The terminal resized while this frame was being composed. Painting it
262
+ # would lay out the old geometry on the new screen (a torn frame), so
263
+ # compose again at the new size instead.
264
+ continue
265
+ terminal_layout.draw(
266
+ screen,
267
+ styles,
268
+ version_label=version_label,
269
+ agent_binding=agent_binding,
270
+ active_core_model=live.core_model_label(),
271
+ side_binding=side_binding,
272
+ status=journal_status or status or surfaces.approvals.status or surfaces.status or str(surfaces.approvals_unavailable or "") or ("Queued" if submitted_turn in live.queued_inputs else ""),
273
+ draft=draft,
274
+ body=body,
275
+ mode=mode,
276
+ panes=panes,
277
+ side=side if panels.group else mounted_side,
278
+ side_offset=panels.offset if panels.group else 0,
279
+ panel_title=panels.title() if panels.group else "Views",
280
+ mounted_inline=mounted_inline,
281
+
282
+ panel_group=panels.group,
283
+ subagents=panels.subagent_summary(),
284
+ summary=terminal_layout.side_footer_summary(viewport.summary, client.edition),
285
+ picker=picker,
286
+ workflow=shown_monitor,
287
+ recipient=live_status,
288
+ choices=choices,
289
+ completion_index=completion_index,
290
+ cache=frame_cache,
291
+ force=force_redraw,
292
+ )
293
+ force_redraw = False
294
+ now = time.monotonic()
295
+ deadlines = (decoder.timeout_ms(now), deliveries.timeout_ms(now))
296
+ wait_ms = min((timeout for timeout in deadlines if timeout is not None), default=None)
297
+ did_resize = inp.poll(0 if viewport.rewrapping else wait_ms)
298
+ prior = (draft, caret, selected, offset, follow, detail_offset, panels.group)
299
+ command_count = len(commands)
300
+ paste_at_start = paste_origin
301
+ paste_aborted = False
302
+ decoded = [("resize", 0)] if did_resize else []
303
+ if inp.buf:
304
+ decoded.extend(decode_batch(inp, decoder, time.monotonic))
305
+ elif timed_out := decoder.on_timeout(time.monotonic()):
306
+ decoded.append((timed_out, 0))
307
+ for kind, stroke in decoded:
308
+ if kind == "eof":
309
+ if paste_origin is not None:
310
+ draft, caret, operation = paste_origin
311
+ store_view()
312
+ return
313
+ if kind == "resize":
314
+ sz = inp.window_size()
315
+ if sz is not None:
316
+ curses.resize_term(sz[0], sz[1])
317
+ else:
318
+ curses.update_lines_cols()
319
+ force_redraw = True
320
+ continue
321
+ if kind in ("wheel_up", "wheel_down"):
322
+ step = -3 if kind == "wheel_up" else 3
323
+ if panes.side_start is not None and stroke >= panes.side_start:
324
+ terminal_layout.move_panel(panels, side, view, step)
325
+ elif shown or overlay:
326
+ detail_offset = terminal_layout.scroll(detail_offset, visible, view, step)
327
+ else:
328
+ offset = terminal_layout.scroll(offset, projected, view, step)
329
+ follow = offset >= len(projected) - view
330
+ if step < 0 and offset == 0:
331
+ journal.request_earlier(submit_waking)
332
+ continue
333
+ if kind == "paste_mode":
334
+ paste_origin = (draft, caret, operation) if stroke else None
335
+ continue
336
+ if kind == "paste_cancel":
337
+ if paste_origin is not None:
338
+ draft, caret, operation = paste_origin
339
+ paste_origin = None
340
+ paste_aborted = True
341
+ status = "Incomplete paste discarded"
342
+ continue
343
+ if kind == "interrupt":
344
+ if current_turn:
345
+ try:
346
+ status = live.control(client, "cancel", "", operation, current_turn)
347
+ except JOURNAL_ERRORS as error:
348
+ status = f"Cancel turn {current_turn} · {error}"
349
+ elif model_picker is not None:
350
+ finish_picker({"outcome": "Unchanged"})
351
+ model_picker = None
352
+ elif permission_index is not None:
353
+ permission_index = None
354
+ elif accounts is not None:
355
+ finish_picker({"outcome": "Viewed accounts"})
356
+ accounts = None
357
+ elif choices:
358
+ dismissed = True
359
+ elif draft:
360
+ draft, caret = "", 0
361
+ input_history.reset()
362
+ continue
363
+ if kind == "detach":
364
+ if surfaces.group_focus is not None:
365
+ surfaces.group_focus = None
366
+ continue
367
+ if permission_index is not None:
368
+ permission_index = None
369
+ continue
370
+ if choices and not stroke:
371
+ dismissed = True
372
+ continue
373
+ if model_picker is not None:
374
+ if not model_picker.back():
375
+ finish_picker({"outcome": "Unchanged"})
376
+ model_picker = None
377
+ continue
378
+ if accounts is None and panels.group:
379
+ panels.back()
380
+ continue
381
+ if accounts is None:
382
+ if (
383
+ draft,
384
+ caret,
385
+ selected,
386
+ offset,
387
+ follow,
388
+ detail_offset,
389
+ panels.group,
390
+ ) != prior or len(commands) != command_count:
391
+ store_view()
392
+ return
393
+ finish_picker({"outcome": "Viewed accounts"})
394
+ accounts = None
395
+ continue
396
+ if kind == "paste_text":
397
+ if permission_index is None and model_picker is None and accounts is None:
398
+ panels.focused = False
399
+ if len(draft) < 2048:
400
+ draft = draft[:caret] + stroke + draft[caret:]
401
+ caret += 1
402
+ continue
403
+ if kind == "edit":
404
+ if permission_index is None and model_picker is None and accounts is None:
405
+ draft, caret, _ = apply_composer_edit(draft, caret, stroke)
406
+ continue
407
+ if kind != "key":
408
+ continue
409
+ # Raw control characters share the editor's ASCII shortcuts. Unicode
410
+ # text stays a string and cannot compare equal to a curses key code.
411
+ key = stroke
412
+ if isinstance(stroke, str) and not is_composer_text(stroke):
413
+ if not stroke.isascii():
414
+ continue
415
+ key = ord(stroke)
416
+ if permission_index is not None:
417
+ if key in (curses.KEY_UP, curses.KEY_DOWN, 9):
418
+ step = -1 if key == curses.KEY_UP else 1
419
+ permission_index = (permission_index + step) % len(
420
+ slash.permission_modes(client.edition)
421
+ )
422
+ elif key in (10, 13):
423
+ mode = slash.permission_modes(client.edition)[permission_index][0]
424
+ try:
425
+ status = slash.permissions(
426
+ client, profile, profile_revision, mode
427
+ )
428
+ except (ValueError, OSError) as error:
429
+ status = str(error)
430
+ permission_index = None
431
+ continue
432
+ if completion_draft != draft:
433
+ completion_draft, completion_index, dismissed = draft, 0, False
434
+ choices = (
435
+ slash.completions(draft, client.edition)
436
+ if accounts is None and model_picker is None and not panels.focused
437
+ else []
438
+ )
439
+ if key == 12:
440
+ force_redraw = True
441
+ continue
442
+ if key == 18:
443
+ store_view()
444
+ return TerminalExit.RELOAD
445
+ if model_picker is not None:
446
+ if key in (curses.KEY_UP, curses.KEY_DOWN, 9):
447
+ model_picker.move(-1 if key == curses.KEY_UP else 1)
448
+ elif key in (10, 13):
449
+ try:
450
+ if result := model_picker.select():
451
+ finish_picker(result)
452
+ status = ""
453
+ model_picker = None
454
+ except (ValueError, OSError) as error:
455
+ status = str(error)
456
+ finish_picker({"outcome": status})
457
+ model_picker = None
458
+ continue
459
+ if accounts is not None:
460
+ if key in (curses.KEY_UP, curses.KEY_DOWN, 9):
461
+ account_index = 1 - account_index
462
+ continue
463
+ if key in (curses.KEY_LEFT, curses.KEY_RIGHT):
464
+ direction = -1 if key == curses.KEY_LEFT else 1
465
+ action_index = (action_index + direction) % len(
466
+ subscriptions.ACTIONS
467
+ )
468
+ continue
469
+ if key in (10, 13):
470
+ provider = subscriptions.OPTIONS[account_index][1]
471
+ action = subscriptions.ACTIONS[action_index]
472
+ draft = (
473
+ "/login " + provider
474
+ if action == "login"
475
+ else f"/providers set {provider} {action}"
476
+ )
477
+ caret = len(draft)
478
+ accounts = None
479
+ if action == "login":
480
+ finish_picker(
481
+ {
482
+ "outcome": "Selected "
483
+ + subscriptions.OPTIONS[account_index][0]
484
+ }
485
+ )
486
+ continue
487
+ try:
488
+ outcome = subscriptions.assign(
489
+ client, draft.partition(" ")[2], profile, profile_revision
490
+ )
491
+ draft, caret = "", 0
492
+ finish_picker({"outcome": outcome})
493
+ status = ""
494
+ except JOURNAL_ERRORS as error:
495
+ status = str(error)
496
+ finish_picker({"outcome": status})
497
+ continue
498
+ continue
499
+ if (
500
+ not draft
501
+ and surfaces.approvals.cards
502
+ and isinstance(stroke, str)
503
+ and (message := surfaces.approval_key(stroke)) is not None
504
+ ):
505
+ status = message
506
+ continue
507
+ if not draft and isinstance(stroke, str) and stroke == "v" and surfaces.approvals.cards:
508
+ continue
509
+ if not draft and surfaces.select_group(key, visible_groups):
510
+ continue
511
+ if not draft and not (overlay or shown or panels.group) and surfaces.navigate(key):
512
+ continue
513
+ if key == curses.KEY_BTAB and not panels.focused:
514
+ mode = "manual" if profile.get("permissions") == "yolo" else "yolo"
515
+ try:
516
+ status = slash.permissions(client, profile, profile_revision, mode)
517
+ except (ValueError, OSError) as error:
518
+ status = str(error)
519
+ continue
520
+ if panels.group and key == 9 and not choices:
521
+ panels.focused = not panels.focused
522
+ continue
523
+ if panels.focused and isinstance(key, str):
524
+ panels.focused = False
525
+ if choices:
526
+ if key in (curses.KEY_UP, curses.KEY_DOWN):
527
+ step = 1 if key == curses.KEY_DOWN else -1
528
+ completion_index = (completion_index + step) % len(choices)
529
+ continue
530
+ if key in (9, 10, 13):
531
+ spelling, spec = choices[completion_index]
532
+ completing = spelling != draft
533
+ draft, caret = spelling, len(spelling)
534
+ if key == 9 or (
535
+ completing
536
+ and spec.arguments
537
+ and not spec.arguments.startswith("[")
538
+ ):
539
+ if spec.arguments:
540
+ draft += " "
541
+ caret += 1
542
+ completion_draft, dismissed = draft, True
543
+ continue
544
+ if key in (2, curses.KEY_F2, curses.KEY_F3) and side_agents_available(client.edition):
545
+ group = "subagents" if key == curses.KEY_F3 else "side"
546
+ panels.open(
547
+ None if panels.group == group else group, finding=group == "side"
548
+ )
549
+ elif (
550
+ panels.focused
551
+ and not choices
552
+ and key in (curses.KEY_UP, curses.KEY_DOWN)
553
+ ):
554
+ step = 1 if key == curses.KEY_DOWN else -1
555
+ terminal_layout.move_panel(panels, side, view, step)
556
+ elif panels.focused and not choices and key in (10, 13):
557
+ panels.enter()
558
+ store_view()
559
+ elif (
560
+ key in (curses.KEY_PPAGE, curses.KEY_NPAGE)
561
+ and panes.side_start is not None
562
+ ):
563
+ direction = 1 if key == curses.KEY_NPAGE else -1
564
+ terminal_layout.move_panel(panels, side, view, direction * view)
565
+ elif key in (4, 27):
566
+ return
567
+ elif (shown or overlay) and key in (curses.KEY_DOWN, curses.KEY_UP):
568
+ step = 1 if key == curses.KEY_DOWN else -1
569
+ detail_offset = terminal_layout.scroll(detail_offset, visible, view, step)
570
+ elif key in (curses.KEY_UP, curses.KEY_DOWN):
571
+ direction = -1 if key == curses.KEY_UP else 1
572
+ if recalled := input_history.move(direction, draft, caret):
573
+ draft, caret = recalled
574
+ operation = uuid4().hex
575
+ elif key == curses.KEY_PPAGE:
576
+ follow = False
577
+ offset = max(0, offset - max(1, view - 2))
578
+ if offset == 0 and not (shown or overlay or selected):
579
+ journal.request_earlier(submit_waking)
580
+ elif key == curses.KEY_NPAGE:
581
+ offset = min(max(0, len(projected) - view), offset + max(1, view - 2))
582
+ follow = offset >= len(projected) - view
583
+ elif apply_composer_edit(draft, caret, key)[2]:
584
+ draft, caret, _ = apply_composer_edit(draft, caret, key)
585
+ elif key in (10, 13) and draft.strip():
586
+ try:
587
+ slash_command = is_slash_command(draft)
588
+ command = slash.resolve(draft, client.edition) if slash_command else None
589
+ info = (
590
+ slash.overlay(command, client)
591
+ if command and command.name != "model"
592
+ else None
593
+ )
594
+ if command and command.name == "exit":
595
+ remember_command()
596
+ draft, caret = "", 0
597
+ operation = uuid4().hex
598
+ store_view()
599
+ return
600
+ outcome = None
601
+ old_pending = pending
602
+ if (
603
+ command
604
+ and info is None
605
+ and command.name not in ("cause", "grade", "constraint")
606
+ ):
607
+ overlay, selected = "", 0
608
+ if command and command.name == "layout":
609
+ if command.rest == "reload":
610
+ draft, caret = "", 0
611
+ store_view()
612
+ return TerminalExit.RELOAD
613
+ surfaces.command(command.rest)
614
+ status = ""
615
+ elif info is not None:
616
+ overlay, selected, detail_offset = info, 0, 0
617
+ status = ""
618
+ elif command and command.name == "model":
619
+ model_picker = ModelPicker(
620
+ client, submit_waking, command.rest, workspace
621
+ )
622
+ trace, follow, status = False, True, ""
623
+ elif command and command.name == "service-tier":
624
+ model_picker = ServiceTierPicker(
625
+ client, submit_waking, command.rest, workspace
626
+ )
627
+ trace, follow, status = False, True, ""
628
+ elif command and command.name in ("permissions", "yolo"):
629
+ mode = "yolo" if command.name == "yolo" else command.rest
630
+ if mode:
631
+ status = slash.permissions(
632
+ client, profile, profile_revision, mode
633
+ )
634
+ else:
635
+ modes = [m for m, _ in slash.permission_modes(client.edition)]
636
+ current = profile.get("permissions", "manual")
637
+ permission_index = modes.index(current) if current in modes else 0
638
+ trace, follow, status = False, True, ""
639
+ elif command and command.name == "providers":
640
+ if client.viewer is not None:
641
+ raise ValueError("Providers requires the owner terminal")
642
+ if command.rest:
643
+ outcome = subscriptions.assign(
644
+ client, command.rest, profile, profile_revision
645
+ )
646
+ else:
647
+ accounts = [
648
+ submit_waking(check) for _, _, check in subscriptions.OPTIONS
649
+ ]
650
+ account_index = action_index = 0
651
+ trace, follow = False, True
652
+ status = ""
653
+ elif command and command.name == "login":
654
+ if client.viewer is not None:
655
+ raise ValueError("Login requires the owner terminal")
656
+ status = subscriptions.synth_login(command.rest)
657
+ elif command and command.name == "compact":
658
+ overlay, detail_offset = slash.compact(client, operation), 0
659
+ elif command and command.name in ("sideagents", "mcp", "subagents"):
660
+ if command.name == "mcp" and command.rest not in ("", "side"):
661
+ raise ValueError("Usage: /mcp side")
662
+ if command.name != "mcp" and command.rest not in (
663
+ "",
664
+ "open",
665
+ "close",
666
+ ):
667
+ raise ValueError(f"Usage: /{command.name} [open|close]")
668
+ group = "subagents" if command.name == "subagents" else "side"
669
+ if command.rest != "close":
670
+ panels.open(group)
671
+ elif panels.group == group:
672
+ panels.open(None)
673
+ status = ""
674
+ elif command and command.name == "sidebar":
675
+ panels.open(None if panels.group == "side" else "side")
676
+ elif command and command.name == "trace":
677
+ trace, follow, overlay, selected = True, True, "", 0
678
+ elif command and command.name in ("new", "resume"):
679
+ if command.name == "resume" and not command.rest:
680
+ overlay, selected, detail_offset = (
681
+ slash.resume_menu(client),
682
+ 0,
683
+ 0,
684
+ )
685
+ else:
686
+ requested = command.rest or ("run-" + uuid4().hex[:12])
687
+ names = slash.run_names(client)
688
+ if command.name == "resume" and requested not in names:
689
+ raise ValueError("Unknown run")
690
+ if command.name == "new":
691
+ if requested in names:
692
+ raise ValueError("Run already exists")
693
+ with Client(client.state, requested, client.version) as fresh:
694
+ fresh.send("start")
695
+ fresh.send("configure", profile=starter())
696
+ remember_command()
697
+ draft, caret = "", 0
698
+ operation = uuid4().hex
699
+ store_view()
700
+ return requested
701
+ elif command and command.name == "event":
702
+ requested = int(command.rest)
703
+ if not 1 <= requested <= cursor:
704
+ raise ValueError("Unknown event")
705
+ overlay = ""
706
+ selected = requested
707
+ detail_offset = 0
708
+ elif draft.partition(" ")[0] in (
709
+ "/explain",
710
+ "/preference",
711
+ "/recommend",
712
+ "/choose",
713
+ ):
714
+ research = require_feature(Feature.RESEARCH)
715
+ overlay, reviewed = research.terminal_handoff(
716
+ client, draft[1:], operation, reviewed
717
+ )
718
+ detail_offset = 0
719
+ elif command and command.name == "back":
720
+ trace, follow = False, True
721
+ overlay = ""
722
+ selected = 0
723
+ elif draft in ("/cause", "/grade", "/constraint") and shown:
724
+ selected = (
725
+ shown["cause"]
726
+ if draft == "/cause"
727
+ else journal.research_link(shown, draft[1:])
728
+ )
729
+ detail_offset = 0
730
+ elif draft == "/trial" and pending is None:
731
+ campaign = require_feature(Feature.CAMPAIGN)
732
+ pending = submit_waking(
733
+ campaign.plan_candidate,
734
+ client,
735
+ os.environ["SYNTH_CODE_INCUMBENT"],
736
+ os.environ["SYNTH_CODE_BASELINE"],
737
+ os.environ["SYNTH_CODE_PROFILE"],
738
+ )
739
+ status = "Comparing public evidence"
740
+ elif command and command.name in ("steer", "cancel"):
741
+ status = live.control(
742
+ client, command.name, command.rest, operation, current_turn
743
+ )
744
+ if command.name == "steer":
745
+ steering_operation = operation
746
+ elif not slash_command:
747
+ deliveries.enqueue(draft, operation, current_turn)
748
+ wake()
749
+ overlay, selected, trace, follow = "", 0, False, True
750
+ submitted_turn = operation
751
+ status = ""
752
+ elif pending is None:
753
+ if slash_command:
754
+ # Long operations use the same client entry points as headless requests.
755
+ command = draft[1:]
756
+ if command == "skills" or command.startswith("skills "):
757
+ overlay = str(
758
+ operator(client, workspace, command, operation)
759
+ )
760
+ detail_offset = 0
761
+ elif command.startswith("review "):
762
+ overlay = operator(
763
+ client, workspace, command, operation
764
+ )
765
+ detail_offset = 0
766
+ else:
767
+ if command.partition(" ")[0] in (
768
+ "ask",
769
+ "edit",
770
+ "create",
771
+ ):
772
+ submitted_turn = operation
773
+ pending = submit_waking(
774
+ operator, client, workspace, command, operation
775
+ )
776
+ status = "Working…"
777
+ else:
778
+ status = "Waiting for the active turn…"
779
+ continue
780
+ except (OSError, ValueError, KeyError, EditionUnavailable) as error:
781
+ status = f"Pending draft · {error}"
782
+ if draft.split()[0] in ("/model", "/providers"):
783
+ remember_command()
784
+ finish_picker({"outcome": str(error)})
785
+ continue
786
+ if slash_command:
787
+ remember_command()
788
+ if outcome:
789
+ commands[-1]["payload"]["outcome"] = outcome
790
+ status = ""
791
+ overlay_command = draft
792
+ if pending is not old_pending:
793
+ pending_command = draft
794
+ draft = ""
795
+ input_history.reset()
796
+ operation = uuid4().hex
797
+ restored_input = None
798
+ caret = 0
799
+ elif isinstance(key, str) and len(draft) < 2048:
800
+ draft = draft[:caret] + key + draft[caret:]
801
+ caret += 1
802
+ changed = (
803
+ (draft, caret, selected, offset, follow, detail_offset, panels.group) != prior
804
+ or len(commands) != command_count
805
+ or paste_at_start is not None
806
+ )
807
+ if paste_origin is None and changed:
808
+ original_draft = paste_at_start[0] if paste_at_start is not None else prior[0]
809
+ if draft and draft != original_draft and not paste_aborted:
810
+ if (
811
+ restored_input is not None
812
+ and restored_input.revision(draft) is DraftRevision.RESEND
813
+ ):
814
+ operation = restored_input.identity
815
+ else:
816
+ operation = uuid4().hex
817
+ store_view()