codejury 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codejury/__init__.py +8 -0
- codejury/agents/__init__.py +6 -0
- codejury/agents/base.py +21 -0
- codejury/agents/debate.py +188 -0
- codejury/agents/mock.py +38 -0
- codejury/agents/parsing.py +42 -0
- codejury/agents/verifier.py +106 -0
- codejury/assembly.py +76 -0
- codejury/cli.py +196 -0
- codejury/data/capabilities/authentication.yaml +67 -0
- codejury/data/capabilities/authorization.yaml +55 -0
- codejury/data/capabilities/business_logic.yaml +58 -0
- codejury/data/capabilities/crypto.yaml +78 -0
- codejury/data/capabilities/data_protection.yaml +57 -0
- codejury/data/capabilities/dependency_config.yaml +52 -0
- codejury/data/capabilities/error_logging.yaml +49 -0
- codejury/data/capabilities/input_validation.yaml +92 -0
- codejury/data/capabilities/output_encoding.yaml +56 -0
- codejury/data/capabilities/secrets.yaml +51 -0
- codejury/data/capabilities/session.yaml +60 -0
- codejury/data/golden/authn_bcrypt_password.yaml +5 -0
- codejury/data/golden/authn_sha256_password.yaml +5 -0
- codejury/data/golden/sqli_fstring_query.yaml +5 -0
- codejury/data/golden/sqli_parameterized_query.yaml +5 -0
- codejury/data/tasks/audit_diff_debate.yaml +4 -0
- codejury/data/tasks/quick_scan_single.yaml +4 -0
- codejury/domain/__init__.py +5 -0
- codejury/domain/artifact.py +20 -0
- codejury/domain/capability.py +123 -0
- codejury/domain/context.py +26 -0
- codejury/domain/observation.py +104 -0
- codejury/domain/result.py +19 -0
- codejury/evaluation.py +107 -0
- codejury/infrastructure/__init__.py +4 -0
- codejury/infrastructure/json_parse.py +57 -0
- codejury/orchestrators/__init__.py +6 -0
- codejury/orchestrators/base.py +19 -0
- codejury/orchestrators/debate.py +57 -0
- codejury/orchestrators/pipeline.py +32 -0
- codejury/orchestrators/reflexion.py +58 -0
- codejury/orchestrators/single.py +24 -0
- codejury/providers/__init__.py +5 -0
- codejury/providers/anthropic.py +68 -0
- codejury/providers/base.py +42 -0
- codejury/providers/litellm.py +68 -0
- codejury/providers/mock.py +32 -0
- codejury/providers/openai.py +57 -0
- codejury/providers/openai_format.py +30 -0
- codejury/providers/retry.py +48 -0
- codejury/reporting.py +114 -0
- codejury/resources.py +13 -0
- codejury/sources/__init__.py +6 -0
- codejury/sources/base.py +17 -0
- codejury/sources/chunker.py +33 -0
- codejury/sources/diff.py +69 -0
- codejury/sources/function.py +35 -0
- codejury/sources/mock.py +25 -0
- codejury/sources/repo.py +44 -0
- codejury/tasks/__init__.py +6 -0
- codejury/tasks/base.py +55 -0
- codejury/tasks/registry.py +22 -0
- codejury-0.1.0.dist-info/METADATA +110 -0
- codejury-0.1.0.dist-info/RECORD +67 -0
- codejury-0.1.0.dist-info/WHEEL +5 -0
- codejury-0.1.0.dist-info/entry_points.txt +2 -0
- codejury-0.1.0.dist-info/licenses/LICENSE +21 -0
- codejury-0.1.0.dist-info/top_level.txt +1 -0
codejury/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""codejury -- a general-purpose Application Security AI audit framework.
|
|
2
|
+
|
|
3
|
+
Five layers: Task / (Capability + Orchestrator + Source + Agent) / Provider / Infrastructure.
|
|
4
|
+
Domain knowledge lives in YAML capability files as a first-class citizen,
|
|
5
|
+
aligned with OWASP ASVS.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "0.0.0"
|
codejury/agents/base.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Agent ABC.
|
|
2
|
+
|
|
3
|
+
An agent runs once over an AnalysisContext and returns a list of observations:
|
|
4
|
+
a single run typically yields several (a verifier emits one Verdict per
|
|
5
|
+
sub_capability; a finder reports several Findings).
|
|
6
|
+
|
|
7
|
+
The base only declares ``run``. Concrete agents take a Provider in their own
|
|
8
|
+
__init__; the orchestrator constructs and addresses them by role.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from abc import ABC, abstractmethod
|
|
14
|
+
|
|
15
|
+
from codejury.domain.context import AnalysisContext
|
|
16
|
+
from codejury.domain.observation import Observation
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Agent(ABC):
|
|
20
|
+
@abstractmethod
|
|
21
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]: ...
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"""Finder / Challenger / Judge agents for the debate orchestrator.
|
|
2
|
+
|
|
3
|
+
Each reads the artifact, the capability hints, and the accumulated history
|
|
4
|
+
(prior findings and rebuttals) from the context, calls the provider once, and
|
|
5
|
+
maps its JSON reply onto observations:
|
|
6
|
+
|
|
7
|
+
- Finder -> Finding (claims) + Concession (its own retractions)
|
|
8
|
+
- Challenger -> Concession (moves to dismiss findings) + Finding (ones it missed)
|
|
9
|
+
- Judge -> Finding (surviving) + Concession (dismissed)
|
|
10
|
+
|
|
11
|
+
The orchestrator threads history and round_num across rounds; convergence is the
|
|
12
|
+
orchestrator's job, not encoded here.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from codejury.agents.base import Agent
|
|
18
|
+
from codejury.agents.parsing import one_of, str_list, to_evidence, to_float
|
|
19
|
+
from codejury.domain.artifact import CodeArtifact
|
|
20
|
+
from codejury.domain.capability import Capability
|
|
21
|
+
from codejury.domain.context import AnalysisContext
|
|
22
|
+
from codejury.domain.observation import Concession, Finding, Observation
|
|
23
|
+
from codejury.infrastructure.json_parse import extract_json_object
|
|
24
|
+
from codejury.providers.base import Message, Provider
|
|
25
|
+
|
|
26
|
+
_SEVERITY = {"CRITICAL", "HIGH", "MEDIUM", "LOW", "INFO"}
|
|
27
|
+
|
|
28
|
+
_FINDING_SHAPE = (
|
|
29
|
+
'{"capability": "id.sub", "title": "...", "severity": "HIGH", "cwe": "CWE-89", '
|
|
30
|
+
'"description": "...", "evidence": [{"file": "...", "line": 0, "code": "..."}], "confidence": 0.0}'
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class _DebateAgent(Agent):
|
|
35
|
+
"""Shared provider plumbing for the three debate roles."""
|
|
36
|
+
|
|
37
|
+
role = "agent"
|
|
38
|
+
system = ""
|
|
39
|
+
|
|
40
|
+
def __init__(self, *, provider: Provider, model: str, max_tokens: int = 2048) -> None:
|
|
41
|
+
self._provider = provider
|
|
42
|
+
self._model = model
|
|
43
|
+
self._max_tokens = max_tokens
|
|
44
|
+
|
|
45
|
+
def _ask(self, prompt: str) -> dict:
|
|
46
|
+
result = self._provider.complete(
|
|
47
|
+
system=self.system,
|
|
48
|
+
messages=[Message(role="user", content=prompt)],
|
|
49
|
+
model=self._model,
|
|
50
|
+
max_tokens=self._max_tokens,
|
|
51
|
+
)
|
|
52
|
+
return extract_json_object(result.text) or {}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class FinderAgent(_DebateAgent):
|
|
56
|
+
role = "finder"
|
|
57
|
+
system = (
|
|
58
|
+
"You are a security finder. Identify real, exploitable vulnerabilities in the code, "
|
|
59
|
+
"being precise and avoiding false positives. Respond with a single JSON object and nothing else."
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]:
|
|
63
|
+
parts = ["Review the code for security vulnerabilities.", _hints(ctx.capabilities), _code(ctx.artifact)]
|
|
64
|
+
if ctx.round_num > 1 and ctx.history:
|
|
65
|
+
parts.append(_render_history(ctx.history))
|
|
66
|
+
parts.append("Concede findings the rebuttals refute, keep the valid ones, and add any you missed.")
|
|
67
|
+
parts.append(
|
|
68
|
+
'Respond as JSON: {"findings": [' + _FINDING_SHAPE + '], '
|
|
69
|
+
'"concessions": [{"target": "finding title", "reason": "..."}]}'
|
|
70
|
+
)
|
|
71
|
+
obj = self._ask("\n\n".join(p for p in parts if p))
|
|
72
|
+
return _findings(obj.get("findings"), self.role, ctx.round_num) + _concessions(
|
|
73
|
+
obj.get("concessions"), self.role, ctx.round_num
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class ChallengerAgent(_DebateAgent):
|
|
78
|
+
role = "challenger"
|
|
79
|
+
system = (
|
|
80
|
+
"You are a skeptical security reviewer. Challenge each reported finding and try to refute it "
|
|
81
|
+
"with concrete reasoning. Respond with a single JSON object and nothing else."
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]:
|
|
85
|
+
parts = [
|
|
86
|
+
"Challenge the findings below. For each one you believe is a false positive, write a rebuttal. "
|
|
87
|
+
"Add new_findings for any real issue that was missed.",
|
|
88
|
+
_code(ctx.artifact),
|
|
89
|
+
_render_history(ctx.history),
|
|
90
|
+
'Respond as JSON: {"rebuttals": [{"target": "finding title", "reason": "..."}], '
|
|
91
|
+
'"new_findings": [' + _FINDING_SHAPE + "]}",
|
|
92
|
+
]
|
|
93
|
+
obj = self._ask("\n\n".join(p for p in parts if p))
|
|
94
|
+
return _concessions(obj.get("rebuttals"), self.role, ctx.round_num) + _findings(
|
|
95
|
+
obj.get("new_findings"), self.role, ctx.round_num
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class JudgeAgent(_DebateAgent):
|
|
100
|
+
role = "judge"
|
|
101
|
+
system = (
|
|
102
|
+
"You are an impartial security judge. Weighing the findings and rebuttals, decide which findings "
|
|
103
|
+
"survive scrutiny and which are dismissed. Respond with a single JSON object and nothing else."
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]:
|
|
107
|
+
parts = [
|
|
108
|
+
"Rule on the debate below. Keep findings that withstand the rebuttals; dismiss the rest.",
|
|
109
|
+
_code(ctx.artifact),
|
|
110
|
+
_render_history(ctx.history),
|
|
111
|
+
'Respond as JSON: {"surviving": [' + _FINDING_SHAPE + '], '
|
|
112
|
+
'"dismissed": [{"target": "finding title", "reason": "..."}]}',
|
|
113
|
+
]
|
|
114
|
+
obj = self._ask("\n\n".join(p for p in parts if p))
|
|
115
|
+
return _findings(obj.get("surviving"), self.role, ctx.round_num) + _concessions(
|
|
116
|
+
obj.get("dismissed"), self.role, ctx.round_num
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _code(artifact: CodeArtifact) -> str:
|
|
121
|
+
return f"Code under review ({artifact.path}):\n```\n{artifact.content}\n```"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _hints(capabilities: list[Capability]) -> str:
|
|
125
|
+
lines = []
|
|
126
|
+
for cap in capabilities:
|
|
127
|
+
for sub_name, sub in cap.sub_capabilities.items():
|
|
128
|
+
for ap in sub.anti_patterns:
|
|
129
|
+
tag = f"{ap.cwe} {ap.severity}" if ap.cwe else ap.severity
|
|
130
|
+
lines.append(f"- {cap.id}.{sub_name} [{tag}]: {ap.description}")
|
|
131
|
+
return "Look especially for:\n" + "\n".join(lines) if lines else ""
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _render_history(history: list[Observation]) -> str:
|
|
135
|
+
findings = [o for o in history if isinstance(o, Finding)]
|
|
136
|
+
concessions = [o for o in history if isinstance(o, Concession)]
|
|
137
|
+
blocks = []
|
|
138
|
+
if findings:
|
|
139
|
+
lines = [
|
|
140
|
+
f'- [{f.produced_by} r{f.round_num}] "{f.title}" ({f.severity}{", " + f.cwe if f.cwe else ""}): '
|
|
141
|
+
f"{f.description}"
|
|
142
|
+
for f in findings
|
|
143
|
+
]
|
|
144
|
+
blocks.append("Findings so far:\n" + "\n".join(lines))
|
|
145
|
+
if concessions:
|
|
146
|
+
lines = [f'- [{c.produced_by} r{c.round_num}] dismiss "{c.target}": {c.reason}' for c in concessions]
|
|
147
|
+
blocks.append("Rebuttals / concessions so far:\n" + "\n".join(lines))
|
|
148
|
+
return "\n\n".join(blocks)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _findings(items: object, produced_by: str, round_num: int) -> list[Finding]:
|
|
152
|
+
out: list[Finding] = []
|
|
153
|
+
for f in _dicts(items):
|
|
154
|
+
title = str(f.get("title", "")).strip()
|
|
155
|
+
if not title:
|
|
156
|
+
continue
|
|
157
|
+
out.append(
|
|
158
|
+
Finding(
|
|
159
|
+
produced_by=produced_by,
|
|
160
|
+
round_num=round_num,
|
|
161
|
+
capability=str(f.get("capability", "")),
|
|
162
|
+
title=title,
|
|
163
|
+
description=str(f.get("description", "")),
|
|
164
|
+
severity=one_of(f.get("severity"), _SEVERITY, "MEDIUM"),
|
|
165
|
+
cwe=str(f.get("cwe", "")),
|
|
166
|
+
evidence=to_evidence(f.get("evidence")),
|
|
167
|
+
recommendation=str(f.get("recommendation", "")),
|
|
168
|
+
matched_anti=str_list(f.get("matched_anti")),
|
|
169
|
+
confidence=to_float(f.get("confidence"), 0.5),
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
return out
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _concessions(items: object, produced_by: str, round_num: int) -> list[Concession]:
|
|
176
|
+
out: list[Concession] = []
|
|
177
|
+
for c in _dicts(items):
|
|
178
|
+
target = str(c.get("target", "")).strip()
|
|
179
|
+
if not target:
|
|
180
|
+
continue
|
|
181
|
+
out.append(
|
|
182
|
+
Concession(produced_by=produced_by, round_num=round_num, target=target, reason=str(c.get("reason", "")))
|
|
183
|
+
)
|
|
184
|
+
return out
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _dicts(items: object) -> list[dict]:
|
|
188
|
+
return [x for x in items if isinstance(x, dict)] if isinstance(items, list) else []
|
codejury/agents/mock.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""MockAgent -- a minimal Agent for the dry-run and tests.
|
|
2
|
+
|
|
3
|
+
It really calls the provider (so the dry-run exercises the agent -> provider
|
|
4
|
+
path), then emits one Verdict per in-scope capability, parking the model's reply
|
|
5
|
+
in ``reasoning``. It does no real parsing or judgement; the Phase 3 VerifierAgent
|
|
6
|
+
will.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from codejury.agents.base import Agent
|
|
12
|
+
from codejury.domain.context import AnalysisContext
|
|
13
|
+
from codejury.domain.observation import Observation, Verdict
|
|
14
|
+
from codejury.providers.base import Message, Provider
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class MockAgent(Agent):
|
|
18
|
+
def __init__(self, *, provider: Provider, role: str = "verifier", model: str = "mock-model") -> None:
|
|
19
|
+
self._provider = provider
|
|
20
|
+
self._role = role
|
|
21
|
+
self._model = model
|
|
22
|
+
|
|
23
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]:
|
|
24
|
+
result = self._provider.complete(
|
|
25
|
+
system=f"You are a {self._role}.",
|
|
26
|
+
messages=[Message(role="user", content=ctx.artifact.content)],
|
|
27
|
+
model=self._model,
|
|
28
|
+
max_tokens=256,
|
|
29
|
+
)
|
|
30
|
+
return [
|
|
31
|
+
Verdict(
|
|
32
|
+
capability=cap.id,
|
|
33
|
+
produced_by=self._role,
|
|
34
|
+
status="UNKNOWN",
|
|
35
|
+
reasoning=result.text,
|
|
36
|
+
)
|
|
37
|
+
for cap in ctx.capabilities
|
|
38
|
+
]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Shared coercion from loosely-typed model JSON into domain values.
|
|
2
|
+
|
|
3
|
+
Agents parse model output that may omit, mistype, or invent fields. These
|
|
4
|
+
helpers coerce defensively -- they never raise on bad input, they fall back.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from codejury.domain.observation import Evidence
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def str_list(value: object) -> list[str]:
|
|
13
|
+
return [str(x) for x in value] if isinstance(value, list) else []
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def to_float(value: object, default: float) -> float:
|
|
17
|
+
try:
|
|
18
|
+
return float(value) # type: ignore[arg-type]
|
|
19
|
+
except (TypeError, ValueError):
|
|
20
|
+
return default
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def one_of(value: object, allowed: set[str], default: str) -> str:
|
|
24
|
+
return value if value in allowed else default
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def to_evidence(items: object) -> list[Evidence]:
|
|
28
|
+
if not isinstance(items, list):
|
|
29
|
+
return []
|
|
30
|
+
out: list[Evidence] = []
|
|
31
|
+
for e in items:
|
|
32
|
+
if not isinstance(e, dict):
|
|
33
|
+
continue
|
|
34
|
+
line = e.get("line")
|
|
35
|
+
out.append(
|
|
36
|
+
Evidence(
|
|
37
|
+
file=str(e.get("file", "")),
|
|
38
|
+
line=line if isinstance(line, int) else None,
|
|
39
|
+
code=str(e.get("code", "")),
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
return out
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""VerifierAgent -- check code against a capability's correct/anti patterns.
|
|
2
|
+
|
|
3
|
+
It renders the capability into a prompt, calls the provider once per capability,
|
|
4
|
+
and parses the JSON reply into Verdicts. It asks for one verdict per
|
|
5
|
+
sub_capability including SECURE / NOT_PRESENT, so a report can say what was
|
|
6
|
+
checked and what passed -- not only what failed.
|
|
7
|
+
|
|
8
|
+
Parsing is defensive: a missing or malformed reply yields no verdicts rather
|
|
9
|
+
than raising, and unknown status values fall back to UNKNOWN.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from codejury.agents.base import Agent
|
|
15
|
+
from codejury.agents.parsing import one_of, str_list, to_evidence, to_float
|
|
16
|
+
from codejury.domain.capability import Capability
|
|
17
|
+
from codejury.domain.context import AnalysisContext
|
|
18
|
+
from codejury.domain.observation import Observation, Verdict
|
|
19
|
+
from codejury.infrastructure.json_parse import extract_json_object
|
|
20
|
+
from codejury.providers.base import Message, Provider
|
|
21
|
+
|
|
22
|
+
_VALID_STATUS = {"SECURE", "VULNERABLE", "PARTIAL", "NOT_PRESENT", "UNKNOWN"}
|
|
23
|
+
|
|
24
|
+
_SYSTEM = (
|
|
25
|
+
"You are a security verifier. You check code against a checklist of correct and "
|
|
26
|
+
"anti patterns and rule on each dimension, reporting what is fine as well as what "
|
|
27
|
+
"is wrong. Respond with a single JSON object and nothing else."
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
_JSON_SHAPE = (
|
|
31
|
+
'{"verdicts": [{"sub_capability": "...", '
|
|
32
|
+
'"status": "SECURE|VULNERABLE|PARTIAL|NOT_PRESENT|UNKNOWN", "reasoning": "...", '
|
|
33
|
+
'"matched_correct": ["id"], "matched_anti": ["id"], '
|
|
34
|
+
'"evidence": [{"file": "path", "line": 0, "code": "..."}], "confidence": 0.0}]}'
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class VerifierAgent(Agent):
|
|
39
|
+
def __init__(self, *, provider: Provider, model: str, max_tokens: int = 2048) -> None:
|
|
40
|
+
self._provider = provider
|
|
41
|
+
self._model = model
|
|
42
|
+
self._max_tokens = max_tokens
|
|
43
|
+
|
|
44
|
+
def run(self, ctx: AnalysisContext) -> list[Observation]:
|
|
45
|
+
verdicts: list[Observation] = []
|
|
46
|
+
for cap in ctx.capabilities:
|
|
47
|
+
prompt = _build_prompt(ctx.artifact.path, ctx.artifact.content, cap)
|
|
48
|
+
result = self._provider.complete(
|
|
49
|
+
system=_SYSTEM,
|
|
50
|
+
messages=[Message(role="user", content=prompt)],
|
|
51
|
+
model=self._model,
|
|
52
|
+
max_tokens=self._max_tokens,
|
|
53
|
+
)
|
|
54
|
+
verdicts.extend(_parse_verdicts(result.text, cap))
|
|
55
|
+
return verdicts
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _render_capability(cap: Capability) -> str:
|
|
59
|
+
lines = [f"Capability: {cap.id} ({cap.name})"]
|
|
60
|
+
for sub_name, sub in cap.sub_capabilities.items():
|
|
61
|
+
lines.append(f"\nsub_capability: {sub_name}")
|
|
62
|
+
if sub.correct_patterns:
|
|
63
|
+
lines.append(" correct patterns:")
|
|
64
|
+
lines += [f" - {p.id}: {p.description}" for p in sub.correct_patterns]
|
|
65
|
+
if sub.anti_patterns:
|
|
66
|
+
lines.append(" anti patterns:")
|
|
67
|
+
for p in sub.anti_patterns:
|
|
68
|
+
tag = f"[{p.cwe} {p.severity}]" if p.cwe else f"[{p.severity}]"
|
|
69
|
+
lines.append(f" - {p.id} {tag}: {p.description}")
|
|
70
|
+
return "\n".join(lines)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _build_prompt(path: str, content: str, cap: Capability) -> str:
|
|
74
|
+
sub_names = ", ".join(cap.sub_capabilities) or "(none)"
|
|
75
|
+
return (
|
|
76
|
+
"Check the code below against this capability.\n\n"
|
|
77
|
+
f"{_render_capability(cap)}\n\n"
|
|
78
|
+
f"Code under review ({path}):\n```\n{content}\n```\n\n"
|
|
79
|
+
f"For EVERY sub_capability ({sub_names}) output one verdict, even if SECURE "
|
|
80
|
+
"or NOT_PRESENT. Cite matched pattern ids and evidence lines.\n\n"
|
|
81
|
+
"Respond with a single JSON object exactly like:\n" + _JSON_SHAPE
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _parse_verdicts(text: str, cap: Capability) -> list[Verdict]:
|
|
86
|
+
obj = extract_json_object(text)
|
|
87
|
+
if not obj:
|
|
88
|
+
return []
|
|
89
|
+
out: list[Verdict] = []
|
|
90
|
+
for v in obj.get("verdicts", []):
|
|
91
|
+
if not isinstance(v, dict):
|
|
92
|
+
continue
|
|
93
|
+
sub = str(v.get("sub_capability", "")).strip()
|
|
94
|
+
out.append(
|
|
95
|
+
Verdict(
|
|
96
|
+
capability=f"{cap.id}.{sub}" if sub else cap.id,
|
|
97
|
+
produced_by="verifier",
|
|
98
|
+
status=one_of(v.get("status"), _VALID_STATUS, "UNKNOWN"),
|
|
99
|
+
reasoning=str(v.get("reasoning", "")),
|
|
100
|
+
matched_correct=str_list(v.get("matched_correct")),
|
|
101
|
+
matched_anti=str_list(v.get("matched_anti")),
|
|
102
|
+
evidence=to_evidence(v.get("evidence")),
|
|
103
|
+
confidence=to_float(v.get("confidence"), 0.5),
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
return out
|
codejury/assembly.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Assembly -- build an orchestration from a strategy name and run it over a source.
|
|
2
|
+
|
|
3
|
+
Shared by the CLI and the task layer so the "which agents + which orchestrator"
|
|
4
|
+
mapping and the per-artifact run loop live in one place.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from codejury.agents.base import Agent
|
|
12
|
+
from codejury.agents.debate import ChallengerAgent, FinderAgent, JudgeAgent
|
|
13
|
+
from codejury.agents.verifier import VerifierAgent
|
|
14
|
+
from codejury.domain.capability import Capability
|
|
15
|
+
from codejury.domain.context import AnalysisContext
|
|
16
|
+
from codejury.domain.result import AnalysisResult
|
|
17
|
+
from codejury.orchestrators.base import Orchestrator
|
|
18
|
+
from codejury.orchestrators.debate import DebateOrchestrator
|
|
19
|
+
from codejury.orchestrators.pipeline import PipelineOrchestrator
|
|
20
|
+
from codejury.orchestrators.reflexion import ReflexionOrchestrator
|
|
21
|
+
from codejury.orchestrators.single import SingleOrchestrator
|
|
22
|
+
from codejury.providers.anthropic import AnthropicProvider
|
|
23
|
+
from codejury.providers.base import Provider
|
|
24
|
+
from codejury.providers.litellm import LiteLLMProvider
|
|
25
|
+
from codejury.providers.openai import OpenAIProvider
|
|
26
|
+
from codejury.providers.retry import RetryProvider
|
|
27
|
+
from codejury.sources.base import Source
|
|
28
|
+
|
|
29
|
+
STRATEGIES = ("single", "pipeline", "debate", "reflexion")
|
|
30
|
+
PROVIDERS = ("anthropic", "openai", "litellm")
|
|
31
|
+
DEFAULT_MODEL = os.environ.get("CODEJURY_MODEL", "claude-sonnet-4-6")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def make_provider(name: str, *, retries: int = 0) -> Provider:
|
|
35
|
+
if name == "openai":
|
|
36
|
+
provider: Provider = OpenAIProvider()
|
|
37
|
+
elif name == "litellm":
|
|
38
|
+
provider = LiteLLMProvider()
|
|
39
|
+
else:
|
|
40
|
+
provider = AnthropicProvider()
|
|
41
|
+
if retries > 0:
|
|
42
|
+
provider = RetryProvider(provider, max_attempts=retries + 1)
|
|
43
|
+
return provider
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def build_orchestration(
|
|
47
|
+
strategy: str, *, provider: Provider, model: str, max_tokens: int
|
|
48
|
+
) -> tuple[dict[str, Agent], Orchestrator]:
|
|
49
|
+
if strategy == "debate":
|
|
50
|
+
roles = (FinderAgent, ChallengerAgent, JudgeAgent)
|
|
51
|
+
agents = {cls.role: cls(provider=provider, model=model, max_tokens=max_tokens) for cls in roles}
|
|
52
|
+
return agents, DebateOrchestrator()
|
|
53
|
+
if strategy == "reflexion":
|
|
54
|
+
agents = {
|
|
55
|
+
"actor": FinderAgent(provider=provider, model=model, max_tokens=max_tokens),
|
|
56
|
+
"critic": ChallengerAgent(provider=provider, model=model, max_tokens=max_tokens),
|
|
57
|
+
}
|
|
58
|
+
return agents, ReflexionOrchestrator()
|
|
59
|
+
verifier = {"verifier": VerifierAgent(provider=provider, model=model, max_tokens=max_tokens)}
|
|
60
|
+
if strategy == "pipeline":
|
|
61
|
+
return verifier, PipelineOrchestrator()
|
|
62
|
+
return verifier, SingleOrchestrator()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def run_over_source(
|
|
66
|
+
source: Source,
|
|
67
|
+
capabilities: list[Capability],
|
|
68
|
+
agents: dict[str, Agent],
|
|
69
|
+
orchestrator: Orchestrator,
|
|
70
|
+
) -> list[tuple[str, AnalysisResult]]:
|
|
71
|
+
"""Run the orchestration over each artifact, returning (path, result) per artifact."""
|
|
72
|
+
results = []
|
|
73
|
+
for artifact in source.list_artifacts():
|
|
74
|
+
ctx = AnalysisContext(artifact=artifact, capabilities=capabilities)
|
|
75
|
+
results.append((artifact.path, orchestrator.run(agents, ctx)))
|
|
76
|
+
return results
|