44template (preserving comments), and injects wiki usage instructions into
55the project's AGENTS.md. This is a zero-config bootstrap: run it once
66before starting any wiki generation or knowledge ingestion workflow.
7+
8+ Under a centralized workspace layout (design doc §6), a business repo's
9+ knowledge lives in the workspace corpus: the default output_dir routes to
10+ the workspace ``repowiki/``, ``wiki/modules/<repo>/`` is ensured, the
11+ workspace's customized ``schema.yaml`` is never clobbered, and no AGENTS.md
12+ block is injected into the business repo (``add_workspace_repo`` removed it
13+ on purpose). Explicit ``output_dir`` arguments are never hijacked.
714"""
815
916from __future__ import annotations
@@ -148,9 +155,35 @@ def handle_init_wiki(arguments: dict) -> str:
148155 ensure_ascii = False ,
149156 )
150157
151- # Resolve output_dir
158+ # ── Step 0: centralized-layout routing (design doc §6) ─────────────
159+ # A business repo registered in a centralized workspace is pure code:
160+ # its knowledge lives in the workspace corpus (modules →
161+ # wiki/modules/<repo>/, shared-pool dirs → workspace repowiki/).
162+ # Running init_wiki here must therefore route the default output_dir
163+ # to the corpus instead of scaffolding <repo>/repowiki/ (which
164+ # lint_wiki's layout_violations check would flag) or re-injecting the
165+ # AGENTS.md wiki block that add_workspace_repo just removed.
166+ # The routing must never break init: any resolution failure falls
167+ # back to the status-quo path.
168+ _centralized = False
169+ try :
170+ from codewiki .mcp .tools .workspace_layout import resolve_workspace
171+
172+ _resolution = resolve_workspace (repo_path_p )
173+ _centralized = _resolution .centralized
174+ except Exception as e : # pragma: no cover - routing must never break init
175+ logger .warning ("centralized-layout routing skipped: %s" , e )
176+
177+ # Resolve output_dir — the default routes to the workspace corpus under
178+ # centralized; an explicit output_dir is the caller's directory-level
179+ # choice and is never hijacked (same semantics as routing_for_write).
152180 if not output_dir :
153- output_dir_p = repo_path_p / "repowiki"
181+ if _centralized :
182+ from codewiki .mcp .tools .workspace_layout import default_output_dir
183+
184+ output_dir_p = default_output_dir (repo_path_p )
185+ else :
186+ output_dir_p = repo_path_p / "repowiki"
154187 elif os .path .isabs (output_dir ):
155188 output_dir_p = Path (output_dir ).resolve ()
156189 else :
@@ -168,62 +201,108 @@ def handle_init_wiki(arguments: dict) -> str:
168201 }
169202
170203 # ── Steps 1-2: Create directory structure and copy template assets ──
171- tree = initialize_wiki_tree (repo_path_p , output_dir_p , overwrite_schema = True )
204+ # Under centralized the corpus already carries the workspace's customized
205+ # schema.yaml — a business-repo re-run must not clobber it (same rule as
206+ # workspace re-runs, see initialize_wiki_tree).
207+ tree = initialize_wiki_tree (repo_path_p , output_dir_p , overwrite_schema = not _centralized )
172208 results ["created_dirs" ] = tree ["created_dirs" ]
173209 results ["schema_yaml" ] = tree ["schema_yaml" ]
174210 results ["ontology_yaml" ] = tree ["ontology_yaml" ]
175211 results ["review_checklist_yaml" ] = tree ["review_checklist_yaml" ]
176212
213+ # ── Step 1.5: ensure the repo's modules partition (centralized, §6) ─
214+ # modules → wiki/modules/<repo>/. add_workspace_repo scaffolds this at
215+ # registration; ensuring it here keeps init_wiki idempotent for members
216+ # registered through other paths.
217+ if _centralized :
218+ try :
219+ from codewiki .mcp .tools .workspace_layout import routing_for_write
220+
221+ partition = routing_for_write (output_dir_p , repo_path_p )
222+ if partition :
223+ partition_dir = output_dir_p / "wiki" / "modules" / partition
224+ if not partition_dir .exists ():
225+ partition_dir .mkdir (parents = True , exist_ok = True )
226+ results ["created_dirs" ].append (str (partition_dir ))
227+ except Exception as e : # best-effort: analyze_repo re-creates it anyway
228+ logger .warning ("modules partition ensure skipped: %s" , e )
229+
177230 # ── Step 2.5: Team-layout gitignore hygiene (Phase 1, D1) ──────────
178231 # Rebuildable derived files (wiki/index.md, .meta/*.json indexes,
179232 # tasks/.index.json, ...) must stay untracked; init appends the ignore
180233 # block idempotently so a fresh project starts on the team layout.
181234 # Best-effort: never blocks init when the repo is not a git repo or
182- # .gitignore is not writable.
183- try :
184- from codewiki .mcp .tools .team_layout import (
185- ensure_gitignore_entries ,
186- find_repo_root ,
187- )
188-
189- repo_root = find_repo_root (repo_path_p )
190- if repo_root is not None :
191- changed , added = ensure_gitignore_entries (repo_root , output_dir_p )
192- results ["gitignore" ] = {
193- "repo_root" : str (repo_root ),
194- "entries_added" : added ,
195- "status" : "updated" if changed else "already-present" ,
196- }
197- else :
198- results ["gitignore" ] = "skipped: not a git repository"
199- except Exception as e :
200- results ["gitignore" ] = f"WARNING: failed to update .gitignore: { e } "
201- logger .warning ("Failed to update .gitignore for team layout: %s" , e )
235+ # .gitignore is not writable. Skipped under centralized routing: the
236+ # corpus belongs to the workspace repo, whose .gitignore is managed by
237+ # init_workspace — the business repo must not gain ignore entries for
238+ # paths outside itself.
239+ if _centralized :
240+ results ["gitignore" ] = "skipped: centralized routing (workspace corpus)"
241+ else :
242+ try :
243+ from codewiki .mcp .tools .team_layout import (
244+ ensure_gitignore_entries ,
245+ find_repo_root ,
246+ )
247+
248+ repo_root = find_repo_root (repo_path_p )
249+ if repo_root is not None :
250+ changed , added = ensure_gitignore_entries (repo_root , output_dir_p )
251+ results ["gitignore" ] = {
252+ "repo_root" : str (repo_root ),
253+ "entries_added" : added ,
254+ "status" : "updated" if changed else "already-present" ,
255+ }
256+ else :
257+ results ["gitignore" ] = "skipped: not a git repository"
258+ except Exception as e :
259+ results ["gitignore" ] = f"WARNING: failed to update .gitignore: { e } "
260+ logger .warning ("Failed to update .gitignore for team layout: %s" , e )
202261
203262 # ── Step 3: Write AGENTS.md ─────────────────────────────────────────
204- try :
205- from codewiki . mcp . tools . agents_md import write_agents_md
206-
207- write_agents_md (
208- repo_path = str ( repo_path_p ),
209- output_dir = str ( output_dir_p ),
210- module_tree = None ,
263+ # Under centralized, business-repo AGENTS.md stays clean: the CodeWiki
264+ # block was removed by add_workspace_repo (§7.2) and re-injecting it
265+ # would point at a repowiki/ that must not exist. The workspace root's
266+ # AGENTS.md (written by init_workspace) carries the routing guidance.
267+ if _centralized :
268+ results [ "agents_md" ] = (
269+ "skipped: centralized business repo (workspace AGENTS.md governs routing)"
211270 )
212- agents_path = repo_path_p / "AGENTS.md"
213- results ["agents_md" ] = str (agents_path )
214- logger .info ("Wrote AGENTS.md at %s" , agents_path )
215- except Exception as e :
216- results ["agents_md" ] = f"WARNING: failed to write AGENTS.md: { e } "
217- logger .warning ("Failed to write AGENTS.md: %s" , e )
271+ else :
272+ try :
273+ from codewiki .mcp .tools .agents_md import write_agents_md
274+
275+ write_agents_md (
276+ repo_path = str (repo_path_p ),
277+ output_dir = str (output_dir_p ),
278+ module_tree = None ,
279+ )
280+ agents_path = repo_path_p / "AGENTS.md"
281+ results ["agents_md" ] = str (agents_path )
282+ logger .info ("Wrote AGENTS.md at %s" , agents_path )
283+ except Exception as e :
284+ results ["agents_md" ] = f"WARNING: failed to write AGENTS.md: { e } "
285+ logger .warning ("Failed to write AGENTS.md: %s" , e )
218286
219287 results ["status" ] = "ok"
220- results ["next_steps" ] = (
221- "Wiki workspace initialized. Next: "
222- "1) Edit schema.yaml to set 'purpose' and adjust conventions; "
223- "2) Edit ontology.yaml to define project terms and aliases for search; "
224- "3) Optionally edit review_checklist.yaml to add team review rules "
225- "(merged into review_changes, same id overrides builtin); "
226- "4) Run analyze_repo to parse code and generate docs; "
227- "5) Or use ingest_note/query_wiki for knowledge management."
228- )
288+ if _centralized :
289+ results ["layout" ] = "centralized"
290+ results ["next_steps" ] = (
291+ "Business repo initialized into the centralized workspace corpus "
292+ f"({ output_dir_p } ). The workspace's schema.yaml/ontology.yaml were "
293+ "left untouched; no AGENTS.md block was injected into the business "
294+ "repo. Next: run analyze_repo on this repo (modules route to "
295+ "wiki/modules/<repo>/), or use ingest_note/query_wiki with "
296+ "output_dir=<workspace repowiki> and repo=<name> filtering."
297+ )
298+ else :
299+ results ["next_steps" ] = (
300+ "Wiki workspace initialized. Next: "
301+ "1) Edit schema.yaml to set 'purpose' and adjust conventions; "
302+ "2) Edit ontology.yaml to define project terms and aliases for search; "
303+ "3) Optionally edit review_checklist.yaml to add team review rules "
304+ "(merged into review_changes, same id overrides builtin); "
305+ "4) Run analyze_repo to parse code and generate docs; "
306+ "5) Or use ingest_note/query_wiki for knowledge management."
307+ )
229308 return json .dumps (results , ensure_ascii = False , indent = 2 )
0 commit comments