Skip to content

feat(ci,scripts): add Go and TypeScript similarity advisories to codequality - #1270

Merged
kyasbal merged 3 commits into
mainfrom
push-lzwrunkwzltq
Nov 13, 2026
Merged

kyasbal merged 3 commits into
mainfrom
push-lzwrunkwzltq

Conversation

@kyasbal

@kyasbal kyasbal commented Nov 13, 2026

Copy link
Copy Markdown
Member

Summary

Integrates multi-language (Go and TypeScript) AST-normalized semantic function similarity advisories into scripts/codequality using Vertex AI gemini-embedding-2 via google.golang.org/genai, helping developers and AI agents spot existing shared helpers before duplicating logic across packages.

Key Changes

  • Language-Agnostic Similarity Engine (scripts/codequality/internal/simcheck):
    • Defines the LanguageAnalyzer interface (FileInput batch extraction, AST identifier normalization, corpus discovery, and shared-module classification) and Registry so additional languages can be plugged into a single differential evaluation pipeline (evaluator.go).
    • Evaluates newly added substantive units (>= 3 statements and >= 6 code lines) against same-language production corpus units using AST-Norm cosine similarity (0.940 general threshold, 0.915 boosted threshold for shared modules, 0.960 intra-change threshold).
    • Lazily resolves embeddings only when new units are introduced, gracefully skips with a clear status message when GCP project/ADC credentials or network access are unavailable (including unauthenticated CI environments), and never blocks the exit code (Advisory).
  • Vertex AI Embeddings & Persistent SQLite Cache (vertex.go, cache.go):
    • Uses the official google.golang.org/genai SDK (BackendVertexAI, Models.EmbedContent, gemini-embedding-2 at 768 dimensions on locations/global) with concurrent worker batching and automatic X-Goog-User-Project quota-project header propagation.
    • Persists L2-normalized vectors in a WAL-mode pure-Go SQLite cache (modernc.org/sqlite, ~/.cache/khi/codesim/embeddings_v1.db) shared across workspaces for near-instant incremental runs.
  • Go & TypeScript Language Analyzers (simcheck/langgo, simcheck/langts):
    • langgo: Extracts Go functions, methods, and top-level KHI task closures (var xxxTask = Define...Task(...)) via go/ast, normalizing local/parameter/receiver identifiers while preserving external selector names and reusing astcheck / rule AST helpers.
    • langts: Drives the web/node_modules/typescript Compiler API in a single batch node invocation (//go:embed extract_ts.js) to extract and AST-normalize TypeScript functions, class methods, and substantive Angular signal/computed/effect closures (web/src/app/{common,utils,shared} marked [shared]).
  • Unified codequality CLI, Reporting & Docs:
    • Wires a shared lazy similaritySession (--similarity, default true, CODEQUALITY_SIMILARITY ?= true) across both executeGoPipeline (including --fast) and executeTSPipeline, rendering advisory tables and code previews in terminal output and GITHUB_STEP_SUMMARY.
    • Updates scripts/make/testing.mk, .agents/rules/{go,ts}-coding-rule.md, and EN/JA development guides.

Verification

  • make codequality-fast: PASS (0 violations, 95 new units checked against 2,444 corpus units, 0 similar matches)
  • make codequality-go CODEQUALITY_MAX=0: PASS (95.0% patch coverage, 100.0% mutation score — 370/370 killed, 0 survived, 0 violations)
  • End-to-end live Vertex AI verification (cold SQLite cache bootstrap + synthetic Go and TypeScript semantic clones verified across make codequality, make codequality-fast, and make codequality-web)
  • make build-go, make test-go, make lint-go, make lint-web, make pre-commit: PASS

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an AST-normalized function similarity checking feature to the 'codequality' tool for both Go and TypeScript, leveraging Vertex AI ('gemini-embedding-2') and a local SQLite-backed vector cache to detect duplicate logic. A valid issue was identified in 'vertex.go' where slicing 'item.text' directly by byte index could cut a multi-byte UTF-8 character in half, and a code suggestion was provided to safely truncate the string using runes.

}

func (c *VertexClient) embedSingle(ctx context.Context, item pendingItem) (CacheEntry, error) {
trimmed := item.text[:min(len(item.text), maxInstanceChars)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Slicing item.text directly by byte index can cut a multi-byte UTF-8 character in half, resulting in an invalid UTF-8 string. Converting the string to a slice of runes before slicing ensures safe truncation of UTF-8 characters.

Suggested change
trimmed := item.text[:min(len(item.text), maxInstanceChars)]
runes := []rune(item.text)
if len(runes) > maxInstanceChars {
runes = runes[:maxInstanceChars]
}
trimmed := string(runes)
@kyasbal
kyasbal merged commit ae0a74c into main Nov 13, 2026
22 checks passed
@kyasbal
kyasbal deleted the push-lzwrunkwzltq branch November 13, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant