Python: Updated instructions/system_message logic in GitHub Copilot agent (#3625)

* Updated instructions handling

* Small improvement

* Included runtime options in session creation logic
This commit is contained in:
Dmytro Struk
2026-02-02 21:40:35 -08:00
committed by GitHub
Unverified
parent 98cd72839e
commit 73033c300f
9 changed files with 206 additions and 65 deletions
@@ -135,12 +135,52 @@ class TestGitHubCopilotAgentInit:
agent = GitHubCopilotAgent(tools=[my_tool])
assert len(agent._tools) == 1 # type: ignore
def test_init_with_instructions(self) -> None:
"""Test initialization with custom instructions."""
def test_init_with_instructions_parameter(self) -> None:
"""Test initialization with instructions parameter."""
agent = GitHubCopilotAgent(instructions="You are a helpful assistant.")
assert agent._default_options.get("system_message") == { # type: ignore
"mode": "append",
"content": "You are a helpful assistant.",
}
def test_init_with_system_message_in_default_options(self) -> None:
"""Test initialization with system_message object in default_options."""
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
default_options={"instructions": "You are a helpful assistant."}
default_options={"system_message": {"mode": "append", "content": "You are a helpful assistant."}}
)
assert agent._instructions == "You are a helpful assistant." # type: ignore
assert agent._default_options.get("system_message") == { # type: ignore
"mode": "append",
"content": "You are a helpful assistant.",
}
def test_init_with_system_message_replace_mode(self) -> None:
"""Test initialization with system_message in replace mode."""
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
default_options={"system_message": {"mode": "replace", "content": "Custom system prompt."}}
)
assert agent._default_options.get("system_message") == { # type: ignore
"mode": "replace",
"content": "Custom system prompt.",
}
def test_instructions_parameter_takes_precedence_for_content(self) -> None:
"""Test that direct instructions parameter takes precedence for content but preserves mode."""
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
instructions="Direct instructions",
default_options={"system_message": {"mode": "replace", "content": "Options system_message"}},
)
assert agent._default_options.get("system_message") == { # type: ignore
"mode": "replace",
"content": "Direct instructions",
}
def test_instructions_parameter_defaults_to_append_mode(self) -> None:
"""Test that instructions parameter defaults to append mode when no system_message provided."""
agent = GitHubCopilotAgent(instructions="Direct instructions")
assert agent._default_options.get("system_message") == { # type: ignore
"mode": "append",
"content": "Direct instructions",
}
class TestGitHubCopilotAgentLifecycle:
@@ -462,10 +502,10 @@ class TestGitHubCopilotAgentSessionManagement:
mock_client: MagicMock,
mock_session: MagicMock,
) -> None:
"""Test that session config includes instructions."""
agent: GitHubCopilotAgent[GitHubCopilotOptions] = GitHubCopilotAgent(
"""Test that session config includes instructions from direct parameter."""
agent = GitHubCopilotAgent(
instructions="You are a helpful assistant.",
client=mock_client,
default_options={"instructions": "You are a helpful assistant."},
)
await agent.start()
@@ -476,6 +516,31 @@ class TestGitHubCopilotAgentSessionManagement:
assert config["system_message"]["mode"] == "append"
assert config["system_message"]["content"] == "You are a helpful assistant."
async def test_runtime_options_take_precedence_over_default(
self,
mock_client: MagicMock,
mock_session: MagicMock,
) -> None:
"""Test that runtime options from run() take precedence over default_options."""
agent = GitHubCopilotAgent(
instructions="Default instructions",
client=mock_client,
)
await agent.start()
runtime_options: GitHubCopilotOptions = {
"system_message": {"mode": "replace", "content": "Runtime instructions"}
}
await agent._get_or_create_session( # type: ignore
AgentThread(),
runtime_options=runtime_options,
)
call_args = mock_client.create_session.call_args
config = call_args[0][0]
assert config["system_message"]["mode"] == "replace"
assert config["system_message"]["content"] == "Runtime instructions"
async def test_session_config_includes_streaming_flag(
self,
mock_client: MagicMock,