Repository navigation
feat(core): address other sessions by name from send_message and list_agents #8733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
b92fde1
88cb405
4a9e64c
a9e9cee
63978ac
ed2fc2f
9d0b053
c9befd6
ea222f2
34b5eec
41bb857
b201019
e68c5c0
06ab9e2
452c448
1ae6576
170a1b0
45b9659
a420dc7
248165f
505a638
8d97cf5
16b1e61
4a19a90
3e5a76d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; | ||
| import type { SessionRegistryRecord } from '@qwen-code/qwen-code-core'; | ||
|
|
||
| const listLiveSessions = vi.fn(); | ||
|
|
||
| vi.mock('@qwen-code/qwen-code-core', () => ({ | ||
| listLiveSessions: (...args: unknown[]) => listLiveSessions(...args), | ||
| })); | ||
|
|
||
| const stdout: string[] = []; | ||
| const stderr: string[] = []; | ||
|
|
||
| vi.mock('../../utils/stdioHelpers.js', () => ({ | ||
| writeStdoutLine: (line: string) => stdout.push(line), | ||
| writeStderrLine: (line: string) => stderr.push(line), | ||
| })); | ||
|
|
||
| const { psCommand, formatAge } = await import('./ps.js'); | ||
|
|
||
| function record( | ||
| over: Partial<SessionRegistryRecord> = {}, | ||
| ): SessionRegistryRecord { | ||
| return { | ||
| schemaVersion: 1, | ||
| pid: 4242, | ||
| procStart: '123', | ||
| sessionId: 'sess-1', | ||
| cwd: '/w/app', | ||
| name: 'app-ab', | ||
| kind: 'interactive', | ||
| startedAt: Date.now() - 90_000, | ||
| qwenVersion: '1.0.0', | ||
| peerProtocol: 1, | ||
| ...over, | ||
| }; | ||
| } | ||
|
|
||
| async function run(argv: Record<string, unknown>): Promise<void> { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| await (psCommand.handler as any)(argv); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| stdout.length = 0; | ||
| stderr.length = 0; | ||
| listLiveSessions.mockReset(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('formatAge', () => { | ||
| it('scales the unit with the magnitude', () => { | ||
| expect(formatAge(5_000)).toBe('5s'); | ||
| expect(formatAge(90_000)).toBe('1m'); | ||
| expect(formatAge(3 * 3600_000)).toBe('3h'); | ||
| expect(formatAge(50 * 3600_000)).toBe('2d'); | ||
| }); | ||
|
|
||
| it('clamps a record from the future to zero rather than showing a negative age', () => { | ||
| expect(formatAge(-10_000)).toBe('0s'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('qwen sessions ps', () => { | ||
| it('prints a table of live sessions', async () => { | ||
| listLiveSessions.mockResolvedValue([record()]); | ||
| await run({ json: false, all: false }); | ||
|
|
||
| expect(stdout[0]).toMatch(/^NAME\s+PID\s+AGE\s+DIRECTORY$/); | ||
| expect(stdout[1]).toContain('app-ab'); | ||
| expect(stdout[1]).toContain('4242'); | ||
| expect(stdout[1]).toContain('/w/app'); | ||
| }); | ||
|
|
||
| it('says so plainly when nothing else is running', async () => { | ||
| listLiveSessions.mockResolvedValue([]); | ||
| await run({ json: false, all: false }); | ||
| expect(stdout).toEqual(['No other Qwen Code sessions are running.']); | ||
| }); | ||
|
|
||
| it('emits one JSON object per line with no header', async () => { | ||
| listLiveSessions.mockResolvedValue([record(), record({ pid: 7 })]); | ||
| await run({ json: true, all: false }); | ||
|
|
||
| expect(stdout).toHaveLength(2); | ||
| expect(JSON.parse(stdout[0]).pid).toBe(4242); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: test-hardening suggestion (mutation pin / efficacy probe). This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:测试加固建议(变异钉/测试效力探针)。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| expect(JSON.parse(stdout[1]).pid).toBe(7); | ||
| }); | ||
|
|
||
| it('prints nothing on stdout for an empty JSON listing', async () => { | ||
| listLiveSessions.mockResolvedValue([]); | ||
| await run({ json: true, all: false }); | ||
| expect(stdout).toEqual([]); | ||
| }); | ||
|
|
||
| it('excludes this process unless --all is passed', async () => { | ||
| listLiveSessions.mockResolvedValue([]); | ||
|
|
||
| await run({ json: true, all: false }); | ||
| expect(listLiveSessions).toHaveBeenLastCalledWith({ includeSelf: false }); | ||
|
|
||
| await run({ json: true, all: true }); | ||
| expect(listLiveSessions).toHaveBeenLastCalledWith({ includeSelf: true }); | ||
| }); | ||
|
|
||
| it('neutralizes control sequences coming from another process record', async () => { | ||
| listLiveSessions.mockResolvedValue([ | ||
| record({ name: 'ev[31mil\r', cwd: '/w/a\nb' }), | ||
| ]); | ||
| await run({ json: false, all: false }); | ||
|
|
||
| const row = stdout[1]; | ||
| expect(row).not.toContain(''); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: test-hardening suggestion (mutation pin / efficacy probe). This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:测试加固建议(变异钉/测试效力探针)。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| expect(row).not.toContain('\r'); | ||
| expect(row).not.toContain('\n'); | ||
| }); | ||
|
|
||
| it('reports a registry read failure on stderr and exits 1', async () => { | ||
| const exit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => undefined) as never); | ||
| listLiveSessions.mockRejectedValue(new Error('EACCES')); | ||
|
|
||
| await run({ json: false, all: false }); | ||
|
|
||
| expect(stderr.join('\n')).toContain( | ||
| 'failed to read the session registry: EACCES', | ||
| ); | ||
| expect(exit).toHaveBeenCalledWith(1); | ||
| expect(stdout).toEqual([]); | ||
| }); | ||
|
|
||
| it('truncates an over-long name instead of breaking the columns', async () => { | ||
| listLiveSessions.mockResolvedValue([record({ name: 'x'.repeat(80) })]); | ||
| await run({ json: false, all: false }); | ||
| expect(stdout[1]).toContain('...'); | ||
| expect(stdout[1]).toContain('4242'); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * `qwen sessions ps` — list the Qwen Code sessions running right now. | ||
| * | ||
| * The sibling `qwen sessions list` walks saved transcripts; this walks the | ||
| * live-process registry, so the two answer different questions: "what have | ||
| * I worked on" versus "what is running on this machine at this moment". | ||
| */ | ||
|
|
||
| import type { CommandModule, Argv } from 'yargs'; | ||
| import { | ||
| listLiveSessions, | ||
| type SessionRegistryRecord, | ||
| } from '@qwen-code/qwen-code-core'; | ||
| import stringWidth from 'string-width'; | ||
| import { escapeAnsiCtrlCodes } from '../../ui/utils/textUtils.js'; | ||
| import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js'; | ||
|
|
||
| /** Fixed column widths for the human-readable table (exported for tests). */ | ||
| export const NAME_COL = 22; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: dead-surface removal. This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:移除无用接口。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| export const PID_COL = 9; | ||
| export const AGE_COL = 10; | ||
|
|
||
| interface PsArgs { | ||
| json?: boolean; | ||
| all?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Sanitize a value for terminal output. | ||
| * | ||
| * `cwd` and `name` originate from another process's on-disk record, so | ||
| * they are attacker-influenced in exactly the way a log line is: a record | ||
| * containing an ANSI sequence or a stray `\r` could otherwise repaint or | ||
| * misalign this table. Mirrors `sessions list`. | ||
| */ | ||
| function sanitize(value: string): string { | ||
| const stripped = value.replace(/[\r\n\t]/g, ''); | ||
| const escaped = escapeAnsiCtrlCodes(stripped); | ||
|
Comment on lines
+42
to
+44
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: shared-helper refactor across an existing sibling command. This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:跨既有兄弟命令的共享助手重构。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| // eslint-disable-next-line no-control-regex | ||
| return escaped.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]/g, ''); | ||
|
Comment on lines
+44
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: small robustness improvement. This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:小的健壮性改进。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| } | ||
|
|
||
| function padDisplay(str: string, width: number): string { | ||
| const currentWidth = stringWidth(str); | ||
| if (currentWidth >= width) return str; | ||
| return str + ' '.repeat(width - currentWidth); | ||
| } | ||
|
|
||
| function truncate(str: string, maxLen: number): string { | ||
| if (stringWidth(str) <= maxLen) return str; | ||
| const suffix = maxLen > 3 ? '...' : ''; | ||
| const target = maxLen - stringWidth(suffix); | ||
| let result = ''; | ||
| let w = 0; | ||
| for (const char of str) { | ||
| const cw = stringWidth(char); | ||
| if (w + cw > target) break; | ||
|
Comment on lines
+62
to
+63
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deferred to a follow-up: test-hardening suggestion (mutation pin / efficacy probe). This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up. 中文说明推迟到后续处理:测试加固建议(变异钉/测试效力探针)。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。 |
||
| result += char; | ||
| w += cw; | ||
| } | ||
| return result + suffix; | ||
| } | ||
|
|
||
| /** | ||
| * Render an age as a short, human-scannable string. | ||
| * | ||
| * A negative delta means the record's clock ran ahead of ours (a paused | ||
| * VM, a corrected clock). Showing "-3m" reads as a bug, so clamp to 0. | ||
| */ | ||
| export function formatAge(ms: number): string { | ||
| const seconds = Math.max(0, Math.floor(ms / 1000)); | ||
| if (seconds < 60) return `${seconds}s`; | ||
| const minutes = Math.floor(seconds / 60); | ||
| if (minutes < 60) return `${minutes}m`; | ||
| const hours = Math.floor(minutes / 60); | ||
| if (hours < 24) return `${hours}h`; | ||
| return `${Math.floor(hours / 24)}d`; | ||
| } | ||
|
|
||
| function outputHuman(records: SessionRegistryRecord[], now: number): void { | ||
| writeStdoutLine( | ||
| padDisplay('NAME', NAME_COL) + | ||
| padDisplay('PID', PID_COL) + | ||
| padDisplay('AGE', AGE_COL) + | ||
| 'DIRECTORY', | ||
| ); | ||
| for (const record of records) { | ||
| writeStdoutLine( | ||
| padDisplay(truncate(sanitize(record.name), NAME_COL - 2), NAME_COL) + | ||
| padDisplay(String(record.pid), PID_COL) + | ||
| padDisplay(formatAge(now - record.startedAt), AGE_COL) + | ||
| sanitize(record.cwd), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async function handlePs(argv: PsArgs): Promise<void> { | ||
| let records: SessionRegistryRecord[]; | ||
| try { | ||
| records = await listLiveSessions({ includeSelf: argv.all ?? false }); | ||
| } catch (err) { | ||
| writeStderrLine( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Critical] This error path is unreachable with the production 中文说明生产环境中的 — qwen3.8-max via Qwen Code /review (v0.21.8) |
||
| `Error: failed to read the session registry: ${ | ||
| err instanceof Error ? err.message : String(err) | ||
| }`, | ||
| ); | ||
| process.exit(1); | ||
| return; | ||
| } | ||
|
|
||
| const now = Date.now(); | ||
|
|
||
| if (argv.json) { | ||
| for (const record of records) { | ||
| writeStdoutLine(JSON.stringify(record)); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (records.length === 0) { | ||
| writeStdoutLine('No other Qwen Code sessions are running.'); | ||
| return; | ||
| } | ||
|
|
||
| outputHuman(records, now); | ||
| } | ||
|
|
||
| export const psCommand: CommandModule<unknown, PsArgs> = { | ||
| command: 'ps', | ||
| describe: 'List Qwen Code sessions running right now', | ||
| builder: (yargs: Argv) => | ||
| yargs | ||
| .option('json', { | ||
| type: 'boolean', | ||
| describe: 'Output as JSON Lines', | ||
| default: false, | ||
| }) | ||
| .option('all', { | ||
| type: 'boolean', | ||
| describe: 'Include this process, if it is itself a registered session', | ||
| default: false, | ||
| }), | ||
| handler: async (argv) => { | ||
| await handlePs(argv); | ||
| }, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deferred to a follow-up: test-hardening suggestion (mutation pin / efficacy probe). This PR is past five change-producing review rounds, so per the repo's review budget this round takes only the Critical fixes; this finding is recorded in the round summary so it is tracked, not silently dropped. The open thread keeps it visible for the follow-up.
中文说明
推迟到后续处理:测试加固建议(变异钉/测试效力探针)。本 PR 已超过五个产生变更的评审轮次,按仓库的评审轮次预算,本轮只接收 Critical 修复;该发现已记录在本轮总结中,会被跟踪而不会被静默丢弃。保持打开的线程让它在后续处理中仍然可见。