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

Draft PR showcasing how to use filtered queries in the extension. #2661

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type AutoModelQueryOptions = {
databaseItem: DatabaseItem;
qlpack: QlPacksForLanguage;
sourceInfo: SourceInfo | undefined;
additionalPacks: string[];
extensionPacks: string[];
queryStorageDir: string;

Expand All @@ -52,6 +53,7 @@ async function runAutoModelQuery({
databaseItem,
qlpack,
sourceInfo,
additionalPacks,
extensionPacks,
queryStorageDir,
progress,
Expand Down Expand Up @@ -99,7 +101,7 @@ async function runAutoModelQuery({
quickEvalCountOnly: false,
},
false,
getOnDiskWorkspaceFolders(),
additionalPacks,
extensionPacks,
queryStorageDir,
undefined,
Expand Down Expand Up @@ -153,6 +155,7 @@ type AutoModelQueriesOptions = {
queryStorageDir: string;

progress: ProgressCallback;
extraExtensionPacks?: string[];
};

export type AutoModelQueriesResult = {
Expand All @@ -166,6 +169,7 @@ export async function runAutoModelQueries({
databaseItem,
queryStorageDir,
progress,
extraExtensionPacks = [],
}: AutoModelQueriesOptions): Promise<AutoModelQueriesResult | undefined> {
// maxStep for this part is 1500
const maxStep = 1500;
Expand All @@ -189,7 +193,10 @@ export async function runAutoModelQueries({
sourceLocationPrefix,
};

const additionalPacks = getOnDiskWorkspaceFolders();
const additionalPacks = [
...getOnDiskWorkspaceFolders(),
...extraExtensionPacks,
];
const extensionPacks = Object.keys(
await cliServer.resolveQlpacks(additionalPacks, true),
);
Expand All @@ -208,6 +215,7 @@ export async function runAutoModelQueries({
databaseItem,
qlpack,
sourceInfo,
additionalPacks,
extensionPacks,
queryStorageDir,
progress: (update) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ import { pickExtensionPack } from "./extension-pack-picker";
import { getLanguageDisplayName } from "../common/query-language";
import { runAutoModelQueries } from "./auto-model-codeml-queries";
import { createAutoModelV2Request } from "./auto-model-v2";
import { load as loadYaml } from "js-yaml";
import { load as loadYaml, dump as dumpYaml } from "js-yaml";
import { loadDataExtensionYaml } from "./yaml";
import { extLogger } from "../common/logging/vscode";
import { dir } from "tmp-promise";
import { writeFile, outputFile } from "fs-extra";
import { autoPickExtensionsDirectory } from "./extensions-workspace-folder";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused import autoPickExtensionsDirectory.
import { sign } from "crypto";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused import sign.

export class DataExtensionsEditorView extends AbstractWebview<
ToDataExtensionsEditorMessage,
Expand Down Expand Up @@ -380,13 +385,54 @@ export class DataExtensionsEditorView extends AbstractWebview<
let predictedModeledMethods: Record<string, ModeledMethod>;

if (useLlmGenerationV2()) {
// Generate a qlpack with a filter that only includes the usages we want to model.
const packDir = (await dir({ unsafeCleanup: true })).path;
Copy link
Contributor

Choose a reason for hiding this comment

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

This dir will be cleaned when vscode exits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should make sure it does. I am working on the actual PR right now, this was mostly to a showcase to show how we could use the queries.


const syntheticConfigPack = {
name: "codeql/automodel-filter",
version: "0.0.0",
library: true,
extensionTargets: {
[`codeql/${this.databaseItem.language}-all`]: "*",
},
dataExtensions: ["filter.yml"],
};
Comment on lines +391 to +399
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice


const qlpackFile = join(packDir, "codeql-pack.yml");
await outputFile(qlpackFile, dumpYaml(syntheticConfigPack), "utf8");

// TODO: this filter is just static as an example. We want to generate this from the usages.
const filter = {
extensions: [
{
addsTo: {
pack: `codeql/${this.databaseItem.language}-queries`,
extensible: "automodelCandidateFilter",
},
data: [
["org.sql2o", "Sql2o", "open", "()"],
[
"org.springframework.boot",
"SpringApplication",
"run",
"(Class,String[])",
],
],
},
],
};

const filterFile = join(packDir, "filter.yml");
await writeFile(filterFile, dumpYaml(filter), "utf8");

const usages = await runAutoModelQueries({
mode: this.mode,
cliServer: this.cliServer,
queryRunner: this.queryRunner,
queryStorageDir: this.queryStorageDir,
databaseItem: this.databaseItem,
progress: (update) => progress({ ...update, maxStep }),
extraExtensionPacks: [packDir],
Copy link
Contributor

Choose a reason for hiding this comment

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

I was expecting to see the name of the extension pack here. I haven't looked at the entire implementation, so you may already be doing this properly. The --extension-packs argument must be the name (and optional version) of the extension/model pack you want to use. And this pack must be available either in the package cache or from addtional-packs or from the workspace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored this slightly in my other PR. I moved package generation to auto-model-codeml-queries.ts. That means we can just do:

const additionalPacks = [...getOnDiskWorkspaceFolders(), filterPackDir];
const extensionPacks = Object.keys(
    await cliServer.resolveQlpacks(additionalPacks, true),
  );

and pass those directly to the run method:

  const candidates = await runAutoModelQuery({
    ...
    additionalPacks,
    extensionPacks,
    ...
  });

});
if (!usages) {
return;
Expand All @@ -398,6 +444,38 @@ export class DataExtensionsEditorView extends AbstractWebview<
message: "Creating request",
});

// TODO: TEMP LOGGING CODE - START
const results = usages.candidates.runs[0].results;
void extLogger.log(`CANDIDATES:`);
results?.forEach((result) => {
const pckage =
result.relatedLocations?.[1].physicalLocation?.artifactLocation?.uri?.substring(
6,
);
const tp =
result.relatedLocations?.[2].physicalLocation?.artifactLocation?.uri?.substring(
6,
);
const method =
result.relatedLocations?.[4].physicalLocation?.artifactLocation?.uri?.substring(
6,
);
let signature =
result.relatedLocations?.[5].physicalLocation?.artifactLocation?.uri?.substring(
6,
);
signature = signature && decodeURI(signature);
let input =
result.relatedLocations?.[6].physicalLocation?.artifactLocation?.uri?.substring(
6,
);
input = input && decodeURI(input);
void extLogger.log(
`${pckage}.${tp}.${method}${signature} @ ${input}`,
);
});
// TODO: TEMP LOGGING CODE - END

const request = await createAutoModelV2Request(this.mode, usages);

progress({
Expand Down