Skip to content

Commit c32625d

Browse files
Merge remote-tracking branch 'origin/main' into codex/chat-metadata-canonical-snapshot
2 parents 0e476d5 + 75ef5ae commit c32625d

3 files changed

Lines changed: 175 additions & 62 deletions

File tree

‎extensions/slack/src/monitor/message-handler/dispatch-progress.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ export function createSlackProgressRuntime(runtimeParams: {
324324
seed: progressSeed,
325325
formatLine: formatSlackProgressDraftLine,
326326
reasoningLinePrefix: "🧠 ",
327-
commentaryLinePrefix: "💬 ",
327+
commentaryLinePrefix: "",
328328
reasoningGate: previewToolProgressEnabled,
329-
commentaryItalics: false,
329+
commentaryItalics: true,
330330
buildProgressEventLine: (input, options) =>
331331
input.event === "tool" || input.event === "item"
332332
? buildChannelProgressDraftLineForEntry(account.config, input, options)
@@ -658,5 +658,29 @@ export function createSlackProgressRuntime(runtimeParams: {
658658
}
659659

660660
function formatSlackProgressDraftLine(line: string): string {
661-
return /^(?:🧠|💬)\s/u.test(line) ? line : escapeSlackMrkdwn(line);
661+
if (/^(?:🧠|💬)\s/u.test(line)) {
662+
return line;
663+
}
664+
665+
const italicCommentary = /^_(.*)_$/su.exec(line);
666+
if (!italicCommentary) {
667+
return escapeSlackMrkdwn(line);
668+
}
669+
670+
const content = italicCommentary[1]!
671+
.split(/(`[^`\n]+`)/u)
672+
.map((segment, index) => {
673+
if (index % 2 === 0) {
674+
return escapeSlackMrkdwn(segment);
675+
}
676+
const code = segment
677+
.slice(1, -1)
678+
.replaceAll("&", "&")
679+
.replaceAll("<", "&lt;")
680+
.replaceAll(">", "&gt;");
681+
return `\`${code}\``;
682+
})
683+
.join("");
684+
685+
return `_${content}_`;
662686
}

‎extensions/slack/src/monitor/message-handler/dispatch.preview-fallback.test.ts‎

Lines changed: 144 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,9 +2149,9 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
21492149
expectMockCallArgFields(finalizeSlackPreviewEditMock, 0, "preview edit params", {
21502150
channelId: "C123",
21512151
messageId: "171234.567",
2152-
text: expect.stringMatching(/⏱️ \d+s$/),
2152+
text: FINAL_REPLY_TEXT,
21532153
});
2154-
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
2154+
expect(deliverRepliesMock).not.toHaveBeenCalled();
21552155
expect(draftStream.clear).not.toHaveBeenCalled();
21562156
});
21572157

@@ -2245,10 +2245,10 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
22452245
],
22462246
});
22472247
expect(finalizeSlackPreviewEditMock).toHaveBeenCalledTimes(1);
2248-
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
2248+
expect(deliverRepliesMock).not.toHaveBeenCalled();
22492249
});
22502250

2251-
it("sends a progress final fresh before collapsing its draft to a receipt", async () => {
2251+
it("replaces the progress draft with the final answer without posting a receipt", async () => {
22522252
const draftStream = createDraftStreamStub();
22532253
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
22542254
finalizeSlackPreviewEditMock.mockResolvedValueOnce(undefined);
@@ -2272,20 +2272,55 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
22722272
}),
22732273
);
22742274

2275-
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
2276-
expectDeliverReplyCall(0, FINAL_REPLY_TEXT);
2277-
expectMockCallArgFields(finalizeSlackPreviewEditMock, 0, "progress receipt edit", {
2275+
expect(deliverRepliesMock).not.toHaveBeenCalled();
2276+
expectMockCallArgFields(finalizeSlackPreviewEditMock, 0, "progress final edit", {
22782277
channelId: "C123",
22792278
messageId: "171234.567",
2280-
text: expect.stringMatching(/^🛠️ 1 tool call · ⏱️ \d+s$/),
2279+
text: FINAL_REPLY_TEXT,
22812280
});
2282-
expect(deliverRepliesMock.mock.invocationCallOrder[0]).toBeLessThan(
2283-
finalizeSlackPreviewEditMock.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
2284-
);
2281+
expect(finalizeSlackPreviewEditMock).toHaveBeenCalledTimes(1);
22852282
expect(draftStream.clear).not.toHaveBeenCalled();
22862283
});
22872284

2288-
it("leaves a progress draft untouched when the fresh final send fails", async () => {
2285+
it.each([
2286+
{ description: "plain text", finalText: "x".repeat(4001) },
2287+
{ description: "multibyte text", finalText: "é".repeat(2001) },
2288+
])(
2289+
"delivers oversized $description intact through the normal chunked sender",
2290+
async ({ finalText }) => {
2291+
const draftStream = createDraftStreamStub();
2292+
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
2293+
mockedSlackStreamingMode = "progress";
2294+
mockedSlackDraftMode = "status_final";
2295+
mockedDispatchSequence = [{ kind: "final", payload: { text: finalText } }];
2296+
mockedReplyOptionEvents = [
2297+
{
2298+
kind: "item",
2299+
itemKind: "preamble",
2300+
itemId: "preamble-1",
2301+
progressText: "Checking the full answer before replying.",
2302+
},
2303+
];
2304+
2305+
await dispatchPreparedSlackMessage(
2306+
createPreparedSlackMessage({
2307+
accountConfig: {
2308+
streaming: {
2309+
mode: "progress",
2310+
progress: { label: false, commentary: true, toolProgress: false },
2311+
},
2312+
},
2313+
}),
2314+
);
2315+
2316+
expect(finalizeSlackPreviewEditMock).not.toHaveBeenCalled();
2317+
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
2318+
expectDeliverReplyCall(0, finalText);
2319+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2320+
},
2321+
);
2322+
2323+
it("retains the progress draft when both the final edit and fallback send fail", async () => {
22892324
const draftStream = createDraftStreamStub();
22902325
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
22912326
deliverRepliesMock.mockRejectedValueOnce(new Error("final send failed"));
@@ -2303,11 +2338,14 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
23032338
).rejects.toThrow("final send failed");
23042339

23052340
expect(draftStream.update).toHaveBeenCalled();
2306-
expect(finalizeSlackPreviewEditMock).not.toHaveBeenCalled();
2341+
expectMockCallArgFields(finalizeSlackPreviewEditMock, 0, "progress final edit", {
2342+
messageId: "171234.567",
2343+
text: FINAL_REPLY_TEXT,
2344+
});
23072345
expect(draftStream.clear).not.toHaveBeenCalled();
23082346
});
23092347

2310-
it("keeps a progress draft for an error final without creating a receipt", async () => {
2348+
it("replaces a progress draft with an error final without creating a receipt", async () => {
23112349
const draftStream = createDraftStreamStub();
23122350
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
23132351
mockedSlackStreamingMode = "progress";
@@ -2323,7 +2361,7 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
23232361

23242362
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
23252363
expect(finalizeSlackPreviewEditMock).not.toHaveBeenCalled();
2326-
expect(draftStream.clear).not.toHaveBeenCalled();
2364+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
23272365
});
23282366

23292367
it("mandatory E2E: streams native Slack progress with the newest meaningful plan title when no explicit label exists", async () => {
@@ -3532,7 +3570,7 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
35323570
],
35333571
});
35343572
expect(finalizeSlackPreviewEditMock).toHaveBeenCalledTimes(1);
3535-
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
3573+
expect(deliverRepliesMock).not.toHaveBeenCalled();
35363574
});
35373575

35383576
it("preserves text Slack progress lines after a draft boundary status update", async () => {
@@ -3736,7 +3774,7 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
37363774

37373775
expect(capturedReplyOptions?.commentaryProgressEnabled).toBe(true);
37383776
expect(capturedReplyOptions?.suppressDefaultToolProgressMessages).toBe(true);
3739-
expect(draftStream.update).toHaveBeenLastCalledWith("💬 Preparing the smallest fix");
3777+
expect(draftStream.update).toHaveBeenLastCalledWith("_Preparing the smallest fix_");
37403778
expect(draftStream.update.mock.calls.flat().join("\n")).not.toContain("pnpm test");
37413779

37423780
const updateCount = draftStream.update.mock.calls.length;
@@ -3776,8 +3814,94 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
37763814
);
37773815

37783816
expect(draftStream.update).toHaveBeenLastCalledWith(
3779-
"💬 I’m using the `monorepo` skill on Linux x86_64.",
3817+
"_I’m using the `monorepo` skill on Linux x86\\_64._",
3818+
);
3819+
});
3820+
3821+
it("escapes Slack mentions and formatting in commentary without losing outer italics or inline code", async () => {
3822+
const draftStream = createDraftStreamStub();
3823+
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
3824+
mockedSlackStreamingMode = "progress";
3825+
mockedSlackDraftMode = "status_final";
3826+
mockedDispatchSequence = [];
3827+
mockedReplyOptionEvents = [
3828+
{
3829+
kind: "item",
3830+
itemKind: "preamble",
3831+
itemId: "preamble-1",
3832+
progressText:
3833+
"checking <@U123> in <#C123> and <!channel> with *urgent* _context_ `src/one.ts`",
3834+
},
3835+
];
3836+
3837+
await dispatchPreparedSlackMessage(
3838+
createPreparedSlackMessage({
3839+
accountConfig: {
3840+
streaming: {
3841+
mode: "progress",
3842+
progress: { label: false, commentary: true, toolProgress: false },
3843+
},
3844+
},
3845+
}),
3846+
);
3847+
3848+
expect(draftStream.update).toHaveBeenLastCalledWith(
3849+
"_checking &lt;@U123&gt; in &lt;#C123&gt; and &lt;!channel&gt; with \\*urgent\\* \\_context\\_ `src/one.ts`_",
3850+
);
3851+
});
3852+
3853+
it("keeps the full latest preamble and turns the same Slack message into the final answer", async () => {
3854+
const draftStream = createDraftStreamStub();
3855+
createSlackDraftStreamMock.mockReturnValueOnce(draftStream);
3856+
finalizeSlackPreviewEditMock.mockResolvedValueOnce(undefined);
3857+
mockedSlackStreamingMode = "progress";
3858+
mockedSlackDraftMode = "status_final";
3859+
mockedDispatchSequence = [{ kind: "final", payload: { text: FINAL_REPLY_TEXT } }];
3860+
const firstPreamble = "Checking the previous conversation before replying.";
3861+
const latestPreamble =
3862+
"I found the earlier decision and am checking the owner, the current rollout, and the original feedback before deciding what would actually be useful here.";
3863+
mockedReplyOptionEvents = [
3864+
{
3865+
kind: "item",
3866+
itemKind: "preamble",
3867+
itemId: "preamble-1",
3868+
progressText: firstPreamble,
3869+
},
3870+
{
3871+
kind: "item",
3872+
itemKind: "preamble",
3873+
itemId: "preamble-2",
3874+
progressText: latestPreamble,
3875+
},
3876+
];
3877+
3878+
await dispatchPreparedSlackMessage(
3879+
createPreparedSlackMessage({
3880+
accountConfig: {
3881+
streaming: {
3882+
mode: "progress",
3883+
progress: {
3884+
label: false,
3885+
commentary: true,
3886+
toolProgress: false,
3887+
maxLines: 1,
3888+
maxLineChars: 4000,
3889+
},
3890+
},
3891+
},
3892+
}),
37803893
);
3894+
3895+
expect(draftStream.update).toHaveBeenCalledWith(`_${firstPreamble}_`);
3896+
expect(draftStream.update).toHaveBeenLastCalledWith(`_${latestPreamble}_`);
3897+
expect(finalizeSlackPreviewEditMock).toHaveBeenCalledTimes(1);
3898+
expectMockCallArgFields(finalizeSlackPreviewEditMock, 0, "progress final edit", {
3899+
channelId: "C123",
3900+
messageId: "171234.567",
3901+
text: FINAL_REPLY_TEXT,
3902+
});
3903+
expect(deliverRepliesMock).not.toHaveBeenCalled();
3904+
expect(draftStream.update.mock.calls.flat().join("\n")).not.toMatch(/Working|💬|•|⏱️/u);
37813905
});
37823906

37833907
it("uses the enterprise event client for Slack commentary drafts", async () => {
@@ -3827,7 +3951,7 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
38273951
expect(createSlackDraftStreamMock).toHaveBeenCalledWith(
38283952
expect.objectContaining({ eventScope }),
38293953
);
3830-
expect(draftStream.update).toHaveBeenLastCalledWith("💬 Using the scoped listener client");
3954+
expect(draftStream.update).toHaveBeenLastCalledWith("_Using the scoped listener client_");
38313955
});
38323956

38333957
it("renders the latest Slack preamble as the status headline by default", async () => {
@@ -4025,7 +4149,7 @@ describe("dispatchPreparedSlackMessage preview fallback", () => {
40254149
],
40264150
});
40274151
expect(finalizeSlackPreviewEditMock).toHaveBeenCalledTimes(1);
4028-
expect(deliverRepliesMock).toHaveBeenCalledTimes(1);
4152+
expect(deliverRepliesMock).not.toHaveBeenCalled();
40294153
});
40304154

40314155
it("suppresses standalone Slack tool progress when partial preview lines are disabled", async () => {

‎extensions/slack/src/monitor/message-handler/dispatch.ts‎

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -110,45 +110,6 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
110110
}
111111
return;
112112
}
113-
114-
if (hadProgressDraft) {
115-
// Best-effort settle of the working draft; a flush failure must never
116-
// suppress the fresh final send below.
117-
try {
118-
await draftStream?.flush();
119-
} catch (err) {
120-
logVerbose(`slack: progress draft flush before final failed (${formatSlackError(err)})`);
121-
}
122-
}
123-
const receiptChannelId = hadProgressDraft ? draftStream?.channelId() : undefined;
124-
const receiptMessageId = hadProgressDraft ? draftStream?.messageId() : undefined;
125-
// The draft already selected the reply thread; re-planning here could
126-
// route the fresh final elsewhere under stateful replyToMode values.
127-
const draftThreadTs = hadProgressDraft
128-
? (delivery.usedReplyThreadTs ?? statusThreadTs)
129-
: undefined;
130-
await delivery.deliverNormally({
131-
payload,
132-
kind: info.kind,
133-
...(draftThreadTs ? { forcedThreadTs: draftThreadTs } : {}),
134-
});
135-
progress.progressDraft.markFinalReplyDelivered();
136-
if (
137-
!payload.isError &&
138-
receiptChannelId &&
139-
receiptMessageId &&
140-
!progress.progressReceiptCollapsed
141-
) {
142-
// Collapse only after the fresh final lands; a failed send leaves the
143-
// working draft untouched as the turn record.
144-
await progress.collapseProgressReceipt({
145-
channelId: receiptChannelId,
146-
messageId: receiptMessageId,
147-
text: progress.progressReceipt.buildSummaryLine(),
148-
threadTs: delivery.usedReplyThreadTs ?? statusThreadTs,
149-
});
150-
}
151-
return;
152113
}
153114
if (progress.useNativeProgressStreaming) {
154115
await delivery.deliverNormally({
@@ -259,6 +220,7 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
259220
forcedThreadTs: finalThreadTs,
260221
});
261222
delivery.markPreviewPayloadDelivered({ kind: info.kind, payload, threadTs: finalThreadTs });
223+
progress.progressDraft.markFinalReplyDelivered();
262224
return;
263225
}
264226
}
@@ -377,6 +339,9 @@ export async function dispatchPreparedSlackMessage(prepared: PreparedSlackMessag
377339
});
378340
},
379341
});
342+
if (info.kind === "final") {
343+
progress.progressDraft.markFinalReplyDelivered();
344+
}
380345
};
381346
let dispatchError: unknown;
382347
let queuedFinal = false;

0 commit comments

Comments
 (0)