Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ksadk/kernel/worker_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ksadk.conversations.runtime_persistence import ensure_conversation_session
from ksadk.kernel.contracts import AgentControlCommand
from ksadk.sessions.agent_identity import session_agent_ids
from ksadk.sessions.invocation_identity import identity_native_user_id


Expand Down Expand Up @@ -44,12 +45,12 @@ async def prepare_worker_identity(
)
if existing is not None:
# Kernel ingress has authorized this exact session/instance. Tenant
# is its authority scope, not the owner of a persisted local session.
# is its authority scope, not the owner of the persisted session.
# Only the host-owned record supplies the storage user, never payload.
agent_id = str(defaults.get("agent_id") or command.agent_instance_id)
if existing.agent_id != agent_id or not existing.user_id:
allowed_agent_ids = session_agent_ids(fallback_agent_id)
if existing.agent_id not in allowed_agent_ids or not existing.user_id:
raise ValueError("prepared session Agent differs from the Kernel Build")
return existing.user_id, None, fallback_agent_id
return existing.user_id, None, existing.agent_id
return effective_user_id, None, fallback_agent_id
if session_service is None:
return identity_native_user_id(invocation_identity), invocation_identity, fallback_agent_id
Expand Down
9 changes: 6 additions & 3 deletions ksadk/runtime/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import os
from typing import Any

from ksadk.plugins.providers.codex_native import (
Expand Down Expand Up @@ -93,9 +94,11 @@ def kernel_start_request_defaults(context: RuntimeLaunchContext) -> dict[str, An

config = dict(context.config)
defaults: dict[str, Any] = {}
detection_name = str(getattr(context.detection, "name", "") or "").strip()
if detection_name:
defaults["agent_id"] = detection_name
agent_id = os.getenv("AGENT_RUNTIME_ID", "").strip() or str(
getattr(context.detection, "name", "") or ""
).strip()
if agent_id:
defaults["agent_id"] = agent_id
model = str(config.get("model") or "").strip()
if model:
defaults["model"] = model
Expand Down
42 changes: 42 additions & 0 deletions tests/kernel/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import asyncio
from types import SimpleNamespace

import pytest

Expand Down Expand Up @@ -93,6 +94,47 @@ async def test_worker_preserves_admitted_command_identity_on_run_transition():
assert {event.causation_id for event in transitions} == {str(submitted.command_id)}


@pytest.mark.parametrize(
"session_agent_id",
[
pytest.param("ar-platform-agent", id="platform-agent"),
pytest.param(AGENT, id="historical-instance"),
],
)
async def test_worker_continues_hosted_scheduled_session(session_agent_id, monkeypatch, tmp_path):
from ksadk.kernel.worker import AgentKernelWorker
from ksadk.runtime.factory import kernel_start_request_defaults
from ksadk.runtime.launch import RuntimeLaunchContext

monkeypatch.setenv("AGENT_RUNTIME_ID", "ar-platform-agent")
monkeypatch.setenv("AGENT_INSTANCE_ID", AGENT)
launch = RuntimeLaunchContext(
runtime_type="langgraph",
project_dir=tmp_path,
detection=SimpleNamespace(name="source-project"),
)
stack = await kernel_stack(sessions=())
await stack.session_service.create_session(session_agent_id, "session-owner", session_id="s1")
submitted = command(payload={"content": "write report"}).model_copy(
update={"source": ControlSource(kind="scheduler", ref="occ_12345678")},
)
await stack.kernel.submit(submitted, permit=stack.permit("enqueue"))
worker = AgentKernelWorker(
stack.store,
adapter_factory=lambda: stack.adapter,
session_service=stack.session_service,
start_request_defaults=kernel_start_request_defaults(launch),
)

assert (await worker.run_once(AGENT, await stack.lease())).outcome == "completed"
[request] = stack.adapter.start_requests
assert (request.session_id, request.agent_id, request.user_id) == (
"s1",
session_agent_id,
"session-owner",
)


@pytest.mark.parametrize("runtime_type", ["adk", "fake"])
async def test_worker_persists_adk_start_before_stream_without_public_disclosure(runtime_type):
from ksadk.kernel.worker import AgentKernelWorker
Expand Down