I managed to put my main application code back to the state I think it was in when first encountering this problem. At least, the compiled .hex files only differ now by one byte - day of build date in the .progmem section. Then I captured a programming operation using AS7's 'Start Without Debugging' command (i.e. build and program).
Didn't manage to reproduce the issue.
There's obviously something circumstance-specific that caused the original issue. Bootloader state, dev environment state, some other external influence, I don't know.
But, I did notice that the 'Start Without Debugging' programming procedure is markedly different from the standalone 'Device Programming' dialog.
For starters, it does
not do a full verification step. So corruption of data can easily go unnoticed. Secondly, it does some odd things, the purpose of which I cannot imagine. It does the initial expected stuff: sign-on, reading device signature and fuses, chip erase, etc. But the flash writing process looks like this:
LOAD ADDR: 0x6800 (26,624) - a page boundary, two pages before end of program code
READ FLASH: 256 bytes - last two pages of program code
LOAD ADDR: 0x0000 (0) - start of flash
PROG FLASH: 128 bytes - one page
[... repeat last operation for entire program code ...]
LOAD ADDR: 0x6800 (26,624) - again, two pages before end
READ FLASH: 256 bytes - two pages
LOAD ADDR: 0x6800 (26,624) - and again?
READ FLASH: 256 bytes - two pages
LOAD ADDR: 0x6880 (26,752) - last page of app code
PROG FLASH: 128 bytes - one page
I can't fathom what it could be doing here. Some kind of abbreviated verification? But then why re-write the final page? Or in some other aspect it's trying to be 'clever' and short-cut something?
One thing is for sure, the latter part of this procedure
will fall foul of the discrepancy of erase address versus write address. The final write of that single, last page will actually erase the page
after the last page, and do the write without having erased the intended page first. Maybe this was a contributing factor in the original corruption?