Skip to content

Commit 11b047d

Browse files
committed
feat(amazonq): skip registering run command log file
1 parent b048f97 commit 11b047d

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-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: 17 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+
const RunCommandLogFileName = '.amazonq/dev/run_command.log'
3233

3334
export interface BaseMessenger {
3435
sendAnswer(params: any): void
@@ -103,6 +104,22 @@ 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+
const filePath = `${this.config.workspaceRoots[0]}/${RunCommandLogFileName}`
114+
const fileUri = vscode.Uri.file(filePath)
115+
116+
await fs.writeFile(fileUri, new TextEncoder().encode(logFileInfo.fileContent), {
117+
create: true,
118+
overwrite: true,
119+
})
120+
newFileContents.splice(newFileContents.indexOf(logFileInfo), 1)
121+
}
122+
106123
const newFileInfo = registerNewFiles(
107124
fs,
108125
newFileContents,

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

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ 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+
const filesModule = require('../../../amazonq/util/files')
1313

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

0 commit comments

Comments
 (0)