Skip to content

Add --context sources: host classification, MCP reader hints, local document inlining and per-source coverage (#125) - #133

Closed
saryeljaramany wants to merge 6 commits into
secfoo-com:mainfrom
saryeljaramany:feat/context-sources
Closed

saryeljaramany wants to merge 6 commits into
secfoo-com:mainfrom
saryeljaramany:feat/context-sources

Conversation

@saryeljaramany

@saryeljaramany saryeljaramany commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #125

Update after review (#133): the review items are fixed in the commits after the first push; see the comment below. The screenshots further down show the first version's test runs; the final numbers are in the comment.

What this does

secfoo run --context <url-or-path> (repeatable) accepts any documentation source, tells the agent which configured MCP server is likely to read each URL, inlines local documents itself, and records one coverage row per source, which is shown on the run page and in --json.

  • Flag and config. --context is repeatable; --confluence / -c stays as an alias feeding the same list; [defaults].context in config.toml applies to every run. Merge order: config, --confluence, --context; duplicates are dropped.
  • Validation (exit code 2). https:// only, no credentials in a URL, local files must be Markdown, text, PDF, DOCX or PPTX, and nothing inside the scanned target or a folder containing it is accepted (the target is untrusted). The error names where the value came from.
  • Brief in the prompt. URLs are classified by host (Confluence, Jira, SharePoint, Notion, Google Docs, other) and the likely MCP server is named from [[mcp_servers]] by name, URL, command or args (never env or headers); if none matches, the brief says so, so the gap is expected rather than discovered. Local files and folders are extracted with docext, capped (20,000 characters per source, 60,000 in total, 50 files per folder, with the caps shown in the brief), never follow symlinks, are shown by name and not by absolute path, and are wrapped in delimiters as reference data. The safety preamble now allows reading any source listed under "Additional context", not only a Confluence URL.
  • Coverage table. When sources are given, the prompt asks for a ### Documentation coverage table (Source | Type | Reached | Notes; Secret Scanning's section 5 is renamed to match). A new parser (report/context_coverage.py) reconciles it with what secfoo knows: a source the agent leaves out is stored as not reached ("not reported by the agent"), and URL sources of a run that did not complete are "run did not complete".
  • Storage, --json and dashboard. New context_sources table (created on open, no migration); context_sources array per run in --json; "Documentation coverage" card plus a receipt line on the run detail page (green/red per source, everything autoescaped, links only for https URLs).
  • Skills and docs. Secret scanning and architecture review say "the provided documentation sources"; README, docs/REFERENCE.md and docs/CI.md updated.

Decisions I'd like a maintainer to confirm

  • Rejected context values exit with code 2, like other argument errors in run.
  • --confluence with a non-https value used to print a warning and be silently dropped by the renderer; it now fails with an error because it shares the new validation.
  • The two skill definitions changed wording only, so their version is unchanged (the "definition changed" marker on the run page uses the sha256, so it will show once for those skills).

Notes for review

  • confluence_urls now holds only URLs classified as Confluence (plus anything passed through --confluence), so cloud sync, run status and the run page no longer label SharePoint, Notion, Google Docs or Jira links as Confluence pages; every source appears under Documentation coverage. Local paths never go into it.
  • Inlined document text is part of the prompt, so it is also in the run's prompt.md.
  • The api agent has no tool loop (MCP for the api agent: a tool loop so API-key runs can read Confluence, Jira, SharePoint and secfoo-memory too #123), so URL sources are recorded as not reached with that agent; local files work with every agent. docs/CI.md says so.
  • I added the new parser to tests/test_property_parsers.py and fuzz/fuzz_parsers.py; atheris doesn't install on my Windows machine, so I could not run the fuzz harness (only py_compile).

Tests

New: test_context_sources.py, test_context_brief.py, test_report/test_context_coverage.py, test_context_sources_storage.py, plus additions to the CLI, settings, runner, renderer, secret-scanning, property-parser and run-page tests.

On Windows (Python 3.12), main before my changes gave 11 failed, 1316 passed (screenshot 7). After my changes: 12 failed, 1436 passed (screenshot 6a). Both runs contain the same 11 failures that do not depend on this PR: ten deterministic ones (permission bits in test_cloud and test_memory, symlink creation in test_memory_bank, POSIX signals in test_port_guard, skill-pack name derivation from a Windows path in test_skill_packs) and test_runtime/test_protocol, which fails intermittently on my machine (it also failed on main before my changes, and it passed in some of my other full runs). The one extra failure in the later run is a Hypothesis too_slow health check in the existing test_property_parsers.py: input generation took 1.4 s while the machine was loaded by the 7-minute full run, and the file passes when rerun straight afterwards (screenshot 6b). ruff error counts on every existing file I touched are unchanged, and the new files are clean.

Testing and validation (screenshots)

1. secfoo run --help: the new --context flag next to the --confluence alias

S1

2. Validation: http://, scheme-less URL, in-target file, unsupported type, and missing file (all exit code 2)

S2

3. Alias, config merge, and CLI wiring tests (-k "context or confluence" -v)

S3

4. New test files (102 passed, 1 skipped)

S4

5. Brief for sample sources: Confluence matched to the atlassian MCP server, SharePoint flagged as having no matching server, local file inlined by name

S5

6a. Full test suite (12 failed, 1436 passed on Windows)

S6a

6b. Rerun of test_property_parsers.py (passes) and test_protocol.py (the intermittent failure that is also on main), plus ruff before/after

S6b

7. Baseline recorded before my changes (11 failed, 1316 passed)

S7

8. --json output with context_sources

S8

9. Run detail page: Documentation coverage card (1 of 2 sources reached)

S9

AI assistance was used to draft parts of this; I reviewed the diff and ran the tests myself.

@vsn411

vsn411 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Thank you for this, @saryeljaramany. Most of #125 is here and works: --context handles a URL, a Markdown file and a PDF with tests, --confluence still works as an alias, the brief names the matching MCP server per URL, http:// is rejected, the coverage card shows on the run detail page, and the docs are updated. I'm closing the PR because the per-source coverage table, which is the core of #125, records wrong results in common report shapes, and there are a few correctness gaps around it. #125 stays open, and you're very welcome to reopen this PR after pushing fixes, or open a new one. Reviewed at b2bb365; the first two and the macOS one were reproduced with scripts against the branch.

Blocking

  1. The coverage parser gives up on nested headings (report/context_coverage.py ~L126). The secret-scanning brief asks for a table "headed exactly ### Documentation coverage" and to fill section 5 with it. When the agent writes ## 5. Documentation coverage, then ### Documentation coverage, then the table, parse_documentation_coverage returns [] and every source is stored as not reached ("not reported by the agent").
  2. Rows are matched to the wrong source (~L250). The second matching pass checks whether a source's label appears anywhere in a row. With sources …/wiki/pages/1 and …/wiki/pages/12, and rows …/pages/12 (Arch) | No | 403 and …/pages/1 (Threat) | Yes, the stored result is pages/1 not reached and pages/12 reached, the reverse of what the agent wrote. a.md vs data.md, or a docs/ folder vs a docs.google.com row, behave the same.
  3. The positional fallback hides gaps (~L255). With sources [A, B] and rows [A, discovered README.md], B inherits the README row instead of showing "not reported by the agent".
    For 1–3, the robust fix is to locate the table by any heading level, match rows to sources by exact label (full URL or full path), and mark any source without an exact match as not reported, with no positional fallback.
  4. The in-target rejection is case-sensitive (context_sources.py ~L96). resolve() doesn't normalise case on macOS's default case-insensitive filesystem, so secfoo run --target ~/code/Repo --context ~/code/repo/docs/a.md is accepted and an untrusted file from the scanned target gets inlined, which --context <url|path>: first-class context sources beyond Confluence (SharePoint, Jira, Notion, local design docs) with a coverage section that names each one #125 says must be rejected. Compare with os.path.samefile/inode checks on each parent, or normalise case where the filesystem is case-insensitive.
  5. Every https context URL is stored as a Confluence page (cli.py ~L559 passes all URL sources as confluence_urls). SharePoint, Notion, Google Docs and Jira links then show as "Confluence pages" on the run page, in run_status.py, and in the cloud sync payload.
  6. Dashboard-started runs ignore context (web/routes/assessments.py ~L355). The Run Assessment form still builds only confluence_urls from its textarea, so those runs skip [defaults].context, validation, reader hints and the coverage table, although the docs say defaults apply to every run.

Should fix

  1. Folder truncation is silent (context_brief.py ~L158). A folder with 55 small .md files reports "50 files read, 0 skipped" and shows as fully reached, although the brief promises truncated sources are marked.
  2. Local sources are labelled by basename only (~L206). --context ../a/README.md --context ../b/README.md produces two identical "README.md" headings and coverage rows.
  3. Self-hosted Atlassian isn't recognised (~L60). confluence.corp.example or jira.corp.example is classified as "other", so the brief says no configured MCP server matches even when an Atlassian server is configured. Consider letting a [[mcp_servers]] entry declare the hosts it reads.

Cleanup

  1. _parse_url is a third copy of the https-only URL rule (also in renderer._https_page_urls and templating.https_url); CoverageRow and CoverageRecord are identical; and build_context_brief duplicates its "not inlined" branch. One shared helper avoids the rules drifting.

Looking forward to the next version.

@vsn411 vsn411 closed this Nov 18, 2026
@saryeljaramany

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, @vsn411. All ten items are addressed, each with regression tests. Reopening for another look.

Blocking

  1. Nested headings. The table is now found under any heading level (##, ###, ####, numbered headings); the scan stops only at the next heading of equal or higher level, so ## 5. Documentation coverage followed by ### Documentation coverage and the table parses. Test: test_nested_headings_and_lookup_boundaries.
  2. Wrong-row matching. Substring matching is gone. A row matches a source only when one of its keys equals the source's label exactly (the whole cell, a markdown link's text or target, an https URL token, or the cell minus one trailing "(note)"). pages/1 vs pages/12 with your reversed rows, a.md vs data.md, and a docs/ folder vs a docs.google.com row are covered: test_pages_1_vs_pages_12_non_collision, test_no_cross_matches_between_substring_lookalikes, test_markdown_link_row_matches_source_url.
  3. Positional fallback. Removed. A source without an exact match is stored as not reached ("not reported by the agent"); an unmatched row appears only as an extra record when the run succeeded: test_coverage_for_run_no_positional_fallback.
  4. Case-insensitive filesystems. The in-target check now compares file-system identity (os.path.samefile on the path and each parent, in both directions) instead of resolved-string prefixes, so case variants and symlink aliases of the target are rejected, and a sibling like repo2 next to repo is not. It fails closed if identity cannot be verified. Tests in tests/test_context_sources.py: the case-variant test runs only on a case-insensitive filesystem and passed on my Windows machine (NTFS); the symlink-alias tests are skipped there (no symlink privilege), so Linux CI is what exercises them. I could not test macOS myself.
  5. Confluence URLs. confluence_urls now holds only URLs classified as Confluence plus anything passed through --confluence. SharePoint, Notion, Google Docs and Jira links no longer show up as "Confluence pages" on the run page, in run_status.py or in the cloud payload (they all read that field). Every source is still listed under Documentation coverage. I had to change three assertions in tests/test_cli.py that encoded the old behavior.
  6. Dashboard runs. The Run Assessment form now uses the same pipeline as the CLI (context_run.resolve_run_context): it merges [defaults].context, validates every source before the background thread starts (HTTP 400 with the reason), passes the configured MCP servers for reader hints, and records per-source coverage. The textarea is renamed "Documentation context" (the old confluence field still works). Tests: test_trigger_run_with_context_and_mcp_servers, test_trigger_run_invalid_context_source_returns_400, test_trigger_run_context_inside_target_returns_400, test_trigger_run_merges_config_defaults_context, test_trigger_run_legacy_confluence_and_context_combined, plus blank-line handling.

Should fix
7. Folder truncation. A folder over the 50-file (or per-source character) cap now says so: 50 files read, 0 skipped; truncated: 5 more files not read (50-file cap). It still counts as reached. The counting walk after truncation is bounded. Tests: test_folder_55_files_truncation_reporting, test_folder_truncation_with_post_truncation_cap.
8. Duplicate basenames. Local sources with the same name get enough parent folder names to tell them apart (a/README.md, b/README.md), never an absolute path, and the coverage parser matches on those labels: test_duplicate_basenames_produce_distinct_labels_and_headings.
9. Self-hosted Atlassian. A [[mcp_servers]] entry can declare hosts = ["confluence.corp.example", "*.corp.example"] (a *. pattern matches subdomains only). A URL on a declared host is classified by path (/wiki, /display/, ... for Confluence; /browse/, /jira, ... for Jira) and the declaring server is named as its reader first. Tests: test_host_matches, test_classify_url_with_mcp_server_hosts and the settings tests; documented in docs/REFERENCE.md.

Cleanup
10. The https-only rule now lives in one place (https_url_problem in context_sources.py) and is used by the context parser, renderer._https_page_urls and templating.https_url. CoverageRow is gone (parse_documentation_coverage returns CoverageRecord), and the duplicated "not inlined" branch in build_context_brief is one code path with identical output.

Tests. pytest tests on Windows (Python 3.12) after merging current main: 10 failed, 1523 passed, 3 skipped. The 10 failures are the same ones that fail on main before my changes (permission bits in test_cloud and test_memory, symlink creation in test_memory_bank, POSIX signals in test_port_guard, and skill-pack name derivation from a Windows path in test_skill_packs). The three skips are symlink tests that Windows can't run without privileges. ruff error counts on every existing file I touched are equal or lower than at the start of this round, and the new files are clean.

@vsn411

vsn411 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Please create a new PR. We will review and merge it. Thank you

@saryeljaramany

Copy link
Copy Markdown
Contributor Author

Since GitHub does not allow contributors to reopen a PR closed by maintainers, I have opened the updated PR with all 10 review fixes and tests here: #146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants