Skip to content

Commit e966cb6

Browse files
fix(invoke): pass session ID to local invoke log files (#894)
The --session-id flag value was correctly sent to Runtime but never passed to InvokeLogger, causing local log files to always show "Session ID: none". Wire options.sessionId through to both the InvokeLogger constructor and logPrompt() calls in exec and standard invoke modes. Closes #890
1 parent 08d452e commit e966cb6

2 files changed

Lines changed: 100 additions & 2 deletions

File tree

src/cli/commands/invoke/action.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ export async function handleInvoke(context: InvokeContext, options: InvokeOption
118118
agentName: agentSpec.name,
119119
runtimeArn: agentState.runtimeArn,
120120
region: targetConfig.region,
121+
sessionId: options.sessionId,
121122
});
122123
const command = options.prompt;
123124
if (!command) {
124125
return { success: false, error: '--exec requires a command (prompt)' };
125126
}
126-
logger.logPrompt(command, undefined, options.userId);
127+
logger.logPrompt(command, options.sessionId, options.userId);
127128

128129
try {
129130
const result = await executeBashCommand({
@@ -324,9 +325,10 @@ export async function handleInvoke(context: InvokeContext, options: InvokeOption
324325
agentName: agentSpec.name,
325326
runtimeArn: agentState.runtimeArn,
326327
region: targetConfig.region,
328+
sessionId: options.sessionId,
327329
});
328330

329-
logger.logPrompt(options.prompt, undefined, options.userId);
331+
logger.logPrompt(options.prompt, options.sessionId, options.userId);
330332

331333
if (options.stream) {
332334
// Streaming mode
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { InvokeLogger } from '../invoke-logger.js';
2+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import path from 'node:path';
5+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6+
7+
vi.mock('../../../lib', async importOriginal => {
8+
const actual = await importOriginal<typeof import('../../../lib')>();
9+
return {
10+
...actual,
11+
findConfigRoot: () => tempDir,
12+
};
13+
});
14+
15+
let tempDir: string;
16+
17+
beforeEach(() => {
18+
tempDir = mkdtempSync(path.join(tmpdir(), 'invoke-logger-test-'));
19+
});
20+
21+
afterEach(() => {
22+
rmSync(tempDir, { recursive: true, force: true });
23+
});
24+
25+
function readLog(logger: InvokeLogger): string {
26+
return readFileSync(logger.logFilePath, 'utf-8');
27+
}
28+
29+
describe('InvokeLogger session ID', () => {
30+
it('writes session ID in header when provided via constructor', () => {
31+
const logger = new InvokeLogger({
32+
agentName: 'testAgent',
33+
runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test',
34+
region: 'us-east-1',
35+
sessionId: 'my-session-123',
36+
});
37+
38+
const content = readLog(logger);
39+
expect(content).toContain('Session ID: my-session-123');
40+
expect(content).not.toContain('Session ID: none');
41+
});
42+
43+
it('writes "none" when session ID is not provided', () => {
44+
const logger = new InvokeLogger({
45+
agentName: 'testAgent',
46+
runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test',
47+
region: 'us-east-1',
48+
});
49+
50+
const content = readLog(logger);
51+
expect(content).toContain('Session ID: none');
52+
});
53+
54+
it('includes session ID in logPrompt output when passed as argument', () => {
55+
const logger = new InvokeLogger({
56+
agentName: 'testAgent',
57+
runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test',
58+
region: 'us-east-1',
59+
sessionId: 'my-session-456',
60+
});
61+
62+
logger.logPrompt('hello world', 'my-session-456', 'user-1');
63+
64+
const content = readLog(logger);
65+
expect(content).toContain('Session: my-session-456');
66+
expect(content).toContain('"sessionId": "my-session-456"');
67+
});
68+
69+
it('logPrompt falls back to constructor sessionId when argument is undefined', () => {
70+
const logger = new InvokeLogger({
71+
agentName: 'testAgent',
72+
runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test',
73+
region: 'us-east-1',
74+
sessionId: 'constructor-session',
75+
});
76+
77+
logger.logPrompt('hello world', undefined, 'user-1');
78+
79+
const content = readLog(logger);
80+
expect(content).toContain('Session: constructor-session');
81+
expect(content).toContain('"sessionId": "constructor-session"');
82+
});
83+
84+
it('logPrompt shows "none" when no session ID anywhere', () => {
85+
const logger = new InvokeLogger({
86+
agentName: 'testAgent',
87+
runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test',
88+
region: 'us-east-1',
89+
});
90+
91+
logger.logPrompt('hello world');
92+
93+
const content = readLog(logger);
94+
expect(content).toContain('Session: none');
95+
});
96+
});

0 commit comments

Comments
 (0)