Skip to content

Commit 0a3ca98

Browse files
committed
feat(amazonq): skip registering run command log file
1 parent c3ea31d commit 0a3ca98

File tree

4 files changed

+177
-2
lines changed

4 files changed

+177
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "The logs emitted by the Agent during user command execution will be accepted and written to .amazonq/dev/run_command.log file in the user's local repository."
4+
}

packages/core/src/amazonq/session/sessionState.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { prepareRepoData, getDeletedFileInfos, registerNewFiles, PrepareRepoData
2929
import { uploadCode } from '../util/upload'
3030

3131
export const EmptyCodeGenID = 'EMPTY_CURRENT_CODE_GENERATION_ID'
32+
export const RunCommandLogFileName = '.amazonq/dev/run_command.log'
3233

3334
export interface BaseMessenger {
3435
sendAnswer(params: any): void
@@ -103,6 +104,16 @@ export abstract class CodeGenBase {
103104
case CodeGenerationStatus.COMPLETE: {
104105
const { newFileContents, deletedFiles, references } =
105106
await this.config.proxyClient.exportResultArchive(this.conversationId)
107+
108+
const logFileInfo = newFileContents.find(
109+
(file: { zipFilePath: string; fileContent: string }) =>
110+
file.zipFilePath === RunCommandLogFileName
111+
)
112+
if (logFileInfo) {
113+
getLogger().info(`Run Command logs: \n ${logFileInfo.fileContent}`)
114+
newFileContents.splice(newFileContents.indexOf(logFileInfo), 1)
115+
}
116+
106117
const newFileInfo = registerNewFiles(
107118
fs,
108119
newFileContents,

packages/core/src/shared/virtualFilesystem.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export class VirtualFileSystem implements vscode.FileSystemProvider {
4646
if (this.fileProviders[key] !== undefined) {
4747
throw new Error('Cannot re-register a provider for the same URI')
4848
}
49-
5049
this.fileProviders[key] = provider
5150
const onDidChange = provider.onDidChange(() => {
5251
this._onDidChangeFile.fire([{ uri, type: vscode.FileChangeType.Changed }])

packages/core/src/test/amazonqDoc/session/sessionState.test.ts

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import assert from 'assert'
88
import sinon from 'sinon'
99
import { DocPrepareCodeGenState } from '../../../amazonqDoc'
1010
import { createMockSessionStateAction } from '../../amazonq/utils'
11-
1211
import { createTestContext, setupTestHooks } from '../../amazonq/session/testSetup'
12+
import * as filesModule from '../../../amazonq/util/files'
13+
import { CodeGenBase } from '../../../amazonq/session/sessionState'
14+
import { RunCommandLogFileName } from '../../../amazonq/session/sessionState'
15+
import { info } from 'console'
1316

1417
describe('sessionStateDoc', () => {
1518
const context = createTestContext()
@@ -26,4 +29,162 @@ describe('sessionStateDoc', () => {
2629
})
2730
})
2831
})
32+
33+
describe('CodeGenBase generateCode log file handling', () => {
34+
class TestCodeGen extends CodeGenBase {
35+
public generatedFiles: any[] = []
36+
constructor(config: any, tabID: string) {
37+
super(config, tabID)
38+
}
39+
protected handleProgress(_messenger: any): void {
40+
// No-op for test.
41+
}
42+
protected getScheme(): string {
43+
return 'file'
44+
}
45+
protected getTimeoutErrorCode(): string {
46+
return 'test_timeout'
47+
}
48+
protected handleGenerationComplete(_messenger: any, newFileInfo: any[]): void {
49+
this.generatedFiles = newFileInfo
50+
}
51+
protected handleError(_messenger: any, _codegenResult: any): Error {
52+
throw new Error('handleError called')
53+
}
54+
}
55+
56+
let fakeProxyClient: any
57+
let testConfig: any
58+
let fsMock: any
59+
let messengerMock: any
60+
let telemetryMock: any
61+
let loggerMock: any
62+
let testAction: any
63+
let registerNewFilesStub: any
64+
65+
beforeEach(() => {
66+
fakeProxyClient = {
67+
getCodeGeneration: sinon.stub().resolves({
68+
codeGenerationStatus: { status: 'Complete' },
69+
codeGenerationRemainingIterationCount: 0,
70+
codeGenerationTotalIterationCount: 1,
71+
}),
72+
exportResultArchive: sinon.stub(),
73+
}
74+
75+
testConfig = {
76+
conversationId: 'conv_test',
77+
uploadId: 'upload_test',
78+
workspaceRoots: ['/workspace'],
79+
proxyClient: fakeProxyClient,
80+
}
81+
82+
fsMock = {
83+
writeFile: sinon.stub().resolves(),
84+
}
85+
86+
messengerMock = { sendAnswer: sinon.spy() }
87+
88+
telemetryMock = {
89+
setCodeGenerationResult: sinon.spy(),
90+
setNumberOfFilesGenerated: sinon.spy(),
91+
setAmazonqNumberOfReferences: sinon.spy(),
92+
setGenerateCodeIteration: sinon.spy(),
93+
setGenerateCodeLastInvocationTime: sinon.spy(),
94+
recordUserCodeGenerationTelemetry: sinon.spy(),
95+
}
96+
97+
loggerMock = {
98+
// addContent: sinon.spy(),
99+
info: sinon.spy(),
100+
}
101+
102+
testAction = {
103+
fs: fsMock,
104+
messenger: messengerMock,
105+
logger: loggerMock,
106+
tokenSource: { token: { isCancellationRequested: false, onCancellationRequested: () => {} } },
107+
}
108+
109+
registerNewFilesStub = sinon
110+
.stub(filesModule, 'registerNewFiles')
111+
.callsFake((_, newFileContents: any[]) => {
112+
return newFileContents
113+
})
114+
})
115+
116+
afterEach(() => {
117+
sinon.restore()
118+
})
119+
120+
it('adds the log content to logger if present and excludes it from new files', async () => {
121+
const logFileInfo = {
122+
zipFilePath: RunCommandLogFileName,
123+
fileContent: 'newLog',
124+
}
125+
const otherFile = { zipFilePath: 'other.ts', fileContent: 'other content' }
126+
fakeProxyClient.exportResultArchive.resolves({
127+
newFileContents: [logFileInfo, otherFile],
128+
deletedFiles: [],
129+
references: [],
130+
})
131+
132+
const testCodeGen = new TestCodeGen(testConfig, 'tab1')
133+
134+
const result = await testCodeGen.generateCode({
135+
messenger: messengerMock,
136+
fs: fsMock,
137+
codeGenerationId: 'codegen1',
138+
telemetry: telemetryMock,
139+
workspaceFolders: [] as any,
140+
action: testAction,
141+
})
142+
143+
sinon.assert.calledWith(loggerMock.info, `Run Command logs: \n ${logFileInfo.fileContent}`)
144+
145+
sinon.assert.calledWith(
146+
registerNewFilesStub,
147+
fsMock,
148+
[otherFile],
149+
testConfig.uploadId,
150+
[] as any,
151+
testConfig.conversationId,
152+
'file'
153+
)
154+
155+
assert.deepStrictEqual(result.newFiles, [otherFile])
156+
})
157+
158+
it('skips log file handling if log file is not present', async () => {
159+
const file1 = { zipFilePath: 'file1.ts', fileContent: 'content1' }
160+
fakeProxyClient.exportResultArchive.resolves({
161+
newFileContents: [file1],
162+
deletedFiles: [],
163+
references: [],
164+
})
165+
166+
const testCodeGen = new TestCodeGen(testConfig, 'tab1')
167+
168+
const result = await testCodeGen.generateCode({
169+
messenger: messengerMock,
170+
fs: fsMock,
171+
codeGenerationId: 'codegen2',
172+
telemetry: telemetryMock,
173+
workspaceFolders: [] as any,
174+
action: testAction,
175+
})
176+
177+
sinon.assert.notCalled(loggerMock.info)
178+
sinon.assert.calledWith(
179+
registerNewFilesStub,
180+
fsMock,
181+
[file1],
182+
testConfig.uploadId,
183+
[] as any,
184+
testConfig.conversationId,
185+
'file'
186+
)
187+
assert.deepStrictEqual(result.newFiles, [file1])
188+
})
189+
})
29190
})

0 commit comments

Comments
 (0)