optio-opencode 0.1.2__tar.gz → 0.1.3__tar.gz

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 (30) hide show
  1. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/PKG-INFO +1 -1
  2. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/pyproject.toml +1 -1
  3. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/prompt.py +16 -3
  4. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/session.py +60 -4
  5. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/types.py +8 -0
  6. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode.egg-info/PKG-INFO +1 -1
  7. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_session_local.py +179 -0
  8. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_session_resume.py +11 -3
  9. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_types.py +13 -0
  10. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/README.md +0 -0
  11. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/setup.cfg +0 -0
  12. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/__init__.py +0 -0
  13. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/host_actions.py +0 -0
  14. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode/snapshots.py +0 -0
  15. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode.egg-info/SOURCES.txt +0 -0
  16. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode.egg-info/dependency_links.txt +0 -0
  17. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode.egg-info/requires.txt +0 -0
  18. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/src/optio_opencode.egg-info/top_level.txt +0 -0
  19. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_host_local.py +0 -0
  20. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_host_primitives_local.py +0 -0
  21. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_host_primitives_remote.py +0 -0
  22. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_host_remote_resume.py +0 -0
  23. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_host_resume.py +0 -0
  24. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_prompt.py +0 -0
  25. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_sanity.py +0 -0
  26. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_session_blob_hooks.py +0 -0
  27. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_session_hooks.py +0 -0
  28. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_session_remote.py +0 -0
  29. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_smart_install.py +0 -0
  30. {optio_opencode-0.1.2 → optio_opencode-0.1.3}/tests/test_snapshots.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: optio-opencode
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Run opencode web as an optio task; local subprocess or remote via SSH.
5
5
  Author-email: Kristof Csillag <kristof.csillag@deai-labs.com>
6
6
  License-Expression: Apache-2.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "optio-opencode"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  description = "Run opencode web as an optio task; local subprocess or remote via SSH."
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -79,9 +79,18 @@ caveats:**
79
79
 
80
80
  ### Detecting a resume: `resume.log`
81
81
 
82
- Each session start (fresh or resumed) appends one ISO 8601 timestamp
83
- to `./resume.log`. The very first line is the original launch
84
- timestamp; each subsequent line is a resume.
82
+ Each session start (fresh or resumed) appends one line to
83
+ `./resume.log`. Line format:
84
+
85
+ ```
86
+ <ISO 8601 UTC timestamp>[ REFRESHED:<comma-separated filenames>]
87
+ ```
88
+
89
+ The very first line is the original launch timestamp; each subsequent
90
+ line is a resume. The optional `REFRESHED:` suffix signals that the
91
+ harness rewrote the listed files on that resume (e.g.
92
+ `2026-05-28T13:15:42Z REFRESHED:AGENTS.md`) — your in-memory copy of
93
+ those files is stale and must be re-read before continuing.
85
94
 
86
95
  **At the start of every new incoming user message, read
87
96
  `./resume.log` first.** Compare the latest line to the value you
@@ -92,6 +101,10 @@ the situation as a resume:
92
101
  outside the workdir are still where you left them.
93
102
  - Re-establish anything that's gone (re-launch a server, re-fetch a
94
103
  file, etc.) before continuing.
104
+ - **If the latest line carries a `REFRESHED:` suffix, re-read each
105
+ listed file** (e.g. `cat ./AGENTS.md`) — the harness updated it
106
+ since your last context snapshot and the version you remember is
107
+ out of date.
95
108
  - Then resume the work you were doing.
96
109
 
97
110
  If a resume slips past unnoticed, a failing tool call is the
@@ -160,6 +160,7 @@ async def run_opencode_session(ctx: ProcessContext, config: OpencodeTaskConfig)
160
160
  """
161
161
  nonlocal launched_handle, opencode_exec, session_id, preserved_session_id
162
162
 
163
+ refreshed_files: list[str] = []
163
164
  if not resuming:
164
165
  # Fresh start: the protocol driver has already created the
165
166
  # workdir, deliverables/ subdir, and empty optio.log. Ensure
@@ -189,9 +190,14 @@ async def run_opencode_session(ctx: ProcessContext, config: OpencodeTaskConfig)
189
190
  # self-healing (snapshot lookup returns None → fresh-start
190
191
  # fallback) handles the rare case where the flag is true but
191
192
  # no snapshot exists.
193
+ else:
194
+ # Resume: when on_resume_refresh is wired, recompute AGENTS.md
195
+ # from the refreshed config and overwrite the workdir copy if
196
+ # the rendered text differs from the snapshot-restored file.
197
+ refreshed_files = await _maybe_refresh_on_resume(host, hook_ctx, config)
192
198
 
193
199
  if config.supports_resume:
194
- await _append_resume_log_entry(host)
200
+ await _append_resume_log_entry(host, refreshed=refreshed_files)
195
201
 
196
202
  # opencode is already installed by run_opencode_session before
197
203
  # this body runs (so resume restore can call opencode_import
@@ -469,16 +475,26 @@ async def _rotate_optio_log(host: Host) -> None:
469
475
  await host.write_text("optio.log", "")
470
476
 
471
477
 
472
- async def _append_resume_log_entry(host) -> None:
473
- """Append one ISO 8601 UTC timestamp line to <workdir>/resume.log.
478
+ async def _append_resume_log_entry(
479
+ host, *, refreshed: list[str] | None = None,
480
+ ) -> None:
481
+ """Append one line to <workdir>/resume.log.
482
+
483
+ Line format: ``<ISO 8601 UTC timestamp>[ REFRESHED:<comma-separated names>]``.
484
+ The optional ``REFRESHED:`` suffix signals that the harness rewrote
485
+ the listed files on this session start. Agents are instructed (via
486
+ the resume section of AGENTS.md) to re-read tagged files.
474
487
 
475
488
  Creates the file if missing (via shell `>>`). Caller is responsible
476
489
  for gating this on config.supports_resume.
477
490
  """
478
491
  ts = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
492
+ line = ts
493
+ if refreshed:
494
+ line = f"{ts} REFRESHED:{','.join(refreshed)}"
479
495
  target = f"{host.workdir}/resume.log"
480
496
  result = await host.run_command(
481
- f"echo {shlex.quote(ts)} >> {shlex.quote(target)}"
497
+ f"echo {shlex.quote(line)} >> {shlex.quote(target)}"
482
498
  )
483
499
  if result.exit_code != 0:
484
500
  raise RuntimeError(
@@ -487,6 +503,46 @@ async def _append_resume_log_entry(host) -> None:
487
503
  )
488
504
 
489
505
 
506
+ async def _maybe_refresh_on_resume(
507
+ host, hook_ctx, config: OpencodeTaskConfig,
508
+ ) -> list[str]:
509
+ """Run the on_resume_refresh hook (if any) and rewrite AGENTS.md when
510
+ the rendered content differs from the workdir copy.
511
+
512
+ Returns the list of filenames the harness rewrote (currently at most
513
+ ``["AGENTS.md"]``), suitable for tagging the next ``resume.log`` line.
514
+ A hook that raises is logged and ignored — the resumed session keeps
515
+ whatever AGENTS.md the snapshot restored.
516
+ """
517
+ if config.on_resume_refresh is None:
518
+ return []
519
+ try:
520
+ new_config = config.on_resume_refresh(config)
521
+ except Exception:
522
+ _LOG.exception(
523
+ "on_resume_refresh raised; keeping existing AGENTS.md from snapshot",
524
+ )
525
+ return []
526
+ new_agents_md = compose_agents_md(
527
+ new_config.consumer_instructions,
528
+ workdir_exclude=new_config.workdir_exclude,
529
+ supports_resume=new_config.supports_resume,
530
+ )
531
+ try:
532
+ existing = await hook_ctx.read_text_from_host("AGENTS.md", silent=True)
533
+ except FileNotFoundError:
534
+ existing = None
535
+ except Exception:
536
+ _LOG.exception(
537
+ "failed to read existing AGENTS.md on resume; rewriting unconditionally",
538
+ )
539
+ existing = None
540
+ if existing == new_agents_md:
541
+ return []
542
+ await host.write_text("AGENTS.md", new_agents_md)
543
+ return ["AGENTS.md"]
544
+
545
+
490
546
  def _pick_local_workdir() -> str:
491
547
  return tempfile.mkdtemp(prefix="optio-opencode-")
492
548
 
@@ -43,6 +43,14 @@ class OpencodeTaskConfig:
43
43
  # raises a config error: asymmetric usage is always a mistake.
44
44
  session_blob_encrypt: Callable[[bytes], bytes] | None = None
45
45
  session_blob_decrypt: Callable[[bytes], bytes] | None = None
46
+ # Optional hook fired on resume only (never on fresh start). Receives
47
+ # the original task config; returns a (possibly mutated/replaced) config.
48
+ # The harness re-renders AGENTS.md from the returned config and writes
49
+ # it back to the workdir only when it differs from the file on disk.
50
+ # When written, the harness tags the new line in resume.log with
51
+ # `REFRESHED:AGENTS.md` so the agent knows to re-read. None (default)
52
+ # → no refresh; the resumed session keeps its original AGENTS.md.
53
+ on_resume_refresh: Callable[["OpencodeTaskConfig"], "OpencodeTaskConfig"] | None = None
46
54
 
47
55
  def __post_init__(self) -> None:
48
56
  e = self.session_blob_encrypt is not None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: optio-opencode
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Run opencode web as an optio task; local subprocess or remote via SSH.
5
5
  Author-email: Kristof Csillag <kristof.csillag@deai-labs.com>
6
6
  License-Expression: Apache-2.0
@@ -348,6 +348,185 @@ async def test_append_resume_log_entry_appends_on_repeat_call(tmp_workdir):
348
348
  assert lines[0] != lines[1]
349
349
 
350
350
 
351
+ async def test_append_resume_log_entry_with_refreshed_tag(tmp_workdir):
352
+ """Passing refreshed=[...] adds ` REFRESHED:<files>` suffix to the line."""
353
+ import os
354
+ import re
355
+ from optio_host.host import LocalHost
356
+ from optio_opencode.session import _append_resume_log_entry
357
+
358
+ host = LocalHost(taskdir=tmp_workdir)
359
+ await host.setup_workdir()
360
+
361
+ await _append_resume_log_entry(host, refreshed=["AGENTS.md"])
362
+
363
+ resume_log = os.path.join(host.workdir, "resume.log")
364
+ with open(resume_log) as f:
365
+ lines = [line for line in f.read().splitlines() if line]
366
+ assert len(lines) == 1
367
+ assert re.match(
368
+ r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z REFRESHED:AGENTS\.md$",
369
+ lines[0],
370
+ )
371
+
372
+
373
+ async def test_append_resume_log_entry_with_multiple_refreshed_files(tmp_workdir):
374
+ """refreshed=[a, b] → REFRESHED:a,b (comma-separated, no spaces)."""
375
+ import os
376
+ import re
377
+ from optio_host.host import LocalHost
378
+ from optio_opencode.session import _append_resume_log_entry
379
+
380
+ host = LocalHost(taskdir=tmp_workdir)
381
+ await host.setup_workdir()
382
+
383
+ await _append_resume_log_entry(host, refreshed=["AGENTS.md", "opencode.json"])
384
+
385
+ resume_log = os.path.join(host.workdir, "resume.log")
386
+ with open(resume_log) as f:
387
+ lines = [line for line in f.read().splitlines() if line]
388
+ assert len(lines) == 1
389
+ assert re.match(
390
+ r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z REFRESHED:AGENTS\.md,opencode\.json$",
391
+ lines[0],
392
+ )
393
+
394
+
395
+ async def test_append_resume_log_entry_empty_refreshed_omits_tag(tmp_workdir):
396
+ """refreshed=[] → bare timestamp, no REFRESHED: suffix (same as None)."""
397
+ import os
398
+ import re
399
+ from optio_host.host import LocalHost
400
+ from optio_opencode.session import _append_resume_log_entry
401
+
402
+ host = LocalHost(taskdir=tmp_workdir)
403
+ await host.setup_workdir()
404
+
405
+ await _append_resume_log_entry(host, refreshed=[])
406
+
407
+ resume_log = os.path.join(host.workdir, "resume.log")
408
+ with open(resume_log) as f:
409
+ lines = [line for line in f.read().splitlines() if line]
410
+ assert len(lines) == 1
411
+ assert re.match(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$", lines[0])
412
+
413
+
414
+ # ---- resume refresh -------------------------------------------------------
415
+
416
+
417
+ async def test_maybe_refresh_on_resume_no_hook(tmp_workdir):
418
+ """on_resume_refresh=None → returns [] and does not write AGENTS.md."""
419
+ import os
420
+ from optio_host.host import LocalHost
421
+ from optio_opencode.session import _maybe_refresh_on_resume
422
+ from optio_opencode.types import OpencodeTaskConfig
423
+
424
+ host = LocalHost(taskdir=tmp_workdir)
425
+ await host.setup_workdir()
426
+ config = OpencodeTaskConfig(consumer_instructions="x")
427
+
428
+ refreshed = await _maybe_refresh_on_resume(host, None, config)
429
+
430
+ assert refreshed == []
431
+ assert not os.path.exists(os.path.join(host.workdir, "AGENTS.md"))
432
+
433
+
434
+ async def test_maybe_refresh_on_resume_unchanged_content_skips_write(tmp_workdir):
435
+ """Hook return identical to existing AGENTS.md → no write, [] returned."""
436
+ import os
437
+ from optio_host.host import LocalHost
438
+ from optio_opencode.prompt import compose_agents_md
439
+ from optio_opencode.session import _maybe_refresh_on_resume
440
+ from optio_opencode.types import OpencodeTaskConfig
441
+
442
+ host = LocalHost(taskdir=tmp_workdir)
443
+ await host.setup_workdir()
444
+ config = OpencodeTaskConfig(
445
+ consumer_instructions="task X",
446
+ on_resume_refresh=lambda c: c,
447
+ )
448
+ expected = compose_agents_md(
449
+ config.consumer_instructions,
450
+ workdir_exclude=config.workdir_exclude,
451
+ supports_resume=config.supports_resume,
452
+ )
453
+ await host.write_text("AGENTS.md", expected)
454
+ mtime_before = os.path.getmtime(os.path.join(host.workdir, "AGENTS.md"))
455
+
456
+ class _FakeHookCtx:
457
+ async def read_text_from_host(self, path, *, silent=False):
458
+ full = os.path.join(host.workdir, path)
459
+ with open(full) as f:
460
+ return f.read()
461
+
462
+ refreshed = await _maybe_refresh_on_resume(host, _FakeHookCtx(), config)
463
+
464
+ assert refreshed == []
465
+ mtime_after = os.path.getmtime(os.path.join(host.workdir, "AGENTS.md"))
466
+ assert mtime_after == mtime_before # write was skipped
467
+
468
+
469
+ async def test_maybe_refresh_on_resume_changed_content_writes(tmp_workdir):
470
+ """Hook returns new instructions → AGENTS.md rewritten, ['AGENTS.md']."""
471
+ import os
472
+ from dataclasses import replace
473
+ from optio_host.host import LocalHost
474
+ from optio_opencode.prompt import compose_agents_md
475
+ from optio_opencode.session import _maybe_refresh_on_resume
476
+ from optio_opencode.types import OpencodeTaskConfig
477
+
478
+ host = LocalHost(taskdir=tmp_workdir)
479
+ await host.setup_workdir()
480
+ config = OpencodeTaskConfig(
481
+ consumer_instructions="old task",
482
+ on_resume_refresh=lambda c: replace(c, consumer_instructions="new task"),
483
+ )
484
+ old_agents = compose_agents_md(
485
+ "old task",
486
+ workdir_exclude=config.workdir_exclude,
487
+ supports_resume=config.supports_resume,
488
+ )
489
+ await host.write_text("AGENTS.md", old_agents)
490
+
491
+ class _FakeHookCtx:
492
+ async def read_text_from_host(self, path, *, silent=False):
493
+ full = os.path.join(host.workdir, path)
494
+ with open(full) as f:
495
+ return f.read()
496
+
497
+ refreshed = await _maybe_refresh_on_resume(host, _FakeHookCtx(), config)
498
+
499
+ assert refreshed == ["AGENTS.md"]
500
+ with open(os.path.join(host.workdir, "AGENTS.md")) as f:
501
+ content = f.read()
502
+ assert "new task" in content
503
+ assert "old task" not in content
504
+
505
+
506
+ async def test_maybe_refresh_on_resume_hook_raises_keeps_existing(tmp_workdir):
507
+ """Hook that raises → returns [] and leaves existing AGENTS.md alone."""
508
+ import os
509
+ from optio_host.host import LocalHost
510
+ from optio_opencode.session import _maybe_refresh_on_resume
511
+ from optio_opencode.types import OpencodeTaskConfig
512
+
513
+ def _boom(c):
514
+ raise RuntimeError("hook is broken")
515
+
516
+ host = LocalHost(taskdir=tmp_workdir)
517
+ await host.setup_workdir()
518
+ await host.write_text("AGENTS.md", "existing content")
519
+ config = OpencodeTaskConfig(
520
+ consumer_instructions="x", on_resume_refresh=_boom,
521
+ )
522
+
523
+ refreshed = await _maybe_refresh_on_resume(host, None, config)
524
+
525
+ assert refreshed == []
526
+ with open(os.path.join(host.workdir, "AGENTS.md")) as f:
527
+ assert f.read() == "existing content"
528
+
529
+
351
530
  async def test_session_local_supports_resume_false_skips_resume_log(
352
531
  ctx_and_captures, _supply_scenario, tmp_workdir, monkeypatch,
353
532
  ):
@@ -184,7 +184,15 @@ async def test_resume_appends_second_line_to_resume_log(mongo_db, task_root):
184
184
  lines = [line for line in contents.splitlines() if line]
185
185
  assert len(lines) == 2, f"expected 2 lines, got {len(lines)}: {contents!r}"
186
186
 
187
- iso_re = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$")
187
+ # Line format: `<ISO 8601 timestamp>[ REFRESHED:<comma-separated names>]`.
188
+ # This test exercises the no-refresh path (no on_resume_refresh hook),
189
+ # so every line is a bare timestamp; the regex still accepts the
190
+ # extended form to stay valid as the format evolves.
191
+ line_re = re.compile(
192
+ r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z(?: REFRESHED:\S+)?$"
193
+ )
188
194
  for line in lines:
189
- assert iso_re.match(line), f"non-ISO-8601 line: {line!r}"
190
- assert lines[0] <= lines[1], f"timestamps not monotonic: {lines!r}"
195
+ assert line_re.match(line), f"unrecognized resume.log line: {line!r}"
196
+ # Timestamps (the leading token of each line) are monotonic.
197
+ timestamps = [line.split()[0] for line in lines]
198
+ assert timestamps[0] <= timestamps[1], f"timestamps not monotonic: {timestamps!r}"
@@ -95,3 +95,16 @@ def test_opencode_task_config_supports_resume_default_true():
95
95
  def test_opencode_task_config_supports_resume_can_be_disabled():
96
96
  cfg = OpencodeTaskConfig(consumer_instructions="x", supports_resume=False)
97
97
  assert cfg.supports_resume is False
98
+
99
+
100
+ def test_opencode_task_config_on_resume_refresh_default_none():
101
+ cfg = OpencodeTaskConfig(consumer_instructions="x")
102
+ assert cfg.on_resume_refresh is None
103
+
104
+
105
+ def test_opencode_task_config_on_resume_refresh_accepts_callable():
106
+ def _refresh(c):
107
+ return c
108
+
109
+ cfg = OpencodeTaskConfig(consumer_instructions="x", on_resume_refresh=_refresh)
110
+ assert cfg.on_resume_refresh is _refresh
File without changes
File without changes