Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

ci(codeql): drop name-only Rust alerts on test code before upload

AkitaOnRails4367f9e459e4
diff --git a/.github/codeql/filter_test_alerts.py b/.github/codeql/filter_test_alerts.py
new file mode 100644
index 00000000..f77fe044
--- /dev/null
+++ b/.github/codeql/filter_test_alerts.py
@@ -0,0 +1,207 @@
+"""Drop CodeQL results that only fire on Rust test code before upload.
+
+Three Rust queries match on names, not on data flow from a real secret:
+`rust/cleartext-logging` and `rust/cleartext-transmission` treat any variable
+called `session_id`, `secret`, `token` or `username` as sensitive, and
+`rust/hard-coded-cryptographic-value` flags every literal key a test feeds a
+cipher or a token check. In tests those names hold synthetic fixtures and the
+"log" is an assertion message, so each new test re-opened alerts that were
+dismissed one by one (more than 80 of them so far).
+
+Results for those rules are removed only when they sit in test code: a file
+under a `tests/` directory, the dev-only test-support crate, a line inside a
+top-level `#[cfg(test)]` module, or the file (and submodules) of a top-level
+`#[cfg(test)] mod name;` declaration. Every other query, and these three on
+production code, reach code scanning unchanged. A source file that cannot be
+read keeps its results.
+
+Usage: python3 -I filter_test_alerts.py <root> <file.sarif>...
+"""
+
+import json
+import re
+import sys
+from pathlib import Path
+
+USAGE = "usage: python3 -I filter_test_alerts.py <root> <file.sarif>..."
+FILTERED_RULES = frozenset(
+ {
+ "rust/cleartext-logging",
+ "rust/cleartext-transmission",
+ "rust/hard-coded-cryptographic-value",
+ }
+)
+TEST_CRATE_PREFIX = "crates/ai-memory-test-support/"
+# rustfmt puts a top-level item's header and closing brace at column 0.
+TEST_MOD_HEADER = re.compile(r"^(pub(\([a-z]+\))? )?mod \w+ \{$")
+TEST_MOD_DECL = re.compile(r"^(pub(\([a-z]+\))? )?mod (\w+);$")
+PATH_ATTR = re.compile(r'^#\[path = "([^"]+)"\]$')
+SOURCE_DIRS = ("crates", "companions", "evals")
+
+
+def cfg_test_item(lines, index):
+ """For a column-0 `#[cfg(test)]` at `index`, the index of the item it gates
+ and the value of any `#[path = "..."]` attribute in between."""
+ if lines[index] != "#[cfg(test)]":
+ return None, None
+ item = index + 1
+ path_attr = None
+ while item < len(lines) and lines[item].startswith("#["):
+ match = PATH_ATTR.match(lines[item])
+ if match:
+ path_attr = match.group(1)
+ item += 1
+ return (item, path_attr) if item < len(lines) else (None, None)
+
+
+def test_module_ranges(lines):
+ """1-based inclusive line ranges of top-level `#[cfg(test)]` modules."""
+ ranges = []
+ index = 0
+ while index < len(lines):
+ header, _ = cfg_test_item(lines, index)
+ if header is None or not TEST_MOD_HEADER.match(lines[header]):
+ index += 1
+ continue
+ end = header + 1
+ while end < len(lines) and lines[end] != "}":
+ end += 1
+ ranges.append((header + 1, end + 1))
+ index = end + 1
+ return ranges
+
+
+def out_of_line_test_modules(relative, lines):
+ """Paths (relative to the root) a file's top-level `#[cfg(test)] mod x;` load.
+
+ Each entry is a module file plus the directory its own submodules live
+ in, following rustc's rules: `#[path]` is relative to the declaring file's
+ directory; otherwise `lib.rs`, `main.rs` and `mod.rs` keep children beside
+ themselves and any other `stem.rs` keeps them under `stem/`.
+ """
+ parent = relative.parent
+ own_dir = parent if relative.name in ("lib.rs", "main.rs", "mod.rs") else parent / relative.stem
+ found = []
+ for index in range(len(lines)):
+ decl, path_attr = cfg_test_item(lines, index)
+ match = TEST_MOD_DECL.match(lines[decl]) if decl is not None else None
+ if not match:
+ continue
+ if path_attr:
+ candidates = [parent / path_attr]
+ else:
+ name = match.group(3)
+ candidates = [own_dir / f"{name}.rs", own_dir / name / "mod.rs"]
+ for module in candidates:
+ sub_dir = module.parent if module.name == "mod.rs" else module.parent / module.stem
+ found.append((normalize(module), normalize(sub_dir) + "/"))
+ return found
+
+
+def normalize(path):
+ parts = []
+ for part in path.parts:
+ if part == "..":
+ if parts:
+ parts.pop()
+ elif part != ".":
+ parts.append(part)
+ return "/".join(parts)
+
+
+class TestCode:
+ def __init__(self, root):
+ self.root = Path(root)
+ self.cache = {}
+ self.test_files = None
+ self.test_dirs = None
+
+ def scan_out_of_line_modules(self):
+ self.test_files, self.test_dirs = set(), set()
+ for top in SOURCE_DIRS:
+ for source in sorted((self.root / top).rglob("*.rs")):
+ try:
+ lines = source.read_text(encoding="utf-8").splitlines()
+ except (OSError, UnicodeDecodeError):
+ continue
+ relative = source.relative_to(self.root)
+ for module, sub_dir in out_of_line_test_modules(relative, lines):
+ self.test_files.add(module)
+ self.test_dirs.add(sub_dir)
+
+ def contains(self, uri, line):
+ if not uri.endswith(".rs"):
+ return False
+ if "/tests/" in f"/{uri}" or uri.startswith(TEST_CRATE_PREFIX):
+ return True
+ if self.test_files is None:
+ self.scan_out_of_line_modules()
+ if uri in self.test_files or any(uri.startswith(d) for d in self.test_dirs):
+ return True
+ if uri not in self.cache:
+ try:
+ text = (self.root / uri).read_text(encoding="utf-8")
+ except (OSError, UnicodeDecodeError):
+ self.cache[uri] = None
+ else:
+ self.cache[uri] = test_module_ranges(text.splitlines())
+ ranges = self.cache[uri]
+ if ranges is None or line is None:
+ return False
+ return any(start <= line <= end for start, end in ranges)
+
+
+def location_of(result):
+ try:
+ physical = result["locations"][0]["physicalLocation"]
+ uri = physical["artifactLocation"]["uri"]
+ except (KeyError, IndexError, TypeError):
+ return None, None
+ return uri, physical.get("region", {}).get("startLine")
+
+
+def rule_of(run, result):
+ if "ruleId" in result:
+ return result["ruleId"]
+ try:
+ index = result["rule"]["index"]
+ return run["tool"]["driver"]["rules"][index]["id"]
+ except (KeyError, IndexError, TypeError):
+ return None
+
+
+def filter_sarif(sarif, test_code):
+ """Remove filtered test-code results in place; return what was dropped."""
+ dropped = []
+ for run in sarif.get("runs", []):
+ kept = []
+ for result in run.get("results", []):
+ rule = rule_of(run, result)
+ uri, line = location_of(result)
+ if rule in FILTERED_RULES and uri and test_code.contains(uri, line):
+ dropped.append(f"{rule} {uri}:{line}")
+ else:
+ kept.append(result)
+ if "results" in run:
+ run["results"] = kept
+ return dropped
+
+
+def main(argv):
+ if len(argv) < 3:
+ print(USAGE, file=sys.stderr)
+ return 2
+ test_code = TestCode(argv[1])
+ for name in argv[2:]:
+ path = Path(name)
+ sarif = json.loads(path.read_text(encoding="utf-8"))
+ dropped = filter_sarif(sarif, test_code)
+ path.write_text(json.dumps(sarif), encoding="utf-8")
+ print(f"{path}: dropped {len(dropped)} test-code result(s)")
+ for entry in dropped:
+ print(f" {entry}")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv))
diff --git a/.github/codeql/test_filter_test_alerts.py b/.github/codeql/test_filter_test_alerts.py
new file mode 100644
index 00000000..dfc7f28c
--- /dev/null
+++ b/.github/codeql/test_filter_test_alerts.py
@@ -0,0 +1,146 @@
+"""Tests for filter_test_alerts.py: python3 -I -m unittest discover -s .github/codeql"""
+
+import tempfile
+import unittest
+from pathlib import Path
+
+import filter_test_alerts as f
+
+SOURCE = """\
+use std::fmt;
+
+pub fn log_session(session_id: &str) {
+ eprintln!("session {session_id}");
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn logs() {
+ let session_id = "s";
+ assert!(true, "{session_id}");
+ }
+}
+
+pub fn after() {}
+"""
+
+
+def result(rule, uri, line):
+ return {
+ "ruleId": rule,
+ "locations": [
+ {
+ "physicalLocation": {
+ "artifactLocation": {"uri": uri},
+ "region": {"startLine": line},
+ }
+ }
+ ],
+ }
+
+
+class FilterTest(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ root = Path(self.tmp.name)
+ (root / "crates/x/src").mkdir(parents=True)
+ (root / "crates/x/src/lib.rs").write_text(SOURCE, encoding="utf-8")
+ self.code = f.TestCode(root)
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def run_filter(self, *results):
+ sarif = {"runs": [{"tool": {"driver": {"rules": []}}, "results": list(results)}]}
+ dropped = f.filter_sarif(sarif, self.code)
+ return [(r["ruleId"], f.location_of(r)[1]) for r in sarif["runs"][0]["results"]], dropped
+
+ def test_inline_test_module_result_is_dropped(self):
+ kept, dropped = self.run_filter(result("rust/cleartext-logging", "crates/x/src/lib.rs", 12))
+ self.assertEqual(kept, [])
+ self.assertEqual(len(dropped), 1)
+
+ def test_production_code_result_is_kept(self):
+ # Control: the same rule on the non-test function stays.
+ kept, _ = self.run_filter(result("rust/cleartext-logging", "crates/x/src/lib.rs", 4))
+ self.assertEqual(kept, [("rust/cleartext-logging", 4)])
+
+ def test_code_after_the_test_module_is_kept(self):
+ kept, _ = self.run_filter(result("rust/cleartext-logging", "crates/x/src/lib.rs", 16))
+ self.assertEqual(kept, [("rust/cleartext-logging", 16)])
+
+ def test_tests_directory_result_is_dropped(self):
+ kept, _ = self.run_filter(
+ result("rust/hard-coded-cryptographic-value", "crates/x/tests/suite/a.rs", 3)
+ )
+ self.assertEqual(kept, [])
+
+ def test_other_rules_in_test_code_are_kept(self):
+ kept, _ = self.run_filter(result("rust/sql-injection", "crates/x/tests/suite/a.rs", 3))
+ self.assertEqual(kept, [("rust/sql-injection", 3)])
+
+ def test_unreadable_source_keeps_its_result(self):
+ kept, _ = self.run_filter(result("rust/cleartext-logging", "crates/x/src/missing.rs", 2))
+ self.assertEqual(kept, [("rust/cleartext-logging", 2)])
+
+ def test_rule_resolved_through_its_index(self):
+ sarif = {
+ "runs": [
+ {
+ "tool": {"driver": {"rules": [{"id": "rust/cleartext-logging"}]}},
+ "results": [
+ {
+ "rule": {"index": 0},
+ "locations": result("", "crates/x/tests/a.rs", 1)["locations"],
+ }
+ ],
+ }
+ ]
+ }
+ f.filter_sarif(sarif, self.code)
+ self.assertEqual(sarif["runs"][0]["results"], [])
+
+ def test_module_ranges_skip_a_cfg_test_function(self):
+ lines = ["#[cfg(test)]", "fn helper() {}", "#[cfg(test)]", "mod tests {", " x", "}"]
+ self.assertEqual(f.test_module_ranges(lines), [(4, 6)])
+
+ def test_out_of_line_module_paths_follow_rustc(self):
+ from pathlib import PurePosixPath as P
+
+ decls = [
+ "#[cfg(test)]",
+ '#[path = "equivalence.rs"]',
+ "mod equivalence;",
+ "#[cfg(test)]",
+ "pub(crate) mod oracle;",
+ "mod not_test;",
+ ]
+ self.assertEqual(
+ f.out_of_line_test_modules(P("crates/x/src/router.rs"), decls),
+ [
+ ("crates/x/src/equivalence.rs", "crates/x/src/equivalence/"),
+ ("crates/x/src/router/oracle.rs", "crates/x/src/router/oracle/"),
+ ("crates/x/src/router/oracle/mod.rs", "crates/x/src/router/oracle/"),
+ ],
+ )
+ suite = ["#[cfg(test)]", '#[path = "../tests/suite/mod.rs"]', "mod integration;"]
+ self.assertEqual(
+ f.out_of_line_test_modules(P("crates/x/src/lib.rs"), suite),
+ [("crates/x/tests/suite/mod.rs", "crates/x/tests/suite/")],
+ )
+
+ def test_out_of_line_test_module_file_is_dropped(self):
+ root = Path(self.tmp.name)
+ (root / "crates/x/src/lib.rs").write_text(
+ SOURCE + '#[cfg(test)]\n#[path = "oracle.rs"]\nmod oracle;\n', encoding="utf-8"
+ )
+ (root / "crates/x/src/oracle.rs").write_text("fn t() {}\n", encoding="utf-8")
+ (root / "crates/x/src/live.rs").write_text("fn t() {}\n", encoding="utf-8")
+ code = f.TestCode(root)
+ self.assertTrue(code.contains("crates/x/src/oracle.rs", 1))
+ self.assertFalse(code.contains("crates/x/src/live.rs", 1))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
new file mode 100644
index 00000000..4c98a69b
--- /dev/null
+++ b/.github/workflows/codeql.yml
@@ -0,0 +1,65 @@
+name: CodeQL
+
+# Advanced setup in place of GitHub's default setup, for one reason: the
+# results pass through .github/codeql/filter_test_alerts.py before upload, so
+# the name-based Rust queries stop re-opening alerts on synthetic test
+# fixtures. Languages, triggers and the weekly schedule match what default
+# setup ran. The model packs in .github/codeql/extensions still load
+# automatically.
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+ schedule:
+ - cron: "23 4 * * 1"
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: codeql-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+jobs:
+ analyze:
+ name: analyze (${{ matrix.language }})
+ runs-on: ${{ matrix.os }}
+ permissions:
+ contents: read
+ security-events: write
+ actions: read
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - { language: actions, build-mode: none, os: ubuntu-latest }
+ - { language: javascript-typescript, build-mode: none, os: ubuntu-latest }
+ - { language: python, build-mode: none, os: ubuntu-latest }
+ - { language: rust, build-mode: none, os: ubuntu-latest }
+ - { language: swift, build-mode: manual, os: macos-latest }
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ - uses: github/codeql-action/init@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
+ with:
+ languages: ${{ matrix.language }}
+ build-mode: ${{ matrix.build-mode }}
+ - name: Build the macOS companion
+ if: matrix.language == 'swift'
+ run: swift build --package-path companions/ai-memory-macos
+ - uses: github/codeql-action/analyze@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
+ with:
+ category: /language:${{ matrix.language }}
+ upload: never
+ output: sarif-results
+ - name: Test the alert filter
+ run: python3 -I -m unittest discover -s .github/codeql
+ - name: Drop name-only alerts on test code
+ run: python3 -I .github/codeql/filter_test_alerts.py . sarif-results/*.sarif
+ - uses: github/codeql-action/upload-sarif@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
+ with:
+ sarif_file: sarif-results
+ category: /language:${{ matrix.language }}
diff --git a/AGENTS.md b/AGENTS.md
index 63aca909..cb01573c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -404,6 +404,14 @@ covers the same tests, slower, no tiers.
listed in `ci.yml`), and differential gitleaks scanning.
`.github/workflows/secret-scan.yml` runs the separate weekly/manual
full-history gitleaks scan.
+- **CodeQL** runs from `.github/workflows/codeql.yml` (advanced setup, not
+ GitHub's default setup). Before upload it drops the three name-based Rust
+ queries (`cleartext-logging`, `cleartext-transmission`,
+ `hard-coded-cryptographic-value`) on test code only: `tests/` directories,
+ the test-support crate and `#[cfg(test)]` modules. Production code keeps every
+ query; a session id or username printed there goes through the log-barrier
+ helpers in `.github/codeql/extensions`. Run the filter's own tests with
+ `python3 -I -m unittest discover -s .github/codeql`.
- **Windows runs in its own workflow** (`.github/workflows/windows.yml`):
nightly, on dispatch, and automatically on any PR touching the
platform-sensitive paths in its `changes` filter (the wiki, store and