Skip to content

Commit fd23fd9

Browse files
authored
feat: adds usage_rules (#74)
1 parent faf2616 commit fd23fd9

File tree

5 files changed

+162
-26
lines changed

5 files changed

+162
-26
lines changed

.github/copilot-instructions.md

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,6 @@ Navigator is a real-time collaborative threat modeling tool built with Elixir/Ph
1515
### Bootstrap and Build Process
1616
**NEVER CANCEL builds or long-running commands. Set timeouts to 60+ minutes.**
1717

18-
### Docker Compose Approach (RECOMMENDED)
19-
**NOTE**: The Dockerfile currently has network connectivity issues that prevent the full build. Use this approach for database only or with manual fixes.
20-
21-
```bash
22-
# Start database only (VALIDATED - works correctly)
23-
docker compose up db -d
24-
# Takes 10-30 seconds for PostgreSQL 13 to start
25-
26-
# Build and run the full application stack (requires Dockerfile fixes)
27-
docker compose up --build
28-
```
29-
- **NEVER CANCEL**: Initial build would take 15-45 minutes if network issues are resolved. Set timeout to 60+ minutes.
30-
- Application would run on `http://localhost:4000`
31-
- Database starts successfully in 10-30 seconds
32-
- To use OpenAI features: `OPENAI_API_KEY=sk-proj... docker compose up`
33-
- To rebuild after code changes: `docker compose up -d --no-deps --build app`
34-
35-
**CURRENT ISSUE**: The Dockerfile has network connectivity problems during the build process. Use native development or fix the Dockerfile network issues first.
36-
3718
#### Native Development Setup
3819
**NOTE**: Requires exact Elixir 1.18.4 and Erlang/OTP 27.0.1. Ubuntu packages provide older versions that cause compatibility issues.
3920

@@ -45,8 +26,8 @@ make setup
4526

4627
# Start development server
4728
make dev
48-
# Runs: cd valentine && mix phx.server
49-
# Starts server on http://localhost:4000
29+
# Runs: cd valentine && MIX_ENV=dev mix phx.server
30+
# Starts server on http://0.0.0.0:4000
5031
```
5132

5233
**CURRENT LIMITATION**: The system Elixir version (1.14.0) is incompatible with project requirements (1.18.4). Use version managers like asdf or rely on Docker development.
@@ -85,8 +66,8 @@ make install
8566
After making changes, **ALWAYS** perform these validation steps:
8667

8768
### Basic Application Validation
88-
1. Start the application with `make dev` or `docker compose up`
89-
2. Navigate to `http://localhost:4000`
69+
1. Start the application with `make dev`
70+
2. Navigate to `http://0.0.0.0:4000`
9071
3. Verify the main navigation works (Workspaces, Architecture, Data flow, etc.)
9172
4. Create a new workspace to test basic functionality
9273
5. Test the threat modeling features (create threats, assumptions, mitigations)

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cover:
44
cd valentine && mix test --cover
55

66
dev:
7-
cd valentine && mix phx.server
7+
cd valentine && MIX_ENV=dev mix phx.server
88

99
docker:
1010
docker build --build-arg GIT_SHA=$(shell git rev-parse HEAD) -t valentine valentine/.
@@ -19,4 +19,7 @@ setup:
1919
cd valentine && mix deps.get && mix ecto.create && mix ecto.migrate && mix run priv/repo/seeds.exs && cd assets && npm install
2020

2121
test:
22-
cd valentine && mix test
22+
cd valentine && mix test
23+
24+
usage_rules:
25+
cd valentine && mix usage_rules.sync AGENTS.md --all --inline usage_rules:all --link-to-folder deps

valentine/AGENTS.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!-- usage-rules-start -->
2+
<!-- usage-rules-header -->
3+
# Usage Rules
4+
5+
**IMPORTANT**: Consult these usage rules early and often when working with the packages listed below.
6+
Before attempting to use any of these packages or to discover if you should use them, review their
7+
usage rules to understand the correct patterns, conventions, and best practices.
8+
<!-- usage-rules-header-end -->
9+
10+
<!-- usage_rules-start -->
11+
## usage_rules usage
12+
_A dev tool for Elixir projects to gather LLM usage rules from dependencies_
13+
14+
## Using Usage Rules
15+
16+
Many packages have usage rules, which you should *thoroughly* consult before taking any
17+
action. These usage rules contain guidelines and rules *directly from the package authors*.
18+
They are your best source of knowledge for making decisions.
19+
20+
## Modules & functions in the current app and dependencies
21+
22+
When looking for docs for modules & functions that are dependencies of the current project,
23+
or for Elixir itself, use `mix usage_rules.docs`
24+
25+
```
26+
# Search a whole module
27+
mix usage_rules.docs Enum
28+
29+
# Search a specific function
30+
mix usage_rules.docs Enum.zip
31+
32+
# Search a specific function & arity
33+
mix usage_rules.docs Enum.zip/1
34+
```
35+
36+
37+
## Searching Documentation
38+
39+
You should also consult the documentation of any tools you are using, early and often. The best
40+
way to accomplish this is to use the `usage_rules.search_docs` mix task. Once you have
41+
found what you are looking for, use the links in the search results to get more detail. For example:
42+
43+
```
44+
# Search docs for all packages in the current application, including Elixir
45+
mix usage_rules.search_docs Enum.zip
46+
47+
# Search docs for specific packages
48+
mix usage_rules.search_docs Req.get -p req
49+
50+
# Search docs for multi-word queries
51+
mix usage_rules.search_docs "making requests" -p req
52+
53+
# Search only in titles (useful for finding specific functions/modules)
54+
mix usage_rules.search_docs "Enum.zip" --query-by title
55+
```
56+
57+
58+
<!-- usage_rules-end -->
59+
<!-- usage_rules:elixir-start -->
60+
## usage_rules:elixir usage
61+
# Elixir Core Usage Rules
62+
63+
## Pattern Matching
64+
- Use pattern matching over conditional logic when possible
65+
- Prefer to match on function heads instead of using `if`/`else` or `case` in function bodies
66+
- `%{}` matches ANY map, not just empty maps. Use `map_size(map) == 0` guard to check for truly empty maps
67+
68+
## Error Handling
69+
- Use `{:ok, result}` and `{:error, reason}` tuples for operations that can fail
70+
- Avoid raising exceptions for control flow
71+
- Use `with` for chaining operations that return `{:ok, _}` or `{:error, _}`
72+
73+
## Common Mistakes to Avoid
74+
- Elixir has no `return` statement, nor early returns. The last expression in a block is always returned.
75+
- Don't use `Enum` functions on large collections when `Stream` is more appropriate
76+
- Avoid nested `case` statements - refactor to a single `case`, `with` or separate functions
77+
- Don't use `String.to_atom/1` on user input (memory leak risk)
78+
- Lists and enumerables cannot be indexed with brackets. Use pattern matching or `Enum` functions
79+
- Prefer `Enum` functions like `Enum.reduce` over recursion
80+
- When recursion is necessary, prefer to use pattern matching in function heads for base case detection
81+
- Using the process dictionary is typically a sign of unidiomatic code
82+
- Only use macros if explicitly requested
83+
- There are many useful standard library functions, prefer to use them where possible
84+
85+
## Function Design
86+
- Use guard clauses: `when is_binary(name) and byte_size(name) > 0`
87+
- Prefer multiple function clauses over complex conditional logic
88+
- Name functions descriptively: `calculate_total_price/2` not `calc/2`
89+
- Predicate function names should not start with `is` and should end in a question mark.
90+
- Names like `is_thing` should be reserved for guards
91+
92+
## Data Structures
93+
- Use structs over maps when the shape is known: `defstruct [:name, :age]`
94+
- Prefer keyword lists for options: `[timeout: 5000, retries: 3]`
95+
- Use maps for dynamic key-value data
96+
- Prefer to prepend to lists `[new | list]` not `list ++ [new]`
97+
98+
## Mix Tasks
99+
100+
- Use `mix help` to list available mix tasks
101+
- Use `mix help task_name` to get docs for an individual task
102+
- Read the docs and options fully before using tasks
103+
104+
## Testing
105+
- Run tests in a specific file with `mix test test/my_test.exs` and a specific test with the line number `mix test path/to/test.exs:123`
106+
- Limit the number of failed tests with `mix test --max-failures n`
107+
- Use `@tag` to tag specific tests, and `mix test --only tag` to run only those tests
108+
- Use `assert_raise` for testing expected exceptions: `assert_raise ArgumentError, fn -> invalid_function() end`
109+
- Use `mix help test` to for full documentation on running tests
110+
111+
## Debugging
112+
113+
- Use `dbg/1` to print values while debugging. This will display the formatted value and other relevant information in the console.
114+
115+
<!-- usage_rules:elixir-end -->
116+
<!-- usage_rules:otp-start -->
117+
## usage_rules:otp usage
118+
# OTP Usage Rules
119+
120+
## GenServer Best Practices
121+
- Keep state simple and serializable
122+
- Handle all expected messages explicitly
123+
- Use `handle_continue/2` for post-init work
124+
- Implement proper cleanup in `terminate/2` when necessary
125+
126+
## Process Communication
127+
- Use `GenServer.call/3` for synchronous requests expecting replies
128+
- Use `GenServer.cast/2` for fire-and-forget messages.
129+
- When in doubt, use `call` over `cast`, to ensure back-pressure
130+
- Set appropriate timeouts for `call/3` operations
131+
132+
## Fault Tolerance
133+
- Set up processes such that they can handle crashing and being restarted by supervisors
134+
- Use `:max_restarts` and `:max_seconds` to prevent restart loops
135+
136+
## Task and Async
137+
- Use `Task.Supervisor` for better fault tolerance
138+
- Handle task failures with `Task.yield/2` or `Task.shutdown/2`
139+
- Set appropriate task timeouts
140+
- Use `Task.async_stream/3` for concurrent enumeration with back-pressure
141+
142+
<!-- usage_rules:otp-end -->
143+
<!-- usage-rules-end -->

valentine/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ defmodule Valentine.MixProject do
6262
{:elixlsx, "~> 0.6.0"},
6363
{:logger_formatter_json, "~> 0.8"},
6464
{:sobelow, "~> 0.13", only: [:dev, :test], runtime: false},
65-
{:guardian, "~> 2.3"}
65+
{:guardian, "~> 2.3"},
66+
{:usage_rules, "~> 0.1"}
6667
]
6768
end
6869

0 commit comments

Comments
 (0)