kodelet-sdk 0.5.2__tar.gz → 0.5.4__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.
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/PKG-INFO +1 -1
- kodelet_sdk-0.5.4/VERSION.txt +1 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/__init__.py +4 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/__init__.py +4 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/session.py +43 -2
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/types.py +25 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/api.py +7 -1
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_agent.py +226 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_api.py +16 -3
- kodelet_sdk-0.5.2/VERSION.txt +0 -1
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/.github/workflows/ci.yml +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/.github/workflows/release.yml +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/.gitignore +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/AGENTS.md +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/LICENSE +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/Makefile +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/README.md +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/review/kodelet-extension-review +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/review/reviewer-prompt.md +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/sdk/README.md +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/sdk/basic-agent-session +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/sdk/inline-extension-session +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/sdk/streaming-agent-session +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/examples/workspace/kodelet-extension-workspace +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/pyproject.toml +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/_utils.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/client.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/relay.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/rpc.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/agent/transport.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/context.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/execution.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/py.typed +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/runtime.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/schemas.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/task_progress.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/template.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/src/kodelet_sdk/test_harness.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/fixtures/workspace_policy_extension.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_acp_transport.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_examples.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_execution.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_persistent_ui.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_runtime.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/tests/test_task_progress.py +0 -0
- {kodelet_sdk-0.5.2 → kodelet_sdk-0.5.4}/uv.lock +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.5.4
|
|
@@ -16,6 +16,8 @@ from .agent import (
|
|
|
16
16
|
BridgeTransport,
|
|
17
17
|
Client,
|
|
18
18
|
ClientOptions,
|
|
19
|
+
CompactionMarker,
|
|
20
|
+
ContextCompactedData,
|
|
19
21
|
CreateSessionOptions,
|
|
20
22
|
Profile,
|
|
21
23
|
ProfileInput,
|
|
@@ -162,6 +164,8 @@ __all__ = [
|
|
|
162
164
|
"CommandRespondResult",
|
|
163
165
|
"CommandResult",
|
|
164
166
|
"CommandRunAgentResult",
|
|
167
|
+
"CompactionMarker",
|
|
168
|
+
"ContextCompactedData",
|
|
165
169
|
"ConversationForkUnavailableError",
|
|
166
170
|
"CreateSessionOptions",
|
|
167
171
|
"EmptyEvent",
|
|
@@ -16,6 +16,8 @@ from .types import (
|
|
|
16
16
|
AssistantThinkingDeltaData,
|
|
17
17
|
BridgeTransport,
|
|
18
18
|
ClientOptions,
|
|
19
|
+
CompactionMarker,
|
|
20
|
+
ContextCompactedData,
|
|
19
21
|
CreateSessionOptions,
|
|
20
22
|
Profile,
|
|
21
23
|
ProfileInput,
|
|
@@ -41,6 +43,8 @@ __all__ = [
|
|
|
41
43
|
"BridgeTransport",
|
|
42
44
|
"Client",
|
|
43
45
|
"ClientOptions",
|
|
46
|
+
"CompactionMarker",
|
|
47
|
+
"ContextCompactedData",
|
|
44
48
|
"CreateSessionOptions",
|
|
45
49
|
"Profile",
|
|
46
50
|
"ProfileInput",
|
|
@@ -4,11 +4,18 @@ import asyncio
|
|
|
4
4
|
import inspect
|
|
5
5
|
import json
|
|
6
6
|
from collections.abc import Callable, Coroutine, Mapping, Sequence
|
|
7
|
-
from typing import TYPE_CHECKING, Any, TypeAlias, Unpack, cast
|
|
7
|
+
from typing import TYPE_CHECKING, Any, Literal, TypeAlias, Unpack, cast
|
|
8
8
|
|
|
9
9
|
from .._utils import AttrDict
|
|
10
10
|
from .rpc import ACPRPCClient
|
|
11
|
-
from .types import
|
|
11
|
+
from .types import (
|
|
12
|
+
AgentResponse,
|
|
13
|
+
AgentStreamEvent,
|
|
14
|
+
CompactionMarker,
|
|
15
|
+
ContextCompactedData,
|
|
16
|
+
RunOptions,
|
|
17
|
+
SessionSteerResult,
|
|
18
|
+
)
|
|
12
19
|
|
|
13
20
|
if TYPE_CHECKING:
|
|
14
21
|
from .client import Client
|
|
@@ -38,6 +45,7 @@ class Session:
|
|
|
38
45
|
self._running = False
|
|
39
46
|
self._listeners: dict[str, list[EventListener]] = {}
|
|
40
47
|
self._listener_tasks: set[asyncio.Task[Any]] = set()
|
|
48
|
+
self._seen_compactions: set[str] = set()
|
|
41
49
|
|
|
42
50
|
@property
|
|
43
51
|
def id(self) -> str:
|
|
@@ -186,6 +194,13 @@ class Session:
|
|
|
186
194
|
|
|
187
195
|
session_update = update.get("sessionUpdate")
|
|
188
196
|
if session_update == "agent_message_chunk":
|
|
197
|
+
compaction = _compaction_from_acp_update(update)
|
|
198
|
+
if compaction is not None:
|
|
199
|
+
marker_id = compaction["compaction"]["id"]
|
|
200
|
+
if marker_id not in self._seen_compactions:
|
|
201
|
+
self._seen_compactions.add(marker_id)
|
|
202
|
+
self._emit_sdk_event("context.compacted", compaction, events, update)
|
|
203
|
+
return
|
|
189
204
|
content = _text_from_acp_content(update.get("content"))
|
|
190
205
|
if content:
|
|
191
206
|
assistant_chunks.append(content)
|
|
@@ -302,6 +317,32 @@ class Session:
|
|
|
302
317
|
task.add_done_callback(self._listener_tasks.discard)
|
|
303
318
|
|
|
304
319
|
|
|
320
|
+
def _compaction_from_acp_update(update: Mapping[str, Any]) -> ContextCompactedData | None:
|
|
321
|
+
meta = update.get("_meta")
|
|
322
|
+
if not isinstance(meta, Mapping):
|
|
323
|
+
return None
|
|
324
|
+
data = meta.get("kodelet/contextCompacted")
|
|
325
|
+
if not isinstance(data, Mapping):
|
|
326
|
+
return None
|
|
327
|
+
marker = data.get("compaction")
|
|
328
|
+
if not isinstance(marker, Mapping):
|
|
329
|
+
return None
|
|
330
|
+
marker_id = _string_field(marker, "id")
|
|
331
|
+
method = _string_field(marker, "method")
|
|
332
|
+
created_at = _string_field(marker, "createdAt")
|
|
333
|
+
if not marker_id or (method != "api" and method != "summary") or created_at is None:
|
|
334
|
+
return None
|
|
335
|
+
compaction: CompactionMarker = {
|
|
336
|
+
"id": marker_id,
|
|
337
|
+
"method": cast(Literal["api", "summary"], method),
|
|
338
|
+
"createdAt": created_at,
|
|
339
|
+
}
|
|
340
|
+
summary = _string_field(marker, "summary")
|
|
341
|
+
if summary is not None:
|
|
342
|
+
compaction["summary"] = summary
|
|
343
|
+
return {"compaction": compaction, "beforeCurrentUser": data.get("beforeCurrentUser") is True}
|
|
344
|
+
|
|
345
|
+
|
|
305
346
|
def _normalize_run_options(
|
|
306
347
|
options: RunOptions | str | None,
|
|
307
348
|
kwargs: Mapping[str, Any],
|
|
@@ -150,6 +150,29 @@ class ToolResultData(TypedDict, total=False):
|
|
|
150
150
|
ToolUpdateData: TypeAlias = ToolResultData
|
|
151
151
|
|
|
152
152
|
|
|
153
|
+
class CompactionMarker(TypedDict):
|
|
154
|
+
"""Context compaction marker with a nonempty ID, method, and creation timestamp.
|
|
155
|
+
|
|
156
|
+
``summary`` is included only when the ACP metadata supplies a string.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
id: str
|
|
160
|
+
method: Literal["api", "summary"]
|
|
161
|
+
summary: NotRequired[str]
|
|
162
|
+
createdAt: str
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class ContextCompactedData(TypedDict):
|
|
166
|
+
"""Payload of a ``context.compacted`` session event.
|
|
167
|
+
|
|
168
|
+
``beforeCurrentUser`` places the marker before the current submitted user
|
|
169
|
+
message during pre-turn compaction; it defaults to false unless literally true.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
compaction: CompactionMarker
|
|
173
|
+
beforeCurrentUser: bool
|
|
174
|
+
|
|
175
|
+
|
|
153
176
|
class SpawnOptions(TypedDict, total=False):
|
|
154
177
|
cwd: str
|
|
155
178
|
env: Mapping[str, str]
|
|
@@ -258,6 +281,8 @@ __all__ = [
|
|
|
258
281
|
"BinaryWriter",
|
|
259
282
|
"BridgeTransport",
|
|
260
283
|
"ClientOptions",
|
|
284
|
+
"CompactionMarker",
|
|
285
|
+
"ContextCompactedData",
|
|
261
286
|
"CreateSessionOptions",
|
|
262
287
|
"DrainableBinaryWriter",
|
|
263
288
|
"Profile",
|
|
@@ -254,10 +254,16 @@ class UserMessageEvent(ExtensionEvent):
|
|
|
254
254
|
|
|
255
255
|
|
|
256
256
|
class AgentInitEvent(ExtensionEvent):
|
|
257
|
-
"""Event payload passed to ``agent.init`` handlers.
|
|
257
|
+
"""Event payload passed to ``agent.init`` handlers.
|
|
258
|
+
|
|
259
|
+
``allowedTools`` contains the current tool allowlist, including patches from
|
|
260
|
+
earlier host-dispatched init handlers. An empty list means no tools are allowed.
|
|
261
|
+
Older hosts may omit it; use ``event.get("allowedTools")`` for compatibility.
|
|
262
|
+
"""
|
|
258
263
|
|
|
259
264
|
event: Literal["agent.init"]
|
|
260
265
|
systemPrompt: str | None
|
|
266
|
+
allowedTools: list[str] | None
|
|
261
267
|
|
|
262
268
|
|
|
263
269
|
class TurnStartEvent(ExtensionEvent):
|
|
@@ -10,10 +10,13 @@ import pytest
|
|
|
10
10
|
|
|
11
11
|
import kodelet_sdk
|
|
12
12
|
from kodelet_sdk import (
|
|
13
|
+
AgentStreamEvent,
|
|
13
14
|
AgentUIHandlers,
|
|
14
15
|
BaseModel,
|
|
15
16
|
BridgeTransport,
|
|
16
17
|
Client,
|
|
18
|
+
CompactionMarker,
|
|
19
|
+
ContextCompactedData,
|
|
17
20
|
CreateSessionOptions,
|
|
18
21
|
ExecutionOptions,
|
|
19
22
|
Extension,
|
|
@@ -295,8 +298,12 @@ def test_agent_package_preserves_public_reexports() -> None:
|
|
|
295
298
|
assert agent.CreateSessionOptions is CreateSessionOptions
|
|
296
299
|
assert agent.AgentUIHandlers is AgentUIHandlers
|
|
297
300
|
assert agent.BridgeTransport is BridgeTransport
|
|
301
|
+
assert agent.CompactionMarker is CompactionMarker
|
|
302
|
+
assert agent.ContextCompactedData is ContextCompactedData
|
|
298
303
|
assert kodelet_sdk.Client is Client
|
|
299
304
|
assert kodelet_sdk.SessionSteerResult is SessionSteerResult
|
|
305
|
+
assert kodelet_sdk.CompactionMarker is CompactionMarker
|
|
306
|
+
assert kodelet_sdk.ContextCompactedData is ContextCompactedData
|
|
300
307
|
assert client_module.Client is Client
|
|
301
308
|
|
|
302
309
|
|
|
@@ -556,6 +563,225 @@ async def test_acp_close_reports_incomplete_cleanup_and_allows_session_retry() -
|
|
|
556
563
|
await client.close()
|
|
557
564
|
|
|
558
565
|
|
|
566
|
+
@pytest.mark.asyncio
|
|
567
|
+
async def test_session_emits_compaction_events_without_adding_notices_to_output() -> None:
|
|
568
|
+
markers: list[ContextCompactedData] = [
|
|
569
|
+
{
|
|
570
|
+
"compaction": {
|
|
571
|
+
"id": "compact-1", "method": "api", "createdAt": "2026-09-20T14:00:00Z",
|
|
572
|
+
},
|
|
573
|
+
"beforeCurrentUser": True,
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
"compaction": {
|
|
577
|
+
"id": "compact-2", "method": "summary", "summary": "Earlier decisions",
|
|
578
|
+
"createdAt": "2026-09-20T15:00:00Z",
|
|
579
|
+
},
|
|
580
|
+
"beforeCurrentUser": False,
|
|
581
|
+
},
|
|
582
|
+
]
|
|
583
|
+
updates = [
|
|
584
|
+
{
|
|
585
|
+
"sessionUpdate": "agent_message_chunk",
|
|
586
|
+
"content": {
|
|
587
|
+
"type": "text",
|
|
588
|
+
"text": f"\n\nContext compacted\n\n{data['compaction'].get('summary', '')}",
|
|
589
|
+
},
|
|
590
|
+
"_meta": {"kodelet/contextCompacted": data},
|
|
591
|
+
}
|
|
592
|
+
for data in markers
|
|
593
|
+
]
|
|
594
|
+
|
|
595
|
+
def on_prompt(_request: Mapping[str, Any], child: FakeACPProcess) -> None:
|
|
596
|
+
for update in [*updates, *updates]:
|
|
597
|
+
child.notify("session/update", {"sessionId": "conv-1", "update": update})
|
|
598
|
+
child.notify("session/update", {
|
|
599
|
+
"sessionId": "conv-1",
|
|
600
|
+
"update": {
|
|
601
|
+
"sessionUpdate": "agent_message_chunk",
|
|
602
|
+
"content": {"type": "text", "text": "The answer"},
|
|
603
|
+
},
|
|
604
|
+
})
|
|
605
|
+
|
|
606
|
+
client = Client(spawn=lambda *_: FakeACPProcess(on_prompt=on_prompt))
|
|
607
|
+
try:
|
|
608
|
+
session = await client.create_session()
|
|
609
|
+
received: list[ContextCompactedData] = []
|
|
610
|
+
deltas: list[str] = []
|
|
611
|
+
messages: list[str] = []
|
|
612
|
+
all_events: list[AgentStreamEvent] = []
|
|
613
|
+
session.on("context.compacted", lambda event: received.append(event.data))
|
|
614
|
+
session.on("assistant.message_delta", lambda event: deltas.append(event.data.deltaContent))
|
|
615
|
+
session.on("assistant.message", lambda event: messages.append(event.data.content))
|
|
616
|
+
session.on("event", all_events.append)
|
|
617
|
+
|
|
618
|
+
for run in range(2):
|
|
619
|
+
response = await session.run_and_wait(message="continue")
|
|
620
|
+
assert response.content == "The answer"
|
|
621
|
+
assert received == markers
|
|
622
|
+
assert deltas == ["The answer"] * (run + 1)
|
|
623
|
+
assert messages == ["The answer"] * (run + 1)
|
|
624
|
+
compactions = [event for event in response.events if event.type == "context.compacted"]
|
|
625
|
+
assert [event.data for event in compactions] == (markers if run == 0 else [])
|
|
626
|
+
assert [event.raw for event in compactions] == (updates if run == 0 else [])
|
|
627
|
+
assert all(event.conversationId == session.id for event in compactions)
|
|
628
|
+
assert all_events == response.events
|
|
629
|
+
all_events.clear()
|
|
630
|
+
|
|
631
|
+
# Marker IDs belong to a Session, not the client or process-global state.
|
|
632
|
+
other_session = await client.create_session()
|
|
633
|
+
other_response = await other_session.run_and_wait(message="continue")
|
|
634
|
+
assert [
|
|
635
|
+
event.data for event in other_response.events if event.type == "context.compacted"
|
|
636
|
+
] == markers
|
|
637
|
+
finally:
|
|
638
|
+
await client.close()
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
@pytest.mark.asyncio
|
|
642
|
+
async def test_compaction_only_output_is_empty_but_ordinary_notice_text_is_preserved() -> None:
|
|
643
|
+
prompt_count = 0
|
|
644
|
+
|
|
645
|
+
def on_prompt(_request: Mapping[str, Any], child: FakeACPProcess) -> None:
|
|
646
|
+
nonlocal prompt_count
|
|
647
|
+
prompt_count += 1
|
|
648
|
+
update: dict[str, Any] = {
|
|
649
|
+
"sessionUpdate": "agent_message_chunk",
|
|
650
|
+
"content": {"type": "text", "text": "Context compacted"},
|
|
651
|
+
}
|
|
652
|
+
if prompt_count <= 2:
|
|
653
|
+
update["_meta"] = {
|
|
654
|
+
"kodelet/contextCompacted": {
|
|
655
|
+
"compaction": {
|
|
656
|
+
"id": "compact-1", "method": "api", "createdAt": "2026-09-20T14:00:00Z",
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
}
|
|
660
|
+
child.notify("session/update", {"sessionId": "conv-1", "update": update})
|
|
661
|
+
|
|
662
|
+
process = FakeACPProcess(on_prompt=on_prompt)
|
|
663
|
+
client = Client(spawn=lambda *_: process)
|
|
664
|
+
try:
|
|
665
|
+
session = await client.create_session()
|
|
666
|
+
deltas: list[str] = []
|
|
667
|
+
messages: list[str] = []
|
|
668
|
+
session.on("assistant.message_delta", lambda event: deltas.append(event.data.deltaContent))
|
|
669
|
+
session.on("assistant.message", lambda event: messages.append(event.data.content))
|
|
670
|
+
for run in range(2):
|
|
671
|
+
response = await session.run_and_wait(message="compact")
|
|
672
|
+
assert response.content == ""
|
|
673
|
+
assert deltas == messages == []
|
|
674
|
+
assert not any(
|
|
675
|
+
event.type in {"assistant.message_delta", "assistant.message"}
|
|
676
|
+
for event in response.events
|
|
677
|
+
)
|
|
678
|
+
assert [
|
|
679
|
+
event.data.beforeCurrentUser
|
|
680
|
+
for event in response.events if event.type == "context.compacted"
|
|
681
|
+
] == ([False] if run == 0 else [])
|
|
682
|
+
|
|
683
|
+
response = await session.run_and_wait(message="say Context compacted")
|
|
684
|
+
assert response.content == "Context compacted"
|
|
685
|
+
assert deltas == messages == ["Context compacted"]
|
|
686
|
+
assert not any(event.type == "context.compacted" for event in response.events)
|
|
687
|
+
finally:
|
|
688
|
+
await client.close()
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
@pytest.mark.asyncio
|
|
692
|
+
@pytest.mark.parametrize(("before_current_user", "summary"), [
|
|
693
|
+
(True, ""), (False, "Earlier decisions"), (1, None), ("true", 123), (None, []), (0, {}),
|
|
694
|
+
])
|
|
695
|
+
async def test_session_normalizes_optional_compaction_metadata(
|
|
696
|
+
before_current_user: Any, summary: Any,
|
|
697
|
+
) -> None:
|
|
698
|
+
# TS accepts any nonempty ID and string timestamp without trimming or parsing.
|
|
699
|
+
marker: CompactionMarker = {"id": " ", "method": "summary", "createdAt": ""}
|
|
700
|
+
update = {
|
|
701
|
+
"sessionUpdate": "agent_message_chunk",
|
|
702
|
+
"content": {"type": "text", "text": "Context compacted"},
|
|
703
|
+
"_meta": {
|
|
704
|
+
"kodelet/contextCompacted": {
|
|
705
|
+
"compaction": {**marker, "summary": summary, "extra": "ignored"},
|
|
706
|
+
"beforeCurrentUser": before_current_user,
|
|
707
|
+
"extra": "ignored",
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
def on_prompt(_request: Mapping[str, Any], child: FakeACPProcess) -> None:
|
|
713
|
+
child.notify("session/update", {"sessionId": "conv-1", "update": update})
|
|
714
|
+
|
|
715
|
+
client = Client(spawn=lambda *_: FakeACPProcess(on_prompt=on_prompt))
|
|
716
|
+
try:
|
|
717
|
+
session = await client.create_session()
|
|
718
|
+
response = await session.run_and_wait(message="compact")
|
|
719
|
+
if isinstance(summary, str):
|
|
720
|
+
marker["summary"] = summary
|
|
721
|
+
assert response.content == ""
|
|
722
|
+
assert [
|
|
723
|
+
event.data for event in response.events if event.type == "context.compacted"
|
|
724
|
+
] == [{"compaction": marker, "beforeCurrentUser": before_current_user is True}]
|
|
725
|
+
finally:
|
|
726
|
+
await client.close()
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
@pytest.mark.asyncio
|
|
730
|
+
@pytest.mark.parametrize("meta", [
|
|
731
|
+
None,
|
|
732
|
+
"invalid",
|
|
733
|
+
[],
|
|
734
|
+
{},
|
|
735
|
+
{"kodelet/contextCompacted": None},
|
|
736
|
+
{"kodelet/contextCompacted": []},
|
|
737
|
+
{"kodelet/contextCompacted": {}},
|
|
738
|
+
*[
|
|
739
|
+
{"kodelet/contextCompacted": {"compaction": marker}}
|
|
740
|
+
for marker in (
|
|
741
|
+
None, [], {},
|
|
742
|
+
{"method": "api", "createdAt": "2026-09-20T14:00:00Z"},
|
|
743
|
+
{"id": "compact-1", "createdAt": "2026-09-20T14:00:00Z"},
|
|
744
|
+
{"id": "compact-1", "method": "api"},
|
|
745
|
+
)
|
|
746
|
+
],
|
|
747
|
+
*[
|
|
748
|
+
{"kodelet/contextCompacted": {"compaction": {
|
|
749
|
+
"id": "compact-1", "method": "api", "createdAt": "2026-09-20T14:00:00Z",
|
|
750
|
+
key: value,
|
|
751
|
+
}}}
|
|
752
|
+
for key, value in (
|
|
753
|
+
("id", ""), ("id", 1), ("id", None), ("method", "unknown"),
|
|
754
|
+
("method", []), ("createdAt", None), ("createdAt", 1),
|
|
755
|
+
)
|
|
756
|
+
],
|
|
757
|
+
])
|
|
758
|
+
async def test_session_keeps_malformed_compaction_metadata_as_answer_text(meta: Any) -> None:
|
|
759
|
+
update = {
|
|
760
|
+
"sessionUpdate": "agent_message_chunk",
|
|
761
|
+
"content": {"type": "text", "text": "Context compacted"},
|
|
762
|
+
"_meta": meta,
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
def on_prompt(_request: Mapping[str, Any], child: FakeACPProcess) -> None:
|
|
766
|
+
child.notify("session/update", {"sessionId": "conv-1", "update": update})
|
|
767
|
+
|
|
768
|
+
client = Client(spawn=lambda *_: FakeACPProcess(on_prompt=on_prompt))
|
|
769
|
+
try:
|
|
770
|
+
session = await client.create_session()
|
|
771
|
+
deltas: list[str] = []
|
|
772
|
+
messages: list[str] = []
|
|
773
|
+
session.on("assistant.message_delta", lambda event: deltas.append(event.data.deltaContent))
|
|
774
|
+
session.on("assistant.message", lambda event: messages.append(event.data.content))
|
|
775
|
+
response = await session.run_and_wait(message="continue")
|
|
776
|
+
assert response.content == "Context compacted"
|
|
777
|
+
assert deltas == messages == ["Context compacted"]
|
|
778
|
+
assert not any(event.type == "context.compacted" for event in response.events)
|
|
779
|
+
delta = next(event for event in response.events if event.type == "assistant.message_delta")
|
|
780
|
+
assert delta.raw == update
|
|
781
|
+
finally:
|
|
782
|
+
await client.close()
|
|
783
|
+
|
|
784
|
+
|
|
559
785
|
@pytest.mark.asyncio
|
|
560
786
|
@pytest.mark.parametrize("as_mapping", [False, True], ids=["kwargs", "mapping"])
|
|
561
787
|
@pytest.mark.parametrize(
|
|
@@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, assert_type
|
|
|
11
11
|
import pytest
|
|
12
12
|
|
|
13
13
|
from kodelet_sdk import (
|
|
14
|
+
AgentInitEvent,
|
|
14
15
|
BackgroundTaskLease,
|
|
15
16
|
BaseModel,
|
|
16
17
|
CommandContext,
|
|
@@ -883,12 +884,24 @@ def test_tool_context_runner_identity_comes_from_initialize_metadata() -> None:
|
|
|
883
884
|
|
|
884
885
|
|
|
885
886
|
@pytest.mark.asyncio
|
|
886
|
-
|
|
887
|
+
@pytest.mark.parametrize(
|
|
888
|
+
"tool_payload",
|
|
889
|
+
[{"allowedTools": ["bash", "subagent"]}, {"allowedTools": []}, {"allowedTools": None}, {}],
|
|
890
|
+
)
|
|
891
|
+
async def test_agent_init_can_disable_tools_using_invoked_by_context(
|
|
892
|
+
tool_payload: dict[str, list[str] | None],
|
|
893
|
+
) -> None:
|
|
887
894
|
ext = Extension()
|
|
888
895
|
|
|
889
896
|
@ext.on("agent.init")
|
|
890
|
-
async def disable_recursive_tool(
|
|
897
|
+
async def disable_recursive_tool(event: AgentInitEvent, ctx: EventContext) -> EventResult:
|
|
891
898
|
assert ctx.invoked_by == "subagent"
|
|
899
|
+
assert event.get("allowedTools") == tool_payload.get("allowedTools")
|
|
900
|
+
if tool_payload.get("allowedTools") is not None:
|
|
901
|
+
assert_type(event.allowedTools, list[str] | None)
|
|
902
|
+
assert event.allowedTools == tool_payload["allowedTools"]
|
|
903
|
+
else:
|
|
904
|
+
assert "allowedTools" not in event
|
|
892
905
|
return {"tools": {"disable": ["subagent"]}}
|
|
893
906
|
|
|
894
907
|
harness = await create_test_harness(ext)
|
|
@@ -896,7 +909,7 @@ async def test_agent_init_can_disable_tools_using_invoked_by_context() -> None:
|
|
|
896
909
|
{
|
|
897
910
|
"id": "evt-agent-init",
|
|
898
911
|
"event": "agent.init",
|
|
899
|
-
"payload": {"systemPrompt": "base"},
|
|
912
|
+
"payload": {"systemPrompt": "base", **tool_payload},
|
|
900
913
|
"context": {"invokedBy": "subagent"},
|
|
901
914
|
}
|
|
902
915
|
)
|
kodelet_sdk-0.5.2/VERSION.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.5.2
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|