Skip to content

Commit 4201fbf

Browse files
committed
feat(amazonq): skip registering run command log file
1 parent e01574f commit 4201fbf

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import * as vscode from 'vscode'
7+
import * as path from 'path'
78
import { ToolkitError } from '../../shared/errors'
89
import globals from '../../shared/extensionGlobals'
910
import { getLogger } from '../../shared/logger/logger'
@@ -29,6 +30,7 @@ import { prepareRepoData, getDeletedFileInfos, registerNewFiles, PrepareRepoData
2930
import { uploadCode } from '../util/upload'
3031

3132
export const EmptyCodeGenID = 'EMPTY_CURRENT_CODE_GENERATION_ID'
33+
const RunCommandLogFileName = '.amazonq/dev/run_command.log'
3234

3335
export interface BaseMessenger {
3436
sendAnswer(params: any): void
@@ -103,6 +105,36 @@ export abstract class CodeGenBase {
103105
case CodeGenerationStatus.COMPLETE: {
104106
const { newFileContents, deletedFiles, references } =
105107
await this.config.proxyClient.exportResultArchive(this.conversationId)
108+
109+
const logFileInfo = newFileContents.find(
110+
(file: { zipFilePath: string; fileContent: string }) =>
111+
file.zipFilePath === RunCommandLogFileName
112+
)
113+
getLogger().info(`Log file info: %O`, logFileInfo)
114+
if (logFileInfo) {
115+
const filePath = path.join(this.config.workspaceRoots[0], RunCommandLogFileName)
116+
const fileUri = vscode.Uri.file(filePath)
117+
getLogger().info(`Writing log file to ${filePath}`)
118+
119+
try {
120+
// ensure the directory exists before writing the log file
121+
// const logDir = path.dirname(fileUri.fsPath)
122+
// Optionally check if the directory exists or create it recursively
123+
await fs.writeFile(fileUri, logFileInfo.fileContent, {
124+
create: true,
125+
overwrite: true,
126+
})
127+
} catch (e) {
128+
const errorDetails =
129+
e instanceof Error ? `${e.name}: ${e.message}\nStack Trace: ${e.stack}` : String(e)
130+
getLogger().error(`Error writing log file. Details: ${errorDetails}`)
131+
}
132+
133+
// getLogger().info(`Log file written to ${filePath}`)
134+
newFileContents.splice(newFileContents.indexOf(logFileInfo), 1)
135+
getLogger().info(`Log file removed from new files %O`, newFileContents)
136+
}
137+
106138
const newFileInfo = registerNewFiles(
107139
fs,
108140
newFileContents,
@@ -111,6 +143,7 @@ export abstract class CodeGenBase {
111143
this.conversationId,
112144
this.getScheme()
113145
)
146+
getLogger().info(`New files Info registered: %O`, newFileInfo)
114147
telemetry.setNumberOfFilesGenerated(newFileInfo.length)
115148

116149
this.handleGenerationComplete(messenger, newFileInfo, action)
@@ -361,6 +394,7 @@ export abstract class BaseCodeGenState extends CodeGenBase implements SessionSta
361394
workspaceFolders: this.config.workspaceFolders,
362395
action,
363396
})
397+
getLogger().debug(`Code generation result: %O`, codeGeneration)
364398

365399
if (codeGeneration && !action.tokenSource?.token.isCancellationRequested) {
366400
this.config.currentCodeGenerationId = codeGenerationId

packages/core/src/shared/virtualFilesystem.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
import * as vscode from 'vscode'
7+
import { getLogger } from './logger/logger'
8+
import { get } from 'lodash'
79

810
type StatNoType = Omit<vscode.FileStat, 'type'>
911

@@ -56,8 +58,11 @@ export class VirtualFileSystem implements vscode.FileSystemProvider {
5658
}
5759

5860
private getProvider(uri: vscode.Uri): FileProvider {
61+
getLogger().info(`in getProvider: ${this.uriToKey(uri)}`)
5962
const provider = this.fileProviders[this.uriToKey(uri)]
63+
getLogger().info(`provider is: ${provider}`)
6064
if (!provider) {
65+
getLogger().info(`error in getProvider: ${this.errorMessage}`)
6166
throw new Error(this.errorMessage)
6267
}
6368
return provider

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

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ 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 as CodeGenBaseClass } from '../../../amazonq/session/sessionState'
1314

1415
describe('sessionStateDoc', () => {
1516
const context = createTestContext()
@@ -26,4 +27,111 @@ describe('sessionStateDoc', () => {
2627
})
2728
})
2829
})
30+
31+
describe('CodeGenBase generateCode log file handling', () => {
32+
const RunCommandLogFileName = '.amazonq/dev/run_command.log'
33+
34+
class TestCodeGen extends CodeGenBaseClass {
35+
public generatedFiles: any[] = []
36+
constructor(config: any, tabID: string) {
37+
super(config, tabID)
38+
}
39+
protected handleProgress(_messenger: any): void {}
40+
protected getScheme(): string {
41+
return 'file'
42+
}
43+
protected getTimeoutErrorCode(): string {
44+
return 'test_timeout'
45+
}
46+
protected handleGenerationComplete(_messenger: any, newFileInfo: any[]): void {
47+
this.generatedFiles = newFileInfo
48+
}
49+
protected handleError(_messenger: any, _codegenResult: any): Error {
50+
throw new Error('handleError called')
51+
}
52+
}
53+
54+
let testConfig: any, fakeProxyClient: any, fsMock: any, messengerMock: any, testAction: any, telemetryMock: any
55+
56+
beforeEach(() => {
57+
fakeProxyClient = {
58+
getCodeGeneration: sinon.stub().resolves({
59+
codeGenerationStatus: { status: 'Complete' },
60+
codeGenerationRemainingIterationCount: 0,
61+
codeGenerationTotalIterationCount: 1,
62+
}),
63+
exportResultArchive: sinon.stub(),
64+
}
65+
66+
testConfig = {
67+
conversationId: 'conv1',
68+
uploadId: 'upload1',
69+
workspaceRoots: ['/workspace'],
70+
proxyClient: fakeProxyClient,
71+
}
72+
73+
fsMock = {
74+
writeFile: sinon.stub().resolves(),
75+
}
76+
77+
messengerMock = { sendAnswer: sinon.spy() }
78+
79+
telemetryMock = {
80+
setCodeGenerationResult: sinon.spy(),
81+
setNumberOfFilesGenerated: sinon.spy(),
82+
setAmazonqNumberOfReferences: sinon.spy(),
83+
setGenerateCodeIteration: sinon.spy(),
84+
setGenerateCodeLastInvocationTime: sinon.spy(),
85+
recordUserCodeGenerationTelemetry: sinon.spy(),
86+
}
87+
88+
testAction = {
89+
fs: fsMock,
90+
messenger: messengerMock,
91+
tokenSource: { token: { isCancellationRequested: false, onCancellationRequested: () => {} } },
92+
}
93+
})
94+
95+
afterEach(() => {
96+
sinon.restore()
97+
})
98+
99+
it('writes to the log file, present or not', async () => {
100+
const logFileInfo = {
101+
zipFilePath: RunCommandLogFileName,
102+
fileContent: 'newLog',
103+
}
104+
const otherFile = { zipFilePath: 'other.ts', fileContent: 'other' }
105+
106+
fakeProxyClient.exportResultArchive.resolves({
107+
newFileContents: [logFileInfo, otherFile],
108+
deletedFiles: [],
109+
references: [],
110+
})
111+
112+
// Minimal stubs to simulate file processing behavior.
113+
sinon.stub(filesModule, 'registerNewFiles').callsFake((_, newFileContents: any[]) => newFileContents)
114+
sinon.stub(filesModule, 'getDeletedFileInfos').callsFake(() => [])
115+
116+
const testCodeGen = new TestCodeGen(testConfig, 'tab1')
117+
118+
await testCodeGen.generateCode({
119+
messenger: messengerMock,
120+
fs: fsMock,
121+
codeGenerationId: 'codegen2',
122+
telemetry: telemetryMock,
123+
workspaceFolders: {} as any,
124+
action: testAction,
125+
})
126+
127+
const expectedFilePath = `${testConfig.workspaceRoots[0]}/${RunCommandLogFileName}`
128+
const fileUri = vscode.Uri.file(expectedFilePath)
129+
sinon.assert.calledWith(fsMock.writeFile, fileUri, new TextEncoder().encode('newLog'), {
130+
create: true,
131+
overwrite: true,
132+
})
133+
134+
assert.deepStrictEqual(testCodeGen.generatedFiles, [otherFile])
135+
})
136+
})
29137
})

0 commit comments

Comments
 (0)