@qwen-code/channel-base 0.26.9 → 0.27.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.
@@ -2638,6 +2638,10 @@ export class ChannelBase {
2638
2638
  this.namedTurnBindings.delete(envelope);
2639
2639
  }
2640
2640
  bypassesNamedTurnBinding(envelope) {
2641
+ // A tagged (automated) sender's text is prose, so it never takes a
2642
+ // command/bang bypass: it binds a named turn like any other message.
2643
+ if (envelope.senderTag)
2644
+ return false;
2641
2645
  const parsed = this.parseCommand(envelope.text);
2642
2646
  if (parsed && this.commands.has(parsed.command))
2643
2647
  return true;
@@ -4426,7 +4430,9 @@ export class ChannelBase {
4426
4430
  const deferredGroupPairing = options.deferPairingRequests === true &&
4427
4431
  groupResult.reason === 'pairing_trigger_required' &&
4428
4432
  (envelope.isMentioned || envelope.isReplyToBot);
4429
- if (!groupResult.allowed && !deferredGroupPairing) {
4433
+ const waivedGroupMention = options.waiveGroupMention === true &&
4434
+ groupResult.reason === 'mention_required';
4435
+ if (!groupResult.allowed && !deferredGroupPairing && !waivedGroupMention) {
4430
4436
  if (groupResult.pairing !== undefined) {
4431
4437
  this.logPreflightRejected('group_pairing_required', envelope);
4432
4438
  return this.onGroupPairingRequired(envelope.chatId, groupResult.pairing, envelope.threadId)
@@ -4464,6 +4470,12 @@ export class ChannelBase {
4464
4470
  this.markPreflighted(envelope);
4465
4471
  return true;
4466
4472
  }
4473
+ // Private comment-watch delivery of the channel account's own comments: the
4474
+ // group/DM/route checks above already ran; only the sender gate is waived.
4475
+ if (options.waiveGroupSender === true) {
4476
+ this.markPreflighted(envelope);
4477
+ return true;
4478
+ }
4467
4479
  const result = senderGate.check(envelope.senderId, envelope.senderName);
4468
4480
  if (!result.allowed) {
4469
4481
  if (result.pairing !== undefined) {
@@ -4714,6 +4726,14 @@ export class ChannelBase {
4714
4726
  markPreflighted(envelope) {
4715
4727
  this.preflightedEnvelopes.add(envelope);
4716
4728
  }
4729
+ /**
4730
+ * Whether a message with this text passes the channel's message routes the
4731
+ * way preflight would route it. Has no side effects.
4732
+ */
4733
+ admitsMessageRoute(text) {
4734
+ return (this.messageRoutes.size === 0 ||
4735
+ matchMessageRoute(text, this.messageRoutes, this.config.defaultMessageRoute) !== undefined);
4736
+ }
4717
4737
  applyMessageRoute(envelope) {
4718
4738
  if (this.routedEnvelopes.has(envelope))
4719
4739
  return true;
@@ -4759,15 +4779,20 @@ export class ChannelBase {
4759
4779
  await this.recordObservedContact(envelope);
4760
4780
  this.onObservedContact(envelope);
4761
4781
  }
4762
- const parsed = this.parseCommand(envelope.text);
4763
- let memoryIntent = parsed?.command === 'btw'
4782
+ // Automated senders (see Envelope.senderTag) get prose treatment: their
4783
+ // command-shaped text reaches the model instead of this channel's local
4784
+ // commands and memory intents.
4785
+ const automated = envelope.senderTag !== undefined;
4786
+ const parsed = automated ? null : this.parseCommand(envelope.text);
4787
+ let memoryIntent = automated || parsed?.command === 'btw'
4764
4788
  ? null
4765
4789
  : parseChannelMemoryIntent(envelope.text);
4766
4790
  let memoryIntentFromClassifier = false;
4767
4791
  if (memoryIntent?.kind === 'update' || memoryIntent?.kind === 'remove') {
4768
4792
  this.deletePendingChannelMemoryMutation(envelope);
4769
4793
  }
4770
- if (!memoryIntent &&
4794
+ if (!automated &&
4795
+ !memoryIntent &&
4771
4796
  parsed?.command !== 'btw' &&
4772
4797
  this.shouldClassifyChannelMemoryIntent(envelope.text)) {
4773
4798
  memoryIntent = await this.classifyChannelMemoryIntent(envelope);
@@ -4831,7 +4856,9 @@ export class ChannelBase {
4831
4856
  // Phase 0 has no per-sender trust model (the [sender] marker is NOT a trust
4832
4857
  // boundary). Any group is multi-operator — even a user-scope group, which is
4833
4858
  // NOT a "shared session" — so an allowed member could `!rm -rf /` the host.
4834
- const bangText = envelope.text.trimStart();
4859
+ // An automated sender's text is never a shell command (Envelope.senderTag),
4860
+ // so it skips both the refusal and the execution arms below.
4861
+ const bangText = automated ? '' : envelope.text.trimStart();
4835
4862
  if (bangText.startsWith('!')) {
4836
4863
  if (envelope.isGroup || this.isSharedSession(envelope)) {
4837
4864
  // Audit a blocked host-shell attempt — a group/shared member trying `!`
@@ -4936,8 +4963,16 @@ export class ChannelBase {
4936
4963
  return;
4937
4964
  }
4938
4965
  }
4939
- const recognizedSlashCommand = this.isSlashCommand(envelope.text) &&
4966
+ const recognizedSlashCommand = !automated &&
4967
+ this.isSlashCommand(envelope.text) &&
4940
4968
  this.isRecognizedCommand(envelope.text, sessionId);
4969
+ // Adapter-tagged automated sender (Envelope.senderTag): rendered here,
4970
+ // after prompt sanitization, because a line-leading `[tag]` inside `text`
4971
+ // would be peeled by unwrapStartOfLineTags. Self-prefixing adapters
4972
+ // (`alreadyPrefixed`) render the tag themselves.
4973
+ const senderTagBar = !envelope.alreadyPrefixed && envelope.senderTag?.trim()
4974
+ ? `[${sanitizeSenderName(envelope.senderTag)}] `
4975
+ : '';
4941
4976
  // Prepend referenced (quoted) message text for reply context
4942
4977
  let promptText = envelope.text;
4943
4978
  // Multiplayer attribution: when a session can carry multiple humans, tag each
@@ -4965,7 +5000,7 @@ export class ChannelBase {
4965
5000
  !envelope.alreadyPrefixed &&
4966
5001
  !recognizedSlashCommand) {
4967
5002
  const who = sanitizeSenderName(envelope.senderName || envelope.senderId || 'unknown');
4968
- promptText = `[${who}] ${sanitizePromptText(promptText)}`;
5003
+ promptText = `${senderTagBar}[${who}] ${sanitizePromptText(promptText)}`;
4969
5004
  // Render the non-bot mention marker AFTER sanitization (like the
4970
5005
  // [Replying to:] wrapper below). Inside `text` it would pass through
4971
5006
  // sanitizePromptText, which strips brackets only on content <=64 chars
@@ -4985,6 +5020,11 @@ export class ChannelBase {
4985
5020
  }
4986
5021
  }
4987
5022
  }
5023
+ else if (senderTagBar) {
5024
+ // No sender attribution to attach the tag to (per-user 1:1 DM), so the
5025
+ // tag opens the prompt on its own.
5026
+ promptText = `${senderTagBar}${promptText}`;
5027
+ }
4988
5028
  if (envelope.referencedText) {
4989
5029
  // Quoted text is attacker-controlled. sanitizeQuotedText strips C0/DEL
4990
5030
  // controls, Unicode line/paragraph separators (U+2028/U+2029) and bidi
@@ -5335,14 +5375,21 @@ export class ChannelBase {
5335
5375
  // bridge only after the latest recovery has restored session routing.
5336
5376
  await this.waitForBridgeRecovery();
5337
5377
  const promptBridge = this.bridge;
5378
+ let turnNotice = '';
5379
+ const onTurnNotice = (sid, notice) => {
5380
+ if (sid === sessionId)
5381
+ turnNotice = notice;
5382
+ };
5338
5383
  promptBridge.on('textChunk', onChunk);
5384
+ promptBridge.on('turnNotice', onTurnNotice);
5339
5385
  promptBridge.on('responseBoundary', onResponseBoundary);
5340
5386
  let taskResultPartial = false;
5387
+ let answerDelivered = false;
5341
5388
  try {
5342
5389
  if (!this.canStartInboundTurn()) {
5343
5390
  throw new ChannelPromptCancelledError();
5344
5391
  }
5345
- const response = await promptBridge.prompt(sessionId, promptToSend, {
5392
+ let response = await promptBridge.prompt(sessionId, promptToSend, {
5346
5393
  ...(this.config.outputMode === 'per_task'
5347
5394
  ? {
5348
5395
  outputMode: 'per_task',
@@ -5365,14 +5412,40 @@ export class ChannelBase {
5365
5412
  // If cancelled, skip sending the response
5366
5413
  if (!promptState.cancelled && response) {
5367
5414
  promptState.deliveryStarted = true;
5415
+ if (turnNotice) {
5416
+ response = response.slice(turnNotice.length + 2);
5417
+ }
5418
+ }
5419
+ if (!promptState.cancelled && response) {
5368
5420
  const segment = this.ensureOutputSegment(sessionId, promptState);
5369
5421
  if (segment && taskResultPartial)
5370
5422
  segment.partial = true;
5371
5423
  await this.onResponseComplete(envelope.chatId, response, sessionId, segment);
5424
+ answerDelivered = true;
5372
5425
  if (segment && promptState.activeSegmentId === segment.segmentId) {
5373
5426
  promptState.activeSegmentId = undefined;
5374
5427
  }
5375
5428
  }
5429
+ if (!promptState.cancelled && turnNotice) {
5430
+ // Keep notices out of stream buffers and let the answer consume
5431
+ // adapter-specific reply metadata (such as DingTalk mentions) first.
5432
+ try {
5433
+ await this.sendResponseMessage(envelope.chatId, turnNotice, sessionId, sourceLabel);
5434
+ }
5435
+ catch (err) {
5436
+ // The prompt result is `turnNotice || body`, so on an empty body
5437
+ // the notice is the turn's only delivery: rethrow instead of
5438
+ // reporting a completed turn the user never received.
5439
+ if (!answerDelivered)
5440
+ throw err;
5441
+ // The answer is already delivered, so the turn's outcome is fixed
5442
+ // (see the deliveryStarted invariant below): log the failed
5443
+ // notice instead of letting it rewrite completed into failed.
5444
+ // The notice was sliced out of the answer above, so this line is
5445
+ // the only remaining record of what the user never received.
5446
+ process.stderr.write(`[${this.name}] turn notice delivery failed for session ${sanitizeLogText(sessionId, 128)}: ${this.lifecycleError(err)} (dropped notice: ${sanitizeLogText(turnNotice, 200)})\n`);
5447
+ }
5448
+ }
5376
5449
  // Once delivery started the turn's outcome is fixed — don't let a
5377
5450
  // cancel settling during the send rewrite completed into cancelled.
5378
5451
  if (!promptState.deliveryStarted) {
@@ -5426,6 +5499,7 @@ export class ChannelBase {
5426
5499
  }
5427
5500
  finally {
5428
5501
  promptBridge.off('textChunk', onChunk);
5502
+ promptBridge.off('turnNotice', onTurnNotice);
5429
5503
  promptBridge.off('responseBoundary', onResponseBoundary);
5430
5504
  // Identity guard: a turn that wedged past /clear's bounded wait gets
5431
5505
  // EVICTED — /clear gives up on active.done, deletes activePrompts, and a