Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ private static int findLatestUserInput(List<Msg> messages) {
Msg msg = messages.get(i);
if (msg.getRole() == MsgRole.USER
&& !SUMMARY_MSG_NAME.equals(msg.getName())
&& !Boolean.TRUE.equals(msg.getMetadata().get(Msg.METADATA_SYNTHETIC))
&& !Boolean.TRUE.equals(msg.getMetadata().get("synthetic"))) {
&& !isSyntheticMessage(msg)) {
return i;
}
}
Expand Down Expand Up @@ -632,15 +631,36 @@ static String formatMessagesForSummary(List<Msg> messages) {

private static final int SUMMARY_PREVIEW_CHARS = 600;

/**
* Metadata key that some in-harness producers use to mark a synthetic message. {@link
* io.agentscope.harness.agent.middleware.InboxMiddleware} and {@link
* io.agentscope.harness.agent.middleware.RepeatedFailureGuard} write this alias instead of
* {@link Msg#METADATA_SYNTHETIC}, and the rest of the harness reads both keys (see {@link
* io.agentscope.harness.agent.middleware.CompactionMiddleware} and {@link #findLatestUserInput}).
*/
private static final String METADATA_SYNTHETIC_ALIAS = "synthetic";

/**
* Whether {@code msg} is a framework-injected message rather than user speech.
*
* <p>Both the canonical {@link Msg#METADATA_SYNTHETIC} key and the {@code "synthetic"} alias
* are recognised, so a background event or reminder is never rendered as a {@code Human} turn.
*/
static boolean isSyntheticMessage(Msg msg) {
if (msg.getMetadata() == null) {
return false;
}
return Boolean.TRUE.equals(msg.getMetadata().get(Msg.METADATA_SYNTHETIC))
|| Boolean.TRUE.equals(msg.getMetadata().get(METADATA_SYNTHETIC_ALIAS));
}

private static String renderMessageForSummary(Msg msg) {
// Prior summaries and synthetic messages can carry USER role without being user
// speech, so label them distinctly in the transcript.
String roleLabel =
SUMMARY_MSG_NAME.equals(msg.getName())
? "PriorSummary"
: msg.getMetadata() != null
&& Boolean.TRUE.equals(
msg.getMetadata().get(Msg.METADATA_SYNTHETIC))
: isSyntheticMessage(msg)
? "FrameworkMessage"
: switch (msg.getRole()) {
case USER -> "Human";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,40 @@ void formatMessagesForSummaryLabelsSyntheticUserMessagesAsFrameworkMessages() {
"genuine user messages keep the Human label: " + formatted);
}

@Test
void formatMessagesForSummaryLabelsLegacySyntheticKeyAsFrameworkMessages() {
// InboxMiddleware and RepeatedFailureGuard mark their reminders with the "synthetic"
// alias instead of Msg.METADATA_SYNTHETIC, so the compactor must recognise both keys.
Msg backgroundEvent =
Msg.builder()
.role(MsgRole.USER)
.content(
TextBlock.builder()
.text(
"Automated background event from tool_output."
+ " This is context, not a user message or"
+ " approval.\n\nTool 'execute' running in"
+ " background has completed.")
.build())
.metadata(Map.of("synthetic", true, "reminderKind", "background_event"))
.build();

String formatted =
ConversationCompactor.formatMessagesForSummary(
List.of(backgroundEvent, message(MsgRole.USER, "real user speech")));

assertTrue(
formatted.startsWith("FrameworkMessage: Automated background event"),
"a message marked with the legacy synthetic key must not render as a Human turn: "
+ formatted);
assertFalse(
formatted.contains("\n\nHuman: Automated background event"),
"a background event may not masquerade as a user message: " + formatted);
assertTrue(
formatted.contains("\n\nHuman: real user speech"),
"genuine user messages keep the Human label: " + formatted);
}

@Test
void formatMessagesForSummaryEscapesLabelLinesInsideMessageBodies() {
Msg assistant =
Expand Down
Loading