Repository navigation
agent: #13 [Agent] Kapitel teilen, zusammenführen und archivieren #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leonatwork
merged 1 commit into
main
from
agent/bearbeite-github-issue-13-vollst-ndig-un-1020-1327
Oct 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { demoDateien, demoFs } from '../../fixtures/demoProjekt'; | ||
| import { ladeProjekt, MemoryFs } from './index'; | ||
| import { archiviereKapitel, stelleKapitelWiederHer } from './archiv'; | ||
| import { uebernehmeExterneAenderungen } from './extern'; | ||
|
|
||
| describe('Kapitelarchiv (Issue #13)', () => { | ||
| it('archiviert Flut mit echten Demo-Ankern und kapitelübergreifenden Hinweisen; externe Archivänderungen bleiben auflösbar', async () => { | ||
| const fs = demoFs(); | ||
| const original = await ladeProjekt(fs); | ||
| const hinweise = original.kiHinweise.filter(h => h.zitate.some(z => z.kapitel === 'flut')); | ||
| expect(hinweise.length).toBeGreaterThan(0); | ||
| expect(original.anmerkungen.some(a => a.anker.kapitel === 'flut')).toBe(true); | ||
| const d = await archiviereKapitel(fs, 'flut'); | ||
| expect(d.kiHinweise.filter(h => hinweise.some(alt => alt.schluessel === h.schluessel))).toEqual(hinweise.map(h => ({ ...h, veraltet: true }))); | ||
| expect(d.anmerkungen).toEqual(original.anmerkungen); | ||
| await fs.schreiben('archiv/plan/kapitel/flut.md', (await fs.lesen('archiv/plan/kapitel/flut.md'))! + '\n'); | ||
| const geladen = await ladeProjekt(fs); | ||
| expect(uebernehmeExterneAenderungen(d, geladen, ['archiv/plan/kapitel/flut.md']).archiv).toBe(geladen.archiv); | ||
| const neu = await stelleKapitelWiederHer(fs, 'flut'); | ||
| expect(neu.anmerkungen).toEqual(original.anmerkungen); | ||
| expect(neu.kapitel.flut).toEqual(original.kapitel.flut); | ||
| }); | ||
|
|
||
| it('Sicherungsfehler beim Wiederherstellen lässt das vollständige Archiv unverändert', async () => { | ||
| const fs = demoFs(); | ||
| await archiviereKapitel(fs, 'flut'); | ||
| class FehlerFs extends MemoryFs { | ||
| override async schreiben(p: string, t: string) { | ||
| if (p.startsWith('.versionen/')) throw new Error('disk full'); | ||
| return super.schreiben(p, t); | ||
| } | ||
| } | ||
| const fehlerFs = new FehlerFs(fs.alleDateien()); | ||
| await expect(stelleKapitelWiederHer(fehlerFs, 'flut')).rejects.toThrow('Schnappschuss'); | ||
| expect(fehlerFs.alleDateien()).toEqual(fs.alleDateien()); | ||
| }); | ||
|
|
||
| it('überschreibt beim Wiederherstellen keinen aktiven Stand derselben ID', async () => { | ||
| const fs = demoFs(); | ||
| await archiviereKapitel(fs, 'flut'); | ||
| await fs.schreiben('manuskript/flut.md', demoDateien()['manuskript/flut.md']!); | ||
| const vorher = fs.alleDateien(); | ||
| await expect(stelleKapitelWiederHer(fs, 'flut')).rejects.toThrow(); | ||
| expect(fs.alleDateien()).toEqual(vorher); | ||
| }); | ||
| it('erhält rohe Dateien, Notizen und IDs; neue Sicherung vor jeder Aktion, auch in derselben Sekunde', async () => { | ||
| const fs = demoFs(); | ||
| const vorher = fs.alleDateien(); | ||
| const original = await ladeProjekt(fs); | ||
| const zeit = new Date('2026-10-20T12:00:00Z'); | ||
| const d = await archiviereKapitel(fs, 'ebbe', zeit); | ||
| expect(d.projekt.kapitel).not.toContain('ebbe'); | ||
| expect(d.kapitel.ebbe).toBeUndefined(); | ||
| expect(d.archiv?.ebbe.kapitel).toEqual(original.kapitel.ebbe); | ||
| expect(await fs.lesen('archiv/manuskript/ebbe.md')).toBe(vorher['manuskript/ebbe.md']); | ||
| expect(await fs.lesen('archiv/plan/kapitel/ebbe.md')).toBe(vorher['plan/kapitel/ebbe.md']); | ||
| expect(await fs.lesen('manuskript/ebbe.md')).toBeNull(); | ||
| expect(await fs.lesen('plan/kapitel/ebbe.md')).toBeNull(); | ||
| expect(d.anmerkungen).toEqual(original.anmerkungen); | ||
| expect(d.analysen.ebbe).toMatchObject({ ...original.analysen.ebbe, veraltet: true }); | ||
| const versionen = await fs.auflisten('.versionen'); | ||
| const neu = await stelleKapitelWiederHer(fs, 'ebbe', zeit); | ||
| expect(neu.projekt.kapitel).toEqual(original.projekt.kapitel); | ||
| expect(neu.kapitel.ebbe).toEqual(original.kapitel.ebbe); | ||
| expect(neu.plaene.ebbe).toEqual(original.plaene.ebbe); | ||
| expect(await fs.lesen('manuskript/ebbe.md')).toBe(vorher['manuskript/ebbe.md']); | ||
| expect(await fs.lesen('plan/kapitel/ebbe.md')).toBe(vorher['plan/kapitel/ebbe.md']); | ||
| expect((await fs.auflisten('.versionen')).length).toBe(versionen.length + 2); | ||
| expect(neu.analysen.ebbe?.veraltet).toBe(true); | ||
| }); | ||
|
|
||
| it('stellt mehrere archivierte Nachbarn unabhängig von der Wiederherstellungsreihenfolge richtig zurück', async () => { | ||
| const fs = demoFs(); | ||
| const vorher = (await ladeProjekt(fs)).projekt.kapitel; | ||
| await archiviereKapitel(fs, vorher[0]!); | ||
| await archiviereKapitel(fs, vorher[1]!); | ||
| await stelleKapitelWiederHer(fs, vorher[0]!); | ||
| expect((await stelleKapitelWiederHer(fs, vorher[1]!)).projekt.kapitel).toEqual(vorher); | ||
| await expect(stelleKapitelWiederHer(fs, vorher[1]!)).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it.each(['de', 'en'] as const)('Sicherungsfehler (%s) hinterlässt auch bei der zweiten Sicherung keinerlei Änderung', async sprache => { | ||
| class FehlerFs extends MemoryFs { | ||
| n = 0; | ||
| override async schreiben(p: string, t: string) { | ||
| if (p.startsWith('.versionen/') && ++this.n === 2) throw new Error('disk full'); | ||
| return super.schreiben(p, t); | ||
| } | ||
| } | ||
| const fs = new FehlerFs(demoDateien()); | ||
| const vorher = fs.alleDateien(); | ||
| await expect(archiviereKapitel(fs, 'ebbe', undefined, sprache)).rejects.toThrow(sprache === 'de' ? 'Schnappschuss' : 'snapshot'); | ||
| expect(fs.alleDateien()).toEqual(vorher); | ||
| }); | ||
|
|
||
| it('rollt einen Fehler beim Verschieben zurück und überschreibt keine vorhandene Archivdatei', async () => { | ||
| class FehlerFs extends MemoryFs { | ||
| override async entfernen(p: string) { | ||
| if (p === 'plan/kapitel/ebbe.md') throw new Error('disk full'); | ||
| return super.entfernen(p); | ||
| } | ||
| } | ||
| const fs = new FehlerFs(demoDateien()); | ||
| const vorher = fs.alleDateien(); | ||
| await expect(archiviereKapitel(fs, 'ebbe')).rejects.toThrow(); | ||
| expect(fs.alleDateien()).toEqual(vorher); | ||
| const normal = demoFs(); | ||
| await normal.schreiben('archiv/manuskript/ebbe.md', 'fremde Datei'); | ||
| const stand = normal.alleDateien(); | ||
| await expect(archiviereKapitel(normal, 'ebbe')).rejects.toThrow(); | ||
| expect(normal.alleDateien()).toEqual(stand); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** Kapitelumbau ohne DOM/Tauri. Rohe Dateien bewegen, niemals Manuskript neu serialisieren. */ | ||
| import type { ProjektDateisystem } from './fs'; | ||
| import type { Id, ProjektDaten } from '../model/types'; | ||
| import { analysePfad, kapitelPfad, ladeProjekt, parseArchivIndex, parseFrontmatter, parseProjekt, PFADE, planPfad } from './lesen'; | ||
| import { serializeProjekt } from './schreiben'; | ||
| import { versionsPfadMitZusatz } from '../versionen'; | ||
|
|
||
| export const ARCHIV_FEHLER = { | ||
| de: { sicherung: 'Schnappschuss fehlgeschlagen. Die Kapitelaktion wurde abgebrochen.', ungueltig: 'Kapitelaktion nicht möglich: Dateien fehlen, sind widersprüchlich oder das Ziel ist bereits belegt.' }, | ||
| en: { sicherung: 'The snapshot failed. The chapter action was cancelled.', ungueltig: 'Chapter action unavailable: files are missing, inconsistent or the destination already exists.' }, | ||
| }; | ||
|
|
||
| /** Neue aktive Reihenfolge respektieren; archivierte Nachbarn bleiben an ihren alten Ankern. */ | ||
| function volleReihenfolge(aktiv: Id[], alt: Id[], archiviert: Id[]): Id[] { | ||
| const neu = [...aktiv]; | ||
| for (const id of alt.filter(id => archiviert.includes(id))) { | ||
| const danach = alt.slice(alt.indexOf(id) + 1).find(k => neu.includes(k)); | ||
| neu.splice(danach ? neu.indexOf(danach) : neu.length, 0, id); | ||
| } | ||
| return neu; | ||
| } | ||
|
|
||
| async function umbauen(fs: ProjektDateisystem, id: Id, wiederherstellen: boolean, zeit: Date, sprache: 'de' | 'en'): Promise<ProjektDaten> { | ||
| const t = ARCHIV_FEHLER[sprache]; | ||
| const ungueltig = () => new Error(t.ungueltig); | ||
| if (!id || /[/\\]|^\.{1,2}$/.test(id)) throw ungueltig(); | ||
| const projektText = await fs.lesen(PFADE.projekt); | ||
| if (projektText === null || parseFrontmatter(projektText).fehler) throw ungueltig(); | ||
| const projekt = parseProjekt(projektText); | ||
| const geladen = await ladeProjekt(fs); | ||
| if (geladen.manuskriptKonflikte?.some(k => k.kapitelIds.includes(id))) throw ungueltig(); | ||
| const indexText = await fs.lesen(PFADE.archiv); | ||
| let index; | ||
| try { index = indexText === null ? { version: 1 as const, kapitel: [], reihenfolge: [] } : parseArchivIndex(indexText); } | ||
| catch { throw ungueltig(); } | ||
| if (wiederherstellen ? (!index.kapitel.includes(id) || projekt.kapitel.includes(id)) : (!projekt.kapitel.includes(id) || index.kapitel.includes(id))) throw ungueltig(); | ||
| const reihenfolge = volleReihenfolge(projekt.kapitel, index.reihenfolge, index.kapitel); | ||
| const dateien: { quelle: string; ziel: string; inhalt: string }[] = []; | ||
| for (const pfad of [kapitelPfad(id), planPfad(id)]) { | ||
| const quelle = wiederherstellen ? `archiv/${pfad}` : pfad; | ||
| const ziel = wiederherstellen ? pfad : `archiv/${pfad}`; | ||
| if (await fs.existiert(ziel)) throw ungueltig(); | ||
| const inhalt = await fs.lesen(quelle); | ||
| // Ohne Manuskriptdatei gibt es keinen Textstand zu archivieren; ein Plan darf fehlen. | ||
| if (inhalt === null) { | ||
| if (pfad === kapitelPfad(id)) throw ungueltig(); | ||
| continue; | ||
| } | ||
| const fm = parseFrontmatter(inhalt); | ||
| if (fm.fehler || (fm.daten.id !== undefined && fm.daten.id !== id)) throw ungueltig(); | ||
| dateien.push({ quelle, ziel, inhalt }); | ||
| } | ||
|
|
||
| // Änderungsjournal: auch bereits geschriebene Sicherungen bei einem Fehler entfernen. | ||
| const journal = new Map<string, string | null>(); | ||
| async function aendere(pfad: string, inhalt: string | null) { | ||
| if (!journal.has(pfad)) journal.set(pfad, await fs.lesen(pfad)); | ||
| if (inhalt === null) await fs.entfernen(pfad); | ||
| else await fs.schreiben(pfad, inhalt); | ||
| } | ||
| let sichert = true; | ||
| try { | ||
| for (const d of dateien) { | ||
| // Archiv-Sicherungen liegen im ursprünglichen Kapitel-Versionsverlauf. | ||
| const pfad = d.quelle.replace(/^archiv\//, ''); | ||
| let n = 1; | ||
| while (await fs.existiert(versionsPfadMitZusatz(pfad, zeit, n))) n++; | ||
| await aendere(versionsPfadMitZusatz(pfad, zeit, n), d.inhalt); | ||
| } | ||
| sichert = false; | ||
| for (const d of dateien) await aendere(d.ziel, d.inhalt); | ||
| if (!wiederherstellen) { | ||
| const analyse = await fs.lesen(analysePfad(id)); | ||
| if (analyse !== null) await aendere(analysePfad(id), JSON.stringify({ ...JSON.parse(analyse), veraltet: true }, null, 2) + '\n'); | ||
| const hinweise = await fs.lesen(PFADE.hinweise); | ||
| if (hinweise !== null) { | ||
| const roh: unknown = JSON.parse(hinweise); | ||
| if (!Array.isArray(roh)) throw ungueltig(); | ||
| const neu = roh.map(h => h.zitate?.some((z: { kapitel: Id }) => z.kapitel === id) || h.plan?.kapitel === id ? { ...h, veraltet: true } : h); | ||
| await aendere(PFADE.hinweise, JSON.stringify(neu, null, 2) + '\n'); | ||
| } | ||
| } | ||
| for (const d of dateien) await aendere(d.quelle, null); | ||
| const archiviert = wiederherstellen ? index.kapitel.filter(k => k !== id) : [...index.kapitel, id]; | ||
| await aendere(PFADE.archiv, JSON.stringify({ version: 1, kapitel: archiviert, reihenfolge }, null, 2) + '\n'); | ||
| const kapitel = wiederherstellen ? reihenfolge.filter(k => !archiviert.includes(k)) : projekt.kapitel.filter(k => k !== id); | ||
| await aendere(PFADE.projekt, serializeProjekt({ ...projekt, kapitel }, projektText)); | ||
| return await ladeProjekt(fs); | ||
| } catch (fehler) { | ||
| const rollbackFehler: unknown[] = []; | ||
| for (const [pfad, alt] of [...journal].reverse()) { | ||
| try { | ||
| if (await fs.lesen(pfad) === alt) continue; | ||
| if (alt === null) await fs.entfernen(pfad); | ||
| else await fs.schreiben(pfad, alt); | ||
| } catch (e) { rollbackFehler.push(e); } | ||
| } | ||
| if (rollbackFehler.length) throw new AggregateError([fehler, ...rollbackFehler], sprache === 'de' ? 'Rücksetzen fehlgeschlagen. Bitte Projektdateien und Schnappschüsse prüfen.' : 'Rollback failed. Please check project files and snapshots.'); | ||
| if (sichert) throw new Error(t.sicherung, { cause: fehler }); | ||
| throw fehler; | ||
| } | ||
| } | ||
|
|
||
| export const archiviereKapitel = (fs: ProjektDateisystem, id: Id, zeit = new Date(), sprache: 'de' | 'en' = 'de') => umbauen(fs, id, false, zeit, sprache); | ||
| export const stelleKapitelWiederHer = (fs: ProjektDateisystem, id: Id, zeit = new Date(), sprache: 'de' | 'en' = 'de') => umbauen(fs, id, true, zeit, sprache); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Malformed hints abort archive
Medium Severity
Archive parses analysis and hints as raw JSON and then reads
h.zitatewithout an object check. Anullarray entry or a non-arrayzitatevalue throwsTypeError; invalid analysis JSON throwsSyntaxError. The journal rolls back, but the action stays blocked and the message is not the translatedungueltigtext.Reviewed by Cursor Bugbot for commit c6ff194. Configure here.