Spaces:
Running
Running
File size: 7,807 Bytes
a9e27c0 | 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 | import pytest
from free_claude_code.application.code_sessions.models import (
CodeConflictError,
CodeItem,
CodeRun,
CodeSession,
HarnessEvent,
ItemUpdate,
NativeThread,
NativeTurn,
PromptRequest,
)
from free_claude_code.application.code_sessions.state import (
SessionProgress,
SessionState,
)
def running_state():
session = CodeSession(
id="session", cwd="project", model="provider/model", native_thread_id="native"
)
run = CodeRun(
id="run",
session_id=session.id,
ordinal=1,
text="hello",
model=session.model,
status="running",
submission_started=True,
native_turn_id="turn",
)
return SessionState(session, run, (), (), (run,))
def test_admission_installs_only_the_acknowledged_records():
session = CodeSession(id="session", cwd="project", model="provider/model")
state = SessionState(session, None, (), (), ())
candidate, run, item = state.prepare_admission(
"send", "hello\nworld", session.model, None, "config"
)
assert (
candidate.title == "hello world" and candidate.revision == session.revision + 1
)
assert state.session == session and state.run is None
assert not state.items and not state.runs
admitted = run.model_copy(update={"ordinal": 7})
state.accept_admission(candidate, admitted, item)
assert state.run == admitted and state.runs[admitted.id] == admitted
assert state.items[item.id] == item and item.sequence == 1
assert state.busy and not state.pending_items
with pytest.raises(CodeConflictError, match="busy"):
state.prepare_admission("another", "next", session.model, None, "config")
def test_terminal_bundle_preserves_null_turn_prompt_and_requires_buffer_acknowledgment():
state = running_state()
for request_id, turn_id in ((1, "turn"), ("1", None)):
progress = state.prepare_prompt(
PromptRequest(request_id, "approval", {}, {}, turn_id), "generation"
)
state.apply_progress(progress)
before = state.session, state.run, tuple(state.prompts.values())
item, changed = state.stage_item(
ItemUpdate("turn", "answer", "text", text="partial"), state.run
)
assert changed
finish = state.prepare_finish("completed")
assert finish.items == (item,)
assert len(finish.prompts) == 1 and finish.prompts[0].request_id == 1
assert finish.prompts[0].status == "expired"
assert (state.session, state.run, tuple(state.prompts.values())) == before
assert state.pending_items == (item,)
state.apply_progress(finish)
assert state.run.status == "completed" and not state.busy
assert state.pending and state.pending_items == (item,)
assert (
next(
prompt for prompt in state.prompts.values() if prompt.request_id == "1"
).status
== "pending"
)
state.acknowledge_flush()
assert not state.pending_items and state.dirty_characters == 0
with pytest.raises(CodeConflictError, match="busy"):
state.check_can_send()
def test_recovery_updates_historical_run_without_replacing_current_run():
session = CodeSession(id="session", cwd="project", model="provider/model")
old = CodeRun(
id="old",
session_id=session.id,
ordinal=1,
text="old input",
model=session.model,
status="failed",
submission_started=True,
error="Connection lost",
finished_at=100,
)
current = CodeRun(
id="new",
session_id=session.id,
ordinal=2,
text="new input",
model=session.model,
)
state = SessionState(session, current, (), (), (old, current))
native = NativeThread(
"native",
(
NativeTurn(
"old-turn",
(ItemUpdate("old-turn", "user", "user", client_id="old"),),
error="Native failed",
error_details={"code": "native-error"},
),
),
)
((turn, recovered),) = state.match_history(native, (old, current))
assert turn.id == recovered.native_turn_id == "old-turn"
assert recovered.status == "failed" and recovered.finished_at == 100
assert recovered.error == "Connection lost"
assert recovered.error_details == {"code": "native-error"}
assert state.runs[old.id] == old
state.apply_progress(SessionProgress(session, run=recovered))
assert state.run == current and state.runs[old.id] == recovered
def test_historical_merge_retains_transcript_identity_and_completed_content():
base = running_state()
saved = CodeItem(
id="entry",
session_id=base.session.id,
run_id=base.run.id,
sequence=12,
native_turn_id="turn",
native_item_id="answer",
kind="text",
text="complete output",
detail="captured details",
complete=True,
raw={"nested": {"captured": "keep"}},
)
state = SessionState(base.session, base.run, (saved,), (), (base.run,))
update = ItemUpdate(
"turn",
"answer",
"text",
text="short",
raw={"nested": {"captured": "short", "new": "fill"}},
)
assert state.run is not None
merged, changed = state.stage_item(update, state.run, historical=True)
assert changed and merged.id == saved.id and merged.sequence == 12
assert (
merged.complete and merged.text == saved.text and merged.detail == saved.detail
)
assert merged.raw == {"nested": {"captured": "keep", "new": "fill"}}
assert saved.raw == {"nested": {"captured": "keep"}}
assert state.dirty_characters == 0 and state.pending_items == (merged,)
def test_review_liveness_can_change_without_changing_its_item_or_run():
state = running_state()
update = ItemUpdate(
"child-turn", "review", "subagent_auto_review", text="Reviewing"
)
item, changed = state.stage_review(update, "first")
assert changed and item.run_id == state.run.id
state.apply_progress(SessionProgress(state.session, items=state.pending_items))
state.acknowledge_flush()
assert state.clear_review_liveness()
state.apply_progress(state.prepare_finish("completed"))
candidate, run, message = state.prepare_admission(
"second", "next", state.session.model, None, "config"
)
state.accept_admission(candidate, run.model_copy(update={"ordinal": 2}), message)
repeated, changed = state.stage_review(update, "second")
assert repeated == item and not changed
assert state.active_review_ids("second") == (item.id,)
assert state.active_review_ids("first") == ()
assert state.pending_items == (item,)
def test_only_new_turn_started_can_bind_an_unacknowledged_submission():
base = running_state()
current = CodeRun(
id="new",
session_id=base.session.id,
ordinal=2,
text="next",
model=base.session.model,
submission_started=True,
)
state = SessionState(base.session, current, (), (), (base.run, current))
assert not state.matches_turn(
HarnessEvent("generation", "native", "turn_started", turn_id="turn"),
known_turn=True,
)
assert not state.matches_turn(
HarnessEvent("generation", "native", "item", turn_id="new-turn"),
known_turn=False,
)
assert state.matches_turn(
HarnessEvent("generation", "native", "turn_started", turn_id="new-turn"),
known_turn=False,
)
state.apply_progress(state.bind_turn("new-turn"))
assert state.matches_turn(
HarnessEvent("generation", "native", "item", turn_id="new-turn"),
known_turn=False,
)
assert not state.accepts_event(HarnessEvent("generation", "other-thread", "notice"))
|