typeclaw 0.45.0 → 0.45.1
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.
- package/package.json +2 -2
- package/src/agent/index.ts +31 -3
- package/src/agent/loop-guard.ts +208 -10
- package/src/agent/plugin-tools.ts +59 -5
- package/src/agent/provider-error.ts +10 -3
- package/src/agent/retry-same-ref.ts +66 -0
- package/src/agent/subagents.ts +28 -2
- package/src/agent/tools/channel-reply.ts +15 -1
- package/src/agent/tools/channel-send.ts +17 -1
- package/src/agent/turn-runner.ts +23 -9
- package/src/bundled-plugins/reviewer/skills/code-review.ts +5 -0
- package/src/channels/adapters/discord-bot.ts +25 -4
- package/src/channels/adapters/kakaotalk-channel-resolver.ts +10 -1
- package/src/channels/adapters/kakaotalk-typing.ts +125 -0
- package/src/channels/adapters/kakaotalk.ts +19 -0
- package/src/channels/router.ts +320 -6
- package/src/run/llm-fetch-observer.ts +147 -9
package/src/channels/router.ts
CHANGED
|
@@ -130,6 +130,10 @@ export const CONTEXT_BUFFER_SIZE = 20
|
|
|
130
130
|
export const OBSERVED_MESSAGE_MAX_CHARS = 800
|
|
131
131
|
// Discord's typing indicator expires after ~10s; an 8s heartbeat keeps it
|
|
132
132
|
// continuously visible while we debounce + generate without spamming the API.
|
|
133
|
+
// This is the default; an adapter whose platform expires the indicator sooner
|
|
134
|
+
// registers a shorter interval via `setTypingHeartbeatInterval` (e.g. KakaoTalk
|
|
135
|
+
// auto-expires after ~5s) so the router itself paces the refresh — the adapter
|
|
136
|
+
// callback stays stateless instead of running its own timer.
|
|
133
137
|
export const TYPING_HEARTBEAT_MS = 8000
|
|
134
138
|
// A stuck model call or an agent that never yields should not keep re-arming
|
|
135
139
|
// platform-side typing forever. Slack Assistant status in particular has a
|
|
@@ -785,6 +789,19 @@ type LiveSession = {
|
|
|
785
789
|
// origin so `channel_react` reacts to the triggering message, not whichever
|
|
786
790
|
// inbound happens to be latest in the queue. Null on reminder-only turns.
|
|
787
791
|
currentTurnReactionRef: ReactionRef | null
|
|
792
|
+
// True when the inbound that triggered THIS turn (the message
|
|
793
|
+
// `currentTurnReactionRef` points at — last item of the drained batch) was
|
|
794
|
+
// EXPLICITLY addressed to the bot: a DM, an @-mention/alias (`isBotMention`
|
|
795
|
+
// folds plain-name matching in at the adapter classify layer), or a reply to
|
|
796
|
+
// the bot's own message. Gates the PERSISTENT silent-ack :eyes: (see
|
|
797
|
+
// `armSilentTurnAck`): a deliberate silence on a message aimed AT us earns a
|
|
798
|
+
// courteous "seen, nothing to add" 👀, but staying quiet during ambient
|
|
799
|
+
// human-to-human chatter (sticky observation, solo-human fallback) must leave
|
|
800
|
+
// NO mark — otherwise a busy room accumulates stale 👀 on messages the bot
|
|
801
|
+
// was never part of. Computed from the SAME message the reaction targets so
|
|
802
|
+
// eligibility and target can never disagree. Reset with `currentTurnReactionRef`
|
|
803
|
+
// in the drain finally; preserved across reminder-only iterations exactly like it.
|
|
804
|
+
currentTurnExplicitlyAddressed: boolean
|
|
788
805
|
// Typing-status anchor of the inbound that triggered THIS turn (last item in
|
|
789
806
|
// the drained batch, mirroring `currentTurnReactionRef`). Adapter-opaque ts
|
|
790
807
|
// carried only to the typing path; null when the triggering inbound supplied
|
|
@@ -1185,6 +1202,11 @@ export type ChannelRouter = {
|
|
|
1185
1202
|
// the wrong signal. autoReactOnEngage reads this to post :eyes: only as a
|
|
1186
1203
|
// fallback when no visible typing exists. Unset defaults to false.
|
|
1187
1204
|
setTypingCapability: (adapter: ChannelKey['adapter'], supported: boolean) => void
|
|
1205
|
+
// Override the typing heartbeat interval for one adapter. Adapters whose
|
|
1206
|
+
// platform expires the indicator faster than the default TYPING_HEARTBEAT_MS
|
|
1207
|
+
// register a shorter interval here so the router paces their refresh; the
|
|
1208
|
+
// adapter callback stays stateless. Unset adapters use TYPING_HEARTBEAT_MS.
|
|
1209
|
+
setTypingHeartbeatInterval: (adapter: ChannelKey['adapter'], intervalMs: number) => void
|
|
1188
1210
|
// Set by the manager for every adapter present in typeclaw.json#channels,
|
|
1189
1211
|
// independent of whether the adapter's start() succeeded (a failed login never
|
|
1190
1212
|
// registers callbacks). Combined with the adapter's static read-capability set
|
|
@@ -1369,6 +1391,7 @@ export type ChannelRouter = {
|
|
|
1369
1391
|
typingEpoch: (key: ChannelKey) => number | undefined
|
|
1370
1392
|
isTypingActive: (key: ChannelKey) => boolean
|
|
1371
1393
|
stopTyping: (key: ChannelKey) => Promise<void>
|
|
1394
|
+
typingHeartbeatIntervalFor: (adapter: ChannelKey['adapter']) => number
|
|
1372
1395
|
runIdleGc: () => Promise<void>
|
|
1373
1396
|
// Returns the seeded author state on the live session matching
|
|
1374
1397
|
// `key`, or undefined when no live session exists. Tests use this
|
|
@@ -1418,6 +1441,10 @@ export type CreateChannelRouterOptions = {
|
|
|
1418
1441
|
logger?: RouterLogger
|
|
1419
1442
|
// Test seam: clock for sticky/debounce/participants. Defaults to Date.now.
|
|
1420
1443
|
now?: () => number
|
|
1444
|
+
// Test seams for synchronizing strict post-tool retry backoff without global
|
|
1445
|
+
// random/timer mutation. Production defaults remain Math.random and no hook.
|
|
1446
|
+
retryRandom?: () => number
|
|
1447
|
+
onRetryBackoffStart?: () => void
|
|
1421
1448
|
// Test seam: measure a transcript file's byte size for the soft-TTL grace
|
|
1422
1449
|
// decision. Defaults to a stat()-based reader returning 0 for a missing or
|
|
1423
1450
|
// unreadable file (grace then fails closed to roll-over-at-soft-TTL).
|
|
@@ -1569,6 +1596,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
1569
1596
|
const removeReactionCallbacks = new Map<ChannelKey['adapter'], Set<RemoveReactionCallback>>()
|
|
1570
1597
|
const typingCallbacks = new Map<ChannelKey['adapter'], Set<TypingCallback>>()
|
|
1571
1598
|
const typingCapableAdapters = new Set<ChannelKey['adapter']>()
|
|
1599
|
+
const typingHeartbeatIntervals = new Map<ChannelKey['adapter'], number>()
|
|
1572
1600
|
const configuredAdapters = new Set<ChannelKey['adapter']>()
|
|
1573
1601
|
const channelNameResolvers = new Map<ChannelKey['adapter'], Set<ChannelNameResolver>>()
|
|
1574
1602
|
const membershipResolvers = new Map<ChannelKey['adapter'], Set<MembershipResolver>>()
|
|
@@ -2093,6 +2121,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2093
2121
|
currentTurnAuthorId: null,
|
|
2094
2122
|
currentTurnAuthorIds: new Set(),
|
|
2095
2123
|
currentTurnReactionRef: null,
|
|
2124
|
+
currentTurnExplicitlyAddressed: false,
|
|
2096
2125
|
currentTurnTypingThread: null,
|
|
2097
2126
|
dirtyTypingThreads: new Set(),
|
|
2098
2127
|
currentTurnEngageReactions: [],
|
|
@@ -2580,7 +2609,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2580
2609
|
return
|
|
2581
2610
|
}
|
|
2582
2611
|
void fireTyping(live, 'tick', epoch)
|
|
2583
|
-
},
|
|
2612
|
+
}, typingHeartbeatIntervalFor(live.key.adapter))
|
|
2584
2613
|
}
|
|
2585
2614
|
|
|
2586
2615
|
const stopTypingHeartbeat = async (live: LiveSession): Promise<void> => {
|
|
@@ -2728,6 +2757,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2728
2757
|
live.firstUnprocessedAt = 0
|
|
2729
2758
|
live.promptQueue.length = 0
|
|
2730
2759
|
live.pendingSystemReminders.length = 0
|
|
2760
|
+
live.continueReplyTurn = null
|
|
2731
2761
|
await stopTypingHeartbeat(live)
|
|
2732
2762
|
try {
|
|
2733
2763
|
await live.session.abort()
|
|
@@ -2745,9 +2775,18 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2745
2775
|
const hasStoppableWork = (live: LiveSession): boolean =>
|
|
2746
2776
|
live.draining || live.promptQueue.length > 0 || live.pendingSystemReminders.length > 0
|
|
2747
2777
|
|
|
2778
|
+
const hasPendingContinueReply = (live: LiveSession): boolean => {
|
|
2779
|
+
const progressReply = live.continueReplyTurn
|
|
2780
|
+
return (
|
|
2781
|
+
progressReply !== null &&
|
|
2782
|
+
progressReply.turnSeq === live.turnSeq &&
|
|
2783
|
+
progressReply.sendCount === live.successfulChannelSends
|
|
2784
|
+
)
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2748
2787
|
const maybePostDeferredProviderError = async (
|
|
2749
2788
|
live: LiveSession,
|
|
2750
|
-
|
|
2789
|
+
completedReplyThisTurn: boolean,
|
|
2751
2790
|
retryQueued: boolean,
|
|
2752
2791
|
): Promise<void> => {
|
|
2753
2792
|
const pending = live.pendingProviderError
|
|
@@ -2757,7 +2796,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2757
2796
|
}
|
|
2758
2797
|
// The turn recovered and replied — the provider blip was transient, so a
|
|
2759
2798
|
// failure notice would be a false alarm stranded above the real reply.
|
|
2760
|
-
if (
|
|
2799
|
+
if (completedReplyThisTurn) {
|
|
2761
2800
|
live.pendingProviderError = null
|
|
2762
2801
|
return
|
|
2763
2802
|
}
|
|
@@ -2838,6 +2877,9 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2838
2877
|
live.currentTurnAuthorId = batch[batch.length - 1]!.authorId
|
|
2839
2878
|
live.currentTurnAuthorIds = new Set(batch.map((m) => m.authorId))
|
|
2840
2879
|
live.currentTurnReactionRef = batch[batch.length - 1]!.reactionRef ?? null
|
|
2880
|
+
const trigger = batch[batch.length - 1]!
|
|
2881
|
+
live.currentTurnExplicitlyAddressed =
|
|
2882
|
+
trigger.isDm || trigger.isBotMention || trigger.replyToBotMessageId !== null
|
|
2841
2883
|
live.currentTurnTypingThread = batch[batch.length - 1]!.typingThread ?? null
|
|
2842
2884
|
live.currentTurnEngageReactions = batch.flatMap((m) =>
|
|
2843
2885
|
m.engageReaction !== undefined ? [m.engageReaction] : [],
|
|
@@ -2908,6 +2950,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2908
2950
|
const successfulSendsBeforePrompt = live.successfulChannelSends
|
|
2909
2951
|
const emptyTurnRetriesBeforePrompt = live.emptyTurnRetries
|
|
2910
2952
|
const toolLeakRetriesBeforePrompt = live.toolLeakRetries
|
|
2953
|
+
const willingnessNudgesBeforePrompt = live.willingnessNudges
|
|
2911
2954
|
const engageAddPromises = live.currentTurnEngageReactions
|
|
2912
2955
|
live.turnSeq++
|
|
2913
2956
|
live.successfulSendsAtTurnStart = successfulSendsBeforePrompt
|
|
@@ -2938,6 +2981,9 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2938
2981
|
session: live.session,
|
|
2939
2982
|
text: promptText,
|
|
2940
2983
|
shouldFailover: (err) => isFailoverWorthy(err.message),
|
|
2984
|
+
authorizeRetryAfterCompletedToolResult: () => hasPendingContinueReply(live),
|
|
2985
|
+
...(options.retryRandom !== undefined ? { retryRandom: options.retryRandom } : {}),
|
|
2986
|
+
...(options.onRetryBackoffStart !== undefined ? { onRetryBackoffStart: options.onRetryBackoffStart } : {}),
|
|
2941
2987
|
setModelForRef: async (ref) => {
|
|
2942
2988
|
await live.session.setModel(applyModelRuntimeOverrides(resolveModel(ref), ref))
|
|
2943
2989
|
live.activeModelRef = ref
|
|
@@ -2970,7 +3016,9 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
2970
3016
|
// tool-leak retry that later replies must commit the signal AT the
|
|
2971
3017
|
// successful retry, not clear it after the first suppressed attempt.
|
|
2972
3018
|
const retryQueuedThisTurn =
|
|
2973
|
-
live.emptyTurnRetries > emptyTurnRetriesBeforePrompt ||
|
|
3019
|
+
live.emptyTurnRetries > emptyTurnRetriesBeforePrompt ||
|
|
3020
|
+
live.toolLeakRetries > toolLeakRetriesBeforePrompt ||
|
|
3021
|
+
live.willingnessNudges > willingnessNudgesBeforePrompt
|
|
2974
3022
|
const providerErrorThisTurn = assistantLeafStopReason(live.session) === 'error'
|
|
2975
3023
|
const fallbackPostedThisTurn = live.emptyTurnFallbackTurn === live.turnSeq
|
|
2976
3024
|
const sentReplyThisTurn = live.successfulChannelSends > successfulSendsBeforePrompt
|
|
@@ -3037,8 +3085,14 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
3037
3085
|
// Either retry budget keeps the turn in flight, so a deferred provider
|
|
3038
3086
|
// error must wait for the reminder-only iteration that actually ends it.
|
|
3039
3087
|
const retryQueuedThisTurn =
|
|
3040
|
-
live.emptyTurnRetries > emptyTurnRetriesBeforePrompt ||
|
|
3041
|
-
|
|
3088
|
+
live.emptyTurnRetries > emptyTurnRetriesBeforePrompt ||
|
|
3089
|
+
live.toolLeakRetries > toolLeakRetriesBeforePrompt ||
|
|
3090
|
+
live.willingnessNudges > willingnessNudgesBeforePrompt
|
|
3091
|
+
await maybePostDeferredProviderError(
|
|
3092
|
+
live,
|
|
3093
|
+
sentReplyThisTurn && !hasPendingContinueReply(live),
|
|
3094
|
+
retryQueuedThisTurn,
|
|
3095
|
+
)
|
|
3042
3096
|
await fireSessionTurnEnd(live)
|
|
3043
3097
|
}
|
|
3044
3098
|
await fireSessionIdle(live)
|
|
@@ -3054,6 +3108,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
3054
3108
|
live.currentTurnAuthorId = null
|
|
3055
3109
|
live.currentTurnAuthorIds = new Set()
|
|
3056
3110
|
live.currentTurnReactionRef = null
|
|
3111
|
+
live.currentTurnExplicitlyAddressed = false
|
|
3057
3112
|
live.currentTurnEngageReactions = []
|
|
3058
3113
|
// Drop any still-held reactions if the loop exited without running the
|
|
3059
3114
|
// per-turn finally (e.g. session destroyed mid-drain): never leave a
|
|
@@ -3750,6 +3805,13 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
3750
3805
|
else typingCapableAdapters.delete(adapter)
|
|
3751
3806
|
}
|
|
3752
3807
|
|
|
3808
|
+
const setTypingHeartbeatInterval = (adapter: ChannelKey['adapter'], intervalMs: number): void => {
|
|
3809
|
+
typingHeartbeatIntervals.set(adapter, intervalMs)
|
|
3810
|
+
}
|
|
3811
|
+
|
|
3812
|
+
const typingHeartbeatIntervalFor = (adapter: ChannelKey['adapter']): number =>
|
|
3813
|
+
typingHeartbeatIntervals.get(adapter) ?? TYPING_HEARTBEAT_MS
|
|
3814
|
+
|
|
3753
3815
|
const setAdapterConfigured = (adapter: ChannelKey['adapter'], configured: boolean): void => {
|
|
3754
3816
|
if (configured) configuredAdapters.add(adapter)
|
|
3755
3817
|
else configuredAdapters.delete(adapter)
|
|
@@ -4758,6 +4820,21 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
4758
4820
|
return
|
|
4759
4821
|
}
|
|
4760
4822
|
|
|
4823
|
+
// Prose-then-trailing-call leak: the model wrote a real reply and then
|
|
4824
|
+
// serialized a tool decision as a trailing block (the Discord skip_response
|
|
4825
|
+
// incident). Strip the plumbing and keep the prose BEFORE the whole-message
|
|
4826
|
+
// classification below, so a message that was ONLY the call still reaches the
|
|
4827
|
+
// existing name-dependent suppress/recover path, while a message with real
|
|
4828
|
+
// prose keeps that prose. Prefix-first: no arg-recovery, no nudge, no ack.
|
|
4829
|
+
const trailingLeak = stripTrailingLeakedToolCall(assistantText)
|
|
4830
|
+
if (trailingLeak !== null && trailingLeak.text !== '') {
|
|
4831
|
+
logger.warn(
|
|
4832
|
+
`[channels] ${live.keyId}: stripped trailing_tool_call_leak tool=${trailingLeak.toolName} ` +
|
|
4833
|
+
`text_len=${trailingLeak.text.length}`,
|
|
4834
|
+
)
|
|
4835
|
+
assistantText = trailingLeak.text
|
|
4836
|
+
}
|
|
4837
|
+
|
|
4761
4838
|
if (isUpstreamEmptyResponseSentinel(assistantText)) {
|
|
4762
4839
|
logger.warn(
|
|
4763
4840
|
`[channels] ${live.keyId}: suppressed upstream_empty_response_sentinel text_len=${assistantText.length}`,
|
|
@@ -5487,6 +5564,14 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
5487
5564
|
}
|
|
5488
5565
|
|
|
5489
5566
|
const armSilentTurnAck = (live: LiveSession, reason: SilentAckReason): void => {
|
|
5567
|
+
// A GitHub review IS its own emoji-shaped output, so it always earns the
|
|
5568
|
+
// 👀; every other deliberate silence earns one ONLY when the triggering
|
|
5569
|
+
// message was addressed to the bot. Staying quiet in ambient chatter leaves
|
|
5570
|
+
// no mark, so a busy room never accumulates stale 👀 on messages the bot
|
|
5571
|
+
// was never part of.
|
|
5572
|
+
const eligible =
|
|
5573
|
+
reason === 'github_review_output' ? live.key.adapter === 'github' : live.currentTurnExplicitlyAddressed
|
|
5574
|
+
if (!eligible) return
|
|
5490
5575
|
live.silentAckTurn = { turnSeq: live.turnSeq, reason }
|
|
5491
5576
|
}
|
|
5492
5577
|
|
|
@@ -5579,6 +5664,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
5579
5664
|
registerTyping,
|
|
5580
5665
|
unregisterTyping,
|
|
5581
5666
|
setTypingCapability,
|
|
5667
|
+
setTypingHeartbeatInterval,
|
|
5582
5668
|
setAdapterConfigured,
|
|
5583
5669
|
registerChannelNameResolver,
|
|
5584
5670
|
unregisterChannelNameResolver,
|
|
@@ -5676,6 +5762,7 @@ export function createChannelRouter(options: CreateChannelRouterOptions): Channe
|
|
|
5676
5762
|
if (!live) return
|
|
5677
5763
|
await stopTypingHeartbeat(live)
|
|
5678
5764
|
},
|
|
5765
|
+
typingHeartbeatIntervalFor,
|
|
5679
5766
|
runIdleGc,
|
|
5680
5767
|
getLiveOriginSnapshot: (key: ChannelKey) => {
|
|
5681
5768
|
const live = liveSessions.get(channelKeyId(key))
|
|
@@ -6866,6 +6953,233 @@ export function isWholeMessageToolCall(text: string): string | null {
|
|
|
6866
6953
|
return null
|
|
6867
6954
|
}
|
|
6868
6955
|
|
|
6956
|
+
export type TrailingToolCallLeak = { text: string; toolName: string; leakedCall: string }
|
|
6957
|
+
|
|
6958
|
+
// Catches the sibling leak that `isWholeMessageToolCall` cannot: the model wrote
|
|
6959
|
+
// a REAL reply, then serialized a tool decision as a trailing block instead of
|
|
6960
|
+
// emitting a real tool call. The production incident (Discord) was Korean prose,
|
|
6961
|
+
// a blank line, then `skip_response({ reason: "..." })` — the whole-message
|
|
6962
|
+
// parser saw prose before the call, returned null, and the plumbing shipped
|
|
6963
|
+
// verbatim, confirming to probing users that it's a bot and exposing an internal
|
|
6964
|
+
// tool name.
|
|
6965
|
+
//
|
|
6966
|
+
// The whole-message contract of `isWholeMessageToolCall` is deliberately NOT
|
|
6967
|
+
// broadened: it stays "the entire message is a call" so a name-dependent
|
|
6968
|
+
// disposition (reply/send recover, skip silent, else nudge) still applies to a
|
|
6969
|
+
// message that is nothing but a call. This is the STRICTLY narrower sibling —
|
|
6970
|
+
// "prose, THEN a trailing standalone call block" — with a prefix-first
|
|
6971
|
+
// disposition: the prose is a legitimate reply the user must see, so we post it
|
|
6972
|
+
// and drop only the trailing plumbing, regardless of which tool leaked. No
|
|
6973
|
+
// arg-recovery, no nudge, no silent-turn ack — the prose already satisfied the
|
|
6974
|
+
// turn.
|
|
6975
|
+
//
|
|
6976
|
+
// The false-positive surface is contained by three STRUCTURAL (language-agnostic,
|
|
6977
|
+
// per the repo's multi-language rule) signals, none of which interpret prose:
|
|
6978
|
+
// 1. a BLANK line must separate prefix from the trailing block — an adjacent
|
|
6979
|
+
// final line (`Explanation:\nskip_response()`) is left untouched, since a
|
|
6980
|
+
// model teaching a tool inline never blank-line-separates the example;
|
|
6981
|
+
// 2. the trailing block must independently satisfy `isWholeMessageToolCall`
|
|
6982
|
+
// (bracket-aware, quote-honoring) — a bare `channel_react whenever…` word or
|
|
6983
|
+
// `read({...}) loads a file` mid-sentence is not a whole call;
|
|
6984
|
+
// 3. the block must NOT begin inside a Markdown fenced code block — a fenced
|
|
6985
|
+
// ``` example ending in a call is legitimate teaching output.
|
|
6986
|
+
// The prefix must stay non-empty after trimming, else this is the whole-message
|
|
6987
|
+
// case and belongs to the existing parser.
|
|
6988
|
+
//
|
|
6989
|
+
// Multiple contiguous trailing call blocks are stripped iteratively (each must
|
|
6990
|
+
// independently pass the predicate); the loop stops at the first non-call block.
|
|
6991
|
+
export function stripTrailingLeakedToolCall(text: string): TrailingToolCallLeak | null {
|
|
6992
|
+
const normalized = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
|
|
6993
|
+
let body = normalized
|
|
6994
|
+
let outerToolName: string | null = null
|
|
6995
|
+
let outerLeakedCall = ''
|
|
6996
|
+
|
|
6997
|
+
for (;;) {
|
|
6998
|
+
// Locate the final blank-line-separated block: <prefix>\n[ \t]*\n<candidate>,
|
|
6999
|
+
// where <candidate> is the trailing run with no interior blank line.
|
|
7000
|
+
const match = /\n[ \t]*\n([^\n]*(?:\n(?![ \t]*\n)[^\n]*)*)[ \t\n]*$/.exec(body)
|
|
7001
|
+
if (match === null) break
|
|
7002
|
+
const rawCandidate = match[1]!
|
|
7003
|
+
const candidate = rawCandidate.trim()
|
|
7004
|
+
if (candidate === '') break
|
|
7005
|
+
const prefix = body.slice(0, match.index)
|
|
7006
|
+
if (prefix.trim() === '') break
|
|
7007
|
+
|
|
7008
|
+
// Classify BEFORE the fence check: a whole-message tool call can never itself
|
|
7009
|
+
// be a fence marker, which is what lets the fence scanner treat the
|
|
7010
|
+
// candidate's first line purely as a container-continuation signal.
|
|
7011
|
+
const toolName = isWholeMessageToolCall(candidate)
|
|
7012
|
+
if (toolName === null) break
|
|
7013
|
+
|
|
7014
|
+
// The candidate's own first line decides whether it still continues an open
|
|
7015
|
+
// list/blockquote container: an unquoted or dedented candidate has LEFT the
|
|
7016
|
+
// container, so a container-owned fence no longer protects it.
|
|
7017
|
+
const candidateFirstLine = rawCandidate.split('\n', 1)[0]!
|
|
7018
|
+
if (startsInsideFencedCodeBlock(prefix, candidateFirstLine)) break
|
|
7019
|
+
|
|
7020
|
+
if (outerToolName === null) {
|
|
7021
|
+
outerToolName = toolName
|
|
7022
|
+
outerLeakedCall = candidate
|
|
7023
|
+
}
|
|
7024
|
+
body = prefix
|
|
7025
|
+
}
|
|
7026
|
+
|
|
7027
|
+
if (outerToolName === null) return null
|
|
7028
|
+
return { text: body.trimEnd(), toolName: outerToolName, leakedCall: outerLeakedCall }
|
|
7029
|
+
}
|
|
7030
|
+
|
|
7031
|
+
// A fence carries the container requirements it was opened under. Both are
|
|
7032
|
+
// independent: a `> - ``` ` opener requires BOTH a blockquote (`requiresQuote`)
|
|
7033
|
+
// and the list content column, so leaving EITHER container abandons the fence.
|
|
7034
|
+
// This captures at most one blockquote requirement + one post-blockquote list
|
|
7035
|
+
// column — a bounded stack, not an arbitrary container parser.
|
|
7036
|
+
type FenceOwner = { requiresQuote: boolean; listContentColumn: number | null }
|
|
7037
|
+
type OpenFence = { char: '`' | '~'; len: number; owner: FenceOwner }
|
|
7038
|
+
|
|
7039
|
+
// Best-effort fenced-code detection for the trailing-tool-call safety guard.
|
|
7040
|
+
// Returns true when the candidate (whose first raw line is `candidateFirstLine`)
|
|
7041
|
+
// sits inside a still-open Markdown fence — meaning the trailing call is fenced
|
|
7042
|
+
// example content and must NOT be stripped.
|
|
7043
|
+
//
|
|
7044
|
+
// Top-level and blockquote-prefixed fences use normal opener/closer bookkeeping;
|
|
7045
|
+
// list-marker-prefixed fences are recognized as OPENERS only. An open
|
|
7046
|
+
// blockquote-owned fence is abandoned by an unquoted non-blank line, and an open
|
|
7047
|
+
// list-owned fence by a non-blank line indented before the marker's recorded
|
|
7048
|
+
// content column (see `abandonIfLeftContainer`). The candidate's OWN first line is
|
|
7049
|
+
// run through the same abandon check last: an unquoted/dedented candidate has left
|
|
7050
|
+
// its container, so a container-owned fence no longer protects it and the call is
|
|
7051
|
+
// stripped. Blank lines never abandon; top-level fences are never abandoned, so a
|
|
7052
|
+
// column-zero candidate under an ordinary open top-level fence stays protected.
|
|
7053
|
+
//
|
|
7054
|
+
// This deliberately approximates container continuity: a fence tracks at most one
|
|
7055
|
+
// blockquote requirement plus one post-blockquote list content column (so a
|
|
7056
|
+
// `> - ``` ` fence requires BOTH), while paragraph lazy continuation, exact
|
|
7057
|
+
// CommonMark list padding, DEEPER nested stacks (list-in-list, quote-in-list-in-
|
|
7058
|
+
// quote), and container re-entry are out of scope. The safety bias is toward "in a
|
|
7059
|
+
// fence" (a missed close leaves a legitimate example unstripped, safer than
|
|
7060
|
+
// stripping it), bounded by the container-exit rules so a real leak that has left
|
|
7061
|
+
// its container is still caught.
|
|
7062
|
+
function startsInsideFencedCodeBlock(prefix: string, candidateFirstLine: string): boolean {
|
|
7063
|
+
let open: OpenFence | null = null
|
|
7064
|
+
// The list item currently in effect (measured post-blockquote-strip), so a
|
|
7065
|
+
// fence that opens on a list item's CONTINUATION line — `- item`\n` ``` `,
|
|
7066
|
+
// not the `- ``` ` marker line — inherits the item's content column and is
|
|
7067
|
+
// abandoned when a later line dedents out of it. Tracks its own quote
|
|
7068
|
+
// requirement so an unquoted line can't inherit a list started inside `> …`.
|
|
7069
|
+
let activeListItem: { contentColumn: number; requiresQuote: boolean } | null = null
|
|
7070
|
+
|
|
7071
|
+
for (const rawLine of prefix.split('\n')) {
|
|
7072
|
+
open = abandonIfLeftContainer(open, rawLine)
|
|
7073
|
+
const { line, quoted } = stripBlockquoteMarkers(rawLine)
|
|
7074
|
+
|
|
7075
|
+
// Maintain the active list item only outside a fence (fenced content must not
|
|
7076
|
+
// create list state): a marker line replaces it; a non-blank line that leaves
|
|
7077
|
+
// its quote context or dedents before its content column expires it; blank
|
|
7078
|
+
// lines preserve it.
|
|
7079
|
+
if (open === null) {
|
|
7080
|
+
const listItem = /^( {0,3})((?:[-+*]|\d{1,9}[.)])[ \t]+)/.exec(line)
|
|
7081
|
+
if (listItem !== null) {
|
|
7082
|
+
activeListItem = { contentColumn: columnWidth(listItem[1]! + listItem[2]!), requiresQuote: quoted }
|
|
7083
|
+
} else if (
|
|
7084
|
+
line.trim() !== '' &&
|
|
7085
|
+
activeListItem !== null &&
|
|
7086
|
+
(activeListItem.requiresQuote !== quoted || leadingIndentColumns(line) < activeListItem.contentColumn)
|
|
7087
|
+
) {
|
|
7088
|
+
activeListItem = null
|
|
7089
|
+
}
|
|
7090
|
+
}
|
|
7091
|
+
|
|
7092
|
+
const plainFence = /^( {0,3})(`{3,}|~{3,})([^\n]*)$/.exec(line)
|
|
7093
|
+
if (plainFence !== null) {
|
|
7094
|
+
const indent = plainFence[1]!
|
|
7095
|
+
const run = plainFence[2]!
|
|
7096
|
+
const char = run[0] as '`' | '~'
|
|
7097
|
+
const rest = plainFence[3]!
|
|
7098
|
+
if (open === null) {
|
|
7099
|
+
if (char === '`' && rest.includes('`')) continue
|
|
7100
|
+
// A plain opener indented into the active list item inherits its content
|
|
7101
|
+
// column (continuation-line fence); a genuinely top-level opener keeps null.
|
|
7102
|
+
const listContentColumn =
|
|
7103
|
+
activeListItem !== null &&
|
|
7104
|
+
activeListItem.requiresQuote === quoted &&
|
|
7105
|
+
columnWidth(indent) >= activeListItem.contentColumn
|
|
7106
|
+
? activeListItem.contentColumn
|
|
7107
|
+
: null
|
|
7108
|
+
open = { char, len: run.length, owner: { requiresQuote: quoted, listContentColumn } }
|
|
7109
|
+
} else if (char === open.char && run.length >= open.len && rest.trim() === '') {
|
|
7110
|
+
open = null
|
|
7111
|
+
}
|
|
7112
|
+
continue
|
|
7113
|
+
}
|
|
7114
|
+
|
|
7115
|
+
// A list-marker-prefixed fence only ever OPENS; inside an open fence it is
|
|
7116
|
+
// fenced content, not a closer. It records BOTH requirements it was opened
|
|
7117
|
+
// under: the blockquote (`> - ``` `) and the list content column — the full
|
|
7118
|
+
// width of the marker prefix (`- `, `1. `, nested chains, post-quote-strip),
|
|
7119
|
+
// tabs expanded — so a later line that leaves EITHER has left the item.
|
|
7120
|
+
if (open !== null) continue
|
|
7121
|
+
const listFence = /^((?: {0,3}(?:[-+*]|\d{1,9}[.)])[ \t]+)+) {0,3}(`{3,}|~{3,})([^\n]*)$/.exec(line)
|
|
7122
|
+
if (listFence === null) continue
|
|
7123
|
+
const markerPrefix = listFence[1]!
|
|
7124
|
+
const run = listFence[2]!
|
|
7125
|
+
const char = run[0] as '`' | '~'
|
|
7126
|
+
const rest = listFence[3]!
|
|
7127
|
+
if (char === '`' && rest.includes('`')) continue
|
|
7128
|
+
open = { char, len: run.length, owner: { requiresQuote: quoted, listContentColumn: columnWidth(markerPrefix) } }
|
|
7129
|
+
}
|
|
7130
|
+
// The candidate's own first line is the final container-continuation signal: an
|
|
7131
|
+
// unquoted (blockquote) or dedented (list) candidate has left the container, so
|
|
7132
|
+
// a container-owned fence no longer protects it. The candidate is never itself a
|
|
7133
|
+
// fence marker (it already passed isWholeMessageToolCall), so only the abandon
|
|
7134
|
+
// check applies here — no fence open/close bookkeeping.
|
|
7135
|
+
return abandonIfLeftContainer(open, candidateFirstLine) !== null
|
|
7136
|
+
}
|
|
7137
|
+
|
|
7138
|
+
// Returns the fence with its owning container(s) still intact, or null when the
|
|
7139
|
+
// given raw line has LEFT any required container: a fence that requires a
|
|
7140
|
+
// blockquote is abandoned by an unquoted non-blank line, and a fence with a list
|
|
7141
|
+
// content column is abandoned by a non-blank line indented before it. The two
|
|
7142
|
+
// requirements are independent (OR), so a `> - ``` ` fence is abandoned by leaving
|
|
7143
|
+
// EITHER the blockquote or the list. A fence with no requirements (top-level) and
|
|
7144
|
+
// blank lines never abandon.
|
|
7145
|
+
function abandonIfLeftContainer(open: OpenFence | null, rawLine: string): OpenFence | null {
|
|
7146
|
+
if (open === null) return null
|
|
7147
|
+
const { line, quoted } = stripBlockquoteMarkers(rawLine)
|
|
7148
|
+
if (line.trim() === '') return open
|
|
7149
|
+
if (open.owner.requiresQuote && !quoted) return null
|
|
7150
|
+
if (open.owner.listContentColumn !== null && leadingIndentColumns(line) < open.owner.listContentColumn) return null
|
|
7151
|
+
return open
|
|
7152
|
+
}
|
|
7153
|
+
|
|
7154
|
+
// Removes one or more leading blockquote markers so a `> ```` fence participates
|
|
7155
|
+
// in bookkeeping as if unquoted, and reports whether the line was quoted so the
|
|
7156
|
+
// caller can detect the blockquote-exit that abandons a blockquote-owned fence.
|
|
7157
|
+
function stripBlockquoteMarkers(line: string): { line: string; quoted: boolean } {
|
|
7158
|
+
const match = /^(?: {0,3}>[ \t]?)+(.*)$/.exec(line)
|
|
7159
|
+
return match === null ? { line, quoted: false } : { line: match[1]!, quoted: true }
|
|
7160
|
+
}
|
|
7161
|
+
|
|
7162
|
+
// Leading-indent width in columns (whitespace only, stopping at the first
|
|
7163
|
+
// non-space), expanding tabs to the next 4-column stop. Used to compare a later
|
|
7164
|
+
// line's indentation against a list item's content column.
|
|
7165
|
+
function leadingIndentColumns(line: string): number {
|
|
7166
|
+
let column = 0
|
|
7167
|
+
for (const char of line) {
|
|
7168
|
+
if (char === ' ') column += 1
|
|
7169
|
+
else if (char === '\t') column += 4 - (column % 4)
|
|
7170
|
+
else break
|
|
7171
|
+
}
|
|
7172
|
+
return column
|
|
7173
|
+
}
|
|
7174
|
+
|
|
7175
|
+
// Full display width of a string in columns, expanding tabs to the next 4-column
|
|
7176
|
+
// stop. Used to measure a list marker prefix so the content column lands after it.
|
|
7177
|
+
function columnWidth(s: string): number {
|
|
7178
|
+
let column = 0
|
|
7179
|
+
for (const char of s) column += char === '\t' ? 4 - (column % 4) : 1
|
|
7180
|
+
return column
|
|
7181
|
+
}
|
|
7182
|
+
|
|
6869
7183
|
// Tolerant single-purpose scanner that pulls the `text` argument out of a
|
|
6870
7184
|
// plain-text-serialized `channel_reply(...)` / `channel_send(...)` leak. A
|
|
6871
7185
|
// single regex covering every shape (double/single/unquoted keys, escaped
|