Skip to content
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

add logs when stuck #40

Merged
merged 7 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/helpers/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { visualGenerate } from './visual-generate';
import { fileExists } from './file-exists';
import { outputFile } from './output-file';
import { removeBackticks } from './remove-backticks';
import { getSimpleCompletion } from './llm';

type Options = {
outputFile: string;
Expand All @@ -19,13 +20,22 @@ type Options = {
visual: string;
prompt?: string;
interactive?: boolean;
addedLogs?: boolean;
};

export async function runOne(options: Options) {
if (options.visual) {
log.step('Running...');
const result = await visualGenerate(options);
if (isFail(result.testResult)) {
if (result.testResult.message.includes('Adding logs to the code')) {
const codeWithLogs = await addLogsToCode(options);
await outputFile(options.outputFile, codeWithLogs);
return {
code: codeWithLogs,
testResult: result.testResult,
};
}
const code = result.code;
await outputFile(options.outputFile, code);
return {
Expand Down Expand Up @@ -128,6 +138,11 @@ export async function runAll(
testResult = await test(options);

if (testResult.type === 'success') {
if (options.addedLogs) {
const codeWithoutLogs = await removeLogsFromCode(options);
await outputFile(options.outputFile, codeWithoutLogs);
options.addedLogs = false;
}
outro(green('All tests passed!'));
return;
}
Expand All @@ -140,3 +155,38 @@ export async function runAll(
}
return results;
}

async function addLogsToCode(options: Options): Promise<string> {
const codeWithLogs = await getSimpleCompletion({
messages: [
{
role: 'system',
content:
'You are an assistant that helps improve code by adding logs for debugging.',
},
{
role: 'user',
content: `Please add detailed logs to the following code to help debug repeated test failures:\n\n<code>${options.priorCode}</code>\n\nThe error you received on that code was:\n\n<error>${options.lastRunError}</error>`,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to pass the test failure text in here too so it has more context about where things are stuck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for an optimal prompt, when we have multiple variables, we should wrap these in xml tags, like

content: `Please add detailed logs to the following code to help debug repeated test failures:\n\n<code>${options.priorCode}</code>\n\nThe error you received on that code was:\n\n<error>${options.lastRunError}</error>`,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

],
});

return codeWithLogs;
}

async function removeLogsFromCode(options: Options): Promise<string> {
const codeWithoutLogs = await getSimpleCompletion({
messages: [
{
role: 'system',
content:
'You are an assistant that helps clean up code by removing logs.',
},
{
role: 'user',
content: `Please remove all logs from the following code:\n\n${options.priorCode}`,
},
],
});
return codeWithoutLogs;
}
27 changes: 16 additions & 11 deletions src/helpers/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ export const fail = (message: string) => {
const testFail = (message: string, options: RunOptions) => {
prevTestFailures.push(message);
if (hasFailedNTimesWithTheSameMessage(message)) {
outro(
red(
'Your test command is failing with the same error several times. Please make sure your test command is correct. Aborting...'
)
);
console.log(
`${green('To continue, run:')}\n${gray(
`${createCommandString(options)}`
)}\n`
);
process.exit(1);
if (!options.addedLogs) {
options.addedLogs = true;
return fail('Repeated test failures detected. Adding logs to the code.');
} else {
outro(
red(
'Your test command is failing with the same error several times. Please make sure your test command is correct. Aborting...'
)
);
console.log(
`${green('To continue, run:')}\n${gray(
`${createCommandString(options)}`
)}\n`
);
process.exit(1);
}
}
return fail(message);
};
Expand Down
Loading