{"id":26130006,"url":"https://github.com/sturdy-dev/suspicious","last_synced_at":"2025-04-13T18:53:20.317Z","repository":{"id":63767481,"uuid":"569724351","full_name":"sturdy-dev/suspicious","owner":"sturdy-dev","description":"Catching bugs in code with AI, fully local CLI app","archived":false,"fork":false,"pushed_at":"2024-03-19T03:45:02.000Z","size":767,"stargazers_count":58,"open_issues_count":2,"forks_count":7,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-27T09:45:34.675Z","etag":null,"topics":["ai","codereview"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sturdy-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-23T13:25:39.000Z","updated_at":"2025-03-20T19:49:02.000Z","dependencies_parsed_at":"2023-02-09T11:47:47.440Z","dependency_job_id":null,"html_url":"https://github.com/sturdy-dev/suspicious","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsuspicious","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsuspicious/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsuspicious/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sturdy-dev%2Fsuspicious/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sturdy-dev","download_url":"https://codeload.github.com/sturdy-dev/suspicious/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766142,"owners_count":21158296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","codereview"],"created_at":"2025-03-10T20:00:04.215Z","updated_at":"2025-04-13T18:53:20.293Z","avatar_url":"https://github.com/sturdy-dev.png","language":"Python","readme":"# Suspicious\n\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"200\" src=\"https://raw.githubusercontent.com/sturdy-dev/suspicious/main/docs/ai_dog_wtf.png\"\u003e\n\u003c/p\n\n\u003cp align='center'\u003e\n    Catching bugs in code with AI, fully local CLI app. No data leaves your computer.\n\u003c/p\u003e\n\u003cp align='center'\u003e\n    \u003ca href=\"https://github.com/sturdy-dev/suspicious/blob/main/LICENSE.txt\"\u003e\n        \u003cimg alt=\"GitHub\"\n        src=\"https://img.shields.io/github/license/sturdy-dev/suspicious\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/suspicious\"\u003e\n     \u003cimg alt=\"PyPi\"\n src=\"https://img.shields.io/pypi/v/suspicious\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#overview\"\u003e🤔 Overview\u003c/a\u003e •\n  \u003ca href=\"#demo\"\u003e🪄 Demos\u003c/a\u003e •\n  \u003ca href=\"#installation\"\u003e🔧 Installation\u003c/a\u003e •\n  \u003ca href=\"#usage\"\u003e💻 Usage\u003c/a\u003e •\n  \u003ca href=\"#how-does-it-work\"\u003e🧠 How it works\u003c/a\u003e\n\u003c/p\u003e\n\n-------------------------------------------------------------------\n\n## Overview \n\nThis is a CLI application that analyzes a source code file using an AI model. It then shows you parts that look suspicious to it.\n\nIt does **not** use rules or static analysis the way a linter tool would. Instead, the model generates its own code suggestions based on the surrounding context. Check out [how it works](#how-does-it-work).\n\n\u003e NB: All processing is done on your hardware and no data is transmitted to the Internet\n\nExample output:\n\n![example results](./docs/screenshot.png)\n\n## Demo\n\nHere's the output of running the application on its own source files (so meta).\n\n- `cli.py` — [source code](./src/suspicious/cli.py) → [generated output](https://sturdy-dev.github.io/suspicious/demos/cli_py/)\n- `render.py` — [source code](./src/suspicious/render.py) → [generated output](https://sturdy-dev.github.io/suspicious/demos/render_py/)\n- `sus.py` — [source code](./src/suspicious/sus.py) → [generated output](https://sturdy-dev.github.io/suspicious/demos/sus_py/)\n\n## Have I seen this before?\n\nThere was this post [AI found a bug in my code](https://news.ycombinator.com/item?id=33632610) on Hacker News which was pretty cool. I wanted to try it on my own code, so I went ahead and built my implementation of the idea.\n\n## Installation\n\nYou can install `sus` via `pip` or from the source.\n\n### Pip (MacOS, Linux, Windows)\n\n```bash\npip3 install suspicious\n```\n\n### From source\n\n```bash\ngit clone git@github.com:sturdy-dev/suspicious.git\ncd suspicious\npython -m pip install .\n```\n\n## Usage\n\nYou can run the program like this:\n\n```bash\nsus /path/to/file.py\n```\n\n\u003e Note that when you run this for the first time, the application will need to download a model (~500 MB) — [more info](#model) section.\n\nThis will generate and open an `.html` file with the results.\n\n- `grey` means prediction is the same as the original\n- `light grey` means the model had a different prediction but with super low confidence\n- `light red` means things are looking a little sus\n- `red` means there was a different prediction and confidence was higher\n\n### Practical usage\n\nUnclear. You run `sus` on a file and skim over the red stuff, maybe it spots something you missed. Ping me on [twitter](https://twitter.com/krlvi) if you catch something cool with it.\n\n## How does it work?\n\nIn a nutshell, it feeds a tokenized representation of your source text into a Transformer model and asks the model to predict one token at a time using [Masked Language Modelling](https://huggingface.co/docs/transformers/tasks/language_modeling#masked-language-modeling).\n\nFor a general overview about Transformer models, check out [The Illustrated Transformer](https://jalammar.github.io/illustrated-transformer/) article by Jay Alammar, which helped me out in understanding the core ideas.\n\n`sus` uses a model called [UniXcoder](https://github.com/microsoft/CodeBERT/tree/master/UniXcoder) which has been trained on the [CodeSearchNet](https://huggingface.co/datasets/code_search_net) dataset. To do the MLM (masked language modelling) we are adding a `lm_head` layer.\n\nWhen `sus` processes your code, it first tokenizes the text, where a token could be a special character or programming language keyword, English word or part of a word.\n\nBefore feeding the sequence of token ids to the model, one or multiple tokens are replaced with a special `\u003cmask\u003e` token. After feeding the input through the network, we extract just the value at the masked location. This masking is done in a loop for each token to generate individual predictions.\n\nSince this process is impractically slow, instead of masking one token at a time, `sus` masks 10% of the tokens, making sure that the masked locations are spread out (so that there is sufficient context around each prediction site).\n\nThe output of this entire process is a list of structs that contain the original and predicted values for each token. Example:\n\n```json5\n{\n    \"idx\": 0, // position in sequence\n    \"original\": \"foo\", // as originally written in the source file\n    \"predicted\": \"bar\", // what the model predicted\n    \"cosine_similarity\": 0.23, // how different the prediction is from the original in the vector space\n    \"probability\": 0.92, // how confident the model is in it's prediction\n}\n```\n\nThis is then fed into an `html` template to be rendered for the user. Easy-peasy.\n\n### Model\n\n`sus` uses the decoder of [UniXcoder](https://github.com/microsoft/CodeBERT/tree/master/UniXcoder), specifically the [unixcoder-base-nine](https://huggingface.co/microsoft/unixcoder-base-nine) checkpoint. What's cool is that it's only 500 MB and ~120M parameters, which means it's quick to download and fast enough to run locally.\n\nLarger models produce higher quality outputs, but you need to run the inference on a server.\n\n## Supported languages\n\nYou can try `sus` on any source file, but you can expect best results with the following languages:\n\n- java\n- ruby\n- python\n- php\n- javascript\n- go\n- c\n- c++\n- c#\n\n## Bugs and limitations\n\n- Accuracy — `sus` is meant to be executed locally (aka not sending code to a server), which puts some constraints on the AI model size. Larger models will produce higher quality results, but they can be tens of GB in size and without a beefy GPU could take a long time to generate the output. Because of this, `sus` uses a [modestly sized model](#model).\n- Large files — The [model](#model) also puts constraints on the input size (analyzed file size). `sus` works around this by batching the input, but as a result of this, batches are not aware of the 'context' / code that is in other batches. Files are split in batches of 2500 characters which is super crude and is meant to correspond to ~1024 tokens.\n- [Masking](#how-does-it-work) is done on per token basis. It could be interesting to first generate syntax tree from the code and then mask the entire node instead.\n\n## License\n\nSemantic Code Search is distributed under [AGPL-3.0-only](LICENSE.txt). For Apache-2.0 exceptions — \u003ckiril@codeball.ai\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsturdy-dev%2Fsuspicious","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsturdy-dev%2Fsuspicious","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsturdy-dev%2Fsuspicious/lists"}